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 itemNum = reply.ReadInt32();
336         CHECK_AND_RETURN_RET_LOG(itemNum >= 0, ERR_UNMARSHALLING, "read int32 itemNum failed");
337         for (int32_t i = 0; i < itemNum; i++) {
338             AVQueueItem *item = reply.ReadParcelable<AVQueueItem>();
339             if (item == nullptr) {
340                 SLOGE("GetAVQueueItems: read parcelable AVQueueItem failed");
341                 delete item;
342                 return ERR_UNMARSHALLING;
343             }
344             items_.emplace_back(*item);
345             delete item;
346         }
347         items = items_;
348     }
349     return ret;
350 }
351 
GetAVQueueTitle(std::string & title)352 int32_t AVSessionProxy::GetAVQueueTitle(std::string& title)
353 {
354     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
355     MessageParcel data;
356     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
357         ERR_MARSHALLING, "write interface token failed");
358     MessageParcel reply;
359     MessageOption option;
360     auto remote = Remote();
361     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
362     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_QUEUE_TITLE, data, reply, option) == 0,
363         ERR_IPC_SEND_REQUEST, "send request failed");
364 
365     int32_t ret = AVSESSION_ERROR;
366     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
367     if (ret == AVSESSION_SUCCESS) {
368         std::string title_;
369         CHECK_AND_RETURN_RET_LOG(reply.ReadString(title), ERR_UNMARSHALLING, "read title string failed");
370         title = title_;
371     }
372     return ret;
373 }
374 
SetAVQueueTitle(const std::string & title)375 int32_t AVSessionProxy::SetAVQueueTitle(const std::string& title)
376 {
377     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVQueueTitle");
378     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
379     MessageParcel data;
380     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
381         ERR_MARSHALLING, "write interface token failed");
382     CHECK_AND_RETURN_RET_LOG(data.WriteString(title),
383         ERR_MARSHALLING, "Write state failed");
384     MessageParcel reply;
385     MessageOption option;
386     auto remote = Remote();
387     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
388     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_QUEUE_TITLE, data, reply, option) == 0,
389         ERR_IPC_SEND_REQUEST, "send request failed");
390 
391     int32_t ret = AVSESSION_ERROR;
392     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
393 }
394 
GetAVPlaybackState(AVPlaybackState & state)395 int32_t AVSessionProxy::GetAVPlaybackState(AVPlaybackState& state)
396 {
397     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
398     MessageParcel data;
399     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
400         ERR_MARSHALLING, "write interface token failed");
401     MessageParcel reply;
402     MessageOption option;
403     auto remote = Remote();
404     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
405     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_PLAYBACK_STATE, data, reply, option) == 0,
406         ERR_IPC_SEND_REQUEST, "send request failed");
407 
408     int32_t ret = AVSESSION_ERROR;
409     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
410     if (ret == AVSESSION_SUCCESS) {
411         std::shared_ptr<AVPlaybackState> playbackState(reply.ReadParcelable<AVPlaybackState>());
412         CHECK_AND_RETURN_RET_LOG(playbackState != nullptr, ERR_UNMARSHALLING, "read playbackState failed");
413         state = *playbackState;
414     }
415     return ret;
416 }
417 
SetAVPlaybackState(const AVPlaybackState & state)418 int32_t AVSessionProxy::SetAVPlaybackState(const AVPlaybackState& state)
419 {
420     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVPlaybackState");
421     CHECK_AND_RETURN_RET_LOG(state.IsValid(), ERR_INVALID_PARAM, "state not valid");
422     std::lock_guard lockGuard(setPlaybackLock_);
423     SLOGI("SetAVPlaybackState in proxy for state %{public}d", state.GetState());
424 
425     std::lock_guard isDestroyedLockGuard(isDestroyedLock_);
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(&state),
431         ERR_MARSHALLING, "Write state 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_PLAYBACK_STATE, 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 
SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent & ability)443 int32_t AVSessionProxy::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability)
444 {
445     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
446     MessageParcel data;
447     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
448         ERR_MARSHALLING, "write interface token failed");
449     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&ability),
450         ERR_MARSHALLING, "Write WantAgent failed");
451     MessageParcel reply;
452     MessageOption option;
453     auto remote = Remote();
454     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
455     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_LAUNCH_ABILITY, data, reply, option) == 0,
456         ERR_IPC_SEND_REQUEST, "send request failed");
457 
458     int32_t ret = AVSESSION_ERROR;
459     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
460 }
461 
GetExtras(AAFwk::WantParams & extras)462 int32_t AVSessionProxy::GetExtras(AAFwk::WantParams& extras)
463 {
464     AVSESSION_TRACE_SYNC_START("AVSessionProxy::GetExtras");
465     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
466     MessageParcel data;
467     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
468         ERR_MARSHALLING, "write interface token failed");
469     MessageParcel reply;
470     MessageOption option;
471     auto remote = Remote();
472     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
473     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_EXTRAS, data, reply, option) == 0,
474         ERR_IPC_SEND_REQUEST, "send request failed");
475 
476     int32_t ret = AVSESSION_ERROR;
477     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
478     if (ret == AVSESSION_SUCCESS) {
479         std::shared_ptr<AAFwk::WantParams> extrasData(reply.ReadParcelable<AAFwk::WantParams>());
480         CHECK_AND_RETURN_RET_LOG(extrasData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
481         extras = *extrasData;
482     }
483     return ret;
484 }
485 
SetExtras(const AAFwk::WantParams & extras)486 int32_t AVSessionProxy::SetExtras(const AAFwk::WantParams& extras)
487 {
488     AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetExtras");
489     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
490     MessageParcel data;
491     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
492         ERR_MARSHALLING, "write interface token failed");
493     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras),
494         ERR_MARSHALLING, "Write extras failed");
495     MessageParcel reply;
496     MessageOption option;
497     auto remote = Remote();
498     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
499     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_EXTRAS, data, reply, option) == 0,
500         ERR_IPC_SEND_REQUEST, "send request failed");
501 
502     int32_t ret = AVSESSION_ERROR;
503     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
504 }
505 
GetControllerInner()506 sptr<IRemoteObject> AVSessionProxy::GetControllerInner()
507 {
508     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
509     MessageParcel data;
510     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
511                              nullptr, "write interface token failed");
512     MessageParcel reply;
513     MessageOption option;
514     auto remote = Remote();
515     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
516     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_CONTROLLER, data, reply, option) == 0,
517                              nullptr, "send request failed");
518 
519     int32_t ret = AVSESSION_ERROR;
520     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
521     sptr <IRemoteObject> controller = nullptr;
522     if (ret == AVSESSION_SUCCESS) {
523         controller = reply.ReadRemoteObject();
524         CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "read IRemoteObject failed");
525     }
526     return controller;
527 }
528 
GetController()529 std::shared_ptr<AVSessionController> AVSessionProxy::GetController()
530 {
531     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
532     CHECK_AND_RETURN_RET_LOG(controller_ == nullptr || controller_->IsDestroy(), controller_,
533         "controller already exist");
534     sptr <IRemoteObject> object = GetControllerInner();
535     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
536     auto controller = iface_cast<AVSessionControllerProxy>(object);
537     controller_ = std::shared_ptr<AVSessionController>(controller.GetRefPtr(), [holder = controller](const auto*) {});
538     return controller_;
539 }
540 
541 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
GetAVCastControllerInner()542 sptr<IRemoteObject> AVSessionProxy::GetAVCastControllerInner()
543 {
544     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
545     MessageParcel data;
546     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
547                              nullptr, "write interface token failed");
548     MessageParcel reply;
549     MessageOption option;
550     auto remote = Remote();
551     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
552     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_AVCAST_CONTROLLER, data, reply, option) == 0,
553         nullptr, "send request failed");
554 
555     int32_t ret = AVSESSION_ERROR;
556     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
557     sptr <IRemoteObject> castController = nullptr;
558     if (ret == AVSESSION_SUCCESS) {
559         castController = reply.ReadRemoteObject();
560         CHECK_AND_RETURN_RET_LOG(castController != nullptr, nullptr, "read IRemoteObject failed");
561     }
562     return castController;
563 }
564 
GetAVCastController()565 std::shared_ptr<AVCastController> AVSessionProxy::GetAVCastController()
566 {
567     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, nullptr, "session is destroyed");
568     sptr <IRemoteObject> object = GetAVCastControllerInner();
569     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
570     auto castController = iface_cast<AVCastControllerProxy>(object);
571     castController_ = std::shared_ptr<AVCastController>(castController.GetRefPtr(),
572         [holder = castController](const auto*) {});
573     return castController_;
574 }
575 
StartCastDisplayListener()576 int32_t AVSessionProxy::StartCastDisplayListener()
577 {
578     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
579     MessageParcel data;
580     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
581                              ERR_MARSHALLING, "write interface token failed");
582     MessageParcel reply;
583     MessageOption option;
584     auto remote = Remote();
585     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
586     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_START_CAST_DISPLAY_LISTENER, data, reply, option) == 0,
587         ERR_IPC_SEND_REQUEST, "send request failed");
588 
589     int32_t ret = AVSESSION_ERROR;
590     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
591 }
592 
StopCastDisplayListener()593 int32_t AVSessionProxy::StopCastDisplayListener()
594 {
595     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
596     MessageParcel data;
597     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
598                              ERR_MARSHALLING, "write interface token failed");
599     MessageParcel reply;
600     MessageOption option;
601     auto remote = Remote();
602     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
603     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_STOP_CAST_DISPLAY_LISTENER, data, reply, option) == 0,
604         ERR_IPC_SEND_REQUEST, "send request failed");
605 
606     int32_t ret = AVSESSION_ERROR;
607     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
608 }
609 
GetAllCastDisplays(std::vector<CastDisplayInfo> & castDisplays)610 int32_t AVSessionProxy::GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays)
611 {
612     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
613     MessageParcel data;
614     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
615                              ERR_MARSHALLING, "write interface token failed");
616     MessageParcel reply;
617     MessageOption option;
618     auto remote = Remote();
619     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
620     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_ALL_CAST_DISPLAYS, data, reply, option) == 0,
621         ERR_IPC_SEND_REQUEST, "send request failed");
622 
623     int32_t ret = AVSESSION_ERROR;
624     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_MARSHALLING, "read int32 failed");
625     if (ret == AVSESSION_SUCCESS) {
626         int32_t castDisplayNum = 0;
627         CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(castDisplayNum), ERR_MARSHALLING, "read castDisplayNum failed");
628         CHECK_AND_RETURN_RET_LOG(castDisplayNum > 0, ERR_MARSHALLING, "castDisplayNum is illegal");
629         std::vector<CastDisplayInfo> displays;
630         for (int32_t i = 0; i < castDisplayNum; i++) {
631             CastDisplayInfo castDisplayInfo;
632             int32_t displayState = -1;
633             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(displayState), ERR_MARSHALLING, "read displayState failed");
634             castDisplayInfo.displayState = static_cast<CastDisplayState>(displayState);
635             uint64_t displayId = 0;
636             CHECK_AND_RETURN_RET_LOG(reply.ReadUint64(displayId), ERR_MARSHALLING, "read displayId failed");
637             castDisplayInfo.displayId = displayId;
638             std::string name = "";
639             CHECK_AND_RETURN_RET_LOG(reply.ReadString(name), ERR_MARSHALLING, "read name failed");
640             castDisplayInfo.name = name;
641             int32_t width = -1;
642             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(width), ERR_MARSHALLING, "read width failed");
643             castDisplayInfo.width = width;
644             int32_t height = -1;
645             CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(height), ERR_MARSHALLING, "read height failed");
646             castDisplayInfo.height = height;
647             displays.push_back(castDisplayInfo);
648         }
649         castDisplays = displays;
650     }
651     return ret;
652 }
653 #endif
654 
Activate()655 int32_t AVSessionProxy::Activate()
656 {
657     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
658     MessageParcel data;
659     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
660         ERR_MARSHALLING, "write interface token failed");
661     MessageParcel reply;
662     MessageOption option;
663     auto remote = Remote();
664     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
665     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ACTIVATE, data, reply, option) == 0,
666         ERR_IPC_SEND_REQUEST, "send request failed");
667 
668     int32_t ret = AVSESSION_ERROR;
669     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
670 }
671 
Deactivate()672 int32_t AVSessionProxy::Deactivate()
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_DEACTIVATE, 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 
IsActive()689 bool AVSessionProxy::IsActive()
690 {
691     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
692     MessageParcel data;
693     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
694         ERR_MARSHALLING, "write interface token failed");
695     MessageParcel reply;
696     MessageOption option;
697     auto remote = Remote();
698     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
699     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ISACTIVE, data, reply, option) == 0,
700         ERR_IPC_SEND_REQUEST, "send request failed");
701 
702     bool ret = false;
703     return reply.ReadBool(ret) ? ret : false;
704 }
705 
AddSupportCommand(const int32_t cmd)706 int32_t AVSessionProxy::AddSupportCommand(const int32_t cmd)
707 {
708     std::lock_guard lockGuard(setCommandLock_);
709     SLOGI("add support command for %{public}d", cmd);
710 
711     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
712     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
713     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
714     MessageParcel data;
715     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
716         ERR_MARSHALLING, "write interface token failed");
717     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
718         ERR_MARSHALLING, "Write cmd failed");
719     MessageParcel reply;
720     MessageOption option;
721     auto remote = Remote();
722     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
723     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ADD_SUPPORT_COMMAND, data, reply, option) == 0,
724         ERR_IPC_SEND_REQUEST, "send request failed");
725 
726     int32_t ret = AVSESSION_ERROR;
727     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
728 }
729 
DeleteSupportCommand(const int32_t cmd)730 int32_t AVSessionProxy::DeleteSupportCommand(const int32_t cmd)
731 {
732     std::lock_guard lockGuard(setCommandLock_);
733     SLOGI("del support command for %{public}d", cmd);
734 
735     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
736     CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
737     CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
738     MessageParcel data;
739     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
740                              ERR_MARSHALLING, "write interface token failed");
741     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
742                              ERR_MARSHALLING, "Write cmd failed");
743     MessageParcel reply;
744     MessageOption option;
745     auto remote = Remote();
746     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
747     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DELETE_SUPPORT_COMMAND, data, reply, option) == 0,\
748                              ERR_IPC_SEND_REQUEST, "send request failed");
749 
750     int32_t ret = AVSESSION_ERROR;
751     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
752 }
753 
SetSessionEvent(const std::string & event,const AAFwk::WantParams & args)754 int32_t AVSessionProxy::SetSessionEvent(const std::string& event, const AAFwk::WantParams& args)
755 {
756     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
757     MessageParcel data;
758     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
759         ERR_MARSHALLING, "write interface token failed");
760     CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
761     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args),
762         ERR_MARSHALLING, "Write Want failed");
763     MessageParcel reply;
764     MessageOption option;
765     auto remote = Remote();
766     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
767     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_SESSION_EVENT, data, reply, option) == 0,
768         ERR_IPC_SEND_REQUEST, "send request failed");
769 
770     int32_t ret = AVSESSION_ERROR;
771     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
772 }
773 
774 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
ReleaseCast()775 int32_t AVSessionProxy::ReleaseCast()
776 {
777     CHECK_AND_RETURN_RET_LOG(!isDestroyed_, ERR_SESSION_NOT_EXIST, "session is destroyed");
778     MessageParcel data;
779     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
780         ERR_MARSHALLING, "write interface token failed");
781     MessageParcel reply;
782     MessageOption option;
783     auto remote = Remote();
784     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
785     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_RELEASE_CAST, data, reply, option) == 0,
786         ERR_IPC_SEND_REQUEST, "send request failed");
787 
788     int32_t ret = AVSESSION_ERROR;
789     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
790 }
791 #endif
792 } // namespace OHOS::AVSession
793