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 "avsession_controller_stub.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "avsession_trace.h"
20 #include "ipc_skeleton.h"
21 #include "session_xcollie.h"
22
23 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)24 bool AVSessionControllerStub::CheckInterfaceToken(MessageParcel& data)
25 {
26 auto localDescriptor = IAVSessionController::GetDescriptor();
27 auto remoteDescriptor = data.ReadInterfaceToken();
28 if (remoteDescriptor != localDescriptor) {
29 SLOGI("interface token is not equal");
30 return false;
31 }
32 return true;
33 }
34
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t AVSessionControllerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
36 MessageOption &option)
37 {
38 CHECK_AND_RETURN_RET_LOG(IPCSkeleton::IsLocalCalling(), AVSESSION_ERROR, "forbid rpc remote request");
39 if (code >= static_cast<uint32_t>(IAVSessionController::CONTROLLER_CMD_REGISTER_CALLBACK)
40 && code < static_cast<uint32_t>(IAVSessionController::CONTROLLER_CMD_MAX)) {
41 SessionXCollie sessionXCollie(mapCodeToFuncNameXCollie[code]);
42 }
43 if (!CheckInterfaceToken(data)) {
44 return AVSESSION_ERROR;
45 }
46 if (code >= static_cast<uint32_t>(IAVSessionController::CONTROLLER_CMD_REGISTER_CALLBACK)
47 && code < static_cast<uint32_t>(IAVSessionController::CONTROLLER_CMD_MAX)) {
48 return handlers[code](data, reply);
49 }
50 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
51 }
52
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)53 int32_t AVSessionControllerStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
54 {
55 auto remoteObject = data.ReadRemoteObject();
56 if (remoteObject == nullptr) {
57 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write RegisterCallback ret failed");
58 } else {
59 CHECK_AND_PRINT_LOG(reply.WriteInt32(RegisterCallbackInner(remoteObject)),
60 "write RegisterCallback ret failed");
61 }
62 return ERR_NONE;
63 }
64
HandleDestroy(MessageParcel & data,MessageParcel & reply)65 int32_t AVSessionControllerStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
66 {
67 CHECK_AND_PRINT_LOG(reply.WriteInt32(Destroy()), "write release() ret failed");
68 return ERR_NONE;
69 }
70
HandleGetAVCallState(MessageParcel & data,MessageParcel & reply)71 int32_t AVSessionControllerStub::HandleGetAVCallState(MessageParcel& data, MessageParcel& reply)
72 {
73 AVCallState state;
74 int32_t ret = GetAVCallState(state);
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.WriteParcelable(&state), "write AVCallState failed");
78 }
79 return ERR_NONE;
80 }
81
HandleGetAVCallMetaData(MessageParcel & data,MessageParcel & reply)82 int32_t AVSessionControllerStub::HandleGetAVCallMetaData(MessageParcel& data, MessageParcel& reply)
83 {
84 AVCallMetaData metaData;
85 int32_t ret = GetAVCallMetaData(metaData);
86 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
87 if (ret == AVSESSION_SUCCESS) {
88 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&metaData), "write AVCallMetaData failed");
89 }
90 return ERR_NONE;
91 }
92
HandleGetAVPlaybackState(MessageParcel & data,MessageParcel & reply)93 int32_t AVSessionControllerStub::HandleGetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
94 {
95 AVPlaybackState state;
96 int32_t ret = GetAVPlaybackState(state);
97 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
98 if (ret == AVSESSION_SUCCESS) {
99 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&state), "write AVPlaybackState failed");
100 }
101 return ERR_NONE;
102 }
103
HandleSendControlCommand(MessageParcel & data,MessageParcel & reply)104 int32_t AVSessionControllerStub::HandleSendControlCommand(MessageParcel& data, MessageParcel& reply)
105 {
106 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendControlCommand");
107 sptr<AVControlCommand> cmd = data.ReadParcelable<AVControlCommand>();
108 if (cmd == nullptr) {
109 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendCommand ret failed");
110 } else {
111 CHECK_AND_PRINT_LOG(reply.WriteInt32(SendControlCommand(*cmd)), "write SendCommand ret failed");
112 }
113 return ERR_NONE;
114 }
115
HandleSendCommonCommand(MessageParcel & data,MessageParcel & reply)116 int32_t AVSessionControllerStub::HandleSendCommonCommand(MessageParcel& data, MessageParcel& reply)
117 {
118 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendCommonCommand");
119 auto commonCommand = data.ReadString();
120 sptr commandArgs = data.ReadParcelable<AAFwk::WantParams>();
121 if (commandArgs == nullptr) {
122 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
123 return ERR_NONE;
124 }
125 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SendCommonCommand(commonCommand, *commandArgs)),
126 ERR_NONE, "WriteInt32 result failed");
127 return ERR_NONE;
128 }
129
HandleSendCustomData(MessageParcel & data,MessageParcel & reply)130 int32_t AVSessionControllerStub::HandleSendCustomData(MessageParcel& data, MessageParcel& reply)
131 {
132 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendCustomData");
133 sptr customData = data.ReadParcelable<AAFwk::WantParams>();
134 if (customData == nullptr) {
135 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
136 return ERR_NONE;
137 }
138 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SendCustomData(*customData)),
139 ERR_NONE, "WriteInt32 result failed");
140 return ERR_NONE;
141 }
142
HandleGetAVMetaData(MessageParcel & data,MessageParcel & reply)143 int32_t AVSessionControllerStub::HandleGetAVMetaData(MessageParcel& data, MessageParcel& reply)
144 {
145 std::lock_guard lockGuard(getMetadataLock_);
146 AVMetaData metaData;
147 int32_t ret = GetAVMetaData(metaData);
148 std::string uri = metaData.GetMediaImageUri();
149 if (ret != ERR_INVALID_PARAM) {
150 SLOGD("ImgSetLoop get controller isFromSession true");
151 } else {
152 ret = AVSESSION_SUCCESS;
153 SLOGD("ImgSetLoop get controller isFromSession false, set empty");
154 metaData.SetMediaImageUri("");
155 }
156 reply.SetMaxCapacity(defaultIpcCapacity);
157 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
158 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVMetaData failed");
159
160 ret = reply.WriteParcelable(&metaData);
161 SLOGD("controllerStub getRes %{public}d", ret);
162 metaData.SetMediaImageUri(uri);
163 DoMetadataImgClean(metaData);
164 return ERR_NONE;
165 }
166
HandleGetAVQueueItems(MessageParcel & data,MessageParcel & reply)167 int32_t AVSessionControllerStub::HandleGetAVQueueItems(MessageParcel& data, MessageParcel& reply)
168 {
169 std::vector<AVQueueItem> items;
170 int32_t ret = GetAVQueueItems(items);
171 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
172 if (ret == AVSESSION_SUCCESS) {
173 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(items.size()), ERR_NONE, "write items num int32 failed");
174 for (auto &parcelable : items) {
175 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&parcelable), ERR_NONE, "Write items failed");
176 }
177 }
178 return ERR_NONE;
179 }
180
HandleGetAVQueueTitle(MessageParcel & data,MessageParcel & reply)181 int32_t AVSessionControllerStub::HandleGetAVQueueTitle(MessageParcel& data, MessageParcel& reply)
182 {
183 std::string title;
184 int32_t ret = GetAVQueueTitle(title);
185 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
186 if (ret == AVSESSION_SUCCESS) {
187 CHECK_AND_PRINT_LOG(reply.WriteString(title), "write title failed");
188 }
189 return ERR_NONE;
190 }
191
HandleSkipToQueueItem(MessageParcel & data,MessageParcel & reply)192 int32_t AVSessionControllerStub::HandleSkipToQueueItem(MessageParcel& data, MessageParcel& reply)
193 {
194 int32_t itemId = 0;
195 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(itemId), ERR_NONE, "read itemId int32 failed");
196 int32_t ret = SkipToQueueItem(itemId);
197 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
198 return ERR_NONE;
199 }
200
HandleGetExtras(MessageParcel & data,MessageParcel & reply)201 int32_t AVSessionControllerStub::HandleGetExtras(MessageParcel& data, MessageParcel& reply)
202 {
203 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::HandleGetExtras");
204 AAFwk::WantParams extras;
205 int32_t ret = GetExtras(extras);
206 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
207 if (ret == AVSESSION_SUCCESS) {
208 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&extras), "write title failed");
209 }
210 return ERR_NONE;
211 }
212
HandleGetExtrasWithEvent(MessageParcel & data,MessageParcel & reply)213 int32_t AVSessionControllerStub::HandleGetExtrasWithEvent(MessageParcel& data, MessageParcel& reply)
214 {
215 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::HandleGetExtrasWithEvent");
216 auto extraEvent = data.ReadString();
217 AAFwk::WantParams extras;
218 int32_t ret = GetExtrasWithEvent(extraEvent, extras);
219 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
220 if (ret == AVSESSION_SUCCESS) {
221 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&extras), "write title failed");
222 }
223 return ERR_NONE;
224 }
225
HandleSendAVKeyEvent(MessageParcel & data,MessageParcel & reply)226 int32_t AVSessionControllerStub::HandleSendAVKeyEvent(MessageParcel& data, MessageParcel& reply)
227 {
228 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendAVKeyEvent");
229 std::shared_ptr<MMI::KeyEvent> event = MMI::KeyEvent::Create();
230 if (event == nullptr) {
231 SLOGI("malloc keyEvent failed");
232 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_NO_MEMORY), "write SendAVKeyEvent ret failed");
233 return ERR_NONE;
234 }
235
236 event->ReadFromParcel(data);
237 if (!event->IsValid()) {
238 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendAVKeyEvent ret failed");
239 } else {
240 CHECK_AND_PRINT_LOG(reply.WriteInt32(SendAVKeyEvent(*(event.get()))),
241 "write SendAVKeyEvent ret failed");
242 }
243 return ERR_NONE;
244 }
245
HandleGetLaunchAbility(MessageParcel & data,MessageParcel & reply)246 int32_t AVSessionControllerStub::HandleGetLaunchAbility(MessageParcel& data, MessageParcel& reply)
247 {
248 AbilityRuntime::WantAgent::WantAgent ability;
249 int32_t ret = GetLaunchAbility(ability);
250 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
251 if (ret == AVSESSION_SUCCESS) {
252 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&ability), "write LaunchAbility failed");
253 }
254 return ERR_NONE;
255 }
256
HandleGetValidCommands(MessageParcel & data,MessageParcel & reply)257 int32_t AVSessionControllerStub::HandleGetValidCommands(MessageParcel& data, MessageParcel& reply)
258 {
259 std::vector<int32_t> cmds;
260 int32_t ret = GetValidCommands(cmds);
261 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
262 if (ret == AVSESSION_SUCCESS) {
263 CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(cmds), "write int32 vector failed");
264 }
265 return ERR_NONE;
266 }
267
HandleSetAVCallMetaFilter(MessageParcel & data,MessageParcel & reply)268 int32_t AVSessionControllerStub::HandleSetAVCallMetaFilter(MessageParcel& data, MessageParcel& reply)
269 {
270 std::string str = data.ReadString();
271 if (str.length() != AVCallMetaData::AVCALL_META_KEY_MAX) {
272 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetAVCallMetaFilter ret failed");
273 return ERR_NONE;
274 }
275 if (str.find_first_not_of("01") != std::string::npos) {
276 SLOGI("mask string not 0 or 1");
277 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
278 return ERR_NONE;
279 }
280 AVCallMetaData::AVCallMetaMaskType filter(str);
281 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetAVCallMetaFilter(filter)), "write int32 failed");
282 return ERR_NONE;
283 }
284
HandleSetAVCallStateFilter(MessageParcel & data,MessageParcel & reply)285 int32_t AVSessionControllerStub::HandleSetAVCallStateFilter(MessageParcel& data, MessageParcel& reply)
286 {
287 std::string str = data.ReadString();
288 if (str.length() != AVCallState::AVCALL_STATE_KEY_MAX) {
289 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetAVCallStateFilter ret failed");
290 return ERR_NONE;
291 }
292 if (str.find_first_not_of("01") != std::string::npos) {
293 SLOGI("mask string not all 0 or 1");
294 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
295 return ERR_NONE;
296 }
297 AVCallState::AVCallStateMaskType filter(str);
298 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetAVCallStateFilter(filter)), "write int32 failed");
299 return ERR_NONE;
300 }
301
HandleSetMetaFilter(MessageParcel & data,MessageParcel & reply)302 int32_t AVSessionControllerStub::HandleSetMetaFilter(MessageParcel& data, MessageParcel& reply)
303 {
304 std::string str = data.ReadString();
305 if (str.length() != AVMetaData::META_KEY_MAX) {
306 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetMetaFilter ret failed");
307 return ERR_NONE;
308 }
309 if (str.find_first_not_of("01") != std::string::npos) {
310 SLOGI("mask string not 0 or 1");
311 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
312 return ERR_NONE;
313 }
314 AVMetaData::MetaMaskType filter(str);
315 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetMetaFilter(filter)), "write int32 failed");
316 return ERR_NONE;
317 }
318
HandleSetPlaybackFilter(MessageParcel & data,MessageParcel & reply)319 int32_t AVSessionControllerStub::HandleSetPlaybackFilter(MessageParcel& data, MessageParcel& reply)
320 {
321 std::string str = data.ReadString();
322 if (str.length() != AVPlaybackState::PLAYBACK_KEY_MAX) {
323 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetPlaybackFilter ret failed");
324 return ERR_NONE;
325 }
326 if (str.find_first_not_of("01") != std::string::npos) {
327 SLOGI("mask string not all 0 or 1");
328 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
329 return ERR_NONE;
330 }
331 AVPlaybackState::PlaybackStateMaskType filter(str);
332 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetPlaybackFilter(filter)), "write int32 failed");
333 return ERR_NONE;
334 }
335
HandleIsSessionActive(MessageParcel & data,MessageParcel & reply)336 int32_t AVSessionControllerStub::HandleIsSessionActive(MessageParcel& data, MessageParcel& reply)
337 {
338 bool isActive = false;
339 int32_t ret = IsSessionActive(isActive);
340 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
341 if (ret == AVSESSION_SUCCESS) {
342 CHECK_AND_PRINT_LOG(reply.WriteBool(isActive), "write bool failed");
343 }
344 return ERR_NONE;
345 }
346
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)347 int32_t AVSessionControllerStub::HandleGetSessionId(MessageParcel& data, MessageParcel& reply)
348 {
349 CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionId()), "write int32_t failed");
350 return ERR_NONE;
351 }
352 } // namespace OHOS::AVSession