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