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 "avsession_proxy.h" 1780922886Sopenharmony_ci#include "avsession_callback_client.h" 1880922886Sopenharmony_ci#include "avsession_controller_proxy.h" 1980922886Sopenharmony_ci#include "iavsession_callback.h" 2080922886Sopenharmony_ci#include "avsession_log.h" 2180922886Sopenharmony_ci#include "avsession_errors.h" 2280922886Sopenharmony_ci#include "avsession_trace.h" 2380922886Sopenharmony_ci 2480922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE 2580922886Sopenharmony_ci#include "avcast_controller_proxy.h" 2680922886Sopenharmony_ci#endif 2780922886Sopenharmony_ci 2880922886Sopenharmony_cinamespace OHOS::AVSession { 2980922886Sopenharmony_ciAVSessionProxy::AVSessionProxy(const sptr<IRemoteObject>& impl) 3080922886Sopenharmony_ci : IRemoteProxy<IAVSession>(impl) 3180922886Sopenharmony_ci{ 3280922886Sopenharmony_ci SLOGD("construct"); 3380922886Sopenharmony_ci} 3480922886Sopenharmony_ci 3580922886Sopenharmony_ciAVSessionProxy::~AVSessionProxy() 3680922886Sopenharmony_ci{ 3780922886Sopenharmony_ci SLOGI("destroy"); 3880922886Sopenharmony_ci Destroy(); 3980922886Sopenharmony_ci} 4080922886Sopenharmony_ci 4180922886Sopenharmony_cistd::string AVSessionProxy::GetSessionId() 4280922886Sopenharmony_ci{ 4380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, "", "session is destroyed"); 4480922886Sopenharmony_ci MessageParcel data; 4580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed"); 4680922886Sopenharmony_ci MessageParcel reply; 4780922886Sopenharmony_ci MessageOption option; 4880922886Sopenharmony_ci auto remote = Remote(); 4980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed"); 5080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_SESSION_ID, data, reply, option) == 0, 5180922886Sopenharmony_ci "", "send request failed"); 5280922886Sopenharmony_ci 5380922886Sopenharmony_ci std::string sessionId; 5480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadString(sessionId), "", "read sessionId failed"); 5580922886Sopenharmony_ci return sessionId; 5680922886Sopenharmony_ci} 5780922886Sopenharmony_ci 5880922886Sopenharmony_cistd::string AVSessionProxy::GetSessionType() 5980922886Sopenharmony_ci{ 6080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, "", "session is destroyed"); 6180922886Sopenharmony_ci MessageParcel data; 6280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed"); 6380922886Sopenharmony_ci MessageParcel reply; 6480922886Sopenharmony_ci MessageOption option; 6580922886Sopenharmony_ci auto remote = Remote(); 6680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed"); 6780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_SESSION_TYPE, data, reply, option) == 0, 6880922886Sopenharmony_ci "", "send request failed"); 6980922886Sopenharmony_ci 7080922886Sopenharmony_ci std::string sessionType; 7180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadString(sessionType), "", "read sessionType failed"); 7280922886Sopenharmony_ci return sessionType; 7380922886Sopenharmony_ci} 7480922886Sopenharmony_ci 7580922886Sopenharmony_ciint32_t AVSessionProxy::RegisterCallback(const std::shared_ptr<AVSessionCallback>& callback) 7680922886Sopenharmony_ci{ 7780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 7880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr"); 7980922886Sopenharmony_ci callback_ = new(std::nothrow) AVSessionCallbackClient(callback); 8080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_NO_MEMORY, "new AVSessionCallbackClient failed"); 8180922886Sopenharmony_ci if (RegisterCallbackInner(callback_) != AVSESSION_SUCCESS) { 8280922886Sopenharmony_ci callback_.clear(); 8380922886Sopenharmony_ci return AVSESSION_ERROR; 8480922886Sopenharmony_ci } 8580922886Sopenharmony_ci return AVSESSION_SUCCESS; 8680922886Sopenharmony_ci} 8780922886Sopenharmony_ci 8880922886Sopenharmony_ciint32_t AVSessionProxy::RegisterCallbackInner(const sptr<IAVSessionCallback>& callback) 8980922886Sopenharmony_ci{ 9080922886Sopenharmony_ci MessageParcel data; 9180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING, 9280922886Sopenharmony_ci "write interface token failed"); 9380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), ERR_MARSHALLING, 9480922886Sopenharmony_ci "write remote object failed"); 9580922886Sopenharmony_ci MessageParcel reply; 9680922886Sopenharmony_ci MessageOption option; 9780922886Sopenharmony_ci auto remote = Remote(); 9880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 9980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_REGISTER_CALLBACK, data, reply, option) == 0, 10080922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 10180922886Sopenharmony_ci 10280922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 10380922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 10480922886Sopenharmony_ci} 10580922886Sopenharmony_ci 10680922886Sopenharmony_ciint32_t AVSessionProxy::Destroy() 10780922886Sopenharmony_ci{ 10880922886Sopenharmony_ci std::lock_guard isDestroyedLockGuard(isDestroyedLock_); 10980922886Sopenharmony_ci SLOGI("enter"); 11080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 11180922886Sopenharmony_ci MessageParcel data; 11280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 11380922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 11480922886Sopenharmony_ci MessageParcel reply; 11580922886Sopenharmony_ci MessageOption option; 11680922886Sopenharmony_ci auto remote = Remote(); 11780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 11880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DESTROY, data, reply, option) == 0, 11980922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 12080922886Sopenharmony_ci 12180922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 12280922886Sopenharmony_ci if (!reply.ReadInt32(ret)) { 12380922886Sopenharmony_ci return AVSESSION_ERROR; 12480922886Sopenharmony_ci } 12580922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 12680922886Sopenharmony_ci isDestroyed_ = true; 12780922886Sopenharmony_ci controller_ = nullptr; 12880922886Sopenharmony_ci } 12980922886Sopenharmony_ci return ret; 13080922886Sopenharmony_ci} 13180922886Sopenharmony_ci 13280922886Sopenharmony_ciint32_t AVSessionProxy::SetAVCallMetaData(const AVCallMetaData& avCallMetaData) 13380922886Sopenharmony_ci{ 13480922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVCallMetaData"); 13580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(avCallMetaData.IsValid(), ERR_INVALID_PARAM, "invalid call meta data"); 13680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 13780922886Sopenharmony_ci MessageParcel data; 13880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 13980922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 14080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&avCallMetaData), 14180922886Sopenharmony_ci ERR_MARSHALLING, "WriteParcelable failed"); 14280922886Sopenharmony_ci MessageParcel reply; 14380922886Sopenharmony_ci MessageOption option; 14480922886Sopenharmony_ci auto remote = Remote(); 14580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 14680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_AVCALL_META_DATA, data, reply, option) == 0, 14780922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 14880922886Sopenharmony_ci 14980922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 15080922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 15180922886Sopenharmony_ci} 15280922886Sopenharmony_ci 15380922886Sopenharmony_ciint32_t AVSessionProxy::SetAVCallState(const AVCallState& avCallState) 15480922886Sopenharmony_ci{ 15580922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVCallState"); 15680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(avCallState.IsValid(), ERR_INVALID_PARAM, "av call state not valid"); 15780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 15880922886Sopenharmony_ci MessageParcel data; 15980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 16080922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 16180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&avCallState), 16280922886Sopenharmony_ci ERR_MARSHALLING, "Write state failed"); 16380922886Sopenharmony_ci MessageParcel reply; 16480922886Sopenharmony_ci MessageOption option; 16580922886Sopenharmony_ci auto remote = Remote(); 16680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 16780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_AVCALL_STATE, data, reply, option) == 0, 16880922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 16980922886Sopenharmony_ci 17080922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 17180922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 17280922886Sopenharmony_ci} 17380922886Sopenharmony_ci 17480922886Sopenharmony_ci 17580922886Sopenharmony_ciint32_t AVSessionProxy::GetPixelMapBuffer(AVMetaData& metaData, MessageParcel& data) 17680922886Sopenharmony_ci{ 17780922886Sopenharmony_ci int mediaImageLength = 0; 17880922886Sopenharmony_ci std::vector<uint8_t> mediaImageBuffer; 17980922886Sopenharmony_ci std::shared_ptr<AVSessionPixelMap> mediaPixelMap = metaData.GetMediaImage(); 18080922886Sopenharmony_ci if (mediaPixelMap != nullptr) { 18180922886Sopenharmony_ci mediaImageBuffer = mediaPixelMap->GetInnerImgBuffer(); 18280922886Sopenharmony_ci mediaImageLength = static_cast<int>(mediaImageBuffer.size()); 18380922886Sopenharmony_ci metaData.SetMediaLength(mediaImageLength); 18480922886Sopenharmony_ci } 18580922886Sopenharmony_ci 18680922886Sopenharmony_ci int avQueueImageLength = 0; 18780922886Sopenharmony_ci std::vector<uint8_t> avQueueImageBuffer; 18880922886Sopenharmony_ci std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData.GetAVQueueImage(); 18980922886Sopenharmony_ci if (avQueuePixelMap != nullptr) { 19080922886Sopenharmony_ci avQueueImageBuffer = avQueuePixelMap->GetInnerImgBuffer(); 19180922886Sopenharmony_ci avQueueImageLength = static_cast<int>(avQueueImageBuffer.size()); 19280922886Sopenharmony_ci metaData.SetAVQueueLength(avQueueImageLength); 19380922886Sopenharmony_ci } 19480922886Sopenharmony_ci 19580922886Sopenharmony_ci int twoImageLength = mediaImageLength + avQueueImageLength; 19680922886Sopenharmony_ci if (twoImageLength == 0) { 19780922886Sopenharmony_ci return 0; 19880922886Sopenharmony_ci } 19980922886Sopenharmony_ci 20080922886Sopenharmony_ci unsigned char *buffer = new (std::nothrow) unsigned char[twoImageLength]; 20180922886Sopenharmony_ci if (buffer == nullptr) { 20280922886Sopenharmony_ci SLOGE("new buffer failed of length = %{public}d", twoImageLength); 20380922886Sopenharmony_ci return -1; 20480922886Sopenharmony_ci } 20580922886Sopenharmony_ci 20680922886Sopenharmony_ci for (int i = 0; i < mediaImageLength; i++) { 20780922886Sopenharmony_ci buffer[i] = mediaImageBuffer[i]; 20880922886Sopenharmony_ci } 20980922886Sopenharmony_ci 21080922886Sopenharmony_ci for (int j = mediaImageLength, k = 0; j < twoImageLength && k < avQueueImageLength; j++, k++) { 21180922886Sopenharmony_ci buffer[j] = avQueueImageBuffer[k]; 21280922886Sopenharmony_ci } 21380922886Sopenharmony_ci 21480922886Sopenharmony_ci if (!data.WriteInt32(twoImageLength) || !AVMetaData::MarshallingExceptImg(data, metaData)) { 21580922886Sopenharmony_ci SLOGE("fail to write image length & metadata except img"); 21680922886Sopenharmony_ci delete[] buffer; 21780922886Sopenharmony_ci return -1; 21880922886Sopenharmony_ci } 21980922886Sopenharmony_ci int32_t retForWriteRawData = data.WriteRawData(buffer, twoImageLength); 22080922886Sopenharmony_ci SLOGI("write img raw data ret %{public}d", retForWriteRawData); 22180922886Sopenharmony_ci 22280922886Sopenharmony_ci delete[] buffer; 22380922886Sopenharmony_ci return twoImageLength; 22480922886Sopenharmony_ci} 22580922886Sopenharmony_ci 22680922886Sopenharmony_ciint32_t AVSessionProxy::SetAVMetaData(const AVMetaData& meta) 22780922886Sopenharmony_ci{ 22880922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVMetaData"); 22980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(meta.IsValid(), ERR_INVALID_PARAM, "invalid meta data"); 23080922886Sopenharmony_ci 23180922886Sopenharmony_ci std::lock_guard lockGuard(setMetadataLock_); 23280922886Sopenharmony_ci SLOGI("SetAVMetaData in proxy"); 23380922886Sopenharmony_ci 23480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 23580922886Sopenharmony_ci MessageParcel data; 23680922886Sopenharmony_ci data.SetMaxCapacity(defaultIpcCapacity); 23780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 23880922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 23980922886Sopenharmony_ci MessageParcel reply; 24080922886Sopenharmony_ci MessageOption option; 24180922886Sopenharmony_ci auto remote = Remote(); 24280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 24380922886Sopenharmony_ci 24480922886Sopenharmony_ci AVMetaData metaData; 24580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(metaData.CopyFrom(meta), AVSESSION_ERROR, "avmetadata CopyFrom error"); 24680922886Sopenharmony_ci int twoImageLength = GetPixelMapBuffer(metaData, data); 24780922886Sopenharmony_ci if (twoImageLength == 0) { 24880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInt32(twoImageLength), ERR_MARSHALLING, "write twoImageLength failed"); 24980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&meta), ERR_MARSHALLING, "write AVMetaData failed"); 25080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) == 0, 25180922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 25280922886Sopenharmony_ci 25380922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 25480922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 25580922886Sopenharmony_ci } 25680922886Sopenharmony_ci 25780922886Sopenharmony_ci if (twoImageLength == -1) { 25880922886Sopenharmony_ci SLOGE("fail to write parcel"); 25980922886Sopenharmony_ci return AVSESSION_ERROR; 26080922886Sopenharmony_ci } 26180922886Sopenharmony_ci 26280922886Sopenharmony_ci if (remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) != 0) { 26380922886Sopenharmony_ci SLOGE("send request fail with raw img"); 26480922886Sopenharmony_ci return ERR_IPC_SEND_REQUEST; 26580922886Sopenharmony_ci } 26680922886Sopenharmony_ci SLOGI("set avmetadata done"); 26780922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 26880922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 26980922886Sopenharmony_ci} 27080922886Sopenharmony_ci 27180922886Sopenharmony_ciint32_t AVSessionProxy::GetAVMetaData(AVMetaData& meta) 27280922886Sopenharmony_ci{ 27380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 27480922886Sopenharmony_ci MessageParcel data; 27580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 27680922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 27780922886Sopenharmony_ci MessageParcel reply; 27880922886Sopenharmony_ci MessageOption option; 27980922886Sopenharmony_ci reply.SetMaxCapacity(defaultIpcCapacity); 28080922886Sopenharmony_ci auto remote = Remote(); 28180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 28280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_META_DATA, data, reply, option) == 0, 28380922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 28480922886Sopenharmony_ci 28580922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 28680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed"); 28780922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 28880922886Sopenharmony_ci std::shared_ptr<AVMetaData> metaData(reply.ReadParcelable<AVMetaData>()); 28980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(metaData != nullptr, ERR_UNMARSHALLING, "read metaData failed"); 29080922886Sopenharmony_ci meta = *metaData; 29180922886Sopenharmony_ci } 29280922886Sopenharmony_ci return ret; 29380922886Sopenharmony_ci} 29480922886Sopenharmony_ci 29580922886Sopenharmony_ciint32_t AVSessionProxy::SetAVQueueItems(const std::vector<AVQueueItem>& items) 29680922886Sopenharmony_ci{ 29780922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueItems"); 29880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 29980922886Sopenharmony_ci MessageParcel data; 30080922886Sopenharmony_ci data.SetMaxCapacity(defaultIpcCapacity); 30180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 30280922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 30380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), ERR_MARSHALLING, "write items num int32 failed"); 30480922886Sopenharmony_ci for (auto &parcelable : items) { 30580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), ERR_MARSHALLING, "Write items failed"); 30680922886Sopenharmony_ci } 30780922886Sopenharmony_ci MessageParcel reply; 30880922886Sopenharmony_ci MessageOption option; 30980922886Sopenharmony_ci auto remote = Remote(); 31080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 31180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_ITEMS, data, reply, option) == 0, 31280922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 31380922886Sopenharmony_ci 31480922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 31580922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 31680922886Sopenharmony_ci} 31780922886Sopenharmony_ci 31880922886Sopenharmony_ciint32_t AVSessionProxy::GetAVQueueItems(std::vector<AVQueueItem>& items) 31980922886Sopenharmony_ci{ 32080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 32180922886Sopenharmony_ci MessageParcel data; 32280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 32380922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 32480922886Sopenharmony_ci MessageParcel reply; 32580922886Sopenharmony_ci MessageOption option; 32680922886Sopenharmony_ci auto remote = Remote(); 32780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 32880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_ITEMS, data, reply, option) == 0, 32980922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 33080922886Sopenharmony_ci 33180922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 33280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed"); 33380922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 33480922886Sopenharmony_ci std::vector<AVQueueItem> items_; 33580922886Sopenharmony_ci int32_t itemNum = reply.ReadInt32(); 33680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(itemNum >= 0, ERR_UNMARSHALLING, "read int32 itemNum failed"); 33780922886Sopenharmony_ci for (int32_t i = 0; i < itemNum; i++) { 33880922886Sopenharmony_ci AVQueueItem *item = reply.ReadParcelable<AVQueueItem>(); 33980922886Sopenharmony_ci if (item == nullptr) { 34080922886Sopenharmony_ci SLOGE("GetAVQueueItems: read parcelable AVQueueItem failed"); 34180922886Sopenharmony_ci delete item; 34280922886Sopenharmony_ci item = nullptr; 34380922886Sopenharmony_ci return ERR_UNMARSHALLING; 34480922886Sopenharmony_ci } 34580922886Sopenharmony_ci items_.emplace_back(*item); 34680922886Sopenharmony_ci delete item; 34780922886Sopenharmony_ci item = nullptr; 34880922886Sopenharmony_ci } 34980922886Sopenharmony_ci items = items_; 35080922886Sopenharmony_ci } 35180922886Sopenharmony_ci return ret; 35280922886Sopenharmony_ci} 35380922886Sopenharmony_ci 35480922886Sopenharmony_ciint32_t AVSessionProxy::GetAVQueueTitle(std::string& title) 35580922886Sopenharmony_ci{ 35680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 35780922886Sopenharmony_ci MessageParcel data; 35880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 35980922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 36080922886Sopenharmony_ci MessageParcel reply; 36180922886Sopenharmony_ci MessageOption option; 36280922886Sopenharmony_ci auto remote = Remote(); 36380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 36480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_TITLE, data, reply, option) == 0, 36580922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 36680922886Sopenharmony_ci 36780922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 36880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed"); 36980922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 37080922886Sopenharmony_ci std::string title_; 37180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadString(title), ERR_UNMARSHALLING, "read title string failed"); 37280922886Sopenharmony_ci title = title_; 37380922886Sopenharmony_ci } 37480922886Sopenharmony_ci return ret; 37580922886Sopenharmony_ci} 37680922886Sopenharmony_ci 37780922886Sopenharmony_ciint32_t AVSessionProxy::SetAVQueueTitle(const std::string& title) 37880922886Sopenharmony_ci{ 37980922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueTitle"); 38080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 38180922886Sopenharmony_ci MessageParcel data; 38280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 38380922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 38480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteString(title), 38580922886Sopenharmony_ci ERR_MARSHALLING, "Write state failed"); 38680922886Sopenharmony_ci MessageParcel reply; 38780922886Sopenharmony_ci MessageOption option; 38880922886Sopenharmony_ci auto remote = Remote(); 38980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 39080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_TITLE, data, reply, option) == 0, 39180922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 39280922886Sopenharmony_ci 39380922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 39480922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 39580922886Sopenharmony_ci} 39680922886Sopenharmony_ci 39780922886Sopenharmony_ciint32_t AVSessionProxy::GetAVPlaybackState(AVPlaybackState& state) 39880922886Sopenharmony_ci{ 39980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 40080922886Sopenharmony_ci MessageParcel data; 40180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 40280922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 40380922886Sopenharmony_ci MessageParcel reply; 40480922886Sopenharmony_ci MessageOption option; 40580922886Sopenharmony_ci auto remote = Remote(); 40680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 40780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_PLAYBACK_STATE, data, reply, option) == 0, 40880922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 40980922886Sopenharmony_ci 41080922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 41180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed"); 41280922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 41380922886Sopenharmony_ci std::shared_ptr<AVPlaybackState> playbackState(reply.ReadParcelable<AVPlaybackState>()); 41480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(playbackState != nullptr, ERR_UNMARSHALLING, "read playbackState failed"); 41580922886Sopenharmony_ci state = *playbackState; 41680922886Sopenharmony_ci } 41780922886Sopenharmony_ci return ret; 41880922886Sopenharmony_ci} 41980922886Sopenharmony_ci 42080922886Sopenharmony_ciint32_t AVSessionProxy::SetAVPlaybackState(const AVPlaybackState& state) 42180922886Sopenharmony_ci{ 42280922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVPlaybackState"); 42380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(state.IsValid(), ERR_INVALID_PARAM, "state not valid"); 42480922886Sopenharmony_ci std::lock_guard lockGuard(setPlaybackLock_); 42580922886Sopenharmony_ci SLOGI("SetAVPlaybackState in proxy for state %{public}d", state.GetState()); 42680922886Sopenharmony_ci 42780922886Sopenharmony_ci std::lock_guard isDestroyedLockGuard(isDestroyedLock_); 42880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 42980922886Sopenharmony_ci MessageParcel data; 43080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 43180922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 43280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&state), 43380922886Sopenharmony_ci ERR_MARSHALLING, "Write state failed"); 43480922886Sopenharmony_ci MessageParcel reply; 43580922886Sopenharmony_ci MessageOption option; 43680922886Sopenharmony_ci auto remote = Remote(); 43780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 43880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_PLAYBACK_STATE, data, reply, option) == 0, 43980922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 44080922886Sopenharmony_ci 44180922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 44280922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 44380922886Sopenharmony_ci} 44480922886Sopenharmony_ci 44580922886Sopenharmony_ciint32_t AVSessionProxy::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability) 44680922886Sopenharmony_ci{ 44780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 44880922886Sopenharmony_ci MessageParcel data; 44980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 45080922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 45180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&ability), 45280922886Sopenharmony_ci ERR_MARSHALLING, "Write WantAgent failed"); 45380922886Sopenharmony_ci MessageParcel reply; 45480922886Sopenharmony_ci MessageOption option; 45580922886Sopenharmony_ci auto remote = Remote(); 45680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 45780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_LAUNCH_ABILITY, data, reply, option) == 0, 45880922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 45980922886Sopenharmony_ci 46080922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 46180922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 46280922886Sopenharmony_ci} 46380922886Sopenharmony_ci 46480922886Sopenharmony_ciint32_t AVSessionProxy::GetExtras(AAFwk::WantParams& extras) 46580922886Sopenharmony_ci{ 46680922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::GetExtras"); 46780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 46880922886Sopenharmony_ci MessageParcel data; 46980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 47080922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 47180922886Sopenharmony_ci MessageParcel reply; 47280922886Sopenharmony_ci MessageOption option; 47380922886Sopenharmony_ci auto remote = Remote(); 47480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 47580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_EXTRAS, data, reply, option) == 0, 47680922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 47780922886Sopenharmony_ci 47880922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 47980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed"); 48080922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 48180922886Sopenharmony_ci std::shared_ptr<AAFwk::WantParams> extrasData(reply.ReadParcelable<AAFwk::WantParams>()); 48280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(extrasData != nullptr, ERR_UNMARSHALLING, "read metaData failed"); 48380922886Sopenharmony_ci extras = *extrasData; 48480922886Sopenharmony_ci } 48580922886Sopenharmony_ci return ret; 48680922886Sopenharmony_ci} 48780922886Sopenharmony_ci 48880922886Sopenharmony_ciint32_t AVSessionProxy::SetExtras(const AAFwk::WantParams& extras) 48980922886Sopenharmony_ci{ 49080922886Sopenharmony_ci AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetExtras"); 49180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 49280922886Sopenharmony_ci MessageParcel data; 49380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 49480922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 49580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras), 49680922886Sopenharmony_ci ERR_MARSHALLING, "Write extras failed"); 49780922886Sopenharmony_ci MessageParcel reply; 49880922886Sopenharmony_ci MessageOption option; 49980922886Sopenharmony_ci auto remote = Remote(); 50080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 50180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_EXTRAS, data, reply, option) == 0, 50280922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 50380922886Sopenharmony_ci 50480922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 50580922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 50680922886Sopenharmony_ci} 50780922886Sopenharmony_ci 50880922886Sopenharmony_cisptr<IRemoteObject> AVSessionProxy::GetControllerInner() 50980922886Sopenharmony_ci{ 51080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed"); 51180922886Sopenharmony_ci MessageParcel data; 51280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 51380922886Sopenharmony_ci nullptr, "write interface token failed"); 51480922886Sopenharmony_ci MessageParcel reply; 51580922886Sopenharmony_ci MessageOption option; 51680922886Sopenharmony_ci auto remote = Remote(); 51780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed"); 51880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_CONTROLLER, data, reply, option) == 0, 51980922886Sopenharmony_ci nullptr, "send request failed"); 52080922886Sopenharmony_ci 52180922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 52280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed"); 52380922886Sopenharmony_ci sptr <IRemoteObject> controller = nullptr; 52480922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 52580922886Sopenharmony_ci controller = reply.ReadRemoteObject(); 52680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "read IRemoteObject failed"); 52780922886Sopenharmony_ci } 52880922886Sopenharmony_ci return controller; 52980922886Sopenharmony_ci} 53080922886Sopenharmony_ci 53180922886Sopenharmony_cistd::shared_ptr<AVSessionController> AVSessionProxy::GetController() 53280922886Sopenharmony_ci{ 53380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed"); 53480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(controller_ == nullptr || controller_->IsDestroy(), controller_, 53580922886Sopenharmony_ci "controller already exist"); 53680922886Sopenharmony_ci sptr <IRemoteObject> object = GetControllerInner(); 53780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed"); 53880922886Sopenharmony_ci auto controller = iface_cast<AVSessionControllerProxy>(object); 53980922886Sopenharmony_ci controller_ = std::shared_ptr<AVSessionController>(controller.GetRefPtr(), [holder = controller](const auto*) {}); 54080922886Sopenharmony_ci return controller_; 54180922886Sopenharmony_ci} 54280922886Sopenharmony_ci 54380922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE 54480922886Sopenharmony_cisptr<IRemoteObject> AVSessionProxy::GetAVCastControllerInner() 54580922886Sopenharmony_ci{ 54680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed"); 54780922886Sopenharmony_ci MessageParcel data; 54880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 54980922886Sopenharmony_ci nullptr, "write interface token failed"); 55080922886Sopenharmony_ci MessageParcel reply; 55180922886Sopenharmony_ci MessageOption option; 55280922886Sopenharmony_ci auto remote = Remote(); 55380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed"); 55480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_AVCAST_CONTROLLER, data, reply, option) == 0, 55580922886Sopenharmony_ci nullptr, "send request failed"); 55680922886Sopenharmony_ci 55780922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 55880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed"); 55980922886Sopenharmony_ci sptr <IRemoteObject> castController = nullptr; 56080922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 56180922886Sopenharmony_ci castController = reply.ReadRemoteObject(); 56280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(castController != nullptr, nullptr, "read IRemoteObject failed"); 56380922886Sopenharmony_ci } 56480922886Sopenharmony_ci return castController; 56580922886Sopenharmony_ci} 56680922886Sopenharmony_ci 56780922886Sopenharmony_cistd::shared_ptr<AVCastController> AVSessionProxy::GetAVCastController() 56880922886Sopenharmony_ci{ 56980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed"); 57080922886Sopenharmony_ci sptr <IRemoteObject> object = GetAVCastControllerInner(); 57180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed"); 57280922886Sopenharmony_ci auto castController = iface_cast<AVCastControllerProxy>(object); 57380922886Sopenharmony_ci castController_ = std::shared_ptr<AVCastController>(castController.GetRefPtr(), 57480922886Sopenharmony_ci [holder = castController](const auto*) {}); 57580922886Sopenharmony_ci return castController_; 57680922886Sopenharmony_ci} 57780922886Sopenharmony_ci 57880922886Sopenharmony_ciint32_t AVSessionProxy::StartCastDisplayListener() 57980922886Sopenharmony_ci{ 58080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 58180922886Sopenharmony_ci MessageParcel data; 58280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 58380922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 58480922886Sopenharmony_ci MessageParcel reply; 58580922886Sopenharmony_ci MessageOption option; 58680922886Sopenharmony_ci auto remote = Remote(); 58780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 58880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_START_CAST_DISPLAY_LISTENER, data, reply, option) == 0, 58980922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 59080922886Sopenharmony_ci 59180922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 59280922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 59380922886Sopenharmony_ci} 59480922886Sopenharmony_ci 59580922886Sopenharmony_ciint32_t AVSessionProxy::StopCastDisplayListener() 59680922886Sopenharmony_ci{ 59780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 59880922886Sopenharmony_ci MessageParcel data; 59980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 60080922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 60180922886Sopenharmony_ci MessageParcel reply; 60280922886Sopenharmony_ci MessageOption option; 60380922886Sopenharmony_ci auto remote = Remote(); 60480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 60580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_STOP_CAST_DISPLAY_LISTENER, data, reply, option) == 0, 60680922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 60780922886Sopenharmony_ci 60880922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 60980922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 61080922886Sopenharmony_ci} 61180922886Sopenharmony_ci 61280922886Sopenharmony_ciint32_t AVSessionProxy::GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays) 61380922886Sopenharmony_ci{ 61480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 61580922886Sopenharmony_ci MessageParcel data; 61680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 61780922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 61880922886Sopenharmony_ci MessageParcel reply; 61980922886Sopenharmony_ci MessageOption option; 62080922886Sopenharmony_ci auto remote = Remote(); 62180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 62280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_ALL_CAST_DISPLAYS, data, reply, option) == 0, 62380922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 62480922886Sopenharmony_ci 62580922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 62680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_MARSHALLING, "read int32 failed"); 62780922886Sopenharmony_ci if (ret == AVSESSION_SUCCESS) { 62880922886Sopenharmony_ci int32_t castDisplayNum = 0; 62980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(castDisplayNum), ERR_MARSHALLING, "read castDisplayNum failed"); 63080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(castDisplayNum > 0, ERR_MARSHALLING, "castDisplayNum is illegal"); 63180922886Sopenharmony_ci std::vector<CastDisplayInfo> displays; 63280922886Sopenharmony_ci for (int32_t i = 0; i < castDisplayNum; i++) { 63380922886Sopenharmony_ci CastDisplayInfo castDisplayInfo; 63480922886Sopenharmony_ci int32_t displayState = -1; 63580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(displayState), ERR_MARSHALLING, "read displayState failed"); 63680922886Sopenharmony_ci castDisplayInfo.displayState = static_cast<CastDisplayState>(displayState); 63780922886Sopenharmony_ci uint64_t displayId = 0; 63880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadUint64(displayId), ERR_MARSHALLING, "read displayId failed"); 63980922886Sopenharmony_ci castDisplayInfo.displayId = displayId; 64080922886Sopenharmony_ci std::string name = ""; 64180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadString(name), ERR_MARSHALLING, "read name failed"); 64280922886Sopenharmony_ci castDisplayInfo.name = name; 64380922886Sopenharmony_ci int32_t width = -1; 64480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(width), ERR_MARSHALLING, "read width failed"); 64580922886Sopenharmony_ci castDisplayInfo.width = width; 64680922886Sopenharmony_ci int32_t height = -1; 64780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(height), ERR_MARSHALLING, "read height failed"); 64880922886Sopenharmony_ci castDisplayInfo.height = height; 64980922886Sopenharmony_ci displays.push_back(castDisplayInfo); 65080922886Sopenharmony_ci } 65180922886Sopenharmony_ci castDisplays = displays; 65280922886Sopenharmony_ci } 65380922886Sopenharmony_ci return ret; 65480922886Sopenharmony_ci} 65580922886Sopenharmony_ci#endif 65680922886Sopenharmony_ci 65780922886Sopenharmony_ciint32_t AVSessionProxy::Activate() 65880922886Sopenharmony_ci{ 65980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 66080922886Sopenharmony_ci MessageParcel data; 66180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 66280922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 66380922886Sopenharmony_ci MessageParcel reply; 66480922886Sopenharmony_ci MessageOption option; 66580922886Sopenharmony_ci auto remote = Remote(); 66680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 66780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ACTIVATE, data, reply, option) == 0, 66880922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 66980922886Sopenharmony_ci 67080922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 67180922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 67280922886Sopenharmony_ci} 67380922886Sopenharmony_ci 67480922886Sopenharmony_ciint32_t AVSessionProxy::Deactivate() 67580922886Sopenharmony_ci{ 67680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 67780922886Sopenharmony_ci MessageParcel data; 67880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 67980922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 68080922886Sopenharmony_ci MessageParcel reply; 68180922886Sopenharmony_ci MessageOption option; 68280922886Sopenharmony_ci auto remote = Remote(); 68380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 68480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DEACTIVATE, data, reply, option) == 0, 68580922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 68680922886Sopenharmony_ci 68780922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 68880922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 68980922886Sopenharmony_ci} 69080922886Sopenharmony_ci 69180922886Sopenharmony_cibool AVSessionProxy::IsActive() 69280922886Sopenharmony_ci{ 69380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 69480922886Sopenharmony_ci MessageParcel data; 69580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 69680922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 69780922886Sopenharmony_ci MessageParcel reply; 69880922886Sopenharmony_ci MessageOption option; 69980922886Sopenharmony_ci auto remote = Remote(); 70080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 70180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ISACTIVE, data, reply, option) == 0, 70280922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 70380922886Sopenharmony_ci 70480922886Sopenharmony_ci bool ret = false; 70580922886Sopenharmony_ci return reply.ReadBool(ret) ? ret : false; 70680922886Sopenharmony_ci} 70780922886Sopenharmony_ci 70880922886Sopenharmony_ciint32_t AVSessionProxy::AddSupportCommand(const int32_t cmd) 70980922886Sopenharmony_ci{ 71080922886Sopenharmony_ci std::lock_guard lockGuard(setCommandLock_); 71180922886Sopenharmony_ci SLOGI("add support command for %{public}d", cmd); 71280922886Sopenharmony_ci 71380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 71480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd"); 71580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd"); 71680922886Sopenharmony_ci MessageParcel data; 71780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 71880922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 71980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd), 72080922886Sopenharmony_ci ERR_MARSHALLING, "Write cmd failed"); 72180922886Sopenharmony_ci MessageParcel reply; 72280922886Sopenharmony_ci MessageOption option; 72380922886Sopenharmony_ci auto remote = Remote(); 72480922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 72580922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ADD_SUPPORT_COMMAND, data, reply, option) == 0, 72680922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 72780922886Sopenharmony_ci 72880922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 72980922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 73080922886Sopenharmony_ci} 73180922886Sopenharmony_ci 73280922886Sopenharmony_ciint32_t AVSessionProxy::DeleteSupportCommand(const int32_t cmd) 73380922886Sopenharmony_ci{ 73480922886Sopenharmony_ci std::lock_guard lockGuard(setCommandLock_); 73580922886Sopenharmony_ci SLOGI("del support command for %{public}d", cmd); 73680922886Sopenharmony_ci 73780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 73880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd"); 73980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd"); 74080922886Sopenharmony_ci MessageParcel data; 74180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 74280922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 74380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd), 74480922886Sopenharmony_ci ERR_MARSHALLING, "Write cmd failed"); 74580922886Sopenharmony_ci MessageParcel reply; 74680922886Sopenharmony_ci MessageOption option; 74780922886Sopenharmony_ci auto remote = Remote(); 74880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 74980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DELETE_SUPPORT_COMMAND, data, reply, option) == 0,\ 75080922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 75180922886Sopenharmony_ci 75280922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 75380922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 75480922886Sopenharmony_ci} 75580922886Sopenharmony_ci 75680922886Sopenharmony_ciint32_t AVSessionProxy::SetSessionEvent(const std::string& event, const AAFwk::WantParams& args) 75780922886Sopenharmony_ci{ 75880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 75980922886Sopenharmony_ci MessageParcel data; 76080922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 76180922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 76280922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed"); 76380922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args), 76480922886Sopenharmony_ci ERR_MARSHALLING, "Write Want failed"); 76580922886Sopenharmony_ci MessageParcel reply; 76680922886Sopenharmony_ci MessageOption option; 76780922886Sopenharmony_ci auto remote = Remote(); 76880922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 76980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_SESSION_EVENT, data, reply, option) == 0, 77080922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 77180922886Sopenharmony_ci 77280922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 77380922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 77480922886Sopenharmony_ci} 77580922886Sopenharmony_ci 77680922886Sopenharmony_ci#ifdef CASTPLUS_CAST_ENGINE_ENABLE 77780922886Sopenharmony_ciint32_t AVSessionProxy::ReleaseCast() 77880922886Sopenharmony_ci{ 77980922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed"); 78080922886Sopenharmony_ci MessageParcel data; 78180922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), 78280922886Sopenharmony_ci ERR_MARSHALLING, "write interface token failed"); 78380922886Sopenharmony_ci MessageParcel reply; 78480922886Sopenharmony_ci MessageOption option; 78580922886Sopenharmony_ci auto remote = Remote(); 78680922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed"); 78780922886Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_RELEASE_CAST, data, reply, option) == 0, 78880922886Sopenharmony_ci ERR_IPC_SEND_REQUEST, "send request failed"); 78980922886Sopenharmony_ci 79080922886Sopenharmony_ci int32_t ret = AVSESSION_ERROR; 79180922886Sopenharmony_ci return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR; 79280922886Sopenharmony_ci} 79380922886Sopenharmony_ci#endif 79480922886Sopenharmony_ci} // namespace OHOS::AVSession 795