Demuxer/Player: 修改解复用与播放器底层逻辑,确保触发播放成功回调时不丢帧

This commit is contained in:
ziyue 2021-11-10 10:58:43 +08:00
parent 37fdb8d135
commit fdfde17ec7
14 changed files with 391 additions and 421 deletions

View File

@ -181,7 +181,7 @@ void MediaSink::emitAllTrackReady() {
void MediaSink::onAllTrackReady_l() { void MediaSink::onAllTrackReady_l() {
//是否添加静音音频 //是否添加静音音频
GET_CONFIG(bool, addMuteAudio, General::kAddMuteAudio); GET_CONFIG(bool, addMuteAudio, General::kAddMuteAudio);
if (addMuteAudio && !_track_map[TrackAudio]) { if (addMuteAudio && _track_map.find(TrackAudio) == _track_map.end()) {
addMuteAudioTrack(); addMuteAudioTrack();
} }
onAllTrackReady(); onAllTrackReady();
@ -253,11 +253,12 @@ bool MuteAudioMaker::inputFrame(const Frame::Ptr &frame) {
} }
bool MediaSink::addMuteAudioTrack() { bool MediaSink::addMuteAudioTrack() {
if (getTrack(TrackAudio, false)) { if (_track_map.find(TrackAudio) != _track_map.end()) {
WarnL << "audio track already existed"; WarnL << "audio track already existed";
return false; return false;
} }
if (addTrack(std::make_shared<AACTrack>())) {
if (MediaSink::addTrack(std::make_shared<AACTrack>(makeAacConfig(MUTE_ADTS_DATA, ADTS_HEADER_LEN)))) {
_mute_audio_maker = std::make_shared<MuteAudioMaker>(); _mute_audio_maker = std::make_shared<MuteAudioMaker>();
_mute_audio_maker->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) { _mute_audio_maker->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
return inputFrame(frame); return inputFrame(frame);

View File

@ -305,7 +305,6 @@ const string kRtspPwdIsMD5 = "rtsp_pwd_md5";
const string kTimeoutMS = "protocol_timeout_ms"; const string kTimeoutMS = "protocol_timeout_ms";
const string kMediaTimeoutMS = "media_timeout_ms"; const string kMediaTimeoutMS = "media_timeout_ms";
const string kBeatIntervalMS = "beat_interval_ms"; const string kBeatIntervalMS = "beat_interval_ms";
const string kMaxAnalysisMS = "max_analysis_ms";
const string kBenchmarkMode = "benchmark_mode"; const string kBenchmarkMode = "benchmark_mode";
} }

View File

@ -329,8 +329,6 @@ extern const string kTimeoutMS;
extern const string kMediaTimeoutMS; extern const string kMediaTimeoutMS;
//rtsp/rtmp心跳时间,默认5000毫秒 //rtsp/rtmp心跳时间,默认5000毫秒
extern const string kBeatIntervalMS; extern const string kBeatIntervalMS;
//Track编码格式探测最大时间单位毫秒默认2000
extern const string kMaxAnalysisMS;
//是否为性能测试模式性能测试模式开启后不会解析rtp或rtmp包 //是否为性能测试模式性能测试模式开启后不会解析rtp或rtmp包
extern const string kBenchmarkMode; extern const string kBenchmarkMode;
} }

View File

@ -20,9 +20,6 @@ MediaPlayer::MediaPlayer(const EventPoller::Ptr &poller) {
_poller = poller ? poller : EventPollerPool::Instance().getPoller(); _poller = poller ? poller : EventPollerPool::Instance().getPoller();
} }
MediaPlayer::~MediaPlayer() {
}
static void setOnCreateSocket_l(const std::shared_ptr<PlayerBase> &delegate, const Socket::onCreateSocket &cb){ static void setOnCreateSocket_l(const std::shared_ptr<PlayerBase> &delegate, const Socket::onCreateSocket &cb){
auto helper = dynamic_pointer_cast<SocketHelper>(delegate); auto helper = dynamic_pointer_cast<SocketHelper>(delegate);
if (helper) { if (helper) {
@ -41,10 +38,10 @@ void MediaPlayer::play(const string &url) {
_delegate = PlayerBase::createPlayer(_poller, url); _delegate = PlayerBase::createPlayer(_poller, url);
assert(_delegate); assert(_delegate);
setOnCreateSocket_l(_delegate, _on_create_socket); setOnCreateSocket_l(_delegate, _on_create_socket);
_delegate->setOnShutdown(_shutdownCB); _delegate->setOnShutdown(_on_shutdown);
_delegate->setOnPlayResult(_playResultCB); _delegate->setOnPlayResult(_on_play_result);
_delegate->setOnResume(_resumeCB); _delegate->setOnResume(_on_resume);
_delegate->setMediaSource(_pMediaSrc); _delegate->setMediaSource(_media_src);
_delegate->mINI::operator=(*this); _delegate->mINI::operator=(*this);
_delegate->play(url); _delegate->play(url);
} }
@ -58,23 +55,4 @@ void MediaPlayer::setOnCreateSocket(Socket::onCreateSocket cb){
_on_create_socket = std::move(cb); _on_create_socket = std::move(cb);
} }
void MediaPlayer::pause(bool pause) {
if (_delegate) {
_delegate->pause(pause);
}
}
void MediaPlayer::speed(float speed) {
if (_delegate) {
_delegate->speed(speed);
}
}
void MediaPlayer::teardown() {
if (_delegate) {
_delegate->teardown();
}
}
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -21,16 +21,14 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
class MediaPlayer : public PlayerImp<PlayerBase,PlayerBase> { class MediaPlayer : public PlayerImp<PlayerBase, PlayerBase> {
public: public:
typedef std::shared_ptr<MediaPlayer> Ptr; using Ptr = std::shared_ptr<MediaPlayer>;
MediaPlayer(const EventPoller::Ptr &poller = nullptr); MediaPlayer(const EventPoller::Ptr &poller = nullptr);
virtual ~MediaPlayer(); ~MediaPlayer() override = default;
void play(const string &url) override; void play(const string &url) override;
void pause(bool pause) override;
void speed(float speed) override;
void teardown() override;
EventPoller::Ptr getPoller(); EventPoller::Ptr getPoller();
void setOnCreateSocket(Socket::onCreateSocket cb); void setOnCreateSocket(Socket::onCreateSocket cb);

View File

@ -13,14 +13,15 @@
#include "Rtsp/RtspPlayerImp.h" #include "Rtsp/RtspPlayerImp.h"
#include "Rtmp/RtmpPlayerImp.h" #include "Rtmp/RtmpPlayerImp.h"
#include "Http/HlsPlayer.h" #include "Http/HlsPlayer.h"
using namespace toolkit; using namespace toolkit;
namespace mediakit { namespace mediakit {
PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller,const string &url_in) { PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller, const string &url_in) {
static auto releasePlayer = [](PlayerBase *ptr){ static auto releasePlayer = [](PlayerBase *ptr) {
onceToken token(nullptr,[&](){ onceToken token(nullptr, [&]() {
delete ptr; delete ptr;
}); });
ptr->teardown(); ptr->teardown();
}; };
@ -32,98 +33,91 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller,const st
url = url.substr(0, pos); url = url.substr(0, pos);
} }
if (strcasecmp("rtsps",prefix.data()) == 0) { if (strcasecmp("rtsps", prefix.data()) == 0) {
return PlayerBase::Ptr(new TcpClientWithSSL<RtspPlayerImp>(poller),releasePlayer); return PlayerBase::Ptr(new TcpClientWithSSL<RtspPlayerImp>(poller), releasePlayer);
} }
if (strcasecmp("rtsp",prefix.data()) == 0) { if (strcasecmp("rtsp", prefix.data()) == 0) {
return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer); return PlayerBase::Ptr(new RtspPlayerImp(poller), releasePlayer);
} }
if (strcasecmp("rtmps",prefix.data()) == 0) { if (strcasecmp("rtmps", prefix.data()) == 0) {
return PlayerBase::Ptr(new TcpClientWithSSL<RtmpPlayerImp>(poller),releasePlayer); return PlayerBase::Ptr(new TcpClientWithSSL<RtmpPlayerImp>(poller), releasePlayer);
} }
if (strcasecmp("rtmp",prefix.data()) == 0) { if (strcasecmp("rtmp", prefix.data()) == 0) {
return PlayerBase::Ptr(new RtmpPlayerImp(poller),releasePlayer); return PlayerBase::Ptr(new RtmpPlayerImp(poller), releasePlayer);
} }
if ((strcasecmp("http",prefix.data()) == 0 || strcasecmp("https",prefix.data()) == 0) && end_with(url, ".m3u8")) { if ((strcasecmp("http", prefix.data()) == 0 || strcasecmp("https", prefix.data()) == 0) && end_with(url, ".m3u8")) {
return PlayerBase::Ptr(new HlsPlayerImp(poller),releasePlayer); return PlayerBase::Ptr(new HlsPlayerImp(poller), releasePlayer);
} }
return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer); return PlayerBase::Ptr(new RtspPlayerImp(poller), releasePlayer);
} }
PlayerBase::PlayerBase() { PlayerBase::PlayerBase() {
this->mINI::operator[](kTimeoutMS) = 10000; this->mINI::operator[](kTimeoutMS) = 10000;
this->mINI::operator[](kMediaTimeoutMS) = 5000; this->mINI::operator[](kMediaTimeoutMS) = 5000;
this->mINI::operator[](kBeatIntervalMS) = 5000; this->mINI::operator[](kBeatIntervalMS) = 5000;
this->mINI::operator[](kMaxAnalysisMS) = 5000;
} }
///////////////////////////Demuxer////////////////////////////// ///////////////////////////Demuxer//////////////////////////////
bool Demuxer::isInited(int analysisMs) {
if(analysisMs && _ticker.createdTime() > (uint64_t)analysisMs){
//analysisMs毫秒后强制初始化完毕
return true;
}
if (_videoTrack && !_videoTrack->ready()) {
//视频未准备好
return false;
}
if (_audioTrack && !_audioTrack->ready()) {
//音频未准备好
return false;
}
return true;
}
vector<Track::Ptr> Demuxer::getTracks(bool trackReady) const { bool Demuxer::addTrack(const Track::Ptr &track) {
vector<Track::Ptr> ret; auto ret = MediaSink::addTrack(track);
if(_videoTrack){ if (ret) {
if(trackReady){ track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
if(_videoTrack->ready()){ return inputFrame(frame);
ret.emplace_back(_videoTrack); }));
}
}else{
ret.emplace_back(_videoTrack);
}
}
if(_audioTrack){
if(trackReady){
if(_audioTrack->ready()){
ret.emplace_back(_audioTrack);
}
}else{
ret.emplace_back(_audioTrack);
}
} }
return ret; return ret;
} }
float Demuxer::getDuration() const {
return _fDuration;
}
bool Demuxer::addTrack(const Track::Ptr &track){
return _listener ? _listener->addTrack(track) : false;
}
void Demuxer::addTrackCompleted(){
if(_listener){
_listener->addTrackCompleted();
}
}
void Demuxer::resetTracks() {
if (_listener) {
_listener->resetTracks();
}
}
void Demuxer::setTrackListener(TrackListener *listener) { void Demuxer::setTrackListener(TrackListener *listener) {
_listener = listener; _listener = listener;
} }
bool Demuxer::onTrackReady(const Track::Ptr &track) {
_tracks[track->getTrackType()] = track->clone();
return true;
}
void Demuxer::onAllTrackReady() {
if (!_listener) {
return;
}
for (auto &track : _tracks) {
if (track) {
_listener->addTrack(track);
}
}
_listener->addTrackCompleted();
}
bool Demuxer::onTrackFrame(const Frame::Ptr &frame) {
return _tracks[frame->getTrackType()]->inputFrame(frame);
}
void Demuxer::resetTracks() {
MediaSink::resetTracks();
for (auto &track : _tracks) {
track = nullptr;
}
if (_listener) {
_listener->resetTracks();
}
}
vector<Track::Ptr> Demuxer::getTracks(bool ready) const {
vector<Track::Ptr> ret;
for (auto &track : _tracks) {
if (!track || (ready && !track->ready())) {
continue;
}
ret.emplace_back(track);
}
return ret;
}
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -22,292 +22,242 @@
#include "Common/MediaSink.h" #include "Common/MediaSink.h"
#include "Extension/Frame.h" #include "Extension/Frame.h"
#include "Extension/Track.h" #include "Extension/Track.h"
using namespace toolkit; using namespace toolkit;
namespace mediakit { namespace mediakit {
class DemuxerBase : public TrackSource{ class PlayerBase : public TrackSource, public mINI {
public: public:
typedef std::shared_ptr<DemuxerBase> Ptr; using Ptr = std::shared_ptr<PlayerBase>;
using Event = std::function<void(const SockException &ex)>;
/** static Ptr createPlayer(const EventPoller::Ptr &poller, const string &strUrl);
*
* @return
*/
virtual float getDuration() const { return 0;}
/**
* getTrack方法
* @param analysisMs
* @return
*/
virtual bool isInited(int analysisMs) { return true; }
};
class PlayerBase : public DemuxerBase, public mINI{
public:
typedef std::shared_ptr<PlayerBase> Ptr;
static Ptr createPlayer(const EventPoller::Ptr &poller,const string &strUrl);
PlayerBase(); PlayerBase();
virtual ~PlayerBase(){} ~PlayerBase() override = default;
/** /**
* *
* @param strUrl urlrtsp/rtmp * @param url urlrtsp/rtmp
*/ */
virtual void play(const string &strUrl) {} virtual void play(const string &url) {};
/** /**
* *
* @param bPause * @param flag true:false:
*/ */
virtual void pause(bool bPause) {} virtual void pause(bool flag) {};
/**
*
*/
virtual float getDuration() const { return 0; };
/** /**
* *
* @param speed 1.0 2.0 0.5 * @param speed 1.0 2.0 0.5
*/ */
virtual void speed(float speed) {} virtual void speed(float speed) {};
/** /**
* *
*/ */
virtual void teardown() {} virtual void teardown() {};
/**
*
* @param cb
*/
virtual void setOnShutdown( const function<void(const SockException &)> &cb) {}
/**
*
* @param cb
*/
virtual void setOnPlayResult( const function<void(const SockException &ex)> &cb) {}
/**
*
* @param cb
*/
virtual void setOnResume( const function<void()> &cb) {}
/** /**
* 0.0 ~ 1.0 * 0.0 ~ 1.0
* @return
*/ */
virtual float getProgress() const { return 0;} virtual float getProgress() const { return 0; };
/** /**
* pos * pos
* @return
*/ */
virtual uint32_t getProgressPos() const { return 0; } virtual uint32_t getProgressPos() const { return 0; };
/** /**
* *
* @param fProgress 0.0 ~ 1.0 * @param progress 0.0 ~ 1.0
*/ */
virtual void seekTo(float fProgress) {} virtual void seekTo(float progress) {};
/** /**
* *
* @param seekPos * @param pos
*/ */
virtual void seekTo(uint32_t seekPos) {} virtual void seekTo(uint32_t pos) {};
/**
* MediaSourcertsp/rtmp代理
* @param src
*/
virtual void setMediaSource(const MediaSource::Ptr & src) {}
/** /**
* rtsp * rtsp
* @param trackType TrackInvalid时为总丢包率 * @param type TrackInvalid时为总丢包率
* @return
*/ */
virtual float getPacketLossRate(TrackType trackType) const {return 0; } virtual float getPacketLossRate(TrackType type) const { return 0; };
/** /**
* track * track
*/ */
vector<Track::Ptr> getTracks(bool trackReady = true) const override{ vector<Track::Ptr> getTracks(bool ready = true) const override { return vector<Track::Ptr>(); };
return vector<Track::Ptr>();
}
protected:
virtual void onShutdown(const SockException &ex) {}
virtual void onPlayResult(const SockException &ex) {}
/** /**
* * MediaSourcertsp/rtmp代理
*/ */
virtual void onResume(){}; virtual void setMediaSource(const MediaSource::Ptr &src) = 0;
/**
*
*/
virtual void setOnShutdown(const Event &cb) = 0;
/**
*
*/
virtual void setOnPlayResult(const Event &cb) = 0;
/**
*
*/
virtual void setOnResume(const function<void()> &cb) = 0;
protected:
virtual void onResume() = 0;
virtual void onShutdown(const SockException &ex) = 0;
virtual void onPlayResult(const SockException &ex) = 0;
}; };
template<typename Parent,typename Delegate> template<typename Parent, typename Delegate>
class PlayerImp : public Parent { class PlayerImp : public Parent {
public: public:
typedef std::shared_ptr<PlayerImp> Ptr; using Ptr = std::shared_ptr<PlayerImp>;
template<typename ...ArgsType> template<typename ...ArgsType>
PlayerImp(ArgsType &&...args):Parent(std::forward<ArgsType>(args)...) {} PlayerImp(ArgsType &&...args) : Parent(std::forward<ArgsType>(args)...) {}
virtual ~PlayerImp() {} ~PlayerImp() override = default;
void setOnShutdown(const function<void(const SockException &)> &cb) override { void play(const string &url) override {
if (_delegate) { return _delegate ? _delegate->play(url) : Parent::play(url);
_delegate->setOnShutdown(cb);
}
_shutdownCB = cb;
} }
void setOnPlayResult(const function<void(const SockException &ex)> &cb) override { void pause(bool flag) override {
if (_delegate) { return _delegate ? _delegate->pause(flag) : Parent::pause(flag);
_delegate->setOnPlayResult(cb);
}
_playResultCB = cb;
} }
void setOnResume(const function<void()> &cb) override { void speed(float speed) override {
if (_delegate) { return _delegate ? _delegate->speed(speed) : Parent::speed(speed);
_delegate->setOnResume(cb);
}
_resumeCB = cb;
} }
bool isInited(int analysisMs) override { void teardown() override {
if (_delegate) { return _delegate ? _delegate->teardown() : Parent::teardown();
return _delegate->isInited(analysisMs); }
}
return Parent::isInited(analysisMs); float getPacketLossRate(TrackType type) const override {
return _delegate ? _delegate->getPacketLossRate(type) : Parent::getPacketLossRate(type);
} }
float getDuration() const override { float getDuration() const override {
if (_delegate) { return _delegate ? _delegate->getDuration() : Parent::getDuration();
return _delegate->getDuration();
}
return Parent::getDuration();
} }
float getProgress() const override { float getProgress() const override {
if (_delegate) { return _delegate ? _delegate->getProgress() : Parent::getProgress();
return _delegate->getProgress();
}
return Parent::getProgress();
} }
uint32_t getProgressPos() const override { uint32_t getProgressPos() const override {
if (_delegate) { return _delegate ? _delegate->getProgressPos() : Parent::getProgressPos();
return _delegate->getProgressPos();
}
return Parent::getProgressPos();
} }
void seekTo(float fProgress) override { void seekTo(float progress) override {
if (_delegate) { return _delegate ? _delegate->seekTo(progress) : Parent::seekTo(progress);
return _delegate->seekTo(fProgress);
}
return Parent::seekTo(fProgress);
} }
void seekTo(uint32_t seekPos) override { void seekTo(uint32_t pos) override {
if (_delegate) { return _delegate ? _delegate->seekTo(pos) : Parent::seekTo(pos);
return _delegate->seekTo(seekPos);
}
return Parent::seekTo(seekPos);
} }
void setMediaSource(const MediaSource::Ptr &src) override { vector<Track::Ptr> getTracks(bool ready = true) const override {
if (_delegate) { return _delegate ? _delegate->getTracks(ready) : Parent::getTracks(ready);
_delegate->setMediaSource(src);
}
_pMediaSrc = src;
}
vector<Track::Ptr> getTracks(bool trackReady = true) const override {
if (_delegate) {
return _delegate->getTracks(trackReady);
}
return Parent::getTracks(trackReady);
} }
std::shared_ptr<SockInfo> getSockInfo() const { std::shared_ptr<SockInfo> getSockInfo() const {
return dynamic_pointer_cast<SockInfo>(_delegate); return dynamic_pointer_cast<SockInfo>(_delegate);
} }
void setMediaSource(const MediaSource::Ptr &src) override {
if (_delegate) {
_delegate->setMediaSource(src);
}
_media_src = src;
}
void setOnShutdown(const function<void(const SockException &)> &cb) override {
if (_delegate) {
_delegate->setOnShutdown(cb);
}
_on_shutdown = cb;
}
void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
if (_delegate) {
_delegate->setOnPlayResult(cb);
}
_on_play_result = cb;
}
void setOnResume(const function<void()> &cb) override {
if (_delegate) {
_delegate->setOnResume(cb);
}
_on_resume = cb;
}
protected: protected:
void onShutdown(const SockException &ex) override { void onShutdown(const SockException &ex) override {
if (_shutdownCB) { if (_on_shutdown) {
_shutdownCB(ex); _on_shutdown(ex);
_shutdownCB = nullptr; _on_shutdown = nullptr;
} }
} }
void onPlayResult(const SockException &ex) override { void onPlayResult(const SockException &ex) override {
if(_playResultCB) { if (_on_play_result) {
_playResultCB(ex); _on_play_result(ex);
_playResultCB = nullptr; _on_play_result = nullptr;
} }
} }
void onResume() override{ void onResume() override {
if(_resumeCB){ if (_on_resume) {
_resumeCB(); _on_resume();
} }
} }
protected: protected:
function<void(const SockException &ex)> _shutdownCB; function<void()> _on_resume;
function<void(const SockException &ex)> _playResultCB; PlayerBase::Event _on_shutdown;
function<void()> _resumeCB; PlayerBase::Event _on_play_result;
MediaSource::Ptr _media_src;
std::shared_ptr<Delegate> _delegate; std::shared_ptr<Delegate> _delegate;
MediaSource::Ptr _pMediaSrc;
}; };
class Demuxer : public PlayerBase, public TrackListener{ class Demuxer : protected MediaSink {
public: public:
Demuxer() = default; Demuxer() = default;
~Demuxer() override = default; ~Demuxer() override = default;
/**
*
* RtspDemuxer对象时有些rtsp的sdp不包含sps pps信息
* sps的rtp包后才能完成
*
* RtmpDemuxer对象时是无法获取sps pps aac_cfg等这些信息
* inputRtmp后才会获取到这些信息
* @param analysisMs
* @return
*/
bool isInited(int analysisMs) override;
/**
* Track
* @return Track
*/
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
/**
*
* @return ,
*/
float getDuration() const override;
/** /**
* track监听器 * track监听器
*/ */
void setTrackListener(TrackListener *listener); void setTrackListener(TrackListener *listener);
vector<Track::Ptr> getTracks(bool ready = true) const override;
protected: protected:
bool addTrack(const Track::Ptr &track) override; bool addTrack(const Track::Ptr & track) override;
void addTrackCompleted() override;
void resetTracks() override; void resetTracks() override;
bool onTrackReady(const Track::Ptr & track) override;
void onAllTrackReady() override;
bool onTrackFrame(const Frame::Ptr &frame) override;
protected: private:
float _fDuration = 0; Track::Ptr _tracks[TrackMax];
Ticker _ticker;
AudioTrack::Ptr _audioTrack;
VideoTrack::Ptr _videoTrack;
TrackListener *_listener = nullptr; TrackListener *_listener = nullptr;
}; };

View File

@ -160,7 +160,7 @@ bool PlayerProxy::close(MediaSource &sender, bool force) {
} }
int PlayerProxy::totalReaderCount() { int PlayerProxy::totalReaderCount() {
return (_muxer ? _muxer->totalReaderCount() : 0) + (_pMediaSrc ? _pMediaSrc->readerCount() : 0); return (_muxer ? _muxer->totalReaderCount() : 0) + (_media_src ? _media_src->readerCount() : 0);
} }
int PlayerProxy::totalReaderCount(MediaSource &sender) { int PlayerProxy::totalReaderCount(MediaSource &sender) {
@ -181,12 +181,12 @@ std::shared_ptr<SockInfo> PlayerProxy::getOriginSock(MediaSource &sender) const
void PlayerProxy::onPlaySuccess() { void PlayerProxy::onPlaySuccess() {
GET_CONFIG(bool, resetWhenRePlay, General::kResetWhenRePlay); GET_CONFIG(bool, resetWhenRePlay, General::kResetWhenRePlay);
if (dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc)) { if (dynamic_pointer_cast<RtspMediaSource>(_media_src)) {
//rtsp拉流代理 //rtsp拉流代理
if (resetWhenRePlay || !_muxer) { if (resetWhenRePlay || !_muxer) {
_muxer.reset(new MultiMediaSourceMuxer(_vhost, _app, _stream_id, getDuration(), false, true, _enable_hls, _enable_mp4)); _muxer.reset(new MultiMediaSourceMuxer(_vhost, _app, _stream_id, getDuration(), false, true, _enable_hls, _enable_mp4));
} }
} else if (dynamic_pointer_cast<RtmpMediaSource>(_pMediaSrc)) { } else if (dynamic_pointer_cast<RtmpMediaSource>(_media_src)) {
//rtmp拉流代理 //rtmp拉流代理
if (resetWhenRePlay || !_muxer) { if (resetWhenRePlay || !_muxer) {
_muxer.reset(new MultiMediaSourceMuxer(_vhost, _app, _stream_id, getDuration(), true, false, _enable_hls, _enable_mp4)); _muxer.reset(new MultiMediaSourceMuxer(_vhost, _app, _stream_id, getDuration(), true, false, _enable_hls, _enable_mp4));
@ -218,9 +218,9 @@ void PlayerProxy::onPlaySuccess() {
//添加完毕所有track防止单track情况下最大等待3秒 //添加完毕所有track防止单track情况下最大等待3秒
_muxer->addTrackCompleted(); _muxer->addTrackCompleted();
if (_pMediaSrc) { if (_media_src) {
//让_muxer对象拦截一部分事件(比如说录像相关事件) //让_muxer对象拦截一部分事件(比如说录像相关事件)
_pMediaSrc->setListener(_muxer); _media_src->setListener(_muxer);
} }
} }

View File

@ -25,7 +25,7 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
const AMFValue *videocodecid = nullptr; const AMFValue *videocodecid = nullptr;
val.object_for_each([&](const string &key, const AMFValue &val) { val.object_for_each([&](const string &key, const AMFValue &val) {
if (key == "duration") { if (key == "duration") {
_fDuration = (float)val.as_number(); _duration = (float)val.as_number();
return; return;
} }
if (key == "audiosamplerate") { if (key == "audiosamplerate") {
@ -80,6 +80,10 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
return ret; return ret;
} }
float RtmpDemuxer::getDuration() const {
return _duration;
}
void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
switch (pkt->type_id) { switch (pkt->type_id) {
case MSG_VIDEO: { case MSG_VIDEO: {
@ -111,41 +115,42 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec, int bit_rate) { void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec, int bit_rate) {
//生成Track对象 //生成Track对象
_videoTrack = dynamic_pointer_cast<VideoTrack>(Factory::getVideoTrackByAmf(videoCodec)); _video_track = dynamic_pointer_cast<VideoTrack>(Factory::getVideoTrackByAmf(videoCodec));
if (_videoTrack) { if (!_video_track) {
_videoTrack->setBitRate(bit_rate); return;
//生成rtmpCodec对象以便解码rtmp
_video_rtmp_decoder = Factory::getRtmpCodecByTrack(_videoTrack, false);
if (_video_rtmp_decoder) {
//设置rtmp解码器代理生成的frame写入该Track
_video_rtmp_decoder->addDelegate(_videoTrack);
addTrack(_videoTrack);
_try_get_video_track = true;
} else {
//找不到相应的rtmp解码器该track无效
_videoTrack.reset();
}
} }
//生成rtmpCodec对象以便解码rtmp
_video_rtmp_decoder = Factory::getRtmpCodecByTrack(_video_track, false);
if (!_video_rtmp_decoder) {
//找不到相应的rtmp解码器该track无效
_video_track.reset();
return;
}
_video_track->setBitRate(bit_rate);
//设置rtmp解码器代理生成的frame写入该Track
_video_rtmp_decoder->addDelegate(_video_track);
addTrack(_video_track);
_try_get_video_track = true;
} }
void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit, int bit_rate) { void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit, int bit_rate) {
//生成Track对象 //生成Track对象
_audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getAudioTrackByAmf(audioCodec, sample_rate, channels, sample_bit)); _audio_track = dynamic_pointer_cast<AudioTrack>(Factory::getAudioTrackByAmf(audioCodec, sample_rate, channels, sample_bit));
if (_audioTrack) { if (!_audio_track) {
_audioTrack->setBitRate(bit_rate); return;
//生成rtmpCodec对象以便解码rtmp
_audio_rtmp_decoder = Factory::getRtmpCodecByTrack(_audioTrack, false);
if (_audio_rtmp_decoder) {
//设置rtmp解码器代理生成的frame写入该Track
_audio_rtmp_decoder->addDelegate(_audioTrack);
addTrack(_audioTrack);
_try_get_audio_track = true;
} else {
//找不到相应的rtmp解码器该track无效
_audioTrack.reset();
}
} }
//生成rtmpCodec对象以便解码rtmp
_audio_rtmp_decoder = Factory::getRtmpCodecByTrack(_audio_track, false);
if (!_audio_rtmp_decoder) {
//找不到相应的rtmp解码器该track无效
_audio_track.reset();
return;
}
_audio_track->setBitRate(bit_rate);
//设置rtmp解码器代理生成的frame写入该Track
_audio_rtmp_decoder->addDelegate(_audio_track);
addTrack(_audio_track);
_try_get_audio_track = true;
} }
} /* namespace mediakit */
} /* namespace mediakit */

View File

@ -23,9 +23,9 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
class RtmpDemuxer : public Demuxer{ class RtmpDemuxer : public Demuxer {
public: public:
typedef std::shared_ptr<RtmpDemuxer> Ptr; using Ptr = std::shared_ptr<RtmpDemuxer>;
RtmpDemuxer() = default; RtmpDemuxer() = default;
~RtmpDemuxer() override = default; ~RtmpDemuxer() override = default;
@ -38,6 +38,12 @@ public:
*/ */
void inputRtmp(const RtmpPacket::Ptr &pkt); void inputRtmp(const RtmpPacket::Ptr &pkt);
/**
*
* @return ,
*/
float getDuration() const;
private: private:
void makeVideoTrack(const AMFValue &val, int bit_rate); void makeVideoTrack(const AMFValue &val, int bit_rate);
void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit, int bit_rate); void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit, int bit_rate);
@ -45,6 +51,9 @@ private:
private: private:
bool _try_get_video_track = false; bool _try_get_video_track = false;
bool _try_get_audio_track = false; bool _try_get_audio_track = false;
float _duration = 0;
AudioTrack::Ptr _audio_track;
VideoTrack::Ptr _video_track;
RtmpCodec::Ptr _audio_rtmp_decoder; RtmpCodec::Ptr _audio_rtmp_decoder;
RtmpCodec::Ptr _video_rtmp_decoder; RtmpCodec::Ptr _video_rtmp_decoder;
}; };

View File

@ -24,11 +24,12 @@ using namespace mediakit::Client;
namespace mediakit { namespace mediakit {
class RtmpPlayerImp: public PlayerImp<RtmpPlayer,RtmpDemuxer> { class RtmpPlayerImp: public PlayerImp<RtmpPlayer,PlayerBase>, private TrackListener {
public: public:
typedef std::shared_ptr<RtmpPlayerImp> Ptr; using Ptr = std::shared_ptr<RtmpPlayerImp>;
using Super = PlayerImp<RtmpPlayer,PlayerBase>;
RtmpPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<RtmpPlayer, RtmpDemuxer>(poller) {}; RtmpPlayerImp(const EventPoller::Ptr &poller) : Super(poller) {};
~RtmpPlayerImp() override { ~RtmpPlayerImp() override {
DebugL << endl; DebugL << endl;
@ -50,43 +51,60 @@ public:
uint32_t pos = MAX(float(0), MIN(seekPos, getDuration())) * 1000; uint32_t pos = MAX(float(0), MIN(seekPos, getDuration())) * 1000;
seekToMilliSecond(pos); seekToMilliSecond(pos);
} }
void play(const string &strUrl) override { float getDuration() const override {
PlayerImp<RtmpPlayer, RtmpDemuxer>::play(strUrl); return _demuxer ? _demuxer->getDuration() : 0;
}
vector<Track::Ptr> getTracks(bool ready = true) const override {
return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready);
} }
private: private:
//派生类回调函数 //派生类回调函数
bool onCheckMeta(const AMFValue &val) override { bool onCheckMeta(const AMFValue &val) override {
_rtmp_src = dynamic_pointer_cast<RtmpMediaSource>(_pMediaSrc); onCheckMeta_l(val);
if (_rtmp_src) {
_rtmp_src->setMetaData(val);
_set_meta_data = true;
}
_delegate.reset(new RtmpDemuxer);
_delegate->loadMetaData(val);
return true; return true;
} }
void onMediaData(RtmpPacket::Ptr chunkData) override { void onMediaData(RtmpPacket::Ptr chunkData) override {
if (!_delegate) { if (!_demuxer) {
//这个流没有metadata //有些rtmp流没metadata
_delegate.reset(new RtmpDemuxer()); onCheckMeta_l(TitleMeta().getMetadata());
} }
_delegate->inputRtmp(chunkData); _demuxer->inputRtmp(chunkData);
if (_rtmp_src) { if (_rtmp_src) {
if (!_set_meta_data && !chunkData->isCfgFrame()) {
_set_meta_data = true;
_rtmp_src->setMetaData(TitleMeta().getMetadata());
}
_rtmp_src->onWrite(std::move(chunkData)); _rtmp_src->onWrite(std::move(chunkData));
} }
} }
void onPlayResult(const SockException &ex) override {
if (ex) {
Super::onPlayResult(ex);
return;
}
}
bool addTrack(const Track::Ptr &track) override { return true; }
void addTrackCompleted() override {
Super::onPlayResult(SockException(Err_success, "play success"));
}
private: private:
void onCheckMeta_l(const AMFValue &val) {
_rtmp_src = dynamic_pointer_cast<RtmpMediaSource>(_media_src);
if (_rtmp_src) {
_rtmp_src->setMetaData(val);
}
_demuxer = std::make_shared<RtmpDemuxer>();
_demuxer->setTrackListener(this);
_demuxer->loadMetaData(val);
}
private:
RtmpDemuxer::Ptr _demuxer;
RtmpMediaSource::Ptr _rtmp_src; RtmpMediaSource::Ptr _rtmp_src;
bool _set_meta_data = false;
}; };

View File

@ -18,13 +18,13 @@ using namespace std;
namespace mediakit { namespace mediakit {
void RtspDemuxer::loadSdp(const string &sdp){ void RtspDemuxer::loadSdp(const string &sdp) {
loadSdp(SdpParser(sdp)); loadSdp(SdpParser(sdp));
} }
void RtspDemuxer::loadSdp(const SdpParser &attr) { void RtspDemuxer::loadSdp(const SdpParser &attr) {
auto tracks = attr.getAvailableTrack(); auto tracks = attr.getAvailableTrack();
for (auto &track : tracks){ for (auto &track : tracks) {
switch (track->_type) { switch (track->_type) {
case TrackVideo: { case TrackVideo: {
makeVideoTrack(track); makeVideoTrack(track);
@ -42,31 +42,35 @@ void RtspDemuxer::loadSdp(const SdpParser &attr) {
addTrackCompleted(); addTrackCompleted();
auto titleTrack = attr.getTrack(TrackTitle); auto titleTrack = attr.getTrack(TrackTitle);
if(titleTrack){ if (titleTrack) {
_fDuration = titleTrack->_duration; _duration = titleTrack->_duration;
}
}
bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) {
switch (rtp->type) {
case TrackVideo:{
if(_videoRtpDecoder){
return _videoRtpDecoder->inputRtp(rtp, true);
}
return false;
}
case TrackAudio:{
if(_audioRtpDecoder){
_audioRtpDecoder->inputRtp(rtp, false);
return false;
}
return false;
}
default:
return false;
} }
} }
static void setBitRate(const SdpTrack::Ptr &sdp, const Track::Ptr &track){ float RtspDemuxer::getDuration() const {
return _duration;
}
bool RtspDemuxer::inputRtp(const RtpPacket::Ptr &rtp) {
switch (rtp->type) {
case TrackVideo: {
if (_videoRtpDecoder) {
return _videoRtpDecoder->inputRtp(rtp, true);
}
return false;
}
case TrackAudio: {
if (_audioRtpDecoder) {
_audioRtpDecoder->inputRtp(rtp, false);
return false;
}
return false;
}
default: return false;
}
}
static void setBitRate(const SdpTrack::Ptr &sdp, const Track::Ptr &track) {
if (!sdp->_b.empty()) { if (!sdp->_b.empty()) {
int data_rate = 0; int data_rate = 0;
sscanf(sdp->_b.data(), "AS:%d", &data_rate); sscanf(sdp->_b.data(), "AS:%d", &data_rate);
@ -78,38 +82,40 @@ static void setBitRate(const SdpTrack::Ptr &sdp, const Track::Ptr &track){
void RtspDemuxer::makeAudioTrack(const SdpTrack::Ptr &audio) { void RtspDemuxer::makeAudioTrack(const SdpTrack::Ptr &audio) {
//生成Track对象 //生成Track对象
_audioTrack = dynamic_pointer_cast<AudioTrack>(Factory::getTrackBySdp(audio)); _audio_track = dynamic_pointer_cast<AudioTrack>(Factory::getTrackBySdp(audio));
if(_audioTrack){ if (!_audio_track) {
setBitRate(audio, _audioTrack); return;
//生成RtpCodec对象以便解码rtp
_audioRtpDecoder = Factory::getRtpDecoderByTrack(_audioTrack);
if(_audioRtpDecoder){
//设置rtp解码器代理生成的frame写入该Track
_audioRtpDecoder->addDelegate(_audioTrack);
addTrack(_audioTrack);
} else{
//找不到相应的rtp解码器该track无效
_audioTrack.reset();
}
} }
setBitRate(audio, _audio_track);
//生成RtpCodec对象以便解码rtp
_audioRtpDecoder = Factory::getRtpDecoderByTrack(_audio_track);
if (!_audioRtpDecoder) {
//找不到相应的rtp解码器该track无效
_audio_track.reset();
return;
}
//设置rtp解码器代理生成的frame写入该Track
_audioRtpDecoder->addDelegate(_audio_track);
addTrack(_audio_track);
} }
void RtspDemuxer::makeVideoTrack(const SdpTrack::Ptr &video) { void RtspDemuxer::makeVideoTrack(const SdpTrack::Ptr &video) {
//生成Track对象 //生成Track对象
_videoTrack = dynamic_pointer_cast<VideoTrack>(Factory::getTrackBySdp(video)); _video_track = dynamic_pointer_cast<VideoTrack>(Factory::getTrackBySdp(video));
if(_videoTrack){ if (!_video_track) {
setBitRate(video, _videoTrack); return;
//生成RtpCodec对象以便解码rtp
_videoRtpDecoder = Factory::getRtpDecoderByTrack(_videoTrack);
if(_videoRtpDecoder){
//设置rtp解码器代理生成的frame写入该Track
_videoRtpDecoder->addDelegate(_videoTrack);
addTrack(_videoTrack);
}else{
//找不到相应的rtp解码器该track无效
_videoTrack.reset();
}
} }
setBitRate(video, _video_track);
//生成RtpCodec对象以便解码rtp
_videoRtpDecoder = Factory::getRtpDecoderByTrack(_video_track);
if (!_videoRtpDecoder) {
//找不到相应的rtp解码器该track无效
_video_track.reset();
return;
}
//设置rtp解码器代理生成的frame写入该Track
_videoRtpDecoder->addDelegate(_video_track);
addTrack(_video_track);
} }
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -21,7 +21,7 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
class RtspDemuxer : public Demuxer{ class RtspDemuxer : public Demuxer {
public: public:
typedef std::shared_ptr<RtspDemuxer> Ptr; typedef std::shared_ptr<RtspDemuxer> Ptr;
RtspDemuxer() = default; RtspDemuxer() = default;
@ -38,11 +38,22 @@ public:
* @return true i帧第一个rtp包 * @return true i帧第一个rtp包
*/ */
bool inputRtp(const RtpPacket::Ptr &rtp); bool inputRtp(const RtpPacket::Ptr &rtp);
/**
*
* @return ,
*/
float getDuration() const;
private: private:
void makeAudioTrack(const SdpTrack::Ptr &audio); void makeAudioTrack(const SdpTrack::Ptr &audio);
void makeVideoTrack(const SdpTrack::Ptr &video); void makeVideoTrack(const SdpTrack::Ptr &video);
void loadSdp(const SdpParser &parser); void loadSdp(const SdpParser &parser);
private: private:
float _duration = 0;
AudioTrack::Ptr _audio_track;
VideoTrack::Ptr _video_track;
RtpCodec::Ptr _audioRtpDecoder; RtpCodec::Ptr _audioRtpDecoder;
RtpCodec::Ptr _videoRtpDecoder; RtpCodec::Ptr _videoRtpDecoder;
}; };

View File

@ -25,12 +25,14 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
class RtspPlayerImp : public PlayerImp<RtspPlayer,RtspDemuxer> { class RtspPlayerImp : public PlayerImp<RtspPlayer, PlayerBase> ,private TrackListener {
public: public:
typedef std::shared_ptr<RtspPlayerImp> Ptr; using Ptr = std::shared_ptr<RtspPlayerImp>;
using Super = PlayerImp<RtspPlayer, PlayerBase>;
RtspPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<RtspPlayer, RtspDemuxer>(poller) {} RtspPlayerImp(const EventPoller::Ptr &poller) : Super(poller) {}
~RtspPlayerImp() override{
~RtspPlayerImp() override {
DebugL << endl; DebugL << endl;
} }
@ -39,7 +41,6 @@ public:
return getProgressMilliSecond() / (getDuration() * 1000); return getProgressMilliSecond() / (getDuration() * 1000);
} }
return PlayerBase::getProgress(); return PlayerBase::getProgress();
} }
uint32_t getProgressPos() const override { uint32_t getProgressPos() const override {
@ -55,29 +56,33 @@ public:
} }
void seekTo(uint32_t seekPos) override { void seekTo(uint32_t seekPos) override {
uint32_t pos = MAX(float(0), MIN(seekPos, getDuration()))*1000; uint32_t pos = MAX(float(0), MIN(seekPos, getDuration())) * 1000;
seekToMilliSecond(pos); seekToMilliSecond(pos);
} }
float getDuration() const override {
return _demuxer ? _demuxer->getDuration() : 0;
}
vector<Track::Ptr> getTracks(bool ready = true) const override {
return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready);
}
private: private:
//派生类回调函数 //派生类回调函数
bool onCheckSDP(const string &sdp) override { bool onCheckSDP(const string &sdp) override {
_rtsp_media_src = dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc); _rtsp_media_src = dynamic_pointer_cast<RtspMediaSource>(_media_src);
if (_rtsp_media_src) { if (_rtsp_media_src) {
_rtsp_media_src->setSdp(sdp); _rtsp_media_src->setSdp(sdp);
} }
_delegate.reset(new RtspDemuxer); _demuxer = std::make_shared<RtspDemuxer>();
_delegate->loadSdp(sdp); _demuxer->setTrackListener(this);
_demuxer->loadSdp(sdp);
return true; return true;
} }
void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) override { void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) override {
_delegate->inputRtp(rtp); _demuxer->inputRtp(rtp);
if (_max_analysis_ms && _delegate->isInited(_max_analysis_ms)) {
PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(SockException(Err_success, "play rtsp success"));
_max_analysis_ms = 0;
}
if (_rtsp_media_src) { if (_rtsp_media_src) {
// rtsp直接代理是无法判断该rtp是否是I帧所以GOP缓存基本是无效的 // rtsp直接代理是无法判断该rtp是否是I帧所以GOP缓存基本是无效的
// 为了减少内存使用那么我们设置为一直关键帧以便清空GOP缓存 // 为了减少内存使用那么我们设置为一直关键帧以便清空GOP缓存
@ -85,23 +90,21 @@ private:
} }
} }
//在RtspPlayer中触发onPlayResult事件只是代表收到play回复了
//并不代表所有track初始化成功了(这跟rtmp播放器不一样)
//如果sdp里面信息不完整只能尝试延后从rtp中恢复关键信息并初始化track
//如果超过这个时间还未获取成功那么会强制触发onPlayResult事件(虽然此时有些track还未初始化成功)
void onPlayResult(const SockException &ex) override { void onPlayResult(const SockException &ex) override {
//isInited判断条件无超时 if (ex) {
if (ex || _delegate->isInited(0)) { Super::onPlayResult(ex);
//已经初始化成功说明sdp里面有完善的信息 return;
PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(ex);
} else {
//还没初始化成功说明sdp里面信息不完善还有一些track未初始化成功
_max_analysis_ms = (*this)[Client::kMaxAnalysisMS];
} }
} }
bool addTrack(const Track::Ptr &track) override { return true; }
void addTrackCompleted() override {
Super::onPlayResult(SockException(Err_success, "play success"));
}
private: private:
int _max_analysis_ms = 0; RtspDemuxer::Ptr _demuxer;
RtspMediaSource::Ptr _rtsp_media_src; RtspMediaSource::Ptr _rtsp_media_src;
}; };