1 /*
2  * Copyright (c) 2022-2024 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         auto pixelMap = new (std::nothrow) 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(std::shared_ptr<AVSessionPixelMap>(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 
CreateController(const std::string & sessionId,std::shared_ptr<AVSessionController> & controller)295 int32_t AVSessionServiceProxy::CreateController(const std::string& sessionId,
296     std::shared_ptr<AVSessionController>& controller)
297 {
298     std::lock_guard lockGuard(createControllerMutex_);
299     SLOGI("create controller in");
300     sptr<IRemoteObject> object;
301     auto ret = AVSessionServiceProxy::CreateControllerInner(sessionId, object);
302     CHECK_AND_RETURN_RET_LOG((ret == AVSESSION_SUCCESS || ret == ERR_CONTROLLER_IS_EXIST),
303         ret, "CreateControllerInner failed");
304     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
305     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
306     auto controllerObject = iface_cast<AVSessionControllerProxy>(object);
307     CHECK_AND_RETURN_RET_LOG(controllerObject, AVSESSION_ERROR, "controllerObject is nullptr");
308 
309     controller = std::shared_ptr<AVSessionController>(controllerObject.GetRefPtr(),
310         [holder = controllerObject](const auto*) {});
311     return ret;
312 }
313 
CreateControllerInner(const std::string & sessionId,sptr<IRemoteObject> & object)314 int32_t AVSessionServiceProxy::CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
315 {
316     MessageParcel data;
317     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
318                              "write interface token failed");
319     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
320 
321     auto remote = Remote();
322     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
323     MessageParcel reply;
324     MessageOption option;
325     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
326         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_CONTROLLER), data, reply, option) == 0,
327         ERR_IPC_SEND_REQUEST, "send request failed");
328     int32_t ret = AVSESSION_ERROR;
329     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
330     if (ret == AVSESSION_SUCCESS || ret == ERR_CONTROLLER_IS_EXIST) {
331         object = reply.ReadRemoteObject();
332     }
333     return ret;
334 }
335 
336 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastController(const std::string & sessionId,std::shared_ptr<AVCastController> & castController)337 int32_t AVSessionServiceProxy::GetAVCastController(const std::string& sessionId,
338     std::shared_ptr<AVCastController>& castController)
339 {
340     sptr<IRemoteObject> object;
341     auto ret = AVSessionServiceProxy::GetAVCastControllerInner(sessionId, object);
342     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
343     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
344     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
345 
346     auto castControllerObject = iface_cast<AVCastControllerProxy>(object);
347     CHECK_AND_RETURN_RET_LOG(castControllerObject, AVSESSION_ERROR, "castControllerObject is nullptr");
348 
349     castController = std::shared_ptr<AVCastController>(castControllerObject.GetRefPtr(),
350         [holder = castControllerObject](const auto*) {});
351     return ret;
352 }
353 
GetAVCastControllerInner(const std::string & sessionId,sptr<IRemoteObject> & object)354 int32_t AVSessionServiceProxy::GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
355 {
356     MessageParcel data;
357     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
358         "write interface token failed");
359     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
360 
361     auto remote = Remote();
362     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
363     MessageParcel reply;
364     MessageOption option;
365     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
366         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_AV_CAST_CONTROLLER),\
367         data, reply, option) == 0,
368         ERR_IPC_SEND_REQUEST, "send request failed");
369     int32_t ret = AVSESSION_ERROR;
370     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
371     CHECK_AND_RETURN_RET_LOG(ret != ERR_NO_PERMISSION, ret, "no permission");
372     CHECK_AND_RETURN_RET_LOG(ret != ERR_PERMISSION_DENIED, ret, "permission denied");
373     if (ret == AVSESSION_SUCCESS) {
374         object = reply.ReadRemoteObject();
375     }
376     return ret;
377 }
378 #endif
379 
RegisterSessionListener(const sptr<ISessionListener> & listener)380 int32_t AVSessionServiceProxy::RegisterSessionListener(const sptr<ISessionListener>& listener)
381 {
382     MessageParcel data;
383     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
384                              "write interface token failed in RegisterSessionListener");
385     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(listener->AsObject()), ERR_MARSHALLING, "write tag failed");
386 
387     auto remote = Remote();
388     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
389     MessageParcel reply;
390     MessageOption option;
391     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
392         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_SESSION_LISTENER),\
393         data, reply, option) == 0,
394         ERR_IPC_SEND_REQUEST, "send request failed");
395     int32_t res = AVSESSION_ERROR;
396     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
397 }
398 
RegisterSessionListenerForAllUsers(const sptr<ISessionListener> & listener)399 int32_t AVSessionServiceProxy::RegisterSessionListenerForAllUsers(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 RegisterSessionListenerForAllUsers");
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_FOR_ALL_USERS),\
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 
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent)418 int32_t AVSessionServiceProxy::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
419 {
420     MessageParcel data;
421     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
422                              "write interface token failed");
423     SLOGI("try SendSystemAVKeyEvent with key=%{public}d", keyEvent.GetKeyCode());
424     CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(data), ERR_MARSHALLING, "write keyEvent failed");
425 
426     auto remote = Remote();
427     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
428     MessageParcel reply;
429     MessageOption option;
430     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
431         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SEND_SYSTEM_AV_KEY_EVENT),\
432         data, reply, option) == 0,
433         ERR_IPC_SEND_REQUEST, "send request failed");
434     int32_t res = AVSESSION_ERROR;
435     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
436 }
437 
SendSystemControlCommand(const AVControlCommand & command)438 int32_t AVSessionServiceProxy::SendSystemControlCommand(const AVControlCommand& command)
439 {
440     MessageParcel data;
441     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
442                              "write interface token failed");
443     SLOGI("try SendSystemControlCommand with cmd=%{public}d", command.GetCommand());
444     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&command), ERR_MARSHALLING, "write keyEvent failed");
445 
446     auto remote = Remote();
447     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
448     MessageParcel reply;
449     MessageOption option;
450     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
451         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SEND_SYSTEM_CONTROL_COMMAND),\
452         data, reply, option) == 0,
453         ERR_IPC_SEND_REQUEST, "send request failed");
454     int32_t res = AVSESSION_ERROR;
455     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
456 }
457 
RegisterClientDeathObserver(const sptr<IClientDeath> & observer)458 int32_t AVSessionServiceProxy::RegisterClientDeathObserver(const sptr<IClientDeath>& observer)
459 {
460     MessageParcel data;
461     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
462                              "write interface token failed");
463     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(observer->AsObject()), ERR_MARSHALLING, "write observer 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_REGISTER_CLIENT_DEATH),\
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 
Close(void)477 int32_t AVSessionServiceProxy::Close(void)
478 {
479     MessageParcel data;
480     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
481                              "write interface token failed");
482 
483     auto remote = Remote();
484     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
485     MessageParcel reply;
486     MessageOption option;
487     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
488         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CLOSE),\
489         data, reply, option) == 0,
490         ERR_IPC_SEND_REQUEST, "send request failed");
491     int32_t res = AVSESSION_ERROR;
492     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
493 }
494 
CastAudio(const SessionToken & token,const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)495 int32_t AVSessionServiceProxy::CastAudio(const SessionToken& token,
496                                          const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
497 {
498     MessageParcel data;
499     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
500                              "write interface token failed");
501     CHECK_AND_RETURN_RET_LOG(data.WriteString(token.sessionId), ERR_MARSHALLING, "write sessionId failed");
502     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.pid), ERR_MARSHALLING, "write pid failed");
503     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.uid), ERR_MARSHALLING, "write uid failed");
504     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
505                              "write descriptors size failed");
506     for (auto descriptor : descriptors) {
507         SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
508               static_cast<int32_t>(descriptor.deviceRole_));
509         CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
510     }
511 
512     auto remote = Remote();
513     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
514     MessageParcel reply;
515     MessageOption option;
516     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
517         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO), data, reply, option) == 0,
518         ERR_IPC_SEND_REQUEST, "send request failed");
519     int32_t res = AVSESSION_ERROR;
520     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
521 }
522 
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)523 int32_t AVSessionServiceProxy::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
524 {
525     MessageParcel data;
526     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
527                              "write interface token failed");
528     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
529                              "write descriptors size failed");
530     for (auto descriptor : descriptors) {
531         SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
532               static_cast<int32_t>(descriptor.deviceRole_));
533         CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
534     }
535 
536     auto remote = Remote();
537     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
538     MessageParcel reply;
539     MessageOption option;
540     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
541         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO_FOR_ALL), data, reply, option) == 0,
542         ERR_IPC_SEND_REQUEST, "send request failed");
543     int32_t res = AVSESSION_ERROR;
544     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
545 }
546 
547 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
StartCastDiscovery(int32_t castDeviceCapability,std::vector<std::string> drmSchemes)548 int32_t AVSessionServiceProxy::StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes)
549 {
550     MessageParcel data;
551     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
552         "write interface token failed");
553     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(castDeviceCapability),
554         ERR_MARSHALLING, "write castDeviceCapability failed");
555     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(drmSchemes.size()), ERR_MARSHALLING, "write drmSchemes size failed");
556     for (auto drmScheme : drmSchemes) {
557         CHECK_AND_RETURN_RET_LOG(data.WriteString(drmScheme), ERR_MARSHALLING, "write drmScheme failed");
558     }
559     auto remote = Remote();
560     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
561     MessageParcel reply;
562     MessageOption option;
563     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
564         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST_DISCOVERY),\
565         data, reply, option) == 0,
566         ERR_IPC_SEND_REQUEST, "send request failed");
567     int32_t res = AVSESSION_ERROR;
568     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
569 }
570 
StopCastDiscovery()571 int32_t AVSessionServiceProxy::StopCastDiscovery()
572 {
573     MessageParcel data;
574     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
575         "write interface token failed");
576 
577     auto remote = Remote();
578     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
579     MessageParcel reply;
580     MessageOption option;
581     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
582         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_CAST_DISCOVERY), data, reply, option) == 0,
583         ERR_IPC_SEND_REQUEST, "send request failed");
584     int32_t res = AVSESSION_ERROR;
585     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
586 }
587 
SetDiscoverable(const bool enable)588 int32_t AVSessionServiceProxy::SetDiscoverable(const bool enable)
589 {
590     MessageParcel data;
591     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
592         "write interface token failed");
593     CHECK_AND_RETURN_RET_LOG(data.WriteBool(enable), ERR_MARSHALLING, "write enable failed");
594 
595     auto remote = Remote();
596     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
597     MessageParcel reply;
598     MessageOption option;
599     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
600         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SET_DISCOVERYABLE), data, reply, option) == 0,
601         ERR_IPC_SEND_REQUEST, "send request failed");
602     int32_t res = AVSESSION_ERROR;
603     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
604 }
605 
StartDeviceLogging(int32_t fd,uint32_t maxSize)606 int32_t AVSessionServiceProxy::StartDeviceLogging(int32_t fd, uint32_t maxSize)
607 {
608     MessageParcel data;
609     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
610         "write interface token failed");
611     CHECK_AND_RETURN_RET_LOG(data.WriteFileDescriptor(fd), ERR_MARSHALLING, "write fd failed");
612     CHECK_AND_RETURN_RET_LOG(data.WriteUint32(maxSize), ERR_MARSHALLING, "write maxSize failed");
613     auto remote = Remote();
614     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
615     MessageParcel reply;
616     MessageOption option;
617     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
618         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_DEVICE_LOGGING),\
619         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 
StopDeviceLogging()625 int32_t AVSessionServiceProxy::StopDeviceLogging()
626 {
627     MessageParcel data;
628     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
629         "write interface token failed");
630 
631     auto remote = Remote();
632     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
633     MessageParcel reply;
634     MessageOption option;
635     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
636         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_DEVICE_LOGGING), data, reply, option) == 0,
637         ERR_IPC_SEND_REQUEST, "send request failed");
638     int32_t res = AVSESSION_ERROR;
639     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
640 }
641 
StartCast(const SessionToken & sessionToken,const OutputDeviceInfo & outputDeviceInfo)642 int32_t AVSessionServiceProxy::StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)
643 {
644     MessageParcel data;
645     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
646         "write interface token failed");
647     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
648     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
649     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
650 
651     int32_t deviceInfoSize = static_cast<int32_t>(outputDeviceInfo.deviceInfos_.size());
652     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfoSize), ERR_MARSHALLING, "write deviceInfoSize failed");
653     for (const DeviceInfo& deviceInfo : outputDeviceInfo.deviceInfos_) {
654         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.castCategory_),
655             ERR_MARSHALLING, "write castCategory failed");
656         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceId_), ERR_MARSHALLING, "write deviceId failed");
657         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceName_), ERR_MARSHALLING, "write deviceName failed");
658         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.deviceType_), ERR_MARSHALLING, "write deviceType failed");
659         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.ipAddress_), ERR_MARSHALLING, "write ipAddress failed");
660         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.manufacturer_),
661             ERR_MARSHALLING, "write manufacturer failed");
662         CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.modelName_), ERR_MARSHALLING, "write modelName failed");
663         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.providerId_), ERR_MARSHALLING, "write providerId failed");
664         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.supportedProtocols_), ERR_MARSHALLING,
665             "write supportedProtocols failed");
666         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.authenticationStatus_), ERR_MARSHALLING,
667             "write authenticationStatus failed");
668         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.supportedDrmCapabilities_.size()), ERR_MARSHALLING,
669             "write supportedDrmCapabilities size failed");
670         for (auto supportedDrmCapability : deviceInfo.supportedDrmCapabilities_) {
671             CHECK_AND_RETURN_RET_LOG(data.WriteString(supportedDrmCapability), ERR_MARSHALLING,
672                 "write supportedDrmCapability failed");
673         }
674         CHECK_AND_RETURN_RET_LOG(data.WriteBool(deviceInfo.isLegacy_), ERR_MARSHALLING, "write isLegacy failed");
675         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.mediumTypes_), ERR_MARSHALLING,
676             "write mediumTypes failed");
677     }
678 
679     auto remote = Remote();
680     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
681     MessageParcel reply;
682     MessageOption option;
683     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
684         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST), data, reply, option) == 0,
685         ERR_IPC_SEND_REQUEST, "send request failed");
686     int32_t res = AVSESSION_ERROR;
687     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
688 }
689 
StopCast(const SessionToken & sessionToken)690 int32_t AVSessionServiceProxy::StopCast(const SessionToken& sessionToken)
691 {
692     MessageParcel data;
693     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
694         "write interface token failed");
695     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
696     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
697     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
698 
699     auto remote = Remote();
700     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
701     MessageParcel reply;
702     MessageOption option;
703     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
704         static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_CAST), data, reply, option) == 0,
705         ERR_IPC_SEND_REQUEST, "send request failed");
706     int32_t res = AVSESSION_ERROR;
707     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
708 }
709 #endif
710 } // namespace OHOS::AVSession
711