diff --git a/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.cc b/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.cc index 4c89b108d..437feb759 100644 --- a/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.cc +++ b/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.cc @@ -423,7 +423,11 @@ int16_t ACMGenericCodec::ResetEncoderSafe() { DisableVAD(); // Set DTX/VAD. - return SetVADSafe(enable_dtx, enable_vad, mode); + int status = SetVADSafe(&enable_dtx, &enable_vad, &mode); + dtx_enabled_ = enable_dtx; + vad_enabled_ = enable_vad; + vad_mode_ = mode; + return status; } int16_t ACMGenericCodec::InternalResetEncoder() { @@ -504,10 +508,8 @@ int16_t ACMGenericCodec::InitEncoderSafe(WebRtcACMCodecParams* codec_params, memset(in_timestamp_, 0, sizeof(uint32_t) * TIMESTAMP_BUFFER_SIZE_W32); } } - status = SetVADSafe(codec_params->enable_dtx, codec_params->enable_vad, - codec_params->vad_mode); - - return status; + return SetVADSafe(&codec_params->enable_dtx, &codec_params->enable_vad, + &codec_params->vad_mode); } void ACMGenericCodec::ResetNoMissedSamples() { @@ -634,70 +636,78 @@ uint32_t ACMGenericCodec::EarliestTimestamp() const { return in_timestamp_[0]; } -int16_t ACMGenericCodec::SetVAD(const bool enable_dtx, - const bool enable_vad, - const ACMVADMode mode) { +int16_t ACMGenericCodec::SetVAD(bool* enable_dtx, + bool* enable_vad, + ACMVADMode* mode) { WriteLockScoped cs(codec_wrapper_lock_); return SetVADSafe(enable_dtx, enable_vad, mode); } -int16_t ACMGenericCodec::SetVADSafe(const bool enable_dtx, - const bool enable_vad, - const ACMVADMode mode) { - if (enable_dtx) { +int16_t ACMGenericCodec::SetVADSafe(bool* enable_dtx, + bool* enable_vad, + ACMVADMode* mode) { + if (!STR_CASE_CMP(encoder_params_.codec_inst.plname, "OPUS") || + encoder_params_.codec_inst.channels == 2 ) { + // VAD/DTX is not supported for Opus (even if sending mono), or other + // stereo codecs. + DisableDTX(); + DisableVAD(); + *enable_dtx = false; + *enable_vad = false; + return 0; + } + + if (*enable_dtx) { // Make G729 AnnexB a special case. if (!STR_CASE_CMP(encoder_params_.codec_inst.plname, "G729") && !has_internal_dtx_) { if (ACMGenericCodec::EnableDTX() < 0) { WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, "SetVADSafe: error in enable DTX"); + *enable_dtx = false; + *enable_vad = vad_enabled_; return -1; } } else { if (EnableDTX() < 0) { WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, "SetVADSafe: error in enable DTX"); + *enable_dtx = false; + *enable_vad = vad_enabled_; return -1; } } - if (has_internal_dtx_) { - // Codec has internal DTX, practically we don't need WebRtc VAD, however, - // we let the user to turn it on if they need call-backs on silence. - // Store VAD mode for future even if VAD is off. - vad_mode_ = mode; - return (enable_vad) ? EnableVAD(mode) : DisableVAD(); - } else { - // Codec does not have internal DTX so enabling DTX requires an active - // VAD. 'enable_dtx == true' overwrites VAD status. - if (EnableVAD(mode) < 0) { - // If we cannot create VAD we have to disable DTX. - if (!vad_enabled_) { - DisableDTX(); - } - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, - "SetVADSafe: error in enable VAD"); - return -1; - } - - // Return '1', to let the caller know VAD was turned on, even if the - // function was called with VAD='false'. - if (enable_vad == false) { - return 1; - } else { - return 0; - } + // If codec does not have internal DTX (normal case) enabling DTX requires + // an active VAD. '*enable_dtx == true' overwrites VAD status. + // If codec has internal DTX, practically we don't need WebRtc VAD, however, + // we let the user to turn it on if they need call-backs on silence. + if (!has_internal_dtx_) { + // DTX is enabled, and VAD will be activated. + *enable_vad = true; } } else { // Make G729 AnnexB a special case. if (!STR_CASE_CMP(encoder_params_.codec_inst.plname, "G729") && !has_internal_dtx_) { ACMGenericCodec::DisableDTX(); + *enable_dtx = false; } else { DisableDTX(); + *enable_dtx = false; } - return (enable_vad) ? EnableVAD(mode) : DisableVAD(); } + + int16_t status = (*enable_vad) ? EnableVAD(*mode) : DisableVAD(); + if (status < 0) { + // Failed to set VAD, disable DTX. + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, unique_id_, + "SetVADSafe: error in enable VAD"); + DisableDTX(); + *enable_dtx = false; + *enable_vad = false; + } + return status; } int16_t ACMGenericCodec::EnableDTX() { diff --git a/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.h b/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.h index 0129bf38b..78feb99f0 100644 --- a/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.h +++ b/webrtc/modules/audio_coding/main/acm2/acm_generic_codec.h @@ -231,16 +231,16 @@ class ACMGenericCodec { /////////////////////////////////////////////////////////////////////////// // int16_t SetVAD() - // This is called to set VAD & DTX. If the codec has internal DTX that will + // This is called to set VAD & DTX. If the codec has internal DTX, it will // be used. If DTX is enabled and the codec does not have internal DTX, // WebRtc-VAD will be used to decide if the frame is active. If DTX is - // disabled but VAD is enabled. The audio is passed through VAD to label it + // disabled but VAD is enabled, the audio is passed through VAD to label it // as active or passive, but the frame is encoded normally. However the // bit-stream is labeled properly so that ACM::Process() can use this // information. In case of failure, the previous states of the VAD & DTX // are kept. // - // Inputs: + // Inputs/Output: // -enable_dtx : if true DTX will be enabled otherwise the DTX is // disabled. If codec has internal DTX that will be // used, otherwise WebRtc-CNG is used. In the latter @@ -256,9 +256,7 @@ class ACMGenericCodec { // -1 if failed to set DTX & VAD as specified, // 0 if succeeded. // - int16_t SetVAD(const bool enable_dtx = true, - const bool enable_vad = false, - const ACMVADMode mode = VADNormal); + int16_t SetVAD(bool* enable_dtx, bool* enable_vad, ACMVADMode* mode); /////////////////////////////////////////////////////////////////////////// // int32_t ReplaceInternalDTX() @@ -639,9 +637,7 @@ class ACMGenericCodec { // See SetVAD() for the description of function, input(s)/output(s) and // return value. // - int16_t SetVADSafe(const bool enable_dtx = true, - const bool enable_vad = false, - const ACMVADMode mode = VADNormal); + int16_t SetVADSafe(bool* enable_dtx, bool* enable_vad, ACMVADMode* mode); /////////////////////////////////////////////////////////////////////////// // See ReplaceInternalDTX() for the description of function, input and diff --git a/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc b/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc index 57b79d61d..3a7d27181 100644 --- a/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc +++ b/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc @@ -525,7 +525,7 @@ int AudioCodingModuleImpl::ProcessSingleStream() { } case kActiveNormalEncoded: case kPassiveNormalEncoded: { - current_payload_type = (uint8_t)send_codec_inst_.pltype; + current_payload_type = static_cast(send_codec_inst_.pltype); frame_type = kAudioFrameSpeech; break; } @@ -685,7 +685,7 @@ int AudioCodingModuleImpl::ProcessSingleStream() { if (vad_callback_ != NULL) { // Callback with VAD decision. - vad_callback_->InFrameType(((int16_t)encoding_type)); + vad_callback_->InFrameType(static_cast(encoding_type)); } } return length_bytes; @@ -998,7 +998,6 @@ int AudioCodingModuleImpl::RegisterSendCodec(const CodecInst& send_codec) { } ACMGenericCodec* codec_ptr = codecs_[codec_id]; - int status; WebRtcACMCodecParams codec_params; memcpy(&(codec_params.codec_inst), &send_codec, sizeof(CodecInst)); @@ -1006,12 +1005,7 @@ int AudioCodingModuleImpl::RegisterSendCodec(const CodecInst& send_codec) { codec_params.enable_dtx = dtx_enabled_; codec_params.vad_mode = vad_mode_; // Force initialization. - status = codec_ptr->InitEncoder(&codec_params, true); - - // Check if VAD was turned on, or if error is reported. - if (status == 1) { - vad_enabled_ = true; - } else if (status < 0) { + if (codec_ptr->InitEncoder(&codec_params, true) < 0) { // Could not initialize the encoder. // Check if already have a registered codec. @@ -1028,17 +1022,17 @@ int AudioCodingModuleImpl::RegisterSendCodec(const CodecInst& send_codec) { return -1; } + // Update states. + dtx_enabled_ = codec_params.enable_dtx; + vad_enabled_ = codec_params.enable_vad; + vad_mode_ = codec_params.vad_mode; + // Everything is fine so we can replace the previous codec with this one. if (send_codec_registered_) { // If we change codec we start fresh with FEC. // This is not strictly required by the standard. is_first_red_ = true; - - if (codec_ptr->SetVAD(dtx_enabled_, vad_enabled_, vad_mode_) < 0) { - // SetVAD failed. - vad_enabled_ = false; - dtx_enabled_ = false; - } + codec_ptr->SetVAD(&dtx_enabled_, &vad_enabled_, &vad_mode_); } current_send_codec_idx_ = codec_id; @@ -1450,6 +1444,9 @@ int AudioCodingModuleImpl::SetVADSafe(bool enable_dtx, if ((enable_dtx || enable_vad) && stereo_send_) { WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_, "VAD/DTX not supported for stereo sending"); + dtx_enabled_ = false; + vad_enabled_ = false; + vad_mode_ = mode; return -1; } @@ -1458,37 +1455,28 @@ int AudioCodingModuleImpl::SetVADSafe(bool enable_dtx, if ((enable_dtx || enable_vad) && secondary_encoder_.get() != NULL) { WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_, "VAD/DTX not supported when dual-streaming is enabled."); + dtx_enabled_ = false; + vad_enabled_ = false; + vad_mode_ = mode; return -1; } - // If a send codec is registered, set VAD/DTX for the codec. - if (HaveValidEncoder("SetVAD")) { - int status = codecs_[current_send_codec_idx_]->SetVAD(enable_dtx, - enable_vad, - mode); - if (status == 1) { - // Vad was enabled. - vad_enabled_ = true; - dtx_enabled_ = enable_dtx; - vad_mode_ = mode; + // Store VAD/DTX settings. Values can be changed in the call to "SetVAD" + // below. + dtx_enabled_ = enable_dtx; + vad_enabled_ = enable_vad; + vad_mode_ = mode; - return 0; - } else if (status < 0) { + // If a send codec is registered, set VAD/DTX for the codec. + if (HaveValidEncoder("SetVAD") && codecs_[current_send_codec_idx_]->SetVAD( + &enable_dtx, &enable_vad, &mode) < 0) { // SetVAD failed. WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, id_, "SetVAD failed"); - vad_enabled_ = false; dtx_enabled_ = false; - return -1; - } } - - vad_enabled_ = enable_vad; - dtx_enabled_ = enable_dtx; - vad_mode_ = mode; - return 0; } diff --git a/webrtc/modules/audio_coding/main/source/acm_generic_codec.cc b/webrtc/modules/audio_coding/main/source/acm_generic_codec.cc index 6c433011d..fe0a13ca4 100644 --- a/webrtc/modules/audio_coding/main/source/acm_generic_codec.cc +++ b/webrtc/modules/audio_coding/main/source/acm_generic_codec.cc @@ -494,8 +494,8 @@ int16_t ACMGenericCodec::ResetEncoderSafe() { // Set DTX/VAD. int status = SetVADSafe(&enable_dtx, &enable_vad, &mode); - vad_enabled_ = enable_dtx; - dtx_enabled_ = enable_vad; + dtx_enabled_ = enable_dtx; + vad_enabled_ = enable_vad; vad_mode_ = mode; return status; }