195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
495489c19Sopenharmony_ci * you may not use this file except in compliance with the License.
595489c19Sopenharmony_ci * You may obtain a copy of the License at
695489c19Sopenharmony_ci *
795489c19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
895489c19Sopenharmony_ci *
995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and
1395489c19Sopenharmony_ci * limitations under the License.
1495489c19Sopenharmony_ci */
1595489c19Sopenharmony_ci
1695489c19Sopenharmony_ci#include "bluetooth_a2dp_src.h"
1795489c19Sopenharmony_ci#include <unistd.h>
1895489c19Sopenharmony_ci#include "bluetooth_a2dp_codec.h"
1995489c19Sopenharmony_ci#include "bluetooth_a2dp_src_proxy.h"
2095489c19Sopenharmony_ci#include "bluetooth_a2dp_src_observer_stub.h"
2195489c19Sopenharmony_ci#include "bluetooth_device.h"
2295489c19Sopenharmony_ci#include "bluetooth_host_proxy.h"
2395489c19Sopenharmony_ci#include "bluetooth_profile_manager.h"
2495489c19Sopenharmony_ci#include "bluetooth_observer_list.h"
2595489c19Sopenharmony_ci#include "raw_address.h"
2695489c19Sopenharmony_ci#include "bluetooth_def.h"
2795489c19Sopenharmony_ci#include "bluetooth_host.h"
2895489c19Sopenharmony_ci
2995489c19Sopenharmony_ci#include "bluetooth_log.h"
3095489c19Sopenharmony_ci#include "bluetooth_utils.h"
3195489c19Sopenharmony_ci#include "iservice_registry.h"
3295489c19Sopenharmony_ci#include "system_ability_definition.h"
3395489c19Sopenharmony_ci
3495489c19Sopenharmony_cinamespace OHOS {
3595489c19Sopenharmony_cinamespace Bluetooth {
3695489c19Sopenharmony_ciusing namespace OHOS::bluetooth;
3795489c19Sopenharmony_cistd::mutex g_a2dpProxyMutex;
3895489c19Sopenharmony_cistruct A2dpSource::impl {
3995489c19Sopenharmony_ci    impl();
4095489c19Sopenharmony_ci    ~impl();
4195489c19Sopenharmony_ci    BluetoothObserverList<A2dpSourceObserver> observers_;
4295489c19Sopenharmony_ci    class BluetoothA2dpSourceObserverImp;
4395489c19Sopenharmony_ci    sptr<BluetoothA2dpSourceObserverImp> observerImp_ = nullptr;
4495489c19Sopenharmony_ci    int32_t profileRegisterId = 0;
4595489c19Sopenharmony_ci};
4695489c19Sopenharmony_ci
4795489c19Sopenharmony_ciclass A2dpSource::impl::BluetoothA2dpSourceObserverImp : public BluetoothA2dpSrcObserverStub {
4895489c19Sopenharmony_cipublic:
4995489c19Sopenharmony_ci    explicit BluetoothA2dpSourceObserverImp(A2dpSource::impl &a2dpSource) : a2dpSource_(a2dpSource) {};
5095489c19Sopenharmony_ci    ~BluetoothA2dpSourceObserverImp() override{};
5195489c19Sopenharmony_ci
5295489c19Sopenharmony_ci    void Register(std::shared_ptr<A2dpSourceObserver> &observer)
5395489c19Sopenharmony_ci    {
5495489c19Sopenharmony_ci        HILOGI("enter");
5595489c19Sopenharmony_ci        a2dpSource_.observers_.Register(observer);
5695489c19Sopenharmony_ci    }
5795489c19Sopenharmony_ci
5895489c19Sopenharmony_ci    void Deregister(std::shared_ptr<A2dpSourceObserver> &observer)
5995489c19Sopenharmony_ci    {
6095489c19Sopenharmony_ci        HILOGI("enter");
6195489c19Sopenharmony_ci        a2dpSource_.observers_.Deregister(observer);
6295489c19Sopenharmony_ci    }
6395489c19Sopenharmony_ci
6495489c19Sopenharmony_ci    void OnConnectionStateChanged(const RawAddress &device, int state, int cause) override
6595489c19Sopenharmony_ci    {
6695489c19Sopenharmony_ci        HILOGD("a2dpSrc conn state, device: %{public}s, state: %{public}s, cause: %{public}d",
6795489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), GetProfileConnStateName(state).c_str(), cause);
6895489c19Sopenharmony_ci        a2dpSource_.observers_.ForEach([device, state, cause](std::shared_ptr<A2dpSourceObserver> observer) {
6995489c19Sopenharmony_ci            observer->OnConnectionStateChanged(BluetoothRemoteDevice(device.GetAddress(), 0), state, cause);
7095489c19Sopenharmony_ci        });
7195489c19Sopenharmony_ci    }
7295489c19Sopenharmony_ci
7395489c19Sopenharmony_ci    void OnPlayingStatusChanged(const RawAddress &device, int playingState, int error) override
7495489c19Sopenharmony_ci    {
7595489c19Sopenharmony_ci        HILOGI("device: %{public}s, playingState: %{public}d, error: %{public}d",
7695489c19Sopenharmony_ci            GetEncryptAddr(device.GetAddress()).c_str(), playingState, error);
7795489c19Sopenharmony_ci        a2dpSource_.observers_.ForEach([device, playingState, error](std::shared_ptr<A2dpSourceObserver> observer) {
7895489c19Sopenharmony_ci            observer->OnPlayingStatusChanged(BluetoothRemoteDevice(device.GetAddress(), 0), playingState, error);
7995489c19Sopenharmony_ci        });
8095489c19Sopenharmony_ci    }
8195489c19Sopenharmony_ci
8295489c19Sopenharmony_ci    void OnConfigurationChanged(const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error) override
8395489c19Sopenharmony_ci    {
8495489c19Sopenharmony_ci        HILOGD("device: %{public}s, error: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), error);
8595489c19Sopenharmony_ci        a2dpSource_.observers_.ForEach([device, info, error](std::shared_ptr<A2dpSourceObserver> observer) {
8695489c19Sopenharmony_ci            A2dpCodecInfo codecInfo;
8795489c19Sopenharmony_ci
8895489c19Sopenharmony_ci            codecInfo.bitsPerSample = info.bitsPerSample;
8995489c19Sopenharmony_ci            codecInfo.channelMode = info.channelMode;
9095489c19Sopenharmony_ci            codecInfo.codecPriority = info.codecPriority;
9195489c19Sopenharmony_ci            codecInfo.codecType = info.codecType;
9295489c19Sopenharmony_ci            codecInfo.sampleRate = info.sampleRate;
9395489c19Sopenharmony_ci
9495489c19Sopenharmony_ci            observer->OnConfigurationChanged(BluetoothRemoteDevice(device.GetAddress(), 0), codecInfo, error);
9595489c19Sopenharmony_ci        });
9695489c19Sopenharmony_ci    };
9795489c19Sopenharmony_ci
9895489c19Sopenharmony_ci    void OnMediaStackChanged(const RawAddress &device, int action) override
9995489c19Sopenharmony_ci    {
10095489c19Sopenharmony_ci        HILOGI("device: %{public}s, action: %{public}s",
10195489c19Sopenharmony_ci            GET_ENCRYPT_RAW_ADDR(device), GetUpdateOutputStackActionName(action).c_str());
10295489c19Sopenharmony_ci        a2dpSource_.observers_.ForEach([device, action](std::shared_ptr<A2dpSourceObserver> observer) {
10395489c19Sopenharmony_ci            observer->OnMediaStackChanged(BluetoothRemoteDevice(device.GetAddress(), 0), action);
10495489c19Sopenharmony_ci        });
10595489c19Sopenharmony_ci    }
10695489c19Sopenharmony_ci
10795489c19Sopenharmony_ci    void OnVirtualDeviceChanged(int action, std::string address) override
10895489c19Sopenharmony_ci    {
10995489c19Sopenharmony_ci        HILOGI("device: %{public}s, action: %{public}d", GetEncryptAddr(address).c_str(), action);
11095489c19Sopenharmony_ci        a2dpSource_.observers_.ForEach([action, address](std::shared_ptr<A2dpSourceObserver> observer) {
11195489c19Sopenharmony_ci            observer->OnVirtualDeviceChanged(action, address);
11295489c19Sopenharmony_ci        });
11395489c19Sopenharmony_ci    }
11495489c19Sopenharmony_ciprivate:
11595489c19Sopenharmony_ci    A2dpSource::impl &a2dpSource_;
11695489c19Sopenharmony_ci    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothA2dpSourceObserverImp);
11795489c19Sopenharmony_ci};
11895489c19Sopenharmony_ci
11995489c19Sopenharmony_ciA2dpSource::impl::impl()
12095489c19Sopenharmony_ci{
12195489c19Sopenharmony_ci    observerImp_ = new (std::nothrow) BluetoothA2dpSourceObserverImp(*this);
12295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(observerImp_ != nullptr, "observerImp_ is nullptr");
12395489c19Sopenharmony_ci    profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_A2DP_SRC,
12495489c19Sopenharmony_ci        [this](sptr<IRemoteObject> remote) {
12595489c19Sopenharmony_ci        sptr<IBluetoothA2dpSrc> proxy = iface_cast<IBluetoothA2dpSrc>(remote);
12695489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
12795489c19Sopenharmony_ci        proxy->RegisterObserver(observerImp_);
12895489c19Sopenharmony_ci    });
12995489c19Sopenharmony_ci};
13095489c19Sopenharmony_ci
13195489c19Sopenharmony_ciA2dpSource::impl::~impl()
13295489c19Sopenharmony_ci{
13395489c19Sopenharmony_ci    HILOGD("start");
13495489c19Sopenharmony_ci    BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
13595489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
13695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
13795489c19Sopenharmony_ci    proxy->DeregisterObserver(observerImp_);
13895489c19Sopenharmony_ci}
13995489c19Sopenharmony_ci
14095489c19Sopenharmony_ciA2dpSource::A2dpSource()
14195489c19Sopenharmony_ci{
14295489c19Sopenharmony_ci    pimpl = std::make_unique<impl>();
14395489c19Sopenharmony_ci    if (!pimpl) {
14495489c19Sopenharmony_ci        HILOGE("fails: no pimpl");
14595489c19Sopenharmony_ci    }
14695489c19Sopenharmony_ci}
14795489c19Sopenharmony_ci
14895489c19Sopenharmony_ciA2dpSource::~A2dpSource()
14995489c19Sopenharmony_ci{
15095489c19Sopenharmony_ci    HILOGD("start");
15195489c19Sopenharmony_ci}
15295489c19Sopenharmony_ci
15395489c19Sopenharmony_civoid A2dpSource::RegisterObserver(std::shared_ptr<A2dpSourceObserver> observer)
15495489c19Sopenharmony_ci{
15595489c19Sopenharmony_ci    HILOGD("enter");
15695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
15795489c19Sopenharmony_ci    pimpl->observers_.Register(observer);
15895489c19Sopenharmony_ci}
15995489c19Sopenharmony_ci
16095489c19Sopenharmony_civoid A2dpSource::DeregisterObserver(std::shared_ptr<A2dpSourceObserver> observer)
16195489c19Sopenharmony_ci{
16295489c19Sopenharmony_ci    HILOGD("enter");
16395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
16495489c19Sopenharmony_ci    pimpl->observers_.Deregister(observer);
16595489c19Sopenharmony_ci}
16695489c19Sopenharmony_ci
16795489c19Sopenharmony_ciint A2dpSource::GetDevicesByStates(const std::vector<int> &states, std::vector<BluetoothRemoteDevice> &devices) const
16895489c19Sopenharmony_ci{
16995489c19Sopenharmony_ci    HILOGI("enter");
17095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
17195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
17295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
17395489c19Sopenharmony_ci    }
17495489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
17595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
17695489c19Sopenharmony_ci
17795489c19Sopenharmony_ci    std::vector<int32_t> convertStates;
17895489c19Sopenharmony_ci    for (auto state : states) {
17995489c19Sopenharmony_ci        convertStates.push_back(static_cast<int32_t>(state));
18095489c19Sopenharmony_ci    }
18195489c19Sopenharmony_ci
18295489c19Sopenharmony_ci    std::vector<RawAddress> rawAddrs;
18395489c19Sopenharmony_ci    int ret = proxy->GetDevicesByStates(convertStates, rawAddrs);
18495489c19Sopenharmony_ci    if (ret != BT_NO_ERROR) {
18595489c19Sopenharmony_ci        HILOGE("GetDevicesByStates return error.");
18695489c19Sopenharmony_ci        return ret;
18795489c19Sopenharmony_ci    }
18895489c19Sopenharmony_ci    for (auto rawAddr : rawAddrs) {
18995489c19Sopenharmony_ci        BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
19095489c19Sopenharmony_ci        devices.push_back(device);
19195489c19Sopenharmony_ci    }
19295489c19Sopenharmony_ci    return BT_NO_ERROR;
19395489c19Sopenharmony_ci}
19495489c19Sopenharmony_ci
19595489c19Sopenharmony_ciint A2dpSource::GetDeviceState(const BluetoothRemoteDevice &device, int &state) const
19695489c19Sopenharmony_ci{
19795489c19Sopenharmony_ci    HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
19895489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
19995489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
20095489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
20195489c19Sopenharmony_ci    }
20295489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
20395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
20495489c19Sopenharmony_ci
20595489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
20695489c19Sopenharmony_ci        HILOGE("input parameter error.");
20795489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
20895489c19Sopenharmony_ci    }
20995489c19Sopenharmony_ci
21095489c19Sopenharmony_ci    int ret = proxy->GetDeviceState(RawAddress(device.GetDeviceAddr()), state);
21195489c19Sopenharmony_ci    HILOGD("state: %{public}d", ret);
21295489c19Sopenharmony_ci    return ret;
21395489c19Sopenharmony_ci}
21495489c19Sopenharmony_ci
21595489c19Sopenharmony_ciint32_t A2dpSource::GetPlayingState(const BluetoothRemoteDevice &device) const
21695489c19Sopenharmony_ci{
21795489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
21895489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
21995489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
22095489c19Sopenharmony_ci        return RET_BAD_STATUS;
22195489c19Sopenharmony_ci    }
22295489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
22395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
22495489c19Sopenharmony_ci
22595489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
22695489c19Sopenharmony_ci        HILOGE("input parameter error.");
22795489c19Sopenharmony_ci        return RET_BAD_PARAM;
22895489c19Sopenharmony_ci    }
22995489c19Sopenharmony_ci
23095489c19Sopenharmony_ci    int ret = RET_NO_ERROR;
23195489c19Sopenharmony_ci    proxy->GetPlayingState(RawAddress(device.GetDeviceAddr()), ret);
23295489c19Sopenharmony_ci    HILOGI("state: %{public}d", ret);
23395489c19Sopenharmony_ci    return ret;
23495489c19Sopenharmony_ci}
23595489c19Sopenharmony_ci
23695489c19Sopenharmony_ciint32_t A2dpSource::GetPlayingState(const BluetoothRemoteDevice &device, int &state) const
23795489c19Sopenharmony_ci{
23895489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
23995489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
24095489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
24195489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
24295489c19Sopenharmony_ci    }
24395489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
24495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
24595489c19Sopenharmony_ci
24695489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
24795489c19Sopenharmony_ci        HILOGE("input parameter error.");
24895489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
24995489c19Sopenharmony_ci    }
25095489c19Sopenharmony_ci
25195489c19Sopenharmony_ci    return proxy->GetPlayingState(RawAddress(device.GetDeviceAddr()), state);
25295489c19Sopenharmony_ci}
25395489c19Sopenharmony_ci
25495489c19Sopenharmony_ciint32_t A2dpSource::Connect(const BluetoothRemoteDevice &device)
25595489c19Sopenharmony_ci{
25695489c19Sopenharmony_ci    HILOGI("a2dp connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
25795489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
25895489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
25995489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
26095489c19Sopenharmony_ci    }
26195489c19Sopenharmony_ci
26295489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
26395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
26495489c19Sopenharmony_ci
26595489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
26695489c19Sopenharmony_ci        HILOGE("input parameter error.");
26795489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
26895489c19Sopenharmony_ci    }
26995489c19Sopenharmony_ci    return proxy->Connect(RawAddress(device.GetDeviceAddr()));
27095489c19Sopenharmony_ci}
27195489c19Sopenharmony_ci
27295489c19Sopenharmony_ciint32_t A2dpSource::Disconnect(const BluetoothRemoteDevice &device)
27395489c19Sopenharmony_ci{
27495489c19Sopenharmony_ci    HILOGI("a2dp disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
27595489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
27695489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
27795489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
27895489c19Sopenharmony_ci    }
27995489c19Sopenharmony_ci
28095489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
28195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
28295489c19Sopenharmony_ci
28395489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
28495489c19Sopenharmony_ci        HILOGE("input parameter error.");
28595489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
28695489c19Sopenharmony_ci    }
28795489c19Sopenharmony_ci
28895489c19Sopenharmony_ci    return proxy->Disconnect(RawAddress(device.GetDeviceAddr()));
28995489c19Sopenharmony_ci}
29095489c19Sopenharmony_ci
29195489c19Sopenharmony_ciA2dpSource *A2dpSource::GetProfile()
29295489c19Sopenharmony_ci{
29395489c19Sopenharmony_ci    HILOGD("enter");
29495489c19Sopenharmony_ci#ifdef DTFUZZ_TEST
29595489c19Sopenharmony_ci    static BluetoothNoDestructor<A2dpSource> service;
29695489c19Sopenharmony_ci    return service.get();
29795489c19Sopenharmony_ci#else
29895489c19Sopenharmony_ci    static A2dpSource service;
29995489c19Sopenharmony_ci    return &service;
30095489c19Sopenharmony_ci#endif
30195489c19Sopenharmony_ci}
30295489c19Sopenharmony_ci
30395489c19Sopenharmony_ciint A2dpSource::SetActiveSinkDevice(const BluetoothRemoteDevice &device)
30495489c19Sopenharmony_ci{
30595489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
30695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
30795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
30895489c19Sopenharmony_ci        return RET_BAD_STATUS;
30995489c19Sopenharmony_ci    }
31095489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
31195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
31295489c19Sopenharmony_ci
31395489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
31495489c19Sopenharmony_ci        HILOGE("input parameter error.");
31595489c19Sopenharmony_ci        return RET_BAD_PARAM;
31695489c19Sopenharmony_ci    }
31795489c19Sopenharmony_ci
31895489c19Sopenharmony_ci    return proxy->SetActiveSinkDevice(RawAddress(device.GetDeviceAddr()));
31995489c19Sopenharmony_ci}
32095489c19Sopenharmony_ci
32195489c19Sopenharmony_ciconst BluetoothRemoteDevice &A2dpSource::GetActiveSinkDevice() const
32295489c19Sopenharmony_ci{
32395489c19Sopenharmony_ci    HILOGI("enter");
32495489c19Sopenharmony_ci    static BluetoothRemoteDevice deviceInfo;
32595489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
32695489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
32795489c19Sopenharmony_ci        return deviceInfo;
32895489c19Sopenharmony_ci    }
32995489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
33095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, deviceInfo, "failed: no proxy");
33195489c19Sopenharmony_ci
33295489c19Sopenharmony_ci    BluetoothRawAddress rawAddress = proxy->GetActiveSinkDevice();
33395489c19Sopenharmony_ci    deviceInfo = BluetoothRemoteDevice(rawAddress.GetAddress(), 0);
33495489c19Sopenharmony_ci    return deviceInfo;
33595489c19Sopenharmony_ci}
33695489c19Sopenharmony_ci
33795489c19Sopenharmony_ciint A2dpSource::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
33895489c19Sopenharmony_ci{
33995489c19Sopenharmony_ci    HILOGI("device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
34095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
34195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
34295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
34395489c19Sopenharmony_ci    }
34495489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
34595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
34695489c19Sopenharmony_ci
34795489c19Sopenharmony_ci    if ((!device.IsValidBluetoothRemoteDevice()) || (
34895489c19Sopenharmony_ci        (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
34995489c19Sopenharmony_ci        (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
35095489c19Sopenharmony_ci        HILOGI("input parameter error.");
35195489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
35295489c19Sopenharmony_ci    }
35395489c19Sopenharmony_ci
35495489c19Sopenharmony_ci    return proxy->SetConnectStrategy(RawAddress(device.GetDeviceAddr()), strategy);
35595489c19Sopenharmony_ci}
35695489c19Sopenharmony_ci
35795489c19Sopenharmony_ciint A2dpSource::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
35895489c19Sopenharmony_ci{
35995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
36095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
36195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
36295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
36395489c19Sopenharmony_ci    }
36495489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
36595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
36695489c19Sopenharmony_ci
36795489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
36895489c19Sopenharmony_ci        HILOGI("input parameter error.");
36995489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
37095489c19Sopenharmony_ci    }
37195489c19Sopenharmony_ci
37295489c19Sopenharmony_ci    return proxy->GetConnectStrategy(RawAddress(device.GetDeviceAddr()), strategy);
37395489c19Sopenharmony_ci}
37495489c19Sopenharmony_ci
37595489c19Sopenharmony_ciA2dpCodecStatus A2dpSource::GetCodecStatus(const BluetoothRemoteDevice &device) const
37695489c19Sopenharmony_ci{
37795489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
37895489c19Sopenharmony_ci    A2dpCodecStatus ret;
37995489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
38095489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
38195489c19Sopenharmony_ci        return ret;
38295489c19Sopenharmony_ci    }
38395489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
38495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, ret, "failed: no proxy");
38595489c19Sopenharmony_ci
38695489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
38795489c19Sopenharmony_ci        HILOGE("input parameter error.");
38895489c19Sopenharmony_ci        return ret;
38995489c19Sopenharmony_ci    }
39095489c19Sopenharmony_ci
39195489c19Sopenharmony_ci    BluetoothA2dpCodecStatus codecStatus = proxy->GetCodecStatus(RawAddress(device.GetDeviceAddr()));
39295489c19Sopenharmony_ci    ret.codecInfo.codecType = codecStatus.codecInfo.codecType;
39395489c19Sopenharmony_ci    ret.codecInfo.sampleRate = codecStatus.codecInfo.sampleRate;
39495489c19Sopenharmony_ci    ret.codecInfo.channelMode = codecStatus.codecInfo.channelMode;
39595489c19Sopenharmony_ci    ret.codecInfo.codecPriority = codecStatus.codecInfo.codecPriority;
39695489c19Sopenharmony_ci    ret.codecInfo.bitsPerSample = codecStatus.codecInfo.bitsPerSample;
39795489c19Sopenharmony_ci
39895489c19Sopenharmony_ci    A2dpCodecInfo serviceInfo;
39995489c19Sopenharmony_ci    for (auto it = codecStatus.codecInfoConfirmCap.begin(); it != codecStatus.codecInfoConfirmCap.end(); it++) {
40095489c19Sopenharmony_ci        serviceInfo.codecType = it->codecType;
40195489c19Sopenharmony_ci        serviceInfo.sampleRate = it->sampleRate;
40295489c19Sopenharmony_ci        serviceInfo.channelMode = it->channelMode;
40395489c19Sopenharmony_ci        serviceInfo.codecPriority = it->codecPriority;
40495489c19Sopenharmony_ci        serviceInfo.bitsPerSample = it->bitsPerSample;
40595489c19Sopenharmony_ci        ret.codecInfoConfirmedCap.push_back(serviceInfo);
40695489c19Sopenharmony_ci    }
40795489c19Sopenharmony_ci
40895489c19Sopenharmony_ci    for (auto it = codecStatus.codecInfoLocalCap.begin(); it != codecStatus.codecInfoLocalCap.end(); it++) {
40995489c19Sopenharmony_ci        serviceInfo.codecType = it->codecType;
41095489c19Sopenharmony_ci        serviceInfo.sampleRate = it->sampleRate;
41195489c19Sopenharmony_ci        serviceInfo.channelMode = it->channelMode;
41295489c19Sopenharmony_ci        serviceInfo.codecPriority = it->codecPriority;
41395489c19Sopenharmony_ci        serviceInfo.bitsPerSample = it->bitsPerSample;
41495489c19Sopenharmony_ci        ret.codecInfoLocalCap.push_back(serviceInfo);
41595489c19Sopenharmony_ci    }
41695489c19Sopenharmony_ci    return ret;
41795489c19Sopenharmony_ci}
41895489c19Sopenharmony_ci
41995489c19Sopenharmony_ciint A2dpSource::GetCodecPreference(const BluetoothRemoteDevice &device, A2dpCodecInfo &info)
42095489c19Sopenharmony_ci{
42195489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
42295489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
42395489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
42495489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
42595489c19Sopenharmony_ci    }
42695489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
42795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
42895489c19Sopenharmony_ci
42995489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
43095489c19Sopenharmony_ci        HILOGE("input parameter error.");
43195489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
43295489c19Sopenharmony_ci    }
43395489c19Sopenharmony_ci
43495489c19Sopenharmony_ci    BluetoothA2dpCodecInfo serviceInfo;
43595489c19Sopenharmony_ci    int ret = proxy->GetCodecPreference(RawAddress(device.GetDeviceAddr()), serviceInfo);
43695489c19Sopenharmony_ci    if (ret != BT_NO_ERROR) {
43795489c19Sopenharmony_ci        HILOGE("GetCodecPreference error.");
43895489c19Sopenharmony_ci        return ret;
43995489c19Sopenharmony_ci    }
44095489c19Sopenharmony_ci    info.codecType = serviceInfo.codecType;
44195489c19Sopenharmony_ci    info.sampleRate = serviceInfo.sampleRate;
44295489c19Sopenharmony_ci    info.channelMode = serviceInfo.channelMode;
44395489c19Sopenharmony_ci    info.bitsPerSample = serviceInfo.bitsPerSample;
44495489c19Sopenharmony_ci    return ret;
44595489c19Sopenharmony_ci}
44695489c19Sopenharmony_ci
44795489c19Sopenharmony_ciint A2dpSource::SetCodecPreference(const BluetoothRemoteDevice &device, const A2dpCodecInfo &info)
44895489c19Sopenharmony_ci{
44995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
45095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
45195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
45295489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
45395489c19Sopenharmony_ci    }
45495489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
45595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
45695489c19Sopenharmony_ci
45795489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
45895489c19Sopenharmony_ci        HILOGE("input parameter error.");
45995489c19Sopenharmony_ci        return BT_ERR_INVALID_PARAM;
46095489c19Sopenharmony_ci    }
46195489c19Sopenharmony_ci
46295489c19Sopenharmony_ci    BluetoothA2dpCodecInfo serviceInfo;
46395489c19Sopenharmony_ci    serviceInfo.codecType = info.codecType;
46495489c19Sopenharmony_ci    serviceInfo.sampleRate = info.sampleRate;
46595489c19Sopenharmony_ci    serviceInfo.channelMode = info.channelMode;
46695489c19Sopenharmony_ci    serviceInfo.bitsPerSample = info.bitsPerSample;
46795489c19Sopenharmony_ci    serviceInfo.codecPriority = info.codecPriority;
46895489c19Sopenharmony_ci    serviceInfo.codecSpecific1 = info.codecSpecific1;
46995489c19Sopenharmony_ci    serviceInfo.codecSpecific2 = info.codecSpecific2;
47095489c19Sopenharmony_ci    serviceInfo.codecSpecific3 = info.codecSpecific3;
47195489c19Sopenharmony_ci    serviceInfo.codecSpecific4 = info.codecSpecific4;
47295489c19Sopenharmony_ci
47395489c19Sopenharmony_ci    return proxy->SetCodecPreference(RawAddress(device.GetDeviceAddr()), serviceInfo);
47495489c19Sopenharmony_ci}
47595489c19Sopenharmony_ci
47695489c19Sopenharmony_civoid A2dpSource::SwitchOptionalCodecs(const BluetoothRemoteDevice &device, bool isEnable)
47795489c19Sopenharmony_ci{
47895489c19Sopenharmony_ci    HILOGI("enter");
47995489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
48095489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
48195489c19Sopenharmony_ci        return;
48295489c19Sopenharmony_ci    }
48395489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
48495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
48595489c19Sopenharmony_ci
48695489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
48795489c19Sopenharmony_ci        HILOGE("input parameter error.");
48895489c19Sopenharmony_ci        return;
48995489c19Sopenharmony_ci    }
49095489c19Sopenharmony_ci
49195489c19Sopenharmony_ci    proxy->SwitchOptionalCodecs(RawAddress(device.GetDeviceAddr()), isEnable);
49295489c19Sopenharmony_ci}
49395489c19Sopenharmony_ci
49495489c19Sopenharmony_ciint A2dpSource::GetOptionalCodecsSupportState(const BluetoothRemoteDevice &device) const
49595489c19Sopenharmony_ci{
49695489c19Sopenharmony_ci    HILOGI("enter");
49795489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
49895489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
49995489c19Sopenharmony_ci        return RET_BAD_STATUS;
50095489c19Sopenharmony_ci    }
50195489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
50295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
50395489c19Sopenharmony_ci
50495489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
50595489c19Sopenharmony_ci        HILOGE("input parameter error.");
50695489c19Sopenharmony_ci        return RET_BAD_PARAM;
50795489c19Sopenharmony_ci    }
50895489c19Sopenharmony_ci
50995489c19Sopenharmony_ci    return proxy->GetOptionalCodecsSupportState(RawAddress(device.GetDeviceAddr()));
51095489c19Sopenharmony_ci}
51195489c19Sopenharmony_ci
51295489c19Sopenharmony_ciint A2dpSource::StartPlaying(const BluetoothRemoteDevice &device)
51395489c19Sopenharmony_ci{
51495489c19Sopenharmony_ci    HILOGI("enter");
51595489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
51695489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
51795489c19Sopenharmony_ci        return RET_BAD_STATUS;
51895489c19Sopenharmony_ci    }
51995489c19Sopenharmony_ci
52095489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
52195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
52295489c19Sopenharmony_ci
52395489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
52495489c19Sopenharmony_ci        HILOGE("input parameter error.");
52595489c19Sopenharmony_ci        return RET_BAD_PARAM;
52695489c19Sopenharmony_ci    }
52795489c19Sopenharmony_ci
52895489c19Sopenharmony_ci    return proxy->StartPlaying(RawAddress(device.GetDeviceAddr()));
52995489c19Sopenharmony_ci}
53095489c19Sopenharmony_ci
53195489c19Sopenharmony_ciint A2dpSource::SuspendPlaying(const BluetoothRemoteDevice &device)
53295489c19Sopenharmony_ci{
53395489c19Sopenharmony_ci    HILOGI("enter");
53495489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
53595489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
53695489c19Sopenharmony_ci        return RET_BAD_STATUS;
53795489c19Sopenharmony_ci    }
53895489c19Sopenharmony_ci
53995489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
54095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
54195489c19Sopenharmony_ci
54295489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
54395489c19Sopenharmony_ci        HILOGE("input parameter error.");
54495489c19Sopenharmony_ci        return RET_BAD_PARAM;
54595489c19Sopenharmony_ci    }
54695489c19Sopenharmony_ci
54795489c19Sopenharmony_ci    return proxy->SuspendPlaying(RawAddress(device.GetDeviceAddr()));
54895489c19Sopenharmony_ci}
54995489c19Sopenharmony_ci
55095489c19Sopenharmony_ciint A2dpSource::StopPlaying(const BluetoothRemoteDevice &device)
55195489c19Sopenharmony_ci{
55295489c19Sopenharmony_ci    HILOGI("enter");
55395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
55495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
55595489c19Sopenharmony_ci        return RET_BAD_STATUS;
55695489c19Sopenharmony_ci    }
55795489c19Sopenharmony_ci
55895489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
55995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
56095489c19Sopenharmony_ci
56195489c19Sopenharmony_ci    if (!device.IsValidBluetoothRemoteDevice()) {
56295489c19Sopenharmony_ci        HILOGE("input parameter error.");
56395489c19Sopenharmony_ci        return RET_BAD_PARAM;
56495489c19Sopenharmony_ci    }
56595489c19Sopenharmony_ci
56695489c19Sopenharmony_ci    return proxy->StopPlaying(RawAddress(device.GetDeviceAddr()));
56795489c19Sopenharmony_ci}
56895489c19Sopenharmony_ci
56995489c19Sopenharmony_ciint A2dpSource::WriteFrame(const uint8_t *data, uint32_t size)
57095489c19Sopenharmony_ci{
57195489c19Sopenharmony_ci    HILOGI("enter");
57295489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
57395489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
57495489c19Sopenharmony_ci        return RET_BAD_STATUS;
57595489c19Sopenharmony_ci    }
57695489c19Sopenharmony_ci
57795489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
57895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
57995489c19Sopenharmony_ci
58095489c19Sopenharmony_ci    return proxy->WriteFrame(data, size);
58195489c19Sopenharmony_ci}
58295489c19Sopenharmony_ci
58395489c19Sopenharmony_ciint A2dpSource::GetRenderPosition(const BluetoothRemoteDevice &device, uint32_t &delayValue, uint64_t &sendDataSize,
58495489c19Sopenharmony_ci                                  uint32_t &timeStamp)
58595489c19Sopenharmony_ci{
58695489c19Sopenharmony_ci    HILOGI("enter");
58795489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
58895489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
58995489c19Sopenharmony_ci        return RET_BAD_STATUS;
59095489c19Sopenharmony_ci    }
59195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device err");
59295489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
59395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "a2dpSrc proxy is nullptr");
59495489c19Sopenharmony_ci    return proxy->GetRenderPosition(RawAddress(device.GetDeviceAddr()), delayValue, sendDataSize, timeStamp);
59595489c19Sopenharmony_ci}
59695489c19Sopenharmony_ci
59795489c19Sopenharmony_ciint A2dpSource::OffloadStartPlaying(const BluetoothRemoteDevice &device, const std::vector<int32_t> &sessionsId)
59895489c19Sopenharmony_ci{
59995489c19Sopenharmony_ci    HILOGI("enter, start playing device:%{public}s", GET_ENCRYPT_ADDR(device));
60095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device err");
60195489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.GetDeviceAddr() != INVALID_MAC_ADDRESS, BT_ERR_INVALID_PARAM, "invaild mac");
60295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(sessionsId.size() != 0, BT_ERR_INVALID_PARAM, "session size zero.");
60395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
60495489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
60595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
60695489c19Sopenharmony_ci        "a2dpSrc proxy is nullptr");
60795489c19Sopenharmony_ci    return proxy->OffloadStartPlaying(RawAddress(device.GetDeviceAddr()), sessionsId);
60895489c19Sopenharmony_ci}
60995489c19Sopenharmony_ci
61095489c19Sopenharmony_ciint A2dpSource::OffloadStopPlaying(const BluetoothRemoteDevice &device, const std::vector<int32_t> &sessionsId)
61195489c19Sopenharmony_ci{
61295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "device err");
61395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.GetDeviceAddr() != INVALID_MAC_ADDRESS, BT_ERR_INVALID_PARAM, "invaild mac");
61495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(sessionsId.size() != 0, BT_ERR_INVALID_PARAM, "session size zero.");
61595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
61695489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
61795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
61895489c19Sopenharmony_ci        "a2dpSrc proxy is nullptr");
61995489c19Sopenharmony_ci    return proxy->OffloadStopPlaying(RawAddress(device.GetDeviceAddr()), sessionsId);
62095489c19Sopenharmony_ci}
62195489c19Sopenharmony_ci
62295489c19Sopenharmony_ciint A2dpSource::A2dpOffloadSessionRequest(const BluetoothRemoteDevice &device, const std::vector<A2dpStreamInfo> &info)
62395489c19Sopenharmony_ci{
62495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), A2DP_STREAM_ENCODE_UNKNOWN, "device err");
62595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.GetDeviceAddr() != INVALID_MAC_ADDRESS, A2DP_STREAM_ENCODE_UNKNOWN, "invaild mac");
62695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(info.size() != 0, A2DP_STREAM_ENCODE_SOFTWARE, "empty stream");
62795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), A2DP_STREAM_ENCODE_UNKNOWN, "bluetooth is off.");
62895489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
62995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, A2DP_STREAM_ENCODE_UNKNOWN, "a2dpSrc proxy is nullptr");
63095489c19Sopenharmony_ci
63195489c19Sopenharmony_ci    std::vector<BluetoothA2dpStreamInfo> streamsInfo = {};
63295489c19Sopenharmony_ci    BluetoothA2dpStreamInfo streamInfo;
63395489c19Sopenharmony_ci    for (auto stream : info) {
63495489c19Sopenharmony_ci        streamInfo.sessionId = stream.sessionId;
63595489c19Sopenharmony_ci        streamInfo.streamType = stream.streamType;
63695489c19Sopenharmony_ci        streamInfo.sampleRate = stream.sampleRate;
63795489c19Sopenharmony_ci        streamInfo.isSpatialAudio = stream.isSpatialAudio;
63895489c19Sopenharmony_ci        streamsInfo.push_back(streamInfo);
63995489c19Sopenharmony_ci    }
64095489c19Sopenharmony_ci    return proxy->A2dpOffloadSessionPathRequest(RawAddress(device.GetDeviceAddr()), streamsInfo);
64195489c19Sopenharmony_ci}
64295489c19Sopenharmony_ci
64395489c19Sopenharmony_ciA2dpOffloadCodecStatus::A2dpOffloadCodecStatus(const BluetoothA2dpOffloadCodecStatus &status)
64495489c19Sopenharmony_ci{
64595489c19Sopenharmony_ci    offloadInfo.mediaPacketHeader = status.offloadInfo.mediaPacketHeader;
64695489c19Sopenharmony_ci    offloadInfo.mPt = status.offloadInfo.mPt;
64795489c19Sopenharmony_ci    offloadInfo.ssrc = status.offloadInfo.ssrc;
64895489c19Sopenharmony_ci    offloadInfo.boundaryFlag = status.offloadInfo.boundaryFlag;
64995489c19Sopenharmony_ci    offloadInfo.broadcastFlag = status.offloadInfo.broadcastFlag;
65095489c19Sopenharmony_ci    offloadInfo.codecType = status.offloadInfo.codecType;
65195489c19Sopenharmony_ci    offloadInfo.maxLatency = status.offloadInfo.maxLatency;
65295489c19Sopenharmony_ci    offloadInfo.scmsTEnable = status.offloadInfo.scmsTEnable;
65395489c19Sopenharmony_ci    offloadInfo.sampleRate = status.offloadInfo.sampleRate;
65495489c19Sopenharmony_ci    offloadInfo.encodedAudioBitrate = status.offloadInfo.encodedAudioBitrate;
65595489c19Sopenharmony_ci    offloadInfo.bitsPerSample = status.offloadInfo.bitsPerSample;
65695489c19Sopenharmony_ci    offloadInfo.chMode = status.offloadInfo.chMode;
65795489c19Sopenharmony_ci    offloadInfo.aclHdl = status.offloadInfo.aclHdl;
65895489c19Sopenharmony_ci    offloadInfo.l2cRcid = status.offloadInfo.l2cRcid;
65995489c19Sopenharmony_ci    offloadInfo.mtu = status.offloadInfo.mtu;
66095489c19Sopenharmony_ci    offloadInfo.codecSpecific0 = status.offloadInfo.codecSpecific0;
66195489c19Sopenharmony_ci    offloadInfo.codecSpecific1 = status.offloadInfo.codecSpecific1;
66295489c19Sopenharmony_ci    offloadInfo.codecSpecific2 = status.offloadInfo.codecSpecific2;
66395489c19Sopenharmony_ci    offloadInfo.codecSpecific3 = status.offloadInfo.codecSpecific3;
66495489c19Sopenharmony_ci    offloadInfo.codecSpecific4 = status.offloadInfo.codecSpecific4;
66595489c19Sopenharmony_ci    offloadInfo.codecSpecific5 = status.offloadInfo.codecSpecific5;
66695489c19Sopenharmony_ci    offloadInfo.codecSpecific6 = status.offloadInfo.codecSpecific6;
66795489c19Sopenharmony_ci    offloadInfo.codecSpecific7 = status.offloadInfo.codecSpecific7;
66895489c19Sopenharmony_ci}
66995489c19Sopenharmony_ci
67095489c19Sopenharmony_ciA2dpOffloadCodecStatus A2dpSource::GetOffloadCodecStatus(const BluetoothRemoteDevice &device) const
67195489c19Sopenharmony_ci{
67295489c19Sopenharmony_ci    HILOGI("enter");
67395489c19Sopenharmony_ci    A2dpOffloadCodecStatus ret;
67495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), ret, "input device err");
67595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.GetDeviceAddr() != INVALID_MAC_ADDRESS, ret, "invaild mac");
67695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), ret, "bluetooth is off.");
67795489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
67895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, ret, "a2dpSrc proxy is nullptr");
67995489c19Sopenharmony_ci    BluetoothA2dpOffloadCodecStatus offloadStatus(proxy->GetOffloadCodecStatus(RawAddress(device.GetDeviceAddr())));
68095489c19Sopenharmony_ci    A2dpOffloadCodecStatus status(offloadStatus);
68195489c19Sopenharmony_ci    HILOGI("codecType:%{public}x,mtu:%{public}d", status.offloadInfo.codecType, status.offloadInfo.mtu);
68295489c19Sopenharmony_ci    return status;
68395489c19Sopenharmony_ci}
68495489c19Sopenharmony_ci
68595489c19Sopenharmony_ciint A2dpSource::EnableAutoPlay(const BluetoothRemoteDevice &device)
68695489c19Sopenharmony_ci{
68795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "input device err");
68895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
68995489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
69095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "a2dpSrc proxy is nullptr");
69195489c19Sopenharmony_ci    return proxy->EnableAutoPlay(RawAddress(device.GetDeviceAddr()));
69295489c19Sopenharmony_ci}
69395489c19Sopenharmony_ci
69495489c19Sopenharmony_ciint A2dpSource::DisableAutoPlay(const BluetoothRemoteDevice &device, const int duration)
69595489c19Sopenharmony_ci{
69695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "input device err");
69795489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
69895489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
69995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "a2dpSrc proxy is nullptr");
70095489c19Sopenharmony_ci    return proxy->DisableAutoPlay(RawAddress(device.GetDeviceAddr()), duration);
70195489c19Sopenharmony_ci}
70295489c19Sopenharmony_ci
70395489c19Sopenharmony_ciint A2dpSource::GetAutoPlayDisabledDuration(const BluetoothRemoteDevice &device, int &duration)
70495489c19Sopenharmony_ci{
70595489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "input device err");
70695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
70795489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
70895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "a2dpSrc proxy is nullptr");
70995489c19Sopenharmony_ci    return proxy->GetAutoPlayDisabledDuration(RawAddress(device.GetDeviceAddr()), duration);
71095489c19Sopenharmony_ci}
71195489c19Sopenharmony_ci
71295489c19Sopenharmony_civoid A2dpSource::GetVirtualDeviceList(std::vector<std::string> &devices)
71395489c19Sopenharmony_ci{
71495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
71595489c19Sopenharmony_ci    sptr<IBluetoothA2dpSrc> proxy = GetRemoteProxy<IBluetoothA2dpSrc>(PROFILE_A2DP_SRC);
71695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "a2dpSrc proxy is nullptr");
71795489c19Sopenharmony_ci    proxy->GetVirtualDeviceList(devices);
71895489c19Sopenharmony_ci}
71995489c19Sopenharmony_ci} // namespace Bluetooth
72095489c19Sopenharmony_ci} // namespace OHOS