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 "common/message_loop_thread.h" 30 #include "transport_instance.h" 31 32 #define BLUETOOTH_AUDIO_HAL_PROP_DISABLED \ 33 "persist.bluetooth.bluetooth_audio_hal.disabled" 34 35 namespace bluetooth { 36 namespace audio { 37 namespace aidl { 38 39 using ::aidl::android::hardware::bluetooth::audio::AudioCapabilities; 40 using ::aidl::android::hardware::bluetooth::audio::AudioConfiguration; 41 using ::aidl::android::hardware::bluetooth::audio::BluetoothAudioStatus; 42 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioPort; 43 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider; 44 using ::aidl::android::hardware::bluetooth::audio:: 45 IBluetoothAudioProviderFactory; 46 using ::aidl::android::hardware::bluetooth::audio::LatencyMode; 47 using ::aidl::android::hardware::bluetooth::audio::PcmConfiguration; 48 49 using ::aidl::android::hardware::common::fmq::MQDescriptor; 50 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; 51 using ::android::AidlMessageQueue; 52 53 using MqDataType = int8_t; 54 using MqDataMode = SynchronizedReadWrite; 55 using DataMQ = AidlMessageQueue<MqDataType, MqDataMode>; 56 using DataMQDesc = MQDescriptor<MqDataType, MqDataMode>; 57 58 /*** 59 * The client interface connects an IBluetoothTransportInstance to 60 * IBluetoothAudioProvider and helps to route callbacks to 61 * IBluetoothTransportInstance 62 ***/ 63 class BluetoothAudioClientInterface { 64 public: 65 BluetoothAudioClientInterface(IBluetoothTransportInstance* instance); 66 virtual ~BluetoothAudioClientInterface() = default; 67 IsValid()68 bool IsValid() const { return provider_ != nullptr; } 69 70 std::vector<AudioCapabilities> GetAudioCapabilities() const; 71 static std::vector<AudioCapabilities> GetAudioCapabilities( 72 SessionType session_type); 73 void StreamStarted(const BluetoothAudioCtrlAck& ack); 74 75 void StreamSuspended(const BluetoothAudioCtrlAck& ack); 76 77 int StartSession(); 78 79 /*** 80 * Renew the connection and usually is used when aidl restarted 81 ***/ 82 void RenewAudioProviderAndSession(); 83 84 int EndSession(); 85 86 bool UpdateAudioConfig(const AudioConfiguration& audioConfig); 87 88 bool SetLowLatencyModeAllowed(bool allowed); 89 90 void FlushAudioData(); 91 92 static constexpr PcmConfiguration kInvalidPcmConfiguration = {}; 93 94 static bool is_aidl_available(); 95 96 static int GetAidlInterfaceVersion(); 97 98 protected: 99 mutable std::mutex internal_mutex_; 100 /*** 101 * Helper function to connect to an IBluetoothAudioProvider 102 ***/ 103 void FetchAudioProvider(); 104 105 /*** 106 * Invoked when binder died 107 ***/ 108 static void binderDiedCallbackAidl(void* cookie_ptr); 109 110 std::shared_ptr<IBluetoothAudioProvider> provider_; 111 112 std::shared_ptr<IBluetoothAudioProviderFactory> provider_factory_; 113 114 bool session_started_; 115 std::unique_ptr<DataMQ> data_mq_; 116 117 ::ndk::ScopedAIBinder_DeathRecipient death_recipient_; 118 // static constexpr const char* kDefaultAudioProviderFactoryInterface = 119 // "android.hardware.bluetooth.audio.IBluetoothAudioProviderFactory/default"; 120 static inline const std::string kDefaultAudioProviderFactoryInterface = 121 std::string() + IBluetoothAudioProviderFactory::descriptor + "/default"; 122 123 private: 124 IBluetoothTransportInstance* transport_; 125 std::vector<AudioCapabilities> capabilities_; 126 bool is_low_latency_allowed_{false}; 127 }; 128 129 /*** 130 * The client interface connects an IBluetoothTransportInstance to 131 * IBluetoothAudioProvider and helps to route callbacks to 132 * IBluetoothTransportInstance 133 ***/ 134 class BluetoothAudioSinkClientInterface : public BluetoothAudioClientInterface { 135 public: 136 /*** 137 * Constructs an BluetoothAudioSinkClientInterface to communicate to 138 * BluetoothAudio HAL. |sink| is the implementation for the transport, and 139 * |message_loop| is the thread where callbacks are invoked. 140 ***/ 141 BluetoothAudioSinkClientInterface( 142 IBluetoothSinkTransportInstance* sink, 143 bluetooth::common::MessageLoopThread* message_loop); 144 virtual ~BluetoothAudioSinkClientInterface(); 145 GetTransportInstance()146 IBluetoothSinkTransportInstance* GetTransportInstance() const { 147 return sink_; 148 } 149 150 /*** 151 * Read data from audio HAL through fmq 152 ***/ 153 size_t ReadAudioData(uint8_t* p_buf, uint32_t len); 154 155 private: 156 IBluetoothSinkTransportInstance* sink_; 157 158 static constexpr int kDefaultDataReadTimeoutMs = 10; 159 static constexpr int kDefaultDataReadPollIntervalMs = 1; 160 }; 161 162 class BluetoothAudioSourceClientInterface 163 : public BluetoothAudioClientInterface { 164 public: 165 /*** 166 * Constructs an BluetoothAudioSourceClientInterface to communicate to 167 * BluetoothAudio HAL. |source| is the implementation for the transport, and 168 * |message_loop| is the thread where callbacks are invoked. 169 ***/ 170 BluetoothAudioSourceClientInterface( 171 IBluetoothSourceTransportInstance* source, 172 bluetooth::common::MessageLoopThread* message_loop); 173 virtual ~BluetoothAudioSourceClientInterface(); 174 GetTransportInstance()175 IBluetoothSourceTransportInstance* GetTransportInstance() const { 176 return source_; 177 } 178 179 /*** 180 * Write data to audio HAL through fmq 181 ***/ 182 size_t WriteAudioData(const uint8_t* p_buf, uint32_t len); 183 184 private: 185 IBluetoothSourceTransportInstance* source_; 186 187 static constexpr int kDefaultDataWriteTimeoutMs = 10; 188 static constexpr int kDefaultDataWritePollIntervalMs = 1; 189 }; 190 191 } // namespace aidl 192 } // namespace audio 193 } // namespace bluetooth 194