初步完成一些类的抽象

This commit is contained in:
xiongziliang 2018-09-19 12:34:29 +08:00
parent fab283da6f
commit 23c2de9acf
3 changed files with 218 additions and 33 deletions

View File

@ -35,6 +35,7 @@
#include "Network/Socket.h"
#include "Util/mini.h"
#include "Common/MediaSource.h"
#include "Util/RingBuffer.h"
using namespace std;
using namespace ZL::Util;
@ -44,6 +45,100 @@ using namespace ZL::Network;
namespace ZL {
namespace Player {
class TrackFrame : public Buffer {
public:
typedef std::shared_ptr<TrackFrame> Ptr;
virtual ~TrackFrame(){}
virtual uint32_t stamp() = 0;
};
class TrackFormat {
public:
typedef std::shared_ptr<TrackFormat> Ptr;
typedef RingBuffer<TrackFrame::Ptr> RingType;
typedef RingType::RingReader::Ptr ReaderType;
typedef enum {
VideoCodecInvalid = -1,
VideoCodecH264 = 0,
VideoCodecMax
} VideoCodecID;
typedef enum {
AudioCodecInvalid = -1,
AudioCodecAAC = 0,
AudioCodecMax
} AudioCodecID;
TrackFormat(){
_ring = std::make_shared<RingType>();
}
virtual ~TrackFormat(){}
virtual TrackType getTrackType() const = 0;
virtual int getCodecId() const = 0;
ReaderType attachReader(bool useBuffer = false){
return _ring->attach(useBuffer);
}
void writeFrame(const TrackFrame::Ptr &frame,bool keypos = true){
_ring->write(frame, keypos);
}
private:
RingType::Ptr _ring;
};
class VideoTrackFormat : public TrackFormat {
public:
TrackType getTrackType() const override { return TrackVideo;};
virtual int getVideoHeight() const = 0;
virtual int getVideoWidth() const = 0;
virtual float getVideoFps() const = 0;
};
class AudioTrackFormat : public TrackFormat {
public:
TrackType getTrackType() const override { return TrackAudio;};
virtual int getAudioSampleRate() const = 0;
virtual int getAudioSampleBit() const = 0;
virtual int getAudioChannel() const = 0;
};
class H264TrackFormat : public VideoTrackFormat{
public:
H264TrackFormat(const string &sps,const string &pps){
_sps = sps;
_pps = pps;
}
const string &getSps() const{
return _sps;
}
const string &getPps() const{
return _pps;
}
int getCodecId() const override{
return TrackFormat::VideoCodecH264;
}
private:
string _sps;
string _pps;
};
class AACTrackFormat : public AudioTrackFormat{
public:
AACTrackFormat(const string &aac_cfg){
_cfg = aac_cfg;
}
const string &getAacCfg() const{
return _cfg;
}
int getCodecId() const override{
return TrackFormat::AudioCodecAAC;
}
private:
string _cfg;
};
class MediaFormat {
public:
virtual ~MediaFormat(){};
@ -58,11 +153,6 @@ public:
virtual const string& getPps() const { static string null;return null; };
virtual const string& getSps() const { static string null;return null; };
virtual const string& getAudioCfg() const { static string null;return null; };
virtual bool containAudio() const { return false; };
virtual bool containVideo() const { return false; };
virtual float getDuration() const { return 0;};
};
class PlayerBase : public MediaFormat,public mINI{
@ -105,6 +195,13 @@ public:
virtual bool isInited() const { return true; };
//TrackVideo = 0, TrackAudio = 1
virtual float getRtpLossRate(int trackType) const {return 0; };
virtual float getDuration() const { return 0;};
virtual bool containAudio() const { return false; };
virtual bool containVideo() const { return false; };
virtual int getTrackCount() const { return 0;};
virtual TrackFormat::Ptr getTrack(int index) const {return nullptr;};
protected:
virtual void onShutdown(const SockException &ex) {};
virtual void onPlayResult(const SockException &ex) {};

View File

@ -37,9 +37,10 @@ using namespace std;
using namespace ZL::Util;
typedef enum {
TrackInvalid = -1,
TrackVideo = 0,
TrackAudio
TrackAudio,
TrackInvalid,
TrackMax
} TrackType;
class RtspTrack{

View File

@ -18,17 +18,47 @@ using namespace ZL::Rtsp;
namespace ZL{
namespace Rtsp{
/**
* sdp基类
*/
class Sdp {
public:
typedef std::shared_ptr<Sdp> Ptr;
virtual ~Sdp(){}
virtual string getSdp() { return "";};
virtual TrackType getTrackType() { return TrackInvalid;};
virtual RtpMaker::Ptr createRtpMaker(const RtpMaker::onGetRTP &cb,uint32_t ui32Ssrc, int iMtuSize) { return nullptr;};
/**
* sdp字符串
* @return
*/
virtual string getSdp() const { return "";};
/**
* track类型
* @return
*/
virtual TrackType getTrackType() const { return TrackInvalid;};
/**
* rtp生成器
* @param cb lambad
* @param ui32Ssrc rtp ssrc
* @param iMtuSize rtp mtu
* @return
*/
virtual RtpMaker::Ptr createRtpMaker(const RtpMaker::onGetRTP &cb,uint32_t ui32Ssrc, int iMtuSize) const{ return nullptr;};
};
/**
* sdp中除音视频外的其他描述部分
*/
class SdpTitle : public Sdp{
public:
/**
* title类型sdp
* @param dur_sec rtsp点播时长0
* @param header sdp描述
* @param version sdp版本
*/
SdpTitle(float dur_sec = 0,
const map<string,string> &header = map<string,string>(),
int version = 0){
@ -53,7 +83,7 @@ public:
}
_printer << "a=control:*\r\n";
}
string getSdp() override {
string getSdp() const override {
return _printer;
}
@ -61,8 +91,21 @@ private:
_StrPrinter _printer;
};
/**
* h264类型sdp
*/
class SdpH264 : public Sdp {
public:
/**
*
* @param sps 264 sps,0x00000001
* @param pps 264 pps,0x00000001
* @param sample_rate 90000
* @param playload_type rtp playload type 96
* @param track_id trackID TrackVideo
* @param bitrate
*/
SdpH264(const string &sps,
const string &pps,
int sample_rate = 90000,
@ -100,15 +143,15 @@ public:
_printer << "a=control:trackID=" << track_id << "\r\n";
}
string getSdp() override {
string getSdp() const override {
return _printer;
}
TrackType getTrackType() override {
TrackType getTrackType() const override {
return TrackVideo;
};
RtpMaker::Ptr createRtpMaker(const RtpMaker::onGetRTP &cb,uint32_t ui32Ssrc, int iMtuSize) override{
RtpMaker::Ptr createRtpMaker(const RtpMaker::onGetRTP &cb,uint32_t ui32Ssrc, int iMtuSize) const override{
return std::make_shared<RtpMaker_H264>(cb,ui32Ssrc,iMtuSize,_sample_rate,_playload_type,_track_id * 2);
};
@ -121,8 +164,20 @@ private:
};
/**
* aac类型SDP
*/
class SdpAAC : public Sdp {
public:
/**
* aac sdp
* @param aac_cfg aac两个字节的配置描述
* @param sample_rate
* @param playload_type rtp playload type 96
* @param track_id trackID TrackVideo
* @param bitrate
*/
SdpAAC(const string &aac_cfg,
int sample_rate,
int playload_type = 98,
@ -145,15 +200,15 @@ public:
_printer << "a=control:trackID=" << track_id << "\r\n";
}
string getSdp() override {
string getSdp() const override {
return _printer;
}
TrackType getTrackType() override {
TrackType getTrackType() const override {
return TrackAudio;
};
RtpMaker::Ptr createRtpMaker(const RtpMaker::onGetRTP &cb,uint32_t ui32Ssrc, int iMtuSize) override{
RtpMaker::Ptr createRtpMaker(const RtpMaker::onGetRTP &cb,uint32_t ui32Ssrc, int iMtuSize) const override{
return std::make_shared<RtpMaker_AAC>(cb,ui32Ssrc,iMtuSize,_sample_rate,_playload_type,_track_id * 2);
};
private:
@ -163,34 +218,67 @@ private:
int _track_id;
};
/**
* rtsp生成器
*/
class RtspMaker{
public:
RtspMaker(const RtpMaker::onGetRTP &callback) : _vec_rtp_maker(8){
/**
*
* @param callback rtp回调lambad
*/
RtspMaker(const RtpMaker::onGetRTP &callback) : _vec_rtp_maker(TrackMax){
_callback = callback;
}
template<typename First,typename ...Others>
void addTrack(First && first,Others &&...others){
addTrack(std::forward<First>(first));
addTrack(std::forward<Others>(others)...);
}
template<typename First>
void addTrack(First && first){
_printer << first->getSdp();
if(first->getTrackType() >= 0){
auto rtpMaker = first-> createRtpMaker(_callback, 0,1400);
_vec_rtp_maker[first->getTrackType()] = rtpMaker;
/**
* track
* @param sdp
* @param ssrc rtp ssrc
* @param mtu rtp mtu
* @return
*/
bool addTrack(const Sdp & sdp,uint32_t ssrc = 0,int mtu = 1400){
auto type = sdp.getTrackType();
if(type < 0 || type >= _vec_rtp_maker.size()){
return false;
}
if(_vec_rtp_maker[type]){
return false;
}
if(ssrc == 0){
ssrc = ((uint64_t) &sdp) & 0xFFFFFFFF;
}
auto rtpMaker = sdp.createRtpMaker(_callback, ssrc,mtu);
_vec_rtp_maker[sdp.getTrackType()] = rtpMaker;
_printer << sdp.getSdp();
return true;
}
virtual ~RtspMaker() {};
/**
* SDP字符串
* @return SDP字符串
*/
string getSdp() {
return _printer;
}
/**
* RTP数据包
* @param type
* @param pcData
* @param iDataLen
* @param uiStamp
* @return
*/
bool makeRtp(TrackType type,const char *pcData, int iDataLen, uint32_t uiStamp){
if(type < 0 || type > _vec_rtp_maker.size()){
if(type < 0 || type >= _vec_rtp_maker.size()){
return false;
}
auto track = _vec_rtp_maker[type];
@ -205,7 +293,6 @@ private:
vector<RtpMaker::Ptr> _vec_rtp_maker;
_StrPrinter _printer;
RtpMaker::onGetRTP _callback;
};