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 SLOGI("object is nullptr");
38 return nullptr;
39 }
40 auto session = iface_cast<AVSessionProxy>(object);
41 if (session == nullptr) {
42 SLOGI("session is nullptr");
43 return nullptr;
44 }
45 return std::shared_ptr<AVSession>(session.GetRefPtr(), [holder = session](const auto*) {});
46 }
47
CreateSessionInner(const std::string & tag,int32_t type,const AppExecFwk::ElementName & elementName)48 sptr<IRemoteObject> AVSessionServiceProxy::CreateSessionInner(const std::string& tag, int32_t type,
49 const AppExecFwk::ElementName& elementName)
50 {
51 MessageParcel data;
52 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), nullptr, "write interface token failed");
53 CHECK_AND_RETURN_RET_LOG(data.WriteString(tag), nullptr, "write tag failed");
54 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(type), nullptr, "write type failed");
55 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&elementName), nullptr, "write bundleName failed");
56
57 auto remote = Remote();
58 CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
59 MessageParcel reply;
60 MessageOption option;
61 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
62 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_SESSION), data, reply, option) == 0,
63 nullptr, "send request failed");
64
65 int32_t res = AVSESSION_ERROR;
66 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(res), nullptr, "read res failed");
67
68 return res == AVSESSION_SUCCESS ? reply.ReadRemoteObject() : nullptr;
69 }
70
GetAllSessionDescriptors(std::vector<AVSessionDescriptor> & descriptors)71 int32_t AVSessionServiceProxy::GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)
72 {
73 MessageParcel data;
74 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
75 "write interface token failed");
76 auto remote = Remote();
77 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
78 MessageParcel reply;
79 MessageOption option;
80 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
81 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_ALL_SESSION_DESCRIPTORS),\
82 data, reply, option) == 0,
83 ERR_IPC_SEND_REQUEST, "send request failed");
84
85 int32_t ret = AVSESSION_ERROR;
86 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
87 if (ret == AVSESSION_SUCCESS) {
88 uint32_t size {};
89 CHECK_AND_RETURN_RET_LOG(reply.ReadUint32(size), ERR_UNMARSHALLING, "read vector size failed");
90 CHECK_AND_RETURN_RET_LOG(size, ret, "size=0");
91
92 std::vector<AVSessionDescriptor> result(size);
93 for (auto& descriptor : result) {
94 CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
95 }
96 descriptors = result;
97 }
98 return ret;
99 }
100
GetSessionDescriptorsBySessionId(const std::string & sessionId,AVSessionDescriptor & descriptor)101 int32_t AVSessionServiceProxy::GetSessionDescriptorsBySessionId(const std::string& sessionId,
102 AVSessionDescriptor& descriptor)
103 {
104 MessageParcel data;
105 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
106 "write interface token failed");
107 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_MARSHALLING, "write sessionId failed");
108
109 auto remote = Remote();
110 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
111 MessageParcel reply;
112 MessageOption option;
113 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
114 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_SESSION_DESCRIPTORS_BY_ID),\
115 data, reply, option) == 0,
116 ERR_IPC_SEND_REQUEST, "send request failed");
117
118 int32_t ret = AVSESSION_ERROR;
119 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
120 if (ret == AVSESSION_SUCCESS) {
121 CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
122 }
123 return ret;
124 }
125
GetHistoricalSessionDescriptors(int32_t maxSize,std::vector<AVSessionDescriptor> & descriptors)126 int32_t AVSessionServiceProxy::GetHistoricalSessionDescriptors(int32_t maxSize,
127 std::vector<AVSessionDescriptor>& descriptors)
128 {
129 MessageParcel data;
130 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
131 "write interface token failed");
132 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(maxSize), ERR_MARSHALLING, "write maxSize failed");
133
134 auto remote = Remote();
135 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
136 MessageParcel reply;
137 MessageOption option;
138 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
139 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_HISTORY_SESSION_DESCRIPTORS),\
140 data, reply, option) == 0,
141 ERR_IPC_SEND_REQUEST, "send request failed");
142
143 int32_t ret = AVSESSION_ERROR;
144 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
145 if (ret == AVSESSION_SUCCESS) {
146 uint32_t size {};
147 CHECK_AND_RETURN_RET_LOG(reply.ReadUint32(size), ERR_UNMARSHALLING, "read vector size failed");
148 CHECK_AND_RETURN_RET_LOG(size, ret, "size=0");
149
150 std::vector<AVSessionDescriptor> result(size);
151 for (auto& descriptor : result) {
152 CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
153 }
154 descriptors = result;
155 }
156 return ret;
157 }
158
UnMarshallingAVQueueInfos(MessageParcel & reply,std::vector<AVQueueInfo> & avQueueInfos)159 void AVSessionServiceProxy::UnMarshallingAVQueueInfos(MessageParcel &reply, std::vector<AVQueueInfo>& avQueueInfos)
160 {
161 uint32_t size {};
162 CHECK_AND_RETURN_LOG(reply.ReadUint32(size), "UnMarshallingAVQueueInfos size failed");
163 CHECK_AND_RETURN_LOG(size, "UnMarshallingAVQueueInfos size=0");
164
165 for (uint32_t i = 0; i < size; i++) {
166 AVQueueInfo avQueueInfo;
167 avQueueInfo.SetBundleName(reply.ReadString());
168 avQueueInfo.SetAVQueueName(reply.ReadString());
169 avQueueInfo.SetAVQueueId(reply.ReadString());
170 avQueueInfo.SetAVQueueImageUri(reply.ReadString());
171 avQueueInfo.SetAVQueueLength(reply.ReadUint32());
172 avQueueInfos.push_back(avQueueInfo);
173 }
174 }
175
BufferToAVQueueInfoImg(const char * buffer,std::vector<AVQueueInfo> & avQueueInfos)176 void AVSessionServiceProxy::BufferToAVQueueInfoImg(const char *buffer, std::vector<AVQueueInfo>& avQueueInfos)
177 {
178 int k = 0;
179 for (auto& avQueueInfo : avQueueInfos) {
180 auto pixelMap = new (std::nothrow) AVSessionPixelMap();
181 std::vector<uint8_t> imgBuffer;
182 int avQueueLength = avQueueInfo.GetAVQueueLength();
183 for (int i = 0; i < avQueueLength; i++, k++) {
184 imgBuffer.push_back((uint8_t)buffer[k]);
185 }
186 pixelMap->SetInnerImgBuffer(imgBuffer);
187 avQueueInfo.SetAVQueueImage(std::shared_ptr<AVSessionPixelMap>(pixelMap));
188 }
189 }
190
GetHistoricalAVQueueInfos(int32_t maxSize,int32_t maxAppSize,std::vector<AVQueueInfo> & avQueueInfos)191 int32_t AVSessionServiceProxy::GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
192 std::vector<AVQueueInfo>& avQueueInfos)
193 {
194 MessageParcel data;
195 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
196 "write interface token failed");
197 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(maxSize), ERR_MARSHALLING, "write maxSize failed");
198 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(maxAppSize), ERR_MARSHALLING, "write maxAppSize failed");
199
200 auto remote = Remote();
201 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
202 MessageParcel reply;
203 MessageOption option;
204 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
205 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_HISTORY_AVQUEUE_INFOS),\
206 data, reply, option) == 0,
207 ERR_IPC_SEND_REQUEST, "send request failed");
208
209 int32_t ret = AVSESSION_ERROR;
210 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
211 if (ret != AVSESSION_SUCCESS) {
212 return ret;
213 }
214 int bufferLength = reply.ReadInt32();
215 if (bufferLength == 0) {
216 uint32_t size {};
217 CHECK_AND_RETURN_RET_LOG(reply.ReadUint32(size), ERR_UNMARSHALLING, "read vector size failed");
218 CHECK_AND_RETURN_RET_LOG(size, ret, "size=0");
219
220 std::vector<AVQueueInfo> result(size);
221 for (auto& avqueueInfo : result) {
222 CHECK_AND_RETURN_RET_LOG(avqueueInfo.Unmarshalling(reply), ERR_UNMARSHALLING, "read avqueueInfo failed");
223 }
224 avQueueInfos = result;
225 return ret;
226 }
227 UnMarshallingAVQueueInfos(reply, avQueueInfos);
228 const char *buffer = nullptr;
229 if ((buffer = reinterpret_cast<const char *>(reply.ReadRawData(bufferLength))) == nullptr) {
230 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
231 SLOGE("read raw data failed, length = %{public}d", bufferLength);
232 return AVSESSION_ERROR;
233 }
234 BufferToAVQueueInfoImg(buffer, avQueueInfos);
235 return AVSESSION_SUCCESS;
236 }
237
StartAVPlayback(const std::string & bundleName,const std::string & assetId)238 int32_t AVSessionServiceProxy::StartAVPlayback(const std::string& bundleName, const std::string& assetId)
239 {
240 MessageParcel data;
241 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
242 "write interface token failed");
243 CHECK_AND_RETURN_RET_LOG(data.WriteString(bundleName), ERR_MARSHALLING, "write bundleName failed");
244 CHECK_AND_RETURN_RET_LOG(data.WriteString(assetId), ERR_MARSHALLING, "write assetId failed");
245
246 auto remote = Remote();
247 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
248 MessageParcel reply;
249 MessageOption option;
250 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
251 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_AV_PLAYBACK),\
252 data, reply, option) == 0,
253 ERR_IPC_SEND_REQUEST, "send request failed");
254
255 int32_t ret = AVSESSION_ERROR;
256 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
257 return ret;
258 }
259
CreateController(const std::string & sessionId,std::shared_ptr<AVSessionController> & controller)260 int32_t AVSessionServiceProxy::CreateController(const std::string& sessionId,
261 std::shared_ptr<AVSessionController>& controller)
262 {
263 std::lock_guard lockGuard(createControllerMutex_);
264 SLOGI("create controller in");
265 sptr<IRemoteObject> object;
266 auto ret = AVSessionServiceProxy::CreateControllerInner(sessionId, object);
267 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
268
269 auto controllerObject = iface_cast<AVSessionControllerProxy>(object);
270 CHECK_AND_RETURN_RET_LOG(controllerObject, AVSESSION_ERROR, "controllerObject is nullptr");
271
272 controller = std::shared_ptr<AVSessionController>(controllerObject.GetRefPtr(),
273 [holder = controllerObject](const auto*) {});
274 return ret;
275 }
276
CreateControllerInner(const std::string & sessionId,sptr<IRemoteObject> & object)277 int32_t AVSessionServiceProxy::CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
278 {
279 MessageParcel data;
280 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
281 "write interface token failed");
282 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
283
284 auto remote = Remote();
285 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
286 MessageParcel reply;
287 MessageOption option;
288 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
289 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_CONTROLLER), data, reply, option) == 0,
290 ERR_IPC_SEND_REQUEST, "send request failed");
291 int32_t ret = AVSESSION_ERROR;
292 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
293 if (ret == AVSESSION_SUCCESS) {
294 object = reply.ReadRemoteObject();
295 }
296 return ret;
297 }
298
299 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastController(const std::string & sessionId,std::shared_ptr<AVCastController> & castController)300 int32_t AVSessionServiceProxy::GetAVCastController(const std::string& sessionId,
301 std::shared_ptr<AVCastController>& castController)
302 {
303 sptr<IRemoteObject> object;
304 auto ret = AVSessionServiceProxy::GetAVCastControllerInner(sessionId, object);
305 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
306
307 auto castControllerObject = iface_cast<AVCastControllerProxy>(object);
308 CHECK_AND_RETURN_RET_LOG(castControllerObject, AVSESSION_ERROR, "castControllerObject is nullptr");
309
310 castController = std::shared_ptr<AVCastController>(castControllerObject.GetRefPtr(),
311 [holder = castControllerObject](const auto*) {});
312 return ret;
313 }
314
GetAVCastControllerInner(const std::string & sessionId,sptr<IRemoteObject> & object)315 int32_t AVSessionServiceProxy::GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
316 {
317 MessageParcel data;
318 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
319 "write interface token failed");
320 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
321
322 auto remote = Remote();
323 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
324 MessageParcel reply;
325 MessageOption option;
326 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
327 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_AV_CAST_CONTROLLER),\
328 data, reply, option) == 0,
329 ERR_IPC_SEND_REQUEST, "send request failed");
330 int32_t ret = AVSESSION_ERROR;
331 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
332 if (ret == AVSESSION_SUCCESS) {
333 object = reply.ReadRemoteObject();
334 }
335 return ret;
336 }
337 #endif
338
RegisterSessionListener(const sptr<ISessionListener> & listener)339 int32_t AVSessionServiceProxy::RegisterSessionListener(const sptr<ISessionListener>& listener)
340 {
341 MessageParcel data;
342 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
343 "write interface token failed");
344 CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(listener->AsObject()), ERR_MARSHALLING, "write tag failed");
345
346 auto remote = Remote();
347 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
348 MessageParcel reply;
349 MessageOption option;
350 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
351 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_SESSION_LISTENER),\
352 data, reply, option) == 0,
353 ERR_IPC_SEND_REQUEST, "send request failed");
354 int32_t res = AVSESSION_ERROR;
355 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
356 }
357
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent)358 int32_t AVSessionServiceProxy::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
359 {
360 MessageParcel data;
361 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
362 "write interface token failed");
363 SLOGI("try SendSystemAVKeyEvent with key=%{public}d", keyEvent.GetKeyCode());
364 CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(data), ERR_MARSHALLING, "write keyEvent failed");
365
366 auto remote = Remote();
367 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
368 MessageParcel reply;
369 MessageOption option;
370 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
371 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SEND_SYSTEM_AV_KEY_EVENT),\
372 data, reply, option) == 0,
373 ERR_IPC_SEND_REQUEST, "send request failed");
374 int32_t res = AVSESSION_ERROR;
375 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
376 }
377
SendSystemControlCommand(const AVControlCommand & command)378 int32_t AVSessionServiceProxy::SendSystemControlCommand(const AVControlCommand& command)
379 {
380 MessageParcel data;
381 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
382 "write interface token failed");
383 SLOGI("try SendSystemControlCommand with cmd=%{public}d", command.GetCommand());
384 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&command), ERR_MARSHALLING, "write keyEvent failed");
385
386 auto remote = Remote();
387 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
388 MessageParcel reply;
389 MessageOption option;
390 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
391 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SEND_SYSTEM_CONTROL_COMMAND),\
392 data, reply, option) == 0,
393 ERR_IPC_SEND_REQUEST, "send request failed");
394 int32_t res = AVSESSION_ERROR;
395 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
396 }
397
RegisterClientDeathObserver(const sptr<IClientDeath> & observer)398 int32_t AVSessionServiceProxy::RegisterClientDeathObserver(const sptr<IClientDeath>& observer)
399 {
400 MessageParcel data;
401 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
402 "write interface token failed");
403 CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(observer->AsObject()), ERR_MARSHALLING, "write observer failed");
404
405 auto remote = Remote();
406 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
407 MessageParcel reply;
408 MessageOption option;
409 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
410 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_CLIENT_DEATH),\
411 data, reply, option) == 0,
412 ERR_IPC_SEND_REQUEST, "send request failed");
413 int32_t res = AVSESSION_ERROR;
414 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
415 }
416
Close(void)417 int32_t AVSessionServiceProxy::Close(void)
418 {
419 MessageParcel data;
420 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
421 "write interface token failed");
422
423 auto remote = Remote();
424 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
425 MessageParcel reply;
426 MessageOption option;
427 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
428 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CLOSE),\
429 data, reply, option) == 0,
430 ERR_IPC_SEND_REQUEST, "send request failed");
431 int32_t res = AVSESSION_ERROR;
432 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
433 }
434
CastAudio(const SessionToken & token,const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)435 int32_t AVSessionServiceProxy::CastAudio(const SessionToken& token,
436 const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
437 {
438 MessageParcel data;
439 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
440 "write interface token failed");
441 CHECK_AND_RETURN_RET_LOG(data.WriteString(token.sessionId), ERR_MARSHALLING, "write sessionId failed");
442 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.pid), ERR_MARSHALLING, "write pid failed");
443 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.uid), ERR_MARSHALLING, "write uid failed");
444 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
445 "write descriptors size failed");
446 for (auto descriptor : descriptors) {
447 SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
448 static_cast<int32_t>(descriptor.deviceRole_));
449 CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
450 }
451
452 auto remote = Remote();
453 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
454 MessageParcel reply;
455 MessageOption option;
456 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
457 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO), data, reply, option) == 0,
458 ERR_IPC_SEND_REQUEST, "send request failed");
459 int32_t res = AVSESSION_ERROR;
460 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
461 }
462
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)463 int32_t AVSessionServiceProxy::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
464 {
465 MessageParcel data;
466 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
467 "write interface token failed");
468 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
469 "write descriptors size failed");
470 for (auto descriptor : descriptors) {
471 SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
472 static_cast<int32_t>(descriptor.deviceRole_));
473 CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
474 }
475
476 auto remote = Remote();
477 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
478 MessageParcel reply;
479 MessageOption option;
480 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
481 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO_FOR_ALL), data, reply, option) == 0,
482 ERR_IPC_SEND_REQUEST, "send request failed");
483 int32_t res = AVSESSION_ERROR;
484 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
485 }
486
487 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
StartCastDiscovery(const int32_t castDeviceCapability)488 int32_t AVSessionServiceProxy::StartCastDiscovery(const int32_t castDeviceCapability)
489 {
490 MessageParcel data;
491 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
492 "write interface token failed");
493 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(castDeviceCapability),
494 ERR_MARSHALLING, "write castDeviceCapability failed");
495
496 auto remote = Remote();
497 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
498 MessageParcel reply;
499 MessageOption option;
500 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
501 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST_DISCOVERY),\
502 data, reply, option) == 0,
503 ERR_IPC_SEND_REQUEST, "send request failed");
504 int32_t res = AVSESSION_ERROR;
505 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
506 }
507
StopCastDiscovery()508 int32_t AVSessionServiceProxy::StopCastDiscovery()
509 {
510 MessageParcel data;
511 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
512 "write interface token 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_STOP_CAST_DISCOVERY), data, reply, option) == 0,
520 ERR_IPC_SEND_REQUEST, "send request failed");
521 int32_t res = AVSESSION_ERROR;
522 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
523 }
524
SetDiscoverable(const bool enable)525 int32_t AVSessionServiceProxy::SetDiscoverable(const bool enable)
526 {
527 MessageParcel data;
528 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
529 "write interface token failed");
530 CHECK_AND_RETURN_RET_LOG(data.WriteBool(enable), ERR_MARSHALLING, "write enable failed");
531
532 auto remote = Remote();
533 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
534 MessageParcel reply;
535 MessageOption option;
536 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
537 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SET_DISCOVERYABLE), data, reply, option) == 0,
538 ERR_IPC_SEND_REQUEST, "send request failed");
539 int32_t res = AVSESSION_ERROR;
540 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
541 }
542
StartCast(const SessionToken & sessionToken,const OutputDeviceInfo & outputDeviceInfo)543 int32_t AVSessionServiceProxy::StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)
544 {
545 MessageParcel data;
546 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
547 "write interface token failed");
548 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
549 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
550 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
551
552 int32_t deviceInfoSize = static_cast<int32_t>(outputDeviceInfo.deviceInfos_.size());
553 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfoSize), ERR_MARSHALLING, "write deviceInfoSize failed");
554 for (const auto& deviceInfo : outputDeviceInfo.deviceInfos_) {
555 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.castCategory_),
556 ERR_MARSHALLING, "write castCategory failed");
557 CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceId_), ERR_MARSHALLING, "write deviceId failed");
558 CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceName_), ERR_MARSHALLING, "write deviceName failed");
559 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.deviceType_), ERR_MARSHALLING, "write deviceType failed");
560 CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.ipAddress_), ERR_MARSHALLING, "write ipAddress failed");
561 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.providerId_), ERR_MARSHALLING, "write providerId failed");
562 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.supportedProtocols_), ERR_MARSHALLING,
563 "write supportedProtocols failed");
564 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.authenticationStatus_), ERR_MARSHALLING,
565 "write authenticationStatus failed");
566 }
567
568 auto remote = Remote();
569 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
570 MessageParcel reply;
571 MessageOption option;
572 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
573 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST), data, reply, option) == 0,
574 ERR_IPC_SEND_REQUEST, "send request failed");
575 int32_t res = AVSESSION_ERROR;
576 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
577 }
578
StopCast(const SessionToken & sessionToken)579 int32_t AVSessionServiceProxy::StopCast(const SessionToken& sessionToken)
580 {
581 MessageParcel data;
582 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
583 "write interface token failed");
584 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
585 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
586 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
587
588 auto remote = Remote();
589 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
590 MessageParcel reply;
591 MessageOption option;
592 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
593 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_CAST), data, reply, option) == 0,
594 ERR_IPC_SEND_REQUEST, "send request failed");
595 int32_t res = AVSESSION_ERROR;
596 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
597 }
598 #endif
599 } // namespace OHOS::AVSession
600