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