180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2022-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 "avcontroller_item.h"
1780922886Sopenharmony_ci#include "ipc_skeleton.h"
1880922886Sopenharmony_ci#include "avsession_errors.h"
1980922886Sopenharmony_ci#include "avsession_log.h"
2080922886Sopenharmony_ci#include "avsession_trace.h"
2180922886Sopenharmony_ci#include "command_send_limit.h"
2280922886Sopenharmony_ci#include "avsession_utils.h"
2380922886Sopenharmony_ci#include "permission_checker.h"
2480922886Sopenharmony_ci#include "avsession_sysevent.h"
2580922886Sopenharmony_ci#include "want_params.h"
2680922886Sopenharmony_ci
2780922886Sopenharmony_ci#if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM) and !defined(IOS_PLATFORM)
2880922886Sopenharmony_ci#include <malloc.h>
2980922886Sopenharmony_ci#endif
3080922886Sopenharmony_ci
3180922886Sopenharmony_cinamespace OHOS::AVSession {
3280922886Sopenharmony_ciAVControllerItem::AVControllerItem(pid_t pid, const sptr<AVSessionItem>& session, int32_t userId)
3380922886Sopenharmony_ci    : pid_(pid), session_(session), userId_(userId)
3480922886Sopenharmony_ci{
3580922886Sopenharmony_ci    std::lock_guard sessionLockGuard(sessionMutex_);
3680922886Sopenharmony_ci    if (session_ != nullptr) {
3780922886Sopenharmony_ci        sessionId_ = session_->GetSessionId();
3880922886Sopenharmony_ci    }
3980922886Sopenharmony_ci}
4080922886Sopenharmony_ci
4180922886Sopenharmony_ciAVControllerItem::~AVControllerItem()
4280922886Sopenharmony_ci{
4380922886Sopenharmony_ci}
4480922886Sopenharmony_ci
4580922886Sopenharmony_ciint32_t AVControllerItem::GetUserId() const
4680922886Sopenharmony_ci{
4780922886Sopenharmony_ci    return userId_;
4880922886Sopenharmony_ci}
4980922886Sopenharmony_ci
5080922886Sopenharmony_ciint32_t AVControllerItem::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
5180922886Sopenharmony_ci{
5280922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
5380922886Sopenharmony_ci    SLOGD("do register callback for controller %{public}d", static_cast<int>(pid_));
5480922886Sopenharmony_ci    callback_ = iface_cast<AVControllerCallbackProxy>(callback);
5580922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVSESSION_ERROR, "RegisterCallbackInner callback_ is nullptr");
5680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
5780922886Sopenharmony_ci}
5880922886Sopenharmony_ci
5980922886Sopenharmony_ciint32_t AVControllerItem::RegisterAVControllerCallback(const std::shared_ptr<AVControllerCallback> &callback)
6080922886Sopenharmony_ci{
6180922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
6280922886Sopenharmony_ci    innerCallback_ = callback;
6380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(innerCallback_ != nullptr, AVSESSION_ERROR, "RegisterCallbackInner callback_ is nullptr");
6480922886Sopenharmony_ci    return AVSESSION_SUCCESS;
6580922886Sopenharmony_ci}
6680922886Sopenharmony_ci
6780922886Sopenharmony_ciint32_t AVControllerItem::GetAVCallMetaData(AVCallMetaData& avCallMetaData)
6880922886Sopenharmony_ci{
6980922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
7080922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
7180922886Sopenharmony_ci    avCallMetaData = session_->GetAVCallMetaData();
7280922886Sopenharmony_ci    return AVSESSION_SUCCESS;
7380922886Sopenharmony_ci}
7480922886Sopenharmony_ci
7580922886Sopenharmony_ciint32_t AVControllerItem::GetAVCallState(AVCallState& avCallState)
7680922886Sopenharmony_ci{
7780922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
7880922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
7980922886Sopenharmony_ci    avCallState = session_->GetAVCallState();
8080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
8180922886Sopenharmony_ci}
8280922886Sopenharmony_ci
8380922886Sopenharmony_ci// LCOV_EXCL_START
8480922886Sopenharmony_ciint32_t AVControllerItem::GetAVPlaybackState(AVPlaybackState& state)
8580922886Sopenharmony_ci{
8680922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
8780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
8880922886Sopenharmony_ci    state = session_->GetPlaybackState();
8980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
9080922886Sopenharmony_ci}
9180922886Sopenharmony_ci// LCOV_EXCL_STOP
9280922886Sopenharmony_ci
9380922886Sopenharmony_ciint32_t AVControllerItem::GetAVMetaData(AVMetaData& data)
9480922886Sopenharmony_ci{
9580922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
9680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
9780922886Sopenharmony_ci    data = session_->GetMetaData();
9880922886Sopenharmony_ci    if (data.GetMediaImage() != nullptr && !data.GetMediaImageUri().empty()) {
9980922886Sopenharmony_ci        SLOGI("check isFromSession %{public}d when have two image resources", isFromSession_);
10080922886Sopenharmony_ci        if (isFromSession_) {
10180922886Sopenharmony_ci            data.GetMediaImage()->Clear();
10280922886Sopenharmony_ci        } else {
10380922886Sopenharmony_ci            return ERR_INVALID_PARAM;
10480922886Sopenharmony_ci        }
10580922886Sopenharmony_ci    }
10680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
10780922886Sopenharmony_ci}
10880922886Sopenharmony_ci
10980922886Sopenharmony_ci// LCOV_EXCL_START
11080922886Sopenharmony_ciint32_t AVControllerItem::GetAVQueueItems(std::vector<AVQueueItem>& items)
11180922886Sopenharmony_ci{
11280922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
11380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
11480922886Sopenharmony_ci    items = session_->GetQueueItems();
11580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
11680922886Sopenharmony_ci}
11780922886Sopenharmony_ci
11880922886Sopenharmony_ciint32_t AVControllerItem::GetAVQueueTitle(std::string& title)
11980922886Sopenharmony_ci{
12080922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
12180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
12280922886Sopenharmony_ci    title = session_->GetQueueTitle();
12380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
12480922886Sopenharmony_ci}
12580922886Sopenharmony_ci
12680922886Sopenharmony_ciint32_t AVControllerItem::SkipToQueueItem(int32_t& itemId)
12780922886Sopenharmony_ci{
12880922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
12980922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
13080922886Sopenharmony_ci    session_->HandleSkipToQueueItem(itemId);
13180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
13280922886Sopenharmony_ci}
13380922886Sopenharmony_ci
13480922886Sopenharmony_ciint32_t AVControllerItem::GetExtras(AAFwk::WantParams& extras)
13580922886Sopenharmony_ci{
13680922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
13780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
13880922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(CommandSendLimit::GetInstance().IsCommandSendEnable(OHOS::IPCSkeleton::GetCallingPid()),
13980922886Sopenharmony_ci        ERR_COMMAND_SEND_EXCEED_MAX, "command send number exceed max");
14080922886Sopenharmony_ci    extras = session_->GetExtras();
14180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
14280922886Sopenharmony_ci}
14380922886Sopenharmony_ci
14480922886Sopenharmony_ciint32_t AVControllerItem::SendAVKeyEvent(const MMI::KeyEvent& keyEvent)
14580922886Sopenharmony_ci{
14680922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
14780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
14880922886Sopenharmony_ci    session_->HandleMediaKeyEvent(keyEvent);
14980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
15080922886Sopenharmony_ci}
15180922886Sopenharmony_ci
15280922886Sopenharmony_ciint32_t AVControllerItem::GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent& ability)
15380922886Sopenharmony_ci{
15480922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
15580922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
15680922886Sopenharmony_ci    ability = session_->GetLaunchAbility();
15780922886Sopenharmony_ci    return AVSESSION_SUCCESS;
15880922886Sopenharmony_ci}
15980922886Sopenharmony_ci
16080922886Sopenharmony_ciint32_t AVControllerItem::GetValidCommands(std::vector<int32_t>& cmds)
16180922886Sopenharmony_ci{
16280922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
16380922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
16480922886Sopenharmony_ci    cmds = session_->GetSupportCommand();
16580922886Sopenharmony_ci    return AVSESSION_SUCCESS;
16680922886Sopenharmony_ci}
16780922886Sopenharmony_ci// LCOV_EXCL_STOP
16880922886Sopenharmony_ci
16980922886Sopenharmony_ciint32_t AVControllerItem::IsSessionActive(bool& isActive)
17080922886Sopenharmony_ci{
17180922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
17280922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
17380922886Sopenharmony_ci    isActive = session_->IsActive();
17480922886Sopenharmony_ci    return AVSESSION_SUCCESS;
17580922886Sopenharmony_ci}
17680922886Sopenharmony_ci
17780922886Sopenharmony_ci// LCOV_EXCL_START
17880922886Sopenharmony_ciint32_t AVControllerItem::SendControlCommand(const AVControlCommand& cmd)
17980922886Sopenharmony_ci{
18080922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
18180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
18280922886Sopenharmony_ci    std::vector<int32_t> cmds = session_->GetSupportCommand();
18380922886Sopenharmony_ci    if (std::find(cmds.begin(), cmds.end(), cmd.GetCommand()) == cmds.end()) {
18480922886Sopenharmony_ci        SLOGE("The command that needs to be sent is not supported.");
18580922886Sopenharmony_ci        return ERR_COMMAND_NOT_SUPPORT;
18680922886Sopenharmony_ci    }
18780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(CommandSendLimit::GetInstance().IsCommandSendEnable(OHOS::IPCSkeleton::GetCallingPid()),
18880922886Sopenharmony_ci        ERR_COMMAND_SEND_EXCEED_MAX, "command send number exceed max");
18980922886Sopenharmony_ci    session_->ExecuteControllerCommand(cmd);
19080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
19180922886Sopenharmony_ci}
19280922886Sopenharmony_ci// LCOV_EXCL_STOP
19380922886Sopenharmony_ci
19480922886Sopenharmony_ciint32_t AVControllerItem::SendCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs)
19580922886Sopenharmony_ci{
19680922886Sopenharmony_ci    std::lock_guard lockGuard(sessionMutex_);
19780922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "Session not exist");
19880922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(CommandSendLimit::GetInstance().IsCommandSendEnable(OHOS::IPCSkeleton::GetCallingPid()),
19980922886Sopenharmony_ci        ERR_COMMAND_SEND_EXCEED_MAX, "common command send number exceed max");
20080922886Sopenharmony_ci    session_->ExecueCommonCommand(commonCommand, commandArgs);
20180922886Sopenharmony_ci    return AVSESSION_SUCCESS;
20280922886Sopenharmony_ci}
20380922886Sopenharmony_ci
20480922886Sopenharmony_ci// LCOV_EXCL_START
20580922886Sopenharmony_ciint32_t AVControllerItem::SetAVCallMetaFilter(const AVCallMetaData::AVCallMetaMaskType& filter)
20680922886Sopenharmony_ci{
20780922886Sopenharmony_ci    std::lock_guard lockGuard(avCallMetaMaskMutex_);
20880922886Sopenharmony_ci    avCallMetaMask_ = filter;
20980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
21080922886Sopenharmony_ci}
21180922886Sopenharmony_ci
21280922886Sopenharmony_ciint32_t AVControllerItem::SetAVCallStateFilter(const AVCallState::AVCallStateMaskType& filter)
21380922886Sopenharmony_ci{
21480922886Sopenharmony_ci    std::lock_guard lockGuard(avCallStateMaskMutex_);
21580922886Sopenharmony_ci    avCallStateMask_ = filter;
21680922886Sopenharmony_ci    return AVSESSION_SUCCESS;
21780922886Sopenharmony_ci}
21880922886Sopenharmony_ci
21980922886Sopenharmony_ciint32_t AVControllerItem::SetMetaFilter(const AVMetaData::MetaMaskType& filter)
22080922886Sopenharmony_ci{
22180922886Sopenharmony_ci    std::lock_guard lockGuard(metaMaskMutex_);
22280922886Sopenharmony_ci    metaMask_ = filter;
22380922886Sopenharmony_ci    return AVSESSION_SUCCESS;
22480922886Sopenharmony_ci}
22580922886Sopenharmony_ci
22680922886Sopenharmony_ciint32_t AVControllerItem::SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
22780922886Sopenharmony_ci{
22880922886Sopenharmony_ci    std::lock_guard lockGuard(playbackMaskMutex_);
22980922886Sopenharmony_ci    playbackMask_ = filter;
23080922886Sopenharmony_ci    return AVSESSION_SUCCESS;
23180922886Sopenharmony_ci}
23280922886Sopenharmony_ci// LCOV_EXCL_STOP
23380922886Sopenharmony_ci
23480922886Sopenharmony_ciint32_t AVControllerItem::Destroy()
23580922886Sopenharmony_ci{
23680922886Sopenharmony_ci    SLOGI("controller destroyed for pid %{public}d", static_cast<int>(pid_));
23780922886Sopenharmony_ci    {
23880922886Sopenharmony_ci        std::lock_guard callbackLockGuard(callbackMutex_);
23980922886Sopenharmony_ci        callback_ = nullptr;
24080922886Sopenharmony_ci        innerCallback_ = nullptr;
24180922886Sopenharmony_ci    }
24280922886Sopenharmony_ci
24380922886Sopenharmony_ci    {
24480922886Sopenharmony_ci        std::lock_guard serviceCallbacklockGuard(serviceCallbackMutex_);
24580922886Sopenharmony_ci        if (serviceCallback_) {
24680922886Sopenharmony_ci            serviceCallback_(*this);
24780922886Sopenharmony_ci        }
24880922886Sopenharmony_ci    }
24980922886Sopenharmony_ci
25080922886Sopenharmony_ci    {
25180922886Sopenharmony_ci        std::lock_guard sessionLockGuard(sessionMutex_);
25280922886Sopenharmony_ci        if (session_ != nullptr) {
25380922886Sopenharmony_ci            session_->HandleControllerRelease(pid_);
25480922886Sopenharmony_ci            session_ = nullptr;
25580922886Sopenharmony_ci            sessionId_.clear();
25680922886Sopenharmony_ci        }
25780922886Sopenharmony_ci    }
25880922886Sopenharmony_ci
25980922886Sopenharmony_ci    return AVSESSION_SUCCESS;
26080922886Sopenharmony_ci}
26180922886Sopenharmony_ci
26280922886Sopenharmony_ci// LCOV_EXCL_START
26380922886Sopenharmony_cistd::string AVControllerItem::GetSessionId()
26480922886Sopenharmony_ci{
26580922886Sopenharmony_ci    std::lock_guard sessionLockGuard(sessionMutex_);
26680922886Sopenharmony_ci    return sessionId_;
26780922886Sopenharmony_ci}
26880922886Sopenharmony_ci// LCOV_EXCL_STOP
26980922886Sopenharmony_ci
27080922886Sopenharmony_civoid AVControllerItem::HandleSessionDestroy()
27180922886Sopenharmony_ci{
27280922886Sopenharmony_ci    {
27380922886Sopenharmony_ci        std::lock_guard callbackLockGuard(callbackMutex_);
27480922886Sopenharmony_ci        if (callback_ != nullptr) {
27580922886Sopenharmony_ci            callback_->OnSessionDestroy();
27680922886Sopenharmony_ci        }
27780922886Sopenharmony_ci        if (innerCallback_ != nullptr) {
27880922886Sopenharmony_ci            innerCallback_->OnSessionDestroy();
27980922886Sopenharmony_ci        }
28080922886Sopenharmony_ci    }
28180922886Sopenharmony_ci    std::lock_guard sessionLockGuard(sessionMutex_);
28280922886Sopenharmony_ci    session_ = nullptr;
28380922886Sopenharmony_ci    sessionId_.clear();
28480922886Sopenharmony_ci}
28580922886Sopenharmony_ci
28680922886Sopenharmony_ci// LCOV_EXCL_START
28780922886Sopenharmony_civoid AVControllerItem::HandleAVCallStateChange(const AVCallState& avCallState)
28880922886Sopenharmony_ci{
28980922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackMutex_);
29080922886Sopenharmony_ci    AVCallState stateOut;
29180922886Sopenharmony_ci    std::lock_guard avCallStateLockGuard(avCallStateMaskMutex_);
29280922886Sopenharmony_ci    if (avCallState.CopyToByMask(avCallStateMask_, stateOut)) {
29380922886Sopenharmony_ci        SLOGI("update avcall state");
29480922886Sopenharmony_ci        AVSESSION_TRACE_SYNC_START("AVControllerItem::OnAVCallStateChange");
29580922886Sopenharmony_ci        if (callback_ != nullptr) {
29680922886Sopenharmony_ci            callback_->OnAVCallStateChange(stateOut);
29780922886Sopenharmony_ci        }
29880922886Sopenharmony_ci        if (innerCallback_ != nullptr) {
29980922886Sopenharmony_ci            innerCallback_->OnAVCallStateChange(stateOut);
30080922886Sopenharmony_ci        }
30180922886Sopenharmony_ci    }
30280922886Sopenharmony_ci}
30380922886Sopenharmony_ci
30480922886Sopenharmony_civoid AVControllerItem::HandleAVCallMetaDataChange(const AVCallMetaData& avCallMetaData)
30580922886Sopenharmony_ci{
30680922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackMutex_);
30780922886Sopenharmony_ci    AVCallMetaData metaOut;
30880922886Sopenharmony_ci    std::lock_guard avCallMetaDataMaskLockGuard(avCallMetaMaskMutex_);
30980922886Sopenharmony_ci    if (avCallMetaData.CopyToByMask(avCallMetaMask_, metaOut)) {
31080922886Sopenharmony_ci        if (avCallMetaMask_.test(AVCallMetaData::AVCALL_META_KEY_MEDIA_IMAGE)) {
31180922886Sopenharmony_ci            std::shared_ptr<AVSessionPixelMap> innerPixelMap = nullptr;
31280922886Sopenharmony_ci            if (metaOut.GetMediaImage() != nullptr) {
31380922886Sopenharmony_ci                std::string fileDir = AVSessionUtils::GetCachePathName(userId_);
31480922886Sopenharmony_ci                std::string fileName = sessionId_ + AVSessionUtils::GetFileSuffix();
31580922886Sopenharmony_ci                innerPixelMap = metaOut.GetMediaImage();
31680922886Sopenharmony_ci                AVSessionUtils::ReadImageFromFile(innerPixelMap, fileDir, fileName);
31780922886Sopenharmony_ci            }
31880922886Sopenharmony_ci            metaOut.SetMediaImage(innerPixelMap);
31980922886Sopenharmony_ci        }
32080922886Sopenharmony_ci
32180922886Sopenharmony_ci        if (avCallMetaMask_.test(AVCallMetaData::AVCALL_META_KEY_NAME)) {
32280922886Sopenharmony_ci            metaOut.SetName(avCallMetaData.GetName());
32380922886Sopenharmony_ci        }
32480922886Sopenharmony_ci
32580922886Sopenharmony_ci        if (avCallMetaMask_.test(AVCallMetaData::AVCALL_META_KEY_PHONE_NUMBER)) {
32680922886Sopenharmony_ci            metaOut.SetPhoneNumber(avCallMetaData.GetPhoneNumber());
32780922886Sopenharmony_ci        }
32880922886Sopenharmony_ci        SLOGI("update avcall meta data");
32980922886Sopenharmony_ci    }
33080922886Sopenharmony_ci
33180922886Sopenharmony_ci    if (callback_ != nullptr) {
33280922886Sopenharmony_ci        callback_->OnAVCallMetaDataChange(metaOut);
33380922886Sopenharmony_ci    }
33480922886Sopenharmony_ci    if (innerCallback_ != nullptr) {
33580922886Sopenharmony_ci        innerCallback_->OnAVCallMetaDataChange(metaOut);
33680922886Sopenharmony_ci    }
33780922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVControllerItem::OnAVCallMetaDataChange");
33880922886Sopenharmony_ci}
33980922886Sopenharmony_ci
34080922886Sopenharmony_civoid AVControllerItem::HandlePlaybackStateChange(const AVPlaybackState& state)
34180922886Sopenharmony_ci{
34280922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackMutex_);
34380922886Sopenharmony_ci    AVPlaybackState stateOut;
34480922886Sopenharmony_ci    std::lock_guard playbackLockGuard(playbackMaskMutex_);
34580922886Sopenharmony_ci    if (state.CopyToByMask(playbackMask_, stateOut)) {
34680922886Sopenharmony_ci        AVSESSION_TRACE_SYNC_START("AVControllerItem::OnPlaybackStateChange");
34780922886Sopenharmony_ci        if (callback_ != nullptr) {
34880922886Sopenharmony_ci            callback_->OnPlaybackStateChange(stateOut);
34980922886Sopenharmony_ci        }
35080922886Sopenharmony_ci        if (innerCallback_ != nullptr) {
35180922886Sopenharmony_ci            innerCallback_->OnPlaybackStateChange(stateOut);
35280922886Sopenharmony_ci        }
35380922886Sopenharmony_ci    }
35480922886Sopenharmony_ci}
35580922886Sopenharmony_ci
35680922886Sopenharmony_civoid AVControllerItem::HandleMetaDataChange(const AVMetaData& data)
35780922886Sopenharmony_ci{
35880922886Sopenharmony_ci    std::lock_guard callbackLockGuard(callbackMutex_);
35980922886Sopenharmony_ci    AVMetaData metaOut;
36080922886Sopenharmony_ci    std::lock_guard metaMaskLockGuard(metaMaskMutex_);
36180922886Sopenharmony_ci    if (data.CopyToByMask(metaMask_, metaOut)) {
36280922886Sopenharmony_ci        if ((metaMask_.test(AVMetaData::META_KEY_MEDIA_IMAGE)) && (metaOut.GetMediaImage() != nullptr)) {
36380922886Sopenharmony_ci            std::string fileDir = AVSessionUtils::GetCachePathName(userId_);
36480922886Sopenharmony_ci            std::shared_ptr<AVSessionPixelMap> innerPixelMap = metaOut.GetMediaImage();
36580922886Sopenharmony_ci            AVSessionUtils::ReadImageFromFile(innerPixelMap, fileDir, sessionId_ + AVSessionUtils::GetFileSuffix());
36680922886Sopenharmony_ci            metaOut.SetMediaImage(innerPixelMap);
36780922886Sopenharmony_ci        }
36880922886Sopenharmony_ci        if ((metaMask_.test(AVMetaData::META_KEY_AVQUEUE_IMAGE)) && (metaOut.GetAVQueueImage() != nullptr)) {
36980922886Sopenharmony_ci            std::string avQueueFileDir = AVSessionUtils::GetFixedPathName(userId_);
37080922886Sopenharmony_ci            std::string avQueueFileName =
37180922886Sopenharmony_ci                session_->GetBundleName() + "_" + metaOut.GetAVQueueId() + AVSessionUtils::GetFileSuffix();
37280922886Sopenharmony_ci            std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaOut.GetAVQueueImage();
37380922886Sopenharmony_ci            AVSessionUtils::ReadImageFromFile(avQueuePixelMap, avQueueFileDir, avQueueFileName);
37480922886Sopenharmony_ci            metaOut.SetAVQueueImage(avQueuePixelMap);
37580922886Sopenharmony_ci        }
37680922886Sopenharmony_ci        if (!metaMask_.test(AVMetaData::META_KEY_ASSET_ID)) {
37780922886Sopenharmony_ci            metaOut.SetAssetId(data.GetAssetId());
37880922886Sopenharmony_ci        }
37980922886Sopenharmony_ci        SLOGI("update metaData pid %{public}d, title %{public}s", static_cast<int>(pid_), metaOut.GetTitle().c_str());
38080922886Sopenharmony_ci        AVSESSION_TRACE_SYNC_START("AVControllerItem::OnMetaDataChange");
38180922886Sopenharmony_ci        if (metaOut.GetMediaImage() != nullptr && !metaOut.GetMediaImageUri().empty()) {
38280922886Sopenharmony_ci            SLOGI("check isFromSession %{public}d when have two image resources", isFromSession_);
38380922886Sopenharmony_ci            if (isFromSession_) {
38480922886Sopenharmony_ci                metaOut.GetMediaImage()->Clear();
38580922886Sopenharmony_ci            } else {
38680922886Sopenharmony_ci                metaOut.SetMediaImageUri("");
38780922886Sopenharmony_ci            }
38880922886Sopenharmony_ci        }
38980922886Sopenharmony_ci        if (callback_ != nullptr) {
39080922886Sopenharmony_ci            callback_->OnMetaDataChange(metaOut);
39180922886Sopenharmony_ci        }
39280922886Sopenharmony_ci        if (innerCallback_ != nullptr) {
39380922886Sopenharmony_ci            innerCallback_->OnMetaDataChange(metaOut);
39480922886Sopenharmony_ci        }
39580922886Sopenharmony_ci    }
39680922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> innerQueuePixelMap = metaOut.GetAVQueueImage();
39780922886Sopenharmony_ci    if (innerQueuePixelMap != nullptr) {
39880922886Sopenharmony_ci        innerQueuePixelMap->Clear();
39980922886Sopenharmony_ci    }
40080922886Sopenharmony_ci    std::shared_ptr<AVSessionPixelMap> innerMediaPixelMap = metaOut.GetMediaImage();
40180922886Sopenharmony_ci    if (innerMediaPixelMap != nullptr) {
40280922886Sopenharmony_ci        innerMediaPixelMap->Clear();
40380922886Sopenharmony_ci    }
40480922886Sopenharmony_ci}
40580922886Sopenharmony_ci
40680922886Sopenharmony_civoid AVControllerItem::HandleOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo)
40780922886Sopenharmony_ci{
40880922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
40980922886Sopenharmony_ci    if (callback_ != nullptr) {
41080922886Sopenharmony_ci        callback_->OnOutputDeviceChange(connectionState, outputDeviceInfo);
41180922886Sopenharmony_ci    }
41280922886Sopenharmony_ci}
41380922886Sopenharmony_ci// LCOV_EXCL_STOP
41480922886Sopenharmony_ci
41580922886Sopenharmony_civoid AVControllerItem::HandleActiveStateChange(bool isActive)
41680922886Sopenharmony_ci{
41780922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
41880922886Sopenharmony_ci    if (callback_ != nullptr) {
41980922886Sopenharmony_ci        callback_->OnActiveStateChange(isActive);
42080922886Sopenharmony_ci    }
42180922886Sopenharmony_ci}
42280922886Sopenharmony_ci
42380922886Sopenharmony_ci// LCOV_EXCL_START
42480922886Sopenharmony_civoid AVControllerItem::HandleValidCommandChange(const std::vector<int32_t>& cmds)
42580922886Sopenharmony_ci{
42680922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
42780922886Sopenharmony_ci    SLOGD("do OnValidCommandChange with pid %{public}d cmd list size %{public}d",
42880922886Sopenharmony_ci        static_cast<int>(pid_), static_cast<int>(cmds.size()));
42980922886Sopenharmony_ci    if (callback_ != nullptr) {
43080922886Sopenharmony_ci        callback_->OnValidCommandChange(cmds);
43180922886Sopenharmony_ci    }
43280922886Sopenharmony_ci}
43380922886Sopenharmony_ci// LCOV_EXCL_STOP
43480922886Sopenharmony_ci
43580922886Sopenharmony_civoid AVControllerItem::HandleSetSessionEvent(const std::string& event, const AAFwk::WantParams& args)
43680922886Sopenharmony_ci{
43780922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
43880922886Sopenharmony_ci    if (callback_ != nullptr) {
43980922886Sopenharmony_ci        callback_->OnSessionEventChange(event, args);
44080922886Sopenharmony_ci    }
44180922886Sopenharmony_ci}
44280922886Sopenharmony_ci
44380922886Sopenharmony_ci// LCOV_EXCL_START
44480922886Sopenharmony_civoid AVControllerItem::HandleQueueItemsChange(const std::vector<AVQueueItem>& items)
44580922886Sopenharmony_ci{
44680922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
44780922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVControllerItem::OnQueueItemsChange");
44880922886Sopenharmony_ci    if (callback_ != nullptr) {
44980922886Sopenharmony_ci        callback_->OnQueueItemsChange(items);
45080922886Sopenharmony_ci    }
45180922886Sopenharmony_ci}
45280922886Sopenharmony_ci
45380922886Sopenharmony_civoid AVControllerItem::HandleQueueTitleChange(const std::string& title)
45480922886Sopenharmony_ci{
45580922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
45680922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVControllerItem::OnQueueTitleChange");
45780922886Sopenharmony_ci    if (callback_ != nullptr) {
45880922886Sopenharmony_ci        callback_->OnQueueTitleChange(title);
45980922886Sopenharmony_ci    }
46080922886Sopenharmony_ci}
46180922886Sopenharmony_ci
46280922886Sopenharmony_civoid AVControllerItem::HandleExtrasChange(const AAFwk::WantParams& extras)
46380922886Sopenharmony_ci{
46480922886Sopenharmony_ci    std::lock_guard lockGuard(callbackMutex_);
46580922886Sopenharmony_ci    AVSESSION_TRACE_SYNC_START("AVControllerItem::OnSetExtras");
46680922886Sopenharmony_ci    if (callback_ != nullptr) {
46780922886Sopenharmony_ci        callback_->OnExtrasChange(extras);
46880922886Sopenharmony_ci    }
46980922886Sopenharmony_ci}
47080922886Sopenharmony_ci// LCOV_EXCL_STOP
47180922886Sopenharmony_ci
47280922886Sopenharmony_cipid_t AVControllerItem::GetPid() const
47380922886Sopenharmony_ci{
47480922886Sopenharmony_ci    return pid_;
47580922886Sopenharmony_ci}
47680922886Sopenharmony_ci
47780922886Sopenharmony_cibool AVControllerItem::HasSession(const std::string& sessionId)
47880922886Sopenharmony_ci{
47980922886Sopenharmony_ci    std::lock_guard sessionLockGuard(sessionMutex_);
48080922886Sopenharmony_ci    return sessionId_ == sessionId;
48180922886Sopenharmony_ci}
48280922886Sopenharmony_ci
48380922886Sopenharmony_civoid AVControllerItem::SetServiceCallbackForRelease(const std::function<void(AVControllerItem&)>& callback)
48480922886Sopenharmony_ci{
48580922886Sopenharmony_ci    std::lock_guard lockGuard(serviceCallbackMutex_);
48680922886Sopenharmony_ci    serviceCallback_ = callback;
48780922886Sopenharmony_ci}
48880922886Sopenharmony_ci} // namespace OHOS::AVSession
489