| OLD | NEW |
| (Empty) |
| 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 | |
| 11 #include "webrtc/api/rtcstatscollector.h" | |
| 12 | |
| 13 #include <memory> | |
| 14 #include <ostream> | |
| 15 #include <string> | |
| 16 #include <vector> | |
| 17 | |
| 18 #include "webrtc/api/jsepsessiondescription.h" | |
| 19 #include "webrtc/api/mediastream.h" | |
| 20 #include "webrtc/api/mediastreamtrack.h" | |
| 21 #include "webrtc/api/rtpparameters.h" | |
| 22 #include "webrtc/api/stats/rtcstats_objects.h" | |
| 23 #include "webrtc/api/stats/rtcstatsreport.h" | |
| 24 #include "webrtc/api/test/mock_datachannel.h" | |
| 25 #include "webrtc/api/test/mock_peerconnection.h" | |
| 26 #include "webrtc/api/test/mock_webrtcsession.h" | |
| 27 #include "webrtc/api/test/rtcstatsobtainer.h" | |
| 28 #include "webrtc/base/checks.h" | |
| 29 #include "webrtc/base/fakeclock.h" | |
| 30 #include "webrtc/base/fakesslidentity.h" | |
| 31 #include "webrtc/base/gunit.h" | |
| 32 #include "webrtc/base/logging.h" | |
| 33 #include "webrtc/base/socketaddress.h" | |
| 34 #include "webrtc/base/thread_checker.h" | |
| 35 #include "webrtc/base/timedelta.h" | |
| 36 #include "webrtc/base/timeutils.h" | |
| 37 #include "webrtc/logging/rtc_event_log/rtc_event_log.h" | |
| 38 #include "webrtc/media/base/fakemediaengine.h" | |
| 39 #include "webrtc/media/base/test/mock_mediachannel.h" | |
| 40 #include "webrtc/p2p/base/p2pconstants.h" | |
| 41 #include "webrtc/p2p/base/port.h" | |
| 42 | |
| 43 using testing::_; | |
| 44 using testing::Invoke; | |
| 45 using testing::Return; | |
| 46 using testing::ReturnNull; | |
| 47 using testing::ReturnRef; | |
| 48 using testing::SetArgPointee; | |
| 49 | |
| 50 namespace webrtc { | |
| 51 | |
| 52 // These are used by gtest code, such as if |EXPECT_EQ| fails. | |
| 53 void PrintTo(const RTCCertificateStats& stats, ::std::ostream* os) { | |
| 54 *os << stats.ToString(); | |
| 55 } | |
| 56 | |
| 57 void PrintTo(const RTCCodecStats& stats, ::std::ostream* os) { | |
| 58 *os << stats.ToString(); | |
| 59 } | |
| 60 | |
| 61 void PrintTo(const RTCDataChannelStats& stats, ::std::ostream* os) { | |
| 62 *os << stats.ToString(); | |
| 63 } | |
| 64 | |
| 65 void PrintTo(const RTCIceCandidatePairStats& stats, ::std::ostream* os) { | |
| 66 *os << stats.ToString(); | |
| 67 } | |
| 68 | |
| 69 void PrintTo(const RTCLocalIceCandidateStats& stats, ::std::ostream* os) { | |
| 70 *os << stats.ToString(); | |
| 71 } | |
| 72 | |
| 73 void PrintTo(const RTCRemoteIceCandidateStats& stats, ::std::ostream* os) { | |
| 74 *os << stats.ToString(); | |
| 75 } | |
| 76 | |
| 77 void PrintTo(const RTCPeerConnectionStats& stats, ::std::ostream* os) { | |
| 78 *os << stats.ToString(); | |
| 79 } | |
| 80 | |
| 81 void PrintTo(const RTCMediaStreamStats& stats, ::std::ostream* os) { | |
| 82 *os << stats.ToString(); | |
| 83 } | |
| 84 | |
| 85 void PrintTo(const RTCMediaStreamTrackStats& stats, ::std::ostream* os) { | |
| 86 *os << stats.ToString(); | |
| 87 } | |
| 88 | |
| 89 void PrintTo(const RTCInboundRTPStreamStats& stats, ::std::ostream* os) { | |
| 90 *os << stats.ToString(); | |
| 91 } | |
| 92 | |
| 93 void PrintTo(const RTCOutboundRTPStreamStats& stats, ::std::ostream* os) { | |
| 94 *os << stats.ToString(); | |
| 95 } | |
| 96 | |
| 97 void PrintTo(const RTCTransportStats& stats, ::std::ostream* os) { | |
| 98 *os << stats.ToString(); | |
| 99 } | |
| 100 | |
| 101 namespace { | |
| 102 | |
| 103 const int64_t kGetStatsReportTimeoutMs = 1000; | |
| 104 const bool kDefaultRtcpEnabled = false; | |
| 105 const bool kDefaultSrtpRequired = true; | |
| 106 | |
| 107 struct CertificateInfo { | |
| 108 rtc::scoped_refptr<rtc::RTCCertificate> certificate; | |
| 109 std::vector<std::string> ders; | |
| 110 std::vector<std::string> pems; | |
| 111 std::vector<std::string> fingerprints; | |
| 112 }; | |
| 113 | |
| 114 std::unique_ptr<CertificateInfo> CreateFakeCertificateAndInfoFromDers( | |
| 115 const std::vector<std::string>& ders) { | |
| 116 RTC_CHECK(!ders.empty()); | |
| 117 std::unique_ptr<CertificateInfo> info(new CertificateInfo()); | |
| 118 info->ders = ders; | |
| 119 for (const std::string& der : ders) { | |
| 120 info->pems.push_back(rtc::SSLIdentity::DerToPem( | |
| 121 "CERTIFICATE", | |
| 122 reinterpret_cast<const unsigned char*>(der.c_str()), | |
| 123 der.length())); | |
| 124 } | |
| 125 info->certificate = | |
| 126 rtc::RTCCertificate::Create(std::unique_ptr<rtc::FakeSSLIdentity>( | |
| 127 new rtc::FakeSSLIdentity(rtc::FakeSSLCertificate(info->pems)))); | |
| 128 // Strip header/footer and newline characters of PEM strings. | |
| 129 for (size_t i = 0; i < info->pems.size(); ++i) { | |
| 130 rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27, | |
| 131 "", 0, &info->pems[i]); | |
| 132 rtc::replace_substrs("-----END CERTIFICATE-----", 25, | |
| 133 "", 0, &info->pems[i]); | |
| 134 rtc::replace_substrs("\n", 1, | |
| 135 "", 0, &info->pems[i]); | |
| 136 } | |
| 137 // Fingerprint of leaf certificate. | |
| 138 std::unique_ptr<rtc::SSLFingerprint> fp( | |
| 139 rtc::SSLFingerprint::Create("sha-1", | |
| 140 &info->certificate->ssl_certificate())); | |
| 141 EXPECT_TRUE(fp); | |
| 142 info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); | |
| 143 // Fingerprints of the rest of the chain. | |
| 144 std::unique_ptr<rtc::SSLCertChain> chain = | |
| 145 info->certificate->ssl_certificate().GetChain(); | |
| 146 if (chain) { | |
| 147 for (size_t i = 0; i < chain->GetSize(); i++) { | |
| 148 fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i))); | |
| 149 EXPECT_TRUE(fp); | |
| 150 info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); | |
| 151 } | |
| 152 } | |
| 153 EXPECT_EQ(info->ders.size(), info->fingerprints.size()); | |
| 154 return info; | |
| 155 } | |
| 156 | |
| 157 std::unique_ptr<cricket::Candidate> CreateFakeCandidate( | |
| 158 const std::string& hostname, | |
| 159 int port, | |
| 160 const std::string& protocol, | |
| 161 const std::string& candidate_type, | |
| 162 uint32_t priority) { | |
| 163 std::unique_ptr<cricket::Candidate> candidate(new cricket::Candidate()); | |
| 164 candidate->set_address(rtc::SocketAddress(hostname, port)); | |
| 165 candidate->set_protocol(protocol); | |
| 166 candidate->set_type(candidate_type); | |
| 167 candidate->set_priority(priority); | |
| 168 return candidate; | |
| 169 } | |
| 170 | |
| 171 class FakeAudioProcessorForStats | |
| 172 : public rtc::RefCountedObject<AudioProcessorInterface> { | |
| 173 public: | |
| 174 FakeAudioProcessorForStats( | |
| 175 AudioProcessorInterface::AudioProcessorStats stats) | |
| 176 : stats_(stats) { | |
| 177 } | |
| 178 | |
| 179 void GetStats(AudioProcessorInterface::AudioProcessorStats* stats) override { | |
| 180 *stats = stats_; | |
| 181 } | |
| 182 | |
| 183 private: | |
| 184 AudioProcessorInterface::AudioProcessorStats stats_; | |
| 185 }; | |
| 186 | |
| 187 class FakeAudioTrackForStats | |
| 188 : public MediaStreamTrack<AudioTrackInterface> { | |
| 189 public: | |
| 190 static rtc::scoped_refptr<FakeAudioTrackForStats> Create( | |
| 191 const std::string& id, | |
| 192 MediaStreamTrackInterface::TrackState state, | |
| 193 int signal_level, | |
| 194 rtc::scoped_refptr<FakeAudioProcessorForStats> processor) { | |
| 195 rtc::scoped_refptr<FakeAudioTrackForStats> audio_track_stats( | |
| 196 new rtc::RefCountedObject<FakeAudioTrackForStats>( | |
| 197 id, signal_level, processor)); | |
| 198 audio_track_stats->set_state(state); | |
| 199 return audio_track_stats; | |
| 200 } | |
| 201 | |
| 202 FakeAudioTrackForStats( | |
| 203 const std::string& id, | |
| 204 int signal_level, | |
| 205 rtc::scoped_refptr<FakeAudioProcessorForStats> processor) | |
| 206 : MediaStreamTrack<AudioTrackInterface>(id), | |
| 207 signal_level_(signal_level), | |
| 208 processor_(processor) { | |
| 209 } | |
| 210 | |
| 211 std::string kind() const override { | |
| 212 return MediaStreamTrackInterface::kAudioKind; | |
| 213 } | |
| 214 webrtc::AudioSourceInterface* GetSource() const override { return nullptr; } | |
| 215 void AddSink(webrtc::AudioTrackSinkInterface* sink) override {} | |
| 216 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override {} | |
| 217 bool GetSignalLevel(int* level) override { | |
| 218 *level = signal_level_; | |
| 219 return true; | |
| 220 } | |
| 221 rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() override { | |
| 222 return processor_; | |
| 223 } | |
| 224 | |
| 225 private: | |
| 226 int signal_level_; | |
| 227 rtc::scoped_refptr<FakeAudioProcessorForStats> processor_; | |
| 228 }; | |
| 229 | |
| 230 class FakeVideoTrackSourceForStats | |
| 231 : public rtc::RefCountedObject<VideoTrackSourceInterface> { | |
| 232 public: | |
| 233 FakeVideoTrackSourceForStats(VideoTrackSourceInterface::Stats stats) | |
| 234 : stats_(stats) { | |
| 235 } | |
| 236 | |
| 237 MediaSourceInterface::SourceState state() const override { | |
| 238 return MediaSourceInterface::kLive; | |
| 239 } | |
| 240 bool remote() const override { return false; } | |
| 241 void RegisterObserver(ObserverInterface* observer) override {} | |
| 242 void UnregisterObserver(ObserverInterface* observer) override {} | |
| 243 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, | |
| 244 const rtc::VideoSinkWants& wants) override {} | |
| 245 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override { | |
| 246 } | |
| 247 bool is_screencast() const override { return false; } | |
| 248 rtc::Optional<bool> needs_denoising() const override { | |
| 249 return rtc::Optional<bool>(); | |
| 250 } | |
| 251 bool GetStats(VideoTrackSourceInterface::Stats* stats) override { | |
| 252 *stats = stats_; | |
| 253 return true; | |
| 254 } | |
| 255 | |
| 256 private: | |
| 257 VideoTrackSourceInterface::Stats stats_; | |
| 258 }; | |
| 259 | |
| 260 class FakeVideoTrackForStats | |
| 261 : public MediaStreamTrack<VideoTrackInterface> { | |
| 262 public: | |
| 263 static rtc::scoped_refptr<FakeVideoTrackForStats> Create( | |
| 264 const std::string& id, | |
| 265 MediaStreamTrackInterface::TrackState state, | |
| 266 rtc::scoped_refptr<VideoTrackSourceInterface> source) { | |
| 267 rtc::scoped_refptr<FakeVideoTrackForStats> video_track( | |
| 268 new rtc::RefCountedObject<FakeVideoTrackForStats>(id, source)); | |
| 269 video_track->set_state(state); | |
| 270 return video_track; | |
| 271 } | |
| 272 | |
| 273 FakeVideoTrackForStats( | |
| 274 const std::string& id, | |
| 275 rtc::scoped_refptr<VideoTrackSourceInterface> source) | |
| 276 : MediaStreamTrack<VideoTrackInterface>(id), | |
| 277 source_(source) { | |
| 278 } | |
| 279 | |
| 280 std::string kind() const override { | |
| 281 return MediaStreamTrackInterface::kVideoKind; | |
| 282 } | |
| 283 VideoTrackSourceInterface* GetSource() const override { | |
| 284 return source_; | |
| 285 } | |
| 286 | |
| 287 private: | |
| 288 rtc::scoped_refptr<VideoTrackSourceInterface> source_; | |
| 289 }; | |
| 290 | |
| 291 class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { | |
| 292 public: | |
| 293 RTCStatsCollectorTestHelper() | |
| 294 : worker_thread_(rtc::Thread::Current()), | |
| 295 network_thread_(rtc::Thread::Current()), | |
| 296 media_engine_(new cricket::FakeMediaEngine()), | |
| 297 channel_manager_( | |
| 298 new cricket::ChannelManager(media_engine_, | |
| 299 worker_thread_, | |
| 300 network_thread_)), | |
| 301 media_controller_( | |
| 302 MediaControllerInterface::Create(cricket::MediaConfig(), | |
| 303 worker_thread_, | |
| 304 channel_manager_.get(), | |
| 305 &event_log_)), | |
| 306 session_(media_controller_.get()), | |
| 307 pc_() { | |
| 308 // Default return values for mocks. | |
| 309 EXPECT_CALL(pc_, local_streams()).WillRepeatedly(Return(nullptr)); | |
| 310 EXPECT_CALL(pc_, remote_streams()).WillRepeatedly(Return(nullptr)); | |
| 311 EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_)); | |
| 312 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly( | |
| 313 ReturnRef(data_channels_)); | |
| 314 EXPECT_CALL(session_, video_channel()).WillRepeatedly(ReturnNull()); | |
| 315 EXPECT_CALL(session_, voice_channel()).WillRepeatedly(ReturnNull()); | |
| 316 EXPECT_CALL(session_, GetTransportStats(_)).WillRepeatedly(Return(false)); | |
| 317 EXPECT_CALL(session_, GetLocalCertificate(_, _)).WillRepeatedly( | |
| 318 Return(false)); | |
| 319 EXPECT_CALL(session_, GetRemoteSSLCertificate_ReturnsRawPointer(_)) | |
| 320 .WillRepeatedly(Return(nullptr)); | |
| 321 } | |
| 322 | |
| 323 rtc::ScopedFakeClock& fake_clock() { return fake_clock_; } | |
| 324 rtc::Thread* worker_thread() { return worker_thread_; } | |
| 325 rtc::Thread* network_thread() { return network_thread_; } | |
| 326 cricket::FakeMediaEngine* media_engine() { return media_engine_; } | |
| 327 MockWebRtcSession& session() { return session_; } | |
| 328 MockPeerConnection& pc() { return pc_; } | |
| 329 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() { | |
| 330 return data_channels_; | |
| 331 } | |
| 332 | |
| 333 // SetSessionDescriptionObserver overrides. | |
| 334 void OnSuccess() override {} | |
| 335 void OnFailure(const std::string& error) override { | |
| 336 RTC_NOTREACHED() << error; | |
| 337 } | |
| 338 | |
| 339 private: | |
| 340 rtc::ScopedFakeClock fake_clock_; | |
| 341 RtcEventLogNullImpl event_log_; | |
| 342 rtc::Thread* const worker_thread_; | |
| 343 rtc::Thread* const network_thread_; | |
| 344 cricket::FakeMediaEngine* media_engine_; | |
| 345 std::unique_ptr<cricket::ChannelManager> channel_manager_; | |
| 346 std::unique_ptr<MediaControllerInterface> media_controller_; | |
| 347 MockWebRtcSession session_; | |
| 348 MockPeerConnection pc_; | |
| 349 | |
| 350 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_; | |
| 351 }; | |
| 352 | |
| 353 class RTCTestStats : public RTCStats { | |
| 354 public: | |
| 355 WEBRTC_RTCSTATS_DECL(); | |
| 356 | |
| 357 RTCTestStats(const std::string& id, int64_t timestamp_us) | |
| 358 : RTCStats(id, timestamp_us), | |
| 359 dummy_stat("dummyStat") {} | |
| 360 | |
| 361 RTCStatsMember<int32_t> dummy_stat; | |
| 362 }; | |
| 363 | |
| 364 WEBRTC_RTCSTATS_IMPL(RTCTestStats, RTCStats, "test-stats", | |
| 365 &dummy_stat); | |
| 366 | |
| 367 // Overrides the stats collection to verify thread usage and that the resulting | |
| 368 // partial reports are merged. | |
| 369 class FakeRTCStatsCollector : public RTCStatsCollector, | |
| 370 public RTCStatsCollectorCallback { | |
| 371 public: | |
| 372 static rtc::scoped_refptr<FakeRTCStatsCollector> Create( | |
| 373 PeerConnection* pc, | |
| 374 int64_t cache_lifetime_us) { | |
| 375 return rtc::scoped_refptr<FakeRTCStatsCollector>( | |
| 376 new rtc::RefCountedObject<FakeRTCStatsCollector>( | |
| 377 pc, cache_lifetime_us)); | |
| 378 } | |
| 379 | |
| 380 // RTCStatsCollectorCallback implementation. | |
| 381 void OnStatsDelivered( | |
| 382 const rtc::scoped_refptr<const RTCStatsReport>& report) override { | |
| 383 EXPECT_TRUE(signaling_thread_->IsCurrent()); | |
| 384 rtc::CritScope cs(&lock_); | |
| 385 delivered_report_ = report; | |
| 386 } | |
| 387 | |
| 388 void VerifyThreadUsageAndResultsMerging() { | |
| 389 GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback>(this)); | |
| 390 EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs); | |
| 391 } | |
| 392 | |
| 393 bool HasVerifiedResults() { | |
| 394 EXPECT_TRUE(signaling_thread_->IsCurrent()); | |
| 395 rtc::CritScope cs(&lock_); | |
| 396 if (!delivered_report_) | |
| 397 return false; | |
| 398 EXPECT_EQ(produced_on_signaling_thread_, 1); | |
| 399 EXPECT_EQ(produced_on_worker_thread_, 1); | |
| 400 EXPECT_EQ(produced_on_network_thread_, 1); | |
| 401 | |
| 402 EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats")); | |
| 403 EXPECT_TRUE(delivered_report_->Get("WorkerThreadStats")); | |
| 404 EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats")); | |
| 405 | |
| 406 produced_on_signaling_thread_ = 0; | |
| 407 produced_on_worker_thread_ = 0; | |
| 408 produced_on_network_thread_ = 0; | |
| 409 delivered_report_ = nullptr; | |
| 410 return true; | |
| 411 } | |
| 412 | |
| 413 protected: | |
| 414 FakeRTCStatsCollector( | |
| 415 PeerConnection* pc, | |
| 416 int64_t cache_lifetime) | |
| 417 : RTCStatsCollector(pc, cache_lifetime), | |
| 418 signaling_thread_(pc->session()->signaling_thread()), | |
| 419 worker_thread_(pc->session()->worker_thread()), | |
| 420 network_thread_(pc->session()->network_thread()) { | |
| 421 } | |
| 422 | |
| 423 void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override { | |
| 424 EXPECT_TRUE(signaling_thread_->IsCurrent()); | |
| 425 { | |
| 426 rtc::CritScope cs(&lock_); | |
| 427 EXPECT_FALSE(delivered_report_); | |
| 428 ++produced_on_signaling_thread_; | |
| 429 } | |
| 430 | |
| 431 rtc::scoped_refptr<RTCStatsReport> signaling_report = | |
| 432 RTCStatsReport::Create(0); | |
| 433 signaling_report->AddStats(std::unique_ptr<const RTCStats>( | |
| 434 new RTCTestStats("SignalingThreadStats", timestamp_us))); | |
| 435 AddPartialResults(signaling_report); | |
| 436 } | |
| 437 void ProducePartialResultsOnWorkerThread(int64_t timestamp_us) override { | |
| 438 EXPECT_TRUE(worker_thread_->IsCurrent()); | |
| 439 { | |
| 440 rtc::CritScope cs(&lock_); | |
| 441 EXPECT_FALSE(delivered_report_); | |
| 442 ++produced_on_worker_thread_; | |
| 443 } | |
| 444 | |
| 445 rtc::scoped_refptr<RTCStatsReport> worker_report = | |
| 446 RTCStatsReport::Create(0); | |
| 447 worker_report->AddStats(std::unique_ptr<const RTCStats>( | |
| 448 new RTCTestStats("WorkerThreadStats", timestamp_us))); | |
| 449 AddPartialResults(worker_report); | |
| 450 } | |
| 451 void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override { | |
| 452 EXPECT_TRUE(network_thread_->IsCurrent()); | |
| 453 { | |
| 454 rtc::CritScope cs(&lock_); | |
| 455 EXPECT_FALSE(delivered_report_); | |
| 456 ++produced_on_network_thread_; | |
| 457 } | |
| 458 | |
| 459 rtc::scoped_refptr<RTCStatsReport> network_report = | |
| 460 RTCStatsReport::Create(0); | |
| 461 network_report->AddStats(std::unique_ptr<const RTCStats>( | |
| 462 new RTCTestStats("NetworkThreadStats", timestamp_us))); | |
| 463 AddPartialResults(network_report); | |
| 464 } | |
| 465 | |
| 466 private: | |
| 467 rtc::Thread* const signaling_thread_; | |
| 468 rtc::Thread* const worker_thread_; | |
| 469 rtc::Thread* const network_thread_; | |
| 470 | |
| 471 rtc::CriticalSection lock_; | |
| 472 rtc::scoped_refptr<const RTCStatsReport> delivered_report_; | |
| 473 int produced_on_signaling_thread_ = 0; | |
| 474 int produced_on_worker_thread_ = 0; | |
| 475 int produced_on_network_thread_ = 0; | |
| 476 }; | |
| 477 | |
| 478 class RTCStatsCollectorTest : public testing::Test { | |
| 479 public: | |
| 480 RTCStatsCollectorTest() | |
| 481 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()), | |
| 482 collector_(RTCStatsCollector::Create( | |
| 483 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { | |
| 484 } | |
| 485 | |
| 486 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() { | |
| 487 rtc::scoped_refptr<RTCStatsObtainer> callback = RTCStatsObtainer::Create(); | |
| 488 collector_->GetStatsReport(callback); | |
| 489 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); | |
| 490 int64_t after = rtc::TimeUTCMicros(); | |
| 491 for (const RTCStats& stats : *callback->report()) { | |
| 492 EXPECT_LE(stats.timestamp_us(), after); | |
| 493 } | |
| 494 return callback->report(); | |
| 495 } | |
| 496 | |
| 497 const RTCIceCandidateStats* ExpectReportContainsCandidate( | |
| 498 const rtc::scoped_refptr<const RTCStatsReport>& report, | |
| 499 const cricket::Candidate& candidate, | |
| 500 bool is_local) { | |
| 501 const RTCStats* stats = report->Get("RTCIceCandidate_" + candidate.id()); | |
| 502 EXPECT_TRUE(stats); | |
| 503 const RTCIceCandidateStats* candidate_stats; | |
| 504 if (is_local) | |
| 505 candidate_stats = &stats->cast_to<RTCLocalIceCandidateStats>(); | |
| 506 else | |
| 507 candidate_stats = &stats->cast_to<RTCRemoteIceCandidateStats>(); | |
| 508 EXPECT_EQ(*candidate_stats->ip, candidate.address().ipaddr().ToString()); | |
| 509 EXPECT_EQ(*candidate_stats->port, | |
| 510 static_cast<int32_t>(candidate.address().port())); | |
| 511 EXPECT_EQ(*candidate_stats->protocol, candidate.protocol()); | |
| 512 EXPECT_EQ(*candidate_stats->candidate_type, | |
| 513 CandidateTypeToRTCIceCandidateTypeForTesting(candidate.type())); | |
| 514 EXPECT_EQ(*candidate_stats->priority, | |
| 515 static_cast<int32_t>(candidate.priority())); | |
| 516 // TODO(hbos): Define candidate_stats->url. crbug.com/632723 | |
| 517 EXPECT_FALSE(candidate_stats->url.is_defined()); | |
| 518 return candidate_stats; | |
| 519 } | |
| 520 | |
| 521 void ExpectReportContainsCertificateInfo( | |
| 522 const rtc::scoped_refptr<const RTCStatsReport>& report, | |
| 523 const CertificateInfo& cert_info) { | |
| 524 for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) { | |
| 525 const RTCStats* stats = report->Get( | |
| 526 "RTCCertificate_" + cert_info.fingerprints[i]); | |
| 527 ASSERT_TRUE(stats); | |
| 528 const RTCCertificateStats& cert_stats = | |
| 529 stats->cast_to<const RTCCertificateStats>(); | |
| 530 EXPECT_EQ(*cert_stats.fingerprint, cert_info.fingerprints[i]); | |
| 531 EXPECT_EQ(*cert_stats.fingerprint_algorithm, "sha-1"); | |
| 532 EXPECT_EQ(*cert_stats.base64_certificate, cert_info.pems[i]); | |
| 533 if (i + 1 < cert_info.fingerprints.size()) { | |
| 534 EXPECT_EQ(*cert_stats.issuer_certificate_id, | |
| 535 "RTCCertificate_" + cert_info.fingerprints[i + 1]); | |
| 536 } else { | |
| 537 EXPECT_FALSE(cert_stats.issuer_certificate_id.is_defined()); | |
| 538 } | |
| 539 } | |
| 540 } | |
| 541 | |
| 542 void ExpectReportContainsDataChannel( | |
| 543 const rtc::scoped_refptr<const RTCStatsReport>& report, | |
| 544 const DataChannel& data_channel) { | |
| 545 const RTCStats* stats = report->Get("RTCDataChannel_" + | |
| 546 rtc::ToString<>(data_channel.id())); | |
| 547 EXPECT_TRUE(stats); | |
| 548 const RTCDataChannelStats& data_channel_stats = | |
| 549 stats->cast_to<const RTCDataChannelStats>(); | |
| 550 EXPECT_EQ(*data_channel_stats.label, data_channel.label()); | |
| 551 EXPECT_EQ(*data_channel_stats.protocol, data_channel.protocol()); | |
| 552 EXPECT_EQ(*data_channel_stats.datachannelid, data_channel.id()); | |
| 553 EXPECT_EQ(*data_channel_stats.state, | |
| 554 DataStateToRTCDataChannelStateForTesting(data_channel.state())); | |
| 555 EXPECT_EQ(*data_channel_stats.messages_sent, data_channel.messages_sent()); | |
| 556 EXPECT_EQ(*data_channel_stats.bytes_sent, data_channel.bytes_sent()); | |
| 557 EXPECT_EQ(*data_channel_stats.messages_received, | |
| 558 data_channel.messages_received()); | |
| 559 EXPECT_EQ(*data_channel_stats.bytes_received, | |
| 560 data_channel.bytes_received()); | |
| 561 } | |
| 562 | |
| 563 protected: | |
| 564 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_; | |
| 565 rtc::scoped_refptr<RTCStatsCollector> collector_; | |
| 566 }; | |
| 567 | |
| 568 TEST_F(RTCStatsCollectorTest, SingleCallback) { | |
| 569 rtc::scoped_refptr<const RTCStatsReport> result; | |
| 570 collector_->GetStatsReport(RTCStatsObtainer::Create(&result)); | |
| 571 EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs); | |
| 572 } | |
| 573 | |
| 574 TEST_F(RTCStatsCollectorTest, MultipleCallbacks) { | |
| 575 rtc::scoped_refptr<const RTCStatsReport> a; | |
| 576 rtc::scoped_refptr<const RTCStatsReport> b; | |
| 577 rtc::scoped_refptr<const RTCStatsReport> c; | |
| 578 collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); | |
| 579 collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); | |
| 580 collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); | |
| 581 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); | |
| 582 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); | |
| 583 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); | |
| 584 EXPECT_EQ(a.get(), b.get()); | |
| 585 EXPECT_EQ(b.get(), c.get()); | |
| 586 } | |
| 587 | |
| 588 TEST_F(RTCStatsCollectorTest, CachedStatsReports) { | |
| 589 // Caching should ensure |a| and |b| are the same report. | |
| 590 rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport(); | |
| 591 rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport(); | |
| 592 EXPECT_EQ(a.get(), b.get()); | |
| 593 // Invalidate cache by clearing it. | |
| 594 collector_->ClearCachedStatsReport(); | |
| 595 rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport(); | |
| 596 EXPECT_NE(b.get(), c.get()); | |
| 597 // Invalidate cache by advancing time. | |
| 598 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); | |
| 599 rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport(); | |
| 600 EXPECT_TRUE(d); | |
| 601 EXPECT_NE(c.get(), d.get()); | |
| 602 } | |
| 603 | |
| 604 TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { | |
| 605 rtc::scoped_refptr<const RTCStatsReport> a; | |
| 606 rtc::scoped_refptr<const RTCStatsReport> b; | |
| 607 rtc::scoped_refptr<const RTCStatsReport> c; | |
| 608 collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); | |
| 609 collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); | |
| 610 // Cache is invalidated after 50 ms. | |
| 611 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); | |
| 612 collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); | |
| 613 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); | |
| 614 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); | |
| 615 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); | |
| 616 EXPECT_EQ(a.get(), b.get()); | |
| 617 // The act of doing |AdvanceTime| processes all messages. If this was not the | |
| 618 // case we might not require |c| to be fresher than |b|. | |
| 619 EXPECT_NE(c.get(), b.get()); | |
| 620 } | |
| 621 | |
| 622 TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) { | |
| 623 std::unique_ptr<CertificateInfo> local_certinfo = | |
| 624 CreateFakeCertificateAndInfoFromDers( | |
| 625 std::vector<std::string>({ "(local) single certificate" })); | |
| 626 std::unique_ptr<CertificateInfo> remote_certinfo = | |
| 627 CreateFakeCertificateAndInfoFromDers( | |
| 628 std::vector<std::string>({ "(remote) single certificate" })); | |
| 629 | |
| 630 // Mock the session to return the local and remote certificates. | |
| 631 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
| 632 [this](SessionStats* stats) { | |
| 633 stats->transport_stats["transport"].transport_name = "transport"; | |
| 634 return true; | |
| 635 })); | |
| 636 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( | |
| 637 Invoke([this, &local_certinfo](const std::string& transport_name, | |
| 638 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { | |
| 639 if (transport_name == "transport") { | |
| 640 *certificate = local_certinfo->certificate; | |
| 641 return true; | |
| 642 } | |
| 643 return false; | |
| 644 })); | |
| 645 EXPECT_CALL(test_->session(), | |
| 646 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( | |
| 647 [this, &remote_certinfo](const std::string& transport_name) { | |
| 648 if (transport_name == "transport") | |
| 649 return remote_certinfo->certificate->ssl_certificate().GetReference(); | |
| 650 return static_cast<rtc::SSLCertificate*>(nullptr); | |
| 651 })); | |
| 652 | |
| 653 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 654 ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); | |
| 655 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); | |
| 656 } | |
| 657 | |
| 658 TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) { | |
| 659 MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); | |
| 660 cricket::VoiceChannel voice_channel( | |
| 661 test_->worker_thread(), test_->network_thread(), test_->media_engine(), | |
| 662 voice_media_channel, nullptr, "VoiceContentName", kDefaultRtcpEnabled, | |
| 663 kDefaultSrtpRequired); | |
| 664 | |
| 665 MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); | |
| 666 cricket::VideoChannel video_channel( | |
| 667 test_->worker_thread(), test_->network_thread(), video_media_channel, | |
| 668 nullptr, "VideoContentName", kDefaultRtcpEnabled, kDefaultSrtpRequired); | |
| 669 | |
| 670 // Audio | |
| 671 cricket::VoiceMediaInfo voice_media_info; | |
| 672 | |
| 673 RtpCodecParameters inbound_audio_codec; | |
| 674 inbound_audio_codec.payload_type = 1; | |
| 675 inbound_audio_codec.mime_type = "opus"; | |
| 676 inbound_audio_codec.clock_rate = 1337; | |
| 677 voice_media_info.receive_codecs.insert( | |
| 678 std::make_pair(inbound_audio_codec.payload_type, inbound_audio_codec)); | |
| 679 | |
| 680 RtpCodecParameters outbound_audio_codec; | |
| 681 outbound_audio_codec.payload_type = 2; | |
| 682 outbound_audio_codec.mime_type = "isac"; | |
| 683 outbound_audio_codec.clock_rate = 1338; | |
| 684 voice_media_info.send_codecs.insert( | |
| 685 std::make_pair(outbound_audio_codec.payload_type, outbound_audio_codec)); | |
| 686 | |
| 687 EXPECT_CALL(*voice_media_channel, GetStats(_)) | |
| 688 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); | |
| 689 | |
| 690 // Video | |
| 691 cricket::VideoMediaInfo video_media_info; | |
| 692 | |
| 693 RtpCodecParameters inbound_video_codec; | |
| 694 inbound_video_codec.payload_type = 3; | |
| 695 inbound_video_codec.mime_type = "H264"; | |
| 696 inbound_video_codec.clock_rate = 1339; | |
| 697 video_media_info.receive_codecs.insert( | |
| 698 std::make_pair(inbound_video_codec.payload_type, inbound_video_codec)); | |
| 699 | |
| 700 RtpCodecParameters outbound_video_codec; | |
| 701 outbound_video_codec.payload_type = 4; | |
| 702 outbound_video_codec.mime_type = "VP8"; | |
| 703 outbound_video_codec.clock_rate = 1340; | |
| 704 video_media_info.send_codecs.insert( | |
| 705 std::make_pair(outbound_video_codec.payload_type, outbound_video_codec)); | |
| 706 | |
| 707 EXPECT_CALL(*video_media_channel, GetStats(_)) | |
| 708 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); | |
| 709 | |
| 710 SessionStats session_stats; | |
| 711 session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; | |
| 712 session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; | |
| 713 session_stats.transport_stats["TransportName"].transport_name = | |
| 714 "TransportName"; | |
| 715 | |
| 716 EXPECT_CALL(test_->session(), GetTransportStats(_)) | |
| 717 .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); | |
| 718 EXPECT_CALL(test_->session(), voice_channel()) | |
| 719 .WillRepeatedly(Return(&voice_channel)); | |
| 720 EXPECT_CALL(test_->session(), video_channel()) | |
| 721 .WillRepeatedly(Return(&video_channel)); | |
| 722 | |
| 723 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 724 | |
| 725 RTCCodecStats expected_inbound_audio_codec( | |
| 726 "RTCCodec_InboundAudio_1", report->timestamp_us()); | |
| 727 expected_inbound_audio_codec.payload_type = 1; | |
| 728 expected_inbound_audio_codec.codec = "audio/opus"; | |
| 729 expected_inbound_audio_codec.clock_rate = 1337; | |
| 730 | |
| 731 RTCCodecStats expected_outbound_audio_codec( | |
| 732 "RTCCodec_OutboundAudio_2", report->timestamp_us()); | |
| 733 expected_outbound_audio_codec.payload_type = 2; | |
| 734 expected_outbound_audio_codec.codec = "audio/isac"; | |
| 735 expected_outbound_audio_codec.clock_rate = 1338; | |
| 736 | |
| 737 RTCCodecStats expected_inbound_video_codec( | |
| 738 "RTCCodec_InboundVideo_3", report->timestamp_us()); | |
| 739 expected_inbound_video_codec.payload_type = 3; | |
| 740 expected_inbound_video_codec.codec = "video/H264"; | |
| 741 expected_inbound_video_codec.clock_rate = 1339; | |
| 742 | |
| 743 RTCCodecStats expected_outbound_video_codec( | |
| 744 "RTCCodec_OutboundVideo_4", report->timestamp_us()); | |
| 745 expected_outbound_video_codec.payload_type = 4; | |
| 746 expected_outbound_video_codec.codec = "video/VP8"; | |
| 747 expected_outbound_video_codec.clock_rate = 1340; | |
| 748 | |
| 749 ASSERT(report->Get(expected_inbound_audio_codec.id())); | |
| 750 EXPECT_EQ(expected_inbound_audio_codec, | |
| 751 report->Get(expected_inbound_audio_codec.id())->cast_to< | |
| 752 RTCCodecStats>()); | |
| 753 | |
| 754 ASSERT(report->Get(expected_outbound_audio_codec.id())); | |
| 755 EXPECT_EQ(expected_outbound_audio_codec, | |
| 756 report->Get(expected_outbound_audio_codec.id())->cast_to< | |
| 757 RTCCodecStats>()); | |
| 758 | |
| 759 ASSERT(report->Get(expected_inbound_video_codec.id())); | |
| 760 EXPECT_EQ(expected_inbound_video_codec, | |
| 761 report->Get(expected_inbound_video_codec.id())->cast_to< | |
| 762 RTCCodecStats>()); | |
| 763 | |
| 764 ASSERT(report->Get(expected_outbound_video_codec.id())); | |
| 765 EXPECT_EQ(expected_outbound_video_codec, | |
| 766 report->Get(expected_outbound_video_codec.id())->cast_to< | |
| 767 RTCCodecStats>()); | |
| 768 } | |
| 769 | |
| 770 TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) { | |
| 771 std::unique_ptr<CertificateInfo> audio_local_certinfo = | |
| 772 CreateFakeCertificateAndInfoFromDers( | |
| 773 std::vector<std::string>({ "(local) audio" })); | |
| 774 audio_local_certinfo = CreateFakeCertificateAndInfoFromDers( | |
| 775 audio_local_certinfo->ders); | |
| 776 std::unique_ptr<CertificateInfo> audio_remote_certinfo = | |
| 777 CreateFakeCertificateAndInfoFromDers( | |
| 778 std::vector<std::string>({ "(remote) audio" })); | |
| 779 audio_remote_certinfo = CreateFakeCertificateAndInfoFromDers( | |
| 780 audio_remote_certinfo->ders); | |
| 781 | |
| 782 std::unique_ptr<CertificateInfo> video_local_certinfo = | |
| 783 CreateFakeCertificateAndInfoFromDers( | |
| 784 std::vector<std::string>({ "(local) video" })); | |
| 785 video_local_certinfo = CreateFakeCertificateAndInfoFromDers( | |
| 786 video_local_certinfo->ders); | |
| 787 std::unique_ptr<CertificateInfo> video_remote_certinfo = | |
| 788 CreateFakeCertificateAndInfoFromDers( | |
| 789 std::vector<std::string>({ "(remote) video" })); | |
| 790 video_remote_certinfo = CreateFakeCertificateAndInfoFromDers( | |
| 791 video_remote_certinfo->ders); | |
| 792 | |
| 793 // Mock the session to return the local and remote certificates. | |
| 794 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
| 795 [this](SessionStats* stats) { | |
| 796 stats->transport_stats["audio"].transport_name = "audio"; | |
| 797 stats->transport_stats["video"].transport_name = "video"; | |
| 798 return true; | |
| 799 })); | |
| 800 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( | |
| 801 Invoke([this, &audio_local_certinfo, &video_local_certinfo]( | |
| 802 const std::string& transport_name, | |
| 803 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { | |
| 804 if (transport_name == "audio") { | |
| 805 *certificate = audio_local_certinfo->certificate; | |
| 806 return true; | |
| 807 } | |
| 808 if (transport_name == "video") { | |
| 809 *certificate = video_local_certinfo->certificate; | |
| 810 return true; | |
| 811 } | |
| 812 return false; | |
| 813 })); | |
| 814 EXPECT_CALL(test_->session(), | |
| 815 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( | |
| 816 [this, &audio_remote_certinfo, &video_remote_certinfo]( | |
| 817 const std::string& transport_name) { | |
| 818 if (transport_name == "audio") { | |
| 819 return audio_remote_certinfo->certificate->ssl_certificate() | |
| 820 .GetReference(); | |
| 821 } | |
| 822 if (transport_name == "video") { | |
| 823 return video_remote_certinfo->certificate->ssl_certificate() | |
| 824 .GetReference(); | |
| 825 } | |
| 826 return static_cast<rtc::SSLCertificate*>(nullptr); | |
| 827 })); | |
| 828 | |
| 829 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 830 ExpectReportContainsCertificateInfo(report, *audio_local_certinfo.get()); | |
| 831 ExpectReportContainsCertificateInfo(report, *audio_remote_certinfo.get()); | |
| 832 ExpectReportContainsCertificateInfo(report, *video_local_certinfo.get()); | |
| 833 ExpectReportContainsCertificateInfo(report, *video_remote_certinfo.get()); | |
| 834 } | |
| 835 | |
| 836 TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsChain) { | |
| 837 std::vector<std::string> local_ders; | |
| 838 local_ders.push_back("(local) this"); | |
| 839 local_ders.push_back("(local) is"); | |
| 840 local_ders.push_back("(local) a"); | |
| 841 local_ders.push_back("(local) chain"); | |
| 842 std::unique_ptr<CertificateInfo> local_certinfo = | |
| 843 CreateFakeCertificateAndInfoFromDers(local_ders); | |
| 844 std::vector<std::string> remote_ders; | |
| 845 remote_ders.push_back("(remote) this"); | |
| 846 remote_ders.push_back("(remote) is"); | |
| 847 remote_ders.push_back("(remote) another"); | |
| 848 remote_ders.push_back("(remote) chain"); | |
| 849 std::unique_ptr<CertificateInfo> remote_certinfo = | |
| 850 CreateFakeCertificateAndInfoFromDers(remote_ders); | |
| 851 | |
| 852 // Mock the session to return the local and remote certificates. | |
| 853 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
| 854 [this](SessionStats* stats) { | |
| 855 stats->transport_stats["transport"].transport_name = "transport"; | |
| 856 return true; | |
| 857 })); | |
| 858 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( | |
| 859 Invoke([this, &local_certinfo](const std::string& transport_name, | |
| 860 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { | |
| 861 if (transport_name == "transport") { | |
| 862 *certificate = local_certinfo->certificate; | |
| 863 return true; | |
| 864 } | |
| 865 return false; | |
| 866 })); | |
| 867 EXPECT_CALL(test_->session(), | |
| 868 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( | |
| 869 [this, &remote_certinfo](const std::string& transport_name) { | |
| 870 if (transport_name == "transport") | |
| 871 return remote_certinfo->certificate->ssl_certificate().GetReference(); | |
| 872 return static_cast<rtc::SSLCertificate*>(nullptr); | |
| 873 })); | |
| 874 | |
| 875 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 876 ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); | |
| 877 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); | |
| 878 } | |
| 879 | |
| 880 TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { | |
| 881 test_->data_channels().push_back( | |
| 882 new MockDataChannel(0, DataChannelInterface::kConnecting)); | |
| 883 test_->data_channels().push_back( | |
| 884 new MockDataChannel(1, DataChannelInterface::kOpen)); | |
| 885 test_->data_channels().push_back( | |
| 886 new MockDataChannel(2, DataChannelInterface::kClosing)); | |
| 887 test_->data_channels().push_back( | |
| 888 new MockDataChannel(3, DataChannelInterface::kClosed)); | |
| 889 | |
| 890 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 891 ExpectReportContainsDataChannel(report, *test_->data_channels()[0]); | |
| 892 ExpectReportContainsDataChannel(report, *test_->data_channels()[1]); | |
| 893 ExpectReportContainsDataChannel(report, *test_->data_channels()[2]); | |
| 894 ExpectReportContainsDataChannel(report, *test_->data_channels()[3]); | |
| 895 | |
| 896 test_->data_channels().clear(); | |
| 897 test_->data_channels().push_back( | |
| 898 new MockDataChannel(0, DataChannelInterface::kConnecting, | |
| 899 1, 2, 3, 4)); | |
| 900 test_->data_channels().push_back( | |
| 901 new MockDataChannel(1, DataChannelInterface::kOpen, | |
| 902 5, 6, 7, 8)); | |
| 903 test_->data_channels().push_back( | |
| 904 new MockDataChannel(2, DataChannelInterface::kClosing, | |
| 905 9, 10, 11, 12)); | |
| 906 test_->data_channels().push_back( | |
| 907 new MockDataChannel(3, DataChannelInterface::kClosed, | |
| 908 13, 14, 15, 16)); | |
| 909 | |
| 910 collector_->ClearCachedStatsReport(); | |
| 911 report = GetStatsReport(); | |
| 912 ExpectReportContainsDataChannel(report, *test_->data_channels()[0]); | |
| 913 ExpectReportContainsDataChannel(report, *test_->data_channels()[1]); | |
| 914 ExpectReportContainsDataChannel(report, *test_->data_channels()[2]); | |
| 915 ExpectReportContainsDataChannel(report, *test_->data_channels()[3]); | |
| 916 } | |
| 917 | |
| 918 TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { | |
| 919 // Candidates in the first transport stats. | |
| 920 std::unique_ptr<cricket::Candidate> a_local_host = CreateFakeCandidate( | |
| 921 "1.2.3.4", 5, | |
| 922 "a_local_host's protocol", | |
| 923 cricket::LOCAL_PORT_TYPE, | |
| 924 0); | |
| 925 std::unique_ptr<cricket::Candidate> a_remote_srflx = CreateFakeCandidate( | |
| 926 "6.7.8.9", 10, | |
| 927 "remote_srflx's protocol", | |
| 928 cricket::STUN_PORT_TYPE, | |
| 929 1); | |
| 930 std::unique_ptr<cricket::Candidate> a_local_prflx = CreateFakeCandidate( | |
| 931 "11.12.13.14", 15, | |
| 932 "a_local_prflx's protocol", | |
| 933 cricket::PRFLX_PORT_TYPE, | |
| 934 2); | |
| 935 std::unique_ptr<cricket::Candidate> a_remote_relay = CreateFakeCandidate( | |
| 936 "16.17.18.19", 20, | |
| 937 "a_remote_relay's protocol", | |
| 938 cricket::RELAY_PORT_TYPE, | |
| 939 3); | |
| 940 // Candidates in the second transport stats. | |
| 941 std::unique_ptr<cricket::Candidate> b_local = CreateFakeCandidate( | |
| 942 "42.42.42.42", 42, | |
| 943 "b_local's protocol", | |
| 944 cricket::LOCAL_PORT_TYPE, | |
| 945 42); | |
| 946 std::unique_ptr<cricket::Candidate> b_remote = CreateFakeCandidate( | |
| 947 "42.42.42.42", 42, | |
| 948 "b_remote's protocol", | |
| 949 cricket::LOCAL_PORT_TYPE, | |
| 950 42); | |
| 951 | |
| 952 SessionStats session_stats; | |
| 953 | |
| 954 cricket::TransportChannelStats a_transport_channel_stats; | |
| 955 a_transport_channel_stats.connection_infos.push_back( | |
| 956 cricket::ConnectionInfo()); | |
| 957 a_transport_channel_stats.connection_infos[0].local_candidate = | |
| 958 *a_local_host.get(); | |
| 959 a_transport_channel_stats.connection_infos[0].remote_candidate = | |
| 960 *a_remote_srflx.get(); | |
| 961 a_transport_channel_stats.connection_infos.push_back( | |
| 962 cricket::ConnectionInfo()); | |
| 963 a_transport_channel_stats.connection_infos[1].local_candidate = | |
| 964 *a_local_prflx.get(); | |
| 965 a_transport_channel_stats.connection_infos[1].remote_candidate = | |
| 966 *a_remote_relay.get(); | |
| 967 session_stats.transport_stats["a"].channel_stats.push_back( | |
| 968 a_transport_channel_stats); | |
| 969 | |
| 970 cricket::TransportChannelStats b_transport_channel_stats; | |
| 971 b_transport_channel_stats.connection_infos.push_back( | |
| 972 cricket::ConnectionInfo()); | |
| 973 b_transport_channel_stats.connection_infos[0].local_candidate = | |
| 974 *b_local.get(); | |
| 975 b_transport_channel_stats.connection_infos[0].remote_candidate = | |
| 976 *b_remote.get(); | |
| 977 session_stats.transport_stats["b"].channel_stats.push_back( | |
| 978 b_transport_channel_stats); | |
| 979 | |
| 980 // Mock the session to return the desired candidates. | |
| 981 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
| 982 [this, &session_stats](SessionStats* stats) { | |
| 983 *stats = session_stats; | |
| 984 return true; | |
| 985 })); | |
| 986 | |
| 987 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 988 ExpectReportContainsCandidate(report, *a_local_host.get(), true); | |
| 989 ExpectReportContainsCandidate(report, *a_remote_srflx.get(), false); | |
| 990 ExpectReportContainsCandidate(report, *a_local_prflx.get(), true); | |
| 991 ExpectReportContainsCandidate(report, *a_remote_relay.get(), false); | |
| 992 ExpectReportContainsCandidate(report, *b_local.get(), true); | |
| 993 ExpectReportContainsCandidate(report, *b_remote.get(), false); | |
| 994 } | |
| 995 | |
| 996 TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { | |
| 997 std::unique_ptr<cricket::Candidate> local_candidate = CreateFakeCandidate( | |
| 998 "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42); | |
| 999 std::unique_ptr<cricket::Candidate> remote_candidate = CreateFakeCandidate( | |
| 1000 "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42); | |
| 1001 | |
| 1002 SessionStats session_stats; | |
| 1003 | |
| 1004 cricket::ConnectionInfo connection_info; | |
| 1005 connection_info.local_candidate = *local_candidate.get(); | |
| 1006 connection_info.remote_candidate = *remote_candidate.get(); | |
| 1007 connection_info.writable = true; | |
| 1008 connection_info.sent_total_bytes = 42; | |
| 1009 connection_info.recv_total_bytes = 1234; | |
| 1010 connection_info.rtt = 1337; | |
| 1011 connection_info.recv_ping_requests = 2020; | |
| 1012 connection_info.sent_ping_requests_total = 2020; | |
| 1013 connection_info.sent_ping_requests_before_first_response = 2000; | |
| 1014 connection_info.recv_ping_responses = 4321; | |
| 1015 connection_info.sent_ping_responses = 1000; | |
| 1016 | |
| 1017 cricket::TransportChannelStats transport_channel_stats; | |
| 1018 transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; | |
| 1019 transport_channel_stats.connection_infos.push_back(connection_info); | |
| 1020 session_stats.transport_stats["transport"].transport_name = "transport"; | |
| 1021 session_stats.transport_stats["transport"].channel_stats.push_back( | |
| 1022 transport_channel_stats); | |
| 1023 | |
| 1024 // Mock the session to return the desired candidates. | |
| 1025 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
| 1026 [this, &session_stats](SessionStats* stats) { | |
| 1027 *stats = session_stats; | |
| 1028 return true; | |
| 1029 })); | |
| 1030 | |
| 1031 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1032 | |
| 1033 RTCIceCandidatePairStats expected_pair("RTCIceCandidatePair_" + | |
| 1034 local_candidate->id() + "_" + | |
| 1035 remote_candidate->id(), | |
| 1036 report->timestamp_us()); | |
| 1037 expected_pair.transport_id = | |
| 1038 "RTCTransport_transport_" + | |
| 1039 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); | |
| 1040 expected_pair.local_candidate_id = "RTCIceCandidate_" + local_candidate->id(); | |
| 1041 expected_pair.remote_candidate_id = | |
| 1042 "RTCIceCandidate_" + remote_candidate->id(); | |
| 1043 expected_pair.writable = true; | |
| 1044 expected_pair.bytes_sent = 42; | |
| 1045 expected_pair.bytes_received = 1234; | |
| 1046 expected_pair.current_round_trip_time = 1.337; | |
| 1047 expected_pair.requests_received = 2020; | |
| 1048 expected_pair.requests_sent = 2000; | |
| 1049 expected_pair.responses_received = 4321; | |
| 1050 expected_pair.responses_sent = 1000; | |
| 1051 expected_pair.consent_requests_sent = (2020 - 2000); | |
| 1052 | |
| 1053 EXPECT_TRUE(report->Get(expected_pair.id())); | |
| 1054 EXPECT_EQ( | |
| 1055 expected_pair, | |
| 1056 report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>()); | |
| 1057 | |
| 1058 EXPECT_TRUE(report->Get(*expected_pair.local_candidate_id)); | |
| 1059 ExpectReportContainsCandidate(report, connection_info.local_candidate, true); | |
| 1060 EXPECT_TRUE(report->Get(*expected_pair.remote_candidate_id)); | |
| 1061 ExpectReportContainsCandidate(report, connection_info.remote_candidate, | |
| 1062 false); | |
| 1063 } | |
| 1064 | |
| 1065 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { | |
| 1066 { | |
| 1067 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1068 RTCPeerConnectionStats expected("RTCPeerConnection", | |
| 1069 report->timestamp_us()); | |
| 1070 expected.data_channels_opened = 0; | |
| 1071 expected.data_channels_closed = 0; | |
| 1072 EXPECT_TRUE(report->Get("RTCPeerConnection")); | |
| 1073 EXPECT_EQ(expected, | |
| 1074 report->Get("RTCPeerConnection")->cast_to< | |
| 1075 RTCPeerConnectionStats>()); | |
| 1076 } | |
| 1077 | |
| 1078 rtc::scoped_refptr<DataChannel> dummy_channel_a = DataChannel::Create( | |
| 1079 nullptr, cricket::DCT_NONE, "DummyChannelA", InternalDataChannelInit()); | |
| 1080 test_->pc().SignalDataChannelCreated(dummy_channel_a.get()); | |
| 1081 rtc::scoped_refptr<DataChannel> dummy_channel_b = DataChannel::Create( | |
| 1082 nullptr, cricket::DCT_NONE, "DummyChannelB", InternalDataChannelInit()); | |
| 1083 test_->pc().SignalDataChannelCreated(dummy_channel_b.get()); | |
| 1084 | |
| 1085 dummy_channel_a->SignalOpened(dummy_channel_a.get()); | |
| 1086 // Closing a channel that is not opened should not affect the counts. | |
| 1087 dummy_channel_b->SignalClosed(dummy_channel_b.get()); | |
| 1088 | |
| 1089 { | |
| 1090 collector_->ClearCachedStatsReport(); | |
| 1091 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1092 RTCPeerConnectionStats expected("RTCPeerConnection", | |
| 1093 report->timestamp_us()); | |
| 1094 expected.data_channels_opened = 1; | |
| 1095 expected.data_channels_closed = 0; | |
| 1096 EXPECT_TRUE(report->Get("RTCPeerConnection")); | |
| 1097 EXPECT_EQ(expected, | |
| 1098 report->Get("RTCPeerConnection")->cast_to< | |
| 1099 RTCPeerConnectionStats>()); | |
| 1100 } | |
| 1101 | |
| 1102 dummy_channel_b->SignalOpened(dummy_channel_b.get()); | |
| 1103 dummy_channel_b->SignalClosed(dummy_channel_b.get()); | |
| 1104 | |
| 1105 { | |
| 1106 collector_->ClearCachedStatsReport(); | |
| 1107 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1108 RTCPeerConnectionStats expected("RTCPeerConnection", | |
| 1109 report->timestamp_us()); | |
| 1110 expected.data_channels_opened = 2; | |
| 1111 expected.data_channels_closed = 1; | |
| 1112 EXPECT_TRUE(report->Get("RTCPeerConnection")); | |
| 1113 EXPECT_EQ(expected, | |
| 1114 report->Get("RTCPeerConnection")->cast_to< | |
| 1115 RTCPeerConnectionStats>()); | |
| 1116 } | |
| 1117 } | |
| 1118 | |
| 1119 TEST_F(RTCStatsCollectorTest, | |
| 1120 CollectRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio) { | |
| 1121 rtc::scoped_refptr<StreamCollection> local_streams = | |
| 1122 StreamCollection::Create(); | |
| 1123 rtc::scoped_refptr<StreamCollection> remote_streams = | |
| 1124 StreamCollection::Create(); | |
| 1125 EXPECT_CALL(test_->pc(), local_streams()) | |
| 1126 .WillRepeatedly(Return(local_streams)); | |
| 1127 EXPECT_CALL(test_->pc(), remote_streams()) | |
| 1128 .WillRepeatedly(Return(remote_streams)); | |
| 1129 | |
| 1130 rtc::scoped_refptr<MediaStream> local_stream = | |
| 1131 MediaStream::Create("LocalStreamLabel"); | |
| 1132 local_streams->AddStream(local_stream); | |
| 1133 rtc::scoped_refptr<MediaStream> remote_stream = | |
| 1134 MediaStream::Create("RemoteStreamLabel"); | |
| 1135 remote_streams->AddStream(remote_stream); | |
| 1136 | |
| 1137 // Local audio track | |
| 1138 AudioProcessorInterface::AudioProcessorStats local_audio_processor_stats; | |
| 1139 local_audio_processor_stats.echo_return_loss = 42; | |
| 1140 local_audio_processor_stats.echo_return_loss_enhancement = 52; | |
| 1141 rtc::scoped_refptr<FakeAudioTrackForStats> local_audio_track = | |
| 1142 FakeAudioTrackForStats::Create( | |
| 1143 "LocalAudioTrackID", | |
| 1144 MediaStreamTrackInterface::TrackState::kEnded, | |
| 1145 32767, | |
| 1146 new FakeAudioProcessorForStats(local_audio_processor_stats)); | |
| 1147 local_stream->AddTrack(local_audio_track); | |
| 1148 | |
| 1149 // Remote audio track | |
| 1150 AudioProcessorInterface::AudioProcessorStats remote_audio_processor_stats; | |
| 1151 remote_audio_processor_stats.echo_return_loss = 13; | |
| 1152 remote_audio_processor_stats.echo_return_loss_enhancement = 37; | |
| 1153 rtc::scoped_refptr<FakeAudioTrackForStats> remote_audio_track = | |
| 1154 FakeAudioTrackForStats::Create( | |
| 1155 "RemoteAudioTrackID", | |
| 1156 MediaStreamTrackInterface::TrackState::kLive, | |
| 1157 0, | |
| 1158 new FakeAudioProcessorForStats(remote_audio_processor_stats)); | |
| 1159 remote_stream->AddTrack(remote_audio_track); | |
| 1160 | |
| 1161 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1162 | |
| 1163 RTCMediaStreamStats expected_local_stream( | |
| 1164 "RTCMediaStream_LocalStreamLabel", report->timestamp_us()); | |
| 1165 expected_local_stream.stream_identifier = local_stream->label(); | |
| 1166 expected_local_stream.track_ids = std::vector<std::string>(); | |
| 1167 expected_local_stream.track_ids->push_back( | |
| 1168 "RTCMediaStreamTrack_LocalAudioTrackID"); | |
| 1169 EXPECT_TRUE(report->Get(expected_local_stream.id())); | |
| 1170 EXPECT_EQ(expected_local_stream, | |
| 1171 report->Get(expected_local_stream.id())->cast_to< | |
| 1172 RTCMediaStreamStats>()); | |
| 1173 | |
| 1174 RTCMediaStreamStats expected_remote_stream( | |
| 1175 "RTCMediaStream_RemoteStreamLabel", report->timestamp_us()); | |
| 1176 expected_remote_stream.stream_identifier = remote_stream->label(); | |
| 1177 expected_remote_stream.track_ids = std::vector<std::string>(); | |
| 1178 expected_remote_stream.track_ids->push_back( | |
| 1179 "RTCMediaStreamTrack_RemoteAudioTrackID"); | |
| 1180 EXPECT_TRUE(report->Get(expected_remote_stream.id())); | |
| 1181 EXPECT_EQ(expected_remote_stream, | |
| 1182 report->Get(expected_remote_stream.id())->cast_to< | |
| 1183 RTCMediaStreamStats>()); | |
| 1184 | |
| 1185 RTCMediaStreamTrackStats expected_local_audio_track( | |
| 1186 "RTCMediaStreamTrack_LocalAudioTrackID", report->timestamp_us()); | |
| 1187 expected_local_audio_track.track_identifier = local_audio_track->id(); | |
| 1188 expected_local_audio_track.remote_source = false; | |
| 1189 expected_local_audio_track.ended = true; | |
| 1190 expected_local_audio_track.detached = false; | |
| 1191 expected_local_audio_track.audio_level = 1.0; | |
| 1192 expected_local_audio_track.echo_return_loss = 42.0; | |
| 1193 expected_local_audio_track.echo_return_loss_enhancement = 52.0; | |
| 1194 EXPECT_TRUE(report->Get(expected_local_audio_track.id())); | |
| 1195 EXPECT_EQ(expected_local_audio_track, | |
| 1196 report->Get(expected_local_audio_track.id())->cast_to< | |
| 1197 RTCMediaStreamTrackStats>()); | |
| 1198 | |
| 1199 RTCMediaStreamTrackStats expected_remote_audio_track( | |
| 1200 "RTCMediaStreamTrack_RemoteAudioTrackID", report->timestamp_us()); | |
| 1201 expected_remote_audio_track.track_identifier = remote_audio_track->id(); | |
| 1202 expected_remote_audio_track.remote_source = true; | |
| 1203 expected_remote_audio_track.ended = false; | |
| 1204 expected_remote_audio_track.detached = false; | |
| 1205 expected_remote_audio_track.audio_level = 0.0; | |
| 1206 expected_remote_audio_track.echo_return_loss = 13.0; | |
| 1207 expected_remote_audio_track.echo_return_loss_enhancement = 37.0; | |
| 1208 EXPECT_TRUE(report->Get(expected_remote_audio_track.id())); | |
| 1209 EXPECT_EQ(expected_remote_audio_track, | |
| 1210 report->Get(expected_remote_audio_track.id())->cast_to< | |
| 1211 RTCMediaStreamTrackStats>()); | |
| 1212 } | |
| 1213 | |
| 1214 TEST_F(RTCStatsCollectorTest, | |
| 1215 CollectRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio_Defaults) { | |
| 1216 rtc::scoped_refptr<StreamCollection> local_streams = | |
| 1217 StreamCollection::Create(); | |
| 1218 EXPECT_CALL(test_->pc(), local_streams()) | |
| 1219 .WillRepeatedly(Return(local_streams)); | |
| 1220 | |
| 1221 rtc::scoped_refptr<MediaStream> local_stream = | |
| 1222 MediaStream::Create("LocalStreamLabel"); | |
| 1223 local_streams->AddStream(local_stream); | |
| 1224 | |
| 1225 // Local audio track | |
| 1226 AudioProcessorInterface::AudioProcessorStats local_audio_processor_stats; | |
| 1227 local_audio_processor_stats.echo_return_loss = -100; | |
| 1228 local_audio_processor_stats.echo_return_loss_enhancement = -100; | |
| 1229 rtc::scoped_refptr<FakeAudioTrackForStats> local_audio_track = | |
| 1230 FakeAudioTrackForStats::Create( | |
| 1231 "LocalAudioTrackID", | |
| 1232 MediaStreamTrackInterface::TrackState::kEnded, | |
| 1233 32767, | |
| 1234 new FakeAudioProcessorForStats(local_audio_processor_stats)); | |
| 1235 local_stream->AddTrack(local_audio_track); | |
| 1236 | |
| 1237 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1238 | |
| 1239 RTCMediaStreamTrackStats expected_local_audio_track( | |
| 1240 "RTCMediaStreamTrack_LocalAudioTrackID", report->timestamp_us()); | |
| 1241 expected_local_audio_track.track_identifier = local_audio_track->id(); | |
| 1242 expected_local_audio_track.remote_source = false; | |
| 1243 expected_local_audio_track.ended = true; | |
| 1244 expected_local_audio_track.detached = false; | |
| 1245 expected_local_audio_track.audio_level = 1.0; | |
| 1246 // Should be undefined: |expected_local_audio_track.echo_return_loss| and | |
| 1247 // |expected_local_audio_track.echo_return_loss_enhancement|. | |
| 1248 EXPECT_TRUE(report->Get(expected_local_audio_track.id())); | |
| 1249 EXPECT_EQ(expected_local_audio_track, | |
| 1250 report->Get(expected_local_audio_track.id())->cast_to< | |
| 1251 RTCMediaStreamTrackStats>()); | |
| 1252 } | |
| 1253 | |
| 1254 TEST_F(RTCStatsCollectorTest, | |
| 1255 CollectRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Video) { | |
| 1256 rtc::scoped_refptr<StreamCollection> local_streams = | |
| 1257 StreamCollection::Create(); | |
| 1258 rtc::scoped_refptr<StreamCollection> remote_streams = | |
| 1259 StreamCollection::Create(); | |
| 1260 EXPECT_CALL(test_->pc(), local_streams()) | |
| 1261 .WillRepeatedly(Return(local_streams)); | |
| 1262 EXPECT_CALL(test_->pc(), remote_streams()) | |
| 1263 .WillRepeatedly(Return(remote_streams)); | |
| 1264 | |
| 1265 rtc::scoped_refptr<MediaStream> local_stream = | |
| 1266 MediaStream::Create("LocalStreamLabel"); | |
| 1267 local_streams->AddStream(local_stream); | |
| 1268 rtc::scoped_refptr<MediaStream> remote_stream = | |
| 1269 MediaStream::Create("RemoteStreamLabel"); | |
| 1270 remote_streams->AddStream(remote_stream); | |
| 1271 | |
| 1272 // Local video track | |
| 1273 VideoTrackSourceInterface::Stats local_video_track_source_stats; | |
| 1274 local_video_track_source_stats.input_width = 1234; | |
| 1275 local_video_track_source_stats.input_height = 4321; | |
| 1276 rtc::scoped_refptr<FakeVideoTrackSourceForStats> local_video_track_source = | |
| 1277 new FakeVideoTrackSourceForStats(local_video_track_source_stats); | |
| 1278 rtc::scoped_refptr<FakeVideoTrackForStats> local_video_track = | |
| 1279 FakeVideoTrackForStats::Create( | |
| 1280 "LocalVideoTrackID", | |
| 1281 MediaStreamTrackInterface::TrackState::kLive, | |
| 1282 local_video_track_source); | |
| 1283 local_stream->AddTrack(local_video_track); | |
| 1284 | |
| 1285 // Remote video track | |
| 1286 VideoTrackSourceInterface::Stats remote_video_track_source_stats; | |
| 1287 remote_video_track_source_stats.input_width = 1234; | |
| 1288 remote_video_track_source_stats.input_height = 4321; | |
| 1289 rtc::scoped_refptr<FakeVideoTrackSourceForStats> remote_video_track_source = | |
| 1290 new FakeVideoTrackSourceForStats(remote_video_track_source_stats); | |
| 1291 rtc::scoped_refptr<FakeVideoTrackForStats> remote_video_track = | |
| 1292 FakeVideoTrackForStats::Create( | |
| 1293 "RemoteVideoTrackID", | |
| 1294 MediaStreamTrackInterface::TrackState::kEnded, | |
| 1295 remote_video_track_source); | |
| 1296 remote_stream->AddTrack(remote_video_track); | |
| 1297 | |
| 1298 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1299 | |
| 1300 RTCMediaStreamStats expected_local_stream( | |
| 1301 "RTCMediaStream_LocalStreamLabel", report->timestamp_us()); | |
| 1302 expected_local_stream.stream_identifier = local_stream->label(); | |
| 1303 expected_local_stream.track_ids = std::vector<std::string>(); | |
| 1304 expected_local_stream.track_ids->push_back( | |
| 1305 "RTCMediaStreamTrack_LocalVideoTrackID"); | |
| 1306 EXPECT_TRUE(report->Get(expected_local_stream.id())); | |
| 1307 EXPECT_EQ(expected_local_stream, | |
| 1308 report->Get(expected_local_stream.id())->cast_to< | |
| 1309 RTCMediaStreamStats>()); | |
| 1310 | |
| 1311 RTCMediaStreamStats expected_remote_stream( | |
| 1312 "RTCMediaStream_RemoteStreamLabel", report->timestamp_us()); | |
| 1313 expected_remote_stream.stream_identifier = remote_stream->label(); | |
| 1314 expected_remote_stream.track_ids = std::vector<std::string>(); | |
| 1315 expected_remote_stream.track_ids->push_back( | |
| 1316 "RTCMediaStreamTrack_RemoteVideoTrackID"); | |
| 1317 EXPECT_TRUE(report->Get(expected_remote_stream.id())); | |
| 1318 EXPECT_EQ(expected_remote_stream, | |
| 1319 report->Get(expected_remote_stream.id())->cast_to< | |
| 1320 RTCMediaStreamStats>()); | |
| 1321 | |
| 1322 RTCMediaStreamTrackStats expected_local_video_track( | |
| 1323 "RTCMediaStreamTrack_LocalVideoTrackID", report->timestamp_us()); | |
| 1324 expected_local_video_track.track_identifier = local_video_track->id(); | |
| 1325 expected_local_video_track.remote_source = false; | |
| 1326 expected_local_video_track.ended = false; | |
| 1327 expected_local_video_track.detached = false; | |
| 1328 expected_local_video_track.frame_width = 1234; | |
| 1329 expected_local_video_track.frame_height = 4321; | |
| 1330 EXPECT_TRUE(report->Get(expected_local_video_track.id())); | |
| 1331 EXPECT_EQ(expected_local_video_track, | |
| 1332 report->Get(expected_local_video_track.id())->cast_to< | |
| 1333 RTCMediaStreamTrackStats>()); | |
| 1334 | |
| 1335 RTCMediaStreamTrackStats expected_remote_video_track( | |
| 1336 "RTCMediaStreamTrack_RemoteVideoTrackID", report->timestamp_us()); | |
| 1337 expected_remote_video_track.track_identifier = remote_video_track->id(); | |
| 1338 expected_remote_video_track.remote_source = true; | |
| 1339 expected_remote_video_track.ended = true; | |
| 1340 expected_remote_video_track.detached = false; | |
| 1341 expected_remote_video_track.frame_width = 1234; | |
| 1342 expected_remote_video_track.frame_height = 4321; | |
| 1343 EXPECT_TRUE(report->Get(expected_remote_video_track.id())); | |
| 1344 EXPECT_EQ(expected_remote_video_track, | |
| 1345 report->Get(expected_remote_video_track.id())->cast_to< | |
| 1346 RTCMediaStreamTrackStats>()); | |
| 1347 } | |
| 1348 | |
| 1349 TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { | |
| 1350 MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); | |
| 1351 cricket::VoiceChannel voice_channel( | |
| 1352 test_->worker_thread(), test_->network_thread(), test_->media_engine(), | |
| 1353 voice_media_channel, nullptr, "VoiceContentName", kDefaultRtcpEnabled, | |
| 1354 kDefaultSrtpRequired); | |
| 1355 | |
| 1356 cricket::VoiceMediaInfo voice_media_info; | |
| 1357 | |
| 1358 voice_media_info.receivers.push_back(cricket::VoiceReceiverInfo()); | |
| 1359 voice_media_info.receivers[0].local_stats.push_back( | |
| 1360 cricket::SsrcReceiverInfo()); | |
| 1361 voice_media_info.receivers[0].local_stats[0].ssrc = 1; | |
| 1362 voice_media_info.receivers[0].packets_lost = 42; | |
| 1363 voice_media_info.receivers[0].packets_rcvd = 2; | |
| 1364 voice_media_info.receivers[0].bytes_rcvd = 3; | |
| 1365 voice_media_info.receivers[0].codec_payload_type = rtc::Optional<int>(42); | |
| 1366 voice_media_info.receivers[0].jitter_ms = 4500; | |
| 1367 voice_media_info.receivers[0].fraction_lost = 5.5f; | |
| 1368 | |
| 1369 RtpCodecParameters codec_parameters; | |
| 1370 codec_parameters.payload_type = 42; | |
| 1371 codec_parameters.mime_type = "dummy"; | |
| 1372 codec_parameters.clock_rate = 0; | |
| 1373 voice_media_info.receive_codecs.insert( | |
| 1374 std::make_pair(codec_parameters.payload_type, codec_parameters)); | |
| 1375 | |
| 1376 EXPECT_CALL(*voice_media_channel, GetStats(_)) | |
| 1377 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); | |
| 1378 | |
| 1379 SessionStats session_stats; | |
| 1380 session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; | |
| 1381 session_stats.transport_stats["TransportName"].transport_name = | |
| 1382 "TransportName"; | |
| 1383 | |
| 1384 // Make sure the associated |RTCTransportStats| is created. | |
| 1385 cricket::TransportChannelStats channel_stats; | |
| 1386 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; | |
| 1387 session_stats.transport_stats["TransportName"].channel_stats.push_back( | |
| 1388 channel_stats); | |
| 1389 | |
| 1390 EXPECT_CALL(test_->session(), GetTransportStats(_)) | |
| 1391 .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); | |
| 1392 EXPECT_CALL(test_->session(), voice_channel()) | |
| 1393 .WillRepeatedly(Return(&voice_channel)); | |
| 1394 | |
| 1395 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1396 | |
| 1397 RTCInboundRTPStreamStats expected_audio( | |
| 1398 "RTCInboundRTPAudioStream_1", report->timestamp_us()); | |
| 1399 expected_audio.ssrc = "1"; | |
| 1400 expected_audio.is_remote = false; | |
| 1401 expected_audio.media_type = "audio"; | |
| 1402 expected_audio.transport_id = "RTCTransport_TransportName_" + | |
| 1403 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); | |
| 1404 expected_audio.codec_id = "RTCCodec_InboundAudio_42"; | |
| 1405 expected_audio.packets_received = 2; | |
| 1406 expected_audio.bytes_received = 3; | |
| 1407 expected_audio.packets_lost = 42; | |
| 1408 expected_audio.jitter = 4.5; | |
| 1409 expected_audio.fraction_lost = 5.5; | |
| 1410 | |
| 1411 ASSERT(report->Get(expected_audio.id())); | |
| 1412 const RTCInboundRTPStreamStats& audio = report->Get( | |
| 1413 expected_audio.id())->cast_to<RTCInboundRTPStreamStats>(); | |
| 1414 EXPECT_EQ(audio, expected_audio); | |
| 1415 | |
| 1416 EXPECT_TRUE(report->Get(*expected_audio.transport_id)); | |
| 1417 EXPECT_TRUE(report->Get(*expected_audio.codec_id)); | |
| 1418 } | |
| 1419 | |
| 1420 TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { | |
| 1421 MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); | |
| 1422 cricket::VideoChannel video_channel( | |
| 1423 test_->worker_thread(), test_->network_thread(), video_media_channel, | |
| 1424 nullptr, "VideoContentName", kDefaultRtcpEnabled, kDefaultSrtpRequired); | |
| 1425 | |
| 1426 cricket::VideoMediaInfo video_media_info; | |
| 1427 | |
| 1428 video_media_info.receivers.push_back(cricket::VideoReceiverInfo()); | |
| 1429 video_media_info.receivers[0].local_stats.push_back( | |
| 1430 cricket::SsrcReceiverInfo()); | |
| 1431 video_media_info.receivers[0].local_stats[0].ssrc = 1; | |
| 1432 video_media_info.receivers[0].packets_rcvd = 2; | |
| 1433 video_media_info.receivers[0].packets_lost = 42; | |
| 1434 video_media_info.receivers[0].bytes_rcvd = 3; | |
| 1435 video_media_info.receivers[0].fraction_lost = 4.5f; | |
| 1436 video_media_info.receivers[0].codec_payload_type = rtc::Optional<int>(42); | |
| 1437 video_media_info.receivers[0].firs_sent = 5; | |
| 1438 video_media_info.receivers[0].plis_sent = 6; | |
| 1439 video_media_info.receivers[0].nacks_sent = 7; | |
| 1440 | |
| 1441 RtpCodecParameters codec_parameters; | |
| 1442 codec_parameters.payload_type = 42; | |
| 1443 codec_parameters.mime_type = "dummy"; | |
| 1444 codec_parameters.clock_rate = 0; | |
| 1445 video_media_info.receive_codecs.insert( | |
| 1446 std::make_pair(codec_parameters.payload_type, codec_parameters)); | |
| 1447 | |
| 1448 EXPECT_CALL(*video_media_channel, GetStats(_)) | |
| 1449 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); | |
| 1450 | |
| 1451 SessionStats session_stats; | |
| 1452 session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; | |
| 1453 session_stats.transport_stats["TransportName"].transport_name = | |
| 1454 "TransportName"; | |
| 1455 | |
| 1456 // Make sure the associated |RTCTransportStats| is created. | |
| 1457 cricket::TransportChannelStats channel_stats; | |
| 1458 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; | |
| 1459 session_stats.transport_stats["TransportName"].channel_stats.push_back( | |
| 1460 channel_stats); | |
| 1461 | |
| 1462 EXPECT_CALL(test_->session(), GetTransportStats(_)) | |
| 1463 .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); | |
| 1464 EXPECT_CALL(test_->session(), video_channel()) | |
| 1465 .WillRepeatedly(Return(&video_channel)); | |
| 1466 | |
| 1467 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1468 | |
| 1469 RTCInboundRTPStreamStats expected_video( | |
| 1470 "RTCInboundRTPVideoStream_1", report->timestamp_us()); | |
| 1471 expected_video.ssrc = "1"; | |
| 1472 expected_video.is_remote = false; | |
| 1473 expected_video.media_type = "video"; | |
| 1474 expected_video.transport_id = "RTCTransport_TransportName_" + | |
| 1475 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); | |
| 1476 expected_video.codec_id = "RTCCodec_InboundVideo_42"; | |
| 1477 expected_video.fir_count = 5; | |
| 1478 expected_video.pli_count = 6; | |
| 1479 expected_video.nack_count = 7; | |
| 1480 expected_video.packets_received = 2; | |
| 1481 expected_video.bytes_received = 3; | |
| 1482 expected_video.packets_lost = 42; | |
| 1483 expected_video.fraction_lost = 4.5; | |
| 1484 | |
| 1485 ASSERT(report->Get(expected_video.id())); | |
| 1486 const RTCInboundRTPStreamStats& video = report->Get( | |
| 1487 expected_video.id())->cast_to<RTCInboundRTPStreamStats>(); | |
| 1488 EXPECT_EQ(video, expected_video); | |
| 1489 | |
| 1490 EXPECT_TRUE(report->Get(*expected_video.transport_id)); | |
| 1491 EXPECT_TRUE(report->Get(*video.codec_id)); | |
| 1492 } | |
| 1493 | |
| 1494 TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { | |
| 1495 MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); | |
| 1496 cricket::VoiceChannel voice_channel( | |
| 1497 test_->worker_thread(), test_->network_thread(), test_->media_engine(), | |
| 1498 voice_media_channel, nullptr, "VoiceContentName", kDefaultRtcpEnabled, | |
| 1499 kDefaultSrtpRequired); | |
| 1500 | |
| 1501 cricket::VoiceMediaInfo voice_media_info; | |
| 1502 | |
| 1503 voice_media_info.senders.push_back(cricket::VoiceSenderInfo()); | |
| 1504 voice_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); | |
| 1505 voice_media_info.senders[0].local_stats[0].ssrc = 1; | |
| 1506 voice_media_info.senders[0].packets_sent = 2; | |
| 1507 voice_media_info.senders[0].bytes_sent = 3; | |
| 1508 voice_media_info.senders[0].rtt_ms = 4500; | |
| 1509 voice_media_info.senders[0].codec_payload_type = rtc::Optional<int>(42); | |
| 1510 | |
| 1511 RtpCodecParameters codec_parameters; | |
| 1512 codec_parameters.payload_type = 42; | |
| 1513 codec_parameters.mime_type = "dummy"; | |
| 1514 codec_parameters.clock_rate = 0; | |
| 1515 voice_media_info.send_codecs.insert( | |
| 1516 std::make_pair(codec_parameters.payload_type, codec_parameters)); | |
| 1517 | |
| 1518 EXPECT_CALL(*voice_media_channel, GetStats(_)) | |
| 1519 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); | |
| 1520 | |
| 1521 SessionStats session_stats; | |
| 1522 session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; | |
| 1523 session_stats.transport_stats["TransportName"].transport_name = | |
| 1524 "TransportName"; | |
| 1525 | |
| 1526 // Make sure the associated |RTCTransportStats| is created. | |
| 1527 cricket::TransportChannelStats channel_stats; | |
| 1528 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; | |
| 1529 session_stats.transport_stats["TransportName"].channel_stats.push_back( | |
| 1530 channel_stats); | |
| 1531 | |
| 1532 EXPECT_CALL(test_->session(), GetTransportStats(_)) | |
| 1533 .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); | |
| 1534 EXPECT_CALL(test_->session(), voice_channel()) | |
| 1535 .WillRepeatedly(Return(&voice_channel)); | |
| 1536 | |
| 1537 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1538 | |
| 1539 RTCOutboundRTPStreamStats expected_audio( | |
| 1540 "RTCOutboundRTPAudioStream_1", report->timestamp_us()); | |
| 1541 expected_audio.ssrc = "1"; | |
| 1542 expected_audio.is_remote = false; | |
| 1543 expected_audio.media_type = "audio"; | |
| 1544 expected_audio.transport_id = "RTCTransport_TransportName_" + | |
| 1545 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); | |
| 1546 expected_audio.codec_id = "RTCCodec_OutboundAudio_42"; | |
| 1547 expected_audio.packets_sent = 2; | |
| 1548 expected_audio.bytes_sent = 3; | |
| 1549 expected_audio.round_trip_time = 4.5; | |
| 1550 | |
| 1551 ASSERT(report->Get(expected_audio.id())); | |
| 1552 const RTCOutboundRTPStreamStats& audio = report->Get( | |
| 1553 expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(); | |
| 1554 EXPECT_EQ(audio, expected_audio); | |
| 1555 | |
| 1556 EXPECT_TRUE(report->Get(*expected_audio.transport_id)); | |
| 1557 EXPECT_TRUE(report->Get(*expected_audio.codec_id)); | |
| 1558 } | |
| 1559 | |
| 1560 TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { | |
| 1561 MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); | |
| 1562 cricket::VideoChannel video_channel( | |
| 1563 test_->worker_thread(), test_->network_thread(), video_media_channel, | |
| 1564 nullptr, "VideoContentName", kDefaultRtcpEnabled, kDefaultSrtpRequired); | |
| 1565 | |
| 1566 cricket::VideoMediaInfo video_media_info; | |
| 1567 | |
| 1568 video_media_info.senders.push_back(cricket::VideoSenderInfo()); | |
| 1569 video_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); | |
| 1570 video_media_info.senders[0].local_stats[0].ssrc = 1; | |
| 1571 video_media_info.senders[0].firs_rcvd = 2; | |
| 1572 video_media_info.senders[0].plis_rcvd = 3; | |
| 1573 video_media_info.senders[0].nacks_rcvd = 4; | |
| 1574 video_media_info.senders[0].packets_sent = 5; | |
| 1575 video_media_info.senders[0].bytes_sent = 6; | |
| 1576 video_media_info.senders[0].rtt_ms = 7500; | |
| 1577 video_media_info.senders[0].codec_payload_type = rtc::Optional<int>(42); | |
| 1578 | |
| 1579 RtpCodecParameters codec_parameters; | |
| 1580 codec_parameters.payload_type = 42; | |
| 1581 codec_parameters.mime_type = "dummy"; | |
| 1582 codec_parameters.clock_rate = 0; | |
| 1583 video_media_info.send_codecs.insert( | |
| 1584 std::make_pair(codec_parameters.payload_type, codec_parameters)); | |
| 1585 | |
| 1586 EXPECT_CALL(*video_media_channel, GetStats(_)) | |
| 1587 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); | |
| 1588 | |
| 1589 SessionStats session_stats; | |
| 1590 session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; | |
| 1591 session_stats.transport_stats["TransportName"].transport_name = | |
| 1592 "TransportName"; | |
| 1593 | |
| 1594 // Make sure the associated |RTCTransportStats| is created. | |
| 1595 cricket::TransportChannelStats channel_stats; | |
| 1596 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; | |
| 1597 session_stats.transport_stats["TransportName"].channel_stats.push_back( | |
| 1598 channel_stats); | |
| 1599 | |
| 1600 EXPECT_CALL(test_->session(), GetTransportStats(_)) | |
| 1601 .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); | |
| 1602 EXPECT_CALL(test_->session(), video_channel()) | |
| 1603 .WillRepeatedly(Return(&video_channel)); | |
| 1604 | |
| 1605 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1606 | |
| 1607 RTCOutboundRTPStreamStats expected_video( | |
| 1608 "RTCOutboundRTPVideoStream_1", report->timestamp_us()); | |
| 1609 expected_video.ssrc = "1"; | |
| 1610 expected_video.is_remote = false; | |
| 1611 expected_video.media_type = "video"; | |
| 1612 expected_video.transport_id = "RTCTransport_TransportName_" + | |
| 1613 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); | |
| 1614 expected_video.codec_id = "RTCCodec_OutboundVideo_42"; | |
| 1615 expected_video.fir_count = 2; | |
| 1616 expected_video.pli_count = 3; | |
| 1617 expected_video.nack_count = 4; | |
| 1618 expected_video.packets_sent = 5; | |
| 1619 expected_video.bytes_sent = 6; | |
| 1620 expected_video.round_trip_time = 7.5; | |
| 1621 | |
| 1622 ASSERT(report->Get(expected_video.id())); | |
| 1623 const RTCOutboundRTPStreamStats& video = report->Get( | |
| 1624 expected_video.id())->cast_to<RTCOutboundRTPStreamStats>(); | |
| 1625 EXPECT_EQ(video, expected_video); | |
| 1626 | |
| 1627 EXPECT_TRUE(report->Get(*expected_video.transport_id)); | |
| 1628 EXPECT_TRUE(report->Get(*expected_video.codec_id)); | |
| 1629 } | |
| 1630 | |
| 1631 TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Default) { | |
| 1632 MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); | |
| 1633 cricket::VoiceChannel voice_channel( | |
| 1634 test_->worker_thread(), test_->network_thread(), test_->media_engine(), | |
| 1635 voice_media_channel, nullptr, "VoiceContentName", kDefaultRtcpEnabled, | |
| 1636 kDefaultSrtpRequired); | |
| 1637 MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); | |
| 1638 cricket::VideoChannel video_channel( | |
| 1639 test_->worker_thread(), test_->network_thread(), video_media_channel, | |
| 1640 nullptr, "VideoContentName", kDefaultRtcpEnabled, kDefaultSrtpRequired); | |
| 1641 | |
| 1642 cricket::VoiceMediaInfo voice_media_info; | |
| 1643 voice_media_info.senders.push_back(cricket::VoiceSenderInfo()); | |
| 1644 voice_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); | |
| 1645 voice_media_info.senders[0].local_stats[0].ssrc = 1; | |
| 1646 voice_media_info.senders[0].packets_sent = 2; | |
| 1647 voice_media_info.senders[0].bytes_sent = 3; | |
| 1648 voice_media_info.senders[0].rtt_ms = -1; | |
| 1649 voice_media_info.senders[0].codec_payload_type = rtc::Optional<int>(42); | |
| 1650 | |
| 1651 cricket::VideoMediaInfo video_media_info; | |
| 1652 video_media_info.senders.push_back(cricket::VideoSenderInfo()); | |
| 1653 video_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); | |
| 1654 video_media_info.senders[0].local_stats[0].ssrc = 1; | |
| 1655 video_media_info.senders[0].firs_rcvd = 2; | |
| 1656 video_media_info.senders[0].plis_rcvd = 3; | |
| 1657 video_media_info.senders[0].nacks_rcvd = 4; | |
| 1658 video_media_info.senders[0].packets_sent = 5; | |
| 1659 video_media_info.senders[0].bytes_sent = 6; | |
| 1660 video_media_info.senders[0].rtt_ms = -1; | |
| 1661 video_media_info.senders[0].codec_payload_type = rtc::Optional<int>(42); | |
| 1662 | |
| 1663 EXPECT_CALL(*voice_media_channel, GetStats(_)) | |
| 1664 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); | |
| 1665 EXPECT_CALL(*video_media_channel, GetStats(_)) | |
| 1666 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); | |
| 1667 | |
| 1668 SessionStats session_stats; | |
| 1669 session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; | |
| 1670 session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; | |
| 1671 session_stats.transport_stats["TransportName"].transport_name = | |
| 1672 "TransportName"; | |
| 1673 | |
| 1674 // Make sure the associated |RTCTransportStats| is created. | |
| 1675 cricket::TransportChannelStats channel_stats; | |
| 1676 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; | |
| 1677 session_stats.transport_stats["TransportName"].channel_stats.push_back( | |
| 1678 channel_stats); | |
| 1679 | |
| 1680 EXPECT_CALL(test_->session(), GetTransportStats(_)) | |
| 1681 .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); | |
| 1682 EXPECT_CALL(test_->session(), voice_channel()) | |
| 1683 .WillRepeatedly(Return(&voice_channel)); | |
| 1684 EXPECT_CALL(test_->session(), video_channel()) | |
| 1685 .WillRepeatedly(Return(&video_channel)); | |
| 1686 | |
| 1687 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1688 | |
| 1689 RTCOutboundRTPStreamStats expected_audio( | |
| 1690 "RTCOutboundRTPAudioStream_1", report->timestamp_us()); | |
| 1691 expected_audio.ssrc = "1"; | |
| 1692 expected_audio.is_remote = false; | |
| 1693 expected_audio.media_type = "audio"; | |
| 1694 expected_audio.transport_id = "RTCTransport_TransportName_" + | |
| 1695 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); | |
| 1696 expected_audio.codec_id = "RTCCodec_OutboundAudio_42"; | |
| 1697 expected_audio.packets_sent = 2; | |
| 1698 expected_audio.bytes_sent = 3; | |
| 1699 // |expected_audio.round_trip_time| should be undefined. | |
| 1700 | |
| 1701 ASSERT(report->Get(expected_audio.id())); | |
| 1702 const RTCOutboundRTPStreamStats& audio = report->Get( | |
| 1703 expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(); | |
| 1704 EXPECT_EQ(audio, expected_audio); | |
| 1705 | |
| 1706 RTCOutboundRTPStreamStats expected_video( | |
| 1707 "RTCOutboundRTPVideoStream_1", report->timestamp_us()); | |
| 1708 expected_video.ssrc = "1"; | |
| 1709 expected_video.is_remote = false; | |
| 1710 expected_video.media_type = "video"; | |
| 1711 expected_video.transport_id = "RTCTransport_TransportName_" + | |
| 1712 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); | |
| 1713 expected_video.codec_id = "RTCCodec_OutboundVideo_42"; | |
| 1714 expected_video.fir_count = 2; | |
| 1715 expected_video.pli_count = 3; | |
| 1716 expected_video.nack_count = 4; | |
| 1717 expected_video.packets_sent = 5; | |
| 1718 expected_video.bytes_sent = 6; | |
| 1719 // |expected_video.round_trip_time| should be undefined. | |
| 1720 | |
| 1721 ASSERT(report->Get(expected_video.id())); | |
| 1722 const RTCOutboundRTPStreamStats& video = report->Get( | |
| 1723 expected_video.id())->cast_to<RTCOutboundRTPStreamStats>(); | |
| 1724 EXPECT_EQ(video, expected_video); | |
| 1725 } | |
| 1726 | |
| 1727 TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { | |
| 1728 std::unique_ptr<cricket::Candidate> rtp_local_candidate = CreateFakeCandidate( | |
| 1729 "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42); | |
| 1730 std::unique_ptr<cricket::Candidate> rtp_remote_candidate = | |
| 1731 CreateFakeCandidate("42.42.42.42", 42, "protocol", | |
| 1732 cricket::LOCAL_PORT_TYPE, 42); | |
| 1733 std::unique_ptr<cricket::Candidate> rtcp_local_candidate = | |
| 1734 CreateFakeCandidate("42.42.42.42", 42, "protocol", | |
| 1735 cricket::LOCAL_PORT_TYPE, 42); | |
| 1736 std::unique_ptr<cricket::Candidate> rtcp_remote_candidate = | |
| 1737 CreateFakeCandidate("42.42.42.42", 42, "protocol", | |
| 1738 cricket::LOCAL_PORT_TYPE, 42); | |
| 1739 | |
| 1740 SessionStats session_stats; | |
| 1741 session_stats.transport_stats["transport"].transport_name = "transport"; | |
| 1742 | |
| 1743 cricket::ConnectionInfo rtp_connection_info; | |
| 1744 rtp_connection_info.best_connection = false; | |
| 1745 rtp_connection_info.local_candidate = *rtp_local_candidate.get(); | |
| 1746 rtp_connection_info.remote_candidate = *rtp_remote_candidate.get(); | |
| 1747 rtp_connection_info.sent_total_bytes = 42; | |
| 1748 rtp_connection_info.recv_total_bytes = 1337; | |
| 1749 cricket::TransportChannelStats rtp_transport_channel_stats; | |
| 1750 rtp_transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; | |
| 1751 rtp_transport_channel_stats.connection_infos.push_back(rtp_connection_info); | |
| 1752 session_stats.transport_stats["transport"].channel_stats.push_back( | |
| 1753 rtp_transport_channel_stats); | |
| 1754 | |
| 1755 | |
| 1756 // Mock the session to return the desired candidates. | |
| 1757 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
| 1758 [this, &session_stats](SessionStats* stats) { | |
| 1759 *stats = session_stats; | |
| 1760 return true; | |
| 1761 })); | |
| 1762 | |
| 1763 // Get stats without RTCP, an active connection or certificates. | |
| 1764 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
| 1765 | |
| 1766 RTCTransportStats expected_rtp_transport( | |
| 1767 "RTCTransport_transport_" + | |
| 1768 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP), | |
| 1769 report->timestamp_us()); | |
| 1770 expected_rtp_transport.bytes_sent = 42; | |
| 1771 expected_rtp_transport.bytes_received = 1337; | |
| 1772 expected_rtp_transport.active_connection = false; | |
| 1773 | |
| 1774 EXPECT_TRUE(report->Get(expected_rtp_transport.id())); | |
| 1775 EXPECT_EQ( | |
| 1776 expected_rtp_transport, | |
| 1777 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); | |
| 1778 | |
| 1779 cricket::ConnectionInfo rtcp_connection_info; | |
| 1780 rtcp_connection_info.best_connection = false; | |
| 1781 rtcp_connection_info.local_candidate = *rtcp_local_candidate.get(); | |
| 1782 rtcp_connection_info.remote_candidate = *rtcp_remote_candidate.get(); | |
| 1783 rtcp_connection_info.sent_total_bytes = 1337; | |
| 1784 rtcp_connection_info.recv_total_bytes = 42; | |
| 1785 cricket::TransportChannelStats rtcp_transport_channel_stats; | |
| 1786 rtcp_transport_channel_stats.component = | |
| 1787 cricket::ICE_CANDIDATE_COMPONENT_RTCP; | |
| 1788 rtcp_transport_channel_stats.connection_infos.push_back(rtcp_connection_info); | |
| 1789 session_stats.transport_stats["transport"].channel_stats.push_back( | |
| 1790 rtcp_transport_channel_stats); | |
| 1791 | |
| 1792 collector_->ClearCachedStatsReport(); | |
| 1793 // Get stats with RTCP and without an active connection or certificates. | |
| 1794 report = GetStatsReport(); | |
| 1795 | |
| 1796 RTCTransportStats expected_rtcp_transport( | |
| 1797 "RTCTransport_transport_" + | |
| 1798 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTCP), | |
| 1799 report->timestamp_us()); | |
| 1800 expected_rtcp_transport.bytes_sent = 1337; | |
| 1801 expected_rtcp_transport.bytes_received = 42; | |
| 1802 expected_rtcp_transport.active_connection = false; | |
| 1803 | |
| 1804 expected_rtp_transport.rtcp_transport_stats_id = expected_rtcp_transport.id(); | |
| 1805 | |
| 1806 EXPECT_TRUE(report->Get(expected_rtp_transport.id())); | |
| 1807 EXPECT_EQ( | |
| 1808 expected_rtp_transport, | |
| 1809 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); | |
| 1810 EXPECT_TRUE(report->Get(expected_rtcp_transport.id())); | |
| 1811 EXPECT_EQ( | |
| 1812 expected_rtcp_transport, | |
| 1813 report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); | |
| 1814 | |
| 1815 // Get stats with an active connection. | |
| 1816 session_stats.transport_stats["transport"] | |
| 1817 .channel_stats[1] | |
| 1818 .connection_infos[0] | |
| 1819 .best_connection = true; | |
| 1820 | |
| 1821 collector_->ClearCachedStatsReport(); | |
| 1822 report = GetStatsReport(); | |
| 1823 | |
| 1824 expected_rtcp_transport.active_connection = true; | |
| 1825 expected_rtcp_transport.selected_candidate_pair_id = | |
| 1826 "RTCIceCandidatePair_" + rtcp_local_candidate->id() + "_" + | |
| 1827 rtcp_remote_candidate->id(); | |
| 1828 | |
| 1829 EXPECT_TRUE(report->Get(expected_rtp_transport.id())); | |
| 1830 EXPECT_EQ( | |
| 1831 expected_rtp_transport, | |
| 1832 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); | |
| 1833 EXPECT_TRUE(report->Get(expected_rtcp_transport.id())); | |
| 1834 EXPECT_EQ( | |
| 1835 expected_rtcp_transport, | |
| 1836 report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); | |
| 1837 | |
| 1838 // Get stats with certificates. | |
| 1839 std::unique_ptr<CertificateInfo> local_certinfo = | |
| 1840 CreateFakeCertificateAndInfoFromDers( | |
| 1841 std::vector<std::string>({ "(local) local", "(local) chain" })); | |
| 1842 std::unique_ptr<CertificateInfo> remote_certinfo = | |
| 1843 CreateFakeCertificateAndInfoFromDers( | |
| 1844 std::vector<std::string>({ "(remote) local", "(remote) chain" })); | |
| 1845 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( | |
| 1846 Invoke([this, &local_certinfo](const std::string& transport_name, | |
| 1847 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { | |
| 1848 if (transport_name == "transport") { | |
| 1849 *certificate = local_certinfo->certificate; | |
| 1850 return true; | |
| 1851 } | |
| 1852 return false; | |
| 1853 })); | |
| 1854 EXPECT_CALL(test_->session(), | |
| 1855 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( | |
| 1856 [this, &remote_certinfo](const std::string& transport_name) { | |
| 1857 if (transport_name == "transport") | |
| 1858 return remote_certinfo->certificate->ssl_certificate().GetReference(); | |
| 1859 return static_cast<rtc::SSLCertificate*>(nullptr); | |
| 1860 })); | |
| 1861 | |
| 1862 collector_->ClearCachedStatsReport(); | |
| 1863 report = GetStatsReport(); | |
| 1864 | |
| 1865 expected_rtp_transport.local_certificate_id = | |
| 1866 "RTCCertificate_" + local_certinfo->fingerprints[0]; | |
| 1867 expected_rtp_transport.remote_certificate_id = | |
| 1868 "RTCCertificate_" + remote_certinfo->fingerprints[0]; | |
| 1869 | |
| 1870 expected_rtcp_transport.local_certificate_id = | |
| 1871 *expected_rtp_transport.local_certificate_id; | |
| 1872 expected_rtcp_transport.remote_certificate_id = | |
| 1873 *expected_rtp_transport.remote_certificate_id; | |
| 1874 | |
| 1875 EXPECT_TRUE(report->Get(expected_rtp_transport.id())); | |
| 1876 EXPECT_EQ( | |
| 1877 expected_rtp_transport, | |
| 1878 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); | |
| 1879 EXPECT_TRUE(report->Get(expected_rtcp_transport.id())); | |
| 1880 EXPECT_EQ( | |
| 1881 expected_rtcp_transport, | |
| 1882 report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); | |
| 1883 } | |
| 1884 | |
| 1885 class RTCStatsCollectorTestWithFakeCollector : public testing::Test { | |
| 1886 public: | |
| 1887 RTCStatsCollectorTestWithFakeCollector() | |
| 1888 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()), | |
| 1889 collector_(FakeRTCStatsCollector::Create( | |
| 1890 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { | |
| 1891 } | |
| 1892 | |
| 1893 protected: | |
| 1894 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_; | |
| 1895 rtc::scoped_refptr<FakeRTCStatsCollector> collector_; | |
| 1896 }; | |
| 1897 | |
| 1898 TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { | |
| 1899 collector_->VerifyThreadUsageAndResultsMerging(); | |
| 1900 } | |
| 1901 | |
| 1902 } // namespace | |
| 1903 | |
| 1904 } // namespace webrtc | |
| OLD | NEW |