1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd. 3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License. 5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at 6fa7767c5Sopenharmony_ci * 7fa7767c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fa7767c5Sopenharmony_ci * 9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and 13fa7767c5Sopenharmony_ci * limitations under the License. 14fa7767c5Sopenharmony_ci */ 15fa7767c5Sopenharmony_ci 16fa7767c5Sopenharmony_ci#define HST_LOG_TAG "HiPlayerImpl" 17fa7767c5Sopenharmony_ci 18fa7767c5Sopenharmony_ci#include "hiplayer_impl.h" 19fa7767c5Sopenharmony_ci#include <audio_info.h> 20fa7767c5Sopenharmony_ci#include <av_common.h> 21fa7767c5Sopenharmony_ci#include <media_errors.h> 22fa7767c5Sopenharmony_ci#include "foundation/cpp_ext/type_traits_ext.h" 23fa7767c5Sopenharmony_ci#include "foundation/log.h" 24fa7767c5Sopenharmony_ci#include "foundation/utils/dump_buffer.h" 25fa7767c5Sopenharmony_ci#include "foundation/utils/hitrace_utils.h" 26fa7767c5Sopenharmony_ci#include "foundation/utils/steady_clock.h" 27fa7767c5Sopenharmony_ci#include "media_utils.h" 28fa7767c5Sopenharmony_ci#include "pipeline/factory/filter_factory.h" 29fa7767c5Sopenharmony_ci#include "plugin/common/media_source.h" 30fa7767c5Sopenharmony_ci#include "plugin/common/plugin_time.h" 31fa7767c5Sopenharmony_ci 32fa7767c5Sopenharmony_cinamespace { 33fa7767c5Sopenharmony_ciconst float MAX_MEDIA_VOLUME = 1.0f; // standard interface volume is between 0 to 1. 34fa7767c5Sopenharmony_ci} 35fa7767c5Sopenharmony_ci 36fa7767c5Sopenharmony_cinamespace OHOS { 37fa7767c5Sopenharmony_cinamespace Media { 38fa7767c5Sopenharmony_ciusing namespace Pipeline; 39fa7767c5Sopenharmony_ciconstexpr double EPSINON = 0.0001; 40fa7767c5Sopenharmony_ciconstexpr double SPEED_0_75_X = 0.75; 41fa7767c5Sopenharmony_ciconstexpr double SPEED_1_00_X = 1.00; 42fa7767c5Sopenharmony_ciconstexpr double SPEED_1_25_X = 1.25; 43fa7767c5Sopenharmony_ciconstexpr double SPEED_1_75_X = 1.75; 44fa7767c5Sopenharmony_ciconstexpr double SPEED_2_00_X = 2.00; 45fa7767c5Sopenharmony_ci 46fa7767c5Sopenharmony_ciHiPlayerImpl::HiPlayerImpl(int32_t appUid, int32_t appPid) 47fa7767c5Sopenharmony_ci : appUid_(appUid), 48fa7767c5Sopenharmony_ci appPid_(appPid), 49fa7767c5Sopenharmony_ci volume_(-1.0f), // default negative, if app not set, will not set it. 50fa7767c5Sopenharmony_ci mediaStats_() 51fa7767c5Sopenharmony_ci{ 52fa7767c5Sopenharmony_ci SYNC_TRACER(); 53fa7767c5Sopenharmony_ci MEDIA_LOG_I("hiPlayerImpl ctor"); 54fa7767c5Sopenharmony_ci FilterFactory::Instance().Init(); 55fa7767c5Sopenharmony_ci syncManager_ = std::make_shared<MediaSyncManager>(); 56fa7767c5Sopenharmony_ci 57fa7767c5Sopenharmony_ci audioSource_ = 58fa7767c5Sopenharmony_ci FilterFactory::Instance().CreateFilterWithType<MediaSourceFilter>("builtin.player.mediasource", "mediaSource"); 59fa7767c5Sopenharmony_ci#ifdef UNIT_TEST 60fa7767c5Sopenharmony_ci demuxer_ = FilterFactory::Instance().CreateFilterWithType<DemuxerFilter>("builtin.player.demuxer", "demuxer"); 61fa7767c5Sopenharmony_ci audioDecoder_ = FilterFactory::Instance().CreateFilterWithType<AudioDecoderFilter>("builtin.player.audiodecoder", 62fa7767c5Sopenharmony_ci "audiodecoder"); 63fa7767c5Sopenharmony_ci audioSink_ = 64fa7767c5Sopenharmony_ci FilterFactory::Instance().CreateFilterWithType<AudioSinkFilter>("builtin.player.audiosink", "audiosink"); 65fa7767c5Sopenharmony_ci#else 66fa7767c5Sopenharmony_ci demuxer_ = FilterFactory::Instance().CreateFilterWithType<DemuxerFilter>("builtin.player.demuxer", "demuxer"); 67fa7767c5Sopenharmony_ci audioSink_ = 68fa7767c5Sopenharmony_ci FilterFactory::Instance().CreateFilterWithType<AudioSinkFilter>("builtin.player.audiosink", "audioSink"); 69fa7767c5Sopenharmony_ci audioSink_->SetParameter(static_cast<int32_t>(Plugin::Tag::APP_PID), appPid_); 70fa7767c5Sopenharmony_ci audioSink_->SetParameter(static_cast<int32_t>(Plugin::Tag::APP_UID), appUid_); 71fa7767c5Sopenharmony_ci#ifdef VIDEO_SUPPORT 72fa7767c5Sopenharmony_ci videoSink_ = 73fa7767c5Sopenharmony_ci FilterFactory::Instance().CreateFilterWithType<VideoSinkFilter>("builtin.player.videosink", "videoSink"); 74fa7767c5Sopenharmony_ci FALSE_RETURN(videoSink_ != nullptr); 75fa7767c5Sopenharmony_ci videoSink_->SetSyncCenter(syncManager_); 76fa7767c5Sopenharmony_ci#endif 77fa7767c5Sopenharmony_ci#endif 78fa7767c5Sopenharmony_ci FALSE_RETURN(audioSource_ != nullptr); 79fa7767c5Sopenharmony_ci FALSE_RETURN(demuxer_ != nullptr); 80fa7767c5Sopenharmony_ci FALSE_RETURN(audioSink_ != nullptr); 81fa7767c5Sopenharmony_ci audioSink_->SetSyncCenter(syncManager_); 82fa7767c5Sopenharmony_ci pipeline_ = std::make_shared<PipelineCore>(); 83fa7767c5Sopenharmony_ci callbackLooper_.SetPlayEngine(this); 84fa7767c5Sopenharmony_ci} 85fa7767c5Sopenharmony_ci 86fa7767c5Sopenharmony_ciHiPlayerImpl::~HiPlayerImpl() 87fa7767c5Sopenharmony_ci{ 88fa7767c5Sopenharmony_ci MEDIA_LOG_I("dtor called."); 89fa7767c5Sopenharmony_ci if (pipelineStates_ != PLAYER_STOPPED) { 90fa7767c5Sopenharmony_ci DoStop(); 91fa7767c5Sopenharmony_ci HiPlayerImpl::OnStateChanged(StateId::STOPPED); 92fa7767c5Sopenharmony_ci } 93fa7767c5Sopenharmony_ci callbackLooper_.Stop(); 94fa7767c5Sopenharmony_ci audioSink_.reset(); 95fa7767c5Sopenharmony_ci#ifdef VIDEO_SUPPORT 96fa7767c5Sopenharmony_ci videoSink_.reset(); 97fa7767c5Sopenharmony_ci#endif 98fa7767c5Sopenharmony_ci syncManager_.reset(); 99fa7767c5Sopenharmony_ci} 100fa7767c5Sopenharmony_civoid HiPlayerImpl::UpdateStateNoLock(PlayerStates newState, bool notifyUpward) 101fa7767c5Sopenharmony_ci{ 102fa7767c5Sopenharmony_ci if (pipelineStates_ == newState) { 103fa7767c5Sopenharmony_ci return; 104fa7767c5Sopenharmony_ci } 105fa7767c5Sopenharmony_ci pipelineStates_ = newState; 106fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_IDLE || pipelineStates_ == PlayerStates::PLAYER_PREPARING) { 107fa7767c5Sopenharmony_ci MEDIA_LOG_W("do not report idle and preparing since av player doesn't need report idle and preparing"); 108fa7767c5Sopenharmony_ci return; 109fa7767c5Sopenharmony_ci } 110fa7767c5Sopenharmony_ci if (notifyUpward) { 111fa7767c5Sopenharmony_ci if (callbackLooper_.IsStarted()) { 112fa7767c5Sopenharmony_ci Format format; 113fa7767c5Sopenharmony_ci while (!pendingStates_.empty()) { 114fa7767c5Sopenharmony_ci auto pendingState = pendingStates_.front(); 115fa7767c5Sopenharmony_ci pendingStates_.pop(); 116fa7767c5Sopenharmony_ci MEDIA_LOG_I("sending pending state change: " PUBLIC_LOG_S, StringnessPlayerState(pendingState).c_str()); 117fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_STATE_CHANGE, pendingState, format); 118fa7767c5Sopenharmony_ci } 119fa7767c5Sopenharmony_ci MEDIA_LOG_I("sending newest state change: " PUBLIC_LOG_S, 120fa7767c5Sopenharmony_ci StringnessPlayerState(pipelineStates_.load()).c_str()); 121fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_STATE_CHANGE, pipelineStates_, format); 122fa7767c5Sopenharmony_ci } else { 123fa7767c5Sopenharmony_ci pendingStates_.push(newState); 124fa7767c5Sopenharmony_ci } 125fa7767c5Sopenharmony_ci } 126fa7767c5Sopenharmony_ci} 127fa7767c5Sopenharmony_ci 128fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::Init() 129fa7767c5Sopenharmony_ci{ 130fa7767c5Sopenharmony_ci MEDIA_LOG_I("Init entered."); 131fa7767c5Sopenharmony_ci mediaStats_.Reset(); 132fa7767c5Sopenharmony_ci if (initialized_.load()) { 133fa7767c5Sopenharmony_ci return ErrorCode::SUCCESS; 134fa7767c5Sopenharmony_ci } 135fa7767c5Sopenharmony_ci pipeline_->Init(this, this); 136fa7767c5Sopenharmony_ci ErrorCode ret = pipeline_->AddFilters({audioSource_.get(), demuxer_.get()}); 137fa7767c5Sopenharmony_ci if (ret == ErrorCode::SUCCESS) { 138fa7767c5Sopenharmony_ci ret = pipeline_->LinkFilters({audioSource_.get(), demuxer_.get()}); 139fa7767c5Sopenharmony_ci } 140fa7767c5Sopenharmony_ci if (ret == ErrorCode::SUCCESS) { 141fa7767c5Sopenharmony_ci initialized_ = true; 142fa7767c5Sopenharmony_ci } else { 143fa7767c5Sopenharmony_ci pipeline_->RemoveFilterChain(audioSource_.get()); 144fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 145fa7767c5Sopenharmony_ci } 146fa7767c5Sopenharmony_ci return ret; 147fa7767c5Sopenharmony_ci} 148fa7767c5Sopenharmony_ci 149fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetSource(const std::string& uri) 150fa7767c5Sopenharmony_ci{ 151fa7767c5Sopenharmony_ci SYNC_TRACER(); 152fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetSource entered source uri: " PUBLIC_LOG_S, uri.c_str()); 153fa7767c5Sopenharmony_ci PROFILE_BEGIN("SetSource begin"); 154fa7767c5Sopenharmony_ci auto ret = Init(); 155fa7767c5Sopenharmony_ci if (ret == ErrorCode::SUCCESS) { 156fa7767c5Sopenharmony_ci ret = DoSetSource(std::make_shared<MediaSource>(uri)); 157fa7767c5Sopenharmony_ci url_ = uri; 158fa7767c5Sopenharmony_ci } 159fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 160fa7767c5Sopenharmony_ci MEDIA_LOG_E("SetSource error: " PUBLIC_LOG_S, GetErrorName(ret)); 161fa7767c5Sopenharmony_ci } else { 162fa7767c5Sopenharmony_ci OnStateChanged(StateId::INIT); 163fa7767c5Sopenharmony_ci } 164fa7767c5Sopenharmony_ci PROFILE_END("SetSource end."); 165fa7767c5Sopenharmony_ci return TransErrorCode(ret); 166fa7767c5Sopenharmony_ci} 167fa7767c5Sopenharmony_ci 168fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetSource(const std::shared_ptr<IMediaDataSource>& dataSrc) 169fa7767c5Sopenharmony_ci{ 170fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetSource entered source stream"); 171fa7767c5Sopenharmony_ci PROFILE_BEGIN("SetSource begin"); 172fa7767c5Sopenharmony_ci auto ret = Init(); 173fa7767c5Sopenharmony_ci if (ret == ErrorCode::SUCCESS) { 174fa7767c5Sopenharmony_ci ret = DoSetSource(std::make_shared<MediaSource>(dataSrc)); 175fa7767c5Sopenharmony_ci } 176fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 177fa7767c5Sopenharmony_ci MEDIA_LOG_E("SetSource error: " PUBLIC_LOG_S, GetErrorName(ret)); 178fa7767c5Sopenharmony_ci } else { 179fa7767c5Sopenharmony_ci OnStateChanged(StateId::INIT); 180fa7767c5Sopenharmony_ci } 181fa7767c5Sopenharmony_ci PROFILE_END("SetSource end."); 182fa7767c5Sopenharmony_ci return TransErrorCode(ret); 183fa7767c5Sopenharmony_ci} 184fa7767c5Sopenharmony_ci 185fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::Prepare() 186fa7767c5Sopenharmony_ci{ 187fa7767c5Sopenharmony_ci DUMP_BUFFER2FILE_PREPARE(); 188fa7767c5Sopenharmony_ci if (!(pipelineStates_ == PlayerStates::PLAYER_INITIALIZED || pipelineStates_ == PlayerStates::PLAYER_STOPPED)) { 189fa7767c5Sopenharmony_ci return MSERR_INVALID_OPERATION; 190fa7767c5Sopenharmony_ci } 191fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_STOPPED) { 192fa7767c5Sopenharmony_ci SetSource(url_); 193fa7767c5Sopenharmony_ci } 194fa7767c5Sopenharmony_ci SYNC_TRACER(); 195fa7767c5Sopenharmony_ci NotifyBufferingUpdate(PlayerKeys::PLAYER_BUFFERING_START, 0); 196fa7767c5Sopenharmony_ci MEDIA_LOG_I("Prepare entered, current pipeline state: " PUBLIC_LOG_S ".", 197fa7767c5Sopenharmony_ci StringnessPlayerState(pipelineStates_).c_str()); 198fa7767c5Sopenharmony_ci PROFILE_BEGIN(); 199fa7767c5Sopenharmony_ci auto ret = PrepareFilters(); 200fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 201fa7767c5Sopenharmony_ci PROFILE_END("Prepare failed,"); 202fa7767c5Sopenharmony_ci MEDIA_LOG_E("Prepare failed with error " PUBLIC_LOG_D32, ret); 203fa7767c5Sopenharmony_ci return TransErrorCode(ret); 204fa7767c5Sopenharmony_ci } 205fa7767c5Sopenharmony_ci OnStateChanged(StateId::PREPARING); 206fa7767c5Sopenharmony_ci OSAL::ScopedLock lock(stateMutex_); 207fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_PREPARING) { // Wait state change to ready 208fa7767c5Sopenharmony_ci cond_.Wait(lock, [this] { return pipelineStates_ != PlayerStates::PLAYER_PREPARING; }); 209fa7767c5Sopenharmony_ci } 210fa7767c5Sopenharmony_ci MEDIA_LOG_D("Prepare finished, current pipeline state: " PUBLIC_LOG "s.", 211fa7767c5Sopenharmony_ci StringnessPlayerState(pipelineStates_).c_str()); 212fa7767c5Sopenharmony_ci PROFILE_END("Prepare finished, current pipeline state: " PUBLIC_LOG "s.", 213fa7767c5Sopenharmony_ci StringnessPlayerState(pipelineStates_).c_str()); 214fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_PREPARED) { 215fa7767c5Sopenharmony_ci NotifyBufferingUpdate(PlayerKeys::PLAYER_BUFFERING_END, 0); 216fa7767c5Sopenharmony_ci Format format; 217fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_POSITION_UPDATE, 0, format); 218fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 219fa7767c5Sopenharmony_ci } 220fa7767c5Sopenharmony_ci 221fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::ERROR_UNKNOWN); 222fa7767c5Sopenharmony_ci} 223fa7767c5Sopenharmony_ci 224fa7767c5Sopenharmony_ciint HiPlayerImpl::PrepareAsync() 225fa7767c5Sopenharmony_ci{ 226fa7767c5Sopenharmony_ci DUMP_BUFFER2FILE_PREPARE(); 227fa7767c5Sopenharmony_ci if (!(pipelineStates_ == PlayerStates::PLAYER_INITIALIZED || pipelineStates_ == PlayerStates::PLAYER_STOPPED)) { 228fa7767c5Sopenharmony_ci return MSERR_INVALID_OPERATION; 229fa7767c5Sopenharmony_ci } 230fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_STOPPED) { 231fa7767c5Sopenharmony_ci SetSource(url_); 232fa7767c5Sopenharmony_ci } 233fa7767c5Sopenharmony_ci ASYNC_TRACER(); 234fa7767c5Sopenharmony_ci NotifyBufferingUpdate(PlayerKeys::PLAYER_BUFFERING_START, 0); 235fa7767c5Sopenharmony_ci MEDIA_LOG_I("Prepare async entered, current pipeline state: " PUBLIC_LOG_S, 236fa7767c5Sopenharmony_ci StringnessPlayerState(pipelineStates_).c_str()); 237fa7767c5Sopenharmony_ci PROFILE_BEGIN(); 238fa7767c5Sopenharmony_ci auto ret = PrepareFilters(); 239fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 240fa7767c5Sopenharmony_ci PROFILE_END("Prepare async failed,"); 241fa7767c5Sopenharmony_ci MEDIA_LOG_E("Prepare async failed with error " PUBLIC_LOG_D32, ret); 242fa7767c5Sopenharmony_ci } else { 243fa7767c5Sopenharmony_ci PROFILE_END("Prepare async successfully,"); 244fa7767c5Sopenharmony_ci } 245fa7767c5Sopenharmony_ci OnStateChanged(StateId::PREPARING); 246fa7767c5Sopenharmony_ci NotifyBufferingUpdate(PlayerKeys::PLAYER_BUFFERING_END, 0); 247fa7767c5Sopenharmony_ci return TransErrorCode(ret); 248fa7767c5Sopenharmony_ci} 249fa7767c5Sopenharmony_ci 250fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::Play() 251fa7767c5Sopenharmony_ci{ 252fa7767c5Sopenharmony_ci SYNC_TRACER(); 253fa7767c5Sopenharmony_ci MEDIA_LOG_I("Play entered."); 254fa7767c5Sopenharmony_ci PROFILE_BEGIN(); 255fa7767c5Sopenharmony_ci auto ret {ErrorCode::SUCCESS}; 256fa7767c5Sopenharmony_ci callbackLooper_.StartReportMediaProgress(100); // 100 MS 257fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_PLAYBACK_COMPLETE) { 258fa7767c5Sopenharmony_ci ret = DoSeek(0, Plugin::SeekMode::SEEK_PREVIOUS_SYNC); 259fa7767c5Sopenharmony_ci } else if (pipelineStates_ == PlayerStates::PLAYER_PAUSED) { 260fa7767c5Sopenharmony_ci ret = DoResume(); 261fa7767c5Sopenharmony_ci } else { 262fa7767c5Sopenharmony_ci ret = DoPlay(); 263fa7767c5Sopenharmony_ci } 264fa7767c5Sopenharmony_ci if (ret == ErrorCode::SUCCESS) { 265fa7767c5Sopenharmony_ci OnStateChanged(StateId::PLAYING); 266fa7767c5Sopenharmony_ci } 267fa7767c5Sopenharmony_ci PROFILE_END("Play ret = " PUBLIC_LOG_D32, TransErrorCode(ret)); 268fa7767c5Sopenharmony_ci return TransErrorCode(ret); 269fa7767c5Sopenharmony_ci} 270fa7767c5Sopenharmony_ci 271fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::Pause() 272fa7767c5Sopenharmony_ci{ 273fa7767c5Sopenharmony_ci SYNC_TRACER(); 274fa7767c5Sopenharmony_ci MEDIA_LOG_I("Pause entered."); 275fa7767c5Sopenharmony_ci PROFILE_BEGIN(); 276fa7767c5Sopenharmony_ci auto ret = TransErrorCode(DoPause()); 277fa7767c5Sopenharmony_ci callbackLooper_.StopReportMediaProgress(); 278fa7767c5Sopenharmony_ci callbackLooper_.ManualReportMediaProgressOnce(); 279fa7767c5Sopenharmony_ci OnStateChanged(StateId::PAUSE); 280fa7767c5Sopenharmony_ci PROFILE_END("Pause ret = " PUBLIC_LOG_D32, ret); 281fa7767c5Sopenharmony_ci return ret; 282fa7767c5Sopenharmony_ci} 283fa7767c5Sopenharmony_ci 284fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::Stop() 285fa7767c5Sopenharmony_ci{ 286fa7767c5Sopenharmony_ci SYNC_TRACER(); 287fa7767c5Sopenharmony_ci MEDIA_LOG_I("Stop entered."); 288fa7767c5Sopenharmony_ci PROFILE_BEGIN(); 289fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_STOPPED) { 290fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 291fa7767c5Sopenharmony_ci } 292fa7767c5Sopenharmony_ci auto ret = TransErrorCode(DoStop()); 293fa7767c5Sopenharmony_ci OnStateChanged(StateId::STOPPED); 294fa7767c5Sopenharmony_ci callbackLooper_.StopReportMediaProgress(); 295fa7767c5Sopenharmony_ci callbackLooper_.ManualReportMediaProgressOnce(); 296fa7767c5Sopenharmony_ci PROFILE_END("Stop ret = " PUBLIC_LOG_D32, ret); 297fa7767c5Sopenharmony_ci return ret; 298fa7767c5Sopenharmony_ci} 299fa7767c5Sopenharmony_ci 300fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::StopAsync() 301fa7767c5Sopenharmony_ci{ 302fa7767c5Sopenharmony_ci MEDIA_LOG_I("StopAsync entered."); 303fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_STOPPED) { 304fa7767c5Sopenharmony_ci return ErrorCode::SUCCESS; 305fa7767c5Sopenharmony_ci } 306fa7767c5Sopenharmony_ci ErrorCode ret = DoStop(); 307fa7767c5Sopenharmony_ci OnStateChanged(StateId::STOPPED); 308fa7767c5Sopenharmony_ci return ret; 309fa7767c5Sopenharmony_ci} 310fa7767c5Sopenharmony_ci 311fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::Seek(int32_t mSeconds, PlayerSeekMode mode) 312fa7767c5Sopenharmony_ci{ 313fa7767c5Sopenharmony_ci SYNC_TRACER(); 314fa7767c5Sopenharmony_ci MEDIA_LOG_I("Seek entered. mSeconds : " PUBLIC_LOG_D32 ", seekMode : " PUBLIC_LOG_D32, 315fa7767c5Sopenharmony_ci mSeconds, static_cast<int32_t>(mode)); 316fa7767c5Sopenharmony_ci int64_t hstTime = 0; 317fa7767c5Sopenharmony_ci int32_t durationMs = 0; 318fa7767c5Sopenharmony_ci NZERO_RETURN(GetDuration(durationMs)); 319fa7767c5Sopenharmony_ci MEDIA_LOG_D("Seek durationMs : " PUBLIC_LOG_D32, durationMs); 320fa7767c5Sopenharmony_ci if (mSeconds >= durationMs) { // if exceeds change to duration 321fa7767c5Sopenharmony_ci mSeconds = durationMs; 322fa7767c5Sopenharmony_ci } 323fa7767c5Sopenharmony_ci mSeconds = mSeconds < 0 ? 0 : mSeconds; 324fa7767c5Sopenharmony_ci if (audioSource_->GetSeekable() != Plugin::Seekable::SEEKABLE) { 325fa7767c5Sopenharmony_ci MEDIA_LOG_E("Seek, invalid operation, audio source is unseekable or invalid"); 326fa7767c5Sopenharmony_ci return MSERR_INVALID_OPERATION; 327fa7767c5Sopenharmony_ci } 328fa7767c5Sopenharmony_ci if (!Plugin::Ms2HstTime(mSeconds, hstTime)) { 329fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::ERROR_INVALID_PARAMETER_VALUE); 330fa7767c5Sopenharmony_ci } 331fa7767c5Sopenharmony_ci auto smode = Transform2SeekMode(mode); 332fa7767c5Sopenharmony_ci auto ret = DoSeek(hstTime, smode); 333fa7767c5Sopenharmony_ci return TransErrorCode(ret); 334fa7767c5Sopenharmony_ci} 335fa7767c5Sopenharmony_ci 336fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetVolume(float leftVolume, float rightVolume) 337fa7767c5Sopenharmony_ci{ 338fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetVolume entered."); 339fa7767c5Sopenharmony_ci if (leftVolume < 0 || leftVolume > MAX_MEDIA_VOLUME || rightVolume < 0 || rightVolume > MAX_MEDIA_VOLUME) { 340fa7767c5Sopenharmony_ci MEDIA_LOG_E("volume not valid, should be in range [0,100]"); 341fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::ERROR_INVALID_PARAMETER_VALUE); 342fa7767c5Sopenharmony_ci } 343fa7767c5Sopenharmony_ci float volume = 0.0f; 344fa7767c5Sopenharmony_ci if (leftVolume < 1e-6 && rightVolume >= 1e-6) { // 1e-6 345fa7767c5Sopenharmony_ci volume = rightVolume; 346fa7767c5Sopenharmony_ci } else if (rightVolume < 1e-6 && leftVolume >= 1e-6) { // 1e-6 347fa7767c5Sopenharmony_ci volume = leftVolume; 348fa7767c5Sopenharmony_ci } else { 349fa7767c5Sopenharmony_ci volume = (leftVolume + rightVolume) / 2; // 2 350fa7767c5Sopenharmony_ci } 351fa7767c5Sopenharmony_ci volume /= MAX_MEDIA_VOLUME; // normalize to 0~1 352fa7767c5Sopenharmony_ci volume_ = volume; 353fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_IDLE || pipelineStates_ == PlayerStates::PLAYER_INITIALIZED || 354fa7767c5Sopenharmony_ci pipelineStates_ == PlayerStates::PLAYER_PREPARING || pipelineStates_ == PlayerStates::PLAYER_STOPPED || 355fa7767c5Sopenharmony_ci audioSink_ == nullptr) { 356fa7767c5Sopenharmony_ci MEDIA_LOG_W("cannot set volume, will do this onReady"); 357fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 358fa7767c5Sopenharmony_ci } 359fa7767c5Sopenharmony_ci return TransErrorCode(SetVolumeToSink(volume)); 360fa7767c5Sopenharmony_ci} 361fa7767c5Sopenharmony_ci 362fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetVideoSurface(sptr<Surface> surface) 363fa7767c5Sopenharmony_ci{ 364fa7767c5Sopenharmony_ci MEDIA_LOG_D("SetVideoSurface entered."); 365fa7767c5Sopenharmony_ci#ifdef VIDEO_SUPPORT 366fa7767c5Sopenharmony_ci FALSE_RETURN_V_MSG_E(surface != nullptr, TransErrorCode(ErrorCode::ERROR_INVALID_PARAMETER_VALUE), 367fa7767c5Sopenharmony_ci "Set video surface failed, surface == nullptr"); 368fa7767c5Sopenharmony_ci return TransErrorCode(videoSink_->SetVideoSurface(surface)); 369fa7767c5Sopenharmony_ci#else 370fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 371fa7767c5Sopenharmony_ci#endif 372fa7767c5Sopenharmony_ci} 373fa7767c5Sopenharmony_ci 374fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::GetVideoTrackInfo(std::vector<Format>& videoTrack) 375fa7767c5Sopenharmony_ci{ 376fa7767c5Sopenharmony_ci MEDIA_LOG_I("GetVideoTrackInfo entered."); 377fa7767c5Sopenharmony_ci std::string mime; 378fa7767c5Sopenharmony_ci std::vector<std::shared_ptr<Plugin::Meta>> metaInfo = demuxer_->GetStreamMetaInfo(); 379fa7767c5Sopenharmony_ci for (const auto& trackInfo : metaInfo) { 380fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::MIME>(mime)) { 381fa7767c5Sopenharmony_ci if (IsVideoMime(mime)) { 382fa7767c5Sopenharmony_ci int64_t bitRate; 383fa7767c5Sopenharmony_ci uint32_t frameRate; 384fa7767c5Sopenharmony_ci uint32_t height; 385fa7767c5Sopenharmony_ci uint32_t width; 386fa7767c5Sopenharmony_ci uint32_t trackIndex; 387fa7767c5Sopenharmony_ci Format videoTrackInfo {}; 388fa7767c5Sopenharmony_ci (void)videoTrackInfo.PutStringValue("codec_mime", mime); 389fa7767c5Sopenharmony_ci (void)videoTrackInfo.PutIntValue("track_type", MediaType::MEDIA_TYPE_VID); 390fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::TRACK_ID>(trackIndex)) { 391fa7767c5Sopenharmony_ci (void)videoTrackInfo.PutIntValue("track_index", static_cast<int32_t>(trackIndex)); 392fa7767c5Sopenharmony_ci } else { 393fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get TRACK_ID failed."); 394fa7767c5Sopenharmony_ci } 395fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::MEDIA_BITRATE>(bitRate)) { 396fa7767c5Sopenharmony_ci (void)videoTrackInfo.PutIntValue("bitrate", static_cast<int32_t>(bitRate)); 397fa7767c5Sopenharmony_ci } else { 398fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get MEDIA_BITRATE fail"); 399fa7767c5Sopenharmony_ci } 400fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::VIDEO_FRAME_RATE>(frameRate)) { 401fa7767c5Sopenharmony_ci (void)videoTrackInfo.PutIntValue("frame_rate", static_cast<int32_t>(frameRate)); 402fa7767c5Sopenharmony_ci } else { 403fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get VIDEO_FRAME_RATE fail"); 404fa7767c5Sopenharmony_ci } 405fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::VIDEO_HEIGHT>(height)) { 406fa7767c5Sopenharmony_ci (void)videoTrackInfo.PutIntValue("height", static_cast<int32_t>(height)); 407fa7767c5Sopenharmony_ci } else { 408fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get VIDEO_HEIGHT fail"); 409fa7767c5Sopenharmony_ci } 410fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::VIDEO_WIDTH>(width)) { 411fa7767c5Sopenharmony_ci (void)videoTrackInfo.PutIntValue("width", static_cast<int32_t>(width)); 412fa7767c5Sopenharmony_ci } else { 413fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get VIDEO_WIDTH failed."); 414fa7767c5Sopenharmony_ci } 415fa7767c5Sopenharmony_ci videoTrack.push_back(videoTrackInfo); 416fa7767c5Sopenharmony_ci } 417fa7767c5Sopenharmony_ci } else { 418fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get MIME fail"); 419fa7767c5Sopenharmony_ci } 420fa7767c5Sopenharmony_ci } 421fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 422fa7767c5Sopenharmony_ci} 423fa7767c5Sopenharmony_ci 424fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::GetAudioTrackInfo(std::vector<Format>& audioTrack) 425fa7767c5Sopenharmony_ci{ 426fa7767c5Sopenharmony_ci MEDIA_LOG_I("GetAudioTrackInfo entered."); 427fa7767c5Sopenharmony_ci std::string mime; 428fa7767c5Sopenharmony_ci std::vector<std::shared_ptr<Plugin::Meta>> metaInfo = demuxer_->GetStreamMetaInfo(); 429fa7767c5Sopenharmony_ci for (const auto& trackInfo : metaInfo) { 430fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::MIME>(mime)) { 431fa7767c5Sopenharmony_ci if (IsAudioMime(mime)) { 432fa7767c5Sopenharmony_ci int64_t bitRate; 433fa7767c5Sopenharmony_ci uint32_t audioChannels; 434fa7767c5Sopenharmony_ci uint32_t audioSampleRate; 435fa7767c5Sopenharmony_ci uint32_t trackIndex; 436fa7767c5Sopenharmony_ci Format audioTrackInfo {}; 437fa7767c5Sopenharmony_ci (void)audioTrackInfo.PutStringValue("codec_mime", mime); 438fa7767c5Sopenharmony_ci (void)audioTrackInfo.PutIntValue("track_type", MediaType::MEDIA_TYPE_AUD); 439fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::TRACK_ID>(trackIndex)) { 440fa7767c5Sopenharmony_ci (void)audioTrackInfo.PutIntValue("track_index", static_cast<int32_t>(trackIndex)); 441fa7767c5Sopenharmony_ci } else { 442fa7767c5Sopenharmony_ci MEDIA_LOG_I("Get TRACK_ID failed."); 443fa7767c5Sopenharmony_ci } 444fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::MEDIA_BITRATE>(bitRate)) { 445fa7767c5Sopenharmony_ci (void)audioTrackInfo.PutIntValue("bitrate", static_cast<int32_t>(bitRate)); 446fa7767c5Sopenharmony_ci } else { 447fa7767c5Sopenharmony_ci MEDIA_LOG_I("Get MEDIA_BITRATE fail"); 448fa7767c5Sopenharmony_ci } 449fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::AUDIO_CHANNELS>(audioChannels)) { 450fa7767c5Sopenharmony_ci (void)audioTrackInfo.PutIntValue("channel_count", static_cast<int32_t>(audioChannels)); 451fa7767c5Sopenharmony_ci } else { 452fa7767c5Sopenharmony_ci MEDIA_LOG_I("Get AUDIO_CHANNELS fail"); 453fa7767c5Sopenharmony_ci } 454fa7767c5Sopenharmony_ci if (trackInfo->Get<Plugin::Tag::AUDIO_SAMPLE_RATE>(audioSampleRate)) { 455fa7767c5Sopenharmony_ci (void)audioTrackInfo.PutIntValue("sample_rate", static_cast<int32_t>(audioSampleRate)); 456fa7767c5Sopenharmony_ci } else { 457fa7767c5Sopenharmony_ci MEDIA_LOG_I("Get AUDIO_SAMPLE_RATE fail"); 458fa7767c5Sopenharmony_ci } 459fa7767c5Sopenharmony_ci audioTrack.push_back(audioTrackInfo); 460fa7767c5Sopenharmony_ci } 461fa7767c5Sopenharmony_ci } else { 462fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get MIME fail"); 463fa7767c5Sopenharmony_ci } 464fa7767c5Sopenharmony_ci } 465fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 466fa7767c5Sopenharmony_ci} 467fa7767c5Sopenharmony_ci 468fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::GetVideoWidth() 469fa7767c5Sopenharmony_ci{ 470fa7767c5Sopenharmony_ci MEDIA_LOG_I("GetVideoWidth entered. video width: " PUBLIC_LOG_D32, videoWidth_); 471fa7767c5Sopenharmony_ci return videoWidth_; 472fa7767c5Sopenharmony_ci} 473fa7767c5Sopenharmony_ci 474fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::GetVideoHeight() 475fa7767c5Sopenharmony_ci{ 476fa7767c5Sopenharmony_ci MEDIA_LOG_I("GetVideoHeight entered. video height: " PUBLIC_LOG_D32, videoHeight_); 477fa7767c5Sopenharmony_ci return videoHeight_; 478fa7767c5Sopenharmony_ci} 479fa7767c5Sopenharmony_ci 480fa7767c5Sopenharmony_civoid HiPlayerImpl::HandleErrorEvent(const Event& event) 481fa7767c5Sopenharmony_ci{ 482fa7767c5Sopenharmony_ci ErrorCode errorCode = ErrorCode::ERROR_UNKNOWN; 483fa7767c5Sopenharmony_ci if (Plugin::Any::IsSameTypeWith<ErrorCode>(event.param)) { 484fa7767c5Sopenharmony_ci errorCode = Plugin::AnyCast<ErrorCode>(event.param); 485fa7767c5Sopenharmony_ci } 486fa7767c5Sopenharmony_ci DoOnError(errorCode); 487fa7767c5Sopenharmony_ci} 488fa7767c5Sopenharmony_ci 489fa7767c5Sopenharmony_civoid HiPlayerImpl::HandleReadyEvent() 490fa7767c5Sopenharmony_ci{ 491fa7767c5Sopenharmony_ci ErrorCode errorCode = DoOnReady(); 492fa7767c5Sopenharmony_ci if (errorCode == ErrorCode::SUCCESS) { 493fa7767c5Sopenharmony_ci OnStateChanged(StateId::READY); 494fa7767c5Sopenharmony_ci Format format; 495fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_POSITION_UPDATE, 0, format); 496fa7767c5Sopenharmony_ci } else { 497fa7767c5Sopenharmony_ci OnStateChanged(StateId::INIT); 498fa7767c5Sopenharmony_ci } 499fa7767c5Sopenharmony_ci} 500fa7767c5Sopenharmony_ci 501fa7767c5Sopenharmony_civoid HiPlayerImpl::HandleCompleteEvent(const Event& event) 502fa7767c5Sopenharmony_ci{ 503fa7767c5Sopenharmony_ci mediaStats_.ReceiveEvent(event); 504fa7767c5Sopenharmony_ci if (mediaStats_.IsEventCompleteAllReceived()) { 505fa7767c5Sopenharmony_ci DoOnComplete(); 506fa7767c5Sopenharmony_ci } 507fa7767c5Sopenharmony_ci} 508fa7767c5Sopenharmony_ci 509fa7767c5Sopenharmony_civoid HiPlayerImpl::HandlePluginErrorEvent(const Event& event) 510fa7767c5Sopenharmony_ci{ 511fa7767c5Sopenharmony_ci Plugin::PluginEvent pluginEvent = Plugin::AnyCast<Plugin::PluginEvent>(event.param); 512fa7767c5Sopenharmony_ci MEDIA_LOG_I("Receive PLUGIN_ERROR, type: " PUBLIC_LOG_D32, CppExt::to_underlying(pluginEvent.type)); 513fa7767c5Sopenharmony_ci if (pluginEvent.type == Plugin::PluginEventType::CLIENT_ERROR && 514fa7767c5Sopenharmony_ci Plugin::Any::IsSameTypeWith<Plugin::NetworkClientErrorCode>(pluginEvent.param)) { 515fa7767c5Sopenharmony_ci auto netClientErrorCode = Plugin::AnyCast<Plugin::NetworkClientErrorCode>(pluginEvent.param); 516fa7767c5Sopenharmony_ci auto errorType {PlayerErrorType::PLAYER_ERROR_UNKNOWN}; 517fa7767c5Sopenharmony_ci auto serviceErrCode { MSERR_UNKNOWN }; 518fa7767c5Sopenharmony_ci if (netClientErrorCode == Plugin::NetworkClientErrorCode::ERROR_TIME_OUT) { 519fa7767c5Sopenharmony_ci errorType = PlayerErrorType::PLAYER_ERROR; 520fa7767c5Sopenharmony_ci serviceErrCode = MSERR_NETWORK_TIMEOUT; 521fa7767c5Sopenharmony_ci } 522fa7767c5Sopenharmony_ci callbackLooper_.OnError(errorType, serviceErrCode); 523fa7767c5Sopenharmony_ci } 524fa7767c5Sopenharmony_ci} 525fa7767c5Sopenharmony_ci 526fa7767c5Sopenharmony_civoid HiPlayerImpl::OnEvent(const Event& event) 527fa7767c5Sopenharmony_ci{ 528fa7767c5Sopenharmony_ci if (event.type != EventType::EVENT_AUDIO_PROGRESS) { 529fa7767c5Sopenharmony_ci MEDIA_LOG_I("[HiStreamer] OnEvent (" PUBLIC_LOG_S ")", GetEventName(event.type)); 530fa7767c5Sopenharmony_ci } 531fa7767c5Sopenharmony_ci switch (event.type) { 532fa7767c5Sopenharmony_ci case EventType::EVENT_ERROR: { 533fa7767c5Sopenharmony_ci HandleErrorEvent(event); 534fa7767c5Sopenharmony_ci break; 535fa7767c5Sopenharmony_ci } 536fa7767c5Sopenharmony_ci case EventType::EVENT_READY: { 537fa7767c5Sopenharmony_ci HandleReadyEvent(); 538fa7767c5Sopenharmony_ci break; 539fa7767c5Sopenharmony_ci } 540fa7767c5Sopenharmony_ci case EventType::EVENT_COMPLETE: { 541fa7767c5Sopenharmony_ci HandleCompleteEvent(event); 542fa7767c5Sopenharmony_ci break; 543fa7767c5Sopenharmony_ci } 544fa7767c5Sopenharmony_ci case EventType::EVENT_PLUGIN_ERROR: { 545fa7767c5Sopenharmony_ci HandlePluginErrorEvent(event); 546fa7767c5Sopenharmony_ci break; 547fa7767c5Sopenharmony_ci } 548fa7767c5Sopenharmony_ci case EventType::EVENT_RESOLUTION_CHANGE: { 549fa7767c5Sopenharmony_ci HandleResolutionChangeEvent(event); 550fa7767c5Sopenharmony_ci break; 551fa7767c5Sopenharmony_ci } 552fa7767c5Sopenharmony_ci case EventType::EVENT_PLUGIN_EVENT: { 553fa7767c5Sopenharmony_ci HandlePluginEvent(event); 554fa7767c5Sopenharmony_ci break; 555fa7767c5Sopenharmony_ci } 556fa7767c5Sopenharmony_ci case EventType::EVENT_VIDEO_RENDERING_START: { 557fa7767c5Sopenharmony_ci Format format; 558fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_MESSAGE, PlayerMessageType::PLAYER_INFO_VIDEO_RENDERING_START, format); 559fa7767c5Sopenharmony_ci break; 560fa7767c5Sopenharmony_ci } 561fa7767c5Sopenharmony_ci case EventType::EVENT_IS_LIVE_STREAM: { 562fa7767c5Sopenharmony_ci Format format; 563fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_IS_LIVE_STREAM, 0, format); 564fa7767c5Sopenharmony_ci break; 565fa7767c5Sopenharmony_ci } 566fa7767c5Sopenharmony_ci default: 567fa7767c5Sopenharmony_ci MEDIA_LOG_E("Unknown event(" PUBLIC_LOG_U32 ")", event.type); 568fa7767c5Sopenharmony_ci } 569fa7767c5Sopenharmony_ci} 570fa7767c5Sopenharmony_ci 571fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoSetSource(const std::shared_ptr<MediaSource>& source) 572fa7767c5Sopenharmony_ci{ 573fa7767c5Sopenharmony_ci auto ret = audioSource_->SetSource(source); 574fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 575fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 576fa7767c5Sopenharmony_ci } 577fa7767c5Sopenharmony_ci return ret; 578fa7767c5Sopenharmony_ci} 579fa7767c5Sopenharmony_ci 580fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::PrepareFilters() 581fa7767c5Sopenharmony_ci{ 582fa7767c5Sopenharmony_ci auto ret = pipeline_->Prepare(); 583fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 584fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 585fa7767c5Sopenharmony_ci } 586fa7767c5Sopenharmony_ci return ret; 587fa7767c5Sopenharmony_ci} 588fa7767c5Sopenharmony_ci 589fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoPlay() 590fa7767c5Sopenharmony_ci{ 591fa7767c5Sopenharmony_ci syncManager_->Resume(); 592fa7767c5Sopenharmony_ci auto ret = pipeline_->Start(); 593fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 594fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 595fa7767c5Sopenharmony_ci } 596fa7767c5Sopenharmony_ci return ret; 597fa7767c5Sopenharmony_ci} 598fa7767c5Sopenharmony_ci 599fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoPause() 600fa7767c5Sopenharmony_ci{ 601fa7767c5Sopenharmony_ci auto ret = pipeline_->Pause(); 602fa7767c5Sopenharmony_ci syncManager_->Pause(); 603fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 604fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 605fa7767c5Sopenharmony_ci } 606fa7767c5Sopenharmony_ci return ret; 607fa7767c5Sopenharmony_ci} 608fa7767c5Sopenharmony_ci 609fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoResume() 610fa7767c5Sopenharmony_ci{ 611fa7767c5Sopenharmony_ci syncManager_->Resume(); 612fa7767c5Sopenharmony_ci auto ret = pipeline_->Resume(); 613fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 614fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 615fa7767c5Sopenharmony_ci } 616fa7767c5Sopenharmony_ci return ret; 617fa7767c5Sopenharmony_ci} 618fa7767c5Sopenharmony_ci 619fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoStop() 620fa7767c5Sopenharmony_ci{ 621fa7767c5Sopenharmony_ci DUMP_BUFFER2FILE_END(); 622fa7767c5Sopenharmony_ci mediaStats_.Reset(); 623fa7767c5Sopenharmony_ci // 先关闭demuxer线程,防止元数据解析prepare过程中出现并发问题 624fa7767c5Sopenharmony_ci if (demuxer_) { 625fa7767c5Sopenharmony_ci demuxer_->StopTask(false); 626fa7767c5Sopenharmony_ci } 627fa7767c5Sopenharmony_ci auto ret = pipeline_->Stop(); 628fa7767c5Sopenharmony_ci syncManager_->Reset(); 629fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 630fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 631fa7767c5Sopenharmony_ci } 632fa7767c5Sopenharmony_ci return ret; 633fa7767c5Sopenharmony_ci} 634fa7767c5Sopenharmony_ci 635fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoReset() 636fa7767c5Sopenharmony_ci{ 637fa7767c5Sopenharmony_ci return DoStop(); 638fa7767c5Sopenharmony_ci} 639fa7767c5Sopenharmony_ci 640fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoSeek(int64_t hstTime, Plugin::SeekMode mode) 641fa7767c5Sopenharmony_ci{ 642fa7767c5Sopenharmony_ci SYNC_TRACER(); 643fa7767c5Sopenharmony_ci PROFILE_BEGIN(); 644fa7767c5Sopenharmony_ci int64_t seekTime = hstTime; 645fa7767c5Sopenharmony_ci Plugin::SeekMode seekMode = mode; 646fa7767c5Sopenharmony_ci auto rtv = seekTime >= 0 ? ErrorCode::SUCCESS : ErrorCode::ERROR_INVALID_OPERATION; 647fa7767c5Sopenharmony_ci if (rtv == ErrorCode::SUCCESS) { 648fa7767c5Sopenharmony_ci pipeline_->FlushStart(); 649fa7767c5Sopenharmony_ci PROFILE_END("Flush start"); 650fa7767c5Sopenharmony_ci PROFILE_RESET(); 651fa7767c5Sopenharmony_ci 652fa7767c5Sopenharmony_ci MEDIA_LOG_I("Do seek ..."); 653fa7767c5Sopenharmony_ci int64_t realSeekTime = seekTime; 654fa7767c5Sopenharmony_ci rtv = audioSource_->SeekToTime(seekTime); 655fa7767c5Sopenharmony_ci if (rtv != ErrorCode::SUCCESS) { 656fa7767c5Sopenharmony_ci MEDIA_LOG_I("SeekToTime failed\n"); 657fa7767c5Sopenharmony_ci rtv = demuxer_->SeekTo(seekTime, seekMode, realSeekTime); 658fa7767c5Sopenharmony_ci } 659fa7767c5Sopenharmony_ci if (rtv == ErrorCode::SUCCESS) { 660fa7767c5Sopenharmony_ci syncManager_->Seek(realSeekTime); 661fa7767c5Sopenharmony_ci } 662fa7767c5Sopenharmony_ci PROFILE_END("SeekTo"); 663fa7767c5Sopenharmony_ci 664fa7767c5Sopenharmony_ci pipeline_->FlushEnd(); 665fa7767c5Sopenharmony_ci PROFILE_END("Flush end"); 666fa7767c5Sopenharmony_ci PROFILE_RESET(); 667fa7767c5Sopenharmony_ci } 668fa7767c5Sopenharmony_ci if (rtv != ErrorCode::SUCCESS) { 669fa7767c5Sopenharmony_ci callbackLooper_.OnError(PLAYER_ERROR, MSERR_SEEK_FAILED); 670fa7767c5Sopenharmony_ci MEDIA_LOG_E("Seek done, seek error."); 671fa7767c5Sopenharmony_ci } else { 672fa7767c5Sopenharmony_ci Format format; 673fa7767c5Sopenharmony_ci int64_t currentPos = Plugin::HstTime2Ms(seekTime); 674fa7767c5Sopenharmony_ci MEDIA_LOG_I("Seek done, currentPos : " PUBLIC_LOG_D64, currentPos); 675fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_SEEKDONE, static_cast<int32_t>(currentPos), format); 676fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_POSITION_UPDATE, static_cast<int32_t>(currentPos), format); 677fa7767c5Sopenharmony_ci } 678fa7767c5Sopenharmony_ci 679fa7767c5Sopenharmony_ci return rtv; 680fa7767c5Sopenharmony_ci} 681fa7767c5Sopenharmony_ci 682fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoOnReady() 683fa7767c5Sopenharmony_ci{ 684fa7767c5Sopenharmony_ci SetVolumeToSink(volume_, false); // do not report 685fa7767c5Sopenharmony_ci auto tmpMeta = demuxer_->GetGlobalMetaInfo(); 686fa7767c5Sopenharmony_ci sourceMeta_ = tmpMeta; 687fa7767c5Sopenharmony_ci int64_t duration = 0; 688fa7767c5Sopenharmony_ci bool found = false; 689fa7767c5Sopenharmony_ci if (tmpMeta->Get<Media::Plugin::Tag::MEDIA_DURATION>(duration)) { 690fa7767c5Sopenharmony_ci found = true; 691fa7767c5Sopenharmony_ci } else { 692fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get media duration failed."); 693fa7767c5Sopenharmony_ci } 694fa7767c5Sopenharmony_ci streamMeta_.clear(); 695fa7767c5Sopenharmony_ci int64_t tmp = 0; 696fa7767c5Sopenharmony_ci for (auto& streamMeta : demuxer_->GetStreamMetaInfo()) { 697fa7767c5Sopenharmony_ci streamMeta_.push_back(streamMeta); 698fa7767c5Sopenharmony_ci if (streamMeta->Get<Media::Plugin::Tag::MEDIA_DURATION>(tmp)) { 699fa7767c5Sopenharmony_ci duration = std::max(duration, tmp); 700fa7767c5Sopenharmony_ci found = true; 701fa7767c5Sopenharmony_ci } else { 702fa7767c5Sopenharmony_ci MEDIA_LOG_W("Get media duration failed."); 703fa7767c5Sopenharmony_ci } 704fa7767c5Sopenharmony_ci } 705fa7767c5Sopenharmony_ci if (found) { 706fa7767c5Sopenharmony_ci duration_ = duration; 707fa7767c5Sopenharmony_ci Format format; 708fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_DURATION_UPDATE, Plugin::HstTime2Ms(duration_), format); 709fa7767c5Sopenharmony_ci } else { 710fa7767c5Sopenharmony_ci MEDIA_LOG_E("INFO_TYPE_DURATION_UPDATE failed"); 711fa7767c5Sopenharmony_ci } 712fa7767c5Sopenharmony_ci std::vector<uint32_t> vBitRates; 713fa7767c5Sopenharmony_ci auto ret = audioSource_->GetBitRates(vBitRates); 714fa7767c5Sopenharmony_ci if ((ret == ErrorCode::SUCCESS) && (vBitRates.size() > 0)) { 715fa7767c5Sopenharmony_ci int msize = vBitRates.size(); 716fa7767c5Sopenharmony_ci const int size_ = msize; 717fa7767c5Sopenharmony_ci uint32_t* bitrates = vBitRates.data(); 718fa7767c5Sopenharmony_ci Format bitRateFormat; 719fa7767c5Sopenharmony_ci (void)bitRateFormat.PutBuffer(std::string(PlayerKeys::PLAYER_BITRATE), 720fa7767c5Sopenharmony_ci static_cast<uint8_t *>(static_cast<void *>(bitrates)), size_ * sizeof(uint32_t)); 721fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_BITRATE_COLLECT, 0, bitRateFormat); 722fa7767c5Sopenharmony_ci } 723fa7767c5Sopenharmony_ci return ErrorCode::SUCCESS; 724fa7767c5Sopenharmony_ci} 725fa7767c5Sopenharmony_ci 726fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoOnComplete() 727fa7767c5Sopenharmony_ci{ 728fa7767c5Sopenharmony_ci MEDIA_LOG_I("OnComplete looping: " PUBLIC_LOG_D32 ".", singleLoop_.load()); 729fa7767c5Sopenharmony_ci Format format; 730fa7767c5Sopenharmony_ci if (singleLoop_.load()) { 731fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_EOS, static_cast<int32_t>(singleLoop_.load()), format); 732fa7767c5Sopenharmony_ci } else { 733fa7767c5Sopenharmony_ci OnStateChanged(StateId::EOS); 734fa7767c5Sopenharmony_ci callbackLooper_.StopReportMediaProgress(); 735fa7767c5Sopenharmony_ci callbackLooper_.ManualReportMediaProgressOnce(); 736fa7767c5Sopenharmony_ci } 737fa7767c5Sopenharmony_ci mediaStats_.ResetEventCompleteAllReceived(); 738fa7767c5Sopenharmony_ci return ErrorCode::SUCCESS; 739fa7767c5Sopenharmony_ci} 740fa7767c5Sopenharmony_ci 741fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::DoOnError(ErrorCode errorCode) 742fa7767c5Sopenharmony_ci{ 743fa7767c5Sopenharmony_ci UpdateStateNoLock(PlayerStates::PLAYER_STATE_ERROR); 744fa7767c5Sopenharmony_ci callbackLooper_.OnError(PLAYER_ERROR, TransErrorCode(errorCode)); 745fa7767c5Sopenharmony_ci return ErrorCode::SUCCESS; 746fa7767c5Sopenharmony_ci} 747fa7767c5Sopenharmony_ci 748fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::SetVolumeToSink(float volume, bool reportUpward) 749fa7767c5Sopenharmony_ci{ 750fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetVolumeToSink entered."); 751fa7767c5Sopenharmony_ci ErrorCode ret = ErrorCode::SUCCESS; 752fa7767c5Sopenharmony_ci if (volume_ >= 0) { 753fa7767c5Sopenharmony_ci MEDIA_LOG_I("set volume " PUBLIC_LOG_F, volume); 754fa7767c5Sopenharmony_ci ret = audioSink_->SetVolume(volume); 755fa7767c5Sopenharmony_ci } 756fa7767c5Sopenharmony_ci 757fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 758fa7767c5Sopenharmony_ci MEDIA_LOG_E("SetVolume failed with error " PUBLIC_LOG_D32, static_cast<int>(ret)); 759fa7767c5Sopenharmony_ci callbackLooper_.OnError(PLAYER_ERROR, TransErrorCode(ret)); 760fa7767c5Sopenharmony_ci } else if (reportUpward) { 761fa7767c5Sopenharmony_ci Format format; 762fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_VOLUME_CHANGE, volume, format); 763fa7767c5Sopenharmony_ci } 764fa7767c5Sopenharmony_ci return ret; 765fa7767c5Sopenharmony_ci} 766fa7767c5Sopenharmony_ci 767fa7767c5Sopenharmony_ciPFilter HiPlayerImpl::CreateAudioDecoder(const std::string& desc) 768fa7767c5Sopenharmony_ci{ 769fa7767c5Sopenharmony_ci if (!audioDecoderMap_[desc]) { 770fa7767c5Sopenharmony_ci audioDecoderMap_[desc] = FilterFactory::Instance().CreateFilterWithType<AudioDecoderFilter>( 771fa7767c5Sopenharmony_ci "builtin.player.audiodecoder", "audiodecoder-" + desc); 772fa7767c5Sopenharmony_ci // set parameters to decoder. 773fa7767c5Sopenharmony_ci } 774fa7767c5Sopenharmony_ci return audioDecoderMap_[desc]; 775fa7767c5Sopenharmony_ci} 776fa7767c5Sopenharmony_ci 777fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetLooping(bool loop) 778fa7767c5Sopenharmony_ci{ 779fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetLooping entered, loop: " PUBLIC_LOG_D32, loop); 780fa7767c5Sopenharmony_ci singleLoop_ = loop; 781fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 782fa7767c5Sopenharmony_ci} 783fa7767c5Sopenharmony_ci 784fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetParameter(const Format& params) 785fa7767c5Sopenharmony_ci{ 786fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetParameter entered."); 787fa7767c5Sopenharmony_ci if (params.ContainKey(PlayerKeys::VIDEO_SCALE_TYPE)) { 788fa7767c5Sopenharmony_ci int32_t videoScaleType = 0; 789fa7767c5Sopenharmony_ci params.GetIntValue(PlayerKeys::VIDEO_SCALE_TYPE, videoScaleType); 790fa7767c5Sopenharmony_ci return SetVideoScaleType(VideoScaleType(videoScaleType)); 791fa7767c5Sopenharmony_ci } 792fa7767c5Sopenharmony_ci if (params.ContainKey(PlayerKeys::CONTENT_TYPE) && params.ContainKey(PlayerKeys::STREAM_USAGE)) { 793fa7767c5Sopenharmony_ci int32_t contentType; 794fa7767c5Sopenharmony_ci int32_t streamUsage; 795fa7767c5Sopenharmony_ci int32_t rendererFlag; 796fa7767c5Sopenharmony_ci params.GetIntValue(PlayerKeys::CONTENT_TYPE, contentType); 797fa7767c5Sopenharmony_ci params.GetIntValue(PlayerKeys::STREAM_USAGE, streamUsage); 798fa7767c5Sopenharmony_ci params.GetIntValue(PlayerKeys::RENDERER_FLAG, rendererFlag); 799fa7767c5Sopenharmony_ci return SetAudioRendererInfo(contentType, streamUsage, rendererFlag); 800fa7767c5Sopenharmony_ci } 801fa7767c5Sopenharmony_ci if (params.ContainKey(PlayerKeys::AUDIO_INTERRUPT_MODE)) { 802fa7767c5Sopenharmony_ci int32_t interruptMode = 0; 803fa7767c5Sopenharmony_ci params.GetIntValue(PlayerKeys::AUDIO_INTERRUPT_MODE, interruptMode); 804fa7767c5Sopenharmony_ci return SetAudioInterruptMode(interruptMode); 805fa7767c5Sopenharmony_ci } 806fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::ERROR_UNIMPLEMENTED); 807fa7767c5Sopenharmony_ci} 808fa7767c5Sopenharmony_ci 809fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetObs(const std::weak_ptr<IPlayerEngineObs>& obs) 810fa7767c5Sopenharmony_ci{ 811fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetObs entered."); 812fa7767c5Sopenharmony_ci callbackLooper_.StartWithPlayerEngineObs(obs); 813fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 814fa7767c5Sopenharmony_ci} 815fa7767c5Sopenharmony_ci 816fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::Reset() 817fa7767c5Sopenharmony_ci{ 818fa7767c5Sopenharmony_ci MEDIA_LOG_I("Reset entered."); 819fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_STOPPED) { 820fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 821fa7767c5Sopenharmony_ci } 822fa7767c5Sopenharmony_ci singleLoop_ = false; 823fa7767c5Sopenharmony_ci mediaStats_.Reset(); 824fa7767c5Sopenharmony_ci auto ret = DoReset(); 825fa7767c5Sopenharmony_ci OnStateChanged(StateId::STOPPED); 826fa7767c5Sopenharmony_ci return TransErrorCode(ret); 827fa7767c5Sopenharmony_ci} 828fa7767c5Sopenharmony_ci 829fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::GetCurrentTime(int32_t& currentPositionMs) 830fa7767c5Sopenharmony_ci{ 831fa7767c5Sopenharmony_ci currentPositionMs = Plugin::HstTime2Ms(syncManager_->GetMediaTimeNow()); 832fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 833fa7767c5Sopenharmony_ci} 834fa7767c5Sopenharmony_ci 835fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::GetDuration(int32_t& durationMs) 836fa7767c5Sopenharmony_ci{ 837fa7767c5Sopenharmony_ci durationMs = 0; 838fa7767c5Sopenharmony_ci if (pipelineStates_ == PlayerStates::PLAYER_IDLE || pipelineStates_ == PlayerStates::PLAYER_PREPARING || 839fa7767c5Sopenharmony_ci audioSource_ == nullptr) { 840fa7767c5Sopenharmony_ci MEDIA_LOG_E("GetDuration, invalid state or audioSource_ is null. state: " PUBLIC_LOG_S, 841fa7767c5Sopenharmony_ci StringnessPlayerState(pipelineStates_).c_str()); 842fa7767c5Sopenharmony_ci return MSERR_INVALID_STATE; 843fa7767c5Sopenharmony_ci } 844fa7767c5Sopenharmony_ci if (duration_ < 0) { 845fa7767c5Sopenharmony_ci durationMs = -1; 846fa7767c5Sopenharmony_ci MEDIA_LOG_W("no valid duration"); 847fa7767c5Sopenharmony_ci return MSERR_UNKNOWN; 848fa7767c5Sopenharmony_ci } 849fa7767c5Sopenharmony_ci durationMs = Plugin::HstTime2Ms(duration_); 850fa7767c5Sopenharmony_ci MEDIA_LOG_DD("GetDuration returned " PUBLIC_LOG_D32, durationMs); 851fa7767c5Sopenharmony_ci return MSERR_OK; 852fa7767c5Sopenharmony_ci} 853fa7767c5Sopenharmony_ci 854fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetPlaybackSpeed(PlaybackRateMode mode) 855fa7767c5Sopenharmony_ci{ 856fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetPlaybackSpeed entered."); 857fa7767c5Sopenharmony_ci double playbackSpeed = ChangeModeToSpeed(mode); 858fa7767c5Sopenharmony_ci demuxer_->SetParameter(static_cast<int32_t>(Plugin::Tag::MEDIA_PLAYBACK_SPEED), playbackSpeed); 859fa7767c5Sopenharmony_ci Format format; 860fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_SPEEDDONE, 0, format); 861fa7767c5Sopenharmony_ci 862fa7767c5Sopenharmony_ci int32_t currentPosMs = 0; 863fa7767c5Sopenharmony_ci int32_t durationMs = 0; 864fa7767c5Sopenharmony_ci NZERO_RETURN(GetDuration(durationMs)); 865fa7767c5Sopenharmony_ci NZERO_RETURN(GetCurrentTime(currentPosMs)); 866fa7767c5Sopenharmony_ci currentPosMs = std::min(currentPosMs, durationMs); 867fa7767c5Sopenharmony_ci currentPosMs = currentPosMs < 0 ? 0 : currentPosMs; 868fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_POSITION_UPDATE, currentPosMs, format); 869fa7767c5Sopenharmony_ci MEDIA_LOG_D("SetPlaybackSpeed entered end."); 870fa7767c5Sopenharmony_ci return MSERR_OK; 871fa7767c5Sopenharmony_ci} 872fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::GetPlaybackSpeed(PlaybackRateMode& mode) 873fa7767c5Sopenharmony_ci{ 874fa7767c5Sopenharmony_ci MEDIA_LOG_I("GetPlaybackSpeed entered."); 875fa7767c5Sopenharmony_ci Plugin::Any any; 876fa7767c5Sopenharmony_ci demuxer_->GetParameter(static_cast<int32_t>(Plugin::Tag::MEDIA_PLAYBACK_SPEED), any); 877fa7767c5Sopenharmony_ci auto playbackSpeed = Plugin::AnyCast<double>(any); 878fa7767c5Sopenharmony_ci mode = ChangeSpeedToMode(playbackSpeed); 879fa7767c5Sopenharmony_ci return MSERR_OK; 880fa7767c5Sopenharmony_ci} 881fa7767c5Sopenharmony_ci 882fa7767c5Sopenharmony_civoid HiPlayerImpl::OnStateChanged(StateId state) 883fa7767c5Sopenharmony_ci{ 884fa7767c5Sopenharmony_ci MEDIA_LOG_I("OnStateChanged from " PUBLIC_LOG_D32 " to " PUBLIC_LOG_D32, pipelineStates_.load(), 885fa7767c5Sopenharmony_ci TransStateId2PlayerState(state)); 886fa7767c5Sopenharmony_ci UpdateStateNoLock(TransStateId2PlayerState(state)); 887fa7767c5Sopenharmony_ci { 888fa7767c5Sopenharmony_ci OSAL::ScopedLock lock(stateMutex_); 889fa7767c5Sopenharmony_ci cond_.NotifyOne(); 890fa7767c5Sopenharmony_ci } 891fa7767c5Sopenharmony_ci} 892fa7767c5Sopenharmony_ci 893fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::OnCallback(const FilterCallbackType& type, Filter* filter, const Plugin::Any& parameter) 894fa7767c5Sopenharmony_ci{ 895fa7767c5Sopenharmony_ci ErrorCode ret = ErrorCode::SUCCESS; 896fa7767c5Sopenharmony_ci switch (type) { 897fa7767c5Sopenharmony_ci case FilterCallbackType::PORT_ADDED: 898fa7767c5Sopenharmony_ci ret = NewAudioPortFound(filter, parameter); 899fa7767c5Sopenharmony_ci if (ret != ErrorCode::SUCCESS) { 900fa7767c5Sopenharmony_ci return ret; 901fa7767c5Sopenharmony_ci } 902fa7767c5Sopenharmony_ci#ifdef VIDEO_SUPPORT 903fa7767c5Sopenharmony_ci ret = NewVideoPortFound(filter, parameter); 904fa7767c5Sopenharmony_ci#endif 905fa7767c5Sopenharmony_ci break; 906fa7767c5Sopenharmony_ci case FilterCallbackType::PORT_REMOVE: 907fa7767c5Sopenharmony_ci ret = RemoveFilterChains(filter, parameter); 908fa7767c5Sopenharmony_ci break; 909fa7767c5Sopenharmony_ci default: 910fa7767c5Sopenharmony_ci break; 911fa7767c5Sopenharmony_ci } 912fa7767c5Sopenharmony_ci return ret; 913fa7767c5Sopenharmony_ci} 914fa7767c5Sopenharmony_ci 915fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::NewAudioPortFound(Filter* filter, const Plugin::Any& parameter) 916fa7767c5Sopenharmony_ci{ 917fa7767c5Sopenharmony_ci if (!Plugin::Any::IsSameTypeWith<PortInfo>(parameter)) { 918fa7767c5Sopenharmony_ci return ErrorCode::ERROR_INVALID_PARAMETER_TYPE; 919fa7767c5Sopenharmony_ci } 920fa7767c5Sopenharmony_ci ErrorCode rtv = ErrorCode::ERROR_INVALID_PARAMETER_VALUE; 921fa7767c5Sopenharmony_ci auto param = Plugin::AnyCast<PortInfo>(parameter); 922fa7767c5Sopenharmony_ci if (filter == demuxer_.get() && param.type == PortType::OUT) { 923fa7767c5Sopenharmony_ci MEDIA_LOG_I("new port found on demuxer " PUBLIC_LOG_ZU, param.ports.size()); 924fa7767c5Sopenharmony_ci for (const auto& portDesc : param.ports) { 925fa7767c5Sopenharmony_ci if (portDesc.name.compare(0, 5, "audio") != 0) { // 5 is length of "audio" 926fa7767c5Sopenharmony_ci continue; 927fa7767c5Sopenharmony_ci } 928fa7767c5Sopenharmony_ci MEDIA_LOG_I("port name " PUBLIC_LOG_S, portDesc.name.c_str()); 929fa7767c5Sopenharmony_ci auto fromPort = filter->GetOutPort(portDesc.name); 930fa7767c5Sopenharmony_ci if (portDesc.isPcm) { 931fa7767c5Sopenharmony_ci pipeline_->AddFilters({audioSink_.get()}); 932fa7767c5Sopenharmony_ci FAIL_LOG(pipeline_->LinkPorts(fromPort, audioSink_->GetInPort(PORT_NAME_DEFAULT))); 933fa7767c5Sopenharmony_ci ActiveFilters({audioSink_.get()}); 934fa7767c5Sopenharmony_ci } else { 935fa7767c5Sopenharmony_ci auto newAudioDecoder = CreateAudioDecoder(portDesc.name); 936fa7767c5Sopenharmony_ci pipeline_->AddFilters({newAudioDecoder.get(), audioSink_.get()}); 937fa7767c5Sopenharmony_ci FAIL_LOG(pipeline_->LinkPorts(fromPort, newAudioDecoder->GetInPort(PORT_NAME_DEFAULT))); 938fa7767c5Sopenharmony_ci FAIL_LOG(pipeline_->LinkPorts(newAudioDecoder->GetOutPort(PORT_NAME_DEFAULT), 939fa7767c5Sopenharmony_ci audioSink_->GetInPort(PORT_NAME_DEFAULT))); 940fa7767c5Sopenharmony_ci ActiveFilters({newAudioDecoder.get(), audioSink_.get()}); 941fa7767c5Sopenharmony_ci } 942fa7767c5Sopenharmony_ci mediaStats_.Append(audioSink_->GetName()); 943fa7767c5Sopenharmony_ci rtv = ErrorCode::SUCCESS; 944fa7767c5Sopenharmony_ci break; 945fa7767c5Sopenharmony_ci } 946fa7767c5Sopenharmony_ci } 947fa7767c5Sopenharmony_ci return rtv; 948fa7767c5Sopenharmony_ci} 949fa7767c5Sopenharmony_ci 950fa7767c5Sopenharmony_ci#ifdef VIDEO_SUPPORT 951fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::NewVideoPortFound(Filter* filter, const Plugin::Any& parameter) 952fa7767c5Sopenharmony_ci{ 953fa7767c5Sopenharmony_ci if (!Plugin::Any::IsSameTypeWith<PortInfo>(parameter)) { 954fa7767c5Sopenharmony_ci return ErrorCode::ERROR_INVALID_PARAMETER_TYPE; 955fa7767c5Sopenharmony_ci } 956fa7767c5Sopenharmony_ci auto param = Plugin::AnyCast<PortInfo>(parameter); 957fa7767c5Sopenharmony_ci if (filter != demuxer_.get() || param.type != PortType::OUT) { 958fa7767c5Sopenharmony_ci return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; 959fa7767c5Sopenharmony_ci } 960fa7767c5Sopenharmony_ci std::vector<Filter*> newFilters; 961fa7767c5Sopenharmony_ci for (const auto& portDesc : param.ports) { 962fa7767c5Sopenharmony_ci if (portDesc.name.compare(0, 5, "video") == 0) { // 5 is length of "video" 963fa7767c5Sopenharmony_ci MEDIA_LOG_I("port name " PUBLIC_LOG_S, portDesc.name.c_str()); 964fa7767c5Sopenharmony_ci videoDecoder_ = FilterFactory::Instance().CreateFilterWithType<VideoDecoderFilter>( 965fa7767c5Sopenharmony_ci "builtin.player.videodecoder", "videodecoder-" + portDesc.name); 966fa7767c5Sopenharmony_ci if (pipeline_->AddFilters({videoDecoder_.get()}) == ErrorCode::SUCCESS) { 967fa7767c5Sopenharmony_ci // link demuxer and video decoder 968fa7767c5Sopenharmony_ci auto fromPort = filter->GetOutPort(portDesc.name); 969fa7767c5Sopenharmony_ci auto toPort = videoDecoder_->GetInPort(PORT_NAME_DEFAULT); 970fa7767c5Sopenharmony_ci FAIL_LOG(pipeline_->LinkPorts(fromPort, toPort)); // link ports 971fa7767c5Sopenharmony_ci newFilters.emplace_back(videoDecoder_.get()); 972fa7767c5Sopenharmony_ci 973fa7767c5Sopenharmony_ci // link video decoder and video sink 974fa7767c5Sopenharmony_ci if (pipeline_->AddFilters({videoSink_.get()}) == ErrorCode::SUCCESS) { 975fa7767c5Sopenharmony_ci fromPort = videoDecoder_->GetOutPort(PORT_NAME_DEFAULT); 976fa7767c5Sopenharmony_ci toPort = videoSink_->GetInPort(PORT_NAME_DEFAULT); 977fa7767c5Sopenharmony_ci FAIL_LOG(pipeline_->LinkPorts(fromPort, toPort)); // link ports 978fa7767c5Sopenharmony_ci newFilters.push_back(videoSink_.get()); 979fa7767c5Sopenharmony_ci mediaStats_.Append(videoSink_->GetName()); 980fa7767c5Sopenharmony_ci } 981fa7767c5Sopenharmony_ci } 982fa7767c5Sopenharmony_ci break; 983fa7767c5Sopenharmony_ci } 984fa7767c5Sopenharmony_ci } 985fa7767c5Sopenharmony_ci if (!newFilters.empty()) { 986fa7767c5Sopenharmony_ci ActiveFilters(newFilters); 987fa7767c5Sopenharmony_ci } 988fa7767c5Sopenharmony_ci return ErrorCode::SUCCESS; 989fa7767c5Sopenharmony_ci} 990fa7767c5Sopenharmony_ci#endif 991fa7767c5Sopenharmony_ci 992fa7767c5Sopenharmony_ciErrorCode HiPlayerImpl::RemoveFilterChains(Filter* filter, const Plugin::Any& parameter) 993fa7767c5Sopenharmony_ci{ 994fa7767c5Sopenharmony_ci ErrorCode ret = ErrorCode::SUCCESS; 995fa7767c5Sopenharmony_ci auto param = Plugin::AnyCast<PortInfo>(parameter); 996fa7767c5Sopenharmony_ci if (filter != demuxer_.get() || param.type != PortType::OUT) { 997fa7767c5Sopenharmony_ci return ret; 998fa7767c5Sopenharmony_ci } 999fa7767c5Sopenharmony_ci for (const auto& portDesc : param.ports) { 1000fa7767c5Sopenharmony_ci MEDIA_LOG_I("remove filter chain for port: " PUBLIC_LOG_S, portDesc.name.c_str()); 1001fa7767c5Sopenharmony_ci auto peerPort = filter->GetOutPort(portDesc.name)->GetPeerPort(); 1002fa7767c5Sopenharmony_ci if (peerPort) { 1003fa7767c5Sopenharmony_ci auto nextFilter = const_cast<Filter*>(reinterpret_cast<const Filter*>(peerPort->GetOwnerFilter())); 1004fa7767c5Sopenharmony_ci if (nextFilter) { 1005fa7767c5Sopenharmony_ci pipeline_->RemoveFilterChain(nextFilter); 1006fa7767c5Sopenharmony_ci } 1007fa7767c5Sopenharmony_ci } 1008fa7767c5Sopenharmony_ci } 1009fa7767c5Sopenharmony_ci return ret; 1010fa7767c5Sopenharmony_ci} 1011fa7767c5Sopenharmony_ci 1012fa7767c5Sopenharmony_civoid HiPlayerImpl::ActiveFilters(const std::vector<Filter*>& filters) 1013fa7767c5Sopenharmony_ci{ 1014fa7767c5Sopenharmony_ci for (auto it = filters.rbegin(); it != filters.rend(); ++it) { 1015fa7767c5Sopenharmony_ci (*it)->Prepare(); 1016fa7767c5Sopenharmony_ci } 1017fa7767c5Sopenharmony_ci} 1018fa7767c5Sopenharmony_ci 1019fa7767c5Sopenharmony_cidouble HiPlayerImpl::ChangeModeToSpeed(const PlaybackRateMode& mode) const 1020fa7767c5Sopenharmony_ci{ 1021fa7767c5Sopenharmony_ci switch (mode) { 1022fa7767c5Sopenharmony_ci case SPEED_FORWARD_0_75_X: 1023fa7767c5Sopenharmony_ci return SPEED_0_75_X; 1024fa7767c5Sopenharmony_ci case SPEED_FORWARD_1_00_X: 1025fa7767c5Sopenharmony_ci return SPEED_1_00_X; 1026fa7767c5Sopenharmony_ci case SPEED_FORWARD_1_25_X: 1027fa7767c5Sopenharmony_ci return SPEED_1_25_X; 1028fa7767c5Sopenharmony_ci case SPEED_FORWARD_1_75_X: 1029fa7767c5Sopenharmony_ci return SPEED_1_75_X; 1030fa7767c5Sopenharmony_ci case SPEED_FORWARD_2_00_X: 1031fa7767c5Sopenharmony_ci return SPEED_2_00_X; 1032fa7767c5Sopenharmony_ci default: 1033fa7767c5Sopenharmony_ci MEDIA_LOG_I("unknown mode:" PUBLIC_LOG_D32 ", return default speed(SPEED_1_00_X)", mode); 1034fa7767c5Sopenharmony_ci } 1035fa7767c5Sopenharmony_ci return SPEED_1_00_X; 1036fa7767c5Sopenharmony_ci} 1037fa7767c5Sopenharmony_ci 1038fa7767c5Sopenharmony_ciPlaybackRateMode HiPlayerImpl::ChangeSpeedToMode(double rate) const 1039fa7767c5Sopenharmony_ci{ 1040fa7767c5Sopenharmony_ci if (abs(rate - SPEED_0_75_X) < EPSINON) { 1041fa7767c5Sopenharmony_ci return SPEED_FORWARD_0_75_X; 1042fa7767c5Sopenharmony_ci } 1043fa7767c5Sopenharmony_ci if (abs(rate - SPEED_1_00_X) < EPSINON) { 1044fa7767c5Sopenharmony_ci return SPEED_FORWARD_1_00_X; 1045fa7767c5Sopenharmony_ci } 1046fa7767c5Sopenharmony_ci if (abs(rate - SPEED_1_25_X) < EPSINON) { 1047fa7767c5Sopenharmony_ci return SPEED_FORWARD_1_25_X; 1048fa7767c5Sopenharmony_ci } 1049fa7767c5Sopenharmony_ci if (abs(rate - SPEED_1_75_X) < EPSINON) { 1050fa7767c5Sopenharmony_ci return SPEED_FORWARD_1_75_X; 1051fa7767c5Sopenharmony_ci } 1052fa7767c5Sopenharmony_ci if (abs(rate - SPEED_2_00_X) < EPSINON) { 1053fa7767c5Sopenharmony_ci return SPEED_FORWARD_2_00_X; 1054fa7767c5Sopenharmony_ci } 1055fa7767c5Sopenharmony_ci MEDIA_LOG_I("unknown rate:" PUBLIC_LOG_F ", return default speed(SPEED_FORWARD_1_00_X)", rate); 1056fa7767c5Sopenharmony_ci return SPEED_FORWARD_1_00_X; 1057fa7767c5Sopenharmony_ci} 1058fa7767c5Sopenharmony_ci 1059fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetVideoScaleType(VideoScaleType videoScaleType) 1060fa7767c5Sopenharmony_ci{ 1061fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetVideoScaleType entered."); 1062fa7767c5Sopenharmony_ci#ifdef VIDEO_SUPPORT 1063fa7767c5Sopenharmony_ci auto ret = videoSink_->SetParameter(static_cast<int32_t>(Tag::VIDEO_SCALE_TYPE), 1064fa7767c5Sopenharmony_ci static_cast<Plugin::VideoScaleType>(static_cast<uint32_t>(videoScaleType))); 1065fa7767c5Sopenharmony_ci return TransErrorCode(ret); 1066fa7767c5Sopenharmony_ci#else 1067fa7767c5Sopenharmony_ci return TransErrorCode(ErrorCode::SUCCESS); 1068fa7767c5Sopenharmony_ci#endif 1069fa7767c5Sopenharmony_ci} 1070fa7767c5Sopenharmony_ci 1071fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetAudioRendererInfo(const int32_t contentType, const int32_t streamUsage, 1072fa7767c5Sopenharmony_ci const int32_t rendererFlag) 1073fa7767c5Sopenharmony_ci{ 1074fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetAudioRendererInfo entered."); 1075fa7767c5Sopenharmony_ci Plugin::AudioRenderInfo audioRenderInfo {contentType, streamUsage, rendererFlag}; 1076fa7767c5Sopenharmony_ci auto ret = audioSink_->SetParameter(static_cast<int32_t>(Tag::AUDIO_RENDER_INFO), audioRenderInfo); 1077fa7767c5Sopenharmony_ci return TransErrorCode(ret); 1078fa7767c5Sopenharmony_ci} 1079fa7767c5Sopenharmony_ci 1080fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SetAudioInterruptMode(const int32_t interruptMode) 1081fa7767c5Sopenharmony_ci{ 1082fa7767c5Sopenharmony_ci MEDIA_LOG_I("SetAudioInterruptMode entered."); 1083fa7767c5Sopenharmony_ci auto ret = audioSink_->SetParameter(static_cast<int32_t>(Tag::AUDIO_INTERRUPT_MODE), interruptMode); 1084fa7767c5Sopenharmony_ci return TransErrorCode(ret); 1085fa7767c5Sopenharmony_ci} 1086fa7767c5Sopenharmony_ci 1087fa7767c5Sopenharmony_ciint32_t HiPlayerImpl::SelectBitRate(uint32_t bitRate) 1088fa7767c5Sopenharmony_ci{ 1089fa7767c5Sopenharmony_ci int64_t mBitRate = static_cast<int64_t>(bitRate); 1090fa7767c5Sopenharmony_ci pipeline_->FlushStart(); 1091fa7767c5Sopenharmony_ci auto ret = audioSource_->SelectBitRate(mBitRate); 1092fa7767c5Sopenharmony_ci pipeline_->FlushEnd(); 1093fa7767c5Sopenharmony_ci return TransErrorCode(ret); 1094fa7767c5Sopenharmony_ci} 1095fa7767c5Sopenharmony_ci 1096fa7767c5Sopenharmony_civoid HiPlayerImpl::NotifyBufferingUpdate(const std::string_view& type, int32_t param) 1097fa7767c5Sopenharmony_ci{ 1098fa7767c5Sopenharmony_ci Format format; 1099fa7767c5Sopenharmony_ci format.PutIntValue(std::string(type), param); 1100fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_BUFFERING_UPDATE, 0, format); 1101fa7767c5Sopenharmony_ci} 1102fa7767c5Sopenharmony_ci 1103fa7767c5Sopenharmony_civoid HiPlayerImpl::HandleResolutionChangeEvent(const Event& event) 1104fa7767c5Sopenharmony_ci{ 1105fa7767c5Sopenharmony_ci auto resolution = Plugin::AnyCast<std::pair<int32_t, int32_t>>(event.param); 1106fa7767c5Sopenharmony_ci Format format; 1107fa7767c5Sopenharmony_ci (void)format.PutIntValue(PlayerKeys::PLAYER_WIDTH, resolution.first); 1108fa7767c5Sopenharmony_ci (void)format.PutIntValue(PlayerKeys::PLAYER_HEIGHT, resolution.second); 1109fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_RESOLUTION_CHANGE, 0, format); 1110fa7767c5Sopenharmony_ci MEDIA_LOG_I("Receive plugin RESOLUTION_CHANGE, video_width: " PUBLIC_LOG_U32 1111fa7767c5Sopenharmony_ci ", video_height: " PUBLIC_LOG_U32, resolution.first, resolution.second); 1112fa7767c5Sopenharmony_ci videoWidth_ = resolution.first; 1113fa7767c5Sopenharmony_ci videoHeight_ = resolution.second; 1114fa7767c5Sopenharmony_ci} 1115fa7767c5Sopenharmony_ci 1116fa7767c5Sopenharmony_civoid HiPlayerImpl::HandlePluginEvent(const Event& event) 1117fa7767c5Sopenharmony_ci{ 1118fa7767c5Sopenharmony_ci auto pluginEvent = Plugin::AnyCast<Plugin::PluginEvent>(event.param); 1119fa7767c5Sopenharmony_ci switch (pluginEvent.type) { 1120fa7767c5Sopenharmony_ci case Plugin::PluginEventType::AUDIO_INTERRUPT: { 1121fa7767c5Sopenharmony_ci auto interruptEvent = Plugin::AnyCast<AudioStandard::InterruptEvent>(pluginEvent.param); 1122fa7767c5Sopenharmony_ci MEDIA_LOG_I("Receive Audio AUDIO_INTERRUPT EVENT, eventType: " PUBLIC_LOG_U32 1123fa7767c5Sopenharmony_ci ", forceType: " PUBLIC_LOG_U32 ", hintType: " PUBLIC_LOG_U32, 1124fa7767c5Sopenharmony_ci interruptEvent.eventType, interruptEvent.forceType, interruptEvent.hintType); 1125fa7767c5Sopenharmony_ci Format format; 1126fa7767c5Sopenharmony_ci (void)format.PutIntValue(PlayerKeys::AUDIO_INTERRUPT_TYPE, interruptEvent.eventType); 1127fa7767c5Sopenharmony_ci (void)format.PutIntValue(PlayerKeys::AUDIO_INTERRUPT_FORCE, interruptEvent.forceType); 1128fa7767c5Sopenharmony_ci (void)format.PutIntValue(PlayerKeys::AUDIO_INTERRUPT_HINT, interruptEvent.hintType); 1129fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_INTERRUPT_EVENT, 0, format); 1130fa7767c5Sopenharmony_ci break; 1131fa7767c5Sopenharmony_ci } 1132fa7767c5Sopenharmony_ci case Plugin::PluginEventType::AUDIO_STATE_CHANGE: { 1133fa7767c5Sopenharmony_ci auto renderState = Plugin::AnyCast<AudioStandard::RendererState>(pluginEvent.param); 1134fa7767c5Sopenharmony_ci MEDIA_LOG_I("Receive Audio STATE_CHANGE EVENT, renderState: " PUBLIC_LOG_U32, 1135fa7767c5Sopenharmony_ci static_cast<uint32_t>(renderState)); 1136fa7767c5Sopenharmony_ci if (renderState == AudioStandard::RendererState::RENDERER_PAUSED) { 1137fa7767c5Sopenharmony_ci Format format; 1138fa7767c5Sopenharmony_ci callbackLooper_.OnInfo(INFO_TYPE_STATE_CHANGE_BY_AUDIO, PlayerStates::PLAYER_PAUSED, format); 1139fa7767c5Sopenharmony_ci } 1140fa7767c5Sopenharmony_ci break; 1141fa7767c5Sopenharmony_ci } 1142fa7767c5Sopenharmony_ci case Plugin::PluginEventType::BELOW_LOW_WATERLINE: 1143fa7767c5Sopenharmony_ci case Plugin::PluginEventType::ABOVE_LOW_WATERLINE: 1144fa7767c5Sopenharmony_ci default: 1145fa7767c5Sopenharmony_ci MEDIA_LOG_I("Receive PLUGIN_EVENT, type: " PUBLIC_LOG_D32, 1146fa7767c5Sopenharmony_ci CppExt::to_underlying(pluginEvent.type)); 1147fa7767c5Sopenharmony_ci break; 1148fa7767c5Sopenharmony_ci } 1149fa7767c5Sopenharmony_ci} 1150fa7767c5Sopenharmony_ci} // namespace Media 1151fa7767c5Sopenharmony_ci} // namespace OHOS 1152