| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <!-- |
| 3 | This 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 Fablet | a6be85c | 2018-11-13 22:25:19 | [diff] [blame] | 15 | <input type="button" onclick="showStats()" value="Show stats"></input> |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 16 | <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 Fablet | a6be85c | 2018-11-13 22:25:19 | [diff] [blame] | 25 | var statsToShow; |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 26 | 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 Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 42 | 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 Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 51 | 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 Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 60 | let reportDictionary = {}; |
| 61 | for (let stats of report.values()) { |
| 62 | reportDictionary[stats.id] = stats; |
| 63 | } |
| Youenn Fablet | a6be85c | 2018-11-13 22:25:19 | [diff] [blame] | 64 | statsToShow = JSON.stringify(reportDictionary, null, 2); |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 65 | // Check the stats properties. |
| Harald Alvestrand | 7d504a2 | 2017-10-17 22:21:55 | [diff] [blame] | 66 | assert_not_equals(report, null, 'No report'); |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 67 | let sessionStat = getStatsRecordByType(report, 'peer-connection'); |
| 68 | assert_not_equals(sessionStat, null, 'Did not find peer-connection stats'); |
| jugglinmike | efa1c1e | 2018-09-24 09:49:58 | [diff] [blame] | 69 | assert_own_property(sessionStat, 'dataChannelsOpened', 'no dataChannelsOpened stat'); |
| Harald Alvestrand | 7d504a2 | 2017-10-17 22:21:55 | [diff] [blame] | 70 | // 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 Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 74 | test.done(); |
| 75 | }) |
| 76 | .catch(test.step_func(function(e) { |
| 77 | assert_unreached(e.name + ': ' + e.message + ': '); |
| 78 | })); |
| 79 | }); |
| 80 | |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 81 | // This function starts the test. |
| 82 | test.step(function() { |
| 83 | gFirstConnection = new RTCPeerConnection(null); |
| Byron Campen [:bwc] | d27f50c | 2020-06-25 14:38:03 | [diff] [blame^] | 84 | test.add_cleanup(() => gFirstConnection.close()); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 85 | gFirstConnection.onicecandidate = onIceCandidateToFirst; |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 86 | gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange; |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 87 | |
| 88 | gSecondConnection = new RTCPeerConnection(null); |
| Byron Campen [:bwc] | d27f50c | 2020-06-25 14:38:03 | [diff] [blame^] | 89 | test.add_cleanup(() => gSecondConnection.close()); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 90 | gSecondConnection.onicecandidate = onIceCandidateToSecond; |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 91 | |
| 92 | // The createDataChannel is necessary and sufficient to make |
| 93 | // sure the ICE connection be attempted. |
| 94 | gFirstConnection.createDataChannel('channel'); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 95 | 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 Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 120 | .catch(test.step_func(function(e) { |
| 121 | assert_unreached('Error ' + e.name + ': ' + e.message + |
| 122 | ' happened at step ' + atStep); |
| 123 | })); |
| 124 | }); |
| Youenn Fablet | a6be85c | 2018-11-13 22:25:19 | [diff] [blame] | 125 | |
| 126 | function showStats() { |
| 127 | // Show the retrieved stats info |
| 128 | var showStats = document.getElementById('stats'); |
| 129 | showStats.innerHTML = statsToShow; |
| 130 | } |
| 131 | |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 132 | </script> |
| 133 | |
| 134 | </body> |
| 135 | </html> |