• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: supply cast session implement proxy
15  * Author: zhangge
16  * Create: 2022-5-29
17  */
18 
19 #include "cast_session_impl_proxy.h"
20 
21 #include "cast_engine_common_helper.h"
22 #include "cast_engine_errors.h"
23 #include "cast_engine_log.h"
24 
25 namespace OHOS {
26 namespace CastEngine {
27 namespace CastEngineClient {
28 DEFINE_CAST_ENGINE_LABEL("Cast-Client-Session");
29 
~CastSessionImplProxy()30 CastSessionImplProxy::~CastSessionImplProxy()
31 {
32     CLOGI("Stop the client cast session proxy.");
33 }
34 
RegisterListener(sptr<ICastSessionListenerImpl> listener)35 int32_t CastSessionImplProxy::RegisterListener(sptr<ICastSessionListenerImpl> listener)
36 {
37     MessageParcel data, reply;
38     MessageOption option;
39 
40     if (!data.WriteInterfaceToken(GetDescriptor())) {
41         CLOGE("Failed to write the interface token");
42         return CAST_ENGINE_ERROR;
43     }
44     if (!data.WriteRemoteObject(listener->AsObject())) {
45         CLOGE("Failed to write cast session listener");
46         return CAST_ENGINE_ERROR;
47     }
48     if (Remote()->SendRequest(REGISTER_LISTENER, data, reply, option) != ERR_NONE) {
49         CLOGE("Failed to send ipc request when registering listener");
50         return CAST_ENGINE_ERROR;
51     }
52 
53     return reply.ReadInt32();
54 }
55 
UnregisterListener()56 int32_t CastSessionImplProxy::UnregisterListener()
57 {
58     MessageParcel data, reply;
59     MessageOption option;
60 
61     if (!data.WriteInterfaceToken(GetDescriptor())) {
62         CLOGE("Failed to write the interface token");
63         return CAST_ENGINE_ERROR;
64     }
65     if (Remote()->SendRequest(UNREGISTER_LISTENER, data, reply, option) != ERR_NONE) {
66         CLOGE("Failed to send ipc request when unregistering listener");
67         return CAST_ENGINE_ERROR;
68     }
69 
70     return reply.ReadInt32();
71 }
72 
AddDevice(const CastRemoteDevice & remoteDevice)73 int32_t CastSessionImplProxy::AddDevice(const CastRemoteDevice &remoteDevice)
74 {
75     MessageParcel data, reply;
76     MessageOption option;
77 
78     if (!data.WriteInterfaceToken(GetDescriptor())) {
79         CLOGE("Failed to write the interface token");
80         return CAST_ENGINE_ERROR;
81     }
82     if (!WriteCastRemoteDevice(data, remoteDevice)) {
83         CLOGE("Failed to write the remote device");
84         return CAST_ENGINE_ERROR;
85     }
86 
87     int32_t ret = Remote()->SendRequest(ADD_DEVICE, data, reply, option);
88     if (ret == ERR_UNKNOWN_TRANSACTION) {
89         CLOGE("No permission when adding device");
90         return ERR_NO_PERMISSION;
91     } else if (ret == ERR_INVALID_DATA) {
92         CLOGE("Invalid parameter when adding device");
93         return ERR_INVALID_PARAM;
94     } else if (ret != ERR_NONE) {
95         CLOGE("Failed to send ipc request when adding device");
96         return CAST_ENGINE_ERROR;
97     }
98 
99     return reply.ReadInt32();
100 }
101 
RemoveDevice(const std::string & deviceId)102 int32_t CastSessionImplProxy::RemoveDevice(const std::string &deviceId)
103 {
104     MessageParcel data, reply;
105     MessageOption option;
106 
107     if (!data.WriteInterfaceToken(GetDescriptor())) {
108         CLOGE("Failed to write the interface token");
109         return CAST_ENGINE_ERROR;
110     }
111     if (!data.WriteString(deviceId)) {
112         CLOGE("Failed to write the the device id");
113         return CAST_ENGINE_ERROR;
114     }
115 
116     int32_t ret = Remote()->SendRequest(REMOVE_DEVICE, data, reply, option);
117     if (ret == ERR_UNKNOWN_TRANSACTION) {
118         CLOGE("No permission when removing device");
119         return ERR_NO_PERMISSION;
120     } else if (ret == ERR_INVALID_DATA) {
121         CLOGE("Invalid parameter when removing device");
122         return ERR_INVALID_PARAM;
123     } else if (ret != ERR_NONE) {
124         CLOGE("Failed to send ipc request when removing device");
125         return CAST_ENGINE_ERROR;
126     }
127 
128     return reply.ReadInt32();
129 }
130 
StartAuth(const AuthInfo & authInfo)131 int32_t CastSessionImplProxy::StartAuth(const AuthInfo &authInfo)
132 {
133     MessageParcel data, reply;
134     MessageOption option;
135 
136     if (!data.WriteInterfaceToken(GetDescriptor())) {
137         CLOGE("Failed to write the interface token");
138         return false;
139     }
140     if (!WriteAuthInfo(data, authInfo)) {
141         CLOGE("Failed to write auth info");
142         return false;
143     }
144 
145     int32_t ret = Remote()->SendRequest(START_AUTH, data, reply, option);
146     if (ret == ERR_UNKNOWN_TRANSACTION) {
147         CLOGE("No permission when starting auth");
148         return ERR_NO_PERMISSION;
149     } else if (ret == ERR_INVALID_DATA) {
150         CLOGE("Invalid parameter when starting auth");
151         return ERR_INVALID_PARAM;
152     } else if (ret != ERR_NONE) {
153         CLOGE("Failed to send ipc request when starting auth");
154         return CAST_ENGINE_ERROR;
155     }
156 
157     return reply.ReadInt32();
158 }
159 
GetSessionId(std::string & sessionId)160 int32_t CastSessionImplProxy::GetSessionId(std::string &sessionId)
161 {
162     MessageParcel data, reply;
163     MessageOption option;
164 
165     sessionId = std::string{};
166     if (!data.WriteInterfaceToken(GetDescriptor())) {
167         CLOGE("Failed to write the interface token");
168         return CAST_ENGINE_ERROR;
169     }
170 
171     int32_t ret = Remote()->SendRequest(GET_SESSION_ID, data, reply, option);
172     if (ret == ERR_UNKNOWN_TRANSACTION) {
173         CLOGE("No permission when getting session id");
174         return ERR_NO_PERMISSION;
175     } else if (ret != ERR_NONE) {
176         CLOGE("Failed to send ipc request when getting session id");
177         return CAST_ENGINE_ERROR;
178     }
179 
180     int32_t errorCode = reply.ReadInt32();
181     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
182     sessionId = reply.ReadString();
183 
184     return errorCode;
185 }
186 
GetDeviceState(const std::string & deviceId,DeviceState & deviceState)187 int32_t CastSessionImplProxy::GetDeviceState(const std::string &deviceId, DeviceState &deviceState)
188 {
189     MessageParcel data, reply;
190     MessageOption option;
191 
192     deviceState = DeviceState::DISCONNECTED;
193     if (!data.WriteInterfaceToken(GetDescriptor())) {
194         CLOGE("Failed to write the interface token");
195         return CAST_ENGINE_ERROR;
196     }
197     if (!data.WriteString(deviceId)) {
198         CLOGE("Failed to write the the device id");
199         return CAST_ENGINE_ERROR;
200     }
201 
202     int32_t ret = Remote()->SendRequest(GET_DEVICE_STATE, data, reply, option);
203     if (ret == ERR_UNKNOWN_TRANSACTION) {
204         CLOGE("No permission when getting device state");
205         return ERR_NO_PERMISSION;
206     } else if (ret == ERR_INVALID_DATA) {
207         CLOGE("Invalid parameter when getting device state");
208         return ERR_INVALID_PARAM;
209     } else if (ret != ERR_NONE) {
210         CLOGE("Failed to send ipc request when getting device state");
211         return CAST_ENGINE_ERROR;
212     }
213 
214     int32_t errorCode = reply.ReadInt32();
215     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
216     int state = reply.ReadInt32();
217     if (IsDeviceState(state)) {
218         deviceState = static_cast<DeviceState>(state);
219     }
220 
221     return errorCode;
222 }
223 
SetSessionProperty(const CastSessionProperty & property)224 int32_t CastSessionImplProxy::SetSessionProperty(const CastSessionProperty &property)
225 {
226     MessageParcel data, reply;
227     MessageOption option;
228 
229     if (!data.WriteInterfaceToken(GetDescriptor())) {
230         CLOGE("Failed to write the interface token");
231         return CAST_ENGINE_ERROR;
232     }
233     if (!WriteCastSessionProperty(data, property)) {
234         CLOGE("Failed to write the property");
235         return CAST_ENGINE_ERROR;
236     }
237 
238     int32_t ret = Remote()->SendRequest(SET_SESSION_PROPERTY, data, reply, option);
239     if (ret == ERR_UNKNOWN_TRANSACTION) {
240         CLOGE("No permission when setting session property");
241         return ERR_NO_PERMISSION;
242     } else if (ret != ERR_NONE) {
243         CLOGE("Failed to send ipc request when setting session property");
244         return CAST_ENGINE_ERROR;
245     }
246 
247     return reply.ReadInt32();
248 }
249 
CreateMirrorPlayer(sptr<IMirrorPlayerImpl> & mirrorPlayer)250 int32_t CastSessionImplProxy::CreateMirrorPlayer(sptr<IMirrorPlayerImpl> &mirrorPlayer)
251 {
252     MessageParcel data;
253     MessageParcel reply;
254     MessageOption option;
255 
256     if (!data.WriteInterfaceToken(GetDescriptor())) {
257         CLOGE("Failed to write the interface token");
258         return CAST_ENGINE_ERROR;
259     }
260 
261     int32_t ret = Remote()->SendRequest(CREAT_MIRROR_PLAYER, data, reply, option);
262     if (ret == ERR_UNKNOWN_TRANSACTION) {
263         CLOGE("No permission when creating mirror player");
264         return ERR_NO_PERMISSION;
265     } else if (ret != ERR_NONE) {
266         CLOGE("Failed to send ipc request when creating mirror player");
267         return CAST_ENGINE_ERROR;
268     }
269 
270     int32_t errorCode = reply.ReadInt32();
271     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
272     auto object = reply.ReadRemoteObject();
273     if (!object) {
274         CLOGE("Failed to get the mirror player object");
275         return CAST_ENGINE_ERROR;
276     }
277     mirrorPlayer = iface_cast<IMirrorPlayerImpl>(object);
278 
279     return errorCode;
280 }
281 
CreateStreamPlayer(sptr<IStreamPlayerIpc> & streamPlayer)282 int32_t CastSessionImplProxy::CreateStreamPlayer(sptr<IStreamPlayerIpc> &streamPlayer)
283 {
284     MessageParcel data;
285     MessageParcel reply;
286     MessageOption option;
287 
288     if (!data.WriteInterfaceToken(GetDescriptor())) {
289         CLOGE("Failed to write the interface token");
290         return CAST_ENGINE_ERROR;
291     }
292 
293     int32_t ret = Remote()->SendRequest(CREAT_STREAM_PLAYER, data, reply, option);
294     if (ret == ERR_UNKNOWN_TRANSACTION) {
295         CLOGE("No permission when creating stream player");
296         return ERR_NO_PERMISSION;
297     } else if (ret != ERR_NONE) {
298         CLOGE("Failed to send ipc request when creating stream player");
299         return CAST_ENGINE_ERROR;
300     }
301 
302     int32_t errorCode = reply.ReadInt32();
303     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
304     auto object = reply.ReadRemoteObject();
305     if (!object) {
306         CLOGE("Failed to get the cast session object");
307         return CAST_ENGINE_ERROR;
308     }
309     streamPlayer = iface_cast<IStreamPlayerIpc>(object);
310 
311     return errorCode;
312 }
313 
Release()314 int32_t CastSessionImplProxy::Release()
315 {
316     MessageParcel data;
317     MessageParcel reply;
318     MessageOption option;
319 
320     if (!data.WriteInterfaceToken(GetDescriptor())) {
321         CLOGE("Failed to write the interface token");
322         return CAST_ENGINE_ERROR;
323     }
324 
325     int32_t ret = Remote()->SendRequest(RELEASE, data, reply, option);
326     if (ret == ERR_UNKNOWN_TRANSACTION) {
327         CLOGE("No permission when releasing the cast session");
328         return ERR_NO_PERMISSION;
329     } else if (ret != ERR_NONE) {
330         CLOGE("Failed to send ipc request when releasing the cast session");
331         return CAST_ENGINE_ERROR;
332     }
333 
334     return reply.ReadInt32();
335 }
336 
SetCastMode(CastMode mode,std::string & jsonParam)337 int32_t CastSessionImplProxy::SetCastMode(CastMode mode, std::string &jsonParam)
338 {
339     MessageParcel data, reply;
340     MessageOption option;
341 
342     if (!data.WriteInterfaceToken(GetDescriptor())) {
343         CLOGE("Failed to write the interface token");
344         return CAST_ENGINE_ERROR;
345     }
346     if (!data.WriteInt32(static_cast<int32_t>(mode))) {
347         CLOGE("Failed to write cast mode");
348         return CAST_ENGINE_ERROR;
349     }
350     if (!data.WriteString(jsonParam)) {
351         CLOGE("Failed to write json param");
352         return CAST_ENGINE_ERROR;
353     }
354     if (Remote()->SendRequest(SET_CAST_MODE, data, reply, option) != ERR_NONE) {
355         CLOGE("Failed to send ipc request when set cast mode");
356         return CAST_ENGINE_ERROR;
357     }
358 
359     return reply.ReadInt32();
360 }
361 
NotifyEvent(EventId eventId,std::string & jsonParam)362 int32_t CastSessionImplProxy::NotifyEvent(EventId eventId, std::string &jsonParam)
363 {
364     MessageParcel data, reply;
365     MessageOption option;
366 
367     if (!data.WriteInterfaceToken(GetDescriptor())) {
368         CLOGE("Failed to write the interface token");
369         return CAST_ENGINE_ERROR;
370     }
371     if (!data.WriteInt32(static_cast<int32_t>(eventId))) {
372         CLOGE("Failed to write event id");
373         return CAST_ENGINE_ERROR;
374     }
375     if (!data.WriteString(jsonParam)) {
376         CLOGE("Failed to write json param");
377         return CAST_ENGINE_ERROR;
378     }
379     if (Remote()->SendRequest(NOTIFY_EVENT, data, reply, option) != ERR_NONE) {
380         CLOGE("Failed to send ipc request when notify event");
381         return CAST_ENGINE_ERROR;
382     }
383     return CAST_ENGINE_SUCCESS;
384 }
385 
386 } // namespace CastEngineClient
387 } // namespace CastEngine
388 } // namespace OHOS
389