1 /*
2  * Copyright (c) 2022-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  */
15 
16 #include "avsession_service_proxy.h"
17 #include "avsession_log.h"
18 #include "avsession_proxy.h"
19 #include "avsession_controller_proxy.h"
20 
21 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
22 #include "avcast_controller_proxy.h"
23 #endif
24 
25 namespace OHOS::AVSession {
AVSessionServiceProxy(const sptr<IRemoteObject>& impl)26 AVSessionServiceProxy::AVSessionServiceProxy(const sptr<IRemoteObject>& impl)
27     : IRemoteProxy<IAVSessionService>(impl)
28 {
29     SLOGI("constructor");
30 }
31 
CreateSession(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName)32 std::shared_ptr<AVSession> AVSessionServiceProxy::CreateSession(const std::string& tag, int32_t type,
33                                                                 const AppExecFwk::ElementName& elementName)
34 {
35     auto object = AVSessionServiceProxy::CreateSessionInner(tag, type, elementName);
36     if (object == nullptr) {
37         SLOGE("object is nullptr");
38         return nullptr;
39     }
40     auto session = iface_cast<AVSessionProxy>(object);
41     if (session == nullptr) {
42         SLOGE("session is nullptr");
43         return nullptr;
44     }
45     return std::shared_ptr<AVSession>(session.GetRefPtr(), [holder = session](const auto*) {});
46 }
47 
CreateSession(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName, std::shared_ptr<AVSession>& session)48 int32_t AVSessionServiceProxy::CreateSession(const std::string& tag, int32_t type,
49                                              const AppExecFwk::ElementName& elementName,
50                                              std::shared_ptr<AVSession>& session)
51 {
52     sptr<IRemoteObject> object;
53     auto ret = AVSessionServiceProxy::CreateSessionInner(tag, type, elementName, object);
54     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateSession failed");
55 
56     auto sessionObj = iface_cast<AVSessionProxy>(object);
57     CHECK_AND_RETURN_RET_LOG(sessionObj, AVSESSION_ERROR, "sessionObj is nullptr");
58 
59     session = std::shared_ptr<AVSession>(sessionObj.GetRefPtr(), [holder = sessionObj](const auto*) {});
60     return ret;
61 }
62 
CreateSessionInner(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName)63 sptr<IRemoteObject> AVSessionServiceProxy::CreateSessionInner(const std::string& tag, int32_t type,
64                                                               const AppExecFwk::ElementName& elementName)
65 {
66     sptr<IRemoteObject> object;
67     auto ret = AVSessionServiceProxy::CreateSessionInner(tag, type, elementName, object);
68     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, nullptr, "CreateSessionInner failed");
69     return object;
70 }
71 
CreateSessionInner(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName, sptr<IRemoteObject>& object)72 int32_t AVSessionServiceProxy::CreateSessionInner(const std::string& tag, int32_t type,
73                                                   const AppExecFwk::ElementName& elementName,
74                                                   sptr<IRemoteObject>& object)
75 {
76     MessageParcel data;
77     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
78                              ERR_UNMARSHALLING, "write interface token failed");
79     CHECK_AND_RETURN_RET_LOG(data.WriteString(tag), ERR_UNMARSHALLING, "write tag failed");
80     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(type), ERR_UNMARSHALLING, "write type failed");
81     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&elementName), ERR_UNMARSHALLING, "write bundleName failed");
82 
83     auto remote = Remote();
84     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_UNMARSHALLING, "get remote service failed");
85     MessageParcel reply;
86     MessageOption option;
87     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
88         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_SESSION), data, reply, option) == 0,
89         ERR_IPC_SEND_REQUEST, "send request failed");
90 
91     int32_t res = AVSESSION_ERROR;
92     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(res), ERR_UNMARSHALLING, "read res failed");
93     if (res == AVSESSION_SUCCESS) {
94         object = reply.ReadRemoteObject();
95     }
96     return res;
97 }
98 
GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)99 int32_t AVSessionServiceProxy::GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)
100 {
101     MessageParcel data;
102     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
103                              "write interface token failed");
104     auto remote = Remote();
105     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
106     MessageParcel reply;
107     MessageOption option;
108     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
109         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_ALL_SESSION_DESCRIPTORS),\
110         data, reply, option) == 0,
111         ERR_IPC_SEND_REQUEST, "send request failed");
112 
113     int32_t ret = AVSESSION_ERROR;
114     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
115     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
116     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
117     if (ret == AVSESSION_SUCCESS) {
118         uint32_t size {};
119         CHECK_AND_RETURN_RET_LOG(reply.ReadUint32(size), ERR_UNMARSHALLING, "read vector size failed");
120         CHECK_AND_RETURN_RET_LOG(size, ret, "get all session with true empty");
121 
122         std::vector<AVSessionDescriptor> result(size);
123         for (auto& descriptor : result) {
124             CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
125         }
126         descriptors = result;
127     }
128     return ret;
129 }
130 
GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor)131 int32_t AVSessionServiceProxy::GetSessionDescriptorsBySessionId(const std::string& sessionId,
132     AVSessionDescriptor& descriptor)
133 {
134     MessageParcel data;
135     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
136         "write interface token failed");
137     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_MARSHALLING, "write sessionId failed");
138 
139     auto remote = Remote();
140     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
141     MessageParcel reply;
142     MessageOption option;
143     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
144         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_SESSION_DESCRIPTORS_BY_ID),\
145         data, reply, option) == 0,
146         ERR_IPC_SEND_REQUEST, "send request failed");
147     int32_t ret = AVSESSION_ERROR;
148     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
149     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
150     if (ret == AVSESSION_SUCCESS) {
151         CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
152     }
153     return ret;
154 }
155 
GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors)156 int32_t AVSessionServiceProxy::GetHistoricalSessionDescriptors(int32_t maxSize,
157     std::vector<AVSessionDescriptor>& descriptors)
158 {
159     MessageParcel data;
160     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
161         "write interface token failed");
162     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(maxSize), ERR_MARSHALLING, "write maxSize failed");
163 
164     auto remote = Remote();
165     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
166     MessageParcel reply;
167     MessageOption option;
168     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
169         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_HISTORY_SESSION_DESCRIPTORS),\
170         data, reply, option) == 0,
171         ERR_IPC_SEND_REQUEST, "send request failed");
172 
173     int32_t ret = AVSESSION_ERROR;
174     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
175     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
176     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
177     if (ret == AVSESSION_SUCCESS) {
178         uint32_t size {};
179         CHECK_AND_RETURN_RET_LOG(reply.ReadUint32(size), ERR_UNMARSHALLING, "read vector size failed");
180         CHECK_AND_RETURN_RET_LOG(size, ret, "size=0");
181 
182         std::vector<AVSessionDescriptor> result(size);
183         for (auto& descriptor : result) {
184             CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
185         }
186         descriptors = result;
187     }
188     return ret;
189 }
190 
UnMarshallingAVQueueInfos(MessageParcel &reply, std::vector<AVQueueInfo>& avQueueInfos)191 void AVSessionServiceProxy::UnMarshallingAVQueueInfos(MessageParcel &reply, std::vector<AVQueueInfo>& avQueueInfos)
192 {
193     uint32_t size {};
194     CHECK_AND_RETURN_LOG(reply.ReadUint32(size), "UnMarshallingAVQueueInfos size failed");
195     CHECK_AND_RETURN_LOG(size, "UnMarshallingAVQueueInfos size=0");
196 
197     for (uint32_t i = 0; i < size; i++) {
198         AVQueueInfo avQueueInfo;
199         avQueueInfo.SetBundleName(reply.ReadString());
200         avQueueInfo.SetAVQueueName(reply.ReadString());
201         avQueueInfo.SetAVQueueId(reply.ReadString());
202         avQueueInfo.SetAVQueueImageUri(reply.ReadString());
203         avQueueInfo.SetAVQueueLength(reply.ReadUint32());
204         avQueueInfos.push_back(avQueueInfo);
205     }
206 }
207 
BufferToAVQueueInfoImg(const char *buffer, std::vector<AVQueueInfo>& avQueueInfos)208 void AVSessionServiceProxy::BufferToAVQueueInfoImg(const char *buffer, std::vector<AVQueueInfo>& avQueueInfos)
209 {
210     int k = 0;
211     for (auto& avQueueInfo : avQueueInfos) {
212         std::shared_ptr<AVSessionPixelMap> pixelMap = std::make_shared<AVSessionPixelMap>();
213         std::vector<uint8_t> imgBuffer;
214         int avQueueLength = avQueueInfo.GetAVQueueLength();
215         for (int i = 0; i < avQueueLength; i++, k++) {
216             imgBuffer.push_back((uint8_t)buffer[k]);
217         }
218         pixelMap->SetInnerImgBuffer(imgBuffer);
219         avQueueInfo.SetAVQueueImage(pixelMap);
220     }
221 }
222 
GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize, std::vector<AVQueueInfo>& avQueueInfos)223 int32_t AVSessionServiceProxy::GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
224     std::vector<AVQueueInfo>& avQueueInfos)
225 {
226     MessageParcel data;
227     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
228         "write interface token failed");
229     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(maxSize), ERR_MARSHALLING, "write maxSize failed");
230     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(maxAppSize), ERR_MARSHALLING, "write maxAppSize failed");
231 
232     auto remote = Remote();
233     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
234     MessageParcel reply;
235     MessageOption option;
236     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
237         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_HISTORY_AVQUEUE_INFOS),\
238         data, reply, option) == 0,
239         ERR_IPC_SEND_REQUEST, "send request failed");
240 
241     int32_t ret = AVSESSION_ERROR;
242     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
243     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
244     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
245     if (ret != AVSESSION_SUCCESS) {
246         return ret;
247     }
248     int bufferLength = reply.ReadInt32();
249     if (bufferLength == 0) {
250         uint32_t size {};
251         CHECK_AND_RETURN_RET_LOG(reply.ReadUint32(size), ERR_UNMARSHALLING, "read vector size failed");
252         CHECK_AND_RETURN_RET_LOG(size, ret, "size=0");
253 
254         std::vector<AVQueueInfo> result(size);
255         for (auto& avqueueInfo : result) {
256             CHECK_AND_RETURN_RET_LOG(avqueueInfo.Unmarshalling(reply), ERR_UNMARSHALLING, "read avqueueInfo failed");
257         }
258         avQueueInfos = result;
259         return ret;
260     }
261     UnMarshallingAVQueueInfos(reply, avQueueInfos);
262     const char *buffer = nullptr;
263     buffer = reinterpret_cast<const char *>(reply.ReadRawData(bufferLength));
264     if (buffer == nullptr) {
265         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
266         SLOGE("read raw data failed, length = %{public}d", bufferLength);
267         return AVSESSION_ERROR;
268     }
269     BufferToAVQueueInfoImg(buffer, avQueueInfos);
270     return AVSESSION_SUCCESS;
271 }
272 
StartAVPlayback(const std::string& bundleName, const std::string& assetId)273 int32_t AVSessionServiceProxy::StartAVPlayback(const std::string& bundleName, const std::string& assetId)
274 {
275     MessageParcel data;
276     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
277         "write interface token failed");
278     CHECK_AND_RETURN_RET_LOG(data.WriteString(bundleName), ERR_MARSHALLING, "write bundleName failed");
279     CHECK_AND_RETURN_RET_LOG(data.WriteString(assetId), ERR_MARSHALLING, "write assetId failed");
280 
281     auto remote = Remote();
282     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
283     MessageParcel reply;
284     MessageOption option;
285     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
286         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_AV_PLAYBACK),\
287         data, reply, option) == 0,
288         ERR_IPC_SEND_REQUEST, "send request failed");
289 
290     int32_t ret = AVSESSION_ERROR;
291     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
292     return ret;
293 }
294 
IsAudioPlaybackAllowed(const int32_t uid, const int32_t pid)295 bool AVSessionServiceProxy::IsAudioPlaybackAllowed(const int32_t uid, const int32_t pid)
296 {
297     MessageParcel data;
298     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
299         "write interface token failed");
300     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(uid), ERR_MARSHALLING, "write uid failed");
301     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(pid), ERR_MARSHALLING, "write uid failed");
302 
303     auto remote = Remote();
304     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
305     MessageParcel reply;
306     MessageOption option;
307     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
308         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CHECK_BACKGROUND_ALLOWED),\
309         data, reply, option) == 0, ERR_IPC_SEND_REQUEST, "send request failed");
310 
311     bool ret = false;
312     CHECK_AND_RETURN_RET_LOG(reply.ReadBool(ret), ERR_UNMARSHALLING, "read bool failed");
313     return ret;
314 }
315 
CreateController(const std::string& sessionId, std::shared_ptr<AVSessionController>& controller)316 int32_t AVSessionServiceProxy::CreateController(const std::string& sessionId,
317     std::shared_ptr<AVSessionController>& controller)
318 {
319     SLOGI("create controller in without lock");
320     sptr<IRemoteObject> object;
321     auto ret = AVSessionServiceProxy::CreateControllerInner(sessionId, object);
322     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
323     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
324     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
325     auto controllerObject = iface_cast<AVSessionControllerProxy>(object);
326     CHECK_AND_RETURN_RET_LOG(controllerObject, AVSESSION_ERROR, "controllerObject is nullptr");
327 
328     controller = std::shared_ptr<AVSessionController>(controllerObject.GetRefPtr(),
329         [holder = controllerObject](const auto*) {});
330     return ret;
331 }
332 
CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)333 int32_t AVSessionServiceProxy::CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
334 {
335     MessageParcel data;
336     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
337                              "write interface token failed");
338     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
339 
340     auto remote = Remote();
341     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
342     MessageParcel reply;
343     MessageOption option;
344     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
345         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_CONTROLLER), data, reply, option) == 0,
346         ERR_IPC_SEND_REQUEST, "send request failed");
347     int32_t ret = AVSESSION_ERROR;
348     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
349     if (ret == AVSESSION_SUCCESS || ret == ERR_CONTROLLER_IS_EXIST) {
350         object = reply.ReadRemoteObject();
351     }
352     return ret;
353 }
354 
355 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastController(const std::string& sessionId, std::shared_ptr<AVCastController>& castController)356 int32_t AVSessionServiceProxy::GetAVCastController(const std::string& sessionId,
357     std::shared_ptr<AVCastController>& castController)
358 {
359     sptr<IRemoteObject> object;
360     auto ret = AVSessionServiceProxy::GetAVCastControllerInner(sessionId, object);
361     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
362     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
363     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
364 
365     auto castControllerObject = iface_cast<AVCastControllerProxy>(object);
366     CHECK_AND_RETURN_RET_LOG(castControllerObject, AVSESSION_ERROR, "castControllerObject is nullptr");
367 
368     castController = std::shared_ptr<AVCastController>(castControllerObject.GetRefPtr(),
369         [holder = castControllerObject](const auto*) {});
370     return ret;
371 }
372 
GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)373 int32_t AVSessionServiceProxy::GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
374 {
375     MessageParcel data;
376     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
377         "write interface token failed");
378     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
379 
380     auto remote = Remote();
381     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
382     MessageParcel reply;
383     MessageOption option;
384     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
385         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_AV_CAST_CONTROLLER),\
386         data, reply, option) == 0,
387         ERR_IPC_SEND_REQUEST, "send request failed");
388     int32_t ret = AVSESSION_ERROR;
389     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
390     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
391     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
392     if (ret == AVSESSION_SUCCESS) {
393         object = reply.ReadRemoteObject();
394     }
395     return ret;
396 }
397 #endif
398 
RegisterSessionListener(const sptr<ISessionListener>& listener)399 int32_t AVSessionServiceProxy::RegisterSessionListener(const sptr<ISessionListener>& listener)
400 {
401     MessageParcel data;
402     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
403                              "write interface token failed in RegisterSessionListener");
404     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(listener->AsObject()), ERR_MARSHALLING, "write tag failed");
405 
406     auto remote = Remote();
407     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
408     MessageParcel reply;
409     MessageOption option;
410     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
411         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_SESSION_LISTENER),\
412         data, reply, option) == 0,
413         ERR_IPC_SEND_REQUEST, "send request failed");
414     int32_t res = AVSESSION_ERROR;
415     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
416 }
417 
RegisterSessionListenerForAllUsers(const sptr<ISessionListener>& listener)418 int32_t AVSessionServiceProxy::RegisterSessionListenerForAllUsers(const sptr<ISessionListener>& listener)
419 {
420     MessageParcel data;
421     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
422                              "write interface token failed in RegisterSessionListenerForAllUsers");
423     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(listener->AsObject()), ERR_MARSHALLING, "write tag failed");
424 
425     auto remote = Remote();
426     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
427     MessageParcel reply;
428     MessageOption option;
429     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
430         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_SESSION_LISTENER_FOR_ALL_USERS),\
431         data, reply, option) == 0,
432         ERR_IPC_SEND_REQUEST, "send request failed");
433     int32_t res = AVSESSION_ERROR;
434     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
435 }
436 
SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)437 int32_t AVSessionServiceProxy::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
438 {
439     MessageParcel data;
440     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
441                              "write interface token failed");
442     SLOGI("try SendSystemAVKeyEvent with key=%{public}d", keyEvent.GetKeyCode());
443     CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(data), ERR_MARSHALLING, "write keyEvent failed");
444 
445     auto remote = Remote();
446     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
447     MessageParcel reply;
448     MessageOption option;
449     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
450         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SEND_SYSTEM_AV_KEY_EVENT),\
451         data, reply, option) == 0,
452         ERR_IPC_SEND_REQUEST, "send request failed");
453     int32_t res = AVSESSION_ERROR;
454     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
455 }
456 
SendSystemControlCommand(const AVControlCommand& command)457 int32_t AVSessionServiceProxy::SendSystemControlCommand(const AVControlCommand& command)
458 {
459     MessageParcel data;
460     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
461                              "write interface token failed");
462     SLOGI("try SendSystemControlCommand with cmd=%{public}d", command.GetCommand());
463     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&command), ERR_MARSHALLING, "write keyEvent failed");
464 
465     auto remote = Remote();
466     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
467     MessageParcel reply;
468     MessageOption option;
469     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
470         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SEND_SYSTEM_CONTROL_COMMAND),\
471         data, reply, option) == 0,
472         ERR_IPC_SEND_REQUEST, "send request failed");
473     int32_t res = AVSESSION_ERROR;
474     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
475 }
476 
RegisterClientDeathObserver(const sptr<IClientDeath>& observer)477 int32_t AVSessionServiceProxy::RegisterClientDeathObserver(const sptr<IClientDeath>& observer)
478 {
479     MessageParcel data;
480     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
481                              "write interface token failed");
482     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(observer->AsObject()), ERR_MARSHALLING, "write observer failed");
483 
484     auto remote = Remote();
485     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
486     MessageParcel reply;
487     MessageOption option;
488     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
489         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_CLIENT_DEATH),\
490         data, reply, option) == 0,
491         ERR_IPC_SEND_REQUEST, "send request failed");
492     int32_t res = AVSESSION_ERROR;
493     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
494 }
495 
Close(void)496 int32_t AVSessionServiceProxy::Close(void)
497 {
498     MessageParcel data;
499     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
500                              "write interface token failed");
501 
502     auto remote = Remote();
503     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
504     MessageParcel reply;
505     MessageOption option;
506     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
507         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CLOSE),\
508         data, reply, option) == 0,
509         ERR_IPC_SEND_REQUEST, "send request failed");
510     int32_t res = AVSESSION_ERROR;
511     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
512 }
513 
CastAudio(const SessionToken& token, const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)514 int32_t AVSessionServiceProxy::CastAudio(const SessionToken& token,
515                                          const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
516 {
517     MessageParcel data;
518     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
519                              "write interface token failed");
520     CHECK_AND_RETURN_RET_LOG(data.WriteString(token.sessionId), ERR_MARSHALLING, "write sessionId failed");
521     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.pid), ERR_MARSHALLING, "write pid failed");
522     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.uid), ERR_MARSHALLING, "write uid failed");
523     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
524                              "write descriptors size failed");
525     for (auto descriptor : descriptors) {
526         SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
527               static_cast<int32_t>(descriptor.deviceRole_));
528         CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
529     }
530 
531     auto remote = Remote();
532     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
533     MessageParcel reply;
534     MessageOption option;
535     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
536         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO), data, reply, option) == 0,
537         ERR_IPC_SEND_REQUEST, "send request failed");
538     int32_t res = AVSESSION_ERROR;
539     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
540 }
541 
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)542 int32_t AVSessionServiceProxy::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
543 {
544     MessageParcel data;
545     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
546                              "write interface token failed");
547     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
548                              "write descriptors size failed");
549     for (auto descriptor : descriptors) {
550         SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
551               static_cast<int32_t>(descriptor.deviceRole_));
552         CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
553     }
554 
555     auto remote = Remote();
556     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
557     MessageParcel reply;
558     MessageOption option;
559     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
560         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO_FOR_ALL), data, reply, option) == 0,
561         ERR_IPC_SEND_REQUEST, "send request failed");
562     int32_t res = AVSESSION_ERROR;
563     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
564 }
565 
566 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)567 int32_t AVSessionServiceProxy::StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
568 {
569     MessageParcel data;
570     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
571         "write interface token failed");
572     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(castDeviceCapability),
573         ERR_MARSHALLING, "write castDeviceCapability failed");
574     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(drmSchemes.size()), ERR_MARSHALLING, "write drmSchemes size failed");
575     for (auto drmScheme : drmSchemes) {
576         CHECK_AND_RETURN_RET_LOG(data.WriteString(drmScheme), ERR_MARSHALLING, "write drmScheme failed");
577     }
578     auto remote = Remote();
579     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
580     MessageParcel reply;
581     MessageOption option;
582     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
583         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST_DISCOVERY),\
584         data, reply, option) == 0,
585         ERR_IPC_SEND_REQUEST, "send request failed");
586     int32_t res = AVSESSION_ERROR;
587     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
588 }
589 
StopCastDiscovery()590 int32_t AVSessionServiceProxy::StopCastDiscovery()
591 {
592     MessageParcel data;
593     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
594         "write interface token failed");
595 
596     auto remote = Remote();
597     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
598     MessageParcel reply;
599     MessageOption option;
600     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
601         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_CAST_DISCOVERY), data, reply, option) == 0,
602         ERR_IPC_SEND_REQUEST, "send request failed");
603     int32_t res = AVSESSION_ERROR;
604     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
605 }
606 
SetDiscoverable(const bool enable)607 int32_t AVSessionServiceProxy::SetDiscoverable(const bool enable)
608 {
609     MessageParcel data;
610     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
611         "write interface token failed");
612     CHECK_AND_RETURN_RET_LOG(data.WriteBool(enable), ERR_MARSHALLING, "write enable failed");
613 
614     auto remote = Remote();
615     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
616     MessageParcel reply;
617     MessageOption option;
618     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
619         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SET_DISCOVERYABLE), data, reply, option) == 0,
620         ERR_IPC_SEND_REQUEST, "send request failed");
621     int32_t res = AVSESSION_ERROR;
622     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
623 }
624 
StartDeviceLogging(int32_t fd, uint32_t maxSize)625 int32_t AVSessionServiceProxy::StartDeviceLogging(int32_t fd, uint32_t maxSize)
626 {
627     MessageParcel data;
628     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
629         "write interface token failed");
630     CHECK_AND_RETURN_RET_LOG(data.WriteFileDescriptor(fd), ERR_MARSHALLING, "write fd failed");
631     CHECK_AND_RETURN_RET_LOG(data.WriteUint32(maxSize), ERR_MARSHALLING, "write maxSize failed");
632     auto remote = Remote();
633     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
634     MessageParcel reply;
635     MessageOption option;
636     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
637         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_DEVICE_LOGGING),\
638         data, reply, option) == 0,
639         ERR_IPC_SEND_REQUEST, "send request failed");
640     int32_t res = AVSESSION_ERROR;
641     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
642 }
643 
StopDeviceLogging()644 int32_t AVSessionServiceProxy::StopDeviceLogging()
645 {
646     MessageParcel data;
647     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
648         "write interface token failed");
649 
650     auto remote = Remote();
651     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
652     MessageParcel reply;
653     MessageOption option;
654     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
655         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_DEVICE_LOGGING), data, reply, option) == 0,
656         ERR_IPC_SEND_REQUEST, "send request failed");
657     int32_t res = AVSESSION_ERROR;
658     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
659 }
660 
StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)661 int32_t AVSessionServiceProxy::StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)
662 {
663     MessageParcel data;
664     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
665         "write interface token failed");
666     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
667     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
668     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
669 
670     int32_t deviceInfoSize = static_cast<int32_t>(outputDeviceInfo.deviceInfos_.size());
671     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfoSize), ERR_MARSHALLING, "write deviceInfoSize failed");
672     for (const DeviceInfo& deviceInfo : outputDeviceInfo.deviceInfos_) {
673         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.castCategory_),
674             ERR_MARSHALLING, "write castCategory failed");
675         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceId_), ERR_MARSHALLING, "write deviceId failed");
676         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceName_), ERR_MARSHALLING, "write deviceName failed");
677         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.deviceType_), ERR_MARSHALLING, "write deviceType failed");
678         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.ipAddress_), ERR_MARSHALLING, "write ipAddress failed");
679         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.manufacturer_),
680             ERR_MARSHALLING, "write manufacturer failed");
681         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.modelName_), ERR_MARSHALLING, "write modelName failed");
682         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.providerId_), ERR_MARSHALLING, "write providerId failed");
683         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.supportedProtocols_), ERR_MARSHALLING,
684             "write supportedProtocols failed");
685         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.authenticationStatus_), ERR_MARSHALLING,
686             "write authenticationStatus failed");
687         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.supportedDrmCapabilities_.size()), ERR_MARSHALLING,
688             "write supportedDrmCapabilities size failed");
689         for (auto supportedDrmCapability : deviceInfo.supportedDrmCapabilities_) {
690             CHECK_AND_RETURN_RET_LOG(data.WriteString(supportedDrmCapability), ERR_MARSHALLING,
691                 "write supportedDrmCapability failed");
692         }
693         CHECK_AND_RETURN_RET_LOG(data.WriteBool(deviceInfo.isLegacy_), ERR_MARSHALLING, "write isLegacy failed");
694         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.mediumTypes_), ERR_MARSHALLING,
695             "write mediumTypes failed");
696     }
697 
698     auto remote = Remote();
699     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
700     MessageParcel reply;
701     MessageOption option;
702     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
703         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST), data, reply, option) == 0,
704         ERR_IPC_SEND_REQUEST, "send request failed");
705     int32_t res = AVSESSION_ERROR;
706     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
707 }
708 
StopCast(const SessionToken& sessionToken)709 int32_t AVSessionServiceProxy::StopCast(const SessionToken& sessionToken)
710 {
711     MessageParcel data;
712     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
713         "write interface token failed");
714     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
715     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
716     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
717 
718     auto remote = Remote();
719     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
720     MessageParcel reply;
721     MessageOption option;
722     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
723         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_CAST), data, reply, option) == 0,
724         ERR_IPC_SEND_REQUEST, "send request failed");
725     int32_t res = AVSESSION_ERROR;
726     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
727 }
728 #endif
729 } // namespace OHOS::AVSession
730