2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_PLAYER_PLAYERBASE_H_
|
|
|
|
|
#define SRC_PLAYER_PLAYERBASE_H_
|
|
|
|
|
|
2017-08-03 13:55:46 +08:00
|
|
|
|
#include <map>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <functional>
|
|
|
|
|
#include "Network/Socket.h"
|
2017-08-03 13:55:46 +08:00
|
|
|
|
#include "Util/mini.h"
|
2018-09-19 12:34:29 +08:00
|
|
|
|
#include "Util/RingBuffer.h"
|
2018-10-21 21:21:14 +08:00
|
|
|
|
#include "Common/MediaSource.h"
|
2018-10-30 14:59:42 +08:00
|
|
|
|
#include "Extension/Frame.h"
|
|
|
|
|
#include "Extension/Track.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-25 11:35:41 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-12-03 12:32:57 +08:00
|
|
|
|
class DemuxerBase : public TrackSource{
|
2018-10-25 15:45:38 +08:00
|
|
|
|
public:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
typedef std::shared_ptr<DemuxerBase> Ptr;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取节目总时长,单位秒
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
virtual float getDuration() const { return 0;}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否初始化完毕,完毕后方可调用getTrack方法
|
|
|
|
|
* @param analysisMs 数据流最大分析时间 单位毫秒
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
virtual bool isInited(int analysisMs) { return true; }
|
2018-10-25 15:45:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlayerBase : public DemuxerBase, public mINI{
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
typedef std::shared_ptr<PlayerBase> Ptr;
|
2019-04-01 10:16:15 +08:00
|
|
|
|
static Ptr createPlayer(const EventPoller::Ptr &poller,const string &strUrl);
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
PlayerBase();
|
|
|
|
|
virtual ~PlayerBase(){}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始播放
|
|
|
|
|
* @param strUrl 视频url,支持rtsp/rtmp
|
|
|
|
|
*/
|
|
|
|
|
virtual void play(const string &strUrl) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 暂停或恢复
|
|
|
|
|
* @param bPause
|
|
|
|
|
*/
|
|
|
|
|
virtual void pause(bool bPause) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 中断播放
|
|
|
|
|
*/
|
|
|
|
|
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) {}
|
2018-09-18 23:49:48 +08:00
|
|
|
|
|
2019-05-08 15:27:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置播放恢复回调
|
|
|
|
|
* @param cb
|
|
|
|
|
*/
|
|
|
|
|
virtual void setOnResume( const function<void()> &cb) {}
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取播放进度,取值 0.0 ~ 1.0
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual float getProgress() const { return 0;}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 拖动进度条
|
|
|
|
|
* @param fProgress 进度,取值 0.0 ~ 1.0
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void seekTo(float fProgress) {}
|
2018-10-26 11:03:53 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置一个MediaSource,直接生产rtsp/rtmp代理
|
|
|
|
|
* @param src
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void setMediaSouce(const MediaSource::Ptr & src) {}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取丢包率,只支持rtsp
|
|
|
|
|
* @param trackType 音频或视频,TrackInvalid时为总丢包率
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2020-03-20 11:51:24 +08:00
|
|
|
|
virtual float getPacketLossRate(TrackType trackType) const {return 0; }
|
2019-12-03 16:10:02 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有track
|
|
|
|
|
*/
|
|
|
|
|
vector<Track::Ptr> getTracks(bool trackReady = true) const override{
|
|
|
|
|
return vector<Track::Ptr>();
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void onShutdown(const SockException &ex) {}
|
|
|
|
|
virtual void onPlayResult(const SockException &ex) {}
|
2019-05-08 15:27:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 暂停后恢复播放时间
|
|
|
|
|
*/
|
|
|
|
|
virtual void onResume(){};
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-12-03 16:10:02 +08:00
|
|
|
|
template<typename Parent,typename Delegate>
|
|
|
|
|
class PlayerImp : public Parent {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
typedef std::shared_ptr<PlayerImp> Ptr;
|
|
|
|
|
|
|
|
|
|
template<typename ...ArgsType>
|
|
|
|
|
PlayerImp(ArgsType &&...args):Parent(std::forward<ArgsType>(args)...){}
|
|
|
|
|
|
|
|
|
|
virtual ~PlayerImp(){}
|
|
|
|
|
void setOnShutdown(const function<void(const SockException &)> &cb) override {
|
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setOnShutdown(cb);
|
|
|
|
|
}
|
|
|
|
|
_shutdownCB = cb;
|
|
|
|
|
}
|
|
|
|
|
void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
|
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setOnPlayResult(cb);
|
|
|
|
|
}
|
|
|
|
|
_playResultCB = cb;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-05-08 15:27:37 +08:00
|
|
|
|
void setOnResume(const function<void()> &cb) override {
|
2019-12-03 16:10:02 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setOnResume(cb);
|
2019-05-08 15:27:37 +08:00
|
|
|
|
}
|
|
|
|
|
_resumeCB = cb;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 14:23:28 +08:00
|
|
|
|
bool isInited(int analysisMs) override{
|
2019-12-03 16:10:02 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
return _delegate->isInited(analysisMs);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-12-03 16:10:02 +08:00
|
|
|
|
return Parent::isInited(analysisMs);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
float getDuration() const override {
|
|
|
|
|
if (_delegate) {
|
|
|
|
|
return _delegate->getDuration();
|
|
|
|
|
}
|
|
|
|
|
return Parent::getDuration();
|
|
|
|
|
}
|
2017-08-14 22:20:53 +08:00
|
|
|
|
float getProgress() const override{
|
2019-12-03 16:10:02 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
return _delegate->getProgress();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-12-03 16:10:02 +08:00
|
|
|
|
return Parent::getProgress();
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void seekTo(float fProgress) override{
|
2019-12-03 16:10:02 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
return _delegate->seekTo(fProgress);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-12-03 16:10:02 +08:00
|
|
|
|
return Parent::seekTo(fProgress);
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2018-09-18 21:40:26 +08:00
|
|
|
|
|
|
|
|
|
void setMediaSouce(const MediaSource::Ptr & src) override {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setMediaSouce(src);
|
|
|
|
|
}
|
|
|
|
|
_pMediaSrc = src;
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2018-09-18 21:40:26 +08:00
|
|
|
|
|
2018-11-16 18:46:05 +08:00
|
|
|
|
vector<Track::Ptr> getTracks(bool trackReady = true) const override{
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
return _delegate->getTracks(trackReady);
|
|
|
|
|
}
|
|
|
|
|
return Parent::getTracks(trackReady);
|
|
|
|
|
}
|
2020-09-27 11:32:49 +08:00
|
|
|
|
|
|
|
|
|
std::shared_ptr<SockInfo> getSockInfo() const{
|
|
|
|
|
return dynamic_pointer_cast<SockInfo>(_delegate);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void onShutdown(const SockException &ex) override {
|
|
|
|
|
if (_shutdownCB) {
|
|
|
|
|
_shutdownCB(ex);
|
|
|
|
|
_shutdownCB = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onPlayResult(const SockException &ex) override {
|
|
|
|
|
if(_playResultCB) {
|
|
|
|
|
_playResultCB(ex);
|
|
|
|
|
_playResultCB = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onResume() override{
|
2019-05-08 15:27:37 +08:00
|
|
|
|
if(_resumeCB){
|
|
|
|
|
_resumeCB();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-18 21:40:26 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
function<void(const SockException &ex)> _shutdownCB;
|
|
|
|
|
function<void(const SockException &ex)> _playResultCB;
|
2019-05-08 15:27:37 +08:00
|
|
|
|
function<void()> _resumeCB;
|
2019-12-03 16:10:02 +08:00
|
|
|
|
std::shared_ptr<Delegate> _delegate;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
MediaSource::Ptr _pMediaSrc;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
2018-10-23 16:41:25 +08:00
|
|
|
|
|
2018-11-15 15:14:05 +08:00
|
|
|
|
|
|
|
|
|
class Demuxer : public PlayerBase{
|
|
|
|
|
public:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
class Listener{
|
|
|
|
|
public:
|
|
|
|
|
Listener() = default;
|
|
|
|
|
virtual ~Listener() = default;
|
|
|
|
|
virtual void onAddTrack(const Track::Ptr &track) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Demuxer(){};
|
|
|
|
|
virtual ~Demuxer(){};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回是否完成初始化完毕
|
|
|
|
|
* 在构造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监听器
|
|
|
|
|
*/
|
|
|
|
|
void setTrackListener(Listener *listener);
|
2019-12-26 11:53:19 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void onAddTrack(const Track::Ptr &track);
|
2018-11-15 15:14:05 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
Listener *_listener = nullptr;
|
|
|
|
|
AudioTrack::Ptr _audioTrack;
|
|
|
|
|
VideoTrack::Ptr _videoTrack;
|
|
|
|
|
Ticker _ticker;
|
|
|
|
|
float _fDuration = 0;
|
2018-11-15 15:14:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SRC_PLAYER_PLAYERBASE_H_ */
|