• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_proxy.h"
17 #include "avsession_callback_client.h"
18 #include "avsession_controller_proxy.h"
19 #include "ia_v_session_callback.h"
20 #include "avsession_log.h"
21 #include "avsession_errors.h"
22 #include "avsession_trace.h"
23 
24 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
25 #include "avcast_controller_proxy.h"
26 #endif
27 
28 namespace OHOS::AVSession {
AVSessionProxy(const sptr<IRemoteObject> & impl)29 AVSessionProxy::AVSessionProxy(const sptr<IRemoteObject>& impl)
30     : IRemoteProxy<IAVSession>(impl)
31 {
32     SLOGD("construct");
33 }
34 
~AVSessionProxy()35 AVSessionProxy::~AVSessionProxy()
36 {
37     SLOGI("destroy");
38     Destroy();
39 }
40 
GetSessionId()41 std::string AVSessionProxy::GetSessionId()
42 {
43     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, "", "session is destroyed");
44     MessageParcel data;
45     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed");
46     MessageParcel reply;
47     MessageOption option;
48     auto remote = Remote();
49     CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed");
50     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_SESSION_ID, data, reply, option) == 0,
51         "", "send request failed");
52 
53     std::string sessionId;
54     CHECK_AND_RETURN_RET_LOG(reply.ReadString(sessionId), "", "read sessionId failed");
55     return sessionId;
56 }
57 
GetSessionType()58 std::string AVSessionProxy::GetSessionType()
59 {
60     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, "", "session is destroyed");
61     MessageParcel data;
62     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed");
63     MessageParcel reply;
64     MessageOption option;
65     auto remote = Remote();
66     CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed");
67     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_SESSION_TYPE, data, reply, option) == 0,
68         "", "send request failed");
69 
70     std::string sessionType;
71     CHECK_AND_RETURN_RET_LOG(reply.ReadString(sessionType), "", "read sessionType failed");
72     return sessionType;
73 }
74 
RegisterCallback(const std::shared_ptr<AVSessionCallback> & callback)75 int32_t AVSessionProxy::RegisterCallback(const std::shared_ptr<AVSessionCallback>& callback)
76 {
77     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
78     CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
79     callback_ = new(std::nothrow) AVSessionCallbackClient(callback);
80     CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_NO_MEMORY, "new AVSessionCallbackClient failed");
81     if (RegisterCallbackInner(callback_) != AVSESSION_SUCCESS) {
82         callback_.clear();
83         return AVSESSION_ERROR;
84     }
85     return AVSESSION_SUCCESS;
86 }
87 
RegisterCallbackInner(const sptr<IAVSessionCallback> & callback)88 int32_t AVSessionProxy::RegisterCallbackInner(const sptr<IAVSessionCallback>& callback)
89 {
90     MessageParcel data;
91     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
92                              "write interface token failed");
93     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), ERR_MARSHALLING,
94                              "write remote object failed");
95     MessageParcel reply;
96     MessageOption option;
97     auto remote = Remote();
98     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
99     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_REGISTER_CALLBACK, data, reply, option) == 0,
100                              ERR_IPC_SEND_REQUEST, "send request failed");
101 
102     int32_t ret = AVSESSION_ERROR;
103     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
104 }
105 
Destroy()106 int32_t AVSessionProxy::Destroy()
107 {
108     std::lock_guard isDestroyedLockGuard(isDestroyedLock_);
109     SLOGI("enter");
110     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
111     MessageParcel data;
112     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
113         ERR_MARSHALLING, "write interface token failed");
114     MessageParcel reply;
115     MessageOption option;
116     auto remote = Remote();
117     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
118     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DESTROY, data, reply, option) == 0,
119         ERR_IPC_SEND_REQUEST, "send request failed");
120 
121     int32_t ret = AVSESSION_ERROR;
122     if (!reply.ReadInt32(ret)) {
123         return AVSESSION_ERROR;
124     }
125     if (ret == AVSESSION_SUCCESS) {
126         isDestroyed_ = true;
127         controller_ = nullptr;
128     }
129     return ret;
130 }
131 
SetAVCallMetaData(const AVCallMetaData & avCallMetaData)132 int32_t AVSessionProxy::SetAVCallMetaData(const AVCallMetaData& avCallMetaData)
133 {
134     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVCallMetaData");
135     CHECK_AND_RETURN_RET_LOG(avCallMetaData.IsValid(), ERR_INVALID_PARAM, "invalid call meta data");
136     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
137     MessageParcel data;
138     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
139         ERR_MARSHALLING, "write interface token failed");
140     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&avCallMetaData),
141         ERR_MARSHALLING, "WriteParcelable failed");
142     MessageParcel reply;
143     MessageOption option;
144     auto remote = Remote();
145     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
146     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_AVCALL_META_DATA, data, reply, option) == 0,
147         ERR_IPC_SEND_REQUEST, "send request failed");
148 
149     int32_t ret = AVSESSION_ERROR;
150     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
151 }
152 
SetAVCallState(const AVCallState & avCallState)153 int32_t AVSessionProxy::SetAVCallState(const AVCallState& avCallState)
154 {
155     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVCallState");
156     CHECK_AND_RETURN_RET_LOG(avCallState.IsValid(), ERR_INVALID_PARAM, "av call state not valid");
157     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
158     MessageParcel data;
159     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
160         ERR_MARSHALLING, "write interface token failed");
161     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&avCallState),
162         ERR_MARSHALLING, "Write state failed");
163     MessageParcel reply;
164     MessageOption option;
165     auto remote = Remote();
166     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
167     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_AVCALL_STATE, data, reply, option) == 0,
168         ERR_IPC_SEND_REQUEST, "send request failed");
169 
170     int32_t ret = AVSESSION_ERROR;
171     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
172 }
173 
SetAVMetaData(const AVMetaData & meta)174 int32_t AVSessionProxy::SetAVMetaData(const AVMetaData& meta)
175 {
176     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVMetaData");
177     CHECK_AND_RETURN_RET_LOG(meta.IsValid(), ERR_INVALID_PARAM, "invalid meta data");
178 
179     std::lock_guard lockGuard(setMetadataLock_);
180     SLOGI("SetAVMetaData in proxy");
181 
182     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
183     MessageParcel data;
184     data.SetMaxCapacity(defaultIpcCapacity);
185     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
186         ERR_MARSHALLING, "write interface token failed");
187     MessageParcel reply;
188     MessageOption option;
189     auto remote = Remote();
190     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
191 
192     AVMetaData metaData;
193     CHECK_AND_RETURN_RET_LOG(metaData.CopyFrom(meta), AVSESSION_ERROR, "avmetadata CopyFrom error");
194     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&meta), ERR_MARSHALLING, "write AVMetaData failed");
195     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) == 0,
196                              ERR_IPC_SEND_REQUEST, "send request failed");
197     SLOGI("set avmetadata done");
198     int32_t ret = AVSESSION_ERROR;
199     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
200 }
201 
GetAVMetaData(AVMetaData & meta)202 int32_t AVSessionProxy::GetAVMetaData(AVMetaData& meta)
203 {
204     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
205     MessageParcel data;
206     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
207                              ERR_MARSHALLING, "write interface token failed");
208     MessageParcel reply;
209     MessageOption option;
210     reply.SetMaxCapacity(defaultIpcCapacity);
211     auto remote = Remote();
212     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
213     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_META_DATA, data, reply, option) == 0,
214                              ERR_IPC_SEND_REQUEST, "send request failed");
215 
216     int32_t ret = AVSESSION_ERROR;
217     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
218     if (ret == AVSESSION_SUCCESS) {
219         std::shared_ptr<AVMetaData> metaData(reply.ReadParcelable<AVMetaData>());
220         CHECK_AND_RETURN_RET_LOG(metaData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
221         meta = *metaData;
222     }
223     return ret;
224 }
225 
UpdateAVQueueInfo(const AVQueueInfo & info)226 int32_t AVSessionProxy::UpdateAVQueueInfo(const AVQueueInfo& info)
227 {
228     AVSESSION_TRACE_SYNC_START("AVSessionProxy::UpdateAVQueueInfo");
229     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
230     MessageParcel data;
231     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
232         ERR_MARSHALLING, "write interface token failed");
233     MessageParcel reply;
234     MessageOption option;
235     auto remote = Remote();
236     CHECK_AND_RETURN_RET_LOG(info.MarshallingMessageParcel(data), ERR_MARSHALLING, "Write info failed");
237     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_UPDATE_QUEUE_INFO, data, reply, option) == 0,
238         ERR_IPC_SEND_REQUEST, "send request failed");
239     int32_t ret = AVSESSION_ERROR;
240     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
241 }
242 
SetAVQueueItems(const std::vector<AVQueueItem> & items)243 int32_t AVSessionProxy::SetAVQueueItems(const std::vector<AVQueueItem>& items)
244 {
245     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueItems");
246     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
247     MessageParcel data;
248     data.SetMaxCapacity(defaultIpcCapacity);
249     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
250         ERR_MARSHALLING, "write interface token failed");
251     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), ERR_MARSHALLING, "write items num int32 failed");
252     for (auto &parcelable : items) {
253         CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), ERR_MARSHALLING, "Write items failed");
254     }
255     MessageParcel reply;
256     MessageOption option;
257     auto remote = Remote();
258     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
259     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_ITEMS, data, reply, option) == 0,
260         ERR_IPC_SEND_REQUEST, "send request failed");
261 
262     int32_t ret = AVSESSION_ERROR;
263     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
264 }
265 
GetAVQueueItems(std::vector<AVQueueItem> & items)266 int32_t AVSessionProxy::GetAVQueueItems(std::vector<AVQueueItem>& items)
267 {
268     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
269     MessageParcel data;
270     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
271                              ERR_MARSHALLING, "write interface token failed");
272     MessageParcel reply;
273     MessageOption option;
274     auto remote = Remote();
275     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
276     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_ITEMS, data, reply, option) == 0,
277                              ERR_IPC_SEND_REQUEST, "send request failed");
278 
279     int32_t ret = AVSESSION_ERROR;
280     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
281     if (ret == AVSESSION_SUCCESS) {
282         std::vector<AVQueueItem> items_;
283         int32_t maxItemNumber = 1000; // A maximum of 1000 queue items can be processed at a time
284         int32_t itemNum = reply.ReadInt32();
285         CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum < maxItemNumber),
286             ERR_UNMARSHALLING, "read int32 itemNum failed");
287         for (int32_t i = 0; i < itemNum; i++) {
288             AVQueueItem *item = reply.ReadParcelable<AVQueueItem>();
289             if (item == nullptr) {
290                 SLOGE("GetAVQueueItems: read parcelable AVQueueItem failed");
291                 delete item;
292                 item = nullptr;
293                 return ERR_UNMARSHALLING;
294             }
295             items_.emplace_back(*item);
296             delete item;
297             item = nullptr;
298         }
299         items = items_;
300     }
301     return ret;
302 }
303 
GetAVQueueTitle(std::string & title)304 int32_t AVSessionProxy::GetAVQueueTitle(std::string& title)
305 {
306     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
307     MessageParcel data;
308     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
309         ERR_MARSHALLING, "write interface token failed");
310     MessageParcel reply;
311     MessageOption option;
312     auto remote = Remote();
313     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
314     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_TITLE, data, reply, option) == 0,
315         ERR_IPC_SEND_REQUEST, "send request failed");
316 
317     int32_t ret = AVSESSION_ERROR;
318     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
319     if (ret == AVSESSION_SUCCESS) {
320         std::string title_;
321         CHECK_AND_RETURN_RET_LOG(reply.ReadString(title), ERR_UNMARSHALLING, "read title string failed");
322         title = title_;
323     }
324     return ret;
325 }
326 
SetAVQueueTitle(const std::string & title)327 int32_t AVSessionProxy::SetAVQueueTitle(const std::string& title)
328 {
329     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueTitle");
330     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
331     MessageParcel data;
332     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
333         ERR_MARSHALLING, "write interface token failed");
334     CHECK_AND_RETURN_RET_LOG(data.WriteString(title),
335         ERR_MARSHALLING, "Write state failed");
336     MessageParcel reply;
337     MessageOption option;
338     auto remote = Remote();
339     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
340     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_TITLE, data, reply, option) == 0,
341         ERR_IPC_SEND_REQUEST, "send request failed");
342 
343     int32_t ret = AVSESSION_ERROR;
344     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
345 }
346 
SendCustomData(const AAFwk::WantParams & customData)347 int32_t AVSessionProxy::SendCustomData(const AAFwk::WantParams& customData)
348 {
349     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SendCustomData");
350     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
351     MessageParcel data;
352     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
353         ERR_MARSHALLING, "write interface token failed");
354     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&customData),
355         ERR_MARSHALLING, "Write state failed");
356     MessageParcel reply;
357     MessageOption option;
358     auto remote = Remote();
359     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
360     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SEND_CUSTOM_DATA, data, reply, option) == 0,
361         ERR_IPC_SEND_REQUEST, "send request failed");
362 
363     int32_t ret = AVSESSION_ERROR;
364     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
365 }
366 
GetAVPlaybackState(AVPlaybackState & state)367 int32_t AVSessionProxy::GetAVPlaybackState(AVPlaybackState& state)
368 {
369     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
370     MessageParcel data;
371     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
372         ERR_MARSHALLING, "write interface token failed");
373     MessageParcel reply;
374     MessageOption option;
375     auto remote = Remote();
376     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
377     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_PLAYBACK_STATE, data, reply, option) == 0,
378         ERR_IPC_SEND_REQUEST, "send request failed");
379 
380     int32_t ret = AVSESSION_ERROR;
381     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
382     if (ret == AVSESSION_SUCCESS) {
383         std::shared_ptr<AVPlaybackState> playbackState(reply.ReadParcelable<AVPlaybackState>());
384         CHECK_AND_RETURN_RET_LOG(playbackState != nullptr, ERR_UNMARSHALLING, "read playbackState failed");
385         state = *playbackState;
386     }
387     return ret;
388 }
389 
SetAVPlaybackState(const AVPlaybackState & state)390 int32_t AVSessionProxy::SetAVPlaybackState(const AVPlaybackState& state)
391 {
392     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVPlaybackState");
393     CHECK_AND_RETURN_RET_LOG(state.IsValid(), ERR_INVALID_PARAM, "state not valid");
394     std::lock_guard lockGuard(setPlaybackLock_);
395     SLOGI("SetAVPlaybackState:%{public}d", state.GetState());
396 
397     std::lock_guard isDestroyedLockGuard(isDestroyedLock_);
398     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
399     MessageParcel data;
400     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
401         ERR_MARSHALLING, "write interface token failed");
402     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&state),
403         ERR_MARSHALLING, "Write state failed");
404     MessageParcel reply;
405     MessageOption option;
406     auto remote = Remote();
407     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
408     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_PLAYBACK_STATE, data, reply, option) == 0,
409         ERR_IPC_SEND_REQUEST, "send request failed");
410 
411     int32_t ret = AVSESSION_ERROR;
412     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
413 }
414 
SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent & ability)415 int32_t AVSessionProxy::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability)
416 {
417     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
418     MessageParcel data;
419     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
420         ERR_MARSHALLING, "write interface token failed");
421     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&ability),
422         ERR_MARSHALLING, "Write WantAgent failed");
423     MessageParcel reply;
424     MessageOption option;
425     auto remote = Remote();
426     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
427     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_LAUNCH_ABILITY, data, reply, option) == 0,
428         ERR_IPC_SEND_REQUEST, "send request failed");
429 
430     int32_t ret = AVSESSION_ERROR;
431     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
432 }
433 
GetExtras(AAFwk::WantParams & extras)434 int32_t AVSessionProxy::GetExtras(AAFwk::WantParams& extras)
435 {
436     AVSESSION_TRACE_SYNC_START("AVSessionProxy::GetExtras");
437     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
438     MessageParcel data;
439     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
440         ERR_MARSHALLING, "write interface token failed");
441     MessageParcel reply;
442     MessageOption option;
443     auto remote = Remote();
444     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
445     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_EXTRAS, data, reply, option) == 0,
446         ERR_IPC_SEND_REQUEST, "send request failed");
447 
448     int32_t ret = AVSESSION_ERROR;
449     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
450     if (ret == AVSESSION_SUCCESS) {
451         std::shared_ptr<AAFwk::WantParams> extrasData(reply.ReadParcelable<AAFwk::WantParams>());
452         CHECK_AND_RETURN_RET_LOG(extrasData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
453         extras = *extrasData;
454     }
455     return ret;
456 }
457 
SetExtras(const AAFwk::WantParams & extras)458 int32_t AVSessionProxy::SetExtras(const AAFwk::WantParams& extras)
459 {
460     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetExtras");
461     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
462     MessageParcel data;
463     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
464         ERR_MARSHALLING, "write interface token failed");
465     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras),
466         ERR_MARSHALLING, "Write extras failed");
467     MessageParcel reply;
468     MessageOption option;
469     auto remote = Remote();
470     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
471     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_EXTRAS, data, reply, option) == 0,
472         ERR_IPC_SEND_REQUEST, "send request failed");
473 
474     int32_t ret = AVSESSION_ERROR;
475     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
476 }
477 
GetControllerInner()478 sptr<IRemoteObject> AVSessionProxy::GetControllerInner()
479 {
480     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
481     MessageParcel data;
482     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
483                              nullptr, "write interface token failed");
484     MessageParcel reply;
485     MessageOption option;
486     auto remote = Remote();
487     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
488     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_CONTROLLER, data, reply, option) == 0,
489                              nullptr, "send request failed");
490 
491     int32_t ret = AVSESSION_ERROR;
492     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
493     sptr <IRemoteObject> controller = nullptr;
494     if (ret == AVSESSION_SUCCESS) {
495         controller = reply.ReadRemoteObject();
496         CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "read IRemoteObject failed");
497     }
498     return controller;
499 }
500 
GetController()501 std::shared_ptr<AVSessionController> AVSessionProxy::GetController()
502 {
503     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
504     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr || controller_->IsDestroy(), controller_,
505         "controller already exist");
506     sptr <IRemoteObject> object = GetControllerInner();
507     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
508     auto controller = iface_cast<AVSessionControllerProxy>(object);
509     controller_ = std::shared_ptr<AVSessionController>(controller.GetRefPtr(), [holder = controller](const auto*) {});
510     return controller_;
511 }
512 
513 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastControllerInner()514 sptr<IRemoteObject> AVSessionProxy::GetAVCastControllerInner()
515 {
516     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
517     MessageParcel data;
518     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
519                              nullptr, "write interface token failed");
520     MessageParcel reply;
521     MessageOption option;
522     auto remote = Remote();
523     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
524     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_AVCAST_CONTROLLER, data, reply, option) == 0,
525         nullptr, "send request failed");
526 
527     int32_t ret = AVSESSION_ERROR;
528     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
529     sptr <IRemoteObject> castController = nullptr;
530     if (ret == AVSESSION_SUCCESS) {
531         castController = reply.ReadRemoteObject();
532         CHECK_AND_RETURN_RET_LOG(castController != nullptr, nullptr, "read IRemoteObject failed");
533     }
534     return castController;
535 }
536 
GetAVCastController()537 std::shared_ptr<AVCastController> AVSessionProxy::GetAVCastController()
538 {
539     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
540     sptr <IRemoteObject> object = GetAVCastControllerInner();
541     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
542     auto castController = iface_cast<AVCastControllerProxy>(object);
543     castController_ = std::shared_ptr<AVCastController>(castController.GetRefPtr(),
544         [holder = castController](const auto*) {});
545     return castController_;
546 }
547 
StartCastDisplayListener()548 int32_t AVSessionProxy::StartCastDisplayListener()
549 {
550     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
551     MessageParcel data;
552     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
553                              ERR_MARSHALLING, "write interface token failed");
554     MessageParcel reply;
555     MessageOption option;
556     auto remote = Remote();
557     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
558     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_START_CAST_DISPLAY_LISTENER, data, reply, option) == 0,
559         ERR_IPC_SEND_REQUEST, "send request failed");
560 
561     int32_t ret = AVSESSION_ERROR;
562     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
563 }
564 
StopCastDisplayListener()565 int32_t AVSessionProxy::StopCastDisplayListener()
566 {
567     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
568     MessageParcel data;
569     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
570                              ERR_MARSHALLING, "write interface token failed");
571     MessageParcel reply;
572     MessageOption option;
573     auto remote = Remote();
574     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
575     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_STOP_CAST_DISPLAY_LISTENER, data, 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 
GetAllCastDisplays(std::vector<CastDisplayInfo> & castDisplays)582 int32_t AVSessionProxy::GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays)
583 {
584     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
585     MessageParcel data;
586     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
587                              ERR_MARSHALLING, "write interface token failed");
588     MessageParcel reply;
589     MessageOption option;
590     auto remote = Remote();
591     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
592     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_ALL_CAST_DISPLAYS, data, reply, option) == 0,
593         ERR_IPC_SEND_REQUEST, "send request failed");
594 
595     int32_t ret = AVSESSION_ERROR;
596     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_MARSHALLING, "read int32 failed");
597     if (ret == AVSESSION_SUCCESS) {
598         int32_t castDisplayNum = 0;
599         CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(castDisplayNum), ERR_MARSHALLING, "read castDisplayNum failed");
600         CHECK_AND_RETURN_RET_LOG(castDisplayNum > 0, ERR_MARSHALLING, "castDisplayNum is illegal");
601         std::vector<CastDisplayInfo> displays;
602         for (int32_t i = 0; i < castDisplayNum; i++) {
603             CastDisplayInfo castDisplayInfo;
604             int32_t displayState = -1;
605             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(displayState), ERR_MARSHALLING, "read displayState failed");
606             castDisplayInfo.displayState = static_cast<CastDisplayState>(displayState);
607             uint64_t displayId = 0;
608             CHECK_AND_RETURN_RET_LOG(reply.ReadUint64(displayId), ERR_MARSHALLING, "read displayId failed");
609             castDisplayInfo.displayId = displayId;
610             std::string name = "";
611             CHECK_AND_RETURN_RET_LOG(reply.ReadString(name), ERR_MARSHALLING, "read name failed");
612             castDisplayInfo.name = name;
613             int32_t width = -1;
614             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(width), ERR_MARSHALLING, "read width failed");
615             castDisplayInfo.width = width;
616             int32_t height = -1;
617             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(height), ERR_MARSHALLING, "read height failed");
618             castDisplayInfo.height = height;
619             displays.push_back(castDisplayInfo);
620         }
621         castDisplays = displays;
622     }
623     return ret;
624 }
625 #endif
626 
Activate()627 int32_t AVSessionProxy::Activate()
628 {
629     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
630     MessageParcel data;
631     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
632         ERR_MARSHALLING, "write interface token failed");
633     MessageParcel reply;
634     MessageOption option;
635     auto remote = Remote();
636     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
637     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ACTIVATE, data, reply, option) == 0,
638         ERR_IPC_SEND_REQUEST, "send request failed");
639 
640     int32_t ret = AVSESSION_ERROR;
641     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
642 }
643 
Deactivate()644 int32_t AVSessionProxy::Deactivate()
645 {
646     std::lock_guard isDestroyedLockGuard(isDestroyedLock_);
647     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
648     MessageParcel data;
649     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
650         ERR_MARSHALLING, "write interface token failed");
651     MessageParcel reply;
652     MessageOption option;
653     auto remote = Remote();
654     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
655     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DEACTIVATE, data, reply, option) == 0,
656         ERR_IPC_SEND_REQUEST, "send request failed");
657 
658     int32_t ret = AVSESSION_ERROR;
659     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
660 }
661 
IsActive()662 bool AVSessionProxy::IsActive()
663 {
664     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
665     MessageParcel data;
666     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
667         ERR_MARSHALLING, "write interface token failed");
668     MessageParcel reply;
669     MessageOption option;
670     auto remote = Remote();
671     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
672     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ISACTIVE, data, reply, option) == 0,
673         ERR_IPC_SEND_REQUEST, "send request failed");
674 
675     bool ret = false;
676     return reply.ReadBool(ret) ? ret : false;
677 }
678 
AddSupportCommand(const int32_t cmd)679 int32_t AVSessionProxy::AddSupportCommand(const int32_t cmd)
680 {
681     std::lock_guard lockGuard(setCommandLock_);
682     SLOGI("add support command for %{public}d", cmd);
683 
684     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
685     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
686     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
687     MessageParcel data;
688     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
689         ERR_MARSHALLING, "write interface token failed");
690     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
691         ERR_MARSHALLING, "Write cmd failed");
692     MessageParcel reply;
693     MessageOption option;
694     auto remote = Remote();
695     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
696     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ADD_SUPPORT_COMMAND, data, reply, option) == 0,
697         ERR_IPC_SEND_REQUEST, "send request failed");
698 
699     int32_t ret = AVSESSION_ERROR;
700     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
701 }
702 
DeleteSupportCommand(const int32_t cmd)703 int32_t AVSessionProxy::DeleteSupportCommand(const int32_t cmd)
704 {
705     std::lock_guard lockGuard(setCommandLock_);
706     SLOGI("del support command for %{public}d", cmd);
707 
708     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
709     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
710     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
711     MessageParcel data;
712     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
713                              ERR_MARSHALLING, "write interface token failed");
714     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
715                              ERR_MARSHALLING, "Write cmd failed");
716     MessageParcel reply;
717     MessageOption option;
718     auto remote = Remote();
719     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
720     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DELETE_SUPPORT_COMMAND, data, reply, option) == 0,\
721                              ERR_IPC_SEND_REQUEST, "send request failed");
722 
723     int32_t ret = AVSESSION_ERROR;
724     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
725 }
726 
SetSessionEvent(const std::string & event,const AAFwk::WantParams & args)727 int32_t AVSessionProxy::SetSessionEvent(const std::string& event, const AAFwk::WantParams& args)
728 {
729     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
730     MessageParcel data;
731     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
732         ERR_MARSHALLING, "write interface token failed");
733     CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
734     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args),
735         ERR_MARSHALLING, "Write Want failed");
736     MessageParcel reply;
737     MessageOption option;
738     auto remote = Remote();
739     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
740     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_SESSION_EVENT, data, reply, option) == 0,
741         ERR_IPC_SEND_REQUEST, "send request failed");
742 
743     int32_t ret = AVSESSION_ERROR;
744     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
745 }
746 
747 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
ReleaseCast(bool continuePlay)748 int32_t AVSessionProxy::ReleaseCast(bool continuePlay)
749 {
750     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
751     MessageParcel data;
752     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
753         ERR_MARSHALLING, "write interface token failed");
754     MessageParcel reply;
755     MessageOption option;
756     auto remote = Remote();
757     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
758     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_RELEASE_CAST, data, reply, option) == 0,
759         ERR_IPC_SEND_REQUEST, "send request failed");
760 
761     int32_t ret = AVSESSION_ERROR;
762     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
763 }
764 #endif
765 } // namespace OHOS::AVSession
766