[getStats] Implement "media-source" audio levels, fixing Chrome bug.

Implements RTCAudioSourceStats members:
- audioLevel
- totalAudioEnergy
- totalSamplesDuration
In this CL description these are collectively referred to as the audio
levels.

The audio levels are removed from sending "track" stats (in Chrome,
these are now reported as undefined instead of 0).

Background:
  For sending tracks, audio levels were always reported as 0 in Chrome
(https://crbug.com/736403), while audio levels were correctly reported
for receiving tracks. This problem affected the standard getStats() but
not the legacy getStats(), blocking some people from migrating. This
was likely not a problem in native third_party/webrtc code because the
delivery of audio frames from device to send-stream uses a different
code path outside of chromium.
  A recent PR (https://github.com/w3c/webrtc-stats/pull/451) moved the
send-side audio levels to the RTCAudioSourceStats, while keeping the
receive-side audio levels on the "track" stats. This allows an
implementation to report the audio levels even if samples are not sent
onto the network (such as if an ICE connection has not been established
yet), reflecting some of the current implementation.

Changes:
1. Audio levels are added to RTCAudioSourceStats. Send-side audio
   "track" stats are left undefined. Receive-side audio "track" stats
   are not changed in this CL and continue to work.
2. Audio level computation is moved from the AudioState and
   AudioTransportImpl to the AudioSendStream. This is because a) the
   AudioTransportImpl::RecordedDataIsAvailable() code path is not
   exercised in chromium, and b) audio levels should, per-spec, not be
   calculated on a per-call basis, for which the AudioState is defined.
3. The audio level computation is now performed in
   AudioSendStream::SendAudioData(), a code path used by both native
   and chromium code.
4. Comments are added to document behavior of existing code, such as
   AudioLevel and AudioSendStream::SendAudioData().

Note:
  In this CL, just like before this CL, audio level is only calculated
after an AudioSendStream has been created. This means that before an
O/A negotiation, audio levels are unavailable.
  According to spec, if we have an audio source, we should have audio
levels. An immediate solution to this would have been to calculate the
audio level at pc/rtp_sender.cc. The problem is that the
LocalAudioSinkAdapter::OnData() code path, while exercised in chromium,
is not exercised in native code. The issue of calculating audio levels
on a per-source bases rather than on a per-send stream basis is left to
https://crbug.com/webrtc/10771, an existing "media-source" bug.

This CL can be verified manually in Chrome at:
https://codepen.io/anon/pen/vqRGyq

Bug: chromium:736403, webrtc:10771
Change-Id: I8036cd9984f3b187c3177470a8c0d6670a201a5a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/143789
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28480}
diff --git a/audio/audio_send_stream_unittest.cc b/audio/audio_send_stream_unittest.cc
index 4531755..022516a 100644
--- a/audio/audio_send_stream_unittest.cc
+++ b/audio/audio_send_stream_unittest.cc
@@ -23,6 +23,7 @@
 #include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
 #include "modules/audio_device/include/mock_audio_device.h"
 #include "modules/audio_mixer/audio_mixer_impl.h"
+#include "modules/audio_mixer/sine_wave_generator.h"
 #include "modules/audio_processing/include/audio_processing_statistics.h"
 #include "modules/audio_processing/include/mock_audio_processing.h"
 #include "modules/rtp_rtcp/mocks/mock_rtcp_bandwidth_observer.h"
@@ -40,6 +41,7 @@
 namespace {
 
 using ::testing::_;
+using ::testing::AnyNumber;
 using ::testing::Eq;
 using ::testing::Field;
 using ::testing::Invoke;
@@ -47,6 +49,8 @@
 using ::testing::Return;
 using ::testing::StrEq;
 
+static const float kTolerance = 0.0001f;
+
 const uint32_t kSsrc = 1234;
 const char* kCName = "foo_name";
 const int kAudioLevelId = 2;
@@ -317,6 +321,24 @@
   TaskQueueForTest worker_queue_;
   std::unique_ptr<AudioEncoder> audio_encoder_;
 };
+
+// The audio level ranges linearly [0,32767].
+std::unique_ptr<AudioFrame> CreateAudioFrame1kHzSineWave(int16_t audio_level,
+                                                         int duration_ms,
+                                                         int sample_rate_hz,
+                                                         size_t num_channels) {
+  size_t samples_per_channel = sample_rate_hz / (1000 / duration_ms);
+  std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
+  std::unique_ptr<AudioFrame> audio_frame = absl::make_unique<AudioFrame>();
+  audio_frame->UpdateFrame(0 /* RTP timestamp */, &audio_data[0],
+                           samples_per_channel, sample_rate_hz,
+                           AudioFrame::SpeechType::kNormalSpeech,
+                           AudioFrame::VADActivity::kVadUnknown, num_channels);
+  SineWaveGenerator wave_generator(1000.0, audio_level);
+  wave_generator.GenerateNextFrame(audio_frame.get());
+  return audio_frame;
+}
+
 }  // namespace
 
 TEST(AudioSendStreamTest, ConfigToString) {
@@ -415,6 +437,46 @@
   EXPECT_FALSE(stats.typing_noise_detected);
 }
 
+TEST(AudioSendStreamTest, GetStatsAudioLevel) {
+  ConfigHelper helper(false, true);
+  auto send_stream = helper.CreateAudioSendStream();
+  helper.SetupMockForGetStats();
+  EXPECT_CALL(*helper.channel_send(), ProcessAndEncodeAudioForMock(_))
+      .Times(AnyNumber());
+
+  constexpr int kSampleRateHz = 48000;
+  constexpr size_t kNumChannels = 1;
+
+  constexpr int16_t kSilentAudioLevel = 0;
+  constexpr int16_t kMaxAudioLevel = 32767;  // Audio level is [0,32767].
+  constexpr int kAudioFrameDurationMs = 10;
+
+  // Process 10 audio frames (100 ms) of silence. After this, on the next
+  // (11-th) frame, the audio level will be updated with the maximum audio level
+  // of the first 11 frames. See AudioLevel.
+  for (size_t i = 0; i < 10; ++i) {
+    send_stream->SendAudioData(CreateAudioFrame1kHzSineWave(
+        kSilentAudioLevel, kAudioFrameDurationMs, kSampleRateHz, kNumChannels));
+  }
+  AudioSendStream::Stats stats = send_stream->GetStats();
+  EXPECT_EQ(kSilentAudioLevel, stats.audio_level);
+  EXPECT_NEAR(0.0f, stats.total_input_energy, kTolerance);
+  EXPECT_NEAR(0.1f, stats.total_input_duration, kTolerance);  // 100 ms = 0.1 s
+
+  // Process 10 audio frames (100 ms) of maximum audio level.
+  // Note that AudioLevel updates the audio level every 11th frame, processing
+  // 10 frames above was needed to see a non-zero audio level here.
+  for (size_t i = 0; i < 10; ++i) {
+    send_stream->SendAudioData(CreateAudioFrame1kHzSineWave(
+        kMaxAudioLevel, kAudioFrameDurationMs, kSampleRateHz, kNumChannels));
+  }
+  stats = send_stream->GetStats();
+  EXPECT_EQ(kMaxAudioLevel, stats.audio_level);
+  // Energy increases by energy*duration, where energy is audio level in [0,1].
+  EXPECT_NEAR(0.1f, stats.total_input_energy, kTolerance);    // 0.1 s of max
+  EXPECT_NEAR(0.2f, stats.total_input_duration, kTolerance);  // 200 ms = 0.2 s
+}
+
 TEST(AudioSendStreamTest, SendCodecAppliesAudioNetworkAdaptor) {
   ConfigHelper helper(false, true);
   helper.config().send_codec_spec =