1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "av_router.h"
17 #include "avsession_log.h"
18 #include "avsession_errors.h"
19 #include "session_listener_proxy.h"
20 #include "client_death_proxy.h"
21 #include "avsession_trace.h"
22 #include "avsession_sysevent.h"
23 #include "avsession_service_stub.h"
24
25 using namespace OHOS::AudioStandard;
26 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)27 bool AVSessionServiceStub::CheckInterfaceToken(MessageParcel& data)
28 {
29 auto localDescriptor = IAVSessionService::GetDescriptor();
30 auto remoteDescriptor = data.ReadInterfaceToken();
31 if (remoteDescriptor != localDescriptor) {
32 SLOGI("interface token is not equal");
33 return false;
34 }
35 return true;
36 }
37
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int32_t AVSessionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
39 MessageOption& option)
40 {
41 if (!CheckInterfaceToken(data)) {
42 return AVSESSION_ERROR;
43 }
44 if (code < static_cast<uint32_t>(AvsessionSeviceInterfaceCode::SERVICE_CMD_MAX)) {
45 return (this->*handlers[code])(data, reply);
46 }
47 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48 }
49
HandleCreateSessionInner(MessageParcel & data,MessageParcel & reply)50 int32_t AVSessionServiceStub::HandleCreateSessionInner(MessageParcel& data, MessageParcel& reply)
51 {
52 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateSessionInner");
53 auto sessionTag = data.ReadString();
54 auto sessionType = data.ReadInt32();
55 sptr elementName = data.ReadParcelable<AppExecFwk::ElementName>();
56 if (elementName == nullptr) {
57 SLOGI("read element name failed");
58 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
59 return ERR_NONE;
60 }
61 auto session = CreateSessionInner(sessionTag, sessionType, *elementName);
62 if (session == nullptr) {
63 SLOGI("session is nullptr");
64 reply.WriteInt32(ERR_UNMARSHALLING);
65 return ERR_NONE;
66 }
67 reply.WriteInt32(AVSESSION_SUCCESS);
68 reply.WriteRemoteObject(session);
69 return ERR_NONE;
70 }
71
HandleGetAllSessionDescriptors(MessageParcel & data,MessageParcel & reply)72 int32_t AVSessionServiceStub::HandleGetAllSessionDescriptors(MessageParcel& data, MessageParcel& reply)
73 {
74 std::vector<AVSessionDescriptor> descriptors;
75 int32_t ret = GetAllSessionDescriptors(descriptors);
76 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
77 CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
78 for (const auto& descriptor : descriptors) {
79 if (!descriptor.WriteToParcel(reply)) {
80 SLOGI("write descriptor failed");
81 break;
82 }
83 }
84 return ERR_NONE;
85 }
86
HandleGetSessionDescriptorsById(MessageParcel & data,MessageParcel & reply)87 int32_t AVSessionServiceStub::HandleGetSessionDescriptorsById(MessageParcel& data, MessageParcel& reply)
88 {
89 AVSessionDescriptor descriptor;
90 int32_t ret = GetSessionDescriptorsBySessionId(data.ReadString(), descriptor);
91 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
92 if (ret == AVSESSION_SUCCESS) {
93 CHECK_AND_PRINT_LOG(descriptor.WriteToParcel(reply), "write AVSessionDescriptor failed");
94 }
95 return ERR_NONE;
96 }
97
HandleGetHistoricalSessionDescriptors(MessageParcel & data,MessageParcel & reply)98 int32_t AVSessionServiceStub::HandleGetHistoricalSessionDescriptors(MessageParcel& data, MessageParcel& reply)
99 {
100 std::vector<AVSessionDescriptor> descriptors;
101 int32_t ret = GetHistoricalSessionDescriptors(data.ReadInt32(), descriptors);
102 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
103 CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
104 for (const auto& descriptor : descriptors) {
105 if (!descriptor.WriteToParcel(reply)) {
106 SLOGI("write descriptor failed");
107 break;
108 }
109 }
110 return ERR_NONE;
111 }
112
HandleCreateControllerInner(MessageParcel & data,MessageParcel & reply)113 int32_t AVSessionServiceStub::HandleCreateControllerInner(MessageParcel& data, MessageParcel& reply)
114 {
115 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateControllerInner");
116 sptr<IRemoteObject> object;
117 int32_t ret = CreateControllerInner(data.ReadString(), object);
118 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
119 if (ret == AVSESSION_SUCCESS) {
120 CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
121 }
122 return ERR_NONE;
123 }
124
HandleGetAVCastControllerInner(MessageParcel & data,MessageParcel & reply)125 int32_t AVSessionServiceStub::HandleGetAVCastControllerInner(MessageParcel& data, MessageParcel& reply)
126 {
127 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
128 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleGetAVCastControllerInner");
129 sptr<IRemoteObject> object;
130 int32_t ret = GetAVCastControllerInner(data.ReadString(), object);
131 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
132 if (ret == AVSESSION_SUCCESS) {
133 CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
134 }
135 #else
136 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
137 #endif
138 return ERR_NONE;
139 }
140
HandleRegisterSessionListener(MessageParcel & data,MessageParcel & reply)141 int32_t AVSessionServiceStub::HandleRegisterSessionListener(MessageParcel& data, MessageParcel& reply)
142 {
143 auto remoteObject = data.ReadRemoteObject();
144 if (remoteObject == nullptr) {
145 SLOGI("read remote object failed");
146 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
147 return ERR_NONE;
148 }
149 auto listener = iface_cast<SessionListenerProxy>(remoteObject);
150 if (listener == nullptr) {
151 SLOGI("iface_cast remote object failed");
152 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
153 return ERR_NONE;
154 }
155 if (!reply.WriteInt32(RegisterSessionListener(listener))) {
156 SLOGI("reply write int32 failed");
157 }
158 return ERR_NONE;
159 }
160
HandleSendSystemAVKeyEvent(MessageParcel & data,MessageParcel & reply)161 int32_t AVSessionServiceStub::HandleSendSystemAVKeyEvent(MessageParcel& data, MessageParcel& reply)
162 {
163 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemAVKeyEvent");
164 auto keyEvent = MMI::KeyEvent::Create();
165 if (keyEvent == nullptr) {
166 SLOGI("create keyEvent failed");
167 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_NO_MEMORY), ERR_NONE, "write int32 failed");
168 return ERR_NONE;
169 }
170 if (!keyEvent->ReadFromParcel(data)) {
171 SLOGI("read keyEvent failed");
172 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
173 return ERR_NONE;
174 }
175 if (!keyEvent->IsValid()) {
176 SLOGI("keyEvent is not valid");
177 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
178 return ERR_NONE;
179 }
180 if (!reply.WriteInt32(SendSystemAVKeyEvent(*keyEvent))) {
181 SLOGI("reply write int32 failed");
182 }
183 return ERR_NONE;
184 }
185
HandleSendSystemControlCommand(MessageParcel & data,MessageParcel & reply)186 int32_t AVSessionServiceStub::HandleSendSystemControlCommand(MessageParcel& data, MessageParcel& reply)
187 {
188 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemControlCommand");
189 sptr command = data.ReadParcelable<AVControlCommand>();
190 if (command == nullptr) {
191 SLOGI("read command failed");
192 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
193 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "READ_PARCELABLE_FAILED",
194 "ERROR_INFO", "handle send system control command read command failed");
195 return ERR_NONE;
196 }
197 if (!reply.WriteInt32(SendSystemControlCommand(*command))) {
198 SLOGI("reply write int32 failed");
199 }
200 return ERR_NONE;
201 }
202
HandleRegisterClientDeathObserver(MessageParcel & data,MessageParcel & reply)203 int32_t AVSessionServiceStub::HandleRegisterClientDeathObserver(MessageParcel& data, MessageParcel& reply)
204 {
205 auto remoteObject = data.ReadRemoteObject();
206 if (remoteObject == nullptr) {
207 SLOGI("read remote object failed");
208 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
209 return ERR_NONE;
210 }
211 auto clientDeathObserver = iface_cast<ClientDeathProxy>(remoteObject);
212 if (clientDeathObserver == nullptr) {
213 SLOGI("iface_cast remote object failed");
214 reply.WriteInt32(ERR_INVALID_PARAM);
215 return ERR_NONE;
216 }
217 int32_t ret = RegisterClientDeathObserver(clientDeathObserver);
218 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "register clientDeathObserver failed");
219 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
220 return ERR_NONE;
221 }
222
HandleCastAudio(MessageParcel & data,MessageParcel & reply)223 int32_t AVSessionServiceStub::HandleCastAudio(MessageParcel& data, MessageParcel& reply)
224 {
225 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudio");
226 SLOGI("start");
227 SessionToken token {};
228 token.sessionId = data.ReadString();
229 token.pid = data.ReadInt32();
230 token.uid = data.ReadInt32();
231 int32_t deviceNum = data.ReadInt32();
232 if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
233 SLOGI("receive deviceNum over range");
234 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
235 return ERR_NONE;
236 }
237
238 std::vector<AudioDeviceDescriptor> sinkAudioDescriptors;
239 for (int i = 0; i < deviceNum; i++) {
240 auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
241 if (audioDeviceDescriptor == nullptr) {
242 SLOGI("read AudioDeviceDescriptor failed");
243 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
244 return ERR_NONE;
245 }
246 SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
247 static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
248 sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
249 }
250 int32_t ret = CastAudio(token, sinkAudioDescriptors);
251 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudio failed");
252 SLOGI("CastAudio ret %{public}d", ret);
253 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
254 SLOGI("success");
255 return ERR_NONE;
256 }
257
HandleCastAudioForAll(MessageParcel & data,MessageParcel & reply)258 int32_t AVSessionServiceStub::HandleCastAudioForAll(MessageParcel& data, MessageParcel& reply)
259 {
260 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudioForAll");
261 SLOGI("start");
262 int32_t deviceNum = data.ReadInt32();
263 if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
264 SLOGI("receive deviceNum over range");
265 reply.WriteInt32(ERR_INVALID_PARAM);
266 return ERR_NONE;
267 }
268
269 std::vector<AudioDeviceDescriptor> sinkAudioDescriptors {};
270 for (int i = 0; i < deviceNum; i++) {
271 auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
272 if (audioDeviceDescriptor == nullptr) {
273 SLOGI("read AudioDeviceDescriptor failed");
274 reply.WriteInt32(ERR_UNMARSHALLING);
275 return ERR_NONE;
276 }
277 SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
278 static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
279 sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
280 }
281 int32_t ret = CastAudioForAll(sinkAudioDescriptors);
282 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudioForAll failed");
283 SLOGI("CastAudioForAll ret %{public}d", ret);
284 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
285 return ERR_NONE;
286 }
287
HandleRemoteCastAudio(MessageParcel & data,MessageParcel & reply)288 int32_t AVSessionServiceStub::HandleRemoteCastAudio(MessageParcel& data, MessageParcel& reply)
289 {
290 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::RemoteCastAudio");
291 SLOGI("start");
292 auto command = static_cast<RemoteServiceCommand>(data.ReadInt32());
293 std::string sessionInfo = data.ReadString();
294 std::string output;
295 int32_t ret = ProcessCastAudioCommand(command, sessionInfo, output);
296 SLOGI("RemoteCastAudio ret %{public}d", ret);
297 if (ret != AVSESSION_SUCCESS) {
298 SLOGI("RemoteCastAudio failed");
299 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
300 return ERR_NONE;
301 }
302 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
303 CHECK_AND_RETURN_RET_LOG(reply.WriteString(output), ERR_NONE, "write int32 failed");
304 return ERR_NONE;
305 }
306
HandleStartCastDiscovery(MessageParcel & data,MessageParcel & reply)307 int32_t AVSessionServiceStub::HandleStartCastDiscovery(MessageParcel& data, MessageParcel& reply)
308 {
309 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCastDiscovery");
310 SLOGI("HandleStartCastDiscovery start");
311 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
312 auto castDeviceCapability = data.ReadInt32();
313 int32_t ret = AVRouter::GetInstance().StartCastDiscovery(castDeviceCapability);
314 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
315 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStartCastDiscovery failed");
316 #else
317 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
318 #endif
319 return ERR_NONE;
320 }
321
HandleStopCastDiscovery(MessageParcel & data,MessageParcel & reply)322 int32_t AVSessionServiceStub::HandleStopCastDiscovery(MessageParcel& data, MessageParcel& reply)
323 {
324 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCastDiscovery");
325 SLOGI("HandleStopCastDiscovery start");
326 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
327 int32_t ret = AVRouter::GetInstance().StopCastDiscovery();
328 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
329 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleStopCastDiscovery failed");
330 #else
331 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
332 #endif
333 return ERR_NONE;
334 }
335
HandleSetDiscoverable(MessageParcel & data,MessageParcel & reply)336 int32_t AVSessionServiceStub::HandleSetDiscoverable(MessageParcel& data, MessageParcel& reply)
337 {
338 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleSetDiscoverable");
339 SLOGI("HandleSetDiscoverable start");
340 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
341 bool enable;
342 CHECK_AND_RETURN_RET_LOG(data.ReadBool(enable), AVSESSION_ERROR, "write enable info failed");
343 int32_t ret = AVRouter::GetInstance().SetDiscoverable(enable);
344 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
345 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "HandleSetDiscoverable failed");
346 #else
347 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
348 #endif
349 return ERR_NONE;
350 }
351
HandleStartCast(MessageParcel & data,MessageParcel & reply)352 int32_t AVSessionServiceStub::HandleStartCast(MessageParcel& data, MessageParcel& reply)
353 {
354 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
355 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStartCast");
356 SLOGI("HandleStartCast start");
357 SessionToken sessionToken {};
358 sessionToken.sessionId = data.ReadString();
359 sessionToken.pid = data.ReadInt32();
360 sessionToken.uid = data.ReadInt32();
361
362 OutputDeviceInfo outputDeviceInfo;
363 int32_t deviceInfoSize;
364 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
365
366 if (deviceInfoSize > RECEIVE_DEVICE_NUM_MAX) {
367 SLOGI("receive deviceNum over range");
368 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
369 return ERR_NONE;
370 }
371 for (int i = 0; i < deviceInfoSize; i++) {
372 DeviceInfo deviceInfo;
373 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
374 CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
375 CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
376 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
377 CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
378 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
379 outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
380 }
381
382 int32_t ret = StartCast(sessionToken, outputDeviceInfo);
383 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StartCast failed");
384 SLOGI("StartCast ret %{public}d", ret);
385 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
386 SLOGI("HandleStartCast success");
387 #else
388 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
389 #endif
390 return ERR_NONE;
391 }
392
HandleStopCast(MessageParcel & data,MessageParcel & reply)393 int32_t AVSessionServiceStub::HandleStopCast(MessageParcel& data, MessageParcel& reply)
394 {
395 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
396 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::HandleStopCast");
397 SLOGI("HandleStopCast start");
398 SessionToken sessionToken {};
399 sessionToken.sessionId = data.ReadString();
400 sessionToken.pid = data.ReadInt32();
401 sessionToken.uid = data.ReadInt32();
402
403 int32_t ret = StopCast(sessionToken);
404 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "StopCast failed");
405 SLOGI("StopCast ret %{public}d", ret);
406 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
407 SLOGI("HandleStopCast success");
408 #else
409 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "WriteInt32 result failed");
410 #endif
411 return ERR_NONE;
412 }
413 } // namespace OHOS::AVSession
414