• 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     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 
174 
GetPixelMapBuffer(AVMetaData & metaData,MessageParcel & data)175 int32_t AVSessionProxy::GetPixelMapBuffer(AVMetaData& metaData, MessageParcel& data)
176 {
177     int mediaImageLength = 0;
178     std::vector<uint8_t> mediaImageBuffer;
179     std::shared_ptr<AVSessionPixelMap> mediaPixelMap = metaData.GetMediaImage();
180     if (mediaPixelMap != nullptr) {
181         mediaImageBuffer = mediaPixelMap->GetInnerImgBuffer();
182         mediaImageLength = static_cast<int>(mediaImageBuffer.size());
183         metaData.SetMediaLength(mediaImageLength);
184     }
185 
186     int avQueueImageLength = 0;
187     std::vector<uint8_t> avQueueImageBuffer;
188     std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData.GetAVQueueImage();
189     if (avQueuePixelMap != nullptr) {
190         avQueueImageBuffer = avQueuePixelMap->GetInnerImgBuffer();
191         avQueueImageLength = static_cast<int>(avQueueImageBuffer.size());
192         metaData.SetAVQueueLength(avQueueImageLength);
193     }
194 
195     int twoImageLength = mediaImageLength + avQueueImageLength;
196     if (twoImageLength == 0) {
197         return 0;
198     }
199 
200     unsigned char *buffer = new (std::nothrow) unsigned char[twoImageLength];
201     if (buffer == nullptr) {
202         SLOGE("new buffer failed of length = %{public}d", twoImageLength);
203         return -1;
204     }
205 
206     for (int i = 0; i < mediaImageLength; i++) {
207         buffer[i] = mediaImageBuffer[i];
208     }
209 
210     for (int j = mediaImageLength, k = 0; j < twoImageLength && k < avQueueImageLength; j++, k++) {
211         buffer[j] = avQueueImageBuffer[k];
212     }
213 
214     if (!data.WriteInt32(twoImageLength) || !AVMetaData::MarshallingExceptImg(data, metaData)) {
215         SLOGE("fail to write image length & metadata except img");
216         delete[] buffer;
217         return -1;
218     }
219     int32_t retForWriteRawData = data.WriteRawData(buffer, twoImageLength);
220     SLOGI("write img raw data ret %{public}d", retForWriteRawData);
221 
222     delete[] buffer;
223     return twoImageLength;
224 }
225 
SetAVMetaData(const AVMetaData & meta)226 int32_t AVSessionProxy::SetAVMetaData(const AVMetaData& meta)
227 {
228     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVMetaData");
229     CHECK_AND_RETURN_RET_LOG(meta.IsValid(), ERR_INVALID_PARAM, "invalid meta data");
230 
231     std::lock_guard lockGuard(setMetadataLock_);
232     SLOGI("SetAVMetaData in proxy");
233 
234     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
235     MessageParcel data;
236     data.SetMaxCapacity(defaultIpcCapacity);
237     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
238         ERR_MARSHALLING, "write interface token failed");
239     MessageParcel reply;
240     MessageOption option;
241     auto remote = Remote();
242     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
243 
244     AVMetaData metaData;
245     CHECK_AND_RETURN_RET_LOG(metaData.CopyFrom(meta), AVSESSION_ERROR, "avmetadata CopyFrom error");
246     int twoImageLength = GetPixelMapBuffer(metaData, data);
247     if (twoImageLength == 0) {
248         CHECK_AND_RETURN_RET_LOG(data.WriteInt32(twoImageLength), ERR_MARSHALLING, "write twoImageLength failed");
249         CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&meta), ERR_MARSHALLING, "write AVMetaData failed");
250         CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) == 0,
251             ERR_IPC_SEND_REQUEST, "send request failed");
252 
253         int32_t ret = AVSESSION_ERROR;
254         return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
255     }
256 
257     if (twoImageLength == -1) {
258         SLOGE("fail to write parcel");
259         return AVSESSION_ERROR;
260     }
261 
262     if (remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) != 0) {
263         SLOGE("send request fail with raw img");
264         return ERR_IPC_SEND_REQUEST;
265     }
266     SLOGI("set avmetadata done");
267     int32_t ret = AVSESSION_ERROR;
268     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
269 }
270 
GetAVMetaData(AVMetaData & meta)271 int32_t AVSessionProxy::GetAVMetaData(AVMetaData& meta)
272 {
273     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
274     MessageParcel data;
275     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
276                              ERR_MARSHALLING, "write interface token failed");
277     MessageParcel reply;
278     MessageOption option;
279     reply.SetMaxCapacity(defaultIpcCapacity);
280     auto remote = Remote();
281     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
282     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_META_DATA, data, reply, option) == 0,
283                              ERR_IPC_SEND_REQUEST, "send request failed");
284 
285     int32_t ret = AVSESSION_ERROR;
286     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
287     if (ret == AVSESSION_SUCCESS) {
288         std::shared_ptr<AVMetaData> metaData(reply.ReadParcelable<AVMetaData>());
289         CHECK_AND_RETURN_RET_LOG(metaData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
290         meta = *metaData;
291     }
292     return ret;
293 }
294 
SetAVQueueItems(const std::vector<AVQueueItem> & items)295 int32_t AVSessionProxy::SetAVQueueItems(const std::vector<AVQueueItem>& items)
296 {
297     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueItems");
298     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
299     MessageParcel data;
300     data.SetMaxCapacity(defaultIpcCapacity);
301     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
302         ERR_MARSHALLING, "write interface token failed");
303     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), ERR_MARSHALLING, "write items num int32 failed");
304     for (auto &parcelable : items) {
305         CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), ERR_MARSHALLING, "Write items failed");
306     }
307     MessageParcel reply;
308     MessageOption option;
309     auto remote = Remote();
310     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
311     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_ITEMS, data, reply, option) == 0,
312         ERR_IPC_SEND_REQUEST, "send request failed");
313 
314     int32_t ret = AVSESSION_ERROR;
315     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
316 }
317 
GetAVQueueItems(std::vector<AVQueueItem> & items)318 int32_t AVSessionProxy::GetAVQueueItems(std::vector<AVQueueItem>& items)
319 {
320     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
321     MessageParcel data;
322     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
323                              ERR_MARSHALLING, "write interface token failed");
324     MessageParcel reply;
325     MessageOption option;
326     auto remote = Remote();
327     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
328     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_ITEMS, data, reply, option) == 0,
329                              ERR_IPC_SEND_REQUEST, "send request failed");
330 
331     int32_t ret = AVSESSION_ERROR;
332     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
333     if (ret == AVSESSION_SUCCESS) {
334         std::vector<AVQueueItem> items_;
335         int32_t maxItemNumber = 1000; // A maximum of 1000 queue items can be processed at a time
336         int32_t itemNum = reply.ReadInt32();
337         CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum < maxItemNumber),
338             ERR_UNMARSHALLING, "read int32 itemNum failed");
339         for (int32_t i = 0; i < itemNum; i++) {
340             AVQueueItem *item = reply.ReadParcelable<AVQueueItem>();
341             if (item == nullptr) {
342                 SLOGE("GetAVQueueItems: read parcelable AVQueueItem failed");
343                 delete item;
344                 item = nullptr;
345                 return ERR_UNMARSHALLING;
346             }
347             items_.emplace_back(*item);
348             delete item;
349             item = nullptr;
350         }
351         items = items_;
352     }
353     return ret;
354 }
355 
GetAVQueueTitle(std::string & title)356 int32_t AVSessionProxy::GetAVQueueTitle(std::string& title)
357 {
358     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
359     MessageParcel data;
360     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
361         ERR_MARSHALLING, "write interface token failed");
362     MessageParcel reply;
363     MessageOption option;
364     auto remote = Remote();
365     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
366     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_TITLE, data, reply, option) == 0,
367         ERR_IPC_SEND_REQUEST, "send request failed");
368 
369     int32_t ret = AVSESSION_ERROR;
370     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
371     if (ret == AVSESSION_SUCCESS) {
372         std::string title_;
373         CHECK_AND_RETURN_RET_LOG(reply.ReadString(title), ERR_UNMARSHALLING, "read title string failed");
374         title = title_;
375     }
376     return ret;
377 }
378 
SetAVQueueTitle(const std::string & title)379 int32_t AVSessionProxy::SetAVQueueTitle(const std::string& title)
380 {
381     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueTitle");
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     CHECK_AND_RETURN_RET_LOG(data.WriteString(title),
387         ERR_MARSHALLING, "Write state failed");
388     MessageParcel reply;
389     MessageOption option;
390     auto remote = Remote();
391     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
392     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_TITLE, data, reply, option) == 0,
393         ERR_IPC_SEND_REQUEST, "send request failed");
394 
395     int32_t ret = AVSESSION_ERROR;
396     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
397 }
398 
GetAVPlaybackState(AVPlaybackState & state)399 int32_t AVSessionProxy::GetAVPlaybackState(AVPlaybackState& state)
400 {
401     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
402     MessageParcel data;
403     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
404         ERR_MARSHALLING, "write interface token failed");
405     MessageParcel reply;
406     MessageOption option;
407     auto remote = Remote();
408     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
409     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_PLAYBACK_STATE, data, reply, option) == 0,
410         ERR_IPC_SEND_REQUEST, "send request failed");
411 
412     int32_t ret = AVSESSION_ERROR;
413     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
414     if (ret == AVSESSION_SUCCESS) {
415         std::shared_ptr<AVPlaybackState> playbackState(reply.ReadParcelable<AVPlaybackState>());
416         CHECK_AND_RETURN_RET_LOG(playbackState != nullptr, ERR_UNMARSHALLING, "read playbackState failed");
417         state = *playbackState;
418     }
419     return ret;
420 }
421 
SetAVPlaybackState(const AVPlaybackState & state)422 int32_t AVSessionProxy::SetAVPlaybackState(const AVPlaybackState& state)
423 {
424     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVPlaybackState");
425     CHECK_AND_RETURN_RET_LOG(state.IsValid(), ERR_INVALID_PARAM, "state not valid");
426     std::lock_guard lockGuard(setPlaybackLock_);
427     SLOGI("SetAVPlaybackState:%{public}d", state.GetState());
428 
429     std::lock_guard isDestroyedLockGuard(isDestroyedLock_);
430     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
431     MessageParcel data;
432     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
433         ERR_MARSHALLING, "write interface token failed");
434     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&state),
435         ERR_MARSHALLING, "Write state failed");
436     MessageParcel reply;
437     MessageOption option;
438     auto remote = Remote();
439     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
440     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_PLAYBACK_STATE, data, reply, option) == 0,
441         ERR_IPC_SEND_REQUEST, "send request failed");
442 
443     int32_t ret = AVSESSION_ERROR;
444     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
445 }
446 
SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent & ability)447 int32_t AVSessionProxy::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability)
448 {
449     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
450     MessageParcel data;
451     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
452         ERR_MARSHALLING, "write interface token failed");
453     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&ability),
454         ERR_MARSHALLING, "Write WantAgent failed");
455     MessageParcel reply;
456     MessageOption option;
457     auto remote = Remote();
458     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
459     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_LAUNCH_ABILITY, data, reply, option) == 0,
460         ERR_IPC_SEND_REQUEST, "send request failed");
461 
462     int32_t ret = AVSESSION_ERROR;
463     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
464 }
465 
GetExtras(AAFwk::WantParams & extras)466 int32_t AVSessionProxy::GetExtras(AAFwk::WantParams& extras)
467 {
468     AVSESSION_TRACE_SYNC_START("AVSessionProxy::GetExtras");
469     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
470     MessageParcel data;
471     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
472         ERR_MARSHALLING, "write interface token failed");
473     MessageParcel reply;
474     MessageOption option;
475     auto remote = Remote();
476     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
477     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_EXTRAS, data, reply, option) == 0,
478         ERR_IPC_SEND_REQUEST, "send request failed");
479 
480     int32_t ret = AVSESSION_ERROR;
481     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
482     if (ret == AVSESSION_SUCCESS) {
483         std::shared_ptr<AAFwk::WantParams> extrasData(reply.ReadParcelable<AAFwk::WantParams>());
484         CHECK_AND_RETURN_RET_LOG(extrasData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
485         extras = *extrasData;
486     }
487     return ret;
488 }
489 
SetExtras(const AAFwk::WantParams & extras)490 int32_t AVSessionProxy::SetExtras(const AAFwk::WantParams& extras)
491 {
492     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetExtras");
493     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
494     MessageParcel data;
495     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
496         ERR_MARSHALLING, "write interface token failed");
497     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras),
498         ERR_MARSHALLING, "Write extras failed");
499     MessageParcel reply;
500     MessageOption option;
501     auto remote = Remote();
502     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
503     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_EXTRAS, data, reply, option) == 0,
504         ERR_IPC_SEND_REQUEST, "send request failed");
505 
506     int32_t ret = AVSESSION_ERROR;
507     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
508 }
509 
GetControllerInner()510 sptr<IRemoteObject> AVSessionProxy::GetControllerInner()
511 {
512     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
513     MessageParcel data;
514     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
515                              nullptr, "write interface token failed");
516     MessageParcel reply;
517     MessageOption option;
518     auto remote = Remote();
519     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
520     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_CONTROLLER, data, reply, option) == 0,
521                              nullptr, "send request failed");
522 
523     int32_t ret = AVSESSION_ERROR;
524     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
525     sptr <IRemoteObject> controller = nullptr;
526     if (ret == AVSESSION_SUCCESS) {
527         controller = reply.ReadRemoteObject();
528         CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "read IRemoteObject failed");
529     }
530     return controller;
531 }
532 
GetController()533 std::shared_ptr<AVSessionController> AVSessionProxy::GetController()
534 {
535     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
536     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr || controller_->IsDestroy(), controller_,
537         "controller already exist");
538     sptr <IRemoteObject> object = GetControllerInner();
539     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
540     auto controller = iface_cast<AVSessionControllerProxy>(object);
541     controller_ = std::shared_ptr<AVSessionController>(controller.GetRefPtr(), [holder = controller](const auto*) {});
542     return controller_;
543 }
544 
545 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastControllerInner()546 sptr<IRemoteObject> AVSessionProxy::GetAVCastControllerInner()
547 {
548     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
549     MessageParcel data;
550     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
551                              nullptr, "write interface token failed");
552     MessageParcel reply;
553     MessageOption option;
554     auto remote = Remote();
555     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
556     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_AVCAST_CONTROLLER, data, reply, option) == 0,
557         nullptr, "send request failed");
558 
559     int32_t ret = AVSESSION_ERROR;
560     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
561     sptr <IRemoteObject> castController = nullptr;
562     if (ret == AVSESSION_SUCCESS) {
563         castController = reply.ReadRemoteObject();
564         CHECK_AND_RETURN_RET_LOG(castController != nullptr, nullptr, "read IRemoteObject failed");
565     }
566     return castController;
567 }
568 
GetAVCastController()569 std::shared_ptr<AVCastController> AVSessionProxy::GetAVCastController()
570 {
571     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
572     sptr <IRemoteObject> object = GetAVCastControllerInner();
573     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
574     auto castController = iface_cast<AVCastControllerProxy>(object);
575     castController_ = std::shared_ptr<AVCastController>(castController.GetRefPtr(),
576         [holder = castController](const auto*) {});
577     return castController_;
578 }
579 
StartCastDisplayListener()580 int32_t AVSessionProxy::StartCastDisplayListener()
581 {
582     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
583     MessageParcel data;
584     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
585                              ERR_MARSHALLING, "write interface token failed");
586     MessageParcel reply;
587     MessageOption option;
588     auto remote = Remote();
589     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
590     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_START_CAST_DISPLAY_LISTENER, data, reply, option) == 0,
591         ERR_IPC_SEND_REQUEST, "send request failed");
592 
593     int32_t ret = AVSESSION_ERROR;
594     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
595 }
596 
StopCastDisplayListener()597 int32_t AVSessionProxy::StopCastDisplayListener()
598 {
599     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
600     MessageParcel data;
601     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
602                              ERR_MARSHALLING, "write interface token failed");
603     MessageParcel reply;
604     MessageOption option;
605     auto remote = Remote();
606     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
607     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_STOP_CAST_DISPLAY_LISTENER, data, reply, option) == 0,
608         ERR_IPC_SEND_REQUEST, "send request failed");
609 
610     int32_t ret = AVSESSION_ERROR;
611     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
612 }
613 
GetAllCastDisplays(std::vector<CastDisplayInfo> & castDisplays)614 int32_t AVSessionProxy::GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays)
615 {
616     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
617     MessageParcel data;
618     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
619                              ERR_MARSHALLING, "write interface token failed");
620     MessageParcel reply;
621     MessageOption option;
622     auto remote = Remote();
623     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
624     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_ALL_CAST_DISPLAYS, data, reply, option) == 0,
625         ERR_IPC_SEND_REQUEST, "send request failed");
626 
627     int32_t ret = AVSESSION_ERROR;
628     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_MARSHALLING, "read int32 failed");
629     if (ret == AVSESSION_SUCCESS) {
630         int32_t castDisplayNum = 0;
631         CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(castDisplayNum), ERR_MARSHALLING, "read castDisplayNum failed");
632         CHECK_AND_RETURN_RET_LOG(castDisplayNum > 0, ERR_MARSHALLING, "castDisplayNum is illegal");
633         std::vector<CastDisplayInfo> displays;
634         for (int32_t i = 0; i < castDisplayNum; i++) {
635             CastDisplayInfo castDisplayInfo;
636             int32_t displayState = -1;
637             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(displayState), ERR_MARSHALLING, "read displayState failed");
638             castDisplayInfo.displayState = static_cast<CastDisplayState>(displayState);
639             uint64_t displayId = 0;
640             CHECK_AND_RETURN_RET_LOG(reply.ReadUint64(displayId), ERR_MARSHALLING, "read displayId failed");
641             castDisplayInfo.displayId = displayId;
642             std::string name = "";
643             CHECK_AND_RETURN_RET_LOG(reply.ReadString(name), ERR_MARSHALLING, "read name failed");
644             castDisplayInfo.name = name;
645             int32_t width = -1;
646             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(width), ERR_MARSHALLING, "read width failed");
647             castDisplayInfo.width = width;
648             int32_t height = -1;
649             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(height), ERR_MARSHALLING, "read height failed");
650             castDisplayInfo.height = height;
651             displays.push_back(castDisplayInfo);
652         }
653         castDisplays = displays;
654     }
655     return ret;
656 }
657 #endif
658 
Activate()659 int32_t AVSessionProxy::Activate()
660 {
661     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
662     MessageParcel data;
663     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
664         ERR_MARSHALLING, "write interface token failed");
665     MessageParcel reply;
666     MessageOption option;
667     auto remote = Remote();
668     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
669     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ACTIVATE, data, reply, option) == 0,
670         ERR_IPC_SEND_REQUEST, "send request failed");
671 
672     int32_t ret = AVSESSION_ERROR;
673     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
674 }
675 
Deactivate()676 int32_t AVSessionProxy::Deactivate()
677 {
678     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
679     MessageParcel data;
680     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
681         ERR_MARSHALLING, "write interface token failed");
682     MessageParcel reply;
683     MessageOption option;
684     auto remote = Remote();
685     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
686     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DEACTIVATE, data, reply, option) == 0,
687         ERR_IPC_SEND_REQUEST, "send request failed");
688 
689     int32_t ret = AVSESSION_ERROR;
690     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
691 }
692 
IsActive()693 bool AVSessionProxy::IsActive()
694 {
695     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
696     MessageParcel data;
697     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
698         ERR_MARSHALLING, "write interface token failed");
699     MessageParcel reply;
700     MessageOption option;
701     auto remote = Remote();
702     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
703     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ISACTIVE, data, reply, option) == 0,
704         ERR_IPC_SEND_REQUEST, "send request failed");
705 
706     bool ret = false;
707     return reply.ReadBool(ret) ? ret : false;
708 }
709 
AddSupportCommand(const int32_t cmd)710 int32_t AVSessionProxy::AddSupportCommand(const int32_t cmd)
711 {
712     std::lock_guard lockGuard(setCommandLock_);
713     SLOGI("add support command for %{public}d", cmd);
714 
715     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
716     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
717     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
718     MessageParcel data;
719     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
720         ERR_MARSHALLING, "write interface token failed");
721     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
722         ERR_MARSHALLING, "Write cmd failed");
723     MessageParcel reply;
724     MessageOption option;
725     auto remote = Remote();
726     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
727     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ADD_SUPPORT_COMMAND, data, reply, option) == 0,
728         ERR_IPC_SEND_REQUEST, "send request failed");
729 
730     int32_t ret = AVSESSION_ERROR;
731     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
732 }
733 
DeleteSupportCommand(const int32_t cmd)734 int32_t AVSessionProxy::DeleteSupportCommand(const int32_t cmd)
735 {
736     std::lock_guard lockGuard(setCommandLock_);
737     SLOGI("del support command for %{public}d", cmd);
738 
739     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
740     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
741     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
742     MessageParcel data;
743     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
744                              ERR_MARSHALLING, "write interface token failed");
745     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
746                              ERR_MARSHALLING, "Write cmd failed");
747     MessageParcel reply;
748     MessageOption option;
749     auto remote = Remote();
750     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
751     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DELETE_SUPPORT_COMMAND, data, reply, option) == 0,\
752                              ERR_IPC_SEND_REQUEST, "send request failed");
753 
754     int32_t ret = AVSESSION_ERROR;
755     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
756 }
757 
SetSessionEvent(const std::string & event,const AAFwk::WantParams & args)758 int32_t AVSessionProxy::SetSessionEvent(const std::string& event, const AAFwk::WantParams& args)
759 {
760     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
761     MessageParcel data;
762     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
763         ERR_MARSHALLING, "write interface token failed");
764     CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
765     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args),
766         ERR_MARSHALLING, "Write Want failed");
767     MessageParcel reply;
768     MessageOption option;
769     auto remote = Remote();
770     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
771     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_SESSION_EVENT, data, reply, option) == 0,
772         ERR_IPC_SEND_REQUEST, "send request failed");
773 
774     int32_t ret = AVSESSION_ERROR;
775     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
776 }
777 
778 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
ReleaseCast(bool continuePlay)779 int32_t AVSessionProxy::ReleaseCast(bool continuePlay)
780 {
781     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
782     MessageParcel data;
783     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
784         ERR_MARSHALLING, "write interface token failed");
785     MessageParcel reply;
786     MessageOption option;
787     auto remote = Remote();
788     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
789     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_RELEASE_CAST, data, reply, option) == 0,
790         ERR_IPC_SEND_REQUEST, "send request failed");
791 
792     int32_t ret = AVSESSION_ERROR;
793     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
794 }
795 #endif
796 } // namespace OHOS::AVSession
797