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