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 <android/hardware/bluetooth/audio/2.1/IBluetoothAudioProvider.h>
20 #include <android/hardware/bluetooth/audio/2.1/types.h>
21 #include <bluetooth/log.h>
22 #include <fmq/MessageQueue.h>
23 #include <hardware/audio.h>
24 #include <time.h>
25 
26 #include <mutex>
27 #include <vector>
28 
29 #include "common/message_loop_thread.h"
30 
31 #define BLUETOOTH_AUDIO_HAL_PROP_DISABLED \
32   "persist.bluetooth.bluetooth_audio_hal.disabled"
33 
34 namespace bluetooth {
35 namespace audio {
36 namespace hidl {
37 
38 using AudioCapabilities =
39     ::android::hardware::bluetooth::audio::V2_0::AudioCapabilities;
40 using AudioCapabilities_2_1 =
41     ::android::hardware::bluetooth::audio::V2_1::AudioCapabilities;
42 using AudioConfiguration =
43     ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
44 using AudioConfiguration_2_1 =
45     ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration;
46 using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
47 using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
48 using IBluetoothAudioProvider =
49     ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioProvider;
50 using IBluetoothAudioProvider_2_1 =
51     ::android::hardware::bluetooth::audio::V2_1::IBluetoothAudioProvider;
52 using PcmParameters =
53     ::android::hardware::bluetooth::audio::V2_0::PcmParameters;
54 using PcmParameters_2_1 =
55     ::android::hardware::bluetooth::audio::V2_1::PcmParameters;
56 using SampleRate = ::android::hardware::bluetooth::audio::V2_0::SampleRate;
57 using SampleRate_2_1 = ::android::hardware::bluetooth::audio::V2_1::SampleRate;
58 using SessionType = ::android::hardware::bluetooth::audio::V2_0::SessionType;
59 using SessionType_2_1 =
60     ::android::hardware::bluetooth::audio::V2_1::SessionType;
61 using ::android::hardware::bluetooth::audio::V2_0::TimeSpec;
62 using BluetoothAudioStatus =
63     ::android::hardware::bluetooth::audio::V2_0::Status;
64 
65 enum class BluetoothAudioCtrlAck : uint8_t {
66   SUCCESS_FINISHED = 0,
67   PENDING,
68   FAILURE_UNSUPPORTED,
69   FAILURE_BUSY,
70   FAILURE_DISCONNECTING,
71   FAILURE
72 };
73 
74 std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack);
75 
BluetoothAudioCtrlAckToHalStatus(const BluetoothAudioCtrlAck & ack)76 inline BluetoothAudioStatus BluetoothAudioCtrlAckToHalStatus(
77     const BluetoothAudioCtrlAck& ack) {
78   switch (ack) {
79     case BluetoothAudioCtrlAck::SUCCESS_FINISHED:
80       return BluetoothAudioStatus::SUCCESS;
81     case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED:
82       return BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION;
83     case BluetoothAudioCtrlAck::PENDING:
84       return BluetoothAudioStatus::FAILURE;
85     case BluetoothAudioCtrlAck::FAILURE_BUSY:
86       return BluetoothAudioStatus::FAILURE;
87     case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING:
88       return BluetoothAudioStatus::FAILURE;
89     default:
90       return BluetoothAudioStatus::FAILURE;
91   }
92 }
93 
94 // An IBluetoothTransportInstance needs to be implemented by a Bluetooth
95 // audio transport, such as A2DP or Hearing Aid, to handle callbacks from Audio
96 // HAL.
97 class IBluetoothTransportInstance {
98  public:
IBluetoothTransportInstance(SessionType sessionType,AudioConfiguration audioConfig)99   IBluetoothTransportInstance(SessionType sessionType,
100                               AudioConfiguration audioConfig)
101       : session_type_(sessionType),
102         session_type_2_1_(SessionType_2_1::UNKNOWN),
103         audio_config_(std::move(audioConfig)),
104         audio_config_2_1_({}){};
IBluetoothTransportInstance(SessionType_2_1 sessionType_2_1,AudioConfiguration_2_1 audioConfig_2_1)105   IBluetoothTransportInstance(SessionType_2_1 sessionType_2_1,
106                               AudioConfiguration_2_1 audioConfig_2_1)
107       : session_type_(SessionType::UNKNOWN),
108         session_type_2_1_(sessionType_2_1),
109         audio_config_({}),
110         audio_config_2_1_(std::move(audioConfig_2_1)){};
111   virtual ~IBluetoothTransportInstance() = default;
112 
GetSessionType()113   SessionType GetSessionType() const { return session_type_; }
GetSessionType_2_1()114   SessionType_2_1 GetSessionType_2_1() const { return session_type_2_1_; }
115 
GetAudioConfiguration()116   AudioConfiguration GetAudioConfiguration() const { return audio_config_; }
GetAudioConfiguration_2_1()117   AudioConfiguration_2_1 GetAudioConfiguration_2_1() const {
118     return audio_config_2_1_;
119   }
120 
UpdateAudioConfiguration(const AudioConfiguration & audio_config)121   void UpdateAudioConfiguration(const AudioConfiguration& audio_config) {
122     audio_config_ = audio_config;
123   }
UpdateAudioConfiguration_2_1(const AudioConfiguration_2_1 & audio_config_2_1)124   void UpdateAudioConfiguration_2_1(
125       const AudioConfiguration_2_1& audio_config_2_1) {
126     audio_config_2_1_ = audio_config_2_1;
127   }
128 
129   virtual BluetoothAudioCtrlAck StartRequest() = 0;
130 
131   virtual BluetoothAudioCtrlAck SuspendRequest() = 0;
132 
133   virtual void StopRequest() = 0;
134 
135   virtual bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
136                                        uint64_t* total_bytes_readed,
137                                        timespec* data_position) = 0;
138 
139   virtual void MetadataChanged(const source_metadata_t& source_metadata) = 0;
140 
141   // Invoked when the transport is requested to reset presentation position
142   virtual void ResetPresentationPosition() = 0;
143 
144  private:
145   const SessionType session_type_;
146   const SessionType_2_1 session_type_2_1_;
147   AudioConfiguration audio_config_;
148   AudioConfiguration_2_1 audio_config_2_1_;
149 };
150 
151 // An IBluetoothSinkTransportInstance needs to be implemented by a Bluetooth
152 // audio transport, such as A2DP, Hearing Aid or LeAudio, to handle callbacks
153 // from Audio HAL.
154 class IBluetoothSinkTransportInstance : public IBluetoothTransportInstance {
155  public:
IBluetoothSinkTransportInstance(SessionType sessionType,AudioConfiguration audioConfig)156   IBluetoothSinkTransportInstance(SessionType sessionType,
157                                   AudioConfiguration audioConfig)
158       : IBluetoothTransportInstance{sessionType, audioConfig} {}
IBluetoothSinkTransportInstance(SessionType_2_1 sessionType_2_1,AudioConfiguration_2_1 audioConfig_2_1)159   IBluetoothSinkTransportInstance(SessionType_2_1 sessionType_2_1,
160                                   AudioConfiguration_2_1 audioConfig_2_1)
161       : IBluetoothTransportInstance{sessionType_2_1, audioConfig_2_1} {}
162   virtual ~IBluetoothSinkTransportInstance() = default;
163 
164   // Invoked when the transport is requested to log bytes read
165   virtual void LogBytesRead(size_t bytes_readed) = 0;
166 };
167 
168 class IBluetoothSourceTransportInstance : public IBluetoothTransportInstance {
169  public:
IBluetoothSourceTransportInstance(SessionType sessionType,AudioConfiguration audioConfig)170   IBluetoothSourceTransportInstance(SessionType sessionType,
171                                     AudioConfiguration audioConfig)
172       : IBluetoothTransportInstance{sessionType, audioConfig} {}
IBluetoothSourceTransportInstance(SessionType_2_1 sessionType_2_1,AudioConfiguration_2_1 audioConfig_2_1)173   IBluetoothSourceTransportInstance(SessionType_2_1 sessionType_2_1,
174                                     AudioConfiguration_2_1 audioConfig_2_1)
175       : IBluetoothTransportInstance{sessionType_2_1, audioConfig_2_1} {}
176   virtual ~IBluetoothSourceTransportInstance() = default;
177 
178   // Invoked when the transport is requested to log bytes written
179   virtual void LogBytesWritten(size_t bytes_written) = 0;
180 };
181 
182 // common object is shared between different kind of SessionType
183 class BluetoothAudioDeathRecipient;
184 
185 // The client interface connects an IBluetoothTransportInstance to
186 // IBluetoothAudioProvider and helps to route callbacks to
187 // IBluetoothTransportInstance
188 class BluetoothAudioClientInterface {
189  public:
190   BluetoothAudioClientInterface(
191       android::sp<BluetoothAudioDeathRecipient> death_recipient,
192       IBluetoothTransportInstance* instance);
193   virtual ~BluetoothAudioClientInterface() = default;
194 
195   bool IsValid() const;
196 
197   std::vector<AudioCapabilities> GetAudioCapabilities() const;
198   std::vector<AudioCapabilities_2_1> GetAudioCapabilities_2_1() const;
199   static std::vector<AudioCapabilities> GetAudioCapabilities(
200       SessionType session_type);
201   static std::vector<AudioCapabilities_2_1> GetAudioCapabilities_2_1(
202       SessionType_2_1 session_type_2_1);
203 
204   void StreamStarted(const BluetoothAudioCtrlAck& ack);
205 
206   void StreamSuspended(const BluetoothAudioCtrlAck& ack);
207 
208   int StartSession();
209   int StartSession_2_1();
210 
211   // Renew the connection and usually is used when HIDL restarted
212   void RenewAudioProviderAndSession();
213 
214   int EndSession();
215 
216   bool UpdateAudioConfig(const AudioConfiguration& audioConfig);
217   bool UpdateAudioConfig_2_1(const AudioConfiguration_2_1& audioConfig_2_1);
218 
219   void FlushAudioData();
220 
221   static constexpr PcmParameters kInvalidPcmConfiguration = {
222       .sampleRate = SampleRate::RATE_UNKNOWN,
223       .channelMode = ChannelMode::UNKNOWN,
224       .bitsPerSample = BitsPerSample::BITS_UNKNOWN};
225 
226  protected:
227   mutable std::mutex internal_mutex_;
228   // Helper function to connect to an IBluetoothAudioProvider
229   void FetchAudioProvider();
230   // Helper function to connect to an IBluetoothAudioProvider 2.1
231   void FetchAudioProvider_2_1();
232 
233   android::sp<IBluetoothAudioProvider> provider_;
234   android::sp<IBluetoothAudioProvider_2_1> provider_2_1_;
235   bool session_started_;
236   std::unique_ptr<::android::hardware::MessageQueue<
237       uint8_t, ::android::hardware::kSynchronizedReadWrite>>
238       mDataMQ;
239   android::sp<BluetoothAudioDeathRecipient> death_recipient_;
240 
241  private:
242   IBluetoothTransportInstance* transport_;
243   std::vector<AudioCapabilities> capabilities_;
244   std::vector<AudioCapabilities_2_1> capabilities_2_1_;
245 };
246 
247 // The client interface connects an IBluetoothTransportInstance to
248 // IBluetoothAudioProvider and helps to route callbacks to
249 // IBluetoothTransportInstance
250 class BluetoothAudioSinkClientInterface : public BluetoothAudioClientInterface {
251  public:
252   // Constructs an BluetoothAudioSinkClientInterface to communicate to
253   // BluetoothAudio HAL. |sink| is the implementation for the transport, and
254   // |message_loop| is the thread where callbacks are invoked.
255   BluetoothAudioSinkClientInterface(
256       IBluetoothSinkTransportInstance* sink,
257       bluetooth::common::MessageLoopThread* message_loop);
258   virtual ~BluetoothAudioSinkClientInterface();
259 
GetTransportInstance()260   IBluetoothSinkTransportInstance* GetTransportInstance() const {
261     return sink_;
262   }
263 
264   // Read data from audio  HAL through fmq
265   size_t ReadAudioData(uint8_t* p_buf, uint32_t len);
266 
267  private:
268   IBluetoothSinkTransportInstance* sink_;
269 };
270 
271 class BluetoothAudioSourceClientInterface
272     : public BluetoothAudioClientInterface {
273  public:
274   // Constructs an BluetoothAudioSourceClientInterface to communicate to
275   // BluetoothAudio HAL. |source| is the implementation for the transport, and
276   // |message_loop| is the thread where callbacks are invoked.
277   BluetoothAudioSourceClientInterface(
278       IBluetoothSourceTransportInstance* source,
279       bluetooth::common::MessageLoopThread* message_loop);
280   virtual ~BluetoothAudioSourceClientInterface();
281 
GetTransportInstance()282   IBluetoothSourceTransportInstance* GetTransportInstance() const {
283     return source_;
284   }
285 
286   // Write data to audio HAL through fmq
287   size_t WriteAudioData(const uint8_t* p_buf, uint32_t len);
288 
289  private:
290   IBluetoothSourceTransportInstance* source_;
291 };
292 
293 }  // namespace hidl
294 }  // namespace audio
295 }  // namespace bluetooth
296 
297 namespace fmt {
298 template <>
299 struct formatter<bluetooth::audio::hidl::BluetoothAudioCtrlAck>
300     : enum_formatter<bluetooth::audio::hidl::BluetoothAudioCtrlAck> {};
301 }  // namespace fmt
302