• 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_, "", "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     SLOGI("enter");
109     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, 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 
SetAVCallMetaData(const AVCallMetaData & avCallMetaData)131 int32_t AVSessionProxy::SetAVCallMetaData(const AVCallMetaData& avCallMetaData)
132 {
133     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVCallMetaData");
134     CHECK_AND_RETURN_RET_LOG(avCallMetaData.IsValid(), ERR_INVALID_PARAM, "invalid call meta data");
135     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, 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(&avCallMetaData),
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_AVCALL_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 
SetAVCallState(const AVCallState & avCallState)152 int32_t AVSessionProxy::SetAVCallState(const AVCallState& avCallState)
153 {
154     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVCallState");
155     CHECK_AND_RETURN_RET_LOG(avCallState.IsValid(), ERR_INVALID_PARAM, "av call state not valid");
156     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
157     MessageParcel data;
158     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
159         ERR_MARSHALLING, "write interface token failed");
160     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&avCallState),
161         ERR_MARSHALLING, "Write state failed");
162     MessageParcel reply;
163     MessageOption option;
164     auto remote = Remote();
165     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
166     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_AVCALL_STATE, data, reply, option) == 0,
167         ERR_IPC_SEND_REQUEST, "send request failed");
168 
169     int32_t ret = AVSESSION_ERROR;
170     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
171 }
172 
173 
GetPixelMapBuffer(AVMetaData & metaData,MessageParcel & data)174 int32_t AVSessionProxy::GetPixelMapBuffer(AVMetaData& metaData, MessageParcel& data)
175 {
176     int mediaImageLength = 0;
177     std::vector<uint8_t> mediaImageBuffer;
178     std::shared_ptr<AVSessionPixelMap> mediaPixelMap = metaData.GetMediaImage();
179     if (mediaPixelMap != nullptr) {
180         mediaImageBuffer = mediaPixelMap->GetInnerImgBuffer();
181         mediaImageLength = static_cast<int>(mediaImageBuffer.size());
182         metaData.SetMediaLength(mediaImageLength);
183     }
184 
185     int avQueueImageLength = 0;
186     std::vector<uint8_t> avQueueImageBuffer;
187     std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData.GetAVQueueImage();
188     if (avQueuePixelMap != nullptr) {
189         avQueueImageBuffer = avQueuePixelMap->GetInnerImgBuffer();
190         avQueueImageLength = static_cast<int>(avQueueImageBuffer.size());
191         metaData.SetAVQueueLength(avQueueImageLength);
192     }
193 
194     int twoImageLength = mediaImageLength + avQueueImageLength;
195     if (twoImageLength == 0) {
196         return 0;
197     }
198 
199     unsigned char *buffer = new (std::nothrow) unsigned char[twoImageLength];
200     if (buffer == nullptr) {
201         SLOGE("new buffer failed of length = %{public}d", twoImageLength);
202         return -1;
203     }
204 
205     for (int i = 0; i < mediaImageLength; i++) {
206         buffer[i] = mediaImageBuffer[i];
207     }
208 
209     for (int j = mediaImageLength, k = 0; j < twoImageLength && k < avQueueImageLength; j++, k++) {
210         buffer[j] = avQueueImageBuffer[k];
211     }
212 
213     if (!data.WriteInt32(twoImageLength) || !AVMetaData::MarshallingExceptImg(data, metaData) ||
214         !data.WriteRawData(buffer, twoImageLength)) {
215         SLOGE("fail to write parcel");
216         delete[] buffer;
217         return -1;
218     }
219     delete[] buffer;
220     return twoImageLength;
221 }
222 
SetAVMetaData(const AVMetaData & meta)223 int32_t AVSessionProxy::SetAVMetaData(const AVMetaData& meta)
224 {
225     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVMetaData");
226     CHECK_AND_RETURN_RET_LOG(meta.IsValid(), ERR_INVALID_PARAM, "invalid meta data");
227     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
228     MessageParcel data;
229     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
230         ERR_MARSHALLING, "write interface token failed");
231     MessageParcel reply;
232     MessageOption option;
233     auto remote = Remote();
234     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
235 
236     AVMetaData metaData;
237     CHECK_AND_RETURN_RET_LOG(metaData.CopyFrom(meta), AVSESSION_ERROR, "avmetadata CopyFrom error");
238     int twoImageLength = GetPixelMapBuffer(metaData, data);
239     if (twoImageLength == 0) {
240         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(twoImageLength), ERR_MARSHALLING, "write twoImageLength failed");
241         CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&meta), ERR_MARSHALLING, "write AVMetaData failed");
242         CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) == 0,
243             ERR_IPC_SEND_REQUEST, "send request failed");
244 
245         int32_t ret = AVSESSION_ERROR;
246         return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
247     }
248 
249     if (twoImageLength == -1) {
250         SLOGE("fail to write parcel");
251         return AVSESSION_ERROR;
252     }
253 
254     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) == 0,
255         ERR_IPC_SEND_REQUEST, "send request failed");
256 
257     int32_t ret = AVSESSION_ERROR;
258     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
259 }
260 
GetAVMetaData(AVMetaData & meta)261 int32_t AVSessionProxy::GetAVMetaData(AVMetaData& meta)
262 {
263     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
264     MessageParcel data;
265     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
266                              ERR_MARSHALLING, "write interface token failed");
267     MessageParcel reply;
268     MessageOption option;
269     auto remote = Remote();
270     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
271     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_META_DATA, data, reply, option) == 0,
272                              ERR_IPC_SEND_REQUEST, "send request failed");
273 
274     int32_t ret = AVSESSION_ERROR;
275     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
276     if (ret == AVSESSION_SUCCESS) {
277         std::shared_ptr<AVMetaData> metaData(reply.ReadParcelable<AVMetaData>());
278         CHECK_AND_RETURN_RET_LOG(metaData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
279         meta = *metaData;
280     }
281     return ret;
282 }
283 
SetAVQueueItems(const std::vector<AVQueueItem> & items)284 int32_t AVSessionProxy::SetAVQueueItems(const std::vector<AVQueueItem>& items)
285 {
286     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueItems");
287     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
288     MessageParcel data;
289     data.SetMaxCapacity(DEFAULT_IPC_CAPACITY);
290     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
291         ERR_MARSHALLING, "write interface token failed");
292     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), ERR_MARSHALLING, "write items num int32 failed");
293     for (auto &parcelable : items) {
294         CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), ERR_MARSHALLING, "Write items failed");
295     }
296     MessageParcel reply;
297     MessageOption option;
298     auto remote = Remote();
299     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
300     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_ITEMS, data, reply, option) == 0,
301         ERR_IPC_SEND_REQUEST, "send request failed");
302 
303     int32_t ret = AVSESSION_ERROR;
304     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
305 }
306 
GetAVQueueItems(std::vector<AVQueueItem> & items)307 int32_t AVSessionProxy::GetAVQueueItems(std::vector<AVQueueItem>& items)
308 {
309     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
310     MessageParcel data;
311     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
312                              ERR_MARSHALLING, "write interface token failed");
313     MessageParcel reply;
314     MessageOption option;
315     auto remote = Remote();
316     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
317     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_ITEMS, data, reply, option) == 0,
318                              ERR_IPC_SEND_REQUEST, "send request failed");
319 
320     int32_t ret = AVSESSION_ERROR;
321     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
322     if (ret == AVSESSION_SUCCESS) {
323         std::vector<AVQueueItem> items_;
324         int32_t itemNum = reply.ReadInt32();
325         CHECK_AND_RETURN_RET_LOG(itemNum >= 0, ERR_UNMARSHALLING, "read int32 itemNum failed");
326         for (int32_t i = 0; i < itemNum; i++) {
327             AVQueueItem *item = reply.ReadParcelable<AVQueueItem>();
328             CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
329             items_.emplace_back(*item);
330             delete item;
331         }
332         items = items_;
333     }
334     return ret;
335 }
336 
GetAVQueueTitle(std::string & title)337 int32_t AVSessionProxy::GetAVQueueTitle(std::string& title)
338 {
339     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
340     MessageParcel data;
341     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
342         ERR_MARSHALLING, "write interface token failed");
343     MessageParcel reply;
344     MessageOption option;
345     auto remote = Remote();
346     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
347     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_TITLE, data, reply, option) == 0,
348         ERR_IPC_SEND_REQUEST, "send request failed");
349 
350     int32_t ret = AVSESSION_ERROR;
351     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
352     if (ret == AVSESSION_SUCCESS) {
353         std::string title_;
354         CHECK_AND_RETURN_RET_LOG(reply.ReadString(title), ERR_UNMARSHALLING, "read title string failed");
355         title = title_;
356     }
357     return ret;
358 }
359 
SetAVQueueTitle(const std::string & title)360 int32_t AVSessionProxy::SetAVQueueTitle(const std::string& title)
361 {
362     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueTitle");
363     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
364     MessageParcel data;
365     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
366         ERR_MARSHALLING, "write interface token failed");
367     CHECK_AND_RETURN_RET_LOG(data.WriteString(title),
368         ERR_MARSHALLING, "Write state failed");
369     MessageParcel reply;
370     MessageOption option;
371     auto remote = Remote();
372     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
373     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_TITLE, data, reply, option) == 0,
374         ERR_IPC_SEND_REQUEST, "send request failed");
375 
376     int32_t ret = AVSESSION_ERROR;
377     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
378 }
379 
GetAVPlaybackState(AVPlaybackState & state)380 int32_t AVSessionProxy::GetAVPlaybackState(AVPlaybackState& state)
381 {
382     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
383     MessageParcel data;
384     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
385         ERR_MARSHALLING, "write interface token failed");
386     MessageParcel reply;
387     MessageOption option;
388     auto remote = Remote();
389     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
390     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_PLAYBACK_STATE, data, reply, option) == 0,
391         ERR_IPC_SEND_REQUEST, "send request failed");
392 
393     int32_t ret = AVSESSION_ERROR;
394     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
395     if (ret == AVSESSION_SUCCESS) {
396         std::shared_ptr<AVPlaybackState> playbackState(reply.ReadParcelable<AVPlaybackState>());
397         CHECK_AND_RETURN_RET_LOG(playbackState != nullptr, ERR_UNMARSHALLING, "read playbackState failed");
398         state = *playbackState;
399     }
400     return ret;
401 }
402 
SetAVPlaybackState(const AVPlaybackState & state)403 int32_t AVSessionProxy::SetAVPlaybackState(const AVPlaybackState& state)
404 {
405     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVPlaybackState");
406     CHECK_AND_RETURN_RET_LOG(state.IsValid(), ERR_INVALID_PARAM, "state not valid");
407     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
408     MessageParcel data;
409     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
410         ERR_MARSHALLING, "write interface token failed");
411     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&state),
412         ERR_MARSHALLING, "Write state failed");
413     MessageParcel reply;
414     MessageOption option;
415     auto remote = Remote();
416     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
417     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_PLAYBACK_STATE, data, reply, option) == 0,
418         ERR_IPC_SEND_REQUEST, "send request failed");
419 
420     int32_t ret = AVSESSION_ERROR;
421     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
422 }
423 
SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent & ability)424 int32_t AVSessionProxy::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability)
425 {
426     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
427     MessageParcel data;
428     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
429         ERR_MARSHALLING, "write interface token failed");
430     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&ability),
431         ERR_MARSHALLING, "Write WantAgent failed");
432     MessageParcel reply;
433     MessageOption option;
434     auto remote = Remote();
435     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
436     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_LAUNCH_ABILITY, data, reply, option) == 0,
437         ERR_IPC_SEND_REQUEST, "send request failed");
438 
439     int32_t ret = AVSESSION_ERROR;
440     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
441 }
442 
GetExtras(AAFwk::WantParams & extras)443 int32_t AVSessionProxy::GetExtras(AAFwk::WantParams& extras)
444 {
445     AVSESSION_TRACE_SYNC_START("AVSessionProxy::GetExtras");
446     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
447     MessageParcel data;
448     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
449         ERR_MARSHALLING, "write interface token failed");
450     MessageParcel reply;
451     MessageOption option;
452     auto remote = Remote();
453     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
454     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_EXTRAS, data, reply, option) == 0,
455         ERR_IPC_SEND_REQUEST, "send request failed");
456 
457     int32_t ret = AVSESSION_ERROR;
458     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
459     if (ret == AVSESSION_SUCCESS) {
460         std::shared_ptr<AAFwk::WantParams> extrasData(reply.ReadParcelable<AAFwk::WantParams>());
461         CHECK_AND_RETURN_RET_LOG(extrasData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
462         extras = *extrasData;
463     }
464     return ret;
465 }
466 
SetExtras(const AAFwk::WantParams & extras)467 int32_t AVSessionProxy::SetExtras(const AAFwk::WantParams& extras)
468 {
469     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetExtras");
470     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
471     MessageParcel data;
472     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
473         ERR_MARSHALLING, "write interface token failed");
474     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras),
475         ERR_MARSHALLING, "Write extras failed");
476     MessageParcel reply;
477     MessageOption option;
478     auto remote = Remote();
479     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
480     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_EXTRAS, data, reply, option) == 0,
481         ERR_IPC_SEND_REQUEST, "send request failed");
482 
483     int32_t ret = AVSESSION_ERROR;
484     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
485 }
486 
GetControllerInner()487 sptr<IRemoteObject> AVSessionProxy::GetControllerInner()
488 {
489     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
490     MessageParcel data;
491     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
492                              nullptr, "write interface token failed");
493     MessageParcel reply;
494     MessageOption option;
495     auto remote = Remote();
496     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
497     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_CONTROLLER, data, reply, option) == 0,
498                              nullptr, "send request failed");
499 
500     int32_t ret = AVSESSION_ERROR;
501     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
502     sptr <IRemoteObject> controller = nullptr;
503     if (ret == AVSESSION_SUCCESS) {
504         controller = reply.ReadRemoteObject();
505         CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "read IRemoteObject failed");
506     }
507     return controller;
508 }
509 
GetController()510 std::shared_ptr<AVSessionController> AVSessionProxy::GetController()
511 {
512     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
513     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr || controller_->IsDestroy(), controller_,
514         "controller already exist");
515     sptr <IRemoteObject> object = GetControllerInner();
516     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
517     auto controller = iface_cast<AVSessionControllerProxy>(object);
518     controller_ = std::shared_ptr<AVSessionController>(controller.GetRefPtr(), [holder = controller](const auto*) {});
519     return controller_;
520 }
521 
522 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastControllerInner()523 sptr<IRemoteObject> AVSessionProxy::GetAVCastControllerInner()
524 {
525     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
526     MessageParcel data;
527     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
528                              nullptr, "write interface token failed");
529     MessageParcel reply;
530     MessageOption option;
531     auto remote = Remote();
532     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
533     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_AVCAST_CONTROLLER, data, reply, option) == 0,
534         nullptr, "send request failed");
535 
536     int32_t ret = AVSESSION_ERROR;
537     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
538     sptr <IRemoteObject> castController = nullptr;
539     if (ret == AVSESSION_SUCCESS) {
540         castController = reply.ReadRemoteObject();
541         CHECK_AND_RETURN_RET_LOG(castController != nullptr, nullptr, "read IRemoteObject failed");
542     }
543     return castController;
544 }
545 
GetAVCastController()546 std::shared_ptr<AVCastController> AVSessionProxy::GetAVCastController()
547 {
548     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
549     sptr <IRemoteObject> object = GetAVCastControllerInner();
550     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
551     auto castController = iface_cast<AVCastControllerProxy>(object);
552     castController_ = std::shared_ptr<AVCastController>(castController.GetRefPtr(),
553         [holder = castController](const auto*) {});
554     return castController_;
555 }
556 #endif
557 
Activate()558 int32_t AVSessionProxy::Activate()
559 {
560     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
561     MessageParcel data;
562     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
563         ERR_MARSHALLING, "write interface token failed");
564     MessageParcel reply;
565     MessageOption option;
566     auto remote = Remote();
567     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
568     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ACTIVATE, data, reply, option) == 0,
569         ERR_IPC_SEND_REQUEST, "send request failed");
570 
571     int32_t ret = AVSESSION_ERROR;
572     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
573 }
574 
Deactivate()575 int32_t AVSessionProxy::Deactivate()
576 {
577     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
578     MessageParcel data;
579     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
580         ERR_MARSHALLING, "write interface token failed");
581     MessageParcel reply;
582     MessageOption option;
583     auto remote = Remote();
584     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
585     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DEACTIVATE, data, reply, option) == 0,
586         ERR_IPC_SEND_REQUEST, "send request failed");
587 
588     int32_t ret = AVSESSION_ERROR;
589     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
590 }
591 
IsActive()592 bool AVSessionProxy::IsActive()
593 {
594     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
595     MessageParcel data;
596     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
597         ERR_MARSHALLING, "write interface token failed");
598     MessageParcel reply;
599     MessageOption option;
600     auto remote = Remote();
601     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
602     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ISACTIVE, data, reply, option) == 0,
603         ERR_IPC_SEND_REQUEST, "send request failed");
604 
605     bool ret = false;
606     return reply.ReadBool(ret) ? ret : false;
607 }
608 
AddSupportCommand(const int32_t cmd)609 int32_t AVSessionProxy::AddSupportCommand(const int32_t cmd)
610 {
611     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
612     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
613     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
614     MessageParcel data;
615     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
616         ERR_MARSHALLING, "write interface token failed");
617     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
618         ERR_MARSHALLING, "Write cmd failed");
619     MessageParcel reply;
620     MessageOption option;
621     auto remote = Remote();
622     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
623     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ADD_SUPPORT_COMMAND, data, reply, option) == 0,
624         ERR_IPC_SEND_REQUEST, "send request failed");
625 
626     int32_t ret = AVSESSION_ERROR;
627     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
628 }
629 
DeleteSupportCommand(const int32_t cmd)630 int32_t AVSessionProxy::DeleteSupportCommand(const int32_t cmd)
631 {
632     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
633     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
634     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
635     MessageParcel data;
636     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
637                              ERR_MARSHALLING, "write interface token failed");
638     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
639                              ERR_MARSHALLING, "Write cmd failed");
640     MessageParcel reply;
641     MessageOption option;
642     auto remote = Remote();
643     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
644     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DELETE_SUPPORT_COMMAND, data, reply, option) == 0,\
645                              ERR_IPC_SEND_REQUEST, "send request failed");
646 
647     int32_t ret = AVSESSION_ERROR;
648     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
649 }
650 
SetSessionEvent(const std::string & event,const AAFwk::WantParams & args)651 int32_t AVSessionProxy::SetSessionEvent(const std::string& event, const AAFwk::WantParams& args)
652 {
653     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
654     MessageParcel data;
655     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
656         ERR_MARSHALLING, "write interface token failed");
657     CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
658     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args),
659         ERR_MARSHALLING, "Write Want failed");
660     MessageParcel reply;
661     MessageOption option;
662     auto remote = Remote();
663     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
664     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_SESSION_EVENT, data, reply, option) == 0,
665         ERR_IPC_SEND_REQUEST, "send request failed");
666 
667     int32_t ret = AVSESSION_ERROR;
668     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
669 }
670 
671 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
ReleaseCast()672 int32_t AVSessionProxy::ReleaseCast()
673 {
674     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
675     MessageParcel data;
676     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
677         ERR_MARSHALLING, "write interface token failed");
678     MessageParcel reply;
679     MessageOption option;
680     auto remote = Remote();
681     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
682     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_RELEASE_CAST, data, reply, option) == 0,
683         ERR_IPC_SEND_REQUEST, "send request failed");
684 
685     int32_t ret = AVSESSION_ERROR;
686     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
687 }
688 #endif
689 } // namespace OHOS::AVSession
690