blob: 3da108300e4062cf8ff08bfa9d0cd97d643833e5 [file] [log] [blame]
hbosdb346a72016-11-29 01:57:01 -08001/*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <set>
12#include <vector>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/audio_codecs/builtin_audio_decoder_factory.h"
15#include "api/audio_codecs/builtin_audio_encoder_factory.h"
16#include "api/datachannelinterface.h"
17#include "api/peerconnectioninterface.h"
18#include "api/stats/rtcstats_objects.h"
19#include "api/stats/rtcstatsreport.h"
20#include "pc/test/peerconnectiontestwrapper.h"
21#include "pc/test/rtcstatsobtainer.h"
22#include "rtc_base/checks.h"
23#include "rtc_base/event_tracer.h"
24#include "rtc_base/gunit.h"
25#include "rtc_base/refcountedobject.h"
26#include "rtc_base/scoped_ref_ptr.h"
27#include "rtc_base/trace_event.h"
28#include "rtc_base/virtualsocketserver.h"
hbosdb346a72016-11-29 01:57:01 -080029
30namespace webrtc {
31
32namespace {
33
34const int64_t kGetStatsTimeoutMs = 10000;
35
ehmaldonado80c829f2017-07-18 07:35:19 -070036const unsigned char* GetCategoryEnabledHandler(const char* name) {
ehmaldonado8ab0fd82017-09-04 14:35:04 -070037 if (strcmp("webrtc_stats", name) != 0) {
38 return reinterpret_cast<const unsigned char*>("");
39 }
40 return reinterpret_cast<const unsigned char*>(name);
ehmaldonado80c829f2017-07-18 07:35:19 -070041}
42
ehmaldonado8ab0fd82017-09-04 14:35:04 -070043class RTCStatsReportTraceListener {
44 public:
45 static void SetUp() {
46 if (!traced_report_)
47 traced_report_ = new RTCStatsReportTraceListener();
48 traced_report_->last_trace_ = "";
49 SetupEventTracer(&GetCategoryEnabledHandler,
50 &RTCStatsReportTraceListener::AddTraceEventHandler);
51 }
52
53 static const std::string& last_trace() {
54 RTC_DCHECK(traced_report_);
55 return traced_report_->last_trace_;
56 }
57
58 private:
Steve Anton36b29d12017-10-30 09:57:42 -070059 static void AddTraceEventHandler(
60 char phase,
61 const unsigned char* category_enabled,
62 const char* name,
63 unsigned long long id, // NOLINT(runtime/int)
64 int num_args,
65 const char** arg_names,
66 const unsigned char* arg_types,
67 const unsigned long long* arg_values, // NOLINT(runtime/int)
68 unsigned char flags) {
ehmaldonado8ab0fd82017-09-04 14:35:04 -070069 RTC_DCHECK(traced_report_);
70 EXPECT_STREQ("webrtc_stats",
71 reinterpret_cast<const char*>(category_enabled));
72 EXPECT_STREQ("webrtc_stats", name);
73 EXPECT_EQ(1, num_args);
74 EXPECT_STREQ("report", arg_names[0]);
75 EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, arg_types[0]);
76
77 traced_report_->last_trace_ = reinterpret_cast<const char*>(arg_values[0]);
78 }
79
80 static RTCStatsReportTraceListener* traced_report_;
81 std::string last_trace_;
82};
83
84RTCStatsReportTraceListener* RTCStatsReportTraceListener::traced_report_ =
85 nullptr;
ehmaldonado80c829f2017-07-18 07:35:19 -070086
hbosdb346a72016-11-29 01:57:01 -080087class RTCStatsIntegrationTest : public testing::Test {
88 public:
89 RTCStatsIntegrationTest()
tommie7251592017-07-14 14:44:46 -070090 : network_thread_(new rtc::Thread(&virtual_socket_server_)),
91 worker_thread_(rtc::Thread::Create()) {
ehmaldonado8ab0fd82017-09-04 14:35:04 -070092 RTCStatsReportTraceListener::SetUp();
ehmaldonado80c829f2017-07-18 07:35:19 -070093
tommie7251592017-07-14 14:44:46 -070094 RTC_CHECK(network_thread_->Start());
95 RTC_CHECK(worker_thread_->Start());
hbosdb346a72016-11-29 01:57:01 -080096
97 caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -070098 "caller", network_thread_.get(), worker_thread_.get());
hbosdb346a72016-11-29 01:57:01 -080099 callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
tommie7251592017-07-14 14:44:46 -0700100 "callee", network_thread_.get(), worker_thread_.get());
hbosdb346a72016-11-29 01:57:01 -0800101 }
102
103 void StartCall() {
104 // Create PeerConnections and "connect" sigslots
105 PeerConnectionInterface::RTCConfiguration config;
106 PeerConnectionInterface::IceServer ice_server;
107 ice_server.uri = "stun:1.1.1.1:3478";
108 config.servers.push_back(ice_server);
kwiberg9e5b11e2017-04-19 03:47:57 -0700109 EXPECT_TRUE(caller_->CreatePc(nullptr, config,
110 CreateBuiltinAudioEncoderFactory(),
111 CreateBuiltinAudioDecoderFactory()));
112 EXPECT_TRUE(callee_->CreatePc(nullptr, config,
113 CreateBuiltinAudioEncoderFactory(),
114 CreateBuiltinAudioDecoderFactory()));
hbosdb346a72016-11-29 01:57:01 -0800115 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
116
117 // Get user media for audio and video
118 caller_->GetAndAddUserMedia(true, FakeConstraints(),
119 true, FakeConstraints());
120 callee_->GetAndAddUserMedia(true, FakeConstraints(),
121 true, FakeConstraints());
122
123 // Create data channels
124 DataChannelInit init;
125 caller_->CreateDataChannel("data", init);
126 callee_->CreateDataChannel("data", init);
127
128 // Negotiate and wait for call to establish
129 caller_->CreateOffer(nullptr);
130 caller_->WaitForCallEstablished();
131 callee_->WaitForCallEstablished();
132 }
133
134 rtc::scoped_refptr<const RTCStatsReport> GetStatsFromCaller() {
135 return GetStats(caller_->pc());
136 }
137
138 rtc::scoped_refptr<const RTCStatsReport> GetStatsFromCallee() {
139 return GetStats(callee_->pc());
140 }
141
142 protected:
143 static rtc::scoped_refptr<const RTCStatsReport> GetStats(
144 PeerConnectionInterface* pc) {
145 rtc::scoped_refptr<RTCStatsObtainer> stats_obtainer =
146 RTCStatsObtainer::Create();
147 pc->GetStats(stats_obtainer);
148 EXPECT_TRUE_WAIT(stats_obtainer->report(), kGetStatsTimeoutMs);
149 return stats_obtainer->report();
150 }
151
deadbeef98e186c2017-05-16 18:00:06 -0700152 // |network_thread_| uses |virtual_socket_server_| so they must be
153 // constructed/destructed in the correct order.
hbosdb346a72016-11-29 01:57:01 -0800154 rtc::VirtualSocketServer virtual_socket_server_;
tommie7251592017-07-14 14:44:46 -0700155 std::unique_ptr<rtc::Thread> network_thread_;
156 std::unique_ptr<rtc::Thread> worker_thread_;
hbosdb346a72016-11-29 01:57:01 -0800157 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
158 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
159};
160
161class RTCStatsVerifier {
162 public:
163 RTCStatsVerifier(const RTCStatsReport* report, const RTCStats* stats)
164 : report_(report), stats_(stats), all_tests_successful_(true) {
165 RTC_CHECK(report_);
166 RTC_CHECK(stats_);
167 for (const RTCStatsMemberInterface* member : stats_->Members()) {
168 untested_members_.insert(member);
169 }
170 }
171
172 void MarkMemberTested(
173 const RTCStatsMemberInterface& member, bool test_successful) {
174 untested_members_.erase(&member);
175 all_tests_successful_ &= test_successful;
176 }
177
178 void TestMemberIsDefined(const RTCStatsMemberInterface& member) {
179 EXPECT_TRUE(member.is_defined()) <<
hbos23351192017-01-02 06:52:19 -0800180 stats_->type() << "." << member.name() << "[" << stats_->id() <<
181 "] was undefined.";
hbosdb346a72016-11-29 01:57:01 -0800182 MarkMemberTested(member, member.is_defined());
183 }
184
185 void TestMemberIsUndefined(const RTCStatsMemberInterface& member) {
186 EXPECT_FALSE(member.is_defined()) <<
hbos23351192017-01-02 06:52:19 -0800187 stats_->type() << "." << member.name() << "[" << stats_->id() <<
188 "] was defined (" << member.ValueToString() << ").";
hbosdb346a72016-11-29 01:57:01 -0800189 MarkMemberTested(member, !member.is_defined());
190 }
191
hbos23351192017-01-02 06:52:19 -0800192 template<typename T>
193 void TestMemberIsPositive(const RTCStatsMemberInterface& member) {
194 EXPECT_TRUE(member.is_defined()) <<
195 stats_->type() << "." << member.name() << "[" << stats_->id() <<
196 "] was undefined.";
197 if (!member.is_defined()) {
198 MarkMemberTested(member, false);
199 return;
200 }
201 bool is_positive = *member.cast_to<RTCStatsMember<T>>() > T(0);
202 EXPECT_TRUE(is_positive) <<
203 stats_->type() << "." << member.name() << "[" << stats_->id() <<
204 "] was not positive (" << member.ValueToString() << ").";
205 MarkMemberTested(member, is_positive);
206 }
207
208 template<typename T>
209 void TestMemberIsNonNegative(const RTCStatsMemberInterface& member) {
210 EXPECT_TRUE(member.is_defined()) <<
211 stats_->type() << "." << member.name() << "[" << stats_->id() <<
212 "] was undefined.";
213 if (!member.is_defined()) {
214 MarkMemberTested(member, false);
215 return;
216 }
217 bool is_non_negative = *member.cast_to<RTCStatsMember<T>>() >= T(0);
218 EXPECT_TRUE(is_non_negative) <<
219 stats_->type() << "." << member.name() << "[" << stats_->id() <<
220 "] was not non-negative (" << member.ValueToString() << ").";
221 MarkMemberTested(member, is_non_negative);
222 }
223
hbosdb346a72016-11-29 01:57:01 -0800224 void TestMemberIsIDReference(
225 const RTCStatsMemberInterface& member,
226 const char* expected_type) {
227 TestMemberIsIDReference(member, expected_type, false);
228 }
229
230 void TestMemberIsOptionalIDReference(
231 const RTCStatsMemberInterface& member,
232 const char* expected_type) {
233 TestMemberIsIDReference(member, expected_type, true);
234 }
235
236 bool ExpectAllMembersSuccessfullyTested() {
237 if (untested_members_.empty())
238 return all_tests_successful_;
239 for (const RTCStatsMemberInterface* member : untested_members_) {
240 EXPECT_TRUE(false) <<
241 stats_->type() << "." << member->name() << "[" << stats_->id() <<
242 "] was not tested.";
243 }
244 return false;
245 }
246
247 private:
248 void TestMemberIsIDReference(
249 const RTCStatsMemberInterface& member,
250 const char* expected_type,
251 bool optional) {
252 if (optional && !member.is_defined()) {
253 MarkMemberTested(member, true);
254 return;
255 }
256 bool valid_reference = false;
257 if (member.is_defined()) {
258 if (member.type() == RTCStatsMemberInterface::kString) {
259 // A single ID.
260 const RTCStatsMember<std::string>& id =
261 member.cast_to<RTCStatsMember<std::string>>();
262 const RTCStats* referenced_stats = report_->Get(*id);
263 valid_reference =
264 referenced_stats && referenced_stats->type() == expected_type;
265 } else if (member.type() == RTCStatsMemberInterface::kSequenceString) {
266 // A vector of IDs.
267 valid_reference = true;
268 const RTCStatsMember<std::vector<std::string>>& ids =
269 member.cast_to<RTCStatsMember<std::vector<std::string>>>();
270 for (const std::string id : *ids) {
271 const RTCStats* referenced_stats = report_->Get(id);
272 if (!referenced_stats || referenced_stats->type() != expected_type) {
273 valid_reference = false;
274 break;
275 }
276 }
277 }
278 }
279 EXPECT_TRUE(valid_reference) <<
280 stats_->type() << "." << member.name() << " is not a reference to an " <<
281 "existing dictionary of type " << expected_type << " (" <<
282 member.ValueToString() << ").";
283 MarkMemberTested(member, valid_reference);
284 }
285
286 rtc::scoped_refptr<const RTCStatsReport> report_;
287 const RTCStats* stats_;
288 std::set<const RTCStatsMemberInterface*> untested_members_;
289 bool all_tests_successful_;
290};
291
292class RTCStatsReportVerifier {
293 public:
294 static std::set<const char*> StatsTypes() {
295 std::set<const char*> stats_types;
296 stats_types.insert(RTCCertificateStats::kType);
297 stats_types.insert(RTCCodecStats::kType);
298 stats_types.insert(RTCDataChannelStats::kType);
299 stats_types.insert(RTCIceCandidatePairStats::kType);
300 stats_types.insert(RTCLocalIceCandidateStats::kType);
301 stats_types.insert(RTCRemoteIceCandidateStats::kType);
302 stats_types.insert(RTCMediaStreamStats::kType);
303 stats_types.insert(RTCMediaStreamTrackStats::kType);
304 stats_types.insert(RTCPeerConnectionStats::kType);
305 stats_types.insert(RTCInboundRTPStreamStats::kType);
306 stats_types.insert(RTCOutboundRTPStreamStats::kType);
307 stats_types.insert(RTCTransportStats::kType);
308 return stats_types;
309 }
310
311 explicit RTCStatsReportVerifier(const RTCStatsReport* report)
312 : report_(report) {
313 }
314
315 void VerifyReport() {
316 std::set<const char*> missing_stats = StatsTypes();
317 bool verify_successful = true;
hbos338f78a2017-02-07 06:41:21 -0800318 std::vector<const RTCTransportStats*> transport_stats =
319 report_->GetStatsOfType<RTCTransportStats>();
320 EXPECT_EQ(transport_stats.size(), 1);
321 std::string selected_candidate_pair_id =
322 *transport_stats[0]->selected_candidate_pair_id;
hbosdb346a72016-11-29 01:57:01 -0800323 for (const RTCStats& stats : *report_) {
324 missing_stats.erase(stats.type());
325 if (stats.type() == RTCCertificateStats::kType) {
326 verify_successful &= VerifyRTCCertificateStats(
327 stats.cast_to<RTCCertificateStats>());
328 } else if (stats.type() == RTCCodecStats::kType) {
329 verify_successful &= VerifyRTCCodecStats(
330 stats.cast_to<RTCCodecStats>());
331 } else if (stats.type() == RTCDataChannelStats::kType) {
332 verify_successful &= VerifyRTCDataChannelStats(
333 stats.cast_to<RTCDataChannelStats>());
334 } else if (stats.type() == RTCIceCandidatePairStats::kType) {
335 verify_successful &= VerifyRTCIceCandidatePairStats(
hbos338f78a2017-02-07 06:41:21 -0800336 stats.cast_to<RTCIceCandidatePairStats>(),
337 stats.id() == selected_candidate_pair_id);
hbosdb346a72016-11-29 01:57:01 -0800338 } else if (stats.type() == RTCLocalIceCandidateStats::kType) {
339 verify_successful &= VerifyRTCLocalIceCandidateStats(
340 stats.cast_to<RTCLocalIceCandidateStats>());
341 } else if (stats.type() == RTCRemoteIceCandidateStats::kType) {
342 verify_successful &= VerifyRTCRemoteIceCandidateStats(
343 stats.cast_to<RTCRemoteIceCandidateStats>());
344 } else if (stats.type() == RTCMediaStreamStats::kType) {
345 verify_successful &= VerifyRTCMediaStreamStats(
346 stats.cast_to<RTCMediaStreamStats>());
347 } else if (stats.type() == RTCMediaStreamTrackStats::kType) {
348 verify_successful &= VerifyRTCMediaStreamTrackStats(
349 stats.cast_to<RTCMediaStreamTrackStats>());
350 } else if (stats.type() == RTCPeerConnectionStats::kType) {
351 verify_successful &= VerifyRTCPeerConnectionStats(
352 stats.cast_to<RTCPeerConnectionStats>());
353 } else if (stats.type() == RTCInboundRTPStreamStats::kType) {
354 verify_successful &= VerifyRTCInboundRTPStreamStats(
355 stats.cast_to<RTCInboundRTPStreamStats>());
356 } else if (stats.type() == RTCOutboundRTPStreamStats::kType) {
357 verify_successful &= VerifyRTCOutboundRTPStreamStats(
358 stats.cast_to<RTCOutboundRTPStreamStats>());
359 } else if (stats.type() == RTCTransportStats::kType) {
360 verify_successful &= VerifyRTCTransportStats(
361 stats.cast_to<RTCTransportStats>());
362 } else {
363 EXPECT_TRUE(false) << "Unrecognized stats type: " << stats.type();
364 verify_successful = false;
365 }
366 }
367 if (!missing_stats.empty()) {
368 verify_successful = false;
369 for (const char* missing : missing_stats) {
370 EXPECT_TRUE(false) << "Missing expected stats type: " << missing;
371 }
372 }
373 EXPECT_TRUE(verify_successful) <<
374 "One or more problems with the stats. This is the report:\n" <<
ehmaldonado35a872c2017-07-28 07:29:12 -0700375 report_->ToJson();
hbosdb346a72016-11-29 01:57:01 -0800376 }
377
378 bool VerifyRTCCertificateStats(
379 const RTCCertificateStats& certificate) {
380 RTCStatsVerifier verifier(report_, &certificate);
381 verifier.TestMemberIsDefined(certificate.fingerprint);
382 verifier.TestMemberIsDefined(certificate.fingerprint_algorithm);
383 verifier.TestMemberIsDefined(certificate.base64_certificate);
384 verifier.TestMemberIsOptionalIDReference(
385 certificate.issuer_certificate_id, RTCCertificateStats::kType);
386 return verifier.ExpectAllMembersSuccessfullyTested();
387 }
388
389 bool VerifyRTCCodecStats(
390 const RTCCodecStats& codec) {
391 RTCStatsVerifier verifier(report_, &codec);
392 verifier.TestMemberIsDefined(codec.payload_type);
hbos13f54b22017-02-28 06:56:04 -0800393 verifier.TestMemberIsDefined(codec.mime_type);
hbos23351192017-01-02 06:52:19 -0800394 verifier.TestMemberIsPositive<uint32_t>(codec.clock_rate);
hbosdb346a72016-11-29 01:57:01 -0800395 verifier.TestMemberIsUndefined(codec.channels);
hbos13f54b22017-02-28 06:56:04 -0800396 verifier.TestMemberIsUndefined(codec.sdp_fmtp_line);
hbosdb346a72016-11-29 01:57:01 -0800397 verifier.TestMemberIsUndefined(codec.implementation);
398 return verifier.ExpectAllMembersSuccessfullyTested();
399 }
400
401 bool VerifyRTCDataChannelStats(
402 const RTCDataChannelStats& data_channel) {
403 RTCStatsVerifier verifier(report_, &data_channel);
404 verifier.TestMemberIsDefined(data_channel.label);
405 verifier.TestMemberIsDefined(data_channel.protocol);
406 verifier.TestMemberIsDefined(data_channel.datachannelid);
407 verifier.TestMemberIsDefined(data_channel.state);
hbos23351192017-01-02 06:52:19 -0800408 verifier.TestMemberIsNonNegative<uint32_t>(data_channel.messages_sent);
409 verifier.TestMemberIsNonNegative<uint64_t>(data_channel.bytes_sent);
410 verifier.TestMemberIsNonNegative<uint32_t>(data_channel.messages_received);
411 verifier.TestMemberIsNonNegative<uint64_t>(data_channel.bytes_received);
hbosdb346a72016-11-29 01:57:01 -0800412 return verifier.ExpectAllMembersSuccessfullyTested();
413 }
414
415 bool VerifyRTCIceCandidatePairStats(
hbos338f78a2017-02-07 06:41:21 -0800416 const RTCIceCandidatePairStats& candidate_pair, bool is_selected_pair) {
hbosdb346a72016-11-29 01:57:01 -0800417 RTCStatsVerifier verifier(report_, &candidate_pair);
hbos0583b282016-11-30 01:50:14 -0800418 verifier.TestMemberIsIDReference(
419 candidate_pair.transport_id, RTCTransportStats::kType);
hbosdb346a72016-11-29 01:57:01 -0800420 verifier.TestMemberIsIDReference(
421 candidate_pair.local_candidate_id, RTCLocalIceCandidateStats::kType);
422 verifier.TestMemberIsIDReference(
423 candidate_pair.remote_candidate_id, RTCRemoteIceCandidateStats::kType);
hbos06495bc2017-01-02 08:08:18 -0800424 verifier.TestMemberIsDefined(candidate_pair.state);
425 verifier.TestMemberIsNonNegative<uint64_t>(candidate_pair.priority);
hbos92eaec62017-02-27 01:38:08 -0800426 verifier.TestMemberIsDefined(candidate_pair.nominated);
hbosdb346a72016-11-29 01:57:01 -0800427 verifier.TestMemberIsDefined(candidate_pair.writable);
428 verifier.TestMemberIsUndefined(candidate_pair.readable);
hbos23351192017-01-02 06:52:19 -0800429 verifier.TestMemberIsNonNegative<uint64_t>(candidate_pair.bytes_sent);
430 verifier.TestMemberIsNonNegative<uint64_t>(candidate_pair.bytes_received);
hbosbf8d3e52017-02-28 06:34:47 -0800431 verifier.TestMemberIsNonNegative<double>(
432 candidate_pair.total_round_trip_time);
hbos23351192017-01-02 06:52:19 -0800433 verifier.TestMemberIsNonNegative<double>(
434 candidate_pair.current_round_trip_time);
hbos338f78a2017-02-07 06:41:21 -0800435 if (is_selected_pair) {
436 verifier.TestMemberIsNonNegative<double>(
437 candidate_pair.available_outgoing_bitrate);
deadbeef86c40a12017-06-28 09:37:23 -0700438 // A pair should be nominated in order to be selected.
439 EXPECT_TRUE(*candidate_pair.nominated);
hbos338f78a2017-02-07 06:41:21 -0800440 } else {
441 verifier.TestMemberIsUndefined(candidate_pair.available_outgoing_bitrate);
442 }
hbosdb346a72016-11-29 01:57:01 -0800443 verifier.TestMemberIsUndefined(candidate_pair.available_incoming_bitrate);
hbos23351192017-01-02 06:52:19 -0800444 verifier.TestMemberIsNonNegative<uint64_t>(
445 candidate_pair.requests_received);
446 verifier.TestMemberIsNonNegative<uint64_t>(candidate_pair.requests_sent);
447 verifier.TestMemberIsNonNegative<uint64_t>(
448 candidate_pair.responses_received);
449 verifier.TestMemberIsNonNegative<uint64_t>(candidate_pair.responses_sent);
hbosdb346a72016-11-29 01:57:01 -0800450 verifier.TestMemberIsUndefined(candidate_pair.retransmissions_received);
451 verifier.TestMemberIsUndefined(candidate_pair.retransmissions_sent);
452 verifier.TestMemberIsUndefined(candidate_pair.consent_requests_received);
hbos23351192017-01-02 06:52:19 -0800453 verifier.TestMemberIsNonNegative<uint64_t>(
454 candidate_pair.consent_requests_sent);
hbosdb346a72016-11-29 01:57:01 -0800455 verifier.TestMemberIsUndefined(candidate_pair.consent_responses_received);
456 verifier.TestMemberIsUndefined(candidate_pair.consent_responses_sent);
457 return verifier.ExpectAllMembersSuccessfullyTested();
458 }
459
460 bool VerifyRTCIceCandidateStats(
461 const RTCIceCandidateStats& candidate) {
462 RTCStatsVerifier verifier(report_, &candidate);
hbosb4e426e2017-01-02 09:59:31 -0800463 verifier.TestMemberIsIDReference(
464 candidate.transport_id, RTCTransportStats::kType);
hbosc3a2b7f2017-01-02 04:46:15 -0800465 verifier.TestMemberIsDefined(candidate.is_remote);
hbosdb346a72016-11-29 01:57:01 -0800466 verifier.TestMemberIsDefined(candidate.ip);
hbos23351192017-01-02 06:52:19 -0800467 verifier.TestMemberIsNonNegative<int32_t>(candidate.port);
hbosdb346a72016-11-29 01:57:01 -0800468 verifier.TestMemberIsDefined(candidate.protocol);
469 verifier.TestMemberIsDefined(candidate.candidate_type);
hbos23351192017-01-02 06:52:19 -0800470 verifier.TestMemberIsNonNegative<int32_t>(candidate.priority);
hbosdb346a72016-11-29 01:57:01 -0800471 verifier.TestMemberIsUndefined(candidate.url);
hbosd17a5a72017-01-02 08:09:59 -0800472 verifier.TestMemberIsDefined(candidate.deleted);
hbosdb346a72016-11-29 01:57:01 -0800473 return verifier.ExpectAllMembersSuccessfullyTested();
474 }
475
476 bool VerifyRTCLocalIceCandidateStats(
477 const RTCLocalIceCandidateStats& local_candidate) {
478 return VerifyRTCIceCandidateStats(local_candidate);
479 }
480
481 bool VerifyRTCRemoteIceCandidateStats(
482 const RTCRemoteIceCandidateStats& remote_candidate) {
483 return VerifyRTCIceCandidateStats(remote_candidate);
484 }
485
486 bool VerifyRTCMediaStreamStats(
487 const RTCMediaStreamStats& media_stream) {
488 RTCStatsVerifier verifier(report_, &media_stream);
489 verifier.TestMemberIsDefined(media_stream.stream_identifier);
490 verifier.TestMemberIsIDReference(
491 media_stream.track_ids, RTCMediaStreamTrackStats::kType);
492 return verifier.ExpectAllMembersSuccessfullyTested();
493 }
494
495 bool VerifyRTCMediaStreamTrackStats(
496 const RTCMediaStreamTrackStats& media_stream_track) {
497 RTCStatsVerifier verifier(report_, &media_stream_track);
498 verifier.TestMemberIsDefined(media_stream_track.track_identifier);
499 verifier.TestMemberIsDefined(media_stream_track.remote_source);
500 verifier.TestMemberIsDefined(media_stream_track.ended);
501 verifier.TestMemberIsDefined(media_stream_track.detached);
hbos160e4a72017-01-17 02:53:23 -0800502 verifier.TestMemberIsDefined(media_stream_track.kind);
hbosdb346a72016-11-29 01:57:01 -0800503 // Video or audio media stream track?
hbos160e4a72017-01-17 02:53:23 -0800504 if (*media_stream_track.kind == RTCMediaStreamTrackKind::kVideo) {
hbosdb346a72016-11-29 01:57:01 -0800505 // Video-only members
hbos9e302742017-01-20 02:47:10 -0800506 verifier.TestMemberIsNonNegative<uint32_t>(
507 media_stream_track.frame_width);
508 verifier.TestMemberIsNonNegative<uint32_t>(
509 media_stream_track.frame_height);
hbosdb346a72016-11-29 01:57:01 -0800510 verifier.TestMemberIsUndefined(media_stream_track.frames_per_second);
hbos42f6d2f2017-01-20 03:56:50 -0800511 if (*media_stream_track.remote_source) {
hbosfefe0762017-01-20 06:14:25 -0800512 verifier.TestMemberIsUndefined(media_stream_track.frames_sent);
hbos42f6d2f2017-01-20 03:56:50 -0800513 verifier.TestMemberIsNonNegative<uint32_t>(
514 media_stream_track.frames_received);
hbosf64941f2017-01-20 07:39:09 -0800515 verifier.TestMemberIsNonNegative<uint32_t>(
516 media_stream_track.frames_decoded);
hbos50cfe1f2017-01-23 07:21:55 -0800517 verifier.TestMemberIsNonNegative<uint32_t>(
518 media_stream_track.frames_dropped);
hbos42f6d2f2017-01-20 03:56:50 -0800519 } else {
hbosfefe0762017-01-20 06:14:25 -0800520 verifier.TestMemberIsNonNegative<uint32_t>(
521 media_stream_track.frames_sent);
hbos42f6d2f2017-01-20 03:56:50 -0800522 verifier.TestMemberIsUndefined(media_stream_track.frames_received);
hbosf64941f2017-01-20 07:39:09 -0800523 verifier.TestMemberIsUndefined(media_stream_track.frames_decoded);
hbos50cfe1f2017-01-23 07:21:55 -0800524 verifier.TestMemberIsUndefined(media_stream_track.frames_dropped);
hbos42f6d2f2017-01-20 03:56:50 -0800525 }
hbosdb346a72016-11-29 01:57:01 -0800526 verifier.TestMemberIsUndefined(media_stream_track.frames_corrupted);
527 verifier.TestMemberIsUndefined(media_stream_track.partial_frames_lost);
528 verifier.TestMemberIsUndefined(media_stream_track.full_frames_lost);
529 // Audio-only members should be undefined
530 verifier.TestMemberIsUndefined(media_stream_track.audio_level);
531 verifier.TestMemberIsUndefined(media_stream_track.echo_return_loss);
532 verifier.TestMemberIsUndefined(
533 media_stream_track.echo_return_loss_enhancement);
zsteine76bd3a2017-07-14 12:17:49 -0700534 verifier.TestMemberIsUndefined(media_stream_track.total_audio_energy);
535 verifier.TestMemberIsUndefined(media_stream_track.total_samples_duration);
hbosdb346a72016-11-29 01:57:01 -0800536 } else {
hbos42f6d2f2017-01-20 03:56:50 -0800537 RTC_DCHECK_EQ(*media_stream_track.kind,
538 RTCMediaStreamTrackKind::kAudio);
hbosdb346a72016-11-29 01:57:01 -0800539 // Video-only members should be undefined
540 verifier.TestMemberIsUndefined(media_stream_track.frame_width);
541 verifier.TestMemberIsUndefined(media_stream_track.frame_height);
542 verifier.TestMemberIsUndefined(media_stream_track.frames_per_second);
543 verifier.TestMemberIsUndefined(media_stream_track.frames_sent);
544 verifier.TestMemberIsUndefined(media_stream_track.frames_received);
545 verifier.TestMemberIsUndefined(media_stream_track.frames_decoded);
546 verifier.TestMemberIsUndefined(media_stream_track.frames_dropped);
547 verifier.TestMemberIsUndefined(media_stream_track.frames_corrupted);
548 verifier.TestMemberIsUndefined(media_stream_track.partial_frames_lost);
549 verifier.TestMemberIsUndefined(media_stream_track.full_frames_lost);
550 // Audio-only members
hbos9e302742017-01-20 02:47:10 -0800551 verifier.TestMemberIsNonNegative<double>(media_stream_track.audio_level);
zsteine76bd3a2017-07-14 12:17:49 -0700552 verifier.TestMemberIsNonNegative<double>(
553 media_stream_track.total_audio_energy);
554 verifier.TestMemberIsNonNegative<double>(
555 media_stream_track.total_samples_duration);
hbos2d4d6532017-01-20 04:16:41 -0800556 // TODO(hbos): |echo_return_loss| and |echo_return_loss_enhancement| are
557 // flaky on msan bot (sometimes defined, sometimes undefined). Should the
558 // test run until available or is there a way to have it always be
559 // defined? crbug.com/627816
560 verifier.MarkMemberTested(media_stream_track.echo_return_loss, true);
561 verifier.MarkMemberTested(
562 media_stream_track.echo_return_loss_enhancement, true);
hbosdb346a72016-11-29 01:57:01 -0800563 }
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200564 // totalSamplesReceived, concealedSamples and concealmentEvents are only
565 // present on inbound audio tracks.
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200566 // jitterBufferDelay is currently only implemented for audio.
Steve Anton2dbc69f2017-08-24 17:15:13 -0700567 if (*media_stream_track.kind == RTCMediaStreamTrackKind::kAudio &&
568 *media_stream_track.remote_source) {
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200569 verifier.TestMemberIsNonNegative<double>(
570 media_stream_track.jitter_buffer_delay);
Steve Anton2dbc69f2017-08-24 17:15:13 -0700571 verifier.TestMemberIsNonNegative<uint64_t>(
572 media_stream_track.total_samples_received);
573 verifier.TestMemberIsNonNegative<uint64_t>(
574 media_stream_track.concealed_samples);
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200575 verifier.TestMemberIsNonNegative<uint64_t>(
576 media_stream_track.concealment_events);
Steve Anton2dbc69f2017-08-24 17:15:13 -0700577 } else {
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200578 verifier.TestMemberIsUndefined(media_stream_track.jitter_buffer_delay);
Steve Anton2dbc69f2017-08-24 17:15:13 -0700579 verifier.TestMemberIsUndefined(media_stream_track.total_samples_received);
580 verifier.TestMemberIsUndefined(media_stream_track.concealed_samples);
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200581 verifier.TestMemberIsUndefined(media_stream_track.concealment_events);
Steve Anton2dbc69f2017-08-24 17:15:13 -0700582 }
hbosdb346a72016-11-29 01:57:01 -0800583 return verifier.ExpectAllMembersSuccessfullyTested();
584 }
585
586 bool VerifyRTCPeerConnectionStats(
587 const RTCPeerConnectionStats& peer_connection) {
588 RTCStatsVerifier verifier(report_, &peer_connection);
hbos23351192017-01-02 06:52:19 -0800589 verifier.TestMemberIsNonNegative<uint32_t>(
590 peer_connection.data_channels_opened);
591 verifier.TestMemberIsNonNegative<uint32_t>(
592 peer_connection.data_channels_closed);
hbosdb346a72016-11-29 01:57:01 -0800593 return verifier.ExpectAllMembersSuccessfullyTested();
594 }
595
596 void VerifyRTCRTPStreamStats(
597 const RTCRTPStreamStats& stream, RTCStatsVerifier* verifier) {
598 verifier->TestMemberIsDefined(stream.ssrc);
599 verifier->TestMemberIsUndefined(stream.associate_stats_id);
600 verifier->TestMemberIsDefined(stream.is_remote);
601 verifier->TestMemberIsDefined(stream.media_type);
hbos84abeb12017-01-16 06:16:44 -0800602 verifier->TestMemberIsIDReference(
hbosb0ae9202017-01-27 06:35:16 -0800603 stream.track_id, RTCMediaStreamTrackStats::kType);
hbosdb346a72016-11-29 01:57:01 -0800604 verifier->TestMemberIsIDReference(
605 stream.transport_id, RTCTransportStats::kType);
hbos7bf53692016-12-15 03:33:35 -0800606 verifier->TestMemberIsIDReference(stream.codec_id, RTCCodecStats::kType);
hbosdb346a72016-11-29 01:57:01 -0800607 if (stream.media_type.is_defined() && *stream.media_type == "video") {
hbos23351192017-01-02 06:52:19 -0800608 verifier->TestMemberIsNonNegative<uint32_t>(stream.fir_count);
609 verifier->TestMemberIsNonNegative<uint32_t>(stream.pli_count);
610 verifier->TestMemberIsNonNegative<uint32_t>(stream.nack_count);
hbosdb346a72016-11-29 01:57:01 -0800611 } else {
612 verifier->TestMemberIsUndefined(stream.fir_count);
613 verifier->TestMemberIsUndefined(stream.pli_count);
614 verifier->TestMemberIsUndefined(stream.nack_count);
615 }
616 verifier->TestMemberIsUndefined(stream.sli_count);
617 }
618
619 bool VerifyRTCInboundRTPStreamStats(
620 const RTCInboundRTPStreamStats& inbound_stream) {
621 RTCStatsVerifier verifier(report_, &inbound_stream);
622 VerifyRTCRTPStreamStats(inbound_stream, &verifier);
sakal5fec1282017-02-20 06:43:58 -0800623 if (inbound_stream.media_type.is_defined() &&
624 *inbound_stream.media_type == "video") {
625 verifier.TestMemberIsNonNegative<uint64_t>(inbound_stream.qp_sum);
626 } else {
hbosa51d4f32017-02-16 05:34:48 -0800627 verifier.TestMemberIsUndefined(inbound_stream.qp_sum);
sakal5fec1282017-02-20 06:43:58 -0800628 }
hbos23351192017-01-02 06:52:19 -0800629 verifier.TestMemberIsNonNegative<uint32_t>(inbound_stream.packets_received);
630 verifier.TestMemberIsNonNegative<uint64_t>(inbound_stream.bytes_received);
631 verifier.TestMemberIsNonNegative<uint32_t>(inbound_stream.packets_lost);
hbosdb346a72016-11-29 01:57:01 -0800632 if (inbound_stream.media_type.is_defined() &&
633 *inbound_stream.media_type == "video") {
634 verifier.TestMemberIsUndefined(inbound_stream.jitter);
635 } else {
hbos23351192017-01-02 06:52:19 -0800636 verifier.TestMemberIsNonNegative<double>(inbound_stream.jitter);
hbosdb346a72016-11-29 01:57:01 -0800637 }
hbos23351192017-01-02 06:52:19 -0800638 verifier.TestMemberIsNonNegative<double>(inbound_stream.fraction_lost);
hbosa7a9be12017-03-01 01:02:45 -0800639 verifier.TestMemberIsUndefined(inbound_stream.round_trip_time);
hbosdb346a72016-11-29 01:57:01 -0800640 verifier.TestMemberIsUndefined(inbound_stream.packets_discarded);
641 verifier.TestMemberIsUndefined(inbound_stream.packets_repaired);
642 verifier.TestMemberIsUndefined(inbound_stream.burst_packets_lost);
643 verifier.TestMemberIsUndefined(inbound_stream.burst_packets_discarded);
644 verifier.TestMemberIsUndefined(inbound_stream.burst_loss_count);
645 verifier.TestMemberIsUndefined(inbound_stream.burst_discard_count);
646 verifier.TestMemberIsUndefined(inbound_stream.burst_loss_rate);
647 verifier.TestMemberIsUndefined(inbound_stream.burst_discard_rate);
648 verifier.TestMemberIsUndefined(inbound_stream.gap_loss_rate);
649 verifier.TestMemberIsUndefined(inbound_stream.gap_discard_rate);
hbos6769c492017-01-02 08:35:13 -0800650 if (inbound_stream.media_type.is_defined() &&
651 *inbound_stream.media_type == "video") {
652 verifier.TestMemberIsDefined(inbound_stream.frames_decoded);
653 } else {
654 verifier.TestMemberIsUndefined(inbound_stream.frames_decoded);
655 }
hbosdb346a72016-11-29 01:57:01 -0800656 return verifier.ExpectAllMembersSuccessfullyTested();
657 }
658
659 bool VerifyRTCOutboundRTPStreamStats(
660 const RTCOutboundRTPStreamStats& outbound_stream) {
661 RTCStatsVerifier verifier(report_, &outbound_stream);
662 VerifyRTCRTPStreamStats(outbound_stream, &verifier);
hbos6769c492017-01-02 08:35:13 -0800663 if (outbound_stream.media_type.is_defined() &&
664 *outbound_stream.media_type == "video") {
665 verifier.TestMemberIsNonNegative<uint64_t>(outbound_stream.qp_sum);
666 } else {
667 verifier.TestMemberIsUndefined(outbound_stream.qp_sum);
668 }
hbos23351192017-01-02 06:52:19 -0800669 verifier.TestMemberIsNonNegative<uint32_t>(outbound_stream.packets_sent);
670 verifier.TestMemberIsNonNegative<uint64_t>(outbound_stream.bytes_sent);
hbosdb346a72016-11-29 01:57:01 -0800671 verifier.TestMemberIsUndefined(outbound_stream.target_bitrate);
hbos6769c492017-01-02 08:35:13 -0800672 if (outbound_stream.media_type.is_defined() &&
673 *outbound_stream.media_type == "video") {
674 verifier.TestMemberIsDefined(outbound_stream.frames_encoded);
675 } else {
676 verifier.TestMemberIsUndefined(outbound_stream.frames_encoded);
677 }
hbosdb346a72016-11-29 01:57:01 -0800678 return verifier.ExpectAllMembersSuccessfullyTested();
679 }
680
681 bool VerifyRTCTransportStats(
682 const RTCTransportStats& transport) {
683 RTCStatsVerifier verifier(report_, &transport);
hbos23351192017-01-02 06:52:19 -0800684 verifier.TestMemberIsNonNegative<uint64_t>(transport.bytes_sent);
685 verifier.TestMemberIsNonNegative<uint64_t>(transport.bytes_received);
hbosdb346a72016-11-29 01:57:01 -0800686 verifier.TestMemberIsOptionalIDReference(
687 transport.rtcp_transport_stats_id, RTCTransportStats::kType);
hbos7064d592017-01-16 07:38:02 -0800688 verifier.TestMemberIsDefined(transport.dtls_state);
hbos7bf53692016-12-15 03:33:35 -0800689 verifier.TestMemberIsIDReference(
690 transport.selected_candidate_pair_id, RTCIceCandidatePairStats::kType);
691 verifier.TestMemberIsIDReference(
692 transport.local_certificate_id, RTCCertificateStats::kType);
693 verifier.TestMemberIsIDReference(
694 transport.remote_certificate_id, RTCCertificateStats::kType);
hbosdb346a72016-11-29 01:57:01 -0800695 return verifier.ExpectAllMembersSuccessfullyTested();
696 }
697
698 private:
699 rtc::scoped_refptr<const RTCStatsReport> report_;
700};
701
deadbeef40610e22016-12-22 10:53:38 -0800702#ifdef HAVE_SCTP
hbosdb346a72016-11-29 01:57:01 -0800703TEST_F(RTCStatsIntegrationTest, GetStatsFromCaller) {
704 StartCall();
705
706 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsFromCaller();
707 RTCStatsReportVerifier(report.get()).VerifyReport();
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700708 EXPECT_EQ(report->ToJson(), RTCStatsReportTraceListener::last_trace());
hbosdb346a72016-11-29 01:57:01 -0800709}
710
711TEST_F(RTCStatsIntegrationTest, GetStatsFromCallee) {
712 StartCall();
713
714 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsFromCallee();
715 RTCStatsReportVerifier(report.get()).VerifyReport();
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700716 EXPECT_EQ(report->ToJson(), RTCStatsReportTraceListener::last_trace());
hbosdb346a72016-11-29 01:57:01 -0800717}
718
hbosb78306a2016-12-19 05:06:57 -0800719TEST_F(RTCStatsIntegrationTest, GetsStatsWhileDestroyingPeerConnections) {
720 StartCall();
721
722 rtc::scoped_refptr<RTCStatsObtainer> stats_obtainer =
723 RTCStatsObtainer::Create();
724 caller_->pc()->GetStats(stats_obtainer);
725 // This will destroy the peer connection.
726 caller_ = nullptr;
727 // Any pending stats requests should have completed in the act of destroying
728 // the peer connection.
729 EXPECT_TRUE(stats_obtainer->report());
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700730 EXPECT_EQ(stats_obtainer->report()->ToJson(),
731 RTCStatsReportTraceListener::last_trace());
hbosb78306a2016-12-19 05:06:57 -0800732}
deadbeef40610e22016-12-22 10:53:38 -0800733#endif // HAVE_SCTP
hbosb78306a2016-12-19 05:06:57 -0800734
hbosdb346a72016-11-29 01:57:01 -0800735} // namespace
736
737} // namespace webrtc