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 <cinttypes>
18 #include <sys/stat.h>
19 #include "media_log.h"
20 #include "player_client.h"
21 #include "pms_interface.h"
22 
23 using namespace std;
24 
25 namespace OHOS {
26 namespace Media {
27 class Player;
28 
29 #define CHK_NULL_RETURN(ptr) \
30 do { \
31     if (ptr == nullptr) { \
32         MEDIA_ERR_LOG("ptr null"); \
33         return -1; \
34     } \
35 } while (0)
36 
Player()37 Player::Player()
38 {
39     MEDIA_INFO_LOG("Player process");
40     player_ = PlayerClient::GetInstance();
41 }
42 
~Player()43 Player::~Player()
44 {
45     MEDIA_INFO_LOG("~Player process");
46 }
47 
SetSource(const Source & source)48 int32_t Player::SetSource(const Source &source)
49 {
50     MEDIA_INFO_LOG("process in");
51     int32_t ret;
52     CHK_NULL_RETURN(player_);
53     ret = player_->SetSource(source);
54     MEDIA_INFO_LOG("process out");
55     return ret;
56 }
57 
Prepare()58 int32_t Player::Prepare()
59 {
60     MEDIA_INFO_LOG("process in");
61     if (CheckSelfPermission("ohos.permission.MODIFY_AUDIO_SETTINGS") != GRANTED) {
62         MEDIA_WARNING_LOG("Process can not access audio-setting.");
63         return MEDIA_PERMISSION_DENIED;
64     }
65     if (CheckSelfPermission("ohos.permission.READ_MEDIA") != GRANTED) {
66         MEDIA_WARNING_LOG("Process can not read media.");
67         return MEDIA_PERMISSION_DENIED;
68     }
69     CHK_NULL_RETURN(player_);
70     return player_->Prepare();
71 }
72 
Play()73 int32_t Player::Play()
74 {
75     MEDIA_INFO_LOG("process in");
76     CHK_NULL_RETURN(player_);
77     return player_->Play();
78 }
79 
IsPlaying()80 bool Player::IsPlaying()
81 {
82     MEDIA_INFO_LOG("process in");
83     if (player_ == nullptr) {
84         MEDIA_ERR_LOG("ptr null");
85         return false;
86     }
87     return player_->IsPlaying();
88 }
89 
Pause()90 int32_t Player::Pause()
91 {
92     MEDIA_INFO_LOG("process in");
93     CHK_NULL_RETURN(player_);
94     return player_->Pause();
95 }
96 
Stop()97 int32_t Player::Stop()
98 {
99     MEDIA_INFO_LOG("process in");
100     CHK_NULL_RETURN(player_);
101     return player_->Stop();
102 }
103 
Rewind(int64_t mSeconds,int32_t mode)104 int32_t Player::Rewind(int64_t mSeconds, int32_t mode)
105 {
106     MEDIA_INFO_LOG("process in");
107     CHK_NULL_RETURN(player_);
108     return player_->Rewind(mSeconds, mode);
109 }
110 
SetVolume(float leftVolume,float rightVolume)111 int32_t Player::SetVolume(float leftVolume, float rightVolume)
112 {
113     MEDIA_INFO_LOG("process in");
114     CHK_NULL_RETURN(player_);
115     return player_->SetVolume(leftVolume, rightVolume);
116 }
117 
SetVideoSurface(Surface * surface)118 int32_t Player::SetVideoSurface(Surface *surface)
119 {
120     MEDIA_INFO_LOG("process in");
121     CHK_NULL_RETURN(player_);
122     return player_->SetSurface(surface);
123 }
124 
IsSingleLooping()125 bool Player::IsSingleLooping()
126 {
127     MEDIA_INFO_LOG("process in");
128     if (player_ == nullptr) {
129         MEDIA_ERR_LOG("ptr null");
130         return false;
131     }
132     return player_->IsSingleLooping();
133 }
134 
GetCurrentTime(int64_t & time) const135 int32_t Player::GetCurrentTime(int64_t &time) const
136 {
137     CHK_NULL_RETURN(player_);
138     return player_->GetCurrentPosition(time);
139 }
140 
GetDuration(int64_t & durationMs) const141 int32_t Player::GetDuration(int64_t &durationMs) const
142 {
143     MEDIA_INFO_LOG("process in");
144     CHK_NULL_RETURN(player_);
145     return player_->GetDuration(durationMs);
146 }
147 
GetVideoWidth(int32_t & videoWidth)148 int32_t Player::GetVideoWidth(int32_t &videoWidth)
149 {
150     MEDIA_INFO_LOG("process in");
151     CHK_NULL_RETURN(player_);
152     return player_->GetVideoWidth(videoWidth);
153 }
154 
GetVideoHeight(int32_t & videoHeight)155 int32_t Player::GetVideoHeight(int32_t &videoHeight)
156 {
157     MEDIA_INFO_LOG("process in");
158     CHK_NULL_RETURN(player_);
159     return player_->GetVideoHeight(videoHeight);
160 }
161 
Reset()162 int32_t Player::Reset()
163 {
164     MEDIA_INFO_LOG("process in");
165     CHK_NULL_RETURN(player_);
166     return player_->Reset();
167 }
168 
Release()169 int32_t Player::Release()
170 {
171     MEDIA_INFO_LOG("process in");
172     CHK_NULL_RETURN(player_);
173     return player_->Release();
174 }
175 
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & cb)176 void Player::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb)
177 {
178     MEDIA_INFO_LOG("process in");
179     if (player_ == nullptr) {
180         MEDIA_ERR_LOG("ptr null");
181         return;
182     }
183     player_->SetPlayerCallback(cb);
184 }
185 
EnableSingleLooping(bool loop)186 int32_t Player::EnableSingleLooping(bool loop)
187 {
188     MEDIA_INFO_LOG("process in");
189     CHK_NULL_RETURN(player_);
190     return player_->SetLoop(loop);
191 }
192 
GetPlayerState(int32_t & state) const193 int32_t Player::GetPlayerState(int32_t &state) const
194 {
195     MEDIA_INFO_LOG("process in");
196     CHK_NULL_RETURN(player_);
197     return player_->GetPlayerState(state);
198 }
199 
SetPlaybackSpeed(float speed)200 int32_t Player::SetPlaybackSpeed(float speed)
201 {
202     MEDIA_INFO_LOG("process in");
203     CHK_NULL_RETURN(player_);
204     return player_->SetPlaybackSpeed(speed);
205 }
206 
GetPlaybackSpeed(float & speed)207 int32_t Player::GetPlaybackSpeed(float &speed)
208 {
209     MEDIA_INFO_LOG("process in");
210     CHK_NULL_RETURN(player_);
211     return player_->GetPlaybackSpeed(speed);
212 }
213 
SetAudioStreamType(int32_t type)214 int32_t Player::SetAudioStreamType(int32_t type)
215 {
216     MEDIA_INFO_LOG("process in");
217     CHK_NULL_RETURN(player_);
218     return player_->SetAudioStreamType(type);
219 }
220 
GetAudioStreamType(int32_t & type)221 void Player::GetAudioStreamType(int32_t &type)
222 {
223     MEDIA_INFO_LOG("process in");
224     if (player_ == nullptr) {
225         MEDIA_ERR_LOG("player_ null");
226         return;
227     }
228     player_->GetAudioStreamType(type);
229 }
230 
SetParameter(const Format & params)231 int32_t Player::SetParameter(const Format &params)
232 {
233     MEDIA_INFO_LOG("process in");
234     CHK_NULL_RETURN(player_);
235     return player_->SetParameter(params);
236 }
237 }  // namespace Media
238 }  // namespace OHOS
239