Commit 23c2de9a by xiongziliang

初步完成一些类的抽象

parent fab283da
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "Network/Socket.h" #include "Network/Socket.h"
#include "Util/mini.h" #include "Util/mini.h"
#include "Common/MediaSource.h" #include "Common/MediaSource.h"
#include "Util/RingBuffer.h"
using namespace std; using namespace std;
using namespace ZL::Util; using namespace ZL::Util;
...@@ -44,6 +45,100 @@ using namespace ZL::Network; ...@@ -44,6 +45,100 @@ using namespace ZL::Network;
namespace ZL { namespace ZL {
namespace Player { 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 { class MediaFormat {
public: public:
virtual ~MediaFormat(){}; virtual ~MediaFormat(){};
...@@ -58,11 +153,6 @@ public: ...@@ -58,11 +153,6 @@ public:
virtual const string& getPps() const { static string null;return null; }; virtual const string& getPps() const { static string null;return null; };
virtual const string& getSps() 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 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{ class PlayerBase : public MediaFormat,public mINI{
...@@ -105,6 +195,13 @@ public: ...@@ -105,6 +195,13 @@ public:
virtual bool isInited() const { return true; }; virtual bool isInited() const { return true; };
//TrackVideo = 0, TrackAudio = 1 //TrackVideo = 0, TrackAudio = 1
virtual float getRtpLossRate(int trackType) const {return 0; }; 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: protected:
virtual void onShutdown(const SockException &ex) {}; virtual void onShutdown(const SockException &ex) {};
virtual void onPlayResult(const SockException &ex) {}; virtual void onPlayResult(const SockException &ex) {};
......
...@@ -37,9 +37,10 @@ using namespace std; ...@@ -37,9 +37,10 @@ using namespace std;
using namespace ZL::Util; using namespace ZL::Util;
typedef enum { typedef enum {
TrackInvalid = -1,
TrackVideo = 0, TrackVideo = 0,
TrackAudio TrackAudio,
TrackInvalid,
TrackMax
} TrackType; } TrackType;
class RtspTrack{ class RtspTrack{
......
...@@ -18,17 +18,47 @@ using namespace ZL::Rtsp; ...@@ -18,17 +18,47 @@ using namespace ZL::Rtsp;
namespace ZL{ namespace ZL{
namespace Rtsp{ namespace Rtsp{
/**
* sdp基类
*/
class Sdp { class Sdp {
public: public:
typedef std::shared_ptr<Sdp> Ptr; typedef std::shared_ptr<Sdp> Ptr;
virtual ~Sdp(){} virtual ~Sdp(){}
virtual string getSdp() { return "";}; /**
virtual TrackType getTrackType() { return TrackInvalid;}; * 获取sdp字符串
virtual RtpMaker::Ptr createRtpMaker(const RtpMaker::onGetRTP &cb,uint32_t ui32Ssrc, int iMtuSize) { return nullptr;}; * @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{ class SdpTitle : public Sdp{
public: public:
/**
* 构造title类型sdp
* @param dur_sec rtsp点播时长,0代表直播,单位秒
* @param header 自定义sdp描述
* @param version sdp版本
*/
SdpTitle(float dur_sec = 0, SdpTitle(float dur_sec = 0,
const map<string,string> &header = map<string,string>(), const map<string,string> &header = map<string,string>(),
int version = 0){ int version = 0){
...@@ -53,7 +83,7 @@ public: ...@@ -53,7 +83,7 @@ public:
} }
_printer << "a=control:*\r\n"; _printer << "a=control:*\r\n";
} }
string getSdp() override { string getSdp() const override {
return _printer; return _printer;
} }
...@@ -61,8 +91,21 @@ private: ...@@ -61,8 +91,21 @@ private:
_StrPrinter _printer; _StrPrinter _printer;
}; };
/**
* h264类型sdp
*/
class SdpH264 : public Sdp { class SdpH264 : public Sdp {
public: 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, SdpH264(const string &sps,
const string &pps, const string &pps,
int sample_rate = 90000, int sample_rate = 90000,
...@@ -100,15 +143,15 @@ public: ...@@ -100,15 +143,15 @@ public:
_printer << "a=control:trackID=" << track_id << "\r\n"; _printer << "a=control:trackID=" << track_id << "\r\n";
} }
string getSdp() override { string getSdp() const override {
return _printer; return _printer;
} }
TrackType getTrackType() override { TrackType getTrackType() const override {
return TrackVideo; 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); return std::make_shared<RtpMaker_H264>(cb,ui32Ssrc,iMtuSize,_sample_rate,_playload_type,_track_id * 2);
}; };
...@@ -121,8 +164,20 @@ private: ...@@ -121,8 +164,20 @@ private:
}; };
/**
* aac类型SDP
*/
class SdpAAC : public Sdp { class SdpAAC : public Sdp {
public: 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, SdpAAC(const string &aac_cfg,
int sample_rate, int sample_rate,
int playload_type = 98, int playload_type = 98,
...@@ -145,15 +200,15 @@ public: ...@@ -145,15 +200,15 @@ public:
_printer << "a=control:trackID=" << track_id << "\r\n"; _printer << "a=control:trackID=" << track_id << "\r\n";
} }
string getSdp() override { string getSdp() const override {
return _printer; return _printer;
} }
TrackType getTrackType() override { TrackType getTrackType() const override {
return TrackAudio; 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); return std::make_shared<RtpMaker_AAC>(cb,ui32Ssrc,iMtuSize,_sample_rate,_playload_type,_track_id * 2);
}; };
private: private:
...@@ -163,34 +218,67 @@ private: ...@@ -163,34 +218,67 @@ private:
int _track_id; int _track_id;
}; };
/**
* rtsp生成器
*/
class RtspMaker{ class RtspMaker{
public: 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; _callback = callback;
} }
template<typename First,typename ...Others> /**
void addTrack(First && first,Others &&...others){ * 添加音视频track
addTrack(std::forward<First>(first)); * @param sdp 媒体描述
addTrack(std::forward<Others>(others)...); * @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;
} }
template<typename First>
void addTrack(First && first){ if(ssrc == 0){
_printer << first->getSdp(); ssrc = ((uint64_t) &sdp) & 0xFFFFFFFF;
if(first->getTrackType() >= 0){
auto rtpMaker = first-> createRtpMaker(_callback, 0,1400);
_vec_rtp_maker[first->getTrackType()] = rtpMaker;
} }
auto rtpMaker = sdp.createRtpMaker(_callback, ssrc,mtu);
_vec_rtp_maker[sdp.getTrackType()] = rtpMaker;
_printer << sdp.getSdp();
return true;
} }
virtual ~RtspMaker() {}; virtual ~RtspMaker() {};
/**
* 获取完整的SDP字符串
* @return SDP字符串
*/
string getSdp() { string getSdp() {
return _printer; return _printer;
} }
/**
* 打包RTP数据包
* @param type 媒体类型
* @param pcData 媒体数据
* @param iDataLen 媒体数据长度
* @param uiStamp 媒体时间戳,单位毫秒
* @return 是否成功
*/
bool makeRtp(TrackType type,const char *pcData, int iDataLen, uint32_t uiStamp){ 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; return false;
} }
auto track = _vec_rtp_maker[type]; auto track = _vec_rtp_maker[type];
...@@ -205,7 +293,6 @@ private: ...@@ -205,7 +293,6 @@ private:
vector<RtpMaker::Ptr> _vec_rtp_maker; vector<RtpMaker::Ptr> _vec_rtp_maker;
_StrPrinter _printer; _StrPrinter _printer;
RtpMaker::onGetRTP _callback; RtpMaker::onGetRTP _callback;
}; };
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论