From bc583e80b5aabb066a500b96c89d7bcd3c97daaf Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Mon, 24 Jan 2022 14:23:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6pr:=20#1391?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Extension/Factory.cpp | 7 ++++--- src/Extension/G711Rtp.cpp | 40 ++++++++++++++++++++++----------------- src/Extension/G711Rtp.h | 13 +++++++------ 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/Extension/Factory.cpp b/src/Extension/Factory.cpp index b1614bf9..9ba2168b 100644 --- a/src/Extension/Factory.cpp +++ b/src/Extension/Factory.cpp @@ -122,11 +122,12 @@ RtpCodec::Ptr Factory::getRtpEncoderBySdp(const Sdp::Ptr &sdp) { case CodecL16 : case CodecOpus : return std::make_shared(codec_id, ssrc, mtu, sample_rate, pt, interleaved); case CodecG711A : - case CodecG711U : - if(pt == Rtsp::PT_PCMA || pt == Rtsp::PT_PCMU){ - return std::make_shared(codec_id, ssrc, mtu, sample_rate, pt, interleaved,1); + case CodecG711U : { + if (pt == Rtsp::PT_PCMA || pt == Rtsp::PT_PCMU) { + return std::make_shared(codec_id, ssrc, mtu, sample_rate, pt, interleaved, 1); } return std::make_shared(codec_id, ssrc, mtu, sample_rate, pt, interleaved); + } default : WarnL << "暂不支持该CodecId:" << codec_id; return nullptr; } } diff --git a/src/Extension/G711Rtp.cpp b/src/Extension/G711Rtp.cpp index 14b256b6..49edce5b 100644 --- a/src/Extension/G711Rtp.cpp +++ b/src/Extension/G711Rtp.cpp @@ -1,30 +1,34 @@ #include "G711Rtp.h" -G711RtpEncoder::G711RtpEncoder(CodecId codec, uint32_t ssrc, uint32_t mtu_size, - uint32_t sample_rate, uint8_t payload_type, uint8_t interleaved,uint32_t channels) - : CommonRtpDecoder(codec), RtpInfo(ssrc, mtu_size, sample_rate, payload_type, interleaved) { - _cache_frame = FrameImp::create(); - _cache_frame->_codec_id = codec; - _channels = channels; +namespace mediakit { + +G711RtpEncoder::G711RtpEncoder( + CodecId codec, uint32_t ssrc, uint32_t mtu_size, uint32_t sample_rate, uint8_t payload_type, uint8_t interleaved, + uint32_t channels) + : CommonRtpDecoder(codec) + , RtpInfo(ssrc, mtu_size, sample_rate, payload_type, interleaved) { + _cache_frame = FrameImp::create(); + _cache_frame->_codec_id = codec; + _channels = channels; } -bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame){ - auto dur = (_cache_frame->size()-_cache_frame->prefixSize())/(8*_channels); - auto next_pts = _cache_frame->pts()+dur; - if(next_pts == 0){ +bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) { + auto dur = (_cache_frame->size() - _cache_frame->prefixSize()) / (8 * _channels); + auto next_pts = _cache_frame->pts() + dur; + if (next_pts == 0) { _cache_frame->_pts = frame->pts(); - }else{ - if((next_pts+20) < frame->pts()){// 有丢包超过20ms + } else { + if ((next_pts + 20) < frame->pts()) { // 有丢包超过20ms _cache_frame->_pts = frame->pts() - dur; } } - _cache_frame->_buffer.append(frame->data() + frame->prefixSize(),frame->size() - frame->prefixSize()); + _cache_frame->_buffer.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); auto stamp = _cache_frame->pts(); auto ptr = _cache_frame->data() + _cache_frame->prefixSize(); auto len = _cache_frame->size() - _cache_frame->prefixSize(); auto remain_size = len; - auto max_size = 160*_channels; //20 ms per rtp + auto max_size = 160 * _channels; // 20 ms per rtp int n = 0; bool mark = false; while (remain_size >= max_size) { @@ -40,7 +44,9 @@ bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame){ ptr += rtp_size; remain_size -= rtp_size; } - _cache_frame->_buffer.erase(0,n*max_size); - _cache_frame->_pts += 20*n; + _cache_frame->_buffer.erase(0, n * max_size); + _cache_frame->_pts += 20 * n; return len > 0; -} \ No newline at end of file +} + +} // namespace mediakit \ No newline at end of file diff --git a/src/Extension/G711Rtp.h b/src/Extension/G711Rtp.h index 3b66bdd8..28f9b15f 100644 --- a/src/Extension/G711Rtp.h +++ b/src/Extension/G711Rtp.h @@ -15,17 +15,16 @@ #include "CommonRtp.h" #include "Rtsp/RtpCodec.h" -namespace mediakit{ - +namespace mediakit { /** * G711 rtp编码类 */ class G711RtpEncoder : public CommonRtpDecoder, public RtpInfo { public: - typedef std::shared_ptr Ptr; + using Ptr = std::shared_ptr; - ~G711RtpEncoder() override {} + ~G711RtpEncoder() override = default; /** * 构造函数 @@ -36,15 +35,17 @@ public: * @param payload_type pt类型 * @param interleaved rtsp interleaved 值 */ - G711RtpEncoder(CodecId codec, uint32_t ssrc, uint32_t mtu_size, uint32_t sample_rate, uint8_t payload_type, uint8_t interleaved,uint32_t channels); + G711RtpEncoder(CodecId codec, uint32_t ssrc, uint32_t mtu_size, uint32_t sample_rate, uint8_t payload_type, + uint8_t interleaved, uint32_t channels); /** * 输入帧数据并编码成rtp */ bool inputFrame(const Frame::Ptr &frame) override; + private: - FrameImp::Ptr _cache_frame; uint32_t _channels = 1; + FrameImp::Ptr _cache_frame; }; }//namespace mediakit