• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdio>
17 
18 #include "av_router.h"
19 #include "avsession_log.h"
20 #include "avsession_errors.h"
21 #include "session_listener_proxy.h"
22 #include "client_death_proxy.h"
23 #include "avsession_trace.h"
24 #include "avsession_sysevent.h"
25 #include "parameter.h"
26 #include "parameters.h"
27 #include "avsession_service_stub.h"
28 
29 using namespace OHOS::AudioStandard;
30 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)31 bool AVSessionServiceStub::CheckInterfaceToken(MessageParcel& data)
32 {
33     auto localDescriptor = IAVSessionService::GetDescriptor();
34     auto remoteDescriptor = data.ReadInterfaceToken();
35     if (remoteDescriptor != localDescriptor) {
36         SLOGI("interface token is not equal");
37         return false;
38     }
39     return true;
40 }
41 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int32_t AVSessionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
43                                               MessageOption& option)
44 {
45     if (!CheckInterfaceToken(data)) {
46         return AVSESSION_ERROR;
47     }
48     if (code < static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_MAX)) {
49         return (this->*handlers[code])(data, reply);
50     }
51     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52 }
53 
HandleCreateSessionInner(MessageParcel & data,MessageParcel & reply)54 int32_t AVSessionServiceStub::HandleCreateSessionInner(MessageParcel& data, MessageParcel& reply)
55 {
56     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateSessionInner");
57     auto sessionTag = data.ReadString();
58     auto sessionType = data.ReadInt32();
59     sptr elementName = data.ReadParcelable<AppExecFwk::ElementName>();
60     if (elementName == nullptr) {
61         SLOGI("read element name failed");
62         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
63         return ERR_NONE;
64     }
65     auto session = CreateSessionInner(sessionTag, sessionType, *elementName);
66     if (session == nullptr) {
67         SLOGI("session is nullptr");
68         reply.WriteInt32(ERR_UNMARSHALLING);
69         return ERR_NONE;
70     }
71     reply.WriteInt32(AVSESSION_SUCCESS);
72     reply.WriteRemoteObject(session);
73     return ERR_NONE;
74 }
75 
HandleGetAllSessionDescriptors(MessageParcel & data,MessageParcel & reply)76 int32_t AVSessionServiceStub::HandleGetAllSessionDescriptors(MessageParcel& data, MessageParcel& reply)
77 {
78     std::vector<AVSessionDescriptor> descriptors;
79     int32_t ret = GetAllSessionDescriptors(descriptors);
80     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
81     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
82     for (const auto& descriptor : descriptors) {
83         if (!descriptor.WriteToParcel(reply)) {
84             SLOGI("write descriptor failed");
85             break;
86         }
87     }
88     return ERR_NONE;
89 }
90 
HandleGetSessionDescriptorsById(MessageParcel & data,MessageParcel & reply)91 int32_t AVSessionServiceStub::HandleGetSessionDescriptorsById(MessageParcel& data, MessageParcel& reply)
92 {
93     AVSessionDescriptor descriptor;
94     int32_t ret = GetSessionDescriptorsBySessionId(data.ReadString(), descriptor);
95     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
96     if (ret == AVSESSION_SUCCESS) {
97         CHECK_AND_PRINT_LOG(descriptor.WriteToParcel(reply), "write AVSessionDescriptor failed");
98     }
99     return ERR_NONE;
100 }
101 
HandleGetHistoricalSessionDescriptors(MessageParcel & data,MessageParcel & reply)102 int32_t AVSessionServiceStub::HandleGetHistoricalSessionDescriptors(MessageParcel& data, MessageParcel& reply)
103 {
104     std::vector<AVSessionDescriptor> descriptors;
105     int32_t ret = GetHistoricalSessionDescriptors(data.ReadInt32(), descriptors);
106     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
107     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
108     for (const auto& descriptor : descriptors) {
109         if (!descriptor.WriteToParcel(reply)) {
110             SLOGI("write descriptor failed");
111             break;
112         }
113     }
114     return ERR_NONE;
115 }
116 
GetAVQueueInfosImgLength(std::vector<AVQueueInfo> & avQueueInfos)117 int32_t AVSessionServiceStub::GetAVQueueInfosImgLength(std::vector<AVQueueInfo>& avQueueInfos)
118 {
119     int sumLength = 0;
120     for (auto& avQueueInfo : avQueueInfos) {
121         int avQueueImgLen = 0;
122         std::shared_ptr<AVSessionPixelMap> pixelMap = avQueueInfo.GetAVQueueImage();
123         if (pixelMap != nullptr) {
124             avQueueImgLen = (pixelMap->GetInnerImgBuffer()).size();
125         }
126         avQueueInfo.SetAVQueueLength(avQueueImgLen);
127         sumLength += avQueueImgLen;
128     }
129     return sumLength;
130 }
131 
132 
MarshallingAVQueueInfos(MessageParcel & reply,const std::vector<AVQueueInfo> & avQueueInfos)133 void AVSessionServiceStub::MarshallingAVQueueInfos(MessageParcel &reply, const std::vector<AVQueueInfo>& avQueueInfos)
134 {
135     CHECK_AND_RETURN_LOG(reply.WriteUint32(avQueueInfos.size()), "MarshallingAVQueueInfos size failed");
136     for (const auto& avQueueInfo : avQueueInfos) {
137         reply.WriteString(avQueueInfo.GetBundleName());
138         reply.WriteString(avQueueInfo.GetAVQueueName());
139         reply.WriteString(avQueueInfo.GetAVQueueId());
140         reply.WriteString(avQueueInfo.GetAVQueueImageUri());
141         reply.WriteUint32(avQueueInfo.GetAVQueueLength());
142     }
143 }
144 
AVQueueInfoImgToBuffer(std::vector<AVQueueInfo> & avQueueInfos,unsigned char * buffer)145 void AVSessionServiceStub::AVQueueInfoImgToBuffer(std::vector<AVQueueInfo>& avQueueInfos, unsigned char *buffer)
146 {
147     int k = 0;
148     for (auto& avQueueInfo : avQueueInfos) {
149         std::shared_ptr<AVSessionPixelMap> pixelMap = avQueueInfo.GetAVQueueImage();
150         if (pixelMap != nullptr) {
151             std::vector<uint8_t> imgBuffer = pixelMap->GetInnerImgBuffer();
152             int length = avQueueInfo.GetAVQueueLength();
153             for (int i = 0; i< length; i++, k++) {
154                 buffer[k] = imgBuffer[i];
155             }
156         }
157     }
158 }
159 
HandleGetHistoricalAVQueueInfos(MessageParcel & data,MessageParcel & reply)160 int32_t AVSessionServiceStub::HandleGetHistoricalAVQueueInfos(MessageParcel& data, MessageParcel& reply)
161 {
162     std::vector<AVQueueInfo> avQueueInfos;
163     auto maxSize = data.ReadInt32();
164     auto maxAppSize = data.ReadInt32();
165     int32_t ret = GetHistoricalAVQueueInfos(maxSize, maxAppSize, avQueueInfos);
166     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
167 
168     int bufferLength = GetAVQueueInfosImgLength(avQueueInfos);
169     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(bufferLength), ERR_NONE, "write buffer length failed");
170     if (bufferLength == 0) {
171         CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(avQueueInfos.size()), ERR_NONE, "write size failed");
172         for (const auto& avQueueInfo : avQueueInfos) {
173             if (!avQueueInfo.Marshalling(reply)) {
174                 SLOGE("write avQueueInfo failed");
175                 break;
176             }
177         }
178         return ERR_NONE;
179     }
180 
181     unsigned char *buffer = new (std::nothrow) unsigned char[bufferLength];
182     if (buffer == nullptr) {
183         SLOGE("new buffer failed of length = %{public}d", bufferLength);
184         return AVSESSION_ERROR;
185     }
186 
187     MarshallingAVQueueInfos(reply, avQueueInfos);
188     AVQueueInfoImgToBuffer(avQueueInfos, buffer);
189     if (!reply.WriteRawData(buffer, bufferLength)) {
190         SLOGE("fail to write parcel");
191         delete[] buffer;
192         return AVSESSION_ERROR;
193     }
194 
195     delete[] buffer;
196     return ERR_NONE;
197 }
198 
HandleStartAVPlayback(MessageParcel & data,MessageParcel & reply)199 int32_t AVSessionServiceStub::HandleStartAVPlayback(MessageParcel& data, MessageParcel& reply)
200 {
201     std::string bundleName = data.ReadString();
202     std::string asserId = data.ReadString();
203     int32_t ret = StartAVPlayback(bundleName, asserId);
204     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
205     return ERR_NONE;
206 }
207 
HandleCreateControllerInner(MessageParcel & data,MessageParcel & reply)208 int32_t AVSessionServiceStub::HandleCreateControllerInner(MessageParcel& data, MessageParcel& reply)
209 {
210     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateControllerInner");
211     sptr<IRemoteObject> object;
212     int32_t ret = CreateControllerInner(data.ReadString(), object);
213     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
214     if (ret == AVSESSION_SUCCESS || ret == ERR_CONTROLLER_IS_EXIST) {
215         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
216     }
217     return ERR_NONE;
218 }
219 
HandleGetAVCastControllerInner(MessageParcel & data,MessageParcel & reply)220 int32_t AVSessionServiceStub::HandleGetAVCastControllerInner(MessageParcel& data, MessageParcel& reply)
221 {
222 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
223     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleGetAVCastControllerInner");
224     sptr<IRemoteObject> object;
225     int32_t ret = GetAVCastControllerInner(data.ReadString(), object);
226     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
227     if (ret == AVSESSION_SUCCESS) {
228         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
229     }
230 #else
231     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
232 #endif
233     return ERR_NONE;
234 }
235 
HandleRegisterSessionListener(MessageParcel & data,MessageParcel & reply)236 int32_t AVSessionServiceStub::HandleRegisterSessionListener(MessageParcel& data, MessageParcel& reply)
237 {
238     auto remoteObject = data.ReadRemoteObject();
239     if (remoteObject == nullptr) {
240         SLOGI("read remote object failed");
241         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
242         return ERR_NONE;
243     }
244     auto listener = iface_cast<SessionListenerProxy>(remoteObject);
245     if (listener == nullptr) {
246         SLOGI("iface_cast remote object failed");
247         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
248         return ERR_NONE;
249     }
250     if (!reply.WriteInt32(RegisterSessionListener(listener))) {
251         SLOGI("reply write int32 failed");
252     }
253     return ERR_NONE;
254 }
255 
HandleSendSystemAVKeyEvent(MessageParcel & data,MessageParcel & reply)256 int32_t AVSessionServiceStub::HandleSendSystemAVKeyEvent(MessageParcel& data, MessageParcel& reply)
257 {
258     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemAVKeyEvent");
259     auto keyEvent = MMI::KeyEvent::Create();
260     if (keyEvent == nullptr) {
261         SLOGI("create keyEvent failed");
262         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_NO_MEMORY), ERR_NONE, "write int32 failed");
263         return ERR_NONE;
264     }
265     if (!keyEvent->ReadFromParcel(data)) {
266         SLOGI("read keyEvent failed");
267         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
268         return ERR_NONE;
269     }
270     if (!keyEvent->IsValid()) {
271         SLOGI("keyEvent is not valid");
272         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
273         return ERR_NONE;
274     }
275     if (!reply.WriteInt32(SendSystemAVKeyEvent(*keyEvent))) {
276         SLOGI("reply write int32 failed");
277     }
278     return ERR_NONE;
279 }
280 
HandleSendSystemControlCommand(MessageParcel & data,MessageParcel & reply)281 int32_t AVSessionServiceStub::HandleSendSystemControlCommand(MessageParcel& data, MessageParcel& reply)
282 {
283     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemControlCommand");
284     sptr command = data.ReadParcelable<AVControlCommand>();
285     if (command == nullptr) {
286         SLOGI("read command failed");
287         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
288         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "READ_PARCELABLE_FAILED",
289             "ERROR_INFO", "handle send system control command read command failed");
290         return ERR_NONE;
291     }
292     if (!reply.WriteInt32(SendSystemControlCommand(*command))) {
293         SLOGI("reply write int32 failed");
294     }
295     return ERR_NONE;
296 }
297 
HandleRegisterClientDeathObserver(MessageParcel & data,MessageParcel & reply)298 int32_t AVSessionServiceStub::HandleRegisterClientDeathObserver(MessageParcel& data, MessageParcel& reply)
299 {
300     auto remoteObject = data.ReadRemoteObject();
301     if (remoteObject == nullptr) {
302         SLOGI("read remote object failed");
303         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
304         return ERR_NONE;
305     }
306     auto clientDeathObserver = iface_cast<ClientDeathProxy>(remoteObject);
307     if (clientDeathObserver == nullptr) {
308         SLOGI("iface_cast remote object failed");
309         reply.WriteInt32(ERR_INVALID_PARAM);
310         return ERR_NONE;
311     }
312     int32_t ret = RegisterClientDeathObserver(clientDeathObserver);
313     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "register clientDeathObserver failed");
314     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
315     return ERR_NONE;
316 }
317 
HandleClose(MessageParcel & data,MessageParcel & reply)318 int32_t AVSessionServiceStub::HandleClose(MessageParcel& data, MessageParcel& reply)
319 {
320     int32_t ret = Close();
321     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Close failed");
322     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
323     return ERR_NONE;
324 }
325 
HandleCastAudio(MessageParcel & data,MessageParcel & reply)326 int32_t AVSessionServiceStub::HandleCastAudio(MessageParcel& data, MessageParcel& reply)
327 {
328     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudio");
329     SLOGI("start");
330     SessionToken token {};
331     token.sessionId = data.ReadString();
332     token.pid = data.ReadInt32();
333     token.uid = data.ReadInt32();
334     int32_t deviceNum = data.ReadInt32();
335     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
336         SLOGI("receive deviceNum over range");
337         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
338         return ERR_NONE;
339     }
340 
341     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors;
342     for (int i = 0; i < deviceNum; i++) {
343         auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
344         if (audioDeviceDescriptor == nullptr) {
345             SLOGI("read AudioDeviceDescriptor failed");
346             CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
347             return ERR_NONE;
348         }
349         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
350               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
351         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
352     }
353     int32_t ret = CastAudio(token, sinkAudioDescriptors);
354     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudio failed");
355     SLOGI("CastAudio ret %{public}d", ret);
356     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
357     SLOGI("success");
358     return ERR_NONE;
359 }
360 
HandleCastAudioForAll(MessageParcel & data,MessageParcel & reply)361 int32_t AVSessionServiceStub::HandleCastAudioForAll(MessageParcel& data, MessageParcel& reply)
362 {
363     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudioForAll");
364     SLOGI("start");
365     int32_t deviceNum = data.ReadInt32();
366     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
367         SLOGI("receive deviceNum over range");
368         reply.WriteInt32(ERR_INVALID_PARAM);
369         return ERR_NONE;
370     }
371 
372     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors {};
373     for (int i = 0; i < deviceNum; i++) {
374         auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
375         if (audioDeviceDescriptor == nullptr) {
376             SLOGI("read AudioDeviceDescriptor failed");
377             reply.WriteInt32(ERR_UNMARSHALLING);
378             return ERR_NONE;
379         }
380         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
381               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
382         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
383     }
384     int32_t ret = CastAudioForAll(sinkAudioDescriptors);
385     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudioForAll failed");
386     SLOGI("CastAudioForAll ret %{public}d", ret);
387     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
388     return ERR_NONE;
389 }
390 
HandleRemoteCastAudio(MessageParcel & data,MessageParcel & reply)391 int32_t AVSessionServiceStub::HandleRemoteCastAudio(MessageParcel& data, MessageParcel& reply)
392 {
393     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::RemoteCastAudio");
394     SLOGI("start");
395     auto command = static_cast<RemoteServiceCommand>(data.ReadInt32());
396     std::string sessionInfo = data.ReadString();
397     std::string output;
398     int32_t ret = ProcessCastAudioCommand(command, sessionInfo, output);
399     SLOGI("RemoteCastAudio ret %{public}d", ret);
400     if (ret != AVSESSION_SUCCESS) {
401         SLOGI("RemoteCastAudio failed");
402         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
403         return ERR_NONE;
404     }
405     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
406     CHECK_AND_RETURN_RET_LOG(reply.WriteString(output), ERR_NONE, "write int32 failed");
407     return ERR_NONE;
408 }
409 
HandleStartCastDiscovery(MessageParcel & data,MessageParcel & reply)410 int32_t AVSessionServiceStub::HandleStartCastDiscovery(MessageParcel& data, MessageParcel& reply)
411 {
412     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCastDiscovery");
413     SLOGI("HandleStartCastDiscovery start");
414 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
415     auto castDeviceCapability = data.ReadInt32();
416     int32_t ret = AVRouter::GetInstance().StartCastDiscovery(castDeviceCapability);
417     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
418     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartCastDiscovery failed");
419 #else
420     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
421 #endif
422     return ERR_NONE;
423 }
424 
HandleStopCastDiscovery(MessageParcel & data,MessageParcel & reply)425 int32_t AVSessionServiceStub::HandleStopCastDiscovery(MessageParcel& data, MessageParcel& reply)
426 {
427     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCastDiscovery");
428     SLOGI("HandleStopCastDiscovery start");
429 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
430     int32_t ret = AVRouter::GetInstance().StopCastDiscovery();
431     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
432     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopCastDiscovery failed");
433 #else
434     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
435 #endif
436     return ERR_NONE;
437 }
438 
HandleSetDiscoverable(MessageParcel & data,MessageParcel & reply)439 int32_t AVSessionServiceStub::HandleSetDiscoverable(MessageParcel& data, MessageParcel& reply)
440 {
441     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleSetDiscoverable");
442     SLOGI("HandleSetDiscoverable start");
443 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
444     bool enable;
445     CHECK_AND_RETURN_RET_LOG(data.ReadBool(enable), AVSESSION_ERROR, "write enable info failed");
446     checkEnableCast(enable);
447     int32_t ret = AVSESSION_SUCCESS;
448 
449     auto deviceProp = system::GetParameter("const.product.devicetype", "default");
450     SLOGI("GetDeviceType, deviceProp=%{public}s", deviceProp.c_str());
451     int32_t is2in1 = strcmp(deviceProp.c_str(), "2in1");
452     if (enable && is2in1 == 0) {
453         ret = AVRouter::GetInstance().SetDiscoverable(enable);
454     }
455 
456     AVRouter::GetInstance().SetDiscoverable(enable);
457     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
458     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleSetDiscoverable failed");
459 #else
460     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
461 #endif
462     return ERR_NONE;
463 }
464 
HandleStartCast(MessageParcel & data,MessageParcel & reply)465 int32_t AVSessionServiceStub::HandleStartCast(MessageParcel& data, MessageParcel& reply)
466 {
467 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
468     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCast");
469     SessionToken sessionToken {};
470     sessionToken.sessionId = data.ReadString();
471     sessionToken.pid = data.ReadInt32();
472     sessionToken.uid = data.ReadInt32();
473 
474     OutputDeviceInfo outputDeviceInfo;
475     int32_t deviceInfoSize;
476     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
477 
478     if (deviceInfoSize > RECEIVE_DEVICE_NUM_MAX) {
479         SLOGI("receive deviceNum over range");
480         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
481         return ERR_NONE;
482     }
483     for (int i = 0; i < deviceInfoSize; i++) {
484         DeviceInfo deviceInfo;
485         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
486         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
487         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
488         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
489         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
490         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
491         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
492             "Read supportedProtocols failed");
493         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
494             "Read authenticationStatus failed");
495         outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
496     }
497 
498     int32_t ret = StartCast(sessionToken, outputDeviceInfo);
499     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StartCast failed");
500     SLOGI("StartCast ret %{public}d", ret);
501     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
502     SLOGI("HandleStartCast success");
503 #else
504     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
505 #endif
506     return ERR_NONE;
507 }
508 
HandleStopCast(MessageParcel & data,MessageParcel & reply)509 int32_t AVSessionServiceStub::HandleStopCast(MessageParcel& data, MessageParcel& reply)
510 {
511 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
512     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCast");
513     SLOGI("HandleStopCast start");
514     SessionToken sessionToken {};
515     sessionToken.sessionId = data.ReadString();
516     sessionToken.pid = data.ReadInt32();
517     sessionToken.uid = data.ReadInt32();
518 
519     int32_t ret = StopCast(sessionToken);
520     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StopCast failed");
521     SLOGI("StopCast ret %{public}d", ret);
522     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
523     SLOGI("HandleStopCast success");
524 #else
525     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
526 #endif
527     return ERR_NONE;
528 }
529 } // namespace OHOS::AVSession
530