blob: 0950a374c3cd8a44cbb8bffc2991fc6dc95417c8 [file] [log] [blame]
Harald Alvestrandfb648512017-01-03 16:49:161<!doctype html>
2<!--
3This test uses data only, and thus does not require fake media devices.
4-->
5
6<html>
7<head>
8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
9 <title>RTCPeerConnection GetStats</title>
10</head>
11<body>
12 <div id="log"></div>
13 <h2>Retrieved stats info</h2>
14 <pre>
Youenn Fableta6be85c2018-11-13 22:25:1915 <input type="button" onclick="showStats()" value="Show stats"></input>
Harald Alvestrandfb648512017-01-03 16:49:1616 <div id="stats">
17 </div>
18 </pre>
19
20 <!-- These files are in place when executing on W3C. -->
21 <script src="/resources/testharness.js"></script>
22 <script src="/resources/testharnessreport.js"></script>
23 <script type="text/javascript">
24 var test = async_test('Can get stats from a basic WebRTC call.');
Youenn Fableta6be85c2018-11-13 22:25:1925 var statsToShow;
Harald Alvestrandfb648512017-01-03 16:49:1626 var gFirstConnection = null;
27 var gSecondConnection = null;
28
29 var onIceCandidateToFirst = test.step_func(function(event) {
30 // If event.candidate is null = no more candidates.
31 if (event.candidate) {
32 gSecondConnection.addIceCandidate(event.candidate);
33 }
34 });
35
36 var onIceCandidateToSecond = test.step_func(function(event) {
37 if (event.candidate) {
38 gFirstConnection.addIceCandidate(event.candidate);
39 }
40 });
41
Harald Alvestrandfb648512017-01-03 16:49:1642 var getStatsRecordByType = function(stats, type) {
43 for (let stat of stats.values()) {
44 if (stat.type == type) {
45 return stat;
46 }
47 }
48 return null;
49 }
50
Harald Alvestrandef4de3d2017-01-04 09:49:2151 var onIceConnectionStateChange = test.step_func(function(event) {
52 // Wait until connection is established.
53 // Note - not all browsers reach 'completed' state, so we're
54 // checking for 'connected' state instead.
55 if (gFirstConnection.iceConnectionState != 'connected') {
56 return;
57 }
58 gFirstConnection.getStats()
59 .then(function(report) {
Harald Alvestrandef4de3d2017-01-04 09:49:2160 let reportDictionary = {};
61 for (let stats of report.values()) {
62 reportDictionary[stats.id] = stats;
63 }
Youenn Fableta6be85c2018-11-13 22:25:1964 statsToShow = JSON.stringify(reportDictionary, null, 2);
Harald Alvestrandef4de3d2017-01-04 09:49:2165 // Check the stats properties.
Harald Alvestrand7d504a22017-10-17 22:21:5566 assert_not_equals(report, null, 'No report');
Harald Alvestrandef4de3d2017-01-04 09:49:2167 let sessionStat = getStatsRecordByType(report, 'peer-connection');
68 assert_not_equals(sessionStat, null, 'Did not find peer-connection stats');
jugglinmikeefa1c1e2018-09-24 09:49:5869 assert_own_property(sessionStat, 'dataChannelsOpened', 'no dataChannelsOpened stat');
Harald Alvestrand7d504a22017-10-17 22:21:5570 // Once every 4000 or so tests, the datachannel won't be opened when the getStats
71 // function is done, so allow both 0 and 1 datachannels.
72 assert_true(sessionStat.dataChannelsOpened == 1 || sessionStat.dataChannelsOpened == 0,
73 'dataChannelsOpened count wrong');
Harald Alvestrandef4de3d2017-01-04 09:49:2174 test.done();
75 })
76 .catch(test.step_func(function(e) {
77 assert_unreached(e.name + ': ' + e.message + ': ');
78 }));
79 });
80
Harald Alvestrandfb648512017-01-03 16:49:1681 // This function starts the test.
82 test.step(function() {
83 gFirstConnection = new RTCPeerConnection(null);
Byron Campen [:bwc]d27f50c2020-06-25 14:38:0384 test.add_cleanup(() => gFirstConnection.close());
Harald Alvestrandfb648512017-01-03 16:49:1685 gFirstConnection.onicecandidate = onIceCandidateToFirst;
Harald Alvestrandef4de3d2017-01-04 09:49:2186 gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange;
Harald Alvestrandfb648512017-01-03 16:49:1687
88 gSecondConnection = new RTCPeerConnection(null);
Byron Campen [:bwc]d27f50c2020-06-25 14:38:0389 test.add_cleanup(() => gSecondConnection.close());
Harald Alvestrandfb648512017-01-03 16:49:1690 gSecondConnection.onicecandidate = onIceCandidateToSecond;
Harald Alvestrandfb648512017-01-03 16:49:1691
92 // The createDataChannel is necessary and sufficient to make
93 // sure the ICE connection be attempted.
94 gFirstConnection.createDataChannel('channel');
Harald Alvestrandfb648512017-01-03 16:49:1695 var atStep = 'Create offer';
96
97 gFirstConnection.createOffer()
98 .then(function(offer) {
99 atStep = 'Set local description at first';
100 return gFirstConnection.setLocalDescription(offer);
101 })
102 .then(function() {
103 atStep = 'Set remote description at second';
104 return gSecondConnection.setRemoteDescription(
105 gFirstConnection.localDescription);
106 })
107 .then(function() {
108 atStep = 'Create answer';
109 return gSecondConnection.createAnswer();
110 })
111 .then(function(answer) {
112 atStep = 'Set local description at second';
113 return gSecondConnection.setLocalDescription(answer);
114 })
115 .then(function() {
116 atStep = 'Set remote description at first';
117 return gFirstConnection.setRemoteDescription(
118 gSecondConnection.localDescription);
119 })
Harald Alvestrandfb648512017-01-03 16:49:16120 .catch(test.step_func(function(e) {
121 assert_unreached('Error ' + e.name + ': ' + e.message +
122 ' happened at step ' + atStep);
123 }));
124 });
Youenn Fableta6be85c2018-11-13 22:25:19125
126 function showStats() {
127 // Show the retrieved stats info
128 var showStats = document.getElementById('stats');
129 showStats.innerHTML = statsToShow;
130 }
131
Harald Alvestrandfb648512017-01-03 16:49:16132</script>
133
134</body>
135</html>