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 #ifdef SUPPORT_AVPLAYER_DRM
21 #include "i_keysession_service.h"
22 #endif
23 
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "PlayerImpl"};
26 }
27 
28 namespace OHOS {
29 namespace Media {
CreatePlayer()30 std::shared_ptr<Player> PlayerFactory::CreatePlayer()
31 {
32     MEDIA_LOGD("PlayerImpl: CreatePlayer in");
33     std::shared_ptr<PlayerImpl> impl = std::make_shared<PlayerImpl>();
34     CHECK_AND_RETURN_RET_LOG(impl != nullptr, nullptr, "failed to new PlayerImpl");
35 
36     int32_t ret = impl->Init();
37     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to init PlayerImpl");
38 
39     return impl;
40 }
41 
Init()42 int32_t PlayerImpl::Init()
43 {
44     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Init in", FAKE_POINTER(this));
45     HiviewDFX::HiTraceChain::SetId(traceId_);
46     playerService_ = MediaServiceFactory::GetInstance().CreatePlayerService();
47     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_UNKNOWN, "failed to create player service");
48     return MSERR_OK;
49 }
50 
PlayerImpl()51 PlayerImpl::PlayerImpl()
52 {
53     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
54     ResetSeekVariables();
55     traceId_ = HiviewDFX::HiTraceChain::Begin("PlayerImpl", HITRACE_FLAG_DEFAULT);
56 }
57 
~PlayerImpl()58 PlayerImpl::~PlayerImpl()
59 {
60     if (playerService_ != nullptr) {
61         (void)MediaServiceFactory::GetInstance().DestroyPlayerService(playerService_);
62         playerService_ = nullptr;
63     }
64     ResetSeekVariables();
65     HiviewDFX::HiTraceChain::End(traceId_);
66     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
67 }
68 
ResetSeekVariables()69 void PlayerImpl::ResetSeekVariables()
70 {
71     mCurrentPosition = INT32_MIN;
72     mCurrentSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
73     mSeekPosition = INT32_MIN;
74     mSeekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
75     isSeeking_ = false;
76 }
77 
SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted)78 int32_t PlayerImpl::SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted)
79 {
80     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_INVALID_VAL, "playerService_ not exist");
81     return playerService_->SetMediaMuted(mediaType, isMuted);
82 }
83 
SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc)84 int32_t PlayerImpl::SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc)
85 {
86     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(dataSrc)", FAKE_POINTER(this));
87     CHECK_AND_RETURN_RET_LOG(dataSrc != nullptr, MSERR_INVALID_VAL, "failed to create data source");
88     return playerService_->SetSource(dataSrc);
89 }
90 
SetSource(const std::string &url)91 int32_t PlayerImpl::SetSource(const std::string &url)
92 {
93     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
94     CHECK_AND_RETURN_RET_LOG(!url.empty(), MSERR_INVALID_VAL, "url is empty..");
95     return playerService_->SetSource(url);
96 }
97 
SetSource(int32_t fd, int64_t offset, int64_t size)98 int32_t PlayerImpl::SetSource(int32_t fd, int64_t offset, int64_t size)
99 {
100     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetSource in(fd)", FAKE_POINTER(this));
101     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
102     return playerService_->SetSource(fd, offset, size);
103 }
104 
AddSubSource(const std::string &url)105 int32_t PlayerImpl::AddSubSource(const std::string &url)
106 {
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 &currentTime)
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 
GetPlaybackPosition(int32_t &currentTime)300 int32_t PlayerImpl::GetPlaybackPosition(int32_t &currentTime)
301 {
302     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackPosition in", FAKE_POINTER(this));
303     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
304     return playerService_->GetPlaybackPosition(currentTime);
305 }
306 
GetVideoTrackInfo(std::vector<Format> &videoTrack)307 int32_t PlayerImpl::GetVideoTrackInfo(std::vector<Format> &videoTrack)
308 {
309     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoTrackInfo in", FAKE_POINTER(this));
310     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
311     return playerService_->GetVideoTrackInfo(videoTrack);
312 }
313 
GetPlaybackInfo(Format &playbackInfo)314 int32_t PlayerImpl::GetPlaybackInfo(Format &playbackInfo)
315 {
316     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackInfo in", FAKE_POINTER(this));
317     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
318     return playerService_->GetPlaybackInfo(playbackInfo);
319 }
320 
GetAudioTrackInfo(std::vector<Format> &audioTrack)321 int32_t PlayerImpl::GetAudioTrackInfo(std::vector<Format> &audioTrack)
322 {
323     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetAudioTrackInfo in", FAKE_POINTER(this));
324     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
325     return playerService_->GetAudioTrackInfo(audioTrack);
326 }
327 
GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack)328 int32_t PlayerImpl::GetSubtitleTrackInfo(std::vector<Format> &subtitleTrack)
329 {
330     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetSubtitleTrackInfo in", FAKE_POINTER(this));
331     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
332     return playerService_->GetSubtitleTrackInfo(subtitleTrack);
333 }
334 
GetVideoWidth()335 int32_t PlayerImpl::GetVideoWidth()
336 {
337     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoWidth in", FAKE_POINTER(this));
338     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
339     return playerService_->GetVideoWidth();
340 }
341 
GetVideoHeight()342 int32_t PlayerImpl::GetVideoHeight()
343 {
344     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetVideoHeight in", FAKE_POINTER(this));
345     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
346     return playerService_->GetVideoHeight();
347 }
348 
SetPlaybackSpeed(PlaybackRateMode mode)349 int32_t PlayerImpl::SetPlaybackSpeed(PlaybackRateMode mode)
350 {
351     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlaybackSpeed in, mode is %{public}d", FAKE_POINTER(this), mode);
352     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
353     return playerService_->SetPlaybackSpeed(mode);
354 }
355 
SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy)356 int32_t PlayerImpl::SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy)
357 {
358     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetMediaSource in(dataSrc)", FAKE_POINTER(this));
359     CHECK_AND_RETURN_RET_LOG(mediaSource != nullptr, MSERR_INVALID_VAL, "mediaSource is nullptr!");
360     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
361     return playerService_->SetMediaSource(mediaSource, strategy);
362 }
363 
GetPlaybackSpeed(PlaybackRateMode &mode)364 int32_t PlayerImpl::GetPlaybackSpeed(PlaybackRateMode &mode)
365 {
366     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetPlaybackSpeed in", FAKE_POINTER(this));
367     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
368     return playerService_->GetPlaybackSpeed(mode);
369 }
370 
SelectBitRate(uint32_t bitRate)371 int32_t PlayerImpl::SelectBitRate(uint32_t bitRate)
372 {
373     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectBitRate(%{public}d) in", FAKE_POINTER(this), bitRate);
374     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
375     return playerService_->SelectBitRate(bitRate);
376 }
377 
GetDuration(int32_t &duration)378 int32_t PlayerImpl::GetDuration(int32_t &duration)
379 {
380     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetDuration in", FAKE_POINTER(this));
381     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
382     return playerService_->GetDuration(duration);
383 }
384 
385 #ifdef SUPPORT_VIDEO
SetVideoSurface(sptr<Surface> surface)386 int32_t PlayerImpl::SetVideoSurface(sptr<Surface> surface)
387 {
388     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetVideoSurface in", FAKE_POINTER(this));
389     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
390     CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_INVALID_VAL, "surface is nullptr");
391     surface_ = surface;
392     return playerService_->SetVideoSurface(surface);
393 }
394 #endif
395 
IsPlaying()396 bool PlayerImpl::IsPlaying()
397 {
398     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsPlaying in", FAKE_POINTER(this));
399     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
400 
401     return playerService_->IsPlaying();
402 }
403 
IsLooping()404 bool PlayerImpl::IsLooping()
405 {
406     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " IsLooping in", FAKE_POINTER(this));
407     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, false, "player service does not exist..");
408 
409     return playerService_->IsLooping();
410 }
411 
SetLooping(bool loop)412 int32_t PlayerImpl::SetLooping(bool loop)
413 {
414     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetLooping in, loop %{public}d", FAKE_POINTER(this), loop);
415     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
416     return playerService_->SetLooping(loop);
417 }
418 
SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)419 int32_t PlayerImpl::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
420 {
421     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetPlayerCallback in", FAKE_POINTER(this));
422     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
423     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "callback is nullptr");
424     {
425         std::unique_lock<std::mutex> lock(cbMutex_);
426         callback_ = callback;
427     }
428 
429     std::shared_ptr<PlayerCallback> playerCb = std::make_shared<PlayerImplCallback>(callback, shared_from_this());
430     return playerService_->SetPlayerCallback(playerCb);
431 }
432 
SetParameter(const Format &param)433 int32_t PlayerImpl::SetParameter(const Format &param)
434 {
435     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetParameter in", FAKE_POINTER(this));
436     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
437     return playerService_->SetParameter(param);
438 }
439 
SelectTrack(int32_t index, PlayerSwitchMode mode)440 int32_t PlayerImpl::SelectTrack(int32_t index, PlayerSwitchMode mode)
441 {
442     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SelectTrack in", FAKE_POINTER(this));
443     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
444     return playerService_->SelectTrack(index, mode);
445 }
446 
DeselectTrack(int32_t index)447 int32_t PlayerImpl::DeselectTrack(int32_t index)
448 {
449     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " DeselectTrack in", FAKE_POINTER(this));
450     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
451     return playerService_->DeselectTrack(index);
452 }
453 
GetCurrentTrack(int32_t trackType, int32_t &index)454 int32_t PlayerImpl::GetCurrentTrack(int32_t trackType, int32_t &index)
455 {
456     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " GetCurrentTrack in", FAKE_POINTER(this));
457     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
458     return playerService_->GetCurrentTrack(trackType, index);
459 }
460 
461 
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy, bool svp)462 int32_t PlayerImpl::SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy, bool svp)
463 {
464     MEDIA_LOGI("PlayerImpl DRM SetDecryptConfig");
465 #ifdef SUPPORT_AVPLAYER_DRM
466     CHECK_AND_RETURN_RET_LOG(keySessionProxy != nullptr, MSERR_INVALID_VAL, "keysessionproxy is nullptr");
467     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
468     MEDIA_LOGD("And it's count is: %{public}d in PlayerImpl", keySessionProxy->GetSptrRefCount());
469     return playerService_->SetDecryptConfig(keySessionProxy, svp);
470 #else
471     (void)keySessionProxy;
472     (void)svp;
473     return 0;
474 #endif
475 }
476 
SetDeviceChangeCbStatus(bool status)477 int32_t PlayerImpl::SetDeviceChangeCbStatus(bool status)
478 {
479     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetDeviceChangeCbStatus in, status is %{public}d",
480         FAKE_POINTER(this), status);
481     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
482     return playerService_->SetDeviceChangeCbStatus(status);
483 }
484 
SetPlaybackStrategy(AVPlayStrategy playbackStrategy)485 int32_t PlayerImpl::SetPlaybackStrategy(AVPlayStrategy playbackStrategy)
486 {
487     MEDIA_LOGD("Set playback strategy");
488     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist..");
489     return playerService_->SetPlaybackStrategy(playbackStrategy);
490 }
491 
PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb, std::shared_ptr<PlayerImpl> player)492 PlayerImplCallback::PlayerImplCallback(const std::shared_ptr<PlayerCallback> playerCb,
493     std::shared_ptr<PlayerImpl> player)
494 {
495     playerCb_ = playerCb;
496     player_ = player;
497 }
498 
SetMaxAmplitudeCbStatus(bool status)499 int32_t PlayerImpl::SetMaxAmplitudeCbStatus(bool status)
500 {
501     MEDIA_LOGD("PlayerImpl:0x%{public}06" PRIXPTR " SetMaxAmplitudeCbStatus in, status is %{public}d",
502         FAKE_POINTER(this), status);
503     CHECK_AND_RETURN_RET_LOG(playerService_ != nullptr, MSERR_SERVICE_DIED, "player service does not exist.");
504     return playerService_->SetMaxAmplitudeCbStatus(status);
505 }
506 
OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)507 void PlayerImplCallback::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
508 {
509     auto player = player_.lock();
510     CHECK_AND_RETURN_LOG(player != nullptr, "player does not exist..");
511     player->OnInfo(type, extra, infoBody);
512 }
513 
OnError(int32_t errorCode, const std::string &errorMsg)514 void PlayerImplCallback::OnError(int32_t errorCode, const std::string &errorMsg)
515 {
516     std::shared_ptr<PlayerCallback> playerCb;
517     {
518         std::unique_lock<std::mutex> lock(playerImplCbMutex_);
519         playerCb = playerCb_;
520     }
521 
522     CHECK_AND_RETURN_LOG(playerCb != nullptr, "playerCb does not exist..");
523     playerCb->OnError(errorCode, errorMsg);
524 }
525 
526 } // namespace Media
527 } // namespace OHOS
528