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