2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
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
|
|
|
|
|
2018-10-25 15:45:38 +08:00
|
|
|
|
class DemuxerBase {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<DemuxerBase> Ptr;
|
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取节目总时长,单位秒
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-10-25 15:45:38 +08:00
|
|
|
|
virtual float getDuration() const { return 0;}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否初始化完毕,完毕后方可调用getTrack方法
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-11-15 15:14:05 +08:00
|
|
|
|
virtual bool isInited() { return true; }
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取全部的Track
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-10-25 15:45:38 +08:00
|
|
|
|
virtual vector<Track::Ptr> getTracks() const { return vector<Track::Ptr>();}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取特定Track
|
|
|
|
|
* @param type
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-10-26 11:03:53 +08:00
|
|
|
|
virtual Track::Ptr getTrack(TrackType type) const {
|
|
|
|
|
auto tracks = getTracks();
|
|
|
|
|
for(auto &track : tracks){
|
|
|
|
|
if(track->getTrackType() == type){
|
|
|
|
|
return track;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-10-25 15:45:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlayerBase : public DemuxerBase, public mINI{
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<PlayerBase> Ptr;
|
|
|
|
|
typedef enum {
|
|
|
|
|
RTP_TCP = 0,
|
|
|
|
|
RTP_UDP = 1,
|
|
|
|
|
RTP_MULTICAST = 2,
|
|
|
|
|
} eRtpType;
|
|
|
|
|
static Ptr createPlayer(const char* strUrl);
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
|
|
|
|
//指定网卡ip
|
2018-05-21 23:18:17 +08:00
|
|
|
|
static const char kNetAdapter[];
|
2018-07-02 15:43:37 +08:00
|
|
|
|
//设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播)
|
|
|
|
|
//设置方法:player[PlayerBase::kRtpType] = 0/1/2;
|
|
|
|
|
static const char kRtpType[];
|
|
|
|
|
//rtsp认证用户名
|
|
|
|
|
static const char kRtspUser[];
|
|
|
|
|
//rtsp认证用用户密码,可以是明文也可以是md5,md5密码生成方式 md5(username:realm:password)
|
|
|
|
|
static const char kRtspPwd[];
|
|
|
|
|
//rtsp认证用用户密码是否为md5
|
|
|
|
|
static const char kRtspPwdIsMD5[];
|
2018-05-21 23:18:17 +08:00
|
|
|
|
|
2018-10-23 16:41:25 +08:00
|
|
|
|
PlayerBase(){}
|
|
|
|
|
virtual ~PlayerBase(){}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始播放
|
|
|
|
|
* @param strUrl 视频url,支持rtsp/rtmp
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void play(const char* strUrl) {}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 暂停或恢复
|
|
|
|
|
* @param bPause
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void pause(bool bPause) {}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 中断播放
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void teardown() {}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置异常中断回调
|
|
|
|
|
* @param cb
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void setOnShutdown( const function<void(const SockException &)> &cb) {}
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置播放结果回调
|
|
|
|
|
* @param cb
|
|
|
|
|
*/
|
2018-10-23 16:41:25 +08:00
|
|
|
|
virtual void setOnPlayResult( const function<void(const SockException &ex)> &cb) {}
|
2018-09-18 23:49:48 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +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
|
|
|
|
|
*/
|
|
|
|
|
virtual float getPacketLossRate(TrackType trackType) const {return 0; }
|
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) {}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Parent,typename Parser>
|
|
|
|
|
class PlayerImp : public Parent
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<PlayerImp> Ptr;
|
2018-10-23 16:41:25 +08:00
|
|
|
|
PlayerImp(){}
|
|
|
|
|
virtual ~PlayerImp(){}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void setOnShutdown(const function<void(const SockException &)> &cb) override {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
_parser->setOnShutdown(cb);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_shutdownCB = cb;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
_parser->setOnPlayResult(cb);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_playResultCB = cb;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 15:14:05 +08:00
|
|
|
|
bool isInited() override{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
return _parser->isInited();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
return PlayerBase::isInited();
|
|
|
|
|
}
|
|
|
|
|
float getDuration() const override {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
return _parser->getDuration();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
return PlayerBase::getDuration();
|
|
|
|
|
}
|
2017-08-14 22:20:53 +08:00
|
|
|
|
float getProgress() const override{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
return _parser->getProgress();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-08-14 22:20:53 +08:00
|
|
|
|
return PlayerBase::getProgress();
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void seekTo(float fProgress) override{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
return _parser->seekTo(fProgress);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
return PlayerBase::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 {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
return _parser->setMediaSouce(src);
|
2018-09-18 21:40:26 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_pMediaSrc = src;
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2018-09-18 21:40:26 +08:00
|
|
|
|
|
2018-10-23 18:39:17 +08:00
|
|
|
|
vector<Track::Ptr> getTracks() const override{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_parser) {
|
|
|
|
|
return _parser->getTracks();
|
2018-10-23 18:39:17 +08:00
|
|
|
|
}
|
|
|
|
|
return PlayerBase::getTracks();
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
|
|
|
|
void onShutdown(const SockException &ex) override {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_shutdownCB) {
|
|
|
|
|
_shutdownCB(ex);
|
2018-11-15 15:14:05 +08:00
|
|
|
|
_shutdownCB = nullptr;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-15 15:14:05 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void onPlayResult(const SockException &ex) override {
|
2018-11-15 15:14:05 +08:00
|
|
|
|
if(!_playResultCB){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(ex){
|
|
|
|
|
//播放失败,则立即回调
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_playResultCB(ex);
|
|
|
|
|
_playResultCB = nullptr;
|
2018-11-15 15:14:05 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//播放成功
|
|
|
|
|
|
|
|
|
|
if(isInited()){
|
|
|
|
|
//初始化完毕则立即回调
|
|
|
|
|
_playResultCB(ex);
|
|
|
|
|
_playResultCB = nullptr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//播放成功却未初始化完毕
|
|
|
|
|
}
|
|
|
|
|
void checkInited(){
|
|
|
|
|
if(!_playResultCB){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(isInited()){
|
|
|
|
|
_playResultCB(SockException(Err_success,"play success"));
|
|
|
|
|
_playResultCB = nullptr;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-18 21:40:26 +08:00
|
|
|
|
protected:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
function<void(const SockException &ex)> _shutdownCB;
|
|
|
|
|
function<void(const SockException &ex)> _playResultCB;
|
|
|
|
|
std::shared_ptr<Parser> _parser;
|
|
|
|
|
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:
|
|
|
|
|
Demuxer(){};
|
|
|
|
|
virtual ~Demuxer(){};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回是否完成初始化完毕
|
|
|
|
|
* 在构造RtspDemuxer对象时有些rtsp的sdp不包含sps pps信息
|
|
|
|
|
* 所以要等待接收到到sps的rtp包后才能完成
|
|
|
|
|
*
|
|
|
|
|
* 在构造RtmpDemuxer对象时是无法获取sps pps aac_cfg等这些信息,
|
|
|
|
|
* 所以要调用inputRtmp后才会获取到这些信息,这时才初始化成功
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool isInited() override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有可用Track,请在isInited()返回true时调用
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
vector<Track::Ptr> getTracks() const override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取节目总时长
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
float getDuration() const override;
|
|
|
|
|
protected:
|
|
|
|
|
AudioTrack::Ptr _audioTrack;
|
|
|
|
|
VideoTrack::Ptr _videoTrack;
|
|
|
|
|
Ticker _ticker;
|
|
|
|
|
float _fDuration = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SRC_PLAYER_PLAYERBASE_H_ */
|