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 // Public interface to tests. These are expected to be called with | 7 // Public interface to tests. These are expected to be called with |
| 8 // ExecuteJavascript invocations from the browser tests and will return answers | 8 // ExecuteJavascript invocations from the browser tests and will return answers |
| 9 // through the DOM automation controller. | 9 // through the DOM automation controller. |
| 10 | 10 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 function verifyRtpSenders(expectedNumTracks = null) { | 38 function verifyRtpSenders(expectedNumTracks = null) { |
| 39 if (expectedNumTracks != null && | 39 if (expectedNumTracks != null && |
| 40 peerConnection_().getSenders().length != expectedNumTracks) { | 40 peerConnection_().getSenders().length != expectedNumTracks) { |
| 41 throw failTest('getSenders().length != expectedNumTracks'); | 41 throw failTest('getSenders().length != expectedNumTracks'); |
| 42 } | 42 } |
| 43 if (!arrayEquals_(peerConnection_().getSenders(), | 43 if (!arrayEquals_(peerConnection_().getSenders(), |
| 44 peerConnection_().getSenders())) { | 44 peerConnection_().getSenders())) { |
| 45 throw failTest('One getSenders() call is not equal to the next.'); | 45 throw failTest('One getSenders() call is not equal to the next.'); |
| 46 } | 46 } |
| 47 | 47 |
| 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(); | 48 let senders = new Set(); |
| 58 let senderTracks = new Set(); | 49 let senderTracks = new Set(); |
| 59 peerConnection_().getSenders().forEach(function(sender) { | 50 peerConnection_().getSenders().forEach(function(sender) { |
| 60 if (sender == null) | 51 if (sender == null) |
| 61 throw failTest('sender is null or undefined.'); | 52 throw failTest('sender is null or undefined.'); |
| 62 if (sender.track == null) | 53 if (sender.track == null) |
| 63 throw failTest('sender.track is null or undefined.'); | 54 throw failTest('sender.track is null or undefined.'); |
| 64 senders.add(sender); | 55 senders.add(sender); |
| 65 senderTracks.add(sender.track); | 56 senderTracks.add(sender.track); |
| 66 }); | 57 }); |
| 67 if (senderTracks.size != senders.size) | 58 if (senderTracks.size != senders.size) |
| 68 throw failTest('senderTracks.size != senders.size'); | 59 throw failTest('senderTracks.size != senders.size'); |
| 69 | 60 |
| 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'); | 61 returnToTest('ok-senders-verified'); |
| 75 } | 62 } |
| 76 | 63 |
| 77 /** | 64 /** |
| 78 * Verifies that the peer connection's getReceivers() returns one receiver per | 65 * Verifies that the peer connection's getReceivers() returns one receiver per |
| 79 * remote track, that there are no duplicates and that object identity is | 66 * remote track, that there are no duplicates and that object identity is |
| 80 * preserved. | 67 * preserved. |
| 81 * | 68 * |
| 82 * Returns "ok-receivers-verified" on success. | 69 * Returns "ok-receivers-verified" on success. |
| 83 */ | 70 */ |
| 84 function verifyRtpReceivers(expectedNumTracks = null) { | 71 function verifyRtpReceivers(expectedNumTracks = null) { |
| 85 if (peerConnection_().getReceivers() == null) | 72 if (peerConnection_().getReceivers() == null) |
| 86 throw failTest('getReceivers() returns null or undefined.'); | 73 throw failTest('getReceivers() returns null or undefined.'); |
| 87 if (expectedNumTracks != null && | 74 if (expectedNumTracks != null && |
| 88 peerConnection_().getReceivers().length != expectedNumTracks) { | 75 peerConnection_().getReceivers().length != expectedNumTracks) { |
| 89 throw failTest('getReceivers().length != expectedNumTracks'); | 76 throw failTest('getReceivers().length != expectedNumTracks'); |
| 90 } | 77 } |
| 91 if (!arrayEquals_(peerConnection_().getReceivers(), | 78 if (!arrayEquals_(peerConnection_().getReceivers(), |
| 92 peerConnection_().getReceivers())) { | 79 peerConnection_().getReceivers())) { |
| 93 throw failTest('One getReceivers() call is not equal to the next.'); | 80 throw failTest('One getReceivers() call is not equal to the next.'); |
| 94 } | 81 } |
| 95 | 82 |
| 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(); | 83 let receivers = new Set(); |
| 106 let receiverTracks = new Set(); | 84 let receiverTracks = new Set(); |
| 107 peerConnection_().getReceivers().forEach(function(receiver) { | 85 peerConnection_().getReceivers().forEach(function(receiver) { |
| 108 if (receiver == null) | 86 if (receiver == null) |
| 109 throw failTest('receiver is null or undefined.'); | 87 throw failTest('receiver is null or undefined.'); |
| 110 if (receiver.track == null) | 88 if (receiver.track == null) |
| 111 throw failTest('receiver.track is null or undefined.'); | 89 throw failTest('receiver.track is null or undefined.'); |
| 112 if (receiver.getContributingSources().length != 0) | 90 if (receiver.getContributingSources().length != 0) |
| 113 throw failTest('receiver.getContributingSources() is not empty.'); | 91 throw failTest('receiver.getContributingSources() is not empty.'); |
| 114 receivers.add(receiver); | 92 receivers.add(receiver); |
| 115 receiverTracks.add(receiver.track); | 93 receiverTracks.add(receiver.track); |
| 116 }); | 94 }); |
| 117 if (receiverTracks.size != receivers.size) | 95 if (receiverTracks.size != receivers.size) |
| 118 throw failTest('receiverTracks.size != receivers.size'); | 96 throw failTest('receiverTracks.size != receivers.size'); |
| 119 | 97 |
| 120 if (!setEquals_(receiverTracks, remoteTracks)) { | |
| 121 throw failTest('The set of receiver tracks is not equal to the set of ' + | |
| 122 'stream tracks.'); | |
| 123 } | |
| 124 returnToTest('ok-receivers-verified'); | 98 returnToTest('ok-receivers-verified'); |
| 125 } | 99 } |
| 126 | 100 |
| 101 /** | |
| 102 * Creates an audio and video track and adds them to the peer connection using | |
| 103 * |addTrack|. They are added with or without a stream in accordance with | |
| 104 * |streamArgumentType|. The test ensures senders are created and parses the SDP | |
| 105 * to determine the resulting stream and track structure is what we expect. In | |
| 106 * the 'no-stream' case the test allows the peer connection to use null streams | |
| 107 * or to advertise streams that are hidden from us. | |
| 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 initialSenderCount = peerConnection_().getSenders().length; | |
| 126 let initialLocalStreams = new Set(peerConnection_().getLocalStreams()); | |
| 127 | |
| 128 let audioStream = undefined; | |
| 129 if (streamArgumentType !== 'no-stream') | |
| 130 audioStream = new MediaStream(); | |
| 131 | |
| 132 let audioTrack = stream.getAudioTracks()[0]; | |
| 133 let audioSender = (!audioStream) | |
|
Guido Urdaneta
2017/07/06 09:49:57
why "(!audioStream)" and not just "audioStream" ?
hbos_chromium
2017/07/06 12:31:37
Done.
| |
| 134 ? peerConnection_().addTrack(audioTrack) | |
| 135 : peerConnection_().addTrack(audioTrack, audioStream); | |
| 136 if (audioSender.track !== audioTrack) | |
| 137 throw failTest('audioSender.track !== audioTrack'); | |
| 138 | |
| 139 peerConnection_().onnegotiationneeded = function() { | |
| 140 let videoStream = undefined; | |
| 141 if (streamArgumentType === 'shared-stream') { | |
| 142 videoStream = audioStream; | |
| 143 } else if (streamArgumentType === 'individual-streams') { | |
| 144 videoStream = new MediaStream(); | |
| 145 } | |
| 146 | |
| 147 let videoTrack = stream.getVideoTracks()[0]; | |
| 148 let videoSender = (!videoStream) | |
|
Guido Urdaneta
2017/07/06 09:49:57
ditto
hbos_chromium
2017/07/06 12:31:37
Donno.
| |
| 149 ? peerConnection_().addTrack(videoTrack) | |
| 150 : peerConnection_().addTrack(videoTrack, videoStream); | |
| 151 if (videoSender.track !== videoTrack) | |
| 152 throw failTest('videoSender.track !== videoTrack'); | |
| 153 | |
| 154 if (peerConnection_().getSenders().length !== | |
| 155 initialSenderCount + 2 || | |
| 156 !peerConnection_().getSenders().includes(audioSender) || | |
| 157 !peerConnection_().getSenders().includes(videoSender)) | |
| 158 throw failTest('Unexpected getSenders() result.'); | |
| 159 | |
| 160 peerConnection_().onnegotiationneeded = function() { | |
| 161 peerConnection_().createOffer().then(function(sessionDecription) { | |
| 162 // TODO(hbos): When |getLocalStreams| is the set of streams of all | |
| 163 // senders the set will be modified to include |addTrack|-streams. | |
| 164 // https://crbug.com/738918 | |
| 165 if (!setEquals_(initialLocalStreams, | |
| 166 new Set(peerConnection_().getLocalStreams()))) | |
| 167 throw failTest('The local stream set was modified.'); | |
| 168 let streamsAndTracks = getSdpStreamsAndTracks( | |
| 169 sessionDecription.sdp); | |
| 170 if (!streamsAndTracks.trackIds.has(audioTrack.id) || | |
| 171 !streamsAndTracks.trackIds.has(videoTrack.id)) | |
| 172 throw failTest('Track ID are missing from the SDP.'); | |
| 173 if (streamsAndTracks.streamIds.has(stream.id)) { | |
| 174 // We never added this stream. | |
| 175 throw failTest( | |
| 176 'The stream we created shouldn\'t be listed in the SDP.'); | |
| 177 } | |
| 178 let audioStreamId = null; | |
| 179 let videoStreamId = null; | |
| 180 for (let [streamId, trackIds] of | |
| 181 streamsAndTracks.streamTrackMap) { | |
| 182 if (trackIds.has(audioTrack.id)) { | |
| 183 if (audioStreamId != null) { | |
| 184 throw failTest( | |
| 185 'The audio track was added to multiple streams.'); | |
| 186 } | |
| 187 audioStreamId = streamId; | |
| 188 } | |
| 189 if (trackIds.has(videoTrack.id)) { | |
| 190 if (videoStreamId != null) { | |
| 191 throw failTest( | |
| 192 'The video track was added to multiple streams.'); | |
| 193 } | |
| 194 videoStreamId = streamId; | |
| 195 } | |
| 196 } | |
| 197 if (audioStreamId == null) | |
| 198 audioStreamId = 'null'; | |
| 199 if (videoStreamId == null) | |
| 200 videoStreamId = 'null'; | |
| 201 if (audioStream && audioStream.id !== audioStreamId) { | |
| 202 throw failTest('Expected audio stream id ' + audioStream.id + | |
| 203 ', but got ' + audioStreamId); | |
| 204 } | |
| 205 if (videoStream && videoStream.id !== videoStreamId) { | |
| 206 throw failTest('Expected video stream id ' + videoStream.id + | |
| 207 ', but got ' + videoStreamId); | |
| 208 } | |
| 209 returnToTest('ok-' + audioStreamId + ' ' + audioTrack.id | |
| 210 + ' ' + videoStreamId + ' ' + videoTrack.id); | |
| 211 }); | |
| 212 }; | |
| 213 }; | |
| 214 }, | |
| 215 function(error) { | |
| 216 throw failTest('getUserMedia failed: ' + error); | |
| 217 }); | |
| 218 } | |
| 219 | |
| 220 /** | |
| 221 * Calls |removeTrack| with the first sender that has the track with |trackId| | |
| 222 * and verifies the SDP is updated accordingly. | |
| 223 * | |
| 224 * Returns "ok-sender-removed" on success. | |
| 225 */ | |
| 226 function removeTrack(trackId) { | |
| 227 let sender = null; | |
| 228 let otherSenderHasTrack = false; | |
| 229 peerConnection_().getSenders().forEach(function(s) { | |
| 230 if (s.track && s.track.id == trackId) { | |
| 231 if (!sender) | |
| 232 sender = s; | |
| 233 else | |
| 234 otherSenderHasTrack = true; | |
| 235 } | |
| 236 }); | |
| 237 if (!sender) | |
| 238 throw failTest('There is no sender for track ' + trackId); | |
| 239 peerConnection_().removeTrack(sender); | |
| 240 if (sender.track) | |
| 241 throw failTest('sender.track was not nulled by removeTrack.'); | |
| 242 peerConnection_().onnegotiationneeded = function() { | |
| 243 peerConnection_().createOffer().then(function(sessionDecription) { | |
| 244 let streamsAndTracks = getSdpStreamsAndTracks(sessionDecription.sdp); | |
| 245 if (!otherSenderHasTrack && streamsAndTracks.trackIds.has(trackId)) | |
| 246 throw failTest('Removing the track did not remove it from the SDP.'); | |
| 247 if (otherSenderHasTrack && !streamsAndTracks.trackIds.has(trackId)) | |
| 248 throw failTest('Track not expected to be removed from the SDP.'); | |
| 249 returnToTest('ok-sender-removed'); | |
| 250 }); | |
| 251 }; | |
| 252 } | |
| 253 | |
| 127 // Internals. | 254 // Internals. |
| 128 | 255 |
| 129 /** @private */ | 256 /** @private */ |
| 130 function arrayEquals_(a, b) { | 257 function arrayEquals_(a, b) { |
| 131 if (a == null) | 258 if (a == null) |
| 132 return b == null; | 259 return b == null; |
| 133 if (a.length != b.length) | 260 if (a.length != b.length) |
| 134 return false; | 261 return false; |
| 135 for (let i = 0; i < a.length; ++i) { | 262 for (let i = 0; i < a.length; ++i) { |
| 136 if (a[i] !== b[i]) | 263 if (a[i] !== b[i]) |
| 137 return false; | 264 return false; |
| 138 } | 265 } |
| 139 return true; | 266 return true; |
| 140 } | 267 } |
| 141 | 268 |
| 142 /** @private */ | 269 /** @private */ |
| 143 function setEquals_(a, b) { | 270 function setEquals_(a, b) { |
| 144 if (a == null) | 271 if (a == null) |
| 145 return b == null; | 272 return b == null; |
| 146 if (a.size != b.size) | 273 if (a.size != b.size) |
| 147 return false; | 274 return false; |
| 148 a.forEach(function(value) { | 275 a.forEach(function(value) { |
| 149 if (!b.has(value)) | 276 if (!b.has(value)) |
| 150 return false; | 277 return false; |
| 151 }); | 278 }); |
| 152 return true; | 279 return true; |
| 153 } | 280 } |
| OLD | NEW |