1 /*
2 * Copyright (C) 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_impl.h"
17 #include "i_media_service.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "PlayerImpl"};
23 }
24
25 namespace OHOS {
26 namespace Media {
CreatePlayer()27 std::shared_ptr<Player> PlayerFactory::CreatePlayer()
28 {
29 MEDIA_LOGD("PlayerImpl: CreatePlayer in");
30 std::shared_ptr<PlayerImpl> impl = std::make_shared<PlayerImpl>();
31 CHECK_AND_RETURN_RET_LOG(impl != nullptr, nullptr, "failed to new PlayerImpl");
32
33 int32_t ret = impl->Init();
34 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to init PlayerImpl");
35
36 return impl;
37 }
38
Init()39 int32_t PlayerImpl::Init()
40 {
41 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Init in", FAKE_POINTER(this));
42 playerService_ = MediaServiceFactory::GetInstance().CreatePlayerService();
43 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_UNKNOWN, "failed to create player service");
44 return MSERR_OK;
45 }
46
PlayerImpl()47 PlayerImpl::PlayerImpl()
48 {
49 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
50 }
51
~PlayerImpl()52 PlayerImpl::~PlayerImpl()
53 {
54 if (playerService_ != nullptr) {
55 (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
56 playerService_ = nullptr;
57 }
58 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
59 }
60
SetSource(const std::shared_ptr<IMediaDataSource> & dataSrc)61 int32_t PlayerImpl::SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc)
62 {
63 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(dataSrc)", FAKE_POINTER(this));
64 CHECK_AND_RETURN_RET_LOG(dataSrc != nullptr, MSERR_INVALID_VAL, "failed to create data source");
65 return playerService_->SetSource(dataSrc);
66 }
67
SetSource(const std::string & url)68 int32_t PlayerImpl::SetSource(const std::string &url)
69 {
70 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(url): %{public}s", FAKE_POINTER(this), url.c_str());
71 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
72 CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
73 return playerService_->SetSource(url);
74 }
75
SetSource(int32_t fd,int64_t offset,int64_t size)76 int32_t PlayerImpl::SetSource(int32_t fd, int64_t offset, int64_t size)
77 {
78 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(fd)", FAKE_POINTER(this));
79 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
80 return playerService_->SetSource(fd, offset, size);
81 }
82
AddSubSource(const std::string & url)83 int32_t PlayerImpl::AddSubSource(const std::string &url)
84 {
85 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " AddSubSource in(url): %{public}s", FAKE_POINTER(this), url.c_str());
86 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
87 CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
88 return playerService_->AddSubSource(url);
89 }
90
AddSubSource(int32_t fd,int64_t offset,int64_t size)91 int32_t PlayerImpl::AddSubSource(int32_t fd, int64_t offset, int64_t size)
92 {
93 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " AddSubSource in(fd)", FAKE_POINTER(this));
94 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
95 return playerService_->AddSubSource(fd, offset, size);
96 }
97
Play()98 int32_t PlayerImpl::Play()
99 {
100 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Play in", FAKE_POINTER(this));
101 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
102 return playerService_->Play();
103 }
104
Prepare()105 int32_t PlayerImpl::Prepare()
106 {
107 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Prepare in", FAKE_POINTER(this));
108 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
109 return playerService_->Prepare();
110 }
111
PrepareAsync()112 int32_t PlayerImpl::PrepareAsync()
113 {
114 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " PrepareAsync in", FAKE_POINTER(this));
115 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
116 return playerService_->PrepareAsync();
117 }
118
Pause()119 int32_t PlayerImpl::Pause()
120 {
121 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Pause in", FAKE_POINTER(this));
122 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
123 return playerService_->Pause();
124 }
125
Stop()126 int32_t PlayerImpl::Stop()
127 {
128 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Stop in", FAKE_POINTER(this));
129 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
130 return playerService_->Stop();
131 }
132
Reset()133 int32_t PlayerImpl::Reset()
134 {
135 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Reset in", FAKE_POINTER(this));
136 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
137 return playerService_->Reset();
138 }
139
Release()140 int32_t PlayerImpl::Release()
141 {
142 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Release in", FAKE_POINTER(this));
143 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
144 (void)playerService_->Release();
145 (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
146 playerService_ = nullptr;
147 return MSERR_OK;
148 }
149
ReleaseSync()150 int32_t PlayerImpl::ReleaseSync()
151 {
152 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " ReleaseSync in", FAKE_POINTER(this));
153 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
154 (void)playerService_->ReleaseSync();
155 (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
156 playerService_ = nullptr;
157 return MSERR_OK;
158 }
159
SetVolume(float leftVolume,float rightVolume)160 int32_t PlayerImpl::SetVolume(float leftVolume, float rightVolume)
161 {
162 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVolume(%{public}f, %{public}f) in",
163 FAKE_POINTER(this), leftVolume, rightVolume);
164 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
165 return playerService_->SetVolume(leftVolume, rightVolume);
166 }
167
Seek(int32_t mSeconds,PlayerSeekMode mode)168 int32_t PlayerImpl::Seek(int32_t mSeconds, PlayerSeekMode mode)
169 {
170 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Seek in, seek to %{public}d ms, mode is %{public}d",
171 FAKE_POINTER(this), mSeconds, mode);
172 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
173 return playerService_->Seek(mSeconds, mode);
174 }
175
GetCurrentTime(int32_t & currentTime)176 int32_t PlayerImpl::GetCurrentTime(int32_t ¤tTime)
177 {
178 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTime in", FAKE_POINTER(this));
179 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
180 return playerService_->GetCurrentTime(currentTime);
181 }
182
GetVideoTrackInfo(std::vector<Format> & videoTrack)183 int32_t PlayerImpl::GetVideoTrackInfo(std::vector<Format> &videoTrack)
184 {
185 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoTrackInfo in", FAKE_POINTER(this));
186 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
187 return playerService_->GetVideoTrackInfo(videoTrack);
188 }
189
GetAudioTrackInfo(std::vector<Format> & audioTrack)190 int32_t PlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
191 {
192 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetAudioTrackInfo in", FAKE_POINTER(this));
193 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
194 return playerService_->GetAudioTrackInfo(audioTrack);
195 }
196
GetSubtitleTrackInfo(std::vector<Format> & subtitleTrack)197 int32_t PlayerImpl::GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack)
198 {
199 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetSubtitleTrackInfo in", FAKE_POINTER(this));
200 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
201 return playerService_->GetSubtitleTrackInfo(subtitleTrack);
202 }
203
GetVideoWidth()204 int32_t PlayerImpl::GetVideoWidth()
205 {
206 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoWidth in", FAKE_POINTER(this));
207 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
208 return playerService_->GetVideoWidth();
209 }
210
GetVideoHeight()211 int32_t PlayerImpl::GetVideoHeight()
212 {
213 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoHeight in", FAKE_POINTER(this));
214 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
215 return playerService_->GetVideoHeight();
216 }
217
SetPlaybackSpeed(PlaybackRateMode mode)218 int32_t PlayerImpl::SetPlaybackSpeed(PlaybackRateMode mode)
219 {
220 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlaybackSpeed in, mode is %{public}d", FAKE_POINTER(this), mode);
221 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
222 return playerService_->SetPlaybackSpeed(mode);
223 }
224
GetPlaybackSpeed(PlaybackRateMode & mode)225 int32_t PlayerImpl::GetPlaybackSpeed(PlaybackRateMode &mode)
226 {
227 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackSpeed in", FAKE_POINTER(this));
228 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
229 return playerService_->GetPlaybackSpeed(mode);
230 }
231
SelectBitRate(uint32_t bitRate)232 int32_t PlayerImpl::SelectBitRate(uint32_t bitRate)
233 {
234 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectBitRate(%{public}d) in", FAKE_POINTER(this), bitRate);
235 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
236 return playerService_->SelectBitRate(bitRate);
237 }
238
GetDuration(int32_t & duration)239 int32_t PlayerImpl::GetDuration(int32_t &duration)
240 {
241 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetDuration in", FAKE_POINTER(this));
242 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
243 return playerService_->GetDuration(duration);
244 }
245
246 #ifdef SUPPORT_VIDEO
SetVideoSurface(sptr<Surface> surface)247 int32_t PlayerImpl::SetVideoSurface(sptr<Surface> surface)
248 {
249 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVideoSurface in", FAKE_POINTER(this));
250 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
251 CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_INVALID_VAL, "surface is nullptr");
252 surface_ = surface;
253 return playerService_->SetVideoSurface(surface);
254 }
255 #endif
256
IsPlaying()257 bool PlayerImpl::IsPlaying()
258 {
259 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsPlaying in", FAKE_POINTER(this));
260 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
261
262 return playerService_->IsPlaying();
263 }
264
IsLooping()265 bool PlayerImpl::IsLooping()
266 {
267 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsLooping in", FAKE_POINTER(this));
268 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
269
270 return playerService_->IsLooping();
271 }
272
SetLooping(bool loop)273 int32_t PlayerImpl::SetLooping(bool loop)
274 {
275 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetLooping in, loop %{public}d", FAKE_POINTER(this), loop);
276 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
277 return playerService_->SetLooping(loop);
278 }
279
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)280 int32_t PlayerImpl::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
281 {
282 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayerCallback in", FAKE_POINTER(this));
283 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
284 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "callback is nullptr");
285 return playerService_->SetPlayerCallback(callback);
286 }
287
SetParameter(const Format & param)288 int32_t PlayerImpl::SetParameter(const Format ¶m)
289 {
290 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetParameter in", FAKE_POINTER(this));
291 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
292 return playerService_->SetParameter(param);
293 }
294
SelectTrack(int32_t index)295 int32_t PlayerImpl::SelectTrack(int32_t index)
296 {
297 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectTrack in", FAKE_POINTER(this));
298 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
299 return playerService_->SelectTrack(index);
300 }
301
DeselectTrack(int32_t index)302 int32_t PlayerImpl::DeselectTrack(int32_t index)
303 {
304 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " DeselectTrack in", FAKE_POINTER(this));
305 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
306 return playerService_->DeselectTrack(index);
307 }
308
GetCurrentTrack(int32_t trackType,int32_t & index)309 int32_t PlayerImpl::GetCurrentTrack(int32_t trackType, int32_t &index)
310 {
311 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTrack in", FAKE_POINTER(this));
312 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
313 return playerService_->GetCurrentTrack(trackType, index);
314 }
315 } // namespace Media
316 } // namespace OHOS
317