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 return CAST_ENGINE_ERROR;
65 }
66 sptr<IBufferProducer> producer = surface->GetProducer();
67 if (!producer) {
68 CLOGE("producer is null");
69 return CAST_ENGINE_ERROR;
70 }
71 return proxy_ ? proxy_->SetSurface(producer) : CAST_ENGINE_ERROR;
72 }
73
Load(const MediaInfo &mediaInfo)74 int32_t StreamPlayer::Load(const MediaInfo &mediaInfo)
75 {
76 return proxy_ ? proxy_->Load(mediaInfo) : CAST_ENGINE_ERROR;
77 }
78
Play(const MediaInfo &mediaInfo)79 int32_t StreamPlayer::Play(const MediaInfo &mediaInfo)
80 {
81 return proxy_ ? proxy_->Play(mediaInfo) : CAST_ENGINE_ERROR;
82 }
83
Play(int index)84 int32_t StreamPlayer::Play(int index)
85 {
86 return proxy_ ? proxy_->Play(index) : CAST_ENGINE_ERROR;
87 }
88
Play()89 int32_t StreamPlayer::Play()
90 {
91 return proxy_ ? proxy_->Play() : CAST_ENGINE_ERROR;
92 }
93
Pause()94 int32_t StreamPlayer::Pause()
95 {
96 return proxy_ ? proxy_->Pause() : CAST_ENGINE_ERROR;
97 }
98
Stop()99 int32_t StreamPlayer::Stop()
100 {
101 return proxy_ ? proxy_->Stop() : CAST_ENGINE_ERROR;
102 }
103
Next()104 int32_t StreamPlayer::Next()
105 {
106 return proxy_ ? proxy_->Next() : CAST_ENGINE_ERROR;
107 }
108
Previous()109 int32_t StreamPlayer::Previous()
110 {
111 return proxy_ ? proxy_->Previous() : CAST_ENGINE_ERROR;
112 }
113
Seek(int position)114 int32_t StreamPlayer::Seek(int position)
115 {
116 return proxy_ ? proxy_->Seek(position) : CAST_ENGINE_ERROR;
117 }
118
FastForward(int delta)119 int32_t StreamPlayer::FastForward(int delta)
120 {
121 return proxy_ ? proxy_->FastForward(delta) : CAST_ENGINE_ERROR;
122 }
123
FastRewind(int delta)124 int32_t StreamPlayer::FastRewind(int delta)
125 {
126 return proxy_ ? proxy_->FastRewind(delta) : CAST_ENGINE_ERROR;
127 }
128
SetVolume(int volume)129 int32_t StreamPlayer::SetVolume(int volume)
130 {
131 return proxy_ ? proxy_->SetVolume(volume) : CAST_ENGINE_ERROR;
132 }
133
SetMute(bool mute)134 int32_t StreamPlayer::SetMute(bool mute)
135 {
136 return proxy_ ? proxy_->SetMute(mute) : CAST_ENGINE_ERROR;
137 }
138
SetLoopMode(const LoopMode mode)139 int32_t StreamPlayer::SetLoopMode(const LoopMode mode)
140 {
141 return proxy_ ? proxy_->SetLoopMode(mode) : CAST_ENGINE_ERROR;
142 }
143
SetSpeed(const PlaybackSpeed speed)144 int32_t StreamPlayer::SetSpeed(const PlaybackSpeed speed)
145 {
146 return proxy_ ? proxy_->SetSpeed(speed) : CAST_ENGINE_ERROR;
147 }
148
GetPlayerStatus(PlayerStates &playerStates)149 int32_t StreamPlayer::GetPlayerStatus(PlayerStates &playerStates)
150 {
151 return proxy_ ? proxy_->GetPlayerStatus(playerStates) : CAST_ENGINE_ERROR;
152 }
153
GetPosition(int &position)154 int32_t StreamPlayer::GetPosition(int &position)
155 {
156 return proxy_ ? proxy_->GetPosition(position) : CAST_ENGINE_ERROR;
157 }
158
GetDuration(int &duration)159 int32_t StreamPlayer::GetDuration(int &duration)
160 {
161 return proxy_ ? proxy_->GetDuration(duration) : CAST_ENGINE_ERROR;
162 }
163
GetVolume(int &volume, int &maxVolume)164 int32_t StreamPlayer::GetVolume(int &volume, int &maxVolume)
165 {
166 return proxy_ ? proxy_->GetVolume(volume, maxVolume) : CAST_ENGINE_ERROR;
167 }
168
GetMute(bool &mute)169 int32_t StreamPlayer::GetMute(bool &mute)
170 {
171 return proxy_ ? proxy_->GetMute(mute) : CAST_ENGINE_ERROR;
172 }
173
GetLoopMode(LoopMode &loopMode)174 int32_t StreamPlayer::GetLoopMode(LoopMode &loopMode)
175 {
176 return proxy_ ? proxy_->GetLoopMode(loopMode) : CAST_ENGINE_ERROR;
177 }
178
GetPlaySpeed(PlaybackSpeed &playbackSpeed)179 int32_t StreamPlayer::GetPlaySpeed(PlaybackSpeed &playbackSpeed)
180 {
181 return proxy_ ? proxy_->GetPlaySpeed(playbackSpeed) : CAST_ENGINE_ERROR;
182 }
183
GetMediaInfoHolder(MediaInfoHolder &mediaInfoHolder)184 int32_t StreamPlayer::GetMediaInfoHolder(MediaInfoHolder &mediaInfoHolder)
185 {
186 return proxy_ ? proxy_->GetMediaInfoHolder(mediaInfoHolder) : CAST_ENGINE_ERROR;
187 }
188
Release()189 int32_t StreamPlayer::Release()
190 {
191 return proxy_ ? proxy_->Release() : CAST_ENGINE_ERROR;
192 }
193 } // namespace CastEngineClient
194 } // namespace CastEngine
195 } // namespace OHOS