| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/api/rtcstatscollector.h" | 11 #include "webrtc/api/rtcstatscollector.h" |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <ostream> | 14 #include <ostream> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "webrtc/api/mediastream.h" | 18 #include "webrtc/api/mediastream.h" |
| 19 #include "webrtc/api/mediastreamtrack.h" | 19 #include "webrtc/api/mediastreamtrack.h" |
| 20 #include "webrtc/api/jsepsessiondescription.h" | 20 #include "webrtc/api/jsepsessiondescription.h" |
| 21 #include "webrtc/api/stats/rtcstats_objects.h" | 21 #include "webrtc/api/stats/rtcstats_objects.h" |
| 22 #include "webrtc/api/stats/rtcstatsreport.h" | 22 #include "webrtc/api/stats/rtcstatsreport.h" |
| 23 #include "webrtc/api/test/mock_datachannel.h" | 23 #include "webrtc/api/test/mock_datachannel.h" |
| 24 #include "webrtc/api/test/mock_peerconnection.h" | 24 #include "webrtc/api/test/mock_peerconnection.h" |
| 25 #include "webrtc/api/test/mock_webrtcsession.h" | 25 #include "webrtc/api/test/mock_webrtcsession.h" |
| 26 #include "webrtc/api/test/rtcstatsobtainer.h" |
| 26 #include "webrtc/base/checks.h" | 27 #include "webrtc/base/checks.h" |
| 27 #include "webrtc/base/fakeclock.h" | 28 #include "webrtc/base/fakeclock.h" |
| 28 #include "webrtc/base/fakesslidentity.h" | 29 #include "webrtc/base/fakesslidentity.h" |
| 29 #include "webrtc/base/gunit.h" | 30 #include "webrtc/base/gunit.h" |
| 30 #include "webrtc/base/logging.h" | 31 #include "webrtc/base/logging.h" |
| 31 #include "webrtc/base/socketaddress.h" | 32 #include "webrtc/base/socketaddress.h" |
| 32 #include "webrtc/base/thread_checker.h" | 33 #include "webrtc/base/thread_checker.h" |
| 33 #include "webrtc/base/timedelta.h" | 34 #include "webrtc/base/timedelta.h" |
| 34 #include "webrtc/base/timeutils.h" | 35 #include "webrtc/base/timeutils.h" |
| 35 #include "webrtc/logging/rtc_event_log/rtc_event_log.h" | 36 #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 rtc::Thread* const worker_thread_; | 461 rtc::Thread* const worker_thread_; |
| 461 rtc::Thread* const network_thread_; | 462 rtc::Thread* const network_thread_; |
| 462 | 463 |
| 463 rtc::CriticalSection lock_; | 464 rtc::CriticalSection lock_; |
| 464 rtc::scoped_refptr<const RTCStatsReport> delivered_report_; | 465 rtc::scoped_refptr<const RTCStatsReport> delivered_report_; |
| 465 int produced_on_signaling_thread_ = 0; | 466 int produced_on_signaling_thread_ = 0; |
| 466 int produced_on_worker_thread_ = 0; | 467 int produced_on_worker_thread_ = 0; |
| 467 int produced_on_network_thread_ = 0; | 468 int produced_on_network_thread_ = 0; |
| 468 }; | 469 }; |
| 469 | 470 |
| 470 class StatsCallback : public RTCStatsCollectorCallback { | |
| 471 public: | |
| 472 static rtc::scoped_refptr<StatsCallback> Create( | |
| 473 rtc::scoped_refptr<const RTCStatsReport>* report_ptr = nullptr) { | |
| 474 return rtc::scoped_refptr<StatsCallback>( | |
| 475 new rtc::RefCountedObject<StatsCallback>(report_ptr)); | |
| 476 } | |
| 477 | |
| 478 void OnStatsDelivered( | |
| 479 const rtc::scoped_refptr<const RTCStatsReport>& report) override { | |
| 480 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | |
| 481 report_ = report; | |
| 482 if (report_ptr_) | |
| 483 *report_ptr_ = report_; | |
| 484 } | |
| 485 | |
| 486 rtc::scoped_refptr<const RTCStatsReport> report() const { | |
| 487 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | |
| 488 return report_; | |
| 489 } | |
| 490 | |
| 491 protected: | |
| 492 explicit StatsCallback(rtc::scoped_refptr<const RTCStatsReport>* report_ptr) | |
| 493 : report_ptr_(report_ptr) {} | |
| 494 | |
| 495 private: | |
| 496 rtc::ThreadChecker thread_checker_; | |
| 497 rtc::scoped_refptr<const RTCStatsReport> report_; | |
| 498 rtc::scoped_refptr<const RTCStatsReport>* report_ptr_; | |
| 499 }; | |
| 500 | |
| 501 class RTCStatsCollectorTest : public testing::Test { | 471 class RTCStatsCollectorTest : public testing::Test { |
| 502 public: | 472 public: |
| 503 RTCStatsCollectorTest() | 473 RTCStatsCollectorTest() |
| 504 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()), | 474 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()), |
| 505 collector_(RTCStatsCollector::Create( | 475 collector_(RTCStatsCollector::Create( |
| 506 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { | 476 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { |
| 507 } | 477 } |
| 508 | 478 |
| 509 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() { | 479 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() { |
| 510 rtc::scoped_refptr<StatsCallback> callback = StatsCallback::Create(); | 480 rtc::scoped_refptr<RTCStatsObtainer> callback = RTCStatsObtainer::Create(); |
| 511 collector_->GetStatsReport(callback); | 481 collector_->GetStatsReport(callback); |
| 512 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); | 482 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); |
| 513 int64_t after = rtc::TimeUTCMicros(); | 483 int64_t after = rtc::TimeUTCMicros(); |
| 514 for (const RTCStats& stats : *callback->report()) { | 484 for (const RTCStats& stats : *callback->report()) { |
| 515 EXPECT_LE(stats.timestamp_us(), after); | 485 EXPECT_LE(stats.timestamp_us(), after); |
| 516 } | 486 } |
| 517 return callback->report(); | 487 return callback->report(); |
| 518 } | 488 } |
| 519 | 489 |
| 520 const RTCIceCandidateStats* ExpectReportContainsCandidate( | 490 const RTCIceCandidateStats* ExpectReportContainsCandidate( |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 data_channel.bytes_received()); | 681 data_channel.bytes_received()); |
| 712 } | 682 } |
| 713 | 683 |
| 714 protected: | 684 protected: |
| 715 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_; | 685 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_; |
| 716 rtc::scoped_refptr<RTCStatsCollector> collector_; | 686 rtc::scoped_refptr<RTCStatsCollector> collector_; |
| 717 }; | 687 }; |
| 718 | 688 |
| 719 TEST_F(RTCStatsCollectorTest, SingleCallback) { | 689 TEST_F(RTCStatsCollectorTest, SingleCallback) { |
| 720 rtc::scoped_refptr<const RTCStatsReport> result; | 690 rtc::scoped_refptr<const RTCStatsReport> result; |
| 721 collector_->GetStatsReport(StatsCallback::Create(&result)); | 691 collector_->GetStatsReport(RTCStatsObtainer::Create(&result)); |
| 722 EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs); | 692 EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs); |
| 723 } | 693 } |
| 724 | 694 |
| 725 TEST_F(RTCStatsCollectorTest, MultipleCallbacks) { | 695 TEST_F(RTCStatsCollectorTest, MultipleCallbacks) { |
| 726 rtc::scoped_refptr<const RTCStatsReport> a; | 696 rtc::scoped_refptr<const RTCStatsReport> a; |
| 727 rtc::scoped_refptr<const RTCStatsReport> b; | 697 rtc::scoped_refptr<const RTCStatsReport> b; |
| 728 rtc::scoped_refptr<const RTCStatsReport> c; | 698 rtc::scoped_refptr<const RTCStatsReport> c; |
| 729 collector_->GetStatsReport(StatsCallback::Create(&a)); | 699 collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); |
| 730 collector_->GetStatsReport(StatsCallback::Create(&b)); | 700 collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); |
| 731 collector_->GetStatsReport(StatsCallback::Create(&c)); | 701 collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); |
| 732 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); | 702 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); |
| 733 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); | 703 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); |
| 734 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); | 704 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); |
| 735 EXPECT_EQ(a.get(), b.get()); | 705 EXPECT_EQ(a.get(), b.get()); |
| 736 EXPECT_EQ(b.get(), c.get()); | 706 EXPECT_EQ(b.get(), c.get()); |
| 737 } | 707 } |
| 738 | 708 |
| 739 TEST_F(RTCStatsCollectorTest, CachedStatsReports) { | 709 TEST_F(RTCStatsCollectorTest, CachedStatsReports) { |
| 740 // Caching should ensure |a| and |b| are the same report. | 710 // Caching should ensure |a| and |b| are the same report. |
| 741 rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport(); | 711 rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport(); |
| 742 rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport(); | 712 rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport(); |
| 743 EXPECT_EQ(a.get(), b.get()); | 713 EXPECT_EQ(a.get(), b.get()); |
| 744 // Invalidate cache by clearing it. | 714 // Invalidate cache by clearing it. |
| 745 collector_->ClearCachedStatsReport(); | 715 collector_->ClearCachedStatsReport(); |
| 746 rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport(); | 716 rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport(); |
| 747 EXPECT_NE(b.get(), c.get()); | 717 EXPECT_NE(b.get(), c.get()); |
| 748 // Invalidate cache by advancing time. | 718 // Invalidate cache by advancing time. |
| 749 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); | 719 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); |
| 750 rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport(); | 720 rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport(); |
| 751 EXPECT_TRUE(d); | 721 EXPECT_TRUE(d); |
| 752 EXPECT_NE(c.get(), d.get()); | 722 EXPECT_NE(c.get(), d.get()); |
| 753 } | 723 } |
| 754 | 724 |
| 755 TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { | 725 TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { |
| 756 rtc::scoped_refptr<const RTCStatsReport> a; | 726 rtc::scoped_refptr<const RTCStatsReport> a; |
| 757 rtc::scoped_refptr<const RTCStatsReport> b; | 727 rtc::scoped_refptr<const RTCStatsReport> b; |
| 758 rtc::scoped_refptr<const RTCStatsReport> c; | 728 rtc::scoped_refptr<const RTCStatsReport> c; |
| 759 collector_->GetStatsReport(StatsCallback::Create(&a)); | 729 collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); |
| 760 collector_->GetStatsReport(StatsCallback::Create(&b)); | 730 collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); |
| 761 // Cache is invalidated after 50 ms. | 731 // Cache is invalidated after 50 ms. |
| 762 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); | 732 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); |
| 763 collector_->GetStatsReport(StatsCallback::Create(&c)); | 733 collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); |
| 764 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); | 734 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); |
| 765 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); | 735 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); |
| 766 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); | 736 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); |
| 767 EXPECT_EQ(a.get(), b.get()); | 737 EXPECT_EQ(a.get(), b.get()); |
| 768 // The act of doing |AdvanceTime| processes all messages. If this was not the | 738 // The act of doing |AdvanceTime| processes all messages. If this was not the |
| 769 // case we might not require |c| to be fresher than |b|. | 739 // case we might not require |c| to be fresher than |b|. |
| 770 EXPECT_NE(c.get(), b.get()); | 740 EXPECT_NE(c.get(), b.get()); |
| 771 } | 741 } |
| 772 | 742 |
| 773 TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) { | 743 TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) { |
| (...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1653 rtc::scoped_refptr<FakeRTCStatsCollector> collector_; | 1623 rtc::scoped_refptr<FakeRTCStatsCollector> collector_; |
| 1654 }; | 1624 }; |
| 1655 | 1625 |
| 1656 TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { | 1626 TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { |
| 1657 collector_->VerifyThreadUsageAndResultsMerging(); | 1627 collector_->VerifyThreadUsageAndResultsMerging(); |
| 1658 } | 1628 } |
| 1659 | 1629 |
| 1660 } // namespace | 1630 } // namespace |
| 1661 | 1631 |
| 1662 } // namespace webrtc | 1632 } // namespace webrtc |
| OLD | NEW |