• 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 "iavsession_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_ == false, "", "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_ == false, "", "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_ == false, 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     SLOGI("enter");
109     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
110     MessageParcel data;
111     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
112         ERR_MARSHALLING, "write interface token failed");
113     MessageParcel reply;
114     MessageOption option;
115     auto remote = Remote();
116     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
117     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DESTROY, data, reply, option) == 0,
118         ERR_IPC_SEND_REQUEST, "send request failed");
119 
120     int32_t ret = AVSESSION_ERROR;
121     if (!reply.ReadInt32(ret)) {
122         return AVSESSION_ERROR;
123     }
124     if (ret == AVSESSION_SUCCESS) {
125         isDestroyed_ = true;
126         controller_ = nullptr;
127     }
128     return ret;
129 }
130 
SetAVMetaData(const AVMetaData & meta)131 int32_t AVSessionProxy::SetAVMetaData(const AVMetaData& meta)
132 {
133     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVMetaData");
134     CHECK_AND_RETURN_RET_LOG(meta.IsValid(), ERR_INVALID_PARAM, "invalid meta data");
135     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
136     MessageParcel data;
137     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
138         ERR_MARSHALLING, "write interface token failed");
139     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&meta),
140         ERR_MARSHALLING, "WriteParcelable failed");
141     MessageParcel reply;
142     MessageOption option;
143     auto remote = Remote();
144     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
145     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) == 0,
146         ERR_IPC_SEND_REQUEST, "send request failed");
147 
148     int32_t ret = AVSESSION_ERROR;
149     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
150 }
151 
GetAVMetaData(AVMetaData & meta)152 int32_t AVSessionProxy::GetAVMetaData(AVMetaData& meta)
153 {
154     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
155     MessageParcel data;
156     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
157                              ERR_MARSHALLING, "write interface token failed");
158     MessageParcel reply;
159     MessageOption option;
160     auto remote = Remote();
161     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
162     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_META_DATA, data, reply, option) == 0,
163                              ERR_IPC_SEND_REQUEST, "send request failed");
164 
165     int32_t ret = AVSESSION_ERROR;
166     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
167     if (ret == AVSESSION_SUCCESS) {
168         std::shared_ptr<AVMetaData> metaData(reply.ReadParcelable<AVMetaData>());
169         CHECK_AND_RETURN_RET_LOG(metaData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
170         meta = *metaData;
171     }
172     return ret;
173 }
174 
SetAVQueueItems(const std::vector<AVQueueItem> & items)175 int32_t AVSessionProxy::SetAVQueueItems(const std::vector<AVQueueItem>& items)
176 {
177     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueItems");
178     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
179     MessageParcel data;
180     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
181         ERR_MARSHALLING, "write interface token failed");
182     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), ERR_UNMARSHALLING, "write items num int32 failed");
183     for (auto &parcelable : items) {
184         CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), ERR_UNMARSHALLING, "Write items failed");
185     }
186     MessageParcel reply;
187     MessageOption option;
188     auto remote = Remote();
189     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
190     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_ITEMS, data, reply, option) == 0,
191         ERR_IPC_SEND_REQUEST, "send request failed");
192 
193     int32_t ret = AVSESSION_ERROR;
194     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
195 }
196 
GetAVQueueItems(std::vector<AVQueueItem> & items)197 int32_t AVSessionProxy::GetAVQueueItems(std::vector<AVQueueItem>& items)
198 {
199     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
200     MessageParcel data;
201     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
202                              ERR_MARSHALLING, "write interface token failed");
203     MessageParcel reply;
204     MessageOption option;
205     auto remote = Remote();
206     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
207     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_ITEMS, data, reply, option) == 0,
208                              ERR_IPC_SEND_REQUEST, "send request failed");
209 
210     int32_t ret = AVSESSION_ERROR;
211     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
212     if (ret == AVSESSION_SUCCESS) {
213         std::vector<AVQueueItem> items_;
214         int32_t itemNum = reply.ReadInt32();
215         CHECK_AND_RETURN_RET_LOG(itemNum >= 0, ERR_UNMARSHALLING, "read int32 itemNum failed");
216         for (int32_t i = 0; i < itemNum; i++) {
217             AVQueueItem *item = reply.ReadParcelable<AVQueueItem>();
218             CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
219             items_.emplace_back(*item);
220             delete item;
221         }
222         items = items_;
223     }
224     return ret;
225 }
226 
GetAVQueueTitle(std::string & title)227 int32_t AVSessionProxy::GetAVQueueTitle(std::string& title)
228 {
229     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, 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(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
237     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_TITLE, data, reply, option) == 0,
238         ERR_IPC_SEND_REQUEST, "send request failed");
239 
240     int32_t ret = AVSESSION_ERROR;
241     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
242     if (ret == AVSESSION_SUCCESS) {
243         std::string title_;
244         CHECK_AND_RETURN_RET_LOG(reply.ReadString(title), ERR_UNMARSHALLING, "read title string failed");
245         title = title_;
246     }
247     return ret;
248 }
249 
SetAVQueueTitle(const std::string & title)250 int32_t AVSessionProxy::SetAVQueueTitle(const std::string& title)
251 {
252     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueTitle");
253     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
254     MessageParcel data;
255     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
256         ERR_MARSHALLING, "write interface token failed");
257     CHECK_AND_RETURN_RET_LOG(data.WriteString(title),
258         ERR_MARSHALLING, "Write state failed");
259     MessageParcel reply;
260     MessageOption option;
261     auto remote = Remote();
262     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
263     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_TITLE, data, reply, option) == 0,
264         ERR_IPC_SEND_REQUEST, "send request failed");
265 
266     int32_t ret = AVSESSION_ERROR;
267     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
268 }
269 
GetAVPlaybackState(AVPlaybackState & state)270 int32_t AVSessionProxy::GetAVPlaybackState(AVPlaybackState& state)
271 {
272     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
273     MessageParcel data;
274     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
275         ERR_MARSHALLING, "write interface token failed");
276     MessageParcel reply;
277     MessageOption option;
278     auto remote = Remote();
279     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
280     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_PLAYBACK_STATE, data, reply, option) == 0,
281         ERR_IPC_SEND_REQUEST, "send request failed");
282 
283     int32_t ret = AVSESSION_ERROR;
284     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
285     if (ret == AVSESSION_SUCCESS) {
286         std::shared_ptr<AVPlaybackState> playbackState(reply.ReadParcelable<AVPlaybackState>());
287         CHECK_AND_RETURN_RET_LOG(playbackState != nullptr, ERR_UNMARSHALLING, "read playbackState failed");
288         state = *playbackState;
289     }
290     return ret;
291 }
292 
SetAVPlaybackState(const AVPlaybackState & state)293 int32_t AVSessionProxy::SetAVPlaybackState(const AVPlaybackState& state)
294 {
295     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVPlaybackState");
296     CHECK_AND_RETURN_RET_LOG(state.IsValid(), ERR_INVALID_PARAM, "state not valid");
297     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
298     MessageParcel data;
299     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
300         ERR_MARSHALLING, "write interface token failed");
301     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&state),
302         ERR_MARSHALLING, "Write state failed");
303     MessageParcel reply;
304     MessageOption option;
305     auto remote = Remote();
306     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
307     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_PLAYBACK_STATE, data, reply, option) == 0,
308         ERR_IPC_SEND_REQUEST, "send request failed");
309 
310     int32_t ret = AVSESSION_ERROR;
311     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
312 }
313 
SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent & ability)314 int32_t AVSessionProxy::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability)
315 {
316     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
317     MessageParcel data;
318     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
319         ERR_MARSHALLING, "write interface token failed");
320     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&ability),
321         ERR_MARSHALLING, "Write WantAgent failed");
322     MessageParcel reply;
323     MessageOption option;
324     auto remote = Remote();
325     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
326     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_LAUNCH_ABILITY, data, reply, option) == 0,
327         ERR_IPC_SEND_REQUEST, "send request failed");
328 
329     int32_t ret = AVSESSION_ERROR;
330     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
331 }
332 
GetExtras(AAFwk::WantParams & extras)333 int32_t AVSessionProxy::GetExtras(AAFwk::WantParams& extras)
334 {
335     AVSESSION_TRACE_SYNC_START("AVSessionProxy::GetExtras");
336     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
337     MessageParcel data;
338     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
339         ERR_MARSHALLING, "write interface token failed");
340     MessageParcel reply;
341     MessageOption option;
342     auto remote = Remote();
343     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
344     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_EXTRAS, data, reply, option) == 0,
345         ERR_IPC_SEND_REQUEST, "send request failed");
346 
347     int32_t ret = AVSESSION_ERROR;
348     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
349     if (ret == AVSESSION_SUCCESS) {
350         std::shared_ptr<AAFwk::WantParams> extrasData(reply.ReadParcelable<AAFwk::WantParams>());
351         CHECK_AND_RETURN_RET_LOG(extrasData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
352         extras = *extrasData;
353     }
354     return ret;
355 }
356 
SetExtras(const AAFwk::WantParams & extras)357 int32_t AVSessionProxy::SetExtras(const AAFwk::WantParams& extras)
358 {
359     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetExtras");
360     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
361     MessageParcel data;
362     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
363         ERR_MARSHALLING, "write interface token failed");
364     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras),
365         ERR_MARSHALLING, "Write extras failed");
366     MessageParcel reply;
367     MessageOption option;
368     auto remote = Remote();
369     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
370     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_EXTRAS, data, reply, option) == 0,
371         ERR_IPC_SEND_REQUEST, "send request failed");
372 
373     int32_t ret = AVSESSION_ERROR;
374     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
375 }
376 
GetControllerInner()377 sptr<IRemoteObject> AVSessionProxy::GetControllerInner()
378 {
379     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, nullptr, "session is destroyed");
380     MessageParcel data;
381     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
382                              nullptr, "write interface token failed");
383     MessageParcel reply;
384     MessageOption option;
385     auto remote = Remote();
386     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
387     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_CONTROLLER, data, reply, option) == 0,
388                              nullptr, "send request failed");
389 
390     int32_t ret = AVSESSION_ERROR;
391     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
392     sptr <IRemoteObject> controller = nullptr;
393     if (ret == AVSESSION_SUCCESS) {
394         controller = reply.ReadRemoteObject();
395         CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "read IRemoteObject failed");
396     }
397     return controller;
398 }
399 
GetController()400 std::shared_ptr<AVSessionController> AVSessionProxy::GetController()
401 {
402     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, nullptr, "session is destroyed");
403     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr || controller_->IsDestroy(), controller_,
404         "controller already exist");
405     sptr <IRemoteObject> object = GetControllerInner();
406     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
407     auto controller = iface_cast<AVSessionControllerProxy>(object);
408     controller_ = std::shared_ptr<AVSessionController>(controller.GetRefPtr(), [holder = controller](const auto*) {});
409     return controller_;
410 }
411 
412 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastControllerInner()413 sptr<IRemoteObject> AVSessionProxy::GetAVCastControllerInner()
414 {
415     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, nullptr, "session is destroyed");
416     MessageParcel data;
417     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
418                              nullptr, "write interface token failed");
419     MessageParcel reply;
420     MessageOption option;
421     auto remote = Remote();
422     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
423     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_AVCAST_CONTROLLER, data, reply, option) == 0,
424         nullptr, "send request failed");
425 
426     int32_t ret = AVSESSION_ERROR;
427     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
428     sptr <IRemoteObject> castController = nullptr;
429     if (ret == AVSESSION_SUCCESS) {
430         castController = reply.ReadRemoteObject();
431         CHECK_AND_RETURN_RET_LOG(castController != nullptr, nullptr, "read IRemoteObject failed");
432     }
433     return castController;
434 }
435 
GetAVCastController()436 std::shared_ptr<AVCastController> AVSessionProxy::GetAVCastController()
437 {
438     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, nullptr, "session is destroyed");
439     sptr <IRemoteObject> object = GetAVCastControllerInner();
440     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
441     auto castController = iface_cast<AVCastControllerProxy>(object);
442     castController_ = std::shared_ptr<AVCastController>(castController.GetRefPtr(),
443         [holder = castController](const auto*) {});
444     return castController_;
445 }
446 #endif
447 
Activate()448 int32_t AVSessionProxy::Activate()
449 {
450     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
451     MessageParcel data;
452     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
453         ERR_MARSHALLING, "write interface token failed");
454     MessageParcel reply;
455     MessageOption option;
456     auto remote = Remote();
457     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
458     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ACTIVATE, data, reply, option) == 0,
459         ERR_IPC_SEND_REQUEST, "send request failed");
460 
461     int32_t ret = AVSESSION_ERROR;
462     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
463 }
464 
Deactivate()465 int32_t AVSessionProxy::Deactivate()
466 {
467     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
468     MessageParcel data;
469     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
470         ERR_MARSHALLING, "write interface token failed");
471     MessageParcel reply;
472     MessageOption option;
473     auto remote = Remote();
474     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
475     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DEACTIVATE, data, reply, option) == 0,
476         ERR_IPC_SEND_REQUEST, "send request failed");
477 
478     int32_t ret = AVSESSION_ERROR;
479     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
480 }
481 
IsActive()482 bool AVSessionProxy::IsActive()
483 {
484     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
485     MessageParcel data;
486     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
487         ERR_MARSHALLING, "write interface token failed");
488     MessageParcel reply;
489     MessageOption option;
490     auto remote = Remote();
491     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
492     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ISACTIVE, data, reply, option) == 0,
493         ERR_IPC_SEND_REQUEST, "send request failed");
494 
495     bool ret = false;
496     return reply.ReadBool(ret) ? ret : false;
497 }
498 
AddSupportCommand(const int32_t cmd)499 int32_t AVSessionProxy::AddSupportCommand(const int32_t cmd)
500 {
501     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
502     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
503     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
504     MessageParcel data;
505     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
506         ERR_MARSHALLING, "write interface token failed");
507     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
508         ERR_MARSHALLING, "Write cmd failed");
509     MessageParcel reply;
510     MessageOption option;
511     auto remote = Remote();
512     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
513     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ADD_SUPPORT_COMMAND, data, reply, option) == 0,
514         ERR_IPC_SEND_REQUEST, "send request failed");
515 
516     int32_t ret = AVSESSION_ERROR;
517     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
518 }
519 
DeleteSupportCommand(const int32_t cmd)520 int32_t AVSessionProxy::DeleteSupportCommand(const int32_t cmd)
521 {
522     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
523     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
524     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
525     MessageParcel data;
526     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
527                              ERR_MARSHALLING, "write interface token failed");
528     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
529                              ERR_MARSHALLING, "Write cmd failed");
530     MessageParcel reply;
531     MessageOption option;
532     auto remote = Remote();
533     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
534     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DELETE_SUPPORT_COMMAND, data, reply, option) == 0,\
535                              ERR_IPC_SEND_REQUEST, "send request failed");
536 
537     int32_t ret = AVSESSION_ERROR;
538     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
539 }
540 
SetSessionEvent(const std::string & event,const AAFwk::WantParams & args)541 int32_t AVSessionProxy::SetSessionEvent(const std::string& event, const AAFwk::WantParams& args)
542 {
543     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
544     MessageParcel data;
545     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
546         ERR_MARSHALLING, "write interface token failed");
547     CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
548     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args),
549         ERR_MARSHALLING, "Write Want failed");
550     MessageParcel reply;
551     MessageOption option;
552     auto remote = Remote();
553     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
554     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_SESSION_EVENT, data, reply, option) == 0,
555         ERR_IPC_SEND_REQUEST, "send request failed");
556 
557     int32_t ret = AVSESSION_ERROR;
558     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
559 }
560 
561 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
ReleaseCast()562 int32_t AVSessionProxy::ReleaseCast()
563 {
564     CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
565     MessageParcel data;
566     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
567         ERR_MARSHALLING, "write interface token failed");
568     MessageParcel reply;
569     MessageOption option;
570     auto remote = Remote();
571     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
572     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_RELEASE_CAST, data, reply, option) == 0,
573         ERR_IPC_SEND_REQUEST, "send request failed");
574 
575     int32_t ret = AVSESSION_ERROR;
576     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
577 }
578 #endif
579 } // namespace OHOS::AVSession
580