| 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) { |
| Blink WPT Bot | 92b3b83 | 2021-02-24 12:27:27 | [diff] [blame] | 30 | gSecondConnection.addIceCandidate(event.candidate); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 31 | }); |
| 32 | |
| 33 | var onIceCandidateToSecond = test.step_func(function(event) { |
| Blink WPT Bot | 92b3b83 | 2021-02-24 12:27:27 | [diff] [blame] | 34 | gFirstConnection.addIceCandidate(event.candidate); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 35 | }); |
| 36 | |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 37 | var getStatsRecordByType = function(stats, type) { |
| 38 | for (let stat of stats.values()) { |
| 39 | if (stat.type == type) { |
| 40 | return stat; |
| 41 | } |
| 42 | } |
| 43 | return null; |
| 44 | } |
| 45 | |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 46 | var onIceConnectionStateChange = test.step_func(function(event) { |
| 47 | // Wait until connection is established. |
| 48 | // Note - not all browsers reach 'completed' state, so we're |
| 49 | // checking for 'connected' state instead. |
| 50 | if (gFirstConnection.iceConnectionState != 'connected') { |
| 51 | return; |
| 52 | } |
| 53 | gFirstConnection.getStats() |
| 54 | .then(function(report) { |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 55 | let reportDictionary = {}; |
| 56 | for (let stats of report.values()) { |
| 57 | reportDictionary[stats.id] = stats; |
| 58 | } |
| Youenn Fablet | a6be85c | 2018-11-13 22:25:19 | [diff] [blame] | 59 | statsToShow = JSON.stringify(reportDictionary, null, 2); |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 60 | // Check the stats properties. |
| Harald Alvestrand | 7d504a2 | 2017-10-17 22:21:55 | [diff] [blame] | 61 | assert_not_equals(report, null, 'No report'); |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 62 | let sessionStat = getStatsRecordByType(report, 'peer-connection'); |
| 63 | assert_not_equals(sessionStat, null, 'Did not find peer-connection stats'); |
| jugglinmike | efa1c1e | 2018-09-24 09:49:58 | [diff] [blame] | 64 | assert_own_property(sessionStat, 'dataChannelsOpened', 'no dataChannelsOpened stat'); |
| Harald Alvestrand | 7d504a2 | 2017-10-17 22:21:55 | [diff] [blame] | 65 | // Once every 4000 or so tests, the datachannel won't be opened when the getStats |
| 66 | // function is done, so allow both 0 and 1 datachannels. |
| 67 | assert_true(sessionStat.dataChannelsOpened == 1 || sessionStat.dataChannelsOpened == 0, |
| 68 | 'dataChannelsOpened count wrong'); |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 69 | test.done(); |
| 70 | }) |
| 71 | .catch(test.step_func(function(e) { |
| 72 | assert_unreached(e.name + ': ' + e.message + ': '); |
| 73 | })); |
| 74 | }); |
| 75 | |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 76 | // This function starts the test. |
| 77 | test.step(function() { |
| 78 | gFirstConnection = new RTCPeerConnection(null); |
| Byron Campen [:bwc] | d27f50c | 2020-06-25 14:38:03 | [diff] [blame] | 79 | test.add_cleanup(() => gFirstConnection.close()); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 80 | gFirstConnection.onicecandidate = onIceCandidateToFirst; |
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 81 | gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange; |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 82 | |
| 83 | gSecondConnection = new RTCPeerConnection(null); |
| Byron Campen [:bwc] | d27f50c | 2020-06-25 14:38:03 | [diff] [blame] | 84 | test.add_cleanup(() => gSecondConnection.close()); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 85 | gSecondConnection.onicecandidate = onIceCandidateToSecond; |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 86 | |
| 87 | // The createDataChannel is necessary and sufficient to make |
| 88 | // sure the ICE connection be attempted. |
| 89 | gFirstConnection.createDataChannel('channel'); |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 90 | var atStep = 'Create offer'; |
| 91 | |
| 92 | gFirstConnection.createOffer() |
| 93 | .then(function(offer) { |
| 94 | atStep = 'Set local description at first'; |
| 95 | return gFirstConnection.setLocalDescription(offer); |
| 96 | }) |
| 97 | .then(function() { |
| 98 | atStep = 'Set remote description at second'; |
| 99 | return gSecondConnection.setRemoteDescription( |
| 100 | gFirstConnection.localDescription); |
| 101 | }) |
| 102 | .then(function() { |
| 103 | atStep = 'Create answer'; |
| 104 | return gSecondConnection.createAnswer(); |
| 105 | }) |
| 106 | .then(function(answer) { |
| 107 | atStep = 'Set local description at second'; |
| 108 | return gSecondConnection.setLocalDescription(answer); |
| 109 | }) |
| 110 | .then(function() { |
| 111 | atStep = 'Set remote description at first'; |
| 112 | return gFirstConnection.setRemoteDescription( |
| 113 | gSecondConnection.localDescription); |
| 114 | }) |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 115 | .catch(test.step_func(function(e) { |
| 116 | assert_unreached('Error ' + e.name + ': ' + e.message + |
| 117 | ' happened at step ' + atStep); |
| 118 | })); |
| 119 | }); |
| Youenn Fablet | a6be85c | 2018-11-13 22:25:19 | [diff] [blame] | 120 | |
| 121 | function showStats() { |
| 122 | // Show the retrieved stats info |
| 123 | var showStats = document.getElementById('stats'); |
| 124 | showStats.innerHTML = statsToShow; |
| 125 | } |
| 126 | |
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 127 | </script> |
| 128 | |
| 129 | </body> |
| 130 | </html> |