Commit 23074048 by xiongziliang

完善rtsp相关代码逻辑

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