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