1 /*
2 * Copyright 2024 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 #include "le_audio_software.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <hardware/audio.h>
22 #include <log/log.h>
23
24 #include <cerrno>
25
26 #include "aidl/android/hardware/bluetooth/audio/IBluetoothAudioProvider.h"
27 #include "aidl/audio_ctrl_ack.h"
28 #include "aidl/le_audio_software_aidl.h"
29 #include "audio_hal_interface/hal_version_manager.h"
30 #include "bta/le_audio/mock_codec_manager.h"
31 #include "gmock/gmock.h"
32 #include "hidl/le_audio_software_hidl.h"
33
34 #pragma GCC diagnostic ignored "-Wunused-private-field"
35
36 using testing::Return;
37 using testing::Test;
38
39 using bluetooth::audio::le_audio::LeAudioClientInterface;
40 using bluetooth::audio::le_audio::StreamCallbacks;
41
42 // MOCKS
43 namespace {
44 class MockHalVersionManager {
45 public:
46 MockHalVersionManager() = default;
47 MOCK_METHOD((bluetooth::audio::BluetoothAudioHalVersion), GetHalVersion, ());
48 MOCK_METHOD((bluetooth::audio::BluetoothAudioHalTransport), GetHalTransport, ());
49 MOCK_METHOD((android::sp<bluetooth::audio::IBluetoothAudioProvidersFactory_2_1>),
50 GetProvidersFactory_2_1, ());
51 MOCK_METHOD((android::sp<bluetooth::audio::IBluetoothAudioProvidersFactory_2_0>),
52 GetProvidersFactory_2_0, ());
53
SetInstance(MockHalVersionManager * ptr)54 static void SetInstance(MockHalVersionManager* ptr) { MockHalVersionManager::instance_ptr = ptr; }
55
GetInstance()56 static MockHalVersionManager* GetInstance() { return instance_ptr; }
57
58 private:
59 static MockHalVersionManager* instance_ptr;
60 };
61 MockHalVersionManager* MockHalVersionManager::instance_ptr = nullptr;
62
63 class MockBluetoothAudioClientInterfaceBidirEndpoint {
64 public:
65 MOCK_METHOD((size_t), WriteAudioData, (const uint8_t* /*p_buf*/, uint32_t /*len*/), ());
66 MOCK_METHOD((size_t), ReadAudioData, (uint8_t* /*p_buf*/, uint32_t /*len*/), ());
67 };
68
69 class MockBluetoothAudioClientInterfaceHidl {
70 public:
71 MockBluetoothAudioClientInterfaceBidirEndpoint endpoint;
72
73 MOCK_METHOD((bool), IsValid, (), (const));
74 MOCK_METHOD((void), FlushAudioData, ());
75 MOCK_METHOD((bool), UpdateAudioConfig_2_1,
76 (const bluetooth::audio::hidl::AudioConfiguration_2_1& /*audio_config_2_1*/));
77 MOCK_METHOD((int), StartSession_2_1, ());
78 MOCK_METHOD((void), StreamStarted,
79 (const bluetooth::audio::hidl::BluetoothAudioCtrlAck& /*ack*/));
80 MOCK_METHOD((int), EndSession, ());
81 MOCK_METHOD((void), StreamSuspended,
82 (const bluetooth::audio::hidl::BluetoothAudioCtrlAck& /*ack*/));
83
SetInstance(MockBluetoothAudioClientInterfaceHidl * ptr)84 static void SetInstance(MockBluetoothAudioClientInterfaceHidl* ptr) { instance_ptr = ptr; }
85
GetInstance()86 static MockBluetoothAudioClientInterfaceHidl* GetInstance() { return instance_ptr; }
87
88 private:
89 static MockBluetoothAudioClientInterfaceHidl* instance_ptr;
90 };
91 MockBluetoothAudioClientInterfaceHidl* MockBluetoothAudioClientInterfaceHidl::instance_ptr =
92 nullptr;
93
94 class MockBluetoothAudioClientInterfaceAidl {
95 public:
96 MockBluetoothAudioClientInterfaceBidirEndpoint endpoint;
97
98 MOCK_METHOD((bool), IsValid, (), (const));
99 MOCK_METHOD((bool), SetAllowedLatencyModes,
100 (std::vector<bluetooth::audio::aidl::LatencyMode> /*latency_modes*/));
101 MOCK_METHOD((void), FlushAudioData, ());
102 MOCK_METHOD((bool), UpdateAudioConfig,
103 (const bluetooth::audio::aidl::AudioConfiguration& /*audio_config*/));
104 MOCK_METHOD((int), StartSession, ());
105 MOCK_METHOD((void), StreamStarted,
106 (const bluetooth::audio::aidl::BluetoothAudioCtrlAck& /*ack*/));
107 MOCK_METHOD((int), EndSession, ());
108 MOCK_METHOD((void), StreamSuspended,
109 (const bluetooth::audio::aidl::BluetoothAudioCtrlAck& /*ack*/));
110 MOCK_METHOD((std::vector<bluetooth::audio::aidl::AudioCapabilities>), GetAudioCapabilities,
111 (bluetooth::audio::aidl::SessionType /*session_type*/));
112 MOCK_METHOD(
113 (std::vector<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
114 LeAudioAseConfigurationSetting>),
115 GetLeAudioAseConfiguration,
116 ((std::optional<std::vector<
117 std::optional<::aidl::android::hardware::bluetooth::audio::
118 IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&),
119 (std::optional<std::vector<
120 std::optional<::aidl::android::hardware::bluetooth::audio::
121 IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&),
122 (std::vector<::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
123 LeAudioConfigurationRequirement>&)));
124 MOCK_METHOD(
125 (::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
126 LeAudioBroadcastConfigurationSetting),
127 getLeAudioBroadcastConfiguration,
128 ((const std::optional<std::vector<
129 std::optional<::aidl::android::hardware::bluetooth::audio::
130 IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&),
131 (const ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider::
132 LeAudioBroadcastConfigurationRequirement&)));
133
SetInstance(MockBluetoothAudioClientInterfaceAidl * ptr)134 static void SetInstance(MockBluetoothAudioClientInterfaceAidl* ptr) { instance_ptr = ptr; }
135
GetInstance()136 static MockBluetoothAudioClientInterfaceAidl* GetInstance() { return instance_ptr; }
137
138 private:
139 static MockBluetoothAudioClientInterfaceAidl* instance_ptr;
140 };
141 MockBluetoothAudioClientInterfaceAidl* MockBluetoothAudioClientInterfaceAidl::instance_ptr =
142 nullptr;
143
144 class MockStreamCallbacks {
145 public:
146 MOCK_METHOD((bool), OnResume, (bool));
147 MOCK_METHOD((bool), OnSuspend, ());
148 MOCK_METHOD((bool), OnSourceMetadataUpdate,
149 ((const source_metadata_v7_t&), ::bluetooth::le_audio::DsaMode));
150 MOCK_METHOD((bool), OnSinkMetadataUpdate, (const sink_metadata_v7_t&));
151 };
152 } // namespace
153
154 namespace bluetooth::audio {
155 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_UNAVAILABLE =
156 BluetoothAudioHalVersion();
157 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_2_1 =
158 BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 1);
159
GetHalTransport()160 BluetoothAudioHalTransport HalVersionManager::GetHalTransport() {
161 auto instance = MockHalVersionManager::GetInstance();
162 if (instance) {
163 return instance->GetHalTransport();
164 }
165 return BluetoothAudioHalTransport::UNKNOWN;
166 }
167
GetHalVersion()168 BluetoothAudioHalVersion HalVersionManager::GetHalVersion() {
169 auto instance = MockHalVersionManager::GetInstance();
170 if (instance) {
171 return instance->GetHalVersion();
172 }
173 return BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
174 }
175
176 namespace hidl {
177 class BluetoothAudioDeathRecipient : public ::android::hardware::hidl_death_recipient {
178 public:
BluetoothAudioDeathRecipient(BluetoothAudioClientInterface * clientif,bluetooth::common::MessageLoopThread * message_loop)179 BluetoothAudioDeathRecipient(BluetoothAudioClientInterface* clientif,
180 bluetooth::common::MessageLoopThread* message_loop)
181 : bluetooth_audio_clientif_(clientif), message_loop_(message_loop) {}
182
183 MOCK_METHOD((void), serviceDied,
184 (uint64_t /*cookie*/,
185 const ::android::wp<::android::hidl::base::V1_0::IBase>& /*who*/),
186 (override));
187
188 private:
189 BluetoothAudioClientInterface* bluetooth_audio_clientif_;
190 bluetooth::common::MessageLoopThread* message_loop_;
191 };
192
BluetoothAudioClientInterface(android::sp<BluetoothAudioDeathRecipient> death_recipient,IBluetoothTransportInstance * instance)193 BluetoothAudioClientInterface::BluetoothAudioClientInterface(
194 android::sp<BluetoothAudioDeathRecipient> death_recipient,
195 IBluetoothTransportInstance* instance)
196 : provider_(nullptr),
197 provider_2_1_(nullptr),
198 session_started_(false),
199 mDataMQ(nullptr),
200 transport_(instance) {
201 death_recipient_ = death_recipient;
202 }
203
BluetoothAudioSinkClientInterface(IBluetoothSinkTransportInstance * sink,bluetooth::common::MessageLoopThread * message_loop)204 BluetoothAudioSinkClientInterface::BluetoothAudioSinkClientInterface(
205 IBluetoothSinkTransportInstance* sink, bluetooth::common::MessageLoopThread* message_loop)
206 : BluetoothAudioClientInterface{new BluetoothAudioDeathRecipient(this, message_loop), sink},
207 sink_(sink) {}
~BluetoothAudioSinkClientInterface()208 BluetoothAudioSinkClientInterface::~BluetoothAudioSinkClientInterface() {}
209
ReadAudioData(uint8_t * p_buf,uint32_t len)210 size_t BluetoothAudioSinkClientInterface::ReadAudioData(uint8_t* p_buf, uint32_t len) {
211 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
212 if (instance) {
213 return instance->endpoint.ReadAudioData(p_buf, len);
214 }
215 return 0;
216 }
217
BluetoothAudioSourceClientInterface(IBluetoothSourceTransportInstance * source,bluetooth::common::MessageLoopThread * message_loop)218 BluetoothAudioSourceClientInterface::BluetoothAudioSourceClientInterface(
219 IBluetoothSourceTransportInstance* source,
220 bluetooth::common::MessageLoopThread* message_loop)
221 : BluetoothAudioClientInterface{new BluetoothAudioDeathRecipient(this, message_loop), source},
222 source_(source) {}
~BluetoothAudioSourceClientInterface()223 BluetoothAudioSourceClientInterface::~BluetoothAudioSourceClientInterface() {}
224
IsValid() const225 bool BluetoothAudioClientInterface::IsValid() const {
226 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
227 if (instance) {
228 return instance->IsValid();
229 }
230 return false;
231 }
232
WriteAudioData(const uint8_t * p_buf,uint32_t len)233 size_t BluetoothAudioSourceClientInterface::WriteAudioData(const uint8_t* p_buf, uint32_t len) {
234 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
235 if (instance) {
236 return instance->endpoint.WriteAudioData(p_buf, len);
237 }
238 return 0;
239 }
240
FlushAudioData()241 void BluetoothAudioClientInterface::FlushAudioData() {
242 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
243 if (instance) {
244 instance->FlushAudioData();
245 }
246 }
247
UpdateAudioConfig_2_1(const AudioConfiguration_2_1 & cfg)248 bool BluetoothAudioClientInterface::UpdateAudioConfig_2_1(const AudioConfiguration_2_1& cfg) {
249 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
250 if (instance) {
251 return instance->UpdateAudioConfig_2_1(cfg);
252 }
253 return false;
254 }
255
StartSession_2_1()256 int BluetoothAudioClientInterface::StartSession_2_1() {
257 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
258 if (instance) {
259 return instance->StartSession_2_1();
260 }
261 return -EINVAL;
262 }
263
StreamStarted(const BluetoothAudioCtrlAck & ack)264 void BluetoothAudioClientInterface::StreamStarted(const BluetoothAudioCtrlAck& ack) {
265 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
266 if (instance) {
267 instance->StreamStarted(ack);
268 }
269 }
270
EndSession()271 int BluetoothAudioClientInterface::EndSession() {
272 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
273 if (instance) {
274 return instance->EndSession();
275 }
276 return -EINVAL;
277 }
278
StreamSuspended(const BluetoothAudioCtrlAck & ack)279 void BluetoothAudioClientInterface::StreamSuspended(const BluetoothAudioCtrlAck& ack) {
280 auto instance = MockBluetoothAudioClientInterfaceHidl::GetInstance();
281 if (instance) {
282 instance->StreamSuspended(ack);
283 }
284 }
285
operator <<(std::ostream & os,const BluetoothAudioCtrlAck & ack)286 std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack) {
287 switch (ack) {
288 case BluetoothAudioCtrlAck::SUCCESS_FINISHED:
289 os << "SUCCESS_FINISHED";
290 break;
291 case BluetoothAudioCtrlAck::PENDING:
292 os << "PENDING";
293 break;
294 case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED:
295 os << "FAILURE_UNSUPPORTED";
296 break;
297 case BluetoothAudioCtrlAck::FAILURE_BUSY:
298 os << "FAILURE_BUSY";
299 break;
300 case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING:
301 os << "FAILURE_DISCONNECTING";
302 break;
303 case BluetoothAudioCtrlAck::FAILURE:
304 os << "FAILURE";
305 break;
306 default:
307 os << "UNKNOWN";
308 break;
309 };
310 return os;
311 }
312 } // namespace hidl
313
314 namespace aidl {
BluetoothAudioClientInterface(IBluetoothTransportInstance * instance)315 BluetoothAudioClientInterface::BluetoothAudioClientInterface(IBluetoothTransportInstance* instance)
316 : provider_(nullptr),
317 provider_factory_(nullptr),
318 session_started_(false),
319 data_mq_(nullptr),
320 transport_(instance),
321 latency_modes_({LatencyMode::FREE}) {}
322
BluetoothAudioSinkClientInterface(IBluetoothSinkTransportInstance * sink)323 BluetoothAudioSinkClientInterface::BluetoothAudioSinkClientInterface(
324 IBluetoothSinkTransportInstance* sink)
325 : BluetoothAudioClientInterface{sink}, sink_(sink) {}
~BluetoothAudioSinkClientInterface()326 BluetoothAudioSinkClientInterface::~BluetoothAudioSinkClientInterface() {}
327
ReadAudioData(uint8_t * p_buf,uint32_t len)328 size_t BluetoothAudioSinkClientInterface::ReadAudioData(uint8_t* p_buf, uint32_t len) {
329 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
330 if (instance) {
331 return instance->endpoint.ReadAudioData(p_buf, len);
332 }
333 return 0;
334 }
335
BluetoothAudioSourceClientInterface(IBluetoothSourceTransportInstance * source)336 BluetoothAudioSourceClientInterface::BluetoothAudioSourceClientInterface(
337 IBluetoothSourceTransportInstance* source)
338 : BluetoothAudioClientInterface{source}, source_(source) {}
~BluetoothAudioSourceClientInterface()339 BluetoothAudioSourceClientInterface::~BluetoothAudioSourceClientInterface() {}
340
WriteAudioData(const uint8_t * p_buf,uint32_t len)341 size_t BluetoothAudioSourceClientInterface::WriteAudioData(const uint8_t* p_buf, uint32_t len) {
342 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
343 if (instance) {
344 return instance->endpoint.WriteAudioData(p_buf, len);
345 }
346 return 0;
347 }
348
IsValid() const349 bool BluetoothAudioClientInterface::IsValid() const {
350 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
351 if (instance) {
352 return instance->IsValid();
353 }
354 return false;
355 }
356
SetAllowedLatencyModes(std::vector<LatencyMode> latency_modes)357 bool BluetoothAudioClientInterface::SetAllowedLatencyModes(std::vector<LatencyMode> latency_modes) {
358 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
359 if (instance) {
360 return instance->SetAllowedLatencyModes(latency_modes);
361 }
362 return false;
363 }
364
FlushAudioData()365 void BluetoothAudioClientInterface::FlushAudioData() {
366 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
367 if (instance) {
368 instance->FlushAudioData();
369 }
370 }
371
UpdateAudioConfig(const AudioConfiguration & audio_config)372 bool BluetoothAudioClientInterface::UpdateAudioConfig(const AudioConfiguration& audio_config) {
373 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
374 if (instance) {
375 return instance->UpdateAudioConfig(audio_config);
376 }
377 return false;
378 }
379
StartSession()380 int BluetoothAudioClientInterface::StartSession() {
381 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
382 if (instance) {
383 return instance->StartSession();
384 }
385 return -EINVAL;
386 }
387
StreamStarted(const BluetoothAudioCtrlAck & ack)388 void BluetoothAudioClientInterface::StreamStarted(const BluetoothAudioCtrlAck& ack) {
389 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
390 if (instance) {
391 instance->StreamStarted(ack);
392 }
393 }
394
EndSession()395 int BluetoothAudioClientInterface::EndSession() {
396 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
397 if (instance) {
398 return instance->EndSession();
399 }
400 return -EINVAL;
401 }
402
StreamSuspended(const BluetoothAudioCtrlAck & ack)403 void BluetoothAudioClientInterface::StreamSuspended(const BluetoothAudioCtrlAck& ack) {
404 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
405 if (instance) {
406 return instance->StreamSuspended(ack);
407 }
408 }
409
GetAudioCapabilities(SessionType session_type)410 std::vector<AudioCapabilities> BluetoothAudioClientInterface::GetAudioCapabilities(
411 SessionType session_type) {
412 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
413 if (instance) {
414 return instance->GetAudioCapabilities(session_type);
415 }
416 return std::vector<AudioCapabilities>(0);
417 }
418
419 std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting>
GetLeAudioAseConfiguration(std::optional<std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & remoteSinkAudioCapabilities,std::optional<std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & remoteSourceAudioCapabilities,std::vector<IBluetoothAudioProvider::LeAudioConfigurationRequirement> & requirements)420 BluetoothAudioClientInterface::GetLeAudioAseConfiguration(
421 std::optional<
422 std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
423 remoteSinkAudioCapabilities,
424 std::optional<
425 std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
426 remoteSourceAudioCapabilities,
427 std::vector<IBluetoothAudioProvider::LeAudioConfigurationRequirement>& requirements) {
428 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
429 if (instance) {
430 return instance->GetLeAudioAseConfiguration(remoteSinkAudioCapabilities,
431 remoteSourceAudioCapabilities, requirements);
432 }
433
434 return std::vector<IBluetoothAudioProvider::LeAudioAseConfigurationSetting>();
435 }
436
437 IBluetoothAudioProvider::LeAudioBroadcastConfigurationSetting
getLeAudioBroadcastConfiguration(const std::optional<std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>> & remoteSinkAudioCapabilities,const IBluetoothAudioProvider::LeAudioBroadcastConfigurationRequirement & requirement)438 BluetoothAudioClientInterface::getLeAudioBroadcastConfiguration(
439 const std::optional<
440 std::vector<std::optional<IBluetoothAudioProvider::LeAudioDeviceCapabilities>>>&
441 remoteSinkAudioCapabilities,
442 const IBluetoothAudioProvider::LeAudioBroadcastConfigurationRequirement& requirement) {
443 auto instance = MockBluetoothAudioClientInterfaceAidl::GetInstance();
444 if (instance) {
445 return instance->getLeAudioBroadcastConfiguration(remoteSinkAudioCapabilities, requirement);
446 }
447
448 return IBluetoothAudioProvider::LeAudioBroadcastConfigurationSetting();
449 }
450
operator <<(std::ostream & os,const BluetoothAudioCtrlAck & ack)451 std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack) {
452 switch (ack) {
453 case BluetoothAudioCtrlAck::SUCCESS_FINISHED:
454 os << "SUCCESS_FINISHED";
455 break;
456 case BluetoothAudioCtrlAck::SUCCESS_RECONFIGURATION:
457 os << "SUCCESS_RECONFIGURATION";
458 break;
459 case BluetoothAudioCtrlAck::PENDING:
460 os << "PENDING";
461 break;
462 case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED:
463 os << "FAILURE_UNSUPPORTED";
464 break;
465 case BluetoothAudioCtrlAck::FAILURE_BUSY:
466 os << "FAILURE_BUSY";
467 break;
468 case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING:
469 os << "FAILURE_DISCONNECTING";
470 break;
471 case BluetoothAudioCtrlAck::FAILURE:
472 os << "FAILURE";
473 break;
474 default:
475 os << "UNKNOWN";
476 break;
477 };
478 return os;
479 }
480 } // namespace aidl
481 } // namespace bluetooth::audio
482
483 namespace bluetooth::le_audio::broadcaster {
operator <<(std::ostream & os,const BroadcastConfiguration &)484 std::ostream& operator<<(std::ostream& os, const BroadcastConfiguration&) { return os; }
485 } // namespace bluetooth::le_audio::broadcaster
486
487 namespace {
488
489 bluetooth::common::MessageLoopThread message_loop_thread("test message loop");
490 static base::MessageLoop* message_loop_;
491
init_message_loop_thread()492 static void init_message_loop_thread() {
493 message_loop_thread.StartUp();
494 if (!message_loop_thread.IsRunning()) {
495 FAIL() << "unable to create message loop thread.";
496 }
497
498 if (!message_loop_thread.EnableRealTimeScheduling()) {
499 bluetooth::log::warn("Unable to set real time scheduling");
500 }
501
502 message_loop_ = message_loop_thread.message_loop();
503 if (message_loop_ == nullptr) {
504 FAIL() << "unable to get message loop.";
505 }
506 }
507
cleanup_message_loop_thread()508 static void cleanup_message_loop_thread() {
509 message_loop_ = nullptr;
510 message_loop_thread.ShutDown();
511 }
512
513 class LeAudioSoftwareUnicastTest : public Test {
514 protected:
SetUp()515 virtual void SetUp() override {
516 init_message_loop_thread();
517 MockHalVersionManager::SetInstance(&hal_version_manager_);
518
519 unicast_sink_stream_cb_.reset(new StreamCallbacks{
520 std::bind(&MockStreamCallbacks::OnResume, &sink_stream_callbacks_,
521 std::placeholders::_1),
522 std::bind(&MockStreamCallbacks::OnSuspend, &sink_stream_callbacks_),
523 std::bind(&MockStreamCallbacks::OnSourceMetadataUpdate, &sink_stream_callbacks_,
524 std::placeholders::_1, std::placeholders::_2),
525 std::bind(&MockStreamCallbacks::OnSinkMetadataUpdate, &sink_stream_callbacks_,
526 std::placeholders::_1),
527 });
528
529 unicast_source_stream_cb_.reset(new StreamCallbacks{
530 std::bind(&MockStreamCallbacks::OnResume, &source_stream_callbacks_,
531 std::placeholders::_1),
532 std::bind(&MockStreamCallbacks::OnSuspend, &source_stream_callbacks_),
533 std::bind(&MockStreamCallbacks::OnSourceMetadataUpdate, &source_stream_callbacks_,
534 std::placeholders::_1, std::placeholders::_2),
535 std::bind(&MockStreamCallbacks::OnSinkMetadataUpdate, &source_stream_callbacks_,
536 std::placeholders::_1),
537 });
538
539 sink_ = LeAudioClientInterface::Get()->GetSink(*unicast_sink_stream_cb_, &message_loop_thread,
540 is_broadcast_);
541 if (is_broadcast_) {
542 ASSERT_TRUE(LeAudioClientInterface::Get()->IsBroadcastSinkAcquired());
543 } else {
544 source_ = LeAudioClientInterface::Get()->GetSource(*unicast_source_stream_cb_,
545 &message_loop_thread);
546 ASSERT_TRUE(LeAudioClientInterface::Get()->IsSourceAcquired());
547 ASSERT_TRUE(LeAudioClientInterface::Get()->IsUnicastSinkAcquired());
548 }
549 }
550
TearDown()551 virtual void TearDown() override {
552 if (LeAudioClientInterface::Get()->IsUnicastSinkAcquired() ||
553 LeAudioClientInterface::Get()->IsBroadcastSinkAcquired()) {
554 LeAudioClientInterface::Get()->ReleaseSink(sink_);
555 if (is_broadcast_) {
556 ASSERT_FALSE(LeAudioClientInterface::Get()->IsBroadcastSinkAcquired());
557 } else {
558 ASSERT_FALSE(LeAudioClientInterface::Get()->IsUnicastSinkAcquired());
559 }
560 }
561
562 if (LeAudioClientInterface::Get()->IsSourceAcquired()) {
563 LeAudioClientInterface::Get()->ReleaseSource(source_);
564 ASSERT_FALSE(LeAudioClientInterface::Get()->IsSourceAcquired());
565 }
566
567 cleanup_message_loop_thread();
568
569 unicast_sink_stream_cb_.reset();
570 unicast_source_stream_cb_.reset();
571
572 MockBluetoothAudioClientInterfaceHidl::SetInstance(nullptr);
573 MockBluetoothAudioClientInterfaceAidl::SetInstance(nullptr);
574 MockHalVersionManager::SetInstance(nullptr);
575 }
576
577 bool is_broadcast_ = false;
578 LeAudioClientInterface::Sink* sink_ = nullptr;
579 LeAudioClientInterface::Source* source_ = nullptr;
580
581 MockHalVersionManager hal_version_manager_;
582 MockStreamCallbacks sink_stream_callbacks_;
583 MockStreamCallbacks source_stream_callbacks_;
584
585 std::unique_ptr<StreamCallbacks> unicast_sink_stream_cb_;
586 std::unique_ptr<StreamCallbacks> unicast_source_stream_cb_;
587 };
588
589 class LeAudioSoftwareUnicastTestAidl : public LeAudioSoftwareUnicastTest {
590 protected:
SetUpMockCodecManager(bluetooth::le_audio::types::CodecLocation location)591 void SetUpMockCodecManager(bluetooth::le_audio::types::CodecLocation location) {
592 codec_manager_ = bluetooth::le_audio::CodecManager::GetInstance();
593 ASSERT_NE(codec_manager_, nullptr);
594 std::vector<bluetooth::le_audio::btle_audio_codec_config_t> mock_offloading_preference(0);
595 codec_manager_->Start(mock_offloading_preference);
596 mock_codec_manager_ = MockCodecManager::GetInstance();
597 ASSERT_NE((void*)mock_codec_manager_, (void*)codec_manager_);
598 ASSERT_NE(mock_codec_manager_, nullptr);
599 ON_CALL(*mock_codec_manager_, GetCodecLocation()).WillByDefault(Return(location));
600 }
601
SetUp()602 virtual void SetUp() override {
603 SetUpMockCodecManager(::bluetooth::le_audio::types::CodecLocation::ADSP);
604 ON_CALL(hal_version_manager_, GetHalTransport)
605 .WillByDefault(Return(bluetooth::audio::BluetoothAudioHalTransport::AIDL));
606
607 MockBluetoothAudioClientInterfaceAidl::SetInstance(&audio_client_interface_);
608 ON_CALL(audio_client_interface_, IsValid).WillByDefault(Return(true));
609
610 LeAudioSoftwareUnicastTest::SetUp();
611 }
612
613 MockBluetoothAudioClientInterfaceAidl audio_client_interface_;
614 bluetooth::le_audio::CodecManager* codec_manager_;
615 MockCodecManager* mock_codec_manager_;
616 };
617
TEST_F(LeAudioSoftwareUnicastTestAidl,AcquireAndRelease)618 TEST_F(LeAudioSoftwareUnicastTestAidl, AcquireAndRelease) {
619 ASSERT_NE(nullptr, sink_);
620 ASSERT_NE(nullptr, source_);
621 }
622
623 class LeAudioSoftwareUnicastTestHidl : public LeAudioSoftwareUnicastTest {
624 protected:
SetUp()625 virtual void SetUp() override {
626 ON_CALL(hal_version_manager_, GetHalTransport)
627 .WillByDefault(Return(bluetooth::audio::BluetoothAudioHalTransport::HIDL));
628
629 MockBluetoothAudioClientInterfaceHidl::SetInstance(&audio_client_interface_);
630 ON_CALL(audio_client_interface_, IsValid).WillByDefault(Return(true));
631
632 LeAudioSoftwareUnicastTest::SetUp();
633 }
634
635 MockBluetoothAudioClientInterfaceHidl audio_client_interface_;
636 };
637
TEST_F(LeAudioSoftwareUnicastTestHidl,AcquireAndRelease)638 TEST_F(LeAudioSoftwareUnicastTestHidl, AcquireAndRelease) {
639 ASSERT_NE(nullptr, sink_);
640 ASSERT_NE(nullptr, source_);
641 }
642
643 class LeAudioSoftwareBroadcastTestAidl : public LeAudioSoftwareUnicastTestAidl {
644 protected:
SetUp()645 virtual void SetUp() override {
646 is_broadcast_ = true;
647 LeAudioSoftwareUnicastTestAidl::SetUp();
648 }
649 };
650
TEST_F(LeAudioSoftwareBroadcastTestAidl,AcquireAndRelease)651 TEST_F(LeAudioSoftwareBroadcastTestAidl, AcquireAndRelease) {
652 ASSERT_NE(nullptr, sink_);
653 ASSERT_EQ(nullptr, source_);
654 ASSERT_NE(::bluetooth::audio::aidl::le_audio::LeAudioSinkTransport::interface_broadcast_,
655 nullptr);
656 ASSERT_EQ(::bluetooth::audio::aidl::le_audio::LeAudioSinkTransport::interface_unicast_, nullptr);
657 ASSERT_EQ(::bluetooth::audio::aidl::le_audio::LeAudioSourceTransport::interface, nullptr);
658 }
659
TEST_F(LeAudioSoftwareBroadcastTestAidl,GetBroadcastConfig)660 TEST_F(LeAudioSoftwareBroadcastTestAidl, GetBroadcastConfig) {
661 ASSERT_NE(nullptr, sink_);
662 ASSERT_NE(sink_->GetBroadcastConfig({}, std::nullopt), std::nullopt);
663 }
664
665 } // namespace
666