• 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 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 
Release()160 int32_t CastSessionImplProxy::Release()
161 {
162     MessageParcel data;
163     MessageParcel reply;
164     MessageOption option;
165 
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(RELEASE, data, reply, option);
172     if (ret == ERR_UNKNOWN_TRANSACTION) {
173         CLOGE("No permission when releasing the cast session");
174         return ERR_NO_PERMISSION;
175     } else if (ret != ERR_NONE) {
176         CLOGE("Failed to send ipc request when releasing the cast session");
177         return CAST_ENGINE_ERROR;
178     }
179 
180     return reply.ReadInt32();
181 }
182 
GetSessionId(std::string & sessionId)183 int32_t CastSessionImplProxy::GetSessionId(std::string &sessionId)
184 {
185     MessageParcel data, reply;
186     MessageOption option;
187 
188     sessionId = std::string{};
189     if (!data.WriteInterfaceToken(GetDescriptor())) {
190         CLOGE("Failed to write the interface token");
191         return CAST_ENGINE_ERROR;
192     }
193 
194     int32_t ret = Remote()->SendRequest(GET_SESSION_ID, data, reply, option);
195     if (ret == ERR_UNKNOWN_TRANSACTION) {
196         CLOGE("No permission when getting session id");
197         return ERR_NO_PERMISSION;
198     } else if (ret != ERR_NONE) {
199         CLOGE("Failed to send ipc request when getting session id");
200         return CAST_ENGINE_ERROR;
201     }
202 
203     int32_t errorCode = reply.ReadInt32();
204     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
205     sessionId = reply.ReadString();
206 
207     return errorCode;
208 }
209 
GetDeviceState(const std::string & deviceId,DeviceState & deviceState)210 int32_t CastSessionImplProxy::GetDeviceState(const std::string &deviceId, DeviceState &deviceState)
211 {
212     MessageParcel data, reply;
213     MessageOption option;
214 
215     deviceState = DeviceState::DISCONNECTED;
216     if (!data.WriteInterfaceToken(GetDescriptor())) {
217         CLOGE("Failed to write the interface token");
218         return CAST_ENGINE_ERROR;
219     }
220     if (!data.WriteString(deviceId)) {
221         CLOGE("Failed to write the the device id");
222         return CAST_ENGINE_ERROR;
223     }
224 
225     int32_t ret = Remote()->SendRequest(GET_DEVICE_STATE, data, reply, option);
226     if (ret == ERR_UNKNOWN_TRANSACTION) {
227         CLOGE("No permission when getting device state");
228         return ERR_NO_PERMISSION;
229     } else if (ret == ERR_INVALID_DATA) {
230         CLOGE("Invalid parameter when getting device state");
231         return ERR_INVALID_PARAM;
232     } else if (ret != ERR_NONE) {
233         CLOGE("Failed to send ipc request when getting device state");
234         return CAST_ENGINE_ERROR;
235     }
236 
237     int32_t errorCode = reply.ReadInt32();
238     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
239     int state = reply.ReadInt32();
240     if (IsDeviceState(state)) {
241         deviceState = static_cast<DeviceState>(state);
242     }
243 
244     return errorCode;
245 }
246 
GetRemoteDeviceInfo(std::string deviceId,CastRemoteDevice & remoteDevice)247 int32_t CastSessionImplProxy::GetRemoteDeviceInfo(std::string deviceId, CastRemoteDevice &remoteDevice)
248 {
249     MessageParcel data;
250     MessageParcel reply;
251     MessageOption option;
252 
253     if (!data.WriteInterfaceToken(GetDescriptor())) {
254         CLOGE("Failed to write the interface token");
255         return CAST_ENGINE_ERROR;
256     }
257     if (!data.WriteString(deviceId)) {
258         CLOGE("Failed to Write remote deviceId");
259         return CAST_ENGINE_ERROR;
260     }
261 
262     int32_t ret = Remote()->SendRequest(GET_REMOTE_DEVICE_INFO, data, reply, option);
263     if (ret == ERR_NO_PERMISSION) {
264         CLOGE("No permission when getting remote device");
265         return ERR_NO_PERMISSION;
266     } else if (ret == ERR_INVALID_DATA) {
267         CLOGE("Invalid parameter when getting remote device");
268         return ERR_INVALID_PARAM;
269     } else if (ret != ERR_NONE) {
270         CLOGE("Failed to send ipc request when getting remote device");
271         return CAST_ENGINE_ERROR;
272     }
273 
274     int32_t errorCode = reply.ReadInt32();
275     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
276 
277     if (!ReadCastRemoteDevice(reply, remoteDevice)) {
278         CLOGE("Failed to get the remote device");
279         return CAST_ENGINE_ERROR;
280     }
281     return errorCode;
282 }
283 
SetSessionProperty(const CastSessionProperty & property)284 int32_t CastSessionImplProxy::SetSessionProperty(const CastSessionProperty &property)
285 {
286     MessageParcel data, reply;
287     MessageOption option;
288 
289     if (!data.WriteInterfaceToken(GetDescriptor())) {
290         CLOGE("Failed to write the interface token");
291         return CAST_ENGINE_ERROR;
292     }
293     if (!WriteCastSessionProperty(data, property)) {
294         CLOGE("Failed to write the property");
295         return CAST_ENGINE_ERROR;
296     }
297 
298     int32_t ret = Remote()->SendRequest(SET_SESSION_PROPERTY, data, reply, option);
299     if (ret == ERR_UNKNOWN_TRANSACTION) {
300         CLOGE("No permission when setting session property");
301         return ERR_NO_PERMISSION;
302     } else if (ret != ERR_NONE) {
303         CLOGE("Failed to send ipc request when setting session property");
304         return CAST_ENGINE_ERROR;
305     }
306 
307     return reply.ReadInt32();
308 }
309 
SetCastMode(CastMode mode,std::string & jsonParam)310 int32_t CastSessionImplProxy::SetCastMode(CastMode mode, std::string &jsonParam)
311 {
312     MessageParcel data, reply;
313     MessageOption option;
314 
315     if (!data.WriteInterfaceToken(GetDescriptor())) {
316         CLOGE("Failed to write the interface token");
317         return CAST_ENGINE_ERROR;
318     }
319     if (!data.WriteInt32(static_cast<int32_t>(mode))) {
320         CLOGE("Failed to write cast mode");
321         return CAST_ENGINE_ERROR;
322     }
323     if (!data.WriteString(jsonParam)) {
324         CLOGE("Failed to write json param");
325         return CAST_ENGINE_ERROR;
326     }
327     if (Remote()->SendRequest(SET_CAST_MODE, data, reply, option) != ERR_NONE) {
328         CLOGE("Failed to send ipc request when set cast mode");
329         return CAST_ENGINE_ERROR;
330     }
331 
332     return reply.ReadInt32();
333 }
334 
CreateMirrorPlayer(sptr<IMirrorPlayerImpl> & mirrorPlayer)335 int32_t CastSessionImplProxy::CreateMirrorPlayer(sptr<IMirrorPlayerImpl> &mirrorPlayer)
336 {
337     MessageParcel data;
338     MessageParcel reply;
339     MessageOption option;
340 
341     if (!data.WriteInterfaceToken(GetDescriptor())) {
342         CLOGE("Failed to write the interface token");
343         return CAST_ENGINE_ERROR;
344     }
345 
346     int32_t ret = Remote()->SendRequest(CREATE_MIRROR_PLAYER, data, reply, option);
347     if (ret == ERR_UNKNOWN_TRANSACTION) {
348         CLOGE("No permission when creating mirror player");
349         return ERR_NO_PERMISSION;
350     } else if (ret != ERR_NONE) {
351         CLOGE("Failed to send ipc request when creating mirror player");
352         return CAST_ENGINE_ERROR;
353     }
354 
355     int32_t errorCode = reply.ReadInt32();
356     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
357     auto object = reply.ReadRemoteObject();
358     if (!object) {
359         CLOGE("Failed to get the mirror player object");
360         return CAST_ENGINE_ERROR;
361     }
362     mirrorPlayer = iface_cast<IMirrorPlayerImpl>(object);
363 
364     return errorCode;
365 }
366 
CreateStreamPlayer(sptr<IStreamPlayerIpc> & streamPlayer)367 int32_t CastSessionImplProxy::CreateStreamPlayer(sptr<IStreamPlayerIpc> &streamPlayer)
368 {
369     MessageParcel data;
370     MessageParcel reply;
371     MessageOption option;
372 
373     if (!data.WriteInterfaceToken(GetDescriptor())) {
374         CLOGE("Failed to write the interface token");
375         return CAST_ENGINE_ERROR;
376     }
377 
378     int32_t ret = Remote()->SendRequest(CREAT_STREAM_PLAYER, data, reply, option);
379     if (ret == ERR_UNKNOWN_TRANSACTION) {
380         CLOGE("No permission when creating stream player");
381         return ERR_NO_PERMISSION;
382     } else if (ret != ERR_NONE) {
383         CLOGE("Failed to send ipc request when creating stream player");
384         return CAST_ENGINE_ERROR;
385     }
386 
387     int32_t errorCode = reply.ReadInt32();
388     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
389     auto object = reply.ReadRemoteObject();
390     if (!object) {
391         CLOGE("Failed to get the cast session object");
392         return CAST_ENGINE_ERROR;
393     }
394     streamPlayer = iface_cast<IStreamPlayerIpc>(object);
395 
396     return errorCode;
397 }
398 
NotifyEvent(EventId eventId,std::string & jsonParam)399 int32_t CastSessionImplProxy::NotifyEvent(EventId eventId, std::string &jsonParam)
400 {
401     MessageParcel data, reply;
402     MessageOption option;
403 
404     if (!data.WriteInterfaceToken(GetDescriptor())) {
405         CLOGE("Failed to write the interface token");
406         return CAST_ENGINE_ERROR;
407     }
408     if (!data.WriteInt32(static_cast<int32_t>(eventId))) {
409         CLOGE("Failed to write event id");
410         return CAST_ENGINE_ERROR;
411     }
412     if (!data.WriteString(jsonParam)) {
413         CLOGE("Failed to write json param");
414         return CAST_ENGINE_ERROR;
415     }
416     if (Remote()->SendRequest(NOTIFY_EVENT, data, reply, option) != ERR_NONE) {
417         CLOGE("Failed to send ipc request when notify event");
418         return CAST_ENGINE_ERROR;
419     }
420     return CAST_ENGINE_SUCCESS;
421 }
422 
423 } // namespace CastEngineClient
424 } // namespace CastEngine
425 } // namespace OHOS
426