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