1 /*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bluetooth_a2dp_src.h"
17 #include <unistd.h>
18 #include "bluetooth_a2dp_codec.h"
19 #include "bluetooth_a2dp_src_proxy.h"
20 #include "bluetooth_a2dp_src_observer_stub.h"
21 #include "bluetooth_device.h"
22 #include "bluetooth_host_proxy.h"
23 #include "bluetooth_observer_list.h"
24 #include "raw_address.h"
25 #include "bluetooth_def.h"
26 #include "bluetooth_host.h"
27
28 #include "bluetooth_log.h"
29 #include "bluetooth_utils.h"
30 #include "iservice_registry.h"
31 #include "system_ability_definition.h"
32
33 namespace OHOS {
34 namespace Bluetooth {
35 using namespace OHOS::bluetooth;
36
37 struct A2dpSource::impl {
38 impl();
39 ~impl();
40
41 BluetoothObserverList<A2dpSourceObserver> observers_;
42 sptr<IBluetoothA2dpSrc> proxy_ = nullptr;
43 class BluetoothA2dpSourceObserverImp;
44 sptr<BluetoothA2dpSourceObserverImp> observerImp_ = nullptr;
45 class BluetoothA2dpSourceDeathRecipient;
46 sptr<BluetoothA2dpSourceDeathRecipient> deathRecipient_ = nullptr;
47
48 private:
49 void GetA2dpSrcProxy();
50 };
51
52 class A2dpSource::impl::BluetoothA2dpSourceObserverImp : public BluetoothA2dpSrcObserverStub {
53 public:
BluetoothA2dpSourceObserverImp(A2dpSource::impl & a2dpSource)54 BluetoothA2dpSourceObserverImp(A2dpSource::impl &a2dpSource) : a2dpSource_(a2dpSource) {};
~BluetoothA2dpSourceObserverImp()55 ~BluetoothA2dpSourceObserverImp() override{};
56
Register(std::shared_ptr<A2dpSourceObserver> & observer)57 void Register(std::shared_ptr<A2dpSourceObserver> &observer)
58 {
59 HILOGI("enter");
60 a2dpSource_.observers_.Register(observer);
61 }
62
Deregister(std::shared_ptr<A2dpSourceObserver> & observer)63 void Deregister(std::shared_ptr<A2dpSourceObserver> &observer)
64 {
65 HILOGI("enter");
66 a2dpSource_.observers_.Deregister(observer);
67 }
68
OnConnectionStateChanged(const RawAddress & device,int state)69 void OnConnectionStateChanged(const RawAddress &device, int state) override
70 {
71 HILOGI("a2dpSrc conn state, device: %{public}s, state: %{public}s",
72 GetEncryptAddr((device).GetAddress()).c_str(), GetProfileConnStateName(state).c_str());
73 a2dpSource_.observers_.ForEach([device, state](std::shared_ptr<A2dpSourceObserver> observer) {
74 observer->OnConnectionStateChanged(BluetoothRemoteDevice(device.GetAddress(), 0), state);
75 });
76 }
77
OnPlayingStatusChanged(const RawAddress & device,int playingState,int error)78 void OnPlayingStatusChanged(const RawAddress &device, int playingState, int error) override
79 {
80 HILOGI("device: %{public}s, playingState: %{public}d, error: %{public}d",
81 GetEncryptAddr(device.GetAddress()).c_str(), playingState, error);
82 a2dpSource_.observers_.ForEach([device, playingState, error](std::shared_ptr<A2dpSourceObserver> observer) {
83 observer->OnPlayingStatusChanged(BluetoothRemoteDevice(device.GetAddress(), 0), playingState, error);
84 });
85 }
86
OnConfigurationChanged(const RawAddress & device,const BluetoothA2dpCodecInfo & info,int error)87 void OnConfigurationChanged(const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error) override
88 {
89 HILOGI("device: %{public}s, error: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), error);
90 a2dpSource_.observers_.ForEach([device, info, error](std::shared_ptr<A2dpSourceObserver> observer) {
91 A2dpCodecInfo codecInfo;
92
93 codecInfo.bitsPerSample = info.bitsPerSample;
94 codecInfo.channelMode = info.channelMode;
95 codecInfo.codecPriority = info.codecPriority;
96 codecInfo.codecType = info.codecType;
97 codecInfo.sampleRate = info.sampleRate;
98
99 observer->OnConfigurationChanged(BluetoothRemoteDevice(device.GetAddress(), 0), codecInfo, error);
100 });
101 };
102
103 private:
104 A2dpSource::impl &a2dpSource_;
105 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothA2dpSourceObserverImp);
106 };
107
108 class A2dpSource::impl::BluetoothA2dpSourceDeathRecipient final : public IRemoteObject::DeathRecipient {
109 public:
BluetoothA2dpSourceDeathRecipient(A2dpSource::impl & a2dpSrcDeath)110 BluetoothA2dpSourceDeathRecipient(A2dpSource::impl &a2dpSrcDeath) : a2dpSrcDeath_(a2dpSrcDeath)
111 {};
112 ~BluetoothA2dpSourceDeathRecipient() final = default;
113 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothA2dpSourceDeathRecipient);
114
OnRemoteDied(const wptr<IRemoteObject> & remote)115 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
116 {
117 HILOGI("enter");
118 a2dpSrcDeath_.proxy_->AsObject()->RemoveDeathRecipient(a2dpSrcDeath_.deathRecipient_);
119 a2dpSrcDeath_.proxy_ = nullptr;
120
121 // Re-obtain the proxy and register the observer.
122 sleep(GET_PROXY_SLEEP_SEC);
123 a2dpSrcDeath_.GetA2dpSrcProxy();
124 if (a2dpSrcDeath_.proxy_ == nullptr) {
125 HILOGE("proxy reset failed");
126 return;
127 }
128 if (a2dpSrcDeath_.deathRecipient_ == nullptr || a2dpSrcDeath_.observerImp_ == nullptr) {
129 HILOGE("deathRecipient_ or observerImp_ is null");
130 return;
131 }
132 a2dpSrcDeath_.proxy_->AsObject()->AddDeathRecipient(a2dpSrcDeath_.deathRecipient_);
133 a2dpSrcDeath_.proxy_->RegisterObserver(a2dpSrcDeath_.observerImp_);
134 }
135
136 private:
137 A2dpSource::impl &a2dpSrcDeath_;
138 };
139
impl()140 A2dpSource::impl::impl()
141 {
142 HILOGI("start");
143 GetA2dpSrcProxy();
144 if (proxy_ == nullptr) {
145 HILOGE("get proxy_ failed");
146 return;
147 }
148 deathRecipient_ = new BluetoothA2dpSourceDeathRecipient(*this);
149 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
150
151 observerImp_ = new (std::nothrow) BluetoothA2dpSourceObserverImp(*this);
152 if (observerImp_ == nullptr) {
153 HILOGE("get proxy_observerImp_ failed");
154 return;
155 }
156 proxy_->RegisterObserver(observerImp_);
157 };
158
~impl()159 A2dpSource::impl::~impl()
160 {
161 HILOGI("start");
162 if (proxy_ != nullptr) {
163 proxy_->DeregisterObserver(observerImp_);
164 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
165 }
166 }
167
GetA2dpSrcProxy()168 void A2dpSource::impl::GetA2dpSrcProxy()
169 {
170 HILOGI("start");
171 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
172 if (!samgr) {
173 HILOGE("error: no samgr");
174 return;
175 }
176
177 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
178 if (!hostRemote) {
179 HILOGE("failed: no hostRemote");
180 return;
181 }
182
183 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
184 if (!hostProxy) {
185 HILOGE("error: host no proxy");
186 return;
187 }
188
189 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_A2DP_SRC);
190 if (!remote) {
191 HILOGE("error: no remote");
192 return;
193 }
194
195 proxy_ = iface_cast<IBluetoothA2dpSrc>(remote);
196 if (!proxy_) {
197 HILOGE("error: no proxy");
198 return;
199 }
200 }
201
A2dpSource()202 A2dpSource::A2dpSource()
203 {
204 pimpl = std::make_unique<impl>();
205 if (!pimpl) {
206 HILOGI("fails: no pimpl");
207 }
208 }
209
~A2dpSource()210 A2dpSource::~A2dpSource()
211 {
212 HILOGI("start");
213 }
214
RegisterObserver(A2dpSourceObserver * observer)215 void A2dpSource::RegisterObserver(A2dpSourceObserver *observer)
216 {
217 HILOGI("enter");
218 std::shared_ptr<A2dpSourceObserver> pointer(observer, [](A2dpSourceObserver *) {});
219 pimpl->observers_.Register(pointer);
220 }
221
DeregisterObserver(A2dpSourceObserver * observer)222 void A2dpSource::DeregisterObserver(A2dpSourceObserver *observer)
223 {
224 HILOGI("enter");
225 std::shared_ptr<A2dpSourceObserver> pointer(observer, [](A2dpSourceObserver *) {});
226 pimpl->observers_.Deregister(pointer);
227 }
228
GetDevicesByStates(std::vector<int> states) const229 std::vector<BluetoothRemoteDevice> A2dpSource::GetDevicesByStates(std::vector<int> states) const
230 {
231 HILOGI("enter");
232
233 std::vector<BluetoothRemoteDevice> devices;
234
235 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
236 std::vector<int32_t> convertStates;
237 for (auto state : states) {
238 convertStates.push_back(static_cast<int32_t>(state));
239 }
240 std::vector<RawAddress> rawAddrs;
241 rawAddrs = pimpl->proxy_->GetDevicesByStates(convertStates);
242
243 for (auto rawAddr : rawAddrs) {
244 BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
245 devices.push_back(device);
246 }
247 }
248
249 return devices;
250 }
251
GetDeviceState(const BluetoothRemoteDevice & device) const252 int A2dpSource::GetDeviceState(const BluetoothRemoteDevice &device) const
253 {
254 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
255
256 if (!device.IsValidBluetoothRemoteDevice()) {
257 HILOGI("input parameter error.");
258 return RET_BAD_STATUS;
259 }
260
261 int ret = RET_NO_ERROR;
262 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
263 ret = pimpl->proxy_->GetDeviceState(RawAddress(device.GetDeviceAddr()));
264 } else {
265 HILOGI("proxy or bt disable.");
266 return RET_BAD_STATUS;
267 }
268 HILOGI("state: %{public}d", ret);
269 return ret;
270 }
271
GetPlayingState(const BluetoothRemoteDevice & device) const272 int32_t A2dpSource::GetPlayingState(const BluetoothRemoteDevice &device) const
273 {
274 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
275
276 if (!device.IsValidBluetoothRemoteDevice()) {
277 HILOGI("input parameter error.");
278 return RET_BAD_STATUS;
279 }
280
281 int ret = RET_NO_ERROR;
282 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
283 pimpl->proxy_->GetPlayingState(RawAddress(device.GetDeviceAddr()), ret);
284 } else {
285 HILOGI("proxy or bt disable.");
286 return RET_BAD_STATUS;
287 }
288 HILOGI("state: %{public}d", ret);
289 return ret;
290 }
291
GetPlayingState(const BluetoothRemoteDevice & device,int & state) const292 int32_t A2dpSource::GetPlayingState(const BluetoothRemoteDevice &device, int &state) const
293 {
294 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
295
296 if (!device.IsValidBluetoothRemoteDevice()) {
297 HILOGI("input parameter error.");
298 return BT_ERR_INVALID_PARAM;
299 }
300 if (!IS_BT_ENABLED()) {
301 HILOGE("bluetooth if off.");
302 return BT_ERR_INVALID_STATE;
303 }
304 if (pimpl->proxy_ != nullptr) {
305 return pimpl->proxy_->GetPlayingState(RawAddress(device.GetDeviceAddr()), state);
306 } else {
307 HILOGI("proxy is nullptr.");
308 return BT_ERR_INTERNAL_ERROR;
309 }
310 }
311
Connect(const BluetoothRemoteDevice & device)312 int32_t A2dpSource::Connect(const BluetoothRemoteDevice &device)
313 {
314 HILOGI("a2dp connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
315
316 if (!device.IsValidBluetoothRemoteDevice()) {
317 HILOGI("input parameter error.");
318 return BT_ERR_INVALID_PARAM;
319 }
320 if (!IS_BT_ENABLED()) {
321 HILOGE("bluetooth if off.");
322 return BT_ERR_INVALID_STATE;
323 }
324
325 int cod = 0;
326 int32_t err = device.GetDeviceClass(cod);
327 if (err != BT_SUCCESS) {
328 HILOGE("GetDeviceClass Failed.");
329 return BT_ERR_INTERNAL_ERROR;
330 }
331 BluetoothDeviceClass devClass = BluetoothDeviceClass(cod);
332 if (!devClass.IsProfileSupported(BluetoothDevice::PROFILE_A2DP)) {
333 HILOGE("a2dp connect failed. The remote device does not support A2DP service.");
334 return BT_ERR_INTERNAL_ERROR;
335 }
336
337 if (pimpl->proxy_ != nullptr) {
338 return pimpl->proxy_->Connect(RawAddress(device.GetDeviceAddr()));
339 } else {
340 HILOGI("a2dp connect failed. proxy_ is nullptr.");
341 return BT_ERR_INTERNAL_ERROR;
342 }
343 }
344
Disconnect(const BluetoothRemoteDevice & device)345 int32_t A2dpSource::Disconnect(const BluetoothRemoteDevice &device)
346 {
347 HILOGI("a2dp disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
348 if (!device.IsValidBluetoothRemoteDevice()) {
349 HILOGI("input parameter error.");
350 return BT_ERR_INVALID_PARAM;
351 }
352 if (!IS_BT_ENABLED()) {
353 HILOGE("bluetooth if off.");
354 return BT_ERR_INVALID_STATE;
355 }
356
357 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
358 return pimpl->proxy_->Disconnect(RawAddress(device.GetDeviceAddr()));
359 } else {
360 HILOGI("a2dp disconnect failed. proxy_ is nullptr.");
361 return BT_ERR_INTERNAL_ERROR;
362 }
363 }
364
GetProfile()365 A2dpSource *A2dpSource::GetProfile()
366 {
367 HILOGI("enter");
368 static A2dpSource service;
369 return &service;
370 }
371
SetActiveSinkDevice(const BluetoothRemoteDevice & device)372 int A2dpSource::SetActiveSinkDevice(const BluetoothRemoteDevice &device)
373 {
374 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
375
376 if (!device.IsValidBluetoothRemoteDevice()) {
377 HILOGI("input parameter error.");
378 return RET_BAD_PARAM;
379 }
380
381 int ret = RET_NO_ERROR;
382 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
383 ret = pimpl->proxy_->SetActiveSinkDevice(RawAddress(device.GetDeviceAddr()));
384 } else {
385 HILOGI("proxy or bt disable.");
386 return RET_BAD_STATUS;
387 }
388
389 return ret;
390 }
391
GetActiveSinkDevice() const392 const BluetoothRemoteDevice &A2dpSource::GetActiveSinkDevice() const
393 {
394 HILOGI("enter");
395
396 BluetoothRawAddress rawAddress;
397 static BluetoothRemoteDevice deviceInfo;
398
399 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
400 pimpl->proxy_->GetActiveSinkDevice();
401 }
402
403 deviceInfo = BluetoothRemoteDevice(rawAddress.GetAddress(), 0);
404 return deviceInfo;
405 }
406
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)407 bool A2dpSource::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
408 {
409 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
410
411 if ((!device.IsValidBluetoothRemoteDevice()) || (((int)BTStrategyType::CONNECTION_ALLOWED != strategy) &&
412 ((int)BTStrategyType::CONNECTION_FORBIDDEN != strategy))) {
413 HILOGI("input parameter error.");
414 return false;
415 }
416
417 int ret = RET_NO_ERROR;
418 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
419 ret = pimpl->proxy_->SetConnectStrategy(RawAddress(device.GetDeviceAddr()), strategy);
420 } else {
421 HILOGI("proxy or bt disable.");
422 return false;
423 }
424
425 return (RET_NO_ERROR == ret);
426 }
427
GetConnectStrategy(const BluetoothRemoteDevice & device) const428 int A2dpSource::GetConnectStrategy(const BluetoothRemoteDevice &device) const
429 {
430 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
431
432 if (!device.IsValidBluetoothRemoteDevice()) {
433 HILOGI("input parameter error.");
434 return RET_BAD_PARAM;
435 }
436
437 int ret = RET_NO_ERROR;
438 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
439 ret = pimpl->proxy_->GetConnectStrategy(RawAddress(device.GetDeviceAddr()));
440 } else {
441 HILOGI("proxy or bt disable.");
442 return RET_BAD_STATUS;
443 }
444
445 return ret;
446 }
447
GetCodecStatus(const BluetoothRemoteDevice & device) const448 A2dpCodecStatus A2dpSource::GetCodecStatus(const BluetoothRemoteDevice &device) const
449 {
450 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
451
452 A2dpCodecStatus ret;
453 BluetoothA2dpCodecStatus codecStatus;
454 A2dpCodecInfo serviceInfo;
455
456 if (!device.IsValidBluetoothRemoteDevice()) {
457 HILOGI("input parameter error.");
458 return ret;
459 }
460
461 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
462 codecStatus = pimpl->proxy_->GetCodecStatus(RawAddress(device.GetDeviceAddr()));
463 }
464
465 ret.codecInfo.codecType = codecStatus.codecInfo.codecType;
466 ret.codecInfo.sampleRate = codecStatus.codecInfo.sampleRate;
467 ret.codecInfo.channelMode = codecStatus.codecInfo.channelMode;
468 ret.codecInfo.codecPriority = codecStatus.codecInfo.codecPriority;
469 ret.codecInfo.bitsPerSample = codecStatus.codecInfo.bitsPerSample;
470
471 for (auto it = codecStatus.codecInfoConfirmCap.begin(); it != codecStatus.codecInfoConfirmCap.end(); it++) {
472 serviceInfo.codecType = it->codecType;
473 serviceInfo.sampleRate = it->sampleRate;
474 serviceInfo.channelMode = it->channelMode;
475 serviceInfo.codecPriority = it->codecPriority;
476 serviceInfo.bitsPerSample = it->bitsPerSample;
477 ret.codecInfoConfirmedCap.push_back(serviceInfo);
478 }
479
480 for (auto it = codecStatus.codecInfoLocalCap.begin(); it != codecStatus.codecInfoLocalCap.end(); it++) {
481 serviceInfo.codecType = it->codecType;
482 serviceInfo.sampleRate = it->sampleRate;
483 serviceInfo.channelMode = it->channelMode;
484 serviceInfo.codecPriority = it->codecPriority;
485 serviceInfo.bitsPerSample = it->bitsPerSample;
486 ret.codecInfoLocalCap.push_back(serviceInfo);
487 }
488 return ret;
489 }
490
SetCodecPreference(const BluetoothRemoteDevice & device,const A2dpCodecInfo & info)491 int A2dpSource::SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info)
492 {
493 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
494
495 int ret = RET_NO_ERROR;
496 if (!device.IsValidBluetoothRemoteDevice()) {
497 HILOGI("input parameter error.");
498 ret = RET_BAD_PARAM;
499 return ret;
500 }
501 BluetoothA2dpCodecInfo serviceInfo;
502
503 serviceInfo.codecType = info.codecType;
504 serviceInfo.sampleRate = info.sampleRate;
505 serviceInfo.channelMode = info.channelMode;
506 serviceInfo.bitsPerSample = info.bitsPerSample;
507 serviceInfo.codecPriority = info.codecPriority;
508 serviceInfo.codecSpecific1 = info.codecSpecific1;
509 serviceInfo.codecSpecific2 = info.codecSpecific2;
510 serviceInfo.codecSpecific3 = info.codecSpecific3;
511 serviceInfo.codecSpecific4 = info.codecSpecific4;
512
513 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
514 ret = pimpl->proxy_->SetCodecPreference(RawAddress(device.GetDeviceAddr()), serviceInfo);
515 } else {
516 HILOGI("proxy or bt disable.");
517 return RET_BAD_STATUS;
518 }
519
520 return ret;
521 }
522
SwitchOptionalCodecs(const BluetoothRemoteDevice & device,bool isEnable)523 void A2dpSource::SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable)
524 {
525 HILOGI("enter");
526
527 if (!device.IsValidBluetoothRemoteDevice()) {
528 HILOGI("input parameter error.");
529 return;
530 }
531
532 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
533 pimpl->proxy_->SwitchOptionalCodecs(RawAddress(device.GetDeviceAddr()), isEnable);
534 } else {
535 HILOGI("proxy or bt disable.");
536 return;
537 }
538 }
539
GetOptionalCodecsSupportState(const BluetoothRemoteDevice & device) const540 int A2dpSource::GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const
541 {
542 HILOGI("enter");
543
544 if (!device.IsValidBluetoothRemoteDevice()) {
545 HILOGI("input parameter error.");
546 return RET_BAD_PARAM;
547 }
548
549 int ret = RET_NO_ERROR;
550 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
551 pimpl->proxy_->GetOptionalCodecsSupportState(RawAddress(device.GetDeviceAddr()));
552 } else {
553 HILOGI("proxy or bt disable.");
554 return RET_BAD_STATUS;
555 }
556
557 return ret;
558 }
559
StartPlaying(const BluetoothRemoteDevice & device)560 int A2dpSource::StartPlaying(const BluetoothRemoteDevice &device)
561 {
562 HILOGI("enter");
563
564 if (!device.IsValidBluetoothRemoteDevice()) {
565 HILOGI("input parameter error.");
566 return RET_BAD_PARAM;
567 }
568
569 int ret = RET_NO_ERROR;
570 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
571 ret = pimpl->proxy_->StartPlaying(RawAddress(device.GetDeviceAddr()));
572 } else {
573 HILOGI("proxy or bt disable.");
574 return RET_BAD_STATUS;
575 }
576
577 return ret;
578 }
579
SuspendPlaying(const BluetoothRemoteDevice & device)580 int A2dpSource::SuspendPlaying(const BluetoothRemoteDevice &device)
581 {
582 HILOGI("enter");
583
584 if (!device.IsValidBluetoothRemoteDevice()) {
585 HILOGI("input parameter error.");
586 return RET_BAD_PARAM;
587 }
588
589 int ret = RET_NO_ERROR;
590 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
591 ret = pimpl->proxy_->SuspendPlaying(RawAddress(device.GetDeviceAddr()));
592 } else {
593 HILOGI("proxy or bt disable.");
594 return RET_BAD_STATUS;
595 }
596
597 return ret;
598 }
599
StopPlaying(const BluetoothRemoteDevice & device)600 int A2dpSource::StopPlaying(const BluetoothRemoteDevice &device)
601 {
602 HILOGI("enter");
603
604 if (!device.IsValidBluetoothRemoteDevice()) {
605 HILOGI("input parameter error.");
606 return RET_BAD_PARAM;
607 }
608
609 int ret = RET_NO_ERROR;
610 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
611 ret = pimpl->proxy_->StopPlaying(RawAddress(device.GetDeviceAddr()));
612 } else {
613 HILOGI("proxy or bt disable.");
614 return RET_BAD_STATUS;
615 }
616
617 return ret;
618 }
619
WriteFrame(const uint8_t * data,uint32_t size)620 int A2dpSource::WriteFrame(const uint8_t *data, uint32_t size)
621 {
622 HILOGI("enter");
623 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
624 return pimpl->proxy_->WriteFrame(data, size);
625 } else {
626 HILOGI("proxy or bt disable.");
627 return RET_BAD_STATUS;
628 }
629 return RET_BAD_STATUS;
630 }
631
GetRenderPosition(uint16_t & delayValue,uint16_t & sendDataSize,uint32_t & timeStamp)632 void A2dpSource::GetRenderPosition(uint16_t &delayValue, uint16_t &sendDataSize, uint32_t &timeStamp)
633 {
634 HILOGI("enter");
635 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
636 pimpl->proxy_->GetRenderPosition(delayValue, sendDataSize, timeStamp);
637 } else {
638 HILOGI("proxy or bt disable.");
639 return;
640 }
641 return;
642 }
643
644 } // namespace Bluetooth
645 } // namespace OHOS