diff --git a/webrtc/api/rtcstatscollector.cc b/webrtc/api/rtcstatscollector.cc index 543181ede4..b14b39670e 100644 --- a/webrtc/api/rtcstatscollector.cc +++ b/webrtc/api/rtcstatscollector.cc @@ -53,6 +53,11 @@ std::string RTCTransportStatsIDFromBaseChannel( 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 @@ const char* DataStateToRTCDataChannelState( } } +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(media_receiver_info.packets_rcvd); + inbound_stats->bytes_received = + static_cast(media_receiver_info.bytes_rcvd); + inbound_stats->fraction_lost = + static_cast(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(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 @@ void RTCStatsCollector::ProduceRTPStreamStats_s( 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 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 @@ void RTCStatsCollector::ProduceRTPStreamStats_s( 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 @@ void RTCStatsCollector::ProduceRTPStreamStats_s( 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 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 @@ void RTCStatsCollector::ProduceRTPStreamStats_s( 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)); } } diff --git a/webrtc/api/rtcstatscollector.h b/webrtc/api/rtcstatscollector.h index fbef1187a5..2c160ee414 100644 --- a/webrtc/api/rtcstatscollector.h +++ b/webrtc/api/rtcstatscollector.h @@ -101,9 +101,7 @@ class RTCStatsCollector : public virtual rtc::RefCountInterface { // Produces |RTCPeerConnectionStats|. void ProducePeerConnectionStats_s( int64_t timestamp_us, RTCStatsReport* report) const; - // Produces |RTCOutboundRTPStreamStats|. TODO(hbos): Produce both types of - // |RTCRTPStreamStats|, the other one being |RTCInboundRTPStreamStats|. - // crbug.com/657855 + // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|. void ProduceRTPStreamStats_s( int64_t timestamp_us, const SessionStats& session_stats, RTCStatsReport* report) const; diff --git a/webrtc/api/rtcstatscollector_unittest.cc b/webrtc/api/rtcstatscollector_unittest.cc index 312f0d2d98..808668b35c 100644 --- a/webrtc/api/rtcstatscollector_unittest.cc +++ b/webrtc/api/rtcstatscollector_unittest.cc @@ -70,6 +70,10 @@ void PrintTo(const RTCPeerConnectionStats& stats, ::std::ostream* os) { *os << stats.ToString(); } +void PrintTo(const RTCInboundRTPStreamStats& stats, ::std::ostream* os) { + *os << stats.ToString(); +} + void PrintTo(const RTCOutboundRTPStreamStats& stats, ::std::ostream* os) { *os << stats.ToString(); } @@ -975,6 +979,116 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { } } +TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { + MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); + cricket::VoiceChannel voice_channel( + test_->worker_thread(), test_->network_thread(), test_->media_engine(), + voice_media_channel, nullptr, "VoiceContentName", false); + + cricket::VoiceMediaInfo voice_media_info; + voice_media_info.receivers.push_back(cricket::VoiceReceiverInfo()); + voice_media_info.receivers[0].local_stats.push_back( + cricket::SsrcReceiverInfo()); + voice_media_info.receivers[0].local_stats[0].ssrc = 1; + voice_media_info.receivers[0].packets_rcvd = 2; + voice_media_info.receivers[0].bytes_rcvd = 3; + voice_media_info.receivers[0].jitter_ms = 4500; + voice_media_info.receivers[0].fraction_lost = 5.5f; + EXPECT_CALL(*voice_media_channel, GetStats(_)) + .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); + + SessionStats session_stats; + session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; + session_stats.transport_stats["TransportName"].transport_name = + "TransportName"; + + // Make sure the associated |RTCTransportStats| is created. + cricket::TransportChannelStats channel_stats; + channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; + session_stats.transport_stats["TransportName"].channel_stats.push_back( + channel_stats); + + EXPECT_CALL(test_->session(), GetTransportStats(_)) + .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); + EXPECT_CALL(test_->session(), voice_channel()) + .WillRepeatedly(Return(&voice_channel)); + + rtc::scoped_refptr report = GetStatsReport(); + + RTCInboundRTPStreamStats expected_audio( + "RTCInboundRTPAudioStream_1", report->timestamp_us()); + expected_audio.ssrc = "1"; + expected_audio.is_remote = false; + expected_audio.media_type = "audio"; + expected_audio.transport_id = "RTCTransport_TransportName_" + + rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); + expected_audio.packets_received = 2; + expected_audio.bytes_received = 3; + expected_audio.jitter = 4.5; + expected_audio.fraction_lost = 5.5; + + ASSERT(report->Get(expected_audio.id())); + const RTCInboundRTPStreamStats& audio = report->Get( + expected_audio.id())->cast_to(); + EXPECT_EQ(audio, expected_audio); + + EXPECT_TRUE(report->Get(*expected_audio.transport_id)); +} + +TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { + MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); + cricket::VideoChannel video_channel( + test_->worker_thread(), test_->network_thread(), video_media_channel, + nullptr, "VideoContentName", false); + + cricket::VideoMediaInfo video_media_info; + video_media_info.receivers.push_back(cricket::VideoReceiverInfo()); + video_media_info.receivers[0].local_stats.push_back( + cricket::SsrcReceiverInfo()); + video_media_info.receivers[0].local_stats[0].ssrc = 1; + video_media_info.receivers[0].packets_rcvd = 2; + video_media_info.receivers[0].bytes_rcvd = 3; + video_media_info.receivers[0].fraction_lost = 4.5f; + EXPECT_CALL(*video_media_channel, GetStats(_)) + .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); + + SessionStats session_stats; + session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; + session_stats.transport_stats["TransportName"].transport_name = + "TransportName"; + + // Make sure the associated |RTCTransportStats| is created. + cricket::TransportChannelStats channel_stats; + channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; + session_stats.transport_stats["TransportName"].channel_stats.push_back( + channel_stats); + + EXPECT_CALL(test_->session(), GetTransportStats(_)) + .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); + EXPECT_CALL(test_->session(), video_channel()) + .WillRepeatedly(Return(&video_channel)); + + rtc::scoped_refptr report = GetStatsReport(); + + RTCInboundRTPStreamStats expected_audio( + "RTCInboundRTPVideoStream_1", report->timestamp_us()); + expected_audio.ssrc = "1"; + expected_audio.is_remote = false; + expected_audio.media_type = "video"; + expected_audio.transport_id = "RTCTransport_TransportName_" + + rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); + expected_audio.packets_received = 2; + expected_audio.bytes_received = 3; + expected_audio.fraction_lost = 4.5; + + ASSERT(report->Get(expected_audio.id())); + const RTCInboundRTPStreamStats& audio = report->Get( + expected_audio.id())->cast_to(); + EXPECT_EQ(audio, expected_audio); + + EXPECT_TRUE(report->Get(*expected_audio.transport_id)); +} + TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); cricket::VoiceChannel voice_channel( @@ -987,7 +1101,7 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { voice_media_info.senders[0].local_stats[0].ssrc = 1; voice_media_info.senders[0].packets_sent = 2; voice_media_info.senders[0].bytes_sent = 3; - voice_media_info.senders[0].rtt_ms = 4500.0; + voice_media_info.senders[0].rtt_ms = 4500; EXPECT_CALL(*voice_media_channel, GetStats(_)) .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); @@ -999,12 +1113,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { // Make sure the associated |RTCTransportStats| is created. cricket::TransportChannelStats channel_stats; channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; - cricket::ConnectionInfo connection_info; - connection_info.local_candidate = *CreateFakeCandidate( - "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42).get(); - connection_info.remote_candidate = *CreateFakeCandidate( - "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42).get(); - channel_stats.connection_infos.push_back(connection_info); session_stats.transport_stats["TransportName"].channel_stats.push_back( channel_stats); @@ -1049,7 +1157,7 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { video_media_info.senders[0].nacks_rcvd = 4; video_media_info.senders[0].packets_sent = 5; video_media_info.senders[0].bytes_sent = 6; - video_media_info.senders[0].rtt_ms = 7500.0; + video_media_info.senders[0].rtt_ms = 7500; EXPECT_CALL(*video_media_channel, GetStats(_)) .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); @@ -1061,12 +1169,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { // Make sure the associated |RTCTransportStats| is created. cricket::TransportChannelStats channel_stats; channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; - cricket::ConnectionInfo connection_info; - connection_info.local_candidate = *CreateFakeCandidate( - "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42).get(); - connection_info.remote_candidate = *CreateFakeCandidate( - "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42).get(); - channel_stats.connection_infos.push_back(connection_info); session_stats.transport_stats["TransportName"].channel_stats.push_back( channel_stats); diff --git a/webrtc/api/stats/rtcstats_objects.h b/webrtc/api/stats/rtcstats_objects.h index 232e66b388..4788a14154 100644 --- a/webrtc/api/stats/rtcstats_objects.h +++ b/webrtc/api/stats/rtcstats_objects.h @@ -239,6 +239,48 @@ class RTCRTPStreamStats : public RTCStats { RTCRTPStreamStats(std::string&& id, int64_t timestamp_us); }; +// https://w3c.github.io/webrtc-stats/#inboundrtpstats-dict* +// TODO(hbos): Finish implementation and support the remote case +// |is_remote = true|. Tracking bug crbug.com/657855 +class RTCInboundRTPStreamStats final : public RTCRTPStreamStats { + public: + WEBRTC_RTCSTATS_DECL(); + + RTCInboundRTPStreamStats(const std::string& id, int64_t timestamp_us); + RTCInboundRTPStreamStats(std::string&& id, int64_t timestamp_us); + RTCInboundRTPStreamStats(const RTCInboundRTPStreamStats& other); + ~RTCInboundRTPStreamStats() override; + + RTCStatsMember packets_received; + RTCStatsMember bytes_received; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember packets_lost; + // TODO(hbos): Not collected in the "video" case by |RTCStatsCollector|. + // crbug.com/657855 + RTCStatsMember jitter; + RTCStatsMember fraction_lost; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember packets_discarded; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember packets_repaired; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember burst_packets_lost; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember burst_packets_discarded; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember burst_loss_count; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember burst_discard_count; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember burst_loss_rate; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember burst_discard_rate; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember gap_loss_rate; + // TODO(hbos): Not collected by |RTCStatsCollector|. crbug.com/657855 + RTCStatsMember gap_discard_rate; +}; + // https://w3c.github.io/webrtc-stats/#outboundrtpstats-dict* // TODO(hbos): Finish implementation and support the remote case // |is_remote = true|. Tracking bug crbug.com/657856 diff --git a/webrtc/stats/rtcstats_objects.cc b/webrtc/stats/rtcstats_objects.cc index 3d1d369407..2523e36a88 100644 --- a/webrtc/stats/rtcstats_objects.cc +++ b/webrtc/stats/rtcstats_objects.cc @@ -346,6 +346,72 @@ RTCRTPStreamStats::RTCRTPStreamStats( RTCRTPStreamStats::~RTCRTPStreamStats() { } +WEBRTC_RTCSTATS_IMPL( + RTCInboundRTPStreamStats, RTCRTPStreamStats, "inbound-rtp", + &packets_received, + &bytes_received, + &packets_lost, + &jitter, + &fraction_lost, + &packets_discarded, + &packets_repaired, + &burst_packets_lost, + &burst_packets_discarded, + &burst_loss_count, + &burst_discard_count, + &burst_loss_rate, + &burst_discard_rate, + &gap_loss_rate, + &gap_discard_rate); + +RTCInboundRTPStreamStats::RTCInboundRTPStreamStats( + const std::string& id, int64_t timestamp_us) + : RTCInboundRTPStreamStats(std::string(id), timestamp_us) { +} + +RTCInboundRTPStreamStats::RTCInboundRTPStreamStats( + std::string&& id, int64_t timestamp_us) + : RTCRTPStreamStats(std::move(id), timestamp_us), + packets_received("packetsReceived"), + bytes_received("bytesReceived"), + packets_lost("packetsLost"), + jitter("jitter"), + fraction_lost("fractionLost"), + packets_discarded("packetsDiscarded"), + packets_repaired("packetsRepaired"), + burst_packets_lost("burstPacketsLost"), + burst_packets_discarded("burstPacketsDiscarded"), + burst_loss_count("burstLossCount"), + burst_discard_count("burstDiscardCount"), + burst_loss_rate("burstLossRate"), + burst_discard_rate("burstDiscardRate"), + gap_loss_rate("gapLossRate"), + gap_discard_rate("gapDiscardRate") { +} + +RTCInboundRTPStreamStats::RTCInboundRTPStreamStats( + const RTCInboundRTPStreamStats& other) + : RTCRTPStreamStats(other), + packets_received(other.packets_received), + bytes_received(other.bytes_received), + packets_lost(other.packets_lost), + jitter(other.jitter), + fraction_lost(other.fraction_lost), + packets_discarded(other.packets_discarded), + packets_repaired(other.packets_repaired), + burst_packets_lost(other.burst_packets_lost), + burst_packets_discarded(other.burst_packets_discarded), + burst_loss_count(other.burst_loss_count), + burst_discard_count(other.burst_discard_count), + burst_loss_rate(other.burst_loss_rate), + burst_discard_rate(other.burst_discard_rate), + gap_loss_rate(other.gap_loss_rate), + gap_discard_rate(other.gap_discard_rate) { +} + +RTCInboundRTPStreamStats::~RTCInboundRTPStreamStats() { +} + WEBRTC_RTCSTATS_IMPL( RTCOutboundRTPStreamStats, RTCRTPStreamStats, "outbound-rtp", &packets_sent,