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 
HandleIsAudioPlaybackAllowed(MessageParcel& data, MessageParcel& reply)250 int32_t AVSessionServiceStub::HandleIsAudioPlaybackAllowed(MessageParcel& data, MessageParcel& reply)
251 {
252     int32_t err = PermissionChecker::GetInstance().CheckPermission(
253         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
254     if (err != ERR_NONE) {
255         SLOGE("IsAudioPlaybackAllowed: CheckPermission failed");
256         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
257             "ERROR_MSG", "avsessionservice IsAudioPlaybackAllowed checkpermission failed");
258         CHECK_AND_RETURN_RET_LOG(reply.WriteBool(err), ERR_NONE, "write bool failed");
259         return ERR_NONE;
260     }
261     int32_t uid = data.ReadInt32();
262     int32_t pid = data.ReadInt32();
263     bool ret = IsAudioPlaybackAllowed(uid, pid);
264     CHECK_AND_RETURN_RET_LOG(reply.WriteBool(ret), ERR_NONE, "write bool failed");
265     return ERR_NONE;
266 }
267 
HandleCreateControllerInner(MessageParcel& data, MessageParcel& reply)268 int32_t AVSessionServiceStub::HandleCreateControllerInner(MessageParcel& data, MessageParcel& reply)
269 {
270     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateControllerInner");
271     int32_t err = PermissionChecker::GetInstance().CheckPermission(
272         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
273     if (err != ERR_NONE) {
274         SLOGE("CreateControllerInner: CheckPermission failed");
275         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(),
276             "CALLER_PID", GetCallingPid(), "SESSION_ID", data.ReadString(),
277             "ERROR_MSG", "avsessionservice createcontrollerinner checkpermission failed");
278         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
279         return ERR_NONE;
280     }
281     sptr<IRemoteObject> object;
282     int32_t ret = CreateControllerInner(data.ReadString(), object);
283     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
284     if (ret == AVSESSION_SUCCESS || ret == ERR_CONTROLLER_IS_EXIST) {
285         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
286     }
287     return ERR_NONE;
288 }
289 
HandleGetAVCastControllerInner(MessageParcel& data, MessageParcel& reply)290 int32_t AVSessionServiceStub::HandleGetAVCastControllerInner(MessageParcel& data, MessageParcel& reply)
291 {
292 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
293     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleGetAVCastControllerInner");
294     int32_t err = PermissionChecker::GetInstance().CheckPermission(
295         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
296     if (err != ERR_NONE) {
297         SLOGE("GetAVCastControllerInner: CheckPermission failed");
298         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
299         return ERR_NONE;
300     }
301     sptr<IRemoteObject> object;
302     int32_t ret = GetAVCastControllerInner(data.ReadString(), object);
303     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
304     if (ret == AVSESSION_SUCCESS) {
305         CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
306     }
307 #else
308     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
309 #endif
310     return ERR_NONE;
311 }
312 
HandleRegisterSessionListener(MessageParcel& data, MessageParcel& reply)313 int32_t AVSessionServiceStub::HandleRegisterSessionListener(MessageParcel& data, MessageParcel& reply)
314 {
315     int32_t err = PermissionChecker::GetInstance().CheckPermission(
316         PermissionChecker::CHECK_SYSTEM_PERMISSION);
317     if (err != ERR_NONE) {
318         SLOGE("RegisterSessionListener: CheckPermission failed");
319         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
320             "ERROR_MSG", "avsessionservice registersessionlistener checkpermission failed");
321         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
322         return ERR_NONE;
323     }
324     auto remoteObject = data.ReadRemoteObject();
325     if (remoteObject == nullptr) {
326         SLOGI("read remote object failed");
327         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
328         return ERR_NONE;
329     }
330     auto listener = iface_cast<SessionListenerProxy>(remoteObject);
331     if (listener == nullptr) {
332         SLOGI("RegisterSessionListener but iface_cast remote object failed");
333         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
334         return ERR_NONE;
335     }
336     if (!reply.WriteInt32(RegisterSessionListener(listener))) {
337         SLOGI("reply write int32 failed");
338     }
339     return ERR_NONE;
340 }
341 
HandleRegisterSessionListenerForAllUsers(MessageParcel& data, MessageParcel& reply)342 int32_t AVSessionServiceStub::HandleRegisterSessionListenerForAllUsers(MessageParcel& data, MessageParcel& reply)
343 {
344     int32_t err = PermissionChecker::GetInstance().CheckPermission(
345         PermissionChecker::CHECK_SYSTEM_PERMISSION);
346     if (err != ERR_NONE) {
347         SLOGE("RegisterSessionListenerForAllUsers: CheckPermission failed");
348         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
349             "ERROR_MSG", "avsessionservice RegisterSessionListenerForAllUsers checkpermission failed");
350         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
351         return ERR_NONE;
352     }
353     auto remoteObject = data.ReadRemoteObject();
354     if (remoteObject == nullptr) {
355         SLOGI("read remote object failed");
356         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
357         return ERR_NONE;
358     }
359     auto listener = iface_cast<SessionListenerProxy>(remoteObject);
360     if (listener == nullptr) {
361         SLOGI("RegisterSessionListenerForAllUsers but iface_cast remote object failed");
362         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
363         return ERR_NONE;
364     }
365     if (!reply.WriteInt32(RegisterSessionListenerForAllUsers(listener))) {
366         SLOGI("reply write int32 failed");
367     }
368     return ERR_NONE;
369 }
370 
HandleSendSystemAVKeyEvent(MessageParcel& data, MessageParcel& reply)371 int32_t AVSessionServiceStub::HandleSendSystemAVKeyEvent(MessageParcel& data, MessageParcel& reply)
372 {
373     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemAVKeyEvent");
374     auto keyEvent = MMI::KeyEvent::Create();
375     if (keyEvent == nullptr) {
376         SLOGI("create keyEvent failed");
377         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_NO_MEMORY), ERR_NONE, "write int32 failed");
378         return ERR_NONE;
379     }
380     if (!keyEvent->ReadFromParcel(data)) {
381         SLOGI("read keyEvent failed");
382         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
383         return ERR_NONE;
384     }
385     if (!keyEvent->IsValid()) {
386         SLOGI("keyEvent is not valid");
387         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
388         return ERR_NONE;
389     }
390     int32_t err = PermissionChecker::GetInstance().CheckPermission(
391         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
392     if (err != ERR_NONE) {
393         SLOGE("SendSystemAVKeyEvent: CheckPermission failed");
394         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
395             "KEY_CODE", keyEvent->GetKeyCode(), "KEY_ACTION", keyEvent->GetKeyAction(),
396             "ERROR_MSG", "avsessionservice sendsystemavkeyevent checkpermission failed");
397         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
398         return ERR_NONE;
399     }
400     if (!reply.WriteInt32(SendSystemAVKeyEvent(*keyEvent))) {
401         SLOGI("reply write int32 failed");
402     }
403     return ERR_NONE;
404 }
405 
HandleSendSystemControlCommand(MessageParcel& data, MessageParcel& reply)406 int32_t AVSessionServiceStub::HandleSendSystemControlCommand(MessageParcel& data, MessageParcel& reply)
407 {
408     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemControlCommand");
409     sptr command = data.ReadParcelable<AVControlCommand>();
410     if (command == nullptr) {
411         SLOGI("read command failed");
412         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
413         HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "READ_PARCELABLE_FAILED",
414             "ERROR_INFO", "handle send system control command read command failed");
415         return ERR_NONE;
416     }
417     int32_t err = PermissionChecker::GetInstance().CheckPermission(
418         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
419     if (err != ERR_NONE) {
420         SLOGE("SendSystemControlCommand: CheckPermission failed");
421         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(),
422             "CALLER_PID", GetCallingPid(), "CMD", command->GetCommand(),
423             "ERROR_MSG", "avsessionservice sendsystemcontrolcommand checkpermission failed");
424         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
425         return ERR_NONE;
426     }
427     if (!reply.WriteInt32(SendSystemControlCommand(*command))) {
428         SLOGI("reply write int32 failed");
429     }
430     return ERR_NONE;
431 }
432 
HandleRegisterClientDeathObserver(MessageParcel& data, MessageParcel& reply)433 int32_t AVSessionServiceStub::HandleRegisterClientDeathObserver(MessageParcel& data, MessageParcel& reply)
434 {
435     auto remoteObject = data.ReadRemoteObject();
436     if (remoteObject == nullptr) {
437         SLOGI("read remote object failed");
438         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
439         return ERR_NONE;
440     }
441     auto clientDeathObserver = iface_cast<ClientDeathProxy>(remoteObject);
442     if (clientDeathObserver == nullptr) {
443         SLOGI("iface_cast remote object failed");
444         reply.WriteInt32(ERR_INVALID_PARAM);
445         return ERR_NONE;
446     }
447     int32_t ret = RegisterClientDeathObserver(clientDeathObserver);
448     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "register clientDeathObserver failed");
449     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
450     return ERR_NONE;
451 }
452 
HandleClose(MessageParcel& data, MessageParcel& reply)453 int32_t AVSessionServiceStub::HandleClose(MessageParcel& data, MessageParcel& reply)
454 {
455     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
456     if (err != ERR_NONE) {
457         SLOGE("Close: CheckPermission failed");
458         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
459             "ERROR_MSG", "avsessionservice Close checkpermission failed");
460         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
461         return ERR_NONE;
462     }
463     int32_t ret = Close();
464     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Close failed");
465     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
466     return ERR_NONE;
467 }
468 
HandleCastAudio(MessageParcel& data, MessageParcel& reply)469 int32_t AVSessionServiceStub::HandleCastAudio(MessageParcel& data, MessageParcel& reply)
470 {
471     int32_t err = PermissionChecker::GetInstance().CheckPermission(
472         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
473     if (err != ERR_NONE) {
474         SLOGE("CastAudio: CheckPermission failed");
475         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
476             "ERROR_MSG", "avsessionservice CastAudio checkmission failed");
477         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
478         return ERR_NONE;
479     }
480     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudio");
481     SLOGI("start");
482     SessionToken token {};
483     token.sessionId = data.ReadString();
484     token.pid = data.ReadInt32();
485     token.uid = data.ReadInt32();
486     int32_t deviceNum = data.ReadInt32();
487     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
488         SLOGI("receive deviceNum over range");
489         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
490         return ERR_NONE;
491     }
492 
493     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors;
494     for (int i = 0; i < deviceNum; i++) {
495         auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
496         if (audioDeviceDescriptor == nullptr) {
497             SLOGI("read AudioDeviceDescriptor failed");
498             CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
499             return ERR_NONE;
500         }
501         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
502               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
503         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
504     }
505     int32_t ret = CastAudio(token, sinkAudioDescriptors);
506     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudio failed");
507     SLOGI("CastAudio ret %{public}d", ret);
508     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
509     SLOGI("success");
510     return ERR_NONE;
511 }
512 
HandleCastAudioForAll(MessageParcel& data, MessageParcel& reply)513 int32_t AVSessionServiceStub::HandleCastAudioForAll(MessageParcel& data, MessageParcel& reply)
514 {
515     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudioForAll");
516     SLOGI("CastAudioForAll start");
517     int32_t err = PermissionChecker::GetInstance().CheckPermission(
518         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
519     if (err != ERR_NONE) {
520         SLOGE("CastAudioForAll: CheckPermission failed");
521         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
522             "ERROR_MSG", "avsessionservice CastAudioForAll checkpermission failed");
523         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
524         return ERR_NONE;
525     }
526     int32_t deviceNum = data.ReadInt32();
527     if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
528         SLOGI("receive deviceNum over range");
529         reply.WriteInt32(ERR_INVALID_PARAM);
530         return ERR_NONE;
531     }
532 
533     std::vector<AudioDeviceDescriptor> sinkAudioDescriptors {};
534     for (int i = 0; i < deviceNum; i++) {
535         auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
536         if (audioDeviceDescriptor == nullptr) {
537             SLOGI("read AudioDeviceDescriptor failed");
538             reply.WriteInt32(ERR_UNMARSHALLING);
539             return ERR_NONE;
540         }
541         SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
542               static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
543         sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
544     }
545     int32_t ret = CastAudioForAll(sinkAudioDescriptors);
546     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudioForAll failed");
547     SLOGI("CastAudioForAll ret %{public}d", ret);
548     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
549     return ERR_NONE;
550 }
551 
HandleRemoteCastAudio(MessageParcel& data, MessageParcel& reply)552 int32_t AVSessionServiceStub::HandleRemoteCastAudio(MessageParcel& data, MessageParcel& reply)
553 {
554     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::RemoteCastAudio");
555     SLOGI("start");
556     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
557     if (err != ERR_NONE) {
558         SLOGE("ProcessCastAudioCommand: CheckPermission failed");
559         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
560             "ERROR_MSG", "avsessionservice ProcessCastAudioCommand checkpermission failed");
561         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
562         return ERR_NONE;
563     }
564     auto command = static_cast<RemoteServiceCommand>(data.ReadInt32());
565     std::string sessionInfo = data.ReadString();
566     std::string output;
567     int32_t ret = ProcessCastAudioCommand(command, sessionInfo, output);
568     SLOGI("RemoteCastAudio ret %{public}d", ret);
569     if (ret != AVSESSION_SUCCESS) {
570         SLOGI("RemoteCastAudio failed");
571         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
572         return ERR_NONE;
573     }
574     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
575     CHECK_AND_RETURN_RET_LOG(reply.WriteString(output), ERR_NONE, "write int32 failed");
576     return ERR_NONE;
577 }
578 
HandleStartCastDiscovery(MessageParcel& data, MessageParcel& reply)579 int32_t AVSessionServiceStub::HandleStartCastDiscovery(MessageParcel& data, MessageParcel& reply)
580 {
581     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCastDiscovery");
582     SLOGI("HandleStartCastDiscovery start");
583     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
584     if (err != ERR_NONE) {
585         SLOGE("StartCastDiscovery: CheckPermission failed");
586         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
587             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
588             "avsessionservice StartCastDiscovery checkPermission failed");
589         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
590         return ERR_NONE;
591     }
592 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
593     auto castDeviceCapability = data.ReadInt32();
594     int32_t drmSchemesLen = data.ReadInt32();
595     std::vector<std::string> drmSchemes;
596     int32_t maxDrmSchemesLen = 10;
597     CHECK_AND_RETURN_RET_LOG((drmSchemesLen >= 0) &&
598         (drmSchemesLen <= maxDrmSchemesLen), ERR_NONE, "drmSchemesLen is illegal");
599     for (int i = 0; i < drmSchemesLen; i++) {
600         std::string drmScheme = data.ReadString();
601         drmSchemes.emplace_back(drmScheme);
602     }
603     int32_t ret = AVRouter::GetInstance().StartCastDiscovery(castDeviceCapability, drmSchemes);
604     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
605     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartCastDiscovery failed");
606 #else
607     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
608 #endif
609     return ERR_NONE;
610 }
611 
HandleStopCastDiscovery(MessageParcel& data, MessageParcel& reply)612 int32_t AVSessionServiceStub::HandleStopCastDiscovery(MessageParcel& data, MessageParcel& reply)
613 {
614     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCastDiscovery");
615     SLOGI("HandleStopCastDiscovery start");
616     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
617     if (err != ERR_NONE) {
618         SLOGE("StopCastDiscovery: CheckPermission failed");
619         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
620             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
621             "avsessionservice StopCastDiscovery checkPermission failed");
622         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
623         return ERR_NONE;
624     }
625 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
626     int32_t ret = AVRouter::GetInstance().StopCastDiscovery();
627     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
628     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopCastDiscovery failed");
629 #else
630     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
631 #endif
632     return ERR_NONE;
633 }
634 
HandleStartDeviceLogging(MessageParcel& data, MessageParcel& reply)635 int32_t AVSessionServiceStub::HandleStartDeviceLogging(MessageParcel& data, MessageParcel& reply)
636 {
637     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartDeviceLogging");
638     SLOGI("HandleStartDeviceLogging start");
639     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
640     if (err != ERR_NONE) {
641         SLOGE("HandleStartDeviceLogging: CheckPermission failed");
642         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
643             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
644             "avsessionservice StartDeviceLogging checkPermission failed");
645         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
646         return ERR_NONE;
647     }
648 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
649     int32_t fd = data.ReadFileDescriptor();
650     uint32_t maxSize = data.ReadUint32();
651     int32_t ret = AVRouter::GetInstance().StartDeviceLogging(fd, maxSize);
652     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
653     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartDeviceLogging failed");
654 #else
655     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
656 #endif
657     return ERR_NONE;
658 }
659 
HandleStopDeviceLogging(MessageParcel& data, MessageParcel& reply)660 int32_t AVSessionServiceStub::HandleStopDeviceLogging(MessageParcel& data, MessageParcel& reply)
661 {
662     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopDeviceLogging");
663     SLOGI("HandleStopDeviceLogging start");
664     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
665     if (err != ERR_NONE) {
666         SLOGE("StopDeviceLogging: CheckPermission failed");
667         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
668             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
669             "avsessionservice StopDeviceLogging checkPermission failed");
670         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
671         return ERR_NONE;
672     }
673 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
674     int32_t ret = AVRouter::GetInstance().StopDeviceLogging();
675     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
676     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopDeviceLogging failed");
677 #else
678     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
679 #endif
680     return ERR_NONE;
681 }
682 
HandleSetDiscoverable(MessageParcel& data, MessageParcel& reply)683 int32_t AVSessionServiceStub::HandleSetDiscoverable(MessageParcel& data, MessageParcel& reply)
684 {
685     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleSetDiscoverable");
686     SLOGI("HandleSetDiscoverable start");
687     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
688     if (err != ERR_NONE) {
689         SLOGE("SetDiscoverable: CheckPermission failed");
690         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", IPCSkeleton::GetCallingUid(),
691             "CALLER_PID", IPCSkeleton::GetCallingPid(), "ERROR_MSG",
692             "avsessionservice SetDiscoverable checkPermission failed");
693         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
694         return ERR_NONE;
695     }
696 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
697     bool enable;
698     CHECK_AND_RETURN_RET_LOG(data.ReadBool(enable), AVSESSION_ERROR, "write enable info failed");
699     int32_t ret = AVSESSION_SUCCESS;
700 
701     bool is2in1 = system::GetBoolParameter("const.audio.volume_apply_to_all", false);
702     SLOGI("GetDeviceEnableCast , Prop=%{public}d", static_cast<int>(is2in1));
703     if (enable) {
704         checkEnableCast(enable);
705         if (is2in1) {
706             AVRouter::GetInstance().SetDiscoverable(false);
707             ret = AVRouter::GetInstance().SetDiscoverable(enable);
708         } else {
709             SLOGI("setdiscoverable not 2in1");
710         }
711     } else {
712         SLOGI("setdiscoverable not set with enable");
713     }
714 
715     AVRouter::GetInstance().SetDiscoverable(enable);
716     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
717     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleSetDiscoverable failed");
718 #else
719     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
720 #endif
721     return ERR_NONE;
722 }
723 
CheckBeforeHandleStartCast(MessageParcel& data, OutputDeviceInfo& outputDeviceInfo)724 int32_t AVSessionServiceStub::CheckBeforeHandleStartCast(MessageParcel& data, OutputDeviceInfo& outputDeviceInfo)
725 {
726     DeviceInfo deviceInfo;
727     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
728     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
729     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
730     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
731     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
732     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.manufacturer_), false, "Read manufacturer failed");
733     CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.modelName_), false, "Read modelName failed");
734     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
735     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.supportedProtocols_), false,
736         "Read supportedProtocols failed");
737     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.authenticationStatus_), false,
738         "Read authenticationStatus failed");
739     int32_t supportedDrmCapabilityLen = 0;
740     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(supportedDrmCapabilityLen), false,
741         "read supportedDrmCapabilityLen failed");
742     int32_t maxSupportedDrmCapabilityLen = 10;
743     CHECK_AND_RETURN_RET_LOG((supportedDrmCapabilityLen >= 0) &&
744         (supportedDrmCapabilityLen <= maxSupportedDrmCapabilityLen), false, "supportedDrmCapabilityLen is illegal");
745     std::vector<std::string> supportedDrmCapabilities;
746     for (int i = 0; i < supportedDrmCapabilityLen; i++) {
747         std::string supportedDrmCapability;
748         CHECK_AND_RETURN_RET_LOG(data.ReadString(supportedDrmCapability), false,
749             "read supportedDrmCapability failed");
750         supportedDrmCapabilities.emplace_back(supportedDrmCapability);
751     }
752     deviceInfo.supportedDrmCapabilities_ = supportedDrmCapabilities;
753     CHECK_AND_RETURN_RET_LOG(data.ReadBool(deviceInfo.isLegacy_), false, "Read isLegacy failed");
754     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.mediumTypes_), false, "Read mediumTypes failed");
755     outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
756     return true;
757 }
758 
HandleStartCast(MessageParcel& data, MessageParcel& reply)759 int32_t AVSessionServiceStub::HandleStartCast(MessageParcel& data, MessageParcel& reply)
760 {
761 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
762     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCast");
763     int32_t err = PermissionChecker::GetInstance().CheckPermission(
764         PermissionChecker::CHECK_MEDIA_RESOURCES_PERMISSION);
765     if (err != ERR_NONE) {
766         SLOGE("StartCast: CheckPermission failed");
767         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
768             "ERROR_MSG", "avsessionservice StartCast checkPermission failed");
769         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
770         return ERR_NONE;
771     }
772     SessionToken sessionToken {};
773     sessionToken.sessionId = data.ReadString();
774     sessionToken.pid = data.ReadInt32();
775     sessionToken.uid = data.ReadInt32();
776 
777     OutputDeviceInfo outputDeviceInfo;
778     int32_t deviceInfoSize;
779     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
780 
781     if (deviceInfoSize > RECEIVE_DEVICE_NUM_MAX) {
782         SLOGI("receive deviceNum over range");
783         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
784         return ERR_NONE;
785     }
786     for (int i = 0; i < deviceInfoSize; i++) {
787         if (!CheckBeforeHandleStartCast(data, outputDeviceInfo)) {
788             SLOGE("check fail");
789             return ERR_NONE;
790         }
791     }
792     uint32_t callerToken = static_cast<uint32_t>(OHOS::IPCSkeleton::GetCallingTokenID());
793     int temp = SetFirstCallerTokenID(callerToken);
794     SLOGI("SetFirstCallerTokenID return %{public}d", temp);
795     if (temp < 0) {
796         SLOGE("SetFirstCallerTokenID fail");
797     }
798     int32_t ret = StartCast(sessionToken, outputDeviceInfo);
799     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StartCast failed");
800     SLOGI("StartCast ret %{public}d", ret);
801     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
802     SLOGI("HandleStartCast success");
803 #else
804     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
805 #endif
806     return ERR_NONE;
807 }
808 
HandleStopCast(MessageParcel& data, MessageParcel& reply)809 int32_t AVSessionServiceStub::HandleStopCast(MessageParcel& data, MessageParcel& reply)
810 {
811 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
812     AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCast");
813     SLOGI("HandleStopCast start");
814     int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
815     if (err != ERR_NONE) {
816         SLOGE("StopCast: CheckPermission failed");
817         HISYSEVENT_SECURITY("CONTROL_PERMISSION_DENIED", "CALLER_UID", GetCallingUid(), "CALLER_PID", GetCallingPid(),
818             "ERROR_MSG", "avsessionservice StopCast checkpermission failed");
819         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
820         return ERR_NONE;
821     }
822     SessionToken sessionToken {};
823     sessionToken.sessionId = data.ReadString();
824     sessionToken.pid = data.ReadInt32();
825     sessionToken.uid = data.ReadInt32();
826 
827     int32_t ret = StopCast(sessionToken);
828     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StopCast failed");
829     SLOGI("StopCast ret %{public}d", ret);
830     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
831     SLOGI("HandleStopCast success");
832 #else
833     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
834 #endif
835     return ERR_NONE;
836 }
837 } // namespace OHOS::AVSession
838