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
GetAVPlaybackState(AVPlaybackState & state)35 int32_t AVSessionControllerProxy::GetAVPlaybackState(AVPlaybackState& state)
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_AV_PLAYBACK_STATE, 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<AVPlaybackState> state_ = reply.ReadParcelable<AVPlaybackState>();
53 CHECK_AND_RETURN_RET_LOG(state_ != nullptr, ERR_UNMARSHALLING, "read AVPlaybackState failed");
54 state = *state_;
55 currentState_ = *state_;
56 }
57 return ret;
58 }
59
GetAVMetaData(AVMetaData & data)60 int32_t AVSessionControllerProxy::GetAVMetaData(AVMetaData& data)
61 {
62 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
63 MessageParcel parcel;
64 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
65 "write interface token failed");
66
67 auto remote = Remote();
68 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
69 MessageParcel reply;
70 MessageOption option;
71 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_META_DATA, parcel, reply, option) == 0,
72 ERR_IPC_SEND_REQUEST, "send request failed");
73
74 int32_t ret = AVSESSION_ERROR;
75 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
76 if (ret == AVSESSION_SUCCESS) {
77 sptr<AVMetaData> data_ = reply.ReadParcelable<AVMetaData>();
78 CHECK_AND_RETURN_RET_LOG(data_ != nullptr, ERR_UNMARSHALLING, "read AVMetaData failed");
79 data = *data_;
80 }
81 return ret;
82 }
83
GetAVQueueItems(std::vector<AVQueueItem> & items)84 int32_t AVSessionControllerProxy::GetAVQueueItems(std::vector<AVQueueItem>& items)
85 {
86 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
87 MessageParcel parcel;
88 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
89 "write interface token failed");
90
91 auto remote = Remote();
92 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
93 MessageParcel reply;
94 MessageOption option;
95 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_QUEUE_ITEMS, parcel, reply, option) == 0,
96 ERR_IPC_SEND_REQUEST, "send request failed");
97
98 int32_t ret = AVSESSION_ERROR;
99 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
100 if (ret == AVSESSION_SUCCESS) {
101 std::vector<AVQueueItem> items_;
102 int32_t itemNum = reply.ReadInt32();
103 CHECK_AND_RETURN_RET_LOG(itemNum >= 0, ERR_UNMARSHALLING, "read int32 itemNum failed");
104 for (int32_t i = 0; i < itemNum; i++) {
105 AVQueueItem *item = reply.ReadParcelable<AVQueueItem>();
106 CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
107 items_.emplace_back(*item);
108 delete item;
109 }
110 items = items_;
111 }
112 return ret;
113 }
114
GetAVQueueTitle(std::string & title)115 int32_t AVSessionControllerProxy::GetAVQueueTitle(std::string& title)
116 {
117 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
118 MessageParcel parcel;
119 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
120 "write interface token failed");
121
122 auto remote = Remote();
123 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
124 MessageParcel reply;
125 MessageOption option;
126 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_QUEUE_TITLE, parcel, reply, option) == 0,
127 ERR_IPC_SEND_REQUEST, "send request failed");
128
129 int32_t ret = AVSESSION_ERROR;
130 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
131 if (ret == AVSESSION_SUCCESS) {
132 std::string title_;
133 CHECK_AND_RETURN_RET_LOG(reply.ReadString(title_), ERR_UNMARSHALLING, "read string failed");
134 title = title_;
135 }
136 return ret;
137 }
138
SkipToQueueItem(int32_t & itemId)139 int32_t AVSessionControllerProxy::SkipToQueueItem(int32_t& itemId)
140 {
141 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
142 MessageParcel parcel;
143 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
144 "write interface token failed");
145 CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(itemId), ERR_MARSHALLING, "write interface token failed");
146 auto remote = Remote();
147 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
148 MessageParcel reply;
149 MessageOption option;
150 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SKIP_TO_QUEUE_ITEM, parcel, reply, option) == 0,
151 ERR_IPC_SEND_REQUEST, "send request failed");
152 int32_t ret = AVSESSION_ERROR;
153 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
154 }
155
GetExtras(AAFwk::WantParams & extras)156 int32_t AVSessionControllerProxy::GetExtras(AAFwk::WantParams& extras)
157 {
158 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
159 MessageParcel parcel;
160 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
161 "write interface token failed");
162
163 auto remote = Remote();
164 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
165 MessageParcel reply;
166 MessageOption option;
167 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_EXTRAS, parcel, reply, option) == 0,
168 ERR_IPC_SEND_REQUEST, "send request failed");
169
170 int32_t ret = AVSESSION_ERROR;
171 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
172 if (ret == AVSESSION_SUCCESS) {
173 sptr<AAFwk::WantParams> extras_ = reply.ReadParcelable<AAFwk::WantParams>();
174 CHECK_AND_RETURN_RET_LOG(extras_ != nullptr, ERR_UNMARSHALLING, "read extras failed");
175 extras = *extras_;
176 }
177 return ret;
178 }
179
SendAVKeyEvent(const MMI::KeyEvent & keyEvent)180 int32_t AVSessionControllerProxy::SendAVKeyEvent(const MMI::KeyEvent& keyEvent)
181 {
182 AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendAVKeyEvent");
183 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
184 CHECK_AND_RETURN_RET_LOG(keyEvent.IsValid(), ERR_COMMAND_NOT_SUPPORT, "keyEvent not valid");
185 bool isActive {};
186 CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
187 isActive, ERR_SESSION_DEACTIVE, "session is deactivate");
188
189 MessageParcel parcel;
190 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
191 "write interface token failed");
192 CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(parcel), ERR_MARSHALLING, "write keyEvent failed");
193
194 auto remote = Remote();
195 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
196 MessageParcel reply;
197 MessageOption option;
198 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_AV_KEYEVENT, parcel, reply, option) == 0,
199 ERR_IPC_SEND_REQUEST, "send request failed");
200
201 int32_t ret = AVSESSION_ERROR;
202 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
203 }
204
GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent & ability)205 int32_t AVSessionControllerProxy::GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent& ability)
206 {
207 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
208 MessageParcel parcel;
209 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
210 "write interface token failed");
211
212 auto remote = Remote();
213 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
214 MessageParcel reply;
215 MessageOption option;
216 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_LAUNCH_ABILITY, parcel, reply, option) == 0,
217 ERR_IPC_SEND_REQUEST, "send request failed");
218
219 int32_t ret = AVSESSION_ERROR;
220 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
221 if (ret == AVSESSION_SUCCESS) {
222 sptr<AbilityRuntime::WantAgent::WantAgent> ability_ =
223 reply.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
224 CHECK_AND_RETURN_RET_LOG(ability_ != nullptr, ERR_UNMARSHALLING, "read LaunchAbility failed");
225 ability = *ability_;
226 }
227 return ret;
228 }
229
GetValidCommands(std::vector<int32_t> & cmds)230 int32_t AVSessionControllerProxy::GetValidCommands(std::vector<int32_t>& cmds)
231 {
232 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
233 MessageParcel parcel;
234 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
235 "write interface token failed");
236
237 auto remote = Remote();
238 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
239 MessageParcel reply;
240 MessageOption option;
241 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_VALID_COMMANDS, parcel, reply, option) == 0,
242 ERR_IPC_SEND_REQUEST, "send request failed");
243
244 int32_t ret = AVSESSION_ERROR;
245 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
246 if (ret == AVSESSION_SUCCESS) {
247 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32Vector(&cmds), ERR_UNMARSHALLING, "read int32 vector failed");
248 }
249 return ret;
250 }
251
IsSessionActive(bool & isActive)252 int32_t AVSessionControllerProxy::IsSessionActive(bool& isActive)
253 {
254 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
255 MessageParcel parcel;
256 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
257 "write interface token failed");
258
259 auto remote = Remote();
260 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
261 MessageParcel reply;
262 MessageOption option;
263 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_IS_SESSION_ACTIVE, parcel, reply, option) == 0,
264 ERR_IPC_SEND_REQUEST, "send request failed");
265
266 int32_t ret = AVSESSION_ERROR;
267 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
268 if (ret == AVSESSION_SUCCESS) {
269 CHECK_AND_RETURN_RET_LOG(reply.ReadBool(isActive), ERR_UNMARSHALLING, "read bool failed");
270 }
271 return ret;
272 }
273
SendControlCommand(const AVControlCommand & cmd)274 int32_t AVSessionControllerProxy::SendControlCommand(const AVControlCommand& cmd)
275 {
276 AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendControlCommand");
277 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
278 CHECK_AND_RETURN_RET_LOG(cmd.IsValid(), ERR_COMMAND_NOT_SUPPORT, "command not valid");
279 bool isActive {};
280 CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
281 isActive, ERR_SESSION_DEACTIVE, "session is deactivate");
282 MessageParcel parcel;
283 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
284 "write interface token failed");
285 CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&cmd), ERR_MARSHALLING, "write cmd failed");
286
287 auto remote = Remote();
288 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
289 MessageParcel reply;
290 MessageOption option;
291 std::lock_guard lockGuard(controllerProxyLock_);
292 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_CONTROL_COMMAND, parcel, reply, option) == 0,
293 ERR_IPC_SEND_REQUEST, "send request failed");
294
295 int32_t ret = AVSESSION_ERROR;
296 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
297 }
298
SendCommonCommand(const std::string & commonCommand,const AAFwk::WantParams & commandArgs)299 int32_t AVSessionControllerProxy::SendCommonCommand(const std::string& commonCommand,
300 const AAFwk::WantParams& commandArgs)
301 {
302 AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendCommonCommand");
303 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "Controller is destroy");
304 bool isActive {};
305 CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
306 isActive, ERR_SESSION_DEACTIVE, "Session is deactivate");
307 MessageParcel parcel;
308 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
309 "Write interface token failed");
310 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(commonCommand), ERR_MARSHALLING, "Write commonCommand string failed");
311 CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&commandArgs),
312 ERR_MARSHALLING, "Write args failed");
313
314 auto remote = Remote();
315 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "Get remote service failed");
316 MessageParcel reply;
317 MessageOption option;
318 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_COMMON_COMMAND, parcel, reply, option) == 0,
319 ERR_IPC_SEND_REQUEST, "Send request failed");
320
321 int32_t ret = AVSESSION_ERROR;
322 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
323 }
324
SetMetaFilter(const AVMetaData::MetaMaskType & filter)325 int32_t AVSessionControllerProxy::SetMetaFilter(const AVMetaData::MetaMaskType& filter)
326 {
327 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
328 MessageParcel parcel;
329 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
330 "write interface token failed");
331 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
332
333 auto remote = Remote();
334 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
335 MessageParcel reply;
336 MessageOption option;
337 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_META_FILTER, parcel, reply, option) == 0,
338 ERR_IPC_SEND_REQUEST, "send request failed");
339
340 int32_t ret = AVSESSION_ERROR;
341 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
342 }
343
SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType & filter)344 int32_t AVSessionControllerProxy::SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
345 {
346 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
347 MessageParcel parcel;
348 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
349 "write interface token failed");
350 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
351
352 auto remote = Remote();
353 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
354 MessageParcel reply;
355 MessageOption option;
356 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_PLAYBACK_FILTER, parcel, reply, option) == 0,
357 ERR_IPC_SEND_REQUEST, "send request failed");
358
359 int32_t ret = AVSESSION_ERROR;
360 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
361 }
362
RegisterCallback(const std::shared_ptr<AVControllerCallback> & callback)363 int32_t AVSessionControllerProxy::RegisterCallback(const std::shared_ptr<AVControllerCallback>& callback)
364 {
365 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
366
367 sptr<AVControllerCallbackClient> callback_;
368 callback_ = new(std::nothrow) AVControllerCallbackClient(callback);
369 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_NO_MEMORY, "new AVControllerCallbackClient failed");
370
371 callback_->AddListenerForPlaybackState([this](const AVPlaybackState& state) { currentState_ = state; });
372
373 return RegisterCallbackInner(callback_);
374 }
375
RegisterCallbackInner(const sptr<IRemoteObject> & callback)376 int32_t AVSessionControllerProxy::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
377 {
378 MessageParcel parcel;
379 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
380 "write interface token failed");
381 CHECK_AND_RETURN_RET_LOG(parcel.WriteRemoteObject(callback), ERR_MARSHALLING,
382 "write remote object failed");
383
384 auto remote = Remote();
385 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
386 MessageParcel reply;
387 MessageOption option;
388 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_REGISTER_CALLBACK, parcel, reply, option) == 0,
389 ERR_IPC_SEND_REQUEST, "send request failed");
390
391 int32_t ret = AVSESSION_ERROR;
392 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
393 }
394
Destroy()395 int32_t AVSessionControllerProxy::Destroy()
396 {
397 SLOGI("Proxy received destroy event");
398 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
399 MessageParcel parcel;
400 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
401 "write interface token failed");
402
403 auto remote = Remote();
404 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
405 MessageParcel reply;
406 MessageOption option;
407 std::lock_guard lockGuard(controllerProxyLock_);
408 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_DESTROY, parcel, reply, option) == 0,
409 ERR_IPC_SEND_REQUEST, "send request failed");
410 isDestroy_ = true;
411
412 int32_t ret = AVSESSION_ERROR;
413 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
414 }
415
GetSessionId()416 std::string AVSessionControllerProxy::GetSessionId()
417 {
418 CHECK_AND_RETURN_RET_LOG(!isDestroy_, "", "controller is destroy");
419 MessageParcel parcel;
420 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed");
421
422 auto remote = Remote();
423 CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed");
424 MessageParcel reply;
425 MessageOption option;
426 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_SESSION_ID, parcel, reply, option) == 0,
427 "", "send request failed");
428
429 std::string result;
430 return reply.ReadString(result) ? result : "";
431 }
432
GetRealPlaybackPosition()433 int64_t AVSessionControllerProxy::GetRealPlaybackPosition()
434 {
435 auto position = currentState_.GetPosition();
436 CHECK_AND_RETURN_RET_LOG(position.updateTime_ > 0, 0, "playbackState not update");
437 auto now = std::chrono::system_clock::now();
438 auto nowMS = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
439
440 int64_t currentSysTime = nowMS.time_since_epoch().count();
441 SLOGI("elapsedTime:%{public}" PRId64 ", currentSysTime:%{public}" PRId64 ", updateTime:%{public}" PRId64,
442 position.elapsedTime_, currentSysTime, position.updateTime_);
443
444 return (position.elapsedTime_ + (currentSysTime - position.updateTime_));
445 }
446
IsDestroy()447 bool AVSessionControllerProxy::IsDestroy()
448 {
449 return isDestroy_;
450 }
451 }
452