From 5e32199661fb339f20ce0e2718784f8545875d7d Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Fri, 20 Dec 2024 19:26:04 +0800 Subject: [PATCH] improve g711 rtp packetizer code --- ext-codec/G711Rtp.cpp | 13 +++++++------ ext-codec/G711Rtp.h | 2 -- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ext-codec/G711Rtp.cpp b/ext-codec/G711Rtp.cpp index ed70b619..60789540 100644 --- a/ext-codec/G711Rtp.cpp +++ b/ext-codec/G711Rtp.cpp @@ -26,19 +26,20 @@ void G711RtpEncoder::setOpt(int opt, const toolkit::Any ¶m) { bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) { auto ptr = frame->data() + frame->prefixSize(); auto size = frame->size() - frame->prefixSize(); - _buffer.append(ptr, size); - _in_size += size; - _in_pts = frame->pts(); if (!_pkt_bytes) { + _in_pts = frame->pts(); // G711压缩率固定是2倍 _pkt_bytes = _pkt_dur_ms * _channels * (_sample_bit / 8) * _sample_rate / 1000 / 2; + } else { + _in_pts = frame->pts() - (float)_buffer.size() / (float)_pkt_bytes * _pkt_dur_ms; } + _buffer.append(ptr, size); + while (_buffer.size() >= _pkt_bytes) { - _out_size += _pkt_bytes; - auto pts = _in_pts - (_in_size - _out_size) * (_pkt_dur_ms / (float)_pkt_bytes); - RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, _buffer.data(), _pkt_bytes, false, pts), false); + _in_pts += _pkt_dur_ms; + RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, _buffer.data(), _pkt_bytes, false, _in_pts), false); _buffer.erase(0, _pkt_bytes); } return true; diff --git a/ext-codec/G711Rtp.h b/ext-codec/G711Rtp.h index 957911d3..d4bdb098 100644 --- a/ext-codec/G711Rtp.h +++ b/ext-codec/G711Rtp.h @@ -59,8 +59,6 @@ private: uint32_t _pkt_dur_ms = 20; uint32_t _pkt_bytes = 0; - uint64_t _in_size = 0; - uint64_t _out_size = 0; int64_t _in_pts = 0; toolkit::BufferLikeString _buffer; };