1 /* 2 * Copyright 2022 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 <fmq/AidlMessageQueue.h> 20 #include <hardware/audio.h> 21 22 #include <ctime> 23 #include <mutex> 24 #include <vector> 25 26 #include "audio_aidl_interfaces.h" 27 #include "audio_ctrl_ack.h" 28 #include "bluetooth_audio_port_impl.h" 29 #include "bta/le_audio/broadcaster/broadcaster_types.h" 30 #include "bta/le_audio/le_audio_types.h" 31 #include "transport_instance.h" 32 33 #define BLUETOOTH_AUDIO_HAL_PROP_DISABLED \ 34 "persist.bluetooth.bluetooth_audio_hal.disabled" 35 36 namespace bluetooth { 37 namespace audio { 38 namespace aidl { 39 40 using ::aidl::android::hardware::bluetooth::audio::A2dpConfiguration; 41 using ::aidl::android::hardware::bluetooth::audio::A2dpConfigurationHint; 42 using ::aidl::android::hardware::bluetooth::audio::A2dpRemoteCapabilities; 43 using ::aidl::android::hardware::bluetooth::audio::A2dpStatus; 44 using ::aidl::android::hardware::bluetooth::audio::AudioCapabilities; 45 using ::aidl::android::hardware::bluetooth::audio::AudioConfiguration; 46 using ::aidl::android::hardware::bluetooth::audio::BluetoothAudioStatus; 47 using ::aidl::android::hardware::bluetooth::audio::CodecId; 48 using ::aidl::android::hardware::bluetooth::audio::CodecInfo; 49 using ::aidl::android::hardware::bluetooth::audio::CodecParameters; 50 using ::aidl::android::hardware::bluetooth::audio::CodecSpecificCapabilitiesLtv; 51 using ::aidl::android::hardware::bluetooth::audio:: 52 CodecSpecificConfigurationLtv; 53 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioPort; 54 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider; 55 using ::aidl::android::hardware::bluetooth::audio:: 56 IBluetoothAudioProviderFactory; 57 using ::aidl::android::hardware::bluetooth::audio::LatencyMode; 58 using ::aidl::android::hardware::bluetooth::audio::MetadataLtv; 59 using ::aidl::android::hardware::bluetooth::audio::PcmConfiguration; 60 61 using ::aidl::android::hardware::common::fmq::MQDescriptor; 62 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; 63 using ::android::AidlMessageQueue; 64 65 using MqDataType = int8_t; 66 using MqDataMode = SynchronizedReadWrite; 67 using DataMQ = AidlMessageQueue<MqDataType, MqDataMode>; 68 using DataMQDesc = MQDescriptor<MqDataType, MqDataMode>; 69 70 /*** 71 * The client interface connects an IBluetoothTransportInstance to 72 * IBluetoothAudioProvider and helps to route callbacks to 73 * IBluetoothTransportInstance 74 ***/ 75 class BluetoothAudioClientInterface { 76 public: 77 BluetoothAudioClientInterface(IBluetoothTransportInstance* instance); 78 virtual ~BluetoothAudioClientInterface() = default; 79 80 bool IsValid() const; 81 82 std::vector<AudioCapabilities> GetAudioCapabilities() const; 83 84 static std::vector<AudioCapabilities> GetAudioCapabilities( 85 SessionType session_type); 86 static std::optional<IBluetoothAudioProviderFactory::ProviderInfo> 87 GetProviderInfo(SessionType session_type, 88 std::shared_ptr<IBluetoothAudioProviderFactory> 89 provider_factory = nullptr); 90 91 std::optional<A2dpStatus> ParseA2dpConfiguration( 92 const CodecId& codec_id, const std::vector<uint8_t>& configuration, 93 CodecParameters* codec_parameters) const; 94 95 std::optional<A2dpConfiguration> GetA2dpConfiguration( 96 std::vector<A2dpRemoteCapabilities> const& remote_capabilities, 97 A2dpConfigurationHint const& hint) const; 98 99 void StreamStarted(const BluetoothAudioCtrlAck& ack); 100 101 void StreamSuspended(const BluetoothAudioCtrlAck& ack); 102 103 int StartSession(); 104 105 /*** 106 * Renew the connection and usually is used when aidl restarted 107 ***/ 108 void RenewAudioProviderAndSession(); 109 110 int EndSession(); 111 112 bool UpdateAudioConfig(const AudioConfiguration& audioConfig); 113 114 bool SetAllowedLatencyModes(std::vector<LatencyMode> latency_modes); 115 116 void FlushAudioData(); 117 118 void SetCodecPriority(CodecId codec_id, int32_t priority); 119 120 std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting> 121 GetLeAudioAseConfiguration( 122 std::optional<std::vector< 123 std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>& 124 remoteSinkAudioCapabilities, 125 std::optional<std::vector< 126 std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>& 127 remoteSourceAudioCapabilities, 128 std::vector<IBluetoothAudioProvider::LeAudioConfigurationRequirement>& 129 requirements); 130 131 IBluetoothAudioProvider::LeAudioAseQosConfigurationPair 132 getLeAudioAseQosConfiguration( 133 IBluetoothAudioProvider::LeAudioAseQosConfigurationRequirement& 134 qosRequirement); 135 136 void onSinkAseMetadataChanged( 137 IBluetoothAudioProvider::AseState state, int32_t cigId, int32_t cisId, 138 std::optional<std::vector<std::optional<MetadataLtv>>>& metadata); 139 140 void onSourceAseMetadataChanged( 141 IBluetoothAudioProvider::AseState state, int32_t cigId, int32_t cisId, 142 std::optional<std::vector<std::optional<MetadataLtv>>>& metadata); 143 144 IBluetoothAudioProvider::LeAudioBroadcastConfigurationSetting 145 getLeAudioBroadcastConfiguration( 146 const std::optional<std::vector< 147 std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>& 148 remoteSinkAudioCapabilities, 149 const IBluetoothAudioProvider::LeAudioBroadcastConfigurationRequirement& 150 requirement); 151 152 static constexpr PcmConfiguration kInvalidPcmConfiguration = {}; 153 154 static bool is_aidl_available(); 155 156 protected: 157 mutable std::mutex internal_mutex_; 158 /*** 159 * Helper function to connect to an IBluetoothAudioProvider 160 ***/ 161 void FetchAudioProvider(); 162 163 /*** 164 * Invoked when binder died 165 ***/ 166 static void binderDiedCallbackAidl(void* cookie_ptr); 167 168 std::shared_ptr<IBluetoothAudioProvider> provider_; 169 170 std::shared_ptr<IBluetoothAudioProviderFactory> provider_factory_; 171 172 bool session_started_; 173 std::unique_ptr<DataMQ> data_mq_; 174 175 ::ndk::ScopedAIBinder_DeathRecipient death_recipient_; 176 // static constexpr const char* kDefaultAudioProviderFactoryInterface = 177 // "android.hardware.bluetooth.audio.IBluetoothAudioProviderFactory/default"; 178 static inline const std::string kDefaultAudioProviderFactoryInterface = 179 std::string() + IBluetoothAudioProviderFactory::descriptor + "/default"; 180 181 private: 182 IBluetoothTransportInstance* transport_; 183 std::vector<AudioCapabilities> capabilities_; 184 std::vector<LatencyMode> latency_modes_; 185 }; 186 187 /*** 188 * The client interface connects an IBluetoothTransportInstance to 189 * IBluetoothAudioProvider and helps to route callbacks to 190 * IBluetoothTransportInstance 191 ***/ 192 class BluetoothAudioSinkClientInterface : public BluetoothAudioClientInterface { 193 public: 194 /*** 195 * Constructs an BluetoothAudioSinkClientInterface to communicate to 196 * BluetoothAudio HAL. |sink| is the implementation for the transport. 197 ***/ 198 BluetoothAudioSinkClientInterface(IBluetoothSinkTransportInstance* sink); 199 virtual ~BluetoothAudioSinkClientInterface(); 200 GetTransportInstance()201 IBluetoothSinkTransportInstance* GetTransportInstance() const { 202 return sink_; 203 } 204 205 /*** 206 * Read data from audio HAL through fmq 207 ***/ 208 size_t ReadAudioData(uint8_t* p_buf, uint32_t len); 209 210 private: 211 IBluetoothSinkTransportInstance* sink_; 212 213 static constexpr int kDefaultDataReadTimeoutMs = 10; 214 static constexpr int kDefaultDataReadPollIntervalMs = 1; 215 }; 216 217 class BluetoothAudioSourceClientInterface 218 : public BluetoothAudioClientInterface { 219 public: 220 /*** 221 * Constructs an BluetoothAudioSourceClientInterface to communicate to 222 * BluetoothAudio HAL. |source| is the implementation for the transport. 223 ***/ 224 BluetoothAudioSourceClientInterface( 225 IBluetoothSourceTransportInstance* source); 226 virtual ~BluetoothAudioSourceClientInterface(); 227 GetTransportInstance()228 IBluetoothSourceTransportInstance* GetTransportInstance() const { 229 return source_; 230 } 231 232 /*** 233 * Write data to audio HAL through fmq 234 ***/ 235 size_t WriteAudioData(const uint8_t* p_buf, uint32_t len); 236 237 private: 238 IBluetoothSourceTransportInstance* source_; 239 240 static constexpr int kDefaultDataWriteTimeoutMs = 10; 241 static constexpr int kDefaultDataWritePollIntervalMs = 1; 242 }; 243 244 } // namespace aidl 245 } // namespace audio 246 } // namespace bluetooth 247