diff --git a/src/RTP/AACRtpCodec.cpp b/src/RTP/AACRtpCodec.cpp index 64c89afe..318f0479 100644 --- a/src/RTP/AACRtpCodec.cpp +++ b/src/RTP/AACRtpCodec.cpp @@ -9,12 +9,12 @@ AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc, uint32_t ui32SampleRate, uint8_t ui8PlayloadType, uint8_t ui8Interleaved) : - RtpEncoder(ui32Ssrc, - ui32MtuSize, - ui32SampleRate, - ui8PlayloadType, - ui8Interleaved) { - + RtpInfo(ui32Ssrc, + ui32MtuSize, + ui32SampleRate, + ui8PlayloadType, + ui8Interleaved), + AACRtpDecoder(ui32SampleRate){ } void AACRtpEncoder::inputFrame(const Frame::Ptr &frame, bool key_pos) { diff --git a/src/RTP/AACRtpCodec.h b/src/RTP/AACRtpCodec.h index e679d27a..2950fca5 100644 --- a/src/RTP/AACRtpCodec.h +++ b/src/RTP/AACRtpCodec.h @@ -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: diff --git a/src/RTP/H264RtpCodec.cpp b/src/RTP/H264RtpCodec.cpp index cf8788cb..94b7885b 100644 --- a/src/RTP/H264RtpCodec.cpp +++ b/src/RTP/H264RtpCodec.cpp @@ -109,11 +109,11 @@ H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc, uint32_t ui32SampleRate, uint8_t ui8PlayloadType, uint8_t ui8Interleaved) : - RtpEncoder(ui32Ssrc, - ui32MtuSize, - ui32SampleRate, - ui8PlayloadType, - ui8Interleaved) { + RtpInfo(ui32Ssrc, + ui32MtuSize, + ui32SampleRate, + ui8PlayloadType, + ui8Interleaved) { } void H264RtpEncoder::inputFrame(const Frame::Ptr &frame, bool key_pos) { diff --git a/src/RTP/H264RtpCodec.h b/src/RTP/H264RtpCodec.h index 2574a8fe..348a5ae9 100644 --- a/src/RTP/H264RtpCodec.h +++ b/src/RTP/H264RtpCodec.h @@ -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: diff --git a/src/RTP/RtpCodec.cpp b/src/RTP/RtpCodec.cpp index b36437bf..29bc21ed 100644 --- a/src/RTP/RtpCodec.cpp +++ b/src/RTP/RtpCodec.cpp @@ -3,16 +3,21 @@ // #include "RtpCodec.h" +#include "AACRtpCodec.h" +#include "H264RtpCodec.h" -RtpEncoder::RtpEncoder(uint32_t ui32Ssrc, - uint32_t ui32MtuSize, - uint32_t ui32SampleRate, - uint8_t ui8PlayloadType, - uint8_t ui8Interleaved) : - RtpInfo(ui32Ssrc, - ui32MtuSize, - ui32SampleRate, - ui8PlayloadType, - ui8Interleaved) { - +RtpCodec::Ptr RtpCodec::getRtpCodec(CodecId codecId, + uint32_t ui32Ssrc, + uint32_t ui32MtuSize, + uint32_t ui32SampleRate, + uint8_t ui8PlayloadType, + uint8_t ui8Interleaved) { + switch (codecId){ + case CodecH264: + return std::make_shared(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved); + case CodecAAC: + return std::make_shared(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved); + default: + return nullptr; + } } diff --git a/src/RTP/RtpCodec.h b/src/RTP/RtpCodec.h index d0262bef..830e3659 100644 --- a/src/RTP/RtpCodec.h +++ b/src/RTP/RtpCodec.h @@ -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 Ptr; + + TrackType getTrackType() const { + return type; + } + + CodecId getCodecId() const { + return CodecInvalid; + } +public: uint8_t interleaved; uint8_t PT; bool mark; @@ -71,10 +80,13 @@ public: typedef std::shared_ptr Ptr; RtpInfo(uint32_t ui32Ssrc, - uint32_t ui32MtuSize, - uint32_t ui32SampleRate, - uint8_t ui8PlayloadType, - uint8_t ui8Interleaved) { + uint32_t ui32MtuSize, + 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,26 +142,15 @@ public: typedef std::shared_ptr Ptr; RtpCodec(){} virtual ~RtpCodec(){} + + static Ptr getRtpCodec(CodecId codecId, + uint32_t ui32Ssrc, + uint32_t ui32MtuSize, + uint32_t ui32SampleRate, + uint8_t ui8PlayloadType, + uint8_t ui8Interleaved); }; -class RtpEncoder : public RtpInfo, public RtpCodec{ -public: - typedef std::shared_ptr 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() {} -}; diff --git a/src/Rtsp/RtspEncoder.h b/src/Rtsp/RtspEncoder.h index 49c7f5a8..64f697b9 100644 --- a/src/Rtsp/RtspEncoder.h +++ b/src/Rtsp/RtspEncoder.h @@ -18,7 +18,10 @@ namespace Rtsp{ class Sdp : public TrackFormat , public RtpRingInterface{ public: typedef std::shared_ptr 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 &header = map(), - 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(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(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); } /**