1 /*
2 * Copyright (c) 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 "avcast_controller_proxy.h"
17 #include "avcast_controller_callback_client.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "avsession_trace.h"
21 #include "media_info_holder.h"
22 #include "surface_utils.h"
23
24 namespace OHOS::AVSession {
AVCastControllerProxy(const sptr<IRemoteObject> & impl)25 AVCastControllerProxy::AVCastControllerProxy(const sptr<IRemoteObject>& impl)
26 : IRemoteProxy<IAVCastController>(impl)
27 {
28 SLOGD("AVCastControllerProxy construct");
29 }
30
~AVCastControllerProxy()31 AVCastControllerProxy::~AVCastControllerProxy()
32 {
33 SLOGI("AVCastControllerProxy destroy");
34 Destroy();
35 }
36
SendControlCommand(const AVCastControlCommand & cmd)37 int32_t AVCastControllerProxy::SendControlCommand(const AVCastControlCommand& cmd)
38 {
39 AVSESSION_TRACE_SYNC_START("AVCastControllerProxy::SendControlCommand");
40 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
41 CHECK_AND_RETURN_RET_LOG(cmd.IsValid(), ERR_COMMAND_NOT_SUPPORT, "command not valid");
42 MessageParcel parcel;
43 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
44 "write interface token failed");
45 CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&cmd), ERR_MARSHALLING, "write cmd failed");
46
47 auto remote = Remote();
48 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
49 MessageParcel reply;
50 MessageOption option;
51 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_SEND_CONTROL_COMMAND, parcel, reply, option) == 0,
52 ERR_IPC_SEND_REQUEST, "send request failed");
53
54 int32_t ret = AVSESSION_ERROR;
55 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
56 }
57
Start(const AVQueueItem & avQueueItem)58 int32_t AVCastControllerProxy::Start(const AVQueueItem& avQueueItem)
59 {
60 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
61 MessageParcel parcel;
62 parcel.SetMaxCapacity(defaultIpcCapacity);
63 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
64 "write interface token failed");
65 CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&avQueueItem), ERR_UNMARSHALLING, "Write avQueueItem failed");
66 CHECK_AND_RETURN_RET_LOG(parcel.WriteFileDescriptor(avQueueItem.GetDescription()->GetFdSrc().fd_),
67 ERR_UNMARSHALLING, "Write avQueueItem failed");
68 SLOGI("Start received fd %{public}d", avQueueItem.GetDescription()->GetFdSrc().fd_);
69
70 auto remote = Remote();
71 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
72 MessageParcel reply;
73 MessageOption option;
74 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_START, parcel, reply, option) == 0,
75 ERR_IPC_SEND_REQUEST, "send request failed");
76 int32_t ret = AVSESSION_ERROR;
77 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
78 }
79
Prepare(const AVQueueItem & avQueueItem)80 int32_t AVCastControllerProxy::Prepare(const AVQueueItem& avQueueItem)
81 {
82 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
83 MessageParcel parcel;
84 parcel.SetMaxCapacity(defaultIpcCapacity);
85 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
86 "write interface token failed");
87 CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&avQueueItem), ERR_UNMARSHALLING, "Write avQueueItem failed");
88 if (avQueueItem.GetDescription()->GetFdSrc().fd_ == 0) {
89 parcel.WriteBool(false);
90 } else {
91 parcel.WriteBool(true);
92 CHECK_AND_RETURN_RET_LOG(parcel.WriteFileDescriptor(avQueueItem.GetDescription()->GetFdSrc().fd_),
93 ERR_UNMARSHALLING, "Write avQueueItem failed");
94 }
95 SLOGI("Prepare received fd %{public}d", avQueueItem.GetDescription()->GetFdSrc().fd_);
96
97 auto remote = Remote();
98 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
99 MessageParcel reply;
100 MessageOption option;
101 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_PREPARE, parcel, reply, option) == 0,
102 ERR_IPC_SEND_REQUEST, "send request failed");
103 int32_t ret = AVSESSION_ERROR;
104 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
105 }
106
GetDuration(int32_t & duration)107 int32_t AVCastControllerProxy::GetDuration(int32_t& duration)
108 {
109 MessageParcel parcel;
110 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
111 "write interface token failed");
112
113 auto remote = Remote();
114 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
115 MessageParcel reply;
116 MessageOption option;
117 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_DURATION, parcel, reply, option) == 0,
118 ERR_IPC_SEND_REQUEST, "send request failed");
119
120 int32_t ret = AVSESSION_ERROR;
121 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
122 if (ret == AVSESSION_SUCCESS) {
123 int32_t tempDuration;
124 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(tempDuration), ERR_UNMARSHALLING, "read string failed");
125 duration = tempDuration;
126 }
127 return ret;
128 }
129
GetCastAVPlaybackState(AVPlaybackState & state)130 int32_t AVCastControllerProxy::GetCastAVPlaybackState(AVPlaybackState& state)
131 {
132 MessageParcel parcel;
133 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()),
134 AVSESSION_ERROR, "write interface token failed");
135
136 auto remote = Remote();
137 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
138 MessageParcel reply;
139 MessageOption option;
140 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_CAST_AV_PLAYBACK_STATE, parcel,
141 reply, option) == 0, AVSESSION_ERROR, "send request failed");
142
143 int32_t ret = AVSESSION_ERROR;
144 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
145 if (ret == AVSESSION_SUCCESS) {
146 sptr<AVPlaybackState> state_ = reply.ReadParcelable<AVPlaybackState>();
147 CHECK_AND_RETURN_RET_LOG(state_ != nullptr, ERR_UNMARSHALLING, "read AVPlaybackState failed");
148 state = *state_;
149 }
150 return ret;
151 }
152
GetSupportedDecoders(std::vector<std::string> & decoderTypes)153 int32_t AVCastControllerProxy::GetSupportedDecoders(std::vector<std::string>& decoderTypes)
154 {
155 MessageParcel parcel;
156 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()),
157 AVSESSION_ERROR, "write interface token failed");
158
159 auto remote = Remote();
160 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
161 MessageParcel reply;
162 MessageOption option;
163 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_SUPPORT_DECODER, parcel,
164 reply, option) == 0, AVSESSION_ERROR, "send request failed");
165
166 int32_t ret = AVSESSION_ERROR;
167 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
168 if (ret == AVSESSION_SUCCESS) {
169 CHECK_AND_RETURN_RET_LOG(reply.ReadStringVector(&decoderTypes), ERR_UNMARSHALLING, "read int32 failed");
170 }
171 return ret;
172 }
173
GetRecommendedResolutionLevel(std::string & decoderType,ResolutionLevel & resolutionLevel)174 int32_t AVCastControllerProxy::GetRecommendedResolutionLevel(std::string& decoderType, ResolutionLevel& resolutionLevel)
175 {
176 MessageParcel parcel;
177 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
178 "write interface token failed");
179 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(decoderType), ERR_MARSHALLING, "write filter failed");
180
181 auto remote = Remote();
182 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
183 MessageParcel reply;
184 MessageOption option;
185 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_RECOMMEND_RESOLUTION_LEVEL, parcel, reply,
186 option) == 0, ERR_IPC_SEND_REQUEST, "send request failed");
187
188 int32_t ret = AVSESSION_ERROR;
189 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
190 if (ret == AVSESSION_SUCCESS) {
191 int32_t resolutionLevelInt = -1;
192 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(resolutionLevelInt), ERR_UNMARSHALLING, "read int32 failed");
193 resolutionLevel = static_cast<ResolutionLevel>(resolutionLevelInt);
194 }
195 return ret;
196 }
197
GetSupportedHdrCapabilities(std::vector<HDRFormat> & hdrFormats)198 int32_t AVCastControllerProxy::GetSupportedHdrCapabilities(std::vector<HDRFormat>& hdrFormats)
199 {
200 MessageParcel parcel;
201 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()),
202 AVSESSION_ERROR, "write interface token failed");
203
204 auto remote = Remote();
205 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
206 MessageParcel reply;
207 MessageOption option;
208 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_SUPPORT_HDR_CAPABILITIES, parcel,
209 reply, option) == 0, AVSESSION_ERROR, "send request failed");
210
211 int32_t ret = AVSESSION_ERROR;
212 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
213 if (ret == AVSESSION_SUCCESS) {
214 std::vector<int32_t> hdrFormatsInt;
215 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32Vector(&hdrFormatsInt), ERR_UNMARSHALLING, "read int32 failed");
216 for (auto it = hdrFormatsInt.begin(); it != hdrFormatsInt.end(); it++) {
217 hdrFormats.emplace_back(static_cast<HDRFormat>(*it));
218 }
219 }
220 return ret;
221 }
222
GetSupportedPlaySpeeds(std::vector<float> & playSpeeds)223 int32_t AVCastControllerProxy::GetSupportedPlaySpeeds(std::vector<float>& playSpeeds)
224 {
225 MessageParcel parcel;
226 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()),
227 AVSESSION_ERROR, "write interface token failed");
228
229 auto remote = Remote();
230 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
231 MessageParcel reply;
232 MessageOption option;
233 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_SUPPORT_PLAY_SPEED, parcel,
234 reply, option) == 0, AVSESSION_ERROR, "send request failed");
235
236 int32_t ret = AVSESSION_ERROR;
237 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
238 if (ret == AVSESSION_SUCCESS) {
239 CHECK_AND_RETURN_RET_LOG(reply.ReadFloatVector(&playSpeeds), ERR_UNMARSHALLING, "read int32 failed");
240 }
241 return ret;
242 }
243
GetCurrentItem(AVQueueItem & currentItem)244 int32_t AVCastControllerProxy::GetCurrentItem(AVQueueItem& currentItem)
245 {
246 MessageParcel parcel;
247 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()),
248 AVSESSION_ERROR, "write interface token failed");
249
250 auto remote = Remote();
251 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
252 MessageParcel reply;
253 reply.SetMaxCapacity(defaultIpcCapacity);
254 MessageOption option;
255 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_CURRENT_ITEM, parcel,
256 reply, option) == 0, AVSESSION_ERROR, "send request failed");
257
258 int32_t ret = AVSESSION_ERROR;
259 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
260 if (ret == AVSESSION_SUCCESS) {
261 sptr<AVQueueItem> currentItem_ = reply.ReadParcelable<AVQueueItem>();
262 CHECK_AND_RETURN_RET_LOG(currentItem_ != nullptr, ERR_UNMARSHALLING, "read AVQueueItem failed");
263 currentItem = *currentItem_;
264 }
265 return ret;
266 }
267
GetValidCommands(std::vector<int32_t> & cmds)268 int32_t AVCastControllerProxy::GetValidCommands(std::vector<int32_t>& cmds)
269 {
270 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
271 MessageParcel parcel;
272 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()),
273 ERR_MARSHALLING, "write interface token failed");
274
275 auto remote = Remote();
276 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
277 MessageParcel reply;
278 MessageOption option;
279 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_GET_VALID_COMMANDS, parcel,
280 reply, option) == 0, AVSESSION_ERROR, "send request failed");
281
282 int32_t ret = AVSESSION_ERROR;
283 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
284 if (ret == AVSESSION_SUCCESS) {
285 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32Vector(&cmds), ERR_UNMARSHALLING, "read int32 vector failed");
286 }
287 return ret;
288 }
289
SetDisplaySurface(std::string & surfaceId)290 int32_t AVCastControllerProxy::SetDisplaySurface(std::string& surfaceId)
291 {
292 AVSESSION_TRACE_SYNC_START("AVCastControllerProxy::SetDisplaySurface");
293 errno = 0;
294 uint64_t surfaceUniqueId = static_cast<uint64_t>(std::strtoll(surfaceId.c_str(), nullptr, 10));
295 if (errno == ERANGE) {
296 return ERR_INVALID_PARAM;
297 }
298
299 sptr<Surface> surface = SurfaceUtils::GetInstance()->GetSurface(surfaceUniqueId);
300 if (!surface) {
301 SLOGE("surface is null, surface uniqueId %{public}llu", (unsigned long long)surfaceUniqueId);
302 return AVSESSION_ERROR;
303 }
304 sptr<IBufferProducer> producer = surface->GetProducer();
305 if (!producer) {
306 SLOGE("producer is null");
307 return AVSESSION_ERROR;
308 }
309
310 MessageParcel parcel;
311 MessageParcel reply;
312 MessageOption option;
313
314 if (!parcel.WriteInterfaceToken(GetDescriptor())) {
315 SLOGE("Failed to write the interface token");
316 return AVSESSION_ERROR;
317 }
318 if (!parcel.WriteRemoteObject(producer->AsObject())) {
319 SLOGE("Failed to write surface producer");
320 return AVSESSION_ERROR;
321 }
322 auto remote = Remote();
323 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_SET_DISPLAY_SURFACE, parcel, reply, option) == 0,
324 ERR_IPC_SEND_REQUEST, "send request failed");
325
326 int32_t ret = AVSESSION_ERROR;
327 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
328 }
329
ProcessMediaKeyResponse(const std::string & assetId,const std::vector<uint8_t> & response)330 int32_t AVCastControllerProxy::ProcessMediaKeyResponse(const std::string& assetId, const std::vector<uint8_t>& response)
331 {
332 AVSESSION_TRACE_SYNC_START("AVCastControllerProxy::ProcessMediaKeyResponse");
333 MessageParcel parcel;
334 MessageParcel reply;
335 MessageOption option;
336 auto len = response.size();
337 if (len > parcel.GetDataCapacity()) {
338 SLOGD("ProcessMediaKeyResponse SetDataCapacity totalSize: %{public}d", static_cast<int>(len));
339 parcel.SetMaxCapacity(len + len);
340 parcel.SetDataCapacity(len);
341 }
342
343 if (!parcel.WriteInterfaceToken(GetDescriptor())) {
344 SLOGE("Failed to write the interface token");
345 return AVSESSION_ERROR;
346 }
347 if (!parcel.WriteString(assetId)) {
348 SLOGE("Failed to write assetId");
349 return AVSESSION_ERROR;
350 }
351
352 if (!parcel.WriteInt32(response.size())) {
353 SLOGE("Failed to write response size");
354 return AVSESSION_ERROR;
355 }
356 if (len != 0) {
357 if (!parcel.WriteBuffer(response.data(), len)) {
358 SLOGE("AVCastControllerProxy ProcessMediaKeyResponse write response failed");
359 return IPC_PROXY_ERR;
360 }
361 }
362
363 auto remote = Remote();
364 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_PROVIDE_KEY_RESPONSE, parcel, reply, option) == 0,
365 ERR_IPC_SEND_REQUEST, "send request failed");
366
367 int32_t ret = AVSESSION_ERROR;
368 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
369 }
370
SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType & filter)371 int32_t AVCastControllerProxy::SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
372 {
373 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
374 MessageParcel parcel;
375 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
376 "write interface token failed");
377 CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
378
379 auto remote = Remote();
380 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
381 MessageParcel reply;
382 MessageOption option;
383 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_SET_CAST_PLAYBACK_FILTER, parcel, reply,
384 option) == 0, ERR_IPC_SEND_REQUEST, "send request failed");
385
386 int32_t ret = AVSESSION_ERROR;
387 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
388 }
389
AddAvailableCommand(const int32_t cmd)390 int32_t AVCastControllerProxy::AddAvailableCommand(const int32_t cmd)
391 {
392 SLOGI("add available command in");
393 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
394 CHECK_AND_RETURN_RET_LOG(cmd > AVCastControlCommand::CAST_CONTROL_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
395 CHECK_AND_RETURN_RET_LOG(cmd < AVCastControlCommand::CAST_CONTROL_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
396 MessageParcel parcel;
397 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
398 "write interface token failed");
399 CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(cmd),
400 ERR_MARSHALLING, "write valid command failed");
401
402 auto remote = Remote();
403 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
404 MessageParcel reply;
405 MessageOption option;
406 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ADD_AVAILABLE_COMMAND, parcel,
407 reply, option) == 0, AVSESSION_ERROR, "send request failed");
408
409 int32_t ret = AVSESSION_ERROR;
410 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
411 }
412
RemoveAvailableCommand(const int32_t cmd)413 int32_t AVCastControllerProxy::RemoveAvailableCommand(const int32_t cmd)
414 {
415 SLOGI("remove available command in");
416 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
417 CHECK_AND_RETURN_RET_LOG(cmd > AVCastControlCommand::CAST_CONTROL_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
418 CHECK_AND_RETURN_RET_LOG(cmd < AVCastControlCommand::CAST_CONTROL_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
419 MessageParcel parcel;
420 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
421 "write interface token failed");
422 CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(cmd),
423 ERR_MARSHALLING, "write valid command failed");
424
425 auto remote = Remote();
426 CHECK_AND_RETURN_RET_LOG(remote != nullptr, AVSESSION_ERROR, "get remote service failed");
427 MessageParcel reply;
428 MessageOption option;
429 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_REMOVE_AVAILABLE_COMMAND, parcel,
430 reply, option) == 0, AVSESSION_ERROR, "send request failed");
431
432 int32_t ret = AVSESSION_ERROR;
433 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
434 }
435
RegisterCallback(const std::shared_ptr<AVCastControllerCallback> & callback)436 int32_t AVCastControllerProxy::RegisterCallback(const std::shared_ptr<AVCastControllerCallback>& callback)
437 {
438 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
439
440 sptr<AVCastControllerCallbackClient> callback_;
441 callback_ = new(std::nothrow) AVCastControllerCallbackClient(callback);
442 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_NO_MEMORY, "new AVCastControllerCallbackClient failed");
443
444 return RegisterCallbackInner(callback_);
445 }
446
RegisterCallbackInner(const sptr<IRemoteObject> & callback)447 int32_t AVCastControllerProxy::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
448 {
449 MessageParcel parcel;
450 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
451 "write interface token failed");
452 CHECK_AND_RETURN_RET_LOG(parcel.WriteRemoteObject(callback), ERR_MARSHALLING,
453 "write remote object failed");
454
455 auto remote = Remote();
456 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
457 MessageParcel reply;
458 MessageOption option;
459 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_REGISTER_CALLBACK, parcel, 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
Destroy()466 int32_t AVCastControllerProxy::Destroy()
467 {
468 CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
469 MessageParcel parcel;
470 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
471 "write interface token failed");
472
473 auto remote = Remote();
474 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
475 MessageParcel reply;
476 MessageOption option;
477 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_DESTROY, parcel, reply, option) == 0,
478 ERR_IPC_SEND_REQUEST, "send request failed");
479 isDestroy_ = true;
480
481 int32_t ret = AVSESSION_ERROR;
482 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
483 }
484 }
485