当前位置:   article > 正文

OpenHarmony 3.2 Beta多媒体系列——音视频播放gstreamer_openharmony gstreamer

openharmony gstreamer

目录

一、 简介

二、 目录

三 、Gstreamer介绍

1. 简介

2.Gstreamer几个重要的概念

四、调用流程

五、源码分析

1. PrepareAsync分析

2. Play分析

六、总结


一、 简介

多媒体播放框架主要的实现在PlayerServer服务中,这个服务提供了媒体播放框架所需要的实现环境,继续跟踪代码分析发现,PlayerServer主要通过gstreamer适配层,对gstreamer进行调用。gstreamer属于更加具体的实现,所以本篇文章主要是分析PlayerServer通过适配层调用到gstreamer的过程。

此前,我在《OpenHarmony 3.2 Beta多媒体系列-音视频播放框架》一文中,主要分析了多媒体播放的框架层代码,本地接口通过服务端的proxy代理类进行IPC调用,最终调用到PlayerServer服务端。本篇主要分析了多媒体gstreamer的调用,涉及到从PlayerServer到gstreamer的整体流程。

二、 目录

  1. gstreamer
  2. ├── BUILD.gn
  3. ├── common
  4. │ ├── BUILD.gn
  5. │ ├── playbin_adapter
  6. │ │ ├── i_playbin_ctrler.h
  7. │ │ ├── playbin2_ctrler.cpp
  8. │ │ ├── playbin2_ctrler.h
  9. │ │ ├── playbin_ctrler_base.cpp
  10. │ │ ├── playbin_ctrler_base.h
  11. │ │ ├── playbin_msg_define.h
  12. │ │ ├── playbin_sink_provider.h
  13. │ │ ├── playbin_state.cpp
  14. │ │ ├── playbin_state.h
  15. │ │ ├── playbin_task_mgr.cpp
  16. │ │ └── playbin_task_mgr.h
  17. │ ├── state_machine
  18. │ │ ├── state_machine.cpp
  19. │ │ └── state_machine.h
  20. ├── factory
  21. │ ├── BUILD.gn
  22. │ └── engine_factory.cpp
  23. └── player
  24. ├── BUILD.gn
  25. ├── player_codec_ctrl.cpp
  26. ├── player_codec_ctrl.h
  27. ├── player_engine_gst_impl.cpp
  28. ├── player_engine_gst_impl.h
  29. ├── player_sinkprovider.cpp
  30. ├── player_sinkprovider.h
  31. ├── player_track_parse.cpp
  32. └── player_track_parse.h

目录主要是多媒体子系统中的engine部分,涉及到了gstreamer的适配层,gstreamer具体的实现是在third_party/gstreamer目录中。

三 、Gstreamer介绍

1. 简介

Gstreamer是一个跨平台的多媒体框架,应用程序可以通过管道(Pipeline)的方式,将多媒体处理的各个步骤串联起来,达到预期的效果。每个步骤通过元素(Element)基于GObject对象系统通过插件(plugins)的方式实现,方便了各项功能的扩展。

2.Gstreamer几个重要的概念

Element

Element是Gstreamer中最重要的对象类型之一。一个element实现一个功能(读取文件,解码,输出等),程序需要创建多个element,并按顺序将其串联起来,构成一个完整的Pipeline。

Pad

Pad是一个element的输入/输出接口,分为src pad(生产数据)和sink pad(消费数据)两种。

两个element必须通过pad才能连接起来,pad拥有当前element能处理数据类型的能力(capabilities),会在连接时通过比较src pad和sink pad中所支持的能力,来选择最恰当的数据类型用于传输,如果element不支持,程序会直接退出。在element通过pad连接成功后,数据会从上一个element的src pad传到下一个element的sink pad然后进行处理。

Bin和Pipeline

Bin是一个容器,用于管理多个element,改变bin的状态时,bin会自动去修改所包含的element的状态,也会转发所收到的消息。如果没有bin,我们需要依次操作我们所使用的element。通过bin降低了应用的复杂度。

Pipeline继承自bin,为程序提供一个bus用于传输消息,并且对所有子element进行同步。当将Pipeline的状态设置为PLAYING时,Pipeline会在一个/多个新的线程中通过element处理数据。

四、调用流程

五、源码分析

1. PrepareAsync分析

首先,在PlayerServer的PrepareAsync中会调用OnPrepare(false),具体是在OnPrepare(false)中实现,参数传入false,表明调用的是异步方法。

  1. int32_t PlayerServer::PrepareAsync()
  2. {
  3. std::lock_guard<std::mutex> lock(mutex_);
  4. MEDIA_LOGW("KPI-TRACE: PlayerServer PrepareAsync in");
  5. if (lastOpStatus_ == PLAYER_INITIALIZED || lastOpStatus_ == PLAYER_STOPPED) {
  6. return OnPrepare(false);
  7. } else {
  8. MEDIA_LOGE("Can not Prepare, currentState is %{public}s", GetStatusDescription(lastOpStatus_).c_str());
  9. return MSERR_INVALID_OPERATION;
  10. }
  11. }

OnPrepare方法中,先通过playerEngine_调用SerVideoSurface的方法,将surface_设置到PlayerEngineGstImpl中(producerSurface_),接着启动一个任务,调用目前状态的Prepare()方法。

  1. int32_t PlayerServer::OnPrepare(bool sync)
  2. {
  3. CHECK_AND_RETURN_RET_LOG(playerEngine_ != nullptr, MSERR_NO_MEMORY, "playerEngine_ is nullptr");
  4. int32_t ret = MSERR_OK;
  5. #ifdef SUPPORT_VIDEO
  6. if (surface_ != nullptr) {
  7. ret = playerEngine_->SetVideoSurface(surface_);
  8. CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "Engine SetVideoSurface Failed!");
  9. }
  10. #endif
  11. lastOpStatus_ = PLAYER_PREPARED;
  12. auto preparedTask = std::make_shared<TaskHandler<int32_t>>([this]() {
  13. MediaTrace::TraceBegin("PlayerServer::PrepareAsync", FAKE_POINTER(this));
  14. auto currState = std::static_pointer_cast<BaseState>(GetCurrState());
  15. return currState->Prepare();
  16. });
  17. ret = taskMgr_.LaunchTask(preparedTask, PlayerServerTaskType::STATE_CHANGE);
  18. CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "Prepare launch task failed");
  19. if (sync) {
  20. (void)preparedTask->GetResult(); // wait HandlePrpare
  21. }
  22. return MSERR_OK;
  23. }

进入Preparing状态后,会触发PlayerServer的HandlePrepare()方法被调用,在这个方法里会通过playerEngine_调用PrepareAsync方法,这个方法调用的是PlayerEngineGstImpl对应的PrepareAsync方法。

  1. int32_t PlayerServer::HandlePrepare()
  2. {
  3. int32_t ret = playerEngine_->PrepareAsync();
  4. CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "Server Prepare Failed!");
  5. if (config_.leftVolume <= 1.0f || config_.rightVolume <= 1.0f) {
  6. ret = playerEngine_->SetVolume(config_.leftVolume, config_.rightVolume);
  7. MEDIA_LOGD("Prepared SetVolume leftVolume:%{public}f rightVolume:%{public}f, ret:%{public}d", \
  8. config_.leftVolume, config_.rightVolume, ret);
  9. }
  10. (void)playerEngine_->SetLooping(config_.looping);
  11. {
  12. auto rateTask = std::make_shared<TaskHandler<void>>([this]() {
  13. auto currState = std::static_pointer_cast<BaseState>(GetCurrState());
  14. (void)currState->SetPlaybackSpeed(config_.speedMode);
  15. });
  16. (void)taskMgr_.LaunchTask(rateTask, PlayerServerTaskType::RATE_CHANGE);
  17. }
  18. return MSERR_OK;
  19. }

首先初始化playBinCtrler_,后续的操作都是通过PlayBinCtrlerBase对象来操作的,所以PlayBinCtrlerInit()方法会创建PlayBinCtrlerBase对象(playBinCtrler_),创建好以后通过playBinCtrler_进行SetSource和SetXXXListener的设置。

  1. int32_t PlayerEngineGstImpl::PrepareAsync()
  2. {
  3. std::unique_lock<std::mutex> lock(mutex_);
  4. MEDIA_LOGD("Prepare in");
  5. int32_t ret = PlayBinCtrlerInit();
  6. CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_VAL, "PlayBinCtrlerInit failed");
  7. CHECK_AND_RETURN_RET_LOG(playBinCtrler_ != nullptr, MSERR_INVALID_VAL, "playBinCtrler_ is nullptr");
  8. ret = playBinCtrler_->PrepareAsync();
  9. CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "PrepareAsync failed");
  10. // The duration of some resources without header information cannot be obtained.
  11. MEDIA_LOGD("Prepared ok out");
  12. return MSERR_OK;
  13. }

初始化完成以后,接下来进行playBinCtrler_的PrepareAsync的调用,PlayBinCtrlerBase中的PrepareAsync的方法间接地调用了PrepareAsyncInternal。

  1. int32_t PlayBinCtrlerBase::PrepareAsync()
  2. {
  3. MEDIA_LOGD("enter");
  4. std::unique_lock<std::mutex> lock(mutex_);
  5. return PrepareAsyncInternal();
  6. }

PrepareAsyncInternal首先判断当前的状态,如果是preparingState或preparedState,那么就直接返回成功,否则继续向下调用。接下来会调用EnterInitializedState(),这个方法中会创建playbin,设置signal的回调以及gstreamer参数的设置。最后调用目前状态的Prepare方法,此时的状态是InitializedState。

  1. int32_t PlayBinCtrlerBase::PrepareAsyncInternal()
  2. {
  3. if ((GetCurrState() == preparingState_) || (GetCurrState() == preparedState_)) {
  4. MEDIA_LOGI("already at preparing state, skip");
  5. return MSERR_OK;
  6. }
  7. CHECK_AND_RETURN_RET_LOG((!uri_.empty() || appsrcWrap_), MSERR_INVALID_OPERATION, "Set uri firsty!");
  8. int32_t ret = EnterInitializedState();
  9. CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
  10. auto currState = std::static_pointer_cast<BaseState>(GetCurrState());
  11. ret = currState->Prepare();
  12. CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "PrepareAsyncInternal failed");
  13. return MSERR_OK;
  14. }

InitializedState的Prepare方法又通过ctrler_调回到PlayBinCtrlerBase的ChangeState方法,这个方法是在PlayBinCtrlerBase的父类StateMachine中,它是一个状态机,管理着各种状态的切换。

  1. int32_t PlayBinCtrlerBase::InitializedState::Prepare()
  2. {
  3. ctrler_.ChangeState(ctrler_.preparingState_);
  4. return MSERR_OK;
  5. }

很多表示状态的类在PlayBinCtrlerBase中进行声明,这些子类的具体实现功能在playbin_state.cpp中。

  1. private:
  2. class BaseState;
  3. class IdleState;
  4. class InitializedState;
  5. class PreparingState;
  6. class PreparedState;
  7. class PlayingState;
  8. class PausedState;
  9. class StoppedState;
  10. class StoppingState;
  11. class PlaybackCompletedState;

接下来看一下状态机的ChangeState方法,可以看出切换状态的时候,先调用切换前状态的StateExit()方法,再调用切换后状态的StateEnter()。如果需要一些操作,我们可以在状态的StateEnter和StateExit中进行。

  1. void StateMachine::ChangeState(const std::shared_ptr<State> &state)
  2. {
  3. ......
  4. if (currState_ != nullptr && currState_->GetStateName() == "stopping_state" && state->GetStateName() != "stopped_state") {
  5. return;
  6. }
  7. if (currState_) {
  8. currState_->StateExit();
  9. }
  10. currState_ = state;
  11. state->StateEnter();
  12. }

因为上面切换状态调用的是ctrler_.ChangeState(ctrler_.preparingState_),所以接下来看一下PreparingState状态的StateEnter方法。这个方法中首先是调用了ctrler_.ReportMessage(msg),字面上看是用来上报msg信息的。

  1. void PlayBinCtrlerBase::PreparingState::StateEnter()
  2. {
  3. PlayBinMessage msg = { PLAYBIN_MSG_SUBTYPE, PLAYBIN_SUB_MSG_BUFFERING_START, 0, {} };
  4. ctrler_.ReportMessage(msg);
  5. GstStateChangeReturn ret;
  6. (void)ChangePlayBinState(GST_STATE_PAUSED, ret);
  7. MEDIA_LOGD("PreparingState::StateEnter finished");
  8. }

ctrler_是PlayBinCtrlerBase类型的变量,直接看PlayBinCtrlerBase的ReportMessage方法,这个方法的核心,是创建一个任务后,将任务放入消息队列中,等待消息被处理,这里我们最想知道的是这个消息会在什么地方被处理。msgReportHandler创建了TaskHandler,这个里面会调用notifier_(msg),这里的notifier_比较重要,我们可以顺着这个变量向上分析。

  1. void PlayBinCtrlerBase::ReportMessage(const PlayBinMessage &msg)
  2. {
  3. ......
  4. auto msgReportHandler = std::make_shared<TaskHandler<void>>([this, msg]() { notifier_(msg); });
  5. int32_t ret = msgQueue_->EnqueueTask(msgReportHandler);
  6. if (ret != MSERR_OK) {
  7. MEDIA_LOGE("async report msg failed, type: %{public}d, subType: %{public}d, code: %{public}d",
  8. msg.type, msg.subType, msg.code);
  9. };
  10. if (msg.type == PlayBinMsgType::PLAYBIN_MSG_EOS) {
  11. ProcessEndOfStream();
  12. }
  13. }

notifier_是在PlayBinCtrlerBase被创建的时候赋值的。

  1. PlayBinCtrlerBase::PlayBinCtrlerBase(const PlayBinCreateParam &createParam)
  2. : renderMode_(createParam.renderMode),
  3. notifier_(createParam.notifier),
  4. sinkProvider_(createParam.sinkProvider)
  5. {
  6. MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR "", FAKE_POINTER(this));
  7. }

在源码分析的前期PlayerEngineGstImpl初始化PlayBinCtrlerBase的时候进行了创建notifier = std::bind(&PlayerEngineGstImpl::OnNotifyMessage, this, std::placeholders::_1) notifier相当于是调用了PlayerEngineGstImpl::OnNotifyMessage方法。所以上述中的处理函数就是PlayerEngineGstImpl::OnNotifyMessage。

  1. int32_t PlayerEngineGstImpl::PlayBinCtrlerPrepare()
  2. {
  3. uint8_t renderMode = IPlayBinCtrler::PlayBinRenderMode::DEFAULT_RENDER;
  4. auto notifier = std::bind(&PlayerEngineGstImpl::OnNotifyMessage, this, std::placeholders::_1);
  5. {
  6. std::unique_lock<std::mutex> lk(trackParseMutex_);
  7. sinkProvider_ = std::make_shared<PlayerSinkProvider>(producerSurface_);
  8. sinkProvider_->SetAppInfo(appuid_, apppid_);
  9. }
  10. IPlayBinCtrler::PlayBinCreateParam createParam = {
  11. static_cast<IPlayBinCtrler::PlayBinRenderMode>(renderMode), notifier, sinkProvider_
  12. };
  13. playBinCtrler_ = IPlayBinCtrler::Create(IPlayBinCtrler::PlayBinKind::PLAYBIN2, createParam);
  14. ......
  15. return MSERR_OK;
  16. }

在OnNotifyMessage中指定了各种消息类型对应的执行函数,上述代码中创建的Message类型是PLAYBIN_MSG_SUBTYPE,子类型为PLAYBIN_SUB_MSG_BUFFERING_START。

  1. void PlayerEngineGstImpl::OnNotifyMessage(const PlayBinMessage &msg)
  2. {
  3. const std::unordered_map<int32_t, MsgNotifyFunc> MSG_NOTIFY_FUNC_TABLE = {
  4. { PLAYBIN_MSG_ERROR, std::bind(&PlayerEngineGstImpl::HandleErrorMessage, this, std::placeholders::_1) },
  5. { PLAYBIN_MSG_SEEKDONE, std::bind(&PlayerEngineGstImpl::HandleSeekDoneMessage, this, std::placeholders::_1) },
  6. { PLAYBIN_MSG_SPEEDDONE, std::bind(&PlayerEngineGstImpl::HandleInfoMessage, this, std::placeholders::_1) },
  7. { PLAYBIN_MSG_BITRATEDONE, std::bind(&PlayerEngineGstImpl::HandleInfoMessage, this, std::placeholders::_1)},
  8. { PLAYBIN_MSG_EOS, std::bind(&PlayerEngineGstImpl::HandleInfoMessage, this, std::placeholders::_1) },
  9. { PLAYBIN_MSG_STATE_CHANGE, std::bind(&PlayerEngineGstImpl::HandleInfoMessage, this, std::placeholders::_1) },
  10. { PLAYBIN_MSG_SUBTYPE, std::bind(&PlayerEngineGstImpl::HandleSubTypeMessage, this, std::placeholders::_1) },
  11. { PLAYBIN_MSG_AUDIO_SINK, std::bind(&PlayerEngineGstImpl::HandleAudioMessage, this, std::placeholders::_1) },
  12. { PLAYBIN_MSG_POSITION_UPDATE, std::bind(&PlayerEngineGstImpl::HandlePositionUpdateMessage, this,
  13. std::placeholders::_1) },
  14. };
  15. if (MSG_NOTIFY_FUNC_TABLE.count(msg.type) != 0) {
  16. MSG_NOTIFY_FUNC_TABLE.at(msg.type)(msg);
  17. }
  18. }

最终的流程走到了PlayerEngineGstImpl::HandleBufferingStart(),在这个方法中,主要通过obs_将format传给IPlayerEngineObs的OnInfo方法。

  1. void PlayerEngineGstImpl::HandleBufferingStart()
  2. {
  3. percent_ = 0;
  4. Format format;
  5. (void)format.PutIntValue(std::string(PlayerKeys::PLAYER_BUFFERING_START), 0);
  6. std::shared_ptr<IPlayerEngineObs> notifyObs = obs_.lock();
  7. if (notifyObs != nullptr) {
  8. notifyObs->OnInfo(INFO_TYPE_BUFFERING_UPDATE, 0, format);
  9. }
  10. }

我们重点看一下obs_是哪里设置的,在PlayerServer的初始化InitPlayEngine。shared_from_this()相当于是把PlayerServer自身赋值给obs,PlayerServer也是实现了IPlayerEngineObs对应的接口。

  1. int32_t PlayerServer::InitPlayEngine(const std::string &url)
  2. {
  3. ......
  4. int32_t ret = taskMgr_.Init();
  5. auto engineFactory = EngineFactoryRepo::Instance().GetEngineFactory(IEngineFactory::Scene::SCENE_PLAYBACK, url);
  6. playerEngine_ = engineFactory->CreatePlayerEngine(appUid_, appPid_);
  7. if (dataSrc_ == nullptr) {
  8. ret = playerEngine_->SetSource(url);
  9. } else {
  10. ret = playerEngine_->SetSource(dataSrc_);
  11. }
  12. std::shared_ptr<IPlayerEngineObs> obs = shared_from_this();
  13. ret = playerEngine_->SetObs(obs);
  14. lastOpStatus_ = PLAYER_INITIALIZED;
  15. ChangeState(initializedState_);
  16. return MSERR_OK;
  17. }

这样我们就跟踪到了PlayerServer的OnInfo()方法。

  1. void PlayerServer::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
  2. {
  3. std::lock_guard<std::mutex> lockCb(mutexCb_);
  4. int32_t ret = HandleMessage(type, extra, infoBody);
  5. if (playerCb_ != nullptr && ret == MSERR_OK) {
  6. playerCb_->OnInfo(type, extra, infoBody);
  7. }
  8. }

2. Play分析

从PlayerServer开始跟踪,调用到PlayerServer的OnPlay()方法。

  1. int32_t PlayerServer::Play()
  2. {
  3. ......
  4. if (lastOpStatus_ == PLAYER_PREPARED || lastOpStatus_ == PLAYER_PLAYBACK_COMPLETE ||
  5. lastOpStatus_ == PLAYER_PAUSED) {
  6. return OnPlay();
  7. } else {
  8. return MSERR_INVALID_OPERATION;
  9. }
  10. }

在OnPlay中会启动一个任务,在任务中获取当前的状态,然后调用当前状态的Play()方法。

  1. int32_t PlayerServer::OnPlay()
  2. {
  3. ......
  4. auto playingTask = std::make_shared<TaskHandler<void>>([this]() {
  5. auto currState = std::static_pointer_cast<BaseState>(GetCurrState());
  6. (void)currState->Play();
  7. });
  8. int ret = taskMgr_.LaunchTask(playingTask, PlayerServerTaskType::STATE_CHANGE);
  9. lastOpStatus_ = PLAYER_STARTED;
  10. return MSERR_OK;
  11. }

前面调用了PrepareAsync,所以当前的状态是Prepared,调用到了PreparedState的Play()方法,这个方法还是按照之前Prepare的方式,调回到PlayerServer的HandlePlay()。

  1. int32_t PlayerServer::PreparedState::Play()
  2. {
  3. return server_.HandlePlay();
  4. }

在PlayServer中通过播放引擎继续向下调用。

  1. int32_t PlayerServer::HandlePlay()
  2. {
  3. int32_t ret = playerEngine_->Play();
  4. CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_INVALID_OPERATION, "Engine Play Failed!");
  5. return MSERR_OK;
  6. }

在PlayerEngineGstImpl的Play()方法会继续调用playBinCtrler_的Play()方法。

  1. int32_t PlayerEngineGstImpl::Play()
  2. {
  3. ......
  4. playBinCtrler_->Play();
  5. return MSERR_OK;
  6. }

PlayBinCtrlerBase的Play()方法根据当前的State,调用currSate->Play()。

  1. int32_t PlayBinCtrlerBase::Play()
  2. {
  3. ......
  4. auto currState = std::static_pointer_cast<BaseState>(GetCurrState());
  5. int32_t ret = currState->Play();
  6. return MSERR_OK;
  7. }

在PreparedState的Play()方法中改变了PlayBin的状态为playing。

  1. int32_t PlayBinCtrlerBase::PreparedState::Play()
  2. {
  3. GstStateChangeReturn ret;
  4. return ChangePlayBinState(GST_STATE_PLAYING, ret);
  5. }

ChangePlayBinState主要是调用了

gst_element_set_state(GST_ELEMENT_CAST(ctrler_.playbin_),GST_STATE_PLAYING),这个直接调用了gstreamer三方库的实现,调用完这个方法以后,gstreamer就开始进行播放了。

  1. int32_t PlayBinCtrlerBase::BaseState::ChangePlayBinState(GstState targetState, GstStateChangeReturn &ret)
  2. {
  3. ......
  4. ret = gst_element_set_state(GST_ELEMENT_CAST(ctrler_.playbin_), targetState);
  5. if (ret == GST_STATE_CHANGE_FAILURE) {
  6. MEDIA_LOGE("Failed to change playbin's state to %{public}s", gst_element_state_get_name(targetState));
  7. return MSERR_INVALID_OPERATION;
  8. }
  9. return MSERR_OK;
  10. }

六、总结

本篇文章主要从PlayerServer播放服务开始分析音视频播放的流程,涉及到gstreamer引擎的调用,相对于多媒体播放框架来说,更加底层,便于熟悉从框架到gstreamer的整体流程。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/984943
推荐阅读
相关标签
  

闽ICP备14008679号