From 098c128a159bfc2848f19b3816f632bab77f0872 Mon Sep 17 00:00:00 2001 From: Lionel Koenig Date: Mon, 16 Sep 2024 15:20:43 +0200 Subject: [PATCH] Explicitly use the Opus DTX encoder state. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the DTX state from inside the Opus encoder instead of trying to mimic the logic outside. Bug: None Change-Id: I852044fee261a5b7f9255c557a27adfd0b1701bd Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/362640 Reviewed-by: Henrik Lundin Commit-Queue: Lionel Koenig Reviewed-by: Jakob Ivarsson‎ Cr-Commit-Position: refs/heads/main@{#43034} --- .../audio_coding/codecs/opus/audio_encoder_opus.cc | 14 +++----------- .../audio_coding/codecs/opus/audio_encoder_opus.h | 1 - modules/audio_coding/codecs/opus/opus_interface.cc | 10 ++++++++++ modules/audio_coding/codecs/opus/opus_interface.h | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc index b2e44c7a19..288ee84a4c 100644 --- a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc +++ b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc @@ -378,8 +378,7 @@ AudioEncoderOpusImpl::AudioEncoderOpusImpl( inst_(nullptr), packet_loss_fraction_smoother_(new PacketLossFractionSmoother()), audio_network_adaptor_creator_(audio_network_adaptor_creator), - bitrate_smoother_(std::move(bitrate_smoother)), - consecutive_dtx_frames_(0) { + bitrate_smoother_(std::move(bitrate_smoother)) { RTC_DCHECK(0 <= payload_type && payload_type <= 127); // Sanity check of the redundant payload type field that we want to get rid @@ -387,6 +386,7 @@ AudioEncoderOpusImpl::AudioEncoderOpusImpl( RTC_CHECK(config.payload_type == -1 || config.payload_type == payload_type); RTC_CHECK(RecreateEncoderInstance(config)); + SetProjectedPacketLossRate(packet_loss_rate_); } @@ -609,8 +609,6 @@ AudioEncoder::EncodedInfo AudioEncoderOpusImpl::EncodeImpl( }); input_buffer_.clear(); - bool dtx_frame = (info.encoded_bytes <= 2); - // Will use new packet size for next encoding. config_.frame_size_ms = next_frame_length_ms_; @@ -625,15 +623,9 @@ AudioEncoder::EncodedInfo AudioEncoderOpusImpl::EncodeImpl( info.encoded_timestamp = first_timestamp_in_buffer_; info.payload_type = payload_type_; info.send_even_if_empty = true; // Allows Opus to send empty packets. - // After 20 DTX frames (MAX_CONSECUTIVE_DTX) Opus will send a frame - // coding the background noise. Avoid flagging this frame as speech - // (even though there is a probability of the frame being speech). - info.speech = !dtx_frame && (consecutive_dtx_frames_ != 20); + info.speech = WebRtcOpus_GetInDtx(inst_) == 0; info.encoder_type = CodecType::kOpus; - // Increase or reset DTX counter. - consecutive_dtx_frames_ = (dtx_frame) ? (consecutive_dtx_frames_ + 1) : (0); - return info; } diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus.h b/modules/audio_coding/codecs/opus/audio_encoder_opus.h index f1c9c65da2..02c4a88767 100644 --- a/modules/audio_coding/codecs/opus/audio_encoder_opus.h +++ b/modules/audio_coding/codecs/opus/audio_encoder_opus.h @@ -179,7 +179,6 @@ class AudioEncoderOpusImpl final : public AudioEncoder { std::optional overhead_bytes_per_packet_; const std::unique_ptr bitrate_smoother_; std::optional bitrate_smoother_last_update_time_; - int consecutive_dtx_frames_; friend struct AudioEncoderOpus; }; diff --git a/modules/audio_coding/codecs/opus/opus_interface.cc b/modules/audio_coding/codecs/opus/opus_interface.cc index 1257f25c7c..4b661c8d0e 100644 --- a/modules/audio_coding/codecs/opus/opus_interface.cc +++ b/modules/audio_coding/codecs/opus/opus_interface.cc @@ -310,6 +310,16 @@ int16_t WebRtcOpus_GetUseDtx(OpusEncInst* inst) { return -1; } +int16_t WebRtcOpus_GetInDtx(OpusEncInst* inst) { + if (inst) { + opus_int32 in_dtx; + if (ENCODER_CTL(inst, OPUS_GET_IN_DTX(&in_dtx)) == 0) { + return in_dtx; + } + } + return -1; +} + int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) { if (inst) { return ENCODER_CTL(inst, OPUS_SET_VBR(0)); diff --git a/modules/audio_coding/codecs/opus/opus_interface.h b/modules/audio_coding/codecs/opus/opus_interface.h index 83f8cbe929..733f7e4ab3 100644 --- a/modules/audio_coding/codecs/opus/opus_interface.h +++ b/modules/audio_coding/codecs/opus/opus_interface.h @@ -245,6 +245,20 @@ int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst); */ int16_t WebRtcOpus_GetUseDtx(OpusEncInst* inst); +/**************************************************************************** + * WebRtcOpus_GetUseDtx() + * + * This function gets if the encoder is in DTX. + * + * Input: + * - inst : Encoder context + * + * Return value : 0 - Encoder is not DTX. + * 1 - Encoder is in DTX. + * -1 - Error. + */ +int16_t WebRtcOpus_GetInDtx(OpusEncInst* inst); + /**************************************************************************** * WebRtcOpus_EnableCbr() *