1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <hardware/audio.h> 20 #include <system/audio.h> 21 #include <list> 22 23 #include "device_port_proxy.h" 24 25 constexpr unsigned int kBluetoothDefaultSampleRate = 44100; 26 constexpr audio_format_t kBluetoothDefaultAudioFormatBitsPerSample = 27 AUDIO_FORMAT_PCM_16_BIT; 28 29 constexpr unsigned int kBluetoothDefaultInputBufferMs = 20; 30 31 constexpr unsigned int kBluetoothDefaultOutputBufferMs = 10; 32 constexpr audio_channel_mask_t kBluetoothDefaultOutputChannelModeMask = 33 AUDIO_CHANNEL_OUT_STEREO; 34 35 enum class BluetoothStreamState : uint8_t { 36 DISABLED = 0, // This stream is closing or set param "suspend=true" 37 STANDBY, 38 STARTING, 39 STARTED, 40 SUSPENDING, 41 UNKNOWN, 42 }; 43 44 std::ostream& operator<<(std::ostream& os, const BluetoothStreamState& state); 45 46 struct BluetoothStreamOut { 47 // Must be the first member so it can be cast from audio_stream 48 // or audio_stream_out pointer 49 audio_stream_out stream_out_{}; 50 ::android::bluetooth::audio::BluetoothAudioPortOut bluetooth_output_; 51 int64_t last_write_time_us_; 52 // Audio PCM Configs 53 uint32_t sample_rate_; 54 audio_channel_mask_t channel_mask_; 55 audio_format_t format_; 56 // frame is the number of samples per channel 57 // frames count per tick 58 size_t frames_count_; 59 // total frames written, reset on standby 60 uint64_t frames_rendered_; 61 // total frames written after opened, never reset 62 uint64_t frames_presented_; 63 mutable std::mutex mutex_; 64 }; 65 66 struct BluetoothAudioDevice { 67 // Important: device must be first as an audio_hw_device* may be cast to 68 // BluetoothAudioDevice* when the type is implicitly known. 69 audio_hw_device audio_device_{}; 70 // protect against device->output and stream_out from being inconsistent 71 std::mutex mutex_; 72 std::list<BluetoothStreamOut*> opened_stream_outs_ = 73 std::list<BluetoothStreamOut*>(0); 74 }; 75 76 int adev_open_output_stream(struct audio_hw_device* dev, 77 audio_io_handle_t handle, audio_devices_t devices, 78 audio_output_flags_t flags, 79 struct audio_config* config, 80 struct audio_stream_out** stream_out, 81 const char* address __unused); 82 83 void adev_close_output_stream(struct audio_hw_device* dev, 84 struct audio_stream_out* stream); 85 86 size_t adev_get_input_buffer_size(const struct audio_hw_device* dev, 87 const struct audio_config* config); 88 89 int adev_open_input_stream(struct audio_hw_device* dev, 90 audio_io_handle_t handle, audio_devices_t devices, 91 struct audio_config* config, 92 struct audio_stream_in** stream_in, 93 audio_input_flags_t flags __unused, 94 const char* address __unused, 95 audio_source_t source __unused); 96 97 void adev_close_input_stream(struct audio_hw_device* dev, 98 struct audio_stream_in* in); 99