180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2022-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 "audio_manager_proxy.h"
1780922886Sopenharmony_ci#include "av_router.h"
1880922886Sopenharmony_ci#include "avsession_service.h"
1980922886Sopenharmony_ci#include "avcontroller_item.h"
2080922886Sopenharmony_ci#include "avsession_log.h"
2180922886Sopenharmony_ci#include "avsession_errors.h"
2280922886Sopenharmony_ci#include "avsession_descriptor.h"
2380922886Sopenharmony_ci#include "avsession_trace.h"
2480922886Sopenharmony_ci#include "avsession_sysevent.h"
2580922886Sopenharmony_ci#include "avsession_utils.h"
2680922886Sopenharmony_ci#include "remote_session_sink.h"
2780922886Sopenharmony_ci#include "remote_session_source.h"
2880922886Sopenharmony_ci#include "remote_session_source_proxy.h"
2980922886Sopenharmony_ci#include "remote_session_sink_proxy.h"
3080922886Sopenharmony_ci#include "permission_checker.h"
3180922886Sopenharmony_ci#include "session_xcollie.h"
3280922886Sopenharmony_ci#include "avsession_item.h"
3380922886Sopenharmony_ci#include "avsession_radar.h"
3480922886Sopenharmony_ci#include "avsession_event_handler.h"
3580922886Sopenharmony_ci#include "bundle_status_adapter.h"
3680922886Sopenharmony_ci#include "want_agent_helper.h"
3780922886Sopenharmony_ci#include "array_wrapper.h"
3880922886Sopenharmony_ci#include "string_wrapper.h"
3980922886Sopenharmony_ci
4080922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
4180922886Sopenharmony_ci#include "avcast_controller_proxy.h"
4280922886Sopenharmony_ci#include "avcast_controller_item.h"
4380922886Sopenharmony_ci#include "collaboration_manager.h"
4480922886Sopenharmony_ci#endif
4580922886Sopenharmony_ci
4680922886Sopenharmony_ci#if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM) and !defined(IOS_PLATFORM)
4780922886Sopenharmony_ci#include <malloc.h>
4880922886Sopenharmony_ci#include <string>
4980922886Sopenharmony_ci#include <openssl/crypto.h>
5080922886Sopenharmony_ci#endif
5180922886Sopenharmony_ci
5280922886Sopenharmony_ciusing namespace OHOS::AudioStandard;
5380922886Sopenharmony_ci
5480922886Sopenharmony_cinamespace OHOS::AVSession {
5580922886Sopenharmony_ci
5680922886Sopenharmony_cistatic const std::string AVSESSION_DYNAMIC_DISPLAY_LIBRARY_PATH = std::string("libavsession_dynamic_display.z.so");
5780922886Sopenharmony_ci
5880922886Sopenharmony_ciconst std::map<int32_t, string> sessionTypeMap_ = {
5980922886Sopenharmony_ci    {AVSession::SESSION_TYPE_VIDEO, "video"},
6080922886Sopenharmony_ci    {AVSession::SESSION_TYPE_VOICE_CALL, "voice_call"},
6180922886Sopenharmony_ci    {AVSession::SESSION_TYPE_VIDEO_CALL, "video_call"},
6280922886Sopenharmony_ci};
6380922886Sopenharmony_ci
6480922886Sopenharmony_ciAVSessionItem::AVSessionItem(const AVSessionDescriptor& descriptor, int32_t userId)
6580922886Sopenharmony_ci    : descriptor_(descriptor), userId_(userId)
6680922886Sopenharmony_ci{
6780922886Sopenharmony_ci    SLOGI("constructor session id=%{public}s, userId=%{public}d",
6880922886Sopenharmony_ci        AVSessionUtils::GetAnonySessionId(descriptor_.sessionId_).c_str(), userId_);
6980922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
7080922886Sopenharmony_ci    cssListener_ = std::make_shared<CssListener>(this);
7180922886Sopenharmony_ci#endif
7280922886Sopenharmony_ci    dynamicLoader_ = std::make_unique<AVSessionDynamicLoader>();
7380922886Sopenharmony_ci    avsessionDisaplayIntf_ = nullptr;
7480922886Sopenharmony_ci}
7580922886Sopenharmony_ci
7680922886Sopenharmony_ciAVSessionItem::~AVSessionItem()
7780922886Sopenharmony_ci{
7880922886Sopenharmony_ci    SLOGI("destroy with activeCheck session id=%{public}s, userId=%{public}d",
7980922886Sopenharmony_ci        AVSessionUtils::GetAnonySessionId(descriptor_.sessionId_).c_str(), userId_);
8080922886Sopenharmony_ci    if (IsActive()) {
8180922886Sopenharmony_ci        Deactivate();
8280922886Sopenharmony_ci    }
8380922886Sopenharmony_ci    if (avsessionDisaplayIntf_) {
8480922886Sopenharmony_ci        delete avsessionDisaplayIntf_;
8580922886Sopenharmony_ci        avsessionDisaplayIntf_ = nullptr;
8680922886Sopenharmony_ci    }
8780922886Sopenharmony_ci}
8880922886Sopenharmony_ci
8980922886Sopenharmony_ci// LCOV_EXCL_START
9080922886Sopenharmony_cistd::string AVSessionItem::GetSessionId()
9180922886Sopenharmony_ci{
9280922886Sopenharmony_ci    return descriptor_.sessionId_;
9380922886Sopenharmony_ci}
9480922886Sopenharmony_ci
9580922886Sopenharmony_cistd::string AVSessionItem::GetSessionType()
9680922886Sopenharmony_ci{
9780922886Sopenharmony_ci    auto iter = sessionTypeMap_.find(descriptor_.sessionType_);
9880922886Sopenharmony_ci    if (iter != sessionTypeMap_.end()) {
9980922886Sopenharmony_ci        return iter->second;
10080922886Sopenharmony_ci    } else {
10180922886Sopenharmony_ci        return "audio";
10280922886Sopenharmony_ci    }
10380922886Sopenharmony_ci}
10480922886Sopenharmony_ci// LCOV_EXCL_STOP
10580922886Sopenharmony_ci
10680922886Sopenharmony_ciint32_t AVSessionItem::Destroy()
10780922886Sopenharmony_ci{
10880922886Sopenharmony_ci    SLOGI("AVSessionItem send service destroy event to service, check serviceCallback exist");
10980922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
11080922886Sopenharmony_ci        "API_NAME", "Destroy",
11180922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
11280922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
11380922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
11480922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
11580922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
11680922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
11780922886Sopenharmony_ci    if (serviceCallback_) {
11880922886Sopenharmony_ci        SLOGI("AVSessionItem send service destroy event to service");
11980922886Sopenharmony_ci        serviceCallback_(*this);
12080922886Sopenharmony_ci    }
12180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
12280922886Sopenharmony_ci}
12380922886Sopenharmony_ci
12480922886Sopenharmony_ciint32_t AVSessionItem::DestroyTask()
12580922886Sopenharmony_ci{
12680922886Sopenharmony_ci    {
12780922886Sopenharmony_ci        std::lock_guard lockGuard(destroyLock_);
12880922886Sopenharmony_ci        if (isDestroyed_) {
12980922886Sopenharmony_ci            SLOGI("session is already destroyed");
13080922886Sopenharmony_ci            return AVSESSION_SUCCESS;
13180922886Sopenharmony_ci        }
13280922886Sopenharmony_ci        isDestroyed_ = true;
13380922886Sopenharmony_ci    }
13480922886Sopenharmony_ci
13580922886Sopenharmony_ci    std::string sessionId = descriptor_.sessionId_;
13680922886Sopenharmony_ci    std::string fileName = AVSessionUtils::GetCachePathName(userId_) + sessionId + AVSessionUtils::GetFileSuffix();
13780922886Sopenharmony_ci    AVSessionUtils::DeleteFile(fileName);
13880922886Sopenharmony_ci    {
13980922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
14080922886Sopenharmony_ci        for (auto it = controllers_.begin(); it != controllers_.end();) {
14180922886Sopenharmony_ci            if (it->second) {
14280922886Sopenharmony_ci                (it->second)->HandleSessionDestroy();
14380922886Sopenharmony_ci            }
14480922886Sopenharmony_ci            it = controllers_.erase(it);
14580922886Sopenharmony_ci        }
14680922886Sopenharmony_ci    }
14780922886Sopenharmony_ci    {
14880922886Sopenharmony_ci        std::lock_guard lockGuard(callbackLock_);
14980922886Sopenharmony_ci        if (callback_) {
15080922886Sopenharmony_ci            callback_.clear();
15180922886Sopenharmony_ci        }
15280922886Sopenharmony_ci    }
15380922886Sopenharmony_ci
15480922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
15580922886Sopenharmony_ci    SLOGI("Session destroy with castHandle: %{public}ld", castHandle_);
15680922886Sopenharmony_ci    ReleaseAVCastControllerInner();
15780922886Sopenharmony_ci    if (descriptor_.sessionTag_ != "RemoteCast" && castHandle_ > 0) {
15880922886Sopenharmony_ci        if (!collaborationNeedNetworkId_.empty()) {
15980922886Sopenharmony_ci            CollaborationManager::GetInstance().PublishServiceState(collaborationNeedNetworkId_.c_str(),
16080922886Sopenharmony_ci                ServiceCollaborationManagerBussinessStatus::SCM_IDLE);
16180922886Sopenharmony_ci        }
16280922886Sopenharmony_ci        AVRouter::GetInstance().UnRegisterCallback(castHandle_, cssListener_);
16380922886Sopenharmony_ci        ReleaseCast();
16480922886Sopenharmony_ci    }
16580922886Sopenharmony_ci    StopCastDisplayListener();
16680922886Sopenharmony_ci#endif
16780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
16880922886Sopenharmony_ci}
16980922886Sopenharmony_ci
17080922886Sopenharmony_ciint32_t AVSessionItem::SetAVCallMetaData(const AVCallMetaData& avCallMetaData)
17180922886Sopenharmony_ci{
17280922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
17380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(avCallMetaData_.CopyFrom(avCallMetaData), AVSESSION_ERROR, "AVCallMetaData set error");
17480922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> innerPixelMap = avCallMetaData_.GetMediaImage();
17580922886Sopenharmony_ci    if (innerPixelMap != nullptr) {
17680922886Sopenharmony_ci        std::string sessionId = GetSessionId();
17780922886Sopenharmony_ci        std::string fileDir = AVSessionUtils::GetCachePathName(userId_);
17880922886Sopenharmony_ci        AVSessionUtils::WriteImageToFile(innerPixelMap, fileDir, sessionId + AVSessionUtils::GetFileSuffix());
17980922886Sopenharmony_ci        innerPixelMap->Clear();
18080922886Sopenharmony_ci        avCallMetaData_.SetMediaImage(innerPixelMap);
18180922886Sopenharmony_ci    }
18280922886Sopenharmony_ci
18380922886Sopenharmony_ci    {
18480922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
18580922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
18680922886Sopenharmony_ci            if (controller != nullptr) {
18780922886Sopenharmony_ci                controller->HandleAVCallMetaDataChange(avCallMetaData);
18880922886Sopenharmony_ci            }
18980922886Sopenharmony_ci        }
19080922886Sopenharmony_ci    }
19180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
19280922886Sopenharmony_ci}
19380922886Sopenharmony_ci
19480922886Sopenharmony_ci// LCOV_EXCL_START
19580922886Sopenharmony_ciint32_t AVSessionItem::SetAVCallState(const AVCallState& avCallState)
19680922886Sopenharmony_ci{
19780922886Sopenharmony_ci    {
19880922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
19980922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(avCallState_.CopyFrom(avCallState), AVSESSION_ERROR, "AVCallState set error");
20080922886Sopenharmony_ci    }
20180922886Sopenharmony_ci
20280922886Sopenharmony_ci    {
20380922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
20480922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
20580922886Sopenharmony_ci            if (controller != nullptr) {
20680922886Sopenharmony_ci                controller->HandleAVCallStateChange(avCallState);
20780922886Sopenharmony_ci            }
20880922886Sopenharmony_ci        }
20980922886Sopenharmony_ci    }
21080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
21180922886Sopenharmony_ci}
21280922886Sopenharmony_ci// LCOV_EXCL_STOP
21380922886Sopenharmony_ci
21480922886Sopenharmony_ciint32_t AVSessionItem::GetAVMetaData(AVMetaData& meta)
21580922886Sopenharmony_ci{
21680922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
21780922886Sopenharmony_ci    SessionXCollie sessionXCollie("avsession::GetAVMetaData");
21880922886Sopenharmony_ci    std::string sessionId = GetSessionId();
21980922886Sopenharmony_ci    std::string fileDir = AVSessionUtils::GetCachePathName(userId_);
22080922886Sopenharmony_ci    std::string fileName = sessionId + AVSessionUtils::GetFileSuffix();
22180922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> innerPixelMap = metaData_.GetMediaImage();
22280922886Sopenharmony_ci    AVSessionUtils::ReadImageFromFile(innerPixelMap, fileDir, fileName);
22380922886Sopenharmony_ci
22480922886Sopenharmony_ci    std::string avQueueFileDir = AVSessionUtils::GetFixedPathName();
22580922886Sopenharmony_ci    std::string avQueueFileName = GetBundleName() + "_" + metaData_.GetAVQueueId() + AVSessionUtils::GetFileSuffix();
22680922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData_.GetAVQueueImage();
22780922886Sopenharmony_ci    AVSessionUtils::ReadImageFromFile(avQueuePixelMap, avQueueFileDir, avQueueFileName);
22880922886Sopenharmony_ci
22980922886Sopenharmony_ci    meta = metaData_;
23080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
23180922886Sopenharmony_ci}
23280922886Sopenharmony_ci
23380922886Sopenharmony_ci// LCOV_EXCL_START
23480922886Sopenharmony_ciint32_t AVSessionItem::ProcessFrontSession(const std::string& source)
23580922886Sopenharmony_ci{
23680922886Sopenharmony_ci    SLOGI("ProcessFrontSession with directly handle %{public}s ", source.c_str());
23780922886Sopenharmony_ci    HandleFrontSession();
23880922886Sopenharmony_ci    return AVSESSION_SUCCESS;
23980922886Sopenharmony_ci}
24080922886Sopenharmony_ci
24180922886Sopenharmony_civoid AVSessionItem::HandleFrontSession()
24280922886Sopenharmony_ci{
24380922886Sopenharmony_ci    bool isMetaEmpty;
24480922886Sopenharmony_ci    {
24580922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
24680922886Sopenharmony_ci        isMetaEmpty = metaData_.GetTitle().empty() && metaData_.GetMediaImage() == nullptr &&
24780922886Sopenharmony_ci            metaData_.GetMediaImageUri().empty();
24880922886Sopenharmony_ci    }
24980922886Sopenharmony_ci    SLOGI("frontSession bundle=%{public}s metaEmpty=%{public}d Cmd=%{public}d castCmd=%{public}d firstAdd=%{public}d",
25080922886Sopenharmony_ci        GetBundleName().c_str(), isMetaEmpty, static_cast<int32_t>(supportedCmd_.size()),
25180922886Sopenharmony_ci        static_cast<int32_t>(supportedCastCmds_.size()), isFirstAddToFront_);
25280922886Sopenharmony_ci    if (isMetaEmpty || (supportedCmd_.size() == 0 && supportedCastCmds_.size() == 0)) {
25380922886Sopenharmony_ci        if (!isFirstAddToFront_ && serviceCallbackForUpdateSession_) {
25480922886Sopenharmony_ci            serviceCallbackForUpdateSession_(GetSessionId(), false);
25580922886Sopenharmony_ci            isFirstAddToFront_ = true;
25680922886Sopenharmony_ci        }
25780922886Sopenharmony_ci    } else {
25880922886Sopenharmony_ci        if (isFirstAddToFront_ && serviceCallbackForUpdateSession_) {
25980922886Sopenharmony_ci            serviceCallbackForUpdateSession_(GetSessionId(), true);
26080922886Sopenharmony_ci            isFirstAddToFront_ = false;
26180922886Sopenharmony_ci        }
26280922886Sopenharmony_ci    }
26380922886Sopenharmony_ci}
26480922886Sopenharmony_ci
26580922886Sopenharmony_cibool AVSessionItem::HasAvQueueInfo()
26680922886Sopenharmony_ci{
26780922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
26880922886Sopenharmony_ci    if (metaData_.GetAVQueueName().empty() || metaData_.GetAVQueueId().empty()) {
26980922886Sopenharmony_ci        SLOGD("avqueuename or avqueueid is empty");
27080922886Sopenharmony_ci        return false;
27180922886Sopenharmony_ci    }
27280922886Sopenharmony_ci    if (metaData_.GetAVQueueImage() == nullptr && metaData_.GetAVQueueImageUri().empty()) {
27380922886Sopenharmony_ci        SLOGD("avqueue img is empty");
27480922886Sopenharmony_ci        return false;
27580922886Sopenharmony_ci    }
27680922886Sopenharmony_ci    if (playbackState_.GetState() != AVPlaybackState::PLAYBACK_STATE_PLAY) {
27780922886Sopenharmony_ci        SLOGD("current avqueueinfo is not playing");
27880922886Sopenharmony_ci        return false;
27980922886Sopenharmony_ci    }
28080922886Sopenharmony_ci
28180922886Sopenharmony_ci    return true;
28280922886Sopenharmony_ci}
28380922886Sopenharmony_ci
28480922886Sopenharmony_civoid AVSessionItem::ReportSetAVMetaDataInfo(const AVMetaData& meta)
28580922886Sopenharmony_ci{
28680922886Sopenharmony_ci    std::string mediaImage = "false";
28780922886Sopenharmony_ci    std::string avQueueImage = "false";
28880922886Sopenharmony_ci    if (meta.GetMediaImage() != nullptr || !(meta.GetMediaImageUri().empty())) {
28980922886Sopenharmony_ci        mediaImage = "true";
29080922886Sopenharmony_ci    }
29180922886Sopenharmony_ci    if (meta.GetAVQueueImage() != nullptr || !(meta.GetAVQueueImageUri().empty())) {
29280922886Sopenharmony_ci        avQueueImage = "true";
29380922886Sopenharmony_ci    }
29480922886Sopenharmony_ci    std::string API_PARAM_STRING = "assetId: " + meta.GetAssetId() + ", "
29580922886Sopenharmony_ci                                    + "artist: " + meta.GetArtist() + ", "
29680922886Sopenharmony_ci                                    + "title: " + meta.GetTitle() + ", "
29780922886Sopenharmony_ci                                    + "subtitle: " + meta.GetSubTitle() + ", "
29880922886Sopenharmony_ci                                    + "avQueueId: " + meta.GetAVQueueId() + ", "
29980922886Sopenharmony_ci                                    + "duration: " + std::to_string(meta.GetDuration()) + ", "
30080922886Sopenharmony_ci                                    + "avQueueName: " + meta.GetAVQueueName() + ", "
30180922886Sopenharmony_ci                                    + "mediaImage: " + mediaImage + ", "
30280922886Sopenharmony_ci                                    + "avqueueImage: " + avQueueImage;
30380922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR", "API_NAME", "SetAVMetaData",
30480922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(), "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
30580922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_, "SESSION_TYPE", GetSessionType(), "API_PARAM", API_PARAM_STRING,
30680922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS, "ERROR_MSG", "SUCCESS");
30780922886Sopenharmony_ci}
30880922886Sopenharmony_ci
30980922886Sopenharmony_ciint32_t AVSessionItem::SetAVMetaData(const AVMetaData& meta)
31080922886Sopenharmony_ci{
31180922886Sopenharmony_ci    {
31280922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
31380922886Sopenharmony_ci        SessionXCollie sessionXCollie("avsession::SetAVMetaData");
31480922886Sopenharmony_ci        ReportSetAVMetaDataInfo(meta);
31580922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(metaData_.CopyFrom(meta), AVSESSION_ERROR, "AVMetaData set error");
31680922886Sopenharmony_ci        std::shared_ptr<AVSessionPixelMap> innerPixelMap = metaData_.GetMediaImage();
31780922886Sopenharmony_ci        if (innerPixelMap != nullptr) {
31880922886Sopenharmony_ci            std::string sessionId = GetSessionId();
31980922886Sopenharmony_ci            std::string fileDir = AVSessionUtils::GetCachePathName(userId_);
32080922886Sopenharmony_ci            AVSessionUtils::WriteImageToFile(innerPixelMap, fileDir, sessionId + AVSessionUtils::GetFileSuffix());
32180922886Sopenharmony_ci            innerPixelMap->Clear();
32280922886Sopenharmony_ci            metaData_.SetMediaImage(innerPixelMap);
32380922886Sopenharmony_ci        }
32480922886Sopenharmony_ci    }
32580922886Sopenharmony_ci    ProcessFrontSession("SetAVMetaData");
32680922886Sopenharmony_ci    if (HasAvQueueInfo() && serviceCallbackForAddAVQueueInfo_) {
32780922886Sopenharmony_ci        serviceCallbackForAddAVQueueInfo_(*this);
32880922886Sopenharmony_ci    }
32980922886Sopenharmony_ci    SLOGI("send metadata change event to controllers with title %{public}s", meta.GetTitle().c_str());
33080922886Sopenharmony_ci    AVSessionEventHandler::GetInstance().AVSessionPostTask([this, meta]() {
33180922886Sopenharmony_ci        SLOGI("HandleMetaDataChange in postTask with title %{public}s and size %{public}d",
33280922886Sopenharmony_ci            meta.GetTitle().c_str(), static_cast<int>(controllers_.size()));
33380922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
33480922886Sopenharmony_ci        CHECK_AND_RETURN_LOG(controllers_.size() > 0, "handle with no controller, return");
33580922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
33680922886Sopenharmony_ci            if (controller != nullptr) {
33780922886Sopenharmony_ci                controller->HandleMetaDataChange(meta);
33880922886Sopenharmony_ci            }
33980922886Sopenharmony_ci        }
34080922886Sopenharmony_ci        }, "HandleMetaDataChange", 0);
34180922886Sopenharmony_ci
34280922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
34380922886Sopenharmony_ci    if (remoteSource_ != nullptr) {
34480922886Sopenharmony_ci        SLOGI("set remote AVMetaData");
34580922886Sopenharmony_ci        auto ret = remoteSource_->SetAVMetaData(meta);
34680922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVMetaData failed");
34780922886Sopenharmony_ci    }
34880922886Sopenharmony_ci    return AVSESSION_SUCCESS;
34980922886Sopenharmony_ci}
35080922886Sopenharmony_ci
35180922886Sopenharmony_ciint32_t AVSessionItem::GetAVQueueItems(std::vector<AVQueueItem>& items)
35280922886Sopenharmony_ci{
35380922886Sopenharmony_ci    std::lock_guard lock_guard(avsessionItemLock_);
35480922886Sopenharmony_ci    items = queueItems_;
35580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
35680922886Sopenharmony_ci}
35780922886Sopenharmony_ci
35880922886Sopenharmony_ciint32_t AVSessionItem::SetAVQueueItems(const std::vector<AVQueueItem>& items)
35980922886Sopenharmony_ci{
36080922886Sopenharmony_ci    {
36180922886Sopenharmony_ci        std::lock_guard lock_guard(avsessionItemLock_);
36280922886Sopenharmony_ci        queueItems_ = items;
36380922886Sopenharmony_ci    }
36480922886Sopenharmony_ci
36580922886Sopenharmony_ci    {
36680922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
36780922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
36880922886Sopenharmony_ci            if (controller != nullptr) {
36980922886Sopenharmony_ci                controller->HandleQueueItemsChange(items);
37080922886Sopenharmony_ci            }
37180922886Sopenharmony_ci        }
37280922886Sopenharmony_ci    }
37380922886Sopenharmony_ci
37480922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
37580922886Sopenharmony_ci    if (remoteSource_ != nullptr) {
37680922886Sopenharmony_ci        auto ret = remoteSource_->SetAVQueueItems(items);
37780922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVQueueItems failed");
37880922886Sopenharmony_ci    }
37980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
38080922886Sopenharmony_ci}
38180922886Sopenharmony_ci
38280922886Sopenharmony_ciint32_t AVSessionItem::GetAVQueueTitle(std::string& title)
38380922886Sopenharmony_ci{
38480922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
38580922886Sopenharmony_ci    title = queueTitle_;
38680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
38780922886Sopenharmony_ci}
38880922886Sopenharmony_ci
38980922886Sopenharmony_ciint32_t AVSessionItem::SetAVQueueTitle(const std::string& title)
39080922886Sopenharmony_ci{
39180922886Sopenharmony_ci    {
39280922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
39380922886Sopenharmony_ci        queueTitle_ = title;
39480922886Sopenharmony_ci    }
39580922886Sopenharmony_ci
39680922886Sopenharmony_ci    {
39780922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
39880922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
39980922886Sopenharmony_ci            if (controller != nullptr) {
40080922886Sopenharmony_ci                controller->HandleQueueTitleChange(title);
40180922886Sopenharmony_ci            }
40280922886Sopenharmony_ci        }
40380922886Sopenharmony_ci    }
40480922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
40580922886Sopenharmony_ci    if (remoteSource_ != nullptr) {
40680922886Sopenharmony_ci        auto ret = remoteSource_->SetAVQueueTitle(title);
40780922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVQueueTitle failed");
40880922886Sopenharmony_ci    }
40980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
41080922886Sopenharmony_ci}
41180922886Sopenharmony_ci
41280922886Sopenharmony_ciint32_t AVSessionItem::SetAVPlaybackState(const AVPlaybackState& state)
41380922886Sopenharmony_ci{
41480922886Sopenharmony_ci    {
41580922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
41680922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(playbackState_.CopyFrom(state), AVSESSION_ERROR, "AVPlaybackState set error");
41780922886Sopenharmony_ci    }
41880922886Sopenharmony_ci    if (HasAvQueueInfo() && serviceCallbackForAddAVQueueInfo_) {
41980922886Sopenharmony_ci        serviceCallbackForAddAVQueueInfo_(*this);
42080922886Sopenharmony_ci    }
42180922886Sopenharmony_ci    {
42280922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
42380922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
42480922886Sopenharmony_ci            if (controller != nullptr) {
42580922886Sopenharmony_ci                controller->HandlePlaybackStateChange(state);
42680922886Sopenharmony_ci            }
42780922886Sopenharmony_ci        }
42880922886Sopenharmony_ci    }
42980922886Sopenharmony_ci
43080922886Sopenharmony_ci    std::string isFavor = state.GetFavorite()? "true" : "false";
43180922886Sopenharmony_ci    std::string API_PARAM_STRING = "state: " + std::to_string(state.GetState()) + ", "
43280922886Sopenharmony_ci                                    + "elapsedTime: " + std::to_string(state.GetPosition().elapsedTime_) + ", "
43380922886Sopenharmony_ci                                    + "updateTime: " + std::to_string(state.GetPosition().updateTime_) + ", "
43480922886Sopenharmony_ci                                    + "loopMode: " + std::to_string(state.GetLoopMode()) + ", "
43580922886Sopenharmony_ci                                    + "isFavorite: " + isFavor;
43680922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
43780922886Sopenharmony_ci        "API_NAME", "SetAVPlaybackState",
43880922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
43980922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
44080922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
44180922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
44280922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
44380922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
44480922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
44580922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
44680922886Sopenharmony_ci    if (remoteSource_ != nullptr) {
44780922886Sopenharmony_ci        remoteSource_->SetAVPlaybackState(state);
44880922886Sopenharmony_ci    }
44980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
45080922886Sopenharmony_ci}
45180922886Sopenharmony_ci
45280922886Sopenharmony_ciint32_t AVSessionItem::GetAVPlaybackState(AVPlaybackState& state)
45380922886Sopenharmony_ci{
45480922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
45580922886Sopenharmony_ci    state = playbackState_;
45680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
45780922886Sopenharmony_ci}
45880922886Sopenharmony_ci// LCOV_EXCL_STOP
45980922886Sopenharmony_ci
46080922886Sopenharmony_ciint32_t AVSessionItem::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability)
46180922886Sopenharmony_ci{
46280922886Sopenharmony_ci    launchAbility_ = ability;
46380922886Sopenharmony_ci    std::shared_ptr<AAFwk::Want> want = std::make_shared<AAFwk::Want>();
46480922886Sopenharmony_ci    std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> launWantAgent =
46580922886Sopenharmony_ci        std::make_shared<AbilityRuntime::WantAgent::WantAgent>(ability);
46680922886Sopenharmony_ci    int res = AVSESSION_SUCCESS;
46780922886Sopenharmony_ci    if (want != nullptr && launWantAgent != nullptr) {
46880922886Sopenharmony_ci        res = AbilityRuntime::WantAgent::WantAgentHelper::GetWant(launWantAgent, want);
46980922886Sopenharmony_ci    }
47080922886Sopenharmony_ci    std::string errMsg = "Get want failed.";
47180922886Sopenharmony_ci    std::string bundleName = "";
47280922886Sopenharmony_ci    std::string abilityName = "";
47380922886Sopenharmony_ci    std::string moduleName = "";
47480922886Sopenharmony_ci    if (res == AVSESSION_SUCCESS) {
47580922886Sopenharmony_ci        bundleName = want->GetElement().GetBundleName().c_str();
47680922886Sopenharmony_ci        abilityName = want->GetElement().GetAbilityName().c_str();
47780922886Sopenharmony_ci        moduleName = want->GetElement().GetModuleName().c_str();
47880922886Sopenharmony_ci        errMsg = "SUCCESS";
47980922886Sopenharmony_ci    }
48080922886Sopenharmony_ci    std::string API_PARAM_STRING = "bundleName: " + bundleName + ", "
48180922886Sopenharmony_ci                                    + "moduleName: " + moduleName + ", "
48280922886Sopenharmony_ci                                    + "abilityName: " + abilityName;
48380922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
48480922886Sopenharmony_ci        "API_NAME", "SetLaunchAbility",
48580922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
48680922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
48780922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
48880922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
48980922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
49080922886Sopenharmony_ci        "ERROR_CODE", res,
49180922886Sopenharmony_ci        "ERROR_MSG", errMsg);
49280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
49380922886Sopenharmony_ci}
49480922886Sopenharmony_ci
49580922886Sopenharmony_ci// LCOV_EXCL_START
49680922886Sopenharmony_ciint32_t AVSessionItem::GetExtras(AAFwk::WantParams& extras)
49780922886Sopenharmony_ci{
49880922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
49980922886Sopenharmony_ci    extras = extras_;
50080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
50180922886Sopenharmony_ci}
50280922886Sopenharmony_ci
50380922886Sopenharmony_ciint32_t AVSessionItem::SetExtras(const AAFwk::WantParams& extras)
50480922886Sopenharmony_ci{
50580922886Sopenharmony_ci    {
50680922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
50780922886Sopenharmony_ci        extras_ = extras;
50880922886Sopenharmony_ci    }
50980922886Sopenharmony_ci
51080922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
51180922886Sopenharmony_ci    if (extras.HasParam("requireAbilityList")) {
51280922886Sopenharmony_ci        auto value = extras.GetParam("requireAbilityList");
51380922886Sopenharmony_ci        AAFwk::IArray* list = AAFwk::IArray::Query(value);
51480922886Sopenharmony_ci        if (list != nullptr && AAFwk::Array::IsStringArray(list)) {
51580922886Sopenharmony_ci            SetExtrasInner(list);
51680922886Sopenharmony_ci        }
51780922886Sopenharmony_ci    }
51880922886Sopenharmony_ci#endif
51980922886Sopenharmony_ci    {
52080922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
52180922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
52280922886Sopenharmony_ci            if (controller != nullptr) {
52380922886Sopenharmony_ci                controller->HandleExtrasChange(extras);
52480922886Sopenharmony_ci            }
52580922886Sopenharmony_ci        }
52680922886Sopenharmony_ci    }
52780922886Sopenharmony_ci
52880922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
52980922886Sopenharmony_ci    if (remoteSource_ != nullptr) {
53080922886Sopenharmony_ci        auto ret = remoteSource_->SetExtrasRemote(extras);
53180922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetRemoteExtras failed");
53280922886Sopenharmony_ci    }
53380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
53480922886Sopenharmony_ci}
53580922886Sopenharmony_ci
53680922886Sopenharmony_cisptr<IRemoteObject> AVSessionItem::GetControllerInner()
53780922886Sopenharmony_ci{
53880922886Sopenharmony_ci    std::lock_guard controllerLockGuard(controllersLock_);
53980922886Sopenharmony_ci    auto iter = controllers_.find(GetPid());
54080922886Sopenharmony_ci    if (iter != controllers_.end()) {
54180922886Sopenharmony_ci        return iter->second;
54280922886Sopenharmony_ci    }
54380922886Sopenharmony_ci
54480922886Sopenharmony_ci    sptr<AVSessionItem> session(this);
54580922886Sopenharmony_ci    sptr<AVControllerItem> result = new(std::nothrow) AVControllerItem(GetPid(), session, userId_);
54680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "malloc controller failed");
54780922886Sopenharmony_ci    result->isFromSession_ = true;
54880922886Sopenharmony_ci    SLOGI("New controller from sessionItem when get controller.");
54980922886Sopenharmony_ci    controllers_.insert({GetPid(), result});
55080922886Sopenharmony_ci    return result;
55180922886Sopenharmony_ci}
55280922886Sopenharmony_ci
55380922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
55480922886Sopenharmony_civoid AVSessionItem::InitAVCastControllerProxy()
55580922886Sopenharmony_ci{
55680922886Sopenharmony_ci    if (castControllerProxy_ == nullptr) {
55780922886Sopenharmony_ci        SLOGI("CastControllerProxy is null, start get new proxy");
55880922886Sopenharmony_ci        {
55980922886Sopenharmony_ci            std::lock_guard lockGuard(castHandleLock_);
56080922886Sopenharmony_ci            castControllerProxy_ = AVRouter::GetInstance().GetRemoteController(castHandle_);
56180922886Sopenharmony_ci        }
56280922886Sopenharmony_ci    }
56380922886Sopenharmony_ci}
56480922886Sopenharmony_ci
56580922886Sopenharmony_civoid AVSessionItem::ReportAVCastControllerInfo()
56680922886Sopenharmony_ci{
56780922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
56880922886Sopenharmony_ci        "API_NAME", "getAVCastController",
56980922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
57080922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
57180922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
57280922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
57380922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
57480922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
57580922886Sopenharmony_ci}
57680922886Sopenharmony_ci
57780922886Sopenharmony_cisptr<IRemoteObject> AVSessionItem::GetAVCastControllerInner()
57880922886Sopenharmony_ci{
57980922886Sopenharmony_ci    InitAVCastControllerProxy();
58080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, nullptr,
58180922886Sopenharmony_ci        "castControllerProxy_ is null when get avCastController");
58280922886Sopenharmony_ci    sptr<AVCastControllerItem> castController = new (std::nothrow) AVCastControllerItem();
58380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castController != nullptr, nullptr, "malloc avCastController failed");
58480922886Sopenharmony_ci    std::shared_ptr<AVCastControllerItem> sharedPtr = std::shared_ptr<AVCastControllerItem>(castController.GetRefPtr(),
58580922886Sopenharmony_ci        [holder = castController](const auto*) {});
58680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(sharedPtr != nullptr, nullptr, "malloc AVCastControllerItem failed");
58780922886Sopenharmony_ci    ReportAVCastControllerInfo();
58880922886Sopenharmony_ci
58980922886Sopenharmony_ci    auto callback = [this](int32_t cmd, std::vector<int32_t>& supportedCastCmds) {
59080922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
59180922886Sopenharmony_ci        SLOGI("process cast valid cmd: %{public}d", cmd);
59280922886Sopenharmony_ci        if (cmd == AVCastControlCommand::CAST_CONTROL_CMD_INVALID) {
59380922886Sopenharmony_ci            supportedCastCmds_.clear();
59480922886Sopenharmony_ci            supportedCastCmds = supportedCastCmds_;
59580922886Sopenharmony_ci            HandleCastValidCommandChange(supportedCastCmds_);
59680922886Sopenharmony_ci            return;
59780922886Sopenharmony_ci        }
59880922886Sopenharmony_ci        if (cmd == AVCastControlCommand::CAST_CONTROL_CMD_MAX) {
59980922886Sopenharmony_ci            supportedCastCmds = supportedCastCmds_;
60080922886Sopenharmony_ci            return;
60180922886Sopenharmony_ci        }
60280922886Sopenharmony_ci        if (descriptor_.sessionTag_ == "RemoteCast") {
60380922886Sopenharmony_ci            SLOGI("sink session should not modify valid cmds");
60480922886Sopenharmony_ci            supportedCastCmds = {};
60580922886Sopenharmony_ci            return;
60680922886Sopenharmony_ci        }
60780922886Sopenharmony_ci        if (cmd > removeCmdStep_) {
60880922886Sopenharmony_ci            DeleteSupportCastCommand(cmd - removeCmdStep_);
60980922886Sopenharmony_ci        } else {
61080922886Sopenharmony_ci            AddSupportCastCommand(cmd);
61180922886Sopenharmony_ci        }
61280922886Sopenharmony_ci        supportedCastCmds = supportedCastCmds_;
61380922886Sopenharmony_ci        return;
61480922886Sopenharmony_ci    }
61580922886Sopenharmony_ci
61680922886Sopenharmony_ci    sharedPtr->Init(castControllerProxy_, callback);
61780922886Sopenharmony_ci    {
61880922886Sopenharmony_ci        std::lock_guard lockGuard(castControllersLock_);
61980922886Sopenharmony_ci        castControllers_.emplace_back(sharedPtr);
62080922886Sopenharmony_ci    }
62180922886Sopenharmony_ci    sptr<IRemoteObject> remoteObject = castController;
62280922886Sopenharmony_ci
62380922886Sopenharmony_ci    sharedPtr->SetSessionTag(descriptor_.sessionTag_);
62480922886Sopenharmony_ci    InitializeCastCommands();
62580922886Sopenharmony_ci    return remoteObject;
62680922886Sopenharmony_ci}
62780922886Sopenharmony_ci
62880922886Sopenharmony_civoid AVSessionItem::ReleaseAVCastControllerInner()
62980922886Sopenharmony_ci{
63080922886Sopenharmony_ci    SLOGI("Release AVCastControllerInner");
63180922886Sopenharmony_ci    std::lock_guard lockGuard(castControllersLock_);
63280922886Sopenharmony_ci    for (auto controller : castControllers_) {
63380922886Sopenharmony_ci        if (controller != nullptr) {
63480922886Sopenharmony_ci            controller->Destroy();
63580922886Sopenharmony_ci        }
63680922886Sopenharmony_ci    }
63780922886Sopenharmony_ci    castControllerProxy_ = nullptr;
63880922886Sopenharmony_ci}
63980922886Sopenharmony_ci#endif
64080922886Sopenharmony_ci
64180922886Sopenharmony_ciint32_t AVSessionItem::RegisterCallbackInner(const sptr<IAVSessionCallback>& callback)
64280922886Sopenharmony_ci{
64380922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
64480922886Sopenharmony_ci    callback_ = callback;
64580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
64680922886Sopenharmony_ci}
64780922886Sopenharmony_ci// LCOV_EXCL_STOP
64880922886Sopenharmony_ci
64980922886Sopenharmony_ciint32_t AVSessionItem::Activate()
65080922886Sopenharmony_ci{
65180922886Sopenharmony_ci    descriptor_.isActive_ = true;
65280922886Sopenharmony_ci    std::lock_guard controllerLockGuard(controllersLock_);
65380922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
65480922886Sopenharmony_ci        "API_NAME", "Activate",
65580922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
65680922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
65780922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
65880922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
65980922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
66080922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
66180922886Sopenharmony_ci    for (const auto& [pid, controller] : controllers_) {
66280922886Sopenharmony_ci        if (controller != nullptr) {
66380922886Sopenharmony_ci            controller->HandleActiveStateChange(true);
66480922886Sopenharmony_ci        }
66580922886Sopenharmony_ci    }
66680922886Sopenharmony_ci    if (descriptor_.sessionType_ == AVSession::SESSION_TYPE_VOICE_CALL ||
66780922886Sopenharmony_ci        descriptor_.sessionType_ == AVSession::SESSION_TYPE_VIDEO_CALL) {
66880922886Sopenharmony_ci        SLOGI("set audio scene for voip start");
66980922886Sopenharmony_ci        AudioSystemManager *audioManager = AudioSystemManager::GetInstance();
67080922886Sopenharmony_ci        AudioScene audioScene = AudioScene::AUDIO_SCENE_CALL_START;
67180922886Sopenharmony_ci        if (audioManager != nullptr) {
67280922886Sopenharmony_ci            audioManager->SetAudioScene(audioScene);
67380922886Sopenharmony_ci        }
67480922886Sopenharmony_ci    }
67580922886Sopenharmony_ci
67680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
67780922886Sopenharmony_ci}
67880922886Sopenharmony_ci
67980922886Sopenharmony_ci// LCOV_EXCL_START
68080922886Sopenharmony_ciint32_t AVSessionItem::Deactivate()
68180922886Sopenharmony_ci{
68280922886Sopenharmony_ci    descriptor_.isActive_ = false;
68380922886Sopenharmony_ci    std::lock_guard controllerLockGuard(controllersLock_);
68480922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
68580922886Sopenharmony_ci        "API_NAME", "Deactivate",
68680922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
68780922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
68880922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
68980922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
69080922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
69180922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
69280922886Sopenharmony_ci    for (const auto& [pid, controller] : controllers_) {
69380922886Sopenharmony_ci        if (controller != nullptr) {
69480922886Sopenharmony_ci            controller->HandleActiveStateChange(false);
69580922886Sopenharmony_ci        }
69680922886Sopenharmony_ci    }
69780922886Sopenharmony_ci    if (descriptor_.sessionType_ == AVSession::SESSION_TYPE_VOICE_CALL ||
69880922886Sopenharmony_ci        descriptor_.sessionType_ == AVSession::SESSION_TYPE_VIDEO_CALL) {
69980922886Sopenharmony_ci        SLOGI("set audio scene for voip end");
70080922886Sopenharmony_ci        AudioSystemManager *audioManager = AudioSystemManager::GetInstance();
70180922886Sopenharmony_ci        AudioScene audioScene = AudioScene::AUDIO_SCENE_CALL_END;
70280922886Sopenharmony_ci        if (audioManager != nullptr) {
70380922886Sopenharmony_ci            audioManager->SetAudioScene(audioScene);
70480922886Sopenharmony_ci        }
70580922886Sopenharmony_ci    }
70680922886Sopenharmony_ci
70780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
70880922886Sopenharmony_ci}
70980922886Sopenharmony_ci// LCOV_EXCL_STOP
71080922886Sopenharmony_ci
71180922886Sopenharmony_cibool AVSessionItem::IsActive()
71280922886Sopenharmony_ci{
71380922886Sopenharmony_ci    return descriptor_.isActive_;
71480922886Sopenharmony_ci}
71580922886Sopenharmony_ci
71680922886Sopenharmony_ci// LCOV_EXCL_START
71780922886Sopenharmony_ciint32_t AVSessionItem::AddSupportCommand(int32_t cmd)
71880922886Sopenharmony_ci{
71980922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
72080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
72180922886Sopenharmony_ci    SLOGI("AddSupportCommand=%{public}d", cmd);
72280922886Sopenharmony_ci    if (cmd == AVControlCommand::SESSION_CMD_MEDIA_KEY_SUPPORT) {
72380922886Sopenharmony_ci        SLOGI("enable media key event listen");
72480922886Sopenharmony_ci        isMediaKeySupport = true;
72580922886Sopenharmony_ci        return AVSESSION_SUCCESS;
72680922886Sopenharmony_ci    }
72780922886Sopenharmony_ci    auto iter = std::find(supportedCmd_.begin(), supportedCmd_.end(), cmd);
72880922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(iter == supportedCmd_.end(), AVSESSION_SUCCESS, "cmd already been added");
72980922886Sopenharmony_ci    {
73080922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
73180922886Sopenharmony_ci        supportedCmd_.push_back(cmd);
73280922886Sopenharmony_ci    }
73380922886Sopenharmony_ci    std::string API_PARAM_STRING = "cmd :" + std::to_string(cmd);
73480922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
73580922886Sopenharmony_ci        "API_NAME", "OnEvent",
73680922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
73780922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
73880922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
73980922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
74080922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
74180922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
74280922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
74380922886Sopenharmony_ci    ProcessFrontSession("AddSupportCommand");
74480922886Sopenharmony_ci
74580922886Sopenharmony_ci    {
74680922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
74780922886Sopenharmony_ci        SLOGI("send add command event to controller, size:%{public}d", static_cast<int>(controllers_.size()));
74880922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
74980922886Sopenharmony_ci            if (controller != nullptr) {
75080922886Sopenharmony_ci                controller->HandleValidCommandChange(supportedCmd_);
75180922886Sopenharmony_ci            }
75280922886Sopenharmony_ci        }
75380922886Sopenharmony_ci    }
75480922886Sopenharmony_ci
75580922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
75680922886Sopenharmony_ci    AddSessionCommandToCast(cmd);
75780922886Sopenharmony_ci#endif
75880922886Sopenharmony_ci    return AVSESSION_SUCCESS;
75980922886Sopenharmony_ci}
76080922886Sopenharmony_ci
76180922886Sopenharmony_ciint32_t AVSessionItem::DeleteSupportCommand(int32_t cmd)
76280922886Sopenharmony_ci{
76380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
76480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
76580922886Sopenharmony_ci    SLOGI("DeleteSupportCommand=%{public}d", cmd);
76680922886Sopenharmony_ci    if (cmd == AVControlCommand::SESSION_CMD_MEDIA_KEY_SUPPORT) {
76780922886Sopenharmony_ci        SLOGI("disable media key event listen");
76880922886Sopenharmony_ci        isMediaKeySupport = false;
76980922886Sopenharmony_ci        return AVSESSION_SUCCESS;
77080922886Sopenharmony_ci    }
77180922886Sopenharmony_ci    auto iter = std::remove(supportedCmd_.begin(), supportedCmd_.end(), cmd);
77280922886Sopenharmony_ci    {
77380922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
77480922886Sopenharmony_ci        supportedCmd_.erase(iter, supportedCmd_.end());
77580922886Sopenharmony_ci    }
77680922886Sopenharmony_ci    std::string API_PARAM_STRING = "cmd :" + std::to_string(cmd);
77780922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
77880922886Sopenharmony_ci        "API_NAME", "OffEvent",
77980922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
78080922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
78180922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
78280922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
78380922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
78480922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
78580922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
78680922886Sopenharmony_ci    ProcessFrontSession("DeleteSupportCommand");
78780922886Sopenharmony_ci
78880922886Sopenharmony_ci    {
78980922886Sopenharmony_ci        SLOGI("send delete command event to controller, size:%{public}d", static_cast<int>(controllers_.size()));
79080922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
79180922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
79280922886Sopenharmony_ci            if (controller != nullptr) {
79380922886Sopenharmony_ci                controller->HandleValidCommandChange(supportedCmd_);
79480922886Sopenharmony_ci            }
79580922886Sopenharmony_ci        }
79680922886Sopenharmony_ci    }
79780922886Sopenharmony_ci
79880922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
79980922886Sopenharmony_ci    RemoveSessionCommandFromCast(cmd);
80080922886Sopenharmony_ci#endif
80180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
80280922886Sopenharmony_ci}
80380922886Sopenharmony_ci// LCOV_EXCL_STOP
80480922886Sopenharmony_ci
80580922886Sopenharmony_ciint32_t AVSessionItem::SetSessionEvent(const std::string& event, const AAFwk::WantParams& args)
80680922886Sopenharmony_ci{
80780922886Sopenharmony_ci    {
80880922886Sopenharmony_ci        std::lock_guard controllerLockGuard(controllersLock_);
80980922886Sopenharmony_ci        for (const auto& [pid, controller] : controllers_) {
81080922886Sopenharmony_ci            if (controller != nullptr) {
81180922886Sopenharmony_ci                controller->HandleSetSessionEvent(event, args);
81280922886Sopenharmony_ci            }
81380922886Sopenharmony_ci        }
81480922886Sopenharmony_ci    }
81580922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
81680922886Sopenharmony_ci    if (remoteSource_ != nullptr) {
81780922886Sopenharmony_ci        auto ret = remoteSource_->SetSessionEventRemote(event, args);
81880922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetSessionEvent failed");
81980922886Sopenharmony_ci    }
82080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
82180922886Sopenharmony_ci}
82280922886Sopenharmony_ci
82380922886Sopenharmony_cistd::string AVSessionItem::GetAnonymousDeviceId(std::string deviceId)
82480922886Sopenharmony_ci{
82580922886Sopenharmony_ci    if (deviceId.empty() || deviceId.length() < DEVICE_ID_MIN_LEN) {
82680922886Sopenharmony_ci        return "unknown";
82780922886Sopenharmony_ci    }
82880922886Sopenharmony_ci    const uint32_t half = DEVICE_ID_MIN_LEN / 2;
82980922886Sopenharmony_ci    return deviceId.substr(0, half) + "**" + deviceId.substr(deviceId.length() - half);
83080922886Sopenharmony_ci}
83180922886Sopenharmony_ci
83280922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
83380922886Sopenharmony_ciint32_t AVSessionItem::RegisterListenerStreamToCast(const std::map<std::string, std::string>& serviceNameMapState,
83480922886Sopenharmony_ci    DeviceInfo deviceInfo)
83580922886Sopenharmony_ci{
83680922886Sopenharmony_ci    if (castHandle_ > 0) {
83780922886Sopenharmony_ci        return AVSESSION_ERROR;
83880922886Sopenharmony_ci    }
83980922886Sopenharmony_ci    {
84080922886Sopenharmony_ci        std::lock_guard displayListenerLockGuard(mirrorToStreamLock_);
84180922886Sopenharmony_ci        mirrorToStreamFlag_ = true;
84280922886Sopenharmony_ci    }
84380922886Sopenharmony_ci    std::lock_guard lockGuard(castHandleLock_);
84480922886Sopenharmony_ci    castServiceNameMapState_ = serviceNameMapState;
84580922886Sopenharmony_ci    OutputDeviceInfo outputDeviceInfo;
84680922886Sopenharmony_ci    outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
84780922886Sopenharmony_ci    int64_t castHandle = AVRouter::GetInstance().StartCast(outputDeviceInfo, castServiceNameMapState_);
84880922886Sopenharmony_ci    castHandle_ = castHandle;
84980922886Sopenharmony_ci    castHandleDeviceId_ = deviceInfo.deviceId_;
85080922886Sopenharmony_ci    SLOGI("RegisterListenerStreamToCast check handle set to %{public}ld", castHandle_);
85180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castHandle != AVSESSION_ERROR, AVSESSION_ERROR, "StartCast failed");
85280922886Sopenharmony_ci    counter_ = firstStep;
85380922886Sopenharmony_ci    AVRouter::GetInstance().RegisterCallback(castHandle, cssListener_);
85480922886Sopenharmony_ci    AVRouter::GetInstance().SetServiceAllConnectState(castHandle, deviceInfo);
85580922886Sopenharmony_ci    counter_ = secondStep;
85680922886Sopenharmony_ci    UpdateCastDeviceMap(deviceInfo);
85780922886Sopenharmony_ci
85880922886Sopenharmony_ci    DoContinuousTaskRegister();
85980922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_CAST_CONTROL",
86080922886Sopenharmony_ci        "CONTROL_TYPE", "MirrorTostreamCast",
86180922886Sopenharmony_ci        "PEER_DEVICE_ID", GetAnonymousDeviceId(deviceInfo.deviceId_),
86280922886Sopenharmony_ci        "PEER_DEVICE_NAME", deviceInfo.deviceName_,
86380922886Sopenharmony_ci        "PEER_DEVICE_TYPE", deviceInfo.deviceType_,
86480922886Sopenharmony_ci        "PEER_NETWORK_ID", GetAnonymousDeviceId(deviceInfo.networkId_),
86580922886Sopenharmony_ci        "PEER_SUPPORTED_PROTOCOL", deviceInfo.supportedProtocols_,
86680922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName());
86780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
86880922886Sopenharmony_ci}
86980922886Sopenharmony_ci
87080922886Sopenharmony_ci// LCOV_EXCL_START
87180922886Sopenharmony_civoid AVSessionItem::InitializeCastCommands()
87280922886Sopenharmony_ci{
87380922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
87480922886Sopenharmony_ci    // always support setVolume command
87580922886Sopenharmony_ci    auto iter = std::find(supportedCastCmds_.begin(), supportedCastCmds_.end(),
87680922886Sopenharmony_ci        AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME);
87780922886Sopenharmony_ci    if (iter == supportedCastCmds_.end()) {
87880922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SET_VOLUME);
87980922886Sopenharmony_ci    }
88080922886Sopenharmony_ci
88180922886Sopenharmony_ci    iter = std::find(supportedCastCmds_.begin(), supportedCastCmds_.end(),
88280922886Sopenharmony_ci        AVCastControlCommand::CAST_CONTROL_CMD_SET_SPEED);
88380922886Sopenharmony_ci    if (iter == supportedCastCmds_.end()) {
88480922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SET_SPEED);
88580922886Sopenharmony_ci    }
88680922886Sopenharmony_ci
88780922886Sopenharmony_ci    iter = std::find(supportedCastCmds_.begin(), supportedCastCmds_.end(),
88880922886Sopenharmony_ci        AVCastControlCommand::CAST_CONTROL_CMD_SEEK);
88980922886Sopenharmony_ci    if (iter == supportedCastCmds_.end()) {
89080922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_SEEK);
89180922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD);
89280922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_REWIND);
89380922886Sopenharmony_ci    }
89480922886Sopenharmony_ci
89580922886Sopenharmony_ci    iter = std::find(supportedCmd_.begin(), supportedCmd_.end(), AVControlCommand::SESSION_CMD_SET_LOOP_MODE);
89680922886Sopenharmony_ci    if (iter != supportedCmd_.end()) {
89780922886Sopenharmony_ci        AddSessionCommandToCast(AVControlCommand::SESSION_CMD_SET_LOOP_MODE);
89880922886Sopenharmony_ci    }
89980922886Sopenharmony_ci}
90080922886Sopenharmony_ci
90180922886Sopenharmony_ciint32_t AVSessionItem::SessionCommandToCastCommand(int32_t cmd)
90280922886Sopenharmony_ci{
90380922886Sopenharmony_ci    if (cmd == AVControlCommand::SESSION_CMD_SET_LOOP_MODE) {
90480922886Sopenharmony_ci        return AVCastControlCommand::CAST_CONTROL_CMD_SET_LOOP_MODE;
90580922886Sopenharmony_ci    }
90680922886Sopenharmony_ci    return AVCastControlCommand::CAST_CONTROL_CMD_INVALID;
90780922886Sopenharmony_ci}
90880922886Sopenharmony_ci
90980922886Sopenharmony_civoid AVSessionItem::AddSessionCommandToCast(int32_t cmd)
91080922886Sopenharmony_ci{
91180922886Sopenharmony_ci    if (cmd != AVControlCommand::SESSION_CMD_SET_LOOP_MODE) {
91280922886Sopenharmony_ci        return;
91380922886Sopenharmony_ci    }
91480922886Sopenharmony_ci
91580922886Sopenharmony_ci    if (castControllerProxy_ != nullptr) {
91680922886Sopenharmony_ci        int32_t castCmd = SessionCommandToCastCommand(cmd);
91780922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
91880922886Sopenharmony_ci        auto iter = std::find(supportedCastCmds_.begin(), supportedCastCmds_.end(), castCmd);
91980922886Sopenharmony_ci        if (iter != supportedCastCmds_.end()) {
92080922886Sopenharmony_ci            SLOGI("castCmd have already been added, cmd:%{public}d", castCmd);
92180922886Sopenharmony_ci            return;
92280922886Sopenharmony_ci        }
92380922886Sopenharmony_ci        supportedCastCmds_.push_back(castCmd);
92480922886Sopenharmony_ci        HandleCastValidCommandChange(supportedCastCmds_);
92580922886Sopenharmony_ci    }
92680922886Sopenharmony_ci}
92780922886Sopenharmony_ci
92880922886Sopenharmony_civoid AVSessionItem::RemoveSessionCommandFromCast(int32_t cmd)
92980922886Sopenharmony_ci{
93080922886Sopenharmony_ci    if (cmd != AVControlCommand::SESSION_CMD_SET_LOOP_MODE) {
93180922886Sopenharmony_ci        return;
93280922886Sopenharmony_ci    }
93380922886Sopenharmony_ci
93480922886Sopenharmony_ci    if (castControllerProxy_ != nullptr) {
93580922886Sopenharmony_ci        int32_t castCmd = SessionCommandToCastCommand(cmd);
93680922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
93780922886Sopenharmony_ci        SLOGI("remove castcmd:%{public}d", castCmd);
93880922886Sopenharmony_ci        auto iter = std::remove(supportedCastCmds_.begin(), supportedCastCmds_.end(), castCmd);
93980922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
94080922886Sopenharmony_ci        HandleCastValidCommandChange(supportedCastCmds_);
94180922886Sopenharmony_ci    }
94280922886Sopenharmony_ci}
94380922886Sopenharmony_ci
94480922886Sopenharmony_ciint32_t AVSessionItem::AddSupportCastCommand(int32_t cmd)
94580922886Sopenharmony_ci{
94680922886Sopenharmony_ci    if (cmd <= AVCastControlCommand::CAST_CONTROL_CMD_INVALID ||
94780922886Sopenharmony_ci        cmd >= AVCastControlCommand::CAST_CONTROL_CMD_MAX) {
94880922886Sopenharmony_ci        SLOGI("add invalid cmd: %{public}d", cmd);
94980922886Sopenharmony_ci        return AVSESSION_ERROR;
95080922886Sopenharmony_ci    }
95180922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
95280922886Sopenharmony_ci    if (cmd == AVCastControlCommand::CAST_CONTROL_CMD_PLAY_STATE_CHANGE) {
95380922886Sopenharmony_ci        auto iter = std::find(
95480922886Sopenharmony_ci            supportedCastCmds_.begin(), supportedCastCmds_.end(), AVCastControlCommand::CAST_CONTROL_CMD_PLAY);
95580922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(iter == supportedCastCmds_.end(), AVSESSION_SUCCESS, "cmd already been added");
95680922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PLAY);
95780922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_PAUSE);
95880922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_STOP);
95980922886Sopenharmony_ci    } else if (cmd == AVCastControlCommand::CAST_CONTROL_CMD_SEEK) {
96080922886Sopenharmony_ci        auto iter = std::find(supportedCastCmds_.begin(), supportedCastCmds_.end(), cmd);
96180922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(iter == supportedCastCmds_.end(), AVSESSION_SUCCESS, "cmd already been added");
96280922886Sopenharmony_ci        supportedCastCmds_.push_back(cmd);
96380922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD);
96480922886Sopenharmony_ci        supportedCastCmds_.push_back(AVCastControlCommand::CAST_CONTROL_CMD_REWIND);
96580922886Sopenharmony_ci    } else {
96680922886Sopenharmony_ci        auto iter = std::find(supportedCastCmds_.begin(), supportedCastCmds_.end(), cmd);
96780922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(iter == supportedCastCmds_.end(), AVSESSION_SUCCESS, "cmd already been added");
96880922886Sopenharmony_ci        supportedCastCmds_.push_back(cmd);
96980922886Sopenharmony_ci    }
97080922886Sopenharmony_ci    ProcessFrontSession("AddSupportCastCommand");
97180922886Sopenharmony_ci    HandleCastValidCommandChange(supportedCastCmds_);
97280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
97380922886Sopenharmony_ci}
97480922886Sopenharmony_ci
97580922886Sopenharmony_ciint32_t AVSessionItem::DeleteSupportCastCommand(int32_t cmd)
97680922886Sopenharmony_ci{
97780922886Sopenharmony_ci    if (cmd <= AVCastControlCommand::CAST_CONTROL_CMD_INVALID ||
97880922886Sopenharmony_ci        cmd >= AVCastControlCommand::CAST_CONTROL_CMD_MAX) {
97980922886Sopenharmony_ci        SLOGI("delete invalid cmd: %{public}d", cmd);
98080922886Sopenharmony_ci        return AVSESSION_ERROR;
98180922886Sopenharmony_ci    }
98280922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
98380922886Sopenharmony_ci    if (cmd == AVCastControlCommand::CAST_CONTROL_CMD_PLAY_STATE_CHANGE) {
98480922886Sopenharmony_ci        auto iter = std::remove(
98580922886Sopenharmony_ci            supportedCastCmds_.begin(), supportedCastCmds_.end(), AVCastControlCommand::CAST_CONTROL_CMD_PLAY);
98680922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
98780922886Sopenharmony_ci
98880922886Sopenharmony_ci        iter = std::remove(
98980922886Sopenharmony_ci            supportedCastCmds_.begin(), supportedCastCmds_.end(), AVCastControlCommand::CAST_CONTROL_CMD_PAUSE);
99080922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
99180922886Sopenharmony_ci
99280922886Sopenharmony_ci        iter = std::remove(
99380922886Sopenharmony_ci            supportedCastCmds_.begin(), supportedCastCmds_.end(), AVCastControlCommand::CAST_CONTROL_CMD_STOP);
99480922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
99580922886Sopenharmony_ci    } else if (cmd == AVCastControlCommand::CAST_CONTROL_CMD_SEEK) {
99680922886Sopenharmony_ci        auto iter = std::remove(supportedCastCmds_.begin(), supportedCastCmds_.end(), cmd);
99780922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
99880922886Sopenharmony_ci
99980922886Sopenharmony_ci        iter = std::remove(
100080922886Sopenharmony_ci            supportedCastCmds_.begin(), supportedCastCmds_.end(), AVCastControlCommand::CAST_CONTROL_CMD_FAST_FORWARD);
100180922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
100280922886Sopenharmony_ci
100380922886Sopenharmony_ci        iter = std::remove(
100480922886Sopenharmony_ci            supportedCastCmds_.begin(), supportedCastCmds_.end(), AVCastControlCommand::CAST_CONTROL_CMD_REWIND);
100580922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
100680922886Sopenharmony_ci    } else {
100780922886Sopenharmony_ci        auto iter = std::remove(supportedCastCmds_.begin(), supportedCastCmds_.end(), cmd);
100880922886Sopenharmony_ci        supportedCastCmds_.erase(iter, supportedCastCmds_.end());
100980922886Sopenharmony_ci    }
101080922886Sopenharmony_ci    ProcessFrontSession("DeleteSupportCastCommand");
101180922886Sopenharmony_ci    HandleCastValidCommandChange(supportedCastCmds_);
101280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
101380922886Sopenharmony_ci}
101480922886Sopenharmony_ci
101580922886Sopenharmony_civoid AVSessionItem::HandleCastValidCommandChange(std::vector<int32_t> &cmds)
101680922886Sopenharmony_ci{
101780922886Sopenharmony_ci    std::lock_guard lockGuard(castControllersLock_);
101880922886Sopenharmony_ci    SLOGI("send command change event to controller, controller size: %{public}d, cmds size is: %{public}d,",
101980922886Sopenharmony_ci        static_cast<int>(castControllers_.size()), cmds.size());
102080922886Sopenharmony_ci    for (auto controller : castControllers_) {
102180922886Sopenharmony_ci        if (controller != nullptr) {
102280922886Sopenharmony_ci            controller->HandleCastValidCommandChange(cmds);
102380922886Sopenharmony_ci        }
102480922886Sopenharmony_ci    }
102580922886Sopenharmony_ci}
102680922886Sopenharmony_ci
102780922886Sopenharmony_ciint32_t AVSessionItem::ReleaseCast()
102880922886Sopenharmony_ci{
102980922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
103080922886Sopenharmony_ci        "API_NAME", "StopCasting",
103180922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
103280922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
103380922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
103480922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
103580922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
103680922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
103780922886Sopenharmony_ci    return StopCast();
103880922886Sopenharmony_ci}
103980922886Sopenharmony_ci
104080922886Sopenharmony_ciint32_t AVSessionItem::CastAddToCollaboration(const OutputDeviceInfo& outputDeviceInfo)
104180922886Sopenharmony_ci{
104280922886Sopenharmony_ci    SLOGI("enter CastAddToCollaboration");
104380922886Sopenharmony_ci    if (castDeviceInfoMap_.count(outputDeviceInfo.deviceInfos_[0].deviceId_) != 1) {
104480922886Sopenharmony_ci        SLOGE("deviceId map deviceinfo is not exit");
104580922886Sopenharmony_ci        return AVSESSION_ERROR;
104680922886Sopenharmony_ci    }
104780922886Sopenharmony_ci    ListenCollaborationRejectToStopCast();
104880922886Sopenharmony_ci    DeviceInfo cacheDeviceInfo = castDeviceInfoMap_[outputDeviceInfo.deviceInfos_[0].deviceId_];
104980922886Sopenharmony_ci    if (cacheDeviceInfo.networkId_.empty()) {
105080922886Sopenharmony_ci        SLOGI("untrusted device, networkId is empty, then input deviceId to ApplyAdvancedResource");
105180922886Sopenharmony_ci        collaborationNeedNetworkId_ = cacheDeviceInfo.deviceId_;
105280922886Sopenharmony_ci        networkIdIsEmpty_ = true;
105380922886Sopenharmony_ci    } else {
105480922886Sopenharmony_ci        collaborationNeedNetworkId_= cacheDeviceInfo.networkId_;
105580922886Sopenharmony_ci    }
105680922886Sopenharmony_ci    CollaborationManager::GetInstance().ApplyAdvancedResource(collaborationNeedNetworkId_.c_str());
105780922886Sopenharmony_ci    //wait collaboration callback 10s
105880922886Sopenharmony_ci    std::unique_lock <std::mutex> applyResultLock(collaborationApplyResultMutex_);
105980922886Sopenharmony_ci    bool flag = connectWaitCallbackCond_.wait_for(applyResultLock, std::chrono::seconds(collaborationCallbackTimeOut_),
106080922886Sopenharmony_ci        [this]() {
106180922886Sopenharmony_ci            return applyResultFlag_;
106280922886Sopenharmony_ci    });
106380922886Sopenharmony_ci    //wait user decision collaboration callback 60s
106480922886Sopenharmony_ci    if (waitUserDecisionFlag_) {
106580922886Sopenharmony_ci        flag = connectWaitCallbackCond_.wait_for(applyResultLock,
106680922886Sopenharmony_ci            std::chrono::seconds(collaborationUserCallbackTimeOut_),
106780922886Sopenharmony_ci        [this]() {
106880922886Sopenharmony_ci            return applyUserResultFlag_;
106980922886Sopenharmony_ci        });
107080922886Sopenharmony_ci    }
107180922886Sopenharmony_ci    applyResultFlag_ = false;
107280922886Sopenharmony_ci    applyUserResultFlag_ = false;
107380922886Sopenharmony_ci    waitUserDecisionFlag_ = false;
107480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(flag, ERR_WAIT_ALLCONNECT_TIMEOUT, "collaboration callback timeout");
107580922886Sopenharmony_ci    if (collaborationRejectFlag_) {
107680922886Sopenharmony_ci        collaborationRejectFlag_ = false;
107780922886Sopenharmony_ci        SLOGE("collaboration callback reject");
107880922886Sopenharmony_ci        return ERR_ALLCONNECT_CAST_REJECT;
107980922886Sopenharmony_ci    }
108080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
108180922886Sopenharmony_ci}
108280922886Sopenharmony_ci
108380922886Sopenharmony_ciint32_t AVSessionItem::StartCast(const OutputDeviceInfo& outputDeviceInfo)
108480922886Sopenharmony_ci{
108580922886Sopenharmony_ci    std::lock_guard lockGuard(castHandleLock_);
108680922886Sopenharmony_ci
108780922886Sopenharmony_ci    // unregister pre castSession callback to avoid previous session timeout disconnect influence current session
108880922886Sopenharmony_ci    if (castHandle_ > 0) {
108980922886Sopenharmony_ci        if (castHandleDeviceId_ == outputDeviceInfo.deviceInfos_[0].deviceId_) {
109080922886Sopenharmony_ci            SLOGI("repeat startcast %{public}ld", castHandle_);
109180922886Sopenharmony_ci            return AVSESSION_ERROR;
109280922886Sopenharmony_ci        } else {
109380922886Sopenharmony_ci            SLOGI("cast check with pre cast alive %{public}ld, unregister callback", castHandle_);
109480922886Sopenharmony_ci            AVRouter::GetInstance().UnRegisterCallback(castHandle_, cssListener_);
109580922886Sopenharmony_ci        }
109680922886Sopenharmony_ci    }
109780922886Sopenharmony_ci    int32_t flag = CastAddToCollaboration(outputDeviceInfo);
109880922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(flag == AVSESSION_SUCCESS, AVSESSION_ERROR, "collaboration to start cast fail");
109980922886Sopenharmony_ci    int64_t castHandle = AVRouter::GetInstance().StartCast(outputDeviceInfo, castServiceNameMapState_);
110080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castHandle != AVSESSION_ERROR, AVSESSION_ERROR, "StartCast failed");
110180922886Sopenharmony_ci
110280922886Sopenharmony_ci    castHandle_ = castHandle;
110380922886Sopenharmony_ci    SLOGI("start cast check handle set to %{public}ld", castHandle_);
110480922886Sopenharmony_ci    int32_t ret = AddDevice(static_cast<int32_t>(castHandle), outputDeviceInfo);
110580922886Sopenharmony_ci    if (ret == AVSESSION_SUCCESS) {
110680922886Sopenharmony_ci        castHandleDeviceId_ = outputDeviceInfo.deviceInfos_[0].deviceId_;
110780922886Sopenharmony_ci    }
110880922886Sopenharmony_ci    DoContinuousTaskRegister();
110980922886Sopenharmony_ci    return ret;
111080922886Sopenharmony_ci}
111180922886Sopenharmony_ci
111280922886Sopenharmony_ciint32_t AVSessionItem::AddDevice(const int64_t castHandle, const OutputDeviceInfo& outputDeviceInfo)
111380922886Sopenharmony_ci{
111480922886Sopenharmony_ci    SLOGI("Add device process");
111580922886Sopenharmony_ci    std::lock_guard lockGuard(castHandleLock_);
111680922886Sopenharmony_ci    AVRouter::GetInstance().RegisterCallback(castHandle_, cssListener_);
111780922886Sopenharmony_ci    int32_t castId = static_cast<int32_t>(castHandle_);
111880922886Sopenharmony_ci    int32_t ret = AVRouter::GetInstance().AddDevice(castId, outputDeviceInfo);
111980922886Sopenharmony_ci    SLOGI("Add device process with ret %{public}d", ret);
112080922886Sopenharmony_ci    return ret;
112180922886Sopenharmony_ci}
112280922886Sopenharmony_ci
112380922886Sopenharmony_civoid AVSessionItem::DealCastState(int32_t castState)
112480922886Sopenharmony_ci{
112580922886Sopenharmony_ci    if (newCastState == castState) {
112680922886Sopenharmony_ci        isUpdate = false;
112780922886Sopenharmony_ci    } else {
112880922886Sopenharmony_ci        if (counter_ == firstStep) {
112980922886Sopenharmony_ci            newCastState = virtualDeviceStateConnection;
113080922886Sopenharmony_ci        } else {
113180922886Sopenharmony_ci            newCastState = castState;
113280922886Sopenharmony_ci        }
113380922886Sopenharmony_ci        isUpdate = true;
113480922886Sopenharmony_ci    }
113580922886Sopenharmony_ci}
113680922886Sopenharmony_ci
113780922886Sopenharmony_civoid AVSessionItem::DealDisconnect(DeviceInfo deviceInfo)
113880922886Sopenharmony_ci{
113980922886Sopenharmony_ci    SLOGI("Is remotecast, received disconnect event for castHandle_: %{public}ld", castHandle_);
114080922886Sopenharmony_ci    AVRouter::GetInstance().UnRegisterCallback(castHandle_, cssListener_);
114180922886Sopenharmony_ci    AVRouter::GetInstance().StopCastSession(castHandle_);
114280922886Sopenharmony_ci    castHandle_ = -1;
114380922886Sopenharmony_ci    castHandleDeviceId_ = "-100";
114480922886Sopenharmony_ci    DoContinuousTaskUnregister();
114580922886Sopenharmony_ci    castControllerProxy_ = nullptr;
114680922886Sopenharmony_ci    {
114780922886Sopenharmony_ci        std::lock_guard lockGuard(avsessionItemLock_);
114880922886Sopenharmony_ci        supportedCastCmds_.clear();
114980922886Sopenharmony_ci    }
115080922886Sopenharmony_ci    SaveLocalDeviceInfo();
115180922886Sopenharmony_ci    ReportStopCastFinish("AVSessionItem::OnCastStateChange", deviceInfo);
115280922886Sopenharmony_ci}
115380922886Sopenharmony_ci
115480922886Sopenharmony_civoid AVSessionItem::DealCollaborationPublishState(int32_t castState, DeviceInfo deviceInfo)
115580922886Sopenharmony_ci{
115680922886Sopenharmony_ci    SLOGI("enter DealCollaborationPublishState");
115780922886Sopenharmony_ci    std::lock_guard displayListenerLockGuard(mirrorToStreamLock_);
115880922886Sopenharmony_ci    if (mirrorToStreamFlag_) {
115980922886Sopenharmony_ci        mirrorToStreamFlag_ = false;
116080922886Sopenharmony_ci        SLOGI("cast not add to collaboration when mirror to stream cast");
116180922886Sopenharmony_ci        return;
116280922886Sopenharmony_ci    }
116380922886Sopenharmony_ci    if (castState == castConnectStateForConnected_) { // 6 is connected status (stream)
116480922886Sopenharmony_ci        if (networkIdIsEmpty_) {
116580922886Sopenharmony_ci            SLOGI("untrusted device, networkId is empty, get netwokId from castplus");
116680922886Sopenharmony_ci            AVRouter::GetInstance().GetRemoteNetWorkId(
116780922886Sopenharmony_ci                castHandle_, deviceInfo.deviceId_, collaborationNeedNetworkId_);
116880922886Sopenharmony_ci            networkIdIsEmpty_ = false;
116980922886Sopenharmony_ci        }
117080922886Sopenharmony_ci        if (collaborationNeedNetworkId_.empty()) {
117180922886Sopenharmony_ci            SLOGI("cast add to collaboration in peer, get netwokId from castplus");
117280922886Sopenharmony_ci            AVRouter::GetInstance().GetRemoteNetWorkId(
117380922886Sopenharmony_ci                castHandle_, deviceInfo.deviceId_, collaborationNeedNetworkId_);
117480922886Sopenharmony_ci        }
117580922886Sopenharmony_ci        CollaborationManager::GetInstance().PublishServiceState(collaborationNeedNetworkId_.c_str(),
117680922886Sopenharmony_ci            ServiceCollaborationManagerBussinessStatus::SCM_CONNECTED);
117780922886Sopenharmony_ci    }
117880922886Sopenharmony_ci    if (castState == castConnectStateForDisconnect_) { // 5 is disconnected status
117980922886Sopenharmony_ci        CollaborationManager::GetInstance().PublishServiceState(collaborationNeedNetworkId_.c_str(),
118080922886Sopenharmony_ci            ServiceCollaborationManagerBussinessStatus::SCM_IDLE);
118180922886Sopenharmony_ci    }
118280922886Sopenharmony_ci}
118380922886Sopenharmony_ci
118480922886Sopenharmony_civoid AVSessionItem::OnCastStateChange(int32_t castState, DeviceInfo deviceInfo)
118580922886Sopenharmony_ci{
118680922886Sopenharmony_ci    SLOGI("OnCastStateChange in with state: %{public}d | id: %{public}s", static_cast<int32_t>(castState),
118780922886Sopenharmony_ci        deviceInfo.deviceId_.c_str());
118880922886Sopenharmony_ci    DealCollaborationPublishState(castState, deviceInfo);
118980922886Sopenharmony_ci    DealCastState(castState);
119080922886Sopenharmony_ci    if (castState == streamStateConnection && counter_ == secondStep) {
119180922886Sopenharmony_ci        SLOGI("interception of one devicestate=6 transmission");
119280922886Sopenharmony_ci        counter_ = 0;
119380922886Sopenharmony_ci        return;
119480922886Sopenharmony_ci    }
119580922886Sopenharmony_ci    OutputDeviceInfo outputDeviceInfo;
119680922886Sopenharmony_ci    if (castDeviceInfoMap_.count(deviceInfo.deviceId_) > 0) {
119780922886Sopenharmony_ci        outputDeviceInfo.deviceInfos_.emplace_back(castDeviceInfoMap_[deviceInfo.deviceId_]);
119880922886Sopenharmony_ci    } else {
119980922886Sopenharmony_ci        outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
120080922886Sopenharmony_ci    }
120180922886Sopenharmony_ci    if (castState == castConnectStateForConnected_) { // 6 is connected status (stream)
120280922886Sopenharmony_ci        castState = 1; // 1 is connected status (local)
120380922886Sopenharmony_ci        descriptor_.outputDeviceInfo_ = outputDeviceInfo;
120480922886Sopenharmony_ci        ReportConnectFinish("AVSessionItem::OnCastStateChange", deviceInfo);
120580922886Sopenharmony_ci        if (callStartCallback_) {
120680922886Sopenharmony_ci            SLOGI("AVSessionItem send callStart event to service for connected");
120780922886Sopenharmony_ci            callStartCallback_(*this);
120880922886Sopenharmony_ci        }
120980922886Sopenharmony_ci    }
121080922886Sopenharmony_ci    if (castState == castConnectStateForDisconnect_) { // 5 is disconnected status
121180922886Sopenharmony_ci        castState = 6; // 6 is disconnected status of AVSession
121280922886Sopenharmony_ci        DealDisconnect(deviceInfo);
121380922886Sopenharmony_ci    }
121480922886Sopenharmony_ci    HandleOutputDeviceChange(castState, outputDeviceInfo);
121580922886Sopenharmony_ci    {
121680922886Sopenharmony_ci        std::lock_guard controllersLockGuard(controllersLock_);
121780922886Sopenharmony_ci        for (const auto& controller : controllers_) {
121880922886Sopenharmony_ci            if (controller.second != nullptr) {
121980922886Sopenharmony_ci                controller.second->HandleOutputDeviceChange(castState, outputDeviceInfo);
122080922886Sopenharmony_ci            }
122180922886Sopenharmony_ci        }
122280922886Sopenharmony_ci    }
122380922886Sopenharmony_ci    {
122480922886Sopenharmony_ci        std::lock_guard lockGuard(destroyLock_);
122580922886Sopenharmony_ci        if (castState == ConnectionState::STATE_DISCONNECTED &&
122680922886Sopenharmony_ci            descriptor_.sessionTag_ == "RemoteCast" && !isDestroyed_) {
122780922886Sopenharmony_ci            SLOGI("Sink cast session is disconnected, avsession item need be destroyed.");
122880922886Sopenharmony_ci            Destroy();
122980922886Sopenharmony_ci        }
123080922886Sopenharmony_ci    }
123180922886Sopenharmony_ci}
123280922886Sopenharmony_ci
123380922886Sopenharmony_civoid AVSessionItem::OnCastEventRecv(int32_t errorCode, std::string& errorMsg)
123480922886Sopenharmony_ci{
123580922886Sopenharmony_ci    SLOGI("OnCastEventRecv in with code and msg %{public}dm %{public}s", errorCode, errorMsg.c_str());
123680922886Sopenharmony_ci    std::lock_guard lockGuard(castControllersLock_);
123780922886Sopenharmony_ci    for (auto controller : castControllers_) {
123880922886Sopenharmony_ci        controller->OnPlayerError(errorCode, errorMsg);
123980922886Sopenharmony_ci    }
124080922886Sopenharmony_ci}
124180922886Sopenharmony_ci
124280922886Sopenharmony_civoid AVSessionItem::OnRemoveCastEngine()
124380922886Sopenharmony_ci{
124480922886Sopenharmony_ci    SLOGI("enter OnRemoveCastEngine");
124580922886Sopenharmony_ci    if (!collaborationNeedNetworkId_.empty()) {
124680922886Sopenharmony_ci        if (descriptor_.sessionTag_ != "RemoteCast" && castHandle_ > 0) {
124780922886Sopenharmony_ci            CollaborationManager::GetInstance().PublishServiceState(collaborationNeedNetworkId_.c_str(),
124880922886Sopenharmony_ci                ServiceCollaborationManagerBussinessStatus::SCM_IDLE);
124980922886Sopenharmony_ci        }
125080922886Sopenharmony_ci    }
125180922886Sopenharmony_ci}
125280922886Sopenharmony_ci
125380922886Sopenharmony_civoid AVSessionItem::ListenCollaborationRejectToStopCast()
125480922886Sopenharmony_ci{
125580922886Sopenharmony_ci    CollaborationManager::GetInstance().SendRejectStateToStopCast([this](const int32_t code) {
125680922886Sopenharmony_ci        std::unique_lock <std::mutex> applyResultLock(collaborationApplyResultMutex_);
125780922886Sopenharmony_ci        if (code == ServiceCollaborationManagerResultCode::ONSTOP && newCastState == castConnectStateForConnected_) {
125880922886Sopenharmony_ci            SLOGI("onstop to stop cast");
125980922886Sopenharmony_ci            StopCast();
126080922886Sopenharmony_ci        }
126180922886Sopenharmony_ci        if (code == ServiceCollaborationManagerResultCode::PASS && newCastState != castConnectStateForConnected_) {
126280922886Sopenharmony_ci            SLOGI("ApplyResult can cast");
126380922886Sopenharmony_ci            applyResultFlag_ = true;
126480922886Sopenharmony_ci            applyUserResultFlag_ = true;
126580922886Sopenharmony_ci            connectWaitCallbackCond_.notify_one();
126680922886Sopenharmony_ci        }
126780922886Sopenharmony_ci        if (code == ServiceCollaborationManagerResultCode::REJECT && newCastState != castConnectStateForConnected_) {
126880922886Sopenharmony_ci            SLOGI("ApplyResult can not cast");
126980922886Sopenharmony_ci            collaborationRejectFlag_ = true;
127080922886Sopenharmony_ci            applyResultFlag_ = true;
127180922886Sopenharmony_ci            applyUserResultFlag_ = true;
127280922886Sopenharmony_ci            connectWaitCallbackCond_.notify_one();
127380922886Sopenharmony_ci        }
127480922886Sopenharmony_ci        if (code == ServiceCollaborationManagerResultCode::USERTIP && newCastState != castConnectStateForConnected_) {
127580922886Sopenharmony_ci            SLOGI("ApplyResult user tip");
127680922886Sopenharmony_ci            applyResultFlag_ = true;
127780922886Sopenharmony_ci            waitUserDecisionFlag_ = true;
127880922886Sopenharmony_ci            connectWaitCallbackCond_.notify_one();
127980922886Sopenharmony_ci        }
128080922886Sopenharmony_ci        if (code == ServiceCollaborationManagerResultCode::USERAGREE && newCastState != castConnectStateForConnected_) {
128180922886Sopenharmony_ci            SLOGI("ApplyResult user agree cast");
128280922886Sopenharmony_ci        }
128380922886Sopenharmony_ci    });
128480922886Sopenharmony_ci}
128580922886Sopenharmony_ci
128680922886Sopenharmony_ciint32_t AVSessionItem::StopCast()
128780922886Sopenharmony_ci{
128880922886Sopenharmony_ci    std::lock_guard lockGuard(castHandleLock_);
128980922886Sopenharmony_ci    if (descriptor_.sessionTag_ == "RemoteCast") {
129080922886Sopenharmony_ci        AVRouter::GetInstance().UnRegisterCallback(castHandle_, cssListener_);
129180922886Sopenharmony_ci        int32_t ret = AVRouter::GetInstance().StopCastSession(castHandle_);
129280922886Sopenharmony_ci        castHandle_ = -1;
129380922886Sopenharmony_ci        castHandleDeviceId_ = "-100";
129480922886Sopenharmony_ci        SLOGI("Unregister and Stop cast process for sink with ret %{public}d", ret);
129580922886Sopenharmony_ci        return ret;
129680922886Sopenharmony_ci    }
129780922886Sopenharmony_ci    SLOGI("Stop cast process");
129880922886Sopenharmony_ci    removeTimes = 1;
129980922886Sopenharmony_ci    if (isUpdate && newCastState == streamStateConnection) {
130080922886Sopenharmony_ci        SLOGE("removeTimes = 0");
130180922886Sopenharmony_ci        removeTimes = 0;
130280922886Sopenharmony_ci    }
130380922886Sopenharmony_ci    {
130480922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(castHandle_ != 0, AVSESSION_SUCCESS, "Not cast session, return");
130580922886Sopenharmony_ci        AVSessionRadarInfo info("AVSessionItem::StopCast");
130680922886Sopenharmony_ci        AVSessionRadar::GetInstance().StopCastBegin(descriptor_.outputDeviceInfo_, info);
130780922886Sopenharmony_ci        int64_t ret = AVRouter::GetInstance().StopCast(castHandle_, removeTimes);
130880922886Sopenharmony_ci        AVSessionRadar::GetInstance().StopCastEnd(descriptor_.outputDeviceInfo_, info);
130980922886Sopenharmony_ci        SLOGI("StopCast with unchange castHandle is %{public}ld", castHandle_);
131080922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(ret != AVSESSION_ERROR, AVSESSION_ERROR, "StopCast failed");
131180922886Sopenharmony_ci        removeTimes = 1;
131280922886Sopenharmony_ci    }
131380922886Sopenharmony_ci
131480922886Sopenharmony_ci    if (castServiceNameMapState_["HuaweiCast"] != deviceStateConnection &&
131580922886Sopenharmony_ci        castServiceNameMapState_["HuaweiCast-Dual"] != deviceStateConnection) {
131680922886Sopenharmony_ci        OutputDeviceInfo outputDeviceInfo;
131780922886Sopenharmony_ci        DeviceInfo deviceInfo;
131880922886Sopenharmony_ci        deviceInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
131980922886Sopenharmony_ci        deviceInfo.deviceId_ = "0";
132080922886Sopenharmony_ci        deviceInfo.deviceName_ = "LocalDevice";
132180922886Sopenharmony_ci        outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
132280922886Sopenharmony_ci        SetOutputDevice(outputDeviceInfo);
132380922886Sopenharmony_ci    }
132480922886Sopenharmony_ci
132580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
132680922886Sopenharmony_ci}
132780922886Sopenharmony_ci
132880922886Sopenharmony_civoid AVSessionItem::SetCastHandle(const int64_t castHandle)
132980922886Sopenharmony_ci{
133080922886Sopenharmony_ci    castHandle_ = castHandle;
133180922886Sopenharmony_ci}
133280922886Sopenharmony_ci
133380922886Sopenharmony_civoid AVSessionItem::RegisterDeviceStateCallback()
133480922886Sopenharmony_ci{
133580922886Sopenharmony_ci    OutputDeviceInfo localDevice;
133680922886Sopenharmony_ci    DeviceInfo localInfo;
133780922886Sopenharmony_ci    localInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
133880922886Sopenharmony_ci    localInfo.deviceId_ = "0";
133980922886Sopenharmony_ci    localInfo.deviceName_ = "LocalDevice";
134080922886Sopenharmony_ci    localDevice.deviceInfos_.emplace_back(localInfo);
134180922886Sopenharmony_ci    descriptor_.outputDeviceInfo_ = localDevice;
134280922886Sopenharmony_ci    AVRouter::GetInstance().RegisterCallback(castHandle_, cssListener_);
134380922886Sopenharmony_ci    SLOGI("register callback for device state change done");
134480922886Sopenharmony_ci}
134580922886Sopenharmony_ci
134680922886Sopenharmony_civoid AVSessionItem::UnRegisterDeviceStateCallback()
134780922886Sopenharmony_ci{
134880922886Sopenharmony_ci    AVRouter::GetInstance().UnRegisterCallback(castHandle_, cssListener_);
134980922886Sopenharmony_ci}
135080922886Sopenharmony_ci
135180922886Sopenharmony_civoid AVSessionItem::StopCastSession()
135280922886Sopenharmony_ci{
135380922886Sopenharmony_ci    SLOGI("Stop cast session process with castHandle: %{public}ld", castHandle_);
135480922886Sopenharmony_ci    int64_t ret = AVRouter::GetInstance().StopCastSession(castHandle_);
135580922886Sopenharmony_ci    DoContinuousTaskUnregister();
135680922886Sopenharmony_ci    if (ret != AVSESSION_ERROR) {
135780922886Sopenharmony_ci        castHandle_ = -1;
135880922886Sopenharmony_ci        castHandleDeviceId_ = "-100";
135980922886Sopenharmony_ci    } else {
136080922886Sopenharmony_ci        SLOGE("Stop cast session process error");
136180922886Sopenharmony_ci    }
136280922886Sopenharmony_ci}
136380922886Sopenharmony_ci
136480922886Sopenharmony_ciAVSessionDisplayIntf* AVSessionItem::GetAVSessionDisplayIntf()
136580922886Sopenharmony_ci{
136680922886Sopenharmony_ci    if (avsessionDisaplayIntf_ == nullptr) {
136780922886Sopenharmony_ci        typedef AVSessionDisplayIntf *(*CreateAVSessionDisplayIntfFunc)();
136880922886Sopenharmony_ci        CreateAVSessionDisplayIntfFunc createAVSessionDisplayIntf =
136980922886Sopenharmony_ci            reinterpret_cast<CreateAVSessionDisplayIntfFunc>(dynamicLoader_->GetFuntion(
137080922886Sopenharmony_ci                AVSESSION_DYNAMIC_DISPLAY_LIBRARY_PATH, "createAVSessionDisplayIntf"));
137180922886Sopenharmony_ci        if (createAVSessionDisplayIntf) {
137280922886Sopenharmony_ci            avsessionDisaplayIntf_ = (*createAVSessionDisplayIntf)();
137380922886Sopenharmony_ci        }
137480922886Sopenharmony_ci    }
137580922886Sopenharmony_ci    return avsessionDisaplayIntf_;
137680922886Sopenharmony_ci}
137780922886Sopenharmony_ci
137880922886Sopenharmony_ciint32_t AVSessionItem::StartCastDisplayListener()
137980922886Sopenharmony_ci{
138080922886Sopenharmony_ci    SLOGI("StartCastDisplayListener in");
138180922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
138280922886Sopenharmony_ci        "API_NAME", "onCastDisplayChange",
138380922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
138480922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
138580922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
138680922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
138780922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
138880922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
138980922886Sopenharmony_ci    sptr<IAVSessionCallback> callback;
139080922886Sopenharmony_ci    {
139180922886Sopenharmony_ci        std::lock_guard callbackLockGuard(callbackLock_);
139280922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVSESSION_ERROR, "callback_ is nullptr");
139380922886Sopenharmony_ci        callback = callback_;
139480922886Sopenharmony_ci    }
139580922886Sopenharmony_ci    GetDisplayListener(callback);
139680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
139780922886Sopenharmony_ci}
139880922886Sopenharmony_ci
139980922886Sopenharmony_ciint32_t AVSessionItem::StopCastDisplayListener()
140080922886Sopenharmony_ci{
140180922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
140280922886Sopenharmony_ci        "API_NAME", "offCastDisplayChange",
140380922886Sopenharmony_ci        "BUNDLE_NAME", GetBundleName(),
140480922886Sopenharmony_ci        "SESSION_ID", AVSessionUtils::GetAnonySessionId(GetSessionId()),
140580922886Sopenharmony_ci        "SESSION_TAG", descriptor_.sessionTag_,
140680922886Sopenharmony_ci        "SESSION_TYPE", GetSessionType(),
140780922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
140880922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
140980922886Sopenharmony_ci    SLOGI("StopCastDisplayListener in");
141080922886Sopenharmony_ci    std::lock_guard displayListenerLockGuard(displayListenerLock_);
141180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(displayListener_ != nullptr, AVSESSION_ERROR, "displayListener_ is nullptr");
141280922886Sopenharmony_ci    Rosen::DMError ret = Rosen::ScreenManager::GetInstance().UnregisterScreenListener(displayListener_);
141380922886Sopenharmony_ci    if (ret != Rosen::DMError::DM_OK) {
141480922886Sopenharmony_ci        SLOGE("UnregisterScreenListener failed, ret: %{public}d.", ret);
141580922886Sopenharmony_ci    }
141680922886Sopenharmony_ci    displayListener_ = nullptr;
141780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
141880922886Sopenharmony_ci}
141980922886Sopenharmony_ci
142080922886Sopenharmony_civoid AVSessionItem::GetDisplayListener(sptr<IAVSessionCallback> callback)
142180922886Sopenharmony_ci{
142280922886Sopenharmony_ci    SLOGI("GetDisplayListener in");
142380922886Sopenharmony_ci    std::lock_guard displayListenerLockGuard(displayListenerLock_);
142480922886Sopenharmony_ci    if (displayListener_ == nullptr) {
142580922886Sopenharmony_ci        SLOGI("displayListener_ is null, try to create new listener");
142680922886Sopenharmony_ci        displayListener_ = new HwCastDisplayListener(callback);
142780922886Sopenharmony_ci        if (displayListener_ == nullptr) {
142880922886Sopenharmony_ci            SLOGI("Create displayListener failed");
142980922886Sopenharmony_ci            return;
143080922886Sopenharmony_ci        }
143180922886Sopenharmony_ci        SLOGI("Start to register display listener");
143280922886Sopenharmony_ci        Rosen::DMError ret = Rosen::ScreenManager::GetInstance().RegisterScreenListener(displayListener_);
143380922886Sopenharmony_ci        if (ret != Rosen::DMError::DM_OK) {
143480922886Sopenharmony_ci            SLOGE("UnregisterScreenListener failed, ret: %{public}d.", ret);
143580922886Sopenharmony_ci        }
143680922886Sopenharmony_ci    }
143780922886Sopenharmony_ci    return;
143880922886Sopenharmony_ci}
143980922886Sopenharmony_ci
144080922886Sopenharmony_ciint32_t AVSessionItem::GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays)
144180922886Sopenharmony_ci{
144280922886Sopenharmony_ci    SLOGI("GetAllCastDisplays in");
144380922886Sopenharmony_ci    std::vector<sptr<Rosen::Screen>> allDisplays;
144480922886Sopenharmony_ci    Rosen::ScreenManager::GetInstance().GetAllScreens(allDisplays);
144580922886Sopenharmony_ci    std::vector<CastDisplayInfo> displays;
144680922886Sopenharmony_ci    for (auto &display : allDisplays) {
144780922886Sopenharmony_ci        SLOGI("GetAllCastDisplays name: %{public}s, id: %{public}lu", display->GetName().c_str(), display->GetId());
144880922886Sopenharmony_ci        auto flag = Rosen::ScreenManager::GetInstance().GetVirtualScreenFlag(display->GetId());
144980922886Sopenharmony_ci        if (flag == Rosen::VirtualScreenFlag::CAST) {
145080922886Sopenharmony_ci            SLOGI("ReportCastDisplay start in");
145180922886Sopenharmony_ci            CastDisplayInfo castDisplayInfo;
145280922886Sopenharmony_ci            castDisplayInfo.displayState = CastDisplayState::STATE_ON;
145380922886Sopenharmony_ci            castDisplayInfo.displayId = display->GetId();
145480922886Sopenharmony_ci            castDisplayInfo.name = display->GetName();
145580922886Sopenharmony_ci            castDisplayInfo.width = display->GetWidth();
145680922886Sopenharmony_ci            castDisplayInfo.height = display->GetHeight();
145780922886Sopenharmony_ci            displays.push_back(castDisplayInfo);
145880922886Sopenharmony_ci            std::lock_guard displayListenerLockGuard(displayListenerLock_);
145980922886Sopenharmony_ci            if (displayListener_ != nullptr) {
146080922886Sopenharmony_ci                displayListener_->SetDisplayInfo(display);
146180922886Sopenharmony_ci            }
146280922886Sopenharmony_ci        }
146380922886Sopenharmony_ci    }
146480922886Sopenharmony_ci    castDisplays = displays;
146580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
146680922886Sopenharmony_ci}
146780922886Sopenharmony_ci
146880922886Sopenharmony_civoid AVSessionItem::SetExtrasInner(AAFwk::IArray* list)
146980922886Sopenharmony_ci{
147080922886Sopenharmony_ci    auto func = [&](AAFwk::IInterface* object) {
147180922886Sopenharmony_ci        if (object != nullptr) {
147280922886Sopenharmony_ci            AAFwk::IString* stringValue = AAFwk::IString::Query(object);
147380922886Sopenharmony_ci            if (stringValue != nullptr && AAFwk::String::Unbox(stringValue) == "url-cast" &&
147480922886Sopenharmony_ci                descriptor_.sessionType_ == AVSession::SESSION_TYPE_VIDEO && serviceCallbackForStream_) {
147580922886Sopenharmony_ci                SLOGI("AVSessionItem send mirrortostream event to service");
147680922886Sopenharmony_ci                serviceCallbackForStream_(GetSessionId());
147780922886Sopenharmony_ci            }
147880922886Sopenharmony_ci        }
147980922886Sopenharmony_ci    };
148080922886Sopenharmony_ci    AAFwk::Array::ForEach(list, func);
148180922886Sopenharmony_ci}
148280922886Sopenharmony_ci
148380922886Sopenharmony_civoid AVSessionItem::SetServiceCallbackForStream(const std::function<void(std::string)>& callback)
148480922886Sopenharmony_ci{
148580922886Sopenharmony_ci    SLOGI("SetServiceCallbackForStream in");
148680922886Sopenharmony_ci    serviceCallbackForStream_ = callback;
148780922886Sopenharmony_ci}
148880922886Sopenharmony_ci#endif
148980922886Sopenharmony_ci
149080922886Sopenharmony_ciAVSessionDescriptor AVSessionItem::GetDescriptor()
149180922886Sopenharmony_ci{
149280922886Sopenharmony_ci    return descriptor_;
149380922886Sopenharmony_ci}
149480922886Sopenharmony_ci
149580922886Sopenharmony_ciAVCallState AVSessionItem::GetAVCallState()
149680922886Sopenharmony_ci{
149780922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
149880922886Sopenharmony_ci    return avCallState_;
149980922886Sopenharmony_ci}
150080922886Sopenharmony_ci
150180922886Sopenharmony_ciAVCallMetaData AVSessionItem::GetAVCallMetaData()
150280922886Sopenharmony_ci{
150380922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
150480922886Sopenharmony_ci    std::string sessionId = GetSessionId();
150580922886Sopenharmony_ci    std::string fileDir = AVSessionUtils::GetCachePathName(userId_);
150680922886Sopenharmony_ci    std::string fileName = sessionId + AVSessionUtils::GetFileSuffix();
150780922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> innerPixelMap = avCallMetaData_.GetMediaImage();
150880922886Sopenharmony_ci    AVSessionUtils::ReadImageFromFile(innerPixelMap, fileDir, fileName);
150980922886Sopenharmony_ci    return avCallMetaData_;
151080922886Sopenharmony_ci}
151180922886Sopenharmony_ci
151280922886Sopenharmony_ci
151380922886Sopenharmony_ciAVPlaybackState AVSessionItem::GetPlaybackState()
151480922886Sopenharmony_ci{
151580922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
151680922886Sopenharmony_ci    return playbackState_;
151780922886Sopenharmony_ci}
151880922886Sopenharmony_ci
151980922886Sopenharmony_ciAVMetaData AVSessionItem::GetMetaData()
152080922886Sopenharmony_ci{
152180922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
152280922886Sopenharmony_ci    std::string sessionId = GetSessionId();
152380922886Sopenharmony_ci    std::string fileDir = AVSessionUtils::GetCachePathName(userId_);
152480922886Sopenharmony_ci    std::string fileName = sessionId + AVSessionUtils::GetFileSuffix();
152580922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> innerPixelMap = metaData_.GetMediaImage();
152680922886Sopenharmony_ci    AVSessionUtils::ReadImageFromFile(innerPixelMap, fileDir, fileName);
152780922886Sopenharmony_ci
152880922886Sopenharmony_ci    std::string avQueueFileDir = AVSessionUtils::GetFixedPathName();
152980922886Sopenharmony_ci    std::string avQueueFileName = GetBundleName() + "_" + metaData_.GetAVQueueId() + AVSessionUtils::GetFileSuffix();
153080922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData_.GetAVQueueImage();
153180922886Sopenharmony_ci    AVSessionUtils::ReadImageFromFile(avQueuePixelMap, avQueueFileDir, avQueueFileName);
153280922886Sopenharmony_ci    return metaData_;
153380922886Sopenharmony_ci}
153480922886Sopenharmony_ci
153580922886Sopenharmony_cistd::vector<AVQueueItem> AVSessionItem::GetQueueItems()
153680922886Sopenharmony_ci{
153780922886Sopenharmony_ci    std::lock_guard lock_guard(avsessionItemLock_);
153880922886Sopenharmony_ci    return queueItems_;
153980922886Sopenharmony_ci}
154080922886Sopenharmony_ci
154180922886Sopenharmony_cistd::string AVSessionItem::GetQueueTitle()
154280922886Sopenharmony_ci{
154380922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
154480922886Sopenharmony_ci    return queueTitle_;
154580922886Sopenharmony_ci}
154680922886Sopenharmony_ci
154780922886Sopenharmony_cistd::vector<int32_t> AVSessionItem::GetSupportCommand()
154880922886Sopenharmony_ci{
154980922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
155080922886Sopenharmony_ci    if (descriptor_.elementName_.GetBundleName() == "castBundleName"
155180922886Sopenharmony_ci        && descriptor_.elementName_.GetAbilityName() == "castAbilityName") {
155280922886Sopenharmony_ci        std::vector<int32_t> supportedCmdForCastSession {
155380922886Sopenharmony_ci            AVControlCommand::SESSION_CMD_PLAY,
155480922886Sopenharmony_ci            AVControlCommand::SESSION_CMD_PAUSE,
155580922886Sopenharmony_ci            AVControlCommand::SESSION_CMD_STOP,
155680922886Sopenharmony_ci            AVControlCommand::SESSION_CMD_PLAY_NEXT,
155780922886Sopenharmony_ci            AVControlCommand::SESSION_CMD_PLAY_PREVIOUS,
155880922886Sopenharmony_ci            AVControlCommand::SESSION_CMD_SEEK
155980922886Sopenharmony_ci        };
156080922886Sopenharmony_ci        return supportedCmdForCastSession;
156180922886Sopenharmony_ci    }
156280922886Sopenharmony_ci    return supportedCmd_;
156380922886Sopenharmony_ci}
156480922886Sopenharmony_ci
156580922886Sopenharmony_ciAbilityRuntime::WantAgent::WantAgent AVSessionItem::GetLaunchAbility()
156680922886Sopenharmony_ci{
156780922886Sopenharmony_ci    return launchAbility_;
156880922886Sopenharmony_ci}
156980922886Sopenharmony_ci
157080922886Sopenharmony_ciAAFwk::WantParams AVSessionItem::GetExtras()
157180922886Sopenharmony_ci{
157280922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
157380922886Sopenharmony_ci    return extras_;
157480922886Sopenharmony_ci}
157580922886Sopenharmony_ci
157680922886Sopenharmony_civoid AVSessionItem::HandleMediaKeyEvent(const MMI::KeyEvent& keyEvent)
157780922886Sopenharmony_ci{
157880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnMediaKeyEvent");
157980922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(descriptor_.isActive_, "session is deactive");
158080922886Sopenharmony_ci    SLOGI("HandleMediaKeyEvent check isMediaKeySupport %{public}d for %{public}d",
158180922886Sopenharmony_ci        static_cast<int>(isMediaKeySupport), static_cast<int>(keyEvent.GetKeyCode()));
158280922886Sopenharmony_ci    if (!isMediaKeySupport && keyEventCaller_.count(keyEvent.GetKeyCode()) > 0) {
158380922886Sopenharmony_ci        AVControlCommand cmd;
158480922886Sopenharmony_ci        cmd.SetRewindTime(metaData_.GetSkipIntervals());
158580922886Sopenharmony_ci        cmd.SetForwardTime(metaData_.GetSkipIntervals());
158680922886Sopenharmony_ci        keyEventCaller_[keyEvent.GetKeyCode()](cmd);
158780922886Sopenharmony_ci    } else {
158880922886Sopenharmony_ci        std::lock_guard callbackLockGuard(callbackLock_);
158980922886Sopenharmony_ci        if (callback_ != nullptr) {
159080922886Sopenharmony_ci            callback_->OnMediaKeyEvent(keyEvent);
159180922886Sopenharmony_ci        }
159280922886Sopenharmony_ci    }
159380922886Sopenharmony_ci}
159480922886Sopenharmony_ci
159580922886Sopenharmony_civoid AVSessionItem::ExecuteControllerCommand(const AVControlCommand& cmd)
159680922886Sopenharmony_ci{
159780922886Sopenharmony_ci    HISYSEVENT_ADD_OPERATION_COUNT(Operation::OPT_ALL_CTRL_COMMAND);
159880922886Sopenharmony_ci    int32_t code = cmd.GetCommand();
159980922886Sopenharmony_ci    if (code < 0 || code >= SESSION_CMD_MAX) {
160080922886Sopenharmony_ci        SLOGE("controlCommand invalid");
160180922886Sopenharmony_ci        return;
160280922886Sopenharmony_ci    }
160380922886Sopenharmony_ci    SLOGI("ExecuteControllerCommand code %{public}d from pid %{public}d to pid %{public}d",
160480922886Sopenharmony_ci        code, static_cast<int>(GetCallingPid()), static_cast<int>(GetPid()));
160580922886Sopenharmony_ci    {
160680922886Sopenharmony_ci        std::lock_guard remoteSinkLockGuard(remoteSinkLock_);
160780922886Sopenharmony_ci        if (remoteSink_ != nullptr) {
160880922886Sopenharmony_ci            SLOGI("set remote ControlCommand");
160980922886Sopenharmony_ci            CHECK_AND_RETURN_LOG(remoteSink_->SetControlCommand(cmd) == AVSESSION_SUCCESS, "SetControlCommand failed");
161080922886Sopenharmony_ci        }
161180922886Sopenharmony_ci    }
161280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
161380922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(descriptor_.isActive_, "session is deactivate");
161480922886Sopenharmony_ci
161580922886Sopenharmony_ci    HISYSEVENT_ADD_OPERATION_COUNT(static_cast<Operation>(cmd.GetCommand()));
161680922886Sopenharmony_ci    HISYSEVENT_ADD_OPERATION_COUNT(Operation::OPT_SUCCESS_CTRL_COMMAND);
161780922886Sopenharmony_ci    HISYSEVENT_ADD_CONTROLLER_COMMAND_INFO(descriptor_.elementName_.GetBundleName(), GetPid(),
161880922886Sopenharmony_ci        cmd.GetCommand(), descriptor_.sessionType_);
161980922886Sopenharmony_ci    return cmdHandlers[code](cmd);
162080922886Sopenharmony_ci
162180922886Sopenharmony_ci    HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "INVALID_COMMAND", "CMD", code,
162280922886Sopenharmony_ci        "ERROR_INFO", "avsessionitem executecontrollercommand, invaild command");
162380922886Sopenharmony_ci}
162480922886Sopenharmony_ci// LCOV_EXCL_STOP
162580922886Sopenharmony_ci
162680922886Sopenharmony_civoid AVSessionItem::ExecueCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs)
162780922886Sopenharmony_ci{
162880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::ExecueCommonCommand");
162980922886Sopenharmony_ci    {
163080922886Sopenharmony_ci        std::lock_guard callbackLockGuard(callbackLock_);
163180922886Sopenharmony_ci        CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
163280922886Sopenharmony_ci        callback_->OnCommonCommand(commonCommand, commandArgs);
163380922886Sopenharmony_ci    }
163480922886Sopenharmony_ci
163580922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
163680922886Sopenharmony_ci    if (remoteSink_ != nullptr) {
163780922886Sopenharmony_ci        CHECK_AND_RETURN_LOG(remoteSink_->SetCommonCommand(commonCommand, commandArgs) == AVSESSION_SUCCESS,
163880922886Sopenharmony_ci            "SetCommonCommand failed");
163980922886Sopenharmony_ci    }
164080922886Sopenharmony_ci}
164180922886Sopenharmony_ci
164280922886Sopenharmony_ci// LCOV_EXCL_START
164380922886Sopenharmony_civoid AVSessionItem::HandleSkipToQueueItem(const int32_t& itemId)
164480922886Sopenharmony_ci{
164580922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnSkipToQueueItem");
164680922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
164780922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
164880922886Sopenharmony_ci    callback_->OnSkipToQueueItem(itemId);
164980922886Sopenharmony_ci}
165080922886Sopenharmony_ci
165180922886Sopenharmony_civoid AVSessionItem::HandleOnAVCallAnswer(const AVControlCommand& cmd)
165280922886Sopenharmony_ci{
165380922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnAVCallAnswer");
165480922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
165580922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
165680922886Sopenharmony_ci    callback_->OnAVCallAnswer();
165780922886Sopenharmony_ci}
165880922886Sopenharmony_ci
165980922886Sopenharmony_civoid AVSessionItem::HandleOnAVCallHangUp(const AVControlCommand& cmd)
166080922886Sopenharmony_ci{
166180922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnAVCallHangUp");
166280922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
166380922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
166480922886Sopenharmony_ci    callback_->OnAVCallHangUp();
166580922886Sopenharmony_ci}
166680922886Sopenharmony_ci
166780922886Sopenharmony_civoid AVSessionItem::HandleOnAVCallToggleCallMute(const AVControlCommand& cmd)
166880922886Sopenharmony_ci{
166980922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnAVCallToggleCallMute");
167080922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
167180922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
167280922886Sopenharmony_ci    callback_->OnAVCallToggleCallMute();
167380922886Sopenharmony_ci}
167480922886Sopenharmony_ci
167580922886Sopenharmony_civoid AVSessionItem::HandleOnPlay(const AVControlCommand& cmd)
167680922886Sopenharmony_ci{
167780922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnPlay");
167880922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
167980922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
168080922886Sopenharmony_ci    callback_->OnPlay();
168180922886Sopenharmony_ci}
168280922886Sopenharmony_ci
168380922886Sopenharmony_civoid AVSessionItem::HandleOnPause(const AVControlCommand& cmd)
168480922886Sopenharmony_ci{
168580922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnPause");
168680922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
168780922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
168880922886Sopenharmony_ci    callback_->OnPause();
168980922886Sopenharmony_ci}
169080922886Sopenharmony_ci
169180922886Sopenharmony_civoid AVSessionItem::HandleOnPlayOrPause(const AVControlCommand& cmd)
169280922886Sopenharmony_ci{
169380922886Sopenharmony_ci    std::lock_guard lockGuard(avsessionItemLock_);
169480922886Sopenharmony_ci    SLOGI("check current playstate : %{public}d", playbackState_.GetState());
169580922886Sopenharmony_ci    if (playbackState_.GetState() == AVPlaybackState::PLAYBACK_STATE_PLAY) {
169680922886Sopenharmony_ci        HandleOnPause(cmd);
169780922886Sopenharmony_ci    } else {
169880922886Sopenharmony_ci        HandleOnPlay(cmd);
169980922886Sopenharmony_ci    }
170080922886Sopenharmony_ci}
170180922886Sopenharmony_ci
170280922886Sopenharmony_civoid AVSessionItem::HandleOnStop(const AVControlCommand& cmd)
170380922886Sopenharmony_ci{
170480922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnStop");
170580922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
170680922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
170780922886Sopenharmony_ci    callback_->OnStop();
170880922886Sopenharmony_ci}
170980922886Sopenharmony_ci
171080922886Sopenharmony_civoid AVSessionItem::HandleOnPlayNext(const AVControlCommand& cmd)
171180922886Sopenharmony_ci{
171280922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnPlayNext");
171380922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
171480922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
171580922886Sopenharmony_ci    callback_->OnPlayNext();
171680922886Sopenharmony_ci}
171780922886Sopenharmony_ci
171880922886Sopenharmony_civoid AVSessionItem::HandleOnPlayPrevious(const AVControlCommand& cmd)
171980922886Sopenharmony_ci{
172080922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnPlayPrevious");
172180922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
172280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
172380922886Sopenharmony_ci    callback_->OnPlayPrevious();
172480922886Sopenharmony_ci}
172580922886Sopenharmony_ci
172680922886Sopenharmony_civoid AVSessionItem::HandleOnFastForward(const AVControlCommand& cmd)
172780922886Sopenharmony_ci{
172880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnFastForward");
172980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
173080922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
173180922886Sopenharmony_ci    int64_t time = 0;
173280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(cmd.GetForwardTime(time) == AVSESSION_SUCCESS, "GetForwardTime failed");
173380922886Sopenharmony_ci    callback_->OnFastForward(time);
173480922886Sopenharmony_ci}
173580922886Sopenharmony_ci
173680922886Sopenharmony_civoid AVSessionItem::HandleOnRewind(const AVControlCommand& cmd)
173780922886Sopenharmony_ci{
173880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnRewind");
173980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
174080922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
174180922886Sopenharmony_ci    int64_t time = 0;
174280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(cmd.GetRewindTime(time) == AVSESSION_SUCCESS, "GetForwardTime failed");
174380922886Sopenharmony_ci    callback_->OnRewind(time);
174480922886Sopenharmony_ci}
174580922886Sopenharmony_ci
174680922886Sopenharmony_civoid AVSessionItem::HandleOnSeek(const AVControlCommand& cmd)
174780922886Sopenharmony_ci{
174880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnSeek");
174980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
175080922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
175180922886Sopenharmony_ci    int64_t time = 0;
175280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(cmd.GetSeekTime(time) == AVSESSION_SUCCESS, "GetSeekTime failed");
175380922886Sopenharmony_ci    callback_->OnSeek(time);
175480922886Sopenharmony_ci}
175580922886Sopenharmony_ci
175680922886Sopenharmony_civoid AVSessionItem::HandleOnSetSpeed(const AVControlCommand& cmd)
175780922886Sopenharmony_ci{
175880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnSetSpeed");
175980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
176080922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
176180922886Sopenharmony_ci    double speed = 0.0;
176280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(cmd.GetSpeed(speed) == AVSESSION_SUCCESS, "GetSpeed failed");
176380922886Sopenharmony_ci    callback_->OnSetSpeed(speed);
176480922886Sopenharmony_ci}
176580922886Sopenharmony_ci
176680922886Sopenharmony_civoid AVSessionItem::HandleOnSetLoopMode(const AVControlCommand& cmd)
176780922886Sopenharmony_ci{
176880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnSetLoopMode");
176980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
177080922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
177180922886Sopenharmony_ci    int32_t loopMode = AVSESSION_ERROR;
177280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(cmd.GetLoopMode(loopMode) == AVSESSION_SUCCESS, "GetLoopMode failed");
177380922886Sopenharmony_ci    callback_->OnSetLoopMode(loopMode);
177480922886Sopenharmony_ci}
177580922886Sopenharmony_ci
177680922886Sopenharmony_civoid AVSessionItem::HandleOnToggleFavorite(const AVControlCommand& cmd)
177780922886Sopenharmony_ci{
177880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnToggleFavorite");
177980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
178080922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
178180922886Sopenharmony_ci    std::string assetId;
178280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(cmd.GetAssetId(assetId) == AVSESSION_SUCCESS, "GetMediaId failed");
178380922886Sopenharmony_ci    callback_->OnToggleFavorite(assetId);
178480922886Sopenharmony_ci}
178580922886Sopenharmony_ci
178680922886Sopenharmony_civoid AVSessionItem::HandleOnPlayFromAssetId(const AVControlCommand& cmd)
178780922886Sopenharmony_ci{
178880922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnPlayFromAssetId");
178980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
179080922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
179180922886Sopenharmony_ci    int64_t assetId = 0;
179280922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(cmd.GetPlayFromAssetId(assetId) == AVSESSION_SUCCESS, "Get playFromAssetId failed");
179380922886Sopenharmony_ci    callback_->OnPlayFromAssetId(assetId);
179480922886Sopenharmony_ci}
179580922886Sopenharmony_ci// LCOV_EXCL_STOP
179680922886Sopenharmony_ci
179780922886Sopenharmony_ciint32_t AVSessionItem::AddController(pid_t pid, sptr<AVControllerItem>& controller)
179880922886Sopenharmony_ci{
179980922886Sopenharmony_ci    std::lock_guard controllersLockGuard(controllersLock_);
180080922886Sopenharmony_ci    SLOGI("handle controller newup for pid: %{public}d", static_cast<int>(pid));
180180922886Sopenharmony_ci    controllers_.insert({pid, controller});
180280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
180380922886Sopenharmony_ci}
180480922886Sopenharmony_ci
180580922886Sopenharmony_civoid AVSessionItem::SetPid(pid_t pid)
180680922886Sopenharmony_ci{
180780922886Sopenharmony_ci    descriptor_.pid_ = pid;
180880922886Sopenharmony_ci}
180980922886Sopenharmony_ci
181080922886Sopenharmony_civoid AVSessionItem::SetUid(pid_t uid)
181180922886Sopenharmony_ci{
181280922886Sopenharmony_ci    descriptor_.uid_ = uid;
181380922886Sopenharmony_ci}
181480922886Sopenharmony_ci
181580922886Sopenharmony_cipid_t AVSessionItem::GetPid() const
181680922886Sopenharmony_ci{
181780922886Sopenharmony_ci    return descriptor_.pid_;
181880922886Sopenharmony_ci}
181980922886Sopenharmony_ci
182080922886Sopenharmony_cipid_t AVSessionItem::GetUid() const
182180922886Sopenharmony_ci{
182280922886Sopenharmony_ci    return descriptor_.uid_;
182380922886Sopenharmony_ci}
182480922886Sopenharmony_ci
182580922886Sopenharmony_ciint32_t AVSessionItem::GetUserId() const
182680922886Sopenharmony_ci{
182780922886Sopenharmony_ci    return userId_;
182880922886Sopenharmony_ci}
182980922886Sopenharmony_ci
183080922886Sopenharmony_cistd::string AVSessionItem::GetAbilityName() const
183180922886Sopenharmony_ci{
183280922886Sopenharmony_ci    return descriptor_.elementName_.GetAbilityName();
183380922886Sopenharmony_ci}
183480922886Sopenharmony_ci
183580922886Sopenharmony_ci// LCOV_EXCL_START
183680922886Sopenharmony_cistd::string AVSessionItem::GetBundleName() const
183780922886Sopenharmony_ci{
183880922886Sopenharmony_ci    return descriptor_.elementName_.GetBundleName();
183980922886Sopenharmony_ci}
184080922886Sopenharmony_ci// LCOV_EXCL_STOP
184180922886Sopenharmony_ci
184280922886Sopenharmony_civoid AVSessionItem::SetTop(bool top)
184380922886Sopenharmony_ci{
184480922886Sopenharmony_ci    descriptor_.isTopSession_ = top;
184580922886Sopenharmony_ci}
184680922886Sopenharmony_ci
184780922886Sopenharmony_cistd::shared_ptr<RemoteSessionSource> AVSessionItem::GetRemoteSource()
184880922886Sopenharmony_ci{
184980922886Sopenharmony_ci    return remoteSource_;
185080922886Sopenharmony_ci}
185180922886Sopenharmony_ci
185280922886Sopenharmony_civoid AVSessionItem::HandleControllerRelease(pid_t pid)
185380922886Sopenharmony_ci{
185480922886Sopenharmony_ci    std::lock_guard controllersLockGuard(controllersLock_);
185580922886Sopenharmony_ci    SLOGI("handle controller release for pid: %{public}d", static_cast<int>(pid));
185680922886Sopenharmony_ci    controllers_.erase(pid);
185780922886Sopenharmony_ci}
185880922886Sopenharmony_ci
185980922886Sopenharmony_civoid AVSessionItem::SetServiceCallbackForRelease(const std::function<void(AVSessionItem&)>& callback)
186080922886Sopenharmony_ci{
186180922886Sopenharmony_ci    SLOGI("SetServiceCallbackForRelease in");
186280922886Sopenharmony_ci    serviceCallback_ = callback;
186380922886Sopenharmony_ci}
186480922886Sopenharmony_ci
186580922886Sopenharmony_civoid AVSessionItem::SetServiceCallbackForAVQueueInfo(const std::function<void(AVSessionItem&)>& callback)
186680922886Sopenharmony_ci{
186780922886Sopenharmony_ci    SLOGI("SetServiceCallbackForAVQueueInfo in");
186880922886Sopenharmony_ci    serviceCallbackForAddAVQueueInfo_ = callback;
186980922886Sopenharmony_ci}
187080922886Sopenharmony_ci
187180922886Sopenharmony_civoid AVSessionItem::SetServiceCallbackForCallStart(const std::function<void(AVSessionItem&)>& callback)
187280922886Sopenharmony_ci{
187380922886Sopenharmony_ci    SLOGI("SetServiceCallbackForCallStart in");
187480922886Sopenharmony_ci    callStartCallback_ = callback;
187580922886Sopenharmony_ci}
187680922886Sopenharmony_ci
187780922886Sopenharmony_civoid AVSessionItem::SetServiceCallbackForUpdateSession(const std::function<void(std::string, bool)>& callback)
187880922886Sopenharmony_ci{
187980922886Sopenharmony_ci    SLOGI("SetServiceCallbackForUpdateSession in");
188080922886Sopenharmony_ci    serviceCallbackForUpdateSession_ = callback;
188180922886Sopenharmony_ci}
188280922886Sopenharmony_ci
188380922886Sopenharmony_civoid AVSessionItem::HandleOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo)
188480922886Sopenharmony_ci{
188580922886Sopenharmony_ci    SLOGI("output device change, connection state is %{public}d", connectionState);
188680922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVSessionItem::OnOutputDeviceChange");
188780922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackLock_);
188880922886Sopenharmony_ci    CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
188980922886Sopenharmony_ci    callback_->OnOutputDeviceChange(connectionState, outputDeviceInfo);
189080922886Sopenharmony_ci}
189180922886Sopenharmony_ci
189280922886Sopenharmony_civoid AVSessionItem::SetOutputDevice(const OutputDeviceInfo& info)
189380922886Sopenharmony_ci{
189480922886Sopenharmony_ci    descriptor_.outputDeviceInfo_ = info;
189580922886Sopenharmony_ci    int32_t connectionStateConnected = 1;
189680922886Sopenharmony_ci    HandleOutputDeviceChange(connectionStateConnected, descriptor_.outputDeviceInfo_);
189780922886Sopenharmony_ci    std::lock_guard controllersLockGuard(controllersLock_);
189880922886Sopenharmony_ci    for (const auto& controller : controllers_) {
189980922886Sopenharmony_ci        if (controller.second != nullptr) {
190080922886Sopenharmony_ci            (controller.second)->HandleOutputDeviceChange(connectionStateConnected, descriptor_.outputDeviceInfo_);
190180922886Sopenharmony_ci        }
190280922886Sopenharmony_ci    }
190380922886Sopenharmony_ci    SLOGI("OutputDeviceInfo device size is %{public}d", static_cast<int32_t>(info.deviceInfos_.size()));
190480922886Sopenharmony_ci}
190580922886Sopenharmony_ci
190680922886Sopenharmony_ci// LCOV_EXCL_START
190780922886Sopenharmony_civoid AVSessionItem::GetOutputDevice(OutputDeviceInfo& info)
190880922886Sopenharmony_ci{
190980922886Sopenharmony_ci    info = GetDescriptor().outputDeviceInfo_;
191080922886Sopenharmony_ci}
191180922886Sopenharmony_ci
191280922886Sopenharmony_ciint32_t AVSessionItem::CastAudioToRemote(const std::string& sourceDevice, const std::string& sinkDevice,
191380922886Sopenharmony_ci                                         const std::string& sinkCapability)
191480922886Sopenharmony_ci{
191580922886Sopenharmony_ci    SLOGI("start cast audio to remote");
191680922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
191780922886Sopenharmony_ci    remoteSource_ = std::make_shared<RemoteSessionSourceProxy>();
191880922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(remoteSource_ != nullptr, AVSESSION_ERROR, "remoteSource_ is nullptr");
191980922886Sopenharmony_ci    int32_t ret = remoteSource_->CastSessionToRemote(this, sourceDevice, sinkDevice, sinkCapability);
192080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastSessionToRemote failed");
192180922886Sopenharmony_ci    ret = remoteSource_->SetAVMetaData(GetMetaData());
192280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVMetaData failed");
192380922886Sopenharmony_ci    ret = remoteSource_->SetAVPlaybackState(GetPlaybackState());
192480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVPlaybackState failed");
192580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
192680922886Sopenharmony_ci}
192780922886Sopenharmony_ci
192880922886Sopenharmony_ciint32_t AVSessionItem::SourceCancelCastAudio(const std::string& sinkDevice)
192980922886Sopenharmony_ci{
193080922886Sopenharmony_ci    SLOGI("start cancel cast audio");
193180922886Sopenharmony_ci    std::lock_guard remoteSourceLockGuard(remoteSourceLock_);
193280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(remoteSource_ != nullptr, AVSESSION_ERROR, "remoteSource_ is nullptr");
193380922886Sopenharmony_ci    int32_t ret = remoteSource_->CancelCastAudio(sinkDevice);
193480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudioToLocal failed");
193580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
193680922886Sopenharmony_ci}
193780922886Sopenharmony_ci
193880922886Sopenharmony_ciint32_t AVSessionItem::CastAudioFromRemote(const std::string& sourceSessionId, const std::string& sourceDevice,
193980922886Sopenharmony_ci                                           const std::string& sinkDevice, const std::string& sourceCapability)
194080922886Sopenharmony_ci{
194180922886Sopenharmony_ci    SLOGI("start cast audio from remote");
194280922886Sopenharmony_ci    std::lock_guard remoteSinkLockGuard(remoteSinkLock_);
194380922886Sopenharmony_ci    remoteSink_ = std::make_shared<RemoteSessionSinkProxy>();
194480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(remoteSink_ != nullptr, AVSESSION_ERROR, "remoteSink_ is nullptr");
194580922886Sopenharmony_ci    int32_t ret = remoteSink_->CastSessionFromRemote(this, sourceSessionId, sourceDevice, sinkDevice,
194680922886Sopenharmony_ci        sourceCapability);
194780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastSessionFromRemote failed");
194880922886Sopenharmony_ci
194980922886Sopenharmony_ci    OutputDeviceInfo outputDeviceInfo;
195080922886Sopenharmony_ci    GetOutputDevice(outputDeviceInfo);
195180922886Sopenharmony_ci    int32_t castCategoryStreaming = ProtocolType::TYPE_CAST_PLUS_STREAM;
195280922886Sopenharmony_ci    for (size_t i = 0; i < outputDeviceInfo.deviceInfos_.size(); i++) {
195380922886Sopenharmony_ci        outputDeviceInfo.deviceInfos_[i].castCategory_ = castCategoryStreaming;
195480922886Sopenharmony_ci    }
195580922886Sopenharmony_ci    SetOutputDevice(outputDeviceInfo);
195680922886Sopenharmony_ci
195780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(Activate() == AVSESSION_SUCCESS, AVSESSION_ERROR, "Activate failed");
195880922886Sopenharmony_ci
195980922886Sopenharmony_ci    std::vector<std::vector<int32_t>> value(SESSION_DATA_CATEGORY_MAX);
196080922886Sopenharmony_ci    ret = JsonUtils::GetVectorCapability(sourceCapability, value);
196180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetVectorCapability error");
196280922886Sopenharmony_ci    for (auto cmd : value[SESSION_DATA_CONTROL_COMMAND]) {
196380922886Sopenharmony_ci        ret = AddSupportCommand(cmd);
196480922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "AddSupportCommand failed");
196580922886Sopenharmony_ci    }
196680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
196780922886Sopenharmony_ci}
196880922886Sopenharmony_ci
196980922886Sopenharmony_ciint32_t AVSessionItem::SinkCancelCastAudio()
197080922886Sopenharmony_ci{
197180922886Sopenharmony_ci    std::lock_guard remoteSinkLockGuard(remoteSinkLock_);
197280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(remoteSink_ != nullptr, AVSESSION_ERROR, "remoteSink_ is nullptr");
197380922886Sopenharmony_ci    int32_t ret = remoteSink_->CancelCastSession();
197480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CancelCastSession failed");
197580922886Sopenharmony_ci    GetDescriptor().outputDeviceInfo_.deviceInfos_.clear();
197680922886Sopenharmony_ci    DeviceInfo deviceInfo;
197780922886Sopenharmony_ci    GetDescriptor().outputDeviceInfo_.deviceInfos_.emplace_back(deviceInfo);
197880922886Sopenharmony_ci    SLOGI("SinkCancelCastAudio");
197980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
198080922886Sopenharmony_ci}
198180922886Sopenharmony_ci
198280922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
198380922886Sopenharmony_civoid AVSessionItem::UpdateCastDeviceMap(DeviceInfo deviceInfo)
198480922886Sopenharmony_ci{
198580922886Sopenharmony_ci    SLOGI("UpdateCastDeviceMap with id: %{public}s", deviceInfo.deviceid_.c_str());
198680922886Sopenharmony_ci    castDeviceInfoMap_[deviceInfo.deviceId_] = deviceInfo;
198780922886Sopenharmony_ci
198880922886Sopenharmony_ci    if (descriptor_.outputDeviceInfo_.deviceInfos_.size() > 0 &&
198980922886Sopenharmony_ci        descriptor_.outputDeviceInfo_.deviceInfos_[0].deviceId_ == deviceInfo.deviceId_) {
199080922886Sopenharmony_ci        OutputDeviceInfo outputDeviceInfo;
199180922886Sopenharmony_ci        outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
199280922886Sopenharmony_ci        descriptor_.outputDeviceInfo_ = outputDeviceInfo;
199380922886Sopenharmony_ci    }
199480922886Sopenharmony_ci}
199580922886Sopenharmony_ci#endif
199680922886Sopenharmony_ci
199780922886Sopenharmony_civoid AVSessionItem::ReportConnectFinish(const std::string func, const DeviceInfo &deviceInfo)
199880922886Sopenharmony_ci{
199980922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
200080922886Sopenharmony_ci    AVSessionRadarInfo info(func);
200180922886Sopenharmony_ci    if (castDeviceInfoMap_.count(deviceInfo.deviceId_) > 0) {
200280922886Sopenharmony_ci        DeviceInfo cacheDeviceInfo = castDeviceInfoMap_[deviceInfo.deviceId_];
200380922886Sopenharmony_ci        AVSessionRadar::GetInstance().ConnectFinish(cacheDeviceInfo, info);
200480922886Sopenharmony_ci    } else {
200580922886Sopenharmony_ci        AVSessionRadar::GetInstance().ConnectFinish(deviceInfo, info);
200680922886Sopenharmony_ci    }
200780922886Sopenharmony_ci#endif
200880922886Sopenharmony_ci}
200980922886Sopenharmony_ci
201080922886Sopenharmony_civoid AVSessionItem::ReportStopCastFinish(const std::string func, const DeviceInfo &deviceInfo)
201180922886Sopenharmony_ci{
201280922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE
201380922886Sopenharmony_ci    AVSessionRadarInfo info(func);
201480922886Sopenharmony_ci    if (castDeviceInfoMap_.count(deviceInfo.deviceId_) > 0) {
201580922886Sopenharmony_ci        DeviceInfo cacheDeviceInfo = castDeviceInfoMap_[deviceInfo.deviceId_];
201680922886Sopenharmony_ci        AVSessionRadar::GetInstance().StopCastFinish(cacheDeviceInfo, info);
201780922886Sopenharmony_ci    } else {
201880922886Sopenharmony_ci        AVSessionRadar::GetInstance().StopCastFinish(deviceInfo, info);
201980922886Sopenharmony_ci    }
202080922886Sopenharmony_ci#endif
202180922886Sopenharmony_ci}
202280922886Sopenharmony_ci
202380922886Sopenharmony_civoid AVSessionItem::SaveLocalDeviceInfo()
202480922886Sopenharmony_ci{
202580922886Sopenharmony_ci    OutputDeviceInfo localDevice;
202680922886Sopenharmony_ci    DeviceInfo localInfo;
202780922886Sopenharmony_ci    localInfo.castCategory_ = AVCastCategory::CATEGORY_LOCAL;
202880922886Sopenharmony_ci    localInfo.deviceId_ = "0";
202980922886Sopenharmony_ci    localInfo.deviceName_ = "LocalDevice";
203080922886Sopenharmony_ci    localDevice.deviceInfos_.emplace_back(localInfo);
203180922886Sopenharmony_ci    descriptor_.outputDeviceInfo_ = localDevice;
203280922886Sopenharmony_ci}
203380922886Sopenharmony_ci
203480922886Sopenharmony_ciint32_t AVSessionItem::DoContinuousTaskRegister()
203580922886Sopenharmony_ci{
203680922886Sopenharmony_ci#ifdef EFFICIENCY_MANAGER_ENABLE
203780922886Sopenharmony_ci    if (descriptor_.sessionTag_ == "RemoteCast") {
203880922886Sopenharmony_ci        SLOGI("sink session no need to register continuousTask");
203980922886Sopenharmony_ci        return AVSESSION_SUCCESS;
204080922886Sopenharmony_ci    }
204180922886Sopenharmony_ci    int32_t uid = GetUid();
204280922886Sopenharmony_ci    int32_t pid = GetPid();
204380922886Sopenharmony_ci    std::string bundleName = BundleStatusAdapter::GetInstance().GetBundleNameFromUid(uid);
204480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(bundleName != "", AVSESSION_ERROR, "GetBundleNameFromUid failed");
204580922886Sopenharmony_ci
204680922886Sopenharmony_ci    void *handle_ = dlopen("libsuspend_manager_client.z.so", RTLD_NOW);
204780922886Sopenharmony_ci    if (handle_ == nullptr) {
204880922886Sopenharmony_ci        SLOGE("failed to open library libsuspend_manager_client reaseon %{public}s", dlerror());
204980922886Sopenharmony_ci        return AVSESSION_ERROR;
205080922886Sopenharmony_ci    }
205180922886Sopenharmony_ci    typedef ErrCode (*handler) (int32_t eventType, int32_t uid, int32_t pid,
205280922886Sopenharmony_ci        const std::string bundleName, int32_t taskState, int32_t serviceId);
205380922886Sopenharmony_ci    handler reportContinuousTaskEventEx = reinterpret_cast<handler>(dlsym(handle_, "ReportContinuousTaskEventEx"));
205480922886Sopenharmony_ci    ErrCode errCode = reportContinuousTaskEventEx(0, uid, pid, bundleName, 1, AVSESSION_SERVICE_ID);
205580922886Sopenharmony_ci    SLOGI("reportContinuousTaskEventEx done, result: %{public}d", errCode);
205680922886Sopenharmony_ci#ifndef TEST_COVERAGE
205780922886Sopenharmony_ci    if (handle_ != nullptr) {
205880922886Sopenharmony_ci        OPENSSL_thread_stop();
205980922886Sopenharmony_ci    }
206080922886Sopenharmony_ci    dlclose(handle_);
206180922886Sopenharmony_ci#endif
206280922886Sopenharmony_ci#endif
206380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
206480922886Sopenharmony_ci}
206580922886Sopenharmony_ci
206680922886Sopenharmony_ciint32_t AVSessionItem::DoContinuousTaskUnregister()
206780922886Sopenharmony_ci{
206880922886Sopenharmony_ci#ifdef EFFICIENCY_MANAGER_ENABLE
206980922886Sopenharmony_ci    if (descriptor_.sessionTag_ == "RemoteCast") {
207080922886Sopenharmony_ci        SLOGI("sink session should not unregister ContinuousTask");
207180922886Sopenharmony_ci        return AVSESSION_SUCCESS;
207280922886Sopenharmony_ci    }
207380922886Sopenharmony_ci    int32_t uid = GetUid();
207480922886Sopenharmony_ci    int32_t pid = GetPid();
207580922886Sopenharmony_ci    std::string bundleName = BundleStatusAdapter::GetInstance().GetBundleNameFromUid(uid);
207680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(bundleName != "", AVSESSION_ERROR, "GetBundleNameFromUid failed");
207780922886Sopenharmony_ci
207880922886Sopenharmony_ci    void *handle_ = dlopen("libsuspend_manager_client.z.so", RTLD_NOW);
207980922886Sopenharmony_ci    if (handle_ == nullptr) {
208080922886Sopenharmony_ci        SLOGE("failed to open library libsuspend_manager_client when stop cast, reaseon %{public}s", dlerror());
208180922886Sopenharmony_ci        return AVSESSION_ERROR;
208280922886Sopenharmony_ci    }
208380922886Sopenharmony_ci    typedef ErrCode (*handler) (int32_t eventType, int32_t uid, int32_t pid,
208480922886Sopenharmony_ci        const std::string bundleName, int32_t taskState, int32_t serviceId);
208580922886Sopenharmony_ci    handler reportContinuousTaskEventEx = reinterpret_cast<handler>(dlsym(handle_, "ReportContinuousTaskEventEx"));
208680922886Sopenharmony_ci    ErrCode errCode = reportContinuousTaskEventEx(0, uid, pid, bundleName, 2, AVSESSION_SERVICE_ID);
208780922886Sopenharmony_ci    SLOGI("reportContinuousTaskEventEx done when stop cast, result: %{public}d", errCode);
208880922886Sopenharmony_ci#ifndef TEST_COVERAGE
208980922886Sopenharmony_ci    if (handle_ != nullptr) {
209080922886Sopenharmony_ci        OPENSSL_thread_stop();
209180922886Sopenharmony_ci    }
209280922886Sopenharmony_ci    dlclose(handle_);
209380922886Sopenharmony_ci#endif
209480922886Sopenharmony_ci#endif
209580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
209680922886Sopenharmony_ci}
209780922886Sopenharmony_ci// LCOV_EXCL_STOP
209880922886Sopenharmony_ci} // namespace OHOS::AVSession
2099