Re-Land: Implement AudioReceiveStream::GetStats().

R=tommi@webrtc.org
BUG=webrtc:4690

Committed: https://chromium.googlesource.com/external/webrtc/+/a457752f4afc496ed7f4d6b584b08d8635f18cc0

Review URL: https://codereview.webrtc.org/1390753002 .

Cr-Commit-Position: refs/heads/master@{#10369}
diff --git a/webrtc/audio/BUILD.gn b/webrtc/audio/BUILD.gn
index c6f4b6b..d5061db 100644
--- a/webrtc/audio/BUILD.gn
+++ b/webrtc/audio/BUILD.gn
@@ -14,6 +14,8 @@
     "audio_receive_stream.h",
     "audio_send_stream.cc",
     "audio_send_stream.h",
+    "conversion.h",
+    "scoped_voe_interface.h",
   ]
 
   configs += [ "..:common_config" ]
diff --git a/webrtc/audio/audio_receive_stream.cc b/webrtc/audio/audio_receive_stream.cc
index c725e37..0fd96d0 100644
--- a/webrtc/audio/audio_receive_stream.cc
+++ b/webrtc/audio/audio_receive_stream.cc
@@ -12,10 +12,17 @@
 
 #include <string>
 
+#include "webrtc/audio/conversion.h"
 #include "webrtc/base/checks.h"
 #include "webrtc/base/logging.h"
 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
 #include "webrtc/system_wrappers/interface/tick_util.h"
+#include "webrtc/voice_engine/include/voe_base.h"
+#include "webrtc/voice_engine/include/voe_codec.h"
+#include "webrtc/voice_engine/include/voe_neteq_stats.h"
+#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
+#include "webrtc/voice_engine/include/voe_video_sync.h"
+#include "webrtc/voice_engine/include/voe_volume_control.h"
 
 namespace webrtc {
 std::string AudioReceiveStream::Config::Rtp::ToString() const {
@@ -24,8 +31,9 @@
   ss << ", extensions: [";
   for (size_t i = 0; i < extensions.size(); ++i) {
     ss << extensions[i].ToString();
-    if (i != extensions.size() - 1)
+    if (i != extensions.size() - 1) {
       ss << ", ";
+    }
   }
   ss << ']';
   ss << '}';
@@ -36,8 +44,9 @@
   std::stringstream ss;
   ss << "{rtp: " << rtp.ToString();
   ss << ", voe_channel_id: " << voe_channel_id;
-  if (!sync_group.empty())
+  if (!sync_group.empty()) {
     ss << ", sync_group: " << sync_group;
+  }
   ss << '}';
   return ss.str();
 }
@@ -45,13 +54,18 @@
 namespace internal {
 AudioReceiveStream::AudioReceiveStream(
       RemoteBitrateEstimator* remote_bitrate_estimator,
-      const webrtc::AudioReceiveStream::Config& config)
+      const webrtc::AudioReceiveStream::Config& config,
+      VoiceEngine* voice_engine)
     : remote_bitrate_estimator_(remote_bitrate_estimator),
       config_(config),
+      voice_engine_(voice_engine),
+      voe_base_(voice_engine),
       rtp_header_parser_(RtpHeaderParser::Create()) {
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
   RTC_DCHECK(config.voe_channel_id != -1);
   RTC_DCHECK(remote_bitrate_estimator_ != nullptr);
+  RTC_DCHECK(voice_engine_ != nullptr);
   RTC_DCHECK(rtp_header_parser_ != nullptr);
   for (const auto& ext : config.rtp.extensions) {
     // One-byte-extension local identifiers are in the range 1-14 inclusive.
@@ -73,33 +87,117 @@
 }
 
 AudioReceiveStream::~AudioReceiveStream() {
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
 }
 
 webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
-  return webrtc::AudioReceiveStream::Stats();
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
+  webrtc::AudioReceiveStream::Stats stats;
+  stats.remote_ssrc = config_.rtp.remote_ssrc;
+  ScopedVoEInterface<VoECodec> codec(voice_engine_);
+  ScopedVoEInterface<VoENetEqStats> neteq(voice_engine_);
+  ScopedVoEInterface<VoERTP_RTCP> rtp(voice_engine_);
+  ScopedVoEInterface<VoEVideoSync> sync(voice_engine_);
+  ScopedVoEInterface<VoEVolumeControl> volume(voice_engine_);
+  unsigned int ssrc = 0;
+  webrtc::CallStatistics cs = {0};
+  webrtc::CodecInst ci = {0};
+  // Only collect stats if we have seen some traffic with the SSRC.
+  if (rtp->GetRemoteSSRC(config_.voe_channel_id, ssrc) == -1 ||
+      rtp->GetRTCPStatistics(config_.voe_channel_id, cs) == -1 ||
+      codec->GetRecCodec(config_.voe_channel_id, ci) == -1) {
+    return stats;
+  }
+
+  stats.bytes_rcvd = cs.bytesReceived;
+  stats.packets_rcvd = cs.packetsReceived;
+  stats.packets_lost = cs.cumulativeLost;
+  stats.fraction_lost = static_cast<float>(cs.fractionLost) / (1 << 8);
+  if (ci.pltype != -1) {
+    stats.codec_name = ci.plname;
+  }
+
+  stats.ext_seqnum = cs.extendedMax;
+  if (ci.plfreq / 1000 > 0) {
+    stats.jitter_ms = cs.jitterSamples / (ci.plfreq / 1000);
+  }
+  {
+    int jitter_buffer_delay_ms = 0;
+    int playout_buffer_delay_ms = 0;
+    sync->GetDelayEstimate(config_.voe_channel_id, &jitter_buffer_delay_ms,
+                           &playout_buffer_delay_ms);
+    stats.delay_estimate_ms =
+        jitter_buffer_delay_ms + playout_buffer_delay_ms;
+  }
+  {
+    unsigned int level = 0;
+    if (volume->GetSpeechOutputLevelFullRange(config_.voe_channel_id, level)
+        != -1) {
+      stats.audio_level = static_cast<int32_t>(level);
+    }
+  }
+
+  webrtc::NetworkStatistics ns = {0};
+  if (neteq->GetNetworkStatistics(config_.voe_channel_id, ns) != -1) {
+    // Get jitter buffer and total delay (alg + jitter + playout) stats.
+    stats.jitter_buffer_ms = ns.currentBufferSize;
+    stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
+    stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
+    stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
+    stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
+    stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
+    stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
+  }
+
+  webrtc::AudioDecodingCallStats ds;
+  if (neteq->GetDecodingCallStatistics(config_.voe_channel_id, &ds) != -1) {
+    stats.decoding_calls_to_silence_generator =
+        ds.calls_to_silence_generator;
+    stats.decoding_calls_to_neteq = ds.calls_to_neteq;
+    stats.decoding_normal = ds.decoded_normal;
+    stats.decoding_plc = ds.decoded_plc;
+    stats.decoding_cng = ds.decoded_cng;
+    stats.decoding_plc_cng = ds.decoded_plc_cng;
+  }
+
+  stats.capture_start_ntp_time_ms = cs.capture_start_ntp_time_ms_;
+
+  return stats;
 }
 
 const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   return config_;
 }
 
 void AudioReceiveStream::Start() {
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
 }
 
 void AudioReceiveStream::Stop() {
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
 }
 
 void AudioReceiveStream::SignalNetworkState(NetworkState state) {
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
 }
 
 bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
+  // TODO(solenberg): Tests call this function on a network thread, libjingle
+  // calls on the worker thread. We should move towards always using a network
+  // thread. Then this check can be enabled.
+  // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
   return false;
 }
 
 bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
                                     size_t length,
                                     const PacketTime& packet_time) {
+  // TODO(solenberg): Tests call this function on a network thread, libjingle
+  // calls on the worker thread. We should move towards always using a network
+  // thread. Then this check can be enabled.
+  // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
   RTPHeader header;
 
   if (!rtp_header_parser_->Parse(packet, length, &header)) {
diff --git a/webrtc/audio/audio_receive_stream.h b/webrtc/audio/audio_receive_stream.h
index 1e52724..5c77653 100644
--- a/webrtc/audio/audio_receive_stream.h
+++ b/webrtc/audio/audio_receive_stream.h
@@ -12,18 +12,23 @@
 #define WEBRTC_AUDIO_AUDIO_RECEIVE_STREAM_H_
 
 #include "webrtc/audio_receive_stream.h"
+#include "webrtc/audio/scoped_voe_interface.h"
+#include "webrtc/base/thread_checker.h"
 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
+#include "webrtc/voice_engine/include/voe_base.h"
 
 namespace webrtc {
 
 class RemoteBitrateEstimator;
+class VoiceEngine;
 
 namespace internal {
 
 class AudioReceiveStream : public webrtc::AudioReceiveStream {
  public:
   AudioReceiveStream(RemoteBitrateEstimator* remote_bitrate_estimator,
-                     const webrtc::AudioReceiveStream::Config& config);
+                     const webrtc::AudioReceiveStream::Config& config,
+                     VoiceEngine* voice_engine);
   ~AudioReceiveStream() override;
 
   // webrtc::ReceiveStream implementation.
@@ -41,8 +46,12 @@
   const webrtc::AudioReceiveStream::Config& config() const;
 
  private:
+  rtc::ThreadChecker thread_checker_;
   RemoteBitrateEstimator* const remote_bitrate_estimator_;
   const webrtc::AudioReceiveStream::Config config_;
+  VoiceEngine* voice_engine_;
+  // We hold one interface pointer to the VoE to make sure it is kept alive.
+  ScopedVoEInterface<VoEBase> voe_base_;
   rtc::scoped_ptr<RtpHeaderParser> rtp_header_parser_;
 };
 }  // namespace internal
diff --git a/webrtc/audio/audio_receive_stream_unittest.cc b/webrtc/audio/audio_receive_stream_unittest.cc
index d6cce69..8809b35 100644
--- a/webrtc/audio/audio_receive_stream_unittest.cc
+++ b/webrtc/audio/audio_receive_stream_unittest.cc
@@ -11,10 +11,14 @@
 #include "testing/gtest/include/gtest/gtest.h"
 
 #include "webrtc/audio/audio_receive_stream.h"
+#include "webrtc/audio/conversion.h"
 #include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_estimator.h"
 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
+#include "webrtc/test/fake_voice_engine.h"
 
-namespace webrtc {
+namespace {
+
+using webrtc::ByteWriter;
 
 const size_t kAbsoluteSendTimeLength = 4;
 
@@ -45,33 +49,93 @@
   ByteWriter<uint16_t>::WriteBigEndian(header + 2, 0x1234);  // Sequence number.
   ByteWriter<uint32_t>::WriteBigEndian(header + 4, 0x5678);  // Timestamp.
   ByteWriter<uint32_t>::WriteBigEndian(header + 8, 0x4321);  // SSRC.
-  int32_t rtp_header_length = kRtpHeaderSize;
+  int32_t rtp_header_length = webrtc::kRtpHeaderSize;
 
   BuildAbsoluteSendTimeExtension(header + rtp_header_length, extension_id,
                                  abs_send_time);
   rtp_header_length += kAbsoluteSendTimeLength;
   return rtp_header_length;
 }
+}  // namespace
+
+namespace webrtc {
+namespace test {
 
 TEST(AudioReceiveStreamTest, AudioPacketUpdatesBweWithTimestamp) {
-  MockRemoteBitrateEstimator rbe;
+  MockRemoteBitrateEstimator remote_bitrate_estimator;
+  FakeVoiceEngine voice_engine;
   AudioReceiveStream::Config config;
   config.combined_audio_video_bwe = true;
-  config.voe_channel_id = 1;
+  config.voe_channel_id = voice_engine.kReceiveChannelId;
   const int kAbsSendTimeId = 3;
   config.rtp.extensions.push_back(
       RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeId));
-  internal::AudioReceiveStream recv_stream(&rbe, config);
+  internal::AudioReceiveStream recv_stream(&remote_bitrate_estimator, config,
+                                           &voice_engine);
   uint8_t rtp_packet[30];
   const int kAbsSendTimeValue = 1234;
   CreateRtpHeaderWithAbsSendTime(rtp_packet, kAbsSendTimeId, kAbsSendTimeValue);
   PacketTime packet_time(5678000, 0);
   const size_t kExpectedHeaderLength = 20;
-  EXPECT_CALL(rbe, IncomingPacket(packet_time.timestamp / 1000,
-                                  sizeof(rtp_packet) - kExpectedHeaderLength,
-                                  testing::_, false))
+  EXPECT_CALL(remote_bitrate_estimator,
+      IncomingPacket(packet_time.timestamp / 1000,
+          sizeof(rtp_packet) - kExpectedHeaderLength, testing::_, false))
       .Times(1);
   EXPECT_TRUE(
       recv_stream.DeliverRtp(rtp_packet, sizeof(rtp_packet), packet_time));
 }
+
+TEST(AudioReceiveStreamTest, GetStats) {
+  const uint32_t kSsrc1 = 667;
+
+  MockRemoteBitrateEstimator remote_bitrate_estimator;
+  FakeVoiceEngine voice_engine;
+  AudioReceiveStream::Config config;
+  config.rtp.remote_ssrc = kSsrc1;
+  config.voe_channel_id = voice_engine.kReceiveChannelId;
+  internal::AudioReceiveStream recv_stream(&remote_bitrate_estimator, config,
+                                           &voice_engine);
+
+  AudioReceiveStream::Stats stats = recv_stream.GetStats();
+  const CallStatistics& call_stats = voice_engine.GetRecvCallStats();
+  const CodecInst& codec_inst = voice_engine.GetRecvRecCodecInst();
+  const NetworkStatistics& net_stats = voice_engine.GetRecvNetworkStats();
+  const AudioDecodingCallStats& decode_stats =
+      voice_engine.GetRecvAudioDecodingCallStats();
+  EXPECT_EQ(kSsrc1, stats.remote_ssrc);
+  EXPECT_EQ(static_cast<int64_t>(call_stats.bytesReceived), stats.bytes_rcvd);
+  EXPECT_EQ(static_cast<uint32_t>(call_stats.packetsReceived),
+            stats.packets_rcvd);
+  EXPECT_EQ(call_stats.cumulativeLost, stats.packets_lost);
+  EXPECT_EQ(static_cast<float>(call_stats.fractionLost) / 256,
+            stats.fraction_lost);
+  EXPECT_EQ(std::string(codec_inst.plname), stats.codec_name);
+  EXPECT_EQ(call_stats.extendedMax, stats.ext_seqnum);
+  EXPECT_EQ(call_stats.jitterSamples / (codec_inst.plfreq / 1000),
+            stats.jitter_ms);
+  EXPECT_EQ(net_stats.currentBufferSize, stats.jitter_buffer_ms);
+  EXPECT_EQ(net_stats.preferredBufferSize, stats.jitter_buffer_preferred_ms);
+  EXPECT_EQ(static_cast<uint32_t>(voice_engine.kRecvJitterBufferDelay +
+      voice_engine.kRecvPlayoutBufferDelay), stats.delay_estimate_ms);
+  EXPECT_EQ(static_cast<int32_t>(voice_engine.kRecvSpeechOutputLevel),
+            stats.audio_level);
+  EXPECT_EQ(Q14ToFloat(net_stats.currentExpandRate), stats.expand_rate);
+  EXPECT_EQ(Q14ToFloat(net_stats.currentSpeechExpandRate),
+            stats.speech_expand_rate);
+  EXPECT_EQ(Q14ToFloat(net_stats.currentSecondaryDecodedRate),
+            stats.secondary_decoded_rate);
+  EXPECT_EQ(Q14ToFloat(net_stats.currentAccelerateRate), stats.accelerate_rate);
+  EXPECT_EQ(Q14ToFloat(net_stats.currentPreemptiveRate),
+            stats.preemptive_expand_rate);
+  EXPECT_EQ(decode_stats.calls_to_silence_generator,
+            stats.decoding_calls_to_silence_generator);
+  EXPECT_EQ(decode_stats.calls_to_neteq, stats.decoding_calls_to_neteq);
+  EXPECT_EQ(decode_stats.decoded_normal, stats.decoding_normal);
+  EXPECT_EQ(decode_stats.decoded_plc, stats.decoding_plc);
+  EXPECT_EQ(decode_stats.decoded_cng, stats.decoding_cng);
+  EXPECT_EQ(decode_stats.decoded_plc_cng, stats.decoding_plc_cng);
+  EXPECT_EQ(call_stats.capture_start_ntp_time_ms_,
+            stats.capture_start_ntp_time_ms);
+}
+}  // namespace test
 }  // namespace webrtc
diff --git a/webrtc/audio/conversion.h b/webrtc/audio/conversion.h
new file mode 100644
index 0000000..c1cf9b6
--- /dev/null
+++ b/webrtc/audio/conversion.h
@@ -0,0 +1,22 @@
+/*
+ *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef WEBRTC_AUDIO_CONVERSION_H_
+#define WEBRTC_AUDIO_CONVERSION_H_
+
+namespace webrtc {
+
+// Convert fixed point number with 14 bit fractional part, to floating point.
+inline float Q14ToFloat(uint16_t v) {
+  return static_cast<float>(v) / (1 << 14);
+}
+}  // namespace webrtc
+
+#endif  // WEBRTC_AUDIO_CONVERSION_H_
diff --git a/webrtc/audio/scoped_voe_interface.h b/webrtc/audio/scoped_voe_interface.h
new file mode 100644
index 0000000..1029337
--- /dev/null
+++ b/webrtc/audio/scoped_voe_interface.h
@@ -0,0 +1,45 @@
+/*
+ *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef WEBRTC_AUDIO_SCOPED_VOE_INTERFACE_H_
+#define WEBRTC_AUDIO_SCOPED_VOE_INTERFACE_H_
+
+#include "webrtc/base/checks.h"
+
+namespace webrtc {
+
+class VoiceEngine;
+
+namespace internal {
+
+// Utility template for obtaining and holding a reference to a VoiceEngine
+// interface and making sure it is released when this object goes out of scope.
+template<class T> class ScopedVoEInterface {
+ public:
+  explicit ScopedVoEInterface(webrtc::VoiceEngine* e)
+      : ptr_(T::GetInterface(e)) {
+    RTC_DCHECK(ptr_);
+  }
+  ~ScopedVoEInterface() {
+    if (ptr_) {
+      ptr_->Release();
+    }
+  }
+  T* operator->() {
+    RTC_DCHECK(ptr_);
+    return ptr_;
+  }
+ private:
+  T* ptr_;
+};
+}  // namespace internal
+}  // namespace webrtc
+
+#endif  // WEBRTC_AUDIO_SCOPED_VOE_INTERFACE_H_
diff --git a/webrtc/audio/webrtc_audio.gypi b/webrtc/audio/webrtc_audio.gypi
index 40ccff6..b9d45db 100644
--- a/webrtc/audio/webrtc_audio.gypi
+++ b/webrtc/audio/webrtc_audio.gypi
@@ -18,6 +18,8 @@
       'audio/audio_receive_stream.h',
       'audio/audio_send_stream.cc',
       'audio/audio_send_stream.h',
+      'audio/conversion.h',
+      'audio/scoped_voe_interface.h',
     ],
   },
 }