180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#include "avrouter_impl.h"
1780922886Sopenharmony_ci#include "ipc_skeleton.h"
1880922886Sopenharmony_ci#include "avsession_errors.h"
1980922886Sopenharmony_ci#include "avsession_log.h"
2080922886Sopenharmony_ci#include "avsession_trace.h"
2180922886Sopenharmony_ci#include "permission_checker.h"
2280922886Sopenharmony_ci#include "avcast_provider_manager.h"
2380922886Sopenharmony_ci#include "hw_cast_provider.h"
2480922886Sopenharmony_ci#include "avsession_sysevent.h"
2580922886Sopenharmony_ci
2680922886Sopenharmony_cistatic std::shared_ptr<OHOS::AVSession::HwCastProvider> hwProvider_;
2780922886Sopenharmony_ci
2880922886Sopenharmony_cinamespace OHOS::AVSession {
2980922886Sopenharmony_ciAVRouterImpl::AVRouterImpl()
3080922886Sopenharmony_ci{
3180922886Sopenharmony_ci    SLOGD("AVRouter construct");
3280922886Sopenharmony_ci}
3380922886Sopenharmony_ci
3480922886Sopenharmony_civoid AVRouterImpl::Init(IAVSessionServiceListener *servicePtr)
3580922886Sopenharmony_ci{
3680922886Sopenharmony_ci    SLOGI("Start init AVRouter");
3780922886Sopenharmony_ci    {
3880922886Sopenharmony_ci        std::lock_guard lockGuard(servicePtrLock_);
3980922886Sopenharmony_ci        servicePtr_ = servicePtr;
4080922886Sopenharmony_ci    }
4180922886Sopenharmony_ci    hwProvider_ = std::make_shared<HwCastProvider>();
4280922886Sopenharmony_ci    if (hwProvider_ != nullptr) {
4380922886Sopenharmony_ci        hwProvider_->Init();
4480922886Sopenharmony_ci    } else {
4580922886Sopenharmony_ci        SLOGE("init with null pvd to init");
4680922886Sopenharmony_ci        return;
4780922886Sopenharmony_ci    }
4880922886Sopenharmony_ci    providerNumber_ = providerNumberEnableDefault_;
4980922886Sopenharmony_ci    std::shared_ptr<AVCastProviderManager> avCastProviderManager = std::make_shared<AVCastProviderManager>();
5080922886Sopenharmony_ci    if (avCastProviderManager == nullptr) {
5180922886Sopenharmony_ci        SLOGE("init with null manager");
5280922886Sopenharmony_ci        return;
5380922886Sopenharmony_ci    }
5480922886Sopenharmony_ci    avCastProviderManager->Init(providerNumber_, hwProvider_);
5580922886Sopenharmony_ci    providerManagerMap_[providerNumber_] = avCastProviderManager;
5680922886Sopenharmony_ci    if (hwProvider_ != nullptr) {
5780922886Sopenharmony_ci        hwProvider_->RegisterCastStateListener(avCastProviderManager);
5880922886Sopenharmony_ci    } else {
5980922886Sopenharmony_ci        SLOGE("init with null pvd to registerlistener");
6080922886Sopenharmony_ci        return;
6180922886Sopenharmony_ci    }
6280922886Sopenharmony_ci    if (cacheStartDiscovery_) {
6380922886Sopenharmony_ci        SLOGI("cacheStartDiscovery check do discovery");
6480922886Sopenharmony_ci        std::lock_guard lockGuard(providerManagerLock_);
6580922886Sopenharmony_ci        StartCastDiscovery(cacheCastDeviceCapability_, cacheDrmSchemes_);
6680922886Sopenharmony_ci        cacheStartDiscovery_ = false;
6780922886Sopenharmony_ci    }
6880922886Sopenharmony_ci    SLOGI("init AVRouter done");
6980922886Sopenharmony_ci}
7080922886Sopenharmony_ci
7180922886Sopenharmony_cibool AVRouterImpl::Release()
7280922886Sopenharmony_ci{
7380922886Sopenharmony_ci    SLOGI("Start Release AVRouter");
7480922886Sopenharmony_ci    if (hasSessionAlive_) {
7580922886Sopenharmony_ci        SLOGE("has session alive, but continue");
7680922886Sopenharmony_ci    }
7780922886Sopenharmony_ci    if (hwProvider_ == nullptr || (castServiceNameMapState_["HuaweiCast"] == deviceStateConnection ||
7880922886Sopenharmony_ci        castServiceNameMapState_["HuaweiCast-Dual"] == deviceStateConnection)) {
7980922886Sopenharmony_ci        SLOGE("Start Release AVRouter err for no provider");
8080922886Sopenharmony_ci        return false;
8180922886Sopenharmony_ci    }
8280922886Sopenharmony_ci    std::lock_ground lockGuard(providerManagerLock_);
8380922886Sopenharmony_ci
8480922886Sopenharmony_ci    if (hwProvider_ == nullptr) {
8580922886Sopenharmony_ci        SLOGE("repeat check for no pvd");
8680922886Sopenharmony_ci        return false;
8780922886Sopenharmony_ci    }
8880922886Sopenharmony_ci    SLOGI("repeat check for pvd alive");
8980922886Sopenharmony_ci    hwProvider_->Release();
9080922886Sopenharmony_ci    hwProvider_ = nullptr;
9180922886Sopenharmony_ci    providerNumber_ = providerNumberDisable_;
9280922886Sopenharmony_ci    providerManagerMap_.clear();
9380922886Sopenharmony_ci    SLOGD("Release AVRouter done");
9480922886Sopenharmony_ci    return false;
9580922886Sopenharmony_ci}
9680922886Sopenharmony_ci
9780922886Sopenharmony_ciint32_t AVRouterImpl::StartDeviceLogging(int32_t fd, uint32_t maxSize)
9880922886Sopenharmony_ci{
9980922886Sopenharmony_ci    SLOGI("AVRouterImpl StartDeviceLogging");
10080922886Sopenharmony_ci    std::lock_guard lockGuard(providerManagerLock_);
10180922886Sopenharmony_ci
10280922886Sopenharmony_ci    if (providerManagerMap_.empty()) {
10380922886Sopenharmony_ci        cacheStartDeviceLogging_ = true;
10480922886Sopenharmony_ci        return AVSESSION_SUCCESS;
10580922886Sopenharmony_ci    }
10680922886Sopenharmony_ci    for (const auto& [number, providerManager] : providerManagerMap_) {
10780922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
10880922886Sopenharmony_ci            AVSESSION_ERROR, "provider is nullptr");
10980922886Sopenharmony_ci        providerManager->provider_->StartDeviceLogging(fd, maxSize);
11080922886Sopenharmony_ci    }
11180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
11280922886Sopenharmony_ci}
11380922886Sopenharmony_ci
11480922886Sopenharmony_ciint32_t AVRouterImpl::StopDeviceLogging()
11580922886Sopenharmony_ci{
11680922886Sopenharmony_ci    SLOGI("AVRouterImpl StopDeviceLogging");
11780922886Sopenharmony_ci    std::lock_guard lockGuard(providerManagerLock_);
11880922886Sopenharmony_ci
11980922886Sopenharmony_ci    if (cacheStartDeviceLogging_) {
12080922886Sopenharmony_ci        SLOGI("clear cacheStartDeviceLogging_ when stop discovery");
12180922886Sopenharmony_ci        cacheStartDeviceLogging_ = false;
12280922886Sopenharmony_ci    }
12380922886Sopenharmony_ci    for (const auto& [number, providerManager] : providerManagerMap_) {
12480922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
12580922886Sopenharmony_ci            AVSESSION_ERROR, "provider is nullptr");
12680922886Sopenharmony_ci        providerManager->provider_->StopDeviceLogging();
12780922886Sopenharmony_ci    }
12880922886Sopenharmony_ci    return AVSESSION_SUCCESS;
12980922886Sopenharmony_ci}
13080922886Sopenharmony_ci
13180922886Sopenharmony_ciint32_t AVRouterImpl::StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
13280922886Sopenharmony_ci{
13380922886Sopenharmony_ci    SLOGI("AVRouterImpl StartCastDiscovery");
13480922886Sopenharmony_ci    std::lock_guard lockGuard(providerManagerLock_);
13580922886Sopenharmony_ci
13680922886Sopenharmony_ci    if (providerManagerMap_.empty()) {
13780922886Sopenharmony_ci        SLOGI("set cacheStartDiscovery with no element with cap %{public}d", static_cast<int>(castDeviceCapability));
13880922886Sopenharmony_ci        cacheStartDiscovery_ = true;
13980922886Sopenharmony_ci        cacheCastDeviceCapability_ = castDeviceCapability;
14080922886Sopenharmony_ci        cacheDrmSchemes_ = drmSchemes;
14180922886Sopenharmony_ci        return AVSESSION_SUCCESS;
14280922886Sopenharmony_ci    }
14380922886Sopenharmony_ci    for (const auto& [number, providerManager] : providerManagerMap_) {
14480922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
14580922886Sopenharmony_ci            AVSESSION_ERROR, "provider is nullptr");
14680922886Sopenharmony_ci        providerManager->provider_->StartDiscovery(castDeviceCapability, drmSchemes);
14780922886Sopenharmony_ci    }
14880922886Sopenharmony_ci    return AVSESSION_SUCCESS;
14980922886Sopenharmony_ci}
15080922886Sopenharmony_ci
15180922886Sopenharmony_ciint32_t AVRouterImpl::StopCastDiscovery()
15280922886Sopenharmony_ci{
15380922886Sopenharmony_ci    SLOGI("AVRouterImpl StopCastDiscovery");
15480922886Sopenharmony_ci    std::lock_guard lockGuard(providerManagerLock_);
15580922886Sopenharmony_ci
15680922886Sopenharmony_ci    if (cacheStartDiscovery_) {
15780922886Sopenharmony_ci        SLOGI("clear cacheStartDiscovery when stop discovery");
15880922886Sopenharmony_ci        cacheStartDiscovery_ = false;
15980922886Sopenharmony_ci    }
16080922886Sopenharmony_ci    for (const auto& [number, providerManager] : providerManagerMap_) {
16180922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
16280922886Sopenharmony_ci            AVSESSION_ERROR, "provider is nullptr");
16380922886Sopenharmony_ci        providerManager->provider_->StopDiscovery();
16480922886Sopenharmony_ci    }
16580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
16680922886Sopenharmony_ci}
16780922886Sopenharmony_ci
16880922886Sopenharmony_ciint32_t AVRouterImpl::SetDiscoverable(const bool enable)
16980922886Sopenharmony_ci{
17080922886Sopenharmony_ci    SLOGI("AVRouterImpl SetDiscoverable %{public}d", enable);
17180922886Sopenharmony_ci
17280922886Sopenharmony_ci    std::lock_guard lockGuard(providerManagerLock_);
17380922886Sopenharmony_ci
17480922886Sopenharmony_ci    for (const auto& [number, providerManager] : providerManagerMap_) {
17580922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(providerManager != nullptr && providerManager->provider_ != nullptr,
17680922886Sopenharmony_ci            AVSESSION_ERROR, "provider is nullptr");
17780922886Sopenharmony_ci        providerManager->provider_->SetDiscoverable(enable);
17880922886Sopenharmony_ci    }
17980922886Sopenharmony_ci
18080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
18180922886Sopenharmony_ci}
18280922886Sopenharmony_ci
18380922886Sopenharmony_ciint32_t AVRouterImpl::OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo)
18480922886Sopenharmony_ci{
18580922886Sopenharmony_ci    SLOGI("AVRouterImpl received OnDeviceAvailable event");
18680922886Sopenharmony_ci
18780922886Sopenharmony_ci    std::lock_guard lockGuard(servicePtrLock_);
18880922886Sopenharmony_ci    if (servicePtr_ == nullptr) {
18980922886Sopenharmony_ci        return ERR_SERVICE_NOT_EXIST;
19080922886Sopenharmony_ci    }
19180922886Sopenharmony_ci    servicePtr_->NotifyDeviceAvailable(castOutputDeviceInfo);
19280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
19380922886Sopenharmony_ci}
19480922886Sopenharmony_ci
19580922886Sopenharmony_ciint32_t AVRouterImpl::OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param)
19680922886Sopenharmony_ci{
19780922886Sopenharmony_ci    SLOGI("AVRouterImpl received OnDeviceLogEvent event");
19880922886Sopenharmony_ci
19980922886Sopenharmony_ci    std::lock_guard lockGuard(servicePtrLock_);
20080922886Sopenharmony_ci    if (servicePtr_ == nullptr) {
20180922886Sopenharmony_ci        return ERR_SERVICE_NOT_EXIST;
20280922886Sopenharmony_ci    }
20380922886Sopenharmony_ci    servicePtr_->NotifyDeviceLogEvent(eventId, param);
20480922886Sopenharmony_ci    return AVSESSION_SUCCESS;
20580922886Sopenharmony_ci}
20680922886Sopenharmony_ci
20780922886Sopenharmony_civoid AVRouterImpl::ReleaseCurrentCastSession()
20880922886Sopenharmony_ci{
20980922886Sopenharmony_ci    SLOGI("Start ReleaseCurrentCastSession");
21080922886Sopenharmony_ci    std::lock_guard lockGuard(servicePtrLock_);
21180922886Sopenharmony_ci    servicePtr_->ReleaseCastSession();
21280922886Sopenharmony_ci}
21380922886Sopenharmony_ci
21480922886Sopenharmony_ciint32_t AVRouterImpl::OnCastSessionCreated(const int32_t castId)
21580922886Sopenharmony_ci{
21680922886Sopenharmony_ci    SLOGI("AVRouterImpl On cast session created, cast id is %{public}d", castId);
21780922886Sopenharmony_ci
21880922886Sopenharmony_ci    int64_t castHandle = -1;
21980922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumberEnableDefault_) !=
22080922886Sopenharmony_ci        providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
22180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[1] != nullptr &&
22280922886Sopenharmony_ci        providerManagerMap_[1]->provider_ ! = nullptr, AVSESSION_ERROR, "provider is nullptr");
22380922886Sopenharmony_ci    int64_t tempId = 1;
22480922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
22580922886Sopenharmony_ci    castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) |
22680922886Sopenharmony_ci        static_cast<const uint32_t>(castId));
22780922886Sopenharmony_ci    {
22880922886Sopenharmony_ci        std::lock_guard lockGuard(servicePtrLock_);
22980922886Sopenharmony_ci        servicePtr_->CreateSessionByCast(castHandle);
23080922886Sopenharmony_ci    }
23180922886Sopenharmony_ci    OutputDeviceInfo outputDeviceInfo;
23280922886Sopenharmony_ci    castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
23380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
23480922886Sopenharmony_ci}
23580922886Sopenharmony_ci
23680922886Sopenharmony_ciint32_t AVRouterImpl::OnDeviceOffline(const std::string& deviceId)
23780922886Sopenharmony_ci{
23880922886Sopenharmony_ci    SLOGI("AVRouterImpl received OnDeviceOffline event");
23980922886Sopenharmony_ci
24080922886Sopenharmony_ci    std::lock_guard lockGuard(servicePtrLock_);
24180922886Sopenharmony_ci    if (servicePtr_ == nullptr) {
24280922886Sopenharmony_ci        return ERR_SERVICE_NOT_EXIST;
24380922886Sopenharmony_ci    }
24480922886Sopenharmony_ci    servicePtr_->NotifyDeviceOffline(deviceId);
24580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
24680922886Sopenharmony_ci}
24780922886Sopenharmony_ci
24880922886Sopenharmony_ciint32_t AVRouterImpl::OnCastServerDied(int32_t providerNumber)
24980922886Sopenharmony_ci{
25080922886Sopenharmony_ci    SLOGI("AVRouterImpl received OnCastServerDied event");
25180922886Sopenharmony_ci    hasSessionAlive_ = false;
25280922886Sopenharmony_ci    std::lock_guard lockGuard(providerManagerLock_);
25380922886Sopenharmony_ci    if (providerManagerMap_.find(providerNumber) != providerManagerMap_.end()) {
25480922886Sopenharmony_ci        providerManagerMap_.erase(providerNumber);
25580922886Sopenharmony_ci    } else {
25680922886Sopenharmony_ci        return AVSESSION_ERROR;
25780922886Sopenharmony_ci    }
25880922886Sopenharmony_ci    return AVSESSION_SUCCESS;
25980922886Sopenharmony_ci}
26080922886Sopenharmony_ci
26180922886Sopenharmony_cistd::shared_ptr<IAVCastControllerProxy> AVRouterImpl::GetRemoteController(const int64_t castHandle)
26280922886Sopenharmony_ci{
26380922886Sopenharmony_ci    SLOGI("AVRouterImpl start get remote controller process");
26480922886Sopenharmony_ci
26580922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
26680922886Sopenharmony_ci    int32_t providerNumber = static_cast<int32_t>(static_cast<const uint64_t>(castHandle) >> 32);
26780922886Sopenharmony_ci    SLOGD("Get remote controller of provider %{public}d", providerNumber);
26880922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
26980922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>((static_cast<const uint64_t>(castHandle) << 32) >> 32);
27080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
27180922886Sopenharmony_ci        nullptr, "Can not find corresponding provider");
27280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr &&
27380922886Sopenharmony_ci        providerManagerMap_[providerNumber]->provider_ != nullptr, nullptr, "provider is nullptr");
27480922886Sopenharmony_ci    return providerManagerMap_[providerNumber]->provider_->GetRemoteController(castId);
27580922886Sopenharmony_ci}
27680922886Sopenharmony_ci
27780922886Sopenharmony_ciint64_t AVRouterImpl::StartCast(const OutputDeviceInfo& outputDeviceInfo,
27880922886Sopenharmony_ci    std::map<std::string, std::string>& serviceNameMapState)
27980922886Sopenharmony_ci{
28080922886Sopenharmony_ci    SLOGI("AVRouterImpl start cast process");
28180922886Sopenharmony_ci    castServiceNameMapState_ = serviceNameMapState;
28280922886Sopenharmony_ci    int64_t castHandle = -1;
28380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(outputDeviceInfo.deviceInfos_[0].providerId_) !=
28480922886Sopenharmony_ci        providerManagerMap_.end(), castHandle, "Can not find corresponding provider");
28580922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_] != nullptr
28680922886Sopenharmony_ci        && providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_ != nullptr,
28780922886Sopenharmony_ci        AVSESSION_ERROR, "provider is nullptr");
28880922886Sopenharmony_ci    int32_t castId = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].
28980922886Sopenharmony_ci        providerId_]->provider_->StartCastSession();
29080922886Sopenharmony_ci    int64_t tempId = outputDeviceInfo.deviceInfos_[0].providerId_;
29180922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
29280922886Sopenharmony_ci    castHandle = static_cast<int64_t>((static_cast<uint64_t>(tempId) << 32) | static_cast<uint32_t>(castId));
29380922886Sopenharmony_ci    hasSessionAlive_ = true;
29480922886Sopenharmony_ci
29580922886Sopenharmony_ci    return castHandle;
29680922886Sopenharmony_ci}
29780922886Sopenharmony_ci
29880922886Sopenharmony_ciint32_t AVRouterImpl::AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo)
29980922886Sopenharmony_ci{
30080922886Sopenharmony_ci    SLOGI("AVRouterImpl AddDevice process");
30180922886Sopenharmony_ci    bool ret = providerManagerMap_[outputDeviceInfo.deviceInfos_[0].providerId_]->provider_->AddCastDevice(castId,
30280922886Sopenharmony_ci        outputDeviceInfo.deviceInfos_[0]);
30380922886Sopenharmony_ci    SLOGI("AVRouterImpl AddDevice process with ret %{public}d", static_cast<int32_t>(ret));
30480922886Sopenharmony_ci    castHandleToOutputDeviceMap_[castId] = outputDeviceInfo;
30580922886Sopenharmony_ci    return ret ? AVSESSION_SUCCESS : ERR_DEVICE_CONNECTION_FAILED;
30680922886Sopenharmony_ci}
30780922886Sopenharmony_ci
30880922886Sopenharmony_ciint32_t AVRouterImpl::StopCast(const int64_t castHandle, int32_t removeTimes)
30980922886Sopenharmony_ci{
31080922886Sopenharmony_ci    SLOGI("AVRouterImpl stop cast process");
31180922886Sopenharmony_ci
31280922886Sopenharmony_ci    int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
31380922886Sopenharmony_ci    SLOGI("Stop cast, the provider number is %{public}d", providerNumber);
31480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
31580922886Sopenharmony_ci        castHandle, "Can not find corresponding provider");
31680922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
31780922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>((static_cast<const uint64_t>(castHandle) << 32) >> 32);
31880922886Sopenharmony_ci    SLOGI("Stop cast, the castId is %{public}d, removeTimes is %{public}d", castId, removeTimes);
31980922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
32080922886Sopenharmony_ci        AVSESSION_ERROR, "Can not find corresponding castId");
32180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
32280922886Sopenharmony_ci        && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
32380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castId != providerManagerMap_[providerNumber]->provider_->GetMirrorCastId() ||
32480922886Sopenharmony_ci        removeTimes == 0, AVSESSION_ERROR, "cannot remove");
32580922886Sopenharmony_ci    providerManagerMap_[providerNumber]->provider_->RemoveCastDevice(castId,
32680922886Sopenharmony_ci        castHandleToOutputDeviceMap_[castId].deviceInfos_[0]);
32780922886Sopenharmony_ci    hasSessionAlive_ = false;
32880922886Sopenharmony_ci    SLOGI("AVRouterImpl stop cast process remove device done");
32980922886Sopenharmony_ci
33080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
33180922886Sopenharmony_ci}
33280922886Sopenharmony_ci
33380922886Sopenharmony_ciint32_t AVRouterImpl::StopCastSession(const int64_t castHandle)
33480922886Sopenharmony_ci{
33580922886Sopenharmony_ci    SLOGI("AVRouterImpl stop cast process");
33680922886Sopenharmony_ci
33780922886Sopenharmony_ci    int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
33880922886Sopenharmony_ci
33980922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
34080922886Sopenharmony_ci        castHandle, "Can not find corresponding provider");
34180922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
34280922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
34380922886Sopenharmony_ci    castHandleToOutputDeviceMap_[castId] = castOutputDeviceInfo_;
34480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castHandleToOutputDeviceMap_.find(castId) != castHandleToOutputDeviceMap_.end(),
34580922886Sopenharmony_ci        AVSESSION_ERROR, "Can not find corresponding castId");
34680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
34780922886Sopenharmony_ci        && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
34880922886Sopenharmony_ci    providerManagerMap_[providerNumber]->provider_->StopCastSession(castId);
34980922886Sopenharmony_ci    hasSessionAlive_ = false;
35080922886Sopenharmony_ci
35180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
35280922886Sopenharmony_ci}
35380922886Sopenharmony_ci
35480922886Sopenharmony_ciint32_t AVRouterImpl::SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo)
35580922886Sopenharmony_ci{
35680922886Sopenharmony_ci    int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
35780922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
35880922886Sopenharmony_ci    castOutputDeviceInfo_.deviceInfos_.emplace_back(deviceInfo);
35980922886Sopenharmony_ci    castHandleToOutputDeviceMap_[castId] = castOutputDeviceInfo_;
36080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
36180922886Sopenharmony_ci        AVSESSION_ERROR, "Can not find corresponding provider");
36280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
36380922886Sopenharmony_ci        && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
36480922886Sopenharmony_ci    providerManagerMap_[providerNumber]->provider_->SetStreamState(castId, deviceInfo);
36580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
36680922886Sopenharmony_ci}
36780922886Sopenharmony_ci
36880922886Sopenharmony_ciint32_t AVRouterImpl::GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId)
36980922886Sopenharmony_ci{
37080922886Sopenharmony_ci    int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
37180922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
37280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
37380922886Sopenharmony_ci        AVSESSION_ERROR, "Can not find corresponding provider");
37480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
37580922886Sopenharmony_ci        && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
37680922886Sopenharmony_ci    providerManagerMap_[providerNumber]->provider_->GetRemoteNetWorkId(castId, deviceId, networkId);
37780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
37880922886Sopenharmony_ci}
37980922886Sopenharmony_ci
38080922886Sopenharmony_ciint32_t AVRouterImpl::RegisterCallback(int64_t castHandle, const std::shared_ptr<IAVCastSessionStateListener> callback)
38180922886Sopenharmony_ci{
38280922886Sopenharmony_ci    SLOGI("AVRouterImpl register IAVCastSessionStateListener callback to provider");
38380922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
38480922886Sopenharmony_ci    int32_t providerNumber = static_cast<int32_t>(static_cast<uint64_t>(castHandle) >> 32);
38580922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
38680922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
38780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
38880922886Sopenharmony_ci        AVSESSION_ERROR, "Can not find corresponding provider");
38980922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
39080922886Sopenharmony_ci        && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
39180922886Sopenharmony_ci    providerManagerMap_[providerNumber]->provider_->RegisterCastSessionStateListener(castId, callback);
39280922886Sopenharmony_ci    SLOGD("AVRouter impl register callback finished");
39380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
39480922886Sopenharmony_ci}
39580922886Sopenharmony_ci
39680922886Sopenharmony_ciint32_t AVRouterImpl::UnRegisterCallback(int64_t castHandle,
39780922886Sopenharmony_ci    const std::shared_ptr<IAVCastSessionStateListener> callback)
39880922886Sopenharmony_ci{
39980922886Sopenharmony_ci    SLOGI("AVRouterImpl UnRegisterCallback IAVCastSessionStateListener callback to provider");
40080922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
40180922886Sopenharmony_ci    int32_t providerNumber = static_cast<uint64_t>(static_cast<uint64_t>(castHandle)) >> 32;
40280922886Sopenharmony_ci    // The first 32 bits are providerId, the last 32 bits are castId
40380922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>((static_cast<uint64_t>(castHandle) << 32) >> 32);
40480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_.find(providerNumber) != providerManagerMap_.end(),
40580922886Sopenharmony_ci        AVSESSION_ERROR, "Can not find corresponding provider");
40680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(providerManagerMap_[providerNumber] != nullptr
40780922886Sopenharmony_ci        && providerManagerMap_[providerNumber]->provider_ != nullptr, AVSESSION_ERROR, "provider is nullptr");
40880922886Sopenharmony_ci    providerManagerMap_[providerNumber]->provider_->UnRegisterCastSessionStateListener(castId, callback);
40980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
41080922886Sopenharmony_ci}
41180922886Sopenharmony_ci} // namespace OHOS::AVSession
412