| 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> | 
|  | 15 | <div id="stats"> | 
|  | 16 | </div> | 
|  | 17 | </pre> | 
|  | 18 |  | 
|  | 19 | <!-- These files are in place when executing on W3C. --> | 
|  | 20 | <script src="/resources/testharness.js"></script> | 
|  | 21 | <script src="/resources/testharnessreport.js"></script> | 
|  | 22 | <script type="text/javascript"> | 
|  | 23 | var test = async_test('Can get stats from a basic WebRTC call.'); | 
|  | 24 |  | 
|  | 25 | var gFirstConnection = null; | 
|  | 26 | var gSecondConnection = null; | 
|  | 27 |  | 
|  | 28 | var onIceCandidateToFirst = test.step_func(function(event) { | 
|  | 29 | // If event.candidate is null = no more candidates. | 
|  | 30 | if (event.candidate) { | 
|  | 31 | gSecondConnection.addIceCandidate(event.candidate); | 
|  | 32 | } | 
|  | 33 | }); | 
|  | 34 |  | 
|  | 35 | var onIceCandidateToSecond = test.step_func(function(event) { | 
|  | 36 | if (event.candidate) { | 
|  | 37 | gFirstConnection.addIceCandidate(event.candidate); | 
|  | 38 | } | 
|  | 39 | }); | 
|  | 40 |  | 
|  | 41 | var onRemoteStream = test.step_func(function(event) { | 
|  | 42 | assert_unreached('WebRTC received a stream when there was none'); | 
|  | 43 | }); | 
|  | 44 |  | 
|  | 45 | var getStatsRecordByType = function(stats, type) { | 
|  | 46 | for (let stat of stats.values()) { | 
|  | 47 | if (stat.type == type) { | 
|  | 48 | return stat; | 
|  | 49 | } | 
|  | 50 | } | 
|  | 51 | return null; | 
|  | 52 | } | 
|  | 53 |  | 
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 54 | var onIceConnectionStateChange = test.step_func(function(event) { | 
|  | 55 | // Wait until connection is established. | 
|  | 56 | // Note - not all browsers reach 'completed' state, so we're | 
|  | 57 | // checking for 'connected' state instead. | 
|  | 58 | if (gFirstConnection.iceConnectionState != 'connected') { | 
|  | 59 | return; | 
|  | 60 | } | 
|  | 61 | gFirstConnection.getStats() | 
|  | 62 | .then(function(report) { | 
|  | 63 | // Show the retrieved stats info | 
|  | 64 | var showStats = document.getElementById('stats'); | 
|  | 65 | let reportDictionary = {}; | 
|  | 66 | for (let stats of report.values()) { | 
|  | 67 | reportDictionary[stats.id] = stats; | 
|  | 68 | } | 
|  | 69 | showStats.innerHTML = JSON.stringify(reportDictionary, null, 2); | 
|  | 70 | // Check the stats properties. | 
|  | 71 | assert_not_equals(report, null); | 
|  | 72 | let sessionStat = getStatsRecordByType(report, 'peer-connection'); | 
|  | 73 | assert_not_equals(sessionStat, null, 'Did not find peer-connection stats'); | 
|  | 74 | assert_exists(sessionStat, 'dataChannelsOpened'); | 
|  | 75 | assert_equals(sessionStat.dataChannelsOpened, 1); | 
|  | 76 | test.done(); | 
|  | 77 | }) | 
|  | 78 | .catch(test.step_func(function(e) { | 
|  | 79 | assert_unreached(e.name + ': ' + e.message + ': '); | 
|  | 80 | })); | 
|  | 81 | }); | 
|  | 82 |  | 
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 83 | // This function starts the test. | 
|  | 84 | test.step(function() { | 
|  | 85 | gFirstConnection = new RTCPeerConnection(null); | 
|  | 86 | gFirstConnection.onicecandidate = onIceCandidateToFirst; | 
| Harald Alvestrand | ef4de3d | 2017-01-04 09:49:21 | [diff] [blame] | 87 | gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange; | 
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 88 |  | 
|  | 89 | gSecondConnection = new RTCPeerConnection(null); | 
|  | 90 | gSecondConnection.onicecandidate = onIceCandidateToSecond; | 
|  | 91 | gSecondConnection.onaddstream = onRemoteStream; | 
|  | 92 |  | 
|  | 93 | // The createDataChannel is necessary and sufficient to make | 
|  | 94 | // sure the ICE connection be attempted. | 
|  | 95 | gFirstConnection.createDataChannel('channel'); | 
|  | 96 |  | 
|  | 97 | var atStep = 'Create offer'; | 
|  | 98 |  | 
|  | 99 | gFirstConnection.createOffer() | 
|  | 100 | .then(function(offer) { | 
|  | 101 | atStep = 'Set local description at first'; | 
|  | 102 | return gFirstConnection.setLocalDescription(offer); | 
|  | 103 | }) | 
|  | 104 | .then(function() { | 
|  | 105 | atStep = 'Set remote description at second'; | 
|  | 106 | return gSecondConnection.setRemoteDescription( | 
|  | 107 | gFirstConnection.localDescription); | 
|  | 108 | }) | 
|  | 109 | .then(function() { | 
|  | 110 | atStep = 'Create answer'; | 
|  | 111 | return gSecondConnection.createAnswer(); | 
|  | 112 | }) | 
|  | 113 | .then(function(answer) { | 
|  | 114 | atStep = 'Set local description at second'; | 
|  | 115 | return gSecondConnection.setLocalDescription(answer); | 
|  | 116 | }) | 
|  | 117 | .then(function() { | 
|  | 118 | atStep = 'Set remote description at first'; | 
|  | 119 | return gFirstConnection.setRemoteDescription( | 
|  | 120 | gSecondConnection.localDescription); | 
|  | 121 | }) | 
| Harald Alvestrand | fb64851 | 2017-01-03 16:49:16 | [diff] [blame] | 122 | .catch(test.step_func(function(e) { | 
|  | 123 | assert_unreached('Error ' + e.name + ': ' + e.message + | 
|  | 124 | ' happened at step ' + atStep); | 
|  | 125 | })); | 
|  | 126 | }); | 
|  | 127 | </script> | 
|  | 128 |  | 
|  | 129 | </body> | 
|  | 130 | </html> |