rtp send rtp g711 audio can config duration (#3325)

optimization for this
[issue](https://github.com/ZLMediaKit/ZLMediaKit/issues/3316)
This commit is contained in:
xiongguangjie
2024-03-02 16:40:13 +08:00
committed by GitHub
parent 87cb488b04
commit 06abbd0eb7
7 changed files with 50 additions and 7 deletions

View File

@@ -8,13 +8,27 @@ G711RtpEncoder::G711RtpEncoder(CodecId codec, uint32_t channels){
_channels = channels;
}
void G711RtpEncoder::setOpt(int opt, const toolkit::Any &param) {
if (opt == RTP_ENCODER_PKT_DUR_MS) {
if (param.is<uint32_t>()) {
auto dur = param.get<uint32_t>();
if (dur < 20 || dur > 180) {
WarnL << "set g711 rtp encoder duration ms failed for " << dur;
return;
}
// 向上 20ms 取整
_pkt_dur_ms = (dur + 19) / 20 * 20;
}
}
}
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
if ((next_pts + _pkt_dur_ms) < frame->pts()) { // 有丢包超过20ms
_cache_frame->_pts = frame->pts() - dur;
}
}
@@ -24,9 +38,9 @@ bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
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
int n = 0;
bool mark = false;
auto max_size = 160 * _channels * _pkt_dur_ms / 20; // 20 ms per 160 byte
uint32_t n = 0;
bool mark = true;
while (remain_size >= max_size) {
size_t rtp_size;
if (remain_size >= max_size) {
@@ -35,13 +49,13 @@ bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
break;
}
n++;
stamp += 20;
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, ptr, rtp_size, mark, stamp), false);
stamp += _pkt_dur_ms;
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, ptr, rtp_size, mark, stamp), true);
ptr += rtp_size;
remain_size -= rtp_size;
}
_cache_frame->_buffer.erase(0, n * max_size);
_cache_frame->_pts += 20 * n;
_cache_frame->_pts += (uint64_t)_pkt_dur_ms * n;
return len > 0;
}