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