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_PLAYER, "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 HiviewDFX::HiTraceChain::SetId(traceId_);
43 playerService_ = MediaServiceFactory::GetInstance().CreatePlayerService();
44 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_UNKNOWN, "failed to create player service");
45 return MSERR_OK;
46 }
47
PlayerImpl()48 PlayerImpl::PlayerImpl()
49 {
50 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
51 ResetSeekVariables();
52 traceId_ = HiviewDFX::HiTraceChain::Begin("PlayerImpl", HITRACE_FLAG_DEFAULT);
53 }
54
~PlayerImpl()55 PlayerImpl::~PlayerImpl()
56 {
57 if (playerService_ != nullptr) {
58 (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
59 playerService_ = nullptr;
60 }
61 ResetSeekVariables();
62 HiviewDFX::HiTraceChain::End(traceId_);
63 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
64 }
65
ResetSeekVariables()66 void PlayerImpl::ResetSeekVariables()
67 {
68 mCurrentPosition = INT32_MIN;
69 mCurrentSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
70 mSeekPosition = INT32_MIN;
71 mSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
72 isSeeking_ = false;
73 }
74
SetMediaMuted(OHOS::Media::MediaType mediaType,bool isMuted)75 int32_t PlayerImpl::SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted)
76 {
77 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_INVALID_VAL, "playerService_ not exist");
78 return playerService_->SetMediaMuted(mediaType, isMuted);
79 }
80
SetSource(const std::shared_ptr<IMediaDataSource> & dataSrc)81 int32_t PlayerImpl::SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc)
82 {
83 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(dataSrc)", FAKE_POINTER(this));
84 CHECK_AND_RETURN_RET_LOG(dataSrc != nullptr, MSERR_INVALID_VAL, "failed to create data source");
85 return playerService_->SetSource(dataSrc);
86 }
87
SetSource(const std::string & url)88 int32_t PlayerImpl::SetSource(const std::string &url)
89 {
90 MEDIA_LOGD("PlayerImpl:0x%{private}06" PRIXPTR " SetSource in(url): %{private}s", FAKE_POINTER(this), url.c_str());
91 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
92 CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
93 return playerService_->SetSource(url);
94 }
95
SetSource(int32_t fd,int64_t offset,int64_t size)96 int32_t PlayerImpl::SetSource(int32_t fd, int64_t offset, int64_t size)
97 {
98 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(fd)", FAKE_POINTER(this));
99 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
100 return playerService_->SetSource(fd, offset, size);
101 }
102
AddSubSource(const std::string & url)103 int32_t PlayerImpl::AddSubSource(const std::string &url)
104 {
105 MEDIA_LOGD("PlayerImpl:0x%{private}06" PRIXPTR " AddSubSource in(url): %{private}s",
106 FAKE_POINTER(this), url.c_str());
107 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
108 CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
109 return playerService_->AddSubSource(url);
110 }
111
AddSubSource(int32_t fd,int64_t offset,int64_t size)112 int32_t PlayerImpl::AddSubSource(int32_t fd, int64_t offset, int64_t size)
113 {
114 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " AddSubSource in(fd)", FAKE_POINTER(this));
115 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
116 return playerService_->AddSubSource(fd, offset, size);
117 }
118
Play()119 int32_t PlayerImpl::Play()
120 {
121 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Play in", FAKE_POINTER(this));
122 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
123 return playerService_->Play();
124 }
125
SetPlayRange(int64_t start,int64_t end)126 int32_t PlayerImpl::SetPlayRange(int64_t start, int64_t end)
127 {
128 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayRange in", FAKE_POINTER(this));
129 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
130 return playerService_->SetPlayRange(start, end);
131 }
132
SetPlayRangeWithMode(int64_t start,int64_t end,PlayerSeekMode mode)133 int32_t PlayerImpl::SetPlayRangeWithMode(int64_t start, int64_t end, PlayerSeekMode mode)
134 {
135 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayRangeWithMode in", FAKE_POINTER(this));
136 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
137 return playerService_->SetPlayRangeWithMode(start, end, mode);
138 }
139
Prepare()140 int32_t PlayerImpl::Prepare()
141 {
142 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Prepare in", FAKE_POINTER(this));
143 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
144 return playerService_->Prepare();
145 }
146
SetRenderFirstFrame(bool display)147 int32_t PlayerImpl::SetRenderFirstFrame(bool display)
148 {
149 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetRenderFirstFrame in, display %{public}d",
150 FAKE_POINTER(this), display);
151 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
152 return playerService_->SetRenderFirstFrame(display);
153 }
154
PrepareAsync()155 int32_t PlayerImpl::PrepareAsync()
156 {
157 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " PrepareAsync in", FAKE_POINTER(this));
158 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
159 return playerService_->PrepareAsync();
160 }
161
Pause()162 int32_t PlayerImpl::Pause()
163 {
164 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Pause in", FAKE_POINTER(this));
165 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
166 return playerService_->Pause();
167 }
168
Stop()169 int32_t PlayerImpl::Stop()
170 {
171 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Stop in", FAKE_POINTER(this));
172 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
173 ResetSeekVariables();
174 return playerService_->Stop();
175 }
176
Reset()177 int32_t PlayerImpl::Reset()
178 {
179 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Reset in", FAKE_POINTER(this));
180 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
181 ResetSeekVariables();
182 return playerService_->Reset();
183 }
184
Release()185 int32_t PlayerImpl::Release()
186 {
187 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Release in", FAKE_POINTER(this));
188 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
189 (void)playerService_->Release();
190 (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
191 playerService_ = nullptr;
192 return MSERR_OK;
193 }
194
ReleaseSync()195 int32_t PlayerImpl::ReleaseSync()
196 {
197 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " ReleaseSync in", FAKE_POINTER(this));
198 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
199 (void)playerService_->ReleaseSync();
200 (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
201 playerService_ = nullptr;
202 return MSERR_OK;
203 }
204
SetVolume(float leftVolume,float rightVolume)205 int32_t PlayerImpl::SetVolume(float leftVolume, float rightVolume)
206 {
207 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVolume(%{public}f, %{public}f) in",
208 FAKE_POINTER(this), leftVolume, rightVolume);
209 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
210 return playerService_->SetVolume(leftVolume, rightVolume);
211 }
212
Seek(int32_t mSeconds,PlayerSeekMode mode)213 int32_t PlayerImpl::Seek(int32_t mSeconds, PlayerSeekMode mode)
214 {
215 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Seek in, seek to %{public}d ms, mode is %{public}d",
216 FAKE_POINTER(this), mSeconds, mode);
217 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
218
219 std::unique_lock<std::recursive_mutex> lock(recMutex_);
220 // SEEK_CONTINOUS is usually called in batches, and will not report seek done event.
221 if (mode == PlayerSeekMode::SEEK_CONTINOUS) {
222 return playerService_->Seek(mSeconds, mode);
223 }
224 mCurrentPosition = mSeconds;
225 mCurrentSeekMode = mode;
226 if ((mSeekPosition != mCurrentPosition || mSeekMode != mCurrentSeekMode) && !isSeeking_) {
227 MEDIA_LOGI("Start seek once.");
228 isSeeking_ = true;
229 mSeekPosition = mSeconds;
230 mSeekMode = mode;
231 auto retCode = playerService_->Seek(mSeconds, mode);
232 if (retCode != MSERR_OK) {
233 ResetSeekVariables();
234 }
235 MEDIA_LOGI("Start seek once end");
236 return retCode;
237 } else {
238 MEDIA_LOGE("Seeking not completed, need wait the lastest seek end, then seek again.");
239 }
240 MEDIA_LOGI("Seeking task end. %{public}d ms, mode is %{public}d", mSeconds, mode);
241 return MSERR_OK;
242 }
243
HandleSeekDoneInfo(PlayerOnInfoType type,int32_t extra)244 void PlayerImpl::HandleSeekDoneInfo(PlayerOnInfoType type, int32_t extra)
245 {
246 if (type == INFO_TYPE_SEEKDONE) {
247 MEDIA_LOGI("HandleSeekDoneInfo entered");
248 CHECK_AND_RETURN_LOG(playerService_ != nullptr, "player service does not exist..");
249 if (extra == -1) {
250 MEDIA_LOGI("seek error, need reset seek variables");
251 ResetSeekVariables();
252 return;
253 }
254 std::unique_lock<std::recursive_mutex> lock(recMutex_);
255 if (mSeekPosition != mCurrentPosition || mSeekMode != mCurrentSeekMode) {
256 MEDIA_LOGI("Start seek again (%{public}d, %{public}d)", mCurrentPosition, mCurrentSeekMode);
257 mSeekPosition = mCurrentPosition;
258 mSeekMode = mCurrentSeekMode;
259 playerService_->Seek(mCurrentPosition, mCurrentSeekMode);
260 } else {
261 MEDIA_LOGI("All seeks complete - return to regularly scheduled program");
262 ResetSeekVariables();
263 }
264 MEDIA_LOGI("HandleSeekDoneInfo end seekTo(%{public}d, %{public}d)", mCurrentPosition, mCurrentSeekMode);
265 }
266 }
267
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)268 void PlayerImpl::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
269 {
270 HandleSeekDoneInfo(type, extra);
271 std::shared_ptr<PlayerCallback> callback;
272 {
273 std::unique_lock<std::mutex> lock(cbMutex_);
274 callback = callback_;
275 }
276
277 CHECK_AND_RETURN_LOG(callback != nullptr, "callback is nullptr.");
278 if (type == INFO_TYPE_SEEKDONE) {
279 if (extra == -1) {
280 MEDIA_LOGI("seek done error callback, no need report");
281 return;
282 }
283 if (!isSeeking_) {
284 callback->OnInfo(type, extra, infoBody);
285 } else {
286 MEDIA_LOGD("Is seeking to (%{public}d, %{public}d), not update now", mCurrentPosition, mCurrentSeekMode);
287 }
288 } else {
289 callback->OnInfo(type, extra, infoBody);
290 }
291 }
292
GetCurrentTime(int32_t & currentTime)293 int32_t PlayerImpl::GetCurrentTime(int32_t ¤tTime)
294 {
295 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTime in", FAKE_POINTER(this));
296 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
297 return playerService_->GetCurrentTime(currentTime);
298 }
299
GetVideoTrackInfo(std::vector<Format> & videoTrack)300 int32_t PlayerImpl::GetVideoTrackInfo(std::vector<Format> &videoTrack)
301 {
302 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoTrackInfo in", FAKE_POINTER(this));
303 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
304 return playerService_->GetVideoTrackInfo(videoTrack);
305 }
306
GetPlaybackInfo(Format & playbackInfo)307 int32_t PlayerImpl::GetPlaybackInfo(Format &playbackInfo)
308 {
309 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackInfo in", FAKE_POINTER(this));
310 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
311 return playerService_->GetPlaybackInfo(playbackInfo);
312 }
313
GetAudioTrackInfo(std::vector<Format> & audioTrack)314 int32_t PlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
315 {
316 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetAudioTrackInfo in", FAKE_POINTER(this));
317 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
318 return playerService_->GetAudioTrackInfo(audioTrack);
319 }
320
GetSubtitleTrackInfo(std::vector<Format> & subtitleTrack)321 int32_t PlayerImpl::GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack)
322 {
323 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetSubtitleTrackInfo in", FAKE_POINTER(this));
324 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
325 return playerService_->GetSubtitleTrackInfo(subtitleTrack);
326 }
327
GetVideoWidth()328 int32_t PlayerImpl::GetVideoWidth()
329 {
330 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoWidth in", FAKE_POINTER(this));
331 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
332 return playerService_->GetVideoWidth();
333 }
334
GetVideoHeight()335 int32_t PlayerImpl::GetVideoHeight()
336 {
337 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoHeight in", FAKE_POINTER(this));
338 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
339 return playerService_->GetVideoHeight();
340 }
341
SetPlaybackSpeed(PlaybackRateMode mode)342 int32_t PlayerImpl::SetPlaybackSpeed(PlaybackRateMode mode)
343 {
344 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlaybackSpeed in, mode is %{public}d", FAKE_POINTER(this), mode);
345 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
346 return playerService_->SetPlaybackSpeed(mode);
347 }
348
SetMediaSource(const std::shared_ptr<AVMediaSource> & mediaSource,AVPlayStrategy strategy)349 int32_t PlayerImpl::SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy)
350 {
351 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetMediaSource in(dataSrc)", FAKE_POINTER(this));
352 CHECK_AND_RETURN_RET_LOG(mediaSource != nullptr, MSERR_INVALID_VAL, "mediaSource is nullptr!");
353 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
354 return playerService_->SetMediaSource(mediaSource, strategy);
355 }
356
GetPlaybackSpeed(PlaybackRateMode & mode)357 int32_t PlayerImpl::GetPlaybackSpeed(PlaybackRateMode &mode)
358 {
359 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackSpeed in", FAKE_POINTER(this));
360 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
361 return playerService_->GetPlaybackSpeed(mode);
362 }
363
SelectBitRate(uint32_t bitRate)364 int32_t PlayerImpl::SelectBitRate(uint32_t bitRate)
365 {
366 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectBitRate(%{public}d) in", FAKE_POINTER(this), bitRate);
367 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
368 return playerService_->SelectBitRate(bitRate);
369 }
370
GetDuration(int32_t & duration)371 int32_t PlayerImpl::GetDuration(int32_t &duration)
372 {
373 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetDuration in", FAKE_POINTER(this));
374 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
375 return playerService_->GetDuration(duration);
376 }
377
378 #ifdef SUPPORT_VIDEO
SetVideoSurface(sptr<Surface> surface)379 int32_t PlayerImpl::SetVideoSurface(sptr<Surface> surface)
380 {
381 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVideoSurface in", FAKE_POINTER(this));
382 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
383 CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_INVALID_VAL, "surface is nullptr");
384 surface_ = surface;
385 return playerService_->SetVideoSurface(surface);
386 }
387 #endif
388
IsPlaying()389 bool PlayerImpl::IsPlaying()
390 {
391 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsPlaying in", FAKE_POINTER(this));
392 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
393
394 return playerService_->IsPlaying();
395 }
396
IsLooping()397 bool PlayerImpl::IsLooping()
398 {
399 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsLooping in", FAKE_POINTER(this));
400 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
401
402 return playerService_->IsLooping();
403 }
404
SetLooping(bool loop)405 int32_t PlayerImpl::SetLooping(bool loop)
406 {
407 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetLooping in, loop %{public}d", FAKE_POINTER(this), loop);
408 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
409 return playerService_->SetLooping(loop);
410 }
411
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)412 int32_t PlayerImpl::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
413 {
414 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayerCallback in", FAKE_POINTER(this));
415 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
416 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "callback is nullptr");
417 {
418 std::unique_lock<std::mutex> lock(cbMutex_);
419 callback_ = callback;
420 }
421
422 std::shared_ptr<PlayerCallback> playerCb = std::make_shared<PlayerImplCallback>(callback, shared_from_this());
423 return playerService_->SetPlayerCallback(playerCb);
424 }
425
SetParameter(const Format & param)426 int32_t PlayerImpl::SetParameter(const Format ¶m)
427 {
428 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetParameter in", FAKE_POINTER(this));
429 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
430 return playerService_->SetParameter(param);
431 }
432
SelectTrack(int32_t index,PlayerSwitchMode mode)433 int32_t PlayerImpl::SelectTrack(int32_t index, PlayerSwitchMode mode)
434 {
435 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectTrack in", FAKE_POINTER(this));
436 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
437 return playerService_->SelectTrack(index, mode);
438 }
439
DeselectTrack(int32_t index)440 int32_t PlayerImpl::DeselectTrack(int32_t index)
441 {
442 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " DeselectTrack in", FAKE_POINTER(this));
443 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
444 return playerService_->DeselectTrack(index);
445 }
446
GetCurrentTrack(int32_t trackType,int32_t & index)447 int32_t PlayerImpl::GetCurrentTrack(int32_t trackType, int32_t &index)
448 {
449 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTrack in", FAKE_POINTER(this));
450 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
451 return playerService_->GetCurrentTrack(trackType, index);
452 }
453
454
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySessionProxy,bool svp)455 int32_t PlayerImpl::SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy, bool svp)
456 {
457 MEDIA_LOGI("PlayerImpl DRM SetDecryptConfig");
458 #ifdef SUPPORT_AVPLAYER_DRM
459 CHECK_AND_RETURN_RET_LOG(keySessionProxy != nullptr, MSERR_INVALID_VAL, "keysessionproxy is nullptr");
460 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
461 MEDIA_LOGD("And it's count is: %{public}d in PlayerImpl", keySessionProxy->GetSptrRefCount());
462 return playerService_->SetDecryptConfig(keySessionProxy, svp);
463 #else
464 (void)keySessionProxy;
465 (void)svp;
466 return 0;
467 #endif
468 }
469
SetDeviceChangeCbStatus(bool status)470 int32_t PlayerImpl::SetDeviceChangeCbStatus(bool status)
471 {
472 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetDeviceChangeCbStatus in, status is %{public}d",
473 FAKE_POINTER(this), status);
474 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
475 return playerService_->SetDeviceChangeCbStatus(status);
476 }
477
SetPlaybackStrategy(AVPlayStrategy playbackStrategy)478 int32_t PlayerImpl::SetPlaybackStrategy(AVPlayStrategy playbackStrategy)
479 {
480 MEDIA_LOGD("Set playback strategy");
481 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
482 return playerService_->SetPlaybackStrategy(playbackStrategy);
483 }
484
SetMaxAmplitudeCbStatus(bool status)485 int32_t PlayerImpl::SetMaxAmplitudeCbStatus(bool status)
486 {
487 MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetMaxAmplitudeCbStatus in, status is %{public}d",
488 FAKE_POINTER(this), status);
489 CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
490 return playerService_->SetMaxAmplitudeCbStatus(status);
491 }
492
PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb,std::shared_ptr<PlayerImpl> player)493 PlayerImplCallback::PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb,
494 std::shared_ptr<PlayerImpl> player)
495 {
496 playerCb_ = playerCb;
497 player_ = player;
498 }
499
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)500 void PlayerImplCallback::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
501 {
502 auto player = player_.lock();
503 CHECK_AND_RETURN_LOG(player != nullptr, "player does not exist..");
504 player->OnInfo(type, extra, infoBody);
505 }
506
OnError(int32_t errorCode,const std::string & errorMsg)507 void PlayerImplCallback::OnError(int32_t errorCode, const std::string &errorMsg)
508 {
509 std::shared_ptr<PlayerCallback> playerCb;
510 {
511 std::unique_lock<std::mutex> lock(playerImplCbMutex_);
512 playerCb = playerCb_;
513 }
514
515 CHECK_AND_RETURN_LOG(playerCb != nullptr, "playerCb does not exist..");
516 playerCb->OnError(errorCode, errorMsg);
517 }
518
519 } // namespace Media
520 } // namespace OHOS
521