1 /*
2  * Copyright (C) 2023-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  * Description: supply stream player implement proxy realization.
15  * Author: huangchanggui
16  * Create: 2023-01-12
17  */
18 
19 #include "stream_player_impl_proxy.h"
20 #include "cast_engine_common_helper.h"
21 #include "cast_engine_errors.h"
22 #include "cast_engine_log.h"
23 
24 namespace OHOS {
25 namespace CastEngine {
26 namespace CastEngineClient {
27 DEFINE_CAST_ENGINE_LABEL("Cast-Client-StreamPlayer");
28 
~StreamPlayerImplProxy()29 StreamPlayerImplProxy::~StreamPlayerImplProxy()
30 {
31     CLOGD("destructor in");
32 }
33 
RegisterListener(sptr<IStreamPlayerListenerImpl> listener)34 int32_t StreamPlayerImplProxy::RegisterListener(sptr<IStreamPlayerListenerImpl> listener)
35 {
36     MessageParcel data;
37     MessageParcel reply;
38     MessageOption option;
39 
40     if (!data.WriteInterfaceToken(GetDescriptor())) {
41         CLOGE("Failed to write the interface token");
42         return CAST_ENGINE_ERROR;
43     }
44     if (!data.WriteRemoteObject(listener->AsObject())) {
45         CLOGE("Failed to write stream player listener");
46         return CAST_ENGINE_ERROR;
47     }
48     if (Remote()->SendRequest(REGISTER_LISTENER, data, reply, option) != ERR_NONE) {
49         CLOGE("Failed to send ipc request when registering listener");
50         return CAST_ENGINE_ERROR;
51     }
52 
53     return reply.ReadInt32();
54 }
55 
UnregisterListener()56 int32_t StreamPlayerImplProxy::UnregisterListener()
57 {
58     MessageParcel data;
59     MessageParcel reply;
60     MessageOption option;
61 
62     if (!data.WriteInterfaceToken(GetDescriptor())) {
63         CLOGE("Failed to write the interface token");
64         return CAST_ENGINE_ERROR;
65     }
66     if (Remote()->SendRequest(UNREGISTER_LISTENER, data, reply, option) != ERR_NONE) {
67         CLOGE("Failed to send ipc request when unregistering listener");
68         return CAST_ENGINE_ERROR;
69     }
70 
71     return reply.ReadInt32();
72 }
73 
SetSurface(sptr<IBufferProducer> producer)74 int32_t StreamPlayerImplProxy::SetSurface(sptr<IBufferProducer> producer)
75 {
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option;
79 
80     if (!data.WriteInterfaceToken(GetDescriptor())) {
81         CLOGE("Failed to write the interface token");
82         return CAST_ENGINE_ERROR;
83     }
84     if (!data.WriteRemoteObject(producer->AsObject())) {
85         CLOGE("Failed to write surface producer");
86         return CAST_ENGINE_ERROR;
87     }
88 
89     int32_t ret = Remote()->SendRequest(SET_SURFACE, data, reply, option);
90     if (ret == ERR_INVALID_DATA) {
91         CLOGE("Invalid parameter when setting surface");
92         return ERR_INVALID_PARAM;
93     } else if (ret != ERR_NONE) {
94         CLOGE("Failed to send ipc request when setting surface");
95         return CAST_ENGINE_ERROR;
96     }
97 
98     return reply.ReadInt32();
99 }
100 
Load(const MediaInfo &mediaInfo)101 int32_t StreamPlayerImplProxy::Load(const MediaInfo &mediaInfo)
102 {
103     MessageParcel data;
104     MessageParcel reply;
105     MessageOption option;
106 
107     if (!data.WriteInterfaceToken(GetDescriptor())) {
108         CLOGE("Failed to write the interface token");
109         return CAST_ENGINE_ERROR;
110     }
111     if (!WriteMediaInfo(data, mediaInfo)) {
112         CLOGE("Failed to write the mediaInfo");
113         return CAST_ENGINE_ERROR;
114     }
115 
116     int32_t ret = Remote()->SendRequest(LOAD, data, reply, option);
117     if (ret == ERR_INVALID_DATA) {
118         CLOGE("Invalid parameter when load");
119         return ERR_INVALID_PARAM;
120     } else if (ret != ERR_NONE) {
121         CLOGE("Failed to send ipc request when load");
122         return CAST_ENGINE_ERROR;
123     }
124 
125     return reply.ReadInt32();
126 }
127 
Play(const MediaInfo &mediaInfo)128 int32_t StreamPlayerImplProxy::Play(const MediaInfo &mediaInfo)
129 {
130     MessageParcel data;
131     MessageParcel reply;
132     MessageOption option;
133 
134     if (!data.WriteInterfaceToken(GetDescriptor())) {
135         CLOGE("Failed to write the interface token");
136         return CAST_ENGINE_ERROR;
137     }
138     if (!WriteMediaInfo(data, mediaInfo)) {
139         CLOGE("Failed to write the mediaInfo");
140         return CAST_ENGINE_ERROR;
141     }
142 
143     int32_t ret = Remote()->SendRequest(START, data, reply, option);
144     if (ret == ERR_INVALID_DATA) {
145         CLOGE("Invalid parameter when play");
146         return ERR_INVALID_PARAM;
147     } else if (ret != ERR_NONE) {
148         CLOGE("Failed to send ipc request when play");
149         return CAST_ENGINE_ERROR;
150     }
151 
152     return reply.ReadInt32();
153 }
154 
Play(int index)155 int32_t StreamPlayerImplProxy::Play(int index)
156 {
157     MessageParcel data;
158     MessageParcel reply;
159     MessageOption option;
160 
161     if (!data.WriteInterfaceToken(GetDescriptor())) {
162         CLOGE("Failed to write the interface token");
163         return CAST_ENGINE_ERROR;
164     }
165     if (!data.WriteInt32(index)) {
166         CLOGE("Failed to write the index");
167         return CAST_ENGINE_ERROR;
168     }
169     if (Remote()->SendRequest(PLAY_INDEX, data, reply, option) != ERR_NONE) {
170         CLOGE("Failed to send ipc request when play");
171         return CAST_ENGINE_ERROR;
172     }
173 
174     return reply.ReadInt32();
175 }
176 
Pause()177 int32_t StreamPlayerImplProxy::Pause()
178 {
179     MessageParcel data;
180     MessageParcel reply;
181     MessageOption option;
182 
183     if (!data.WriteInterfaceToken(GetDescriptor())) {
184         CLOGE("Failed to write the interface token");
185         return CAST_ENGINE_ERROR;
186     }
187     if (Remote()->SendRequest(PAUSE, data, reply, option) != ERR_NONE) {
188         CLOGE("Failed to send ipc request when pause");
189         return CAST_ENGINE_ERROR;
190     }
191 
192     return reply.ReadInt32();
193 }
194 
Play()195 int32_t StreamPlayerImplProxy::Play()
196 {
197     MessageParcel data;
198     MessageParcel reply;
199     MessageOption option;
200 
201     if (!data.WriteInterfaceToken(GetDescriptor())) {
202         CLOGE("Failed to write the interface token");
203         return CAST_ENGINE_ERROR;
204     }
205     if (Remote()->SendRequest(PLAY, data, reply, option) != ERR_NONE) {
206         CLOGE("Failed to send ipc request when resume");
207         return CAST_ENGINE_ERROR;
208     }
209 
210     return reply.ReadInt32();
211 }
212 
Stop()213 int32_t StreamPlayerImplProxy::Stop()
214 {
215     MessageParcel data;
216     MessageParcel reply;
217     MessageOption option;
218 
219     if (!data.WriteInterfaceToken(GetDescriptor())) {
220         CLOGE("Failed to write the interface token");
221         return CAST_ENGINE_ERROR;
222     }
223     if (Remote()->SendRequest(STOP, data, reply, option) != ERR_NONE) {
224         CLOGE("Failed to send ipc request when stop");
225         return CAST_ENGINE_ERROR;
226     }
227 
228     return reply.ReadInt32();
229 }
230 
Next()231 int32_t StreamPlayerImplProxy::Next()
232 {
233     MessageParcel data;
234     MessageParcel reply;
235     MessageOption option;
236 
237     if (!data.WriteInterfaceToken(GetDescriptor())) {
238         CLOGE("Failed to write the interface token");
239         return CAST_ENGINE_ERROR;
240     }
241     if (Remote()->SendRequest(NEXT, data, reply, option) != ERR_NONE) {
242         CLOGE("Failed to send ipc request when stop");
243         return CAST_ENGINE_ERROR;
244     }
245 
246     return reply.ReadInt32();
247 }
248 
Previous()249 int32_t StreamPlayerImplProxy::Previous()
250 {
251     MessageParcel data;
252     MessageParcel reply;
253     MessageOption option;
254 
255     if (!data.WriteInterfaceToken(GetDescriptor())) {
256         CLOGE("Failed to write the interface token");
257         return CAST_ENGINE_ERROR;
258     }
259     if (Remote()->SendRequest(PREVIOUS, data, reply, option) != ERR_NONE) {
260         CLOGE("Failed to send ipc request when stop");
261         return CAST_ENGINE_ERROR;
262     }
263 
264     return reply.ReadInt32();
265 }
266 
Seek(int position)267 int32_t StreamPlayerImplProxy::Seek(int position)
268 {
269     MessageParcel data;
270     MessageParcel reply;
271     MessageOption option;
272 
273     if (!data.WriteInterfaceToken(GetDescriptor())) {
274         CLOGE("Failed to write the interface token");
275         return CAST_ENGINE_ERROR;
276     }
277     if (!data.WriteInt32(position)) {
278         CLOGE("Failed to write the position");
279         return CAST_ENGINE_ERROR;
280     }
281     if (Remote()->SendRequest(SEEK, data, reply, option) != ERR_NONE) {
282         CLOGE("Failed to send ipc request when seek");
283         return CAST_ENGINE_ERROR;
284     }
285 
286     return reply.ReadInt32();
287 }
288 
FastForward(int delta)289 int32_t StreamPlayerImplProxy::FastForward(int delta)
290 {
291     MessageParcel data;
292     MessageParcel reply;
293     MessageOption option;
294 
295     if (!data.WriteInterfaceToken(GetDescriptor())) {
296         CLOGE("Failed to write the interface token");
297         return CAST_ENGINE_ERROR;
298     }
299     if (!data.WriteInt32(delta)) {
300         CLOGE("Failed to write the delta");
301         return CAST_ENGINE_ERROR;
302     }
303     if (Remote()->SendRequest(FAST_FORWARD, data, reply, option) != ERR_NONE) {
304         CLOGE("Failed to send ipc request when fastForward");
305         return CAST_ENGINE_ERROR;
306     }
307 
308     return reply.ReadInt32();
309 }
310 
FastRewind(int delta)311 int32_t StreamPlayerImplProxy::FastRewind(int delta)
312 {
313     MessageParcel data;
314     MessageParcel reply;
315     MessageOption option;
316 
317     if (!data.WriteInterfaceToken(GetDescriptor())) {
318         CLOGE("Failed to write the interface token");
319         return CAST_ENGINE_ERROR;
320     }
321     if (!data.WriteInt32(delta)) {
322         CLOGE("Failed to write the delta");
323         return CAST_ENGINE_ERROR;
324     }
325     if (Remote()->SendRequest(FAST_REWIND, data, reply, option) != ERR_NONE) {
326         CLOGE("Failed to send ipc request when fastRewind");
327         return CAST_ENGINE_ERROR;
328     }
329 
330     return reply.ReadInt32();
331 }
332 
SetVolume(int volume)333 int32_t StreamPlayerImplProxy::SetVolume(int volume)
334 {
335     MessageParcel data;
336     MessageParcel reply;
337     MessageOption option;
338 
339     if (!data.WriteInterfaceToken(GetDescriptor())) {
340         CLOGE("Failed to write the interface token");
341         return CAST_ENGINE_ERROR;
342     }
343     if (!data.WriteInt32(volume)) {
344         CLOGE("Failed to write the position");
345         return CAST_ENGINE_ERROR;
346     }
347     if (Remote()->SendRequest(SET_VOLUME, data, reply, option) != ERR_NONE) {
348         CLOGE("Failed to send ipc request when set volume");
349         return CAST_ENGINE_ERROR;
350     }
351 
352     return reply.ReadInt32();
353 }
354 
SetMute(bool mute)355 int32_t StreamPlayerImplProxy::SetMute(bool mute)
356 {
357     MessageParcel data;
358     MessageParcel reply;
359     MessageOption option;
360 
361     if (!data.WriteInterfaceToken(GetDescriptor())) {
362         CLOGE("Failed to write the interface token");
363         return CAST_ENGINE_ERROR;
364     }
365     if (!data.WriteBool(mute)) {
366         CLOGE("Failed to write the mute");
367         return CAST_ENGINE_ERROR;
368     }
369     if (Remote()->SendRequest(SET_MUTE, data, reply, option) != ERR_NONE) {
370         CLOGE("Failed to send ipc request when set mute");
371         return CAST_ENGINE_ERROR;
372     }
373 
374     return reply.ReadInt32();
375 }
376 
SetLoopMode(const LoopMode mode)377 int32_t StreamPlayerImplProxy::SetLoopMode(const LoopMode mode)
378 {
379     MessageParcel data;
380     MessageParcel reply;
381     MessageOption option;
382 
383     if (!data.WriteInterfaceToken(GetDescriptor())) {
384         CLOGE("Failed to write the interface token");
385         return CAST_ENGINE_ERROR;
386     }
387     if (!data.WriteInt32(static_cast<int32_t>(mode))) {
388         CLOGE("Failed to write the position");
389         return CAST_ENGINE_ERROR;
390     }
391     if (Remote()->SendRequest(SET_LOOP_MODE, data, reply, option) != ERR_NONE) {
392         CLOGE("Failed to send ipc request when set volume");
393         return CAST_ENGINE_ERROR;
394     }
395 
396     return reply.ReadInt32();
397 }
398 
SetSpeed(const PlaybackSpeed speed)399 int32_t StreamPlayerImplProxy::SetSpeed(const PlaybackSpeed speed)
400 {
401     MessageParcel data;
402     MessageParcel reply;
403     MessageOption option;
404 
405     if (!data.WriteInterfaceToken(GetDescriptor())) {
406         CLOGE("Failed to write the interface token");
407         return CAST_ENGINE_ERROR;
408     }
409     if (!data.WriteInt32(static_cast<int32_t>(speed))) {
410         CLOGE("Failed to write the position");
411         return CAST_ENGINE_ERROR;
412     }
413     if (Remote()->SendRequest(SET_SPEED, data, reply, option) != ERR_NONE) {
414         CLOGE("Failed to send ipc request when set volume");
415         return CAST_ENGINE_ERROR;
416     }
417 
418     return reply.ReadInt32();
419 }
420 
GetPlayerStatus(PlayerStates &playerStates)421 int32_t StreamPlayerImplProxy::GetPlayerStatus(PlayerStates &playerStates)
422 {
423     MessageParcel data;
424     MessageParcel reply;
425     MessageOption option;
426 
427     playerStates = PlayerStates::PLAYER_STATE_ERROR;
428     if (!data.WriteInterfaceToken(GetDescriptor())) {
429         CLOGE("Failed to write the interface token");
430         return CAST_ENGINE_ERROR;
431     }
432     if (Remote()->SendRequest(GET_PLAYER_STATUS, data, reply, option) != ERR_NONE) {
433         CLOGE("Failed to send ipc request when get player status");
434         return CAST_ENGINE_ERROR;
435     }
436 
437     int32_t errorCode = reply.ReadInt32();
438     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
439     playerStates = static_cast<PlayerStates>(reply.ReadInt32());
440 
441     return errorCode;
442 }
443 
GetPosition(int &position)444 int32_t StreamPlayerImplProxy::GetPosition(int &position)
445 {
446     MessageParcel data;
447     MessageParcel reply;
448     MessageOption option;
449 
450     if (!data.WriteInterfaceToken(GetDescriptor())) {
451         CLOGE("Failed to write the interface token");
452         return CAST_ENGINE_ERROR;
453     }
454     if (Remote()->SendRequest(GET_POSITION, data, reply, option) != ERR_NONE) {
455         CLOGE("Failed to send ipc request when get position");
456         return CAST_ENGINE_ERROR;
457     }
458 
459     int32_t errorCode = reply.ReadInt32();
460     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
461     position = reply.ReadInt32();
462 
463     return errorCode;
464 }
465 
GetDuration(int &duration)466 int32_t StreamPlayerImplProxy::GetDuration(int &duration)
467 {
468     MessageParcel data;
469     MessageParcel reply;
470     MessageOption option;
471 
472     if (!data.WriteInterfaceToken(GetDescriptor())) {
473         CLOGE("Failed to write the interface token");
474         return CAST_ENGINE_ERROR;
475     }
476     if (Remote()->SendRequest(GET_DURATION, data, reply, option) != ERR_NONE) {
477         CLOGE("Failed to send ipc request when get duration");
478         return CAST_ENGINE_ERROR;
479     }
480 
481     int32_t errorCode = reply.ReadInt32();
482     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
483     duration = reply.ReadInt32();
484 
485     return errorCode;
486 }
487 
GetVolume(int &volume, int &maxVolume)488 int32_t StreamPlayerImplProxy::GetVolume(int &volume, int &maxVolume)
489 {
490     MessageParcel data;
491     MessageParcel reply;
492     MessageOption option;
493 
494     if (!data.WriteInterfaceToken(GetDescriptor())) {
495         CLOGE("Failed to write the interface token");
496         return CAST_ENGINE_ERROR;
497     }
498     if (Remote()->SendRequest(GET_VOLUME, data, reply, option) != ERR_NONE) {
499         CLOGE("Failed to send ipc request when get duration");
500         return CAST_ENGINE_ERROR;
501     }
502 
503     int32_t errorCode = reply.ReadInt32();
504     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
505     volume = reply.ReadInt32();
506     maxVolume = reply.ReadInt32();
507 
508     return errorCode;
509 }
510 
GetMute(bool &mute)511 int32_t StreamPlayerImplProxy::GetMute(bool &mute)
512 {
513     MessageParcel data;
514     MessageParcel reply;
515     MessageOption option;
516 
517     if (!data.WriteInterfaceToken(GetDescriptor())) {
518         CLOGE("Failed to write the interface token");
519         return CAST_ENGINE_ERROR;
520     }
521     if (Remote()->SendRequest(GET_MUTE, data, reply, option) != ERR_NONE) {
522         CLOGE("Failed to send ipc request when query is mute");
523         return CAST_ENGINE_ERROR;
524     }
525 
526     int32_t errorCode = reply.ReadInt32();
527     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
528     mute = reply.ReadBool();
529 
530     return errorCode;
531 }
532 
GetLoopMode(LoopMode &loopMode)533 int32_t StreamPlayerImplProxy::GetLoopMode(LoopMode &loopMode)
534 {
535     MessageParcel data;
536     MessageParcel reply;
537     MessageOption option;
538 
539     loopMode = LoopMode::LOOP_MODE_LIST;
540     if (!data.WriteInterfaceToken(GetDescriptor())) {
541         CLOGE("Failed to write the interface token");
542         return CAST_ENGINE_ERROR;
543     }
544     if (Remote()->SendRequest(GET_LOOP_MODE, data, reply, option) != ERR_NONE) {
545         CLOGE("Failed to send ipc request when get duration");
546         return CAST_ENGINE_ERROR;
547     }
548 
549     int32_t errorCode = reply.ReadInt32();
550     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
551     loopMode = static_cast<LoopMode>(reply.ReadInt32());
552 
553     return errorCode;
554 }
555 
GetPlaySpeed(PlaybackSpeed &playbackSpeed)556 int32_t StreamPlayerImplProxy::GetPlaySpeed(PlaybackSpeed &playbackSpeed)
557 {
558     MessageParcel data;
559     MessageParcel reply;
560     MessageOption option;
561 
562     playbackSpeed = PlaybackSpeed::SPEED_FORWARD_1_00_X;
563     if (!data.WriteInterfaceToken(GetDescriptor())) {
564         CLOGE("Failed to write the interface token");
565         return CAST_ENGINE_ERROR;
566     }
567     if (Remote()->SendRequest(GET_PLAY_SPEED, data, reply, option) != ERR_NONE) {
568         CLOGE("Failed to send ipc request when get duration");
569         return CAST_ENGINE_ERROR;
570     }
571 
572     int32_t errorCode = reply.ReadInt32();
573     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
574     playbackSpeed = static_cast<PlaybackSpeed>(reply.ReadInt32());
575 
576     return errorCode;
577 }
578 
GetMediaInfoHolder(MediaInfoHolder &mediaInfoHolder)579 int32_t StreamPlayerImplProxy::GetMediaInfoHolder(MediaInfoHolder &mediaInfoHolder)
580 {
581     MessageParcel data;
582     MessageParcel reply;
583     MessageOption option;
584 
585     if (!data.WriteInterfaceToken(GetDescriptor())) {
586         CLOGE("Failed to write the interface token");
587         return CAST_ENGINE_ERROR;
588     }
589     if (Remote()->SendRequest(GET_MEDIA_INFO_HOLDER, data, reply, option) != ERR_NONE) {
590         CLOGE("Failed to send ipc request when get duration");
591         return CAST_ENGINE_ERROR;
592     }
593 
594     int32_t errorCode = reply.ReadInt32();
595     CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
596     auto mediaInfos = ReadMediaInfoHolder(reply);
597     if (mediaInfos == nullptr) {
598         CLOGE("GetMediaInfoHolder, mediaInfoHolder is null");
599         return CAST_ENGINE_ERROR;
600     }
601     mediaInfoHolder = *mediaInfos;
602 
603     return errorCode;
604 }
605 
Release()606 int32_t StreamPlayerImplProxy::Release()
607 {
608     MessageParcel data;
609     MessageParcel reply;
610     MessageOption option;
611 
612     if (!data.WriteInterfaceToken(GetDescriptor())) {
613         CLOGE("Failed to write the interface token");
614         return false;
615     }
616     if (Remote()->SendRequest(RELEASE, data, reply, option) != ERR_NONE) {
617         CLOGE("Failed to send ipc request when Releasing stream player");
618         return false;
619     }
620 
621     return reply.ReadInt32();
622 }
623 } // namespace CastEngineClient
624 } // namespace CastEngine
625 } // namespace OHOS
626