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