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 <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 #include "session_xcollie.h"
29 #include "permission_checker.h"
30 
31 using namespace OHOS::AudioStandard;
32 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)33 bool AVSessionServiceStub::CheckInterfaceToken(MessageParcel& data)
34 {
35     auto localDescriptor = IAVSessionService::GetDescriptor();
36     auto remoteDescriptor = data.ReadInterfaceToken();
37     if (remoteDescriptor != localDescriptor) {
38         SLOGI("interface token is not equal");
39         return false;
40     }
41     return true;
42 }
43 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t AVSessionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
45                                               MessageOption& option)
46 {
47     if (code >= static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_SESSION) &&
48         code < static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_MAX) &&
49         code != static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_START_CAST)) {
50         SessionXCollie sessionXCollie(mapCodeToFuncNameXCollie[code]);
51     }
52     if (!CheckInterfaceToken(data)) {
53         return AVSESSION_ERROR;
54     }
55     if (code >= static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_CREATE_SESSION) &&
56         code < static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_MAX)) {
57         return handlers[code](data, reply);
58     }
59     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 
HandleCreateSessionInner(MessageParcel & data,MessageParcel & reply)62 int32_t AVSessionServiceStub::HandleCreateSessionInner(MessageParcel& data, MessageParcel& reply)
63 {
64     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateSessionInner");
65     auto sessionTag = data.ReadString();
66     auto sessionType = data.ReadInt32();
67     sptr elementName = data.ReadParcelable<AppExecFwk::ElementName>();
68     if (elementName == nullptr) {
69         SLOGI("read element name failed");
70         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
71         return ERR_NONE;
72     }
73     sptr<IRemoteObject> object;
74     auto ret = CreateSessionInner(sessionTag, sessionType, *elementName, object);
75     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
76     if (ret == AVSESSION_SUCCESS) {
77         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
78     }
79     return ERR_NONE;
80 }
81 
HandleGetAllSessionDescriptors(MessageParcel & data,MessageParcel & reply)82 int32_t AVSessionServiceStub::HandleGetAllSessionDescriptors(MessageParcel& data, MessageParcel& reply)
83 {
84     int32_t err = PermissionChecker::GetInstance().CheckPermission(
85         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
86     if (err != ERR_NONE) {
87         SLOGE("GetAllSessionDescriptors: CheckPermission failed");
88         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
89             "ERROR_MSG", "avsessionservice getallsessiondescriptors checkpermission failed");
90         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
91         return ERR_NONE;
92     }
93     std::vector<AVSessionDescriptor> descriptors;
94     int32_t ret = GetAllSessionDescriptors(descriptors);
95     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
96     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
97     for (const auto& descriptor : descriptors) {
98         if (!descriptor.WriteToParcel(reply)) {
99             SLOGI("write descriptor failed");
100             break;
101         }
102     }
103     return ERR_NONE;
104 }
105 
HandleGetSessionDescriptorsById(MessageParcel & data,MessageParcel & reply)106 int32_t AVSessionServiceStub::HandleGetSessionDescriptorsById(MessageParcel& data, MessageParcel& reply)
107 {
108     AVSessionDescriptor descriptor;
109     int32_t ret = GetSessionDescriptorsBySessionId(data.ReadString(), descriptor);
110     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
111     if (ret == AVSESSION_SUCCESS) {
112         CHECK_AND_PRINT_LOG(descriptor.WriteToParcel(reply), "write AVSessionDescriptor failed");
113     }
114     return ERR_NONE;
115 }
116 
HandleGetHistoricalSessionDescriptors(MessageParcel & data,MessageParcel & reply)117 int32_t AVSessionServiceStub::HandleGetHistoricalSessionDescriptors(MessageParcel& data, MessageParcel& reply)
118 {
119     int32_t err = PermissionChecker::GetInstance().CheckPermission(
120         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
121         if (err != ERR_NONE) {
122         SLOGE("GetHistoricalSessionDescriptors: CheckPermission failed");
123         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
124             "ERROR_MSG", "avsessionservice getHistoricalSessionDescriptors checkpermission failed");
125         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
126         return ERR_NONE;
127     }
128     std::vector<AVSessionDescriptor> descriptors;
129     int32_t ret = GetHistoricalSessionDescriptors(data.ReadInt32(), descriptors);
130     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
131     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
132     for (const auto& descriptor : descriptors) {
133         if (!descriptor.WriteToParcel(reply)) {
134             SLOGI("write descriptor failed");
135             break;
136         }
137     }
138     return ERR_NONE;
139 }
140 
GetAVQueueInfosImgLength(std::vector<AVQueueInfo> & avQueueInfos)141 int32_t AVSessionServiceStub::GetAVQueueInfosImgLength(std::vector<AVQueueInfo>& avQueueInfos)
142 {
143     int sumLength = 0;
144     for (auto& avQueueInfo : avQueueInfos) {
145         int avQueueImgLen = 0;
146         std::shared_ptr<AVSessionPixelMap> pixelMap = avQueueInfo.GetAVQueueImage();
147         if (pixelMap != nullptr) {
148             avQueueImgLen = static_cast<int>((pixelMap->GetInnerImgBuffer()).size());
149         }
150         avQueueInfo.SetAVQueueLength(avQueueImgLen);
151         sumLength += avQueueImgLen;
152     }
153     return sumLength;
154 }
155 
156 
MarshallingAVQueueInfos(MessageParcel & reply,const std::vector<AVQueueInfo> & avQueueInfos)157 void AVSessionServiceStub::MarshallingAVQueueInfos(MessageParcel &reply, const std::vector<AVQueueInfo>& avQueueInfos)
158 {
159     CHECK_AND_RETURN_LOG(reply.WriteUint32(avQueueInfos.size()), "MarshallingAVQueueInfos size failed");
160     for (const auto& avQueueInfo : avQueueInfos) {
161         reply.WriteString(avQueueInfo.GetBundleName());
162         reply.WriteString(avQueueInfo.GetAVQueueName());
163         reply.WriteString(avQueueInfo.GetAVQueueId());
164         reply.WriteString(avQueueInfo.GetAVQueueImageUri());
165         reply.WriteUint32(avQueueInfo.GetAVQueueLength());
166     }
167 }
168 
AVQueueInfoImgToBuffer(std::vector<AVQueueInfo> & avQueueInfos,unsigned char * buffer)169 void AVSessionServiceStub::AVQueueInfoImgToBuffer(std::vector<AVQueueInfo>& avQueueInfos, unsigned char *buffer)
170 {
171     int k = 0;
172     for (auto& avQueueInfo : avQueueInfos) {
173         std::shared_ptr<AVSessionPixelMap> pixelMap = avQueueInfo.GetAVQueueImage();
174         if (pixelMap != nullptr) {
175             std::vector<uint8_t> imgBuffer = pixelMap->GetInnerImgBuffer();
176             int length = avQueueInfo.GetAVQueueLength();
177             for (int i = 0; i< length; i++, k++) {
178                 buffer[k] = imgBuffer[i];
179             }
180         }
181     }
182 }
183 
HandleGetHistoricalAVQueueInfos(MessageParcel & data,MessageParcel & reply)184 int32_t AVSessionServiceStub::HandleGetHistoricalAVQueueInfos(MessageParcel& data, MessageParcel& reply)
185 {
186     int32_t err = PermissionChecker::GetInstance().CheckPermission(
187         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
188     if (err != ERR_NONE) {
189         SLOGE("GetHistoricalAVQueueInfos: CheckPermission failed");
190         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
191             "ERROR_MSG", "avsessionservice GetHistoricalAVQueueInfos checkpermission failed");
192         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
193         return ERR_NONE;
194     }
195     std::vector<AVQueueInfo> avQueueInfos;
196     auto maxSize = data.ReadInt32();
197     auto maxAppSize = data.ReadInt32();
198     int32_t ret = GetHistoricalAVQueueInfos(maxSize, maxAppSize, avQueueInfos);
199     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
200 
201     int bufferLength = GetAVQueueInfosImgLength(avQueueInfos);
202     CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(bufferLength), ERR_NONE, "write buffer length failed");
203     if (bufferLength == 0) {
204         CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(avQueueInfos.size()), ERR_NONE, "write size failed");
205         for (const auto& avQueueInfo : avQueueInfos) {
206             if (!avQueueInfo.Marshalling(reply)) {
207                 SLOGE("write avQueueInfo failed");
208                 break;
209             }
210         }
211         return ERR_NONE;
212     }
213 
214     unsigned char *buffer = new (std::nothrow) unsigned char[bufferLength];
215     if (buffer == nullptr) {
216         SLOGE("new buffer failed of length = %{public}d", bufferLength);
217         return AVSESSION_ERROR;
218     }
219 
220     MarshallingAVQueueInfos(reply, avQueueInfos);
221     AVQueueInfoImgToBuffer(avQueueInfos, buffer);
222     if (!reply.WriteRawData(buffer, bufferLength)) {
223         SLOGE("fail to write parcel");
224         delete[] buffer;
225         return AVSESSION_ERROR;
226     }
227 
228     delete[] buffer;
229     return ERR_NONE;
230 }
231 
HandleStartAVPlayback(MessageParcel & data,MessageParcel & reply)232 int32_t AVSessionServiceStub::HandleStartAVPlayback(MessageParcel& data, MessageParcel& reply)
233 {
234     int32_t err = PermissionChecker::GetInstance().CheckPermission(
235         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
236     if (err != ERR_NONE) {
237         SLOGE("StartAVPlayback: CheckPermission failed");
238         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
239             "ERROR_MSG", "avsessionservice StartAVPlayback checkpermission failed");
240         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
241         return ERR_NONE;
242     }
243     std::string bundleName = data.ReadString();
244     std::string asserId = data.ReadString();
245     int32_t ret = StartAVPlayback(bundleName, asserId);
246     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
247     return ERR_NONE;
248 }
249 
HandleCreateControllerInner(MessageParcel & data,MessageParcel & reply)250 int32_t AVSessionServiceStub::HandleCreateControllerInner(MessageParcel& data, MessageParcel& reply)
251 {
252     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateControllerInner");
253     int32_t err = PermissionChecker::GetInstance().CheckPermission(
254         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
255     if (err != ERR_NONE) {
256         SLOGE("CreateControllerInner: CheckPermission failed");
257         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(),
258             "CALLER_PID", GetCallingPid(), "SESSION_ID", data.ReadString(),
259             "ERROR_MSG", "avsessionservice createcontrollerinner checkpermission failed");
260         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
261         return ERR_NONE;
262     }
263     sptr<IRemoteObject> object;
264     int32_t ret = CreateControllerInner(data.ReadString(), object);
265     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
266     if (ret == AVSESSION_SUCCESS || ret == ERR_CONTROLLER_IS_EXIST) {
267         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
268     }
269     return ERR_NONE;
270 }
271 
HandleGetAVCastControllerInner(MessageParcel & data,MessageParcel & reply)272 int32_t AVSessionServiceStub::HandleGetAVCastControllerInner(MessageParcel& data, MessageParcel& reply)
273 {
274 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
275     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleGetAVCastControllerInner");
276     int32_t err = PermissionChecker::GetInstance().CheckPermission(
277         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
278     if (err != ERR_NONE) {
279         SLOGE("GetAVCastControllerInner: CheckPermission failed");
280         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
281         return ERR_NONE;
282     }
283     sptr<IRemoteObject> object;
284     int32_t ret = GetAVCastControllerInner(data.ReadString(), object);
285     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
286     if (ret == AVSESSION_SUCCESS) {
287         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
288     }
289 #else
290     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
291 #endif
292     return ERR_NONE;
293 }
294 
HandleRegisterSessionListener(MessageParcel & data,MessageParcel & reply)295 int32_t AVSessionServiceStub::HandleRegisterSessionListener(MessageParcel& data, MessageParcel& reply)
296 {
297     int32_t err = PermissionChecker::GetInstance().CheckPermission(
298         PermissionChecker::CHECK_SYSTEM_PERMISSION);
299     if (err != ERR_NONE) {
300         SLOGE("RegisterSessionListener: CheckPermission failed");
301         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
302             "ERROR_MSG", "avsessionservice registersessionlistener checkpermission failed");
303         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
304         return ERR_NONE;
305     }
306     auto remoteObject = data.ReadRemoteObject();
307     if (remoteObject == nullptr) {
308         SLOGI("read remote object failed");
309         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
310         return ERR_NONE;
311     }
312     auto listener = iface_cast<SessionListenerProxy>(remoteObject);
313     if (listener == nullptr) {
314         SLOGI("RegisterSessionListener but iface_cast remote object failed");
315         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
316         return ERR_NONE;
317     }
318     if (!reply.WriteInt32(RegisterSessionListener(listener))) {
319         SLOGI("reply write int32 failed");
320     }
321     return ERR_NONE;
322 }
323 
HandleRegisterSessionListenerForAllUsers(MessageParcel & data,MessageParcel & reply)324 int32_t AVSessionServiceStub::HandleRegisterSessionListenerForAllUsers(MessageParcel& data, MessageParcel& reply)
325 {
326     int32_t err = PermissionChecker::GetInstance().CheckPermission(
327         PermissionChecker::CHECK_SYSTEM_PERMISSION);
328     if (err != ERR_NONE) {
329         SLOGE("RegisterSessionListenerForAllUsers: CheckPermission failed");
330         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
331             "ERROR_MSG", "avsessionservice RegisterSessionListenerForAllUsers checkpermission failed");
332         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
333         return ERR_NONE;
334     }
335     auto remoteObject = data.ReadRemoteObject();
336     if (remoteObject == nullptr) {
337         SLOGI("read remote object failed");
338         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
339         return ERR_NONE;
340     }
341     auto listener = iface_cast<SessionListenerProxy>(remoteObject);
342     if (listener == nullptr) {
343         SLOGI("RegisterSessionListenerForAllUsers but iface_cast remote object failed");
344         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
345         return ERR_NONE;
346     }
347     if (!reply.WriteInt32(RegisterSessionListenerForAllUsers(listener))) {
348         SLOGI("reply write int32 failed");
349     }
350     return ERR_NONE;
351 }
352 
HandleSendSystemAVKeyEvent(MessageParcel & data,MessageParcel & reply)353 int32_t AVSessionServiceStub::HandleSendSystemAVKeyEvent(MessageParcel& data, MessageParcel& reply)
354 {
355     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemAVKeyEvent");
356     auto keyEvent = MMI::KeyEvent::Create();
357     if (keyEvent == nullptr) {
358         SLOGI("create keyEvent failed");
359         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_NO_MEMORY), ERR_NONE, "write int32 failed");
360         return ERR_NONE;
361     }
362     if (!keyEvent->ReadFromParcel(data)) {
363         SLOGI("read keyEvent failed");
364         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
365         return ERR_NONE;
366     }
367     if (!keyEvent->IsValid()) {
368         SLOGI("keyEvent is not valid");
369         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
370         return ERR_NONE;
371     }
372     int32_t err = PermissionChecker::GetInstance().CheckPermission(
373         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
374     if (err != ERR_NONE) {
375         SLOGE("SendSystemAVKeyEvent: CheckPermission failed");
376         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
377             "KEY_CODE", keyEvent->GetKeyCode(), "KEY_ACTION", keyEvent->GetKeyAction(),
378             "ERROR_MSG", "avsessionservice sendsystemavkeyevent checkpermission failed");
379         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
380         return ERR_NONE;
381     }
382     if (!reply.WriteInt32(SendSystemAVKeyEvent(*keyEvent))) {
383         SLOGI("reply write int32 failed");
384     }
385     return ERR_NONE;
386 }
387 
HandleSendSystemControlCommand(MessageParcel & data,MessageParcel & reply)388 int32_t AVSessionServiceStub::HandleSendSystemControlCommand(MessageParcel& data, MessageParcel& reply)
389 {
390     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemControlCommand");
391     sptr command = data.ReadParcelable<AVControlCommand>();
392     if (command == nullptr) {
393         SLOGI("read command failed");
394         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
395         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "READ_PARCELABLE_FAILED",
396             "ERROR_INFO", "handle send system control command read command failed");
397         return ERR_NONE;
398     }
399     int32_t err = PermissionChecker::GetInstance().CheckPermission(
400         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
401     if (err != ERR_NONE) {
402         SLOGE("SendSystemControlCommand: CheckPermission failed");
403         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(),
404             "CALLER_PID", GetCallingPid(), "CMD", command->GetCommand(),
405             "ERROR_MSG", "avsessionservice sendsystemcontrolcommand checkpermission failed");
406         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
407         return ERR_NONE;
408     }
409     if (!reply.WriteInt32(SendSystemControlCommand(*command))) {
410         SLOGI("reply write int32 failed");
411     }
412     return ERR_NONE;
413 }
414 
HandleRegisterClientDeathObserver(MessageParcel & data,MessageParcel & reply)415 int32_t AVSessionServiceStub::HandleRegisterClientDeathObserver(MessageParcel& data, MessageParcel& reply)
416 {
417     auto remoteObject = data.ReadRemoteObject();
418     if (remoteObject == nullptr) {
419         SLOGI("read remote object failed");
420         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
421         return ERR_NONE;
422     }
423     auto clientDeathObserver = iface_cast<ClientDeathProxy>(remoteObject);
424     if (clientDeathObserver == nullptr) {
425         SLOGI("iface_cast remote object failed");
426         reply.WriteInt32(ERR_INVALID_PARAM);
427         return ERR_NONE;
428     }
429     int32_t ret = RegisterClientDeathObserver(clientDeathObserver);
430     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "register clientDeathObserver failed");
431     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
432     return ERR_NONE;
433 }
434 
HandleClose(MessageParcel & data,MessageParcel & reply)435 int32_t AVSessionServiceStub::HandleClose(MessageParcel& data, MessageParcel& reply)
436 {
437     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
438     if (err != ERR_NONE) {
439         SLOGE("Close: CheckPermission failed");
440         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
441             "ERROR_MSG", "avsessionservice Close checkpermission failed");
442         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
443         return ERR_NONE;
444     }
445     int32_t ret = Close();
446     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Close failed");
447     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
448     return ERR_NONE;
449 }
450 
HandleCastAudio(MessageParcel & data,MessageParcel & reply)451 int32_t AVSessionServiceStub::HandleCastAudio(MessageParcel& data, MessageParcel& reply)
452 {
453     int32_t err = PermissionChecker::GetInstance().CheckPermission(
454         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
455     if (err != ERR_NONE) {
456         SLOGE("CastAudio: CheckPermission failed");
457         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
458             "ERROR_MSG", "avsessionservice CastAudio checkmission failed");
459         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
460         return ERR_NONE;
461     }
462     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudio");
463     SLOGI("start");
464     SessionToken token {};
465     token.sessionId = data.ReadString();
466     token.pid = data.ReadInt32();
467     token.uid = data.ReadInt32();
468     int32_t deviceNum = data.ReadInt32();
469     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
470         SLOGI("receive deviceNum over range");
471         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
472         return ERR_NONE;
473     }
474 
475     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors;
476     for (int i = 0; i < deviceNum; i++) {
477         auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
478         if (audioDeviceDescriptor == nullptr) {
479             SLOGI("read AudioDeviceDescriptor failed");
480             CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
481             return ERR_NONE;
482         }
483         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
484               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
485         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
486     }
487     int32_t ret = CastAudio(token, sinkAudioDescriptors);
488     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudio failed");
489     SLOGI("CastAudio ret %{public}d", ret);
490     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
491     SLOGI("success");
492     return ERR_NONE;
493 }
494 
HandleCastAudioForAll(MessageParcel & data,MessageParcel & reply)495 int32_t AVSessionServiceStub::HandleCastAudioForAll(MessageParcel& data, MessageParcel& reply)
496 {
497     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudioForAll");
498     SLOGI("CastAudioForAll start");
499     int32_t err = PermissionChecker::GetInstance().CheckPermission(
500         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
501     if (err != ERR_NONE) {
502         SLOGE("CastAudioForAll: CheckPermission failed");
503         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
504             "ERROR_MSG", "avsessionservice CastAudioForAll checkpermission failed");
505         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
506         return ERR_NONE;
507     }
508     int32_t deviceNum = data.ReadInt32();
509     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
510         SLOGI("receive deviceNum over range");
511         reply.WriteInt32(ERR_INVALID_PARAM);
512         return ERR_NONE;
513     }
514 
515     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors {};
516     for (int i = 0; i < deviceNum; i++) {
517         auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
518         if (audioDeviceDescriptor == nullptr) {
519             SLOGI("read AudioDeviceDescriptor failed");
520             reply.WriteInt32(ERR_UNMARSHALLING);
521             return ERR_NONE;
522         }
523         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
524               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
525         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
526     }
527     int32_t ret = CastAudioForAll(sinkAudioDescriptors);
528     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudioForAll failed");
529     SLOGI("CastAudioForAll ret %{public}d", ret);
530     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
531     return ERR_NONE;
532 }
533 
HandleRemoteCastAudio(MessageParcel & data,MessageParcel & reply)534 int32_t AVSessionServiceStub::HandleRemoteCastAudio(MessageParcel& data, MessageParcel& reply)
535 {
536     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::RemoteCastAudio");
537     SLOGI("start");
538     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
539     if (err != ERR_NONE) {
540         SLOGE("ProcessCastAudioCommand: CheckPermission failed");
541         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
542             "ERROR_MSG", "avsessionservice ProcessCastAudioCommand checkpermission failed");
543         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
544         return ERR_NONE;
545     }
546     auto command = static_cast<RemoteServiceCommand>(data.ReadInt32());
547     std::string sessionInfo = data.ReadString();
548     std::string output;
549     int32_t ret = ProcessCastAudioCommand(command, sessionInfo, output);
550     SLOGI("RemoteCastAudio ret %{public}d", ret);
551     if (ret != AVSESSION_SUCCESS) {
552         SLOGI("RemoteCastAudio failed");
553         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
554         return ERR_NONE;
555     }
556     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
557     CHECK_AND_RETURN_RET_LOG(reply.WriteString(output), ERR_NONE, "write int32 failed");
558     return ERR_NONE;
559 }
560 
HandleStartCastDiscovery(MessageParcel & data,MessageParcel & reply)561 int32_t AVSessionServiceStub::HandleStartCastDiscovery(MessageParcel& data, MessageParcel& reply)
562 {
563     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCastDiscovery");
564     SLOGI("HandleStartCastDiscovery start");
565     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
566     if (err != ERR_NONE) {
567         SLOGE("StartCastDiscovery: CheckPermission failed");
568         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
569             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
570             "avsessionservice StartCastDiscovery checkPermission failed");
571         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
572         return ERR_NONE;
573     }
574 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
575     auto castDeviceCapability = data.ReadInt32();
576     int32_t drmSchemesLen = data.ReadInt32();
577     std::vector<std::string> drmSchemes;
578     int32_t maxDrmSchemesLen = 10;
579     CHECK_AND_RETURN_RET_LOG((drmSchemesLen >= 0) &&
580         (drmSchemesLen <= maxDrmSchemesLen), ERR_NONE, "drmSchemesLen is illegal");
581     for (int i = 0; i < drmSchemesLen; i++) {
582         std::string drmScheme = data.ReadString();
583         drmSchemes.emplace_back(drmScheme);
584     }
585     int32_t ret = AVRouter::GetInstance().StartCastDiscovery(castDeviceCapability, drmSchemes);
586     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
587     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartCastDiscovery failed");
588 #else
589     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
590 #endif
591     return ERR_NONE;
592 }
593 
HandleStopCastDiscovery(MessageParcel & data,MessageParcel & reply)594 int32_t AVSessionServiceStub::HandleStopCastDiscovery(MessageParcel& data, MessageParcel& reply)
595 {
596     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCastDiscovery");
597     SLOGI("HandleStopCastDiscovery start");
598     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
599     if (err != ERR_NONE) {
600         SLOGE("StopCastDiscovery: CheckPermission failed");
601         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
602             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
603             "avsessionservice StopCastDiscovery checkPermission failed");
604         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
605         return ERR_NONE;
606     }
607 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
608     int32_t ret = AVRouter::GetInstance().StopCastDiscovery();
609     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
610     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopCastDiscovery failed");
611 #else
612     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
613 #endif
614     return ERR_NONE;
615 }
616 
HandleStartDeviceLogging(MessageParcel & data,MessageParcel & reply)617 int32_t AVSessionServiceStub::HandleStartDeviceLogging(MessageParcel& data, MessageParcel& reply)
618 {
619     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartDeviceLogging");
620     SLOGI("HandleStartDeviceLogging start");
621     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
622     if (err != ERR_NONE) {
623         SLOGE("HandleStartDeviceLogging: CheckPermission failed");
624         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
625             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
626             "avsessionservice StartDeviceLogging checkPermission failed");
627         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
628         return ERR_NONE;
629     }
630 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
631     int32_t fd = data.ReadFileDescriptor();
632     uint32_t maxSize = data.ReadUint32();
633     int32_t ret = AVRouter::GetInstance().StartDeviceLogging(fd, maxSize);
634     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
635     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartDeviceLogging failed");
636 #else
637     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
638 #endif
639     return ERR_NONE;
640 }
641 
HandleStopDeviceLogging(MessageParcel & data,MessageParcel & reply)642 int32_t AVSessionServiceStub::HandleStopDeviceLogging(MessageParcel& data, MessageParcel& reply)
643 {
644     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopDeviceLogging");
645     SLOGI("HandleStopDeviceLogging start");
646     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
647     if (err != ERR_NONE) {
648         SLOGE("StopDeviceLogging: CheckPermission failed");
649         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
650             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
651             "avsessionservice StopDeviceLogging checkPermission failed");
652         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
653         return ERR_NONE;
654     }
655 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
656     int32_t ret = AVRouter::GetInstance().StopDeviceLogging();
657     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
658     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopDeviceLogging failed");
659 #else
660     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
661 #endif
662     return ERR_NONE;
663 }
664 
HandleSetDiscoverable(MessageParcel & data,MessageParcel & reply)665 int32_t AVSessionServiceStub::HandleSetDiscoverable(MessageParcel& data, MessageParcel& reply)
666 {
667     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleSetDiscoverable");
668     SLOGI("HandleSetDiscoverable start");
669     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
670     if (err != ERR_NONE) {
671         SLOGE("SetDiscoverable: CheckPermission failed");
672         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
673             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
674             "avsessionservice SetDiscoverable checkPermission failed");
675         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
676         return ERR_NONE;
677     }
678 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
679     bool enable;
680     CHECK_AND_RETURN_RET_LOG(data.ReadBool(enable), AVSESSION_ERROR, "write enable info failed");
681     int32_t ret = checkEnableCast(enable);
682 
683     bool is2in1 = system::GetBoolParameter("const.audio.volume_apply_to_all", false);
684     SLOGI("GetDeviceEnableCast,Prop=%{public}d,enable=%{public}d", static_cast<int>(is2in1), static_cast<int>(enable));
685     if (enable && is2in1 && ret) {
686         AVRouter::GetInstance().SetDiscoverable(false);
687         ret = AVRouter::GetInstance().SetDiscoverable(enable);
688     }
689 
690     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
691     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleSetDiscoverable failed");
692 #else
693     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
694 #endif
695     return ERR_NONE;
696 }
697 
CheckBeforeHandleStartCast(MessageParcel & data,OutputDeviceInfo & outputDeviceInfo)698 int32_t AVSessionServiceStub::CheckBeforeHandleStartCast(MessageParcel& data, OutputDeviceInfo& outputDeviceInfo)
699 {
700     DeviceInfo deviceInfo;
701     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
702     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
703     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
704     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
705     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
706     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.manufacturer_), false, "Read manufacturer failed");
707     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.modelName_), false, "Read modelName failed");
708     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
709     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
710         "Read supportedProtocols failed");
711     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
712         "Read authenticationStatus failed");
713     int32_t supportedDrmCapabilityLen = 0;
714     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(supportedDrmCapabilityLen), false,
715         "read supportedDrmCapabilityLen failed");
716     int32_t maxSupportedDrmCapabilityLen = 10;
717     CHECK_AND_RETURN_RET_LOG((supportedDrmCapabilityLen >= 0) &&
718         (supportedDrmCapabilityLen <= maxSupportedDrmCapabilityLen), false, "supportedDrmCapabilityLen is illegal");
719     std::vector<std::string> supportedDrmCapabilities;
720     for (int i = 0; i < supportedDrmCapabilityLen; i++) {
721         std::string supportedDrmCapability;
722         CHECK_AND_RETURN_RET_LOG(data.ReadString(supportedDrmCapability), false,
723             "read supportedDrmCapability failed");
724         supportedDrmCapabilities.emplace_back(supportedDrmCapability);
725     }
726     deviceInfo.supportedDrmCapabilities_ = supportedDrmCapabilities;
727     CHECK_AND_RETURN_RET_LOG(data.ReadBool(deviceInfo.isLegacy_), false, "Read isLegacy failed");
728     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.mediumTypes_), false, "Read mediumTypes failed");
729     outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
730     return true;
731 }
732 
HandleStartCast(MessageParcel & data,MessageParcel & reply)733 int32_t AVSessionServiceStub::HandleStartCast(MessageParcel& data, MessageParcel& reply)
734 {
735 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
736     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCast");
737     int32_t err = PermissionChecker::GetInstance().CheckPermission(
738         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
739     if (err != ERR_NONE) {
740         SLOGE("StartCast: CheckPermission failed");
741         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
742             "ERROR_MSG", "avsessionservice StartCast checkPermission failed");
743         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
744         return ERR_NONE;
745     }
746     SessionToken sessionToken {};
747     sessionToken.sessionId = data.ReadString();
748     sessionToken.pid = data.ReadInt32();
749     sessionToken.uid = data.ReadInt32();
750 
751     OutputDeviceInfo outputDeviceInfo;
752     int32_t deviceInfoSize;
753     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
754 
755     if (deviceInfoSize > RECEIVE_DEVICE_NUM_MAX) {
756         SLOGI("receive deviceNum over range");
757         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
758         return ERR_NONE;
759     }
760     for (int i = 0; i < deviceInfoSize; i++) {
761         if (!CheckBeforeHandleStartCast(data, outputDeviceInfo)) {
762             SLOGE("check fail");
763             return ERR_NONE;
764         }
765     }
766     uint32_t callerToken = static_cast<uint32_t>(OHOS::IPCSkeleton::GetCallingTokenID());
767     int temp = SetFirstCallerTokenID(callerToken);
768     SLOGI("SetFirstCallerTokenID return %{public}d", temp);
769     if (temp < 0) {
770         SLOGE("SetFirstCallerTokenID fail");
771     }
772     int32_t ret = StartCast(sessionToken, outputDeviceInfo);
773     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StartCast failed");
774     SLOGI("StartCast ret %{public}d", ret);
775     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
776     SLOGI("HandleStartCast success");
777 #else
778     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
779 #endif
780     return ERR_NONE;
781 }
782 
HandleStopCast(MessageParcel & data,MessageParcel & reply)783 int32_t AVSessionServiceStub::HandleStopCast(MessageParcel& data, MessageParcel& reply)
784 {
785 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
786     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCast");
787     SLOGI("HandleStopCast start");
788     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
789     if (err != ERR_NONE) {
790         SLOGE("StopCast: CheckPermission failed");
791         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
792             "ERROR_MSG", "avsessionservice StopCast checkpermission failed");
793         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
794         return ERR_NONE;
795     }
796     SessionToken sessionToken {};
797     sessionToken.sessionId = data.ReadString();
798     sessionToken.pid = data.ReadInt32();
799     sessionToken.uid = data.ReadInt32();
800 
801     int32_t ret = StopCast(sessionToken);
802     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StopCast failed");
803     SLOGI("StopCast ret %{public}d", ret);
804     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
805     SLOGI("HandleStopCast success");
806 #else
807     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
808 #endif
809     return ERR_NONE;
810 }
811 } // namespace OHOS::AVSession