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