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