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: Stream Player function realization.
15 * Author: huangchanggui
16 * Create: 2023-01-12
17 */
18
19 #include "stream_player.h"
20 #include "cast_engine_errors.h"
21 #include "cast_engine_log.h"
22 #include "stream_player_listener_impl_stub.h"
23
24 namespace OHOS {
25 namespace CastEngine {
26 namespace CastEngineClient {
27 DEFINE_CAST_ENGINE_LABEL("Cast-Client-StreamPlayer");
28
~StreamPlayer()29 StreamPlayer::~StreamPlayer()
30 {
31 CLOGD("destructor in");
32 }
33
RegisterListener(std::shared_ptr<IStreamPlayerListener> listener)34 int32_t StreamPlayer::RegisterListener(std::shared_ptr<IStreamPlayerListener> listener)
35 {
36 if (listener == nullptr) {
37 CLOGE("listener is null");
38 return ERR_INVALID_PARAM;
39 }
40 sptr<IStreamPlayerListenerImpl> listenerStub = new (std::nothrow) StreamPlayerListenerImplStub(listener);
41 if (listenerStub == nullptr) {
42 CLOGE("Failed to new a stream player listener");
43 return CAST_ENGINE_ERROR;
44 }
45
46 return proxy_ ? proxy_->RegisterListener(listenerStub) : CAST_ENGINE_ERROR;
47 }
48
UnregisterListener()49 int32_t StreamPlayer::UnregisterListener()
50 {
51 return proxy_ ? proxy_->UnregisterListener() : CAST_ENGINE_ERROR;
52 }
53
SetSurface(const std::string & surfaceId)54 int32_t StreamPlayer::SetSurface(const std::string &surfaceId)
55 {
56 errno = 0;
57 uint64_t surfaceUniqueId = static_cast<uint64_t>(std::strtoll(surfaceId.c_str(), nullptr, 10));
58 if (errno == ERANGE) {
59 return ERR_INVALID_PARAM;
60 }
61
62 sptr<Surface> surface = SurfaceUtils::GetInstance()->GetSurface(surfaceUniqueId);
63 if (!surface) {
64 CLOGE("surface is null, surface uniqueId %llu", surfaceUniqueId);
65 return CAST_ENGINE_ERROR;
66 }
67 sptr<IBufferProducer> producer = surface->GetProducer();
68 if (!producer) {
69 CLOGE("producer is null");
70 return CAST_ENGINE_ERROR;
71 }
72 return proxy_ ? proxy_->SetSurface(producer) : CAST_ENGINE_ERROR;
73 }
74
Load(const MediaInfo & mediaInfo)75 int32_t StreamPlayer::Load(const MediaInfo &mediaInfo)
76 {
77 return proxy_ ? proxy_->Load(mediaInfo) : CAST_ENGINE_ERROR;
78 }
79
Play(const MediaInfo & mediaInfo)80 int32_t StreamPlayer::Play(const MediaInfo &mediaInfo)
81 {
82 return proxy_ ? proxy_->Play(mediaInfo) : CAST_ENGINE_ERROR;
83 }
84
Play(int index)85 int32_t StreamPlayer::Play(int index)
86 {
87 return proxy_ ? proxy_->Play(index) : CAST_ENGINE_ERROR;
88 }
89
Play()90 int32_t StreamPlayer::Play()
91 {
92 return proxy_ ? proxy_->Play() : CAST_ENGINE_ERROR;
93 }
94
Pause()95 int32_t StreamPlayer::Pause()
96 {
97 return proxy_ ? proxy_->Pause() : CAST_ENGINE_ERROR;
98 }
99
Stop()100 int32_t StreamPlayer::Stop()
101 {
102 return proxy_ ? proxy_->Stop() : CAST_ENGINE_ERROR;
103 }
104
Next()105 int32_t StreamPlayer::Next()
106 {
107 return proxy_ ? proxy_->Next() : CAST_ENGINE_ERROR;
108 }
109
Previous()110 int32_t StreamPlayer::Previous()
111 {
112 return proxy_ ? proxy_->Previous() : CAST_ENGINE_ERROR;
113 }
114
Seek(int position)115 int32_t StreamPlayer::Seek(int position)
116 {
117 return proxy_ ? proxy_->Seek(position) : CAST_ENGINE_ERROR;
118 }
119
FastForward(int delta)120 int32_t StreamPlayer::FastForward(int delta)
121 {
122 return proxy_ ? proxy_->FastForward(delta) : CAST_ENGINE_ERROR;
123 }
124
FastRewind(int delta)125 int32_t StreamPlayer::FastRewind(int delta)
126 {
127 return proxy_ ? proxy_->FastRewind(delta) : CAST_ENGINE_ERROR;
128 }
129
SetVolume(int volume)130 int32_t StreamPlayer::SetVolume(int volume)
131 {
132 return proxy_ ? proxy_->SetVolume(volume) : CAST_ENGINE_ERROR;
133 }
134
SetMute(bool mute)135 int32_t StreamPlayer::SetMute(bool mute)
136 {
137 return proxy_ ? proxy_->SetMute(mute) : CAST_ENGINE_ERROR;
138 }
139
SetLoopMode(const LoopMode mode)140 int32_t StreamPlayer::SetLoopMode(const LoopMode mode)
141 {
142 return proxy_ ? proxy_->SetLoopMode(mode) : CAST_ENGINE_ERROR;
143 }
144
SetSpeed(const PlaybackSpeed speed)145 int32_t StreamPlayer::SetSpeed(const PlaybackSpeed speed)
146 {
147 return proxy_ ? proxy_->SetSpeed(speed) : CAST_ENGINE_ERROR;
148 }
149
GetPlayerStatus(PlayerStates & playerStates)150 int32_t StreamPlayer::GetPlayerStatus(PlayerStates &playerStates)
151 {
152 return proxy_ ? proxy_->GetPlayerStatus(playerStates) : CAST_ENGINE_ERROR;
153 }
154
GetPosition(int & position)155 int32_t StreamPlayer::GetPosition(int &position)
156 {
157 return proxy_ ? proxy_->GetPosition(position) : CAST_ENGINE_ERROR;
158 }
159
GetDuration(int & duration)160 int32_t StreamPlayer::GetDuration(int &duration)
161 {
162 return proxy_ ? proxy_->GetDuration(duration) : CAST_ENGINE_ERROR;
163 }
164
GetVolume(int & volume,int & maxVolume)165 int32_t StreamPlayer::GetVolume(int &volume, int &maxVolume)
166 {
167 return proxy_ ? proxy_->GetVolume(volume, maxVolume) : CAST_ENGINE_ERROR;
168 }
169
GetMute(bool & mute)170 int32_t StreamPlayer::GetMute(bool &mute)
171 {
172 return proxy_ ? proxy_->GetMute(mute) : CAST_ENGINE_ERROR;
173 }
174
GetLoopMode(LoopMode & loopMode)175 int32_t StreamPlayer::GetLoopMode(LoopMode &loopMode)
176 {
177 return proxy_ ? proxy_->GetLoopMode(loopMode) : CAST_ENGINE_ERROR;
178 }
179
GetPlaySpeed(PlaybackSpeed & playbackSpeed)180 int32_t StreamPlayer::GetPlaySpeed(PlaybackSpeed &playbackSpeed)
181 {
182 return proxy_ ? proxy_->GetPlaySpeed(playbackSpeed) : CAST_ENGINE_ERROR;
183 }
184
GetMediaInfoHolder(MediaInfoHolder & mediaInfoHolder)185 int32_t StreamPlayer::GetMediaInfoHolder(MediaInfoHolder &mediaInfoHolder)
186 {
187 return proxy_ ? proxy_->GetMediaInfoHolder(mediaInfoHolder) : CAST_ENGINE_ERROR;
188 }
189
Release()190 int32_t StreamPlayer::Release()
191 {
192 return proxy_ ? proxy_->Release() : CAST_ENGINE_ERROR;
193 }
194 } // namespace CastEngineClient
195 } // namespace CastEngine
196 } // namespace OHOS