12e147c35Sopenharmony_ci/*
22e147c35Sopenharmony_ci * Copyright (C) 2023-2023 Huawei Device Co., Ltd.
32e147c35Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42e147c35Sopenharmony_ci * you may not use this file except in compliance with the License.
52e147c35Sopenharmony_ci * You may obtain a copy of the License at
62e147c35Sopenharmony_ci *
72e147c35Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82e147c35Sopenharmony_ci *
92e147c35Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102e147c35Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112e147c35Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122e147c35Sopenharmony_ci * See the License for the specific language governing permissions and
132e147c35Sopenharmony_ci * limitations under the License.
142e147c35Sopenharmony_ci * Description: Cast Session function realization.
152e147c35Sopenharmony_ci * Author: zhangge
162e147c35Sopenharmony_ci * Create: 2022-06-15
172e147c35Sopenharmony_ci */
182e147c35Sopenharmony_ci
192e147c35Sopenharmony_ci#include "cast_session.h"
202e147c35Sopenharmony_ci
212e147c35Sopenharmony_ci#include "cast_engine_errors.h"
222e147c35Sopenharmony_ci#include "cast_engine_log.h"
232e147c35Sopenharmony_ci#include "cast_session_listener_impl_stub.h"
242e147c35Sopenharmony_ci#include "surface_utils.h"
252e147c35Sopenharmony_ci#include "stream_player.h"
262e147c35Sopenharmony_ci#include "mirror_player.h"
272e147c35Sopenharmony_ci
282e147c35Sopenharmony_cinamespace OHOS {
292e147c35Sopenharmony_cinamespace CastEngine {
302e147c35Sopenharmony_cinamespace CastEngineClient {
312e147c35Sopenharmony_ciDEFINE_CAST_ENGINE_LABEL("Cast-Client-Session");
322e147c35Sopenharmony_ci
332e147c35Sopenharmony_ciCastSession::~CastSession()
342e147c35Sopenharmony_ci{
352e147c35Sopenharmony_ci    CLOGI("Stop the client cast session.");
362e147c35Sopenharmony_ci}
372e147c35Sopenharmony_ci
382e147c35Sopenharmony_ciint32_t CastSession::RegisterListener(std::shared_ptr<ICastSessionListener> listener)
392e147c35Sopenharmony_ci{
402e147c35Sopenharmony_ci    if (listener == nullptr) {
412e147c35Sopenharmony_ci        CLOGE("The listener is null");
422e147c35Sopenharmony_ci        return ERR_INVALID_PARAM;
432e147c35Sopenharmony_ci    }
442e147c35Sopenharmony_ci    sptr<ICastSessionListenerImpl> listenerStub = new (std::nothrow) CastSessionListenerImplStub(listener);
452e147c35Sopenharmony_ci    if (listenerStub == nullptr) {
462e147c35Sopenharmony_ci        CLOGE("Failed to new a session listener");
472e147c35Sopenharmony_ci        return ERR_NO_MEMORY;
482e147c35Sopenharmony_ci    }
492e147c35Sopenharmony_ci
502e147c35Sopenharmony_ci    return proxy_ ? proxy_->RegisterListener(listenerStub) : CAST_ENGINE_ERROR;
512e147c35Sopenharmony_ci}
522e147c35Sopenharmony_ci
532e147c35Sopenharmony_ciint32_t CastSession::SetSessionId(std::string sessionId)
542e147c35Sopenharmony_ci{
552e147c35Sopenharmony_ci    sessionId_ = sessionId;
562e147c35Sopenharmony_ci    return CAST_ENGINE_SUCCESS;
572e147c35Sopenharmony_ci}
582e147c35Sopenharmony_ci
592e147c35Sopenharmony_ciint32_t CastSession::UnregisterListener()
602e147c35Sopenharmony_ci{
612e147c35Sopenharmony_ci    return proxy_ ? proxy_->UnregisterListener() : CAST_ENGINE_ERROR;
622e147c35Sopenharmony_ci}
632e147c35Sopenharmony_ci
642e147c35Sopenharmony_ciint32_t CastSession::AddDevice(const CastRemoteDevice &remoteDevice)
652e147c35Sopenharmony_ci{
662e147c35Sopenharmony_ci    if (remoteDevice.deviceId.empty()) {
672e147c35Sopenharmony_ci        CLOGE("The remote device id is null");
682e147c35Sopenharmony_ci        return ERR_INVALID_PARAM;
692e147c35Sopenharmony_ci    }
702e147c35Sopenharmony_ci    return proxy_ ? proxy_->AddDevice(remoteDevice) : CAST_ENGINE_ERROR;
712e147c35Sopenharmony_ci}
722e147c35Sopenharmony_ci
732e147c35Sopenharmony_ciint32_t CastSession::RemoveDevice(const std::string &deviceId)
742e147c35Sopenharmony_ci{
752e147c35Sopenharmony_ci    if (deviceId.empty()) {
762e147c35Sopenharmony_ci        CLOGE("The device id is null");
772e147c35Sopenharmony_ci        return ERR_INVALID_PARAM;
782e147c35Sopenharmony_ci    }
792e147c35Sopenharmony_ci    return proxy_ ? proxy_->RemoveDevice(deviceId) : CAST_ENGINE_ERROR;
802e147c35Sopenharmony_ci}
812e147c35Sopenharmony_ci
822e147c35Sopenharmony_ciint32_t CastSession::StartAuth(const AuthInfo &authInfo)
832e147c35Sopenharmony_ci{
842e147c35Sopenharmony_ci    if (authInfo.deviceId.empty()) {
852e147c35Sopenharmony_ci        CLOGE("The device id is null");
862e147c35Sopenharmony_ci        return ERR_INVALID_PARAM;
872e147c35Sopenharmony_ci    }
882e147c35Sopenharmony_ci    return proxy_ ? proxy_->StartAuth(authInfo) : CAST_ENGINE_ERROR;
892e147c35Sopenharmony_ci}
902e147c35Sopenharmony_ci
912e147c35Sopenharmony_ciint32_t CastSession::GetSessionId(std::string &sessionId)
922e147c35Sopenharmony_ci{
932e147c35Sopenharmony_ci    if (!proxy_) {
942e147c35Sopenharmony_ci        CLOGE("proxy is null");
952e147c35Sopenharmony_ci        return CAST_ENGINE_ERROR;
962e147c35Sopenharmony_ci    }
972e147c35Sopenharmony_ci    int32_t ret = proxy_->GetSessionId(sessionId_);
982e147c35Sopenharmony_ci    sessionId = sessionId_;
992e147c35Sopenharmony_ci    return ret;
1002e147c35Sopenharmony_ci}
1012e147c35Sopenharmony_ci
1022e147c35Sopenharmony_ciint32_t CastSession::SetSessionProperty(const CastSessionProperty &property)
1032e147c35Sopenharmony_ci{
1042e147c35Sopenharmony_ci    return proxy_ ? proxy_->SetSessionProperty(property) : CAST_ENGINE_ERROR;
1052e147c35Sopenharmony_ci}
1062e147c35Sopenharmony_ci
1072e147c35Sopenharmony_ciint32_t CastSession::CreateMirrorPlayer(std::shared_ptr<IMirrorPlayer> &mirrorPlayer)
1082e147c35Sopenharmony_ci{
1092e147c35Sopenharmony_ci    if (!proxy_) {
1102e147c35Sopenharmony_ci        CLOGE("proxy_ is null");
1112e147c35Sopenharmony_ci        return CAST_ENGINE_ERROR;
1122e147c35Sopenharmony_ci    }
1132e147c35Sopenharmony_ci    sptr<IMirrorPlayerImpl> impl;
1142e147c35Sopenharmony_ci    int32_t ret = proxy_->CreateMirrorPlayer(impl);
1152e147c35Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
1162e147c35Sopenharmony_ci    if (!impl) {
1172e147c35Sopenharmony_ci        return CAST_ENGINE_ERROR;
1182e147c35Sopenharmony_ci    }
1192e147c35Sopenharmony_ci
1202e147c35Sopenharmony_ci    auto player = std::make_shared<MirrorPlayer>(impl);
1212e147c35Sopenharmony_ci    if (!player) {
1222e147c35Sopenharmony_ci        CLOGE("Failed to malloc mirror player");
1232e147c35Sopenharmony_ci        return ERR_NO_MEMORY;
1242e147c35Sopenharmony_ci    }
1252e147c35Sopenharmony_ci    mirrorPlayer = player;
1262e147c35Sopenharmony_ci    return ret;
1272e147c35Sopenharmony_ci}
1282e147c35Sopenharmony_ci
1292e147c35Sopenharmony_ciint32_t CastSession::CreateStreamPlayer(std::shared_ptr<IStreamPlayer> &streamPlayer)
1302e147c35Sopenharmony_ci{
1312e147c35Sopenharmony_ci    if (!proxy_) {
1322e147c35Sopenharmony_ci        CLOGE("proxy_ is null");
1332e147c35Sopenharmony_ci        return CAST_ENGINE_ERROR;
1342e147c35Sopenharmony_ci    }
1352e147c35Sopenharmony_ci    sptr<IStreamPlayerIpc> streamPlayerIpc;
1362e147c35Sopenharmony_ci    int32_t ret = proxy_->CreateStreamPlayer(streamPlayerIpc);
1372e147c35Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
1382e147c35Sopenharmony_ci    if (!streamPlayerIpc) {
1392e147c35Sopenharmony_ci        return CAST_ENGINE_ERROR;
1402e147c35Sopenharmony_ci    }
1412e147c35Sopenharmony_ci
1422e147c35Sopenharmony_ci    auto player = std::make_shared<StreamPlayer>(streamPlayerIpc);
1432e147c35Sopenharmony_ci    if (!player) {
1442e147c35Sopenharmony_ci        CLOGE("Failed to malloc stream player");
1452e147c35Sopenharmony_ci        return ERR_NO_MEMORY;
1462e147c35Sopenharmony_ci    }
1472e147c35Sopenharmony_ci    streamPlayer = player;
1482e147c35Sopenharmony_ci    return ret;
1492e147c35Sopenharmony_ci}
1502e147c35Sopenharmony_ci
1512e147c35Sopenharmony_ciint32_t CastSession::Release()
1522e147c35Sopenharmony_ci{
1532e147c35Sopenharmony_ci    return proxy_ ? proxy_->Release() : CAST_ENGINE_ERROR;
1542e147c35Sopenharmony_ci}
1552e147c35Sopenharmony_ci
1562e147c35Sopenharmony_ciint32_t CastSession::SetCastMode(CastMode mode, std::string &jsonParam)
1572e147c35Sopenharmony_ci{
1582e147c35Sopenharmony_ci    return proxy_ ? proxy_->SetCastMode(mode, jsonParam) : false;
1592e147c35Sopenharmony_ci}
1602e147c35Sopenharmony_ci
1612e147c35Sopenharmony_ciint32_t CastSession::NotifyEvent(EventId eventId, std::string &jsonParam)
1622e147c35Sopenharmony_ci{
1632e147c35Sopenharmony_ci    if (proxy_ != nullptr) {
1642e147c35Sopenharmony_ci        proxy_->NotifyEvent(eventId, jsonParam);
1652e147c35Sopenharmony_ci        return CAST_ENGINE_SUCCESS;
1662e147c35Sopenharmony_ci    }
1672e147c35Sopenharmony_ci    return CAST_ENGINE_ERROR;
1682e147c35Sopenharmony_ci}
1692e147c35Sopenharmony_ci} // namespace CastEngineClient
1702e147c35Sopenharmony_ci} // namespace CastEngine
1712e147c35Sopenharmony_ci} // namespace OHOS