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