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