From 5b38731f0bcbfa35b277e193e1b43684a23340cd Mon Sep 17 00:00:00 2001 From: Steve Anton Date: Fri, 2 Feb 2018 16:00:20 -0800 Subject: [PATCH] Use fake PeerConnection for RTCStatsCollector tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This removes use of the MockPeerConnection and replaces it with the FakePeerConnectionForStats testing class. Bug: webrtc:8764 Change-Id: I78553c5a4e4d68cb6666a83f443f72f7c25488dc Reviewed-on: https://webrtc-review.googlesource.com/46940 Commit-Queue: Steve Anton Reviewed-by: Henrik Boström Reviewed-by: Taylor Brandstetter Cr-Commit-Position: refs/heads/master@{#21882} --- pc/rtcstatscollector_unittest.cc | 1091 ++++++++------------------ pc/statscollector_unittest.cc | 69 +- pc/test/fakepeerconnectionforstats.h | 75 +- 3 files changed, 449 insertions(+), 786 deletions(-) diff --git a/pc/rtcstatscollector_unittest.cc b/pc/rtcstatscollector_unittest.cc index 8c37b77fce..566d677661 100644 --- a/pc/rtcstatscollector_unittest.cc +++ b/pc/rtcstatscollector_unittest.cc @@ -8,8 +8,6 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "pc/rtcstatscollector.h" - #include #include #include @@ -17,35 +15,31 @@ #include #include -#include "api/jsepsessiondescription.h" #include "api/rtpparameters.h" #include "api/stats/rtcstats_objects.h" #include "api/stats/rtcstatsreport.h" #include "api/test/mock_rtpreceiver.h" #include "api/test/mock_rtpsender.h" -#include "media/base/fakemediaengine.h" -#include "media/base/test/mock_mediachannel.h" #include "p2p/base/p2pconstants.h" #include "p2p/base/port.h" #include "pc/mediastream.h" #include "pc/mediastreamtrack.h" +#include "pc/rtcstatscollector.h" +#include "pc/test/fakepeerconnectionforstats.h" #include "pc/test/mock_datachannel.h" -#include "pc/test/mock_peerconnection.h" #include "pc/test/rtcstatsobtainer.h" #include "rtc_base/checks.h" #include "rtc_base/fakeclock.h" #include "rtc_base/fakesslidentity.h" #include "rtc_base/gunit.h" #include "rtc_base/logging.h" -#include "rtc_base/thread_checker.h" +#include "rtc_base/ptr_util.h" +#include "rtc_base/stringutils.h" #include "rtc_base/timedelta.h" #include "rtc_base/timeutils.h" -using testing::_; using testing::Invoke; using testing::Return; -using testing::ReturnNull; -using testing::SetArgPointee; namespace webrtc { @@ -101,8 +95,6 @@ void PrintTo(const RTCTransportStats& stats, ::std::ostream* os) { namespace { const int64_t kGetStatsReportTimeoutMs = 1000; -const bool kDefaultRtcpMuxRequired = true; -const bool kDefaultSrtpRequired = true; struct CertificateInfo { rtc::scoped_refptr certificate; @@ -300,66 +292,43 @@ rtc::scoped_refptr CreateMockReceiver( return receiver; } -class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { +class RTCStatsCollectorWrapper { public: - RTCStatsCollectorTestHelper() - : worker_thread_(rtc::Thread::Current()), - network_thread_(rtc::Thread::Current()), - signaling_thread_(rtc::Thread::Current()), - media_engine_(new cricket::FakeMediaEngine()), - pc_factory_(new FakePeerConnectionFactory( - std::unique_ptr(media_engine_))), - pc_(pc_factory_) { - // Default return values for mocks. - EXPECT_CALL(pc_, GetCallStats()).WillRepeatedly(Return(Call::Stats())); - EXPECT_CALL(pc_, local_streams()).WillRepeatedly(Return(nullptr)); - EXPECT_CALL(pc_, remote_streams()).WillRepeatedly(Return(nullptr)); - EXPECT_CALL(pc_, GetSenders()).WillRepeatedly(Return( - std::vector>())); - EXPECT_CALL(pc_, GetReceivers()).WillRepeatedly(Return( - std::vector>())); - EXPECT_CALL(pc_, sctp_data_channels()) - .WillRepeatedly(Return(data_channels_)); - EXPECT_CALL(pc_, video_channel()).WillRepeatedly(ReturnNull()); - EXPECT_CALL(pc_, voice_channel()).WillRepeatedly(ReturnNull()); - EXPECT_CALL(pc_, GetSessionStats(_)).WillRepeatedly(ReturnNull()); - EXPECT_CALL(pc_, GetLocalCertificate(_, _)).WillRepeatedly(Return(false)); - EXPECT_CALL(pc_, GetRemoteSSLCertificate_ReturnsRawPointer(_)) - .WillRepeatedly(Return(nullptr)); + explicit RTCStatsCollectorWrapper( + rtc::scoped_refptr pc) + : pc_(pc), + stats_collector_( + RTCStatsCollector::Create(pc, 50 * rtc::kNumMicrosecsPerMillisec)) { } - rtc::ScopedFakeClock& fake_clock() { return fake_clock_; } - rtc::Thread* worker_thread() { return worker_thread_; } - rtc::Thread* network_thread() { return network_thread_; } - rtc::Thread* signaling_thread() { return signaling_thread_; } - cricket::FakeMediaEngine* media_engine() { return media_engine_; } - MockPeerConnection& pc() { return pc_; } - - void AddDataChannel(rtc::scoped_refptr data_channel) { - data_channels_.push_back(data_channel); - EXPECT_CALL(pc_, sctp_data_channels()) - .WillRepeatedly(Return(data_channels_)); + rtc::scoped_refptr stats_collector() { + return stats_collector_; } - // SetSessionDescriptionObserver overrides. - void OnSuccess() override {} - void OnFailure(const std::string& error) override { - RTC_NOTREACHED() << error; + rtc::scoped_refptr GetFreshStatsReport() { + stats_collector_->ClearCachedStatsReport(); + return GetStatsReport(); + } + + rtc::scoped_refptr GetStatsReport() { + rtc::scoped_refptr callback = RTCStatsObtainer::Create(); + stats_collector_->GetStatsReport(callback); + EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); + int64_t after = rtc::TimeUTCMicros(); + for (const RTCStats& stats : *callback->report()) { + EXPECT_LE(stats.timestamp_us(), after); + } + return callback->report(); } void SetupLocalTrackAndSender(cricket::MediaType media_type, const std::string& track_id, uint32_t ssrc, bool add_stream) { - rtc::scoped_refptr local_streams = - StreamCollection::Create(); - EXPECT_CALL(pc_, local_streams()) - .WillRepeatedly(Return(local_streams)); - rtc::scoped_refptr local_stream; if (add_stream) { local_stream = MediaStream::Create("LocalStreamLabel"); - local_streams->AddStream(local_stream); + pc_->mutable_local_streams()->AddStream(local_stream); } rtc::scoped_refptr track; @@ -379,22 +348,15 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { rtc::scoped_refptr sender = CreateMockSender(track, ssrc, 50, {}); - EXPECT_CALL(pc_, GetSenders()).WillRepeatedly(Return( - std::vector>({ - rtc::scoped_refptr(sender.get()) }))); + pc_->AddSender(sender); } void SetupRemoteTrackAndReceiver(cricket::MediaType media_type, const std::string& track_id, uint32_t ssrc) { - rtc::scoped_refptr remote_streams = - StreamCollection::Create(); - EXPECT_CALL(pc_, remote_streams()) - .WillRepeatedly(Return(remote_streams)); - rtc::scoped_refptr remote_stream = MediaStream::Create("RemoteStreamLabel"); - remote_streams->AddStream(remote_stream); + pc_->mutable_remote_streams()->AddStream(remote_stream); rtc::scoped_refptr track; if (media_type == cricket::MEDIA_TYPE_AUDIO) { @@ -413,9 +375,7 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { .WillRepeatedly( Return(std::vector>( {remote_stream}))); - EXPECT_CALL(pc_, GetReceivers()).WillRepeatedly(Return( - std::vector>({ - rtc::scoped_refptr(receiver.get()) }))); + pc_->AddReceiver(receiver); } // Attaches tracks to peer connections by configuring RTP senders and RTP @@ -438,10 +398,9 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { remote_video_track_info_pairs, std::vector local_stream_ids, std::vector> remote_streams) { - voice_media_info_.reset(new cricket::VoiceMediaInfo()); - video_media_info_.reset(new cricket::VideoMediaInfo()); - rtp_senders_.clear(); - rtp_receivers_.clear(); + cricket::VoiceMediaInfo voice_media_info; + cricket::VideoMediaInfo video_media_info; + // Local audio tracks and voice sender infos int attachment_id = 147; for (auto& pair : local_audio_track_info_pairs) { @@ -450,14 +409,14 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { RTC_DCHECK_EQ(local_audio_track->kind(), MediaStreamTrackInterface::kAudioKind); - voice_media_info_->senders.push_back(voice_sender_info); + voice_media_info.senders.push_back(voice_sender_info); rtc::scoped_refptr rtp_sender = CreateMockSender( rtc::scoped_refptr(local_audio_track), voice_sender_info.local_stats[0].ssrc, attachment_id++, local_stream_ids); - rtp_senders_.push_back(rtc::scoped_refptr( - rtp_sender.get())); + pc_->AddSender(rtp_sender); } + // Remote audio tracks and voice receiver infos attachment_id = 181; for (auto& pair : remote_audio_track_info_pairs) { @@ -466,15 +425,15 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { RTC_DCHECK_EQ(remote_audio_track->kind(), MediaStreamTrackInterface::kAudioKind); - voice_media_info_->receivers.push_back(voice_receiver_info); + voice_media_info.receivers.push_back(voice_receiver_info); rtc::scoped_refptr rtp_receiver = CreateMockReceiver( rtc::scoped_refptr(remote_audio_track), voice_receiver_info.local_stats[0].ssrc, attachment_id++); EXPECT_CALL(*rtp_receiver, streams()) .WillRepeatedly(Return(remote_streams)); - rtp_receivers_.push_back(rtc::scoped_refptr( - rtp_receiver.get())); + pc_->AddReceiver(rtp_receiver); } + // Local video tracks and video sender infos attachment_id = 151; for (auto& pair : local_video_track_info_pairs) { @@ -483,14 +442,14 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { RTC_DCHECK_EQ(local_video_track->kind(), MediaStreamTrackInterface::kVideoKind); - video_media_info_->senders.push_back(video_sender_info); + video_media_info.senders.push_back(video_sender_info); rtc::scoped_refptr rtp_sender = CreateMockSender( rtc::scoped_refptr(local_video_track), video_sender_info.local_stats[0].ssrc, attachment_id++, local_stream_ids); - rtp_senders_.push_back(rtc::scoped_refptr( - rtp_sender.get())); + pc_->AddSender(rtp_sender); } + // Remote video tracks and video receiver infos attachment_id = 191; for (auto& pair : remote_video_track_info_pairs) { @@ -499,181 +458,32 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { RTC_DCHECK_EQ(remote_video_track->kind(), MediaStreamTrackInterface::kVideoKind); - video_media_info_->receivers.push_back(video_receiver_info); + video_media_info.receivers.push_back(video_receiver_info); rtc::scoped_refptr rtp_receiver = CreateMockReceiver( rtc::scoped_refptr(remote_video_track), video_receiver_info.local_stats[0].ssrc, attachment_id++); EXPECT_CALL(*rtp_receiver, streams()) .WillRepeatedly(Return(remote_streams)); - rtp_receivers_.push_back(rtc::scoped_refptr( - rtp_receiver.get())); + pc_->AddReceiver(rtp_receiver); } - EXPECT_CALL(pc_, GetSenders()).WillRepeatedly(Return(rtp_senders_)); - EXPECT_CALL(pc_, GetReceivers()).WillRepeatedly(Return(rtp_receivers_)); - auto* voice_media_channel = new MockVoiceMediaChannel(); - voice_channel_.reset(new cricket::VoiceChannel( - worker_thread_, network_thread_, nullptr, media_engine_, - rtc::WrapUnique(voice_media_channel), "VoiceContentName", - kDefaultRtcpMuxRequired, kDefaultSrtpRequired)); - EXPECT_CALL(pc_, voice_channel()) - .WillRepeatedly(Return(voice_channel_.get())); - EXPECT_CALL(*voice_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(*voice_media_info_), Return(true))); + auto* voice_media_channel = pc_->AddVoiceChannel("audio", "transport"); + voice_media_channel->SetStats(voice_media_info); - auto* video_media_channel = new MockVideoMediaChannel(); - video_channel_.reset(new cricket::VideoChannel( - worker_thread_, network_thread_, nullptr, - rtc::WrapUnique(video_media_channel), "VideoContentName", - kDefaultRtcpMuxRequired, kDefaultSrtpRequired)); - EXPECT_CALL(pc_, video_channel()) - .WillRepeatedly(Return(video_channel_.get())); - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(*video_media_info_), Return(true))); + auto* video_media_channel = pc_->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_media_info); } private: - rtc::ScopedFakeClock fake_clock_; - RtcEventLogNullImpl event_log_; - rtc::Thread* const worker_thread_; - rtc::Thread* const network_thread_; - rtc::Thread* const signaling_thread_; - // |media_engine_| is actually owned by |pc_factory_|. - cricket::FakeMediaEngine* media_engine_; - rtc::scoped_refptr pc_factory_; - MockPeerConnection pc_; - - std::vector> data_channels_; - std::unique_ptr voice_channel_; - std::unique_ptr video_channel_; - std::unique_ptr voice_media_info_; - std::unique_ptr video_media_info_; - std::vector> rtp_senders_; - std::vector> rtp_receivers_; -}; - -class RTCTestStats : public RTCStats { - public: - WEBRTC_RTCSTATS_DECL(); - - RTCTestStats(const std::string& id, int64_t timestamp_us) - : RTCStats(id, timestamp_us), - dummy_stat("dummyStat") {} - - RTCStatsMember dummy_stat; -}; - -WEBRTC_RTCSTATS_IMPL(RTCTestStats, RTCStats, "test-stats", - &dummy_stat); - -// Overrides the stats collection to verify thread usage and that the resulting -// partial reports are merged. -class FakeRTCStatsCollector : public RTCStatsCollector, - public RTCStatsCollectorCallback { - public: - static rtc::scoped_refptr Create( - PeerConnectionInternal* pc, - int64_t cache_lifetime_us) { - return rtc::scoped_refptr( - new rtc::RefCountedObject( - pc, cache_lifetime_us)); - } - - // RTCStatsCollectorCallback implementation. - void OnStatsDelivered( - const rtc::scoped_refptr& report) override { - EXPECT_TRUE(signaling_thread_->IsCurrent()); - rtc::CritScope cs(&lock_); - delivered_report_ = report; - } - - void VerifyThreadUsageAndResultsMerging() { - GetStatsReport(rtc::scoped_refptr(this)); - EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs); - } - - bool HasVerifiedResults() { - EXPECT_TRUE(signaling_thread_->IsCurrent()); - rtc::CritScope cs(&lock_); - if (!delivered_report_) - return false; - EXPECT_EQ(produced_on_signaling_thread_, 1); - EXPECT_EQ(produced_on_network_thread_, 1); - - EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats")); - EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats")); - - produced_on_signaling_thread_ = 0; - produced_on_network_thread_ = 0; - delivered_report_ = nullptr; - return true; - } - - protected: - FakeRTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime) - : RTCStatsCollector(pc, cache_lifetime), - signaling_thread_(pc->signaling_thread()), - worker_thread_(pc->worker_thread()), - network_thread_(pc->network_thread()) {} - - void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override { - EXPECT_TRUE(signaling_thread_->IsCurrent()); - { - rtc::CritScope cs(&lock_); - EXPECT_FALSE(delivered_report_); - ++produced_on_signaling_thread_; - } - - rtc::scoped_refptr signaling_report = - RTCStatsReport::Create(0); - signaling_report->AddStats(std::unique_ptr( - new RTCTestStats("SignalingThreadStats", timestamp_us))); - AddPartialResults(signaling_report); - } - void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override { - EXPECT_TRUE(network_thread_->IsCurrent()); - { - rtc::CritScope cs(&lock_); - EXPECT_FALSE(delivered_report_); - ++produced_on_network_thread_; - } - - rtc::scoped_refptr network_report = - RTCStatsReport::Create(0); - network_report->AddStats(std::unique_ptr( - new RTCTestStats("NetworkThreadStats", timestamp_us))); - AddPartialResults(network_report); - } - - private: - rtc::Thread* const signaling_thread_; - rtc::Thread* const worker_thread_; - rtc::Thread* const network_thread_; - - rtc::CriticalSection lock_; - rtc::scoped_refptr delivered_report_; - int produced_on_signaling_thread_ = 0; - int produced_on_network_thread_ = 0; + rtc::scoped_refptr pc_; + rtc::scoped_refptr stats_collector_; }; class RTCStatsCollectorTest : public testing::Test { public: RTCStatsCollectorTest() - : test_(new rtc::RefCountedObject()), - collector_(RTCStatsCollector::Create( - &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { - } - - rtc::scoped_refptr GetStatsReport() { - rtc::scoped_refptr callback = RTCStatsObtainer::Create(); - collector_->GetStatsReport(callback); - EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); - int64_t after = rtc::TimeUTCMicros(); - for (const RTCStats& stats : *callback->report()) { - EXPECT_LE(stats.timestamp_us(), after); - } - return callback->report(); - } + : pc_(new rtc::RefCountedObject()), + stats_(new RTCStatsCollectorWrapper(pc_)) {} void ExpectReportContainsCertificateInfo( const rtc::scoped_refptr& report, @@ -697,55 +507,53 @@ class RTCStatsCollectorTest : public testing::Test { } protected: - rtc::scoped_refptr test_; - rtc::scoped_refptr collector_; + rtc::ScopedFakeClock fake_clock_; + rtc::scoped_refptr pc_; + std::unique_ptr stats_; }; TEST_F(RTCStatsCollectorTest, SingleCallback) { rtc::scoped_refptr result; - collector_->GetStatsReport(RTCStatsObtainer::Create(&result)); + stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&result)); EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs); } TEST_F(RTCStatsCollectorTest, MultipleCallbacks) { - rtc::scoped_refptr a; - rtc::scoped_refptr b; - rtc::scoped_refptr c; - collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); - collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); - collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); + rtc::scoped_refptr a, b, c; + stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&a)); + stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&b)); + stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&c)); EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); + EXPECT_EQ(a.get(), b.get()); EXPECT_EQ(b.get(), c.get()); } TEST_F(RTCStatsCollectorTest, CachedStatsReports) { // Caching should ensure |a| and |b| are the same report. - rtc::scoped_refptr a = GetStatsReport(); - rtc::scoped_refptr b = GetStatsReport(); + rtc::scoped_refptr a = stats_->GetStatsReport(); + rtc::scoped_refptr b = stats_->GetStatsReport(); EXPECT_EQ(a.get(), b.get()); // Invalidate cache by clearing it. - collector_->ClearCachedStatsReport(); - rtc::scoped_refptr c = GetStatsReport(); + stats_->stats_collector()->ClearCachedStatsReport(); + rtc::scoped_refptr c = stats_->GetStatsReport(); EXPECT_NE(b.get(), c.get()); // Invalidate cache by advancing time. - test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); - rtc::scoped_refptr d = GetStatsReport(); + fake_clock_.AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); + rtc::scoped_refptr d = stats_->GetStatsReport(); EXPECT_TRUE(d); EXPECT_NE(c.get(), d.get()); } TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { - rtc::scoped_refptr a; - rtc::scoped_refptr b; - rtc::scoped_refptr c; - collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); - collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); + rtc::scoped_refptr a, b, c; + stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&a)); + stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&b)); // Cache is invalidated after 50 ms. - test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); - collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); + fake_clock_.AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); + stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&c)); EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); @@ -756,58 +564,29 @@ TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { } TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) { + const char kTransportName[] = "transport"; + + pc_->AddVoiceChannel("audio", kTransportName); + std::unique_ptr local_certinfo = CreateFakeCertificateAndInfoFromDers( std::vector({ "(local) single certificate" })); + pc_->SetLocalCertificate(kTransportName, local_certinfo->certificate); + std::unique_ptr remote_certinfo = CreateFakeCertificateAndInfoFromDers( std::vector({ "(remote) single certificate" })); + pc_->SetRemoteCertificate( + kTransportName, + remote_certinfo->certificate->ssl_certificate().GetUniqueReference()); - // Mock the session to return the local and remote certificates. - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([](const ChannelNamePairs&) { - std::unique_ptr stats(new SessionStats()); - stats->transport_stats["transport"].transport_name = "transport"; - return stats; - })); - EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _)) - .WillRepeatedly( - Invoke([&local_certinfo]( - const std::string& transport_name, - rtc::scoped_refptr* certificate) { - if (transport_name == "transport") { - *certificate = local_certinfo->certificate; - return true; - } - return false; - })); - EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_)) - .WillRepeatedly(Invoke([&remote_certinfo]( - const std::string& transport_name) { - if (transport_name == "transport") - return remote_certinfo->certificate->ssl_certificate().GetReference(); - return static_cast(nullptr); - })); + rtc::scoped_refptr report = stats_->GetStatsReport(); - rtc::scoped_refptr report = GetStatsReport(); ExpectReportContainsCertificateInfo(report, *local_certinfo); ExpectReportContainsCertificateInfo(report, *remote_certinfo); } TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) { - auto* voice_media_channel = new MockVoiceMediaChannel(); - cricket::VoiceChannel voice_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), test_->media_engine(), - rtc::WrapUnique(voice_media_channel), "VoiceContentName", - kDefaultRtcpMuxRequired, kDefaultSrtpRequired); - - auto* video_media_channel = new MockVideoMediaChannel(); - cricket::VideoChannel video_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), rtc::WrapUnique(video_media_channel), - "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired); - // Audio cricket::VoiceMediaInfo voice_media_info; @@ -827,8 +606,8 @@ TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) { voice_media_info.send_codecs.insert( std::make_pair(outbound_audio_codec.payload_type, outbound_audio_codec)); - EXPECT_CALL(*voice_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); + auto* voice_media_channel = pc_->AddVoiceChannel("audio", "TransportName"); + voice_media_channel->SetStats(voice_media_info); // Video cricket::VideoMediaInfo video_media_info; @@ -849,23 +628,10 @@ TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) { video_media_info.send_codecs.insert( std::make_pair(outbound_video_codec.payload_type, outbound_video_codec)); - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); + auto* video_media_channel = pc_->AddVideoChannel("video", "TransportName"); + video_media_channel->SetStats(video_media_info); - SessionStats session_stats; - session_stats.transport_stats["TransportName"].transport_name = - "TransportName"; - - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); - EXPECT_CALL(test_->pc(), voice_channel()) - .WillRepeatedly(Return(&voice_channel)); - EXPECT_CALL(test_->pc(), video_channel()) - .WillRepeatedly(Return(&video_channel)); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCCodecStats expected_inbound_audio_codec( "RTCCodec_InboundAudio_1", report->timestamp_us()); @@ -913,66 +679,34 @@ TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) { } TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) { + const char kAudioTransport[] = "audio"; + const char kVideoTransport[] = "video"; + + pc_->AddVoiceChannel("audio", kAudioTransport); std::unique_ptr audio_local_certinfo = CreateFakeCertificateAndInfoFromDers( std::vector({ "(local) audio" })); - audio_local_certinfo = CreateFakeCertificateAndInfoFromDers( - audio_local_certinfo->ders); + pc_->SetLocalCertificate(kAudioTransport, audio_local_certinfo->certificate); std::unique_ptr audio_remote_certinfo = CreateFakeCertificateAndInfoFromDers( std::vector({ "(remote) audio" })); - audio_remote_certinfo = CreateFakeCertificateAndInfoFromDers( - audio_remote_certinfo->ders); + pc_->SetRemoteCertificate( + kAudioTransport, audio_remote_certinfo->certificate->ssl_certificate() + .GetUniqueReference()); + pc_->AddVideoChannel("video", kVideoTransport); std::unique_ptr video_local_certinfo = CreateFakeCertificateAndInfoFromDers( std::vector({ "(local) video" })); - video_local_certinfo = CreateFakeCertificateAndInfoFromDers( - video_local_certinfo->ders); + pc_->SetLocalCertificate(kVideoTransport, video_local_certinfo->certificate); std::unique_ptr video_remote_certinfo = CreateFakeCertificateAndInfoFromDers( std::vector({ "(remote) video" })); - video_remote_certinfo = CreateFakeCertificateAndInfoFromDers( - video_remote_certinfo->ders); + pc_->SetRemoteCertificate( + kVideoTransport, video_remote_certinfo->certificate->ssl_certificate() + .GetUniqueReference()); - // Mock the session to return the local and remote certificates. - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([](const ChannelNamePairs&) { - std::unique_ptr stats(new SessionStats()); - stats->transport_stats["audio"].transport_name = "audio"; - stats->transport_stats["video"].transport_name = "video"; - return stats; - })); - EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _)) - .WillRepeatedly( - Invoke([&audio_local_certinfo, &video_local_certinfo]( - const std::string& transport_name, - rtc::scoped_refptr* certificate) { - if (transport_name == "audio") { - *certificate = audio_local_certinfo->certificate; - return true; - } - if (transport_name == "video") { - *certificate = video_local_certinfo->certificate; - return true; - } - return false; - })); - EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_)) - .WillRepeatedly(Invoke([&audio_remote_certinfo, &video_remote_certinfo]( - const std::string& transport_name) { - if (transport_name == "audio") { - return audio_remote_certinfo->certificate->ssl_certificate() - .GetReference(); - } - if (transport_name == "video") { - return video_remote_certinfo->certificate->ssl_certificate() - .GetReference(); - } - return static_cast(nullptr); - })); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); ExpectReportContainsCertificateInfo(report, *audio_local_certinfo); ExpectReportContainsCertificateInfo(report, *audio_remote_certinfo); ExpectReportContainsCertificateInfo(report, *video_local_certinfo); @@ -980,56 +714,32 @@ TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) { } TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsChain) { - std::vector local_ders; - local_ders.push_back("(local) this"); - local_ders.push_back("(local) is"); - local_ders.push_back("(local) a"); - local_ders.push_back("(local) chain"); + const char kTransportName[] = "transport"; + + pc_->AddVoiceChannel("audio", kTransportName); + std::unique_ptr local_certinfo = - CreateFakeCertificateAndInfoFromDers(local_ders); - std::vector remote_ders; - remote_ders.push_back("(remote) this"); - remote_ders.push_back("(remote) is"); - remote_ders.push_back("(remote) another"); - remote_ders.push_back("(remote) chain"); + CreateFakeCertificateAndInfoFromDers( + {"(local) this", "(local) is", "(local) a", "(local) chain"}); + pc_->SetLocalCertificate(kTransportName, local_certinfo->certificate); + std::unique_ptr remote_certinfo = - CreateFakeCertificateAndInfoFromDers(remote_ders); + CreateFakeCertificateAndInfoFromDers({"(remote) this", "(remote) is", + "(remote) another", + "(remote) chain"}); + pc_->SetRemoteCertificate( + kTransportName, + remote_certinfo->certificate->ssl_certificate().GetUniqueReference()); - // Mock the session to return the local and remote certificates. - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([](const ChannelNamePairs&) { - std::unique_ptr stats(new SessionStats()); - stats->transport_stats["transport"].transport_name = "transport"; - return stats; - })); - EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _)) - .WillRepeatedly( - Invoke([&local_certinfo]( - const std::string& transport_name, - rtc::scoped_refptr* certificate) { - if (transport_name == "transport") { - *certificate = local_certinfo->certificate; - return true; - } - return false; - })); - EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_)) - .WillRepeatedly(Invoke([&remote_certinfo]( - const std::string& transport_name) { - if (transport_name == "transport") - return remote_certinfo->certificate->ssl_certificate().GetReference(); - return static_cast(nullptr); - })); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); ExpectReportContainsCertificateInfo(report, *local_certinfo); ExpectReportContainsCertificateInfo(report, *remote_certinfo); } TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { - test_->AddDataChannel(new MockDataChannel(0, "MockDataChannel0", - DataChannelInterface::kConnecting, - "udp", 1, 2, 3, 4)); + pc_->AddSctpDataChannel(new MockDataChannel(0, "MockDataChannel0", + DataChannelInterface::kConnecting, + "udp", 1, 2, 3, 4)); RTCDataChannelStats expected_data_channel0("RTCDataChannel_0", 0); expected_data_channel0.label = "MockDataChannel0"; expected_data_channel0.protocol = "udp"; @@ -1040,7 +750,7 @@ TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { expected_data_channel0.messages_received = 3; expected_data_channel0.bytes_received = 4; - test_->AddDataChannel(new MockDataChannel( + pc_->AddSctpDataChannel(new MockDataChannel( 1, "MockDataChannel1", DataChannelInterface::kOpen, "tcp", 5, 6, 7, 8)); RTCDataChannelStats expected_data_channel1("RTCDataChannel_1", 0); expected_data_channel1.label = "MockDataChannel1"; @@ -1052,9 +762,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { expected_data_channel1.messages_received = 7; expected_data_channel1.bytes_received = 8; - test_->AddDataChannel(new MockDataChannel(2, "MockDataChannel2", - DataChannelInterface::kClosing, - "udp", 9, 10, 11, 12)); + pc_->AddSctpDataChannel(new MockDataChannel(2, "MockDataChannel2", + DataChannelInterface::kClosing, + "udp", 9, 10, 11, 12)); RTCDataChannelStats expected_data_channel2("RTCDataChannel_2", 0); expected_data_channel2.label = "MockDataChannel2"; expected_data_channel2.protocol = "udp"; @@ -1065,9 +775,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { expected_data_channel2.messages_received = 11; expected_data_channel2.bytes_received = 12; - test_->AddDataChannel(new MockDataChannel(3, "MockDataChannel3", - DataChannelInterface::kClosed, - "tcp", 13, 14, 15, 16)); + pc_->AddSctpDataChannel(new MockDataChannel(3, "MockDataChannel3", + DataChannelInterface::kClosed, + "tcp", 13, 14, 15, 16)); RTCDataChannelStats expected_data_channel3("RTCDataChannel_3", 0); expected_data_channel3.label = "MockDataChannel3"; expected_data_channel3.protocol = "tcp"; @@ -1078,7 +788,8 @@ TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { expected_data_channel3.messages_received = 15; expected_data_channel3.bytes_received = 16; - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); + ASSERT_TRUE(report->Get(expected_data_channel0.id())); EXPECT_EQ(expected_data_channel0, report->Get(expected_data_channel0.id())->cast_to< @@ -1186,8 +897,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { expected_b_remote.deleted = false; EXPECT_TRUE(*expected_b_remote.is_remote); - SessionStats session_stats; - cricket::TransportChannelStats a_transport_channel_stats; a_transport_channel_stats.connection_infos.push_back( cricket::ConnectionInfo()); @@ -1201,9 +910,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { *a_local_prflx.get(); a_transport_channel_stats.connection_infos[1].remote_candidate = *a_remote_relay.get(); - session_stats.transport_stats["a"].transport_name = "a"; - session_stats.transport_stats["a"].channel_stats.push_back( - a_transport_channel_stats); + + pc_->AddVoiceChannel("audio", "a"); + pc_->SetTransportStats("a", a_transport_channel_stats); cricket::TransportChannelStats b_transport_channel_stats; b_transport_channel_stats.connection_infos.push_back( @@ -1212,17 +921,11 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { *b_local.get(); b_transport_channel_stats.connection_infos[0].remote_candidate = *b_remote.get(); - session_stats.transport_stats["b"].transport_name = "b"; - session_stats.transport_stats["b"].channel_stats.push_back( - b_transport_channel_stats); - // Mock the session to return the desired candidates. - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); + pc_->AddVideoChannel("video", "b"); + pc_->SetTransportStats("b", b_transport_channel_stats); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); ASSERT_TRUE(report->Get(expected_a_local_host.id())); EXPECT_EQ(expected_a_local_host, @@ -1253,11 +956,7 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { } TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { - auto* video_media_channel = new MockVideoMediaChannel(); - cricket::VideoChannel video_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), rtc::WrapUnique(video_media_channel), - "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired); + const char kTransportName[] = "transport"; std::unique_ptr local_candidate = CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI, @@ -1266,8 +965,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { "42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_UNKNOWN, cricket::LOCAL_PORT_TYPE, 42); - SessionStats session_stats; - cricket::ConnectionInfo connection_info; connection_info.best_connection = false; connection_info.local_candidate = *local_candidate.get(); @@ -1289,25 +986,11 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { cricket::TransportChannelStats transport_channel_stats; transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; transport_channel_stats.connection_infos.push_back(connection_info); - session_stats.transport_stats["transport"].transport_name = "transport"; - session_stats.transport_stats["transport"].channel_stats.push_back( - transport_channel_stats); - // Mock the session to return the desired candidates. - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); + pc_->AddVideoChannel("video", kTransportName); + pc_->SetTransportStats(kTransportName, transport_channel_stats); - // Mock the session to return bandwidth estimation info. These should only - // be used for a selected candidate pair. - cricket::VideoMediaInfo video_media_info; - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); - EXPECT_CALL(test_->pc(), video_channel()) - .WillRepeatedly(Return(&video_channel)); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCIceCandidatePairStats expected_pair("RTCIceCandidatePair_" + local_candidate->id() + "_" + @@ -1343,14 +1026,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { EXPECT_TRUE(report->Get(*expected_pair.transport_id)); // Set nominated and "GetStats" again. - session_stats.transport_stats["transport"] - .channel_stats[0] - .connection_infos[0] - .nominated = true; - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + transport_channel_stats.connection_infos[0].nominated = true; + pc_->SetTransportStats(kTransportName, transport_channel_stats); + report = stats_->GetFreshStatsReport(); expected_pair.nominated = true; ASSERT_TRUE(report->Get(expected_pair.id())); EXPECT_EQ( @@ -1359,14 +1037,10 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { EXPECT_TRUE(report->Get(*expected_pair.transport_id)); // Set round trip times and "GetStats" again. - session_stats.transport_stats["transport"].channel_stats[0] - .connection_infos[0].total_round_trip_time_ms = 7331; - session_stats.transport_stats["transport"].channel_stats[0] - .connection_infos[0].current_round_trip_time_ms = 1337; - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + transport_channel_stats.connection_infos[0].total_round_trip_time_ms = 7331; + transport_channel_stats.connection_infos[0].current_round_trip_time_ms = 1337; + pc_->SetTransportStats(kTransportName, transport_channel_stats); + report = stats_->GetFreshStatsReport(); expected_pair.total_round_trip_time = 7.331; expected_pair.current_round_trip_time = 1.337; ASSERT_TRUE(report->Get(expected_pair.id())); @@ -1376,14 +1050,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { EXPECT_TRUE(report->Get(*expected_pair.transport_id)); // Make pair the current pair, clear bandwidth and "GetStats" again. - session_stats.transport_stats["transport"] - .channel_stats[0] - .connection_infos[0] - .best_connection = true; - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + transport_channel_stats.connection_infos[0].best_connection = true; + pc_->SetTransportStats(kTransportName, transport_channel_stats); + report = stats_->GetFreshStatsReport(); // |expected_pair.available_[outgoing/incoming]_bitrate| should still be // undefined because bandwidth is not set. ASSERT_TRUE(report->Get(expected_pair.id())); @@ -1398,11 +1067,8 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { call_stats.send_bandwidth_bps = kSendBandwidth; const int kRecvBandwidth = 999; call_stats.recv_bandwidth_bps = kRecvBandwidth; - EXPECT_CALL(test_->pc(), GetCallStats()).WillRepeatedly(Return(call_stats)); - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + pc_->SetCallStats(call_stats); + report = stats_->GetFreshStatsReport(); expected_pair.available_outgoing_bitrate = kSendBandwidth; expected_pair.available_incoming_bitrate = kRecvBandwidth; ASSERT_TRUE(report->Get(expected_pair.id())); @@ -1445,7 +1111,7 @@ TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { { - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCPeerConnectionStats expected("RTCPeerConnection", report->timestamp_us()); expected.data_channels_opened = 0; @@ -1458,18 +1124,18 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { rtc::scoped_refptr dummy_channel_a = DataChannel::Create( nullptr, cricket::DCT_NONE, "DummyChannelA", InternalDataChannelInit()); - test_->pc().SignalDataChannelCreated()(dummy_channel_a.get()); + pc_->SignalDataChannelCreated()(dummy_channel_a.get()); rtc::scoped_refptr dummy_channel_b = DataChannel::Create( nullptr, cricket::DCT_NONE, "DummyChannelB", InternalDataChannelInit()); - test_->pc().SignalDataChannelCreated()(dummy_channel_b.get()); + pc_->SignalDataChannelCreated()(dummy_channel_b.get()); dummy_channel_a->SignalOpened(dummy_channel_a.get()); // Closing a channel that is not opened should not affect the counts. dummy_channel_b->SignalClosed(dummy_channel_b.get()); { - collector_->ClearCachedStatsReport(); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = + stats_->GetFreshStatsReport(); RTCPeerConnectionStats expected("RTCPeerConnection", report->timestamp_us()); expected.data_channels_opened = 1; @@ -1484,8 +1150,8 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { dummy_channel_b->SignalClosed(dummy_channel_b.get()); { - collector_->ClearCachedStatsReport(); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = + stats_->GetFreshStatsReport(); RTCPeerConnectionStats expected("RTCPeerConnection", report->timestamp_us()); expected.data_channels_opened = 2; @@ -1501,8 +1167,8 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { dummy_channel_b->SignalOpened(dummy_channel_b.get()); { - collector_->ClearCachedStatsReport(); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = + stats_->GetFreshStatsReport(); RTCPeerConnectionStats expected("RTCPeerConnection", report->timestamp_us()); expected.data_channels_opened = 3; @@ -1517,8 +1183,8 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { dummy_channel_b->SignalClosed(dummy_channel_b.get()); { - collector_->ClearCachedStatsReport(); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = + stats_->GetFreshStatsReport(); RTCPeerConnectionStats expected("RTCPeerConnection", report->timestamp_us()); expected.data_channels_opened = 3; @@ -1532,14 +1198,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { TEST_F(RTCStatsCollectorTest, CollectLocalRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio) { - rtc::scoped_refptr local_streams = - StreamCollection::Create(); - EXPECT_CALL(test_->pc(), local_streams()) - .WillRepeatedly(Return(local_streams)); - rtc::scoped_refptr local_stream = MediaStream::Create("LocalStreamLabel"); - local_streams->AddStream(local_stream); + pc_->mutable_local_streams()->AddStream(local_stream); // Local audio track rtc::scoped_refptr local_audio_track = @@ -1557,11 +1218,11 @@ TEST_F(RTCStatsCollectorTest, voice_sender_info_ssrc1.apm_statistics.echo_return_loss = 42.0; voice_sender_info_ssrc1.apm_statistics.echo_return_loss_enhancement = 52.0; - test_->CreateMockRtpSendersReceiversAndChannels( + stats_->CreateMockRtpSendersReceiversAndChannels( {std::make_pair(local_audio_track.get(), voice_sender_info_ssrc1)}, {}, {}, {}, {local_stream->label()}, {}); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCMediaStreamStats expected_local_stream( IdForType(report), report->timestamp_us()); @@ -1597,14 +1258,9 @@ TEST_F(RTCStatsCollectorTest, TEST_F(RTCStatsCollectorTest, CollectRemoteRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio) { - rtc::scoped_refptr remote_streams = - StreamCollection::Create(); - EXPECT_CALL(test_->pc(), remote_streams()) - .WillRepeatedly(Return(remote_streams)); - rtc::scoped_refptr remote_stream = MediaStream::Create("RemoteStreamLabel"); - remote_streams->AddStream(remote_stream); + pc_->mutable_remote_streams()->AddStream(remote_stream); // Remote audio track rtc::scoped_refptr remote_audio_track = @@ -1624,11 +1280,11 @@ TEST_F(RTCStatsCollectorTest, voice_receiver_info.concealment_events = 12; voice_receiver_info.jitter_buffer_delay_seconds = 3456; - test_->CreateMockRtpSendersReceiversAndChannels( + stats_->CreateMockRtpSendersReceiversAndChannels( {}, {std::make_pair(remote_audio_track.get(), voice_receiver_info)}, {}, {}, {}, {remote_stream}); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCMediaStreamStats expected_remote_stream( IdForType(report), report->timestamp_us()); @@ -1664,14 +1320,9 @@ TEST_F(RTCStatsCollectorTest, TEST_F(RTCStatsCollectorTest, CollectLocalRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Video) { - rtc::scoped_refptr local_streams = - StreamCollection::Create(); - EXPECT_CALL(test_->pc(), local_streams()) - .WillRepeatedly(Return(local_streams)); - rtc::scoped_refptr local_stream = MediaStream::Create("LocalStreamLabel"); - local_streams->AddStream(local_stream); + pc_->mutable_local_streams()->AddStream(local_stream); // Local video track rtc::scoped_refptr local_video_track = @@ -1687,12 +1338,12 @@ TEST_F(RTCStatsCollectorTest, video_sender_info_ssrc1.send_frame_height = 4321; video_sender_info_ssrc1.frames_encoded = 11; - test_->CreateMockRtpSendersReceiversAndChannels( + stats_->CreateMockRtpSendersReceiversAndChannels( {}, {}, {std::make_pair(local_video_track.get(), video_sender_info_ssrc1)}, {}, {local_stream->label()}, {}); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); auto stats_of_my_type = report->GetStatsOfType(); ASSERT_EQ(1, stats_of_my_type.size()) << "No stream in " << report->ToJson(); @@ -1728,14 +1379,9 @@ TEST_F(RTCStatsCollectorTest, TEST_F(RTCStatsCollectorTest, CollectRemoteRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Video) { - rtc::scoped_refptr remote_streams = - StreamCollection::Create(); - EXPECT_CALL(test_->pc(), remote_streams()) - .WillRepeatedly(Return(remote_streams)); - rtc::scoped_refptr remote_stream = MediaStream::Create("RemoteStreamLabel"); - remote_streams->AddStream(remote_stream); + pc_->mutable_remote_streams()->AddStream(remote_stream); // Remote video track with values rtc::scoped_refptr remote_video_track_ssrc3 = @@ -1753,13 +1399,13 @@ TEST_F(RTCStatsCollectorTest, video_receiver_info_ssrc3.frames_decoded = 995; video_receiver_info_ssrc3.frames_rendered = 990; - test_->CreateMockRtpSendersReceiversAndChannels( + stats_->CreateMockRtpSendersReceiversAndChannels( {}, {}, {}, {std::make_pair(remote_video_track_ssrc3.get(), video_receiver_info_ssrc3)}, {}, {remote_stream}); - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); auto stats_of_my_type = report->GetStatsOfType(); ASSERT_EQ(1, stats_of_my_type.size()) << "No stream in " << report->ToJson(); @@ -1798,16 +1444,7 @@ TEST_F(RTCStatsCollectorTest, } TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { - auto* voice_media_channel = new MockVoiceMediaChannel(); - cricket::VoiceChannel voice_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), test_->media_engine(), - rtc::WrapUnique(voice_media_channel), "VoiceContentName", - kDefaultRtcpMuxRequired, kDefaultSrtpRequired); - voice_channel.set_transport_name_for_testing("TransportName"); - - test_->SetupRemoteTrackAndReceiver( - cricket::MEDIA_TYPE_AUDIO, "RemoteAudioTrackID", 1); + const char kTransportName[] = "TransportName"; cricket::VoiceMediaInfo voice_media_info; @@ -1830,27 +1467,12 @@ TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { voice_media_info.receive_codecs.insert( std::make_pair(codec_parameters.payload_type, codec_parameters)); - EXPECT_CALL(*voice_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); + auto* voice_media_channel = pc_->AddVoiceChannel("voice", kTransportName); + voice_media_channel->SetStats(voice_media_info); + stats_->SetupRemoteTrackAndReceiver(cricket::MEDIA_TYPE_AUDIO, + "RemoteAudioTrackID", 1); - SessionStats session_stats; - session_stats.transport_stats["TransportName"].transport_name = - "TransportName"; - - // Make sure the associated |RTCTransportStats| is created. - cricket::TransportChannelStats channel_stats; - channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; - session_stats.transport_stats["TransportName"].channel_stats.push_back( - channel_stats); - - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); - EXPECT_CALL(test_->pc(), voice_channel()) - .WillRepeatedly(Return(&voice_channel)); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); auto stats_of_track_type = report->GetStatsOfType(); ASSERT_EQ(1, stats_of_track_type.size()); @@ -1861,7 +1483,8 @@ TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { expected_audio.is_remote = false; expected_audio.media_type = "audio"; expected_audio.track_id = stats_of_track_type[0]->id(); - expected_audio.transport_id = "RTCTransport_TransportName_" + + expected_audio.transport_id = + "RTCTransport_TransportName_" + rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); expected_audio.codec_id = "RTCCodec_InboundAudio_42"; expected_audio.packets_received = 2; @@ -1879,16 +1502,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { } TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { - auto* video_media_channel = new MockVideoMediaChannel(); - cricket::VideoChannel video_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), rtc::WrapUnique(video_media_channel), - "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired); - video_channel.set_transport_name_for_testing("TransportName"); - - test_->SetupRemoteTrackAndReceiver( - cricket::MEDIA_TYPE_VIDEO, "RemoteVideoTrackID", 1); - cricket::VideoMediaInfo video_media_info; video_media_info.receivers.push_back(cricket::VideoReceiverInfo()); @@ -1914,27 +1527,12 @@ TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { video_media_info.receive_codecs.insert( std::make_pair(codec_parameters.payload_type, codec_parameters)); - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); + auto* video_media_channel = pc_->AddVideoChannel("video", "TransportName"); + video_media_channel->SetStats(video_media_info); + stats_->SetupRemoteTrackAndReceiver(cricket::MEDIA_TYPE_VIDEO, + "RemoteVideoTrackID", 1); - SessionStats session_stats; - session_stats.transport_stats["TransportName"].transport_name = - "TransportName"; - - // Make sure the associated |RTCTransportStats| is created. - cricket::TransportChannelStats channel_stats; - channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; - session_stats.transport_stats["TransportName"].channel_stats.push_back( - channel_stats); - - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); - EXPECT_CALL(test_->pc(), video_channel()) - .WillRepeatedly(Return(&video_channel)); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCInboundRTPStreamStats expected_video( "RTCInboundRTPVideoStream_1", report->timestamp_us()); @@ -1963,11 +1561,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { // Set previously undefined values and "GetStats" again. video_media_info.receivers[0].qp_sum = 9; expected_video.qp_sum = 9; + video_media_channel->SetStats(video_media_info); - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + report = stats_->GetFreshStatsReport(); ASSERT_TRUE(report->Get(expected_video.id())); EXPECT_EQ( @@ -1979,17 +1575,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { } TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { - auto* voice_media_channel = new MockVoiceMediaChannel(); - cricket::VoiceChannel voice_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), test_->media_engine(), - rtc::WrapUnique(voice_media_channel), "VoiceContentName", - kDefaultRtcpMuxRequired, kDefaultSrtpRequired); - voice_channel.set_transport_name_for_testing("TransportName"); - - test_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO, - "LocalAudioTrackID", 1, true); - cricket::VoiceMediaInfo voice_media_info; voice_media_info.senders.push_back(cricket::VoiceSenderInfo()); @@ -2007,27 +1592,12 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { voice_media_info.send_codecs.insert( std::make_pair(codec_parameters.payload_type, codec_parameters)); - EXPECT_CALL(*voice_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); + auto* voice_media_channel = pc_->AddVoiceChannel("audio", "TransportName"); + voice_media_channel->SetStats(voice_media_info); + stats_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO, + "LocalAudioTrackID", 1, true); - SessionStats session_stats; - session_stats.transport_stats["TransportName"].transport_name = - "TransportName"; - - // Make sure the associated |RTCTransportStats| is created. - cricket::TransportChannelStats channel_stats; - channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; - session_stats.transport_stats["TransportName"].channel_stats.push_back( - channel_stats); - - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); - EXPECT_CALL(test_->pc(), voice_channel()) - .WillRepeatedly(Return(&voice_channel)); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCOutboundRTPStreamStats expected_audio( "RTCOutboundRTPAudioStream_1", report->timestamp_us()); @@ -2056,16 +1626,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { } TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { - auto* video_media_channel = new MockVideoMediaChannel(); - cricket::VideoChannel video_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), rtc::WrapUnique(video_media_channel), - "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired); - video_channel.set_transport_name_for_testing("TransportName"); - - test_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_VIDEO, - "LocalVideoTrackID", 1, true); - cricket::VideoMediaInfo video_media_info; video_media_info.senders.push_back(cricket::VideoSenderInfo()); @@ -2088,27 +1648,12 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { video_media_info.send_codecs.insert( std::make_pair(codec_parameters.payload_type, codec_parameters)); - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); + auto* video_media_channel = pc_->AddVideoChannel("video", "TransportName"); + video_media_channel->SetStats(video_media_info); + stats_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_VIDEO, + "LocalVideoTrackID", 1, true); - SessionStats session_stats; - session_stats.transport_stats["TransportName"].transport_name = - "TransportName"; - - // Make sure the associated |RTCTransportStats| is created. - cricket::TransportChannelStats channel_stats; - channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; - session_stats.transport_stats["TransportName"].channel_stats.push_back( - channel_stats); - - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); - EXPECT_CALL(test_->pc(), video_channel()) - .WillRepeatedly(Return(&video_channel)); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); auto stats_of_my_type = report->GetStatsOfType(); ASSERT_EQ(1, stats_of_my_type.size()); @@ -2140,11 +1685,9 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { // Set previously undefined values and "GetStats" again. video_media_info.senders[0].qp_sum = 9; expected_video.qp_sum = 9; + video_media_channel->SetStats(video_media_info); - EXPECT_CALL(*video_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + report = stats_->GetFreshStatsReport(); ASSERT_TRUE(report->Get(expected_video.id())); EXPECT_EQ( @@ -2156,6 +1699,10 @@ TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { } TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { + const char kTransportName[] = "transport"; + + pc_->AddVoiceChannel("audio", kTransportName); + std::unique_ptr rtp_local_candidate = CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI, cricket::LOCAL_PORT_TYPE, 42); @@ -2171,9 +1718,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { rtc::ADAPTER_TYPE_UNKNOWN, cricket::LOCAL_PORT_TYPE, 42); - SessionStats session_stats; - session_stats.transport_stats["transport"].transport_name = "transport"; - cricket::ConnectionInfo rtp_connection_info; rtp_connection_info.best_connection = false; rtp_connection_info.local_candidate = *rtp_local_candidate.get(); @@ -2184,18 +1728,10 @@ TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { rtp_transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; rtp_transport_channel_stats.connection_infos.push_back(rtp_connection_info); rtp_transport_channel_stats.dtls_state = cricket::DTLS_TRANSPORT_NEW; - session_stats.transport_stats["transport"].channel_stats.push_back( - rtp_transport_channel_stats); - - - // Mock the session to return the desired candidates. - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); + pc_->SetTransportStats(kTransportName, {rtp_transport_channel_stats}); // Get stats without RTCP, an active connection or certificates. - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCTransportStats expected_rtp_transport( "RTCTransport_transport_" + @@ -2221,12 +1757,11 @@ TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { cricket::ICE_CANDIDATE_COMPONENT_RTCP; rtcp_transport_channel_stats.connection_infos.push_back(rtcp_connection_info); rtcp_transport_channel_stats.dtls_state = cricket::DTLS_TRANSPORT_CONNECTING; - session_stats.transport_stats["transport"].channel_stats.push_back( - rtcp_transport_channel_stats); + pc_->SetTransportStats(kTransportName, {rtp_transport_channel_stats, + rtcp_transport_channel_stats}); - collector_->ClearCachedStatsReport(); // Get stats with RTCP and without an active connection or certificates. - report = GetStatsReport(); + report = stats_->GetFreshStatsReport(); RTCTransportStats expected_rtcp_transport( "RTCTransport_transport_" + @@ -2248,13 +1783,11 @@ TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { report->Get(expected_rtcp_transport.id())->cast_to()); // Get stats with an active connection (selected candidate pair). - session_stats.transport_stats["transport"] - .channel_stats[1] - .connection_infos[0] - .best_connection = true; + rtcp_transport_channel_stats.connection_infos[0].best_connection = true; + pc_->SetTransportStats(kTransportName, {rtp_transport_channel_stats, + rtcp_transport_channel_stats}); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + report = stats_->GetFreshStatsReport(); expected_rtcp_transport.selected_candidate_pair_id = "RTCIceCandidatePair_" + rtcp_local_candidate->id() + "_" + @@ -2271,32 +1804,16 @@ TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { // Get stats with certificates. std::unique_ptr local_certinfo = - CreateFakeCertificateAndInfoFromDers( - std::vector({ "(local) local", "(local) chain" })); + CreateFakeCertificateAndInfoFromDers({"(local) local", "(local) chain"}); + pc_->SetLocalCertificate(kTransportName, local_certinfo->certificate); std::unique_ptr remote_certinfo = CreateFakeCertificateAndInfoFromDers( - std::vector({ "(remote) local", "(remote) chain" })); - EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _)) - .WillRepeatedly( - Invoke([&local_certinfo]( - const std::string& transport_name, - rtc::scoped_refptr* certificate) { - if (transport_name == "transport") { - *certificate = local_certinfo->certificate; - return true; - } - return false; - })); - EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_)) - .WillRepeatedly(Invoke([&remote_certinfo]( - const std::string& transport_name) { - if (transport_name == "transport") - return remote_certinfo->certificate->ssl_certificate().GetReference(); - return static_cast(nullptr); - })); + {"(remote) local", "(remote) chain"}); + pc_->SetRemoteCertificate( + kTransportName, + remote_certinfo->certificate->ssl_certificate().GetUniqueReference()); - collector_->ClearCachedStatsReport(); - report = GetStatsReport(); + report = stats_->GetFreshStatsReport(); expected_rtp_transport.local_certificate_id = "RTCCertificate_" + local_certinfo->fingerprints[0]; @@ -2319,18 +1836,6 @@ TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { } TEST_F(RTCStatsCollectorTest, CollectNoStreamRTCOutboundRTPStreamStats_Audio) { - // Emulates the case where AddTrack is used without an associated MediaStream - auto* voice_media_channel = new MockVoiceMediaChannel(); - cricket::VoiceChannel voice_channel( - test_->worker_thread(), test_->network_thread(), - test_->signaling_thread(), test_->media_engine(), - rtc::WrapUnique(voice_media_channel), "VoiceContentName", - kDefaultRtcpMuxRequired, kDefaultSrtpRequired); - voice_channel.set_transport_name_for_testing("TransportName"); - - test_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO, - "LocalAudioTrackID", 1, false); - cricket::VoiceMediaInfo voice_media_info; voice_media_info.senders.push_back(cricket::VoiceSenderInfo()); @@ -2348,27 +1853,13 @@ TEST_F(RTCStatsCollectorTest, CollectNoStreamRTCOutboundRTPStreamStats_Audio) { voice_media_info.send_codecs.insert( std::make_pair(codec_parameters.payload_type, codec_parameters)); - EXPECT_CALL(*voice_media_channel, GetStats(_)) - .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); + // Emulates the case where AddTrack is used without an associated MediaStream + auto* voice_media_channel = pc_->AddVoiceChannel("audio", "TransportName"); + voice_media_channel->SetStats(voice_media_info); + stats_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO, + "LocalAudioTrackID", 1, false); - SessionStats session_stats; - session_stats.transport_stats["TransportName"].transport_name = - "TransportName"; - - // Make sure the associated |RTCTransportStats| is created. - cricket::TransportChannelStats channel_stats; - channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; - session_stats.transport_stats["TransportName"].channel_stats.push_back( - channel_stats); - - EXPECT_CALL(test_->pc(), GetSessionStats(_)) - .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) { - return std::unique_ptr(new SessionStats(session_stats)); - })); - EXPECT_CALL(test_->pc(), voice_channel()) - .WillRepeatedly(Return(&voice_channel)); - - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); RTCOutboundRTPStreamStats expected_audio("RTCOutboundRTPAudioStream_1", report->timestamp_us()); @@ -2400,14 +1891,14 @@ TEST_F(RTCStatsCollectorTest, StatsReportedOnZeroSsrc) { CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "audioTrack", MediaStreamTrackInterface::kLive); rtc::scoped_refptr sender = CreateMockSender(track, 0, 49, {}); - EXPECT_CALL(test_->pc(), GetSenders()) - .WillRepeatedly( - Return(std::vector>( - {rtc::scoped_refptr(sender.get())}))); - rtc::scoped_refptr report = GetStatsReport(); + pc_->AddSender(sender); + + rtc::scoped_refptr report = stats_->GetStatsReport(); + std::vector track_stats = report->GetStatsOfType(); EXPECT_EQ(1, track_stats.size()); + std::vector rtp_stream_stats = report->GetStatsOfType(); EXPECT_EQ(0, rtp_stream_stats.size()); @@ -2419,32 +1910,124 @@ TEST_F(RTCStatsCollectorTest, DoNotCrashOnSsrcChange) { MediaStreamTrackInterface::kLive); rtc::scoped_refptr sender = CreateMockSender(track, 4711, 49, {}); - EXPECT_CALL(test_->pc(), GetSenders()) - .WillRepeatedly( - Return(std::vector>( - {rtc::scoped_refptr(sender.get())}))); + pc_->AddSender(sender); + // We do not generate any matching voice_sender_info stats. - rtc::scoped_refptr report = GetStatsReport(); + rtc::scoped_refptr report = stats_->GetStatsReport(); + std::vector track_stats = report->GetStatsOfType(); EXPECT_EQ(1, track_stats.size()); } -class RTCStatsCollectorTestWithFakeCollector : public testing::Test { +class RTCTestStats : public RTCStats { public: - RTCStatsCollectorTestWithFakeCollector() - : test_(new rtc::RefCountedObject()), - collector_(FakeRTCStatsCollector::Create( - &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { + WEBRTC_RTCSTATS_DECL(); + + RTCTestStats(const std::string& id, int64_t timestamp_us) + : RTCStats(id, timestamp_us), dummy_stat("dummyStat") {} + + RTCStatsMember dummy_stat; +}; + +WEBRTC_RTCSTATS_IMPL(RTCTestStats, RTCStats, "test-stats", &dummy_stat); + +// Overrides the stats collection to verify thread usage and that the resulting +// partial reports are merged. +class FakeRTCStatsCollector : public RTCStatsCollector, + public RTCStatsCollectorCallback { + public: + static rtc::scoped_refptr Create( + PeerConnectionInternal* pc, + int64_t cache_lifetime_us) { + return rtc::scoped_refptr( + new rtc::RefCountedObject(pc, + cache_lifetime_us)); + } + + // RTCStatsCollectorCallback implementation. + void OnStatsDelivered( + const rtc::scoped_refptr& report) override { + EXPECT_TRUE(signaling_thread_->IsCurrent()); + rtc::CritScope cs(&lock_); + delivered_report_ = report; + } + + void VerifyThreadUsageAndResultsMerging() { + GetStatsReport(rtc::scoped_refptr(this)); + EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs); + } + + bool HasVerifiedResults() { + EXPECT_TRUE(signaling_thread_->IsCurrent()); + rtc::CritScope cs(&lock_); + if (!delivered_report_) + return false; + EXPECT_EQ(produced_on_signaling_thread_, 1); + EXPECT_EQ(produced_on_network_thread_, 1); + + EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats")); + EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats")); + + produced_on_signaling_thread_ = 0; + produced_on_network_thread_ = 0; + delivered_report_ = nullptr; + return true; } protected: - rtc::scoped_refptr test_; - rtc::scoped_refptr collector_; + FakeRTCStatsCollector(PeerConnectionInternal* pc, int64_t cache_lifetime) + : RTCStatsCollector(pc, cache_lifetime), + signaling_thread_(pc->signaling_thread()), + worker_thread_(pc->worker_thread()), + network_thread_(pc->network_thread()) {} + + void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override { + EXPECT_TRUE(signaling_thread_->IsCurrent()); + { + rtc::CritScope cs(&lock_); + EXPECT_FALSE(delivered_report_); + ++produced_on_signaling_thread_; + } + + rtc::scoped_refptr signaling_report = + RTCStatsReport::Create(0); + signaling_report->AddStats(std::unique_ptr( + new RTCTestStats("SignalingThreadStats", timestamp_us))); + AddPartialResults(signaling_report); + } + void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override { + EXPECT_TRUE(network_thread_->IsCurrent()); + { + rtc::CritScope cs(&lock_); + EXPECT_FALSE(delivered_report_); + ++produced_on_network_thread_; + } + + rtc::scoped_refptr network_report = + RTCStatsReport::Create(0); + network_report->AddStats(std::unique_ptr( + new RTCTestStats("NetworkThreadStats", timestamp_us))); + AddPartialResults(network_report); + } + + private: + rtc::Thread* const signaling_thread_; + rtc::Thread* const worker_thread_; + rtc::Thread* const network_thread_; + + rtc::CriticalSection lock_; + rtc::scoped_refptr delivered_report_; + int produced_on_signaling_thread_ = 0; + int produced_on_network_thread_ = 0; }; -TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { - collector_->VerifyThreadUsageAndResultsMerging(); +TEST(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { + rtc::scoped_refptr pc( + new rtc::RefCountedObject()); + rtc::scoped_refptr stats_collector( + FakeRTCStatsCollector::Create(pc, 50 * rtc::kNumMicrosecsPerMillisec)); + stats_collector->VerifyThreadUsageAndResultsMerging(); } } // namespace diff --git a/pc/statscollector_unittest.cc b/pc/statscollector_unittest.cc index 6e980e566d..46e7bd24ca 100644 --- a/pc/statscollector_unittest.cc +++ b/pc/statscollector_unittest.cc @@ -646,7 +646,7 @@ class StatsCollectorTest : public testing::Test { auto pc = CreatePeerConnection(); auto stats = CreateStatsCollector(pc); - pc->AddVoiceChannel("audio", kTransportName, VoiceMediaInfo()); + pc->AddVoiceChannel("audio", kTransportName); // Fake stats to process. TransportChannelStats channel_stats; @@ -859,7 +859,9 @@ TEST_P(StatsCollectorTrackTest, BytesCounterHandles64Bits) { video_sender_info.bytes_sent = kBytesSent; VideoMediaInfo video_info; video_info.senders.push_back(video_sender_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_channel = pc->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_info); AddOutgoingVideoTrack(pc, stats.get()); @@ -889,7 +891,9 @@ TEST_P(StatsCollectorTrackTest, AudioBandwidthEstimationInfoIsReported) { voice_sender_info.bytes_sent = kBytesSent; VoiceMediaInfo voice_info; voice_info.senders.push_back(voice_sender_info); - pc->AddVoiceChannel("audio", "transport", voice_info); + + auto* voice_media_channel = pc->AddVoiceChannel("audio", "transport"); + voice_media_channel->SetStats(voice_info); AddOutgoingAudioTrack(pc, stats.get()); @@ -935,7 +939,9 @@ TEST_P(StatsCollectorTrackTest, VideoBandwidthEstimationInfoIsReported) { video_sender_info.bytes_sent = kBytesSent; VideoMediaInfo video_info; video_info.senders.push_back(video_sender_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_channel = pc->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_info); AddOutgoingVideoTrack(pc, stats.get()); @@ -1001,7 +1007,7 @@ TEST_P(StatsCollectorTrackTest, TrackObjectExistsWithoutUpdateStats) { auto pc = CreatePeerConnection(); auto stats = CreateStatsCollector(pc); - pc->AddVideoChannel("video", "transport", VideoMediaInfo()); + pc->AddVideoChannel("video", "transport"); AddOutgoingVideoTrack(pc, stats.get()); // Verfies the existence of the track report. @@ -1031,7 +1037,9 @@ TEST_P(StatsCollectorTrackTest, TrackAndSsrcObjectExistAfterUpdateSsrcStats) { video_sender_info.bytes_sent = kBytesSent; VideoMediaInfo video_info; video_info.senders.push_back(video_sender_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_channel = pc->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_info); AddOutgoingVideoTrack(pc, stats.get()); @@ -1083,7 +1091,9 @@ TEST_P(StatsCollectorTrackTest, TransportObjectLinkedFromSsrcObject) { video_sender_info.bytes_sent = kBytesSent; VideoMediaInfo video_info; video_info.senders.push_back(video_sender_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_channel = pc->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_info); AddOutgoingVideoTrack(pc, stats.get()); @@ -1119,7 +1129,7 @@ TEST_P(StatsCollectorTrackTest, RemoteSsrcInfoIsAbsent) { auto pc = CreatePeerConnection(); auto stats = CreateStatsCollector(pc); - pc->AddVideoChannel("video", "transport", VideoMediaInfo()); + pc->AddVideoChannel("video", "transport"); AddOutgoingVideoTrack(pc, stats.get()); stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); @@ -1145,7 +1155,9 @@ TEST_P(StatsCollectorTrackTest, RemoteSsrcInfoIsPresent) { video_sender_info.remote_stats.push_back(remote_ssrc_stats); VideoMediaInfo video_info; video_info.senders.push_back(video_sender_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_channel = pc->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_info); AddOutgoingVideoTrack(pc, stats.get()); @@ -1172,7 +1184,9 @@ TEST_P(StatsCollectorTrackTest, ReportsFromRemoteTrack) { video_receiver_info.packets_concealed = kNumOfPacketsConcealed; VideoMediaInfo video_info; video_info.receivers.push_back(video_receiver_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_info = pc->AddVideoChannel("video", "transport"); + video_media_info->SetStats(video_info); AddIncomingVideoTrack(pc, stats.get()); @@ -1237,7 +1251,7 @@ TEST_F(StatsCollectorTest, IceCandidateReport) { TransportChannelStats channel_stats; channel_stats.connection_infos.push_back(connection_info); - pc->AddVoiceChannel("audio", kTransportName, VoiceMediaInfo()); + pc->AddVoiceChannel("audio", kTransportName); pc->SetTransportStats(kTransportName, channel_stats); stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); @@ -1355,7 +1369,7 @@ TEST_F(StatsCollectorTest, NoTransport) { // This will cause the fake PeerConnection to generate a TransportStats entry // but with only a single dummy TransportChannelStats. - pc->AddVoiceChannel("audio", "transport", VoiceMediaInfo()); + pc->AddVoiceChannel("audio", "transport"); stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); StatsReports reports; @@ -1459,7 +1473,8 @@ TEST_P(StatsCollectorTrackTest, FilterOutNegativeInitialValues) { voice_info.senders.push_back(voice_sender_info); voice_info.receivers.push_back(voice_receiver_info); - pc->AddVoiceChannel("voice", "transport", voice_info); + auto* voice_media_channel = pc->AddVoiceChannel("voice", "transport"); + voice_media_channel->SetStats(voice_info); stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); @@ -1508,7 +1523,9 @@ TEST_P(StatsCollectorTrackTest, GetStatsFromLocalAudioTrack) { UpdateVoiceSenderInfoFromAudioTrack(audio_track_, &voice_sender_info, false); VoiceMediaInfo voice_info; voice_info.senders.push_back(voice_sender_info); - pc->AddVoiceChannel("audio", "transport", voice_info); + + auto* voice_media_channel = pc->AddVoiceChannel("audio", "transport"); + voice_media_channel->SetStats(voice_info); StatsReports reports; // returned values. VerifyAudioTrackStats(audio_track_, stats.get(), voice_info, &reports); @@ -1533,7 +1550,9 @@ TEST_P(StatsCollectorTrackTest, GetStatsFromRemoteStream) { voice_receiver_info.codec_name = "fake_codec"; VoiceMediaInfo voice_info; voice_info.receivers.push_back(voice_receiver_info); - pc->AddVoiceChannel("audio", "transport", voice_info); + + auto* voice_media_channel = pc->AddVoiceChannel("audio", "transport"); + voice_media_channel->SetStats(voice_info); StatsReports reports; // returned values. VerifyAudioTrackStats(audio_track_, stats.get(), voice_info, &reports); @@ -1553,7 +1572,8 @@ TEST_P(StatsCollectorTrackTest, GetStatsAfterRemoveAudioStream) { VoiceMediaInfo voice_info; voice_info.senders.push_back(voice_sender_info); - pc->AddVoiceChannel("audio", "transport", voice_info); + auto* voice_media_channel = pc->AddVoiceChannel("audio", "transport"); + voice_media_channel->SetStats(voice_info); stats->RemoveLocalAudioTrack(audio_track_.get(), kSsrcOfTrack); @@ -1614,7 +1634,8 @@ TEST_P(StatsCollectorTrackTest, LocalAndRemoteTracksWithSameSsrc) { voice_info.receivers.push_back(voice_receiver_info); // Instruct the session to return stats containing the transport channel. - pc->AddVoiceChannel("audio", "transport", voice_info); + auto* voice_media_channel = pc->AddVoiceChannel("audio", "transport"); + voice_media_channel->SetStats(voice_info); stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); @@ -1667,7 +1688,9 @@ TEST_P(StatsCollectorTrackTest, TwoLocalTracksWithSameSsrc) { voice_sender_info.add_ssrc(kSsrcOfTrack); VoiceMediaInfo voice_info; voice_info.senders.push_back(voice_sender_info); - auto* media_channel = pc->AddVoiceChannel("voice", "transport", voice_info); + + auto* voice_media_channel = pc->AddVoiceChannel("voice", "transport"); + voice_media_channel->SetStats(voice_info); StatsReports reports; // returned values. VerifyAudioTrackStats(audio_track_, stats.get(), voice_info, &reports); @@ -1692,7 +1715,7 @@ TEST_P(StatsCollectorTrackTest, TwoLocalTracksWithSameSsrc) { false); VoiceMediaInfo new_voice_info; new_voice_info.senders.push_back(new_voice_sender_info); - media_channel->SetStats(new_voice_info); + voice_media_channel->SetStats(new_voice_info); reports.clear(); VerifyAudioTrackStats(new_audio_track, stats.get(), new_voice_info, &reports); @@ -1711,7 +1734,9 @@ TEST_P(StatsCollectorTrackTest, VerifyVideoSendSsrcStats) { video_sender_info.qp_sum = 11; VideoMediaInfo video_info; video_info.senders.push_back(video_sender_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_channel = pc->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_info); stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); StatsReports reports; @@ -1737,7 +1762,9 @@ TEST_P(StatsCollectorTrackTest, VerifyVideoReceiveSsrcStatsNew) { video_receiver_info.qp_sum = 11; VideoMediaInfo video_info; video_info.receivers.push_back(video_receiver_info); - pc->AddVideoChannel("video", "transport", video_info); + + auto* video_media_channel = pc->AddVideoChannel("video", "transport"); + video_media_channel->SetStats(video_info); stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); StatsReports reports; diff --git a/pc/test/fakepeerconnectionforstats.h b/pc/test/fakepeerconnectionforstats.h index 7d99ba38c5..bc32e2a7c1 100644 --- a/pc/test/fakepeerconnectionforstats.h +++ b/pc/test/fakepeerconnectionforstats.h @@ -19,6 +19,7 @@ #include #include "media/base/fakemediaengine.h" +#include "pc/streamcollection.h" #include "pc/test/fakedatachannelprovider.h" #include "pc/test/fakepeerconnectionbase.h" @@ -84,12 +85,29 @@ class FakePeerConnectionForStats : public FakePeerConnectionBase { FakePeerConnectionForStats() : network_thread_(rtc::Thread::Current()), worker_thread_(rtc::Thread::Current()), - signaling_thread_(rtc::Thread::Current()) {} + signaling_thread_(rtc::Thread::Current()), + local_streams_(StreamCollection::Create()), + remote_streams_(StreamCollection::Create()) {} + + rtc::scoped_refptr mutable_local_streams() { + return local_streams_; + } + + rtc::scoped_refptr mutable_remote_streams() { + return remote_streams_; + } + + void AddSender(rtc::scoped_refptr sender) { + senders_.push_back(sender); + } + + void AddReceiver(rtc::scoped_refptr receiver) { + receivers_.push_back(receiver); + } FakeVoiceMediaChannelForStats* AddVoiceChannel( const std::string& mid, - const std::string& transport_name, - const cricket::VoiceMediaInfo& voice_info) { + const std::string& transport_name) { RTC_DCHECK(!voice_channel_); auto voice_media_channel = rtc::MakeUnique(); auto* voice_media_channel_ptr = voice_media_channel.get(); @@ -98,14 +116,12 @@ class FakePeerConnectionForStats : public FakePeerConnectionBase { std::move(voice_media_channel), mid, kDefaultRtcpMuxRequired, kDefaultSrtpRequired); voice_channel_->set_transport_name_for_testing(transport_name); - voice_media_channel_ptr->SetStats(voice_info); return voice_media_channel_ptr; } FakeVideoMediaChannelForStats* AddVideoChannel( const std::string& mid, - const std::string& transport_name, - const cricket::VideoMediaInfo& video_stats) { + const std::string& transport_name) { RTC_DCHECK(!video_channel_); auto video_media_channel = rtc::MakeUnique(); auto video_media_channel_ptr = video_media_channel.get(); @@ -114,7 +130,6 @@ class FakePeerConnectionForStats : public FakePeerConnectionBase { std::move(video_media_channel), mid, kDefaultRtcpMuxRequired, kDefaultSrtpRequired); video_channel_->set_transport_name_for_testing(transport_name); - video_media_channel_ptr->SetStats(video_stats); return video_media_channel_ptr; } @@ -132,15 +147,27 @@ class FakePeerConnectionForStats : public FakePeerConnectionBase { void AddSctpDataChannel(const std::string& label, const InternalDataChannelInit& init) { - sctp_data_channels_.push_back(DataChannel::Create( - &data_channel_provider_, cricket::DCT_SCTP, label, init)); + AddSctpDataChannel(DataChannel::Create(&data_channel_provider_, + cricket::DCT_SCTP, label, init)); + } + + void AddSctpDataChannel(rtc::scoped_refptr data_channel) { + sctp_data_channels_.push_back(data_channel); } void SetTransportStats(const std::string& transport_name, const cricket::TransportChannelStats& channel_stats) { + SetTransportStats( + transport_name, + std::vector{channel_stats}); + } + + void SetTransportStats( + const std::string& transport_name, + const std::vector& channel_stats_list) { cricket::TransportStats transport_stats; transport_stats.transport_name = transport_name; - transport_stats.channel_stats.push_back(channel_stats); + transport_stats.channel_stats = channel_stats_list; transport_stats_by_name_[transport_name] = transport_stats; } @@ -157,6 +184,26 @@ class FakePeerConnectionForStats : public FakePeerConnectionBase { remote_certificates_by_transport_[transport_name] = std::move(certificate); } + // PeerConnectionInterface overrides. + + rtc::scoped_refptr local_streams() override { + return local_streams_; + } + + rtc::scoped_refptr remote_streams() override { + return remote_streams_; + } + + std::vector> GetSenders() + const override { + return senders_; + } + + std::vector> GetReceivers() + const override { + return receivers_; + } + // PeerConnectionInternal overrides. rtc::Thread* network_thread() const override { return network_thread_; } @@ -268,7 +315,7 @@ class FakePeerConnectionForStats : public FakePeerConnectionBase { } // Otherwise, generate some dummy stats. cricket::TransportChannelStats channel_stats; - channel_stats.component = 1; + channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; cricket::TransportStats transport_stats; transport_stats.transport_name = transport_name; transport_stats.channel_stats.push_back(channel_stats); @@ -279,6 +326,12 @@ class FakePeerConnectionForStats : public FakePeerConnectionBase { rtc::Thread* const worker_thread_; rtc::Thread* const signaling_thread_; + rtc::scoped_refptr local_streams_; + rtc::scoped_refptr remote_streams_; + + std::vector> senders_; + std::vector> receivers_; + FakeDataChannelProvider data_channel_provider_; std::unique_ptr voice_channel_;