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