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 "avsession_stub.h"
17 #include "avsession_callback_proxy.h"
18 #include "avsession_trace.h"
19
20 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)21 bool AVSessionStub::CheckInterfaceToken(MessageParcel& data)
22 {
23 auto localDescriptor = IAVSession::GetDescriptor();
24 auto remoteDescriptor = data.ReadInterfaceToken();
25 if (remoteDescriptor != localDescriptor) {
26 SLOGI("interface token is not equal");
27 return false;
28 }
29 return true;
30 }
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t AVSessionStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
33 {
34 if (!CheckInterfaceToken(data)) {
35 return AVSESSION_ERROR;
36 }
37 SLOGI("cmd code is %{public}d", code);
38 if (code < SESSION_CMD_MAX) {
39 return (this->*handlers[code])(data, reply);
40 }
41 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42 }
43
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)44 int32_t AVSessionStub::HandleGetSessionId(MessageParcel& data, MessageParcel& reply)
45 {
46 CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionId()), "write int32_t failed");
47 return ERR_NONE;
48 }
49
HandleGetSessionType(MessageParcel & data,MessageParcel & reply)50 int32_t AVSessionStub::HandleGetSessionType(MessageParcel& data, MessageParcel& reply)
51 {
52 CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionType()), "write int32_t failed");
53 return ERR_NONE;
54 }
55
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)56 int32_t AVSessionStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
57 {
58 auto remoteObject = data.ReadRemoteObject();
59 if (remoteObject == nullptr) {
60 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32_t failed");
61 return ERR_NONE;
62 }
63 auto callback = iface_cast<AVSessionCallbackProxy>(remoteObject);
64 CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_NONE, "callback is nullptr");
65 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(RegisterCallbackInner(callback)),
66 ERR_NONE, "write int32_t failed");
67 return ERR_NONE;
68 }
69
HandleDestroy(MessageParcel & data,MessageParcel & reply)70 int32_t AVSessionStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
71 {
72 int32_t ret = Destroy();
73 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32_t failed");
74 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Destroy failed");
75 return ERR_NONE;
76 }
77
HandleSetAVCallMetaData(MessageParcel & data,MessageParcel & reply)78 int32_t AVSessionStub::HandleSetAVCallMetaData(MessageParcel& data, MessageParcel& reply)
79 {
80 AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVCallMetaData");
81 sptr avCallMetaData = data.ReadParcelable<AVCallMetaData>();
82 if (avCallMetaData == nullptr) {
83 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
84 return ERR_NONE;
85 }
86 int32_t ret = SetAVCallMetaData(*avCallMetaData);
87 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
88 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVCallMetaData failed");
89 return ERR_NONE;
90 }
91
HandleSetAVCallState(MessageParcel & data,MessageParcel & reply)92 int32_t AVSessionStub::HandleSetAVCallState(MessageParcel& data, MessageParcel& reply)
93 {
94 AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVCallState");
95 sptr avCallState = data.ReadParcelable<AVCallState>();
96 if (avCallState == nullptr) {
97 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
98 return ERR_NONE;
99 }
100 int32_t ret = SetAVCallState(*avCallState);
101 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
102 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVCallState failed");
103 return ERR_NONE;
104 }
105
HandleGetAVPlaybackState(MessageParcel & data,MessageParcel & reply)106 int32_t AVSessionStub::HandleGetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
107 {
108 AVPlaybackState avPlaybackState;
109 int32_t ret = GetAVPlaybackState(avPlaybackState);
110 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
111 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
112 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&avPlaybackState), ERR_NONE, "write avPlaybackState failed");
113 return ERR_NONE;
114 }
115
HandleSetAVPlaybackState(MessageParcel & data,MessageParcel & reply)116 int32_t AVSessionStub::HandleSetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
117 {
118 AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVPlaybackState");
119 sptr avPlaybackState = data.ReadParcelable<AVPlaybackState>();
120 if (avPlaybackState == nullptr) {
121 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
122 return ERR_NONE;
123 }
124 int32_t ret = SetAVPlaybackState(*avPlaybackState);
125 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
126 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
127 return ERR_NONE;
128 }
129
HandleSetAVMetaData(MessageParcel & data,MessageParcel & reply)130 int32_t AVSessionStub::HandleSetAVMetaData(MessageParcel& data, MessageParcel& reply)
131 {
132 AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVMetaData");
133 int twoImageLength = data.ReadInt32();
134 SLOGD("read length from twoImage %{public}d", twoImageLength);
135 if (twoImageLength <= 0 || twoImageLength > MAX_IMAGE_SIZE) {
136 sptr avMetaData = data.ReadParcelable<AVMetaData>();
137 if (avMetaData == nullptr) {
138 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
139 return ERR_NONE;
140 }
141 int32_t ret = SetAVMetaData(*avMetaData);
142 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
143 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "SetAVMetaData failed");
144 return ERR_NONE;
145 }
146
147 AVMetaData meta;
148 AVMetaData::UnmarshallingExceptImg(data, meta);
149 const char *buffer = nullptr;
150 if ((buffer = reinterpret_cast<const char *>(data.ReadRawData(twoImageLength))) == nullptr) {
151 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
152 SLOGE("read raw data failed, length = %{public}d", twoImageLength);
153 return AVSESSION_ERROR;
154 }
155
156 int mediaImageLength = meta.GetMediaLength();
157 auto mediaPixelMap = new (std::nothrow) AVSessionPixelMap();
158 std::vector<uint8_t> mediaImageBuffer;
159 for (int i = 0; i < mediaImageLength; i++) {
160 mediaImageBuffer.push_back((uint8_t)buffer[i]);
161 }
162 mediaPixelMap->SetInnerImgBuffer(mediaImageBuffer);
163 meta.SetMediaImage(std::shared_ptr<AVSessionPixelMap>(mediaPixelMap));
164
165 if (twoImageLength > mediaImageLength) {
166 auto avQueuePixelMap = new (std::nothrow) AVSessionPixelMap();
167 std::vector<uint8_t> avQueueImageBuffer;
168 for (int i = mediaImageLength; i < twoImageLength; i++) {
169 avQueueImageBuffer.push_back((uint8_t)buffer[i]);
170 }
171 avQueuePixelMap->SetInnerImgBuffer(avQueueImageBuffer);
172 meta.SetAVQueueImage(std::shared_ptr<AVSessionPixelMap>(avQueuePixelMap));
173 }
174 int32_t ret = SetAVMetaData(meta);
175 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
176 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "SetAVMetaData failed");
177 return ERR_NONE;
178 }
179
HandleSetLaunchAbility(MessageParcel & data,MessageParcel & reply)180 int32_t AVSessionStub::HandleSetLaunchAbility(MessageParcel& data, MessageParcel& reply)
181 {
182 sptr want = data.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
183 if (want == nullptr) {
184 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
185 return ERR_NONE;
186 }
187 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetLaunchAbility(*want)), ERR_NONE, "WriteInt32 result failed");
188 return ERR_NONE;
189 }
190
HandleGetAVMetaData(MessageParcel & data,MessageParcel & reply)191 int32_t AVSessionStub::HandleGetAVMetaData(MessageParcel& data, MessageParcel& reply)
192 {
193 AVMetaData avMetaData;
194 int32_t ret = GetAVMetaData(avMetaData);
195 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
196 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVMetaData failed");
197 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&avMetaData), ERR_NONE, "write avMetaData failed");
198 return ERR_NONE;
199 }
200
HandleGetAVQueueItems(MessageParcel & data,MessageParcel & reply)201 int32_t AVSessionStub::HandleGetAVQueueItems(MessageParcel& data, MessageParcel& reply)
202 {
203 std::vector<AVQueueItem> avQueueItems;
204 int32_t ret = GetAVQueueItems(avQueueItems);
205 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
206 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVQueueItems failed");
207 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(avQueueItems.size()), ERR_NONE, "write items num int32 failed");
208 for (auto &parcelable : avQueueItems) {
209 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&parcelable), ERR_NONE, "Write items failed");
210 }
211 return ERR_NONE;
212 }
213
HandleSetAVQueueItems(MessageParcel & data,MessageParcel & reply)214 int32_t AVSessionStub::HandleSetAVQueueItems(MessageParcel& data, MessageParcel& reply)
215 {
216 AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVQueueItems");
217 int32_t maxQueueItemLength = 1000; // The maximum allowed playlist size is 1000
218 std::vector<AVQueueItem> items_;
219 int32_t itemNum = data.ReadInt32();
220 CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum < maxQueueItemLength),
221 ERR_UNMARSHALLING, "read int32 itemNum failed");
222 for (int32_t i = 0; i < itemNum; i++) {
223 AVQueueItem *item = data.ReadParcelable<AVQueueItem>();
224 CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
225 items_.emplace_back(*item);
226 delete item;
227 }
228 int32_t ret = SetAVQueueItems(items_);
229 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
230 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVQueueItems failed");
231 return ERR_NONE;
232 }
233
HandleGetAVQueueTitle(MessageParcel & data,MessageParcel & reply)234 int32_t AVSessionStub::HandleGetAVQueueTitle(MessageParcel& data, MessageParcel& reply)
235 {
236 std::string title;
237 int32_t ret = GetAVQueueTitle(title);
238 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
239 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVQueueTitle failed");
240 CHECK_AND_RETURN_RET_LOG(reply.WriteString(title), ERR_NONE, "write title string failed");
241 return ERR_NONE;
242 }
243
HandleSetAVQueueTitle(MessageParcel & data,MessageParcel & reply)244 int32_t AVSessionStub::HandleSetAVQueueTitle(MessageParcel& data, MessageParcel& reply)
245 {
246 AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVQueueTitle");
247 std::string title;
248 CHECK_AND_RETURN_RET_LOG(data.ReadString(title), ERR_NONE, "read title string failed");
249 int32_t ret = SetAVQueueTitle(title);
250 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
251 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVQueueTitle failed");
252 return ERR_NONE;
253 }
254
HandleGetExtras(MessageParcel & data,MessageParcel & reply)255 int32_t AVSessionStub::HandleGetExtras(MessageParcel& data, MessageParcel& reply)
256 {
257 AVSESSION_TRACE_SYNC_START("AVSessionStub::HandleGetExtras");
258 AAFwk::WantParams extras;
259 int32_t ret = GetExtras(extras);
260 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
261 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetExtras failed");
262 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&extras), ERR_NONE, "write extras failed");
263 return ERR_NONE;
264 }
265
HandleSetExtras(MessageParcel & data,MessageParcel & reply)266 int32_t AVSessionStub::HandleSetExtras(MessageParcel& data, MessageParcel& reply)
267 {
268 AVSESSION_TRACE_SYNC_START("AVSessionStub::HandleSetExtras");
269 sptr extrasWant = data.ReadParcelable<AAFwk::WantParams>();
270 if (extrasWant == nullptr) {
271 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
272 return ERR_NONE;
273 }
274 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetExtras(*extrasWant)), ERR_NONE, "WriteInt32 result failed");
275 return ERR_NONE;
276 }
277
HandleGetController(MessageParcel & data,MessageParcel & reply)278 int32_t AVSessionStub::HandleGetController(MessageParcel& data, MessageParcel& reply)
279 {
280 sptr<IRemoteObject> controller = GetControllerInner();
281 if (controller == nullptr) {
282 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "write int32 failed");
283 return ERR_NONE;
284 }
285 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32 failed");
286 CHECK_AND_RETURN_RET_LOG(reply.WriteRemoteObject(controller), ERR_NONE, "write object failed");
287 return ERR_NONE;
288 }
289
290 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
HandleGetAVCastController(MessageParcel & data,MessageParcel & reply)291 int32_t AVSessionStub::HandleGetAVCastController(MessageParcel& data, MessageParcel& reply)
292 {
293 sptr<IRemoteObject> castController = GetAVCastControllerInner();
294 if (castController == nullptr) {
295 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "write int32 failed");
296 return ERR_NONE;
297 }
298 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32 failed");
299 CHECK_AND_RETURN_RET_LOG(reply.WriteRemoteObject(castController), ERR_NONE, "write object failed");
300 return ERR_NONE;
301 }
302 #endif
303
HandleActivate(MessageParcel & data,MessageParcel & reply)304 int32_t AVSessionStub::HandleActivate(MessageParcel& data, MessageParcel& reply)
305 {
306 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Activate()), ERR_NONE, "WriteInt32 failed");
307 return ERR_NONE;
308 }
309
HandleDeactivate(MessageParcel & data,MessageParcel & reply)310 int32_t AVSessionStub::HandleDeactivate(MessageParcel& data, MessageParcel& reply)
311 {
312 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Deactivate()), ERR_NONE, "WriteInt32 failed");
313 return ERR_NONE;
314 }
315
HandleIsActive(MessageParcel & data,MessageParcel & reply)316 int32_t AVSessionStub::HandleIsActive(MessageParcel& data, MessageParcel& reply)
317 {
318 CHECK_AND_RETURN_RET_LOG(reply.WriteBool(IsActive()), ERR_NONE, "WriteBool failed");
319 return ERR_NONE;
320 }
321
HandleAddSupportCommand(MessageParcel & data,MessageParcel & reply)322 int32_t AVSessionStub::HandleAddSupportCommand(MessageParcel& data, MessageParcel& reply)
323 {
324 int32_t ret = AddSupportCommand(data.ReadInt32());
325 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
326 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "AddSupportCommand failed");
327 return ERR_NONE;
328 }
329
HandleDeleteSupportCommand(MessageParcel & data,MessageParcel & reply)330 int32_t AVSessionStub::HandleDeleteSupportCommand(MessageParcel& data, MessageParcel& reply)
331 {
332 int32_t ret = DeleteSupportCommand(data.ReadInt32());
333 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
334 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "DeleteSupportCommand failed");
335 return ERR_NONE;
336 }
337
HandleSetSessionEvent(MessageParcel & data,MessageParcel & reply)338 int32_t AVSessionStub::HandleSetSessionEvent(MessageParcel& data, MessageParcel& reply)
339 {
340 auto event = data.ReadString();
341 sptr want = data.ReadParcelable<AAFwk::WantParams>();
342 if (want == nullptr) {
343 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
344 return ERR_NONE;
345 }
346 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetSessionEvent(event, *want)), ERR_NONE, "WriteInt32 result failed");
347 return ERR_NONE;
348 }
349
350 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
HandleReleaseCast(MessageParcel & data,MessageParcel & reply)351 int32_t AVSessionStub::HandleReleaseCast(MessageParcel& data, MessageParcel& reply)
352 {
353 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ReleaseCast()), ERR_NONE, "WriteInt32 failed");
354 return ERR_NONE;
355 }
356 #endif
357 } // namespace OHOS::AVSession
358