hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 11 | #include <initializer_list> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 12 | #include <memory> |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 13 | #include <ostream> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 14 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 15 | #include <utility> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/rtpparameters.h" |
| 19 | #include "api/stats/rtcstats_objects.h" |
| 20 | #include "api/stats/rtcstatsreport.h" |
| 21 | #include "api/test/mock_rtpreceiver.h" |
| 22 | #include "api/test/mock_rtpsender.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "p2p/base/p2pconstants.h" |
| 24 | #include "p2p/base/port.h" |
| 25 | #include "pc/mediastream.h" |
| 26 | #include "pc/mediastreamtrack.h" |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 27 | #include "pc/rtcstatscollector.h" |
| 28 | #include "pc/test/fakepeerconnectionforstats.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "pc/test/mock_datachannel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "pc/test/rtcstatsobtainer.h" |
| 31 | #include "rtc_base/checks.h" |
| 32 | #include "rtc_base/fakeclock.h" |
| 33 | #include "rtc_base/fakesslidentity.h" |
| 34 | #include "rtc_base/gunit.h" |
| 35 | #include "rtc_base/logging.h" |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 36 | #include "rtc_base/ptr_util.h" |
| 37 | #include "rtc_base/stringutils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "rtc_base/timedelta.h" |
| 39 | #include "rtc_base/timeutils.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 40 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 41 | using testing::Invoke; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 42 | using testing::Return; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 43 | |
| 44 | namespace webrtc { |
| 45 | |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 46 | // These are used by gtest code, such as if |EXPECT_EQ| fails. |
| 47 | void PrintTo(const RTCCertificateStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 48 | *os << stats.ToJson(); |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 51 | void PrintTo(const RTCCodecStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 52 | *os << stats.ToJson(); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 53 | } |
| 54 | |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 55 | void PrintTo(const RTCDataChannelStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 56 | *os << stats.ToJson(); |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void PrintTo(const RTCIceCandidatePairStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 60 | *os << stats.ToJson(); |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void PrintTo(const RTCLocalIceCandidateStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 64 | *os << stats.ToJson(); |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void PrintTo(const RTCRemoteIceCandidateStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 68 | *os << stats.ToJson(); |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void PrintTo(const RTCPeerConnectionStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 72 | *os << stats.ToJson(); |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 75 | void PrintTo(const RTCMediaStreamStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 76 | *os << stats.ToJson(); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void PrintTo(const RTCMediaStreamTrackStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 80 | *os << stats.ToJson(); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 81 | } |
| 82 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 83 | void PrintTo(const RTCInboundRTPStreamStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 84 | *os << stats.ToJson(); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 85 | } |
| 86 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 87 | void PrintTo(const RTCOutboundRTPStreamStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 88 | *os << stats.ToJson(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 89 | } |
| 90 | |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 91 | void PrintTo(const RTCTransportStats& stats, ::std::ostream* os) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 92 | *os << stats.ToJson(); |
hbos | da389e3 | 2016-10-25 10:55:08 -0700 | [diff] [blame] | 93 | } |
| 94 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 95 | namespace { |
| 96 | |
| 97 | const int64_t kGetStatsReportTimeoutMs = 1000; |
| 98 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 99 | struct CertificateInfo { |
| 100 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 101 | std::vector<std::string> ders; |
| 102 | std::vector<std::string> pems; |
| 103 | std::vector<std::string> fingerprints; |
| 104 | }; |
| 105 | |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 106 | // Return the ID for an object of the given type in a report. |
| 107 | // The object must be present and be unique. |
| 108 | template <typename T> |
| 109 | std::string IdForType(const RTCStatsReport* report) { |
| 110 | auto stats_of_my_type = report->RTCStatsReport::GetStatsOfType<T>(); |
| 111 | // We cannot use ASSERT here, since we're within a function. |
| 112 | EXPECT_EQ(1, stats_of_my_type.size()) |
| 113 | << "Unexpected number of stats of this type"; |
| 114 | if (stats_of_my_type.size() == 1) { |
| 115 | return stats_of_my_type[0]->id(); |
| 116 | } else { |
| 117 | // Return something that is not going to be a valid stas ID. |
| 118 | return "Type not found"; |
| 119 | } |
| 120 | } |
| 121 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 122 | std::unique_ptr<CertificateInfo> CreateFakeCertificateAndInfoFromDers( |
| 123 | const std::vector<std::string>& ders) { |
| 124 | RTC_CHECK(!ders.empty()); |
| 125 | std::unique_ptr<CertificateInfo> info(new CertificateInfo()); |
| 126 | info->ders = ders; |
| 127 | for (const std::string& der : ders) { |
| 128 | info->pems.push_back(rtc::SSLIdentity::DerToPem( |
| 129 | "CERTIFICATE", |
| 130 | reinterpret_cast<const unsigned char*>(der.c_str()), |
| 131 | der.length())); |
| 132 | } |
| 133 | info->certificate = |
| 134 | rtc::RTCCertificate::Create(std::unique_ptr<rtc::FakeSSLIdentity>( |
| 135 | new rtc::FakeSSLIdentity(rtc::FakeSSLCertificate(info->pems)))); |
| 136 | // Strip header/footer and newline characters of PEM strings. |
| 137 | for (size_t i = 0; i < info->pems.size(); ++i) { |
| 138 | rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27, |
| 139 | "", 0, &info->pems[i]); |
| 140 | rtc::replace_substrs("-----END CERTIFICATE-----", 25, |
| 141 | "", 0, &info->pems[i]); |
| 142 | rtc::replace_substrs("\n", 1, |
| 143 | "", 0, &info->pems[i]); |
| 144 | } |
| 145 | // Fingerprint of leaf certificate. |
| 146 | std::unique_ptr<rtc::SSLFingerprint> fp( |
| 147 | rtc::SSLFingerprint::Create("sha-1", |
| 148 | &info->certificate->ssl_certificate())); |
| 149 | EXPECT_TRUE(fp); |
| 150 | info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); |
| 151 | // Fingerprints of the rest of the chain. |
| 152 | std::unique_ptr<rtc::SSLCertChain> chain = |
| 153 | info->certificate->ssl_certificate().GetChain(); |
| 154 | if (chain) { |
| 155 | for (size_t i = 0; i < chain->GetSize(); i++) { |
| 156 | fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i))); |
| 157 | EXPECT_TRUE(fp); |
| 158 | info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); |
| 159 | } |
| 160 | } |
| 161 | EXPECT_EQ(info->ders.size(), info->fingerprints.size()); |
| 162 | return info; |
| 163 | } |
| 164 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 165 | std::unique_ptr<cricket::Candidate> CreateFakeCandidate( |
| 166 | const std::string& hostname, |
| 167 | int port, |
| 168 | const std::string& protocol, |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 169 | const rtc::AdapterType adapter_type, |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 170 | const std::string& candidate_type, |
| 171 | uint32_t priority) { |
| 172 | std::unique_ptr<cricket::Candidate> candidate(new cricket::Candidate()); |
| 173 | candidate->set_address(rtc::SocketAddress(hostname, port)); |
| 174 | candidate->set_protocol(protocol); |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 175 | candidate->set_network_type(adapter_type); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 176 | candidate->set_type(candidate_type); |
| 177 | candidate->set_priority(priority); |
| 178 | return candidate; |
| 179 | } |
| 180 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 181 | class FakeAudioTrackForStats |
| 182 | : public MediaStreamTrack<AudioTrackInterface> { |
| 183 | public: |
| 184 | static rtc::scoped_refptr<FakeAudioTrackForStats> Create( |
| 185 | const std::string& id, |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 186 | MediaStreamTrackInterface::TrackState state) { |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 187 | rtc::scoped_refptr<FakeAudioTrackForStats> audio_track_stats( |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 188 | new rtc::RefCountedObject<FakeAudioTrackForStats>(id)); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 189 | audio_track_stats->set_state(state); |
| 190 | return audio_track_stats; |
| 191 | } |
| 192 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 193 | explicit FakeAudioTrackForStats(const std::string& id) |
| 194 | : MediaStreamTrack<AudioTrackInterface>(id) {} |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 195 | |
| 196 | std::string kind() const override { |
| 197 | return MediaStreamTrackInterface::kAudioKind; |
| 198 | } |
| 199 | webrtc::AudioSourceInterface* GetSource() const override { return nullptr; } |
| 200 | void AddSink(webrtc::AudioTrackSinkInterface* sink) override {} |
| 201 | void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override {} |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 202 | bool GetSignalLevel(int* level) override { return false; } |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 203 | rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() override { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 204 | return nullptr; |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 205 | } |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | class FakeVideoTrackForStats |
| 209 | : public MediaStreamTrack<VideoTrackInterface> { |
| 210 | public: |
| 211 | static rtc::scoped_refptr<FakeVideoTrackForStats> Create( |
| 212 | const std::string& id, |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 213 | MediaStreamTrackInterface::TrackState state) { |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 214 | rtc::scoped_refptr<FakeVideoTrackForStats> video_track( |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 215 | new rtc::RefCountedObject<FakeVideoTrackForStats>(id)); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 216 | video_track->set_state(state); |
| 217 | return video_track; |
| 218 | } |
| 219 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 220 | explicit FakeVideoTrackForStats(const std::string& id) |
| 221 | : MediaStreamTrack<VideoTrackInterface>(id) {} |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 222 | |
| 223 | std::string kind() const override { |
| 224 | return MediaStreamTrackInterface::kVideoKind; |
| 225 | } |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 226 | |
| 227 | void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 228 | const rtc::VideoSinkWants& wants) override{}; |
| 229 | void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override{}; |
| 230 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 231 | VideoTrackSourceInterface* GetSource() const override { return nullptr; } |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 234 | rtc::scoped_refptr<MediaStreamTrackInterface> CreateFakeTrack( |
| 235 | cricket::MediaType media_type, |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 236 | const std::string& track_id, |
| 237 | MediaStreamTrackInterface::TrackState track_state) { |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 238 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 239 | return FakeAudioTrackForStats::Create(track_id, track_state); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 240 | } else { |
| 241 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 242 | return FakeVideoTrackForStats::Create(track_id, track_state); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 246 | rtc::scoped_refptr<MockRtpSender> CreateMockSender( |
Mirko Bonadei | c61ce0d | 2017-11-21 17:04:20 +0100 | [diff] [blame] | 247 | const rtc::scoped_refptr<MediaStreamTrackInterface>& track, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 248 | uint32_t ssrc, |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 249 | int attachment_id, |
| 250 | std::vector<std::string> local_stream_ids) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 251 | rtc::scoped_refptr<MockRtpSender> sender( |
| 252 | new rtc::RefCountedObject<MockRtpSender>()); |
| 253 | EXPECT_CALL(*sender, track()).WillRepeatedly(Return(track)); |
| 254 | EXPECT_CALL(*sender, ssrc()).WillRepeatedly(Return(ssrc)); |
| 255 | EXPECT_CALL(*sender, media_type()).WillRepeatedly(Return( |
| 256 | track->kind() == MediaStreamTrackInterface::kAudioKind |
| 257 | ? cricket::MEDIA_TYPE_AUDIO : cricket::MEDIA_TYPE_VIDEO)); |
| 258 | EXPECT_CALL(*sender, GetParameters()).WillRepeatedly(Invoke( |
| 259 | [ssrc]() { |
| 260 | RtpParameters params; |
| 261 | params.encodings.push_back(RtpEncodingParameters()); |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 262 | params.encodings[0].ssrc = ssrc; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 263 | return params; |
| 264 | })); |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 265 | EXPECT_CALL(*sender, AttachmentId()).WillRepeatedly(Return(attachment_id)); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 266 | EXPECT_CALL(*sender, stream_ids()).WillRepeatedly(Return(local_stream_ids)); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 267 | return sender; |
| 268 | } |
| 269 | |
| 270 | rtc::scoped_refptr<MockRtpReceiver> CreateMockReceiver( |
Mirko Bonadei | c61ce0d | 2017-11-21 17:04:20 +0100 | [diff] [blame] | 271 | const rtc::scoped_refptr<MediaStreamTrackInterface>& track, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 272 | uint32_t ssrc, |
| 273 | int attachment_id) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 274 | rtc::scoped_refptr<MockRtpReceiver> receiver( |
| 275 | new rtc::RefCountedObject<MockRtpReceiver>()); |
| 276 | EXPECT_CALL(*receiver, track()).WillRepeatedly(Return(track)); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 277 | EXPECT_CALL(*receiver, streams()) |
| 278 | .WillRepeatedly( |
| 279 | Return(std::vector<rtc::scoped_refptr<MediaStreamInterface>>({}))); |
| 280 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 281 | EXPECT_CALL(*receiver, media_type()).WillRepeatedly(Return( |
| 282 | track->kind() == MediaStreamTrackInterface::kAudioKind |
| 283 | ? cricket::MEDIA_TYPE_AUDIO : cricket::MEDIA_TYPE_VIDEO)); |
| 284 | EXPECT_CALL(*receiver, GetParameters()).WillRepeatedly(Invoke( |
| 285 | [ssrc]() { |
| 286 | RtpParameters params; |
| 287 | params.encodings.push_back(RtpEncodingParameters()); |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 288 | params.encodings[0].ssrc = ssrc; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 289 | return params; |
| 290 | })); |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 291 | EXPECT_CALL(*receiver, AttachmentId()).WillRepeatedly(Return(attachment_id)); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 292 | return receiver; |
| 293 | } |
| 294 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 295 | class RTCStatsCollectorWrapper { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 296 | public: |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 297 | explicit RTCStatsCollectorWrapper( |
| 298 | rtc::scoped_refptr<FakePeerConnectionForStats> pc) |
| 299 | : pc_(pc), |
| 300 | stats_collector_( |
| 301 | RTCStatsCollector::Create(pc, 50 * rtc::kNumMicrosecsPerMillisec)) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 304 | rtc::scoped_refptr<RTCStatsCollector> stats_collector() { |
| 305 | return stats_collector_; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 308 | rtc::scoped_refptr<const RTCStatsReport> GetFreshStatsReport() { |
| 309 | stats_collector_->ClearCachedStatsReport(); |
| 310 | return GetStatsReport(); |
| 311 | } |
| 312 | |
| 313 | rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() { |
| 314 | rtc::scoped_refptr<RTCStatsObtainer> callback = RTCStatsObtainer::Create(); |
| 315 | stats_collector_->GetStatsReport(callback); |
| 316 | EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); |
| 317 | int64_t after = rtc::TimeUTCMicros(); |
| 318 | for (const RTCStats& stats : *callback->report()) { |
| 319 | EXPECT_LE(stats.timestamp_us(), after); |
| 320 | } |
| 321 | return callback->report(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 322 | } |
| 323 | |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 324 | void SetupLocalTrackAndSender(cricket::MediaType media_type, |
| 325 | const std::string& track_id, |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 326 | uint32_t ssrc, |
| 327 | bool add_stream) { |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 328 | rtc::scoped_refptr<MediaStream> local_stream; |
| 329 | if (add_stream) { |
| 330 | local_stream = MediaStream::Create("LocalStreamLabel"); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 331 | pc_->mutable_local_streams()->AddStream(local_stream); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 332 | } |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 333 | |
| 334 | rtc::scoped_refptr<MediaStreamTrackInterface> track; |
| 335 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 336 | track = CreateFakeTrack(media_type, track_id, |
| 337 | MediaStreamTrackInterface::kLive); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 338 | if (add_stream) { |
| 339 | local_stream->AddTrack(static_cast<AudioTrackInterface*>(track.get())); |
| 340 | } |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 341 | } else { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 342 | track = CreateFakeTrack(media_type, track_id, |
| 343 | MediaStreamTrackInterface::kLive); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 344 | if (add_stream) { |
| 345 | local_stream->AddTrack(static_cast<VideoTrackInterface*>(track.get())); |
| 346 | } |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 349 | rtc::scoped_refptr<MockRtpSender> sender = |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 350 | CreateMockSender(track, ssrc, 50, {}); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 351 | pc_->AddSender(sender); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void SetupRemoteTrackAndReceiver(cricket::MediaType media_type, |
| 355 | const std::string& track_id, |
| 356 | uint32_t ssrc) { |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 357 | rtc::scoped_refptr<MediaStream> remote_stream = |
| 358 | MediaStream::Create("RemoteStreamLabel"); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 359 | pc_->mutable_remote_streams()->AddStream(remote_stream); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 360 | |
| 361 | rtc::scoped_refptr<MediaStreamTrackInterface> track; |
| 362 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 363 | track = CreateFakeTrack(media_type, track_id, |
| 364 | MediaStreamTrackInterface::kLive); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 365 | remote_stream->AddTrack(static_cast<AudioTrackInterface*>(track.get())); |
| 366 | } else { |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 367 | track = CreateFakeTrack(media_type, track_id, |
| 368 | MediaStreamTrackInterface::kLive); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 369 | remote_stream->AddTrack(static_cast<VideoTrackInterface*>(track.get())); |
| 370 | } |
| 371 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 372 | rtc::scoped_refptr<MockRtpReceiver> receiver = |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 373 | CreateMockReceiver(track, ssrc, 62); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 374 | EXPECT_CALL(*receiver, streams()) |
| 375 | .WillRepeatedly( |
| 376 | Return(std::vector<rtc::scoped_refptr<MediaStreamInterface>>( |
| 377 | {remote_stream}))); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 378 | pc_->AddReceiver(receiver); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 379 | } |
| 380 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 381 | // Attaches tracks to peer connections by configuring RTP senders and RTP |
| 382 | // receivers according to the tracks' pairings with |
| 383 | // |[Voice/Video][Sender/Receiver]Info| and their SSRCs. Local tracks can be |
| 384 | // associated with multiple |[Voice/Video]SenderInfo|s, remote tracks can only |
| 385 | // be associated with one |[Voice/Video]ReceiverInfo|. |
| 386 | void CreateMockRtpSendersReceiversAndChannels( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 387 | std::initializer_list< |
| 388 | std::pair<MediaStreamTrackInterface*, cricket::VoiceSenderInfo>> |
| 389 | local_audio_track_info_pairs, |
| 390 | std::initializer_list< |
| 391 | std::pair<MediaStreamTrackInterface*, cricket::VoiceReceiverInfo>> |
| 392 | remote_audio_track_info_pairs, |
| 393 | std::initializer_list< |
| 394 | std::pair<MediaStreamTrackInterface*, cricket::VideoSenderInfo>> |
| 395 | local_video_track_info_pairs, |
| 396 | std::initializer_list< |
| 397 | std::pair<MediaStreamTrackInterface*, cricket::VideoReceiverInfo>> |
| 398 | remote_video_track_info_pairs, |
| 399 | std::vector<std::string> local_stream_ids, |
| 400 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> remote_streams) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 401 | cricket::VoiceMediaInfo voice_media_info; |
| 402 | cricket::VideoMediaInfo video_media_info; |
| 403 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 404 | // Local audio tracks and voice sender infos |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 405 | int attachment_id = 147; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 406 | for (auto& pair : local_audio_track_info_pairs) { |
| 407 | MediaStreamTrackInterface* local_audio_track = pair.first; |
| 408 | const cricket::VoiceSenderInfo& voice_sender_info = pair.second; |
| 409 | RTC_DCHECK_EQ(local_audio_track->kind(), |
| 410 | MediaStreamTrackInterface::kAudioKind); |
| 411 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 412 | voice_media_info.senders.push_back(voice_sender_info); |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 413 | rtc::scoped_refptr<MockRtpSender> rtp_sender = CreateMockSender( |
| 414 | rtc::scoped_refptr<MediaStreamTrackInterface>(local_audio_track), |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 415 | voice_sender_info.local_stats[0].ssrc, attachment_id++, |
| 416 | local_stream_ids); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 417 | pc_->AddSender(rtp_sender); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 418 | } |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 419 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 420 | // Remote audio tracks and voice receiver infos |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 421 | attachment_id = 181; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 422 | for (auto& pair : remote_audio_track_info_pairs) { |
| 423 | MediaStreamTrackInterface* remote_audio_track = pair.first; |
| 424 | const cricket::VoiceReceiverInfo& voice_receiver_info = pair.second; |
| 425 | RTC_DCHECK_EQ(remote_audio_track->kind(), |
| 426 | MediaStreamTrackInterface::kAudioKind); |
| 427 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 428 | voice_media_info.receivers.push_back(voice_receiver_info); |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 429 | rtc::scoped_refptr<MockRtpReceiver> rtp_receiver = CreateMockReceiver( |
| 430 | rtc::scoped_refptr<MediaStreamTrackInterface>(remote_audio_track), |
| 431 | voice_receiver_info.local_stats[0].ssrc, attachment_id++); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 432 | EXPECT_CALL(*rtp_receiver, streams()) |
| 433 | .WillRepeatedly(Return(remote_streams)); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 434 | pc_->AddReceiver(rtp_receiver); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 435 | } |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 436 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 437 | // Local video tracks and video sender infos |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 438 | attachment_id = 151; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 439 | for (auto& pair : local_video_track_info_pairs) { |
| 440 | MediaStreamTrackInterface* local_video_track = pair.first; |
| 441 | const cricket::VideoSenderInfo& video_sender_info = pair.second; |
| 442 | RTC_DCHECK_EQ(local_video_track->kind(), |
| 443 | MediaStreamTrackInterface::kVideoKind); |
| 444 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 445 | video_media_info.senders.push_back(video_sender_info); |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 446 | rtc::scoped_refptr<MockRtpSender> rtp_sender = CreateMockSender( |
| 447 | rtc::scoped_refptr<MediaStreamTrackInterface>(local_video_track), |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 448 | video_sender_info.local_stats[0].ssrc, attachment_id++, |
| 449 | local_stream_ids); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 450 | pc_->AddSender(rtp_sender); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 451 | } |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 452 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 453 | // Remote video tracks and video receiver infos |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 454 | attachment_id = 191; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 455 | for (auto& pair : remote_video_track_info_pairs) { |
| 456 | MediaStreamTrackInterface* remote_video_track = pair.first; |
| 457 | const cricket::VideoReceiverInfo& video_receiver_info = pair.second; |
| 458 | RTC_DCHECK_EQ(remote_video_track->kind(), |
| 459 | MediaStreamTrackInterface::kVideoKind); |
| 460 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 461 | video_media_info.receivers.push_back(video_receiver_info); |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 462 | rtc::scoped_refptr<MockRtpReceiver> rtp_receiver = CreateMockReceiver( |
| 463 | rtc::scoped_refptr<MediaStreamTrackInterface>(remote_video_track), |
| 464 | video_receiver_info.local_stats[0].ssrc, attachment_id++); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 465 | EXPECT_CALL(*rtp_receiver, streams()) |
| 466 | .WillRepeatedly(Return(remote_streams)); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 467 | pc_->AddReceiver(rtp_receiver); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 468 | } |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 469 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 470 | auto* voice_media_channel = pc_->AddVoiceChannel("audio", "transport"); |
| 471 | voice_media_channel->SetStats(voice_media_info); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 472 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 473 | auto* video_media_channel = pc_->AddVideoChannel("video", "transport"); |
| 474 | video_media_channel->SetStats(video_media_info); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 475 | } |
| 476 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 477 | private: |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 478 | rtc::scoped_refptr<FakePeerConnectionForStats> pc_; |
| 479 | rtc::scoped_refptr<RTCStatsCollector> stats_collector_; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 480 | }; |
| 481 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 482 | class RTCStatsCollectorTest : public testing::Test { |
| 483 | public: |
| 484 | RTCStatsCollectorTest() |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 485 | : pc_(new rtc::RefCountedObject<FakePeerConnectionForStats>()), |
| 486 | stats_(new RTCStatsCollectorWrapper(pc_)) {} |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 487 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 488 | void ExpectReportContainsCertificateInfo( |
| 489 | const rtc::scoped_refptr<const RTCStatsReport>& report, |
hbos | 23368e1 | 2016-12-21 04:29:17 -0800 | [diff] [blame] | 490 | const CertificateInfo& certinfo) { |
| 491 | for (size_t i = 0; i < certinfo.fingerprints.size(); ++i) { |
| 492 | RTCCertificateStats expected_certificate_stats( |
| 493 | "RTCCertificate_" + certinfo.fingerprints[i], |
| 494 | report->timestamp_us()); |
| 495 | expected_certificate_stats.fingerprint = certinfo.fingerprints[i]; |
| 496 | expected_certificate_stats.fingerprint_algorithm = "sha-1"; |
| 497 | expected_certificate_stats.base64_certificate = certinfo.pems[i]; |
| 498 | if (i + 1 < certinfo.fingerprints.size()) { |
| 499 | expected_certificate_stats.issuer_certificate_id = |
| 500 | "RTCCertificate_" + certinfo.fingerprints[i + 1]; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 501 | } |
hbos | 23368e1 | 2016-12-21 04:29:17 -0800 | [diff] [blame] | 502 | ASSERT_TRUE(report->Get(expected_certificate_stats.id())); |
| 503 | EXPECT_EQ(expected_certificate_stats, |
| 504 | report->Get(expected_certificate_stats.id())->cast_to< |
| 505 | RTCCertificateStats>()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 509 | protected: |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 510 | rtc::ScopedFakeClock fake_clock_; |
| 511 | rtc::scoped_refptr<FakePeerConnectionForStats> pc_; |
| 512 | std::unique_ptr<RTCStatsCollectorWrapper> stats_; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | TEST_F(RTCStatsCollectorTest, SingleCallback) { |
| 516 | rtc::scoped_refptr<const RTCStatsReport> result; |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 517 | stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&result)); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 518 | EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs); |
| 519 | } |
| 520 | |
| 521 | TEST_F(RTCStatsCollectorTest, MultipleCallbacks) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 522 | rtc::scoped_refptr<const RTCStatsReport> a, b, c; |
| 523 | stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&a)); |
| 524 | stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&b)); |
| 525 | stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&c)); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 526 | EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); |
| 527 | EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); |
| 528 | EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 529 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 530 | EXPECT_EQ(a.get(), b.get()); |
| 531 | EXPECT_EQ(b.get(), c.get()); |
| 532 | } |
| 533 | |
| 534 | TEST_F(RTCStatsCollectorTest, CachedStatsReports) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 535 | // Caching should ensure |a| and |b| are the same report. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 536 | rtc::scoped_refptr<const RTCStatsReport> a = stats_->GetStatsReport(); |
| 537 | rtc::scoped_refptr<const RTCStatsReport> b = stats_->GetStatsReport(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 538 | EXPECT_EQ(a.get(), b.get()); |
| 539 | // Invalidate cache by clearing it. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 540 | stats_->stats_collector()->ClearCachedStatsReport(); |
| 541 | rtc::scoped_refptr<const RTCStatsReport> c = stats_->GetStatsReport(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 542 | EXPECT_NE(b.get(), c.get()); |
| 543 | // Invalidate cache by advancing time. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 544 | fake_clock_.AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); |
| 545 | rtc::scoped_refptr<const RTCStatsReport> d = stats_->GetStatsReport(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 546 | EXPECT_TRUE(d); |
| 547 | EXPECT_NE(c.get(), d.get()); |
| 548 | } |
| 549 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 550 | TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 551 | rtc::scoped_refptr<const RTCStatsReport> a, b, c; |
| 552 | stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&a)); |
| 553 | stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&b)); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 554 | // Cache is invalidated after 50 ms. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 555 | fake_clock_.AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); |
| 556 | stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&c)); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 557 | EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); |
| 558 | EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); |
| 559 | EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); |
| 560 | EXPECT_EQ(a.get(), b.get()); |
| 561 | // The act of doing |AdvanceTime| processes all messages. If this was not the |
| 562 | // case we might not require |c| to be fresher than |b|. |
| 563 | EXPECT_NE(c.get(), b.get()); |
| 564 | } |
| 565 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 566 | TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 567 | const char kTransportName[] = "transport"; |
| 568 | |
| 569 | pc_->AddVoiceChannel("audio", kTransportName); |
| 570 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 571 | std::unique_ptr<CertificateInfo> local_certinfo = |
| 572 | CreateFakeCertificateAndInfoFromDers( |
| 573 | std::vector<std::string>({ "(local) single certificate" })); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 574 | pc_->SetLocalCertificate(kTransportName, local_certinfo->certificate); |
| 575 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 576 | std::unique_ptr<CertificateInfo> remote_certinfo = |
| 577 | CreateFakeCertificateAndInfoFromDers( |
| 578 | std::vector<std::string>({ "(remote) single certificate" })); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 579 | pc_->SetRemoteCertificate( |
| 580 | kTransportName, |
| 581 | remote_certinfo->certificate->ssl_certificate().GetUniqueReference()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 582 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 583 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 584 | |
hbos | 23368e1 | 2016-12-21 04:29:17 -0800 | [diff] [blame] | 585 | ExpectReportContainsCertificateInfo(report, *local_certinfo); |
| 586 | ExpectReportContainsCertificateInfo(report, *remote_certinfo); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 587 | } |
| 588 | |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 589 | TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) { |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 590 | // Audio |
| 591 | cricket::VoiceMediaInfo voice_media_info; |
| 592 | |
| 593 | RtpCodecParameters inbound_audio_codec; |
| 594 | inbound_audio_codec.payload_type = 1; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 595 | inbound_audio_codec.kind = cricket::MEDIA_TYPE_AUDIO; |
| 596 | inbound_audio_codec.name = "opus"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 597 | inbound_audio_codec.clock_rate = 1337; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 598 | voice_media_info.receive_codecs.insert( |
| 599 | std::make_pair(inbound_audio_codec.payload_type, inbound_audio_codec)); |
| 600 | |
| 601 | RtpCodecParameters outbound_audio_codec; |
| 602 | outbound_audio_codec.payload_type = 2; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 603 | outbound_audio_codec.kind = cricket::MEDIA_TYPE_AUDIO; |
| 604 | outbound_audio_codec.name = "isac"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 605 | outbound_audio_codec.clock_rate = 1338; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 606 | voice_media_info.send_codecs.insert( |
| 607 | std::make_pair(outbound_audio_codec.payload_type, outbound_audio_codec)); |
| 608 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 609 | auto* voice_media_channel = pc_->AddVoiceChannel("audio", "TransportName"); |
| 610 | voice_media_channel->SetStats(voice_media_info); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 611 | |
| 612 | // Video |
| 613 | cricket::VideoMediaInfo video_media_info; |
| 614 | |
| 615 | RtpCodecParameters inbound_video_codec; |
| 616 | inbound_video_codec.payload_type = 3; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 617 | inbound_video_codec.kind = cricket::MEDIA_TYPE_VIDEO; |
| 618 | inbound_video_codec.name = "H264"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 619 | inbound_video_codec.clock_rate = 1339; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 620 | video_media_info.receive_codecs.insert( |
| 621 | std::make_pair(inbound_video_codec.payload_type, inbound_video_codec)); |
| 622 | |
| 623 | RtpCodecParameters outbound_video_codec; |
| 624 | outbound_video_codec.payload_type = 4; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 625 | outbound_video_codec.kind = cricket::MEDIA_TYPE_VIDEO; |
| 626 | outbound_video_codec.name = "VP8"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 627 | outbound_video_codec.clock_rate = 1340; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 628 | video_media_info.send_codecs.insert( |
| 629 | std::make_pair(outbound_video_codec.payload_type, outbound_video_codec)); |
| 630 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 631 | auto* video_media_channel = pc_->AddVideoChannel("video", "TransportName"); |
| 632 | video_media_channel->SetStats(video_media_info); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 633 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 634 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 635 | |
| 636 | RTCCodecStats expected_inbound_audio_codec( |
| 637 | "RTCCodec_InboundAudio_1", report->timestamp_us()); |
| 638 | expected_inbound_audio_codec.payload_type = 1; |
hbos | 13f54b2 | 2017-02-28 06:56:04 -0800 | [diff] [blame] | 639 | expected_inbound_audio_codec.mime_type = "audio/opus"; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 640 | expected_inbound_audio_codec.clock_rate = 1337; |
| 641 | |
| 642 | RTCCodecStats expected_outbound_audio_codec( |
| 643 | "RTCCodec_OutboundAudio_2", report->timestamp_us()); |
| 644 | expected_outbound_audio_codec.payload_type = 2; |
hbos | 13f54b2 | 2017-02-28 06:56:04 -0800 | [diff] [blame] | 645 | expected_outbound_audio_codec.mime_type = "audio/isac"; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 646 | expected_outbound_audio_codec.clock_rate = 1338; |
| 647 | |
| 648 | RTCCodecStats expected_inbound_video_codec( |
| 649 | "RTCCodec_InboundVideo_3", report->timestamp_us()); |
| 650 | expected_inbound_video_codec.payload_type = 3; |
hbos | 13f54b2 | 2017-02-28 06:56:04 -0800 | [diff] [blame] | 651 | expected_inbound_video_codec.mime_type = "video/H264"; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 652 | expected_inbound_video_codec.clock_rate = 1339; |
| 653 | |
| 654 | RTCCodecStats expected_outbound_video_codec( |
| 655 | "RTCCodec_OutboundVideo_4", report->timestamp_us()); |
| 656 | expected_outbound_video_codec.payload_type = 4; |
hbos | 13f54b2 | 2017-02-28 06:56:04 -0800 | [diff] [blame] | 657 | expected_outbound_video_codec.mime_type = "video/VP8"; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 658 | expected_outbound_video_codec.clock_rate = 1340; |
| 659 | |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 660 | ASSERT_TRUE(report->Get(expected_inbound_audio_codec.id())); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 661 | EXPECT_EQ(expected_inbound_audio_codec, |
| 662 | report->Get(expected_inbound_audio_codec.id())->cast_to< |
| 663 | RTCCodecStats>()); |
| 664 | |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 665 | ASSERT_TRUE(report->Get(expected_outbound_audio_codec.id())); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 666 | EXPECT_EQ(expected_outbound_audio_codec, |
| 667 | report->Get(expected_outbound_audio_codec.id())->cast_to< |
| 668 | RTCCodecStats>()); |
| 669 | |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 670 | ASSERT_TRUE(report->Get(expected_inbound_video_codec.id())); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 671 | EXPECT_EQ(expected_inbound_video_codec, |
| 672 | report->Get(expected_inbound_video_codec.id())->cast_to< |
| 673 | RTCCodecStats>()); |
| 674 | |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 675 | ASSERT_TRUE(report->Get(expected_outbound_video_codec.id())); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 676 | EXPECT_EQ(expected_outbound_video_codec, |
| 677 | report->Get(expected_outbound_video_codec.id())->cast_to< |
| 678 | RTCCodecStats>()); |
| 679 | } |
| 680 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 681 | TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 682 | const char kAudioTransport[] = "audio"; |
| 683 | const char kVideoTransport[] = "video"; |
| 684 | |
| 685 | pc_->AddVoiceChannel("audio", kAudioTransport); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 686 | std::unique_ptr<CertificateInfo> audio_local_certinfo = |
| 687 | CreateFakeCertificateAndInfoFromDers( |
| 688 | std::vector<std::string>({ "(local) audio" })); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 689 | pc_->SetLocalCertificate(kAudioTransport, audio_local_certinfo->certificate); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 690 | std::unique_ptr<CertificateInfo> audio_remote_certinfo = |
| 691 | CreateFakeCertificateAndInfoFromDers( |
| 692 | std::vector<std::string>({ "(remote) audio" })); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 693 | pc_->SetRemoteCertificate( |
| 694 | kAudioTransport, audio_remote_certinfo->certificate->ssl_certificate() |
| 695 | .GetUniqueReference()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 696 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 697 | pc_->AddVideoChannel("video", kVideoTransport); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 698 | std::unique_ptr<CertificateInfo> video_local_certinfo = |
| 699 | CreateFakeCertificateAndInfoFromDers( |
| 700 | std::vector<std::string>({ "(local) video" })); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 701 | pc_->SetLocalCertificate(kVideoTransport, video_local_certinfo->certificate); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 702 | std::unique_ptr<CertificateInfo> video_remote_certinfo = |
| 703 | CreateFakeCertificateAndInfoFromDers( |
| 704 | std::vector<std::string>({ "(remote) video" })); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 705 | pc_->SetRemoteCertificate( |
| 706 | kVideoTransport, video_remote_certinfo->certificate->ssl_certificate() |
| 707 | .GetUniqueReference()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 708 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 709 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 23368e1 | 2016-12-21 04:29:17 -0800 | [diff] [blame] | 710 | ExpectReportContainsCertificateInfo(report, *audio_local_certinfo); |
| 711 | ExpectReportContainsCertificateInfo(report, *audio_remote_certinfo); |
| 712 | ExpectReportContainsCertificateInfo(report, *video_local_certinfo); |
| 713 | ExpectReportContainsCertificateInfo(report, *video_remote_certinfo); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsChain) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 717 | const char kTransportName[] = "transport"; |
| 718 | |
| 719 | pc_->AddVoiceChannel("audio", kTransportName); |
| 720 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 721 | std::unique_ptr<CertificateInfo> local_certinfo = |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 722 | CreateFakeCertificateAndInfoFromDers( |
| 723 | {"(local) this", "(local) is", "(local) a", "(local) chain"}); |
| 724 | pc_->SetLocalCertificate(kTransportName, local_certinfo->certificate); |
| 725 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 726 | std::unique_ptr<CertificateInfo> remote_certinfo = |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 727 | CreateFakeCertificateAndInfoFromDers({"(remote) this", "(remote) is", |
| 728 | "(remote) another", |
| 729 | "(remote) chain"}); |
| 730 | pc_->SetRemoteCertificate( |
| 731 | kTransportName, |
| 732 | remote_certinfo->certificate->ssl_certificate().GetUniqueReference()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 733 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 734 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 23368e1 | 2016-12-21 04:29:17 -0800 | [diff] [blame] | 735 | ExpectReportContainsCertificateInfo(report, *local_certinfo); |
| 736 | ExpectReportContainsCertificateInfo(report, *remote_certinfo); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 737 | } |
| 738 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 739 | TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 740 | pc_->AddSctpDataChannel(new MockDataChannel(0, "MockDataChannel0", |
| 741 | DataChannelInterface::kConnecting, |
| 742 | "udp", 1, 2, 3, 4)); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 743 | RTCDataChannelStats expected_data_channel0("RTCDataChannel_0", 0); |
| 744 | expected_data_channel0.label = "MockDataChannel0"; |
| 745 | expected_data_channel0.protocol = "udp"; |
| 746 | expected_data_channel0.datachannelid = 0; |
| 747 | expected_data_channel0.state = "connecting"; |
| 748 | expected_data_channel0.messages_sent = 1; |
| 749 | expected_data_channel0.bytes_sent = 2; |
| 750 | expected_data_channel0.messages_received = 3; |
| 751 | expected_data_channel0.bytes_received = 4; |
| 752 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 753 | pc_->AddSctpDataChannel(new MockDataChannel( |
Steve Anton | be5e208 | 2018-01-24 15:29:17 -0800 | [diff] [blame] | 754 | 1, "MockDataChannel1", DataChannelInterface::kOpen, "tcp", 5, 6, 7, 8)); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 755 | RTCDataChannelStats expected_data_channel1("RTCDataChannel_1", 0); |
| 756 | expected_data_channel1.label = "MockDataChannel1"; |
| 757 | expected_data_channel1.protocol = "tcp"; |
| 758 | expected_data_channel1.datachannelid = 1; |
| 759 | expected_data_channel1.state = "open"; |
| 760 | expected_data_channel1.messages_sent = 5; |
| 761 | expected_data_channel1.bytes_sent = 6; |
| 762 | expected_data_channel1.messages_received = 7; |
| 763 | expected_data_channel1.bytes_received = 8; |
| 764 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 765 | pc_->AddSctpDataChannel(new MockDataChannel(2, "MockDataChannel2", |
| 766 | DataChannelInterface::kClosing, |
| 767 | "udp", 9, 10, 11, 12)); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 768 | RTCDataChannelStats expected_data_channel2("RTCDataChannel_2", 0); |
| 769 | expected_data_channel2.label = "MockDataChannel2"; |
| 770 | expected_data_channel2.protocol = "udp"; |
| 771 | expected_data_channel2.datachannelid = 2; |
| 772 | expected_data_channel2.state = "closing"; |
| 773 | expected_data_channel2.messages_sent = 9; |
| 774 | expected_data_channel2.bytes_sent = 10; |
| 775 | expected_data_channel2.messages_received = 11; |
| 776 | expected_data_channel2.bytes_received = 12; |
| 777 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 778 | pc_->AddSctpDataChannel(new MockDataChannel(3, "MockDataChannel3", |
| 779 | DataChannelInterface::kClosed, |
| 780 | "tcp", 13, 14, 15, 16)); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 781 | RTCDataChannelStats expected_data_channel3("RTCDataChannel_3", 0); |
| 782 | expected_data_channel3.label = "MockDataChannel3"; |
| 783 | expected_data_channel3.protocol = "tcp"; |
| 784 | expected_data_channel3.datachannelid = 3; |
| 785 | expected_data_channel3.state = "closed"; |
| 786 | expected_data_channel3.messages_sent = 13; |
| 787 | expected_data_channel3.bytes_sent = 14; |
| 788 | expected_data_channel3.messages_received = 15; |
| 789 | expected_data_channel3.bytes_received = 16; |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 790 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 791 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
| 792 | |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 793 | ASSERT_TRUE(report->Get(expected_data_channel0.id())); |
| 794 | EXPECT_EQ(expected_data_channel0, |
| 795 | report->Get(expected_data_channel0.id())->cast_to< |
| 796 | RTCDataChannelStats>()); |
| 797 | ASSERT_TRUE(report->Get(expected_data_channel1.id())); |
| 798 | EXPECT_EQ(expected_data_channel1, |
| 799 | report->Get(expected_data_channel1.id())->cast_to< |
| 800 | RTCDataChannelStats>()); |
| 801 | ASSERT_TRUE(report->Get(expected_data_channel2.id())); |
| 802 | EXPECT_EQ(expected_data_channel2, |
| 803 | report->Get(expected_data_channel2.id())->cast_to< |
| 804 | RTCDataChannelStats>()); |
| 805 | ASSERT_TRUE(report->Get(expected_data_channel3.id())); |
| 806 | EXPECT_EQ(expected_data_channel3, |
| 807 | report->Get(expected_data_channel3.id())->cast_to< |
| 808 | RTCDataChannelStats>()); |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 809 | } |
| 810 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 811 | TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { |
| 812 | // Candidates in the first transport stats. |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 813 | std::unique_ptr<cricket::Candidate> a_local_host = |
| 814 | CreateFakeCandidate("1.2.3.4", 5, "a_local_host's protocol", |
| 815 | rtc::ADAPTER_TYPE_VPN, cricket::LOCAL_PORT_TYPE, 0); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 816 | RTCLocalIceCandidateStats expected_a_local_host( |
| 817 | "RTCIceCandidate_" + a_local_host->id(), 0); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 818 | expected_a_local_host.transport_id = "RTCTransport_a_0"; |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 819 | expected_a_local_host.network_type = "vpn"; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 820 | expected_a_local_host.ip = "1.2.3.4"; |
| 821 | expected_a_local_host.port = 5; |
| 822 | expected_a_local_host.protocol = "a_local_host's protocol"; |
| 823 | expected_a_local_host.candidate_type = "host"; |
| 824 | expected_a_local_host.priority = 0; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 825 | EXPECT_FALSE(*expected_a_local_host.is_remote); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 826 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 827 | std::unique_ptr<cricket::Candidate> a_remote_srflx = CreateFakeCandidate( |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 828 | "6.7.8.9", 10, "remote_srflx's protocol", rtc::ADAPTER_TYPE_UNKNOWN, |
| 829 | cricket::STUN_PORT_TYPE, 1); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 830 | RTCRemoteIceCandidateStats expected_a_remote_srflx( |
| 831 | "RTCIceCandidate_" + a_remote_srflx->id(), 0); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 832 | expected_a_remote_srflx.transport_id = "RTCTransport_a_0"; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 833 | expected_a_remote_srflx.ip = "6.7.8.9"; |
| 834 | expected_a_remote_srflx.port = 10; |
| 835 | expected_a_remote_srflx.protocol = "remote_srflx's protocol"; |
| 836 | expected_a_remote_srflx.candidate_type = "srflx"; |
| 837 | expected_a_remote_srflx.priority = 1; |
hbos | d17a5a7 | 2017-01-02 08:09:59 -0800 | [diff] [blame] | 838 | expected_a_remote_srflx.deleted = false; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 839 | EXPECT_TRUE(*expected_a_remote_srflx.is_remote); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 840 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 841 | std::unique_ptr<cricket::Candidate> a_local_prflx = CreateFakeCandidate( |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 842 | "11.12.13.14", 15, "a_local_prflx's protocol", rtc::ADAPTER_TYPE_CELLULAR, |
| 843 | cricket::PRFLX_PORT_TYPE, 2); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 844 | RTCLocalIceCandidateStats expected_a_local_prflx( |
| 845 | "RTCIceCandidate_" + a_local_prflx->id(), 0); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 846 | expected_a_local_prflx.transport_id = "RTCTransport_a_0"; |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 847 | expected_a_local_prflx.network_type = "cellular"; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 848 | expected_a_local_prflx.ip = "11.12.13.14"; |
| 849 | expected_a_local_prflx.port = 15; |
| 850 | expected_a_local_prflx.protocol = "a_local_prflx's protocol"; |
| 851 | expected_a_local_prflx.candidate_type = "prflx"; |
| 852 | expected_a_local_prflx.priority = 2; |
hbos | d17a5a7 | 2017-01-02 08:09:59 -0800 | [diff] [blame] | 853 | expected_a_local_prflx.deleted = false; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 854 | EXPECT_FALSE(*expected_a_local_prflx.is_remote); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 855 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 856 | std::unique_ptr<cricket::Candidate> a_remote_relay = CreateFakeCandidate( |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 857 | "16.17.18.19", 20, "a_remote_relay's protocol", rtc::ADAPTER_TYPE_UNKNOWN, |
| 858 | cricket::RELAY_PORT_TYPE, 3); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 859 | RTCRemoteIceCandidateStats expected_a_remote_relay( |
| 860 | "RTCIceCandidate_" + a_remote_relay->id(), 0); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 861 | expected_a_remote_relay.transport_id = "RTCTransport_a_0"; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 862 | expected_a_remote_relay.ip = "16.17.18.19"; |
| 863 | expected_a_remote_relay.port = 20; |
| 864 | expected_a_remote_relay.protocol = "a_remote_relay's protocol"; |
| 865 | expected_a_remote_relay.candidate_type = "relay"; |
| 866 | expected_a_remote_relay.priority = 3; |
hbos | d17a5a7 | 2017-01-02 08:09:59 -0800 | [diff] [blame] | 867 | expected_a_remote_relay.deleted = false; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 868 | EXPECT_TRUE(*expected_a_remote_relay.is_remote); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 869 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 870 | // Candidates in the second transport stats. |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 871 | std::unique_ptr<cricket::Candidate> b_local = |
| 872 | CreateFakeCandidate("42.42.42.42", 42, "b_local's protocol", |
| 873 | rtc::ADAPTER_TYPE_WIFI, cricket::LOCAL_PORT_TYPE, 42); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 874 | RTCLocalIceCandidateStats expected_b_local( |
| 875 | "RTCIceCandidate_" + b_local->id(), 0); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 876 | expected_b_local.transport_id = "RTCTransport_b_0"; |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 877 | expected_b_local.network_type = "wifi"; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 878 | expected_b_local.ip = "42.42.42.42"; |
| 879 | expected_b_local.port = 42; |
| 880 | expected_b_local.protocol = "b_local's protocol"; |
| 881 | expected_b_local.candidate_type = "host"; |
| 882 | expected_b_local.priority = 42; |
hbos | d17a5a7 | 2017-01-02 08:09:59 -0800 | [diff] [blame] | 883 | expected_b_local.deleted = false; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 884 | EXPECT_FALSE(*expected_b_local.is_remote); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 885 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 886 | std::unique_ptr<cricket::Candidate> b_remote = CreateFakeCandidate( |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 887 | "42.42.42.42", 42, "b_remote's protocol", rtc::ADAPTER_TYPE_UNKNOWN, |
| 888 | cricket::LOCAL_PORT_TYPE, 42); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 889 | RTCRemoteIceCandidateStats expected_b_remote( |
| 890 | "RTCIceCandidate_" + b_remote->id(), 0); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 891 | expected_b_remote.transport_id = "RTCTransport_b_0"; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 892 | expected_b_remote.ip = "42.42.42.42"; |
| 893 | expected_b_remote.port = 42; |
| 894 | expected_b_remote.protocol = "b_remote's protocol"; |
| 895 | expected_b_remote.candidate_type = "host"; |
| 896 | expected_b_remote.priority = 42; |
hbos | d17a5a7 | 2017-01-02 08:09:59 -0800 | [diff] [blame] | 897 | expected_b_remote.deleted = false; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 898 | EXPECT_TRUE(*expected_b_remote.is_remote); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 899 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 900 | cricket::TransportChannelStats a_transport_channel_stats; |
| 901 | a_transport_channel_stats.connection_infos.push_back( |
| 902 | cricket::ConnectionInfo()); |
| 903 | a_transport_channel_stats.connection_infos[0].local_candidate = |
| 904 | *a_local_host.get(); |
| 905 | a_transport_channel_stats.connection_infos[0].remote_candidate = |
| 906 | *a_remote_srflx.get(); |
| 907 | a_transport_channel_stats.connection_infos.push_back( |
| 908 | cricket::ConnectionInfo()); |
| 909 | a_transport_channel_stats.connection_infos[1].local_candidate = |
| 910 | *a_local_prflx.get(); |
| 911 | a_transport_channel_stats.connection_infos[1].remote_candidate = |
| 912 | *a_remote_relay.get(); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 913 | |
| 914 | pc_->AddVoiceChannel("audio", "a"); |
| 915 | pc_->SetTransportStats("a", a_transport_channel_stats); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 916 | |
| 917 | cricket::TransportChannelStats b_transport_channel_stats; |
| 918 | b_transport_channel_stats.connection_infos.push_back( |
| 919 | cricket::ConnectionInfo()); |
| 920 | b_transport_channel_stats.connection_infos[0].local_candidate = |
| 921 | *b_local.get(); |
| 922 | b_transport_channel_stats.connection_infos[0].remote_candidate = |
| 923 | *b_remote.get(); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 924 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 925 | pc_->AddVideoChannel("video", "b"); |
| 926 | pc_->SetTransportStats("b", b_transport_channel_stats); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 927 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 928 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 929 | |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 930 | ASSERT_TRUE(report->Get(expected_a_local_host.id())); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 931 | EXPECT_EQ(expected_a_local_host, |
| 932 | report->Get(expected_a_local_host.id())->cast_to< |
| 933 | RTCLocalIceCandidateStats>()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 934 | ASSERT_TRUE(report->Get(expected_a_remote_srflx.id())); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 935 | EXPECT_EQ(expected_a_remote_srflx, |
| 936 | report->Get(expected_a_remote_srflx.id())->cast_to< |
| 937 | RTCRemoteIceCandidateStats>()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 938 | ASSERT_TRUE(report->Get(expected_a_local_prflx.id())); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 939 | EXPECT_EQ(expected_a_local_prflx, |
| 940 | report->Get(expected_a_local_prflx.id())->cast_to< |
| 941 | RTCLocalIceCandidateStats>()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 942 | ASSERT_TRUE(report->Get(expected_a_remote_relay.id())); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 943 | EXPECT_EQ(expected_a_remote_relay, |
| 944 | report->Get(expected_a_remote_relay.id())->cast_to< |
| 945 | RTCRemoteIceCandidateStats>()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 946 | ASSERT_TRUE(report->Get(expected_b_local.id())); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 947 | EXPECT_EQ(expected_b_local, |
| 948 | report->Get(expected_b_local.id())->cast_to< |
| 949 | RTCLocalIceCandidateStats>()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 950 | ASSERT_TRUE(report->Get(expected_b_remote.id())); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 951 | EXPECT_EQ(expected_b_remote, |
| 952 | report->Get(expected_b_remote.id())->cast_to< |
| 953 | RTCRemoteIceCandidateStats>()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 954 | EXPECT_TRUE(report->Get("RTCTransport_a_0")); |
| 955 | EXPECT_TRUE(report->Get("RTCTransport_b_0")); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 956 | } |
| 957 | |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 958 | TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 959 | const char kTransportName[] = "transport"; |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 960 | |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 961 | std::unique_ptr<cricket::Candidate> local_candidate = |
| 962 | CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI, |
| 963 | cricket::LOCAL_PORT_TYPE, 42); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 964 | std::unique_ptr<cricket::Candidate> remote_candidate = CreateFakeCandidate( |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 965 | "42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_UNKNOWN, |
| 966 | cricket::LOCAL_PORT_TYPE, 42); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 967 | |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 968 | cricket::ConnectionInfo connection_info; |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 969 | connection_info.best_connection = false; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 970 | connection_info.local_candidate = *local_candidate.get(); |
| 971 | connection_info.remote_candidate = *remote_candidate.get(); |
| 972 | connection_info.writable = true; |
| 973 | connection_info.sent_total_bytes = 42; |
| 974 | connection_info.recv_total_bytes = 1234; |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 975 | connection_info.total_round_trip_time_ms = 0; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 976 | connection_info.current_round_trip_time_ms = rtc::nullopt; |
hbos | d82f512 | 2016-12-09 04:12:39 -0800 | [diff] [blame] | 977 | connection_info.recv_ping_requests = 2020; |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 978 | connection_info.sent_ping_requests_total = 2020; |
| 979 | connection_info.sent_ping_requests_before_first_response = 2000; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 980 | connection_info.recv_ping_responses = 4321; |
| 981 | connection_info.sent_ping_responses = 1000; |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 982 | connection_info.state = cricket::IceCandidatePairState::IN_PROGRESS; |
| 983 | connection_info.priority = 5555; |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 984 | connection_info.nominated = false; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 985 | |
| 986 | cricket::TransportChannelStats transport_channel_stats; |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 987 | transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 988 | transport_channel_stats.connection_infos.push_back(connection_info); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 989 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 990 | pc_->AddVideoChannel("video", kTransportName); |
| 991 | pc_->SetTransportStats(kTransportName, transport_channel_stats); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 992 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 993 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 994 | |
| 995 | RTCIceCandidatePairStats expected_pair("RTCIceCandidatePair_" + |
| 996 | local_candidate->id() + "_" + |
| 997 | remote_candidate->id(), |
| 998 | report->timestamp_us()); |
| 999 | expected_pair.transport_id = |
| 1000 | "RTCTransport_transport_" + |
| 1001 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1002 | expected_pair.local_candidate_id = "RTCIceCandidate_" + local_candidate->id(); |
| 1003 | expected_pair.remote_candidate_id = |
| 1004 | "RTCIceCandidate_" + remote_candidate->id(); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1005 | expected_pair.state = RTCStatsIceCandidatePairState::kInProgress; |
| 1006 | expected_pair.priority = 5555; |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1007 | expected_pair.nominated = false; |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1008 | expected_pair.writable = true; |
| 1009 | expected_pair.bytes_sent = 42; |
| 1010 | expected_pair.bytes_received = 1234; |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1011 | expected_pair.total_round_trip_time = 0.0; |
hbos | d82f512 | 2016-12-09 04:12:39 -0800 | [diff] [blame] | 1012 | expected_pair.requests_received = 2020; |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 1013 | expected_pair.requests_sent = 2000; |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1014 | expected_pair.responses_received = 4321; |
| 1015 | expected_pair.responses_sent = 1000; |
hbos | e448dd5 | 2016-12-12 01:22:53 -0800 | [diff] [blame] | 1016 | expected_pair.consent_requests_sent = (2020 - 2000); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1017 | // |expected_pair.current_round_trip_time| should be undefined because the |
| 1018 | // current RTT is not set. |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1019 | // |expected_pair.available_[outgoing/incoming]_bitrate| should be undefined |
| 1020 | // because is is not the current pair. |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1021 | |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1022 | ASSERT_TRUE(report->Get(expected_pair.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1023 | EXPECT_EQ( |
| 1024 | expected_pair, |
| 1025 | report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 1026 | EXPECT_TRUE(report->Get(*expected_pair.transport_id)); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1027 | |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1028 | // Set nominated and "GetStats" again. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1029 | transport_channel_stats.connection_infos[0].nominated = true; |
| 1030 | pc_->SetTransportStats(kTransportName, transport_channel_stats); |
| 1031 | report = stats_->GetFreshStatsReport(); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1032 | expected_pair.nominated = true; |
| 1033 | ASSERT_TRUE(report->Get(expected_pair.id())); |
| 1034 | EXPECT_EQ( |
| 1035 | expected_pair, |
| 1036 | report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>()); |
| 1037 | EXPECT_TRUE(report->Get(*expected_pair.transport_id)); |
| 1038 | |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1039 | // Set round trip times and "GetStats" again. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1040 | transport_channel_stats.connection_infos[0].total_round_trip_time_ms = 7331; |
| 1041 | transport_channel_stats.connection_infos[0].current_round_trip_time_ms = 1337; |
| 1042 | pc_->SetTransportStats(kTransportName, transport_channel_stats); |
| 1043 | report = stats_->GetFreshStatsReport(); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1044 | expected_pair.total_round_trip_time = 7.331; |
| 1045 | expected_pair.current_round_trip_time = 1.337; |
| 1046 | ASSERT_TRUE(report->Get(expected_pair.id())); |
| 1047 | EXPECT_EQ( |
| 1048 | expected_pair, |
| 1049 | report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>()); |
| 1050 | EXPECT_TRUE(report->Get(*expected_pair.transport_id)); |
| 1051 | |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1052 | // Make pair the current pair, clear bandwidth and "GetStats" again. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1053 | transport_channel_stats.connection_infos[0].best_connection = true; |
| 1054 | pc_->SetTransportStats(kTransportName, transport_channel_stats); |
| 1055 | report = stats_->GetFreshStatsReport(); |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1056 | // |expected_pair.available_[outgoing/incoming]_bitrate| should still be |
| 1057 | // undefined because bandwidth is not set. |
| 1058 | ASSERT_TRUE(report->Get(expected_pair.id())); |
| 1059 | EXPECT_EQ( |
| 1060 | expected_pair, |
| 1061 | report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>()); |
| 1062 | EXPECT_TRUE(report->Get(*expected_pair.transport_id)); |
| 1063 | |
| 1064 | // Set bandwidth and "GetStats" again. |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1065 | webrtc::Call::Stats call_stats; |
| 1066 | const int kSendBandwidth = 888; |
| 1067 | call_stats.send_bandwidth_bps = kSendBandwidth; |
| 1068 | const int kRecvBandwidth = 999; |
| 1069 | call_stats.recv_bandwidth_bps = kRecvBandwidth; |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1070 | pc_->SetCallStats(call_stats); |
| 1071 | report = stats_->GetFreshStatsReport(); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1072 | expected_pair.available_outgoing_bitrate = kSendBandwidth; |
| 1073 | expected_pair.available_incoming_bitrate = kRecvBandwidth; |
hbos | 338f78a | 2017-02-07 06:41:21 -0800 | [diff] [blame] | 1074 | ASSERT_TRUE(report->Get(expected_pair.id())); |
| 1075 | EXPECT_EQ( |
| 1076 | expected_pair, |
| 1077 | report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>()); |
| 1078 | EXPECT_TRUE(report->Get(*expected_pair.transport_id)); |
| 1079 | |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 1080 | RTCLocalIceCandidateStats expected_local_candidate( |
| 1081 | *expected_pair.local_candidate_id, report->timestamp_us()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 1082 | expected_local_candidate.transport_id = *expected_pair.transport_id; |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 1083 | expected_local_candidate.network_type = "wifi"; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 1084 | expected_local_candidate.ip = "42.42.42.42"; |
| 1085 | expected_local_candidate.port = 42; |
| 1086 | expected_local_candidate.protocol = "protocol"; |
| 1087 | expected_local_candidate.candidate_type = "host"; |
| 1088 | expected_local_candidate.priority = 42; |
hbos | d17a5a7 | 2017-01-02 08:09:59 -0800 | [diff] [blame] | 1089 | expected_local_candidate.deleted = false; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 1090 | EXPECT_FALSE(*expected_local_candidate.is_remote); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 1091 | ASSERT_TRUE(report->Get(expected_local_candidate.id())); |
| 1092 | EXPECT_EQ(expected_local_candidate, |
| 1093 | report->Get(expected_local_candidate.id())->cast_to< |
| 1094 | RTCLocalIceCandidateStats>()); |
| 1095 | |
| 1096 | RTCRemoteIceCandidateStats expected_remote_candidate( |
| 1097 | *expected_pair.remote_candidate_id, report->timestamp_us()); |
hbos | b4e426e | 2017-01-02 09:59:31 -0800 | [diff] [blame] | 1098 | expected_remote_candidate.transport_id = *expected_pair.transport_id; |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 1099 | expected_remote_candidate.ip = "42.42.42.42"; |
| 1100 | expected_remote_candidate.port = 42; |
| 1101 | expected_remote_candidate.protocol = "protocol"; |
| 1102 | expected_remote_candidate.candidate_type = "host"; |
| 1103 | expected_remote_candidate.priority = 42; |
hbos | d17a5a7 | 2017-01-02 08:09:59 -0800 | [diff] [blame] | 1104 | expected_remote_candidate.deleted = false; |
hbos | c3a2b7f | 2017-01-02 04:46:15 -0800 | [diff] [blame] | 1105 | EXPECT_TRUE(*expected_remote_candidate.is_remote); |
hbos | c42ba32 | 2016-12-21 03:31:45 -0800 | [diff] [blame] | 1106 | ASSERT_TRUE(report->Get(expected_remote_candidate.id())); |
| 1107 | EXPECT_EQ(expected_remote_candidate, |
| 1108 | report->Get(expected_remote_candidate.id())->cast_to< |
| 1109 | RTCRemoteIceCandidateStats>()); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1112 | TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1113 | { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1114 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1115 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1116 | report->timestamp_us()); |
| 1117 | expected.data_channels_opened = 0; |
| 1118 | expected.data_channels_closed = 0; |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1119 | ASSERT_TRUE(report->Get("RTCPeerConnection")); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1120 | EXPECT_EQ(expected, |
| 1121 | report->Get("RTCPeerConnection")->cast_to< |
| 1122 | RTCPeerConnectionStats>()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1125 | rtc::scoped_refptr<DataChannel> dummy_channel_a = DataChannel::Create( |
| 1126 | nullptr, cricket::DCT_NONE, "DummyChannelA", InternalDataChannelInit()); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1127 | pc_->SignalDataChannelCreated()(dummy_channel_a.get()); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1128 | rtc::scoped_refptr<DataChannel> dummy_channel_b = DataChannel::Create( |
| 1129 | nullptr, cricket::DCT_NONE, "DummyChannelB", InternalDataChannelInit()); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1130 | pc_->SignalDataChannelCreated()(dummy_channel_b.get()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1131 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1132 | dummy_channel_a->SignalOpened(dummy_channel_a.get()); |
| 1133 | // Closing a channel that is not opened should not affect the counts. |
| 1134 | dummy_channel_b->SignalClosed(dummy_channel_b.get()); |
| 1135 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1136 | { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1137 | rtc::scoped_refptr<const RTCStatsReport> report = |
| 1138 | stats_->GetFreshStatsReport(); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1139 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1140 | report->timestamp_us()); |
| 1141 | expected.data_channels_opened = 1; |
| 1142 | expected.data_channels_closed = 0; |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1143 | ASSERT_TRUE(report->Get("RTCPeerConnection")); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1144 | EXPECT_EQ(expected, |
| 1145 | report->Get("RTCPeerConnection")->cast_to< |
| 1146 | RTCPeerConnectionStats>()); |
| 1147 | } |
| 1148 | |
| 1149 | dummy_channel_b->SignalOpened(dummy_channel_b.get()); |
| 1150 | dummy_channel_b->SignalClosed(dummy_channel_b.get()); |
| 1151 | |
| 1152 | { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1153 | rtc::scoped_refptr<const RTCStatsReport> report = |
| 1154 | stats_->GetFreshStatsReport(); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1155 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1156 | report->timestamp_us()); |
| 1157 | expected.data_channels_opened = 2; |
| 1158 | expected.data_channels_closed = 1; |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1159 | ASSERT_TRUE(report->Get("RTCPeerConnection")); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 1160 | EXPECT_EQ(expected, |
| 1161 | report->Get("RTCPeerConnection")->cast_to< |
| 1162 | RTCPeerConnectionStats>()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1163 | } |
hbos | 5bf9def | 2017-03-20 03:14:14 -0700 | [diff] [blame] | 1164 | |
| 1165 | // Re-opening a data channel (or opening a new data channel that is re-using |
| 1166 | // the same address in memory) should increase the opened count. |
| 1167 | dummy_channel_b->SignalOpened(dummy_channel_b.get()); |
| 1168 | |
| 1169 | { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1170 | rtc::scoped_refptr<const RTCStatsReport> report = |
| 1171 | stats_->GetFreshStatsReport(); |
hbos | 5bf9def | 2017-03-20 03:14:14 -0700 | [diff] [blame] | 1172 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1173 | report->timestamp_us()); |
| 1174 | expected.data_channels_opened = 3; |
| 1175 | expected.data_channels_closed = 1; |
| 1176 | ASSERT_TRUE(report->Get("RTCPeerConnection")); |
| 1177 | EXPECT_EQ(expected, |
| 1178 | report->Get("RTCPeerConnection")->cast_to< |
| 1179 | RTCPeerConnectionStats>()); |
| 1180 | } |
| 1181 | |
| 1182 | dummy_channel_a->SignalClosed(dummy_channel_a.get()); |
| 1183 | dummy_channel_b->SignalClosed(dummy_channel_b.get()); |
| 1184 | |
| 1185 | { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1186 | rtc::scoped_refptr<const RTCStatsReport> report = |
| 1187 | stats_->GetFreshStatsReport(); |
hbos | 5bf9def | 2017-03-20 03:14:14 -0700 | [diff] [blame] | 1188 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1189 | report->timestamp_us()); |
| 1190 | expected.data_channels_opened = 3; |
| 1191 | expected.data_channels_closed = 3; |
| 1192 | ASSERT_TRUE(report->Get("RTCPeerConnection")); |
| 1193 | EXPECT_EQ(expected, |
| 1194 | report->Get("RTCPeerConnection")->cast_to< |
| 1195 | RTCPeerConnectionStats>()); |
| 1196 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1199 | TEST_F(RTCStatsCollectorTest, |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1200 | CollectLocalRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio) { |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1201 | rtc::scoped_refptr<MediaStream> local_stream = |
| 1202 | MediaStream::Create("LocalStreamLabel"); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1203 | pc_->mutable_local_streams()->AddStream(local_stream); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1204 | |
| 1205 | // Local audio track |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1206 | rtc::scoped_refptr<MediaStreamTrackInterface> local_audio_track = |
| 1207 | CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "LocalAudioTrackID", |
| 1208 | MediaStreamTrackInterface::kEnded); |
| 1209 | local_stream->AddTrack(static_cast<AudioTrackInterface*>( |
| 1210 | local_audio_track.get())); |
| 1211 | |
| 1212 | cricket::VoiceSenderInfo voice_sender_info_ssrc1; |
| 1213 | voice_sender_info_ssrc1.local_stats.push_back(cricket::SsrcSenderInfo()); |
| 1214 | voice_sender_info_ssrc1.local_stats[0].ssrc = 1; |
| 1215 | voice_sender_info_ssrc1.audio_level = 32767; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 1216 | voice_sender_info_ssrc1.total_input_energy = 0.25; |
| 1217 | voice_sender_info_ssrc1.total_input_duration = 0.5; |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 1218 | voice_sender_info_ssrc1.apm_statistics.echo_return_loss = 42.0; |
| 1219 | voice_sender_info_ssrc1.apm_statistics.echo_return_loss_enhancement = 52.0; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1220 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1221 | stats_->CreateMockRtpSendersReceiversAndChannels( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1222 | {std::make_pair(local_audio_track.get(), voice_sender_info_ssrc1)}, {}, |
| 1223 | {}, {}, {local_stream->label()}, {}); |
| 1224 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1225 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1226 | |
| 1227 | RTCMediaStreamStats expected_local_stream( |
| 1228 | IdForType<RTCMediaStreamStats>(report), report->timestamp_us()); |
| 1229 | expected_local_stream.stream_identifier = local_stream->label(); |
| 1230 | expected_local_stream.track_ids = { |
| 1231 | IdForType<RTCMediaStreamTrackStats>(report)}; |
| 1232 | ASSERT_TRUE(report->Get(expected_local_stream.id())) |
| 1233 | << "Did not find " << expected_local_stream.id() << " in " |
| 1234 | << report->ToJson(); |
| 1235 | EXPECT_EQ( |
| 1236 | expected_local_stream, |
| 1237 | report->Get(expected_local_stream.id())->cast_to<RTCMediaStreamStats>()); |
| 1238 | |
| 1239 | RTCMediaStreamTrackStats expected_local_audio_track_ssrc1( |
| 1240 | IdForType<RTCMediaStreamTrackStats>(report), report->timestamp_us(), |
| 1241 | RTCMediaStreamTrackKind::kAudio); |
| 1242 | expected_local_audio_track_ssrc1.track_identifier = local_audio_track->id(); |
| 1243 | expected_local_audio_track_ssrc1.remote_source = false; |
| 1244 | expected_local_audio_track_ssrc1.ended = true; |
| 1245 | expected_local_audio_track_ssrc1.detached = false; |
| 1246 | expected_local_audio_track_ssrc1.audio_level = 1.0; |
| 1247 | expected_local_audio_track_ssrc1.total_audio_energy = 0.25; |
| 1248 | expected_local_audio_track_ssrc1.total_samples_duration = 0.5; |
| 1249 | expected_local_audio_track_ssrc1.echo_return_loss = 42.0; |
| 1250 | expected_local_audio_track_ssrc1.echo_return_loss_enhancement = 52.0; |
| 1251 | ASSERT_TRUE(report->Get(expected_local_audio_track_ssrc1.id())) |
| 1252 | << "Did not find " << expected_local_audio_track_ssrc1.id() << " in " |
| 1253 | << report->ToJson(); |
| 1254 | EXPECT_EQ(expected_local_audio_track_ssrc1, |
| 1255 | report->Get(expected_local_audio_track_ssrc1.id()) |
| 1256 | ->cast_to<RTCMediaStreamTrackStats>()); |
| 1257 | } |
| 1258 | |
| 1259 | TEST_F(RTCStatsCollectorTest, |
| 1260 | CollectRemoteRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio) { |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1261 | rtc::scoped_refptr<MediaStream> remote_stream = |
| 1262 | MediaStream::Create("RemoteStreamLabel"); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1263 | pc_->mutable_remote_streams()->AddStream(remote_stream); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1264 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1265 | // Remote audio track |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1266 | rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio_track = |
| 1267 | CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "RemoteAudioTrackID", |
| 1268 | MediaStreamTrackInterface::kLive); |
| 1269 | remote_stream->AddTrack(static_cast<AudioTrackInterface*>( |
| 1270 | remote_audio_track.get())); |
| 1271 | |
| 1272 | cricket::VoiceReceiverInfo voice_receiver_info; |
| 1273 | voice_receiver_info.local_stats.push_back(cricket::SsrcReceiverInfo()); |
| 1274 | voice_receiver_info.local_stats[0].ssrc = 3; |
| 1275 | voice_receiver_info.audio_level = 16383; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 1276 | voice_receiver_info.total_output_energy = 0.125; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 1277 | voice_receiver_info.total_samples_received = 4567; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 1278 | voice_receiver_info.total_output_duration = 0.25; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 1279 | voice_receiver_info.concealed_samples = 123; |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 1280 | voice_receiver_info.concealment_events = 12; |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 1281 | voice_receiver_info.jitter_buffer_delay_seconds = 3456; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1282 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1283 | stats_->CreateMockRtpSendersReceiversAndChannels( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1284 | {}, {std::make_pair(remote_audio_track.get(), voice_receiver_info)}, {}, |
| 1285 | {}, {}, {remote_stream}); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1286 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1287 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1288 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1289 | RTCMediaStreamStats expected_remote_stream( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1290 | IdForType<RTCMediaStreamStats>(report), report->timestamp_us()); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1291 | expected_remote_stream.stream_identifier = remote_stream->label(); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1292 | expected_remote_stream.track_ids = |
| 1293 | std::vector<std::string>({IdForType<RTCMediaStreamTrackStats>(report)}); |
| 1294 | ASSERT_TRUE(report->Get(expected_remote_stream.id())) |
| 1295 | << "Did not find " << expected_remote_stream.id() << " in " |
| 1296 | << report->ToJson(); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1297 | EXPECT_EQ(expected_remote_stream, |
| 1298 | report->Get(expected_remote_stream.id())->cast_to< |
| 1299 | RTCMediaStreamStats>()); |
| 1300 | |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1301 | RTCMediaStreamTrackStats expected_remote_audio_track( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1302 | IdForType<RTCMediaStreamTrackStats>(report), report->timestamp_us(), |
| 1303 | RTCMediaStreamTrackKind::kAudio); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1304 | expected_remote_audio_track.track_identifier = remote_audio_track->id(); |
| 1305 | expected_remote_audio_track.remote_source = true; |
| 1306 | expected_remote_audio_track.ended = false; |
| 1307 | expected_remote_audio_track.detached = false; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1308 | expected_remote_audio_track.audio_level = 16383.0 / 32767.0; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 1309 | expected_remote_audio_track.total_audio_energy = 0.125; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 1310 | expected_remote_audio_track.total_samples_received = 4567; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 1311 | expected_remote_audio_track.total_samples_duration = 0.25; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 1312 | expected_remote_audio_track.concealed_samples = 123; |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 1313 | expected_remote_audio_track.concealment_events = 12; |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 1314 | expected_remote_audio_track.jitter_buffer_delay = 3456; |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1315 | ASSERT_TRUE(report->Get(expected_remote_audio_track.id())); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1316 | EXPECT_EQ(expected_remote_audio_track, |
| 1317 | report->Get(expected_remote_audio_track.id())->cast_to< |
| 1318 | RTCMediaStreamTrackStats>()); |
| 1319 | } |
| 1320 | |
| 1321 | TEST_F(RTCStatsCollectorTest, |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1322 | CollectLocalRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Video) { |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1323 | rtc::scoped_refptr<MediaStream> local_stream = |
| 1324 | MediaStream::Create("LocalStreamLabel"); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1325 | pc_->mutable_local_streams()->AddStream(local_stream); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1326 | |
| 1327 | // Local video track |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1328 | rtc::scoped_refptr<MediaStreamTrackInterface> local_video_track = |
| 1329 | CreateFakeTrack(cricket::MEDIA_TYPE_VIDEO, "LocalVideoTrackID", |
| 1330 | MediaStreamTrackInterface::kLive); |
| 1331 | local_stream->AddTrack(static_cast<VideoTrackInterface*>( |
| 1332 | local_video_track.get())); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1333 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1334 | cricket::VideoSenderInfo video_sender_info_ssrc1; |
| 1335 | video_sender_info_ssrc1.local_stats.push_back(cricket::SsrcSenderInfo()); |
| 1336 | video_sender_info_ssrc1.local_stats[0].ssrc = 1; |
| 1337 | video_sender_info_ssrc1.send_frame_width = 1234; |
| 1338 | video_sender_info_ssrc1.send_frame_height = 4321; |
hbos | fefe076 | 2017-01-20 06:14:25 -0800 | [diff] [blame] | 1339 | video_sender_info_ssrc1.frames_encoded = 11; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1340 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1341 | stats_->CreateMockRtpSendersReceiversAndChannels( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1342 | {}, {}, |
| 1343 | {std::make_pair(local_video_track.get(), video_sender_info_ssrc1)}, {}, |
| 1344 | {local_stream->label()}, {}); |
| 1345 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1346 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1347 | |
| 1348 | auto stats_of_my_type = report->GetStatsOfType<RTCMediaStreamStats>(); |
| 1349 | ASSERT_EQ(1, stats_of_my_type.size()) << "No stream in " << report->ToJson(); |
| 1350 | auto stats_of_track_type = report->GetStatsOfType<RTCMediaStreamTrackStats>(); |
| 1351 | ASSERT_EQ(1, stats_of_track_type.size()) |
| 1352 | << "Wrong number of tracks in " << report->ToJson(); |
| 1353 | |
| 1354 | RTCMediaStreamStats expected_local_stream(stats_of_my_type[0]->id(), |
| 1355 | report->timestamp_us()); |
| 1356 | expected_local_stream.stream_identifier = local_stream->label(); |
| 1357 | expected_local_stream.track_ids = |
| 1358 | std::vector<std::string>({stats_of_track_type[0]->id()}); |
| 1359 | ASSERT_TRUE(report->Get(expected_local_stream.id())); |
| 1360 | EXPECT_EQ( |
| 1361 | expected_local_stream, |
| 1362 | report->Get(expected_local_stream.id())->cast_to<RTCMediaStreamStats>()); |
| 1363 | |
| 1364 | RTCMediaStreamTrackStats expected_local_video_track_ssrc1( |
| 1365 | stats_of_track_type[0]->id(), report->timestamp_us(), |
| 1366 | RTCMediaStreamTrackKind::kVideo); |
| 1367 | expected_local_video_track_ssrc1.track_identifier = local_video_track->id(); |
| 1368 | expected_local_video_track_ssrc1.remote_source = false; |
| 1369 | expected_local_video_track_ssrc1.ended = false; |
| 1370 | expected_local_video_track_ssrc1.detached = false; |
| 1371 | expected_local_video_track_ssrc1.frame_width = 1234; |
| 1372 | expected_local_video_track_ssrc1.frame_height = 4321; |
| 1373 | expected_local_video_track_ssrc1.frames_sent = 11; |
| 1374 | ASSERT_TRUE(report->Get(expected_local_video_track_ssrc1.id())); |
| 1375 | EXPECT_EQ(expected_local_video_track_ssrc1, |
| 1376 | report->Get(expected_local_video_track_ssrc1.id()) |
| 1377 | ->cast_to<RTCMediaStreamTrackStats>()); |
| 1378 | } |
| 1379 | |
| 1380 | TEST_F(RTCStatsCollectorTest, |
| 1381 | CollectRemoteRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Video) { |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1382 | rtc::scoped_refptr<MediaStream> remote_stream = |
| 1383 | MediaStream::Create("RemoteStreamLabel"); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1384 | pc_->mutable_remote_streams()->AddStream(remote_stream); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1385 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1386 | // Remote video track with values |
| 1387 | rtc::scoped_refptr<MediaStreamTrackInterface> remote_video_track_ssrc3 = |
| 1388 | CreateFakeTrack(cricket::MEDIA_TYPE_VIDEO, "RemoteVideoTrackID3", |
| 1389 | MediaStreamTrackInterface::kEnded); |
| 1390 | remote_stream->AddTrack(static_cast<VideoTrackInterface*>( |
| 1391 | remote_video_track_ssrc3.get())); |
| 1392 | |
| 1393 | cricket::VideoReceiverInfo video_receiver_info_ssrc3; |
| 1394 | video_receiver_info_ssrc3.local_stats.push_back(cricket::SsrcReceiverInfo()); |
| 1395 | video_receiver_info_ssrc3.local_stats[0].ssrc = 3; |
| 1396 | video_receiver_info_ssrc3.frame_width = 6789; |
| 1397 | video_receiver_info_ssrc3.frame_height = 9876; |
hbos | 50cfe1f | 2017-01-23 07:21:55 -0800 | [diff] [blame] | 1398 | video_receiver_info_ssrc3.frames_received = 1000; |
| 1399 | video_receiver_info_ssrc3.frames_decoded = 995; |
| 1400 | video_receiver_info_ssrc3.frames_rendered = 990; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1401 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1402 | stats_->CreateMockRtpSendersReceiversAndChannels( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1403 | {}, {}, {}, |
Harald Alvestrand | c72af93 | 2018-01-11 17:18:19 +0100 | [diff] [blame] | 1404 | {std::make_pair(remote_video_track_ssrc3.get(), |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1405 | video_receiver_info_ssrc3)}, |
| 1406 | {}, {remote_stream}); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1407 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1408 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1409 | |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1410 | auto stats_of_my_type = report->GetStatsOfType<RTCMediaStreamStats>(); |
| 1411 | ASSERT_EQ(1, stats_of_my_type.size()) << "No stream in " << report->ToJson(); |
| 1412 | auto stats_of_track_type = report->GetStatsOfType<RTCMediaStreamTrackStats>(); |
| 1413 | ASSERT_EQ(1, stats_of_track_type.size()) |
| 1414 | << "Wrong number of tracks in " << report->ToJson(); |
| 1415 | ASSERT_TRUE(*(stats_of_track_type[0]->remote_source)); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1416 | |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1417 | RTCMediaStreamStats expected_remote_stream(stats_of_my_type[0]->id(), |
| 1418 | report->timestamp_us()); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1419 | expected_remote_stream.stream_identifier = remote_stream->label(); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1420 | expected_remote_stream.track_ids = |
| 1421 | std::vector<std::string>({stats_of_track_type[0]->id()}); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1422 | ASSERT_TRUE(report->Get(expected_remote_stream.id())); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1423 | EXPECT_EQ(expected_remote_stream, |
| 1424 | report->Get(expected_remote_stream.id())->cast_to< |
| 1425 | RTCMediaStreamStats>()); |
| 1426 | |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1427 | RTCMediaStreamTrackStats expected_remote_video_track_ssrc3( |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1428 | stats_of_track_type[0]->id(), report->timestamp_us(), |
| 1429 | RTCMediaStreamTrackKind::kVideo); |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1430 | expected_remote_video_track_ssrc3.track_identifier = |
| 1431 | remote_video_track_ssrc3->id(); |
| 1432 | expected_remote_video_track_ssrc3.remote_source = true; |
| 1433 | expected_remote_video_track_ssrc3.ended = true; |
| 1434 | expected_remote_video_track_ssrc3.detached = false; |
| 1435 | expected_remote_video_track_ssrc3.frame_width = 6789; |
| 1436 | expected_remote_video_track_ssrc3.frame_height = 9876; |
hbos | 50cfe1f | 2017-01-23 07:21:55 -0800 | [diff] [blame] | 1437 | expected_remote_video_track_ssrc3.frames_received = 1000; |
| 1438 | expected_remote_video_track_ssrc3.frames_decoded = 995; |
| 1439 | expected_remote_video_track_ssrc3.frames_dropped = 1000 - 990; |
hbos | 9e30274 | 2017-01-20 02:47:10 -0800 | [diff] [blame] | 1440 | ASSERT_TRUE(report->Get(expected_remote_video_track_ssrc3.id())); |
| 1441 | EXPECT_EQ(expected_remote_video_track_ssrc3, |
| 1442 | report->Get(expected_remote_video_track_ssrc3.id())->cast_to< |
| 1443 | RTCMediaStreamTrackStats>()); |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1446 | TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1447 | const char kTransportName[] = "TransportName"; |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1448 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1449 | cricket::VoiceMediaInfo voice_media_info; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1450 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1451 | voice_media_info.receivers.push_back(cricket::VoiceReceiverInfo()); |
| 1452 | voice_media_info.receivers[0].local_stats.push_back( |
| 1453 | cricket::SsrcReceiverInfo()); |
| 1454 | voice_media_info.receivers[0].local_stats[0].ssrc = 1; |
Harald Alvestrand | 719487e | 2017-12-13 12:26:04 +0100 | [diff] [blame] | 1455 | voice_media_info.receivers[0].packets_lost = -1; // Signed per RFC3550 |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1456 | voice_media_info.receivers[0].packets_rcvd = 2; |
| 1457 | voice_media_info.receivers[0].bytes_rcvd = 3; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1458 | voice_media_info.receivers[0].codec_payload_type = 42; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1459 | voice_media_info.receivers[0].jitter_ms = 4500; |
| 1460 | voice_media_info.receivers[0].fraction_lost = 5.5f; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1461 | |
| 1462 | RtpCodecParameters codec_parameters; |
| 1463 | codec_parameters.payload_type = 42; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 1464 | codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO; |
| 1465 | codec_parameters.name = "dummy"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1466 | codec_parameters.clock_rate = 0; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1467 | voice_media_info.receive_codecs.insert( |
| 1468 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1469 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1470 | auto* voice_media_channel = pc_->AddVoiceChannel("voice", kTransportName); |
| 1471 | voice_media_channel->SetStats(voice_media_info); |
| 1472 | stats_->SetupRemoteTrackAndReceiver(cricket::MEDIA_TYPE_AUDIO, |
| 1473 | "RemoteAudioTrackID", 1); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1474 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1475 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1476 | |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1477 | auto stats_of_track_type = report->GetStatsOfType<RTCMediaStreamTrackStats>(); |
| 1478 | ASSERT_EQ(1, stats_of_track_type.size()); |
| 1479 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1480 | RTCInboundRTPStreamStats expected_audio( |
| 1481 | "RTCInboundRTPAudioStream_1", report->timestamp_us()); |
hbos | 3443bb7 | 2017-02-07 06:28:11 -0800 | [diff] [blame] | 1482 | expected_audio.ssrc = 1; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1483 | expected_audio.is_remote = false; |
| 1484 | expected_audio.media_type = "audio"; |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1485 | expected_audio.track_id = stats_of_track_type[0]->id(); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1486 | expected_audio.transport_id = |
| 1487 | "RTCTransport_TransportName_" + |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1488 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1489 | expected_audio.codec_id = "RTCCodec_InboundAudio_42"; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1490 | expected_audio.packets_received = 2; |
| 1491 | expected_audio.bytes_received = 3; |
Harald Alvestrand | 719487e | 2017-12-13 12:26:04 +0100 | [diff] [blame] | 1492 | expected_audio.packets_lost = -1; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1493 | expected_audio.jitter = 4.5; |
| 1494 | expected_audio.fraction_lost = 5.5; |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 1495 | ASSERT_TRUE(report->Get(expected_audio.id())); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1496 | EXPECT_EQ( |
| 1497 | report->Get(expected_audio.id())->cast_to<RTCInboundRTPStreamStats>(), |
| 1498 | expected_audio); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 1499 | EXPECT_TRUE(report->Get(*expected_audio.track_id)); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1500 | EXPECT_TRUE(report->Get(*expected_audio.transport_id)); |
| 1501 | EXPECT_TRUE(report->Get(*expected_audio.codec_id)); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1505 | cricket::VideoMediaInfo video_media_info; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1506 | |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1507 | video_media_info.receivers.push_back(cricket::VideoReceiverInfo()); |
| 1508 | video_media_info.receivers[0].local_stats.push_back( |
| 1509 | cricket::SsrcReceiverInfo()); |
| 1510 | video_media_info.receivers[0].local_stats[0].ssrc = 1; |
| 1511 | video_media_info.receivers[0].packets_rcvd = 2; |
hbos | 02cd4d6 | 2016-12-09 04:19:44 -0800 | [diff] [blame] | 1512 | video_media_info.receivers[0].packets_lost = 42; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1513 | video_media_info.receivers[0].bytes_rcvd = 3; |
| 1514 | video_media_info.receivers[0].fraction_lost = 4.5f; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1515 | video_media_info.receivers[0].codec_payload_type = 42; |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 1516 | video_media_info.receivers[0].firs_sent = 5; |
| 1517 | video_media_info.receivers[0].plis_sent = 6; |
| 1518 | video_media_info.receivers[0].nacks_sent = 7; |
hbos | 6769c49 | 2017-01-02 08:35:13 -0800 | [diff] [blame] | 1519 | video_media_info.receivers[0].frames_decoded = 8; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1520 | video_media_info.receivers[0].qp_sum = rtc::nullopt; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1521 | |
| 1522 | RtpCodecParameters codec_parameters; |
| 1523 | codec_parameters.payload_type = 42; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 1524 | codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO; |
| 1525 | codec_parameters.name = "dummy"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1526 | codec_parameters.clock_rate = 0; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1527 | video_media_info.receive_codecs.insert( |
| 1528 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1529 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1530 | auto* video_media_channel = pc_->AddVideoChannel("video", "TransportName"); |
| 1531 | video_media_channel->SetStats(video_media_info); |
| 1532 | stats_->SetupRemoteTrackAndReceiver(cricket::MEDIA_TYPE_VIDEO, |
| 1533 | "RemoteVideoTrackID", 1); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1534 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1535 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1536 | |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 1537 | RTCInboundRTPStreamStats expected_video( |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1538 | "RTCInboundRTPVideoStream_1", report->timestamp_us()); |
hbos | 3443bb7 | 2017-02-07 06:28:11 -0800 | [diff] [blame] | 1539 | expected_video.ssrc = 1; |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 1540 | expected_video.is_remote = false; |
| 1541 | expected_video.media_type = "video"; |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1542 | expected_video.track_id = IdForType<RTCMediaStreamTrackStats>(report); |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 1543 | expected_video.transport_id = "RTCTransport_TransportName_" + |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1544 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1545 | expected_video.codec_id = "RTCCodec_InboundVideo_42"; |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 1546 | expected_video.fir_count = 5; |
| 1547 | expected_video.pli_count = 6; |
| 1548 | expected_video.nack_count = 7; |
| 1549 | expected_video.packets_received = 2; |
| 1550 | expected_video.bytes_received = 3; |
hbos | 02cd4d6 | 2016-12-09 04:19:44 -0800 | [diff] [blame] | 1551 | expected_video.packets_lost = 42; |
hbos | 820f578 | 2016-11-22 03:16:50 -0800 | [diff] [blame] | 1552 | expected_video.fraction_lost = 4.5; |
hbos | 6769c49 | 2017-01-02 08:35:13 -0800 | [diff] [blame] | 1553 | expected_video.frames_decoded = 8; |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1554 | // |expected_video.qp_sum| should be undefined. |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1555 | |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 1556 | ASSERT_TRUE(report->Get(expected_video.id())); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1557 | EXPECT_EQ( |
| 1558 | report->Get(expected_video.id())->cast_to<RTCInboundRTPStreamStats>(), |
| 1559 | expected_video); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1560 | |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1561 | // Set previously undefined values and "GetStats" again. |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1562 | video_media_info.receivers[0].qp_sum = 9; |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1563 | expected_video.qp_sum = 9; |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1564 | video_media_channel->SetStats(video_media_info); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1565 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1566 | report = stats_->GetFreshStatsReport(); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1567 | |
| 1568 | ASSERT_TRUE(report->Get(expected_video.id())); |
| 1569 | EXPECT_EQ( |
| 1570 | report->Get(expected_video.id())->cast_to<RTCInboundRTPStreamStats>(), |
| 1571 | expected_video); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 1572 | EXPECT_TRUE(report->Get(*expected_video.track_id)); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1573 | EXPECT_TRUE(report->Get(*expected_video.transport_id)); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1574 | EXPECT_TRUE(report->Get(*expected_video.codec_id)); |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1577 | TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1578 | cricket::VoiceMediaInfo voice_media_info; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1579 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1580 | voice_media_info.senders.push_back(cricket::VoiceSenderInfo()); |
| 1581 | voice_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); |
| 1582 | voice_media_info.senders[0].local_stats[0].ssrc = 1; |
| 1583 | voice_media_info.senders[0].packets_sent = 2; |
| 1584 | voice_media_info.senders[0].bytes_sent = 3; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1585 | voice_media_info.senders[0].codec_payload_type = 42; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1586 | |
| 1587 | RtpCodecParameters codec_parameters; |
| 1588 | codec_parameters.payload_type = 42; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 1589 | codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO; |
| 1590 | codec_parameters.name = "dummy"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1591 | codec_parameters.clock_rate = 0; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1592 | voice_media_info.send_codecs.insert( |
| 1593 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1594 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1595 | auto* voice_media_channel = pc_->AddVoiceChannel("audio", "TransportName"); |
| 1596 | voice_media_channel->SetStats(voice_media_info); |
| 1597 | stats_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO, |
| 1598 | "LocalAudioTrackID", 1, true); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1599 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1600 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1601 | |
| 1602 | RTCOutboundRTPStreamStats expected_audio( |
| 1603 | "RTCOutboundRTPAudioStream_1", report->timestamp_us()); |
hbos | 3443bb7 | 2017-02-07 06:28:11 -0800 | [diff] [blame] | 1604 | expected_audio.ssrc = 1; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1605 | expected_audio.is_remote = false; |
| 1606 | expected_audio.media_type = "audio"; |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1607 | expected_audio.track_id = IdForType<RTCMediaStreamTrackStats>(report); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1608 | expected_audio.transport_id = "RTCTransport_TransportName_" + |
| 1609 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1610 | expected_audio.codec_id = "RTCCodec_OutboundAudio_42"; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1611 | expected_audio.packets_sent = 2; |
| 1612 | expected_audio.bytes_sent = 3; |
hbos | cd195be | 2017-02-07 08:31:27 -0800 | [diff] [blame] | 1613 | |
hbos | cd195be | 2017-02-07 08:31:27 -0800 | [diff] [blame] | 1614 | ASSERT_TRUE(report->Get(expected_audio.id())); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1615 | EXPECT_EQ( |
| 1616 | report->Get(expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(), |
| 1617 | expected_audio); |
skvlad | ed02c6d | 2017-02-07 10:45:31 -0800 | [diff] [blame] | 1618 | |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1619 | ASSERT_TRUE(report->Get(expected_audio.id())); |
| 1620 | EXPECT_EQ( |
| 1621 | report->Get(expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(), |
| 1622 | expected_audio); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 1623 | EXPECT_TRUE(report->Get(*expected_audio.track_id)); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1624 | EXPECT_TRUE(report->Get(*expected_audio.transport_id)); |
| 1625 | EXPECT_TRUE(report->Get(*expected_audio.codec_id)); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1629 | cricket::VideoMediaInfo video_media_info; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1630 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1631 | video_media_info.senders.push_back(cricket::VideoSenderInfo()); |
| 1632 | video_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); |
| 1633 | video_media_info.senders[0].local_stats[0].ssrc = 1; |
| 1634 | video_media_info.senders[0].firs_rcvd = 2; |
| 1635 | video_media_info.senders[0].plis_rcvd = 3; |
| 1636 | video_media_info.senders[0].nacks_rcvd = 4; |
| 1637 | video_media_info.senders[0].packets_sent = 5; |
| 1638 | video_media_info.senders[0].bytes_sent = 6; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1639 | video_media_info.senders[0].codec_payload_type = 42; |
hbos | 6769c49 | 2017-01-02 08:35:13 -0800 | [diff] [blame] | 1640 | video_media_info.senders[0].frames_encoded = 8; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1641 | video_media_info.senders[0].qp_sum = rtc::nullopt; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1642 | |
| 1643 | RtpCodecParameters codec_parameters; |
| 1644 | codec_parameters.payload_type = 42; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 1645 | codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO; |
| 1646 | codec_parameters.name = "dummy"; |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1647 | codec_parameters.clock_rate = 0; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1648 | video_media_info.send_codecs.insert( |
| 1649 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1650 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1651 | auto* video_media_channel = pc_->AddVideoChannel("video", "TransportName"); |
| 1652 | video_media_channel->SetStats(video_media_info); |
| 1653 | stats_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_VIDEO, |
| 1654 | "LocalVideoTrackID", 1, true); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1655 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1656 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1657 | |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1658 | auto stats_of_my_type = report->GetStatsOfType<RTCOutboundRTPStreamStats>(); |
| 1659 | ASSERT_EQ(1, stats_of_my_type.size()); |
| 1660 | auto stats_of_track_type = report->GetStatsOfType<RTCMediaStreamTrackStats>(); |
| 1661 | ASSERT_EQ(1, stats_of_track_type.size()); |
| 1662 | |
| 1663 | RTCOutboundRTPStreamStats expected_video(stats_of_my_type[0]->id(), |
| 1664 | report->timestamp_us()); |
hbos | 3443bb7 | 2017-02-07 06:28:11 -0800 | [diff] [blame] | 1665 | expected_video.ssrc = 1; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1666 | expected_video.is_remote = false; |
| 1667 | expected_video.media_type = "video"; |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1668 | expected_video.track_id = stats_of_track_type[0]->id(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1669 | expected_video.transport_id = "RTCTransport_TransportName_" + |
| 1670 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame] | 1671 | expected_video.codec_id = "RTCCodec_OutboundVideo_42"; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1672 | expected_video.fir_count = 2; |
| 1673 | expected_video.pli_count = 3; |
| 1674 | expected_video.nack_count = 4; |
| 1675 | expected_video.packets_sent = 5; |
| 1676 | expected_video.bytes_sent = 6; |
skvlad | ed02c6d | 2017-02-07 10:45:31 -0800 | [diff] [blame] | 1677 | expected_video.frames_encoded = 8; |
hbos | a7a9be1 | 2017-03-01 01:02:45 -0800 | [diff] [blame] | 1678 | // |expected_video.qp_sum| should be undefined. |
hbos | cd195be | 2017-02-07 08:31:27 -0800 | [diff] [blame] | 1679 | ASSERT_TRUE(report->Get(expected_video.id())); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1680 | |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1681 | EXPECT_EQ( |
| 1682 | report->Get(expected_video.id())->cast_to<RTCOutboundRTPStreamStats>(), |
| 1683 | expected_video); |
skvlad | ed02c6d | 2017-02-07 10:45:31 -0800 | [diff] [blame] | 1684 | |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1685 | // Set previously undefined values and "GetStats" again. |
Oskar Sundbom | cbc71b2 | 2017-11-16 10:56:07 +0100 | [diff] [blame] | 1686 | video_media_info.senders[0].qp_sum = 9; |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1687 | expected_video.qp_sum = 9; |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1688 | video_media_channel->SetStats(video_media_info); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1689 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1690 | report = stats_->GetFreshStatsReport(); |
hbos | a51d4f3 | 2017-02-16 05:34:48 -0800 | [diff] [blame] | 1691 | |
| 1692 | ASSERT_TRUE(report->Get(expected_video.id())); |
| 1693 | EXPECT_EQ( |
| 1694 | report->Get(expected_video.id())->cast_to<RTCOutboundRTPStreamStats>(), |
| 1695 | expected_video); |
hbos | b0ae920 | 2017-01-27 06:35:16 -0800 | [diff] [blame] | 1696 | EXPECT_TRUE(report->Get(*expected_video.track_id)); |
hbos | 84abeb1 | 2017-01-16 06:16:44 -0800 | [diff] [blame] | 1697 | EXPECT_TRUE(report->Get(*expected_video.transport_id)); |
| 1698 | EXPECT_TRUE(report->Get(*expected_video.codec_id)); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1701 | TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1702 | const char kTransportName[] = "transport"; |
| 1703 | |
| 1704 | pc_->AddVoiceChannel("audio", kTransportName); |
| 1705 | |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 1706 | std::unique_ptr<cricket::Candidate> rtp_local_candidate = |
| 1707 | CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI, |
| 1708 | cricket::LOCAL_PORT_TYPE, 42); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1709 | std::unique_ptr<cricket::Candidate> rtp_remote_candidate = |
| 1710 | CreateFakeCandidate("42.42.42.42", 42, "protocol", |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 1711 | rtc::ADAPTER_TYPE_UNKNOWN, cricket::LOCAL_PORT_TYPE, |
| 1712 | 42); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1713 | std::unique_ptr<cricket::Candidate> rtcp_local_candidate = |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 1714 | CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI, |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1715 | cricket::LOCAL_PORT_TYPE, 42); |
| 1716 | std::unique_ptr<cricket::Candidate> rtcp_remote_candidate = |
| 1717 | CreateFakeCandidate("42.42.42.42", 42, "protocol", |
Gary Liu | 37e489c | 2017-11-21 10:49:36 -0800 | [diff] [blame] | 1718 | rtc::ADAPTER_TYPE_UNKNOWN, cricket::LOCAL_PORT_TYPE, |
| 1719 | 42); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1720 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1721 | cricket::ConnectionInfo rtp_connection_info; |
| 1722 | rtp_connection_info.best_connection = false; |
| 1723 | rtp_connection_info.local_candidate = *rtp_local_candidate.get(); |
| 1724 | rtp_connection_info.remote_candidate = *rtp_remote_candidate.get(); |
| 1725 | rtp_connection_info.sent_total_bytes = 42; |
| 1726 | rtp_connection_info.recv_total_bytes = 1337; |
| 1727 | cricket::TransportChannelStats rtp_transport_channel_stats; |
| 1728 | rtp_transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
| 1729 | rtp_transport_channel_stats.connection_infos.push_back(rtp_connection_info); |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1730 | rtp_transport_channel_stats.dtls_state = cricket::DTLS_TRANSPORT_NEW; |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1731 | pc_->SetTransportStats(kTransportName, {rtp_transport_channel_stats}); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1732 | |
| 1733 | // Get stats without RTCP, an active connection or certificates. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1734 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1735 | |
| 1736 | RTCTransportStats expected_rtp_transport( |
| 1737 | "RTCTransport_transport_" + |
| 1738 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP), |
| 1739 | report->timestamp_us()); |
| 1740 | expected_rtp_transport.bytes_sent = 42; |
| 1741 | expected_rtp_transport.bytes_received = 1337; |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1742 | expected_rtp_transport.dtls_state = RTCDtlsTransportState::kNew; |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1743 | |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1744 | ASSERT_TRUE(report->Get(expected_rtp_transport.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1745 | EXPECT_EQ( |
| 1746 | expected_rtp_transport, |
| 1747 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1748 | |
| 1749 | cricket::ConnectionInfo rtcp_connection_info; |
| 1750 | rtcp_connection_info.best_connection = false; |
| 1751 | rtcp_connection_info.local_candidate = *rtcp_local_candidate.get(); |
| 1752 | rtcp_connection_info.remote_candidate = *rtcp_remote_candidate.get(); |
| 1753 | rtcp_connection_info.sent_total_bytes = 1337; |
| 1754 | rtcp_connection_info.recv_total_bytes = 42; |
| 1755 | cricket::TransportChannelStats rtcp_transport_channel_stats; |
| 1756 | rtcp_transport_channel_stats.component = |
| 1757 | cricket::ICE_CANDIDATE_COMPONENT_RTCP; |
| 1758 | rtcp_transport_channel_stats.connection_infos.push_back(rtcp_connection_info); |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1759 | rtcp_transport_channel_stats.dtls_state = cricket::DTLS_TRANSPORT_CONNECTING; |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1760 | pc_->SetTransportStats(kTransportName, {rtp_transport_channel_stats, |
| 1761 | rtcp_transport_channel_stats}); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1762 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1763 | // Get stats with RTCP and without an active connection or certificates. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1764 | report = stats_->GetFreshStatsReport(); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1765 | |
| 1766 | RTCTransportStats expected_rtcp_transport( |
| 1767 | "RTCTransport_transport_" + |
| 1768 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTCP), |
| 1769 | report->timestamp_us()); |
| 1770 | expected_rtcp_transport.bytes_sent = 1337; |
| 1771 | expected_rtcp_transport.bytes_received = 42; |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1772 | expected_rtcp_transport.dtls_state = RTCDtlsTransportState::kConnecting; |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1773 | |
| 1774 | expected_rtp_transport.rtcp_transport_stats_id = expected_rtcp_transport.id(); |
| 1775 | |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1776 | ASSERT_TRUE(report->Get(expected_rtp_transport.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1777 | EXPECT_EQ( |
| 1778 | expected_rtp_transport, |
| 1779 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1780 | ASSERT_TRUE(report->Get(expected_rtcp_transport.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1781 | EXPECT_EQ( |
| 1782 | expected_rtcp_transport, |
| 1783 | report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1784 | |
hbos | 7064d59 | 2017-01-16 07:38:02 -0800 | [diff] [blame] | 1785 | // Get stats with an active connection (selected candidate pair). |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1786 | rtcp_transport_channel_stats.connection_infos[0].best_connection = true; |
| 1787 | pc_->SetTransportStats(kTransportName, {rtp_transport_channel_stats, |
| 1788 | rtcp_transport_channel_stats}); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1789 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1790 | report = stats_->GetFreshStatsReport(); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1791 | |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1792 | expected_rtcp_transport.selected_candidate_pair_id = |
| 1793 | "RTCIceCandidatePair_" + rtcp_local_candidate->id() + "_" + |
| 1794 | rtcp_remote_candidate->id(); |
| 1795 | |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1796 | ASSERT_TRUE(report->Get(expected_rtp_transport.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1797 | EXPECT_EQ( |
| 1798 | expected_rtp_transport, |
| 1799 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1800 | ASSERT_TRUE(report->Get(expected_rtcp_transport.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1801 | EXPECT_EQ( |
| 1802 | expected_rtcp_transport, |
| 1803 | report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1804 | |
| 1805 | // Get stats with certificates. |
| 1806 | std::unique_ptr<CertificateInfo> local_certinfo = |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1807 | CreateFakeCertificateAndInfoFromDers({"(local) local", "(local) chain"}); |
| 1808 | pc_->SetLocalCertificate(kTransportName, local_certinfo->certificate); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1809 | std::unique_ptr<CertificateInfo> remote_certinfo = |
| 1810 | CreateFakeCertificateAndInfoFromDers( |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1811 | {"(remote) local", "(remote) chain"}); |
| 1812 | pc_->SetRemoteCertificate( |
| 1813 | kTransportName, |
| 1814 | remote_certinfo->certificate->ssl_certificate().GetUniqueReference()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1815 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1816 | report = stats_->GetFreshStatsReport(); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1817 | |
| 1818 | expected_rtp_transport.local_certificate_id = |
| 1819 | "RTCCertificate_" + local_certinfo->fingerprints[0]; |
| 1820 | expected_rtp_transport.remote_certificate_id = |
| 1821 | "RTCCertificate_" + remote_certinfo->fingerprints[0]; |
| 1822 | |
| 1823 | expected_rtcp_transport.local_certificate_id = |
| 1824 | *expected_rtp_transport.local_certificate_id; |
| 1825 | expected_rtcp_transport.remote_certificate_id = |
| 1826 | *expected_rtp_transport.remote_certificate_id; |
| 1827 | |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1828 | ASSERT_TRUE(report->Get(expected_rtp_transport.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1829 | EXPECT_EQ( |
| 1830 | expected_rtp_transport, |
| 1831 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | dbb64d8 | 2016-12-21 01:57:46 -0800 | [diff] [blame] | 1832 | ASSERT_TRUE(report->Get(expected_rtcp_transport.id())); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 1833 | EXPECT_EQ( |
| 1834 | expected_rtcp_transport, |
| 1835 | report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1838 | TEST_F(RTCStatsCollectorTest, CollectNoStreamRTCOutboundRTPStreamStats_Audio) { |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1839 | cricket::VoiceMediaInfo voice_media_info; |
| 1840 | |
| 1841 | voice_media_info.senders.push_back(cricket::VoiceSenderInfo()); |
| 1842 | voice_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); |
| 1843 | voice_media_info.senders[0].local_stats[0].ssrc = 1; |
| 1844 | voice_media_info.senders[0].packets_sent = 2; |
| 1845 | voice_media_info.senders[0].bytes_sent = 3; |
| 1846 | voice_media_info.senders[0].codec_payload_type = 42; |
| 1847 | |
| 1848 | RtpCodecParameters codec_parameters; |
| 1849 | codec_parameters.payload_type = 42; |
| 1850 | codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO; |
| 1851 | codec_parameters.name = "dummy"; |
| 1852 | codec_parameters.clock_rate = 0; |
| 1853 | voice_media_info.send_codecs.insert( |
| 1854 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1855 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1856 | // Emulates the case where AddTrack is used without an associated MediaStream |
| 1857 | auto* voice_media_channel = pc_->AddVoiceChannel("audio", "TransportName"); |
| 1858 | voice_media_channel->SetStats(voice_media_info); |
| 1859 | stats_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO, |
| 1860 | "LocalAudioTrackID", 1, false); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1861 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1862 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1863 | |
| 1864 | RTCOutboundRTPStreamStats expected_audio("RTCOutboundRTPAudioStream_1", |
| 1865 | report->timestamp_us()); |
| 1866 | expected_audio.ssrc = 1; |
| 1867 | expected_audio.is_remote = false; |
| 1868 | expected_audio.media_type = "audio"; |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1869 | expected_audio.track_id = IdForType<RTCMediaStreamTrackStats>(report); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1870 | expected_audio.transport_id = |
| 1871 | "RTCTransport_TransportName_" + |
| 1872 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1873 | expected_audio.codec_id = "RTCCodec_OutboundAudio_42"; |
| 1874 | expected_audio.packets_sent = 2; |
| 1875 | expected_audio.bytes_sent = 3; |
| 1876 | |
| 1877 | ASSERT_TRUE(report->Get(expected_audio.id())); |
| 1878 | EXPECT_EQ( |
| 1879 | report->Get(expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(), |
| 1880 | expected_audio); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1881 | EXPECT_TRUE(report->Get(*expected_audio.track_id)); |
| 1882 | EXPECT_TRUE(report->Get(*expected_audio.transport_id)); |
| 1883 | EXPECT_TRUE(report->Get(*expected_audio.codec_id)); |
| 1884 | } |
| 1885 | |
| 1886 | // When the PC has not had SetLocalDescription done, tracks all have |
| 1887 | // SSRC 0, meaning "unconnected". |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 1888 | // In this state, we report on track stats, but not RTP stats. |
| 1889 | TEST_F(RTCStatsCollectorTest, StatsReportedOnZeroSsrc) { |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1890 | rtc::scoped_refptr<MediaStreamTrackInterface> track = |
| 1891 | CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "audioTrack", |
| 1892 | MediaStreamTrackInterface::kLive); |
Harald Alvestrand | a3dab84 | 2018-01-14 09:18:58 +0100 | [diff] [blame] | 1893 | rtc::scoped_refptr<MockRtpSender> sender = CreateMockSender(track, 0, 49, {}); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1894 | pc_->AddSender(sender); |
| 1895 | |
| 1896 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
| 1897 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1898 | std::vector<const RTCMediaStreamTrackStats*> track_stats = |
| 1899 | report->GetStatsOfType<RTCMediaStreamTrackStats>(); |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 1900 | EXPECT_EQ(1, track_stats.size()); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1901 | |
Harald Alvestrand | b8e1201 | 2018-01-23 15:28:16 +0100 | [diff] [blame] | 1902 | std::vector<const RTCRTPStreamStats*> rtp_stream_stats = |
| 1903 | report->GetStatsOfType<RTCRTPStreamStats>(); |
| 1904 | EXPECT_EQ(0, rtp_stream_stats.size()); |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 1905 | } |
| 1906 | |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 1907 | TEST_F(RTCStatsCollectorTest, DoNotCrashOnSsrcChange) { |
| 1908 | rtc::scoped_refptr<MediaStreamTrackInterface> track = |
| 1909 | CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "audioTrack", |
| 1910 | MediaStreamTrackInterface::kLive); |
| 1911 | rtc::scoped_refptr<MockRtpSender> sender = |
| 1912 | CreateMockSender(track, 4711, 49, {}); |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1913 | pc_->AddSender(sender); |
| 1914 | |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 1915 | // We do not generate any matching voice_sender_info stats. |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1916 | rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport(); |
| 1917 | |
Harald Alvestrand | 76d2952 | 2018-01-30 14:43:29 +0100 | [diff] [blame] | 1918 | std::vector<const RTCMediaStreamTrackStats*> track_stats = |
| 1919 | report->GetStatsOfType<RTCMediaStreamTrackStats>(); |
| 1920 | EXPECT_EQ(1, track_stats.size()); |
| 1921 | } |
| 1922 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1923 | class RTCTestStats : public RTCStats { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 1924 | public: |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1925 | WEBRTC_RTCSTATS_DECL(); |
| 1926 | |
| 1927 | RTCTestStats(const std::string& id, int64_t timestamp_us) |
| 1928 | : RTCStats(id, timestamp_us), dummy_stat("dummyStat") {} |
| 1929 | |
| 1930 | RTCStatsMember<int32_t> dummy_stat; |
| 1931 | }; |
| 1932 | |
| 1933 | WEBRTC_RTCSTATS_IMPL(RTCTestStats, RTCStats, "test-stats", &dummy_stat); |
| 1934 | |
| 1935 | // Overrides the stats collection to verify thread usage and that the resulting |
| 1936 | // partial reports are merged. |
| 1937 | class FakeRTCStatsCollector : public RTCStatsCollector, |
| 1938 | public RTCStatsCollectorCallback { |
| 1939 | public: |
| 1940 | static rtc::scoped_refptr<FakeRTCStatsCollector> Create( |
| 1941 | PeerConnectionInternal* pc, |
| 1942 | int64_t cache_lifetime_us) { |
| 1943 | return rtc::scoped_refptr<FakeRTCStatsCollector>( |
| 1944 | new rtc::RefCountedObject<FakeRTCStatsCollector>(pc, |
| 1945 | cache_lifetime_us)); |
| 1946 | } |
| 1947 | |
| 1948 | // RTCStatsCollectorCallback implementation. |
| 1949 | void OnStatsDelivered( |
| 1950 | const rtc::scoped_refptr<const RTCStatsReport>& report) override { |
| 1951 | EXPECT_TRUE(signaling_thread_->IsCurrent()); |
| 1952 | rtc::CritScope cs(&lock_); |
| 1953 | delivered_report_ = report; |
| 1954 | } |
| 1955 | |
| 1956 | void VerifyThreadUsageAndResultsMerging() { |
| 1957 | GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback>(this)); |
| 1958 | EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs); |
| 1959 | } |
| 1960 | |
| 1961 | bool HasVerifiedResults() { |
| 1962 | EXPECT_TRUE(signaling_thread_->IsCurrent()); |
| 1963 | rtc::CritScope cs(&lock_); |
| 1964 | if (!delivered_report_) |
| 1965 | return false; |
| 1966 | EXPECT_EQ(produced_on_signaling_thread_, 1); |
| 1967 | EXPECT_EQ(produced_on_network_thread_, 1); |
| 1968 | |
| 1969 | EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats")); |
| 1970 | EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats")); |
| 1971 | |
| 1972 | produced_on_signaling_thread_ = 0; |
| 1973 | produced_on_network_thread_ = 0; |
| 1974 | delivered_report_ = nullptr; |
| 1975 | return true; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | protected: |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 1979 | FakeRTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime) |
| 1980 | : RTCStatsCollector(pc, cache_lifetime), |
| 1981 | signaling_thread_(pc->signaling_thread()), |
| 1982 | worker_thread_(pc->worker_thread()), |
| 1983 | network_thread_(pc->network_thread()) {} |
| 1984 | |
| 1985 | void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override { |
| 1986 | EXPECT_TRUE(signaling_thread_->IsCurrent()); |
| 1987 | { |
| 1988 | rtc::CritScope cs(&lock_); |
| 1989 | EXPECT_FALSE(delivered_report_); |
| 1990 | ++produced_on_signaling_thread_; |
| 1991 | } |
| 1992 | |
| 1993 | rtc::scoped_refptr<RTCStatsReport> signaling_report = |
| 1994 | RTCStatsReport::Create(0); |
| 1995 | signaling_report->AddStats(std::unique_ptr<const RTCStats>( |
| 1996 | new RTCTestStats("SignalingThreadStats", timestamp_us))); |
| 1997 | AddPartialResults(signaling_report); |
| 1998 | } |
| 1999 | void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override { |
| 2000 | EXPECT_TRUE(network_thread_->IsCurrent()); |
| 2001 | { |
| 2002 | rtc::CritScope cs(&lock_); |
| 2003 | EXPECT_FALSE(delivered_report_); |
| 2004 | ++produced_on_network_thread_; |
| 2005 | } |
| 2006 | |
| 2007 | rtc::scoped_refptr<RTCStatsReport> network_report = |
| 2008 | RTCStatsReport::Create(0); |
| 2009 | network_report->AddStats(std::unique_ptr<const RTCStats>( |
| 2010 | new RTCTestStats("NetworkThreadStats", timestamp_us))); |
| 2011 | AddPartialResults(network_report); |
| 2012 | } |
| 2013 | |
| 2014 | private: |
| 2015 | rtc::Thread* const signaling_thread_; |
| 2016 | rtc::Thread* const worker_thread_; |
| 2017 | rtc::Thread* const network_thread_; |
| 2018 | |
| 2019 | rtc::CriticalSection lock_; |
| 2020 | rtc::scoped_refptr<const RTCStatsReport> delivered_report_; |
| 2021 | int produced_on_signaling_thread_ = 0; |
| 2022 | int produced_on_network_thread_ = 0; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 2023 | }; |
| 2024 | |
Steve Anton | 5b38731 | 2018-02-02 16:00:20 -0800 | [diff] [blame^] | 2025 | TEST(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { |
| 2026 | rtc::scoped_refptr<FakePeerConnectionForStats> pc( |
| 2027 | new rtc::RefCountedObject<FakePeerConnectionForStats>()); |
| 2028 | rtc::scoped_refptr<FakeRTCStatsCollector> stats_collector( |
| 2029 | FakeRTCStatsCollector::Create(pc, 50 * rtc::kNumMicrosecsPerMillisec)); |
| 2030 | stats_collector->VerifyThreadUsageAndResultsMerging(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | } // namespace |
| 2034 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 2035 | } // namespace webrtc |