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 <time.h>
20 #include <mutex>
21 #include <vector>
22
23 #include <android/hardware/bluetooth/audio/2.1/IBluetoothAudioProvider.h>
24 #include <android/hardware/bluetooth/audio/2.1/types.h>
25 #include <fmq/MessageQueue.h>
26 #include <hardware/audio.h>
27
28 #include "common/message_loop_thread.h"
29
30 #define BLUETOOTH_AUDIO_HAL_PROP_DISABLED "persist.bluetooth.bluetooth_audio_hal.disabled"
31
32 namespace bluetooth {
33 namespace audio {
34
35 using ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioPort;
36 using AudioCapabilities =
37 ::android::hardware::bluetooth::audio::V2_0::AudioCapabilities;
38 using AudioCapabilities_2_1 =
39 ::android::hardware::bluetooth::audio::V2_1::AudioCapabilities;
40 using AudioConfiguration =
41 ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
42 using AudioConfiguration_2_1 =
43 ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration;
44 using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
45 using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
46 using IBluetoothAudioProvider =
47 ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioProvider;
48 using IBluetoothAudioProvider_2_1 =
49 ::android::hardware::bluetooth::audio::V2_1::IBluetoothAudioProvider;
50 using PcmParameters =
51 ::android::hardware::bluetooth::audio::V2_0::PcmParameters;
52 using PcmParameters_2_1 =
53 ::android::hardware::bluetooth::audio::V2_1::PcmParameters;
54 using SampleRate = ::android::hardware::bluetooth::audio::V2_0::SampleRate;
55 using SampleRate_2_1 = ::android::hardware::bluetooth::audio::V2_1::SampleRate;
56 using SessionType = ::android::hardware::bluetooth::audio::V2_0::SessionType;
57 using SessionType_2_1 =
58 ::android::hardware::bluetooth::audio::V2_1::SessionType;
59 using ::android::hardware::bluetooth::audio::V2_0::TimeSpec;
60 using BluetoothAudioStatus =
61 ::android::hardware::bluetooth::audio::V2_0::Status;
62
63 enum class BluetoothAudioCtrlAck : uint8_t {
64 SUCCESS_FINISHED = 0,
65 PENDING,
66 FAILURE_UNSUPPORTED,
67 FAILURE_BUSY,
68 FAILURE_DISCONNECTING,
69 FAILURE
70 };
71
72 std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack);
73
BluetoothAudioCtrlAckToHalStatus(const BluetoothAudioCtrlAck & ack)74 inline BluetoothAudioStatus BluetoothAudioCtrlAckToHalStatus(
75 const BluetoothAudioCtrlAck& ack) {
76 switch (ack) {
77 case BluetoothAudioCtrlAck::SUCCESS_FINISHED:
78 return BluetoothAudioStatus::SUCCESS;
79 case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED:
80 return BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION;
81 case BluetoothAudioCtrlAck::PENDING:
82 return BluetoothAudioStatus::FAILURE;
83 case BluetoothAudioCtrlAck::FAILURE_BUSY:
84 return BluetoothAudioStatus::FAILURE;
85 case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING:
86 return BluetoothAudioStatus::FAILURE;
87 default:
88 return BluetoothAudioStatus::FAILURE;
89 }
90 }
91
92 // An IBluetoothTransportInstance needs to be implemented by a Bluetooth
93 // audio transport, such as A2DP or Hearing Aid, to handle callbacks from Audio
94 // HAL.
95 class IBluetoothTransportInstance {
96 public:
IBluetoothTransportInstance(SessionType sessionType,AudioConfiguration audioConfig)97 IBluetoothTransportInstance(SessionType sessionType,
98 AudioConfiguration audioConfig)
99 : session_type_(sessionType),
100 session_type_2_1_(SessionType_2_1::UNKNOWN),
101 audio_config_(std::move(audioConfig)),
102 audio_config_2_1_({}){};
IBluetoothTransportInstance(SessionType_2_1 sessionType_2_1,AudioConfiguration_2_1 audioConfig_2_1)103 IBluetoothTransportInstance(SessionType_2_1 sessionType_2_1,
104 AudioConfiguration_2_1 audioConfig_2_1)
105 : session_type_(SessionType::UNKNOWN),
106 session_type_2_1_(sessionType_2_1),
107 audio_config_({}),
108 audio_config_2_1_(std::move(audioConfig_2_1)){};
109 virtual ~IBluetoothTransportInstance() = default;
110
GetSessionType()111 SessionType GetSessionType() const { return session_type_; }
GetSessionType_2_1()112 SessionType_2_1 GetSessionType_2_1() const { return session_type_2_1_; }
113
GetAudioConfiguration()114 AudioConfiguration GetAudioConfiguration() const { return audio_config_; }
GetAudioConfiguration_2_1()115 AudioConfiguration_2_1 GetAudioConfiguration_2_1() const {
116 return audio_config_2_1_;
117 }
118
UpdateAudioConfiguration(const AudioConfiguration & audio_config)119 void UpdateAudioConfiguration(const AudioConfiguration& audio_config) {
120 audio_config_ = audio_config;
121 }
UpdateAudioConfiguration_2_1(const AudioConfiguration_2_1 & audio_config_2_1)122 void UpdateAudioConfiguration_2_1(
123 const AudioConfiguration_2_1& audio_config_2_1) {
124 audio_config_2_1_ = audio_config_2_1;
125 }
126
127 virtual BluetoothAudioCtrlAck StartRequest() = 0;
128
129 virtual BluetoothAudioCtrlAck SuspendRequest() = 0;
130
131 virtual void StopRequest() = 0;
132
133 virtual bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
134 uint64_t* total_bytes_readed,
135 timespec* data_position) = 0;
136
137 virtual void MetadataChanged(const source_metadata_t& source_metadata) = 0;
138
139 // Invoked when the transport is requested to reset presentation position
140 virtual void ResetPresentationPosition() = 0;
141
142 private:
143 const SessionType session_type_;
144 const SessionType_2_1 session_type_2_1_;
145 AudioConfiguration audio_config_;
146 AudioConfiguration_2_1 audio_config_2_1_;
147 };
148
149 // An IBluetoothSinkTransportInstance needs to be implemented by a Bluetooth
150 // audio transport, such as A2DP, Hearing Aid or LeAudio, to handle callbacks
151 // from Audio HAL.
152 class IBluetoothSinkTransportInstance : public IBluetoothTransportInstance {
153 public:
IBluetoothSinkTransportInstance(SessionType_2_1 sessionType_2_1,AudioConfiguration_2_1 audioConfig_2_1)154 IBluetoothSinkTransportInstance(SessionType_2_1 sessionType_2_1,
155 AudioConfiguration_2_1 audioConfig_2_1)
156 : IBluetoothTransportInstance{sessionType_2_1, audioConfig_2_1} {}
IBluetoothSinkTransportInstance(SessionType sessionType,AudioConfiguration audioConfig)157 IBluetoothSinkTransportInstance(SessionType sessionType,
158 AudioConfiguration audioConfig)
159 : IBluetoothTransportInstance{sessionType, audioConfig} {}
160 virtual ~IBluetoothSinkTransportInstance() = default;
161
162 // Invoked when the transport is requested to log bytes read
163 virtual void LogBytesRead(size_t bytes_readed) = 0;
164 };
165
166 class IBluetoothSourceTransportInstance : public IBluetoothTransportInstance {
167 public:
IBluetoothSourceTransportInstance(SessionType sessionType,AudioConfiguration audioConfig)168 IBluetoothSourceTransportInstance(SessionType sessionType,
169 AudioConfiguration audioConfig)
170 : IBluetoothTransportInstance{sessionType, audioConfig} {}
IBluetoothSourceTransportInstance(SessionType_2_1 sessionType_2_1,AudioConfiguration_2_1 audioConfig_2_1)171 IBluetoothSourceTransportInstance(SessionType_2_1 sessionType_2_1,
172 AudioConfiguration_2_1 audioConfig_2_1)
173 : IBluetoothTransportInstance{sessionType_2_1, audioConfig_2_1} {}
174 virtual ~IBluetoothSourceTransportInstance() = default;
175
176 // Invoked when the transport is requested to log bytes written
177 virtual void LogBytesWritten(size_t bytes_written) = 0;
178 };
179
180 // common object is shared between different kind of SessionType
181 class BluetoothAudioDeathRecipient;
182
183 // The client interface connects an IBluetoothTransportInstance to
184 // IBluetoothAudioProvider and helps to route callbacks to
185 // IBluetoothTransportInstance
186 class BluetoothAudioClientInterface {
187 public:
188 BluetoothAudioClientInterface(
189 android::sp<BluetoothAudioDeathRecipient> death_recipient,
190 IBluetoothTransportInstance* instance);
191 virtual ~BluetoothAudioClientInterface() = default;
192
IsValid()193 bool IsValid() const {
194 return provider_ != nullptr || provider_2_1_ != nullptr;
195 }
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
282 // Write data to audio HAL through fmq
283 size_t WriteAudioData(const uint8_t* p_buf, uint32_t len);
284
285 private:
286 IBluetoothSourceTransportInstance* source_;
287 };
288
289 } // namespace audio
290 } // namespace bluetooth
291