Adds a histogram metric tracking for how long audio RTP packets are sent
through streams related to a call object.

The Call object does not know directly when packets pass through it, only which
AudioSendStreams are used. Each AudioSendStream has a pointer to the Transport
object through which its packets are send.

This CL:
By registering an internal wrapper class, TimedTransport, the AudioSendStream
can stay up-to-date on when packets have passed through its Transport. This
lifetime (as an interval) is then queried by the Call when the AudioSendStream
is destroyed. When Call is destroyed, all streams are guaranteed to have been
destroyed and hence Call is up-to-date on packet activity.

The class TimeInterval keeps the code in Call and AudioSendStream smaller, with
fewer get methods in their APIs and less code for updating values.

Also modifies the unit test for AudioSendStream: it previously enforced that
the stream registers (with its channel proxy) the same transport that it was
constructed with.

BUG=webrtc:7882

Review-Url: https://codereview.webrtc.org/2979833002
Cr-Commit-Position: refs/heads/master@{#19087}
diff --git a/webrtc/audio/audio_send_stream.cc b/webrtc/audio/audio_send_stream.cc
index 0659cbf..103d630 100644
--- a/webrtc/audio/audio_send_stream.cc
+++ b/webrtc/audio/audio_send_stream.cc
@@ -51,6 +51,30 @@
 }
 }  // namespace
 
+// TODO(saza): Move this declaration further down when we can use
+// std::make_unique.
+class AudioSendStream::TimedTransport : public Transport {
+ public:
+  TimedTransport(Transport* transport, TimeInterval* time_interval)
+      : transport_(transport), lifetime_(time_interval) {}
+  bool SendRtp(const uint8_t* packet,
+               size_t length,
+               const PacketOptions& options) {
+    if (lifetime_) {
+      lifetime_->Extend();
+    }
+    return transport_->SendRtp(packet, length, options);
+  }
+  bool SendRtcp(const uint8_t* packet, size_t length) {
+    return transport_->SendRtcp(packet, length);
+  }
+  ~TimedTransport() {}
+
+ private:
+  Transport* transport_;
+  TimeInterval* lifetime_;
+};
+
 AudioSendStream::AudioSendStream(
     const webrtc::AudioSendStream::Config& config,
     const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
@@ -137,8 +161,14 @@
     if (old_config.send_transport) {
       channel_proxy->DeRegisterExternalTransport();
     }
-
-    channel_proxy->RegisterExternalTransport(new_config.send_transport);
+    if (new_config.send_transport) {
+      stream->timed_send_transport_adapter_.reset(new TimedTransport(
+          new_config.send_transport, &stream->active_lifetime_));
+    } else {
+      stream->timed_send_transport_adapter_.reset(nullptr);
+    }
+    channel_proxy->RegisterExternalTransport(
+        stream->timed_send_transport_adapter_.get());
   }
 
   // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
@@ -391,6 +421,10 @@
   return rtp_rtcp_module_->GetRtpState();
 }
 
+const TimeInterval& AudioSendStream::GetActiveLifetime() const {
+  return active_lifetime_;
+}
+
 VoiceEngine* AudioSendStream::voice_engine() const {
   internal::AudioState* audio_state =
       static_cast<internal::AudioState*>(audio_state_.get());