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