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 constexpr unsigned int kBluetoothDefaultInputStateTimeoutMs = 20; 31 32 constexpr unsigned int kBluetoothDefaultOutputBufferMs = 10; 33 constexpr audio_channel_mask_t kBluetoothDefaultOutputChannelModeMask = 34 AUDIO_CHANNEL_OUT_STEREO; 35 constexpr audio_channel_mask_t kBluetoothDefaultInputChannelModeMask = 36 AUDIO_CHANNEL_IN_MONO; 37 38 enum class BluetoothStreamState : uint8_t { 39 DISABLED = 0, // This stream is closing or set param "suspend=true" 40 STANDBY, 41 STARTING, 42 STARTED, 43 SUSPENDING, 44 UNKNOWN, 45 }; 46 47 std::ostream& operator<<(std::ostream& os, const BluetoothStreamState& state); 48 49 struct BluetoothStreamOut { 50 // Must be the first member so it can be cast from audio_stream 51 // or audio_stream_out pointer 52 audio_stream_out stream_out_{}; 53 ::android::bluetooth::audio::BluetoothAudioPortOut bluetooth_output_; 54 int64_t last_write_time_us_; 55 // Audio PCM Configs 56 uint32_t sample_rate_; 57 audio_channel_mask_t channel_mask_; 58 audio_format_t format_; 59 // frame is the number of samples per channel 60 // frames count per tick 61 size_t frames_count_; 62 // total frames written, reset on standby 63 uint64_t frames_rendered_; 64 // total frames written after opened, never reset 65 uint64_t frames_presented_; 66 mutable std::mutex mutex_; 67 }; 68 69 struct BluetoothAudioDevice { 70 // Important: device must be first as an audio_hw_device* may be cast to 71 // BluetoothAudioDevice* when the type is implicitly known. 72 audio_hw_device audio_device_{}; 73 // protect against device->output and stream_out from being inconsistent 74 std::mutex mutex_; 75 std::list<BluetoothStreamOut*> opened_stream_outs_ = 76 std::list<BluetoothStreamOut*>(0); 77 }; 78 79 struct BluetoothStreamIn { 80 // Must be the first member so it can be cast from audio_stream 81 // or audio_stream_in pointer 82 audio_stream_in stream_in_; 83 ::android::bluetooth::audio::BluetoothAudioPortIn bluetooth_input_; 84 int64_t last_read_time_us_; 85 // Audio PCM Configs 86 uint32_t sample_rate_; 87 audio_channel_mask_t channel_mask_; 88 audio_format_t format_; 89 // frame is the number of samples per channel 90 // frames count per tick 91 size_t frames_count_; 92 // total frames read after opened, never reset 93 uint64_t frames_presented_; 94 mutable std::mutex mutex_; 95 }; 96 97 int adev_open_output_stream(struct audio_hw_device* dev, 98 audio_io_handle_t handle, audio_devices_t devices, 99 audio_output_flags_t flags, 100 struct audio_config* config, 101 struct audio_stream_out** stream_out, 102 const char* address __unused); 103 104 void adev_close_output_stream(struct audio_hw_device* dev, 105 struct audio_stream_out* stream); 106 107 size_t adev_get_input_buffer_size(const struct audio_hw_device* dev, 108 const struct audio_config* config); 109 110 int adev_open_input_stream(struct audio_hw_device* dev, 111 audio_io_handle_t handle, audio_devices_t devices, 112 struct audio_config* config, 113 struct audio_stream_in** stream_in, 114 audio_input_flags_t flags __unused, 115 const char* address __unused, 116 audio_source_t source __unused); 117 118 void adev_close_input_stream(struct audio_hw_device* dev, 119 struct audio_stream_in* in); 120