RTCInboundRTPStreamStats[1] added.

Not all stats are collected in this CL, this must be addressed before
closing the issue.

[1] https://w3c.github.io/webrtc-stats/#inboundrtpstats-dict*

  Re-landed after having to be reverted
  https://codereview.webrtc.org/2470683002/ due to depending on a CL
  that was reverted. Now that that has re-landed
  https://codereview.webrtc.org/2470703002/ this is ready to re-land.

BUG=chromium:627816, chromium:657855, chromium:657854
R=hta@webrtc.org
TBR=deadbeef@webrtc.org

Review-Url: https://codereview.webrtc.org/2465173003
Cr-Commit-Position: refs/heads/master@{#14868}
diff --git a/webrtc/api/rtcstatscollector.cc b/webrtc/api/rtcstatscollector.cc
index 543181e..b14b396 100644
--- a/webrtc/api/rtcstatscollector.cc
+++ b/webrtc/api/rtcstatscollector.cc
@@ -53,6 +53,11 @@
       proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
 }
 
+std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
+  return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
+               : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
+}
+
 std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
   return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
                : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
@@ -88,6 +93,42 @@
   }
 }
 
+void SetInboundRTPStreamStatsFromMediaReceiverInfo(
+    const cricket::MediaReceiverInfo& media_receiver_info,
+    RTCInboundRTPStreamStats* inbound_stats) {
+  RTC_DCHECK(inbound_stats);
+  inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc());
+  // TODO(hbos): Support the remote case. crbug.com/657855
+  inbound_stats->is_remote = false;
+  // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
+  // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117
+  inbound_stats->packets_received =
+      static_cast<uint32_t>(media_receiver_info.packets_rcvd);
+  inbound_stats->bytes_received =
+      static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
+  inbound_stats->fraction_lost =
+      static_cast<double>(media_receiver_info.fraction_lost);
+}
+
+void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
+    const cricket::VoiceReceiverInfo& voice_receiver_info,
+    RTCInboundRTPStreamStats* inbound_stats) {
+  SetInboundRTPStreamStatsFromMediaReceiverInfo(
+      voice_receiver_info, inbound_stats);
+  inbound_stats->media_type = "audio";
+  inbound_stats->jitter =
+      static_cast<double>(voice_receiver_info.jitter_ms) /
+          rtc::kNumMillisecsPerSec;
+}
+
+void SetInboundRTPStreamStatsFromVideoReceiverInfo(
+    const cricket::VideoReceiverInfo& video_receiver_info,
+    RTCInboundRTPStreamStats* inbound_stats) {
+  SetInboundRTPStreamStatsFromMediaReceiverInfo(
+      video_receiver_info, inbound_stats);
+  inbound_stats->media_type = "video";
+}
+
 void SetOutboundRTPStreamStatsFromMediaSenderInfo(
     const cricket::MediaSenderInfo& media_sender_info,
     RTCOutboundRTPStreamStats* outbound_stats) {
@@ -460,6 +501,25 @@
     if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
       std::string transport_id = RTCTransportStatsIDFromBaseChannel(
           session_stats.proxy_to_transport, *pc_->session()->voice_channel());
+      RTC_DCHECK(!transport_id.empty());
+      // Inbound
+      for (const cricket::VoiceReceiverInfo& voice_receiver_info :
+           voice_media_info.receivers) {
+        // TODO(nisse): SSRC == 0 currently means none. Delete check when that
+        // is fixed.
+        if (voice_receiver_info.ssrc() == 0)
+          continue;
+        std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
+            new RTCInboundRTPStreamStats(
+                RTCInboundRTPStreamStatsIDFromSSRC(
+                    true, voice_receiver_info.ssrc()),
+                timestamp_us));
+        SetInboundRTPStreamStatsFromVoiceReceiverInfo(
+            voice_receiver_info, inbound_audio.get());
+        inbound_audio->transport_id = transport_id;
+        report->AddStats(std::move(inbound_audio));
+      }
+      // Outbound
       for (const cricket::VoiceSenderInfo& voice_sender_info :
            voice_media_info.senders) {
         // TODO(nisse): SSRC == 0 currently means none. Delete check when that
@@ -473,8 +533,7 @@
                 timestamp_us));
         SetOutboundRTPStreamStatsFromVoiceSenderInfo(
             voice_sender_info, outbound_audio.get());
-        if (!transport_id.empty())
-          outbound_audio->transport_id = transport_id;
+        outbound_audio->transport_id = transport_id;
         report->AddStats(std::move(outbound_audio));
       }
     }
@@ -485,6 +544,25 @@
     if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
       std::string transport_id = RTCTransportStatsIDFromBaseChannel(
           session_stats.proxy_to_transport, *pc_->session()->video_channel());
+      RTC_DCHECK(!transport_id.empty());
+      // Inbound
+      for (const cricket::VideoReceiverInfo& video_receiver_info :
+           video_media_info.receivers) {
+        // TODO(nisse): SSRC == 0 currently means none. Delete check when that
+        // is fixed.
+        if (video_receiver_info.ssrc() == 0)
+          continue;
+        std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
+            new RTCInboundRTPStreamStats(
+                RTCInboundRTPStreamStatsIDFromSSRC(
+                    false, video_receiver_info.ssrc()),
+                timestamp_us));
+        SetInboundRTPStreamStatsFromVideoReceiverInfo(
+            video_receiver_info, inbound_video.get());
+        inbound_video->transport_id = transport_id;
+        report->AddStats(std::move(inbound_video));
+      }
+      // Outbound
       for (const cricket::VideoSenderInfo& video_sender_info :
            video_media_info.senders) {
         // TODO(nisse): SSRC == 0 currently means none. Delete check when that
@@ -498,8 +576,7 @@
                 timestamp_us));
         SetOutboundRTPStreamStatsFromVideoSenderInfo(
             video_sender_info, outbound_video.get());
-        if (!transport_id.empty())
-          outbound_video->transport_id = transport_id;
+        outbound_video->transport_id = transport_id;
         report->AddStats(std::move(outbound_video));
       }
     }