Explicitly use the Opus DTX encoder state.

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 <henrik.lundin@webrtc.org>
Commit-Queue: Lionel Koenig <lionelk@webrtc.org>
Reviewed-by: Jakob Ivarsson‎ <jakobi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43034}
This commit is contained in:
Lionel Koenig 2024-09-16 15:20:43 +02:00 committed by WebRTC LUCI CQ
parent d153de6d33
commit 098c128a15
4 changed files with 27 additions and 12 deletions

View File

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

View File

@ -179,7 +179,6 @@ class AudioEncoderOpusImpl final : public AudioEncoder {
std::optional<size_t> overhead_bytes_per_packet_;
const std::unique_ptr<SmoothingFilter> bitrate_smoother_;
std::optional<int64_t> bitrate_smoother_last_update_time_;
int consecutive_dtx_frames_;
friend struct AudioEncoderOpus;
};

View File

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

View File

@ -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()
*