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