From bd9dae7a7c5982377e1b6644a371d322b225cd82 Mon Sep 17 00:00:00 2001 From: pingkai Date: Wed, 24 Jun 2020 19:20:08 +0800 Subject: [PATCH] feat(mediaplayer): imple mediaPlayerDCA Signed-off-by: pingkai --- mediaPlayer/CMakeLists.txt | 3 +- mediaPlayer/EventCodeMap.cpp | 1 + mediaPlayer/SMP_DCAManager.cpp | 67 ++++++++++++++++++++++++++++ mediaPlayer/SMP_DCAManager.h | 60 +++++++++++++++++++++++++ mediaPlayer/SuperMediaPlayer.cpp | 13 ++++-- mediaPlayer/SuperMediaPlayer.h | 11 +++-- mediaPlayer/media_player_error_def.h | 8 ++-- 7 files changed, 151 insertions(+), 12 deletions(-) create mode 100644 mediaPlayer/SMP_DCAManager.cpp create mode 100644 mediaPlayer/SMP_DCAManager.h diff --git a/mediaPlayer/CMakeLists.txt b/mediaPlayer/CMakeLists.txt index 20af05ea8..44cf5f11a 100644 --- a/mediaPlayer/CMakeLists.txt +++ b/mediaPlayer/CMakeLists.txt @@ -106,7 +106,8 @@ set(SOURCE_FILES mediaPlayerSubTitleListener.cpp mediaPlayerSubTitleListener.h playerOptions.cpp - playerOptions.h) + playerOptions.h + SMP_DCAManager.cpp) if (TARGET_PLATFORM STREQUAL "Android") set(SOURCE_FILES ${SOURCE_FILES} TrafficStats.c diff --git a/mediaPlayer/EventCodeMap.cpp b/mediaPlayer/EventCodeMap.cpp index 898d91867..2b79e01ed 100644 --- a/mediaPlayer/EventCodeMap.cpp +++ b/mediaPlayer/EventCodeMap.cpp @@ -57,4 +57,5 @@ void EventCodeMap::init() codeMap.insert(pair(MEDIA_PLAYER_EVENT_NETWORK_RETRY_SUCCESS, 113)); codeMap.insert(pair(MEDIA_PLAYER_EVENT_SUBTITLE_SELECT_ERROR, 114)); codeMap.insert(pair(MEDIA_PLAYER_EVENT_DECODER_RECOVER_SIZE, 115)); + codeMap.insert(pair(MEDIA_PLAYER_EVENT_DIRECT_COMPONENT_MSG, 116)); } diff --git a/mediaPlayer/SMP_DCAManager.cpp b/mediaPlayer/SMP_DCAManager.cpp new file mode 100644 index 000000000..38aaf8f55 --- /dev/null +++ b/mediaPlayer/SMP_DCAManager.cpp @@ -0,0 +1,67 @@ +// +// Created by moqi on 2020/6/24. +// +#include "SMP_DCAManager.h" +#include "SuperMediaPlayer.h" +#include +using namespace std; +using namespace Cicada; +void SMP_DCAObserver::onEvent(int level, const string &content) +{ + CicadaJSONItem item; + item.addValue("name", mName); + item.addValue("obj", to_string((uint64_t) mObj)); + item.addValue("level", level); + item.addValue("content", content); + if (mListener) { + mListener->onEvent(item.printJSON()); + } +} +void SMP_DCAObserver::setListener(mediaPlayerDCAObserverListener *listener) +{ + mListener = listener; +} +void SMP_DCAManager::createObservers() +{ + if (mDemuxerObserver == nullptr && mPlayer.mDemuxerService && mPlayer.mDemuxerService->getDemuxerHandle()) { + mDemuxerObserver = static_cast>(new SMP_DCAObserver("demuxer", mPlayer.mDemuxerService)); + mDemuxerObserver->setListener(this); + mPlayer.mDemuxerService->getDemuxerHandle()->setDCAObserver(mDemuxerObserver.get()); + } +} +int SMP_DCAManager::invoke(const string &content) +{ + CicadaJSONItem item(content); + string name = item.getString("name"); + if (name == "demuxer" && mDemuxerObserver != nullptr) { + if ((void *) atoll(item.getString("obj").c_str()) == (void *) mPlayer.mDemuxerService) { + assert(mPlayer.mDemuxerService->getDemuxerHandle()); + return mPlayer.mDemuxerService->getDemuxerHandle()->invoke(item.getInt("cmd", -1), item.getString("content")); + } + } + // TODO: error code + return 0; +} +void SMP_DCAManager::onEvent(const string &content) +{ + std::lock_guard guard(mMutex); + mEventQue.push(content); +} +string SMP_DCAManager::getEvent() +{ + std::lock_guard guard(mMutex); + if (!mEventQue.empty()) { + string event = std::move(mEventQue.front()); + mEventQue.pop(); + return event; + } + return string(); +} +void SMP_DCAManager::reset() +{ + std::lock_guard guard(mMutex); + while (!mEventQue.empty()) { + mEventQue.pop(); + } + mDemuxerObserver = nullptr; +} diff --git a/mediaPlayer/SMP_DCAManager.h b/mediaPlayer/SMP_DCAManager.h new file mode 100644 index 000000000..d70d282fd --- /dev/null +++ b/mediaPlayer/SMP_DCAManager.h @@ -0,0 +1,60 @@ +// +// Created by moqi on 2020/6/24. +// + +#ifndef CICADAMEDIA_SMP_DCAMANAGER_H +#define CICADAMEDIA_SMP_DCAMANAGER_H + +#include + +#include +#include +#include + +namespace Cicada { + class SuperMediaPlayer; + + class mediaPlayerDCAObserverListener { + public: + virtual void onEvent(const std::string &content) = 0; + }; + class SMP_DCAObserver : public IDCAObserver { + public: + explicit SMP_DCAObserver(std::string name, void *obj) : mName(std::move(name)), mObj(obj) + {} + void setListener(mediaPlayerDCAObserverListener *listener); + + private: + void onEvent(int level, const std::string &content) override; + + private: + std::string mName{}; + void *mObj{nullptr}; + mediaPlayerDCAObserverListener *mListener{nullptr}; + }; + class SMP_DCAManager : public mediaPlayerDCAObserverListener { + public: + explicit SMP_DCAManager(SuperMediaPlayer &player) : mPlayer(player) + {} + + void createObservers(); + + int invoke(const std::string &content); + + std::string getEvent(); + + void reset(); + + private: + void onEvent(const std::string &content) override; + + private: + SuperMediaPlayer &mPlayer; + std::unique_ptr mDemuxerObserver{nullptr}; + std::queue mEventQue; + std::mutex mMutex; + }; +}// namespace Cicada + + +#endif//CICADAMEDIA_SMP_DCAMANAGER_H diff --git a/mediaPlayer/SuperMediaPlayer.cpp b/mediaPlayer/SuperMediaPlayer.cpp index eadbe7665..8e86664ed 100644 --- a/mediaPlayer/SuperMediaPlayer.cpp +++ b/mediaPlayer/SuperMediaPlayer.cpp @@ -50,10 +50,8 @@ namespace Cicada { const int64_t SuperMediaPlayer::SEEK_ACCURATE_MAX = 11 * 1000 * 1000; SuperMediaPlayer::SuperMediaPlayer() - : mMessageControl(*this), - mAudioRenderCB(*this), - mApsaraThread([this]() -> int { return this->mainService(); }, LOG_TAG), - mSourceListener(*this) + : mMessageControl(*this), mAudioRenderCB(*this), mApsaraThread([this]() -> int { return this->mainService(); }, LOG_TAG), + mSourceListener(*this), mDcaManager(*this) { AF_LOGD("SuperMediaPlayer()"); mPNotifier = new PlayerNotifier(); @@ -1085,6 +1083,11 @@ namespace Cicada { { int64_t curTime = af_gettime_relative(); mUtil.notifyPlayerLoop(curTime); + string event = mDcaManager.getEvent(); + while (!event.empty()) { + mPNotifier->NotifyEvent(MEDIA_PLAYER_EVENT_DIRECT_COMPONENT_MSG, event.c_str()); + event = mDcaManager.getEvent(); + } if (mMessageControl.empty() || (0 == mMessageControl.processMsg())) { ProcessVideoLoop(); @@ -3301,6 +3304,7 @@ namespace Cicada { mBSSeekCb = nullptr; mBSCbArg = nullptr; mUtil.reset(); + mDcaManager.reset(); mVideoInterlaced = InterlacedType_UNKNOWN; mVideoParserTimes = 0; mVideoPtsRevert = mAudioPtsRevert = false; @@ -3451,6 +3455,7 @@ namespace Cicada { mDataSource->Get_config(config); mDemuxerService->getDemuxerHandle()->setDataSourceConfig(config); } + mDcaManager.createObservers(); } //step2: Demuxer init and getstream index diff --git a/mediaPlayer/SuperMediaPlayer.h b/mediaPlayer/SuperMediaPlayer.h index cce9ad9af..187ad5b5b 100644 --- a/mediaPlayer/SuperMediaPlayer.h +++ b/mediaPlayer/SuperMediaPlayer.h @@ -16,16 +16,17 @@ using namespace std; #include #include "system_refer_clock.h" +#include "SMP_DCAManager.h" +#include "SuperMediaPlayerDataSourceListener.h" +#include "codec/videoDecoderFactory.h" #include "hls_adaptive_manager.h" -#include "player_types.h" #include "player_notifier.h" +#include "player_types.h" #include "render/video/IVideoRender.h" #include -#include #include #include -#include "codec/videoDecoderFactory.h" -#include "SuperMediaPlayerDataSourceListener.h" +#include #include #include @@ -67,6 +68,7 @@ namespace Cicada { private PlayerMessageControllerListener { friend class SuperMediaPlayerDataSourceListener; + friend class SMP_DCAManager; public: @@ -476,6 +478,7 @@ namespace Cicada { MediaPlayerUtil mUtil; SuperMediaPlayerDataSourceListener mSourceListener; + SMP_DCAManager mDcaManager; std::unique_ptr mVideoPacket{}; std::unique_ptr mAudioPacket{}; diff --git a/mediaPlayer/media_player_error_def.h b/mediaPlayer/media_player_error_def.h index 8f8a964fb..07a064847 100644 --- a/mediaPlayer/media_player_error_def.h +++ b/mediaPlayer/media_player_error_def.h @@ -61,7 +61,7 @@ namespace Cicada{ MEDIA_PLAYER_ERROR_UNKNOWN = 0x30000000 - 1, }; - enum MediaPlayerEventType { + enum MediaPlayerEventType { MEDIA_PLAYER_EVENT_START = 0, MEDIA_PLAYER_EVENT_SW_VIDEO_DECODER = MEDIA_PLAYER_EVENT_START, MEDIA_PLAYER_EVENT_AUDIO_CODEC_NOT_SUPPORT, @@ -80,8 +80,10 @@ namespace Cicada{ MEDIA_PLAYER_EVENT_DEMUXER_STARTUP_INFO, MEDIA_PLAYER_EVENT_SUBTITLE_SELECT_ERROR, - MEDIA_PLAYER_EVENT_DECODER_RECOVER_SIZE, - }; + MEDIA_PLAYER_EVENT_DECODER_RECOVER_SIZE, + + MEDIA_PLAYER_EVENT_DIRECT_COMPONENT_MSG, + }; } #endif //MEDIA_PLAYER_ERROR_DEF_H