From 9c3efd00523a81d0f2b582799fbe67afe44139b2 Mon Sep 17 00:00:00 2001 From: "henrik.lundin" Date: Thu, 27 Aug 2015 13:12:22 -0700 Subject: [PATCH] Reland: Implement NetEq's CurrentDelay function This was not implemented before. It returns the current total delay (packet buffer and sync buffer) of NetEq. This is the same information that was already available in NetEqNetworkStatistics::current_buffer_size_ms, that can be obtained through NetEq::NetworkStatistics(). But, since the current delay is a key metric of NetEq, it is convenient to have it available in a simpler way. This is a re-landing of r9359, https://webrtc-codereview.appspot.com/51149004, which was reverted in r9360. The refactoring made in r9669 facilitated the relanding. TBR=minyue@webrtc.org Review URL: https://codereview.webrtc.org/1313873003 Cr-Commit-Position: refs/heads/master@{#9801} --- .../audio_coding/neteq/interface/neteq.h | 4 ++-- webrtc/modules/audio_coding/neteq/neteq_impl.cc | 17 +++++++++++++++-- webrtc/modules/audio_coding/neteq/neteq_impl.h | 2 +- .../neteq/neteq_network_stats_unittest.cc | 3 +++ .../audio_coding/neteq/neteq_unittest.cc | 2 ++ 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/webrtc/modules/audio_coding/neteq/interface/neteq.h b/webrtc/modules/audio_coding/neteq/interface/neteq.h index e2ea00debb..54458b92e2 100644 --- a/webrtc/modules/audio_coding/neteq/interface/neteq.h +++ b/webrtc/modules/audio_coding/neteq/interface/neteq.h @@ -215,8 +215,8 @@ class NetEq { // Not implemented. virtual int TargetDelay() = 0; - // Not implemented. - virtual int CurrentDelay() = 0; + // Returns the current total delay (packet buffer and sync buffer) in ms. + virtual int CurrentDelayMs() const = 0; // Sets the playout mode to |mode|. // Deprecated. Set the mode in the Config struct passed to the constructor. diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl.cc b/webrtc/modules/audio_coding/neteq/neteq_impl.cc index cf7afbc8d9..c9c1f86926 100644 --- a/webrtc/modules/audio_coding/neteq/neteq_impl.cc +++ b/webrtc/modules/audio_coding/neteq/neteq_impl.cc @@ -15,6 +15,7 @@ #include +#include "webrtc/base/checks.h" #include "webrtc/base/logging.h" #include "webrtc/base/safe_conversions.h" #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" @@ -282,8 +283,20 @@ int NetEqImpl::TargetDelay() { return kNotImplemented; } -int NetEqImpl::CurrentDelay() { - return kNotImplemented; +int NetEqImpl::CurrentDelayMs() const { + CriticalSectionScoped lock(crit_sect_.get()); + if (fs_hz_ == 0) + return 0; + // Sum up the samples in the packet buffer with the future length of the sync + // buffer, and divide the sum by the sample rate. + const size_t delay_samples = + packet_buffer_->NumSamplesInBuffer(decoder_database_.get(), + decoder_frame_length_) + + sync_buffer_->FutureLength(); + // The division below will truncate. + const int delay_ms = + static_cast(delay_samples) / rtc::CheckedDivExact(fs_hz_, 1000); + return delay_ms; } // Deprecated. diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl.h b/webrtc/modules/audio_coding/neteq/neteq_impl.h index 182d8b8d89..6a45db6db9 100644 --- a/webrtc/modules/audio_coding/neteq/neteq_impl.h +++ b/webrtc/modules/audio_coding/neteq/neteq_impl.h @@ -136,7 +136,7 @@ class NetEqImpl : public webrtc::NetEq { int TargetDelay() override; - int CurrentDelay() override; + int CurrentDelayMs() const override; // Sets the playout mode to |mode|. // Deprecated. diff --git a/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc b/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc index 139106bd3c..e6ac37cfff 100644 --- a/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc +++ b/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc @@ -170,6 +170,9 @@ struct NetEqNetworkStatsCheck { CHECK_NETEQ_NETWORK_STATS(added_zero_samples); #undef CHECK_NETEQ_NETWORK_STATS + + // Compare with CurrentDelay, which should be identical. + EXPECT_EQ(stats.current_buffer_size_ms, neteq()->CurrentDelayMs()); } void RunTest(int num_loops, NetEqNetworkStatsCheck expects) { diff --git a/webrtc/modules/audio_coding/neteq/neteq_unittest.cc b/webrtc/modules/audio_coding/neteq/neteq_unittest.cc index 0c43024799..8a560dc200 100644 --- a/webrtc/modules/audio_coding/neteq/neteq_unittest.cc +++ b/webrtc/modules/audio_coding/neteq/neteq_unittest.cc @@ -406,6 +406,8 @@ void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file, ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); ASSERT_NO_FATAL_FAILURE( network_stat_files.ProcessReference(network_stats)); + // Compare with CurrentDelay, which should be identical. + EXPECT_EQ(network_stats.current_buffer_size_ms, neteq_->CurrentDelayMs()); // Process RTCPstat. RtcpStatistics rtcp_stats;