Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /** | 1 /** |
| 2 * Copyright 2017 The Chromium Authors. All rights reserved. | 2 * Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /** @private */ | |
| 8 var gOnNegotiationNeededCount = undefined; | |
| 9 | |
| 7 // Public interface to tests. These are expected to be called with | 10 // Public interface to tests. These are expected to be called with |
| 8 // ExecuteJavascript invocations from the browser tests and will return answers | 11 // ExecuteJavascript invocations from the browser tests and will return answers |
| 9 // through the DOM automation controller. | 12 // through the DOM automation controller. |
| 10 | 13 |
| 11 /** | 14 /** |
| 12 * Adds |count| streams to the peer connection, with one audio and one video | 15 * Adds |count| streams to the peer connection, with one audio and one video |
| 13 * track per stream. | 16 * track per stream. |
| 14 * | 17 * |
| 15 * Returns "ok-streams-created-and-added" on success. | 18 * Returns "ok-streams-created-and-added" on success. |
| 16 */ | 19 */ |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 38 function verifyRtpSenders(expectedNumTracks = null) { | 41 function verifyRtpSenders(expectedNumTracks = null) { |
| 39 if (expectedNumTracks != null && | 42 if (expectedNumTracks != null && |
| 40 peerConnection_().getSenders().length != expectedNumTracks) { | 43 peerConnection_().getSenders().length != expectedNumTracks) { |
| 41 throw failTest('getSenders().length != expectedNumTracks'); | 44 throw failTest('getSenders().length != expectedNumTracks'); |
| 42 } | 45 } |
| 43 if (!arrayEquals_(peerConnection_().getSenders(), | 46 if (!arrayEquals_(peerConnection_().getSenders(), |
| 44 peerConnection_().getSenders())) { | 47 peerConnection_().getSenders())) { |
| 45 throw failTest('One getSenders() call is not equal to the next.'); | 48 throw failTest('One getSenders() call is not equal to the next.'); |
| 46 } | 49 } |
| 47 | 50 |
| 48 let localTracks = new Set(); | |
| 49 peerConnection_().getLocalStreams().forEach(function(stream) { | |
| 50 stream.getTracks().forEach(function(track) { | |
| 51 localTracks.add(track); | |
| 52 }); | |
| 53 }); | |
| 54 if (peerConnection_().getSenders().length != localTracks.size) | |
| 55 throw failTest('The number of senders and tracks are not the same.'); | |
| 56 | |
| 57 let senders = new Set(); | 51 let senders = new Set(); |
| 58 let senderTracks = new Set(); | 52 let senderTracks = new Set(); |
| 59 peerConnection_().getSenders().forEach(function(sender) { | 53 peerConnection_().getSenders().forEach(function(sender) { |
| 60 if (sender == null) | 54 if (sender == null) |
| 61 throw failTest('sender is null or undefined.'); | 55 throw failTest('sender is null or undefined.'); |
| 62 if (sender.track == null) | 56 if (sender.track == null) |
| 63 throw failTest('sender.track is null or undefined.'); | 57 throw failTest('sender.track is null or undefined.'); |
| 64 senders.add(sender); | 58 senders.add(sender); |
| 65 senderTracks.add(sender.track); | 59 senderTracks.add(sender.track); |
| 66 }); | 60 }); |
| 67 if (senderTracks.size != senders.size) | 61 if (senderTracks.size != senders.size) |
| 68 throw failTest('senderTracks.size != senders.size'); | 62 throw failTest('senderTracks.size != senders.size'); |
| 69 | 63 |
| 70 if (!setEquals_(senderTracks, localTracks)) { | |
| 71 throw failTest('The set of sender tracks is not equal to the set of ' + | |
| 72 'stream tracks.'); | |
| 73 } | |
| 74 returnToTest('ok-senders-verified'); | 64 returnToTest('ok-senders-verified'); |
| 75 } | 65 } |
| 76 | 66 |
| 77 /** | 67 /** |
| 78 * Verifies that the peer connection's getReceivers() returns one receiver per | 68 * Verifies that the peer connection's getReceivers() returns one receiver per |
| 79 * remote track, that there are no duplicates and that object identity is | 69 * remote track, that there are no duplicates and that object identity is |
| 80 * preserved. | 70 * preserved. |
| 81 * | 71 * |
| 82 * Returns "ok-receivers-verified" on success. | 72 * Returns "ok-receivers-verified" on success. |
| 83 */ | 73 */ |
| 84 function verifyRtpReceivers(expectedNumTracks = null) { | 74 function verifyRtpReceivers(expectedNumTracks = null) { |
| 85 if (peerConnection_().getReceivers() == null) | 75 if (peerConnection_().getReceivers() == null) |
| 86 throw failTest('getReceivers() returns null or undefined.'); | 76 throw failTest('getReceivers() returns null or undefined.'); |
| 87 if (expectedNumTracks != null && | 77 if (expectedNumTracks != null && |
| 88 peerConnection_().getReceivers().length != expectedNumTracks) { | 78 peerConnection_().getReceivers().length != expectedNumTracks) { |
| 89 throw failTest('getReceivers().length != expectedNumTracks'); | 79 throw failTest('getReceivers().length != expectedNumTracks'); |
| 90 } | 80 } |
| 91 if (!arrayEquals_(peerConnection_().getReceivers(), | 81 if (!arrayEquals_(peerConnection_().getReceivers(), |
| 92 peerConnection_().getReceivers())) { | 82 peerConnection_().getReceivers())) { |
| 93 throw failTest('One getReceivers() call is not equal to the next.'); | 83 throw failTest('One getReceivers() call is not equal to the next.'); |
| 94 } | 84 } |
| 95 | 85 |
| 96 let remoteTracks = new Set(); | |
| 97 peerConnection_().getRemoteStreams().forEach(function(stream) { | |
| 98 stream.getTracks().forEach(function(track) { | |
| 99 remoteTracks.add(track); | |
| 100 }); | |
| 101 }); | |
| 102 if (peerConnection_().getReceivers().length != remoteTracks.size) | |
| 103 throw failTest('The number of receivers and tracks are not the same.'); | |
| 104 | |
| 105 let receivers = new Set(); | 86 let receivers = new Set(); |
| 106 let receiverTracks = new Set(); | 87 let receiverTracks = new Set(); |
| 107 peerConnection_().getReceivers().forEach(function(receiver) { | 88 peerConnection_().getReceivers().forEach(function(receiver) { |
| 108 if (receiver == null) | 89 if (receiver == null) |
| 109 throw failTest('receiver is null or undefined.'); | 90 throw failTest('receiver is null or undefined.'); |
| 110 if (receiver.track == null) | 91 if (receiver.track == null) |
| 111 throw failTest('receiver.track is null or undefined.'); | 92 throw failTest('receiver.track is null or undefined.'); |
| 112 if (receiver.getContributingSources().length != 0) | 93 if (receiver.getContributingSources().length != 0) |
| 113 throw failTest('receiver.getContributingSources() is not empty.'); | 94 throw failTest('receiver.getContributingSources() is not empty.'); |
| 114 receivers.add(receiver); | 95 receivers.add(receiver); |
| 115 receiverTracks.add(receiver.track); | 96 receiverTracks.add(receiver.track); |
| 116 }); | 97 }); |
| 117 if (receiverTracks.size != receivers.size) | 98 if (receiverTracks.size != receivers.size) |
| 118 throw failTest('receiverTracks.size != receivers.size'); | 99 throw failTest('receiverTracks.size != receivers.size'); |
| 119 | 100 |
| 120 if (!setEquals_(receiverTracks, remoteTracks)) { | 101 returnToTest('ok-receivers-verified'); |
| 121 throw failTest('The set of receiver tracks is not equal to the set of ' + | 102 } |
| 122 'stream tracks.'); | 103 |
| 104 /** | |
| 105 * Creates an audio and video track and adds them to the peer connection using | |
| 106 * |addTrack|. They are added with or without a stream in accordance with | |
| 107 * |streamArgumentType|. | |
| 108 * | |
| 109 * Returns | |
| 110 * "ok-<audio stream id> <audio track id> <video stream id> <video track id>" on | |
| 111 * success. If no stream is backing up the track, <stream id> is "null". | |
| 112 * | |
| 113 * @param {string} streamArgumentType Must be one of the following values: | |
| 114 * 'no-stream' - The tracks are added without an associated stream. | |
| 115 * 'shared-stream' - The tracks are added with the same associated stream. | |
| 116 * 'individual-streams' - A stream is created for each track. | |
| 117 */ | |
| 118 function createAndAddAudioAndVideoTrack(streamArgumentType) { | |
| 119 if (streamArgumentType !== 'no-stream' && | |
| 120 streamArgumentType !== 'shared-stream' && | |
| 121 streamArgumentType !== 'individual-streams') | |
| 122 throw failTest('Unsupported streamArgumentType.'); | |
| 123 getUserMedia({ audio: true, video: true }, | |
| 124 function(stream) { | |
| 125 let audioStream = undefined; | |
| 126 if (streamArgumentType !== 'no-stream') | |
| 127 audioStream = new MediaStream(); | |
| 128 | |
| 129 let audioTrack = stream.getAudioTracks()[0]; | |
| 130 let audioSender = | |
| 131 audioStream ? peerConnection_().addTrack(audioTrack, audioStream) | |
| 132 : peerConnection_().addTrack(audioTrack); | |
| 133 if (!audioSender || audioSender.track != audioTrack) | |
| 134 throw failTest('addTrack did not return a sender with the track.'); | |
| 135 | |
| 136 let videoStream = undefined; | |
| 137 if (streamArgumentType === 'shared-stream') { | |
| 138 videoStream = audioStream; | |
| 139 } else if (streamArgumentType === 'individual-streams') { | |
| 140 videoStream = new MediaStream(); | |
| 141 } | |
| 142 | |
| 143 let videoTrack = stream.getVideoTracks()[0]; | |
| 144 let videoSender = | |
| 145 videoStream ? peerConnection_().addTrack(videoTrack, videoStream) | |
| 146 : peerConnection_().addTrack(videoTrack); | |
| 147 if (!videoSender || videoSender.track != videoTrack) | |
| 148 throw failTest('addTrack did not return a sender with the track.'); | |
| 149 | |
| 150 let audioStreamId = audioStream ? audioStream.id : 'null'; | |
| 151 let videoStreamId = videoStream ? videoStream.id : 'null'; | |
| 152 returnToTest('ok-' + audioStreamId + ' ' + audioTrack.id | |
| 153 + ' ' + videoStreamId + ' ' + videoTrack.id); | |
| 154 }, | |
| 155 function(error) { | |
| 156 throw failTest('getUserMedia failed: ' + error); | |
| 157 }); | |
| 158 } | |
| 159 | |
| 160 /** | |
| 161 * Calls |removeTrack| with the first sender that has the track with |trackId| | |
| 162 * and verifies the SDP is updated accordingly. | |
| 163 * | |
| 164 * Returns "ok-sender-removed" on success. | |
| 165 */ | |
| 166 function removeTrack(trackId) { | |
| 167 let sender = null; | |
| 168 let otherSenderHasTrack = false; | |
| 169 peerConnection_().getSenders().forEach(function(s) { | |
| 170 if (s.track && s.track.id == trackId) { | |
| 171 if (!sender) | |
| 172 sender = s; | |
| 173 else | |
| 174 otherSenderHasTrack = true; | |
| 175 } | |
| 176 }); | |
| 177 if (!sender) | |
| 178 throw failTest('There is no sender for track ' + trackId); | |
| 179 peerConnection_().removeTrack(sender); | |
| 180 if (sender.track) | |
| 181 throw failTest('sender.track was not nulled by removeTrack.'); | |
| 182 returnToTest('ok-sender-removed'); | |
| 183 } | |
| 184 | |
| 185 /** | |
| 186 */ | |
| 187 function hasLocalStreamWithTrack(streamId, trackId) { | |
| 188 if (hasStreamWithTrack( | |
| 189 peerConnection_().getLocalStreams(), streamId, trackId)) { | |
| 190 returnToTest('ok-stream-with-track-found'); | |
| 191 return; | |
| 123 } | 192 } |
| 124 returnToTest('ok-receivers-verified'); | 193 returnToTest('ok-stream-with-track-not-found'); |
| 194 } | |
| 195 | |
| 196 /** | |
| 197 */ | |
| 198 function hasRemoteStreamWithTrack(streamId, trackId) { | |
| 199 if (hasStreamWithTrack( | |
| 200 peerConnection_().getRemoteStreams(), streamId, trackId)) { | |
| 201 returnToTest('ok-stream-with-track-found'); | |
| 202 return; | |
| 203 } | |
| 204 returnToTest('ok-stream-with-track-not-found'); | |
| 205 } | |
| 206 | |
| 207 /** | |
| 208 */ | |
| 209 function hasSenderWithTrack(trackId) { | |
| 210 if (hasSenderOrReceiverWithTrack(peerConnection_().getSenders(), trackId)) { | |
| 211 returnToTest('ok-sender-with-track-found'); | |
| 212 return; | |
| 213 } | |
| 214 returnToTest('ok-sender-with-track-not-found'); | |
| 215 } | |
| 216 | |
| 217 /** | |
| 218 */ | |
| 219 function hasReceiverWithTrack(trackId) { | |
| 220 if (hasSenderOrReceiverWithTrack(peerConnection_().getReceivers(), trackId)) { | |
| 221 returnToTest('ok-receiver-with-track-found'); | |
| 222 return; | |
| 223 } | |
| 224 returnToTest('ok-receiver-with-track-not-found'); | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 */ | |
| 229 function startCountingOnNegotiationNeeded() { | |
|
Taylor_Brandstetter
2017/07/07 19:44:14
I don't think you really need a "start counting" m
hbos_chromium
2017/07/10 12:32:59
I moved this to peerconnection.js where it always
| |
| 230 gOnNegotiationNeededCount = 0; | |
| 231 peerConnection_().onnegotiationneeded = function() { | |
| 232 ++gOnNegotiationNeededCount; | |
| 233 } | |
| 234 returnToTest('ok-onnegotiationneeded-wired'); | |
| 235 } | |
| 236 | |
| 237 /** | |
| 238 */ | |
| 239 function waitUntilOnNegotiationCountIs(count) { | |
| 240 if (gOnNegotiationNeededCount === undefined) | |
| 241 throw failTest('Must call startCountingOnNegotiationNeeded() first.'); | |
| 242 let checkCount = function() { | |
| 243 if (gOnNegotiationNeededCount === count) { | |
| 244 returnToTest('ok-expected-count'); | |
| 245 return; | |
| 246 } | |
| 247 window.setTimeout(checkCount, 10); | |
|
Taylor_Brandstetter
2017/07/07 19:44:13
Does this really need a timeout, even if it's only
hbos_chromium
2017/07/10 12:32:59
Done, changed to a getter instead of a "wait" and
| |
| 248 }; | |
| 249 checkCount(); | |
| 125 } | 250 } |
| 126 | 251 |
| 127 // Internals. | 252 // Internals. |
| 128 | 253 |
| 129 /** @private */ | 254 /** @private */ |
| 255 function hasStreamWithTrack(streams, streamId, trackId) { | |
| 256 for (let i = 0; i < streams.length; ++i) { | |
| 257 let stream = streams[i]; | |
| 258 if (streamId && stream.id !== streamId) | |
| 259 continue; | |
| 260 let tracks = stream.getTracks(); | |
| 261 for (let j = 0; j < tracks.length; ++j) { | |
| 262 let track = tracks[j]; | |
| 263 if (track.id == trackId) { | |
| 264 return true; | |
| 265 } | |
| 266 } | |
| 267 } | |
| 268 return false; | |
| 269 } | |
| 270 | |
| 271 /** @private */ | |
| 272 function hasSenderOrReceiverWithTrack(sendersOrReceivers, trackId) { | |
| 273 for (let i = 0; i < sendersOrReceivers.length; ++i) { | |
| 274 if (sendersOrReceivers[i].track && | |
| 275 sendersOrReceivers[i].track.id === trackId) { | |
| 276 return true; | |
| 277 } | |
| 278 } | |
| 279 return false; | |
| 280 } | |
| 281 | |
| 282 /** @private */ | |
| 130 function arrayEquals_(a, b) { | 283 function arrayEquals_(a, b) { |
| 131 if (a == null) | 284 if (a == null) |
| 132 return b == null; | 285 return b == null; |
| 133 if (a.length != b.length) | 286 if (a.length != b.length) |
| 134 return false; | 287 return false; |
| 135 for (let i = 0; i < a.length; ++i) { | 288 for (let i = 0; i < a.length; ++i) { |
| 136 if (a[i] !== b[i]) | 289 if (a[i] !== b[i]) |
| 137 return false; | 290 return false; |
| 138 } | 291 } |
| 139 return true; | 292 return true; |
| 140 } | 293 } |
| 141 | 294 |
| 142 /** @private */ | 295 /** @private */ |
| 143 function setEquals_(a, b) { | 296 function setEquals_(a, b) { |
| 144 if (a == null) | 297 if (a == null) |
| 145 return b == null; | 298 return b == null; |
| 146 if (a.size != b.size) | 299 if (a.size != b.size) |
| 147 return false; | 300 return false; |
| 148 a.forEach(function(value) { | 301 a.forEach(function(value) { |
| 149 if (!b.has(value)) | 302 if (!b.has(value)) |
| 150 return false; | 303 return false; |
| 151 }); | 304 }); |
| 152 return true; | 305 return true; |
| 153 } | 306 } |
| OLD | NEW |