1 /*
2  * Copyright (c) 2020-2021 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 "player.h"
17 
18 #include <cinttypes>
19 #include <memory>
20 #include <sys/stat.h>
21 #include "histreamer/hiplayer.h"
22 #include "media_log.h"
23 
24 using namespace std;
25 
26 namespace OHOS {
27 namespace Media {
28 class Player;
29 
30 namespace {
31 std::shared_ptr<Media::PlayerInterface> g_player;
32 }
33 
Player()34 Player::Player()
35 {
36     MEDIA_INFO_LOG("Player constructor in");
37     g_player = CreateHiPlayer();
38     MEDIA_INFO_LOG("Player constructor out");
39 }
40 
~Player()41 Player::~Player()
42 {
43     MEDIA_INFO_LOG("~Player destructor");
44 }
45 
SetSource(const Source & source)46 int32_t Player::SetSource(const Source &source)
47 {
48     MEDIA_INFO_LOG("Player SetSource in");
49     int32_t ret;
50     if (g_player == nullptr) {
51         MEDIA_ERR_LOG("player null");
52         return -1;
53     }
54     ret = g_player->SetSource(source);
55     MEDIA_INFO_LOG("Player SetSource out, ret: %d", ret);
56     return ret;
57 }
58 
Prepare()59 int32_t Player::Prepare()
60 {
61     MEDIA_INFO_LOG("Player Prepare in");
62     if (g_player == nullptr) {
63         MEDIA_ERR_LOG("player null");
64         return -1;
65     }
66     return g_player->Prepare();
67 }
68 
Play()69 int32_t Player::Play()
70 {
71     MEDIA_INFO_LOG("Player Play in");
72     if (g_player == nullptr) {
73         MEDIA_ERR_LOG("player null");
74         return -1;
75     }
76     return g_player->Play();
77 }
78 
IsPlaying()79 bool Player::IsPlaying()
80 {
81     MEDIA_INFO_LOG("Player IsPlaying in");
82     if (g_player == nullptr) {
83         MEDIA_ERR_LOG("player null");
84         return false;
85     }
86     return g_player->IsPlaying();
87 }
88 
Pause()89 int32_t Player::Pause()
90 {
91     MEDIA_INFO_LOG("Player Pause in");
92     if (g_player == nullptr) {
93         MEDIA_ERR_LOG("player null");
94         return -1;
95     }
96     return g_player->Pause();
97 }
98 
Stop()99 int32_t Player::Stop()
100 {
101     MEDIA_INFO_LOG("Player Stop in");
102     if (g_player == nullptr) {
103         MEDIA_ERR_LOG("player null");
104         return -1;
105     }
106     return g_player->Stop();
107 }
108 
Rewind(int64_t mSeconds,int32_t mode)109 int32_t Player::Rewind(int64_t mSeconds, int32_t mode)
110 {
111     MEDIA_INFO_LOG("Player Rewind in");
112     if (g_player == nullptr) {
113         MEDIA_ERR_LOG("player null");
114         return -1;
115     }
116     return g_player->Rewind(mSeconds, mode);
117 }
118 
SetVolume(float leftVolume,float rightVolume)119 int32_t Player::SetVolume(float leftVolume, float rightVolume)
120 {
121     MEDIA_INFO_LOG("Player SetVolume in");
122     if (g_player == nullptr) {
123         MEDIA_ERR_LOG("player null");
124         return -1;
125     }
126     return g_player->SetVolume(leftVolume, rightVolume);
127 }
128 
IsSingleLooping()129 bool Player::IsSingleLooping()
130 {
131     MEDIA_INFO_LOG("Player IsSingleLooping in");
132     if (g_player == nullptr) {
133         MEDIA_ERR_LOG("player null");
134         return false;
135     }
136     return g_player->IsSingleLooping();
137 }
138 
GetCurrentTime(int64_t & time) const139 int32_t Player::GetCurrentTime(int64_t &time) const
140 {
141     MEDIA_INFO_LOG("Player GetCurrentTime in");
142     if (g_player == nullptr) {
143         MEDIA_ERR_LOG("player null");
144         return -1;
145     }
146     return g_player->GetCurrentPosition(time);
147 }
148 
GetDuration(int64_t & durationMs) const149 int32_t Player::GetDuration(int64_t &durationMs) const
150 {
151     MEDIA_INFO_LOG("Player GetDuration in");
152     if (g_player == nullptr) {
153         MEDIA_ERR_LOG("player null");
154         return -1;
155     }
156     return g_player->GetDuration(durationMs);
157 }
158 
GetVideoWidth(int32_t & videoWidth)159 int32_t Player::GetVideoWidth(int32_t &videoWidth)
160 {
161     MEDIA_INFO_LOG("Player GetVideoWidth in");
162     if (g_player == nullptr) {
163         MEDIA_ERR_LOG("player null");
164         return -1;
165     }
166     return g_player->GetVideoWidth(videoWidth);
167 }
168 
GetVideoHeight(int32_t & videoHeight)169 int32_t Player::GetVideoHeight(int32_t &videoHeight)
170 {
171     MEDIA_INFO_LOG("Player GetVideoHeight in");
172     if (g_player == nullptr) {
173         MEDIA_ERR_LOG("player null");
174         return -1;
175     }
176     return g_player->GetVideoHeight(videoHeight);
177 }
178 
Reset()179 int32_t Player::Reset()
180 {
181     MEDIA_INFO_LOG("Player Reset in");
182     if (g_player == nullptr) {
183         MEDIA_ERR_LOG("player null");
184         return -1;
185     }
186     return g_player->Reset();
187 }
188 
Release()189 int32_t Player::Release()
190 {
191     MEDIA_INFO_LOG("Player Release in");
192     if (g_player == nullptr) {
193         MEDIA_ERR_LOG("player null");
194         return -1;
195     }
196     return g_player->Release();
197 }
198 
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & cb)199 void Player::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb)
200 {
201     MEDIA_INFO_LOG("Player SetPlayerCallback in");
202     if (g_player == nullptr) {
203         MEDIA_ERR_LOG("ptr null");
204         return;
205     }
206     g_player->SetPlayerCallback(cb);
207 }
208 
EnableSingleLooping(bool loop)209 int32_t Player::EnableSingleLooping(bool loop)
210 {
211     MEDIA_INFO_LOG("Player EnableSingleLooping in");
212     if (g_player == nullptr) {
213         MEDIA_ERR_LOG("player null");
214         return -1;
215     }
216     return g_player->SetLoop(loop);
217 }
218 
GetPlayerState(int32_t & state) const219 int32_t Player::GetPlayerState(int32_t &state) const
220 {
221     MEDIA_INFO_LOG("Player GetPlayerState in");
222     if (g_player == nullptr) {
223         MEDIA_ERR_LOG("player null");
224         return PLAYER_STATE_ERROR;
225     }
226     return g_player->GetPlayerState(state);
227 }
228 
SetPlaybackSpeed(float speed)229 int32_t Player::SetPlaybackSpeed(float speed)
230 {
231     MEDIA_INFO_LOG("Player SetPlaybackSpeed in");
232     if (g_player == nullptr) {
233         MEDIA_ERR_LOG("player null");
234         return -1;
235     }
236     return g_player->SetPlaybackSpeed(speed);
237 }
238 
GetPlaybackSpeed(float & speed)239 int32_t Player::GetPlaybackSpeed(float &speed)
240 {
241     MEDIA_INFO_LOG("Player GetPlaybackSpeed in");
242     if (g_player == nullptr) {
243         MEDIA_ERR_LOG("player null");
244         return -1;
245     }
246     return g_player->GetPlaybackSpeed(speed);
247 }
248 
SetAudioStreamType(int32_t type)249 int32_t Player::SetAudioStreamType(int32_t type)
250 {
251     MEDIA_INFO_LOG("Player SetAudioStreamType in");
252     if (g_player == nullptr) {
253         MEDIA_ERR_LOG("player null");
254         return -1;
255     }
256     return g_player->SetAudioStreamType(type);
257 }
258 
GetAudioStreamType(int32_t & type)259 void Player::GetAudioStreamType(int32_t &type)
260 {
261     MEDIA_INFO_LOG("Player GetAudioStreamType in");
262     if (g_player == nullptr) {
263         MEDIA_ERR_LOG("ptr null");
264         return;
265     }
266     g_player->GetAudioStreamType(type);
267 }
268 
SetParameter(const Format & params)269 int32_t Player::SetParameter(const Format &params)
270 {
271     MEDIA_INFO_LOG("Player SetParameter in");
272     if (g_player == nullptr) {
273         MEDIA_ERR_LOG("player null");
274         return -1;
275     }
276     return g_player->SetParameter(params);
277 }
278 } // namespace Media
279 } // namespace OHOS
280