• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "cj_avplayer.h"
17 
18 #include "media_log.h"
19 #ifdef SUPPORT_VIDEO
20 #include "surface_utils.h"
21 #endif
22 
23 using namespace OHOS::AudioStandard;
24 
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "CJAVPlayer"};
27 }
28 
29 template <typename T, typename = std::enable_if_t<std::is_same_v<int64_t, T> || std::is_same_v<int32_t, T>>>
StrToInt(const std::string_view & str,T & value)30 bool StrToInt(const std::string_view &str, T &value)
31 {
32     CHECK_AND_RETURN_RET(!str.empty() && (isdigit(str.front()) || (str.front() == '-')), false);
33     std::string valStr(str);
34     char *end = nullptr;
35     errno = 0;
36     const char *addr = valStr.c_str();
37     long long result = strtoll(addr, &end, 10); /* 10 means decimal */
38     CHECK_AND_RETURN_RET_LOG(result >= LLONG_MIN && result <= LLONG_MAX, false,
39                              "call StrToInt func false,  input str is: %{public}s!", valStr.c_str());
40     CHECK_AND_RETURN_RET_LOG(end != addr && end[0] == '\0' && errno != ERANGE, false,
41                              "call StrToInt func false,  input str is: %{public}s!", valStr.c_str());
42     if constexpr (std::is_same<int32_t, T>::value) {
43         CHECK_AND_RETURN_RET_LOG(result >= INT_MIN && result <= INT_MAX, false,
44                                  "call StrToInt func false,  input str is: %{public}s!", valStr.c_str());
45         value = static_cast<int32_t>(result);
46         return true;
47     }
48     value = result;
49     return true;
50 }
51 
52 namespace OHOS {
53 namespace Media {
CJAVPlayer()54 CJAVPlayer::CJAVPlayer()
55 {
56     MEDIA_LOGI("0x%{public}06" PRIXPTR " ctor", FAKE_POINTER(this));
57 }
58 
~CJAVPlayer()59 CJAVPlayer::~CJAVPlayer()
60 {
61 }
62 
Constructor()63 bool CJAVPlayer::Constructor()
64 {
65     player_ = PlayerFactory::CreatePlayer();
66     CHECK_AND_RETURN_RET_LOG(player_ != nullptr, false, "failed to CreatePlayer");
67 
68     playerCb_ = std::make_shared<CJAVPlayerCallback>(this);
69     (void)player_->SetPlayerCallback(playerCb_);
70     return true;
71 }
72 
NotifyDuration(int32_t duration)73 void CJAVPlayer::NotifyDuration(int32_t duration)
74 {
75     std::lock_guard<std::recursive_mutex> lock(taskMutex_);
76     duration_ = duration;
77 }
78 
NotifyPosition(int32_t position)79 void CJAVPlayer::NotifyPosition(int32_t position)
80 {
81     std::lock_guard<std::recursive_mutex> lock(taskMutex_);
82     position_ = position;
83 }
84 
NotifyState(PlayerStates state)85 void CJAVPlayer::NotifyState(PlayerStates state)
86 {
87     std::lock_guard<std::recursive_mutex> lock(taskMutex_);
88     if (state_ != state) {
89         state_ = state;
90         MEDIA_LOGI("0x%{public}06" PRIXPTR " notify %{public}s", FAKE_POINTER(this), GetCurrentState().c_str());
91         stateChangeCond_.notify_all();
92     }
93 }
94 
NotifyVideoSize(int32_t width,int32_t height)95 void CJAVPlayer::NotifyVideoSize(int32_t width, int32_t height)
96 {
97     std::lock_guard<std::recursive_mutex> lock(taskMutex_);
98     width_ = width;
99     height_ = height;
100 }
101 
NotifyIsLiveStream()102 void CJAVPlayer::NotifyIsLiveStream()
103 {
104     isLiveStream_ = true;
105 }
106 
NotifyDrmInfoUpdated(const std::multimap<std::string,std::vector<uint8_t>> & infos)107 void CJAVPlayer::NotifyDrmInfoUpdated(const std::multimap<std::string, std::vector<uint8_t>> &infos)
108 {
109     MEDIA_LOGD("NotifyDrmInfoUpdated");
110     for (auto &newItem : infos) {
111         auto pos = localDrmInfos_.equal_range(newItem.first);
112         if (pos.first == pos.second && pos.first == localDrmInfos_.end()) {
113             localDrmInfos_.insert(newItem);
114             continue;
115         }
116         bool isSame = false;
117         for (; pos.first != pos.second; ++pos.first) {
118             if (newItem.second == pos.first->second) {
119                 isSame = true;
120                 break;
121             }
122         }
123         if (!isSame) {
124             localDrmInfos_.insert(newItem);
125         }
126     }
127 }
128 
GetUrl()129 char *CJAVPlayer::GetUrl()
130 {
131     return MallocCString(url_.c_str());
132 }
133 
SetUrl(char * url)134 void CJAVPlayer::SetUrl(char *url)
135 {
136     MEDIA_LOGD("SetUrl In");
137     if (GetCurrentState() != AVPlayerState::STATE_IDLE) {
138         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "current state is not idle, unsupport set url");
139         return;
140     }
141 
142     StartListenCurrentResource(); // Listen to the events of the current resource
143 
144     url_ = url;
145     MEDIA_LOGD("SetUrl url: %{private}s", url_.c_str());
146     SetSource(url_);
147     MEDIA_LOGD("SetUrl Out");
148 }
149 
GetAVFileDescriptor()150 CAVFileDescriptor CJAVPlayer::GetAVFileDescriptor()
151 {
152     return CAVFileDescriptor{
153         .fd = fileDescriptor_.fd, .offset = fileDescriptor_.offset, .length = fileDescriptor_.length};
154 }
155 
SetAVFileDescriptor(CAVFileDescriptor fileDescriptor)156 void CJAVPlayer::SetAVFileDescriptor(CAVFileDescriptor fileDescriptor)
157 {
158     MEDIA_LOGI("SetAVFileDescriptor In");
159     if (GetCurrentState() != AVPlayerState::STATE_IDLE) {
160         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "current state is not idle, unsupport set fd");
161         return;
162     }
163 
164     StartListenCurrentResource(); // Listen to the events of the current resource
165     fileDescriptor_.fd = fileDescriptor.fd;
166     fileDescriptor_.offset = fileDescriptor.offset;
167     fileDescriptor_.length = fileDescriptor.length;
168 
169     if (player_ != nullptr) {
170         auto playerFd = fileDescriptor_;
171         MEDIA_LOGI("SetAVFileDescriptor fd: %{public}d, offset: %{public}" PRId64 ", size: %{public}" PRId64,
172                    playerFd.fd, playerFd.offset, playerFd.length);
173         if (player_->SetSource(playerFd.fd, playerFd.offset, playerFd.length) != MSERR_OK) {
174             OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "player SetSource FileDescriptor failed");
175         }
176     }
177 
178     MEDIA_LOGI("SetAVFileDescriptor Out");
179 }
180 
GetDataSrc()181 CAVDataSrcDescriptor CJAVPlayer::GetDataSrc()
182 {
183     MEDIA_LOGI("GetDataSrc In");
184 
185     int64_t fileSize;
186     if (dataSrcCb_ == nullptr) {
187         return CAVDataSrcDescriptor{.fileSize = -1, .callback = -1};
188     }
189     (void)dataSrcCb_->GetSize(fileSize);
190     int32_t callbackId = dataSrcCb_->GetCallbackId();
191 
192     MEDIA_LOGI("GetDataSrc Out");
193     return CAVDataSrcDescriptor{.fileSize = fileSize, .callback = callbackId};
194 }
195 
SetDataSrc(CAVDataSrcDescriptor dataSrcDescriptor)196 void CJAVPlayer::SetDataSrc(CAVDataSrcDescriptor dataSrcDescriptor)
197 {
198     MEDIA_LOGI("SetDataSrc In");
199 
200     if (GetCurrentState() != AVPlayerState::STATE_IDLE) {
201         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "current state is not idle, unsupport set dataSrc");
202         return;
203     }
204     StartListenCurrentResource(); // Listen to the events of the current resource
205 
206     if (dataSrcDescriptor.fileSize < -1 || dataSrcDescriptor.fileSize == 0) {
207         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "invalid parameters, please check parameter fileSize");
208         return;
209     }
210     MEDIA_LOGD("Recvive filesize is %{public}" PRId64 "", dataSrcDescriptor.fileSize);
211     dataSrcCb_ = std::make_shared<CJMediaDataSourceCallback>(dataSrcDescriptor.fileSize);
212     dataSrcCb_->SetCallback(dataSrcDescriptor.callback);
213 
214     if (player_ != nullptr) {
215         if (player_->SetSource(dataSrcCb_) != MSERR_OK) {
216             OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "player SetSource DataSrc failed");
217         } else {
218             std::lock_guard<std::recursive_mutex> lock(taskMutex_);
219             state_ = PlayerStates::PLAYER_INITIALIZED;
220         }
221         int64_t fileSize;
222         (void)dataSrcCb_->GetSize(fileSize);
223         if (fileSize == -1) {
224             isLiveStream_ = true;
225         }
226     }
227 
228     MEDIA_LOGI("SetDataSrc Out");
229 }
230 
GetSurfaceID()231 char *CJAVPlayer::GetSurfaceID()
232 {
233     return MallocCString(surface_.c_str());
234 }
235 
SetSurfaceID(char * surfaceId)236 void CJAVPlayer::SetSurfaceID(char *surfaceId)
237 {
238     MEDIA_LOGD("SetSurfaceID In");
239 
240     std::string curState = GetCurrentState();
241     bool setSurfaceFirst = curState == AVPlayerState::STATE_INITIALIZED;
242     bool switchSurface = curState == AVPlayerState::STATE_PREPARED || curState == AVPlayerState::STATE_PLAYING ||
243                          curState == AVPlayerState::STATE_PAUSED || curState == AVPlayerState::STATE_STOPPED ||
244                          curState == AVPlayerState::STATE_COMPLETED;
245 
246     if (setSurfaceFirst) {
247         MEDIA_LOGI("SetSurfaceID set surface first in %{public}s state", curState.c_str());
248     } else if (switchSurface) {
249         MEDIA_LOGI("SetSurfaceID switch surface in %{public}s state", curState.c_str());
250         std::string oldSurface = std::string(surface_);
251         if (oldSurface.empty()) {
252             OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "switch surface with no old surface");
253             return;
254         }
255     } else {
256         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
257                   "the attribute(SurfaceID) can only be set in the initialized state");
258         return;
259     }
260     surface_ = surfaceId;
261     SetSurface(std::string(surface_));
262 }
263 
GetLoop()264 bool CJAVPlayer::GetLoop()
265 {
266     return loop_;
267 }
268 
SetLoop(bool loop)269 void CJAVPlayer::SetLoop(bool loop)
270 {
271     MEDIA_LOGI("SetLoop In");
272     if (IsLiveSource()) {
273         OnErrorCb(MSERR_EXT_API9_UNSUPPORT_CAPABILITY, "The stream is live stream, not support loop");
274         return;
275     }
276 
277     if (!IsControllable()) {
278         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
279                   "current state is not prepared/playing/paused/completed, unsupport loop operation");
280         return;
281     }
282 
283     loop_ = loop;
284 
285     if (player_ != nullptr) {
286         (void)player_->SetLooping(loop_);
287     }
288 }
289 
GetVideoScaleType()290 int32_t CJAVPlayer::GetVideoScaleType()
291 {
292     return videoScaleType_;
293 }
294 
SetVideoScaleType(int32_t videoScaleType)295 void CJAVPlayer::SetVideoScaleType(int32_t videoScaleType)
296 {
297     MEDIA_LOGI("SetVideoScaleType In");
298 
299     if (!IsControllable()) {
300         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
301                   "current state is not prepared/playing/paused/completed, unsupport video scale operation");
302         return;
303     }
304 
305     videoScaleType_ = videoScaleType;
306 
307     if (player_ != nullptr) {
308         Format format;
309         (void)format.PutIntValue(PlayerKeys::VIDEO_SCALE_TYPE, videoScaleType);
310         (void)player_->SetParameter(format);
311     }
312     MEDIA_LOGI("SetVideoScaleType Out");
313 }
314 
GetAudioInterruptMode()315 int32_t CJAVPlayer::GetAudioInterruptMode()
316 {
317     return interruptMode_;
318 }
319 
SetAudioInterruptMode(int32_t interruptMode)320 void CJAVPlayer::SetAudioInterruptMode(int32_t interruptMode)
321 {
322     MEDIA_LOGI("SetAudioInterruptMode In");
323 
324     if (!IsControllable()) {
325         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
326                   "current state is not prepared/playing/paused/completed, unsupport audio interrupt operation");
327         return;
328     }
329 
330     if (interruptMode < AudioStandard::InterruptMode::SHARE_MODE ||
331         interruptMode > AudioStandard::InterruptMode::INDEPENDENT_MODE) {
332         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "invalid parameters, please check the input interrupt Mode");
333         return;
334     }
335     interruptMode_ = static_cast<AudioStandard::InterruptMode>(interruptMode);
336 
337     if (player_ != nullptr) {
338         Format format;
339         (void)format.PutIntValue(PlayerKeys::AUDIO_INTERRUPT_MODE, interruptMode_);
340         (void)player_->SetParameter(format);
341     }
342     MEDIA_LOGI("SetAudioInterruptMode Out");
343 }
344 
GetAudioRendererInfo()345 CAudioRendererInfo CJAVPlayer::GetAudioRendererInfo()
346 {
347     MEDIA_LOGI("GetAudioRendererInfo In");
348 
349     CAudioRendererInfo info;
350     info.usage = static_cast<int32_t>(audioRendererInfo_.streamUsage);
351     info.rendererFlags = audioRendererInfo_.rendererFlags;
352     MEDIA_LOGI("GetAudioRendererInfo Out");
353     return info;
354 }
355 
SetAudioRendererInfo(CAudioRendererInfo info)356 void CJAVPlayer::SetAudioRendererInfo(CAudioRendererInfo info)
357 {
358     MEDIA_LOGI("SetAudioRendererInfo In");
359 
360     if (GetCurrentState() != AVPlayerState::STATE_INITIALIZED) {
361         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
362                   "current state is not initialized, unsupport to set audio renderer info");
363         return;
364     }
365     if (!HandleParameter(info)) {
366         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "invalid parameters, please check the input audio renderer info");
367         return;
368     }
369     if (player_ != nullptr) {
370         Format format;
371         (void)format.PutIntValue(PlayerKeys::STREAM_USAGE, audioRendererInfo_.streamUsage);
372         (void)format.PutIntValue(PlayerKeys::RENDERER_FLAG, audioRendererInfo_.rendererFlags);
373         (void)player_->SetParameter(format);
374     }
375     MEDIA_LOGI("SetAudioRendererInfo Out");
376 }
377 
GetAudioEffectMode()378 int32_t CJAVPlayer::GetAudioEffectMode()
379 {
380     return audioEffectMode_;
381 }
382 
SetAudioEffectMode(int32_t effectMode)383 void CJAVPlayer::SetAudioEffectMode(int32_t effectMode)
384 {
385     MEDIA_LOGI("SetAudioEffectMode In");
386 
387     if (!IsControllable()) {
388         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
389                   "current state is not prepared/playing/paused/completed, unsupport audio effect mode operation");
390         return;
391     }
392 
393     if (effectMode > OHOS::AudioStandard::AudioEffectMode::EFFECT_DEFAULT ||
394         effectMode < OHOS::AudioStandard::AudioEffectMode::EFFECT_NONE) {
395         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER,
396                   "invalid audioEffectMode, please check the input audio effect Mode");
397         return;
398     }
399 
400     if (audioEffectMode_ == effectMode) {
401         MEDIA_LOGI("Same effectMode parameter");
402         return;
403     }
404 
405     audioEffectMode_ = effectMode;
406 
407     if (player_ != nullptr) {
408         Format format;
409         (void)format.PutIntValue(PlayerKeys::AUDIO_EFFECT_MODE, effectMode);
410         (void)player_->SetParameter(format);
411     }
412     MEDIA_LOGI("SetAudioEffectMode Out");
413 }
414 
GetState()415 char *CJAVPlayer::GetState()
416 {
417     std::string curState = GetCurrentState();
418     return MallocCString(curState.c_str());
419 }
420 
GetCurrentTime()421 int32_t CJAVPlayer::GetCurrentTime()
422 {
423     MEDIA_LOGD("GetCurrentTime In");
424 
425     int32_t currentTime = -1;
426     if (IsControllable()) {
427         std::lock_guard<std::recursive_mutex> lock(taskMutex_);
428         currentTime = position_;
429     }
430 
431     if (IsLiveSource() && dataSrcCb_ == nullptr) {
432         currentTime = -1;
433     }
434     std::string curState = GetCurrentState();
435     if (currentTime != -1) {
436         MEDIA_LOGI("GetCurrenTime Out, state %{public}s, time: %{public}d", curState.c_str(), currentTime);
437     }
438     return currentTime;
439 }
440 
GetDuration()441 int32_t CJAVPlayer::GetDuration()
442 {
443     MEDIA_LOGD("GetDuration In");
444 
445     int32_t duration = -1;
446     if (IsControllable() && !IsLiveSource()) {
447         std::lock_guard<std::recursive_mutex> lock(taskMutex_);
448         duration = duration_;
449     }
450 
451     std::string curState = GetCurrentState();
452     MEDIA_LOGD("GetDuration Out, state %{public}s, duration %{public}d", curState.c_str(), duration);
453     return duration;
454 }
455 
GetWidth()456 int32_t CJAVPlayer::GetWidth()
457 {
458     int32_t width = 0;
459     if (IsControllable()) {
460         std::lock_guard<std::recursive_mutex> lock(taskMutex_);
461         width = width_;
462     }
463     return width;
464 }
465 
GetHeight()466 int32_t CJAVPlayer::GetHeight()
467 {
468     int32_t height = 0;
469     if (IsControllable()) {
470         std::lock_guard<std::recursive_mutex> lock(taskMutex_);
471         height = height_;
472     }
473     return height;
474 }
475 
PrepareTask()476 int32_t CJAVPlayer::PrepareTask()
477 {
478     MEDIA_LOGI("0x%{public}06" PRIXPTR " Prepare Task In", FAKE_POINTER(this));
479     std::unique_lock<std::recursive_mutex> lock(taskMutex_);
480     auto state = GetCurrentState();
481     if (state == AVPlayerState::STATE_INITIALIZED || state == AVPlayerState::STATE_STOPPED) {
482         int32_t ret = player_->PrepareAsync();
483         if (ret != MSERR_OK) {
484             auto errCode = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(ret));
485             return errCode;
486         }
487 
488         if (GetCurrentState() == AVPlayerState::STATE_ERROR) {
489             return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
490         }
491     } else if (state == AVPlayerState::STATE_PREPARED) {
492         MEDIA_LOGI("current state is prepared, invalid operation");
493     } else {
494         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
495     }
496 
497     MEDIA_LOGI("0x%{public}06" PRIXPTR " Prepare Task Out", FAKE_POINTER(this));
498     return MSERR_EXT_API9_OK;
499 }
500 
Prepare()501 int32_t CJAVPlayer::Prepare()
502 {
503     MEDIA_LOGI("Prepare In");
504     auto state = GetCurrentState();
505     if (state != AVPlayerState::STATE_INITIALIZED && state != AVPlayerState::STATE_STOPPED &&
506         state != AVPlayerState::STATE_PREPARED) {
507         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
508     } else {
509         return PrepareTask();
510     }
511 }
512 
PlayTask()513 int32_t CJAVPlayer::PlayTask()
514 {
515     MEDIA_LOGI("0x%{public}06" PRIXPTR " Play Task In", FAKE_POINTER(this));
516     std::unique_lock<std::recursive_mutex> lock(taskMutex_);
517     auto state = GetCurrentState();
518     if (state == AVPlayerState::STATE_PREPARED || state == AVPlayerState::STATE_PAUSED ||
519         state == AVPlayerState::STATE_COMPLETED) {
520         int32_t ret = player_->Play();
521         if (ret != MSERR_OK) {
522             return MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(ret));
523         }
524 
525         if (GetCurrentState() == AVPlayerState::STATE_ERROR) {
526             return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
527         }
528     } else if (state == AVPlayerState::STATE_PLAYING) {
529         MEDIA_LOGI("current state is playing, invalid operation");
530     } else {
531         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
532     }
533 
534     MEDIA_LOGI("0x%{public}06" PRIXPTR " Play Task Out", FAKE_POINTER(this));
535     return MSERR_EXT_API9_OK;
536 }
537 
Play()538 int32_t CJAVPlayer::Play()
539 {
540     MEDIA_LOGI("Play In");
541     auto state = GetCurrentState();
542     if (state != AVPlayerState::STATE_PREPARED && state != AVPlayerState::STATE_PAUSED &&
543         state != AVPlayerState::STATE_COMPLETED && state != AVPlayerState::STATE_PLAYING) {
544         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
545     } else if (state == AVPlayerState::STATE_COMPLETED && IsLiveSource()) {
546         return MSERR_EXT_API9_UNSUPPORT_CAPABILITY;
547     } else {
548         return PlayTask();
549     }
550 }
551 
PauseTask()552 int32_t CJAVPlayer::PauseTask()
553 {
554     MEDIA_LOGI("0x%{public}06" PRIXPTR " Pause Task In", FAKE_POINTER(this));
555     std::unique_lock<std::recursive_mutex> lock(taskMutex_);
556     auto state = GetCurrentState();
557     if (state == AVPlayerState::STATE_PLAYING) {
558         int32_t ret = player_->Pause();
559         if (ret != MSERR_OK) {
560             auto errCode = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(ret));
561             return errCode;
562         }
563     } else if (state == AVPlayerState::STATE_PAUSED) {
564         MEDIA_LOGI("current state is paused, invalid operation");
565     } else {
566         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
567     }
568 
569     MEDIA_LOGI("0x%{public}06" PRIXPTR " Pause Task Out", FAKE_POINTER(this));
570     return MSERR_EXT_API9_OK;
571 }
572 
Pause()573 int32_t CJAVPlayer::Pause()
574 {
575     MEDIA_LOGI("Pause In");
576     auto state = GetCurrentState();
577     if (state != AVPlayerState::STATE_PLAYING && state != AVPlayerState::STATE_PAUSED) {
578         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
579     } else {
580         return PauseTask();
581     }
582 }
583 
StopTask()584 int32_t CJAVPlayer::StopTask()
585 {
586     MEDIA_LOGI("0x%{public}06" PRIXPTR " Stop Task In", FAKE_POINTER(this));
587     std::unique_lock<std::recursive_mutex> lock(taskMutex_);
588     if (IsControllable()) {
589         int32_t ret = player_->Stop();
590         if (ret != MSERR_OK) {
591             auto errCode = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(ret));
592             return errCode;
593         }
594     } else if (GetCurrentState() == AVPlayerState::STATE_STOPPED) {
595         MEDIA_LOGI("current state is stopped, invalid operation");
596     } else {
597         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
598     }
599 
600     MEDIA_LOGI("0x%{public}06" PRIXPTR " Stop Task Out", FAKE_POINTER(this));
601     return MSERR_EXT_API9_OK;
602 }
603 
Stop()604 int32_t CJAVPlayer::Stop()
605 {
606     MEDIA_LOGI("Stop In");
607     auto state = GetCurrentState();
608     if (state == AVPlayerState::STATE_IDLE || state == AVPlayerState::STATE_INITIALIZED ||
609         state == AVPlayerState::STATE_RELEASED || state == AVPlayerState::STATE_ERROR) {
610         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
611     } else {
612         return StopTask();
613     }
614 }
615 
ResetTask()616 int32_t CJAVPlayer::ResetTask()
617 {
618     MEDIA_LOGI("0x%{public}06" PRIXPTR " Reset Task In", FAKE_POINTER(this));
619     PauseListenCurrentResource(); // Pause event listening for the current resource
620     ResetUserParameters();
621     {
622         isInterrupted_.store(false);
623         std::unique_lock<std::recursive_mutex> lock(taskMutex_);
624         if (GetCurrentState() == AVPlayerState::STATE_RELEASED) {
625             return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
626         } else if (GetCurrentState() == AVPlayerState::STATE_IDLE) {
627             MEDIA_LOGI("current state is idle, invalid operation");
628         } else {
629             int32_t ret = player_->Reset();
630             if (ret != MSERR_OK) {
631                 auto errCode = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(ret));
632                 return errCode;
633             }
634         }
635     }
636     MEDIA_LOGI("0x%{public}06" PRIXPTR " Reset Task Out", FAKE_POINTER(this));
637     return MSERR_EXT_API9_OK;
638 }
639 
Reset()640 int32_t CJAVPlayer::Reset()
641 {
642     MEDIA_LOGI("Reset In");
643     if (GetCurrentState() == AVPlayerState::STATE_RELEASED) {
644         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
645     } else {
646         int32_t res = ResetTask();
647         if (dataSrcCb_ != nullptr) {
648             dataSrcCb_->ClearCallbackReference();
649             dataSrcCb_ = nullptr;
650         }
651         isLiveStream_ = false;
652         return res;
653     }
654 }
655 
ReleaseTask()656 int32_t CJAVPlayer::ReleaseTask()
657 {
658     if (!isReleased_.load()) {
659         MEDIA_LOGI("0x%{public}06" PRIXPTR " Release Task In", FAKE_POINTER(this));
660         PauseListenCurrentResource(); // Pause event listening for the current resource
661         ResetUserParameters();
662         {
663             std::lock_guard<std::mutex> lock(syncMutex_);
664             if (player_ != nullptr) {
665                 (void)player_->ReleaseSync();
666                 player_ = nullptr;
667             }
668         }
669 
670         if (playerCb_ != nullptr) {
671             playerCb_->Release();
672         }
673         MEDIA_LOGI("0x%{public}06" PRIXPTR " Release Task Out", FAKE_POINTER(this));
674         isReleased_.store(true);
675     }
676     return MSERR_EXT_API9_OK;
677 }
678 
Release()679 int32_t CJAVPlayer::Release()
680 {
681     MEDIA_LOGI("Release In");
682     int32_t res = ReleaseTask();
683     if (dataSrcCb_ != nullptr) {
684         dataSrcCb_->ClearCallbackReference();
685         dataSrcCb_ = nullptr;
686     }
687     return res;
688 }
689 
SeekTask(int32_t time,int32_t mode)690 void CJAVPlayer::SeekTask(int32_t time, int32_t mode)
691 {
692     if (player_ != nullptr) {
693         (void)player_->Seek(time, TransferSeekMode(mode));
694     }
695 }
696 
Seek(int32_t time,int32_t mode)697 void CJAVPlayer::Seek(int32_t time, int32_t mode)
698 {
699     if (!IsControllable()) {
700         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
701                   "current state is not prepared/playing/paused/completed, unsupport seek operation");
702     }
703     SeekTask(time, mode);
704 }
705 
OnStateChange(int64_t callbackId)706 int32_t CJAVPlayer::OnStateChange(int64_t callbackId)
707 {
708     if (playerCb_ == nullptr) {
709         return MSERR_EXT_API9_NO_MEMORY;
710     }
711     auto cFunc = reinterpret_cast<void (*)(char *stateStr, int32_t reason)>(callbackId);
712     playerCb_->stateChangeCallback = [lambda = CJLambda::Create(cFunc)](std::string &stateStr, int32_t reason) -> void {
713         auto cstr = MallocCString(stateStr);
714         lambda(cstr, reason);
715         free(cstr);
716     };
717     playerCb_->stateChangeCallbackId = callbackId;
718     return MSERR_EXT_API9_OK;
719 }
720 
OffStateChange(int64_t callbackId)721 int32_t CJAVPlayer::OffStateChange(int64_t callbackId)
722 {
723     if (playerCb_ == nullptr) {
724         return MSERR_EXT_API9_NO_MEMORY;
725     }
726     if (playerCb_->stateChangeCallbackId == callbackId) {
727         playerCb_->stateChangeCallbackId = INVALID_ID;
728         playerCb_->stateChangeCallback = nullptr;
729     }
730     return MSERR_EXT_API9_OK;
731 }
732 
OffStateChangeAll()733 int32_t CJAVPlayer::OffStateChangeAll()
734 {
735     if (playerCb_ == nullptr) {
736         return MSERR_EXT_API9_NO_MEMORY;
737     }
738     playerCb_->stateChangeCallbackId = INVALID_ID;
739     playerCb_->stateChangeCallback = nullptr;
740     return MSERR_EXT_API9_OK;
741 }
742 
OnError(int64_t callbackId)743 int32_t CJAVPlayer::OnError(int64_t callbackId)
744 {
745     if (playerCb_ == nullptr) {
746         return MSERR_EXT_API9_NO_MEMORY;
747     }
748     auto cFunc = reinterpret_cast<void (*)(int32_t errorCode, const char *errorMsg)>(callbackId);
749     playerCb_->errorCallback = [lambda = CJLambda::Create(cFunc)](int32_t errorCode,
750                                                                   const std::string &errorMsg) -> void {
751         auto cstr = MallocCString(errorMsg);
752         lambda(errorCode, cstr);
753         free(cstr);
754     };
755     playerCb_->errorCallbackId = callbackId;
756     return MSERR_EXT_API9_OK;
757 }
758 
OffError(int64_t callbackId)759 int32_t CJAVPlayer::OffError(int64_t callbackId)
760 {
761     if (playerCb_ == nullptr) {
762         return MSERR_EXT_API9_NO_MEMORY;
763     }
764     if (playerCb_->errorCallbackId == callbackId) {
765         playerCb_->errorCallbackId = INVALID_ID;
766         playerCb_->errorCallback = nullptr;
767     }
768     return MSERR_EXT_API9_OK;
769 }
770 
OffErrorAll()771 int32_t CJAVPlayer::OffErrorAll()
772 {
773     if (playerCb_ == nullptr) {
774         return MSERR_EXT_API9_NO_MEMORY;
775     }
776     playerCb_->errorCallbackId = INVALID_ID;
777     playerCb_->errorCallback = nullptr;
778     return MSERR_EXT_API9_OK;
779 }
780 
OnSeekDone(int64_t callbackId)781 int32_t CJAVPlayer::OnSeekDone(int64_t callbackId)
782 {
783     if (playerCb_ == nullptr) {
784         return MSERR_EXT_API9_NO_MEMORY;
785     }
786     auto cFunc = reinterpret_cast<void (*)(int32_t currentPositon)>(callbackId);
787     playerCb_->seekDoneCallback = [lambda = CJLambda::Create(cFunc)](int32_t currentPositon) -> void {
788         lambda(currentPositon);
789     };
790     playerCb_->seekDoneCallbackId = callbackId;
791     return MSERR_EXT_API9_OK;
792 }
793 
OffSeekDone(int64_t callbackId)794 int32_t CJAVPlayer::OffSeekDone(int64_t callbackId)
795 {
796     if (playerCb_ == nullptr) {
797         return MSERR_EXT_API9_NO_MEMORY;
798     }
799     if (playerCb_->seekDoneCallbackId == callbackId) {
800         playerCb_->seekDoneCallbackId = INVALID_ID;
801         playerCb_->seekDoneCallback = nullptr;
802     }
803     return MSERR_EXT_API9_OK;
804 }
805 
OffSeekDoneAll()806 int32_t CJAVPlayer::OffSeekDoneAll()
807 {
808     if (playerCb_ == nullptr) {
809         return MSERR_EXT_API9_NO_MEMORY;
810     }
811     playerCb_->seekDoneCallbackId = INVALID_ID;
812     playerCb_->seekDoneCallback = nullptr;
813     return MSERR_EXT_API9_OK;
814 }
815 
OnSpeedDone(int64_t callbackId)816 int32_t CJAVPlayer::OnSpeedDone(int64_t callbackId)
817 {
818     if (playerCb_ == nullptr) {
819         return MSERR_EXT_API9_NO_MEMORY;
820     }
821     auto cFunc = reinterpret_cast<void (*)(int32_t speedMode)>(callbackId);
822     playerCb_->speedDoneCallback = [lambda = CJLambda::Create(cFunc)](int32_t speedMode) -> void { lambda(speedMode); };
823     playerCb_->speedDoneCallbackId = callbackId;
824     return MSERR_EXT_API9_OK;
825 }
826 
OffSpeedDone(int64_t callbackId)827 int32_t CJAVPlayer::OffSpeedDone(int64_t callbackId)
828 {
829     if (playerCb_ == nullptr) {
830         return MSERR_EXT_API9_NO_MEMORY;
831     }
832     if (playerCb_->speedDoneCallbackId == callbackId) {
833         playerCb_->speedDoneCallbackId = INVALID_ID;
834         playerCb_->speedDoneCallback = nullptr;
835     }
836     return MSERR_EXT_API9_OK;
837 }
838 
OffSpeedDoneAll()839 int32_t CJAVPlayer::OffSpeedDoneAll()
840 {
841     if (playerCb_ == nullptr) {
842         return MSERR_EXT_API9_NO_MEMORY;
843     }
844     playerCb_->speedDoneCallbackId = INVALID_ID;
845     playerCb_->speedDoneCallback = nullptr;
846     return MSERR_EXT_API9_OK;
847 }
848 
OnBitRateDone(int64_t callbackId)849 int32_t CJAVPlayer::OnBitRateDone(int64_t callbackId)
850 {
851     if (playerCb_ == nullptr) {
852         return MSERR_EXT_API9_NO_MEMORY;
853     }
854     auto cFunc = reinterpret_cast<void (*)(int32_t bitRate)>(callbackId);
855     playerCb_->bitRateDoneCallback = [lambda = CJLambda::Create(cFunc)](int32_t bitRate) -> void { lambda(bitRate); };
856     playerCb_->bitRateDoneCallbackId = callbackId;
857     return MSERR_EXT_API9_OK;
858 }
859 
OffBitRateDone(int64_t callbackId)860 int32_t CJAVPlayer::OffBitRateDone(int64_t callbackId)
861 {
862     if (playerCb_ == nullptr) {
863         return MSERR_EXT_API9_NO_MEMORY;
864     }
865     if (playerCb_->bitRateDoneCallbackId == callbackId) {
866         playerCb_->bitRateDoneCallbackId = INVALID_ID;
867         playerCb_->bitRateDoneCallback = nullptr;
868     }
869     return MSERR_EXT_API9_OK;
870 }
871 
OffBitRateDoneAll()872 int32_t CJAVPlayer::OffBitRateDoneAll()
873 {
874     if (playerCb_ == nullptr) {
875         return MSERR_EXT_API9_NO_MEMORY;
876     }
877     playerCb_->bitRateDoneCallbackId = INVALID_ID;
878     playerCb_->bitRateDoneCallback = nullptr;
879     return MSERR_EXT_API9_OK;
880 }
881 
OnAvailableBitrates(int64_t callbackId)882 int32_t CJAVPlayer::OnAvailableBitrates(int64_t callbackId)
883 {
884     if (playerCb_ == nullptr) {
885         return MSERR_EXT_API9_NO_MEMORY;
886     }
887     auto cFunc = reinterpret_cast<void (*)(CArrI32 bitrateVec)>(callbackId);
888     playerCb_->availableBitratesCallback = [lambda = CJLambda::Create(cFunc)](std::vector<int32_t> bitrateVec) -> void {
889         auto carr = Convert2CArrI32(bitrateVec);
890         lambda(carr);
891         free(carr.head);
892     };
893     playerCb_->availableBitratesCallbackId = callbackId;
894     return MSERR_EXT_API9_OK;
895 }
896 
OffAvailableBitrates(int64_t callbackId)897 int32_t CJAVPlayer::OffAvailableBitrates(int64_t callbackId)
898 {
899     if (playerCb_ == nullptr) {
900         return MSERR_EXT_API9_NO_MEMORY;
901     }
902     if (playerCb_->availableBitratesCallbackId == callbackId) {
903         playerCb_->availableBitratesCallbackId = INVALID_ID;
904         playerCb_->availableBitratesCallback = nullptr;
905     }
906     return MSERR_EXT_API9_OK;
907 }
908 
OffAvailableBitratesAll()909 int32_t CJAVPlayer::OffAvailableBitratesAll()
910 {
911     if (playerCb_ == nullptr) {
912         return MSERR_EXT_API9_NO_MEMORY;
913     }
914     playerCb_->availableBitratesCallbackId = INVALID_ID;
915     playerCb_->availableBitratesCallback = nullptr;
916     return MSERR_EXT_API9_OK;
917 }
918 
OnMediaKeySystemInfoUpdate(int64_t callbackId)919 int32_t CJAVPlayer::OnMediaKeySystemInfoUpdate(int64_t callbackId)
920 {
921     if (playerCb_ == nullptr) {
922         return MSERR_EXT_API9_NO_MEMORY;
923     }
924     auto cFunc = reinterpret_cast<void (*)(CArrCMediaKeySystemInfo drmInfoMap)>(callbackId);
925     playerCb_->mediaKeySystemInfoUpdateCallback =
926         [lambda = CJLambda::Create(cFunc)](CArrCMediaKeySystemInfo drmInfoMap) -> void {
927         lambda(drmInfoMap);
928     };
929     playerCb_->mediaKeySystemInfoUpdateCallbackId = callbackId;
930     return MSERR_EXT_API9_OK;
931 }
932 
OffMediaKeySystemInfoUpdate(int64_t callbackId)933 int32_t CJAVPlayer::OffMediaKeySystemInfoUpdate(int64_t callbackId)
934 {
935     if (playerCb_ == nullptr) {
936         return MSERR_EXT_API9_NO_MEMORY;
937     }
938     if (playerCb_->mediaKeySystemInfoUpdateCallbackId == callbackId) {
939         playerCb_->mediaKeySystemInfoUpdateCallbackId = INVALID_ID;
940         playerCb_->mediaKeySystemInfoUpdateCallback = nullptr;
941     }
942     return MSERR_EXT_API9_OK;
943 }
944 
OffMediaKeySystemInfoUpdateAll()945 int32_t CJAVPlayer::OffMediaKeySystemInfoUpdateAll()
946 {
947     if (playerCb_ == nullptr) {
948         return MSERR_EXT_API9_NO_MEMORY;
949     }
950     playerCb_->mediaKeySystemInfoUpdateCallbackId = INVALID_ID;
951     playerCb_->mediaKeySystemInfoUpdateCallback = nullptr;
952     return MSERR_EXT_API9_OK;
953 }
954 
OnVolumeChange(int64_t callbackId)955 int32_t CJAVPlayer::OnVolumeChange(int64_t callbackId)
956 {
957     if (playerCb_ == nullptr) {
958         return MSERR_EXT_API9_NO_MEMORY;
959     }
960     auto cFunc = reinterpret_cast<void (*)(float volumeLevel)>(callbackId);
961     playerCb_->volumeChangeCallback = [lambda = CJLambda::Create(cFunc)](float volumeLevel) -> void {
962         lambda(volumeLevel);
963     };
964     playerCb_->volumeChangeCallbackId = callbackId;
965     return MSERR_EXT_API9_OK;
966 }
967 
OffVolumeChange(int64_t callbackId)968 int32_t CJAVPlayer::OffVolumeChange(int64_t callbackId)
969 {
970     if (playerCb_ == nullptr) {
971         return MSERR_EXT_API9_NO_MEMORY;
972     }
973     if (playerCb_->volumeChangeCallbackId == callbackId) {
974         playerCb_->volumeChangeCallbackId = INVALID_ID;
975         playerCb_->volumeChangeCallback = nullptr;
976     }
977     return MSERR_EXT_API9_OK;
978 }
979 
OffVolumeChangeAll()980 int32_t CJAVPlayer::OffVolumeChangeAll()
981 {
982     if (playerCb_ == nullptr) {
983         return MSERR_EXT_API9_NO_MEMORY;
984     }
985     playerCb_->volumeChangeCallbackId = INVALID_ID;
986     playerCb_->volumeChangeCallback = nullptr;
987     return MSERR_EXT_API9_OK;
988 }
989 
OnEndOfStream(int64_t callbackId)990 int32_t CJAVPlayer::OnEndOfStream(int64_t callbackId)
991 {
992     if (playerCb_ == nullptr) {
993         return MSERR_EXT_API9_NO_MEMORY;
994     }
995     auto cFunc = reinterpret_cast<void (*)()>(callbackId);
996     playerCb_->endOfStreamCallback = [lambda = CJLambda::Create(cFunc)]() -> void { lambda(); };
997     playerCb_->endOfStreamCallbackId = callbackId;
998     return MSERR_EXT_API9_OK;
999 }
1000 
OffEndOfStream(int64_t callbackId)1001 int32_t CJAVPlayer::OffEndOfStream(int64_t callbackId)
1002 {
1003     if (playerCb_ == nullptr) {
1004         return MSERR_EXT_API9_NO_MEMORY;
1005     }
1006     if (playerCb_->endOfStreamCallbackId == callbackId) {
1007         playerCb_->endOfStreamCallbackId = INVALID_ID;
1008         playerCb_->endOfStreamCallback = nullptr;
1009     }
1010     return MSERR_EXT_API9_OK;
1011 }
1012 
OffEndOfStreamAll()1013 int32_t CJAVPlayer::OffEndOfStreamAll()
1014 {
1015     if (playerCb_ == nullptr) {
1016         return MSERR_EXT_API9_NO_MEMORY;
1017     }
1018     playerCb_->endOfStreamCallbackId = INVALID_ID;
1019     playerCb_->endOfStreamCallback = nullptr;
1020     return MSERR_EXT_API9_OK;
1021 }
1022 
OnTimeUpdate(int64_t callbackId)1023 int32_t CJAVPlayer::OnTimeUpdate(int64_t callbackId)
1024 {
1025     if (playerCb_ == nullptr) {
1026         return MSERR_EXT_API9_NO_MEMORY;
1027     }
1028     auto cFunc = reinterpret_cast<void (*)(int32_t position)>(callbackId);
1029     playerCb_->timeUpdateCallback = [lambda = CJLambda::Create(cFunc)](int32_t position) -> void { lambda(position); };
1030     playerCb_->timeUpdateCallbackId = callbackId;
1031     return MSERR_EXT_API9_OK;
1032 }
1033 
OffTimeUpdate(int64_t callbackId)1034 int32_t CJAVPlayer::OffTimeUpdate(int64_t callbackId)
1035 {
1036     if (playerCb_ == nullptr) {
1037         return MSERR_EXT_API9_NO_MEMORY;
1038     }
1039     if (playerCb_->timeUpdateCallbackId == callbackId) {
1040         playerCb_->timeUpdateCallbackId = INVALID_ID;
1041         playerCb_->timeUpdateCallback = nullptr;
1042     }
1043     return MSERR_EXT_API9_OK;
1044 }
1045 
OffTimeUpdateAll()1046 int32_t CJAVPlayer::OffTimeUpdateAll()
1047 {
1048     if (playerCb_ == nullptr) {
1049         return MSERR_EXT_API9_NO_MEMORY;
1050     }
1051     playerCb_->timeUpdateCallbackId = INVALID_ID;
1052     playerCb_->timeUpdateCallback = nullptr;
1053     return MSERR_EXT_API9_OK;
1054 }
1055 
OnDurationUpdate(int64_t callbackId)1056 int32_t CJAVPlayer::OnDurationUpdate(int64_t callbackId)
1057 {
1058     if (playerCb_ == nullptr) {
1059         return MSERR_EXT_API9_NO_MEMORY;
1060     }
1061     auto cFunc = reinterpret_cast<void (*)(int32_t duration)>(callbackId);
1062     playerCb_->durationUpdateCallback = [lambda = CJLambda::Create(cFunc)](int32_t duration) -> void {
1063         lambda(duration);
1064     };
1065     playerCb_->durationUpdateCallbackId = callbackId;
1066     return MSERR_EXT_API9_OK;
1067 }
1068 
OffDurationUpdate(int64_t callbackId)1069 int32_t CJAVPlayer::OffDurationUpdate(int64_t callbackId)
1070 {
1071     if (playerCb_ == nullptr) {
1072         return MSERR_EXT_API9_NO_MEMORY;
1073     }
1074     if (playerCb_->durationUpdateCallbackId == callbackId) {
1075         playerCb_->durationUpdateCallbackId = INVALID_ID;
1076         playerCb_->durationUpdateCallback = nullptr;
1077     }
1078     return MSERR_EXT_API9_OK;
1079 }
1080 
OffDurationUpdateAll()1081 int32_t CJAVPlayer::OffDurationUpdateAll()
1082 {
1083     if (playerCb_ == nullptr) {
1084         return MSERR_EXT_API9_NO_MEMORY;
1085     }
1086     playerCb_->durationUpdateCallbackId = INVALID_ID;
1087     playerCb_->durationUpdateCallback = nullptr;
1088     return MSERR_EXT_API9_OK;
1089 }
1090 
OnBufferingUpdate(int64_t callbackId)1091 int32_t CJAVPlayer::OnBufferingUpdate(int64_t callbackId)
1092 {
1093     if (playerCb_ == nullptr) {
1094         return MSERR_EXT_API9_NO_MEMORY;
1095     }
1096     auto cFunc = reinterpret_cast<void (*)(int32_t bufferingType, int32_t val)>(callbackId);
1097     playerCb_->bufferingUpdateCallback =
1098         [lambda = CJLambda::Create(cFunc)](int32_t bufferingType, int32_t val) -> void { lambda(bufferingType, val); };
1099     playerCb_->bufferingUpdateCallbackId = callbackId;
1100     return MSERR_EXT_API9_OK;
1101 }
1102 
OffBufferingUpdate(int64_t callbackId)1103 int32_t CJAVPlayer::OffBufferingUpdate(int64_t callbackId)
1104 {
1105     if (playerCb_ == nullptr) {
1106         return MSERR_EXT_API9_NO_MEMORY;
1107     }
1108     if (playerCb_->bufferingUpdateCallbackId == callbackId) {
1109         playerCb_->bufferingUpdateCallbackId = INVALID_ID;
1110         playerCb_->bufferingUpdateCallback = nullptr;
1111     }
1112     return MSERR_EXT_API9_OK;
1113 }
1114 
OffBufferingUpdateAll()1115 int32_t CJAVPlayer::OffBufferingUpdateAll()
1116 {
1117     if (playerCb_ == nullptr) {
1118         return MSERR_EXT_API9_NO_MEMORY;
1119     }
1120     playerCb_->bufferingUpdateCallbackId = INVALID_ID;
1121     playerCb_->bufferingUpdateCallback = nullptr;
1122     return MSERR_EXT_API9_OK;
1123 }
1124 
OnStartRenderFrame(int64_t callbackId)1125 int32_t CJAVPlayer::OnStartRenderFrame(int64_t callbackId)
1126 {
1127     if (playerCb_ == nullptr) {
1128         return MSERR_EXT_API9_NO_MEMORY;
1129     }
1130     auto cFunc = reinterpret_cast<void (*)()>(callbackId);
1131     playerCb_->startRenderFrameCallback = [lambda = CJLambda::Create(cFunc)]() -> void { lambda(); };
1132     playerCb_->startRenderFrameCallbackId = callbackId;
1133     return MSERR_EXT_API9_OK;
1134 }
1135 
OffStartRenderFrame(int64_t callbackId)1136 int32_t CJAVPlayer::OffStartRenderFrame(int64_t callbackId)
1137 {
1138     if (playerCb_ == nullptr) {
1139         return MSERR_EXT_API9_NO_MEMORY;
1140     }
1141     if (playerCb_->startRenderFrameCallbackId == callbackId) {
1142         playerCb_->startRenderFrameCallbackId = INVALID_ID;
1143         playerCb_->startRenderFrameCallback = nullptr;
1144     }
1145     return MSERR_EXT_API9_OK;
1146 }
1147 
OffStartRenderFrameAll()1148 int32_t CJAVPlayer::OffStartRenderFrameAll()
1149 {
1150     if (playerCb_ == nullptr) {
1151         return MSERR_EXT_API9_NO_MEMORY;
1152     }
1153     playerCb_->startRenderFrameCallbackId = INVALID_ID;
1154     playerCb_->startRenderFrameCallback = nullptr;
1155     return MSERR_EXT_API9_OK;
1156 }
1157 
OnVideoSizeChange(int64_t callbackId)1158 int32_t CJAVPlayer::OnVideoSizeChange(int64_t callbackId)
1159 {
1160     if (playerCb_ == nullptr) {
1161         return MSERR_EXT_API9_NO_MEMORY;
1162     }
1163     auto cFunc = reinterpret_cast<void (*)(int32_t width, int32_t height)>(callbackId);
1164     playerCb_->videoSizeChangeCallback = [lambda = CJLambda::Create(cFunc)](int32_t width, int32_t height) -> void {
1165         lambda(width, height);
1166     };
1167     playerCb_->videoSizeChangeCallbackId = callbackId;
1168     return MSERR_EXT_API9_OK;
1169 }
1170 
OffVideoSizeChange(int64_t callbackId)1171 int32_t CJAVPlayer::OffVideoSizeChange(int64_t callbackId)
1172 {
1173     if (playerCb_ == nullptr) {
1174         return MSERR_EXT_API9_NO_MEMORY;
1175     }
1176     if (playerCb_->videoSizeChangeCallbackId == callbackId) {
1177         playerCb_->videoSizeChangeCallbackId = INVALID_ID;
1178         playerCb_->videoSizeChangeCallback = nullptr;
1179     }
1180     return MSERR_EXT_API9_OK;
1181 }
1182 
OffVideoSizeChangeAll()1183 int32_t CJAVPlayer::OffVideoSizeChangeAll()
1184 {
1185     if (playerCb_ == nullptr) {
1186         return MSERR_EXT_API9_NO_MEMORY;
1187     }
1188     playerCb_->videoSizeChangeCallbackId = INVALID_ID;
1189     playerCb_->videoSizeChangeCallback = nullptr;
1190     return MSERR_EXT_API9_OK;
1191 }
1192 
OnAudioInterrupt(int64_t callbackId)1193 int32_t CJAVPlayer::OnAudioInterrupt(int64_t callbackId)
1194 {
1195     if (playerCb_ == nullptr) {
1196         return MSERR_EXT_API9_NO_MEMORY;
1197     }
1198     auto cFunc = reinterpret_cast<void (*)(int32_t eventType, int32_t forceType, int32_t hintType)>(callbackId);
1199     playerCb_->audioInterruptCallback = [lambda = CJLambda::Create(cFunc)](int32_t eventType, int32_t forceType,
1200                                                                            int32_t hintType) -> void {
1201         lambda(eventType, forceType, hintType);
1202     };
1203     playerCb_->audioInterruptCallbackId = callbackId;
1204     return MSERR_EXT_API9_OK;
1205 }
1206 
OffAudioInterrupt(int64_t callbackId)1207 int32_t CJAVPlayer::OffAudioInterrupt(int64_t callbackId)
1208 {
1209     if (playerCb_ == nullptr) {
1210         return MSERR_EXT_API9_NO_MEMORY;
1211     }
1212     if (playerCb_->audioInterruptCallbackId == callbackId) {
1213         playerCb_->audioInterruptCallbackId = INVALID_ID;
1214         playerCb_->audioInterruptCallback = nullptr;
1215     }
1216     return MSERR_EXT_API9_OK;
1217 }
1218 
OffAudioInterruptAll()1219 int32_t CJAVPlayer::OffAudioInterruptAll()
1220 {
1221     if (playerCb_ == nullptr) {
1222         return MSERR_EXT_API9_NO_MEMORY;
1223     }
1224     playerCb_->audioInterruptCallbackId = INVALID_ID;
1225     playerCb_->audioInterruptCallback = nullptr;
1226     return MSERR_EXT_API9_OK;
1227 }
1228 
OnAudioDeviceChange(int64_t callbackId)1229 int32_t CJAVPlayer::OnAudioDeviceChange(int64_t callbackId)
1230 {
1231     if (playerCb_ == nullptr) {
1232         return MSERR_EXT_API9_NO_MEMORY;
1233     }
1234     auto cFunc = reinterpret_cast<void (*)(AudioStandard::CAudioStreamDeviceChangeInfo info)>(callbackId);
1235     playerCb_->audioDeviceChangeCallback =
1236         [lambda = CJLambda::Create(cFunc)](AudioStandard::AudioDeviceDescriptor deviceInfo, int32_t reason) -> void {
1237         AudioStandard::CArrDeviceDescriptor arr;
1238         int32_t errCode = SUCCESS_CODE;
1239         AudioStandard::Convert2CArrDeviceDescriptorByDeviceInfo(arr, deviceInfo, &errCode);
1240         if (errCode != SUCCESS_CODE) {
1241             return;
1242         }
1243         lambda(AudioStandard::CAudioStreamDeviceChangeInfo{.changeReason = reason, .deviceDescriptors = arr});
1244         FreeCArrDeviceDescriptor(arr);
1245     };
1246     playerCb_->audioDeviceChangeCallbackId = callbackId;
1247     return MSERR_EXT_API9_OK;
1248 }
1249 
OffAudioDeviceChange(int64_t callbackId)1250 int32_t CJAVPlayer::OffAudioDeviceChange(int64_t callbackId)
1251 {
1252     if (playerCb_ == nullptr) {
1253         return MSERR_EXT_API9_NO_MEMORY;
1254     }
1255     if (playerCb_->audioDeviceChangeCallbackId == callbackId) {
1256         playerCb_->audioDeviceChangeCallbackId = INVALID_ID;
1257         playerCb_->audioDeviceChangeCallback = nullptr;
1258     }
1259     return MSERR_EXT_API9_OK;
1260 }
1261 
OffAudioDeviceChangeAll()1262 int32_t CJAVPlayer::OffAudioDeviceChangeAll()
1263 {
1264     if (playerCb_ == nullptr) {
1265         return MSERR_EXT_API9_NO_MEMORY;
1266     }
1267     playerCb_->audioDeviceChangeCallbackId = INVALID_ID;
1268     playerCb_->audioDeviceChangeCallback = nullptr;
1269     return MSERR_EXT_API9_OK;
1270 }
1271 
OnSubtitleUpdate(int64_t callbackId)1272 int32_t CJAVPlayer::OnSubtitleUpdate(int64_t callbackId)
1273 {
1274     if (playerCb_ == nullptr) {
1275         return MSERR_EXT_API9_NO_MEMORY;
1276     }
1277     auto cFunc = reinterpret_cast<void (*)(CSubtitleInfo info)>(callbackId);
1278     playerCb_->subtitleUpdateCallback = [lambda = CJLambda::Create(cFunc)](std::string text, int32_t pts,
1279                                                                            int32_t duration) -> void {
1280         auto info = Convert2CSubtitleInfo(text, pts, duration);
1281         lambda(info);
1282         free(info.text);
1283     };
1284     playerCb_->subtitleUpdateCallbackId = callbackId;
1285     return MSERR_EXT_API9_OK;
1286 }
1287 
OffSubtitleUpdate(int64_t callbackId)1288 int32_t CJAVPlayer::OffSubtitleUpdate(int64_t callbackId)
1289 {
1290     if (playerCb_ == nullptr) {
1291         return MSERR_EXT_API9_NO_MEMORY;
1292     }
1293     if (playerCb_->subtitleUpdateCallbackId == callbackId) {
1294         playerCb_->subtitleUpdateCallbackId = INVALID_ID;
1295         playerCb_->subtitleUpdateCallback = nullptr;
1296     }
1297     return MSERR_EXT_API9_OK;
1298 }
1299 
OffSubtitleUpdateAll()1300 int32_t CJAVPlayer::OffSubtitleUpdateAll()
1301 {
1302     if (playerCb_ == nullptr) {
1303         return MSERR_EXT_API9_NO_MEMORY;
1304     }
1305     playerCb_->subtitleUpdateCallbackId = INVALID_ID;
1306     playerCb_->subtitleUpdateCallback = nullptr;
1307     return MSERR_EXT_API9_OK;
1308 }
1309 
OnTrackChange(int64_t callbackId)1310 int32_t CJAVPlayer::OnTrackChange(int64_t callbackId)
1311 {
1312     if (playerCb_ == nullptr) {
1313         return MSERR_EXT_API9_NO_MEMORY;
1314     }
1315     auto cFunc = reinterpret_cast<void (*)(int32_t index, bool isSelect)>(callbackId);
1316     playerCb_->trackChangeCallback = [lambda = CJLambda::Create(cFunc)](int32_t index, int32_t isSelect) -> void {
1317         lambda(index, static_cast<bool>(isSelect));
1318     };
1319     playerCb_->trackChangeCallbackId = callbackId;
1320     return MSERR_EXT_API9_OK;
1321 }
1322 
OffTrackChange(int64_t callbackId)1323 int32_t CJAVPlayer::OffTrackChange(int64_t callbackId)
1324 {
1325     if (playerCb_ == nullptr) {
1326         return MSERR_EXT_API9_NO_MEMORY;
1327     }
1328     if (playerCb_->trackChangeCallbackId == callbackId) {
1329         playerCb_->trackChangeCallbackId = INVALID_ID;
1330         playerCb_->trackChangeCallback = nullptr;
1331     }
1332     return MSERR_EXT_API9_OK;
1333 }
1334 
OffTrackChangeAll()1335 int32_t CJAVPlayer::OffTrackChangeAll()
1336 {
1337     if (playerCb_ == nullptr) {
1338         return MSERR_EXT_API9_NO_MEMORY;
1339     }
1340     playerCb_->trackChangeCallbackId = INVALID_ID;
1341     playerCb_->trackChangeCallback = nullptr;
1342     return MSERR_EXT_API9_OK;
1343 }
1344 
OnTrackInfoUpdate(int64_t callbackId)1345 int32_t CJAVPlayer::OnTrackInfoUpdate(int64_t callbackId)
1346 {
1347     if (playerCb_ == nullptr) {
1348         return MSERR_EXT_API9_NO_MEMORY;
1349     }
1350     auto cFunc = reinterpret_cast<void (*)(CArrCMediaDescription trackInfo)>(callbackId);
1351     playerCb_->trackInfoUpdateCallback = [lambda = CJLambda::Create(cFunc)](CArrCMediaDescription trackInfo) -> void {
1352         lambda(trackInfo);
1353         free(trackInfo.head);
1354     };
1355     playerCb_->trackInfoUpdateCallbackId = callbackId;
1356     return MSERR_EXT_API9_OK;
1357 }
1358 
OffTrackInfoUpdate(int64_t callbackId)1359 int32_t CJAVPlayer::OffTrackInfoUpdate(int64_t callbackId)
1360 {
1361     if (playerCb_ == nullptr) {
1362         return MSERR_EXT_API9_NO_MEMORY;
1363     }
1364     if (playerCb_->trackInfoUpdateCallbackId == callbackId) {
1365         playerCb_->trackInfoUpdateCallbackId = INVALID_ID;
1366         playerCb_->trackInfoUpdateCallback = nullptr;
1367     }
1368     return MSERR_EXT_API9_OK;
1369 }
1370 
OffTrackInfoUpdateAll()1371 int32_t CJAVPlayer::OffTrackInfoUpdateAll()
1372 {
1373     if (playerCb_ == nullptr) {
1374         return MSERR_EXT_API9_NO_MEMORY;
1375     }
1376     playerCb_->trackInfoUpdateCallbackId = INVALID_ID;
1377     playerCb_->trackInfoUpdateCallback = nullptr;
1378     return MSERR_EXT_API9_OK;
1379 }
1380 
OnAmplitudeUpdate(int64_t callbackId)1381 int32_t CJAVPlayer::OnAmplitudeUpdate(int64_t callbackId)
1382 {
1383     if (playerCb_ == nullptr) {
1384         return MSERR_EXT_API9_NO_MEMORY;
1385     }
1386     auto cFunc = reinterpret_cast<void (*)(CArrFloat arr)>(callbackId);
1387     playerCb_->amplitudeUpdateCallback = [lambda =
1388                                               CJLambda::Create(cFunc)](std::vector<float> MaxAmplitudeVec) -> void {
1389         auto vec = Convert2CArrFloat(MaxAmplitudeVec);
1390         lambda(vec);
1391         free(vec.head);
1392     };
1393     playerCb_->amplitudeUpdateCallbackId = callbackId;
1394     return MSERR_EXT_API9_OK;
1395 }
1396 
OffAmplitudeUpdate(int64_t callbackId)1397 int32_t CJAVPlayer::OffAmplitudeUpdate(int64_t callbackId)
1398 {
1399     if (playerCb_ == nullptr) {
1400         return MSERR_EXT_API9_NO_MEMORY;
1401     }
1402     if (playerCb_->amplitudeUpdateCallbackId == callbackId) {
1403         playerCb_->amplitudeUpdateCallbackId = INVALID_ID;
1404         playerCb_->amplitudeUpdateCallback = nullptr;
1405     }
1406     return MSERR_EXT_API9_OK;
1407 }
1408 
OffAmplitudeUpdateAll()1409 int32_t CJAVPlayer::OffAmplitudeUpdateAll()
1410 {
1411     if (playerCb_ == nullptr) {
1412         return MSERR_EXT_API9_NO_MEMORY;
1413     }
1414     playerCb_->amplitudeUpdateCallbackId = INVALID_ID;
1415     playerCb_->amplitudeUpdateCallback = nullptr;
1416     return MSERR_EXT_API9_OK;
1417 }
1418 
GetCurrentState()1419 std::string CJAVPlayer::GetCurrentState()
1420 {
1421     if (isReleased_.load()) {
1422         return AVPlayerState::STATE_RELEASED;
1423     }
1424 
1425     std::string curState = AVPlayerState::STATE_ERROR;
1426     static const std::map<PlayerStates, std::string> stateMap = {
1427         {PLAYER_IDLE, AVPlayerState::STATE_IDLE},
1428         {PLAYER_INITIALIZED, AVPlayerState::STATE_INITIALIZED},
1429         {PLAYER_PREPARED, AVPlayerState::STATE_PREPARED},
1430         {PLAYER_STARTED, AVPlayerState::STATE_PLAYING},
1431         {PLAYER_PAUSED, AVPlayerState::STATE_PAUSED},
1432         {PLAYER_STOPPED, AVPlayerState::STATE_STOPPED},
1433         {PLAYER_PLAYBACK_COMPLETE, AVPlayerState::STATE_COMPLETED},
1434         {PLAYER_STATE_ERROR, AVPlayerState::STATE_ERROR},
1435     };
1436 
1437     std::lock_guard<std::recursive_mutex> lock(taskMutex_);
1438     if (stateMap.find(state_) != stateMap.end()) {
1439         curState = stateMap.at(state_);
1440     }
1441     return curState;
1442 }
1443 
OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode,const std::string & errorMsg)1444 void CJAVPlayer::OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg)
1445 {
1446     std::lock_guard<std::mutex> lock(mutex_);
1447     if (playerCb_ != nullptr) {
1448         playerCb_->OnErrorCb(errorCode, errorMsg);
1449     }
1450 }
1451 
StartListenCurrentResource()1452 void CJAVPlayer::StartListenCurrentResource()
1453 {
1454     std::lock_guard<std::mutex> lock(mutex_);
1455     if (playerCb_ != nullptr) {
1456         playerCb_->Start();
1457     }
1458 }
1459 
SetSource(std::string url)1460 void CJAVPlayer::SetSource(std::string url)
1461 {
1462     bool isFd = (url.find("fd://") != std::string::npos) ? true : false;
1463     bool isNetwork = (url.find("http") != std::string::npos) ? true : false;
1464     if (isNetwork) {
1465         EnqueueNetworkTask(url);
1466     } else if (isFd) {
1467         std::string inputFd = url.substr(sizeof("fd://") - 1);
1468         int32_t fd = -1;
1469         if (!StrToInt(inputFd, fd) || fd < 0) {
1470             OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER,
1471                       "invalid parameters, The input parameter is not a fd://+numeric string");
1472             return;
1473         }
1474         EnqueueFdTask(fd);
1475     } else {
1476         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER,
1477                   "invalid parameters, The input parameter is not fd:// or network address");
1478     }
1479 }
1480 
EnqueueNetworkTask(const std::string url)1481 void CJAVPlayer::EnqueueNetworkTask(const std::string url)
1482 {
1483     std::unique_lock<std::recursive_mutex> lock(taskMutex_);
1484     auto state = GetCurrentState();
1485     if (state != AVPlayerState::STATE_IDLE) {
1486         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "current state is not idle, unsupport set url");
1487         return;
1488     }
1489     if (player_ != nullptr) {
1490         if (player_->SetSource(url) != MSERR_OK) {
1491             QueueOnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "failed to SetSourceNetWork");
1492             return;
1493         }
1494         MEDIA_LOGI("0x%{public}06" PRIXPTR " Set source network out", FAKE_POINTER(this));
1495     }
1496 }
1497 
EnqueueFdTask(const int32_t fd)1498 void CJAVPlayer::EnqueueFdTask(const int32_t fd)
1499 {
1500     std::unique_lock<std::recursive_mutex> lock(taskMutex_);
1501     auto state = GetCurrentState();
1502     if (state != AVPlayerState::STATE_IDLE) {
1503         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "current state is not idle, unsupport set source fd");
1504         return;
1505     }
1506     if (player_ != nullptr) {
1507         if (player_->SetSource(fd, 0, -1) != MSERR_OK) {
1508             QueueOnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "failed to SetSourceFd");
1509             return;
1510         }
1511         MEDIA_LOGI("Set source fd out");
1512     }
1513 }
1514 
QueueOnErrorCb(MediaServiceExtErrCodeAPI9 errorCode,const std::string & errorMsg)1515 void CJAVPlayer::QueueOnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg)
1516 {
1517     CHECK_AND_RETURN(!isReleased_.load());
1518     OnErrorCb(errorCode, errorMsg);
1519 }
1520 
IsLiveSource() const1521 bool CJAVPlayer::IsLiveSource() const
1522 {
1523     return isLiveStream_;
1524 }
1525 
IsControllable()1526 bool CJAVPlayer::IsControllable()
1527 {
1528     auto state = GetCurrentState();
1529     if (state == AVPlayerState::STATE_PREPARED || state == AVPlayerState::STATE_PLAYING ||
1530         state == AVPlayerState::STATE_PAUSED || state == AVPlayerState::STATE_COMPLETED) {
1531         return true;
1532     } else {
1533         return false;
1534     }
1535 }
1536 
PauseListenCurrentResource()1537 void CJAVPlayer::PauseListenCurrentResource()
1538 {
1539     std::lock_guard<std::mutex> lock(mutex_);
1540     if (playerCb_ != nullptr) {
1541         playerCb_->Pause();
1542     }
1543 }
1544 
ResetUserParameters()1545 void CJAVPlayer::ResetUserParameters()
1546 {
1547     std::lock_guard<std::recursive_mutex> lock(taskMutex_);
1548     url_.clear();
1549     fileDescriptor_.fd = 0;
1550     fileDescriptor_.offset = 0;
1551     fileDescriptor_.length = -1;
1552     width_ = 0;
1553     height_ = 0;
1554     position_ = -1;
1555     duration_ = -1;
1556     loop_ = false;
1557 }
1558 
TransferSeekMode(int32_t mode)1559 PlayerSeekMode CJAVPlayer::TransferSeekMode(int32_t mode)
1560 {
1561     MEDIA_LOGI("Seek Task TransferSeekMode, mode: %{public}d", mode);
1562     PlayerSeekMode seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
1563     switch (mode) {
1564         case 0: // Seek to the next sync frame of the given timestamp.
1565             seekMode = PlayerSeekMode::SEEK_NEXT_SYNC;
1566             break;
1567         case 1: // Seek to the previous sync frame of the given timestamp.
1568             seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
1569             break;
1570         case 2: // Seek to the closest frame of the given timestamp. 2 refers SeekMode in @ohos.multimedia.media.d.ts
1571             seekMode = PlayerSeekMode::SEEK_CLOSEST;
1572             break;
1573         case 3: // Seek continous of the given timestamp. 3 refers SeekMode in @ohos.multimedia.media.d.ts
1574             seekMode = PlayerSeekMode::SEEK_CONTINOUS;
1575             break;
1576         default:
1577             seekMode = PlayerSeekMode::SEEK_PREVIOUS_SYNC;
1578             break;
1579     }
1580     return seekMode;
1581 }
1582 
SetMediaSource(const std::shared_ptr<AVMediaSource> & mediaSource,AVPlayStrategy strategy)1583 int32_t CJAVPlayer::SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy)
1584 {
1585     if (GetCurrentState() != AVPlayerState::STATE_IDLE) {
1586         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "current state is not idle, unsupport set mediaSource");
1587         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1588     }
1589     StartListenCurrentResource(); // Listen to the events of the current resource
1590     return player_->SetMediaSource(mediaSource, strategy);
1591 }
1592 
SetPlaybackStrategy(AVPlayStrategy strategy)1593 int32_t CJAVPlayer::SetPlaybackStrategy(AVPlayStrategy strategy)
1594 {
1595     std::string currentState = GetCurrentState();
1596     if (currentState != AVPlayerState::STATE_INITIALIZED && currentState != AVPlayerState::STATE_STOPPED) {
1597         MEDIA_LOGE("current state is not initialized / stopped, unsupport set playback strategy");
1598         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1599     }
1600     if (strategy.mutedMediaType != MediaType::MEDIA_TYPE_AUD) {
1601         MEDIA_LOGE("only support mute media type audio now");
1602         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1603     } else {
1604         AVPlayStrategy playStrategy;
1605         std::string state = GetCurrentState();
1606         if (state == AVPlayerState::STATE_INITIALIZED || state == AVPlayerState::STATE_STOPPED) {
1607             int32_t ret = player_->SetPlaybackStrategy(playStrategy);
1608             if (ret != MSERR_OK) {
1609                 MEDIA_LOGE("failed to set playback strategy");
1610                 return MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(ret));
1611             }
1612             MEDIA_LOGD("SetPlaybackStrategy Success");
1613             return MSERR_EXT_API9_OK;
1614         }
1615         MEDIA_LOGE("current state is not initialized or stopped, unsupport set playback strategy operation");
1616         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1617     }
1618 }
1619 
SetMediaMuted(int32_t mediaType,bool muted)1620 int32_t CJAVPlayer::SetMediaMuted(int32_t mediaType, bool muted)
1621 {
1622     auto curState = GetCurrentState();
1623     bool canSetMute = curState == AVPlayerState::STATE_PREPARED || curState == AVPlayerState::STATE_PLAYING ||
1624                       curState == AVPlayerState::STATE_PAUSED || curState == AVPlayerState::STATE_COMPLETED;
1625     if (!canSetMute) {
1626         MEDIA_LOGE("current state is not initialized / stopped, unsupport set playback strategy operation");
1627         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1628     }
1629     auto state = GetCurrentState();
1630     if (state == AVPlayerState::STATE_INITIALIZED || IsControllable()) {
1631         int32_t ret = player_->SetMediaMuted(static_cast<MediaType>(mediaType), muted);
1632         if (ret != MSERR_OK) {
1633             MEDIA_LOGE("failed to set muted");
1634             return MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(ret));
1635         }
1636         MEDIA_LOGD("SetMediaMuted Success");
1637         return MSERR_EXT_API9_OK;
1638     }
1639     MEDIA_LOGE("current state is not stopped or initialized, unsupport prepare operation");
1640     return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1641 }
1642 
GetSelectedTracks(std::vector<int32_t> & trackIndex)1643 int32_t CJAVPlayer::GetSelectedTracks(std::vector<int32_t> &trackIndex)
1644 {
1645     if (IsControllable()) {
1646         int32_t videoIndex = -1;
1647         (void)player_->GetCurrentTrack(MediaType::MEDIA_TYPE_VID, videoIndex);
1648         if (videoIndex != -1) {
1649             trackIndex.push_back(videoIndex);
1650         }
1651 
1652         int32_t audioIndex = -1;
1653         (void)player_->GetCurrentTrack(MediaType::MEDIA_TYPE_AUD, audioIndex);
1654         if (audioIndex != -1) {
1655             trackIndex.push_back(audioIndex);
1656         }
1657 
1658         int32_t subtitleIndex = -1;
1659         (void)player_->GetCurrentTrack(MediaType::MEDIA_TYPE_SUBTITLE, subtitleIndex);
1660         if (subtitleIndex != -1) {
1661             trackIndex.push_back(subtitleIndex);
1662         }
1663         return MSERR_EXT_API9_OK;
1664     }
1665     MEDIA_LOGE("current state unsupport get current selections");
1666     return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1667 }
1668 
TransferSwitchMode(int32_t mode)1669 PlayerSwitchMode TransferSwitchMode(int32_t mode)
1670 {
1671     MEDIA_LOGI("TransferSwitchMode, mode: %{public}d", mode);
1672     PlayerSwitchMode switchMode = PlayerSwitchMode::SWITCH_CLOSEST;
1673     switch (mode) {
1674         case 0:
1675             switchMode = PlayerSwitchMode::SWITCH_SMOOTH;
1676             break;
1677         case 1:
1678             switchMode = PlayerSwitchMode::SWITCH_SEGMENT;
1679             break;
1680         default:
1681             break;
1682     }
1683     return switchMode;
1684 }
1685 
SelectTrack(int32_t index,int32_t mode)1686 int32_t CJAVPlayer::SelectTrack(int32_t index, int32_t mode)
1687 {
1688     if (index < 0) {
1689         MEDIA_LOGE("invalid parameters, please check the track index");
1690         return MSERR_EXT_API9_INVALID_PARAMETER;
1691     }
1692     if (!IsControllable()) {
1693         MEDIA_LOGE("current state is not prepared/playing/paused/completed, unsupport selectTrack operation");
1694         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1695     }
1696     return player_->SelectTrack(index, TransferSwitchMode(mode));
1697 }
1698 
DeselectTrack(int32_t index)1699 int32_t CJAVPlayer::DeselectTrack(int32_t index)
1700 {
1701     if (index < 0) {
1702         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "invalid parameters, please check the track index");
1703         return MSERR_EXT_API9_INVALID_PARAMETER;
1704     }
1705     if (!IsControllable()) {
1706         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
1707                   "current state is not prepared/playing/paused/completed, unsupport deselecttrack operation");
1708         return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1709     }
1710     return player_->DeselectTrack(index);
1711 }
1712 
GetMediaKeySystemInfos()1713 std::multimap<std::string, std::vector<uint8_t>> CJAVPlayer::GetMediaKeySystemInfos()
1714 {
1715     return localDrmInfos_;
1716 }
1717 
SetSpeed(int32_t speed)1718 void CJAVPlayer::SetSpeed(int32_t speed)
1719 {
1720     if (IsLiveSource()) {
1721         OnErrorCb(MSERR_EXT_API9_UNSUPPORT_CAPABILITY, "The stream is live stream, not support speed");
1722         return;
1723     }
1724 
1725     if (!IsControllable()) {
1726         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
1727                   "current state is not prepared/playing/paused/completed, unsupport speed operation");
1728         return;
1729     }
1730 
1731     (void)player_->SetPlaybackSpeed(static_cast<PlaybackRateMode>(speed));
1732 }
1733 
SetBitrate(int32_t bitrate)1734 void CJAVPlayer::SetBitrate(int32_t bitrate)
1735 {
1736     if (bitrate < 0) {
1737         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "invalid parameters, please check the input bitrate");
1738         return;
1739     }
1740     if (!IsControllable()) {
1741         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
1742                   "current state is not prepared/playing/paused/completed, unsupport select bitrate operation");
1743         return;
1744     }
1745     (void)player_->SelectBitRate(static_cast<uint32_t>(bitrate));
1746 }
1747 
SetVolume(float volume)1748 void CJAVPlayer::SetVolume(float volume)
1749 {
1750     if (playerCb_->isSetVolume_) {
1751         MEDIA_LOGI("SetVolume is processing, skip this task until onVolumeChangedCb");
1752     }
1753     playerCb_->isSetVolume_ = true;
1754 
1755     if (volume < 0.0f || volume > 1.0f) {
1756         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "invalid parameters, check volume level");
1757         return;
1758     }
1759 
1760     if (!IsControllable()) {
1761         OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT,
1762                   "current state is not prepared/playing/paused/completed, unsupport volume operation");
1763         return;
1764     }
1765     (void)player_->SetVolume(volume, volume);
1766 }
1767 
AddSubtitleFromFd(int32_t fd,int64_t offset,int64_t length)1768 int32_t CJAVPlayer::AddSubtitleFromFd(int32_t fd, int64_t offset, int64_t length)
1769 {
1770     if (player_->AddSubSource(fd, offset, length) != MSERR_OK) {
1771         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "failed to AddSubtitleAVFileDescriptor");
1772         return MSERR_EXT_API9_INVALID_PARAMETER;
1773     }
1774     return MSERR_EXT_API9_OK;
1775 }
1776 
AddSubtitleFromUrl(std::string url)1777 int32_t CJAVPlayer::AddSubtitleFromUrl(std::string url)
1778 {
1779     MEDIA_LOGI("input url is %{private}s!", url.c_str());
1780     bool isFd = (url.find("fd://") != std::string::npos) ? true : false;
1781     bool isNetwork = (url.find("http") != std::string::npos) ? true : false;
1782     if (isNetwork) {
1783         if (player_->AddSubSource(url) != MSERR_OK) {
1784             OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "failed to AddSubtitleNetworkSource");
1785             return MSERR_EXT_API9_INVALID_PARAMETER;
1786         }
1787     } else if (isFd) {
1788         const std::string fdHead = "fd://";
1789         std::string inputFd = url.substr(fdHead.size());
1790         int32_t fd = -1;
1791         if (!StrToInt(inputFd, fd) || fd < 0) {
1792             OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER,
1793                       "invalid parameters, The input parameter is not a fd://+numeric string");
1794             return MSERR_EXT_API9_INVALID_PARAMETER;
1795         }
1796 
1797         if (player_->AddSubSource(fd, 0, -1) != MSERR_OK) {
1798             OnErrorCb(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "failed to AddSubtitleFdSource");
1799             return MSERR_EXT_API9_INVALID_PARAMETER;
1800         }
1801     } else {
1802         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER,
1803                   "invalid parameters, The input parameter is not fd:// or network address");
1804         return MSERR_EXT_API9_INVALID_PARAMETER;
1805     }
1806     return MSERR_EXT_API9_OK;
1807 }
1808 
GetPlaybackInfo(Format & format)1809 int32_t CJAVPlayer::GetPlaybackInfo(Format &format)
1810 {
1811     if (IsControllable()) {
1812         return player_->GetPlaybackInfo(format);
1813     }
1814     MEDIA_LOGE("current state unsupport get playback info");
1815     return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1816 }
1817 
GetTrackDescription(std::vector<Format> & trackInfos)1818 int32_t CJAVPlayer::GetTrackDescription(std::vector<Format> &trackInfos)
1819 {
1820     if (IsControllable()) {
1821         (void)player_->GetVideoTrackInfo(trackInfos);
1822         (void)player_->GetAudioTrackInfo(trackInfos);
1823         (void)player_->GetSubtitleTrackInfo(trackInfos);
1824         return MSERR_EXT_API9_OK;
1825     }
1826     MEDIA_LOGE("current state unsupport get track description");
1827     return MSERR_EXT_API9_OPERATE_NOT_PERMIT;
1828 }
1829 
HandleParameter(CAudioRendererInfo info)1830 bool CJAVPlayer::HandleParameter(CAudioRendererInfo info)
1831 {
1832     int32_t content = CONTENT_TYPE_UNKNOWN;
1833     int32_t usage = info.usage;
1834     int32_t rendererFlags = info.rendererFlags;
1835     MEDIA_LOGI("content = %{public}d, usage = %{public}d, rendererFlags = %{public}d", content, usage, rendererFlags);
1836     std::vector<int32_t> contents = {CONTENT_TYPE_UNKNOWN, CONTENT_TYPE_SPEECH,       CONTENT_TYPE_MUSIC,
1837                                      CONTENT_TYPE_MOVIE,   CONTENT_TYPE_SONIFICATION, CONTENT_TYPE_RINGTONE};
1838     std::vector<int32_t> usages = {STREAM_USAGE_UNKNOWN,
1839                                    STREAM_USAGE_MEDIA,
1840                                    STREAM_USAGE_MUSIC,
1841                                    STREAM_USAGE_VOICE_COMMUNICATION,
1842                                    STREAM_USAGE_VOICE_ASSISTANT,
1843                                    STREAM_USAGE_ALARM,
1844                                    STREAM_USAGE_VOICE_MESSAGE,
1845                                    STREAM_USAGE_NOTIFICATION_RINGTONE,
1846                                    STREAM_USAGE_RINGTONE,
1847                                    STREAM_USAGE_NOTIFICATION,
1848                                    STREAM_USAGE_ACCESSIBILITY,
1849                                    STREAM_USAGE_SYSTEM,
1850                                    STREAM_USAGE_MOVIE,
1851                                    STREAM_USAGE_GAME,
1852                                    STREAM_USAGE_AUDIOBOOK,
1853                                    STREAM_USAGE_NAVIGATION,
1854                                    STREAM_USAGE_DTMF,
1855                                    STREAM_USAGE_ENFORCED_TONE,
1856                                    STREAM_USAGE_ULTRASONIC,
1857                                    STREAM_USAGE_VIDEO_COMMUNICATION,
1858                                    STREAM_USAGE_ULTRASONIC};
1859     if (std::find(contents.begin(), contents.end(), content) == contents.end() ||
1860         std::find(usages.begin(), usages.end(), usage) == usages.end()) {
1861         return false;
1862     }
1863 
1864     if (audioRendererInfo_.contentType != content || audioRendererInfo_.streamUsage != usage) {
1865         audioEffectMode_ = OHOS::AudioStandard::AudioEffectMode::EFFECT_DEFAULT;
1866     }
1867 
1868     audioRendererInfo_ = AudioStandard::AudioRendererInfo{
1869         static_cast<AudioStandard::ContentType>(content),
1870         static_cast<AudioStandard::StreamUsage>(usage),
1871         rendererFlags,
1872     };
1873     return true;
1874 }
1875 
1876 #ifdef SUPPORT_VIDEO
SetSurface(const std::string & surfaceStr)1877 void CJAVPlayer::SetSurface(const std::string &surfaceStr)
1878 {
1879     MEDIA_LOGI("get surface, surfaceStr = %{public}s", surfaceStr.c_str());
1880     uint64_t surfaceId = 0;
1881     if (surfaceStr.empty() || surfaceStr[0] < '0' || surfaceStr[0] > '9') {
1882         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER,
1883                   "Please obtain the surface from XComponentController.getXComponentSurfaceId");
1884         return;
1885     }
1886     if (!StrToULL(surfaceStr, surfaceId)) {
1887         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER,
1888             "invalid parameters, failed to obtain surfaceId");
1889         return;
1890     }
1891     MEDIA_LOGI("get surface, surfaceId = (%{public}" PRIu64 ")", surfaceId);
1892 
1893     auto surface = SurfaceUtils::GetInstance()->GetSurface(surfaceId);
1894     if (surface == nullptr) {
1895         OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, "SurfaceUtils cannot convert ID to Surface");
1896         return;
1897     }
1898 
1899     MEDIA_LOGI("0x%{public}06" PRIXPTR " SetSurface Task", FAKE_POINTER(this));
1900     if (player_ != nullptr) {
1901         (void)player_->SetVideoSurface(surface);
1902     }
1903 }
1904 #else
SetSurface(const std::string & surfaceStr)1905 void CJAVPlayer::SetSurface(const std::string &surfaceStr)
1906 {
1907     (void)surfaceStr;
1908     OnErrorCb(MSERR_EXT_API9_UNSUPPORT_CAPABILITY, "The music player does not need to support (Surface)");
1909 }
1910 #endif
1911 } // namespace Media
1912 } // namespace OHOS