improve g711 rtp packetizer code

This commit is contained in:
xiongguangjie 2024-12-20 19:26:04 +08:00 committed by 夏楚
parent 3f772ea1d4
commit 5e32199661
2 changed files with 7 additions and 8 deletions

View File

@ -26,19 +26,20 @@ void G711RtpEncoder::setOpt(int opt, const toolkit::Any &param) {
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;

View File

@ -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;
};