1 /*
2 * Copyright (C) 2023-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: Cast Session function realization.
15 * Author: zhangge
16 * Create: 2022-06-15
17 */
18
19 #include "cast_session.h"
20
21 #include "cast_engine_errors.h"
22 #include "cast_engine_log.h"
23 #include "cast_session_listener_impl_stub.h"
24 #include "surface_utils.h"
25 #include "stream_player.h"
26 #include "mirror_player.h"
27
28 namespace OHOS {
29 namespace CastEngine {
30 namespace CastEngineClient {
31 DEFINE_CAST_ENGINE_LABEL("Cast-Client-Session");
32
~CastSession()33 CastSession::~CastSession()
34 {
35 CLOGI("Stop the client cast session.");
36 }
37
RegisterListener(std::shared_ptr<ICastSessionListener> listener)38 int32_t CastSession::RegisterListener(std::shared_ptr<ICastSessionListener> listener)
39 {
40 if (listener == nullptr) {
41 CLOGE("The listener is null");
42 return ERR_INVALID_PARAM;
43 }
44 sptr<ICastSessionListenerImpl> listenerStub = new (std::nothrow) CastSessionListenerImplStub(listener);
45 if (listenerStub == nullptr) {
46 CLOGE("Failed to new a session listener");
47 return ERR_NO_MEMORY;
48 }
49
50 return proxy_ ? proxy_->RegisterListener(listenerStub) : CAST_ENGINE_ERROR;
51 }
52
SetSessionId(std::string sessionId)53 int32_t CastSession::SetSessionId(std::string sessionId)
54 {
55 sessionId_ = sessionId;
56 return CAST_ENGINE_SUCCESS;
57 }
58
UnregisterListener()59 int32_t CastSession::UnregisterListener()
60 {
61 return proxy_ ? proxy_->UnregisterListener() : CAST_ENGINE_ERROR;
62 }
63
AddDevice(const CastRemoteDevice & remoteDevice)64 int32_t CastSession::AddDevice(const CastRemoteDevice &remoteDevice)
65 {
66 if (remoteDevice.deviceId.empty()) {
67 CLOGE("The remote device id is null");
68 return ERR_INVALID_PARAM;
69 }
70 return proxy_ ? proxy_->AddDevice(remoteDevice) : CAST_ENGINE_ERROR;
71 }
72
RemoveDevice(const std::string & deviceId)73 int32_t CastSession::RemoveDevice(const std::string &deviceId)
74 {
75 if (deviceId.empty()) {
76 CLOGE("The device id is null");
77 return ERR_INVALID_PARAM;
78 }
79 return proxy_ ? proxy_->RemoveDevice(deviceId) : CAST_ENGINE_ERROR;
80 }
81
StartAuth(const AuthInfo & authInfo)82 int32_t CastSession::StartAuth(const AuthInfo &authInfo)
83 {
84 if (authInfo.deviceId.empty()) {
85 CLOGE("The device id is null");
86 return ERR_INVALID_PARAM;
87 }
88 return proxy_ ? proxy_->StartAuth(authInfo) : CAST_ENGINE_ERROR;
89 }
90
GetSessionId(std::string & sessionId)91 int32_t CastSession::GetSessionId(std::string &sessionId)
92 {
93 if (!proxy_) {
94 CLOGE("proxy is null");
95 return CAST_ENGINE_ERROR;
96 }
97 int32_t ret = proxy_->GetSessionId(sessionId_);
98 sessionId = sessionId_;
99 return ret;
100 }
101
SetSessionProperty(const CastSessionProperty & property)102 int32_t CastSession::SetSessionProperty(const CastSessionProperty &property)
103 {
104 return proxy_ ? proxy_->SetSessionProperty(property) : CAST_ENGINE_ERROR;
105 }
106
CreateMirrorPlayer(std::shared_ptr<IMirrorPlayer> & mirrorPlayer)107 int32_t CastSession::CreateMirrorPlayer(std::shared_ptr<IMirrorPlayer> &mirrorPlayer)
108 {
109 if (!proxy_) {
110 CLOGE("proxy_ is null");
111 return CAST_ENGINE_ERROR;
112 }
113 sptr<IMirrorPlayerImpl> impl;
114 int32_t ret = proxy_->CreateMirrorPlayer(impl);
115 CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
116 if (!impl) {
117 return CAST_ENGINE_ERROR;
118 }
119
120 auto player = std::make_shared<MirrorPlayer>(impl);
121 if (!player) {
122 CLOGE("Failed to malloc mirror player");
123 return ERR_NO_MEMORY;
124 }
125 mirrorPlayer = player;
126 return ret;
127 }
128
CreateStreamPlayer(std::shared_ptr<IStreamPlayer> & streamPlayer)129 int32_t CastSession::CreateStreamPlayer(std::shared_ptr<IStreamPlayer> &streamPlayer)
130 {
131 if (!proxy_) {
132 CLOGE("proxy_ is null");
133 return CAST_ENGINE_ERROR;
134 }
135 sptr<IStreamPlayerIpc> streamPlayerIpc;
136 int32_t ret = proxy_->CreateStreamPlayer(streamPlayerIpc);
137 CHECK_AND_RETURN_RET_LOG(ret != CAST_ENGINE_SUCCESS, ret, "CastEngine Errors");
138 if (!streamPlayerIpc) {
139 return CAST_ENGINE_ERROR;
140 }
141
142 auto player = std::make_shared<StreamPlayer>(streamPlayerIpc);
143 if (!player) {
144 CLOGE("Failed to malloc stream player");
145 return ERR_NO_MEMORY;
146 }
147 streamPlayer = player;
148 return ret;
149 }
150
Release()151 int32_t CastSession::Release()
152 {
153 return proxy_ ? proxy_->Release() : CAST_ENGINE_ERROR;
154 }
155
SetCastMode(CastMode mode,std::string & jsonParam)156 int32_t CastSession::SetCastMode(CastMode mode, std::string &jsonParam)
157 {
158 return proxy_ ? proxy_->SetCastMode(mode, jsonParam) : false;
159 }
160
NotifyEvent(EventId eventId,std::string & jsonParam)161 int32_t CastSession::NotifyEvent(EventId eventId, std::string &jsonParam)
162 {
163 if (proxy_ != nullptr) {
164 proxy_->NotifyEvent(eventId, jsonParam);
165 return CAST_ENGINE_SUCCESS;
166 }
167 return CAST_ENGINE_ERROR;
168 }
169 } // namespace CastEngineClient
170 } // namespace CastEngine
171 } // namespace OHOS