diff --git a/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc b/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc index 17750291da..6c2893336a 100644 --- a/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc +++ b/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc @@ -156,6 +156,11 @@ int AcmReceiver::LeastRequiredDelayMs() const { return neteq_->LeastRequiredDelayMs(); } +rtc::Optional AcmReceiver::last_packet_sample_rate_hz() const { + CriticalSectionScoped lock(crit_sect_.get()); + return last_packet_sample_rate_hz_; +} + int AcmReceiver::last_output_sample_rate_hz() const { return neteq_->last_output_sample_rate_hz(); } @@ -190,6 +195,7 @@ int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header, decoder->acm_codec_id != *RentACodec::CodecIndexFromId(RentACodec::CodecId::kAVT)) { last_audio_decoder_ = decoder; + last_packet_sample_rate_hz_ = rtc::Optional(decoder->sample_rate_hz); } } // |crit_sect_| is released. @@ -392,6 +398,7 @@ int AcmReceiver::RemoveAllCodecs() { // No codec is registered, invalidate last audio decoder. last_audio_decoder_ = nullptr; + last_packet_sample_rate_hz_ = rtc::Optional(); return ret_val; } @@ -405,8 +412,10 @@ int AcmReceiver::RemoveCodec(uint8_t payload_type) { LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast(payload_type); return -1; } - if (last_audio_decoder_ == &it->second) + if (last_audio_decoder_ == &it->second) { last_audio_decoder_ = nullptr; + last_packet_sample_rate_hz_ = rtc::Optional(); + } decoders_.erase(it); return 0; } @@ -420,11 +429,6 @@ bool AcmReceiver::GetPlayoutTimestamp(uint32_t* timestamp) { return neteq_->GetPlayoutTimestamp(timestamp); } -int AcmReceiver::last_audio_codec_id() const { - CriticalSectionScoped lock(crit_sect_.get()); - return last_audio_decoder_ ? last_audio_decoder_->acm_codec_id : -1; -} - int AcmReceiver::LastAudioCodec(CodecInst* codec) const { CriticalSectionScoped lock(crit_sect_.get()); if (!last_audio_decoder_) { diff --git a/webrtc/modules/audio_coding/main/acm2/acm_receiver.h b/webrtc/modules/audio_coding/main/acm2/acm_receiver.h index f02605bb71..bcedacd14f 100644 --- a/webrtc/modules/audio_coding/main/acm2/acm_receiver.h +++ b/webrtc/modules/audio_coding/main/acm2/acm_receiver.h @@ -15,6 +15,7 @@ #include #include "webrtc/base/array_view.h" +#include "webrtc/base/optional.h" #include "webrtc/base/scoped_ptr.h" #include "webrtc/base/thread_annotations.h" #include "webrtc/common_audio/vad/include/webrtc_vad.h" @@ -154,6 +155,12 @@ class AcmReceiver { // void ResetInitialDelay(); + // Returns the sample rate of the decoder associated with the last incoming + // packet. If no packet of a registered non-CNG codec has been received, the + // return value is empty. Also, if the decoder was unregistered since the last + // packet was inserted, the return value is empty. + rtc::Optional last_packet_sample_rate_hz() const; + // Returns last_output_sample_rate_hz from the NetEq instance. int last_output_sample_rate_hz() const; @@ -212,13 +219,6 @@ class AcmReceiver { // bool GetPlayoutTimestamp(uint32_t* timestamp); - // - // Return the index of the codec associated with the last non-CNG/non-DTMF - // received payload. If no non-CNG/non-DTMF payload is received -1 is - // returned. - // - int last_audio_codec_id() const; // TODO(turajs): can be inline. - // // Get the audio codec associated with the last non-CNG/non-DTMF received // payload. If no non-CNG/non-DTMF packet is received -1 is returned, @@ -295,6 +295,7 @@ class AcmReceiver { bool vad_enabled_; Clock* clock_; // TODO(henrik.lundin) Make const if possible. bool resampled_last_output_frame_ GUARDED_BY(crit_sect_); + rtc::Optional last_packet_sample_rate_hz_ GUARDED_BY(crit_sect_); }; } // namespace acm2 diff --git a/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc b/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc index 3bda1168d4..8f43ac456a 100644 --- a/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc +++ b/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc @@ -330,7 +330,7 @@ TEST_F(AcmReceiverTestOldApi, // Has received, only, DTX. Last Audio codec is undefined. EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec)); - EXPECT_EQ(-1, receiver_->last_audio_codec_id()); + EXPECT_FALSE(receiver_->last_packet_sample_rate_hz()); for (auto id : kCodecId) { const CodecIdInst c(id); @@ -344,7 +344,8 @@ TEST_F(AcmReceiverTestOldApi, // of type "speech." ASSERT_TRUE(packet_sent_); ASSERT_EQ(kAudioFrameSpeech, last_frame_type_); - EXPECT_EQ(c.id, receiver_->last_audio_codec_id()); + EXPECT_EQ(rtc::Optional(c.inst.plfreq), + receiver_->last_packet_sample_rate_hz()); // Set VAD on to send DTX. Then check if the "Last Audio codec" returns // the expected codec. @@ -356,7 +357,8 @@ TEST_F(AcmReceiverTestOldApi, InsertOnePacketOfSilence(c.id); ASSERT_TRUE(packet_sent_); } - EXPECT_EQ(c.id, receiver_->last_audio_codec_id()); + EXPECT_EQ(rtc::Optional(c.inst.plfreq), + receiver_->last_packet_sample_rate_hz()); EXPECT_EQ(0, receiver_->LastAudioCodec(&codec)); EXPECT_TRUE(CodecsEqual(c.inst, codec)); } 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 19ae4cbc2b..5d18bda00c 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,14 +525,9 @@ int AudioCodingModuleImpl::InitializeReceiverSafe() { // Get current receive frequency. int AudioCodingModuleImpl::ReceiveFrequency() const { - WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, id_, - "ReceiveFrequency()"); - - CriticalSectionScoped lock(acm_crit_sect_.get()); - - auto codec_id = RentACodec::CodecIdFromIndex(receiver_.last_audio_codec_id()); - return codec_id ? RentACodec::CodecInstById(*codec_id)->plfreq - : receiver_.last_output_sample_rate_hz(); + const auto last_packet_sample_rate = receiver_.last_packet_sample_rate_hz(); + return last_packet_sample_rate ? *last_packet_sample_rate + : receiver_.last_output_sample_rate_hz(); } // Get current playout frequency.