blob: 21496950051d161952f6241ea14743554643d730 [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
hbos9e302742017-01-20 02:47:10 -080013#include <initializer_list>
hbosd565b732016-08-30 14:04:35 -070014#include <memory>
hbosda389e32016-10-25 10:55:08 -070015#include <ostream>
hbosd565b732016-08-30 14:04:35 -070016#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070017#include <utility>
hbosd565b732016-08-30 14:04:35 -070018#include <vector>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/jsepsessiondescription.h"
21#include "api/rtpparameters.h"
22#include "api/stats/rtcstats_objects.h"
23#include "api/stats/rtcstatsreport.h"
24#include "api/test/mock_rtpreceiver.h"
25#include "api/test/mock_rtpsender.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/fakemediaengine.h"
27#include "media/base/test/mock_mediachannel.h"
28#include "p2p/base/p2pconstants.h"
29#include "p2p/base/port.h"
30#include "pc/mediastream.h"
31#include "pc/mediastreamtrack.h"
32#include "pc/test/mock_datachannel.h"
33#include "pc/test/mock_peerconnection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "pc/test/rtcstatsobtainer.h"
35#include "rtc_base/checks.h"
36#include "rtc_base/fakeclock.h"
37#include "rtc_base/fakesslidentity.h"
38#include "rtc_base/gunit.h"
39#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/thread_checker.h"
41#include "rtc_base/timedelta.h"
42#include "rtc_base/timeutils.h"
hbosd565b732016-08-30 14:04:35 -070043
hbos6ab97ce2016-10-03 14:16:56 -070044using testing::_;
45using testing::Invoke;
hbosd565b732016-08-30 14:04:35 -070046using testing::Return;
hbos6ded1902016-11-01 01:50:46 -070047using testing::ReturnNull;
hbosd565b732016-08-30 14:04:35 -070048using testing::ReturnRef;
hbos6ded1902016-11-01 01:50:46 -070049using testing::SetArgPointee;
hbosd565b732016-08-30 14:04:35 -070050
51namespace webrtc {
52
hbosda389e32016-10-25 10:55:08 -070053// These are used by gtest code, such as if |EXPECT_EQ| fails.
54void PrintTo(const RTCCertificateStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070055 *os << stats.ToJson();
hbosda389e32016-10-25 10:55:08 -070056}
57
hbos0adb8282016-11-23 02:32:06 -080058void PrintTo(const RTCCodecStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070059 *os << stats.ToJson();
hbos0adb8282016-11-23 02:32:06 -080060}
61
hbosda389e32016-10-25 10:55:08 -070062void PrintTo(const RTCDataChannelStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070063 *os << stats.ToJson();
hbosda389e32016-10-25 10:55:08 -070064}
65
66void PrintTo(const RTCIceCandidatePairStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070067 *os << stats.ToJson();
hbosda389e32016-10-25 10:55:08 -070068}
69
70void PrintTo(const RTCLocalIceCandidateStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070071 *os << stats.ToJson();
hbosda389e32016-10-25 10:55:08 -070072}
73
74void PrintTo(const RTCRemoteIceCandidateStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070075 *os << stats.ToJson();
hbosda389e32016-10-25 10:55:08 -070076}
77
78void PrintTo(const RTCPeerConnectionStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070079 *os << stats.ToJson();
hbosda389e32016-10-25 10:55:08 -070080}
81
hbos09bc1282016-11-08 06:29:22 -080082void PrintTo(const RTCMediaStreamStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070083 *os << stats.ToJson();
hbos09bc1282016-11-08 06:29:22 -080084}
85
86void PrintTo(const RTCMediaStreamTrackStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070087 *os << stats.ToJson();
hbos09bc1282016-11-08 06:29:22 -080088}
89
hboseeafe942016-11-01 03:00:17 -070090void PrintTo(const RTCInboundRTPStreamStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070091 *os << stats.ToJson();
hboseeafe942016-11-01 03:00:17 -070092}
93
hbos6ded1902016-11-01 01:50:46 -070094void PrintTo(const RTCOutboundRTPStreamStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070095 *os << stats.ToJson();
hbos6ded1902016-11-01 01:50:46 -070096}
97
hbosda389e32016-10-25 10:55:08 -070098void PrintTo(const RTCTransportStats& stats, ::std::ostream* os) {
ehmaldonado35a872c2017-07-28 07:29:12 -070099 *os << stats.ToJson();
hbosda389e32016-10-25 10:55:08 -0700100}
101
hbosc82f2e12016-09-05 01:36:50 -0700102namespace {
103
104const int64_t kGetStatsReportTimeoutMs = 1000;
deadbeefac22f702017-01-12 21:59:29 -0800105const bool kDefaultRtcpMuxRequired = true;
deadbeef7af91dd2016-12-13 11:29:11 -0800106const bool kDefaultSrtpRequired = true;
hbosc82f2e12016-09-05 01:36:50 -0700107
hbos6ab97ce2016-10-03 14:16:56 -0700108struct CertificateInfo {
109 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
110 std::vector<std::string> ders;
111 std::vector<std::string> pems;
112 std::vector<std::string> fingerprints;
113};
114
115std::unique_ptr<CertificateInfo> CreateFakeCertificateAndInfoFromDers(
116 const std::vector<std::string>& ders) {
117 RTC_CHECK(!ders.empty());
118 std::unique_ptr<CertificateInfo> info(new CertificateInfo());
119 info->ders = ders;
120 for (const std::string& der : ders) {
121 info->pems.push_back(rtc::SSLIdentity::DerToPem(
122 "CERTIFICATE",
123 reinterpret_cast<const unsigned char*>(der.c_str()),
124 der.length()));
125 }
126 info->certificate =
127 rtc::RTCCertificate::Create(std::unique_ptr<rtc::FakeSSLIdentity>(
128 new rtc::FakeSSLIdentity(rtc::FakeSSLCertificate(info->pems))));
129 // Strip header/footer and newline characters of PEM strings.
130 for (size_t i = 0; i < info->pems.size(); ++i) {
131 rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27,
132 "", 0, &info->pems[i]);
133 rtc::replace_substrs("-----END CERTIFICATE-----", 25,
134 "", 0, &info->pems[i]);
135 rtc::replace_substrs("\n", 1,
136 "", 0, &info->pems[i]);
137 }
138 // Fingerprint of leaf certificate.
139 std::unique_ptr<rtc::SSLFingerprint> fp(
140 rtc::SSLFingerprint::Create("sha-1",
141 &info->certificate->ssl_certificate()));
142 EXPECT_TRUE(fp);
143 info->fingerprints.push_back(fp->GetRfc4572Fingerprint());
144 // Fingerprints of the rest of the chain.
145 std::unique_ptr<rtc::SSLCertChain> chain =
146 info->certificate->ssl_certificate().GetChain();
147 if (chain) {
148 for (size_t i = 0; i < chain->GetSize(); i++) {
149 fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i)));
150 EXPECT_TRUE(fp);
151 info->fingerprints.push_back(fp->GetRfc4572Fingerprint());
152 }
153 }
154 EXPECT_EQ(info->ders.size(), info->fingerprints.size());
155 return info;
156}
157
hbosab9f6e42016-10-07 02:18:47 -0700158std::unique_ptr<cricket::Candidate> CreateFakeCandidate(
159 const std::string& hostname,
160 int port,
161 const std::string& protocol,
Gary Liu37e489c2017-11-21 10:49:36 -0800162 const rtc::AdapterType adapter_type,
hbosab9f6e42016-10-07 02:18:47 -0700163 const std::string& candidate_type,
164 uint32_t priority) {
165 std::unique_ptr<cricket::Candidate> candidate(new cricket::Candidate());
166 candidate->set_address(rtc::SocketAddress(hostname, port));
167 candidate->set_protocol(protocol);
Gary Liu37e489c2017-11-21 10:49:36 -0800168 candidate->set_network_type(adapter_type);
hbosab9f6e42016-10-07 02:18:47 -0700169 candidate->set_type(candidate_type);
170 candidate->set_priority(priority);
171 return candidate;
172}
173
hbos09bc1282016-11-08 06:29:22 -0800174class FakeAudioTrackForStats
175 : public MediaStreamTrack<AudioTrackInterface> {
176 public:
177 static rtc::scoped_refptr<FakeAudioTrackForStats> Create(
178 const std::string& id,
hbos9e302742017-01-20 02:47:10 -0800179 MediaStreamTrackInterface::TrackState state) {
hbos09bc1282016-11-08 06:29:22 -0800180 rtc::scoped_refptr<FakeAudioTrackForStats> audio_track_stats(
hbos9e302742017-01-20 02:47:10 -0800181 new rtc::RefCountedObject<FakeAudioTrackForStats>(id));
hbos09bc1282016-11-08 06:29:22 -0800182 audio_track_stats->set_state(state);
183 return audio_track_stats;
184 }
185
Steve Anton36b29d12017-10-30 09:57:42 -0700186 explicit FakeAudioTrackForStats(const std::string& id)
187 : MediaStreamTrack<AudioTrackInterface>(id) {}
hbos09bc1282016-11-08 06:29:22 -0800188
189 std::string kind() const override {
190 return MediaStreamTrackInterface::kAudioKind;
191 }
192 webrtc::AudioSourceInterface* GetSource() const override { return nullptr; }
193 void AddSink(webrtc::AudioTrackSinkInterface* sink) override {}
194 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override {}
hbos9e302742017-01-20 02:47:10 -0800195 bool GetSignalLevel(int* level) override { return false; }
hbos09bc1282016-11-08 06:29:22 -0800196 rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() override {
hbos9e302742017-01-20 02:47:10 -0800197 return nullptr;
hbos09bc1282016-11-08 06:29:22 -0800198 }
hbos09bc1282016-11-08 06:29:22 -0800199};
200
201class FakeVideoTrackForStats
202 : public MediaStreamTrack<VideoTrackInterface> {
203 public:
204 static rtc::scoped_refptr<FakeVideoTrackForStats> Create(
205 const std::string& id,
hbos9e302742017-01-20 02:47:10 -0800206 MediaStreamTrackInterface::TrackState state) {
hbos09bc1282016-11-08 06:29:22 -0800207 rtc::scoped_refptr<FakeVideoTrackForStats> video_track(
hbos9e302742017-01-20 02:47:10 -0800208 new rtc::RefCountedObject<FakeVideoTrackForStats>(id));
hbos09bc1282016-11-08 06:29:22 -0800209 video_track->set_state(state);
210 return video_track;
211 }
212
Steve Anton36b29d12017-10-30 09:57:42 -0700213 explicit FakeVideoTrackForStats(const std::string& id)
214 : MediaStreamTrack<VideoTrackInterface>(id) {}
hbos09bc1282016-11-08 06:29:22 -0800215
216 std::string kind() const override {
217 return MediaStreamTrackInterface::kVideoKind;
218 }
perkj773be362017-07-31 23:22:01 -0700219
220 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
221 const rtc::VideoSinkWants& wants) override{};
222 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override{};
223
hbos9e302742017-01-20 02:47:10 -0800224 VideoTrackSourceInterface* GetSource() const override { return nullptr; }
hbos09bc1282016-11-08 06:29:22 -0800225};
226
hbos84abeb12017-01-16 06:16:44 -0800227rtc::scoped_refptr<MediaStreamTrackInterface> CreateFakeTrack(
228 cricket::MediaType media_type,
hbos9e302742017-01-20 02:47:10 -0800229 const std::string& track_id,
230 MediaStreamTrackInterface::TrackState track_state) {
hbos84abeb12017-01-16 06:16:44 -0800231 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
hbos9e302742017-01-20 02:47:10 -0800232 return FakeAudioTrackForStats::Create(track_id, track_state);
hbos84abeb12017-01-16 06:16:44 -0800233 } else {
234 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
hbos9e302742017-01-20 02:47:10 -0800235 return FakeVideoTrackForStats::Create(track_id, track_state);
hbos84abeb12017-01-16 06:16:44 -0800236 }
237}
238
hbos9e302742017-01-20 02:47:10 -0800239rtc::scoped_refptr<MockRtpSender> CreateMockSender(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100240 const rtc::scoped_refptr<MediaStreamTrackInterface>& track,
241 uint32_t ssrc) {
hbos9e302742017-01-20 02:47:10 -0800242 rtc::scoped_refptr<MockRtpSender> sender(
243 new rtc::RefCountedObject<MockRtpSender>());
244 EXPECT_CALL(*sender, track()).WillRepeatedly(Return(track));
245 EXPECT_CALL(*sender, ssrc()).WillRepeatedly(Return(ssrc));
246 EXPECT_CALL(*sender, media_type()).WillRepeatedly(Return(
247 track->kind() == MediaStreamTrackInterface::kAudioKind
248 ? cricket::MEDIA_TYPE_AUDIO : cricket::MEDIA_TYPE_VIDEO));
249 EXPECT_CALL(*sender, GetParameters()).WillRepeatedly(Invoke(
250 [ssrc]() {
251 RtpParameters params;
252 params.encodings.push_back(RtpEncodingParameters());
Oskar Sundbomcbc71b22017-11-16 10:56:07 +0100253 params.encodings[0].ssrc = ssrc;
hbos9e302742017-01-20 02:47:10 -0800254 return params;
255 }));
256 return sender;
257}
258
259rtc::scoped_refptr<MockRtpReceiver> CreateMockReceiver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100260 const rtc::scoped_refptr<MediaStreamTrackInterface>& track,
261 uint32_t ssrc) {
hbos9e302742017-01-20 02:47:10 -0800262 rtc::scoped_refptr<MockRtpReceiver> receiver(
263 new rtc::RefCountedObject<MockRtpReceiver>());
264 EXPECT_CALL(*receiver, track()).WillRepeatedly(Return(track));
265 EXPECT_CALL(*receiver, media_type()).WillRepeatedly(Return(
266 track->kind() == MediaStreamTrackInterface::kAudioKind
267 ? cricket::MEDIA_TYPE_AUDIO : cricket::MEDIA_TYPE_VIDEO));
268 EXPECT_CALL(*receiver, GetParameters()).WillRepeatedly(Invoke(
269 [ssrc]() {
270 RtpParameters params;
271 params.encodings.push_back(RtpEncodingParameters());
Oskar Sundbomcbc71b22017-11-16 10:56:07 +0100272 params.encodings[0].ssrc = ssrc;
hbos9e302742017-01-20 02:47:10 -0800273 return params;
274 }));
275 return receiver;
276}
277
hbosc82f2e12016-09-05 01:36:50 -0700278class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver {
hbosd565b732016-08-30 14:04:35 -0700279 public:
hbosc82f2e12016-09-05 01:36:50 -0700280 RTCStatsCollectorTestHelper()
hbosd565b732016-08-30 14:04:35 -0700281 : worker_thread_(rtc::Thread::Current()),
282 network_thread_(rtc::Thread::Current()),
deadbeefbad5dad2017-01-17 18:32:35 -0800283 signaling_thread_(rtc::Thread::Current()),
hbos6ded1902016-11-01 01:50:46 -0700284 media_engine_(new cricket::FakeMediaEngine()),
Steve Antonba818672017-11-06 10:21:57 -0800285 pc_factory_(new FakePeerConnectionFactory(
286 std::unique_ptr<cricket::MediaEngineInterface>(media_engine_))),
Steve Anton75737c02017-11-06 10:37:17 -0800287 pc_(pc_factory_) {
hbos6ab97ce2016-10-03 14:16:56 -0700288 // Default return values for mocks.
Harald Alvestrand89061872018-01-02 14:08:34 +0100289 EXPECT_CALL(pc_, GetCallStats()).WillRepeatedly(Return(Call::Stats()));
hbos09bc1282016-11-08 06:29:22 -0800290 EXPECT_CALL(pc_, local_streams()).WillRepeatedly(Return(nullptr));
291 EXPECT_CALL(pc_, remote_streams()).WillRepeatedly(Return(nullptr));
hbos84abeb12017-01-16 06:16:44 -0800292 EXPECT_CALL(pc_, GetSenders()).WillRepeatedly(Return(
293 std::vector<rtc::scoped_refptr<RtpSenderInterface>>()));
294 EXPECT_CALL(pc_, GetReceivers()).WillRepeatedly(Return(
295 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>()));
hbosd565b732016-08-30 14:04:35 -0700296 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly(
297 ReturnRef(data_channels_));
Steve Anton75737c02017-11-06 10:37:17 -0800298 EXPECT_CALL(pc_, video_channel()).WillRepeatedly(ReturnNull());
299 EXPECT_CALL(pc_, voice_channel()).WillRepeatedly(ReturnNull());
300 EXPECT_CALL(pc_, GetSessionStats(_)).WillRepeatedly(ReturnNull());
301 EXPECT_CALL(pc_, GetLocalCertificate(_, _)).WillRepeatedly(Return(false));
302 EXPECT_CALL(pc_, GetRemoteSSLCertificate_ReturnsRawPointer(_))
hbosab9f6e42016-10-07 02:18:47 -0700303 .WillRepeatedly(Return(nullptr));
hbosd565b732016-08-30 14:04:35 -0700304 }
305
hbosfdafab82016-09-14 06:02:13 -0700306 rtc::ScopedFakeClock& fake_clock() { return fake_clock_; }
hbos6ded1902016-11-01 01:50:46 -0700307 rtc::Thread* worker_thread() { return worker_thread_; }
308 rtc::Thread* network_thread() { return network_thread_; }
deadbeefbad5dad2017-01-17 18:32:35 -0800309 rtc::Thread* signaling_thread() { return signaling_thread_; }
hbos6ded1902016-11-01 01:50:46 -0700310 cricket::FakeMediaEngine* media_engine() { return media_engine_; }
hbosd565b732016-08-30 14:04:35 -0700311 MockPeerConnection& pc() { return pc_; }
312 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() {
313 return data_channels_;
314 }
315
316 // SetSessionDescriptionObserver overrides.
317 void OnSuccess() override {}
318 void OnFailure(const std::string& error) override {
319 RTC_NOTREACHED() << error;
320 }
321
hbos84abeb12017-01-16 06:16:44 -0800322 void SetupLocalTrackAndSender(cricket::MediaType media_type,
323 const std::string& track_id,
Harald Alvestrand89061872018-01-02 14:08:34 +0100324 uint32_t ssrc,
325 bool add_stream) {
hbos84abeb12017-01-16 06:16:44 -0800326 rtc::scoped_refptr<StreamCollection> local_streams =
327 StreamCollection::Create();
328 EXPECT_CALL(pc_, local_streams())
329 .WillRepeatedly(Return(local_streams));
330
Harald Alvestrand89061872018-01-02 14:08:34 +0100331 rtc::scoped_refptr<MediaStream> local_stream;
332 if (add_stream) {
333 local_stream = MediaStream::Create("LocalStreamLabel");
334 local_streams->AddStream(local_stream);
335 }
hbos84abeb12017-01-16 06:16:44 -0800336
337 rtc::scoped_refptr<MediaStreamTrackInterface> track;
338 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
hbos9e302742017-01-20 02:47:10 -0800339 track = CreateFakeTrack(media_type, track_id,
340 MediaStreamTrackInterface::kLive);
Harald Alvestrand89061872018-01-02 14:08:34 +0100341 if (add_stream) {
342 local_stream->AddTrack(static_cast<AudioTrackInterface*>(track.get()));
343 }
hbos84abeb12017-01-16 06:16:44 -0800344 } else {
hbos9e302742017-01-20 02:47:10 -0800345 track = CreateFakeTrack(media_type, track_id,
346 MediaStreamTrackInterface::kLive);
Harald Alvestrand89061872018-01-02 14:08:34 +0100347 if (add_stream) {
348 local_stream->AddTrack(static_cast<VideoTrackInterface*>(track.get()));
349 }
hbos84abeb12017-01-16 06:16:44 -0800350 }
351
hbos9e302742017-01-20 02:47:10 -0800352 rtc::scoped_refptr<MockRtpSender> sender = CreateMockSender(track, ssrc);
hbos84abeb12017-01-16 06:16:44 -0800353 EXPECT_CALL(pc_, GetSenders()).WillRepeatedly(Return(
354 std::vector<rtc::scoped_refptr<RtpSenderInterface>>({
355 rtc::scoped_refptr<RtpSenderInterface>(sender.get()) })));
356 }
357
358 void SetupRemoteTrackAndReceiver(cricket::MediaType media_type,
359 const std::string& track_id,
360 uint32_t ssrc) {
361 rtc::scoped_refptr<StreamCollection> remote_streams =
362 StreamCollection::Create();
363 EXPECT_CALL(pc_, remote_streams())
364 .WillRepeatedly(Return(remote_streams));
365
366 rtc::scoped_refptr<MediaStream> remote_stream =
367 MediaStream::Create("RemoteStreamLabel");
368 remote_streams->AddStream(remote_stream);
369
370 rtc::scoped_refptr<MediaStreamTrackInterface> track;
371 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
hbos9e302742017-01-20 02:47:10 -0800372 track = CreateFakeTrack(media_type, track_id,
373 MediaStreamTrackInterface::kLive);
hbos84abeb12017-01-16 06:16:44 -0800374 remote_stream->AddTrack(static_cast<AudioTrackInterface*>(track.get()));
375 } else {
hbos9e302742017-01-20 02:47:10 -0800376 track = CreateFakeTrack(media_type, track_id,
377 MediaStreamTrackInterface::kLive);
hbos84abeb12017-01-16 06:16:44 -0800378 remote_stream->AddTrack(static_cast<VideoTrackInterface*>(track.get()));
379 }
380
hbos9e302742017-01-20 02:47:10 -0800381 rtc::scoped_refptr<MockRtpReceiver> receiver =
382 CreateMockReceiver(track, ssrc);
hbos84abeb12017-01-16 06:16:44 -0800383 EXPECT_CALL(pc_, GetReceivers()).WillRepeatedly(Return(
384 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>({
385 rtc::scoped_refptr<RtpReceiverInterface>(receiver.get()) })));
386 }
387
hbos9e302742017-01-20 02:47:10 -0800388 // Attaches tracks to peer connections by configuring RTP senders and RTP
389 // receivers according to the tracks' pairings with
390 // |[Voice/Video][Sender/Receiver]Info| and their SSRCs. Local tracks can be
391 // associated with multiple |[Voice/Video]SenderInfo|s, remote tracks can only
392 // be associated with one |[Voice/Video]ReceiverInfo|.
393 void CreateMockRtpSendersReceiversAndChannels(
394 std::initializer_list<std::pair<MediaStreamTrackInterface*,
395 cricket::VoiceSenderInfo>> local_audio_track_info_pairs,
396 std::initializer_list<std::pair<MediaStreamTrackInterface*,
397 cricket::VoiceReceiverInfo>> remote_audio_track_info_pairs,
398 std::initializer_list<std::pair<MediaStreamTrackInterface*,
399 cricket::VideoSenderInfo>> local_video_track_info_pairs,
400 std::initializer_list<std::pair<MediaStreamTrackInterface*,
401 cricket::VideoReceiverInfo>> remote_video_track_info_pairs) {
402 voice_media_info_.reset(new cricket::VoiceMediaInfo());
403 video_media_info_.reset(new cricket::VideoMediaInfo());
404 rtp_senders_.clear();
405 rtp_receivers_.clear();
406 // Local audio tracks and voice sender infos
407 for (auto& pair : local_audio_track_info_pairs) {
408 MediaStreamTrackInterface* local_audio_track = pair.first;
409 const cricket::VoiceSenderInfo& voice_sender_info = pair.second;
410 RTC_DCHECK_EQ(local_audio_track->kind(),
411 MediaStreamTrackInterface::kAudioKind);
412
413 voice_media_info_->senders.push_back(voice_sender_info);
414 rtc::scoped_refptr<MockRtpSender> rtp_sender =
415 CreateMockSender(rtc::scoped_refptr<MediaStreamTrackInterface>(
416 local_audio_track),
417 voice_sender_info.local_stats[0].ssrc);
418 rtp_senders_.push_back(rtc::scoped_refptr<RtpSenderInterface>(
419 rtp_sender.get()));
420 }
421 // Remote audio tracks and voice receiver infos
422 for (auto& pair : remote_audio_track_info_pairs) {
423 MediaStreamTrackInterface* remote_audio_track = pair.first;
424 const cricket::VoiceReceiverInfo& voice_receiver_info = pair.second;
425 RTC_DCHECK_EQ(remote_audio_track->kind(),
426 MediaStreamTrackInterface::kAudioKind);
427
428 voice_media_info_->receivers.push_back(voice_receiver_info);
429 rtc::scoped_refptr<MockRtpReceiver> rtp_receiver =
430 CreateMockReceiver(rtc::scoped_refptr<MediaStreamTrackInterface>(
431 remote_audio_track),
432 voice_receiver_info.local_stats[0].ssrc);
433 rtp_receivers_.push_back(rtc::scoped_refptr<RtpReceiverInterface>(
434 rtp_receiver.get()));
435 }
436 // Local video tracks and video sender infos
437 for (auto& pair : local_video_track_info_pairs) {
438 MediaStreamTrackInterface* local_video_track = pair.first;
439 const cricket::VideoSenderInfo& video_sender_info = pair.second;
440 RTC_DCHECK_EQ(local_video_track->kind(),
441 MediaStreamTrackInterface::kVideoKind);
442
443 video_media_info_->senders.push_back(video_sender_info);
444 rtc::scoped_refptr<MockRtpSender> rtp_sender =
445 CreateMockSender(rtc::scoped_refptr<MediaStreamTrackInterface>(
446 local_video_track),
447 video_sender_info.local_stats[0].ssrc);
448 rtp_senders_.push_back(rtc::scoped_refptr<RtpSenderInterface>(
449 rtp_sender.get()));
450 }
451 // Remote video tracks and video receiver infos
452 for (auto& pair : remote_video_track_info_pairs) {
453 MediaStreamTrackInterface* remote_video_track = pair.first;
454 const cricket::VideoReceiverInfo& video_receiver_info = pair.second;
455 RTC_DCHECK_EQ(remote_video_track->kind(),
456 MediaStreamTrackInterface::kVideoKind);
457
458 video_media_info_->receivers.push_back(video_receiver_info);
459 rtc::scoped_refptr<MockRtpReceiver> rtp_receiver =
460 CreateMockReceiver(rtc::scoped_refptr<MediaStreamTrackInterface>(
461 remote_video_track),
462 video_receiver_info.local_stats[0].ssrc);
463 rtp_receivers_.push_back(rtc::scoped_refptr<RtpReceiverInterface>(
464 rtp_receiver.get()));
465 }
466 EXPECT_CALL(pc_, GetSenders()).WillRepeatedly(Return(rtp_senders_));
467 EXPECT_CALL(pc_, GetReceivers()).WillRepeatedly(Return(rtp_receivers_));
468
Steve Anton8699a322017-11-06 15:53:33 -0800469 auto* voice_media_channel = new MockVoiceMediaChannel();
hbos9e302742017-01-20 02:47:10 -0800470 voice_channel_.reset(new cricket::VoiceChannel(
471 worker_thread_, network_thread_, nullptr, media_engine_,
Steve Anton8699a322017-11-06 15:53:33 -0800472 rtc::WrapUnique(voice_media_channel), "VoiceContentName",
473 kDefaultRtcpMuxRequired, kDefaultSrtpRequired));
Steve Anton75737c02017-11-06 10:37:17 -0800474 EXPECT_CALL(pc_, voice_channel())
hbos9e302742017-01-20 02:47:10 -0800475 .WillRepeatedly(Return(voice_channel_.get()));
476 EXPECT_CALL(*voice_media_channel, GetStats(_))
477 .WillOnce(DoAll(SetArgPointee<0>(*voice_media_info_), Return(true)));
478
Steve Anton8699a322017-11-06 15:53:33 -0800479 auto* video_media_channel = new MockVideoMediaChannel();
hbos9e302742017-01-20 02:47:10 -0800480 video_channel_.reset(new cricket::VideoChannel(
Steve Anton8699a322017-11-06 15:53:33 -0800481 worker_thread_, network_thread_, nullptr,
482 rtc::WrapUnique(video_media_channel), "VideoContentName",
483 kDefaultRtcpMuxRequired, kDefaultSrtpRequired));
Steve Anton75737c02017-11-06 10:37:17 -0800484 EXPECT_CALL(pc_, video_channel())
hbos9e302742017-01-20 02:47:10 -0800485 .WillRepeatedly(Return(video_channel_.get()));
486 EXPECT_CALL(*video_media_channel, GetStats(_))
487 .WillOnce(DoAll(SetArgPointee<0>(*video_media_info_), Return(true)));
488 }
489
hbosd565b732016-08-30 14:04:35 -0700490 private:
hbosfdafab82016-09-14 06:02:13 -0700491 rtc::ScopedFakeClock fake_clock_;
hbos09bc1282016-11-08 06:29:22 -0800492 RtcEventLogNullImpl event_log_;
hbosd565b732016-08-30 14:04:35 -0700493 rtc::Thread* const worker_thread_;
494 rtc::Thread* const network_thread_;
deadbeefbad5dad2017-01-17 18:32:35 -0800495 rtc::Thread* const signaling_thread_;
Steve Antonba818672017-11-06 10:21:57 -0800496 // |media_engine_| is actually owned by |pc_factory_|.
hbos6ded1902016-11-01 01:50:46 -0700497 cricket::FakeMediaEngine* media_engine_;
Steve Antonba818672017-11-06 10:21:57 -0800498 rtc::scoped_refptr<FakePeerConnectionFactory> pc_factory_;
hbosd565b732016-08-30 14:04:35 -0700499 MockPeerConnection pc_;
500
501 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_;
hbos9e302742017-01-20 02:47:10 -0800502 std::unique_ptr<cricket::VoiceChannel> voice_channel_;
503 std::unique_ptr<cricket::VideoChannel> video_channel_;
504 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info_;
505 std::unique_ptr<cricket::VideoMediaInfo> video_media_info_;
506 std::vector<rtc::scoped_refptr<RtpSenderInterface>> rtp_senders_;
507 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> rtp_receivers_;
hbosd565b732016-08-30 14:04:35 -0700508};
509
hbosc82f2e12016-09-05 01:36:50 -0700510class RTCTestStats : public RTCStats {
hbosd565b732016-08-30 14:04:35 -0700511 public:
hbosfc5e0502016-10-06 02:06:10 -0700512 WEBRTC_RTCSTATS_DECL();
513
hbosc82f2e12016-09-05 01:36:50 -0700514 RTCTestStats(const std::string& id, int64_t timestamp_us)
515 : RTCStats(id, timestamp_us),
516 dummy_stat("dummyStat") {}
517
hbosc82f2e12016-09-05 01:36:50 -0700518 RTCStatsMember<int32_t> dummy_stat;
519};
520
hbosfc5e0502016-10-06 02:06:10 -0700521WEBRTC_RTCSTATS_IMPL(RTCTestStats, RTCStats, "test-stats",
522 &dummy_stat);
hbosc82f2e12016-09-05 01:36:50 -0700523
524// Overrides the stats collection to verify thread usage and that the resulting
525// partial reports are merged.
526class FakeRTCStatsCollector : public RTCStatsCollector,
527 public RTCStatsCollectorCallback {
528 public:
529 static rtc::scoped_refptr<FakeRTCStatsCollector> Create(
530 PeerConnection* pc,
531 int64_t cache_lifetime_us) {
532 return rtc::scoped_refptr<FakeRTCStatsCollector>(
533 new rtc::RefCountedObject<FakeRTCStatsCollector>(
534 pc, cache_lifetime_us));
535 }
536
537 // RTCStatsCollectorCallback implementation.
538 void OnStatsDelivered(
539 const rtc::scoped_refptr<const RTCStatsReport>& report) override {
540 EXPECT_TRUE(signaling_thread_->IsCurrent());
541 rtc::CritScope cs(&lock_);
542 delivered_report_ = report;
543 }
544
545 void VerifyThreadUsageAndResultsMerging() {
546 GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback>(this));
547 EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs);
548 }
549
550 bool HasVerifiedResults() {
551 EXPECT_TRUE(signaling_thread_->IsCurrent());
552 rtc::CritScope cs(&lock_);
553 if (!delivered_report_)
554 return false;
555 EXPECT_EQ(produced_on_signaling_thread_, 1);
hbosc82f2e12016-09-05 01:36:50 -0700556 EXPECT_EQ(produced_on_network_thread_, 1);
557
558 EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats"));
hbosc82f2e12016-09-05 01:36:50 -0700559 EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats"));
560
561 produced_on_signaling_thread_ = 0;
hbosc82f2e12016-09-05 01:36:50 -0700562 produced_on_network_thread_ = 0;
563 delivered_report_ = nullptr;
564 return true;
hbosd565b732016-08-30 14:04:35 -0700565 }
566
567 protected:
Steve Anton978b8762017-09-29 12:15:02 -0700568 FakeRTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime)
hbosc82f2e12016-09-05 01:36:50 -0700569 : RTCStatsCollector(pc, cache_lifetime),
Steve Anton978b8762017-09-29 12:15:02 -0700570 signaling_thread_(pc->signaling_thread()),
571 worker_thread_(pc->worker_thread()),
572 network_thread_(pc->network_thread()) {}
hbosc82f2e12016-09-05 01:36:50 -0700573
574 void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override {
575 EXPECT_TRUE(signaling_thread_->IsCurrent());
576 {
577 rtc::CritScope cs(&lock_);
578 EXPECT_FALSE(delivered_report_);
579 ++produced_on_signaling_thread_;
580 }
581
582 rtc::scoped_refptr<RTCStatsReport> signaling_report =
hbos6ded1902016-11-01 01:50:46 -0700583 RTCStatsReport::Create(0);
hbosc82f2e12016-09-05 01:36:50 -0700584 signaling_report->AddStats(std::unique_ptr<const RTCStats>(
585 new RTCTestStats("SignalingThreadStats", timestamp_us)));
586 AddPartialResults(signaling_report);
587 }
hbosc82f2e12016-09-05 01:36:50 -0700588 void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override {
589 EXPECT_TRUE(network_thread_->IsCurrent());
590 {
591 rtc::CritScope cs(&lock_);
592 EXPECT_FALSE(delivered_report_);
593 ++produced_on_network_thread_;
594 }
595
596 rtc::scoped_refptr<RTCStatsReport> network_report =
hbos6ded1902016-11-01 01:50:46 -0700597 RTCStatsReport::Create(0);
hbosc82f2e12016-09-05 01:36:50 -0700598 network_report->AddStats(std::unique_ptr<const RTCStats>(
599 new RTCTestStats("NetworkThreadStats", timestamp_us)));
600 AddPartialResults(network_report);
601 }
602
603 private:
604 rtc::Thread* const signaling_thread_;
605 rtc::Thread* const worker_thread_;
606 rtc::Thread* const network_thread_;
607
608 rtc::CriticalSection lock_;
609 rtc::scoped_refptr<const RTCStatsReport> delivered_report_;
610 int produced_on_signaling_thread_ = 0;
hbosc82f2e12016-09-05 01:36:50 -0700611 int produced_on_network_thread_ = 0;
hbosd565b732016-08-30 14:04:35 -0700612};
613
hbosc82f2e12016-09-05 01:36:50 -0700614class RTCStatsCollectorTest : public testing::Test {
615 public:
616 RTCStatsCollectorTest()
617 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()),
618 collector_(RTCStatsCollector::Create(
619 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) {
620 }
621
622 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() {
hbosdb346a72016-11-29 01:57:01 -0800623 rtc::scoped_refptr<RTCStatsObtainer> callback = RTCStatsObtainer::Create();
hbosc82f2e12016-09-05 01:36:50 -0700624 collector_->GetStatsReport(callback);
625 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs);
hboscc555c52016-10-18 12:48:31 -0700626 int64_t after = rtc::TimeUTCMicros();
627 for (const RTCStats& stats : *callback->report()) {
628 EXPECT_LE(stats.timestamp_us(), after);
629 }
hbosc82f2e12016-09-05 01:36:50 -0700630 return callback->report();
631 }
632
hbos6ab97ce2016-10-03 14:16:56 -0700633 void ExpectReportContainsCertificateInfo(
634 const rtc::scoped_refptr<const RTCStatsReport>& report,
hbos23368e12016-12-21 04:29:17 -0800635 const CertificateInfo& certinfo) {
636 for (size_t i = 0; i < certinfo.fingerprints.size(); ++i) {
637 RTCCertificateStats expected_certificate_stats(
638 "RTCCertificate_" + certinfo.fingerprints[i],
639 report->timestamp_us());
640 expected_certificate_stats.fingerprint = certinfo.fingerprints[i];
641 expected_certificate_stats.fingerprint_algorithm = "sha-1";
642 expected_certificate_stats.base64_certificate = certinfo.pems[i];
643 if (i + 1 < certinfo.fingerprints.size()) {
644 expected_certificate_stats.issuer_certificate_id =
645 "RTCCertificate_" + certinfo.fingerprints[i + 1];
hbos6ab97ce2016-10-03 14:16:56 -0700646 }
hbos23368e12016-12-21 04:29:17 -0800647 ASSERT_TRUE(report->Get(expected_certificate_stats.id()));
648 EXPECT_EQ(expected_certificate_stats,
649 report->Get(expected_certificate_stats.id())->cast_to<
650 RTCCertificateStats>());
hbos6ab97ce2016-10-03 14:16:56 -0700651 }
652 }
653
hbosc82f2e12016-09-05 01:36:50 -0700654 protected:
655 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_;
656 rtc::scoped_refptr<RTCStatsCollector> collector_;
657};
658
659TEST_F(RTCStatsCollectorTest, SingleCallback) {
660 rtc::scoped_refptr<const RTCStatsReport> result;
hbosdb346a72016-11-29 01:57:01 -0800661 collector_->GetStatsReport(RTCStatsObtainer::Create(&result));
hbosc82f2e12016-09-05 01:36:50 -0700662 EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs);
663}
664
665TEST_F(RTCStatsCollectorTest, MultipleCallbacks) {
666 rtc::scoped_refptr<const RTCStatsReport> a;
667 rtc::scoped_refptr<const RTCStatsReport> b;
668 rtc::scoped_refptr<const RTCStatsReport> c;
hbosdb346a72016-11-29 01:57:01 -0800669 collector_->GetStatsReport(RTCStatsObtainer::Create(&a));
670 collector_->GetStatsReport(RTCStatsObtainer::Create(&b));
671 collector_->GetStatsReport(RTCStatsObtainer::Create(&c));
hbosc82f2e12016-09-05 01:36:50 -0700672 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
673 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
674 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
675 EXPECT_EQ(a.get(), b.get());
676 EXPECT_EQ(b.get(), c.get());
677}
678
679TEST_F(RTCStatsCollectorTest, CachedStatsReports) {
hbosd565b732016-08-30 14:04:35 -0700680 // Caching should ensure |a| and |b| are the same report.
hbosc82f2e12016-09-05 01:36:50 -0700681 rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport();
682 rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700683 EXPECT_EQ(a.get(), b.get());
684 // Invalidate cache by clearing it.
hbosc82f2e12016-09-05 01:36:50 -0700685 collector_->ClearCachedStatsReport();
686 rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700687 EXPECT_NE(b.get(), c.get());
688 // Invalidate cache by advancing time.
hbosfdafab82016-09-14 06:02:13 -0700689 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
hbosc82f2e12016-09-05 01:36:50 -0700690 rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700691 EXPECT_TRUE(d);
692 EXPECT_NE(c.get(), d.get());
693}
694
hbosc82f2e12016-09-05 01:36:50 -0700695TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) {
hbosc82f2e12016-09-05 01:36:50 -0700696 rtc::scoped_refptr<const RTCStatsReport> a;
697 rtc::scoped_refptr<const RTCStatsReport> b;
698 rtc::scoped_refptr<const RTCStatsReport> c;
hbosdb346a72016-11-29 01:57:01 -0800699 collector_->GetStatsReport(RTCStatsObtainer::Create(&a));
700 collector_->GetStatsReport(RTCStatsObtainer::Create(&b));
hbosc82f2e12016-09-05 01:36:50 -0700701 // Cache is invalidated after 50 ms.
hbosfdafab82016-09-14 06:02:13 -0700702 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
hbosdb346a72016-11-29 01:57:01 -0800703 collector_->GetStatsReport(RTCStatsObtainer::Create(&c));
hbosc82f2e12016-09-05 01:36:50 -0700704 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
705 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
706 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
707 EXPECT_EQ(a.get(), b.get());
708 // The act of doing |AdvanceTime| processes all messages. If this was not the
709 // case we might not require |c| to be fresher than |b|.
710 EXPECT_NE(c.get(), b.get());
711}
712
hbos6ab97ce2016-10-03 14:16:56 -0700713TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) {
714 std::unique_ptr<CertificateInfo> local_certinfo =
715 CreateFakeCertificateAndInfoFromDers(
716 std::vector<std::string>({ "(local) single certificate" }));
717 std::unique_ptr<CertificateInfo> remote_certinfo =
718 CreateFakeCertificateAndInfoFromDers(
719 std::vector<std::string>({ "(remote) single certificate" }));
720
721 // Mock the session to return the local and remote certificates.
Steve Anton75737c02017-11-06 10:37:17 -0800722 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -0800723 .WillRepeatedly(Invoke([](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -0800724 std::unique_ptr<SessionStats> stats(new SessionStats());
hbos6ab97ce2016-10-03 14:16:56 -0700725 stats->transport_stats["transport"].transport_name = "transport";
hbosdf6075a2016-12-19 04:58:02 -0800726 return stats;
hbos6ab97ce2016-10-03 14:16:56 -0700727 }));
Steve Anton75737c02017-11-06 10:37:17 -0800728 EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _))
729 .WillRepeatedly(
730 Invoke([&local_certinfo](
731 const std::string& transport_name,
732 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
733 if (transport_name == "transport") {
734 *certificate = local_certinfo->certificate;
735 return true;
736 }
737 return false;
738 }));
739 EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_))
740 .WillRepeatedly(Invoke([&remote_certinfo](
741 const std::string& transport_name) {
hbos6ab97ce2016-10-03 14:16:56 -0700742 if (transport_name == "transport")
743 return remote_certinfo->certificate->ssl_certificate().GetReference();
744 return static_cast<rtc::SSLCertificate*>(nullptr);
745 }));
746
747 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbos23368e12016-12-21 04:29:17 -0800748 ExpectReportContainsCertificateInfo(report, *local_certinfo);
749 ExpectReportContainsCertificateInfo(report, *remote_certinfo);
hbos6ab97ce2016-10-03 14:16:56 -0700750}
751
hbos0adb8282016-11-23 02:32:06 -0800752TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) {
Steve Anton8699a322017-11-06 15:53:33 -0800753 auto* voice_media_channel = new MockVoiceMediaChannel();
hbos0adb8282016-11-23 02:32:06 -0800754 cricket::VoiceChannel voice_channel(
deadbeefbad5dad2017-01-17 18:32:35 -0800755 test_->worker_thread(), test_->network_thread(),
Steve Anton8699a322017-11-06 15:53:33 -0800756 test_->signaling_thread(), test_->media_engine(),
757 rtc::WrapUnique(voice_media_channel), "VoiceContentName",
758 kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
hbos0adb8282016-11-23 02:32:06 -0800759
Steve Anton8699a322017-11-06 15:53:33 -0800760 auto* video_media_channel = new MockVideoMediaChannel();
hbos0adb8282016-11-23 02:32:06 -0800761 cricket::VideoChannel video_channel(
deadbeefbad5dad2017-01-17 18:32:35 -0800762 test_->worker_thread(), test_->network_thread(),
Steve Anton8699a322017-11-06 15:53:33 -0800763 test_->signaling_thread(), rtc::WrapUnique(video_media_channel),
764 "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
hbos0adb8282016-11-23 02:32:06 -0800765
766 // Audio
767 cricket::VoiceMediaInfo voice_media_info;
768
769 RtpCodecParameters inbound_audio_codec;
770 inbound_audio_codec.payload_type = 1;
deadbeefe702b302017-02-04 12:09:01 -0800771 inbound_audio_codec.kind = cricket::MEDIA_TYPE_AUDIO;
772 inbound_audio_codec.name = "opus";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +0100773 inbound_audio_codec.clock_rate = 1337;
hbos0adb8282016-11-23 02:32:06 -0800774 voice_media_info.receive_codecs.insert(
775 std::make_pair(inbound_audio_codec.payload_type, inbound_audio_codec));
776
777 RtpCodecParameters outbound_audio_codec;
778 outbound_audio_codec.payload_type = 2;
deadbeefe702b302017-02-04 12:09:01 -0800779 outbound_audio_codec.kind = cricket::MEDIA_TYPE_AUDIO;
780 outbound_audio_codec.name = "isac";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +0100781 outbound_audio_codec.clock_rate = 1338;
hbos0adb8282016-11-23 02:32:06 -0800782 voice_media_info.send_codecs.insert(
783 std::make_pair(outbound_audio_codec.payload_type, outbound_audio_codec));
784
785 EXPECT_CALL(*voice_media_channel, GetStats(_))
786 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true)));
787
788 // Video
789 cricket::VideoMediaInfo video_media_info;
790
791 RtpCodecParameters inbound_video_codec;
792 inbound_video_codec.payload_type = 3;
deadbeefe702b302017-02-04 12:09:01 -0800793 inbound_video_codec.kind = cricket::MEDIA_TYPE_VIDEO;
794 inbound_video_codec.name = "H264";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +0100795 inbound_video_codec.clock_rate = 1339;
hbos0adb8282016-11-23 02:32:06 -0800796 video_media_info.receive_codecs.insert(
797 std::make_pair(inbound_video_codec.payload_type, inbound_video_codec));
798
799 RtpCodecParameters outbound_video_codec;
800 outbound_video_codec.payload_type = 4;
deadbeefe702b302017-02-04 12:09:01 -0800801 outbound_video_codec.kind = cricket::MEDIA_TYPE_VIDEO;
802 outbound_video_codec.name = "VP8";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +0100803 outbound_video_codec.clock_rate = 1340;
hbos0adb8282016-11-23 02:32:06 -0800804 video_media_info.send_codecs.insert(
805 std::make_pair(outbound_video_codec.payload_type, outbound_video_codec));
806
807 EXPECT_CALL(*video_media_channel, GetStats(_))
808 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
809
810 SessionStats session_stats;
hbos0adb8282016-11-23 02:32:06 -0800811 session_stats.transport_stats["TransportName"].transport_name =
812 "TransportName";
813
Steve Anton75737c02017-11-06 10:37:17 -0800814 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -0800815 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -0800816 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
817 }));
Steve Anton75737c02017-11-06 10:37:17 -0800818 EXPECT_CALL(test_->pc(), voice_channel())
hbos0adb8282016-11-23 02:32:06 -0800819 .WillRepeatedly(Return(&voice_channel));
Steve Anton75737c02017-11-06 10:37:17 -0800820 EXPECT_CALL(test_->pc(), video_channel())
hbos0adb8282016-11-23 02:32:06 -0800821 .WillRepeatedly(Return(&video_channel));
822
823 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
824
825 RTCCodecStats expected_inbound_audio_codec(
826 "RTCCodec_InboundAudio_1", report->timestamp_us());
827 expected_inbound_audio_codec.payload_type = 1;
hbos13f54b22017-02-28 06:56:04 -0800828 expected_inbound_audio_codec.mime_type = "audio/opus";
hbos0adb8282016-11-23 02:32:06 -0800829 expected_inbound_audio_codec.clock_rate = 1337;
830
831 RTCCodecStats expected_outbound_audio_codec(
832 "RTCCodec_OutboundAudio_2", report->timestamp_us());
833 expected_outbound_audio_codec.payload_type = 2;
hbos13f54b22017-02-28 06:56:04 -0800834 expected_outbound_audio_codec.mime_type = "audio/isac";
hbos0adb8282016-11-23 02:32:06 -0800835 expected_outbound_audio_codec.clock_rate = 1338;
836
837 RTCCodecStats expected_inbound_video_codec(
838 "RTCCodec_InboundVideo_3", report->timestamp_us());
839 expected_inbound_video_codec.payload_type = 3;
hbos13f54b22017-02-28 06:56:04 -0800840 expected_inbound_video_codec.mime_type = "video/H264";
hbos0adb8282016-11-23 02:32:06 -0800841 expected_inbound_video_codec.clock_rate = 1339;
842
843 RTCCodecStats expected_outbound_video_codec(
844 "RTCCodec_OutboundVideo_4", report->timestamp_us());
845 expected_outbound_video_codec.payload_type = 4;
hbos13f54b22017-02-28 06:56:04 -0800846 expected_outbound_video_codec.mime_type = "video/VP8";
hbos0adb8282016-11-23 02:32:06 -0800847 expected_outbound_video_codec.clock_rate = 1340;
848
nissec8ee8822017-01-18 07:20:55 -0800849 ASSERT_TRUE(report->Get(expected_inbound_audio_codec.id()));
hbos0adb8282016-11-23 02:32:06 -0800850 EXPECT_EQ(expected_inbound_audio_codec,
851 report->Get(expected_inbound_audio_codec.id())->cast_to<
852 RTCCodecStats>());
853
nissec8ee8822017-01-18 07:20:55 -0800854 ASSERT_TRUE(report->Get(expected_outbound_audio_codec.id()));
hbos0adb8282016-11-23 02:32:06 -0800855 EXPECT_EQ(expected_outbound_audio_codec,
856 report->Get(expected_outbound_audio_codec.id())->cast_to<
857 RTCCodecStats>());
858
nissec8ee8822017-01-18 07:20:55 -0800859 ASSERT_TRUE(report->Get(expected_inbound_video_codec.id()));
hbos0adb8282016-11-23 02:32:06 -0800860 EXPECT_EQ(expected_inbound_video_codec,
861 report->Get(expected_inbound_video_codec.id())->cast_to<
862 RTCCodecStats>());
863
nissec8ee8822017-01-18 07:20:55 -0800864 ASSERT_TRUE(report->Get(expected_outbound_video_codec.id()));
hbos0adb8282016-11-23 02:32:06 -0800865 EXPECT_EQ(expected_outbound_video_codec,
866 report->Get(expected_outbound_video_codec.id())->cast_to<
867 RTCCodecStats>());
868}
869
hbos6ab97ce2016-10-03 14:16:56 -0700870TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) {
871 std::unique_ptr<CertificateInfo> audio_local_certinfo =
872 CreateFakeCertificateAndInfoFromDers(
873 std::vector<std::string>({ "(local) audio" }));
874 audio_local_certinfo = CreateFakeCertificateAndInfoFromDers(
875 audio_local_certinfo->ders);
876 std::unique_ptr<CertificateInfo> audio_remote_certinfo =
877 CreateFakeCertificateAndInfoFromDers(
878 std::vector<std::string>({ "(remote) audio" }));
879 audio_remote_certinfo = CreateFakeCertificateAndInfoFromDers(
880 audio_remote_certinfo->ders);
881
882 std::unique_ptr<CertificateInfo> video_local_certinfo =
883 CreateFakeCertificateAndInfoFromDers(
884 std::vector<std::string>({ "(local) video" }));
885 video_local_certinfo = CreateFakeCertificateAndInfoFromDers(
886 video_local_certinfo->ders);
887 std::unique_ptr<CertificateInfo> video_remote_certinfo =
888 CreateFakeCertificateAndInfoFromDers(
889 std::vector<std::string>({ "(remote) video" }));
890 video_remote_certinfo = CreateFakeCertificateAndInfoFromDers(
891 video_remote_certinfo->ders);
892
893 // Mock the session to return the local and remote certificates.
Steve Anton75737c02017-11-06 10:37:17 -0800894 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -0800895 .WillRepeatedly(Invoke([](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -0800896 std::unique_ptr<SessionStats> stats(new SessionStats());
hbos6ab97ce2016-10-03 14:16:56 -0700897 stats->transport_stats["audio"].transport_name = "audio";
898 stats->transport_stats["video"].transport_name = "video";
hbosdf6075a2016-12-19 04:58:02 -0800899 return stats;
hbos6ab97ce2016-10-03 14:16:56 -0700900 }));
Steve Anton75737c02017-11-06 10:37:17 -0800901 EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _))
902 .WillRepeatedly(
903 Invoke([&audio_local_certinfo, &video_local_certinfo](
904 const std::string& transport_name,
905 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
906 if (transport_name == "audio") {
907 *certificate = audio_local_certinfo->certificate;
908 return true;
909 }
910 if (transport_name == "video") {
911 *certificate = video_local_certinfo->certificate;
912 return true;
913 }
914 return false;
915 }));
916 EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_))
917 .WillRepeatedly(Invoke([&audio_remote_certinfo, &video_remote_certinfo](
918 const std::string& transport_name) {
hbos6ab97ce2016-10-03 14:16:56 -0700919 if (transport_name == "audio") {
920 return audio_remote_certinfo->certificate->ssl_certificate()
921 .GetReference();
922 }
923 if (transport_name == "video") {
924 return video_remote_certinfo->certificate->ssl_certificate()
925 .GetReference();
926 }
927 return static_cast<rtc::SSLCertificate*>(nullptr);
928 }));
929
930 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbos23368e12016-12-21 04:29:17 -0800931 ExpectReportContainsCertificateInfo(report, *audio_local_certinfo);
932 ExpectReportContainsCertificateInfo(report, *audio_remote_certinfo);
933 ExpectReportContainsCertificateInfo(report, *video_local_certinfo);
934 ExpectReportContainsCertificateInfo(report, *video_remote_certinfo);
hbos6ab97ce2016-10-03 14:16:56 -0700935}
936
937TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsChain) {
938 std::vector<std::string> local_ders;
939 local_ders.push_back("(local) this");
940 local_ders.push_back("(local) is");
941 local_ders.push_back("(local) a");
942 local_ders.push_back("(local) chain");
943 std::unique_ptr<CertificateInfo> local_certinfo =
944 CreateFakeCertificateAndInfoFromDers(local_ders);
945 std::vector<std::string> remote_ders;
946 remote_ders.push_back("(remote) this");
947 remote_ders.push_back("(remote) is");
948 remote_ders.push_back("(remote) another");
949 remote_ders.push_back("(remote) chain");
950 std::unique_ptr<CertificateInfo> remote_certinfo =
951 CreateFakeCertificateAndInfoFromDers(remote_ders);
952
953 // Mock the session to return the local and remote certificates.
Steve Anton75737c02017-11-06 10:37:17 -0800954 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -0800955 .WillRepeatedly(Invoke([](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -0800956 std::unique_ptr<SessionStats> stats(new SessionStats());
hbos6ab97ce2016-10-03 14:16:56 -0700957 stats->transport_stats["transport"].transport_name = "transport";
hbosdf6075a2016-12-19 04:58:02 -0800958 return stats;
hbos6ab97ce2016-10-03 14:16:56 -0700959 }));
Steve Anton75737c02017-11-06 10:37:17 -0800960 EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _))
961 .WillRepeatedly(
962 Invoke([&local_certinfo](
963 const std::string& transport_name,
964 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
965 if (transport_name == "transport") {
966 *certificate = local_certinfo->certificate;
967 return true;
968 }
969 return false;
970 }));
971 EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_))
972 .WillRepeatedly(Invoke([&remote_certinfo](
973 const std::string& transport_name) {
hbos6ab97ce2016-10-03 14:16:56 -0700974 if (transport_name == "transport")
975 return remote_certinfo->certificate->ssl_certificate().GetReference();
976 return static_cast<rtc::SSLCertificate*>(nullptr);
977 }));
978
979 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbos23368e12016-12-21 04:29:17 -0800980 ExpectReportContainsCertificateInfo(report, *local_certinfo);
981 ExpectReportContainsCertificateInfo(report, *remote_certinfo);
hbos6ab97ce2016-10-03 14:16:56 -0700982}
983
hboscc555c52016-10-18 12:48:31 -0700984TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) {
985 test_->data_channels().push_back(
hbosdbb64d82016-12-21 01:57:46 -0800986 new MockDataChannel(
987 0, "MockDataChannel0", DataChannelInterface::kConnecting, "udp",
988 1, 2, 3, 4));
989 RTCDataChannelStats expected_data_channel0("RTCDataChannel_0", 0);
990 expected_data_channel0.label = "MockDataChannel0";
991 expected_data_channel0.protocol = "udp";
992 expected_data_channel0.datachannelid = 0;
993 expected_data_channel0.state = "connecting";
994 expected_data_channel0.messages_sent = 1;
995 expected_data_channel0.bytes_sent = 2;
996 expected_data_channel0.messages_received = 3;
997 expected_data_channel0.bytes_received = 4;
998
hboscc555c52016-10-18 12:48:31 -0700999 test_->data_channels().push_back(
hbosdbb64d82016-12-21 01:57:46 -08001000 new MockDataChannel(
1001 1, "MockDataChannel1", DataChannelInterface::kOpen, "tcp",
1002 5, 6, 7, 8));
1003 RTCDataChannelStats expected_data_channel1("RTCDataChannel_1", 0);
1004 expected_data_channel1.label = "MockDataChannel1";
1005 expected_data_channel1.protocol = "tcp";
1006 expected_data_channel1.datachannelid = 1;
1007 expected_data_channel1.state = "open";
1008 expected_data_channel1.messages_sent = 5;
1009 expected_data_channel1.bytes_sent = 6;
1010 expected_data_channel1.messages_received = 7;
1011 expected_data_channel1.bytes_received = 8;
1012
hboscc555c52016-10-18 12:48:31 -07001013 test_->data_channels().push_back(
hbosdbb64d82016-12-21 01:57:46 -08001014 new MockDataChannel(
1015 2, "MockDataChannel2", DataChannelInterface::kClosing, "udp",
1016 9, 10, 11, 12));
1017 RTCDataChannelStats expected_data_channel2("RTCDataChannel_2", 0);
1018 expected_data_channel2.label = "MockDataChannel2";
1019 expected_data_channel2.protocol = "udp";
1020 expected_data_channel2.datachannelid = 2;
1021 expected_data_channel2.state = "closing";
1022 expected_data_channel2.messages_sent = 9;
1023 expected_data_channel2.bytes_sent = 10;
1024 expected_data_channel2.messages_received = 11;
1025 expected_data_channel2.bytes_received = 12;
1026
hboscc555c52016-10-18 12:48:31 -07001027 test_->data_channels().push_back(
hbosdbb64d82016-12-21 01:57:46 -08001028 new MockDataChannel(
1029 3, "MockDataChannel3", DataChannelInterface::kClosed, "tcp",
1030 13, 14, 15, 16));
1031 RTCDataChannelStats expected_data_channel3("RTCDataChannel_3", 0);
1032 expected_data_channel3.label = "MockDataChannel3";
1033 expected_data_channel3.protocol = "tcp";
1034 expected_data_channel3.datachannelid = 3;
1035 expected_data_channel3.state = "closed";
1036 expected_data_channel3.messages_sent = 13;
1037 expected_data_channel3.bytes_sent = 14;
1038 expected_data_channel3.messages_received = 15;
1039 expected_data_channel3.bytes_received = 16;
hboscc555c52016-10-18 12:48:31 -07001040
1041 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbosdbb64d82016-12-21 01:57:46 -08001042 ASSERT_TRUE(report->Get(expected_data_channel0.id()));
1043 EXPECT_EQ(expected_data_channel0,
1044 report->Get(expected_data_channel0.id())->cast_to<
1045 RTCDataChannelStats>());
1046 ASSERT_TRUE(report->Get(expected_data_channel1.id()));
1047 EXPECT_EQ(expected_data_channel1,
1048 report->Get(expected_data_channel1.id())->cast_to<
1049 RTCDataChannelStats>());
1050 ASSERT_TRUE(report->Get(expected_data_channel2.id()));
1051 EXPECT_EQ(expected_data_channel2,
1052 report->Get(expected_data_channel2.id())->cast_to<
1053 RTCDataChannelStats>());
1054 ASSERT_TRUE(report->Get(expected_data_channel3.id()));
1055 EXPECT_EQ(expected_data_channel3,
1056 report->Get(expected_data_channel3.id())->cast_to<
1057 RTCDataChannelStats>());
hboscc555c52016-10-18 12:48:31 -07001058}
1059
hbosab9f6e42016-10-07 02:18:47 -07001060TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) {
1061 // Candidates in the first transport stats.
Gary Liu37e489c2017-11-21 10:49:36 -08001062 std::unique_ptr<cricket::Candidate> a_local_host =
1063 CreateFakeCandidate("1.2.3.4", 5, "a_local_host's protocol",
1064 rtc::ADAPTER_TYPE_VPN, cricket::LOCAL_PORT_TYPE, 0);
hbosc42ba322016-12-21 03:31:45 -08001065 RTCLocalIceCandidateStats expected_a_local_host(
1066 "RTCIceCandidate_" + a_local_host->id(), 0);
hbosb4e426e2017-01-02 09:59:31 -08001067 expected_a_local_host.transport_id = "RTCTransport_a_0";
Gary Liu37e489c2017-11-21 10:49:36 -08001068 expected_a_local_host.network_type = "vpn";
hbosc42ba322016-12-21 03:31:45 -08001069 expected_a_local_host.ip = "1.2.3.4";
1070 expected_a_local_host.port = 5;
1071 expected_a_local_host.protocol = "a_local_host's protocol";
1072 expected_a_local_host.candidate_type = "host";
1073 expected_a_local_host.priority = 0;
hbosc3a2b7f2017-01-02 04:46:15 -08001074 EXPECT_FALSE(*expected_a_local_host.is_remote);
hbosc42ba322016-12-21 03:31:45 -08001075
hbosab9f6e42016-10-07 02:18:47 -07001076 std::unique_ptr<cricket::Candidate> a_remote_srflx = CreateFakeCandidate(
Gary Liu37e489c2017-11-21 10:49:36 -08001077 "6.7.8.9", 10, "remote_srflx's protocol", rtc::ADAPTER_TYPE_UNKNOWN,
1078 cricket::STUN_PORT_TYPE, 1);
hbosc42ba322016-12-21 03:31:45 -08001079 RTCRemoteIceCandidateStats expected_a_remote_srflx(
1080 "RTCIceCandidate_" + a_remote_srflx->id(), 0);
hbosb4e426e2017-01-02 09:59:31 -08001081 expected_a_remote_srflx.transport_id = "RTCTransport_a_0";
hbosc42ba322016-12-21 03:31:45 -08001082 expected_a_remote_srflx.ip = "6.7.8.9";
1083 expected_a_remote_srflx.port = 10;
1084 expected_a_remote_srflx.protocol = "remote_srflx's protocol";
1085 expected_a_remote_srflx.candidate_type = "srflx";
1086 expected_a_remote_srflx.priority = 1;
hbosd17a5a72017-01-02 08:09:59 -08001087 expected_a_remote_srflx.deleted = false;
hbosc3a2b7f2017-01-02 04:46:15 -08001088 EXPECT_TRUE(*expected_a_remote_srflx.is_remote);
hbosc42ba322016-12-21 03:31:45 -08001089
hbosab9f6e42016-10-07 02:18:47 -07001090 std::unique_ptr<cricket::Candidate> a_local_prflx = CreateFakeCandidate(
Gary Liu37e489c2017-11-21 10:49:36 -08001091 "11.12.13.14", 15, "a_local_prflx's protocol", rtc::ADAPTER_TYPE_CELLULAR,
1092 cricket::PRFLX_PORT_TYPE, 2);
hbosc42ba322016-12-21 03:31:45 -08001093 RTCLocalIceCandidateStats expected_a_local_prflx(
1094 "RTCIceCandidate_" + a_local_prflx->id(), 0);
hbosb4e426e2017-01-02 09:59:31 -08001095 expected_a_local_prflx.transport_id = "RTCTransport_a_0";
Gary Liu37e489c2017-11-21 10:49:36 -08001096 expected_a_local_prflx.network_type = "cellular";
hbosc42ba322016-12-21 03:31:45 -08001097 expected_a_local_prflx.ip = "11.12.13.14";
1098 expected_a_local_prflx.port = 15;
1099 expected_a_local_prflx.protocol = "a_local_prflx's protocol";
1100 expected_a_local_prflx.candidate_type = "prflx";
1101 expected_a_local_prflx.priority = 2;
hbosd17a5a72017-01-02 08:09:59 -08001102 expected_a_local_prflx.deleted = false;
hbosc3a2b7f2017-01-02 04:46:15 -08001103 EXPECT_FALSE(*expected_a_local_prflx.is_remote);
hbosc42ba322016-12-21 03:31:45 -08001104
hbosab9f6e42016-10-07 02:18:47 -07001105 std::unique_ptr<cricket::Candidate> a_remote_relay = CreateFakeCandidate(
Gary Liu37e489c2017-11-21 10:49:36 -08001106 "16.17.18.19", 20, "a_remote_relay's protocol", rtc::ADAPTER_TYPE_UNKNOWN,
1107 cricket::RELAY_PORT_TYPE, 3);
hbosc42ba322016-12-21 03:31:45 -08001108 RTCRemoteIceCandidateStats expected_a_remote_relay(
1109 "RTCIceCandidate_" + a_remote_relay->id(), 0);
hbosb4e426e2017-01-02 09:59:31 -08001110 expected_a_remote_relay.transport_id = "RTCTransport_a_0";
hbosc42ba322016-12-21 03:31:45 -08001111 expected_a_remote_relay.ip = "16.17.18.19";
1112 expected_a_remote_relay.port = 20;
1113 expected_a_remote_relay.protocol = "a_remote_relay's protocol";
1114 expected_a_remote_relay.candidate_type = "relay";
1115 expected_a_remote_relay.priority = 3;
hbosd17a5a72017-01-02 08:09:59 -08001116 expected_a_remote_relay.deleted = false;
hbosc3a2b7f2017-01-02 04:46:15 -08001117 EXPECT_TRUE(*expected_a_remote_relay.is_remote);
hbosc42ba322016-12-21 03:31:45 -08001118
hbosab9f6e42016-10-07 02:18:47 -07001119 // Candidates in the second transport stats.
Gary Liu37e489c2017-11-21 10:49:36 -08001120 std::unique_ptr<cricket::Candidate> b_local =
1121 CreateFakeCandidate("42.42.42.42", 42, "b_local's protocol",
1122 rtc::ADAPTER_TYPE_WIFI, cricket::LOCAL_PORT_TYPE, 42);
hbosc42ba322016-12-21 03:31:45 -08001123 RTCLocalIceCandidateStats expected_b_local(
1124 "RTCIceCandidate_" + b_local->id(), 0);
hbosb4e426e2017-01-02 09:59:31 -08001125 expected_b_local.transport_id = "RTCTransport_b_0";
Gary Liu37e489c2017-11-21 10:49:36 -08001126 expected_b_local.network_type = "wifi";
hbosc42ba322016-12-21 03:31:45 -08001127 expected_b_local.ip = "42.42.42.42";
1128 expected_b_local.port = 42;
1129 expected_b_local.protocol = "b_local's protocol";
1130 expected_b_local.candidate_type = "host";
1131 expected_b_local.priority = 42;
hbosd17a5a72017-01-02 08:09:59 -08001132 expected_b_local.deleted = false;
hbosc3a2b7f2017-01-02 04:46:15 -08001133 EXPECT_FALSE(*expected_b_local.is_remote);
hbosc42ba322016-12-21 03:31:45 -08001134
hbosab9f6e42016-10-07 02:18:47 -07001135 std::unique_ptr<cricket::Candidate> b_remote = CreateFakeCandidate(
Gary Liu37e489c2017-11-21 10:49:36 -08001136 "42.42.42.42", 42, "b_remote's protocol", rtc::ADAPTER_TYPE_UNKNOWN,
1137 cricket::LOCAL_PORT_TYPE, 42);
hbosc42ba322016-12-21 03:31:45 -08001138 RTCRemoteIceCandidateStats expected_b_remote(
1139 "RTCIceCandidate_" + b_remote->id(), 0);
hbosb4e426e2017-01-02 09:59:31 -08001140 expected_b_remote.transport_id = "RTCTransport_b_0";
hbosc42ba322016-12-21 03:31:45 -08001141 expected_b_remote.ip = "42.42.42.42";
1142 expected_b_remote.port = 42;
1143 expected_b_remote.protocol = "b_remote's protocol";
1144 expected_b_remote.candidate_type = "host";
1145 expected_b_remote.priority = 42;
hbosd17a5a72017-01-02 08:09:59 -08001146 expected_b_remote.deleted = false;
hbosc3a2b7f2017-01-02 04:46:15 -08001147 EXPECT_TRUE(*expected_b_remote.is_remote);
hbosab9f6e42016-10-07 02:18:47 -07001148
1149 SessionStats session_stats;
1150
1151 cricket::TransportChannelStats a_transport_channel_stats;
1152 a_transport_channel_stats.connection_infos.push_back(
1153 cricket::ConnectionInfo());
1154 a_transport_channel_stats.connection_infos[0].local_candidate =
1155 *a_local_host.get();
1156 a_transport_channel_stats.connection_infos[0].remote_candidate =
1157 *a_remote_srflx.get();
1158 a_transport_channel_stats.connection_infos.push_back(
1159 cricket::ConnectionInfo());
1160 a_transport_channel_stats.connection_infos[1].local_candidate =
1161 *a_local_prflx.get();
1162 a_transport_channel_stats.connection_infos[1].remote_candidate =
1163 *a_remote_relay.get();
hbos02d2a922016-12-21 01:29:05 -08001164 session_stats.transport_stats["a"].transport_name = "a";
hbosab9f6e42016-10-07 02:18:47 -07001165 session_stats.transport_stats["a"].channel_stats.push_back(
1166 a_transport_channel_stats);
1167
1168 cricket::TransportChannelStats b_transport_channel_stats;
1169 b_transport_channel_stats.connection_infos.push_back(
1170 cricket::ConnectionInfo());
1171 b_transport_channel_stats.connection_infos[0].local_candidate =
1172 *b_local.get();
1173 b_transport_channel_stats.connection_infos[0].remote_candidate =
1174 *b_remote.get();
hbos02d2a922016-12-21 01:29:05 -08001175 session_stats.transport_stats["b"].transport_name = "b";
hbosab9f6e42016-10-07 02:18:47 -07001176 session_stats.transport_stats["b"].channel_stats.push_back(
1177 b_transport_channel_stats);
1178
1179 // Mock the session to return the desired candidates.
Steve Anton75737c02017-11-06 10:37:17 -08001180 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -08001181 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -08001182 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
hbosab9f6e42016-10-07 02:18:47 -07001183 }));
1184
1185 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbosc42ba322016-12-21 03:31:45 -08001186
hbosb4e426e2017-01-02 09:59:31 -08001187 ASSERT_TRUE(report->Get(expected_a_local_host.id()));
hbosc42ba322016-12-21 03:31:45 -08001188 EXPECT_EQ(expected_a_local_host,
1189 report->Get(expected_a_local_host.id())->cast_to<
1190 RTCLocalIceCandidateStats>());
hbosb4e426e2017-01-02 09:59:31 -08001191 ASSERT_TRUE(report->Get(expected_a_remote_srflx.id()));
hbosc42ba322016-12-21 03:31:45 -08001192 EXPECT_EQ(expected_a_remote_srflx,
1193 report->Get(expected_a_remote_srflx.id())->cast_to<
1194 RTCRemoteIceCandidateStats>());
hbosb4e426e2017-01-02 09:59:31 -08001195 ASSERT_TRUE(report->Get(expected_a_local_prflx.id()));
hbosc42ba322016-12-21 03:31:45 -08001196 EXPECT_EQ(expected_a_local_prflx,
1197 report->Get(expected_a_local_prflx.id())->cast_to<
1198 RTCLocalIceCandidateStats>());
hbosb4e426e2017-01-02 09:59:31 -08001199 ASSERT_TRUE(report->Get(expected_a_remote_relay.id()));
hbosc42ba322016-12-21 03:31:45 -08001200 EXPECT_EQ(expected_a_remote_relay,
1201 report->Get(expected_a_remote_relay.id())->cast_to<
1202 RTCRemoteIceCandidateStats>());
hbosb4e426e2017-01-02 09:59:31 -08001203 ASSERT_TRUE(report->Get(expected_b_local.id()));
hbosc42ba322016-12-21 03:31:45 -08001204 EXPECT_EQ(expected_b_local,
1205 report->Get(expected_b_local.id())->cast_to<
1206 RTCLocalIceCandidateStats>());
hbosb4e426e2017-01-02 09:59:31 -08001207 ASSERT_TRUE(report->Get(expected_b_remote.id()));
hbosc42ba322016-12-21 03:31:45 -08001208 EXPECT_EQ(expected_b_remote,
1209 report->Get(expected_b_remote.id())->cast_to<
1210 RTCRemoteIceCandidateStats>());
hbosb4e426e2017-01-02 09:59:31 -08001211 EXPECT_TRUE(report->Get("RTCTransport_a_0"));
1212 EXPECT_TRUE(report->Get("RTCTransport_b_0"));
hbosab9f6e42016-10-07 02:18:47 -07001213}
1214
hbosc47a0c32016-10-11 14:54:49 -07001215TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) {
Steve Anton8699a322017-11-06 15:53:33 -08001216 auto* video_media_channel = new MockVideoMediaChannel();
hbos338f78a2017-02-07 06:41:21 -08001217 cricket::VideoChannel video_channel(
1218 test_->worker_thread(), test_->network_thread(),
Steve Anton8699a322017-11-06 15:53:33 -08001219 test_->signaling_thread(), rtc::WrapUnique(video_media_channel),
1220 "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
hbos338f78a2017-02-07 06:41:21 -08001221
Gary Liu37e489c2017-11-21 10:49:36 -08001222 std::unique_ptr<cricket::Candidate> local_candidate =
1223 CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI,
1224 cricket::LOCAL_PORT_TYPE, 42);
hbosc47a0c32016-10-11 14:54:49 -07001225 std::unique_ptr<cricket::Candidate> remote_candidate = CreateFakeCandidate(
Gary Liu37e489c2017-11-21 10:49:36 -08001226 "42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_UNKNOWN,
1227 cricket::LOCAL_PORT_TYPE, 42);
hbosc47a0c32016-10-11 14:54:49 -07001228
1229 SessionStats session_stats;
1230
1231 cricket::ConnectionInfo connection_info;
hbos338f78a2017-02-07 06:41:21 -08001232 connection_info.best_connection = false;
hbosc47a0c32016-10-11 14:54:49 -07001233 connection_info.local_candidate = *local_candidate.get();
1234 connection_info.remote_candidate = *remote_candidate.get();
1235 connection_info.writable = true;
1236 connection_info.sent_total_bytes = 42;
1237 connection_info.recv_total_bytes = 1234;
hbosbf8d3e52017-02-28 06:34:47 -08001238 connection_info.total_round_trip_time_ms = 0;
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001239 connection_info.current_round_trip_time_ms = rtc::nullopt;
hbosd82f5122016-12-09 04:12:39 -08001240 connection_info.recv_ping_requests = 2020;
hbose448dd52016-12-12 01:22:53 -08001241 connection_info.sent_ping_requests_total = 2020;
1242 connection_info.sent_ping_requests_before_first_response = 2000;
hbosc47a0c32016-10-11 14:54:49 -07001243 connection_info.recv_ping_responses = 4321;
1244 connection_info.sent_ping_responses = 1000;
hbos06495bc2017-01-02 08:08:18 -08001245 connection_info.state = cricket::IceCandidatePairState::IN_PROGRESS;
1246 connection_info.priority = 5555;
hbos92eaec62017-02-27 01:38:08 -08001247 connection_info.nominated = false;
hbosc47a0c32016-10-11 14:54:49 -07001248
1249 cricket::TransportChannelStats transport_channel_stats;
hbos0583b282016-11-30 01:50:14 -08001250 transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
hbosc47a0c32016-10-11 14:54:49 -07001251 transport_channel_stats.connection_infos.push_back(connection_info);
1252 session_stats.transport_stats["transport"].transport_name = "transport";
1253 session_stats.transport_stats["transport"].channel_stats.push_back(
1254 transport_channel_stats);
1255
1256 // Mock the session to return the desired candidates.
Steve Anton75737c02017-11-06 10:37:17 -08001257 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -08001258 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -08001259 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
hbosc47a0c32016-10-11 14:54:49 -07001260 }));
1261
hbos338f78a2017-02-07 06:41:21 -08001262 // Mock the session to return bandwidth estimation info. These should only
1263 // be used for a selected candidate pair.
1264 cricket::VideoMediaInfo video_media_info;
hbos338f78a2017-02-07 06:41:21 -08001265 EXPECT_CALL(*video_media_channel, GetStats(_))
1266 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
Steve Anton75737c02017-11-06 10:37:17 -08001267 EXPECT_CALL(test_->pc(), video_channel())
hbos338f78a2017-02-07 06:41:21 -08001268 .WillRepeatedly(Return(&video_channel));
1269
hbosc47a0c32016-10-11 14:54:49 -07001270 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbos0583b282016-11-30 01:50:14 -08001271
1272 RTCIceCandidatePairStats expected_pair("RTCIceCandidatePair_" +
1273 local_candidate->id() + "_" +
1274 remote_candidate->id(),
1275 report->timestamp_us());
1276 expected_pair.transport_id =
1277 "RTCTransport_transport_" +
1278 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP);
1279 expected_pair.local_candidate_id = "RTCIceCandidate_" + local_candidate->id();
1280 expected_pair.remote_candidate_id =
1281 "RTCIceCandidate_" + remote_candidate->id();
hbos06495bc2017-01-02 08:08:18 -08001282 expected_pair.state = RTCStatsIceCandidatePairState::kInProgress;
1283 expected_pair.priority = 5555;
hbos92eaec62017-02-27 01:38:08 -08001284 expected_pair.nominated = false;
hbos0583b282016-11-30 01:50:14 -08001285 expected_pair.writable = true;
1286 expected_pair.bytes_sent = 42;
1287 expected_pair.bytes_received = 1234;
hbosbf8d3e52017-02-28 06:34:47 -08001288 expected_pair.total_round_trip_time = 0.0;
hbosd82f5122016-12-09 04:12:39 -08001289 expected_pair.requests_received = 2020;
hbose448dd52016-12-12 01:22:53 -08001290 expected_pair.requests_sent = 2000;
hbos0583b282016-11-30 01:50:14 -08001291 expected_pair.responses_received = 4321;
1292 expected_pair.responses_sent = 1000;
hbose448dd52016-12-12 01:22:53 -08001293 expected_pair.consent_requests_sent = (2020 - 2000);
hbosbf8d3e52017-02-28 06:34:47 -08001294 // |expected_pair.current_round_trip_time| should be undefined because the
1295 // current RTT is not set.
hbos338f78a2017-02-07 06:41:21 -08001296 // |expected_pair.available_[outgoing/incoming]_bitrate| should be undefined
1297 // because is is not the current pair.
hbos0583b282016-11-30 01:50:14 -08001298
hbosdbb64d82016-12-21 01:57:46 -08001299 ASSERT_TRUE(report->Get(expected_pair.id()));
hbos0583b282016-11-30 01:50:14 -08001300 EXPECT_EQ(
1301 expected_pair,
1302 report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>());
hbosb4e426e2017-01-02 09:59:31 -08001303 EXPECT_TRUE(report->Get(*expected_pair.transport_id));
hbos0583b282016-11-30 01:50:14 -08001304
hbos92eaec62017-02-27 01:38:08 -08001305 // Set nominated and "GetStats" again.
1306 session_stats.transport_stats["transport"]
1307 .channel_stats[0]
1308 .connection_infos[0]
1309 .nominated = true;
1310 EXPECT_CALL(*video_media_channel, GetStats(_))
1311 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
1312 collector_->ClearCachedStatsReport();
1313 report = GetStatsReport();
1314 expected_pair.nominated = true;
1315 ASSERT_TRUE(report->Get(expected_pair.id()));
1316 EXPECT_EQ(
1317 expected_pair,
1318 report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>());
1319 EXPECT_TRUE(report->Get(*expected_pair.transport_id));
1320
hbosbf8d3e52017-02-28 06:34:47 -08001321 // Set round trip times and "GetStats" again.
1322 session_stats.transport_stats["transport"].channel_stats[0]
1323 .connection_infos[0].total_round_trip_time_ms = 7331;
1324 session_stats.transport_stats["transport"].channel_stats[0]
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001325 .connection_infos[0].current_round_trip_time_ms = 1337;
hbosbf8d3e52017-02-28 06:34:47 -08001326 EXPECT_CALL(*video_media_channel, GetStats(_))
1327 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
1328 collector_->ClearCachedStatsReport();
1329 report = GetStatsReport();
1330 expected_pair.total_round_trip_time = 7.331;
1331 expected_pair.current_round_trip_time = 1.337;
1332 ASSERT_TRUE(report->Get(expected_pair.id()));
1333 EXPECT_EQ(
1334 expected_pair,
1335 report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>());
1336 EXPECT_TRUE(report->Get(*expected_pair.transport_id));
1337
hbos338f78a2017-02-07 06:41:21 -08001338 // Make pair the current pair, clear bandwidth and "GetStats" again.
1339 session_stats.transport_stats["transport"]
1340 .channel_stats[0]
1341 .connection_infos[0]
1342 .best_connection = true;
hbos338f78a2017-02-07 06:41:21 -08001343 EXPECT_CALL(*video_media_channel, GetStats(_))
1344 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
1345 collector_->ClearCachedStatsReport();
1346 report = GetStatsReport();
1347 // |expected_pair.available_[outgoing/incoming]_bitrate| should still be
1348 // undefined because bandwidth is not set.
1349 ASSERT_TRUE(report->Get(expected_pair.id()));
1350 EXPECT_EQ(
1351 expected_pair,
1352 report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>());
1353 EXPECT_TRUE(report->Get(*expected_pair.transport_id));
1354
1355 // Set bandwidth and "GetStats" again.
stefanf79ade12017-06-02 06:44:03 -07001356 webrtc::Call::Stats call_stats;
1357 const int kSendBandwidth = 888;
1358 call_stats.send_bandwidth_bps = kSendBandwidth;
1359 const int kRecvBandwidth = 999;
1360 call_stats.recv_bandwidth_bps = kRecvBandwidth;
Steve Anton75737c02017-11-06 10:37:17 -08001361 EXPECT_CALL(test_->pc(), GetCallStats()).WillRepeatedly(Return(call_stats));
hbos338f78a2017-02-07 06:41:21 -08001362 EXPECT_CALL(*video_media_channel, GetStats(_))
1363 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
1364 collector_->ClearCachedStatsReport();
1365 report = GetStatsReport();
stefanf79ade12017-06-02 06:44:03 -07001366 expected_pair.available_outgoing_bitrate = kSendBandwidth;
1367 expected_pair.available_incoming_bitrate = kRecvBandwidth;
hbos338f78a2017-02-07 06:41:21 -08001368 ASSERT_TRUE(report->Get(expected_pair.id()));
1369 EXPECT_EQ(
1370 expected_pair,
1371 report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>());
1372 EXPECT_TRUE(report->Get(*expected_pair.transport_id));
1373
hbosc42ba322016-12-21 03:31:45 -08001374 RTCLocalIceCandidateStats expected_local_candidate(
1375 *expected_pair.local_candidate_id, report->timestamp_us());
hbosb4e426e2017-01-02 09:59:31 -08001376 expected_local_candidate.transport_id = *expected_pair.transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -08001377 expected_local_candidate.network_type = "wifi";
hbosc42ba322016-12-21 03:31:45 -08001378 expected_local_candidate.ip = "42.42.42.42";
1379 expected_local_candidate.port = 42;
1380 expected_local_candidate.protocol = "protocol";
1381 expected_local_candidate.candidate_type = "host";
1382 expected_local_candidate.priority = 42;
hbosd17a5a72017-01-02 08:09:59 -08001383 expected_local_candidate.deleted = false;
hbosc3a2b7f2017-01-02 04:46:15 -08001384 EXPECT_FALSE(*expected_local_candidate.is_remote);
hbosc42ba322016-12-21 03:31:45 -08001385 ASSERT_TRUE(report->Get(expected_local_candidate.id()));
1386 EXPECT_EQ(expected_local_candidate,
1387 report->Get(expected_local_candidate.id())->cast_to<
1388 RTCLocalIceCandidateStats>());
1389
1390 RTCRemoteIceCandidateStats expected_remote_candidate(
1391 *expected_pair.remote_candidate_id, report->timestamp_us());
hbosb4e426e2017-01-02 09:59:31 -08001392 expected_remote_candidate.transport_id = *expected_pair.transport_id;
hbosc42ba322016-12-21 03:31:45 -08001393 expected_remote_candidate.ip = "42.42.42.42";
1394 expected_remote_candidate.port = 42;
1395 expected_remote_candidate.protocol = "protocol";
1396 expected_remote_candidate.candidate_type = "host";
1397 expected_remote_candidate.priority = 42;
hbosd17a5a72017-01-02 08:09:59 -08001398 expected_remote_candidate.deleted = false;
hbosc3a2b7f2017-01-02 04:46:15 -08001399 EXPECT_TRUE(*expected_remote_candidate.is_remote);
hbosc42ba322016-12-21 03:31:45 -08001400 ASSERT_TRUE(report->Get(expected_remote_candidate.id()));
1401 EXPECT_EQ(expected_remote_candidate,
1402 report->Get(expected_remote_candidate.id())->cast_to<
1403 RTCRemoteIceCandidateStats>());
hbosc47a0c32016-10-11 14:54:49 -07001404}
1405
hbosd565b732016-08-30 14:04:35 -07001406TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) {
hbosd565b732016-08-30 14:04:35 -07001407 {
hbos82ebe022016-11-14 01:41:09 -08001408 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1409 RTCPeerConnectionStats expected("RTCPeerConnection",
1410 report->timestamp_us());
1411 expected.data_channels_opened = 0;
1412 expected.data_channels_closed = 0;
hbosdbb64d82016-12-21 01:57:46 -08001413 ASSERT_TRUE(report->Get("RTCPeerConnection"));
hbos82ebe022016-11-14 01:41:09 -08001414 EXPECT_EQ(expected,
1415 report->Get("RTCPeerConnection")->cast_to<
1416 RTCPeerConnectionStats>());
hbosd565b732016-08-30 14:04:35 -07001417 }
1418
hbos82ebe022016-11-14 01:41:09 -08001419 rtc::scoped_refptr<DataChannel> dummy_channel_a = DataChannel::Create(
1420 nullptr, cricket::DCT_NONE, "DummyChannelA", InternalDataChannelInit());
1421 test_->pc().SignalDataChannelCreated(dummy_channel_a.get());
1422 rtc::scoped_refptr<DataChannel> dummy_channel_b = DataChannel::Create(
1423 nullptr, cricket::DCT_NONE, "DummyChannelB", InternalDataChannelInit());
1424 test_->pc().SignalDataChannelCreated(dummy_channel_b.get());
hbosd565b732016-08-30 14:04:35 -07001425
hbos82ebe022016-11-14 01:41:09 -08001426 dummy_channel_a->SignalOpened(dummy_channel_a.get());
1427 // Closing a channel that is not opened should not affect the counts.
1428 dummy_channel_b->SignalClosed(dummy_channel_b.get());
1429
hbosd565b732016-08-30 14:04:35 -07001430 {
hbos82ebe022016-11-14 01:41:09 -08001431 collector_->ClearCachedStatsReport();
1432 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1433 RTCPeerConnectionStats expected("RTCPeerConnection",
1434 report->timestamp_us());
1435 expected.data_channels_opened = 1;
1436 expected.data_channels_closed = 0;
hbosdbb64d82016-12-21 01:57:46 -08001437 ASSERT_TRUE(report->Get("RTCPeerConnection"));
hbos82ebe022016-11-14 01:41:09 -08001438 EXPECT_EQ(expected,
1439 report->Get("RTCPeerConnection")->cast_to<
1440 RTCPeerConnectionStats>());
1441 }
1442
1443 dummy_channel_b->SignalOpened(dummy_channel_b.get());
1444 dummy_channel_b->SignalClosed(dummy_channel_b.get());
1445
1446 {
1447 collector_->ClearCachedStatsReport();
1448 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1449 RTCPeerConnectionStats expected("RTCPeerConnection",
1450 report->timestamp_us());
1451 expected.data_channels_opened = 2;
1452 expected.data_channels_closed = 1;
hbosdbb64d82016-12-21 01:57:46 -08001453 ASSERT_TRUE(report->Get("RTCPeerConnection"));
hbos82ebe022016-11-14 01:41:09 -08001454 EXPECT_EQ(expected,
1455 report->Get("RTCPeerConnection")->cast_to<
1456 RTCPeerConnectionStats>());
hbosd565b732016-08-30 14:04:35 -07001457 }
hbos5bf9def2017-03-20 03:14:14 -07001458
1459 // Re-opening a data channel (or opening a new data channel that is re-using
1460 // the same address in memory) should increase the opened count.
1461 dummy_channel_b->SignalOpened(dummy_channel_b.get());
1462
1463 {
1464 collector_->ClearCachedStatsReport();
1465 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1466 RTCPeerConnectionStats expected("RTCPeerConnection",
1467 report->timestamp_us());
1468 expected.data_channels_opened = 3;
1469 expected.data_channels_closed = 1;
1470 ASSERT_TRUE(report->Get("RTCPeerConnection"));
1471 EXPECT_EQ(expected,
1472 report->Get("RTCPeerConnection")->cast_to<
1473 RTCPeerConnectionStats>());
1474 }
1475
1476 dummy_channel_a->SignalClosed(dummy_channel_a.get());
1477 dummy_channel_b->SignalClosed(dummy_channel_b.get());
1478
1479 {
1480 collector_->ClearCachedStatsReport();
1481 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1482 RTCPeerConnectionStats expected("RTCPeerConnection",
1483 report->timestamp_us());
1484 expected.data_channels_opened = 3;
1485 expected.data_channels_closed = 3;
1486 ASSERT_TRUE(report->Get("RTCPeerConnection"));
1487 EXPECT_EQ(expected,
1488 report->Get("RTCPeerConnection")->cast_to<
1489 RTCPeerConnectionStats>());
1490 }
hbosd565b732016-08-30 14:04:35 -07001491}
1492
hbos09bc1282016-11-08 06:29:22 -08001493TEST_F(RTCStatsCollectorTest,
1494 CollectRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio) {
1495 rtc::scoped_refptr<StreamCollection> local_streams =
1496 StreamCollection::Create();
1497 rtc::scoped_refptr<StreamCollection> remote_streams =
1498 StreamCollection::Create();
1499 EXPECT_CALL(test_->pc(), local_streams())
1500 .WillRepeatedly(Return(local_streams));
1501 EXPECT_CALL(test_->pc(), remote_streams())
1502 .WillRepeatedly(Return(remote_streams));
1503
1504 rtc::scoped_refptr<MediaStream> local_stream =
1505 MediaStream::Create("LocalStreamLabel");
1506 local_streams->AddStream(local_stream);
1507 rtc::scoped_refptr<MediaStream> remote_stream =
1508 MediaStream::Create("RemoteStreamLabel");
1509 remote_streams->AddStream(remote_stream);
1510
1511 // Local audio track
hbos9e302742017-01-20 02:47:10 -08001512 rtc::scoped_refptr<MediaStreamTrackInterface> local_audio_track =
1513 CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "LocalAudioTrackID",
1514 MediaStreamTrackInterface::kEnded);
1515 local_stream->AddTrack(static_cast<AudioTrackInterface*>(
1516 local_audio_track.get()));
1517
1518 cricket::VoiceSenderInfo voice_sender_info_ssrc1;
1519 voice_sender_info_ssrc1.local_stats.push_back(cricket::SsrcSenderInfo());
1520 voice_sender_info_ssrc1.local_stats[0].ssrc = 1;
1521 voice_sender_info_ssrc1.audio_level = 32767;
zsteine76bd3a2017-07-14 12:17:49 -07001522 voice_sender_info_ssrc1.total_input_energy = 0.25;
1523 voice_sender_info_ssrc1.total_input_duration = 0.5;
Ivo Creusen56d46092017-11-24 17:29:59 +01001524 voice_sender_info_ssrc1.apm_statistics.echo_return_loss = 42.0;
1525 voice_sender_info_ssrc1.apm_statistics.echo_return_loss_enhancement = 52.0;
hbos9e302742017-01-20 02:47:10 -08001526
1527 // Uses default values, the corresponding stats object should contain
1528 // undefined members.
1529 cricket::VoiceSenderInfo voice_sender_info_ssrc2;
1530 voice_sender_info_ssrc2.local_stats.push_back(cricket::SsrcSenderInfo());
1531 voice_sender_info_ssrc2.local_stats[0].ssrc = 2;
1532 voice_sender_info_ssrc2.audio_level = 0;
zsteine76bd3a2017-07-14 12:17:49 -07001533 voice_sender_info_ssrc2.total_input_energy = 0.0;
1534 voice_sender_info_ssrc2.total_input_duration = 0.0;
hbos09bc1282016-11-08 06:29:22 -08001535
1536 // Remote audio track
hbos9e302742017-01-20 02:47:10 -08001537 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio_track =
1538 CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "RemoteAudioTrackID",
1539 MediaStreamTrackInterface::kLive);
1540 remote_stream->AddTrack(static_cast<AudioTrackInterface*>(
1541 remote_audio_track.get()));
1542
1543 cricket::VoiceReceiverInfo voice_receiver_info;
1544 voice_receiver_info.local_stats.push_back(cricket::SsrcReceiverInfo());
1545 voice_receiver_info.local_stats[0].ssrc = 3;
1546 voice_receiver_info.audio_level = 16383;
zsteine76bd3a2017-07-14 12:17:49 -07001547 voice_receiver_info.total_output_energy = 0.125;
Steve Anton2dbc69f2017-08-24 17:15:13 -07001548 voice_receiver_info.total_samples_received = 4567;
zsteine76bd3a2017-07-14 12:17:49 -07001549 voice_receiver_info.total_output_duration = 0.25;
Steve Anton2dbc69f2017-08-24 17:15:13 -07001550 voice_receiver_info.concealed_samples = 123;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001551 voice_receiver_info.concealment_events = 12;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001552 voice_receiver_info.jitter_buffer_delay_seconds = 3456;
hbos9e302742017-01-20 02:47:10 -08001553
1554 test_->CreateMockRtpSendersReceiversAndChannels(
1555 { std::make_pair(local_audio_track.get(), voice_sender_info_ssrc1),
1556 std::make_pair(local_audio_track.get(), voice_sender_info_ssrc2) },
1557 { std::make_pair(remote_audio_track.get(), voice_receiver_info) },
1558 {}, {});
hbos09bc1282016-11-08 06:29:22 -08001559
1560 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1561
1562 RTCMediaStreamStats expected_local_stream(
hbos02d2a922016-12-21 01:29:05 -08001563 "RTCMediaStream_local_LocalStreamLabel", report->timestamp_us());
hbos09bc1282016-11-08 06:29:22 -08001564 expected_local_stream.stream_identifier = local_stream->label();
hbos9e302742017-01-20 02:47:10 -08001565 expected_local_stream.track_ids = std::vector<std::string>(
1566 { "RTCMediaStreamTrack_local_audio_LocalAudioTrackID_1",
1567 "RTCMediaStreamTrack_local_audio_LocalAudioTrackID_2" });
hbosdbb64d82016-12-21 01:57:46 -08001568 ASSERT_TRUE(report->Get(expected_local_stream.id()));
hbos09bc1282016-11-08 06:29:22 -08001569 EXPECT_EQ(expected_local_stream,
1570 report->Get(expected_local_stream.id())->cast_to<
1571 RTCMediaStreamStats>());
1572
1573 RTCMediaStreamStats expected_remote_stream(
hbos02d2a922016-12-21 01:29:05 -08001574 "RTCMediaStream_remote_RemoteStreamLabel", report->timestamp_us());
hbos09bc1282016-11-08 06:29:22 -08001575 expected_remote_stream.stream_identifier = remote_stream->label();
hbos9e302742017-01-20 02:47:10 -08001576 expected_remote_stream.track_ids = std::vector<std::string>({
1577 "RTCMediaStreamTrack_remote_audio_RemoteAudioTrackID_3" });
hbosdbb64d82016-12-21 01:57:46 -08001578 ASSERT_TRUE(report->Get(expected_remote_stream.id()));
hbos09bc1282016-11-08 06:29:22 -08001579 EXPECT_EQ(expected_remote_stream,
1580 report->Get(expected_remote_stream.id())->cast_to<
1581 RTCMediaStreamStats>());
1582
hbos9e302742017-01-20 02:47:10 -08001583 RTCMediaStreamTrackStats expected_local_audio_track_ssrc1(
1584 "RTCMediaStreamTrack_local_audio_LocalAudioTrackID_1",
1585 report->timestamp_us(), RTCMediaStreamTrackKind::kAudio);
1586 expected_local_audio_track_ssrc1.track_identifier = local_audio_track->id();
1587 expected_local_audio_track_ssrc1.remote_source = false;
1588 expected_local_audio_track_ssrc1.ended = true;
1589 expected_local_audio_track_ssrc1.detached = false;
1590 expected_local_audio_track_ssrc1.audio_level = 1.0;
zsteine76bd3a2017-07-14 12:17:49 -07001591 expected_local_audio_track_ssrc1.total_audio_energy = 0.25;
1592 expected_local_audio_track_ssrc1.total_samples_duration = 0.5;
hbos9e302742017-01-20 02:47:10 -08001593 expected_local_audio_track_ssrc1.echo_return_loss = 42.0;
1594 expected_local_audio_track_ssrc1.echo_return_loss_enhancement = 52.0;
1595 ASSERT_TRUE(report->Get(expected_local_audio_track_ssrc1.id()));
1596 EXPECT_EQ(expected_local_audio_track_ssrc1,
1597 report->Get(expected_local_audio_track_ssrc1.id())->cast_to<
1598 RTCMediaStreamTrackStats>());
1599
1600 RTCMediaStreamTrackStats expected_local_audio_track_ssrc2(
1601 "RTCMediaStreamTrack_local_audio_LocalAudioTrackID_2",
1602 report->timestamp_us(), RTCMediaStreamTrackKind::kAudio);
1603 expected_local_audio_track_ssrc2.track_identifier = local_audio_track->id();
1604 expected_local_audio_track_ssrc2.remote_source = false;
1605 expected_local_audio_track_ssrc2.ended = true;
1606 expected_local_audio_track_ssrc2.detached = false;
1607 expected_local_audio_track_ssrc2.audio_level = 0.0;
zsteine76bd3a2017-07-14 12:17:49 -07001608 expected_local_audio_track_ssrc2.total_audio_energy = 0.0;
1609 expected_local_audio_track_ssrc2.total_samples_duration = 0.0;
hbos9e302742017-01-20 02:47:10 -08001610 // Should be undefined: |expected_local_audio_track_ssrc2.echo_return_loss|
1611 // and |expected_local_audio_track_ssrc2.echo_return_loss_enhancement|.
1612 ASSERT_TRUE(report->Get(expected_local_audio_track_ssrc2.id()));
1613 EXPECT_EQ(expected_local_audio_track_ssrc2,
1614 report->Get(expected_local_audio_track_ssrc2.id())->cast_to<
hbos09bc1282016-11-08 06:29:22 -08001615 RTCMediaStreamTrackStats>());
1616
1617 RTCMediaStreamTrackStats expected_remote_audio_track(
hbos9e302742017-01-20 02:47:10 -08001618 "RTCMediaStreamTrack_remote_audio_RemoteAudioTrackID_3",
1619 report->timestamp_us(), RTCMediaStreamTrackKind::kAudio);
hbos09bc1282016-11-08 06:29:22 -08001620 expected_remote_audio_track.track_identifier = remote_audio_track->id();
1621 expected_remote_audio_track.remote_source = true;
1622 expected_remote_audio_track.ended = false;
1623 expected_remote_audio_track.detached = false;
hbos9e302742017-01-20 02:47:10 -08001624 expected_remote_audio_track.audio_level = 16383.0 / 32767.0;
zsteine76bd3a2017-07-14 12:17:49 -07001625 expected_remote_audio_track.total_audio_energy = 0.125;
Steve Anton2dbc69f2017-08-24 17:15:13 -07001626 expected_remote_audio_track.total_samples_received = 4567;
zsteine76bd3a2017-07-14 12:17:49 -07001627 expected_remote_audio_track.total_samples_duration = 0.25;
Steve Anton2dbc69f2017-08-24 17:15:13 -07001628 expected_remote_audio_track.concealed_samples = 123;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02001629 expected_remote_audio_track.concealment_events = 12;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02001630 expected_remote_audio_track.jitter_buffer_delay = 3456;
hbosdbb64d82016-12-21 01:57:46 -08001631 ASSERT_TRUE(report->Get(expected_remote_audio_track.id()));
hbos09bc1282016-11-08 06:29:22 -08001632 EXPECT_EQ(expected_remote_audio_track,
1633 report->Get(expected_remote_audio_track.id())->cast_to<
1634 RTCMediaStreamTrackStats>());
1635}
1636
1637TEST_F(RTCStatsCollectorTest,
1638 CollectRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Video) {
1639 rtc::scoped_refptr<StreamCollection> local_streams =
1640 StreamCollection::Create();
1641 rtc::scoped_refptr<StreamCollection> remote_streams =
1642 StreamCollection::Create();
1643 EXPECT_CALL(test_->pc(), local_streams())
1644 .WillRepeatedly(Return(local_streams));
1645 EXPECT_CALL(test_->pc(), remote_streams())
1646 .WillRepeatedly(Return(remote_streams));
1647
1648 rtc::scoped_refptr<MediaStream> local_stream =
1649 MediaStream::Create("LocalStreamLabel");
1650 local_streams->AddStream(local_stream);
1651 rtc::scoped_refptr<MediaStream> remote_stream =
1652 MediaStream::Create("RemoteStreamLabel");
1653 remote_streams->AddStream(remote_stream);
1654
1655 // Local video track
hbos9e302742017-01-20 02:47:10 -08001656 rtc::scoped_refptr<MediaStreamTrackInterface> local_video_track =
1657 CreateFakeTrack(cricket::MEDIA_TYPE_VIDEO, "LocalVideoTrackID",
1658 MediaStreamTrackInterface::kLive);
1659 local_stream->AddTrack(static_cast<VideoTrackInterface*>(
1660 local_video_track.get()));
hbos09bc1282016-11-08 06:29:22 -08001661
hbos9e302742017-01-20 02:47:10 -08001662 cricket::VideoSenderInfo video_sender_info_ssrc1;
1663 video_sender_info_ssrc1.local_stats.push_back(cricket::SsrcSenderInfo());
1664 video_sender_info_ssrc1.local_stats[0].ssrc = 1;
1665 video_sender_info_ssrc1.send_frame_width = 1234;
1666 video_sender_info_ssrc1.send_frame_height = 4321;
hbosfefe0762017-01-20 06:14:25 -08001667 video_sender_info_ssrc1.frames_encoded = 11;
hbos9e302742017-01-20 02:47:10 -08001668
1669 cricket::VideoSenderInfo video_sender_info_ssrc2;
1670 video_sender_info_ssrc2.local_stats.push_back(cricket::SsrcSenderInfo());
1671 video_sender_info_ssrc2.local_stats[0].ssrc = 2;
1672 video_sender_info_ssrc2.send_frame_width = 4321;
1673 video_sender_info_ssrc2.send_frame_height = 1234;
hbosfefe0762017-01-20 06:14:25 -08001674 video_sender_info_ssrc2.frames_encoded = 22;
hbos9e302742017-01-20 02:47:10 -08001675
1676 // Remote video track with values
1677 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video_track_ssrc3 =
1678 CreateFakeTrack(cricket::MEDIA_TYPE_VIDEO, "RemoteVideoTrackID3",
1679 MediaStreamTrackInterface::kEnded);
1680 remote_stream->AddTrack(static_cast<VideoTrackInterface*>(
1681 remote_video_track_ssrc3.get()));
1682
1683 cricket::VideoReceiverInfo video_receiver_info_ssrc3;
1684 video_receiver_info_ssrc3.local_stats.push_back(cricket::SsrcReceiverInfo());
1685 video_receiver_info_ssrc3.local_stats[0].ssrc = 3;
1686 video_receiver_info_ssrc3.frame_width = 6789;
1687 video_receiver_info_ssrc3.frame_height = 9876;
hbos50cfe1f2017-01-23 07:21:55 -08001688 video_receiver_info_ssrc3.frames_received = 1000;
1689 video_receiver_info_ssrc3.frames_decoded = 995;
1690 video_receiver_info_ssrc3.frames_rendered = 990;
hbos9e302742017-01-20 02:47:10 -08001691
1692 // Remote video track with undefined (default) values
1693 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video_track_ssrc4 =
1694 CreateFakeTrack(cricket::MEDIA_TYPE_VIDEO, "RemoteVideoTrackID4",
1695 MediaStreamTrackInterface::kLive);
1696 remote_stream->AddTrack(static_cast<VideoTrackInterface*>(
1697 remote_video_track_ssrc4.get()));
1698
1699 cricket::VideoReceiverInfo video_receiver_info_ssrc4;
1700 video_receiver_info_ssrc4.local_stats.push_back(cricket::SsrcReceiverInfo());
1701 video_receiver_info_ssrc4.local_stats[0].ssrc = 4;
1702 video_receiver_info_ssrc4.frame_width = 0;
1703 video_receiver_info_ssrc4.frame_height = 0;
hbos42f6d2f2017-01-20 03:56:50 -08001704 video_receiver_info_ssrc4.frames_received = 0;
hbosf64941f2017-01-20 07:39:09 -08001705 video_receiver_info_ssrc4.frames_decoded = 0;
hbos50cfe1f2017-01-23 07:21:55 -08001706 video_receiver_info_ssrc4.frames_rendered = 0;
hbos9e302742017-01-20 02:47:10 -08001707
1708 test_->CreateMockRtpSendersReceiversAndChannels(
1709 {}, {},
1710 { std::make_pair(local_video_track.get(), video_sender_info_ssrc1),
1711 std::make_pair(local_video_track.get(), video_sender_info_ssrc2) },
1712 { std::make_pair(remote_video_track_ssrc3.get(),
1713 video_receiver_info_ssrc3),
1714 std::make_pair(remote_video_track_ssrc4.get(),
1715 video_receiver_info_ssrc4) });
hbos09bc1282016-11-08 06:29:22 -08001716
1717 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1718
1719 RTCMediaStreamStats expected_local_stream(
hbos02d2a922016-12-21 01:29:05 -08001720 "RTCMediaStream_local_LocalStreamLabel", report->timestamp_us());
hbos09bc1282016-11-08 06:29:22 -08001721 expected_local_stream.stream_identifier = local_stream->label();
hbos9e302742017-01-20 02:47:10 -08001722 expected_local_stream.track_ids = std::vector<std::string>({
1723 "RTCMediaStreamTrack_local_video_LocalVideoTrackID_1",
1724 "RTCMediaStreamTrack_local_video_LocalVideoTrackID_2" });
hbosdbb64d82016-12-21 01:57:46 -08001725 ASSERT_TRUE(report->Get(expected_local_stream.id()));
hbos09bc1282016-11-08 06:29:22 -08001726 EXPECT_EQ(expected_local_stream,
1727 report->Get(expected_local_stream.id())->cast_to<
1728 RTCMediaStreamStats>());
1729
1730 RTCMediaStreamStats expected_remote_stream(
hbos02d2a922016-12-21 01:29:05 -08001731 "RTCMediaStream_remote_RemoteStreamLabel", report->timestamp_us());
hbos09bc1282016-11-08 06:29:22 -08001732 expected_remote_stream.stream_identifier = remote_stream->label();
hbos9e302742017-01-20 02:47:10 -08001733 expected_remote_stream.track_ids = std::vector<std::string>({
1734 "RTCMediaStreamTrack_remote_video_RemoteVideoTrackID3_3",
1735 "RTCMediaStreamTrack_remote_video_RemoteVideoTrackID4_4" });
hbosdbb64d82016-12-21 01:57:46 -08001736 ASSERT_TRUE(report->Get(expected_remote_stream.id()));
hbos09bc1282016-11-08 06:29:22 -08001737 EXPECT_EQ(expected_remote_stream,
1738 report->Get(expected_remote_stream.id())->cast_to<
1739 RTCMediaStreamStats>());
1740
hbos9e302742017-01-20 02:47:10 -08001741 RTCMediaStreamTrackStats expected_local_video_track_ssrc1(
1742 "RTCMediaStreamTrack_local_video_LocalVideoTrackID_1",
1743 report->timestamp_us(), RTCMediaStreamTrackKind::kVideo);
1744 expected_local_video_track_ssrc1.track_identifier = local_video_track->id();
1745 expected_local_video_track_ssrc1.remote_source = false;
1746 expected_local_video_track_ssrc1.ended = false;
1747 expected_local_video_track_ssrc1.detached = false;
1748 expected_local_video_track_ssrc1.frame_width = 1234;
1749 expected_local_video_track_ssrc1.frame_height = 4321;
hbosfefe0762017-01-20 06:14:25 -08001750 expected_local_video_track_ssrc1.frames_sent = 11;
hbos9e302742017-01-20 02:47:10 -08001751 ASSERT_TRUE(report->Get(expected_local_video_track_ssrc1.id()));
1752 EXPECT_EQ(expected_local_video_track_ssrc1,
1753 report->Get(expected_local_video_track_ssrc1.id())->cast_to<
hbos09bc1282016-11-08 06:29:22 -08001754 RTCMediaStreamTrackStats>());
1755
hbos9e302742017-01-20 02:47:10 -08001756 RTCMediaStreamTrackStats expected_local_video_track_ssrc2(
1757 "RTCMediaStreamTrack_local_video_LocalVideoTrackID_2",
1758 report->timestamp_us(), RTCMediaStreamTrackKind::kVideo);
1759 expected_local_video_track_ssrc2.track_identifier = local_video_track->id();
1760 expected_local_video_track_ssrc2.remote_source = false;
1761 expected_local_video_track_ssrc2.ended = false;
1762 expected_local_video_track_ssrc2.detached = false;
1763 expected_local_video_track_ssrc2.frame_width = 4321;
1764 expected_local_video_track_ssrc2.frame_height = 1234;
hbosfefe0762017-01-20 06:14:25 -08001765 expected_local_video_track_ssrc2.frames_sent = 22;
hbos9e302742017-01-20 02:47:10 -08001766 ASSERT_TRUE(report->Get(expected_local_video_track_ssrc2.id()));
1767 EXPECT_EQ(expected_local_video_track_ssrc2,
1768 report->Get(expected_local_video_track_ssrc2.id())->cast_to<
1769 RTCMediaStreamTrackStats>());
1770
1771 RTCMediaStreamTrackStats expected_remote_video_track_ssrc3(
1772 "RTCMediaStreamTrack_remote_video_RemoteVideoTrackID3_3",
1773 report->timestamp_us(), RTCMediaStreamTrackKind::kVideo);
1774 expected_remote_video_track_ssrc3.track_identifier =
1775 remote_video_track_ssrc3->id();
1776 expected_remote_video_track_ssrc3.remote_source = true;
1777 expected_remote_video_track_ssrc3.ended = true;
1778 expected_remote_video_track_ssrc3.detached = false;
1779 expected_remote_video_track_ssrc3.frame_width = 6789;
1780 expected_remote_video_track_ssrc3.frame_height = 9876;
hbos50cfe1f2017-01-23 07:21:55 -08001781 expected_remote_video_track_ssrc3.frames_received = 1000;
1782 expected_remote_video_track_ssrc3.frames_decoded = 995;
1783 expected_remote_video_track_ssrc3.frames_dropped = 1000 - 990;
hbos9e302742017-01-20 02:47:10 -08001784 ASSERT_TRUE(report->Get(expected_remote_video_track_ssrc3.id()));
1785 EXPECT_EQ(expected_remote_video_track_ssrc3,
1786 report->Get(expected_remote_video_track_ssrc3.id())->cast_to<
1787 RTCMediaStreamTrackStats>());
1788
1789 RTCMediaStreamTrackStats expected_remote_video_track_ssrc4(
1790 "RTCMediaStreamTrack_remote_video_RemoteVideoTrackID4_4",
1791 report->timestamp_us(), RTCMediaStreamTrackKind::kVideo);
1792 expected_remote_video_track_ssrc4.track_identifier =
1793 remote_video_track_ssrc4->id();
1794 expected_remote_video_track_ssrc4.remote_source = true;
1795 expected_remote_video_track_ssrc4.ended = false;
1796 expected_remote_video_track_ssrc4.detached = false;
hbos42f6d2f2017-01-20 03:56:50 -08001797 expected_remote_video_track_ssrc4.frames_received = 0;
hbosf64941f2017-01-20 07:39:09 -08001798 expected_remote_video_track_ssrc4.frames_decoded = 0;
hbos50cfe1f2017-01-23 07:21:55 -08001799 expected_remote_video_track_ssrc4.frames_dropped = 0;
hbos9e302742017-01-20 02:47:10 -08001800 // Should be undefined: |expected_remote_video_track_ssrc4.frame_width| and
1801 // |expected_remote_video_track_ssrc4.frame_height|.
1802 ASSERT_TRUE(report->Get(expected_remote_video_track_ssrc4.id()));
1803 EXPECT_EQ(expected_remote_video_track_ssrc4,
1804 report->Get(expected_remote_video_track_ssrc4.id())->cast_to<
hbos09bc1282016-11-08 06:29:22 -08001805 RTCMediaStreamTrackStats>());
1806}
1807
hboseeafe942016-11-01 03:00:17 -07001808TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) {
Steve Anton8699a322017-11-06 15:53:33 -08001809 auto* voice_media_channel = new MockVoiceMediaChannel();
hboseeafe942016-11-01 03:00:17 -07001810 cricket::VoiceChannel voice_channel(
deadbeefbad5dad2017-01-17 18:32:35 -08001811 test_->worker_thread(), test_->network_thread(),
Steve Anton8699a322017-11-06 15:53:33 -08001812 test_->signaling_thread(), test_->media_engine(),
1813 rtc::WrapUnique(voice_media_channel), "VoiceContentName",
1814 kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
Steve Anton593e3252017-12-15 11:44:48 -08001815 voice_channel.set_transport_name_for_testing("TransportName");
hboseeafe942016-11-01 03:00:17 -07001816
hbos84abeb12017-01-16 06:16:44 -08001817 test_->SetupRemoteTrackAndReceiver(
1818 cricket::MEDIA_TYPE_AUDIO, "RemoteAudioTrackID", 1);
1819
hboseeafe942016-11-01 03:00:17 -07001820 cricket::VoiceMediaInfo voice_media_info;
hbos0adb8282016-11-23 02:32:06 -08001821
hboseeafe942016-11-01 03:00:17 -07001822 voice_media_info.receivers.push_back(cricket::VoiceReceiverInfo());
1823 voice_media_info.receivers[0].local_stats.push_back(
1824 cricket::SsrcReceiverInfo());
1825 voice_media_info.receivers[0].local_stats[0].ssrc = 1;
Harald Alvestrand719487e2017-12-13 12:26:04 +01001826 voice_media_info.receivers[0].packets_lost = -1; // Signed per RFC3550
hboseeafe942016-11-01 03:00:17 -07001827 voice_media_info.receivers[0].packets_rcvd = 2;
1828 voice_media_info.receivers[0].bytes_rcvd = 3;
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001829 voice_media_info.receivers[0].codec_payload_type = 42;
hboseeafe942016-11-01 03:00:17 -07001830 voice_media_info.receivers[0].jitter_ms = 4500;
1831 voice_media_info.receivers[0].fraction_lost = 5.5f;
hbos0adb8282016-11-23 02:32:06 -08001832
1833 RtpCodecParameters codec_parameters;
1834 codec_parameters.payload_type = 42;
deadbeefe702b302017-02-04 12:09:01 -08001835 codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO;
1836 codec_parameters.name = "dummy";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001837 codec_parameters.clock_rate = 0;
hbos0adb8282016-11-23 02:32:06 -08001838 voice_media_info.receive_codecs.insert(
1839 std::make_pair(codec_parameters.payload_type, codec_parameters));
1840
hboseeafe942016-11-01 03:00:17 -07001841 EXPECT_CALL(*voice_media_channel, GetStats(_))
1842 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true)));
1843
1844 SessionStats session_stats;
hboseeafe942016-11-01 03:00:17 -07001845 session_stats.transport_stats["TransportName"].transport_name =
1846 "TransportName";
1847
1848 // Make sure the associated |RTCTransportStats| is created.
1849 cricket::TransportChannelStats channel_stats;
1850 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
1851 session_stats.transport_stats["TransportName"].channel_stats.push_back(
1852 channel_stats);
1853
Steve Anton75737c02017-11-06 10:37:17 -08001854 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -08001855 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -08001856 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
1857 }));
Steve Anton75737c02017-11-06 10:37:17 -08001858 EXPECT_CALL(test_->pc(), voice_channel())
hboseeafe942016-11-01 03:00:17 -07001859 .WillRepeatedly(Return(&voice_channel));
1860
1861 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1862
1863 RTCInboundRTPStreamStats expected_audio(
1864 "RTCInboundRTPAudioStream_1", report->timestamp_us());
hbos3443bb72017-02-07 06:28:11 -08001865 expected_audio.ssrc = 1;
hboseeafe942016-11-01 03:00:17 -07001866 expected_audio.is_remote = false;
1867 expected_audio.media_type = "audio";
hbosb0ae9202017-01-27 06:35:16 -08001868 expected_audio.track_id =
hbos9e302742017-01-20 02:47:10 -08001869 "RTCMediaStreamTrack_remote_audio_RemoteAudioTrackID_1";
hboseeafe942016-11-01 03:00:17 -07001870 expected_audio.transport_id = "RTCTransport_TransportName_" +
1871 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001872 expected_audio.codec_id = "RTCCodec_InboundAudio_42";
hboseeafe942016-11-01 03:00:17 -07001873 expected_audio.packets_received = 2;
1874 expected_audio.bytes_received = 3;
Harald Alvestrand719487e2017-12-13 12:26:04 +01001875 expected_audio.packets_lost = -1;
hboseeafe942016-11-01 03:00:17 -07001876 expected_audio.jitter = 4.5;
1877 expected_audio.fraction_lost = 5.5;
1878
nissec8ee8822017-01-18 07:20:55 -08001879 ASSERT_TRUE(report->Get(expected_audio.id()));
hbosa51d4f32017-02-16 05:34:48 -08001880 EXPECT_EQ(
1881 report->Get(expected_audio.id())->cast_to<RTCInboundRTPStreamStats>(),
1882 expected_audio);
hbosb0ae9202017-01-27 06:35:16 -08001883 EXPECT_TRUE(report->Get(*expected_audio.track_id));
hbos84abeb12017-01-16 06:16:44 -08001884 EXPECT_TRUE(report->Get(*expected_audio.transport_id));
1885 EXPECT_TRUE(report->Get(*expected_audio.codec_id));
hboseeafe942016-11-01 03:00:17 -07001886}
1887
1888TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) {
Steve Anton8699a322017-11-06 15:53:33 -08001889 auto* video_media_channel = new MockVideoMediaChannel();
hboseeafe942016-11-01 03:00:17 -07001890 cricket::VideoChannel video_channel(
deadbeefbad5dad2017-01-17 18:32:35 -08001891 test_->worker_thread(), test_->network_thread(),
Steve Anton8699a322017-11-06 15:53:33 -08001892 test_->signaling_thread(), rtc::WrapUnique(video_media_channel),
1893 "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
Steve Anton593e3252017-12-15 11:44:48 -08001894 video_channel.set_transport_name_for_testing("TransportName");
hboseeafe942016-11-01 03:00:17 -07001895
hbos84abeb12017-01-16 06:16:44 -08001896 test_->SetupRemoteTrackAndReceiver(
1897 cricket::MEDIA_TYPE_VIDEO, "RemoteVideoTrackID", 1);
1898
hboseeafe942016-11-01 03:00:17 -07001899 cricket::VideoMediaInfo video_media_info;
hbos0adb8282016-11-23 02:32:06 -08001900
hboseeafe942016-11-01 03:00:17 -07001901 video_media_info.receivers.push_back(cricket::VideoReceiverInfo());
1902 video_media_info.receivers[0].local_stats.push_back(
1903 cricket::SsrcReceiverInfo());
1904 video_media_info.receivers[0].local_stats[0].ssrc = 1;
1905 video_media_info.receivers[0].packets_rcvd = 2;
hbos02cd4d62016-12-09 04:19:44 -08001906 video_media_info.receivers[0].packets_lost = 42;
hboseeafe942016-11-01 03:00:17 -07001907 video_media_info.receivers[0].bytes_rcvd = 3;
1908 video_media_info.receivers[0].fraction_lost = 4.5f;
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001909 video_media_info.receivers[0].codec_payload_type = 42;
hbos820f5782016-11-22 03:16:50 -08001910 video_media_info.receivers[0].firs_sent = 5;
1911 video_media_info.receivers[0].plis_sent = 6;
1912 video_media_info.receivers[0].nacks_sent = 7;
hbos6769c492017-01-02 08:35:13 -08001913 video_media_info.receivers[0].frames_decoded = 8;
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001914 video_media_info.receivers[0].qp_sum = rtc::nullopt;
hbos0adb8282016-11-23 02:32:06 -08001915
1916 RtpCodecParameters codec_parameters;
1917 codec_parameters.payload_type = 42;
deadbeefe702b302017-02-04 12:09:01 -08001918 codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO;
1919 codec_parameters.name = "dummy";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001920 codec_parameters.clock_rate = 0;
hbos0adb8282016-11-23 02:32:06 -08001921 video_media_info.receive_codecs.insert(
1922 std::make_pair(codec_parameters.payload_type, codec_parameters));
1923
hboseeafe942016-11-01 03:00:17 -07001924 EXPECT_CALL(*video_media_channel, GetStats(_))
1925 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
1926
1927 SessionStats session_stats;
hboseeafe942016-11-01 03:00:17 -07001928 session_stats.transport_stats["TransportName"].transport_name =
1929 "TransportName";
1930
1931 // Make sure the associated |RTCTransportStats| is created.
1932 cricket::TransportChannelStats channel_stats;
1933 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
1934 session_stats.transport_stats["TransportName"].channel_stats.push_back(
1935 channel_stats);
1936
Steve Anton75737c02017-11-06 10:37:17 -08001937 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -08001938 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -08001939 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
1940 }));
Steve Anton75737c02017-11-06 10:37:17 -08001941 EXPECT_CALL(test_->pc(), video_channel())
hboseeafe942016-11-01 03:00:17 -07001942 .WillRepeatedly(Return(&video_channel));
1943
1944 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
1945
hbos820f5782016-11-22 03:16:50 -08001946 RTCInboundRTPStreamStats expected_video(
hboseeafe942016-11-01 03:00:17 -07001947 "RTCInboundRTPVideoStream_1", report->timestamp_us());
hbos3443bb72017-02-07 06:28:11 -08001948 expected_video.ssrc = 1;
hbos820f5782016-11-22 03:16:50 -08001949 expected_video.is_remote = false;
1950 expected_video.media_type = "video";
hbosb0ae9202017-01-27 06:35:16 -08001951 expected_video.track_id =
hbos9e302742017-01-20 02:47:10 -08001952 "RTCMediaStreamTrack_remote_video_RemoteVideoTrackID_1";
hbos820f5782016-11-22 03:16:50 -08001953 expected_video.transport_id = "RTCTransport_TransportName_" +
hboseeafe942016-11-01 03:00:17 -07001954 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08001955 expected_video.codec_id = "RTCCodec_InboundVideo_42";
hbos820f5782016-11-22 03:16:50 -08001956 expected_video.fir_count = 5;
1957 expected_video.pli_count = 6;
1958 expected_video.nack_count = 7;
1959 expected_video.packets_received = 2;
1960 expected_video.bytes_received = 3;
hbos02cd4d62016-12-09 04:19:44 -08001961 expected_video.packets_lost = 42;
hbos820f5782016-11-22 03:16:50 -08001962 expected_video.fraction_lost = 4.5;
hbos6769c492017-01-02 08:35:13 -08001963 expected_video.frames_decoded = 8;
hbosa51d4f32017-02-16 05:34:48 -08001964 // |expected_video.qp_sum| should be undefined.
hboseeafe942016-11-01 03:00:17 -07001965
nissec8ee8822017-01-18 07:20:55 -08001966 ASSERT_TRUE(report->Get(expected_video.id()));
hbosa51d4f32017-02-16 05:34:48 -08001967 EXPECT_EQ(
1968 report->Get(expected_video.id())->cast_to<RTCInboundRTPStreamStats>(),
1969 expected_video);
hboseeafe942016-11-01 03:00:17 -07001970
hbosa51d4f32017-02-16 05:34:48 -08001971 // Set previously undefined values and "GetStats" again.
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01001972 video_media_info.receivers[0].qp_sum = 9;
hbosa51d4f32017-02-16 05:34:48 -08001973 expected_video.qp_sum = 9;
1974
1975 EXPECT_CALL(*video_media_channel, GetStats(_))
1976 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
1977 collector_->ClearCachedStatsReport();
1978 report = GetStatsReport();
1979
1980 ASSERT_TRUE(report->Get(expected_video.id()));
1981 EXPECT_EQ(
1982 report->Get(expected_video.id())->cast_to<RTCInboundRTPStreamStats>(),
1983 expected_video);
hbosb0ae9202017-01-27 06:35:16 -08001984 EXPECT_TRUE(report->Get(*expected_video.track_id));
hbos84abeb12017-01-16 06:16:44 -08001985 EXPECT_TRUE(report->Get(*expected_video.transport_id));
hbosa51d4f32017-02-16 05:34:48 -08001986 EXPECT_TRUE(report->Get(*expected_video.codec_id));
hboseeafe942016-11-01 03:00:17 -07001987}
1988
hbos6ded1902016-11-01 01:50:46 -07001989TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) {
Steve Anton8699a322017-11-06 15:53:33 -08001990 auto* voice_media_channel = new MockVoiceMediaChannel();
hbos6ded1902016-11-01 01:50:46 -07001991 cricket::VoiceChannel voice_channel(
deadbeefbad5dad2017-01-17 18:32:35 -08001992 test_->worker_thread(), test_->network_thread(),
Steve Anton8699a322017-11-06 15:53:33 -08001993 test_->signaling_thread(), test_->media_engine(),
1994 rtc::WrapUnique(voice_media_channel), "VoiceContentName",
1995 kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
Steve Anton593e3252017-12-15 11:44:48 -08001996 voice_channel.set_transport_name_for_testing("TransportName");
hbos6ded1902016-11-01 01:50:46 -07001997
Harald Alvestrand89061872018-01-02 14:08:34 +01001998 test_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO,
1999 "LocalAudioTrackID", 1, true);
hbos84abeb12017-01-16 06:16:44 -08002000
hbos6ded1902016-11-01 01:50:46 -07002001 cricket::VoiceMediaInfo voice_media_info;
hbos0adb8282016-11-23 02:32:06 -08002002
hbos6ded1902016-11-01 01:50:46 -07002003 voice_media_info.senders.push_back(cricket::VoiceSenderInfo());
2004 voice_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo());
2005 voice_media_info.senders[0].local_stats[0].ssrc = 1;
2006 voice_media_info.senders[0].packets_sent = 2;
2007 voice_media_info.senders[0].bytes_sent = 3;
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01002008 voice_media_info.senders[0].codec_payload_type = 42;
hbos0adb8282016-11-23 02:32:06 -08002009
2010 RtpCodecParameters codec_parameters;
2011 codec_parameters.payload_type = 42;
deadbeefe702b302017-02-04 12:09:01 -08002012 codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO;
2013 codec_parameters.name = "dummy";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01002014 codec_parameters.clock_rate = 0;
hbos0adb8282016-11-23 02:32:06 -08002015 voice_media_info.send_codecs.insert(
2016 std::make_pair(codec_parameters.payload_type, codec_parameters));
2017
hbos6ded1902016-11-01 01:50:46 -07002018 EXPECT_CALL(*voice_media_channel, GetStats(_))
2019 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true)));
2020
2021 SessionStats session_stats;
hbos6ded1902016-11-01 01:50:46 -07002022 session_stats.transport_stats["TransportName"].transport_name =
2023 "TransportName";
2024
2025 // Make sure the associated |RTCTransportStats| is created.
2026 cricket::TransportChannelStats channel_stats;
2027 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
hbos6ded1902016-11-01 01:50:46 -07002028 session_stats.transport_stats["TransportName"].channel_stats.push_back(
2029 channel_stats);
2030
Steve Anton75737c02017-11-06 10:37:17 -08002031 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -08002032 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -08002033 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
2034 }));
Steve Anton75737c02017-11-06 10:37:17 -08002035 EXPECT_CALL(test_->pc(), voice_channel())
hbos6ded1902016-11-01 01:50:46 -07002036 .WillRepeatedly(Return(&voice_channel));
2037
2038 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
2039
2040 RTCOutboundRTPStreamStats expected_audio(
2041 "RTCOutboundRTPAudioStream_1", report->timestamp_us());
hbos3443bb72017-02-07 06:28:11 -08002042 expected_audio.ssrc = 1;
hbos6ded1902016-11-01 01:50:46 -07002043 expected_audio.is_remote = false;
2044 expected_audio.media_type = "audio";
hbosb0ae9202017-01-27 06:35:16 -08002045 expected_audio.track_id =
hbos9e302742017-01-20 02:47:10 -08002046 "RTCMediaStreamTrack_local_audio_LocalAudioTrackID_1";
hbos6ded1902016-11-01 01:50:46 -07002047 expected_audio.transport_id = "RTCTransport_TransportName_" +
2048 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08002049 expected_audio.codec_id = "RTCCodec_OutboundAudio_42";
hbos6ded1902016-11-01 01:50:46 -07002050 expected_audio.packets_sent = 2;
2051 expected_audio.bytes_sent = 3;
hboscd195be2017-02-07 08:31:27 -08002052
hboscd195be2017-02-07 08:31:27 -08002053 ASSERT_TRUE(report->Get(expected_audio.id()));
hbosa51d4f32017-02-16 05:34:48 -08002054 EXPECT_EQ(
2055 report->Get(expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(),
2056 expected_audio);
skvladed02c6d2017-02-07 10:45:31 -08002057
hbosa51d4f32017-02-16 05:34:48 -08002058 ASSERT_TRUE(report->Get(expected_audio.id()));
2059 EXPECT_EQ(
2060 report->Get(expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(),
2061 expected_audio);
hbosb0ae9202017-01-27 06:35:16 -08002062 EXPECT_TRUE(report->Get(*expected_audio.track_id));
hbos84abeb12017-01-16 06:16:44 -08002063 EXPECT_TRUE(report->Get(*expected_audio.transport_id));
2064 EXPECT_TRUE(report->Get(*expected_audio.codec_id));
hbos6ded1902016-11-01 01:50:46 -07002065}
2066
2067TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) {
Steve Anton8699a322017-11-06 15:53:33 -08002068 auto* video_media_channel = new MockVideoMediaChannel();
hbos6ded1902016-11-01 01:50:46 -07002069 cricket::VideoChannel video_channel(
deadbeefbad5dad2017-01-17 18:32:35 -08002070 test_->worker_thread(), test_->network_thread(),
Steve Anton8699a322017-11-06 15:53:33 -08002071 test_->signaling_thread(), rtc::WrapUnique(video_media_channel),
2072 "VideoContentName", kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
Steve Anton593e3252017-12-15 11:44:48 -08002073 video_channel.set_transport_name_for_testing("TransportName");
hbos6ded1902016-11-01 01:50:46 -07002074
Harald Alvestrand89061872018-01-02 14:08:34 +01002075 test_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_VIDEO,
2076 "LocalVideoTrackID", 1, true);
hbos84abeb12017-01-16 06:16:44 -08002077
hbos6ded1902016-11-01 01:50:46 -07002078 cricket::VideoMediaInfo video_media_info;
hbos0adb8282016-11-23 02:32:06 -08002079
hbos6ded1902016-11-01 01:50:46 -07002080 video_media_info.senders.push_back(cricket::VideoSenderInfo());
2081 video_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo());
2082 video_media_info.senders[0].local_stats[0].ssrc = 1;
2083 video_media_info.senders[0].firs_rcvd = 2;
2084 video_media_info.senders[0].plis_rcvd = 3;
2085 video_media_info.senders[0].nacks_rcvd = 4;
2086 video_media_info.senders[0].packets_sent = 5;
2087 video_media_info.senders[0].bytes_sent = 6;
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01002088 video_media_info.senders[0].codec_payload_type = 42;
hbos6769c492017-01-02 08:35:13 -08002089 video_media_info.senders[0].frames_encoded = 8;
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01002090 video_media_info.senders[0].qp_sum = rtc::nullopt;
hbos0adb8282016-11-23 02:32:06 -08002091
2092 RtpCodecParameters codec_parameters;
2093 codec_parameters.payload_type = 42;
deadbeefe702b302017-02-04 12:09:01 -08002094 codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO;
2095 codec_parameters.name = "dummy";
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01002096 codec_parameters.clock_rate = 0;
hbos0adb8282016-11-23 02:32:06 -08002097 video_media_info.send_codecs.insert(
2098 std::make_pair(codec_parameters.payload_type, codec_parameters));
2099
hbos6ded1902016-11-01 01:50:46 -07002100 EXPECT_CALL(*video_media_channel, GetStats(_))
2101 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
2102
2103 SessionStats session_stats;
hbos6ded1902016-11-01 01:50:46 -07002104 session_stats.transport_stats["TransportName"].transport_name =
2105 "TransportName";
2106
2107 // Make sure the associated |RTCTransportStats| is created.
2108 cricket::TransportChannelStats channel_stats;
2109 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
hbos6ded1902016-11-01 01:50:46 -07002110 session_stats.transport_stats["TransportName"].channel_stats.push_back(
2111 channel_stats);
2112
Steve Anton75737c02017-11-06 10:37:17 -08002113 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -08002114 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -08002115 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
2116 }));
Steve Anton75737c02017-11-06 10:37:17 -08002117 EXPECT_CALL(test_->pc(), video_channel())
hbos6ded1902016-11-01 01:50:46 -07002118 .WillRepeatedly(Return(&video_channel));
2119
2120 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
2121
2122 RTCOutboundRTPStreamStats expected_video(
2123 "RTCOutboundRTPVideoStream_1", report->timestamp_us());
hbos3443bb72017-02-07 06:28:11 -08002124 expected_video.ssrc = 1;
hbos6ded1902016-11-01 01:50:46 -07002125 expected_video.is_remote = false;
2126 expected_video.media_type = "video";
hbosb0ae9202017-01-27 06:35:16 -08002127 expected_video.track_id =
hbos9e302742017-01-20 02:47:10 -08002128 "RTCMediaStreamTrack_local_video_LocalVideoTrackID_1";
hbos6ded1902016-11-01 01:50:46 -07002129 expected_video.transport_id = "RTCTransport_TransportName_" +
2130 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP);
hbos0adb8282016-11-23 02:32:06 -08002131 expected_video.codec_id = "RTCCodec_OutboundVideo_42";
hbos6ded1902016-11-01 01:50:46 -07002132 expected_video.fir_count = 2;
2133 expected_video.pli_count = 3;
2134 expected_video.nack_count = 4;
2135 expected_video.packets_sent = 5;
2136 expected_video.bytes_sent = 6;
skvladed02c6d2017-02-07 10:45:31 -08002137 expected_video.frames_encoded = 8;
hbosa7a9be12017-03-01 01:02:45 -08002138 // |expected_video.qp_sum| should be undefined.
hboscd195be2017-02-07 08:31:27 -08002139
2140 ASSERT_TRUE(report->Get(expected_video.id()));
hbosa51d4f32017-02-16 05:34:48 -08002141 EXPECT_EQ(
2142 report->Get(expected_video.id())->cast_to<RTCOutboundRTPStreamStats>(),
2143 expected_video);
skvladed02c6d2017-02-07 10:45:31 -08002144
hbosa51d4f32017-02-16 05:34:48 -08002145 // Set previously undefined values and "GetStats" again.
Oskar Sundbomcbc71b22017-11-16 10:56:07 +01002146 video_media_info.senders[0].qp_sum = 9;
hbosa51d4f32017-02-16 05:34:48 -08002147 expected_video.qp_sum = 9;
2148
2149 EXPECT_CALL(*video_media_channel, GetStats(_))
2150 .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true)));
2151 collector_->ClearCachedStatsReport();
2152 report = GetStatsReport();
2153
2154 ASSERT_TRUE(report->Get(expected_video.id()));
2155 EXPECT_EQ(
2156 report->Get(expected_video.id())->cast_to<RTCOutboundRTPStreamStats>(),
2157 expected_video);
hbosb0ae9202017-01-27 06:35:16 -08002158 EXPECT_TRUE(report->Get(*expected_video.track_id));
hbos84abeb12017-01-16 06:16:44 -08002159 EXPECT_TRUE(report->Get(*expected_video.transport_id));
2160 EXPECT_TRUE(report->Get(*expected_video.codec_id));
hbos6ded1902016-11-01 01:50:46 -07002161}
2162
hbos2fa7c672016-10-24 04:00:05 -07002163TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) {
Gary Liu37e489c2017-11-21 10:49:36 -08002164 std::unique_ptr<cricket::Candidate> rtp_local_candidate =
2165 CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI,
2166 cricket::LOCAL_PORT_TYPE, 42);
hbos2fa7c672016-10-24 04:00:05 -07002167 std::unique_ptr<cricket::Candidate> rtp_remote_candidate =
2168 CreateFakeCandidate("42.42.42.42", 42, "protocol",
Gary Liu37e489c2017-11-21 10:49:36 -08002169 rtc::ADAPTER_TYPE_UNKNOWN, cricket::LOCAL_PORT_TYPE,
2170 42);
hbos2fa7c672016-10-24 04:00:05 -07002171 std::unique_ptr<cricket::Candidate> rtcp_local_candidate =
Gary Liu37e489c2017-11-21 10:49:36 -08002172 CreateFakeCandidate("42.42.42.42", 42, "protocol", rtc::ADAPTER_TYPE_WIFI,
hbos2fa7c672016-10-24 04:00:05 -07002173 cricket::LOCAL_PORT_TYPE, 42);
2174 std::unique_ptr<cricket::Candidate> rtcp_remote_candidate =
2175 CreateFakeCandidate("42.42.42.42", 42, "protocol",
Gary Liu37e489c2017-11-21 10:49:36 -08002176 rtc::ADAPTER_TYPE_UNKNOWN, cricket::LOCAL_PORT_TYPE,
2177 42);
hbos2fa7c672016-10-24 04:00:05 -07002178
2179 SessionStats session_stats;
2180 session_stats.transport_stats["transport"].transport_name = "transport";
2181
2182 cricket::ConnectionInfo rtp_connection_info;
2183 rtp_connection_info.best_connection = false;
2184 rtp_connection_info.local_candidate = *rtp_local_candidate.get();
2185 rtp_connection_info.remote_candidate = *rtp_remote_candidate.get();
2186 rtp_connection_info.sent_total_bytes = 42;
2187 rtp_connection_info.recv_total_bytes = 1337;
2188 cricket::TransportChannelStats rtp_transport_channel_stats;
2189 rtp_transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
2190 rtp_transport_channel_stats.connection_infos.push_back(rtp_connection_info);
hbos7064d592017-01-16 07:38:02 -08002191 rtp_transport_channel_stats.dtls_state = cricket::DTLS_TRANSPORT_NEW;
hbos2fa7c672016-10-24 04:00:05 -07002192 session_stats.transport_stats["transport"].channel_stats.push_back(
2193 rtp_transport_channel_stats);
2194
2195
2196 // Mock the session to return the desired candidates.
Steve Anton75737c02017-11-06 10:37:17 -08002197 EXPECT_CALL(test_->pc(), GetSessionStats(_))
Steve Antonba818672017-11-06 10:21:57 -08002198 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
hbosdf6075a2016-12-19 04:58:02 -08002199 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
hbos2fa7c672016-10-24 04:00:05 -07002200 }));
2201
2202 // Get stats without RTCP, an active connection or certificates.
2203 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbos0583b282016-11-30 01:50:14 -08002204
2205 RTCTransportStats expected_rtp_transport(
2206 "RTCTransport_transport_" +
2207 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP),
2208 report->timestamp_us());
2209 expected_rtp_transport.bytes_sent = 42;
2210 expected_rtp_transport.bytes_received = 1337;
hbos7064d592017-01-16 07:38:02 -08002211 expected_rtp_transport.dtls_state = RTCDtlsTransportState::kNew;
hbos0583b282016-11-30 01:50:14 -08002212
hbosdbb64d82016-12-21 01:57:46 -08002213 ASSERT_TRUE(report->Get(expected_rtp_transport.id()));
hbos0583b282016-11-30 01:50:14 -08002214 EXPECT_EQ(
2215 expected_rtp_transport,
2216 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>());
hbos2fa7c672016-10-24 04:00:05 -07002217
2218 cricket::ConnectionInfo rtcp_connection_info;
2219 rtcp_connection_info.best_connection = false;
2220 rtcp_connection_info.local_candidate = *rtcp_local_candidate.get();
2221 rtcp_connection_info.remote_candidate = *rtcp_remote_candidate.get();
2222 rtcp_connection_info.sent_total_bytes = 1337;
2223 rtcp_connection_info.recv_total_bytes = 42;
2224 cricket::TransportChannelStats rtcp_transport_channel_stats;
2225 rtcp_transport_channel_stats.component =
2226 cricket::ICE_CANDIDATE_COMPONENT_RTCP;
2227 rtcp_transport_channel_stats.connection_infos.push_back(rtcp_connection_info);
hbos7064d592017-01-16 07:38:02 -08002228 rtcp_transport_channel_stats.dtls_state = cricket::DTLS_TRANSPORT_CONNECTING;
hbos2fa7c672016-10-24 04:00:05 -07002229 session_stats.transport_stats["transport"].channel_stats.push_back(
2230 rtcp_transport_channel_stats);
2231
2232 collector_->ClearCachedStatsReport();
2233 // Get stats with RTCP and without an active connection or certificates.
2234 report = GetStatsReport();
hbos0583b282016-11-30 01:50:14 -08002235
2236 RTCTransportStats expected_rtcp_transport(
2237 "RTCTransport_transport_" +
2238 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTCP),
2239 report->timestamp_us());
2240 expected_rtcp_transport.bytes_sent = 1337;
2241 expected_rtcp_transport.bytes_received = 42;
hbos7064d592017-01-16 07:38:02 -08002242 expected_rtcp_transport.dtls_state = RTCDtlsTransportState::kConnecting;
hbos0583b282016-11-30 01:50:14 -08002243
2244 expected_rtp_transport.rtcp_transport_stats_id = expected_rtcp_transport.id();
2245
hbosdbb64d82016-12-21 01:57:46 -08002246 ASSERT_TRUE(report->Get(expected_rtp_transport.id()));
hbos0583b282016-11-30 01:50:14 -08002247 EXPECT_EQ(
2248 expected_rtp_transport,
2249 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>());
hbosdbb64d82016-12-21 01:57:46 -08002250 ASSERT_TRUE(report->Get(expected_rtcp_transport.id()));
hbos0583b282016-11-30 01:50:14 -08002251 EXPECT_EQ(
2252 expected_rtcp_transport,
2253 report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>());
hbos2fa7c672016-10-24 04:00:05 -07002254
hbos7064d592017-01-16 07:38:02 -08002255 // Get stats with an active connection (selected candidate pair).
hbos0583b282016-11-30 01:50:14 -08002256 session_stats.transport_stats["transport"]
2257 .channel_stats[1]
2258 .connection_infos[0]
2259 .best_connection = true;
hbos2fa7c672016-10-24 04:00:05 -07002260
2261 collector_->ClearCachedStatsReport();
2262 report = GetStatsReport();
hbos0583b282016-11-30 01:50:14 -08002263
hbos0583b282016-11-30 01:50:14 -08002264 expected_rtcp_transport.selected_candidate_pair_id =
2265 "RTCIceCandidatePair_" + rtcp_local_candidate->id() + "_" +
2266 rtcp_remote_candidate->id();
2267
hbosdbb64d82016-12-21 01:57:46 -08002268 ASSERT_TRUE(report->Get(expected_rtp_transport.id()));
hbos0583b282016-11-30 01:50:14 -08002269 EXPECT_EQ(
2270 expected_rtp_transport,
2271 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>());
hbosdbb64d82016-12-21 01:57:46 -08002272 ASSERT_TRUE(report->Get(expected_rtcp_transport.id()));
hbos0583b282016-11-30 01:50:14 -08002273 EXPECT_EQ(
2274 expected_rtcp_transport,
2275 report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>());
hbos2fa7c672016-10-24 04:00:05 -07002276
2277 // Get stats with certificates.
2278 std::unique_ptr<CertificateInfo> local_certinfo =
2279 CreateFakeCertificateAndInfoFromDers(
2280 std::vector<std::string>({ "(local) local", "(local) chain" }));
2281 std::unique_ptr<CertificateInfo> remote_certinfo =
2282 CreateFakeCertificateAndInfoFromDers(
2283 std::vector<std::string>({ "(remote) local", "(remote) chain" }));
Steve Anton75737c02017-11-06 10:37:17 -08002284 EXPECT_CALL(test_->pc(), GetLocalCertificate(_, _))
2285 .WillRepeatedly(
2286 Invoke([&local_certinfo](
2287 const std::string& transport_name,
2288 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
2289 if (transport_name == "transport") {
2290 *certificate = local_certinfo->certificate;
2291 return true;
2292 }
2293 return false;
2294 }));
2295 EXPECT_CALL(test_->pc(), GetRemoteSSLCertificate_ReturnsRawPointer(_))
2296 .WillRepeatedly(Invoke([&remote_certinfo](
2297 const std::string& transport_name) {
hbos2fa7c672016-10-24 04:00:05 -07002298 if (transport_name == "transport")
2299 return remote_certinfo->certificate->ssl_certificate().GetReference();
2300 return static_cast<rtc::SSLCertificate*>(nullptr);
2301 }));
2302
2303 collector_->ClearCachedStatsReport();
2304 report = GetStatsReport();
hbos0583b282016-11-30 01:50:14 -08002305
2306 expected_rtp_transport.local_certificate_id =
2307 "RTCCertificate_" + local_certinfo->fingerprints[0];
2308 expected_rtp_transport.remote_certificate_id =
2309 "RTCCertificate_" + remote_certinfo->fingerprints[0];
2310
2311 expected_rtcp_transport.local_certificate_id =
2312 *expected_rtp_transport.local_certificate_id;
2313 expected_rtcp_transport.remote_certificate_id =
2314 *expected_rtp_transport.remote_certificate_id;
2315
hbosdbb64d82016-12-21 01:57:46 -08002316 ASSERT_TRUE(report->Get(expected_rtp_transport.id()));
hbos0583b282016-11-30 01:50:14 -08002317 EXPECT_EQ(
2318 expected_rtp_transport,
2319 report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>());
hbosdbb64d82016-12-21 01:57:46 -08002320 ASSERT_TRUE(report->Get(expected_rtcp_transport.id()));
hbos0583b282016-11-30 01:50:14 -08002321 EXPECT_EQ(
2322 expected_rtcp_transport,
2323 report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>());
hbos2fa7c672016-10-24 04:00:05 -07002324}
2325
Harald Alvestrand89061872018-01-02 14:08:34 +01002326TEST_F(RTCStatsCollectorTest, CollectNoStreamRTCOutboundRTPStreamStats_Audio) {
2327 // Emulates the case where AddTrack is used without an associated MediaStream
2328 auto* voice_media_channel = new MockVoiceMediaChannel();
2329 cricket::VoiceChannel voice_channel(
2330 test_->worker_thread(), test_->network_thread(),
2331 test_->signaling_thread(), test_->media_engine(),
2332 rtc::WrapUnique(voice_media_channel), "VoiceContentName",
2333 kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
2334 voice_channel.set_transport_name_for_testing("TransportName");
2335
2336 test_->SetupLocalTrackAndSender(cricket::MEDIA_TYPE_AUDIO,
2337 "LocalAudioTrackID", 1, false);
2338
2339 cricket::VoiceMediaInfo voice_media_info;
2340
2341 voice_media_info.senders.push_back(cricket::VoiceSenderInfo());
2342 voice_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo());
2343 voice_media_info.senders[0].local_stats[0].ssrc = 1;
2344 voice_media_info.senders[0].packets_sent = 2;
2345 voice_media_info.senders[0].bytes_sent = 3;
2346 voice_media_info.senders[0].codec_payload_type = 42;
2347
2348 RtpCodecParameters codec_parameters;
2349 codec_parameters.payload_type = 42;
2350 codec_parameters.kind = cricket::MEDIA_TYPE_AUDIO;
2351 codec_parameters.name = "dummy";
2352 codec_parameters.clock_rate = 0;
2353 voice_media_info.send_codecs.insert(
2354 std::make_pair(codec_parameters.payload_type, codec_parameters));
2355
2356 EXPECT_CALL(*voice_media_channel, GetStats(_))
2357 .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true)));
2358
2359 SessionStats session_stats;
2360 session_stats.transport_stats["TransportName"].transport_name =
2361 "TransportName";
2362
2363 // Make sure the associated |RTCTransportStats| is created.
2364 cricket::TransportChannelStats channel_stats;
2365 channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
2366 session_stats.transport_stats["TransportName"].channel_stats.push_back(
2367 channel_stats);
2368
2369 EXPECT_CALL(test_->pc(), GetSessionStats(_))
2370 .WillRepeatedly(Invoke([&session_stats](const ChannelNamePairs&) {
2371 return std::unique_ptr<SessionStats>(new SessionStats(session_stats));
2372 }));
2373 EXPECT_CALL(test_->pc(), voice_channel())
2374 .WillRepeatedly(Return(&voice_channel));
2375
2376 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
2377
2378 RTCOutboundRTPStreamStats expected_audio("RTCOutboundRTPAudioStream_1",
2379 report->timestamp_us());
2380 expected_audio.ssrc = 1;
2381 expected_audio.is_remote = false;
2382 expected_audio.media_type = "audio";
2383 expected_audio.track_id =
2384 "RTCMediaStreamTrack_local_audio_LocalAudioTrackID_1";
2385 expected_audio.transport_id =
2386 "RTCTransport_TransportName_" +
2387 rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP);
2388 expected_audio.codec_id = "RTCCodec_OutboundAudio_42";
2389 expected_audio.packets_sent = 2;
2390 expected_audio.bytes_sent = 3;
2391
2392 ASSERT_TRUE(report->Get(expected_audio.id()));
2393 EXPECT_EQ(
2394 report->Get(expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(),
2395 expected_audio);
2396
2397 ASSERT_TRUE(report->Get(expected_audio.id()));
2398 EXPECT_EQ(
2399 report->Get(expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(),
2400 expected_audio);
2401 EXPECT_TRUE(report->Get(*expected_audio.track_id));
2402 EXPECT_TRUE(report->Get(*expected_audio.transport_id));
2403 EXPECT_TRUE(report->Get(*expected_audio.codec_id));
2404}
2405
2406// When the PC has not had SetLocalDescription done, tracks all have
2407// SSRC 0, meaning "unconnected".
2408// We do not report stats on those tracks. https://bugs.webrtc.org/8673
2409TEST_F(RTCStatsCollectorTest, StatsNotReportedOnZeroSsrc) {
2410 rtc::scoped_refptr<MediaStreamTrackInterface> track =
2411 CreateFakeTrack(cricket::MEDIA_TYPE_AUDIO, "audioTrack",
2412 MediaStreamTrackInterface::kLive);
2413 rtc::scoped_refptr<MockRtpSender> sender = CreateMockSender(track, 0);
2414 EXPECT_CALL(test_->pc(), GetSenders())
2415 .WillRepeatedly(
2416 Return(std::vector<rtc::scoped_refptr<RtpSenderInterface>>(
2417 {rtc::scoped_refptr<RtpSenderInterface>(sender.get())})));
2418 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
2419 std::vector<const RTCMediaStreamTrackStats*> track_stats =
2420 report->GetStatsOfType<RTCMediaStreamTrackStats>();
2421 EXPECT_EQ(0, track_stats.size());
2422}
2423
hbosc82f2e12016-09-05 01:36:50 -07002424class RTCStatsCollectorTestWithFakeCollector : public testing::Test {
2425 public:
2426 RTCStatsCollectorTestWithFakeCollector()
2427 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()),
2428 collector_(FakeRTCStatsCollector::Create(
2429 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) {
2430 }
2431
2432 protected:
2433 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_;
2434 rtc::scoped_refptr<FakeRTCStatsCollector> collector_;
2435};
2436
2437TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) {
2438 collector_->VerifyThreadUsageAndResultsMerging();
2439}
2440
2441} // namespace
2442
hbosd565b732016-08-30 14:04:35 -07002443} // namespace webrtc