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