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
CreateController(const std::string & sessionId,std::shared_ptr<AVSessionController> & controller)159 int32_t AVSessionServiceProxy::CreateController(const std::string& sessionId,
160 std::shared_ptr<AVSessionController>& controller)
161 {
162 sptr<IRemoteObject> object;
163 auto ret = AVSessionServiceProxy::CreateControllerInner(sessionId, object);
164 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
165
166 auto controllerObject = iface_cast<AVSessionControllerProxy>(object);
167 CHECK_AND_RETURN_RET_LOG(controllerObject, AVSESSION_ERROR, "controllerObject is nullptr");
168
169 controller = std::shared_ptr<AVSessionController>(controllerObject.GetRefPtr(),
170 [holder = controllerObject](const auto*) {});
171 return ret;
172 }
173
CreateControllerInner(const std::string & sessionId,sptr<IRemoteObject> & object)174 int32_t AVSessionServiceProxy::CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
175 {
176 MessageParcel data;
177 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
178 "write interface token failed");
179 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
180
181 auto remote = Remote();
182 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
183 MessageParcel reply;
184 MessageOption option;
185 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
186 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_CONTROLLER), data, reply, option) == 0,
187 ERR_IPC_SEND_REQUEST, "send request failed");
188 int32_t ret = AVSESSION_ERROR;
189 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
190 if (ret == AVSESSION_SUCCESS) {
191 object = reply.ReadRemoteObject();
192 }
193 return ret;
194 }
195
196 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastController(const std::string & sessionId,std::shared_ptr<AVCastController> & castController)197 int32_t AVSessionServiceProxy::GetAVCastController(const std::string& sessionId,
198 std::shared_ptr<AVCastController>& castController)
199 {
200 sptr<IRemoteObject> object;
201 auto ret = AVSessionServiceProxy::GetAVCastControllerInner(sessionId, object);
202 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
203
204 auto castControllerObject = iface_cast<AVCastControllerProxy>(object);
205 CHECK_AND_RETURN_RET_LOG(castControllerObject, AVSESSION_ERROR, "castControllerObject is nullptr");
206
207 castController = std::shared_ptr<AVCastController>(castControllerObject.GetRefPtr(),
208 [holder = castControllerObject](const auto*) {});
209 return ret;
210 }
211
GetAVCastControllerInner(const std::string & sessionId,sptr<IRemoteObject> & object)212 int32_t AVSessionServiceProxy::GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
213 {
214 MessageParcel data;
215 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
216 "write interface token failed");
217 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
218
219 auto remote = Remote();
220 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
221 MessageParcel reply;
222 MessageOption option;
223 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
224 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_GET_AV_CAST_CONTROLLER),\
225 data, reply, option) == 0,
226 ERR_IPC_SEND_REQUEST, "send request failed");
227 int32_t ret = AVSESSION_ERROR;
228 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
229 if (ret == AVSESSION_SUCCESS) {
230 object = reply.ReadRemoteObject();
231 }
232 return ret;
233 }
234 #endif
235
RegisterSessionListener(const sptr<ISessionListener> & listener)236 int32_t AVSessionServiceProxy::RegisterSessionListener(const sptr<ISessionListener>& listener)
237 {
238 MessageParcel data;
239 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
240 "write interface token failed");
241 CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(listener->AsObject()), ERR_MARSHALLING, "write tag failed");
242
243 auto remote = Remote();
244 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
245 MessageParcel reply;
246 MessageOption option;
247 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
248 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_SESSION_LISTENER),\
249 data, reply, option) == 0,
250 ERR_IPC_SEND_REQUEST, "send request failed");
251 int32_t res = AVSESSION_ERROR;
252 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
253 }
254
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent)255 int32_t AVSessionServiceProxy::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
256 {
257 MessageParcel data;
258 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
259 "write interface token failed");
260 CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(data), ERR_MARSHALLING, "write keyEvent failed");
261
262 auto remote = Remote();
263 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
264 MessageParcel reply;
265 MessageOption option;
266 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
267 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SEND_SYSTEM_AV_KEY_EVENT),\
268 data, reply, option) == 0,
269 ERR_IPC_SEND_REQUEST, "send request failed");
270 int32_t res = AVSESSION_ERROR;
271 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
272 }
273
SendSystemControlCommand(const AVControlCommand & command)274 int32_t AVSessionServiceProxy::SendSystemControlCommand(const AVControlCommand& command)
275 {
276 MessageParcel data;
277 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
278 "write interface token failed");
279 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&command), ERR_MARSHALLING, "write keyEvent 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_SEND_SYSTEM_CONTROL_COMMAND),\
287 data, reply, option) == 0,
288 ERR_IPC_SEND_REQUEST, "send request failed");
289 int32_t res = AVSESSION_ERROR;
290 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
291 }
292
RegisterClientDeathObserver(const sptr<IClientDeath> & observer)293 int32_t AVSessionServiceProxy::RegisterClientDeathObserver(const sptr<IClientDeath>& observer)
294 {
295 MessageParcel data;
296 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
297 "write interface token failed");
298 CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(observer->AsObject()), ERR_MARSHALLING, "write observer failed");
299
300 auto remote = Remote();
301 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
302 MessageParcel reply;
303 MessageOption option;
304 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
305 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_REGISTER_CLIENT_DEATH),\
306 data, reply, option) == 0,
307 ERR_IPC_SEND_REQUEST, "send request failed");
308 int32_t res = AVSESSION_ERROR;
309 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
310 }
311
CastAudio(const SessionToken & token,const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)312 int32_t AVSessionServiceProxy::CastAudio(const SessionToken& token,
313 const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
314 {
315 MessageParcel data;
316 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
317 "write interface token failed");
318 CHECK_AND_RETURN_RET_LOG(data.WriteString(token.sessionId), ERR_MARSHALLING, "write sessionId failed");
319 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.pid), ERR_MARSHALLING, "write pid failed");
320 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.uid), ERR_MARSHALLING, "write uid failed");
321 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
322 "write descriptors size failed");
323 for (auto descriptor : descriptors) {
324 SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
325 static_cast<int32_t>(descriptor.deviceRole_));
326 CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
327 }
328
329 auto remote = Remote();
330 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
331 MessageParcel reply;
332 MessageOption option;
333 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
334 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO), data, reply, option) == 0,
335 ERR_IPC_SEND_REQUEST, "send request failed");
336 int32_t res = AVSESSION_ERROR;
337 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
338 }
339
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)340 int32_t AVSessionServiceProxy::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
341 {
342 MessageParcel data;
343 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
344 "write interface token failed");
345 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
346 "write descriptors size failed");
347 for (auto descriptor : descriptors) {
348 SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
349 static_cast<int32_t>(descriptor.deviceRole_));
350 CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
351 }
352
353 auto remote = Remote();
354 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
355 MessageParcel reply;
356 MessageOption option;
357 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
358 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CAST_AUDIO_FOR_ALL), data, reply, option) == 0,
359 ERR_IPC_SEND_REQUEST, "send request failed");
360 int32_t res = AVSESSION_ERROR;
361 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
362 }
363
364 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
StartCastDiscovery(const int32_t castDeviceCapability)365 int32_t AVSessionServiceProxy::StartCastDiscovery(const int32_t castDeviceCapability)
366 {
367 MessageParcel data;
368 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
369 "write interface token failed");
370 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(castDeviceCapability),
371 ERR_MARSHALLING, "write castDeviceCapability failed");
372
373 auto remote = Remote();
374 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
375 MessageParcel reply;
376 MessageOption option;
377 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
378 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST_DISCOVERY),\
379 data, reply, option) == 0,
380 ERR_IPC_SEND_REQUEST, "send request failed");
381 int32_t res = AVSESSION_ERROR;
382 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
383 }
384
StopCastDiscovery()385 int32_t AVSessionServiceProxy::StopCastDiscovery()
386 {
387 MessageParcel data;
388 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
389 "write interface token failed");
390
391 auto remote = Remote();
392 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
393 MessageParcel reply;
394 MessageOption option;
395 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
396 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_CAST_DISCOVERY), data, reply, option) == 0,
397 ERR_IPC_SEND_REQUEST, "send request failed");
398 int32_t res = AVSESSION_ERROR;
399 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
400 }
401
SetDiscoverable(const bool enable)402 int32_t AVSessionServiceProxy::SetDiscoverable(const bool enable)
403 {
404 MessageParcel data;
405 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
406 "write interface token failed");
407 CHECK_AND_RETURN_RET_LOG(data.WriteBool(enable), ERR_MARSHALLING, "write enable failed");
408
409 auto remote = Remote();
410 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
411 MessageParcel reply;
412 MessageOption option;
413 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
414 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_SET_DISCOVERYABLE), data, reply, option) == 0,
415 ERR_IPC_SEND_REQUEST, "send request failed");
416 int32_t res = AVSESSION_ERROR;
417 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
418 }
419
StartCast(const SessionToken & sessionToken,const OutputDeviceInfo & outputDeviceInfo)420 int32_t AVSessionServiceProxy::StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo)
421 {
422 MessageParcel data;
423 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
424 "write interface token failed");
425 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
426 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
427 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
428
429 int32_t deviceInfoSize = static_cast<int32_t>(outputDeviceInfo.deviceInfos_.size());
430 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfoSize), ERR_MARSHALLING, "write deviceInfoSize failed");
431 for (const auto& deviceInfo : outputDeviceInfo.deviceInfos_) {
432 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.castCategory_),
433 ERR_MARSHALLING, "write castCategory failed");
434 CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceId_), ERR_MARSHALLING, "write deviceId failed");
435 CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.deviceName_), ERR_MARSHALLING, "write deviceName failed");
436 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.deviceType_), ERR_MARSHALLING, "write deviceType failed");
437 CHECK_AND_RETURN_RET_LOG(data.WriteString(deviceInfo.ipAddress_), ERR_MARSHALLING, "write ipAddress failed");
438 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(deviceInfo.providerId_), ERR_MARSHALLING, "write providerId failed");
439 }
440
441 auto remote = Remote();
442 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
443 MessageParcel reply;
444 MessageOption option;
445 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
446 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST), data, reply, option) == 0,
447 ERR_IPC_SEND_REQUEST, "send request failed");
448 int32_t res = AVSESSION_ERROR;
449 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
450 }
451
StopCast(const SessionToken & sessionToken)452 int32_t AVSessionServiceProxy::StopCast(const SessionToken& sessionToken)
453 {
454 MessageParcel data;
455 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
456 "write interface token failed");
457 CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionToken.sessionId), ERR_MARSHALLING, "write sessionId failed");
458 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.pid), ERR_MARSHALLING, "write pid failed");
459 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(sessionToken.uid), ERR_MARSHALLING, "write uid failed");
460
461 auto remote = Remote();
462 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
463 MessageParcel reply;
464 MessageOption option;
465 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(
466 static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_STOP_CAST), data, reply, option) == 0,
467 ERR_IPC_SEND_REQUEST, "send request failed");
468 int32_t res = AVSESSION_ERROR;
469 return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
470 }
471 #endif
472 } // namespace OHOS::AVSession
473