1 /*
2 * Copyright (c) 2022 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_proxy.h"
17 #include "avcontroller_callback_client.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "avsession_trace.h"
21
22 namespace OHOS::AVSession {
AVSessionControllerProxy(const sptr<IRemoteObject> & impl)23 AVSessionControllerProxy::AVSessionControllerProxy(const sptr<IRemoteObject>& impl)
24 : IRemoteProxy<IAVSessionController>(impl)
25 {
26 SLOGD("construct");
27 }
28
~AVSessionControllerProxy()29 AVSessionControllerProxy::~AVSessionControllerProxy()
30 {
31 SLOGI("destroy");
32 if (callback_) {
33 callback_->RemoveListenerForPlaybackState();
34 }
35 }
36
GetAVCallMetaData(AVCallMetaData & avCallMetaData)37 int32_t AVSessionControllerProxy::GetAVCallMetaData(AVCallMetaData& avCallMetaData)
38 {
39 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
40 MessageParcel parcel;
41 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
42 "write interface token failed");
43
44 auto remote = Remote();
45 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
46 MessageParcel reply;
47 MessageOption option;
48 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AVCALL_META_DATA, parcel, reply, option) == 0,
49 ERR_IPC_SEND_REQUEST, "send request failed");
50
51 int32_t ret = AVSESSION_ERROR;
52 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
53 if (ret == AVSESSION_SUCCESS) {
54 sptr<AVCallMetaData> data = reply.ReadParcelable<AVCallMetaData>();
55 CHECK_AND_RETURN_RET_LOG(data != nullptr, ERR_UNMARSHALLING, "read AVCallMetaData failed");
56 avCallMetaData = *data;
57 }
58 return ret;
59 }
60
GetAVCallState(AVCallState & avCallState)61 int32_t AVSessionControllerProxy::GetAVCallState(AVCallState& avCallState)
62 {
63 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
64 MessageParcel parcel;
65 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
66 "write interface token failed");
67
68 auto remote = Remote();
69 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
70 MessageParcel reply;
71 MessageOption option;
72 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AVCALL_STATE, parcel, reply, option) == 0,
73 ERR_IPC_SEND_REQUEST, "send request failed");
74
75 int32_t ret = AVSESSION_ERROR;
76 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
77 if (ret == AVSESSION_SUCCESS) {
78 sptr<AVCallState> statePtr = reply.ReadParcelable<AVCallState>();
79 CHECK_AND_RETURN_RET_LOG(statePtr != nullptr, ERR_UNMARSHALLING, "read AVCallState failed");
80 avCallState = *statePtr;
81 }
82 return ret;
83 }
84
GetAVPlaybackState(AVPlaybackState & state)85 int32_t AVSessionControllerProxy::GetAVPlaybackState(AVPlaybackState& state)
86 {
87 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
88 MessageParcel parcel;
89 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
90 "write interface token failed");
91
92 auto remote = Remote();
93 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
94 MessageParcel reply;
95 MessageOption option;
96 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_PLAYBACK_STATE, parcel, reply, option) == 0,
97 ERR_IPC_SEND_REQUEST, "send request failed");
98
99 int32_t ret = AVSESSION_ERROR;
100 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
101 if (ret == AVSESSION_SUCCESS) {
102 AVPlaybackState* statePtr = reply.ReadParcelable<AVPlaybackState>();
103 if (statePtr == nullptr) {
104 SLOGE("GetAVPlaybackState: read AVPlaybackState failed");
105 delete statePtr;
106 statePtr = nullptr;
107 return ERR_UNMARSHALLING;
108 }
109 state = *statePtr;
110
111 std::lock_guard lockGuard(currentStateLock_);
112 currentState_ = *statePtr;
113 delete statePtr;
114 statePtr = nullptr;
115 }
116 return ret;
117 }
118
GetAVMetaData(AVMetaData & data)119 int32_t AVSessionControllerProxy::GetAVMetaData(AVMetaData& data)
120 {
121 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
122 MessageParcel parcel;
123 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
124 "write interface token failed");
125
126 auto remote = Remote();
127 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
128 MessageParcel reply;
129 MessageOption option;
130 reply.SetMaxCapacity(defaultIpcCapacity);
131 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_META_DATA, parcel, reply, option) == 0,
132 ERR_IPC_SEND_REQUEST, "send request failed");
133 int32_t ret = AVSESSION_ERROR;
134 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
135 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVMetaData failed");
136
137 int twoImageLength = reply.ReadInt32();
138 if (twoImageLength <= 0 || twoImageLength > maxImageSize) {
139 sptr<AVMetaData> data_ = reply.ReadParcelable<AVMetaData>();
140 CHECK_AND_RETURN_RET_LOG(data_ != nullptr, ERR_UNMARSHALLING, "read AVMetaData failed");
141 data = *data_;
142 return AVSESSION_SUCCESS;
143 }
144
145 AVMetaData::UnmarshallingExceptImg(reply, data);
146 const char *buffer = nullptr;
147 buffer = reinterpret_cast<const char *>(reply.ReadRawData(twoImageLength));
148 if (buffer == nullptr) {
149 SLOGE("read raw data with null, length = %{public}d", twoImageLength);
150 return AVSESSION_SUCCESS;
151 }
152
153 int mediaImageLength = data.GetMediaLength();
154 std::shared_ptr<AVSessionPixelMap> mediaPixelMap = std::make_shared<AVSessionPixelMap>();
155 CHECK_AND_RETURN_RET_LOG(mediaPixelMap != nullptr, AVSESSION_ERROR, "mediaPixelMap new fail");
156 SLOGD("change for-loop to vector init");
157 std::vector<uint8_t> mediaImageBuffer(buffer, buffer + mediaImageLength);
158 mediaPixelMap->SetInnerImgBuffer(mediaImageBuffer);
159 data.SetMediaImage(mediaPixelMap);
160
161 if (twoImageLength > mediaImageLength) {
162 std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = std::make_shared<AVSessionPixelMap>();
163 CHECK_AND_RETURN_RET_LOG(avQueuePixelMap != nullptr, AVSESSION_ERROR, "avQueuePixelMap new fail");
164 std::vector<uint8_t> avQueueImageBuffer(buffer + mediaImageLength, buffer + twoImageLength);
165 avQueuePixelMap->SetInnerImgBuffer(avQueueImageBuffer);
166 data.SetAVQueueImage(avQueuePixelMap);
167 }
168 return AVSESSION_SUCCESS;
169 }
170
GetAVQueueItems(std::vector<AVQueueItem> & items)171 int32_t AVSessionControllerProxy::GetAVQueueItems(std::vector<AVQueueItem>& items)
172 {
173 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
174 MessageParcel parcel;
175 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
176 "write interface token failed");
177
178 auto remote = Remote();
179 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
180 MessageParcel reply;
181 MessageOption option;
182 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_QUEUE_ITEMS, parcel, reply, option) == 0,
183 ERR_IPC_SEND_REQUEST, "send request failed");
184
185 int32_t ret = AVSESSION_ERROR;
186 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
187 if (ret == AVSESSION_SUCCESS) {
188 std::vector<AVQueueItem> items_;
189 int32_t itemNum = reply.ReadInt32();
190 CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum < maxItemNumber), ERR_UNMARSHALLING,
191 "read int32 itemNum failed");
192 for (int32_t i = 0; i < itemNum; i++) {
193 AVQueueItem *item = reply.ReadParcelable<AVQueueItem>();
194 if (item == nullptr) {
195 SLOGE("GetAVQueueItems: read parcelable AVQueueItem failed");
196 delete item;
197 item = nullptr;
198 return ERR_UNMARSHALLING;
199 }
200 items_.emplace_back(*item);
201 delete item;
202 item = nullptr;
203 }
204 items = items_;
205 }
206 return ret;
207 }
208
GetAVQueueTitle(std::string & title)209 int32_t AVSessionControllerProxy::GetAVQueueTitle(std::string& title)
210 {
211 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
212 MessageParcel parcel;
213 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
214 "write interface token failed");
215
216 auto remote = Remote();
217 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
218 MessageParcel reply;
219 MessageOption option;
220 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_QUEUE_TITLE, parcel, reply, option) == 0,
221 ERR_IPC_SEND_REQUEST, "send request failed");
222
223 int32_t ret = AVSESSION_ERROR;
224 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
225 if (ret == AVSESSION_SUCCESS) {
226 std::string title_;
227 CHECK_AND_RETURN_RET_LOG(reply.ReadString(title_), ERR_UNMARSHALLING, "read string failed");
228 title = title_;
229 }
230 return ret;
231 }
232
SkipToQueueItem(int32_t & itemId)233 int32_t AVSessionControllerProxy::SkipToQueueItem(int32_t& itemId)
234 {
235 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
236 MessageParcel parcel;
237 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
238 "write interface token failed");
239 CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(itemId), ERR_MARSHALLING, "write interface token failed");
240 auto remote = Remote();
241 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
242 MessageParcel reply;
243 MessageOption option;
244 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SKIP_TO_QUEUE_ITEM, parcel, reply, option) == 0,
245 ERR_IPC_SEND_REQUEST, "send request failed");
246 int32_t ret = AVSESSION_ERROR;
247 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
248 }
249
GetExtras(AAFwk::WantParams & extras)250 int32_t AVSessionControllerProxy::GetExtras(AAFwk::WantParams& extras)
251 {
252 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
253 MessageParcel parcel;
254 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
255 "write interface token failed");
256
257 auto remote = Remote();
258 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
259 MessageParcel reply;
260 MessageOption option;
261 std::lock_guard lockGuard(controllerProxyLock_);
262 SLOGI("prepare to get extras sendRequest");
263 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "check again controller is destroy");
264 SLOGI("get extras sendRequest");
265 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_EXTRAS, parcel, reply, option) == 0,
266 ERR_IPC_SEND_REQUEST, "send request failed");
267
268 int32_t ret = AVSESSION_ERROR;
269 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
270 if (ret == AVSESSION_SUCCESS) {
271 sptr<AAFwk::WantParams> extras_ = reply.ReadParcelable<AAFwk::WantParams>();
272 CHECK_AND_RETURN_RET_LOG(extras_ != nullptr, ERR_UNMARSHALLING, "read extras failed");
273 extras = *extras_;
274 }
275 return ret;
276 }
277
GetExtrasWithEvent(const std::string & extraEvent,AAFwk::WantParams & extras)278 int32_t AVSessionControllerProxy::GetExtrasWithEvent(const std::string& extraEvent, AAFwk::WantParams& extras)
279 {
280 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
281 MessageParcel parcel;
282 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
283 "write interface token failed");
284 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(extraEvent), ERR_MARSHALLING, "Write extraEvent string failed");
285
286 auto remote = Remote();
287 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
288 MessageParcel reply;
289 MessageOption option;
290 std::lock_guard lockGuard(controllerProxyLock_);
291 SLOGI("prepare to get extras with event sendRequest");
292 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "check again controller is destroy");
293 SLOGI("get extras with event sendRequest");
294 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_EXTRAS_WITH_EVENT, parcel, reply, option) == 0,
295 ERR_IPC_SEND_REQUEST, "send request failed");
296
297 int32_t ret = AVSESSION_ERROR;
298 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
299 if (ret == AVSESSION_SUCCESS) {
300 sptr<AAFwk::WantParams> extras_ = reply.ReadParcelable<AAFwk::WantParams>();
301 CHECK_AND_RETURN_RET_LOG(extras_ != nullptr, ERR_UNMARSHALLING, "read extras failed");
302 extras = *extras_;
303 }
304 return ret;
305 }
306
SendAVKeyEvent(const MMI::KeyEvent & keyEvent)307 int32_t AVSessionControllerProxy::SendAVKeyEvent(const MMI::KeyEvent& keyEvent)
308 {
309 AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendAVKeyEvent");
310 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
311 CHECK_AND_RETURN_RET_LOG(keyEvent.IsValid(), ERR_COMMAND_NOT_SUPPORT, "keyEvent not valid");
312 bool isActive {};
313 CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
314 isActive, ERR_SESSION_DEACTIVE, "session is deactivate");
315
316 MessageParcel parcel;
317 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
318 "write interface token failed");
319 CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(parcel), ERR_MARSHALLING, "write keyEvent failed");
320
321 auto remote = Remote();
322 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
323 MessageParcel reply;
324 MessageOption option;
325 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_AV_KEYEVENT, parcel, reply, option) == 0,
326 ERR_IPC_SEND_REQUEST, "send request failed");
327
328 int32_t ret = AVSESSION_ERROR;
329 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
330 }
331
GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent & ability)332 int32_t AVSessionControllerProxy::GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent& ability)
333 {
334 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
335 MessageParcel parcel;
336 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
337 "write interface token failed");
338
339 auto remote = Remote();
340 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
341 MessageParcel reply;
342 MessageOption option;
343 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_LAUNCH_ABILITY, parcel, reply, option) == 0,
344 ERR_IPC_SEND_REQUEST, "send request failed");
345
346 int32_t ret = AVSESSION_ERROR;
347 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
348 if (ret == AVSESSION_SUCCESS) {
349 sptr<AbilityRuntime::WantAgent::WantAgent> ability_ =
350 reply.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
351 CHECK_AND_RETURN_RET_LOG(ability_ != nullptr, ERR_UNMARSHALLING, "read LaunchAbility failed");
352 ability = *ability_;
353 }
354 return ret;
355 }
356
GetLaunchAbilityInner(AbilityRuntime::WantAgent::WantAgent * & ability)357 int32_t AVSessionControllerProxy::GetLaunchAbilityInner(AbilityRuntime::WantAgent::WantAgent*& ability)
358 {
359 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
360 MessageParcel parcel;
361 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
362 "write interface token failed");
363
364 auto remote = Remote();
365 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
366 MessageParcel reply;
367 MessageOption option;
368 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_LAUNCH_ABILITY, parcel, reply, option) == 0,
369 ERR_IPC_SEND_REQUEST, "send request failed");
370
371 int32_t ret = AVSESSION_ERROR;
372 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
373 if (ret == AVSESSION_SUCCESS) {
374 sptr<AbilityRuntime::WantAgent::WantAgent> ability_ =
375 reply.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
376 CHECK_AND_RETURN_RET_LOG(ability_ != nullptr, ERR_UNMARSHALLING, "read LaunchAbility failed");
377 ability = new AbilityRuntime::WantAgent::WantAgent(*ability_);
378 }
379 return ret;
380 }
381
GetValidCommands(std::vector<int32_t> & cmds)382 int32_t AVSessionControllerProxy::GetValidCommands(std::vector<int32_t>& cmds)
383 {
384 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
385 MessageParcel parcel;
386 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
387 "write interface token failed");
388
389 auto remote = Remote();
390 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
391 MessageParcel reply;
392 MessageOption option;
393 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_VALID_COMMANDS, parcel, reply, option) == 0,
394 ERR_IPC_SEND_REQUEST, "send request failed");
395
396 int32_t ret = AVSESSION_ERROR;
397 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
398 if (ret == AVSESSION_SUCCESS) {
399 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32Vector(&cmds), ERR_UNMARSHALLING, "read int32 vector failed");
400 }
401 return ret;
402 }
403
IsSessionActive(bool & isActive)404 int32_t AVSessionControllerProxy::IsSessionActive(bool& isActive)
405 {
406 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
407 MessageParcel parcel;
408 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
409 "write interface token failed");
410
411 auto remote = Remote();
412 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
413 MessageParcel reply;
414 MessageOption option;
415 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_IS_SESSION_ACTIVE, parcel, reply, option) == 0,
416 ERR_IPC_SEND_REQUEST, "send request failed");
417
418 int32_t ret = AVSESSION_ERROR;
419 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
420 if (ret == AVSESSION_SUCCESS) {
421 CHECK_AND_RETURN_RET_LOG(reply.ReadBool(isActive), ERR_UNMARSHALLING, "read bool failed");
422 }
423 return ret;
424 }
425
SendControlCommand(const AVControlCommand & cmd)426 int32_t AVSessionControllerProxy::SendControlCommand(const AVControlCommand& cmd)
427 {
428 AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendControlCommand");
429 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
430 CHECK_AND_RETURN_RET_LOG(cmd.IsValid(), ERR_COMMAND_NOT_SUPPORT, "command not valid");
431 bool isActive {};
432 CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
433 isActive, ERR_SESSION_DEACTIVE, "session is deactivate");
434 MessageParcel parcel;
435 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
436 "write interface token failed");
437 CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&cmd), ERR_MARSHALLING, "write cmd failed");
438
439 std::lock_guard lockGuard(controllerProxyLock_);
440 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
441 SLOGI("check destroy bef get remote");
442 auto remote = Remote();
443 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
444 MessageParcel reply;
445 MessageOption option;
446
447 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_CONTROL_COMMAND, parcel, reply, option) == 0,
448 ERR_IPC_SEND_REQUEST, "send request failed");
449
450 int32_t ret = AVSESSION_ERROR;
451 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
452 }
453
SendCommonCommand(const std::string & commonCommand,const AAFwk::WantParams & commandArgs)454 int32_t AVSessionControllerProxy::SendCommonCommand(const std::string& commonCommand,
455 const AAFwk::WantParams& commandArgs)
456 {
457 AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendCommonCommand");
458 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "Controller is destroy");
459 bool isActive {};
460 CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
461 isActive, ERR_SESSION_DEACTIVE, "Session is deactivate");
462 MessageParcel parcel;
463 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
464 "Write interface token failed");
465 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(commonCommand), ERR_MARSHALLING, "Write commonCommand string failed");
466 CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&commandArgs),
467 ERR_MARSHALLING, "Write args failed");
468
469 auto remote = Remote();
470 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "Get remote service failed");
471 MessageParcel reply;
472 MessageOption option;
473 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_COMMON_COMMAND, parcel, reply, option) == 0,
474 ERR_IPC_SEND_REQUEST, "Send request failed");
475
476 int32_t ret = AVSESSION_ERROR;
477 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
478 }
479
SetAVCallMetaFilter(const AVCallMetaData::AVCallMetaMaskType & filter)480 int32_t AVSessionControllerProxy::SetAVCallMetaFilter(const AVCallMetaData::AVCallMetaMaskType& filter)
481 {
482 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
483 MessageParcel parcel;
484 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
485 "write interface token failed");
486 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
487
488 auto remote = Remote();
489 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
490 MessageParcel reply;
491 MessageOption option;
492 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_AVCALL_META_FILTER, parcel, reply, option) == 0,
493 ERR_IPC_SEND_REQUEST, "send request failed");
494
495 int32_t ret = AVSESSION_ERROR;
496 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
497 }
498
SetAVCallStateFilter(const AVCallState::AVCallStateMaskType & filter)499 int32_t AVSessionControllerProxy::SetAVCallStateFilter(const AVCallState::AVCallStateMaskType& filter)
500 {
501 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
502 MessageParcel parcel;
503 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
504 "write interface token failed");
505 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
506
507 auto remote = Remote();
508 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
509 MessageParcel reply;
510 MessageOption option;
511 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_AVCALL_STATE_FILTER, parcel, reply, option) == 0,
512 ERR_IPC_SEND_REQUEST, "send request failed");
513
514 int32_t ret = AVSESSION_ERROR;
515 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
516 }
517
SetMetaFilter(const AVMetaData::MetaMaskType & filter)518 int32_t AVSessionControllerProxy::SetMetaFilter(const AVMetaData::MetaMaskType& filter)
519 {
520 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
521 MessageParcel parcel;
522 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
523 "write interface token failed");
524 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
525
526 auto remote = Remote();
527 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
528 MessageParcel reply;
529 MessageOption option;
530 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_META_FILTER, parcel, reply, option) == 0,
531 ERR_IPC_SEND_REQUEST, "send request failed");
532
533 int32_t ret = AVSESSION_ERROR;
534 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
535 }
536
SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType & filter)537 int32_t AVSessionControllerProxy::SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
538 {
539 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
540 MessageParcel parcel;
541 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
542 "write interface token failed");
543 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
544
545 auto remote = Remote();
546 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
547 MessageParcel reply;
548 MessageOption option;
549 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_PLAYBACK_FILTER, parcel, reply, option) == 0,
550 ERR_IPC_SEND_REQUEST, "send request failed");
551
552 int32_t ret = AVSESSION_ERROR;
553 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
554 }
555
RegisterCallback(const std::shared_ptr<AVControllerCallback> & callback)556 int32_t AVSessionControllerProxy::RegisterCallback(const std::shared_ptr<AVControllerCallback>& callback)
557 {
558 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
559
560 callback_ = new(std::nothrow) AVControllerCallbackClient(callback);
561 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_NO_MEMORY, "new AVControllerCallbackClient failed");
562
563 callback_->AddListenerForPlaybackState([this](const AVPlaybackState& state) {
564 std::lock_guard lockGuard(currentStateLock_);
565 currentState_ = state;
566 });
567
568 return RegisterCallbackInner(callback_);
569 }
570
RegisterCallbackInner(const sptr<IRemoteObject> & callback)571 int32_t AVSessionControllerProxy::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
572 {
573 MessageParcel parcel;
574 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
575 "write interface token failed");
576 CHECK_AND_RETURN_RET_LOG(parcel.WriteRemoteObject(callback), ERR_MARSHALLING,
577 "write remote object failed");
578
579 auto remote = Remote();
580 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
581 MessageParcel reply;
582 MessageOption option;
583 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_REGISTER_CALLBACK, parcel, reply, option) == 0,
584 ERR_IPC_SEND_REQUEST, "send request failed");
585
586 int32_t ret = AVSESSION_ERROR;
587 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
588 }
589
Destroy()590 int32_t AVSessionControllerProxy::Destroy()
591 {
592 SLOGI("Proxy received destroy event");
593 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
594 MessageParcel parcel;
595 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
596 "write interface token failed");
597
598 std::lock_guard lockGuard(controllerProxyLock_);
599 SLOGI("check lock bef destroy in");
600 auto remote = Remote();
601 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
602 MessageParcel reply;
603 MessageOption option;
604
605 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_DESTROY, parcel, reply, option) == 0,
606 ERR_IPC_SEND_REQUEST, "send request failed");
607 isDestroy_ = true;
608
609 int32_t ret = AVSESSION_ERROR;
610 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
611 }
612
GetSessionId()613 std::string AVSessionControllerProxy::GetSessionId()
614 {
615 CHECK_AND_RETURN_RET_LOG(!isDestroy_, "", "controller is destroy");
616 MessageParcel parcel;
617 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed");
618
619 auto remote = Remote();
620 CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed");
621 MessageParcel reply;
622 MessageOption option;
623 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_SESSION_ID, parcel, reply, option) == 0,
624 "", "send request failed");
625
626 std::string result;
627 return reply.ReadString(result) ? result : "";
628 }
629
GetRealPlaybackPosition()630 int64_t AVSessionControllerProxy::GetRealPlaybackPosition()
631 {
632 AVPlaybackState::Position position;
633 {
634 std::lock_guard lockGuard(currentStateLock_);
635 position = currentState_.GetPosition();
636 }
637 CHECK_AND_RETURN_RET_LOG(position.updateTime_ > 0, 0, "playbackState not update");
638 auto now = std::chrono::system_clock::now();
639 auto nowMS = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
640
641 int64_t currentSysTime = nowMS.time_since_epoch().count();
642 SLOGI("elapsedTime:%{public}" PRId64 ", currentSysTime:%{public}" PRId64 ", updateTime:%{public}" PRId64,
643 position.elapsedTime_, currentSysTime, position.updateTime_);
644
645 return (position.elapsedTime_ + (currentSysTime - position.updateTime_));
646 }
647
IsDestroy()648 bool AVSessionControllerProxy::IsDestroy()
649 {
650 return isDestroy_;
651 }
652 }
653