180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2023 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 "avcast_controller_item.h"
1780922886Sopenharmony_ci#include "avsession_radar.h"
1880922886Sopenharmony_ci#include "avsession_errors.h"
1980922886Sopenharmony_ci#include "avsession_log.h"
2080922886Sopenharmony_ci#include "avsession_trace.h"
2180922886Sopenharmony_ci#include "av_router.h"
2280922886Sopenharmony_ci#include "avsession_sysevent.h"
2380922886Sopenharmony_ci#include "avmedia_description.h"
2480922886Sopenharmony_ci#include "bundle_status_adapter.h"
2580922886Sopenharmony_ci
2680922886Sopenharmony_ci#include <string>
2780922886Sopenharmony_ci
2880922886Sopenharmony_cinamespace OHOS::AVSession {
2980922886Sopenharmony_ciAVCastControllerItem::AVCastControllerItem()
3080922886Sopenharmony_ci{
3180922886Sopenharmony_ci}
3280922886Sopenharmony_ci
3380922886Sopenharmony_ciAVCastControllerItem::~AVCastControllerItem()
3480922886Sopenharmony_ci{
3580922886Sopenharmony_ci}
3680922886Sopenharmony_ci
3780922886Sopenharmony_civoid AVCastControllerItem::Init(std::shared_ptr<IAVCastControllerProxy> castControllerProxy,
3880922886Sopenharmony_ci    const std::function<void(int32_t, std::vector<int32_t>&)>& validCommandsChangecallback)
3980922886Sopenharmony_ci{
4080922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
4180922886Sopenharmony_ci    castControllerProxy_ = castControllerProxy;
4280922886Sopenharmony_ci    if (castControllerProxy_ != nullptr) {
4380922886Sopenharmony_ci        castControllerProxy_->RegisterControllerListener(shared_from_this());
4480922886Sopenharmony_ci    }
4580922886Sopenharmony_ci    validCommandsChangecallback_ = validCommandsChangecallback;
4680922886Sopenharmony_ci    {
4780922886Sopenharmony_ci        std::lock_guard<std::mutex> lock(callbackToSessionLock_);
4880922886Sopenharmony_ci        isSessionCallbackAvailable_ = true;
4980922886Sopenharmony_ci    }
5080922886Sopenharmony_ci}
5180922886Sopenharmony_ci
5280922886Sopenharmony_civoid AVCastControllerItem::OnCastPlaybackStateChange(const AVPlaybackState& state)
5380922886Sopenharmony_ci{
5480922886Sopenharmony_ci    SLOGI("OnCastPlaybackStateChange with state: %{public}d", state.GetState());
5580922886Sopenharmony_ci    if (state.GetState() == AVPlaybackState::PLAYBACK_STATE_PLAY) {
5680922886Sopenharmony_ci        AVSessionRadarInfo info("AVCastControllerItem::OnCastPlaybackStateChange");
5780922886Sopenharmony_ci        AVSessionRadar::GetInstance().PlayerStarted(info);
5880922886Sopenharmony_ci    } else if (state.GetState() != currentState_) {
5980922886Sopenharmony_ci        currentState_ = state.GetState();
6080922886Sopenharmony_ci        AVSessionRadarInfo info("AVCastControllerItem::OnCastPlaybackStateChange");
6180922886Sopenharmony_ci        AVSessionRadar::GetInstance().ControlCommandRespond(info);
6280922886Sopenharmony_ci    }
6380922886Sopenharmony_ci    AVPlaybackState stateOut;
6480922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
6580922886Sopenharmony_ci    if (state.CopyToByMask(castPlaybackMask_, stateOut)) {
6680922886Sopenharmony_ci        SLOGI("update cast playback state");
6780922886Sopenharmony_ci        AVSESSION_TRACE_SYNC_START("AVCastControllerItem::OnCastPlaybackStateChange");
6880922886Sopenharmony_ci        if (callback_ != nullptr) {
6980922886Sopenharmony_ci            callback_->OnCastPlaybackStateChange(stateOut);
7080922886Sopenharmony_ci        }
7180922886Sopenharmony_ci    }
7280922886Sopenharmony_ci}
7380922886Sopenharmony_ci
7480922886Sopenharmony_civoid AVCastControllerItem::OnMediaItemChange(const AVQueueItem& avQueueItem)
7580922886Sopenharmony_ci{
7680922886Sopenharmony_ci    SLOGI("Enter OnMediaItemChange in AVCastControllerItem.");
7780922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
7880922886Sopenharmony_ci    if (callback_ != nullptr) {
7980922886Sopenharmony_ci        callback_->OnMediaItemChange(avQueueItem);
8080922886Sopenharmony_ci    }
8180922886Sopenharmony_ci}
8280922886Sopenharmony_ci
8380922886Sopenharmony_civoid AVCastControllerItem::OnPlayNext()
8480922886Sopenharmony_ci{
8580922886Sopenharmony_ci    SLOGI("Enter OnPlayNext in AVCastControllerItem.");
8680922886Sopenharmony_ci    AVSessionRadarInfo info("AVCastControllerItem::OnPlayNext");
8780922886Sopenharmony_ci    AVSessionRadar::GetInstance().ControlCommandRespond(info);
8880922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
8980922886Sopenharmony_ci    if (callback_ != nullptr) {
9080922886Sopenharmony_ci        callback_->OnPlayNext();
9180922886Sopenharmony_ci    }
9280922886Sopenharmony_ci}
9380922886Sopenharmony_ci
9480922886Sopenharmony_civoid AVCastControllerItem::OnPlayPrevious()
9580922886Sopenharmony_ci{
9680922886Sopenharmony_ci    SLOGI("Enter OnPlayPrevious in AVCastControllerItem.");
9780922886Sopenharmony_ci    AVSessionRadarInfo info("AVCastControllerItem::OnPlayPrevious");
9880922886Sopenharmony_ci    AVSessionRadar::GetInstance().ControlCommandRespond(info);
9980922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
10080922886Sopenharmony_ci    if (callback_ != nullptr) {
10180922886Sopenharmony_ci        callback_->OnPlayPrevious();
10280922886Sopenharmony_ci    }
10380922886Sopenharmony_ci}
10480922886Sopenharmony_ci
10580922886Sopenharmony_civoid AVCastControllerItem::OnSeekDone(const int32_t seekNumber)
10680922886Sopenharmony_ci{
10780922886Sopenharmony_ci    SLOGI("Enter OnSeekDone in AVCastControllerItem.");
10880922886Sopenharmony_ci    AVSessionRadarInfo info("AVCastControllerItem::OnSeekDone");
10980922886Sopenharmony_ci    AVSessionRadar::GetInstance().ControlCommandRespond(info);
11080922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
11180922886Sopenharmony_ci    if (callback_ != nullptr) {
11280922886Sopenharmony_ci        callback_->OnSeekDone(seekNumber);
11380922886Sopenharmony_ci    }
11480922886Sopenharmony_ci}
11580922886Sopenharmony_ci
11680922886Sopenharmony_civoid AVCastControllerItem::OnVideoSizeChange(const int32_t width, const int32_t height)
11780922886Sopenharmony_ci{
11880922886Sopenharmony_ci    SLOGI("Enter OnVideoSizeChange in AVCastControllerItem.");
11980922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
12080922886Sopenharmony_ci    if (callback_ != nullptr) {
12180922886Sopenharmony_ci        callback_->OnVideoSizeChange(width, height);
12280922886Sopenharmony_ci    }
12380922886Sopenharmony_ci}
12480922886Sopenharmony_ci
12580922886Sopenharmony_civoid AVCastControllerItem::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
12680922886Sopenharmony_ci{
12780922886Sopenharmony_ci    SLOGI("OnPlayerError error:%{public}d", errorCode);
12880922886Sopenharmony_ci    AVSessionRadarInfo info("AVCastControllerItem::OnPlayerError");
12980922886Sopenharmony_ci    info.errorCode_ = errorCode;
13080922886Sopenharmony_ci    AVSessionRadar::GetInstance().ControlCommandError(info);
13180922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
13280922886Sopenharmony_ci    if (callback_ != nullptr) {
13380922886Sopenharmony_ci        callback_->OnPlayerError(errorCode, errorMsg);
13480922886Sopenharmony_ci    }
13580922886Sopenharmony_ci}
13680922886Sopenharmony_ci
13780922886Sopenharmony_civoid AVCastControllerItem::OnEndOfStream(const int32_t isLooping)
13880922886Sopenharmony_ci{
13980922886Sopenharmony_ci    SLOGI("Enter OnEndOfStream in AVCastControllerItem.");
14080922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
14180922886Sopenharmony_ci    if (callback_ != nullptr) {
14280922886Sopenharmony_ci        callback_->OnEndOfStream(isLooping);
14380922886Sopenharmony_ci    }
14480922886Sopenharmony_ci}
14580922886Sopenharmony_ci
14680922886Sopenharmony_civoid AVCastControllerItem::OnPlayRequest(const AVQueueItem& avQueueItem)
14780922886Sopenharmony_ci{
14880922886Sopenharmony_ci    SLOGI("Enter OnPlayRequest in AVCastControllerItem.");
14980922886Sopenharmony_ci    AVSessionRadarInfo info("AVCastControllerItem::OnPlayRequest");
15080922886Sopenharmony_ci    AVSessionRadar::GetInstance().ControlCommandRespond(info);
15180922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
15280922886Sopenharmony_ci    if (callback_ != nullptr) {
15380922886Sopenharmony_ci        callback_->OnPlayRequest(avQueueItem);
15480922886Sopenharmony_ci    }
15580922886Sopenharmony_ci}
15680922886Sopenharmony_ci
15780922886Sopenharmony_civoid AVCastControllerItem::OnKeyRequest(const std::string &assetId, const std::vector<uint8_t> &keyRequestData)
15880922886Sopenharmony_ci{
15980922886Sopenharmony_ci    SLOGI("Enter OnKeyRequest in AVCastControllerItem.");
16080922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
16180922886Sopenharmony_ci    if (callback_ != nullptr) {
16280922886Sopenharmony_ci        callback_->OnKeyRequest(assetId, keyRequestData);
16380922886Sopenharmony_ci    }
16480922886Sopenharmony_ci}
16580922886Sopenharmony_ci
16680922886Sopenharmony_civoid AVCastControllerItem::OnValidCommandChange(const std::vector<int32_t>& cmds)
16780922886Sopenharmony_ci{
16880922886Sopenharmony_ci    HandleCastValidCommandChange(cmds);
16980922886Sopenharmony_ci}
17080922886Sopenharmony_ci
17180922886Sopenharmony_ciint32_t AVCastControllerItem::SendControlCommand(const AVCastControlCommand& cmd)
17280922886Sopenharmony_ci{
17380922886Sopenharmony_ci    SLOGI("Call SendControlCommand of cast controller proxy");
17480922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
17580922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
17680922886Sopenharmony_ci    AVSessionRadarInfo info("AVCastControllerItem::SendControlCommand");
17780922886Sopenharmony_ci    AVSessionRadar::GetInstance().SendControlCommandBegin(info);
17880922886Sopenharmony_ci    castControllerProxy_->SendControlCommand(cmd);
17980922886Sopenharmony_ci    AVSessionRadar::GetInstance().SendControlCommandEnd(info);
18080922886Sopenharmony_ci    std::string API_PARAM_STRING = "cmd: " + std::to_string(cmd.GetCommand());
18180922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
18280922886Sopenharmony_ci        "API_NAME", "SendControlCommand",
18380922886Sopenharmony_ci        "BUNDLE_NAME", BundleStatusAdapter::GetInstance().GetBundleNameFromUid(GetCallingUid()),
18480922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
18580922886Sopenharmony_ci        "ERROR_CODE", AVSESSION_SUCCESS,
18680922886Sopenharmony_ci        "ERROR_MSG", "SUCCESS");
18780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
18880922886Sopenharmony_ci}
18980922886Sopenharmony_ci
19080922886Sopenharmony_ciint32_t AVCastControllerItem::Start(const AVQueueItem& avQueueItem)
19180922886Sopenharmony_ci{
19280922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
19380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
19480922886Sopenharmony_ci    AVSessionRadarInfo info("AVCastControllerItem::Start");
19580922886Sopenharmony_ci    int32_t ret = castControllerProxy_->Start(avQueueItem);
19680922886Sopenharmony_ci    std::string errMsg = (ret == AVSESSION_SUCCESS) ? "SUCCESS" : "start failed";
19780922886Sopenharmony_ci    std::string mediaIcon = "false";
19880922886Sopenharmony_ci    std::string API_PARAM_STRING = "";
19980922886Sopenharmony_ci    std::string startPosition = "";
20080922886Sopenharmony_ci    std::string duration = "";
20180922886Sopenharmony_ci    std::string mediauri = "";
20280922886Sopenharmony_ci    if (avQueueItem.GetDescription() != nullptr) {
20380922886Sopenharmony_ci        startPosition = std::to_string(avQueueItem.GetDescription()->GetStartPosition());
20480922886Sopenharmony_ci        duration =  std::to_string(avQueueItem.GetDescription()->GetDuration());
20580922886Sopenharmony_ci        if (avQueueItem.GetDescription()->GetIcon() != nullptr ||
20680922886Sopenharmony_ci            !(avQueueItem.GetDescription()->GetIconUri().empty())) {
20780922886Sopenharmony_ci            mediaIcon = "true";
20880922886Sopenharmony_ci        }
20980922886Sopenharmony_ci        mediauri = avQueueItem.GetDescription()->GetMediaUri().empty() ? "false" : "true";
21080922886Sopenharmony_ci        API_PARAM_STRING = "mediauri: " + mediauri + "," + "iconImage: " + mediaIcon + ","
21180922886Sopenharmony_ci                                        + "mediaId: " + avQueueItem.GetDescription()->GetMediaId() + ","
21280922886Sopenharmony_ci                                        + "title: " + avQueueItem.GetDescription()->GetTitle() + ","
21380922886Sopenharmony_ci                                        + "subtitle: " + avQueueItem.GetDescription()->GetSubtitle() + ","
21480922886Sopenharmony_ci                                        + "mediaType: " + avQueueItem.GetDescription()->GetMediaType() + ","
21580922886Sopenharmony_ci                                        + "startPosition: " + startPosition + ","
21680922886Sopenharmony_ci                                        + "duration: " + duration;
21780922886Sopenharmony_ci    }
21880922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
21980922886Sopenharmony_ci        "API_NAME", "Start",
22080922886Sopenharmony_ci        "BUNDLE_NAME", BundleStatusAdapter::GetInstance().GetBundleNameFromUid(GetCallingUid()),
22180922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
22280922886Sopenharmony_ci        "ERROR_CODE", ret,
22380922886Sopenharmony_ci        "ERROR_MSG", errMsg);
22480922886Sopenharmony_ci    if (ret != AVSESSION_SUCCESS) {
22580922886Sopenharmony_ci        info.errorCode_ = AVSessionRadar::GetRadarErrorCode(ret);
22680922886Sopenharmony_ci        AVSessionRadar::GetInstance().StartPlayFailed(info);
22780922886Sopenharmony_ci    } else {
22880922886Sopenharmony_ci        AVSessionRadar::GetInstance().StartPlayBegin(info);
22980922886Sopenharmony_ci    }
23080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
23180922886Sopenharmony_ci}
23280922886Sopenharmony_ci
23380922886Sopenharmony_ciint32_t AVCastControllerItem::Prepare(const AVQueueItem& avQueueItem)
23480922886Sopenharmony_ci{
23580922886Sopenharmony_ci    SLOGI("Call prepare of cast controller proxy");
23680922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
23780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
23880922886Sopenharmony_ci    auto ret = castControllerProxy_->Prepare(avQueueItem);
23980922886Sopenharmony_ci    std::string errMsg = (ret == AVSESSION_SUCCESS) ? "SUCCESS" : "prepare failed";
24080922886Sopenharmony_ci    std::string mediaIcon = "false";
24180922886Sopenharmony_ci    std::string API_PARAM_STRING = "";
24280922886Sopenharmony_ci    std::string startPosition = "";
24380922886Sopenharmony_ci    std::string duration = "";
24480922886Sopenharmony_ci    std::string mediauri = "";
24580922886Sopenharmony_ci    if (avQueueItem.GetDescription() != nullptr) {
24680922886Sopenharmony_ci        startPosition = std::to_string(avQueueItem.GetDescription()->GetStartPosition());
24780922886Sopenharmony_ci        duration =  std::to_string(avQueueItem.GetDescription()->GetDuration());
24880922886Sopenharmony_ci        if (avQueueItem.GetDescription()->GetIcon() != nullptr ||
24980922886Sopenharmony_ci            !(avQueueItem.GetDescription()->GetIconUri().empty())) {
25080922886Sopenharmony_ci            mediaIcon = "true";
25180922886Sopenharmony_ci        }
25280922886Sopenharmony_ci        mediauri = avQueueItem.GetDescription()->GetMediaUri().empty() ? "false" : "true";
25380922886Sopenharmony_ci        API_PARAM_STRING = "mediauri: " + mediauri + "," + "iconImage: " + mediaIcon + ","
25480922886Sopenharmony_ci                                        + "mediaId: " + avQueueItem.GetDescription()->GetMediaId() + ","
25580922886Sopenharmony_ci                                        + "title: " + avQueueItem.GetDescription()->GetTitle() + ","
25680922886Sopenharmony_ci                                        + "subtitle: " + avQueueItem.GetDescription()->GetSubtitle() + ","
25780922886Sopenharmony_ci                                        + "mediaType: " + avQueueItem.GetDescription()->GetMediaType() + ","
25880922886Sopenharmony_ci                                        + "startPosition: " + startPosition + ","
25980922886Sopenharmony_ci                                        + "duration: " + duration;
26080922886Sopenharmony_ci    }
26180922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
26280922886Sopenharmony_ci        "API_NAME", "Prepare",
26380922886Sopenharmony_ci        "BUNDLE_NAME", BundleStatusAdapter::GetInstance().GetBundleNameFromUid(GetCallingUid()),
26480922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
26580922886Sopenharmony_ci        "ERROR_CODE", ret,
26680922886Sopenharmony_ci        "ERROR_MSG", errMsg);
26780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
26880922886Sopenharmony_ci}
26980922886Sopenharmony_ci
27080922886Sopenharmony_ciint32_t AVCastControllerItem::GetDuration(int32_t& duration)
27180922886Sopenharmony_ci{
27280922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
27380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
27480922886Sopenharmony_ci    return castControllerProxy_->GetDuration(duration);
27580922886Sopenharmony_ci}
27680922886Sopenharmony_ci
27780922886Sopenharmony_ciint32_t AVCastControllerItem::GetCastAVPlaybackState(AVPlaybackState& avPlaybackState)
27880922886Sopenharmony_ci{
27980922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
28080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
28180922886Sopenharmony_ci    auto ret = castControllerProxy_->GetCastAVPlaybackState(avPlaybackState);
28280922886Sopenharmony_ci    std::string errMsg = (ret == AVSESSION_SUCCESS) ? "SUCCESS" : "GetCastAVPlaybackState failed";
28380922886Sopenharmony_ci    int64_t avElapsedTime = avPlaybackState.GetPosition().elapsedTime_;
28480922886Sopenharmony_ci    int64_t avUpdateTime = avPlaybackState.GetPosition().updateTime_;
28580922886Sopenharmony_ci    std::string isFavor = avPlaybackState.GetFavorite() ? "true" : "false";
28680922886Sopenharmony_ci    std::string API_PARAM_STRING = "state: " + std::to_string(avPlaybackState.GetState()) + ", "
28780922886Sopenharmony_ci                                    + "elapsedTime: " + std::to_string(avElapsedTime) + ", "
28880922886Sopenharmony_ci                                    + "updateTime: " + std::to_string(avUpdateTime) + ", "
28980922886Sopenharmony_ci                                    + "loopMode: " + std::to_string(avPlaybackState.GetLoopMode()) + ", "
29080922886Sopenharmony_ci                                    + "isFavorite: " + isFavor;
29180922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
29280922886Sopenharmony_ci        "API_NAME", "GetCastAVPlaybackState",
29380922886Sopenharmony_ci        "BUNDLE_NAME", BundleStatusAdapter::GetInstance().GetBundleNameFromUid(GetCallingUid()),
29480922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
29580922886Sopenharmony_ci        "ERROR_CODE", ret,
29680922886Sopenharmony_ci        "ERROR_MSG", errMsg);
29780922886Sopenharmony_ci    return ret;
29880922886Sopenharmony_ci}
29980922886Sopenharmony_ci
30080922886Sopenharmony_ciint32_t AVCastControllerItem::GetCurrentItem(AVQueueItem& currentItem)
30180922886Sopenharmony_ci{
30280922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
30380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR,
30480922886Sopenharmony_ci        "cast controller proxy is nullptr");
30580922886Sopenharmony_ci    currentItem =  castControllerProxy_->GetCurrentItem();
30680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
30780922886Sopenharmony_ci}
30880922886Sopenharmony_ci
30980922886Sopenharmony_ciint32_t AVCastControllerItem::GetValidCommands(std::vector<int32_t>& cmds)
31080922886Sopenharmony_ci{
31180922886Sopenharmony_ci    if (sessionTag_ == "RemoteCast") {
31280922886Sopenharmony_ci        std::lock_guard lockGuard(castControllerLock_);
31380922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR,
31480922886Sopenharmony_ci            "cast controller proxy is nullptr");
31580922886Sopenharmony_ci        castControllerProxy_->GetValidAbility(cmds);
31680922886Sopenharmony_ci        SLOGI("get available commands from cast with size %{public}zd", cmds.size());
31780922886Sopenharmony_ci        return AVSESSION_SUCCESS;
31880922886Sopenharmony_ci    }
31980922886Sopenharmony_ci    {
32080922886Sopenharmony_ci        std::lock_guard<std::mutex> lock(callbackToSessionLock_);
32180922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(isSessionCallbackAvailable_, AVSESSION_ERROR, "sessionCallback not available");
32280922886Sopenharmony_ci    }
32380922886Sopenharmony_ci    validCommandsChangecallback_(AVCastControlCommand::CAST_CONTROL_CMD_MAX, cmds);
32480922886Sopenharmony_ci    SLOGI("get available command with size %{public}zd", cmds.size());
32580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
32680922886Sopenharmony_ci}
32780922886Sopenharmony_ci
32880922886Sopenharmony_ciint32_t AVCastControllerItem::SetDisplaySurface(std::string& surfaceId)
32980922886Sopenharmony_ci{
33080922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
33180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
33280922886Sopenharmony_ci    return castControllerProxy_->SetDisplaySurface(surfaceId);
33380922886Sopenharmony_ci}
33480922886Sopenharmony_ci
33580922886Sopenharmony_ciint32_t AVCastControllerItem::SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
33680922886Sopenharmony_ci{
33780922886Sopenharmony_ci    castPlaybackMask_ = filter;
33880922886Sopenharmony_ci    return AVSESSION_SUCCESS;
33980922886Sopenharmony_ci}
34080922886Sopenharmony_ci
34180922886Sopenharmony_ciint32_t AVCastControllerItem::ProcessMediaKeyResponse(const std::string &assetId, const std::vector<uint8_t> &response)
34280922886Sopenharmony_ci{
34380922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerLock_);
34480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
34580922886Sopenharmony_ci    auto ret =  castControllerProxy_->ProcessMediaKeyResponse(assetId, response);
34680922886Sopenharmony_ci    std::string API_PARAM_STRING = "assetId: " + assetId;
34780922886Sopenharmony_ci    std::string errMsg = (ret == AVSESSION_SUCCESS) ? "SUCCESS" : "ProcessMediaKeyResponse failed";
34880922886Sopenharmony_ci    HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
34980922886Sopenharmony_ci        "API_NAME", "ProcessMediaKeyResponse",
35080922886Sopenharmony_ci        "BUNDLE_NAME",  BundleStatusAdapter::GetInstance().GetBundleNameFromUid(GetCallingUid()),
35180922886Sopenharmony_ci        "API_PARAM", API_PARAM_STRING,
35280922886Sopenharmony_ci        "ERROR_CODE", ret,
35380922886Sopenharmony_ci        "ERROR_MSG", errMsg);
35480922886Sopenharmony_ci    return ret;
35580922886Sopenharmony_ci}
35680922886Sopenharmony_ci
35780922886Sopenharmony_ciint32_t AVCastControllerItem::AddAvailableCommand(const int32_t cmd)
35880922886Sopenharmony_ci{
35980922886Sopenharmony_ci    SLOGI("add available command %{public}d with isSessionCallbackAvailable check %{public}d",
36080922886Sopenharmony_ci        cmd, static_cast<int>(isSessionCallbackAvailable_));
36180922886Sopenharmony_ci    std::vector<int32_t> cmds(AVCastControlCommand::CAST_CONTROL_CMD_MAX);
36280922886Sopenharmony_ci    {
36380922886Sopenharmony_ci        std::lock_guard<std::mutex> lock(callbackToSessionLock_);
36480922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(isSessionCallbackAvailable_, AVSESSION_ERROR, "sessionCallback not available");
36580922886Sopenharmony_ci    }
36680922886Sopenharmony_ci    validCommandsChangecallback_(cmd, cmds);
36780922886Sopenharmony_ci    SLOGI("add available command with size %{public}d", static_cast<int32_t>(cmds.size()));
36880922886Sopenharmony_ci    if (cmds.empty()) {
36980922886Sopenharmony_ci        SLOGI("check is sink session with empty, not set");
37080922886Sopenharmony_ci    } else {
37180922886Sopenharmony_ci        std::lock_guard lockGuard(castControllerLock_);
37280922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR,
37380922886Sopenharmony_ci            "cast controller proxy is nullptr");
37480922886Sopenharmony_ci        auto ret = castControllerProxy_->SetValidAbility(cmds);
37580922886Sopenharmony_ci        std::string errMsg = (ret == AVSESSION_SUCCESS) ? "SUCCESS" : "onCastEvent failed";
37680922886Sopenharmony_ci        std::string API_PARAM_STRING = "cmd: " + std::to_string(cmd);
37780922886Sopenharmony_ci        HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
37880922886Sopenharmony_ci            "API_NAME", "onCastEvent",
37980922886Sopenharmony_ci            "BUNDLE_NAME", BundleStatusAdapter::GetInstance().GetBundleNameFromUid(GetCallingUid()),
38080922886Sopenharmony_ci            "API_PARAM", API_PARAM_STRING,
38180922886Sopenharmony_ci            "ERROR_CODE", ret,
38280922886Sopenharmony_ci            "ERROR_MSG", errMsg);
38380922886Sopenharmony_ci    }
38480922886Sopenharmony_ci    return AVSESSION_SUCCESS;
38580922886Sopenharmony_ci}
38680922886Sopenharmony_ci
38780922886Sopenharmony_ciint32_t AVCastControllerItem::RemoveAvailableCommand(const int32_t cmd)
38880922886Sopenharmony_ci{
38980922886Sopenharmony_ci    SLOGI("remove available command %{public}d", cmd);
39080922886Sopenharmony_ci    std::vector<int32_t> cmds(AVCastControlCommand::CAST_CONTROL_CMD_MAX);
39180922886Sopenharmony_ci    {
39280922886Sopenharmony_ci        std::lock_guard<std::mutex> lock(callbackToSessionLock_);
39380922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(isSessionCallbackAvailable_, AVSESSION_ERROR, "sessionCallback not available");
39480922886Sopenharmony_ci    }
39580922886Sopenharmony_ci    validCommandsChangecallback_(cmd + removeCmdStep_, cmds);
39680922886Sopenharmony_ci    SLOGI("remove available command with size %{public}d", static_cast<int32_t>(cmds.size()));
39780922886Sopenharmony_ci    if (cmds.empty()) {
39880922886Sopenharmony_ci        SLOGI("check is sink session with empty, not set");
39980922886Sopenharmony_ci    } else {
40080922886Sopenharmony_ci        std::lock_guard lockGuard(castControllerLock_);
40180922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(castControllerProxy_ != nullptr, AVSESSION_ERROR,
40280922886Sopenharmony_ci            "cast controller proxy is nullptr");
40380922886Sopenharmony_ci        auto ret = castControllerProxy_->SetValidAbility(cmds);
40480922886Sopenharmony_ci        std::string errMsg = (ret == AVSESSION_SUCCESS) ? "SUCCESS" : "offCastEvent failed";
40580922886Sopenharmony_ci        std::string API_PARAM_STRING = "cmd: " + std::to_string(cmd);
40680922886Sopenharmony_ci        HISYSEVENT_BEHAVIOR("SESSION_API_BEHAVIOR",
40780922886Sopenharmony_ci            "API_NAME", "offCastEvent",
40880922886Sopenharmony_ci            "BUNDLE_NAME", BundleStatusAdapter::GetInstance().GetBundleNameFromUid(GetCallingUid()),
40980922886Sopenharmony_ci            "API_PARAM", API_PARAM_STRING,
41080922886Sopenharmony_ci            "ERROR_CODE", ret,
41180922886Sopenharmony_ci            "ERROR_MSG", errMsg);
41280922886Sopenharmony_ci    }
41380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
41480922886Sopenharmony_ci}
41580922886Sopenharmony_ci
41680922886Sopenharmony_ciint32_t AVCastControllerItem::HandleCastValidCommandChange(const std::vector<int32_t>& cmds)
41780922886Sopenharmony_ci{
41880922886Sopenharmony_ci    SLOGI("HandleCastValidCommandChange cmd size:%{public}zd", cmds.size());
41980922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVSESSION_ERROR, "callback_ is nullptr");
42080922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
42180922886Sopenharmony_ci    callback_->OnCastValidCommandChanged(cmds);
42280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
42380922886Sopenharmony_ci}
42480922886Sopenharmony_ci
42580922886Sopenharmony_civoid AVCastControllerItem::SetSessionTag(const std::string tag)
42680922886Sopenharmony_ci{
42780922886Sopenharmony_ci    sessionTag_ = tag;
42880922886Sopenharmony_ci}
42980922886Sopenharmony_ci
43080922886Sopenharmony_cibool AVCastControllerItem::RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy)
43180922886Sopenharmony_ci{
43280922886Sopenharmony_ci    SLOGI("Call RegisterControllerListener of cast controller proxy");
43380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(castControllerProxy != nullptr, AVSESSION_ERROR, "cast controller proxy is nullptr");
43480922886Sopenharmony_ci    return castControllerProxy->RegisterControllerListener(shared_from_this());
43580922886Sopenharmony_ci}
43680922886Sopenharmony_ci
43780922886Sopenharmony_ciint32_t AVCastControllerItem::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
43880922886Sopenharmony_ci{
43980922886Sopenharmony_ci    SLOGI("call RegisterCallbackInner of cast controller proxy");
44080922886Sopenharmony_ci    std::lock_guard lockGuard(castControllerCallbackLock_);
44180922886Sopenharmony_ci    callback_ = iface_cast<AVCastControllerCallbackProxy>(callback);
44280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVSESSION_ERROR, "callback_ is nullptr");
44380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
44480922886Sopenharmony_ci}
44580922886Sopenharmony_ci
44680922886Sopenharmony_ciint32_t AVCastControllerItem::Destroy()
44780922886Sopenharmony_ci{
44880922886Sopenharmony_ci    SLOGI("Start cast controller destroy process with sessionCallback available set");
44980922886Sopenharmony_ci    {
45080922886Sopenharmony_ci        std::lock_guard lockGuard(castControllerLock_);
45180922886Sopenharmony_ci        if (castControllerProxy_) {
45280922886Sopenharmony_ci            castControllerProxy_ = nullptr;
45380922886Sopenharmony_ci        }
45480922886Sopenharmony_ci    }
45580922886Sopenharmony_ci    {
45680922886Sopenharmony_ci        std::lock_guard lockGuard(castControllerCallbackLock_);
45780922886Sopenharmony_ci        if (callback_) {
45880922886Sopenharmony_ci            callback_ = nullptr;
45980922886Sopenharmony_ci        }
46080922886Sopenharmony_ci    }
46180922886Sopenharmony_ci    {
46280922886Sopenharmony_ci        std::lock_guard<std::mutex> lock(callbackToSessionLock_);
46380922886Sopenharmony_ci        isSessionCallbackAvailable_ = false;
46480922886Sopenharmony_ci    }
46580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
46680922886Sopenharmony_ci}
46780922886Sopenharmony_ci} // namespace OHOS::AVSession
468