Commit 23074048 by xiongziliang

完善rtsp相关代码逻辑

parent c1e91620
......@@ -9,12 +9,12 @@ AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) :
RtpEncoder(ui32Ssrc,
RtpInfo(ui32Ssrc,
ui32MtuSize,
ui32SampleRate,
ui8PlayloadType,
ui8Interleaved) {
ui8Interleaved),
AACRtpDecoder(ui32SampleRate){
}
void AACRtpEncoder::inputFrame(const Frame::Ptr &frame, bool key_pos) {
......
......@@ -45,7 +45,7 @@ private:
/**
* aac adts转rtp类
*/
class AACRtpEncoder : public RtpEncoder {
class AACRtpEncoder : public AACRtpDecoder , public RtpInfo {
public:
/**
* @param ui32Ssrc ssrc
......@@ -67,15 +67,6 @@ public:
* @param key_pos 此参数内部强制转换为false,请忽略之
*/
void inputFrame(const Frame::Ptr &frame, bool key_pos = false) override;
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return CodecAAC;
}
private:
void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private:
......
......@@ -109,7 +109,7 @@ H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) :
RtpEncoder(ui32Ssrc,
RtpInfo(ui32Ssrc,
ui32MtuSize,
ui32SampleRate,
ui8PlayloadType,
......
......@@ -44,7 +44,7 @@ private:
/**
* 264 rtp打包类
*/
class H264RtpEncoder : public RtpEncoder{
class H264RtpEncoder : public H264RtpDecoder ,public RtpInfo{
public:
/**
......@@ -67,14 +67,6 @@ public:
* @param key_pos
*/
void inputFrame(const Frame::Ptr &frame, bool key_pos) override;
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH264;
}
private:
void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private:
......
......@@ -3,16 +3,21 @@
//
#include "RtpCodec.h"
#include "AACRtpCodec.h"
#include "H264RtpCodec.h"
RtpEncoder::RtpEncoder(uint32_t ui32Ssrc,
RtpCodec::Ptr RtpCodec::getRtpCodec(CodecId codecId,
uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) :
RtpInfo(ui32Ssrc,
ui32MtuSize,
ui32SampleRate,
ui8PlayloadType,
ui8Interleaved) {
uint8_t ui8Interleaved) {
switch (codecId){
case CodecH264:
return std::make_shared<H264RtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
case CodecAAC:
return std::make_shared<AACRtpEncoder>(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved);
default:
return nullptr;
}
}
......@@ -14,9 +14,18 @@ using namespace std;
using namespace ZL::Util;
using namespace ZL::Player;
class RtpPacket {
class RtpPacket : public CodecInfo {
public:
typedef std::shared_ptr<RtpPacket> Ptr;
TrackType getTrackType() const {
return type;
}
CodecId getCodecId() const {
return CodecInvalid;
}
public:
uint8_t interleaved;
uint8_t PT;
bool mark;
......@@ -75,6 +84,9 @@ public:
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) {
if(ui32Ssrc == 0){
ui32Ssrc = ((uint64_t)this) & 0xFFFFFFFF;
}
m_ui32Ssrc = ui32Ssrc;
m_ui32SampleRate = ui32SampleRate;
m_ui32MtuSize = ui32MtuSize;
......@@ -130,29 +142,18 @@ public:
typedef std::shared_ptr<RtpCodec> Ptr;
RtpCodec(){}
virtual ~RtpCodec(){}
};
class RtpEncoder : public RtpInfo, public RtpCodec{
public:
typedef std::shared_ptr<RtpEncoder> Ptr;
/**
* @param ui32Ssrc ssrc
* @param ui32MtuSize mtu大小
* @param ui32SampleRate 采样率,强制为90000
* @param ui8PlayloadType pt类型
* @param ui8Interleaved rtsp interleaved
*/
RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize = 1400,
uint32_t ui32SampleRate = 90000,
uint8_t ui8PlayloadType = 96,
uint8_t ui8Interleaved = TrackVideo * 2);
~RtpEncoder() {}
static Ptr getRtpCodec(CodecId codecId,
uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved);
};
#endif //ZLMEDIAKIT_RTPCODEC_H
......@@ -18,7 +18,10 @@ namespace Rtsp{
class Sdp : public TrackFormat , public RtpRingInterface{
public:
typedef std::shared_ptr<Sdp> Ptr;
Sdp(){}
Sdp(uint32_t sample_rate, uint8_t playload_type){
_sample_rate = sample_rate;
_playload_type = playload_type;
}
virtual ~Sdp(){}
/**
* 获取sdp字符串
......@@ -50,8 +53,6 @@ public:
_encoder->inputRtp(rtp,key_pos);
}
virtual void createRtpEncoder(uint32_t ssrc, int mtu) = 0;
void setFrameRing(const FrameRingInterface::RingType::Ptr &ring) override{
if(_encoder){
_encoder->setFrameRing(ring);
......@@ -63,8 +64,18 @@ public:
}
}
protected:
RtpEncoder::Ptr _encoder;
virtual void createRtpEncoder(uint32_t ssrc, int mtu) {
_encoder = RtpCodec::getRtpCodec (getCodecId(),
ssrc,
mtu,
_sample_rate,
_playload_type,
getTrackType() * 2);
}
private:
RtpCodec::Ptr _encoder;
uint8_t _playload_type;
uint32_t _sample_rate;
};
/**
......@@ -81,7 +92,7 @@ public:
*/
SdpTitle(float dur_sec = 0,
const map<string,string> &header = map<string,string>(),
int version = 0){
int version = 0) : Sdp(0,0){
_printer << "v=" << version << "\r\n";
if(!header.empty()){
......@@ -106,7 +117,6 @@ public:
string getSdp() const override {
return _printer;
}
void createRtpEncoder(uint32_t ssrc, int mtu) override {}
private:
_StrPrinter _printer;
};
......@@ -130,13 +140,7 @@ public:
const string &pps,
int sample_rate = 90000,
int playload_type = 96,
int track_id = TrackVideo,
int bitrate = 4000) {
_playload_type = playload_type;
_sample_rate = sample_rate;
_track_id = track_id;
int bitrate = 4000) : Sdp(sample_rate,playload_type) {
//视频通道
_printer << "m=video 0 RTP/AVP " << playload_type << "\r\n";
_printer << "b=AS:" << bitrate << "\r\n";
......@@ -160,7 +164,7 @@ public:
memset(strTemp, 0, 100);
av_base64_encode(strTemp, 100, (uint8_t *) strPPS.data(), strPPS.size());
_printer << strTemp << "\r\n";
_printer << "a=control:trackID=" << track_id << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
}
string getSdp() const override {
......@@ -174,20 +178,8 @@ public:
CodecId getCodecId() const override {
return CodecH264;
}
void createRtpEncoder(uint32_t ssrc, int mtu) override{
_encoder = std::make_shared<H264RtpEncoder>(ssrc,
mtu,
_sample_rate,
_playload_type,
_track_id * 2);
}
private:
_StrPrinter _printer;
int _playload_type;
int _sample_rate;
int _track_id;
};
......@@ -208,13 +200,7 @@ public:
SdpAAC(const string &aac_cfg,
int sample_rate,
int playload_type = 98,
int track_id = TrackAudio,
int bitrate = 128){
_playload_type = playload_type;
_sample_rate = sample_rate;
_track_id = track_id;
int bitrate = 128) : Sdp(sample_rate,playload_type){
_printer << "m=audio 0 RTP/AVP " << playload_type << "\r\n";
_printer << "b=AS:" << bitrate << "\r\n";
_printer << "a=rtpmap:" << playload_type << " MPEG4-GENERIC/" << sample_rate << "\r\n";
......@@ -224,7 +210,7 @@ public:
_printer << "a=fmtp:" << playload_type << " streamtype=5;profile-level-id=1;mode=AAC-hbr;"
<< "sizelength=13;indexlength=3;indexdeltalength=3;config="
<< configStr << "\r\n";
_printer << "a=control:trackID=" << track_id << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
}
string getSdp() const override {
......@@ -237,31 +223,8 @@ public:
CodecId getCodecId() const override {
return CodecAAC;
}
void createRtpEncoder(uint32_t ssrc,
int mtu) override{
_encoder = std::make_shared<AACRtpEncoder>(ssrc,
mtu,
_sample_rate,
_playload_type,
_track_id * 2);
}
void setFrameRing(const FrameRingInterface::RingType::Ptr &ring) override{
if(_encoder){
_encoder->setFrameRing(ring);
}
}
void setRtpRing(const RtpRingInterface::RingType::Ptr &ring) override{
if(_encoder){
_encoder->setRtpRing(ring);
}
}
private:
_StrPrinter _printer;
int _playload_type;
int _sample_rate;
int _track_id;
};
/**
......@@ -329,7 +292,11 @@ public:
* @param key_pos 是否为关键帧的第一个rtp包
*/
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override {
_rtpRing->write(rtp,key_pos);
auto it = _sdp_map.find(rtp->getTrackType());
if(it == _sdp_map.end()){
return ;
}
it->second->inputRtp(rtp,key_pos);
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论