OLD | NEW |
(Empty) | |
| 1 /* |
| 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 |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.e
xports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var
g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined")
{g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.adapter = f()}
})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u
){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return
a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw
f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,funct
ion(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].export
s}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);r
eturn s})({1:[function(require,module,exports){ |
| 7 /* eslint-env node */ |
| 8 'use strict'; |
| 9 |
| 10 // SDP helpers. |
| 11 var SDPUtils = {}; |
| 12 |
| 13 // Generate an alphanumeric identifier for cname or mids. |
| 14 // TODO: use UUIDs instead? https://gist.github.com/jed/982883 |
| 15 SDPUtils.generateIdentifier = function() { |
| 16 return Math.random().toString(36).substr(2, 10); |
| 17 }; |
| 18 |
| 19 // The RTCP CNAME used by all peerconnections from the same JS. |
| 20 SDPUtils.localCName = SDPUtils.generateIdentifier(); |
| 21 |
| 22 // Splits SDP into lines, dealing with both CRLF and LF. |
| 23 SDPUtils.splitLines = function(blob) { |
| 24 return blob.trim().split('\n').map(function(line) { |
| 25 return line.trim(); |
| 26 }); |
| 27 }; |
| 28 // Splits SDP into sessionpart and mediasections. Ensures CRLF. |
| 29 SDPUtils.splitSections = function(blob) { |
| 30 var parts = blob.split('\nm='); |
| 31 return parts.map(function(part, index) { |
| 32 return (index > 0 ? 'm=' + part : part).trim() + '\r\n'; |
| 33 }); |
| 34 }; |
| 35 |
| 36 // Returns lines that start with a certain prefix. |
| 37 SDPUtils.matchPrefix = function(blob, prefix) { |
| 38 return SDPUtils.splitLines(blob).filter(function(line) { |
| 39 return line.indexOf(prefix) === 0; |
| 40 }); |
| 41 }; |
| 42 |
| 43 // Parses an ICE candidate line. Sample input: |
| 44 // candidate:702786350 2 udp 41819902 8.8.8.8 60769 typ relay raddr 8.8.8.8 |
| 45 // rport 55996" |
| 46 SDPUtils.parseCandidate = function(line) { |
| 47 var parts; |
| 48 // Parse both variants. |
| 49 if (line.indexOf('a=candidate:') === 0) { |
| 50 parts = line.substring(12).split(' '); |
| 51 } else { |
| 52 parts = line.substring(10).split(' '); |
| 53 } |
| 54 |
| 55 var candidate = { |
| 56 foundation: parts[0], |
| 57 component: parts[1], |
| 58 protocol: parts[2].toLowerCase(), |
| 59 priority: parseInt(parts[3], 10), |
| 60 ip: parts[4], |
| 61 port: parseInt(parts[5], 10), |
| 62 // skip parts[6] == 'typ' |
| 63 type: parts[7] |
| 64 }; |
| 65 |
| 66 for (var i = 8; i < parts.length; i += 2) { |
| 67 switch (parts[i]) { |
| 68 case 'raddr': |
| 69 candidate.relatedAddress = parts[i + 1]; |
| 70 break; |
| 71 case 'rport': |
| 72 candidate.relatedPort = parseInt(parts[i + 1], 10); |
| 73 break; |
| 74 case 'tcptype': |
| 75 candidate.tcpType = parts[i + 1]; |
| 76 break; |
| 77 default: // Unknown extensions are silently ignored. |
| 78 break; |
| 79 } |
| 80 } |
| 81 return candidate; |
| 82 }; |
| 83 |
| 84 // Translates a candidate object into SDP candidate attribute. |
| 85 SDPUtils.writeCandidate = function(candidate) { |
| 86 var sdp = []; |
| 87 sdp.push(candidate.foundation); |
| 88 sdp.push(candidate.component); |
| 89 sdp.push(candidate.protocol.toUpperCase()); |
| 90 sdp.push(candidate.priority); |
| 91 sdp.push(candidate.ip); |
| 92 sdp.push(candidate.port); |
| 93 |
| 94 var type = candidate.type; |
| 95 sdp.push('typ'); |
| 96 sdp.push(type); |
| 97 if (type !== 'host' && candidate.relatedAddress && |
| 98 candidate.relatedPort) { |
| 99 sdp.push('raddr'); |
| 100 sdp.push(candidate.relatedAddress); // was: relAddr |
| 101 sdp.push('rport'); |
| 102 sdp.push(candidate.relatedPort); // was: relPort |
| 103 } |
| 104 if (candidate.tcpType && candidate.protocol.toLowerCase() === 'tcp') { |
| 105 sdp.push('tcptype'); |
| 106 sdp.push(candidate.tcpType); |
| 107 } |
| 108 return 'candidate:' + sdp.join(' '); |
| 109 }; |
| 110 |
| 111 // Parses an rtpmap line, returns RTCRtpCoddecParameters. Sample input: |
| 112 // a=rtpmap:111 opus/48000/2 |
| 113 SDPUtils.parseRtpMap = function(line) { |
| 114 var parts = line.substr(9).split(' '); |
| 115 var parsed = { |
| 116 payloadType: parseInt(parts.shift(), 10) // was: id |
| 117 }; |
| 118 |
| 119 parts = parts[0].split('/'); |
| 120 |
| 121 parsed.name = parts[0]; |
| 122 parsed.clockRate = parseInt(parts[1], 10); // was: clockrate |
| 123 // was: channels |
| 124 parsed.numChannels = parts.length === 3 ? parseInt(parts[2], 10) : 1; |
| 125 return parsed; |
| 126 }; |
| 127 |
| 128 // Generate an a=rtpmap line from RTCRtpCodecCapability or |
| 129 // RTCRtpCodecParameters. |
| 130 SDPUtils.writeRtpMap = function(codec) { |
| 131 var pt = codec.payloadType; |
| 132 if (codec.preferredPayloadType !== undefined) { |
| 133 pt = codec.preferredPayloadType; |
| 134 } |
| 135 return 'a=rtpmap:' + pt + ' ' + codec.name + '/' + codec.clockRate + |
| 136 (codec.numChannels !== 1 ? '/' + codec.numChannels : '') + '\r\n'; |
| 137 }; |
| 138 |
| 139 // Parses an a=extmap line (headerextension from RFC 5285). Sample input: |
| 140 // a=extmap:2 urn:ietf:params:rtp-hdrext:toffset |
| 141 SDPUtils.parseExtmap = function(line) { |
| 142 var parts = line.substr(9).split(' '); |
| 143 return { |
| 144 id: parseInt(parts[0], 10), |
| 145 uri: parts[1] |
| 146 }; |
| 147 }; |
| 148 |
| 149 // Generates a=extmap line from RTCRtpHeaderExtensionParameters or |
| 150 // RTCRtpHeaderExtension. |
| 151 SDPUtils.writeExtmap = function(headerExtension) { |
| 152 return 'a=extmap:' + (headerExtension.id || headerExtension.preferredId) + |
| 153 ' ' + headerExtension.uri + '\r\n'; |
| 154 }; |
| 155 |
| 156 // Parses an ftmp line, returns dictionary. Sample input: |
| 157 // a=fmtp:96 vbr=on;cng=on |
| 158 // Also deals with vbr=on; cng=on |
| 159 SDPUtils.parseFmtp = function(line) { |
| 160 var parsed = {}; |
| 161 var kv; |
| 162 var parts = line.substr(line.indexOf(' ') + 1).split(';'); |
| 163 for (var j = 0; j < parts.length; j++) { |
| 164 kv = parts[j].trim().split('='); |
| 165 parsed[kv[0].trim()] = kv[1]; |
| 166 } |
| 167 return parsed; |
| 168 }; |
| 169 |
| 170 // Generates an a=ftmp line from RTCRtpCodecCapability or RTCRtpCodecParameters. |
| 171 SDPUtils.writeFmtp = function(codec) { |
| 172 var line = ''; |
| 173 var pt = codec.payloadType; |
| 174 if (codec.preferredPayloadType !== undefined) { |
| 175 pt = codec.preferredPayloadType; |
| 176 } |
| 177 if (codec.parameters && Object.keys(codec.parameters).length) { |
| 178 var params = []; |
| 179 Object.keys(codec.parameters).forEach(function(param) { |
| 180 params.push(param + '=' + codec.parameters[param]); |
| 181 }); |
| 182 line += 'a=fmtp:' + pt + ' ' + params.join(';') + '\r\n'; |
| 183 } |
| 184 return line; |
| 185 }; |
| 186 |
| 187 // Parses an rtcp-fb line, returns RTCPRtcpFeedback object. Sample input: |
| 188 // a=rtcp-fb:98 nack rpsi |
| 189 SDPUtils.parseRtcpFb = function(line) { |
| 190 var parts = line.substr(line.indexOf(' ') + 1).split(' '); |
| 191 return { |
| 192 type: parts.shift(), |
| 193 parameter: parts.join(' ') |
| 194 }; |
| 195 }; |
| 196 // Generate a=rtcp-fb lines from RTCRtpCodecCapability or RTCRtpCodecParameters. |
| 197 SDPUtils.writeRtcpFb = function(codec) { |
| 198 var lines = ''; |
| 199 var pt = codec.payloadType; |
| 200 if (codec.preferredPayloadType !== undefined) { |
| 201 pt = codec.preferredPayloadType; |
| 202 } |
| 203 if (codec.rtcpFeedback && codec.rtcpFeedback.length) { |
| 204 // FIXME: special handling for trr-int? |
| 205 codec.rtcpFeedback.forEach(function(fb) { |
| 206 lines += 'a=rtcp-fb:' + pt + ' ' + fb.type + |
| 207 (fb.parameter && fb.parameter.length ? ' ' + fb.parameter : '') + |
| 208 '\r\n'; |
| 209 }); |
| 210 } |
| 211 return lines; |
| 212 }; |
| 213 |
| 214 // Parses an RFC 5576 ssrc media attribute. Sample input: |
| 215 // a=ssrc:3735928559 cname:something |
| 216 SDPUtils.parseSsrcMedia = function(line) { |
| 217 var sp = line.indexOf(' '); |
| 218 var parts = { |
| 219 ssrc: parseInt(line.substr(7, sp - 7), 10) |
| 220 }; |
| 221 var colon = line.indexOf(':', sp); |
| 222 if (colon > -1) { |
| 223 parts.attribute = line.substr(sp + 1, colon - sp - 1); |
| 224 parts.value = line.substr(colon + 1); |
| 225 } else { |
| 226 parts.attribute = line.substr(sp + 1); |
| 227 } |
| 228 return parts; |
| 229 }; |
| 230 |
| 231 // Extracts DTLS parameters from SDP media section or sessionpart. |
| 232 // FIXME: for consistency with other functions this should only |
| 233 // get the fingerprint line as input. See also getIceParameters. |
| 234 SDPUtils.getDtlsParameters = function(mediaSection, sessionpart) { |
| 235 var lines = SDPUtils.splitLines(mediaSection); |
| 236 // Search in session part, too. |
| 237 lines = lines.concat(SDPUtils.splitLines(sessionpart)); |
| 238 var fpLine = lines.filter(function(line) { |
| 239 return line.indexOf('a=fingerprint:') === 0; |
| 240 })[0].substr(14); |
| 241 // Note: a=setup line is ignored since we use the 'auto' role. |
| 242 var dtlsParameters = { |
| 243 role: 'auto', |
| 244 fingerprints: [{ |
| 245 algorithm: fpLine.split(' ')[0], |
| 246 value: fpLine.split(' ')[1] |
| 247 }] |
| 248 }; |
| 249 return dtlsParameters; |
| 250 }; |
| 251 |
| 252 // Serializes DTLS parameters to SDP. |
| 253 SDPUtils.writeDtlsParameters = function(params, setupType) { |
| 254 var sdp = 'a=setup:' + setupType + '\r\n'; |
| 255 params.fingerprints.forEach(function(fp) { |
| 256 sdp += 'a=fingerprint:' + fp.algorithm + ' ' + fp.value + '\r\n'; |
| 257 }); |
| 258 return sdp; |
| 259 }; |
| 260 // Parses ICE information from SDP media section or sessionpart. |
| 261 // FIXME: for consistency with other functions this should only |
| 262 // get the ice-ufrag and ice-pwd lines as input. |
| 263 SDPUtils.getIceParameters = function(mediaSection, sessionpart) { |
| 264 var lines = SDPUtils.splitLines(mediaSection); |
| 265 // Search in session part, too. |
| 266 lines = lines.concat(SDPUtils.splitLines(sessionpart)); |
| 267 var iceParameters = { |
| 268 usernameFragment: lines.filter(function(line) { |
| 269 return line.indexOf('a=ice-ufrag:') === 0; |
| 270 })[0].substr(12), |
| 271 password: lines.filter(function(line) { |
| 272 return line.indexOf('a=ice-pwd:') === 0; |
| 273 })[0].substr(10) |
| 274 }; |
| 275 return iceParameters; |
| 276 }; |
| 277 |
| 278 // Serializes ICE parameters to SDP. |
| 279 SDPUtils.writeIceParameters = function(params) { |
| 280 return 'a=ice-ufrag:' + params.usernameFragment + '\r\n' + |
| 281 'a=ice-pwd:' + params.password + '\r\n'; |
| 282 }; |
| 283 |
| 284 // Parses the SDP media section and returns RTCRtpParameters. |
| 285 SDPUtils.parseRtpParameters = function(mediaSection) { |
| 286 var description = { |
| 287 codecs: [], |
| 288 headerExtensions: [], |
| 289 fecMechanisms: [], |
| 290 rtcp: [] |
| 291 }; |
| 292 var lines = SDPUtils.splitLines(mediaSection); |
| 293 var mline = lines[0].split(' '); |
| 294 for (var i = 3; i < mline.length; i++) { // find all codecs from mline[3..] |
| 295 var pt = mline[i]; |
| 296 var rtpmapline = SDPUtils.matchPrefix( |
| 297 mediaSection, 'a=rtpmap:' + pt + ' ')[0]; |
| 298 if (rtpmapline) { |
| 299 var codec = SDPUtils.parseRtpMap(rtpmapline); |
| 300 var fmtps = SDPUtils.matchPrefix( |
| 301 mediaSection, 'a=fmtp:' + pt + ' '); |
| 302 // Only the first a=fmtp:<pt> is considered. |
| 303 codec.parameters = fmtps.length ? SDPUtils.parseFmtp(fmtps[0]) : {}; |
| 304 codec.rtcpFeedback = SDPUtils.matchPrefix( |
| 305 mediaSection, 'a=rtcp-fb:' + pt + ' ') |
| 306 .map(SDPUtils.parseRtcpFb); |
| 307 description.codecs.push(codec); |
| 308 // parse FEC mechanisms from rtpmap lines. |
| 309 switch (codec.name.toUpperCase()) { |
| 310 case 'RED': |
| 311 case 'ULPFEC': |
| 312 description.fecMechanisms.push(codec.name.toUpperCase()); |
| 313 break; |
| 314 default: // only RED and ULPFEC are recognized as FEC mechanisms. |
| 315 break; |
| 316 } |
| 317 } |
| 318 } |
| 319 SDPUtils.matchPrefix(mediaSection, 'a=extmap:').forEach(function(line) { |
| 320 description.headerExtensions.push(SDPUtils.parseExtmap(line)); |
| 321 }); |
| 322 // FIXME: parse rtcp. |
| 323 return description; |
| 324 }; |
| 325 |
| 326 // Generates parts of the SDP media section describing the capabilities / |
| 327 // parameters. |
| 328 SDPUtils.writeRtpDescription = function(kind, caps) { |
| 329 var sdp = ''; |
| 330 |
| 331 // Build the mline. |
| 332 sdp += 'm=' + kind + ' '; |
| 333 sdp += caps.codecs.length > 0 ? '9' : '0'; // reject if no codecs. |
| 334 sdp += ' UDP/TLS/RTP/SAVPF '; |
| 335 sdp += caps.codecs.map(function(codec) { |
| 336 if (codec.preferredPayloadType !== undefined) { |
| 337 return codec.preferredPayloadType; |
| 338 } |
| 339 return codec.payloadType; |
| 340 }).join(' ') + '\r\n'; |
| 341 |
| 342 sdp += 'c=IN IP4 0.0.0.0\r\n'; |
| 343 sdp += 'a=rtcp:9 IN IP4 0.0.0.0\r\n'; |
| 344 |
| 345 // Add a=rtpmap lines for each codec. Also fmtp and rtcp-fb. |
| 346 caps.codecs.forEach(function(codec) { |
| 347 sdp += SDPUtils.writeRtpMap(codec); |
| 348 sdp += SDPUtils.writeFmtp(codec); |
| 349 sdp += SDPUtils.writeRtcpFb(codec); |
| 350 }); |
| 351 var maxptime = 0; |
| 352 caps.codecs.forEach(function(codec) { |
| 353 if (codec.maxptime > maxptime) { |
| 354 maxptime = codec.maxptime; |
| 355 } |
| 356 }); |
| 357 if (maxptime > 0) { |
| 358 sdp += 'a=maxptime:' + maxptime + '\r\n'; |
| 359 } |
| 360 sdp += 'a=rtcp-mux\r\n'; |
| 361 |
| 362 caps.headerExtensions.forEach(function(extension) { |
| 363 sdp += SDPUtils.writeExtmap(extension); |
| 364 }); |
| 365 // FIXME: write fecMechanisms. |
| 366 return sdp; |
| 367 }; |
| 368 |
| 369 // Parses the SDP media section and returns an array of |
| 370 // RTCRtpEncodingParameters. |
| 371 SDPUtils.parseRtpEncodingParameters = function(mediaSection) { |
| 372 var encodingParameters = []; |
| 373 var description = SDPUtils.parseRtpParameters(mediaSection); |
| 374 var hasRed = description.fecMechanisms.indexOf('RED') !== -1; |
| 375 var hasUlpfec = description.fecMechanisms.indexOf('ULPFEC') !== -1; |
| 376 |
| 377 // filter a=ssrc:... cname:, ignore PlanB-msid |
| 378 var ssrcs = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:') |
| 379 .map(function(line) { |
| 380 return SDPUtils.parseSsrcMedia(line); |
| 381 }) |
| 382 .filter(function(parts) { |
| 383 return parts.attribute === 'cname'; |
| 384 }); |
| 385 var primarySsrc = ssrcs.length > 0 && ssrcs[0].ssrc; |
| 386 var secondarySsrc; |
| 387 |
| 388 var flows = SDPUtils.matchPrefix(mediaSection, 'a=ssrc-group:FID') |
| 389 .map(function(line) { |
| 390 var parts = line.split(' '); |
| 391 parts.shift(); |
| 392 return parts.map(function(part) { |
| 393 return parseInt(part, 10); |
| 394 }); |
| 395 }); |
| 396 if (flows.length > 0 && flows[0].length > 1 && flows[0][0] === primarySsrc) { |
| 397 secondarySsrc = flows[0][1]; |
| 398 } |
| 399 |
| 400 description.codecs.forEach(function(codec) { |
| 401 if (codec.name.toUpperCase() === 'RTX' && codec.parameters.apt) { |
| 402 var encParam = { |
| 403 ssrc: primarySsrc, |
| 404 codecPayloadType: parseInt(codec.parameters.apt, 10), |
| 405 rtx: { |
| 406 ssrc: secondarySsrc |
| 407 } |
| 408 }; |
| 409 encodingParameters.push(encParam); |
| 410 if (hasRed) { |
| 411 encParam = JSON.parse(JSON.stringify(encParam)); |
| 412 encParam.fec = { |
| 413 ssrc: secondarySsrc, |
| 414 mechanism: hasUlpfec ? 'red+ulpfec' : 'red' |
| 415 }; |
| 416 encodingParameters.push(encParam); |
| 417 } |
| 418 } |
| 419 }); |
| 420 if (encodingParameters.length === 0 && primarySsrc) { |
| 421 encodingParameters.push({ |
| 422 ssrc: primarySsrc |
| 423 }); |
| 424 } |
| 425 |
| 426 // we support both b=AS and b=TIAS but interpret AS as TIAS. |
| 427 var bandwidth = SDPUtils.matchPrefix(mediaSection, 'b='); |
| 428 if (bandwidth.length) { |
| 429 if (bandwidth[0].indexOf('b=TIAS:') === 0) { |
| 430 bandwidth = parseInt(bandwidth[0].substr(7), 10); |
| 431 } else if (bandwidth[0].indexOf('b=AS:') === 0) { |
| 432 bandwidth = parseInt(bandwidth[0].substr(5), 10); |
| 433 } |
| 434 encodingParameters.forEach(function(params) { |
| 435 params.maxBitrate = bandwidth; |
| 436 }); |
| 437 } |
| 438 return encodingParameters; |
| 439 }; |
| 440 |
| 441 // parses http://draft.ortc.org/#rtcrtcpparameters* |
| 442 SDPUtils.parseRtcpParameters = function(mediaSection) { |
| 443 var rtcpParameters = {}; |
| 444 |
| 445 var cname; |
| 446 // Gets the first SSRC. Note that with RTX there might be multiple |
| 447 // SSRCs. |
| 448 var remoteSsrc = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:') |
| 449 .map(function(line) { |
| 450 return SDPUtils.parseSsrcMedia(line); |
| 451 }) |
| 452 .filter(function(obj) { |
| 453 return obj.attribute === 'cname'; |
| 454 })[0]; |
| 455 if (remoteSsrc) { |
| 456 rtcpParameters.cname = remoteSsrc.value; |
| 457 rtcpParameters.ssrc = remoteSsrc.ssrc; |
| 458 } |
| 459 |
| 460 // Edge uses the compound attribute instead of reducedSize |
| 461 // compound is !reducedSize |
| 462 var rsize = SDPUtils.matchPrefix(mediaSection, 'a=rtcp-rsize'); |
| 463 rtcpParameters.reducedSize = rsize.length > 0; |
| 464 rtcpParameters.compound = rsize.length === 0; |
| 465 |
| 466 // parses the rtcp-mux attrіbute. |
| 467 // Note that Edge does not support unmuxed RTCP. |
| 468 var mux = SDPUtils.matchPrefix(mediaSection, 'a=rtcp-mux'); |
| 469 rtcpParameters.mux = mux.length > 0; |
| 470 |
| 471 return rtcpParameters; |
| 472 }; |
| 473 |
| 474 // parses either a=msid: or a=ssrc:... msid lines an returns |
| 475 // the id of the MediaStream and MediaStreamTrack. |
| 476 SDPUtils.parseMsid = function(mediaSection) { |
| 477 var parts; |
| 478 var spec = SDPUtils.matchPrefix(mediaSection, 'a=msid:'); |
| 479 if (spec.length === 1) { |
| 480 parts = spec[0].substr(7).split(' '); |
| 481 return {stream: parts[0], track: parts[1]}; |
| 482 } |
| 483 var planB = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:') |
| 484 .map(function(line) { |
| 485 return SDPUtils.parseSsrcMedia(line); |
| 486 }) |
| 487 .filter(function(parts) { |
| 488 return parts.attribute === 'msid'; |
| 489 }); |
| 490 if (planB.length > 0) { |
| 491 parts = planB[0].value.split(' '); |
| 492 return {stream: parts[0], track: parts[1]}; |
| 493 } |
| 494 }; |
| 495 |
| 496 SDPUtils.writeSessionBoilerplate = function() { |
| 497 // FIXME: sess-id should be an NTP timestamp. |
| 498 return 'v=0\r\n' + |
| 499 'o=thisisadapterortc 8169639915646943137 2 IN IP4 127.0.0.1\r\n' + |
| 500 's=-\r\n' + |
| 501 't=0 0\r\n'; |
| 502 }; |
| 503 |
| 504 SDPUtils.writeMediaSection = function(transceiver, caps, type, stream) { |
| 505 var sdp = SDPUtils.writeRtpDescription(transceiver.kind, caps); |
| 506 |
| 507 // Map ICE parameters (ufrag, pwd) to SDP. |
| 508 sdp += SDPUtils.writeIceParameters( |
| 509 transceiver.iceGatherer.getLocalParameters()); |
| 510 |
| 511 // Map DTLS parameters to SDP. |
| 512 sdp += SDPUtils.writeDtlsParameters( |
| 513 transceiver.dtlsTransport.getLocalParameters(), |
| 514 type === 'offer' ? 'actpass' : 'active'); |
| 515 |
| 516 sdp += 'a=mid:' + transceiver.mid + '\r\n'; |
| 517 |
| 518 if (transceiver.rtpSender && transceiver.rtpReceiver) { |
| 519 sdp += 'a=sendrecv\r\n'; |
| 520 } else if (transceiver.rtpSender) { |
| 521 sdp += 'a=sendonly\r\n'; |
| 522 } else if (transceiver.rtpReceiver) { |
| 523 sdp += 'a=recvonly\r\n'; |
| 524 } else { |
| 525 sdp += 'a=inactive\r\n'; |
| 526 } |
| 527 |
| 528 if (transceiver.rtpSender) { |
| 529 // spec. |
| 530 var msid = 'msid:' + stream.id + ' ' + |
| 531 transceiver.rtpSender.track.id + '\r\n'; |
| 532 sdp += 'a=' + msid; |
| 533 |
| 534 // for Chrome. |
| 535 sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc + |
| 536 ' ' + msid; |
| 537 if (transceiver.sendEncodingParameters[0].rtx) { |
| 538 sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc + |
| 539 ' ' + msid; |
| 540 sdp += 'a=ssrc-group:FID ' + |
| 541 transceiver.sendEncodingParameters[0].ssrc + ' ' + |
| 542 transceiver.sendEncodingParameters[0].rtx.ssrc + |
| 543 '\r\n'; |
| 544 } |
| 545 } |
| 546 // FIXME: this should be written by writeRtpDescription. |
| 547 sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc + |
| 548 ' cname:' + SDPUtils.localCName + '\r\n'; |
| 549 if (transceiver.rtpSender && transceiver.sendEncodingParameters[0].rtx) { |
| 550 sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc + |
| 551 ' cname:' + SDPUtils.localCName + '\r\n'; |
| 552 } |
| 553 return sdp; |
| 554 }; |
| 555 |
| 556 // Gets the direction from the mediaSection or the sessionpart. |
| 557 SDPUtils.getDirection = function(mediaSection, sessionpart) { |
| 558 // Look for sendrecv, sendonly, recvonly, inactive, default to sendrecv. |
| 559 var lines = SDPUtils.splitLines(mediaSection); |
| 560 for (var i = 0; i < lines.length; i++) { |
| 561 switch (lines[i]) { |
| 562 case 'a=sendrecv': |
| 563 case 'a=sendonly': |
| 564 case 'a=recvonly': |
| 565 case 'a=inactive': |
| 566 return lines[i].substr(2); |
| 567 default: |
| 568 // FIXME: What should happen here? |
| 569 } |
| 570 } |
| 571 if (sessionpart) { |
| 572 return SDPUtils.getDirection(sessionpart); |
| 573 } |
| 574 return 'sendrecv'; |
| 575 }; |
| 576 |
| 577 SDPUtils.getKind = function(mediaSection) { |
| 578 var lines = SDPUtils.splitLines(mediaSection); |
| 579 var mline = lines[0].split(' '); |
| 580 return mline[0].substr(2); |
| 581 }; |
| 582 |
| 583 SDPUtils.isRejected = function(mediaSection) { |
| 584 return mediaSection.split(' ', 2)[1] === '0'; |
| 585 }; |
| 586 |
| 587 // Expose public methods. |
| 588 module.exports = SDPUtils; |
| 589 |
| 590 },{}],2:[function(require,module,exports){ |
| 591 /* |
| 592 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 593 * |
| 594 * Use of this source code is governed by a BSD-style license |
| 595 * that can be found in the LICENSE file in the root of the source |
| 596 * tree. |
| 597 */ |
| 598 /* eslint-env node */ |
| 599 |
| 600 'use strict'; |
| 601 |
| 602 // Shimming starts here. |
| 603 (function() { |
| 604 // Utils. |
| 605 var utils = require('./utils'); |
| 606 var logging = utils.log; |
| 607 var browserDetails = utils.browserDetails; |
| 608 // Export to the adapter global object visible in the browser. |
| 609 module.exports.browserDetails = browserDetails; |
| 610 module.exports.extractVersion = utils.extractVersion; |
| 611 module.exports.disableLog = utils.disableLog; |
| 612 |
| 613 // Uncomment the line below if you want logging to occur, including logging |
| 614 // for the switch statement below. Can also be turned on in the browser via |
| 615 // adapter.disableLog(false), but then logging from the switch statement below |
| 616 // will not appear. |
| 617 // require('./utils').disableLog(false); |
| 618 |
| 619 // Browser shims. |
| 620 var chromeShim = require('./chrome/chrome_shim') || null; |
| 621 var edgeShim = require('./edge/edge_shim') || null; |
| 622 var firefoxShim = require('./firefox/firefox_shim') || null; |
| 623 var safariShim = require('./safari/safari_shim') || null; |
| 624 |
| 625 // Shim browser if found. |
| 626 switch (browserDetails.browser) { |
| 627 case 'chrome': |
| 628 if (!chromeShim || !chromeShim.shimPeerConnection) { |
| 629 logging('Chrome shim is not included in this adapter release.'); |
| 630 return; |
| 631 } |
| 632 logging('adapter.js shimming chrome.'); |
| 633 // Export to the adapter global object visible in the browser. |
| 634 module.exports.browserShim = chromeShim; |
| 635 |
| 636 chromeShim.shimGetUserMedia(); |
| 637 chromeShim.shimMediaStream(); |
| 638 utils.shimCreateObjectURL(); |
| 639 chromeShim.shimSourceObject(); |
| 640 chromeShim.shimPeerConnection(); |
| 641 chromeShim.shimOnTrack(); |
| 642 chromeShim.shimGetSendersWithDtmf(); |
| 643 break; |
| 644 case 'firefox': |
| 645 if (!firefoxShim || !firefoxShim.shimPeerConnection) { |
| 646 logging('Firefox shim is not included in this adapter release.'); |
| 647 return; |
| 648 } |
| 649 logging('adapter.js shimming firefox.'); |
| 650 // Export to the adapter global object visible in the browser. |
| 651 module.exports.browserShim = firefoxShim; |
| 652 |
| 653 firefoxShim.shimGetUserMedia(); |
| 654 utils.shimCreateObjectURL(); |
| 655 firefoxShim.shimSourceObject(); |
| 656 firefoxShim.shimPeerConnection(); |
| 657 firefoxShim.shimOnTrack(); |
| 658 break; |
| 659 case 'edge': |
| 660 if (!edgeShim || !edgeShim.shimPeerConnection) { |
| 661 logging('MS edge shim is not included in this adapter release.'); |
| 662 return; |
| 663 } |
| 664 logging('adapter.js shimming edge.'); |
| 665 // Export to the adapter global object visible in the browser. |
| 666 module.exports.browserShim = edgeShim; |
| 667 |
| 668 edgeShim.shimGetUserMedia(); |
| 669 utils.shimCreateObjectURL(); |
| 670 edgeShim.shimPeerConnection(); |
| 671 break; |
| 672 case 'safari': |
| 673 if (!safariShim) { |
| 674 logging('Safari shim is not included in this adapter release.'); |
| 675 return; |
| 676 } |
| 677 logging('adapter.js shimming safari.'); |
| 678 // Export to the adapter global object visible in the browser. |
| 679 module.exports.browserShim = safariShim; |
| 680 |
| 681 safariShim.shimGetUserMedia(); |
| 682 break; |
| 683 default: |
| 684 logging('Unsupported browser!'); |
| 685 } |
| 686 })(); |
| 687 |
| 688 },{"./chrome/chrome_shim":3,"./edge/edge_shim":5,"./firefox/firefox_shim":7,"./s
afari/safari_shim":9,"./utils":10}],3:[function(require,module,exports){ |
| 689 |
| 690 /* |
| 691 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 692 * |
| 693 * Use of this source code is governed by a BSD-style license |
| 694 * that can be found in the LICENSE file in the root of the source |
| 695 * tree. |
| 696 */ |
| 697 /* eslint-env node */ |
| 698 'use strict'; |
| 699 var logging = require('../utils.js').log; |
| 700 var browserDetails = require('../utils.js').browserDetails; |
| 701 |
| 702 var chromeShim = { |
| 703 shimMediaStream: function() { |
| 704 window.MediaStream = window.MediaStream || window.webkitMediaStream; |
| 705 }, |
| 706 |
| 707 shimOnTrack: function() { |
| 708 if (typeof window === 'object' && window.RTCPeerConnection && !('ontrack' in |
| 709 window.RTCPeerConnection.prototype)) { |
| 710 Object.defineProperty(window.RTCPeerConnection.prototype, 'ontrack', { |
| 711 get: function() { |
| 712 return this._ontrack; |
| 713 }, |
| 714 set: function(f) { |
| 715 var self = this; |
| 716 if (this._ontrack) { |
| 717 this.removeEventListener('track', this._ontrack); |
| 718 this.removeEventListener('addstream', this._ontrackpoly); |
| 719 } |
| 720 this.addEventListener('track', this._ontrack = f); |
| 721 this.addEventListener('addstream', this._ontrackpoly = function(e) { |
| 722 // onaddstream does not fire when a track is added to an existing |
| 723 // stream. But stream.onaddtrack is implemented so we use that. |
| 724 e.stream.addEventListener('addtrack', function(te) { |
| 725 var event = new Event('track'); |
| 726 event.track = te.track; |
| 727 event.receiver = {track: te.track}; |
| 728 event.streams = [e.stream]; |
| 729 self.dispatchEvent(event); |
| 730 }); |
| 731 e.stream.getTracks().forEach(function(track) { |
| 732 var event = new Event('track'); |
| 733 event.track = track; |
| 734 event.receiver = {track: track}; |
| 735 event.streams = [e.stream]; |
| 736 this.dispatchEvent(event); |
| 737 }.bind(this)); |
| 738 }.bind(this)); |
| 739 } |
| 740 }); |
| 741 } |
| 742 }, |
| 743 |
| 744 shimGetSendersWithDtmf: function() { |
| 745 if (typeof window === 'object' && window.RTCPeerConnection && |
| 746 !('getSenders' in RTCPeerConnection.prototype) && |
| 747 'createDTMFSender' in RTCPeerConnection.prototype) { |
| 748 RTCPeerConnection.prototype.getSenders = function() { |
| 749 return this._senders; |
| 750 }; |
| 751 var origAddStream = RTCPeerConnection.prototype.addStream; |
| 752 var origRemoveStream = RTCPeerConnection.prototype.removeStream; |
| 753 |
| 754 RTCPeerConnection.prototype.addStream = function(stream) { |
| 755 var pc = this; |
| 756 pc._senders = pc._senders || []; |
| 757 origAddStream.apply(pc, [stream]); |
| 758 stream.getTracks().forEach(function(track) { |
| 759 pc._senders.push({ |
| 760 track: track, |
| 761 get dtmf() { |
| 762 if (this._dtmf === undefined) { |
| 763 if (track.kind === 'audio') { |
| 764 this._dtmf = pc.createDTMFSender(track); |
| 765 } else { |
| 766 this._dtmf = null; |
| 767 } |
| 768 } |
| 769 return this._dtmf; |
| 770 } |
| 771 }); |
| 772 }); |
| 773 }; |
| 774 |
| 775 RTCPeerConnection.prototype.removeStream = function(stream) { |
| 776 var pc = this; |
| 777 pc._senders = pc._senders || []; |
| 778 origRemoveStream.apply(pc, [stream]); |
| 779 stream.getTracks().forEach(function(track) { |
| 780 var sender = pc._senders.find(function(s) { |
| 781 return s.track === track; |
| 782 }); |
| 783 if (sender) { |
| 784 pc._senders.splice(pc._senders.indexOf(sender), 1); // remove sender |
| 785 } |
| 786 }); |
| 787 }; |
| 788 } |
| 789 }, |
| 790 |
| 791 shimSourceObject: function() { |
| 792 if (typeof window === 'object') { |
| 793 if (window.HTMLMediaElement && |
| 794 !('srcObject' in window.HTMLMediaElement.prototype)) { |
| 795 // Shim the srcObject property, once, when HTMLMediaElement is found. |
| 796 Object.defineProperty(window.HTMLMediaElement.prototype, 'srcObject', { |
| 797 get: function() { |
| 798 return this._srcObject; |
| 799 }, |
| 800 set: function(stream) { |
| 801 var self = this; |
| 802 // Use _srcObject as a private property for this shim |
| 803 this._srcObject = stream; |
| 804 if (this.src) { |
| 805 URL.revokeObjectURL(this.src); |
| 806 } |
| 807 |
| 808 if (!stream) { |
| 809 this.src = ''; |
| 810 return undefined; |
| 811 } |
| 812 this.src = URL.createObjectURL(stream); |
| 813 // We need to recreate the blob url when a track is added or |
| 814 // removed. Doing it manually since we want to avoid a recursion. |
| 815 stream.addEventListener('addtrack', function() { |
| 816 if (self.src) { |
| 817 URL.revokeObjectURL(self.src); |
| 818 } |
| 819 self.src = URL.createObjectURL(stream); |
| 820 }); |
| 821 stream.addEventListener('removetrack', function() { |
| 822 if (self.src) { |
| 823 URL.revokeObjectURL(self.src); |
| 824 } |
| 825 self.src = URL.createObjectURL(stream); |
| 826 }); |
| 827 } |
| 828 }); |
| 829 } |
| 830 } |
| 831 }, |
| 832 |
| 833 shimPeerConnection: function() { |
| 834 // The RTCPeerConnection object. |
| 835 if (!window.RTCPeerConnection) { |
| 836 window.RTCPeerConnection = function(pcConfig, pcConstraints) { |
| 837 // Translate iceTransportPolicy to iceTransports, |
| 838 // see https://code.google.com/p/webrtc/issues/detail?id=4869 |
| 839 // this was fixed in M56 along with unprefixing RTCPeerConnection. |
| 840 logging('PeerConnection'); |
| 841 if (pcConfig && pcConfig.iceTransportPolicy) { |
| 842 pcConfig.iceTransports = pcConfig.iceTransportPolicy; |
| 843 } |
| 844 |
| 845 return new webkitRTCPeerConnection(pcConfig, pcConstraints); |
| 846 }; |
| 847 window.RTCPeerConnection.prototype = webkitRTCPeerConnection.prototype; |
| 848 // wrap static methods. Currently just generateCertificate. |
| 849 if (webkitRTCPeerConnection.generateCertificate) { |
| 850 Object.defineProperty(window.RTCPeerConnection, 'generateCertificate', { |
| 851 get: function() { |
| 852 return webkitRTCPeerConnection.generateCertificate; |
| 853 } |
| 854 }); |
| 855 } |
| 856 } |
| 857 |
| 858 var origGetStats = RTCPeerConnection.prototype.getStats; |
| 859 RTCPeerConnection.prototype.getStats = function(selector, |
| 860 successCallback, errorCallback) { |
| 861 var self = this; |
| 862 var args = arguments; |
| 863 |
| 864 // If selector is a function then we are in the old style stats so just |
| 865 // pass back the original getStats format to avoid breaking old users. |
| 866 if (arguments.length > 0 && typeof selector === 'function') { |
| 867 return origGetStats.apply(this, arguments); |
| 868 } |
| 869 |
| 870 // When spec-style getStats is supported, return those when called with |
| 871 // either no arguments or the selector argument is null. |
| 872 if (origGetStats.length === 0 && (arguments.length === 0 || |
| 873 typeof arguments[0] !== 'function')) { |
| 874 return origGetStats.apply(this, []); |
| 875 } |
| 876 |
| 877 var fixChromeStats_ = function(response) { |
| 878 var standardReport = {}; |
| 879 var reports = response.result(); |
| 880 reports.forEach(function(report) { |
| 881 var standardStats = { |
| 882 id: report.id, |
| 883 timestamp: report.timestamp, |
| 884 type: { |
| 885 localcandidate: 'local-candidate', |
| 886 remotecandidate: 'remote-candidate' |
| 887 }[report.type] || report.type |
| 888 }; |
| 889 report.names().forEach(function(name) { |
| 890 standardStats[name] = report.stat(name); |
| 891 }); |
| 892 standardReport[standardStats.id] = standardStats; |
| 893 }); |
| 894 |
| 895 return standardReport; |
| 896 }; |
| 897 |
| 898 // shim getStats with maplike support |
| 899 var makeMapStats = function(stats) { |
| 900 return new Map(Object.keys(stats).map(function(key) { |
| 901 return[key, stats[key]]; |
| 902 })); |
| 903 }; |
| 904 |
| 905 if (arguments.length >= 2) { |
| 906 var successCallbackWrapper_ = function(response) { |
| 907 args[1](makeMapStats(fixChromeStats_(response))); |
| 908 }; |
| 909 |
| 910 return origGetStats.apply(this, [successCallbackWrapper_, |
| 911 arguments[0]]); |
| 912 } |
| 913 |
| 914 // promise-support |
| 915 return new Promise(function(resolve, reject) { |
| 916 origGetStats.apply(self, [ |
| 917 function(response) { |
| 918 resolve(makeMapStats(fixChromeStats_(response))); |
| 919 }, reject]); |
| 920 }).then(successCallback, errorCallback); |
| 921 }; |
| 922 |
| 923 // add promise support -- natively available in Chrome 51 |
| 924 if (browserDetails.version < 51) { |
| 925 ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate'] |
| 926 .forEach(function(method) { |
| 927 var nativeMethod = RTCPeerConnection.prototype[method]; |
| 928 RTCPeerConnection.prototype[method] = function() { |
| 929 var args = arguments; |
| 930 var self = this; |
| 931 var promise = new Promise(function(resolve, reject) { |
| 932 nativeMethod.apply(self, [args[0], resolve, reject]); |
| 933 }); |
| 934 if (args.length < 2) { |
| 935 return promise; |
| 936 } |
| 937 return promise.then(function() { |
| 938 args[1].apply(null, []); |
| 939 }, |
| 940 function(err) { |
| 941 if (args.length >= 3) { |
| 942 args[2].apply(null, [err]); |
| 943 } |
| 944 }); |
| 945 }; |
| 946 }); |
| 947 } |
| 948 |
| 949 // promise support for createOffer and createAnswer. Available (without |
| 950 // bugs) since M52: crbug/619289 |
| 951 if (browserDetails.version < 52) { |
| 952 ['createOffer', 'createAnswer'].forEach(function(method) { |
| 953 var nativeMethod = RTCPeerConnection.prototype[method]; |
| 954 RTCPeerConnection.prototype[method] = function() { |
| 955 var self = this; |
| 956 if (arguments.length < 1 || (arguments.length === 1 && |
| 957 typeof arguments[0] === 'object')) { |
| 958 var opts = arguments.length === 1 ? arguments[0] : undefined; |
| 959 return new Promise(function(resolve, reject) { |
| 960 nativeMethod.apply(self, [resolve, reject, opts]); |
| 961 }); |
| 962 } |
| 963 return nativeMethod.apply(this, arguments); |
| 964 }; |
| 965 }); |
| 966 } |
| 967 |
| 968 // shim implicit creation of RTCSessionDescription/RTCIceCandidate |
| 969 ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate'] |
| 970 .forEach(function(method) { |
| 971 var nativeMethod = RTCPeerConnection.prototype[method]; |
| 972 RTCPeerConnection.prototype[method] = function() { |
| 973 arguments[0] = new ((method === 'addIceCandidate') ? |
| 974 RTCIceCandidate : RTCSessionDescription)(arguments[0]); |
| 975 return nativeMethod.apply(this, arguments); |
| 976 }; |
| 977 }); |
| 978 |
| 979 // support for addIceCandidate(null or undefined) |
| 980 var nativeAddIceCandidate = |
| 981 RTCPeerConnection.prototype.addIceCandidate; |
| 982 RTCPeerConnection.prototype.addIceCandidate = function() { |
| 983 if (!arguments[0]) { |
| 984 if (arguments[1]) { |
| 985 arguments[1].apply(null); |
| 986 } |
| 987 return Promise.resolve(); |
| 988 } |
| 989 return nativeAddIceCandidate.apply(this, arguments); |
| 990 }; |
| 991 } |
| 992 }; |
| 993 |
| 994 |
| 995 // Expose public methods. |
| 996 module.exports = { |
| 997 shimMediaStream: chromeShim.shimMediaStream, |
| 998 shimOnTrack: chromeShim.shimOnTrack, |
| 999 shimGetSendersWithDtmf: chromeShim.shimGetSendersWithDtmf, |
| 1000 shimSourceObject: chromeShim.shimSourceObject, |
| 1001 shimPeerConnection: chromeShim.shimPeerConnection, |
| 1002 shimGetUserMedia: require('./getusermedia') |
| 1003 }; |
| 1004 |
| 1005 },{"../utils.js":10,"./getusermedia":4}],4:[function(require,module,exports){ |
| 1006 /* |
| 1007 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 1008 * |
| 1009 * Use of this source code is governed by a BSD-style license |
| 1010 * that can be found in the LICENSE file in the root of the source |
| 1011 * tree. |
| 1012 */ |
| 1013 /* eslint-env node */ |
| 1014 'use strict'; |
| 1015 var logging = require('../utils.js').log; |
| 1016 var browserDetails = require('../utils.js').browserDetails; |
| 1017 |
| 1018 // Expose public methods. |
| 1019 module.exports = function() { |
| 1020 var constraintsToChrome_ = function(c) { |
| 1021 if (typeof c !== 'object' || c.mandatory || c.optional) { |
| 1022 return c; |
| 1023 } |
| 1024 var cc = {}; |
| 1025 Object.keys(c).forEach(function(key) { |
| 1026 if (key === 'require' || key === 'advanced' || key === 'mediaSource') { |
| 1027 return; |
| 1028 } |
| 1029 var r = (typeof c[key] === 'object') ? c[key] : {ideal: c[key]}; |
| 1030 if (r.exact !== undefined && typeof r.exact === 'number') { |
| 1031 r.min = r.max = r.exact; |
| 1032 } |
| 1033 var oldname_ = function(prefix, name) { |
| 1034 if (prefix) { |
| 1035 return prefix + name.charAt(0).toUpperCase() + name.slice(1); |
| 1036 } |
| 1037 return (name === 'deviceId') ? 'sourceId' : name; |
| 1038 }; |
| 1039 if (r.ideal !== undefined) { |
| 1040 cc.optional = cc.optional || []; |
| 1041 var oc = {}; |
| 1042 if (typeof r.ideal === 'number') { |
| 1043 oc[oldname_('min', key)] = r.ideal; |
| 1044 cc.optional.push(oc); |
| 1045 oc = {}; |
| 1046 oc[oldname_('max', key)] = r.ideal; |
| 1047 cc.optional.push(oc); |
| 1048 } else { |
| 1049 oc[oldname_('', key)] = r.ideal; |
| 1050 cc.optional.push(oc); |
| 1051 } |
| 1052 } |
| 1053 if (r.exact !== undefined && typeof r.exact !== 'number') { |
| 1054 cc.mandatory = cc.mandatory || {}; |
| 1055 cc.mandatory[oldname_('', key)] = r.exact; |
| 1056 } else { |
| 1057 ['min', 'max'].forEach(function(mix) { |
| 1058 if (r[mix] !== undefined) { |
| 1059 cc.mandatory = cc.mandatory || {}; |
| 1060 cc.mandatory[oldname_(mix, key)] = r[mix]; |
| 1061 } |
| 1062 }); |
| 1063 } |
| 1064 }); |
| 1065 if (c.advanced) { |
| 1066 cc.optional = (cc.optional || []).concat(c.advanced); |
| 1067 } |
| 1068 return cc; |
| 1069 }; |
| 1070 |
| 1071 var shimConstraints_ = function(constraints, func) { |
| 1072 constraints = JSON.parse(JSON.stringify(constraints)); |
| 1073 if (constraints && constraints.audio) { |
| 1074 constraints.audio = constraintsToChrome_(constraints.audio); |
| 1075 } |
| 1076 if (constraints && typeof constraints.video === 'object') { |
| 1077 // Shim facingMode for mobile, where it defaults to "user". |
| 1078 var face = constraints.video.facingMode; |
| 1079 face = face && ((typeof face === 'object') ? face : {ideal: face}); |
| 1080 var getSupportedFacingModeLies = browserDetails.version < 59; |
| 1081 |
| 1082 if ((face && (face.exact === 'user' || face.exact === 'environment' || |
| 1083 face.ideal === 'user' || face.ideal === 'environment')) && |
| 1084 !(navigator.mediaDevices.getSupportedConstraints && |
| 1085 navigator.mediaDevices.getSupportedConstraints().facingMode && |
| 1086 !getSupportedFacingModeLies)) { |
| 1087 delete constraints.video.facingMode; |
| 1088 if (face.exact === 'environment' || face.ideal === 'environment') { |
| 1089 // Look for "back" in label, or use last cam (typically back cam). |
| 1090 return navigator.mediaDevices.enumerateDevices() |
| 1091 .then(function(devices) { |
| 1092 devices = devices.filter(function(d) { |
| 1093 return d.kind === 'videoinput'; |
| 1094 }); |
| 1095 var back = devices.find(function(d) { |
| 1096 return d.label.toLowerCase().indexOf('back') !== -1; |
| 1097 }) || (devices.length && devices[devices.length - 1]); |
| 1098 if (back) { |
| 1099 constraints.video.deviceId = face.exact ? {exact: back.deviceId} : |
| 1100 {ideal: back.deviceId}; |
| 1101 } |
| 1102 constraints.video = constraintsToChrome_(constraints.video); |
| 1103 logging('chrome: ' + JSON.stringify(constraints)); |
| 1104 return func(constraints); |
| 1105 }); |
| 1106 } |
| 1107 } |
| 1108 constraints.video = constraintsToChrome_(constraints.video); |
| 1109 } |
| 1110 logging('chrome: ' + JSON.stringify(constraints)); |
| 1111 return func(constraints); |
| 1112 }; |
| 1113 |
| 1114 var shimError_ = function(e) { |
| 1115 return { |
| 1116 name: { |
| 1117 PermissionDeniedError: 'NotAllowedError', |
| 1118 ConstraintNotSatisfiedError: 'OverconstrainedError' |
| 1119 }[e.name] || e.name, |
| 1120 message: e.message, |
| 1121 constraint: e.constraintName, |
| 1122 toString: function() { |
| 1123 return this.name + (this.message && ': ') + this.message; |
| 1124 } |
| 1125 }; |
| 1126 }; |
| 1127 |
| 1128 var getUserMedia_ = function(constraints, onSuccess, onError) { |
| 1129 shimConstraints_(constraints, function(c) { |
| 1130 navigator.webkitGetUserMedia(c, onSuccess, function(e) { |
| 1131 onError(shimError_(e)); |
| 1132 }); |
| 1133 }); |
| 1134 }; |
| 1135 |
| 1136 navigator.getUserMedia = getUserMedia_; |
| 1137 |
| 1138 // Returns the result of getUserMedia as a Promise. |
| 1139 var getUserMediaPromise_ = function(constraints) { |
| 1140 return new Promise(function(resolve, reject) { |
| 1141 navigator.getUserMedia(constraints, resolve, reject); |
| 1142 }); |
| 1143 }; |
| 1144 |
| 1145 if (!navigator.mediaDevices) { |
| 1146 navigator.mediaDevices = { |
| 1147 getUserMedia: getUserMediaPromise_, |
| 1148 enumerateDevices: function() { |
| 1149 return new Promise(function(resolve) { |
| 1150 var kinds = {audio: 'audioinput', video: 'videoinput'}; |
| 1151 return MediaStreamTrack.getSources(function(devices) { |
| 1152 resolve(devices.map(function(device) { |
| 1153 return {label: device.label, |
| 1154 kind: kinds[device.kind], |
| 1155 deviceId: device.id, |
| 1156 groupId: ''}; |
| 1157 })); |
| 1158 }); |
| 1159 }); |
| 1160 }, |
| 1161 getSupportedConstraints: function() { |
| 1162 return { |
| 1163 deviceId: true, echoCancellation: true, facingMode: true, |
| 1164 frameRate: true, height: true, width: true |
| 1165 }; |
| 1166 } |
| 1167 }; |
| 1168 } |
| 1169 |
| 1170 // A shim for getUserMedia method on the mediaDevices object. |
| 1171 // TODO(KaptenJansson) remove once implemented in Chrome stable. |
| 1172 if (!navigator.mediaDevices.getUserMedia) { |
| 1173 navigator.mediaDevices.getUserMedia = function(constraints) { |
| 1174 return getUserMediaPromise_(constraints); |
| 1175 }; |
| 1176 } else { |
| 1177 // Even though Chrome 45 has navigator.mediaDevices and a getUserMedia |
| 1178 // function which returns a Promise, it does not accept spec-style |
| 1179 // constraints. |
| 1180 var origGetUserMedia = navigator.mediaDevices.getUserMedia. |
| 1181 bind(navigator.mediaDevices); |
| 1182 navigator.mediaDevices.getUserMedia = function(cs) { |
| 1183 return shimConstraints_(cs, function(c) { |
| 1184 return origGetUserMedia(c).then(function(stream) { |
| 1185 if (c.audio && !stream.getAudioTracks().length || |
| 1186 c.video && !stream.getVideoTracks().length) { |
| 1187 stream.getTracks().forEach(function(track) { |
| 1188 track.stop(); |
| 1189 }); |
| 1190 throw new DOMException('', 'NotFoundError'); |
| 1191 } |
| 1192 return stream; |
| 1193 }, function(e) { |
| 1194 return Promise.reject(shimError_(e)); |
| 1195 }); |
| 1196 }); |
| 1197 }; |
| 1198 } |
| 1199 |
| 1200 // Dummy devicechange event methods. |
| 1201 // TODO(KaptenJansson) remove once implemented in Chrome stable. |
| 1202 if (typeof navigator.mediaDevices.addEventListener === 'undefined') { |
| 1203 navigator.mediaDevices.addEventListener = function() { |
| 1204 logging('Dummy mediaDevices.addEventListener called.'); |
| 1205 }; |
| 1206 } |
| 1207 if (typeof navigator.mediaDevices.removeEventListener === 'undefined') { |
| 1208 navigator.mediaDevices.removeEventListener = function() { |
| 1209 logging('Dummy mediaDevices.removeEventListener called.'); |
| 1210 }; |
| 1211 } |
| 1212 }; |
| 1213 |
| 1214 },{"../utils.js":10}],5:[function(require,module,exports){ |
| 1215 /* |
| 1216 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 1217 * |
| 1218 * Use of this source code is governed by a BSD-style license |
| 1219 * that can be found in the LICENSE file in the root of the source |
| 1220 * tree. |
| 1221 */ |
| 1222 /* eslint-env node */ |
| 1223 'use strict'; |
| 1224 |
| 1225 var SDPUtils = require('sdp'); |
| 1226 var browserDetails = require('../utils').browserDetails; |
| 1227 |
| 1228 // sort tracks such that they follow an a-v-a-v... |
| 1229 // pattern. |
| 1230 function sortTracks(tracks) { |
| 1231 var audioTracks = tracks.filter(function(track) { |
| 1232 return track.kind === 'audio'; |
| 1233 }); |
| 1234 var videoTracks = tracks.filter(function(track) { |
| 1235 return track.kind === 'video'; |
| 1236 }); |
| 1237 tracks = []; |
| 1238 while (audioTracks.length || videoTracks.length) { |
| 1239 if (audioTracks.length) { |
| 1240 tracks.push(audioTracks.shift()); |
| 1241 } |
| 1242 if (videoTracks.length) { |
| 1243 tracks.push(videoTracks.shift()); |
| 1244 } |
| 1245 } |
| 1246 return tracks; |
| 1247 } |
| 1248 |
| 1249 // Edge does not like |
| 1250 // 1) stun: |
| 1251 // 2) turn: that does not have all of turn:host:port?transport=udp |
| 1252 // 3) turn: with ipv6 addresses |
| 1253 // 4) turn: occurring muliple times |
| 1254 function filterIceServers(iceServers) { |
| 1255 var hasTurn = false; |
| 1256 iceServers = JSON.parse(JSON.stringify(iceServers)); |
| 1257 return iceServers.filter(function(server) { |
| 1258 if (server && (server.urls || server.url)) { |
| 1259 var urls = server.urls || server.url; |
| 1260 var isString = typeof urls === 'string'; |
| 1261 if (isString) { |
| 1262 urls = [urls]; |
| 1263 } |
| 1264 urls = urls.filter(function(url) { |
| 1265 var validTurn = url.indexOf('turn:') === 0 && |
| 1266 url.indexOf('transport=udp') !== -1 && |
| 1267 url.indexOf('turn:[') === -1 && |
| 1268 !hasTurn; |
| 1269 |
| 1270 if (validTurn) { |
| 1271 hasTurn = true; |
| 1272 return true; |
| 1273 } |
| 1274 return url.indexOf('stun:') === 0 && |
| 1275 browserDetails.version >= 14393; |
| 1276 }); |
| 1277 |
| 1278 delete server.url; |
| 1279 server.urls = isString ? urls[0] : urls; |
| 1280 return !!urls.length; |
| 1281 } |
| 1282 return false; |
| 1283 }); |
| 1284 } |
| 1285 |
| 1286 var edgeShim = { |
| 1287 shimPeerConnection: function() { |
| 1288 if (window.RTCIceGatherer) { |
| 1289 // ORTC defines an RTCIceCandidate object but no constructor. |
| 1290 // Not implemented in Edge. |
| 1291 if (!window.RTCIceCandidate) { |
| 1292 window.RTCIceCandidate = function(args) { |
| 1293 return args; |
| 1294 }; |
| 1295 } |
| 1296 // ORTC does not have a session description object but |
| 1297 // other browsers (i.e. Chrome) that will support both PC and ORTC |
| 1298 // in the future might have this defined already. |
| 1299 if (!window.RTCSessionDescription) { |
| 1300 window.RTCSessionDescription = function(args) { |
| 1301 return args; |
| 1302 }; |
| 1303 } |
| 1304 // this adds an additional event listener to MediaStrackTrack that signals |
| 1305 // when a tracks enabled property was changed. Workaround for a bug in |
| 1306 // addStream, see below. No longer required in 15025+ |
| 1307 if (browserDetails.version < 15025) { |
| 1308 var origMSTEnabled = Object.getOwnPropertyDescriptor( |
| 1309 MediaStreamTrack.prototype, 'enabled'); |
| 1310 Object.defineProperty(MediaStreamTrack.prototype, 'enabled', { |
| 1311 set: function(value) { |
| 1312 origMSTEnabled.set.call(this, value); |
| 1313 var ev = new Event('enabled'); |
| 1314 ev.enabled = value; |
| 1315 this.dispatchEvent(ev); |
| 1316 } |
| 1317 }); |
| 1318 } |
| 1319 } |
| 1320 |
| 1321 window.RTCPeerConnection = function(config) { |
| 1322 var self = this; |
| 1323 |
| 1324 var _eventTarget = document.createDocumentFragment(); |
| 1325 ['addEventListener', 'removeEventListener', 'dispatchEvent'] |
| 1326 .forEach(function(method) { |
| 1327 self[method] = _eventTarget[method].bind(_eventTarget); |
| 1328 }); |
| 1329 |
| 1330 this.onicecandidate = null; |
| 1331 this.onaddstream = null; |
| 1332 this.ontrack = null; |
| 1333 this.onremovestream = null; |
| 1334 this.onsignalingstatechange = null; |
| 1335 this.oniceconnectionstatechange = null; |
| 1336 this.onicegatheringstatechange = null; |
| 1337 this.onnegotiationneeded = null; |
| 1338 this.ondatachannel = null; |
| 1339 |
| 1340 this.localStreams = []; |
| 1341 this.remoteStreams = []; |
| 1342 this.getLocalStreams = function() { |
| 1343 return self.localStreams; |
| 1344 }; |
| 1345 this.getRemoteStreams = function() { |
| 1346 return self.remoteStreams; |
| 1347 }; |
| 1348 |
| 1349 this.localDescription = new RTCSessionDescription({ |
| 1350 type: '', |
| 1351 sdp: '' |
| 1352 }); |
| 1353 this.remoteDescription = new RTCSessionDescription({ |
| 1354 type: '', |
| 1355 sdp: '' |
| 1356 }); |
| 1357 this.signalingState = 'stable'; |
| 1358 this.iceConnectionState = 'new'; |
| 1359 this.iceGatheringState = 'new'; |
| 1360 |
| 1361 this.iceOptions = { |
| 1362 gatherPolicy: 'all', |
| 1363 iceServers: [] |
| 1364 }; |
| 1365 if (config && config.iceTransportPolicy) { |
| 1366 switch (config.iceTransportPolicy) { |
| 1367 case 'all': |
| 1368 case 'relay': |
| 1369 this.iceOptions.gatherPolicy = config.iceTransportPolicy; |
| 1370 break; |
| 1371 default: |
| 1372 // don't set iceTransportPolicy. |
| 1373 break; |
| 1374 } |
| 1375 } |
| 1376 this.usingBundle = config && config.bundlePolicy === 'max-bundle'; |
| 1377 |
| 1378 if (config && config.iceServers) { |
| 1379 this.iceOptions.iceServers = filterIceServers(config.iceServers); |
| 1380 } |
| 1381 this._config = config; |
| 1382 |
| 1383 // per-track iceGathers, iceTransports, dtlsTransports, rtpSenders, ... |
| 1384 // everything that is needed to describe a SDP m-line. |
| 1385 this.transceivers = []; |
| 1386 |
| 1387 // since the iceGatherer is currently created in createOffer but we |
| 1388 // must not emit candidates until after setLocalDescription we buffer |
| 1389 // them in this array. |
| 1390 this._localIceCandidatesBuffer = []; |
| 1391 }; |
| 1392 |
| 1393 window.RTCPeerConnection.prototype._emitGatheringStateChange = function() { |
| 1394 var event = new Event('icegatheringstatechange'); |
| 1395 this.dispatchEvent(event); |
| 1396 if (this.onicegatheringstatechange !== null) { |
| 1397 this.onicegatheringstatechange(event); |
| 1398 } |
| 1399 }; |
| 1400 |
| 1401 window.RTCPeerConnection.prototype._emitBufferedCandidates = function() { |
| 1402 var self = this; |
| 1403 var sections = SDPUtils.splitSections(self.localDescription.sdp); |
| 1404 // FIXME: need to apply ice candidates in a way which is async but |
| 1405 // in-order |
| 1406 this._localIceCandidatesBuffer.forEach(function(event) { |
| 1407 var end = !event.candidate || Object.keys(event.candidate).length === 0; |
| 1408 if (end) { |
| 1409 for (var j = 1; j < sections.length; j++) { |
| 1410 if (sections[j].indexOf('\r\na=end-of-candidates\r\n') === -1) { |
| 1411 sections[j] += 'a=end-of-candidates\r\n'; |
| 1412 } |
| 1413 } |
| 1414 } else { |
| 1415 sections[event.candidate.sdpMLineIndex + 1] += |
| 1416 'a=' + event.candidate.candidate + '\r\n'; |
| 1417 } |
| 1418 self.localDescription.sdp = sections.join(''); |
| 1419 self.dispatchEvent(event); |
| 1420 if (self.onicecandidate !== null) { |
| 1421 self.onicecandidate(event); |
| 1422 } |
| 1423 if (!event.candidate && self.iceGatheringState !== 'complete') { |
| 1424 var complete = self.transceivers.every(function(transceiver) { |
| 1425 return transceiver.iceGatherer && |
| 1426 transceiver.iceGatherer.state === 'completed'; |
| 1427 }); |
| 1428 if (complete && self.iceGatheringStateChange !== 'complete') { |
| 1429 self.iceGatheringState = 'complete'; |
| 1430 self._emitGatheringStateChange(); |
| 1431 } |
| 1432 } |
| 1433 }); |
| 1434 this._localIceCandidatesBuffer = []; |
| 1435 }; |
| 1436 |
| 1437 window.RTCPeerConnection.prototype.getConfiguration = function() { |
| 1438 return this._config; |
| 1439 }; |
| 1440 |
| 1441 window.RTCPeerConnection.prototype.addStream = function(stream) { |
| 1442 if (browserDetails.version >= 15025) { |
| 1443 this.localStreams.push(stream); |
| 1444 } else { |
| 1445 // Clone is necessary for local demos mostly, attaching directly |
| 1446 // to two different senders does not work (build 10547). |
| 1447 // Fixed in 15025 (or earlier) |
| 1448 var clonedStream = stream.clone(); |
| 1449 stream.getTracks().forEach(function(track, idx) { |
| 1450 var clonedTrack = clonedStream.getTracks()[idx]; |
| 1451 track.addEventListener('enabled', function(event) { |
| 1452 clonedTrack.enabled = event.enabled; |
| 1453 }); |
| 1454 }); |
| 1455 this.localStreams.push(clonedStream); |
| 1456 } |
| 1457 this._maybeFireNegotiationNeeded(); |
| 1458 }; |
| 1459 |
| 1460 window.RTCPeerConnection.prototype.removeStream = function(stream) { |
| 1461 var idx = this.localStreams.indexOf(stream); |
| 1462 if (idx > -1) { |
| 1463 this.localStreams.splice(idx, 1); |
| 1464 this._maybeFireNegotiationNeeded(); |
| 1465 } |
| 1466 }; |
| 1467 |
| 1468 window.RTCPeerConnection.prototype.getSenders = function() { |
| 1469 return this.transceivers.filter(function(transceiver) { |
| 1470 return !!transceiver.rtpSender; |
| 1471 }) |
| 1472 .map(function(transceiver) { |
| 1473 return transceiver.rtpSender; |
| 1474 }); |
| 1475 }; |
| 1476 |
| 1477 window.RTCPeerConnection.prototype.getReceivers = function() { |
| 1478 return this.transceivers.filter(function(transceiver) { |
| 1479 return !!transceiver.rtpReceiver; |
| 1480 }) |
| 1481 .map(function(transceiver) { |
| 1482 return transceiver.rtpReceiver; |
| 1483 }); |
| 1484 }; |
| 1485 |
| 1486 // Determines the intersection of local and remote capabilities. |
| 1487 window.RTCPeerConnection.prototype._getCommonCapabilities = |
| 1488 function(localCapabilities, remoteCapabilities) { |
| 1489 var commonCapabilities = { |
| 1490 codecs: [], |
| 1491 headerExtensions: [], |
| 1492 fecMechanisms: [] |
| 1493 }; |
| 1494 |
| 1495 var findCodecByPayloadType = function(pt, codecs) { |
| 1496 pt = parseInt(pt, 10); |
| 1497 for (var i = 0; i < codecs.length; i++) { |
| 1498 if (codecs[i].payloadType === pt || |
| 1499 codecs[i].preferredPayloadType === pt) { |
| 1500 return codecs[i]; |
| 1501 } |
| 1502 } |
| 1503 }; |
| 1504 |
| 1505 var rtxCapabilityMatches = function(lRtx, rRtx, lCodecs, rCodecs) { |
| 1506 var lCodec = findCodecByPayloadType(lRtx.parameters.apt, lCodecs); |
| 1507 var rCodec = findCodecByPayloadType(rRtx.parameters.apt, rCodecs); |
| 1508 return lCodec && rCodec && |
| 1509 lCodec.name.toLowerCase() === rCodec.name.toLowerCase(); |
| 1510 }; |
| 1511 |
| 1512 localCapabilities.codecs.forEach(function(lCodec) { |
| 1513 for (var i = 0; i < remoteCapabilities.codecs.length; i++) { |
| 1514 var rCodec = remoteCapabilities.codecs[i]; |
| 1515 if (lCodec.name.toLowerCase() === rCodec.name.toLowerCase() && |
| 1516 lCodec.clockRate === rCodec.clockRate) { |
| 1517 if (lCodec.name.toLowerCase() === 'rtx' && |
| 1518 lCodec.parameters && rCodec.parameters.apt) { |
| 1519 // for RTX we need to find the local rtx that has a apt |
| 1520 // which points to the same local codec as the remote one. |
| 1521 if (!rtxCapabilityMatches(lCodec, rCodec, |
| 1522 localCapabilities.codecs, remoteCapabilities.codecs)) { |
| 1523 continue; |
| 1524 } |
| 1525 } |
| 1526 rCodec = JSON.parse(JSON.stringify(rCodec)); // deepcopy |
| 1527 // number of channels is the highest common number of channels |
| 1528 rCodec.numChannels = Math.min(lCodec.numChannels, |
| 1529 rCodec.numChannels); |
| 1530 // push rCodec so we reply with offerer payload type |
| 1531 commonCapabilities.codecs.push(rCodec); |
| 1532 |
| 1533 // determine common feedback mechanisms |
| 1534 rCodec.rtcpFeedback = rCodec.rtcpFeedback.filter(function(fb) { |
| 1535 for (var j = 0; j < lCodec.rtcpFeedback.length; j++) { |
| 1536 if (lCodec.rtcpFeedback[j].type === fb.type && |
| 1537 lCodec.rtcpFeedback[j].parameter === fb.parameter) { |
| 1538 return true; |
| 1539 } |
| 1540 } |
| 1541 return false; |
| 1542 }); |
| 1543 // FIXME: also need to determine .parameters |
| 1544 // see https://github.com/openpeer/ortc/issues/569 |
| 1545 break; |
| 1546 } |
| 1547 } |
| 1548 }); |
| 1549 |
| 1550 localCapabilities.headerExtensions |
| 1551 .forEach(function(lHeaderExtension) { |
| 1552 for (var i = 0; i < remoteCapabilities.headerExtensions.length; |
| 1553 i++) { |
| 1554 var rHeaderExtension = remoteCapabilities.headerExtensions[i]; |
| 1555 if (lHeaderExtension.uri === rHeaderExtension.uri) { |
| 1556 commonCapabilities.headerExtensions.push(rHeaderExtension); |
| 1557 break; |
| 1558 } |
| 1559 } |
| 1560 }); |
| 1561 |
| 1562 // FIXME: fecMechanisms |
| 1563 return commonCapabilities; |
| 1564 }; |
| 1565 |
| 1566 // Create ICE gatherer, ICE transport and DTLS transport. |
| 1567 window.RTCPeerConnection.prototype._createIceAndDtlsTransports = |
| 1568 function(mid, sdpMLineIndex) { |
| 1569 var self = this; |
| 1570 var iceGatherer = new RTCIceGatherer(self.iceOptions); |
| 1571 var iceTransport = new RTCIceTransport(iceGatherer); |
| 1572 iceGatherer.onlocalcandidate = function(evt) { |
| 1573 var event = new Event('icecandidate'); |
| 1574 event.candidate = {sdpMid: mid, sdpMLineIndex: sdpMLineIndex}; |
| 1575 |
| 1576 var cand = evt.candidate; |
| 1577 var end = !cand || Object.keys(cand).length === 0; |
| 1578 // Edge emits an empty object for RTCIceCandidateComplete‥ |
| 1579 if (end) { |
| 1580 // polyfill since RTCIceGatherer.state is not implemented in |
| 1581 // Edge 10547 yet. |
| 1582 if (iceGatherer.state === undefined) { |
| 1583 iceGatherer.state = 'completed'; |
| 1584 } |
| 1585 } else { |
| 1586 // RTCIceCandidate doesn't have a component, needs to be added |
| 1587 cand.component = iceTransport.component === 'RTCP' ? 2 : 1; |
| 1588 event.candidate.candidate = SDPUtils.writeCandidate(cand); |
| 1589 } |
| 1590 |
| 1591 // update local description. |
| 1592 var sections = SDPUtils.splitSections(self.localDescription.sdp); |
| 1593 if (!end) { |
| 1594 sections[event.candidate.sdpMLineIndex + 1] += |
| 1595 'a=' + event.candidate.candidate + '\r\n'; |
| 1596 } else { |
| 1597 sections[event.candidate.sdpMLineIndex + 1] += |
| 1598 'a=end-of-candidates\r\n'; |
| 1599 } |
| 1600 self.localDescription.sdp = sections.join(''); |
| 1601 var transceivers = self._pendingOffer ? self._pendingOffer : |
| 1602 self.transceivers; |
| 1603 var complete = transceivers.every(function(transceiver) { |
| 1604 return transceiver.iceGatherer && |
| 1605 transceiver.iceGatherer.state === 'completed'; |
| 1606 }); |
| 1607 |
| 1608 // Emit candidate if localDescription is set. |
| 1609 // Also emits null candidate when all gatherers are complete. |
| 1610 switch (self.iceGatheringState) { |
| 1611 case 'new': |
| 1612 if (!end) { |
| 1613 self._localIceCandidatesBuffer.push(event); |
| 1614 } |
| 1615 if (end && complete) { |
| 1616 self._localIceCandidatesBuffer.push( |
| 1617 new Event('icecandidate')); |
| 1618 } |
| 1619 break; |
| 1620 case 'gathering': |
| 1621 self._emitBufferedCandidates(); |
| 1622 if (!end) { |
| 1623 self.dispatchEvent(event); |
| 1624 if (self.onicecandidate !== null) { |
| 1625 self.onicecandidate(event); |
| 1626 } |
| 1627 } |
| 1628 if (complete) { |
| 1629 self.dispatchEvent(new Event('icecandidate')); |
| 1630 if (self.onicecandidate !== null) { |
| 1631 self.onicecandidate(new Event('icecandidate')); |
| 1632 } |
| 1633 self.iceGatheringState = 'complete'; |
| 1634 self._emitGatheringStateChange(); |
| 1635 } |
| 1636 break; |
| 1637 case 'complete': |
| 1638 // should not happen... currently! |
| 1639 break; |
| 1640 default: // no-op. |
| 1641 break; |
| 1642 } |
| 1643 }; |
| 1644 iceTransport.onicestatechange = function() { |
| 1645 self._updateConnectionState(); |
| 1646 }; |
| 1647 |
| 1648 var dtlsTransport = new RTCDtlsTransport(iceTransport); |
| 1649 dtlsTransport.ondtlsstatechange = function() { |
| 1650 self._updateConnectionState(); |
| 1651 }; |
| 1652 dtlsTransport.onerror = function() { |
| 1653 // onerror does not set state to failed by itself. |
| 1654 dtlsTransport.state = 'failed'; |
| 1655 self._updateConnectionState(); |
| 1656 }; |
| 1657 |
| 1658 return { |
| 1659 iceGatherer: iceGatherer, |
| 1660 iceTransport: iceTransport, |
| 1661 dtlsTransport: dtlsTransport |
| 1662 }; |
| 1663 }; |
| 1664 |
| 1665 // Start the RTP Sender and Receiver for a transceiver. |
| 1666 window.RTCPeerConnection.prototype._transceive = function(transceiver, |
| 1667 send, recv) { |
| 1668 var params = this._getCommonCapabilities(transceiver.localCapabilities, |
| 1669 transceiver.remoteCapabilities); |
| 1670 if (send && transceiver.rtpSender) { |
| 1671 params.encodings = transceiver.sendEncodingParameters; |
| 1672 params.rtcp = { |
| 1673 cname: SDPUtils.localCName |
| 1674 }; |
| 1675 if (transceiver.recvEncodingParameters.length) { |
| 1676 params.rtcp.ssrc = transceiver.recvEncodingParameters[0].ssrc; |
| 1677 } |
| 1678 transceiver.rtpSender.send(params); |
| 1679 } |
| 1680 if (recv && transceiver.rtpReceiver) { |
| 1681 // remove RTX field in Edge 14942 |
| 1682 if (transceiver.kind === 'video' |
| 1683 && transceiver.recvEncodingParameters |
| 1684 && browserDetails.version < 15019) { |
| 1685 transceiver.recvEncodingParameters.forEach(function(p) { |
| 1686 delete p.rtx; |
| 1687 }); |
| 1688 } |
| 1689 params.encodings = transceiver.recvEncodingParameters; |
| 1690 params.rtcp = { |
| 1691 cname: transceiver.cname |
| 1692 }; |
| 1693 if (transceiver.sendEncodingParameters.length) { |
| 1694 params.rtcp.ssrc = transceiver.sendEncodingParameters[0].ssrc; |
| 1695 } |
| 1696 transceiver.rtpReceiver.receive(params); |
| 1697 } |
| 1698 }; |
| 1699 |
| 1700 window.RTCPeerConnection.prototype.setLocalDescription = |
| 1701 function(description) { |
| 1702 var self = this; |
| 1703 var sections; |
| 1704 var sessionpart; |
| 1705 if (description.type === 'offer') { |
| 1706 // FIXME: What was the purpose of this empty if statement? |
| 1707 // if (!this._pendingOffer) { |
| 1708 // } else { |
| 1709 if (this._pendingOffer) { |
| 1710 // VERY limited support for SDP munging. Limited to: |
| 1711 // * changing the order of codecs |
| 1712 sections = SDPUtils.splitSections(description.sdp); |
| 1713 sessionpart = sections.shift(); |
| 1714 sections.forEach(function(mediaSection, sdpMLineIndex) { |
| 1715 var caps = SDPUtils.parseRtpParameters(mediaSection); |
| 1716 self._pendingOffer[sdpMLineIndex].localCapabilities = caps; |
| 1717 }); |
| 1718 this.transceivers = this._pendingOffer; |
| 1719 delete this._pendingOffer; |
| 1720 } |
| 1721 } else if (description.type === 'answer') { |
| 1722 sections = SDPUtils.splitSections(self.remoteDescription.sdp); |
| 1723 sessionpart = sections.shift(); |
| 1724 var isIceLite = SDPUtils.matchPrefix(sessionpart, |
| 1725 'a=ice-lite').length > 0; |
| 1726 sections.forEach(function(mediaSection, sdpMLineIndex) { |
| 1727 var transceiver = self.transceivers[sdpMLineIndex]; |
| 1728 var iceGatherer = transceiver.iceGatherer; |
| 1729 var iceTransport = transceiver.iceTransport; |
| 1730 var dtlsTransport = transceiver.dtlsTransport; |
| 1731 var localCapabilities = transceiver.localCapabilities; |
| 1732 var remoteCapabilities = transceiver.remoteCapabilities; |
| 1733 |
| 1734 var rejected = mediaSection.split('\n', 1)[0] |
| 1735 .split(' ', 2)[1] === '0'; |
| 1736 |
| 1737 if (!rejected && !transceiver.isDatachannel) { |
| 1738 var remoteIceParameters = SDPUtils.getIceParameters( |
| 1739 mediaSection, sessionpart); |
| 1740 var remoteDtlsParameters = SDPUtils.getDtlsParameters( |
| 1741 mediaSection, sessionpart); |
| 1742 if (isIceLite) { |
| 1743 remoteDtlsParameters.role = 'server'; |
| 1744 } |
| 1745 |
| 1746 if (!self.usingBundle || sdpMLineIndex === 0) { |
| 1747 iceTransport.start(iceGatherer, remoteIceParameters, |
| 1748 isIceLite ? 'controlling' : 'controlled'); |
| 1749 dtlsTransport.start(remoteDtlsParameters); |
| 1750 } |
| 1751 |
| 1752 // Calculate intersection of capabilities. |
| 1753 var params = self._getCommonCapabilities(localCapabilities, |
| 1754 remoteCapabilities); |
| 1755 |
| 1756 // Start the RTCRtpSender. The RTCRtpReceiver for this |
| 1757 // transceiver has already been started in setRemoteDescription. |
| 1758 self._transceive(transceiver, |
| 1759 params.codecs.length > 0, |
| 1760 false); |
| 1761 } |
| 1762 }); |
| 1763 } |
| 1764 |
| 1765 this.localDescription = { |
| 1766 type: description.type, |
| 1767 sdp: description.sdp |
| 1768 }; |
| 1769 switch (description.type) { |
| 1770 case 'offer': |
| 1771 this._updateSignalingState('have-local-offer'); |
| 1772 break; |
| 1773 case 'answer': |
| 1774 this._updateSignalingState('stable'); |
| 1775 break; |
| 1776 default: |
| 1777 throw new TypeError('unsupported type "' + description.type + |
| 1778 '"'); |
| 1779 } |
| 1780 |
| 1781 // If a success callback was provided, emit ICE candidates after it |
| 1782 // has been executed. Otherwise, emit callback after the Promise is |
| 1783 // resolved. |
| 1784 var hasCallback = arguments.length > 1 && |
| 1785 typeof arguments[1] === 'function'; |
| 1786 if (hasCallback) { |
| 1787 var cb = arguments[1]; |
| 1788 window.setTimeout(function() { |
| 1789 cb(); |
| 1790 if (self.iceGatheringState === 'new') { |
| 1791 self.iceGatheringState = 'gathering'; |
| 1792 self._emitGatheringStateChange(); |
| 1793 } |
| 1794 self._emitBufferedCandidates(); |
| 1795 }, 0); |
| 1796 } |
| 1797 var p = Promise.resolve(); |
| 1798 p.then(function() { |
| 1799 if (!hasCallback) { |
| 1800 if (self.iceGatheringState === 'new') { |
| 1801 self.iceGatheringState = 'gathering'; |
| 1802 self._emitGatheringStateChange(); |
| 1803 } |
| 1804 // Usually candidates will be emitted earlier. |
| 1805 window.setTimeout(self._emitBufferedCandidates.bind(self), 500); |
| 1806 } |
| 1807 }); |
| 1808 return p; |
| 1809 }; |
| 1810 |
| 1811 window.RTCPeerConnection.prototype.setRemoteDescription = |
| 1812 function(description) { |
| 1813 var self = this; |
| 1814 var stream = new MediaStream(); |
| 1815 var receiverList = []; |
| 1816 var sections = SDPUtils.splitSections(description.sdp); |
| 1817 var sessionpart = sections.shift(); |
| 1818 var isIceLite = SDPUtils.matchPrefix(sessionpart, |
| 1819 'a=ice-lite').length > 0; |
| 1820 this.usingBundle = SDPUtils.matchPrefix(sessionpart, |
| 1821 'a=group:BUNDLE ').length > 0; |
| 1822 sections.forEach(function(mediaSection, sdpMLineIndex) { |
| 1823 var lines = SDPUtils.splitLines(mediaSection); |
| 1824 var mline = lines[0].substr(2).split(' '); |
| 1825 var kind = mline[0]; |
| 1826 var rejected = mline[1] === '0'; |
| 1827 var direction = SDPUtils.getDirection(mediaSection, sessionpart); |
| 1828 |
| 1829 var mid = SDPUtils.matchPrefix(mediaSection, 'a=mid:'); |
| 1830 if (mid.length) { |
| 1831 mid = mid[0].substr(6); |
| 1832 } else { |
| 1833 mid = SDPUtils.generateIdentifier(); |
| 1834 } |
| 1835 |
| 1836 // Reject datachannels which are not implemented yet. |
| 1837 if (kind === 'application' && mline[2] === 'DTLS/SCTP') { |
| 1838 self.transceivers[sdpMLineIndex] = { |
| 1839 mid: mid, |
| 1840 isDatachannel: true |
| 1841 }; |
| 1842 return; |
| 1843 } |
| 1844 |
| 1845 var transceiver; |
| 1846 var iceGatherer; |
| 1847 var iceTransport; |
| 1848 var dtlsTransport; |
| 1849 var rtpSender; |
| 1850 var rtpReceiver; |
| 1851 var sendEncodingParameters; |
| 1852 var recvEncodingParameters; |
| 1853 var localCapabilities; |
| 1854 |
| 1855 var track; |
| 1856 // FIXME: ensure the mediaSection has rtcp-mux set. |
| 1857 var remoteCapabilities = SDPUtils.parseRtpParameters(mediaSection); |
| 1858 var remoteIceParameters; |
| 1859 var remoteDtlsParameters; |
| 1860 if (!rejected) { |
| 1861 remoteIceParameters = SDPUtils.getIceParameters(mediaSection, |
| 1862 sessionpart); |
| 1863 remoteDtlsParameters = SDPUtils.getDtlsParameters(mediaSection, |
| 1864 sessionpart); |
| 1865 remoteDtlsParameters.role = 'client'; |
| 1866 } |
| 1867 recvEncodingParameters = |
| 1868 SDPUtils.parseRtpEncodingParameters(mediaSection); |
| 1869 |
| 1870 var cname; |
| 1871 // Gets the first SSRC. Note that with RTX there might be multiple |
| 1872 // SSRCs. |
| 1873 var remoteSsrc = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:') |
| 1874 .map(function(line) { |
| 1875 return SDPUtils.parseSsrcMedia(line); |
| 1876 }) |
| 1877 .filter(function(obj) { |
| 1878 return obj.attribute === 'cname'; |
| 1879 })[0]; |
| 1880 if (remoteSsrc) { |
| 1881 cname = remoteSsrc.value; |
| 1882 } |
| 1883 |
| 1884 var isComplete = SDPUtils.matchPrefix(mediaSection, |
| 1885 'a=end-of-candidates', sessionpart).length > 0; |
| 1886 var cands = SDPUtils.matchPrefix(mediaSection, 'a=candidate:') |
| 1887 .map(function(cand) { |
| 1888 return SDPUtils.parseCandidate(cand); |
| 1889 }) |
| 1890 .filter(function(cand) { |
| 1891 return cand.component === '1'; |
| 1892 }); |
| 1893 if (description.type === 'offer' && !rejected) { |
| 1894 var transports = self.usingBundle && sdpMLineIndex > 0 ? { |
| 1895 iceGatherer: self.transceivers[0].iceGatherer, |
| 1896 iceTransport: self.transceivers[0].iceTransport, |
| 1897 dtlsTransport: self.transceivers[0].dtlsTransport |
| 1898 } : self._createIceAndDtlsTransports(mid, sdpMLineIndex); |
| 1899 |
| 1900 if (isComplete && (!self.usingBundle || sdpMLineIndex === 0)) { |
| 1901 transports.iceTransport.setRemoteCandidates(cands); |
| 1902 } |
| 1903 |
| 1904 localCapabilities = RTCRtpReceiver.getCapabilities(kind); |
| 1905 |
| 1906 // filter RTX until additional stuff needed for RTX is implemented |
| 1907 // in adapter.js |
| 1908 if (browserDetails.version < 15019) { |
| 1909 localCapabilities.codecs = localCapabilities.codecs.filter( |
| 1910 function(codec) { |
| 1911 return codec.name !== 'rtx'; |
| 1912 }); |
| 1913 } |
| 1914 |
| 1915 sendEncodingParameters = [{ |
| 1916 ssrc: (2 * sdpMLineIndex + 2) * 1001 |
| 1917 }]; |
| 1918 |
| 1919 if (direction === 'sendrecv' || direction === 'sendonly') { |
| 1920 rtpReceiver = new RTCRtpReceiver(transports.dtlsTransport, |
| 1921 kind); |
| 1922 |
| 1923 track = rtpReceiver.track; |
| 1924 receiverList.push([track, rtpReceiver]); |
| 1925 // FIXME: not correct when there are multiple streams but that |
| 1926 // is not currently supported in this shim. |
| 1927 stream.addTrack(track); |
| 1928 } |
| 1929 |
| 1930 // FIXME: look at direction. |
| 1931 if (self.localStreams.length > 0 && |
| 1932 self.localStreams[0].getTracks().length >= sdpMLineIndex) { |
| 1933 var localTrack; |
| 1934 if (kind === 'audio') { |
| 1935 localTrack = self.localStreams[0].getAudioTracks()[0]; |
| 1936 } else if (kind === 'video') { |
| 1937 localTrack = self.localStreams[0].getVideoTracks()[0]; |
| 1938 } |
| 1939 if (localTrack) { |
| 1940 // add RTX |
| 1941 if (browserDetails.version >= 15019 && kind === 'video') { |
| 1942 sendEncodingParameters[0].rtx = { |
| 1943 ssrc: (2 * sdpMLineIndex + 2) * 1001 + 1 |
| 1944 }; |
| 1945 } |
| 1946 rtpSender = new RTCRtpSender(localTrack, |
| 1947 transports.dtlsTransport); |
| 1948 } |
| 1949 } |
| 1950 |
| 1951 self.transceivers[sdpMLineIndex] = { |
| 1952 iceGatherer: transports.iceGatherer, |
| 1953 iceTransport: transports.iceTransport, |
| 1954 dtlsTransport: transports.dtlsTransport, |
| 1955 localCapabilities: localCapabilities, |
| 1956 remoteCapabilities: remoteCapabilities, |
| 1957 rtpSender: rtpSender, |
| 1958 rtpReceiver: rtpReceiver, |
| 1959 kind: kind, |
| 1960 mid: mid, |
| 1961 cname: cname, |
| 1962 sendEncodingParameters: sendEncodingParameters, |
| 1963 recvEncodingParameters: recvEncodingParameters |
| 1964 }; |
| 1965 // Start the RTCRtpReceiver now. The RTPSender is started in |
| 1966 // setLocalDescription. |
| 1967 self._transceive(self.transceivers[sdpMLineIndex], |
| 1968 false, |
| 1969 direction === 'sendrecv' || direction === 'sendonly'); |
| 1970 } else if (description.type === 'answer' && !rejected) { |
| 1971 transceiver = self.transceivers[sdpMLineIndex]; |
| 1972 iceGatherer = transceiver.iceGatherer; |
| 1973 iceTransport = transceiver.iceTransport; |
| 1974 dtlsTransport = transceiver.dtlsTransport; |
| 1975 rtpSender = transceiver.rtpSender; |
| 1976 rtpReceiver = transceiver.rtpReceiver; |
| 1977 sendEncodingParameters = transceiver.sendEncodingParameters; |
| 1978 localCapabilities = transceiver.localCapabilities; |
| 1979 |
| 1980 self.transceivers[sdpMLineIndex].recvEncodingParameters = |
| 1981 recvEncodingParameters; |
| 1982 self.transceivers[sdpMLineIndex].remoteCapabilities = |
| 1983 remoteCapabilities; |
| 1984 self.transceivers[sdpMLineIndex].cname = cname; |
| 1985 |
| 1986 if ((isIceLite || isComplete) && cands.length) { |
| 1987 iceTransport.setRemoteCandidates(cands); |
| 1988 } |
| 1989 if (!self.usingBundle || sdpMLineIndex === 0) { |
| 1990 iceTransport.start(iceGatherer, remoteIceParameters, |
| 1991 'controlling'); |
| 1992 dtlsTransport.start(remoteDtlsParameters); |
| 1993 } |
| 1994 |
| 1995 self._transceive(transceiver, |
| 1996 direction === 'sendrecv' || direction === 'recvonly', |
| 1997 direction === 'sendrecv' || direction === 'sendonly'); |
| 1998 |
| 1999 if (rtpReceiver && |
| 2000 (direction === 'sendrecv' || direction === 'sendonly')) { |
| 2001 track = rtpReceiver.track; |
| 2002 receiverList.push([track, rtpReceiver]); |
| 2003 stream.addTrack(track); |
| 2004 } else { |
| 2005 // FIXME: actually the receiver should be created later. |
| 2006 delete transceiver.rtpReceiver; |
| 2007 } |
| 2008 } |
| 2009 }); |
| 2010 |
| 2011 this.remoteDescription = { |
| 2012 type: description.type, |
| 2013 sdp: description.sdp |
| 2014 }; |
| 2015 switch (description.type) { |
| 2016 case 'offer': |
| 2017 this._updateSignalingState('have-remote-offer'); |
| 2018 break; |
| 2019 case 'answer': |
| 2020 this._updateSignalingState('stable'); |
| 2021 break; |
| 2022 default: |
| 2023 throw new TypeError('unsupported type "' + description.type + |
| 2024 '"'); |
| 2025 } |
| 2026 if (stream.getTracks().length) { |
| 2027 self.remoteStreams.push(stream); |
| 2028 window.setTimeout(function() { |
| 2029 var event = new Event('addstream'); |
| 2030 event.stream = stream; |
| 2031 self.dispatchEvent(event); |
| 2032 if (self.onaddstream !== null) { |
| 2033 window.setTimeout(function() { |
| 2034 self.onaddstream(event); |
| 2035 }, 0); |
| 2036 } |
| 2037 |
| 2038 receiverList.forEach(function(item) { |
| 2039 var track = item[0]; |
| 2040 var receiver = item[1]; |
| 2041 var trackEvent = new Event('track'); |
| 2042 trackEvent.track = track; |
| 2043 trackEvent.receiver = receiver; |
| 2044 trackEvent.streams = [stream]; |
| 2045 self.dispatchEvent(trackEvent); |
| 2046 if (self.ontrack !== null) { |
| 2047 window.setTimeout(function() { |
| 2048 self.ontrack(trackEvent); |
| 2049 }, 0); |
| 2050 } |
| 2051 }); |
| 2052 }, 0); |
| 2053 } |
| 2054 if (arguments.length > 1 && typeof arguments[1] === 'function') { |
| 2055 window.setTimeout(arguments[1], 0); |
| 2056 } |
| 2057 return Promise.resolve(); |
| 2058 }; |
| 2059 |
| 2060 window.RTCPeerConnection.prototype.close = function() { |
| 2061 this.transceivers.forEach(function(transceiver) { |
| 2062 /* not yet |
| 2063 if (transceiver.iceGatherer) { |
| 2064 transceiver.iceGatherer.close(); |
| 2065 } |
| 2066 */ |
| 2067 if (transceiver.iceTransport) { |
| 2068 transceiver.iceTransport.stop(); |
| 2069 } |
| 2070 if (transceiver.dtlsTransport) { |
| 2071 transceiver.dtlsTransport.stop(); |
| 2072 } |
| 2073 if (transceiver.rtpSender) { |
| 2074 transceiver.rtpSender.stop(); |
| 2075 } |
| 2076 if (transceiver.rtpReceiver) { |
| 2077 transceiver.rtpReceiver.stop(); |
| 2078 } |
| 2079 }); |
| 2080 // FIXME: clean up tracks, local streams, remote streams, etc |
| 2081 this._updateSignalingState('closed'); |
| 2082 }; |
| 2083 |
| 2084 // Update the signaling state. |
| 2085 window.RTCPeerConnection.prototype._updateSignalingState = |
| 2086 function(newState) { |
| 2087 this.signalingState = newState; |
| 2088 var event = new Event('signalingstatechange'); |
| 2089 this.dispatchEvent(event); |
| 2090 if (this.onsignalingstatechange !== null) { |
| 2091 this.onsignalingstatechange(event); |
| 2092 } |
| 2093 }; |
| 2094 |
| 2095 // Determine whether to fire the negotiationneeded event. |
| 2096 window.RTCPeerConnection.prototype._maybeFireNegotiationNeeded = |
| 2097 function() { |
| 2098 // Fire away (for now). |
| 2099 var event = new Event('negotiationneeded'); |
| 2100 this.dispatchEvent(event); |
| 2101 if (this.onnegotiationneeded !== null) { |
| 2102 this.onnegotiationneeded(event); |
| 2103 } |
| 2104 }; |
| 2105 |
| 2106 // Update the connection state. |
| 2107 window.RTCPeerConnection.prototype._updateConnectionState = function() { |
| 2108 var self = this; |
| 2109 var newState; |
| 2110 var states = { |
| 2111 'new': 0, |
| 2112 closed: 0, |
| 2113 connecting: 0, |
| 2114 checking: 0, |
| 2115 connected: 0, |
| 2116 completed: 0, |
| 2117 failed: 0 |
| 2118 }; |
| 2119 this.transceivers.forEach(function(transceiver) { |
| 2120 states[transceiver.iceTransport.state]++; |
| 2121 states[transceiver.dtlsTransport.state]++; |
| 2122 }); |
| 2123 // ICETransport.completed and connected are the same for this purpose. |
| 2124 states.connected += states.completed; |
| 2125 |
| 2126 newState = 'new'; |
| 2127 if (states.failed > 0) { |
| 2128 newState = 'failed'; |
| 2129 } else if (states.connecting > 0 || states.checking > 0) { |
| 2130 newState = 'connecting'; |
| 2131 } else if (states.disconnected > 0) { |
| 2132 newState = 'disconnected'; |
| 2133 } else if (states.new > 0) { |
| 2134 newState = 'new'; |
| 2135 } else if (states.connected > 0 || states.completed > 0) { |
| 2136 newState = 'connected'; |
| 2137 } |
| 2138 |
| 2139 if (newState !== self.iceConnectionState) { |
| 2140 self.iceConnectionState = newState; |
| 2141 var event = new Event('iceconnectionstatechange'); |
| 2142 this.dispatchEvent(event); |
| 2143 if (this.oniceconnectionstatechange !== null) { |
| 2144 this.oniceconnectionstatechange(event); |
| 2145 } |
| 2146 } |
| 2147 }; |
| 2148 |
| 2149 window.RTCPeerConnection.prototype.createOffer = function() { |
| 2150 var self = this; |
| 2151 if (this._pendingOffer) { |
| 2152 throw new Error('createOffer called while there is a pending offer.'); |
| 2153 } |
| 2154 var offerOptions; |
| 2155 if (arguments.length === 1 && typeof arguments[0] !== 'function') { |
| 2156 offerOptions = arguments[0]; |
| 2157 } else if (arguments.length === 3) { |
| 2158 offerOptions = arguments[2]; |
| 2159 } |
| 2160 |
| 2161 var tracks = []; |
| 2162 var numAudioTracks = 0; |
| 2163 var numVideoTracks = 0; |
| 2164 // Default to sendrecv. |
| 2165 if (this.localStreams.length) { |
| 2166 numAudioTracks = this.localStreams[0].getAudioTracks().length; |
| 2167 numVideoTracks = this.localStreams[0].getVideoTracks().length; |
| 2168 } |
| 2169 // Determine number of audio and video tracks we need to send/recv. |
| 2170 if (offerOptions) { |
| 2171 // Reject Chrome legacy constraints. |
| 2172 if (offerOptions.mandatory || offerOptions.optional) { |
| 2173 throw new TypeError( |
| 2174 'Legacy mandatory/optional constraints not supported.'); |
| 2175 } |
| 2176 if (offerOptions.offerToReceiveAudio !== undefined) { |
| 2177 numAudioTracks = offerOptions.offerToReceiveAudio; |
| 2178 } |
| 2179 if (offerOptions.offerToReceiveVideo !== undefined) { |
| 2180 numVideoTracks = offerOptions.offerToReceiveVideo; |
| 2181 } |
| 2182 } |
| 2183 if (this.localStreams.length) { |
| 2184 // Push local streams. |
| 2185 this.localStreams[0].getTracks().forEach(function(track) { |
| 2186 tracks.push({ |
| 2187 kind: track.kind, |
| 2188 track: track, |
| 2189 wantReceive: track.kind === 'audio' ? |
| 2190 numAudioTracks > 0 : numVideoTracks > 0 |
| 2191 }); |
| 2192 if (track.kind === 'audio') { |
| 2193 numAudioTracks--; |
| 2194 } else if (track.kind === 'video') { |
| 2195 numVideoTracks--; |
| 2196 } |
| 2197 }); |
| 2198 } |
| 2199 // Create M-lines for recvonly streams. |
| 2200 while (numAudioTracks > 0 || numVideoTracks > 0) { |
| 2201 if (numAudioTracks > 0) { |
| 2202 tracks.push({ |
| 2203 kind: 'audio', |
| 2204 wantReceive: true |
| 2205 }); |
| 2206 numAudioTracks--; |
| 2207 } |
| 2208 if (numVideoTracks > 0) { |
| 2209 tracks.push({ |
| 2210 kind: 'video', |
| 2211 wantReceive: true |
| 2212 }); |
| 2213 numVideoTracks--; |
| 2214 } |
| 2215 } |
| 2216 // reorder tracks |
| 2217 tracks = sortTracks(tracks); |
| 2218 |
| 2219 var sdp = SDPUtils.writeSessionBoilerplate(); |
| 2220 var transceivers = []; |
| 2221 tracks.forEach(function(mline, sdpMLineIndex) { |
| 2222 // For each track, create an ice gatherer, ice transport, |
| 2223 // dtls transport, potentially rtpsender and rtpreceiver. |
| 2224 var track = mline.track; |
| 2225 var kind = mline.kind; |
| 2226 var mid = SDPUtils.generateIdentifier(); |
| 2227 |
| 2228 var transports = self.usingBundle && sdpMLineIndex > 0 ? { |
| 2229 iceGatherer: transceivers[0].iceGatherer, |
| 2230 iceTransport: transceivers[0].iceTransport, |
| 2231 dtlsTransport: transceivers[0].dtlsTransport |
| 2232 } : self._createIceAndDtlsTransports(mid, sdpMLineIndex); |
| 2233 |
| 2234 var localCapabilities = RTCRtpSender.getCapabilities(kind); |
| 2235 // filter RTX until additional stuff needed for RTX is implemented |
| 2236 // in adapter.js |
| 2237 if (browserDetails.version < 15019) { |
| 2238 localCapabilities.codecs = localCapabilities.codecs.filter( |
| 2239 function(codec) { |
| 2240 return codec.name !== 'rtx'; |
| 2241 }); |
| 2242 } |
| 2243 localCapabilities.codecs.forEach(function(codec) { |
| 2244 // work around https://bugs.chromium.org/p/webrtc/issues/detail?id=655
2 |
| 2245 // by adding level-asymmetry-allowed=1 |
| 2246 if (codec.name === 'H264' && |
| 2247 codec.parameters['level-asymmetry-allowed'] === undefined) { |
| 2248 codec.parameters['level-asymmetry-allowed'] = '1'; |
| 2249 } |
| 2250 }); |
| 2251 |
| 2252 var rtpSender; |
| 2253 var rtpReceiver; |
| 2254 |
| 2255 // generate an ssrc now, to be used later in rtpSender.send |
| 2256 var sendEncodingParameters = [{ |
| 2257 ssrc: (2 * sdpMLineIndex + 1) * 1001 |
| 2258 }]; |
| 2259 if (track) { |
| 2260 // add RTX |
| 2261 if (browserDetails.version >= 15019 && kind === 'video') { |
| 2262 sendEncodingParameters[0].rtx = { |
| 2263 ssrc: (2 * sdpMLineIndex + 1) * 1001 + 1 |
| 2264 }; |
| 2265 } |
| 2266 rtpSender = new RTCRtpSender(track, transports.dtlsTransport); |
| 2267 } |
| 2268 |
| 2269 if (mline.wantReceive) { |
| 2270 rtpReceiver = new RTCRtpReceiver(transports.dtlsTransport, kind); |
| 2271 } |
| 2272 |
| 2273 transceivers[sdpMLineIndex] = { |
| 2274 iceGatherer: transports.iceGatherer, |
| 2275 iceTransport: transports.iceTransport, |
| 2276 dtlsTransport: transports.dtlsTransport, |
| 2277 localCapabilities: localCapabilities, |
| 2278 remoteCapabilities: null, |
| 2279 rtpSender: rtpSender, |
| 2280 rtpReceiver: rtpReceiver, |
| 2281 kind: kind, |
| 2282 mid: mid, |
| 2283 sendEncodingParameters: sendEncodingParameters, |
| 2284 recvEncodingParameters: null |
| 2285 }; |
| 2286 }); |
| 2287 if (this.usingBundle) { |
| 2288 sdp += 'a=group:BUNDLE ' + transceivers.map(function(t) { |
| 2289 return t.mid; |
| 2290 }).join(' ') + '\r\n'; |
| 2291 } |
| 2292 tracks.forEach(function(mline, sdpMLineIndex) { |
| 2293 var transceiver = transceivers[sdpMLineIndex]; |
| 2294 sdp += SDPUtils.writeMediaSection(transceiver, |
| 2295 transceiver.localCapabilities, 'offer', self.localStreams[0]); |
| 2296 }); |
| 2297 |
| 2298 this._pendingOffer = transceivers; |
| 2299 var desc = new RTCSessionDescription({ |
| 2300 type: 'offer', |
| 2301 sdp: sdp |
| 2302 }); |
| 2303 if (arguments.length && typeof arguments[0] === 'function') { |
| 2304 window.setTimeout(arguments[0], 0, desc); |
| 2305 } |
| 2306 return Promise.resolve(desc); |
| 2307 }; |
| 2308 |
| 2309 window.RTCPeerConnection.prototype.createAnswer = function() { |
| 2310 var self = this; |
| 2311 |
| 2312 var sdp = SDPUtils.writeSessionBoilerplate(); |
| 2313 if (this.usingBundle) { |
| 2314 sdp += 'a=group:BUNDLE ' + this.transceivers.map(function(t) { |
| 2315 return t.mid; |
| 2316 }).join(' ') + '\r\n'; |
| 2317 } |
| 2318 this.transceivers.forEach(function(transceiver) { |
| 2319 if (transceiver.isDatachannel) { |
| 2320 sdp += 'm=application 0 DTLS/SCTP 5000\r\n' + |
| 2321 'c=IN IP4 0.0.0.0\r\n' + |
| 2322 'a=mid:' + transceiver.mid + '\r\n'; |
| 2323 return; |
| 2324 } |
| 2325 // Calculate intersection of capabilities. |
| 2326 var commonCapabilities = self._getCommonCapabilities( |
| 2327 transceiver.localCapabilities, |
| 2328 transceiver.remoteCapabilities); |
| 2329 |
| 2330 sdp += SDPUtils.writeMediaSection(transceiver, commonCapabilities, |
| 2331 'answer', self.localStreams[0]); |
| 2332 }); |
| 2333 |
| 2334 var desc = new RTCSessionDescription({ |
| 2335 type: 'answer', |
| 2336 sdp: sdp |
| 2337 }); |
| 2338 if (arguments.length && typeof arguments[0] === 'function') { |
| 2339 window.setTimeout(arguments[0], 0, desc); |
| 2340 } |
| 2341 return Promise.resolve(desc); |
| 2342 }; |
| 2343 |
| 2344 window.RTCPeerConnection.prototype.addIceCandidate = function(candidate) { |
| 2345 if (!candidate) { |
| 2346 for (var j = 0; j < this.transceivers.length; j++) { |
| 2347 this.transceivers[j].iceTransport.addRemoteCandidate({}); |
| 2348 if (this.usingBundle) { |
| 2349 return Promise.resolve(); |
| 2350 } |
| 2351 } |
| 2352 } else { |
| 2353 var mLineIndex = candidate.sdpMLineIndex; |
| 2354 if (candidate.sdpMid) { |
| 2355 for (var i = 0; i < this.transceivers.length; i++) { |
| 2356 if (this.transceivers[i].mid === candidate.sdpMid) { |
| 2357 mLineIndex = i; |
| 2358 break; |
| 2359 } |
| 2360 } |
| 2361 } |
| 2362 var transceiver = this.transceivers[mLineIndex]; |
| 2363 if (transceiver) { |
| 2364 var cand = Object.keys(candidate.candidate).length > 0 ? |
| 2365 SDPUtils.parseCandidate(candidate.candidate) : {}; |
| 2366 // Ignore Chrome's invalid candidates since Edge does not like them. |
| 2367 if (cand.protocol === 'tcp' && (cand.port === 0 || cand.port === 9)) { |
| 2368 return Promise.resolve(); |
| 2369 } |
| 2370 // Ignore RTCP candidates, we assume RTCP-MUX. |
| 2371 if (cand.component !== '1') { |
| 2372 return Promise.resolve(); |
| 2373 } |
| 2374 transceiver.iceTransport.addRemoteCandidate(cand); |
| 2375 |
| 2376 // update the remoteDescription. |
| 2377 var sections = SDPUtils.splitSections(this.remoteDescription.sdp); |
| 2378 sections[mLineIndex + 1] += (cand.type ? candidate.candidate.trim() |
| 2379 : 'a=end-of-candidates') + '\r\n'; |
| 2380 this.remoteDescription.sdp = sections.join(''); |
| 2381 } |
| 2382 } |
| 2383 if (arguments.length > 1 && typeof arguments[1] === 'function') { |
| 2384 window.setTimeout(arguments[1], 0); |
| 2385 } |
| 2386 return Promise.resolve(); |
| 2387 }; |
| 2388 |
| 2389 window.RTCPeerConnection.prototype.getStats = function() { |
| 2390 var promises = []; |
| 2391 this.transceivers.forEach(function(transceiver) { |
| 2392 ['rtpSender', 'rtpReceiver', 'iceGatherer', 'iceTransport', |
| 2393 'dtlsTransport'].forEach(function(method) { |
| 2394 if (transceiver[method]) { |
| 2395 promises.push(transceiver[method].getStats()); |
| 2396 } |
| 2397 }); |
| 2398 }); |
| 2399 var cb = arguments.length > 1 && typeof arguments[1] === 'function' && |
| 2400 arguments[1]; |
| 2401 var fixStatsType = function(stat) { |
| 2402 return { |
| 2403 inboundrtp: 'inbound-rtp', |
| 2404 outboundrtp: 'outbound-rtp', |
| 2405 candidatepair: 'candidate-pair', |
| 2406 localcandidate: 'local-candidate', |
| 2407 remotecandidate: 'remote-candidate' |
| 2408 }[stat.type] || stat.type; |
| 2409 }; |
| 2410 return new Promise(function(resolve) { |
| 2411 // shim getStats with maplike support |
| 2412 var results = new Map(); |
| 2413 Promise.all(promises).then(function(res) { |
| 2414 res.forEach(function(result) { |
| 2415 Object.keys(result).forEach(function(id) { |
| 2416 result[id].type = fixStatsType(result[id]); |
| 2417 results.set(id, result[id]); |
| 2418 }); |
| 2419 }); |
| 2420 if (cb) { |
| 2421 window.setTimeout(cb, 0, results); |
| 2422 } |
| 2423 resolve(results); |
| 2424 }); |
| 2425 }); |
| 2426 }; |
| 2427 } |
| 2428 }; |
| 2429 |
| 2430 // Expose public methods. |
| 2431 module.exports = { |
| 2432 shimPeerConnection: edgeShim.shimPeerConnection, |
| 2433 shimGetUserMedia: require('./getusermedia') |
| 2434 }; |
| 2435 |
| 2436 },{"../utils":10,"./getusermedia":6,"sdp":1}],6:[function(require,module,exports
){ |
| 2437 /* |
| 2438 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 2439 * |
| 2440 * Use of this source code is governed by a BSD-style license |
| 2441 * that can be found in the LICENSE file in the root of the source |
| 2442 * tree. |
| 2443 */ |
| 2444 /* eslint-env node */ |
| 2445 'use strict'; |
| 2446 |
| 2447 // Expose public methods. |
| 2448 module.exports = function() { |
| 2449 var shimError_ = function(e) { |
| 2450 return { |
| 2451 name: {PermissionDeniedError: 'NotAllowedError'}[e.name] || e.name, |
| 2452 message: e.message, |
| 2453 constraint: e.constraint, |
| 2454 toString: function() { |
| 2455 return this.name; |
| 2456 } |
| 2457 }; |
| 2458 }; |
| 2459 |
| 2460 // getUserMedia error shim. |
| 2461 var origGetUserMedia = navigator.mediaDevices.getUserMedia. |
| 2462 bind(navigator.mediaDevices); |
| 2463 navigator.mediaDevices.getUserMedia = function(c) { |
| 2464 return origGetUserMedia(c).catch(function(e) { |
| 2465 return Promise.reject(shimError_(e)); |
| 2466 }); |
| 2467 }; |
| 2468 }; |
| 2469 |
| 2470 },{}],7:[function(require,module,exports){ |
| 2471 /* |
| 2472 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 2473 * |
| 2474 * Use of this source code is governed by a BSD-style license |
| 2475 * that can be found in the LICENSE file in the root of the source |
| 2476 * tree. |
| 2477 */ |
| 2478 /* eslint-env node */ |
| 2479 'use strict'; |
| 2480 |
| 2481 var browserDetails = require('../utils').browserDetails; |
| 2482 |
| 2483 var firefoxShim = { |
| 2484 shimOnTrack: function() { |
| 2485 if (typeof window === 'object' && window.RTCPeerConnection && !('ontrack' in |
| 2486 window.RTCPeerConnection.prototype)) { |
| 2487 Object.defineProperty(window.RTCPeerConnection.prototype, 'ontrack', { |
| 2488 get: function() { |
| 2489 return this._ontrack; |
| 2490 }, |
| 2491 set: function(f) { |
| 2492 if (this._ontrack) { |
| 2493 this.removeEventListener('track', this._ontrack); |
| 2494 this.removeEventListener('addstream', this._ontrackpoly); |
| 2495 } |
| 2496 this.addEventListener('track', this._ontrack = f); |
| 2497 this.addEventListener('addstream', this._ontrackpoly = function(e) { |
| 2498 e.stream.getTracks().forEach(function(track) { |
| 2499 var event = new Event('track'); |
| 2500 event.track = track; |
| 2501 event.receiver = {track: track}; |
| 2502 event.streams = [e.stream]; |
| 2503 this.dispatchEvent(event); |
| 2504 }.bind(this)); |
| 2505 }.bind(this)); |
| 2506 } |
| 2507 }); |
| 2508 } |
| 2509 }, |
| 2510 |
| 2511 shimSourceObject: function() { |
| 2512 // Firefox has supported mozSrcObject since FF22, unprefixed in 42. |
| 2513 if (typeof window === 'object') { |
| 2514 if (window.HTMLMediaElement && |
| 2515 !('srcObject' in window.HTMLMediaElement.prototype)) { |
| 2516 // Shim the srcObject property, once, when HTMLMediaElement is found. |
| 2517 Object.defineProperty(window.HTMLMediaElement.prototype, 'srcObject', { |
| 2518 get: function() { |
| 2519 return this.mozSrcObject; |
| 2520 }, |
| 2521 set: function(stream) { |
| 2522 this.mozSrcObject = stream; |
| 2523 } |
| 2524 }); |
| 2525 } |
| 2526 } |
| 2527 }, |
| 2528 |
| 2529 shimPeerConnection: function() { |
| 2530 if (typeof window !== 'object' || !(window.RTCPeerConnection || |
| 2531 window.mozRTCPeerConnection)) { |
| 2532 return; // probably media.peerconnection.enabled=false in about:config |
| 2533 } |
| 2534 // The RTCPeerConnection object. |
| 2535 if (!window.RTCPeerConnection) { |
| 2536 window.RTCPeerConnection = function(pcConfig, pcConstraints) { |
| 2537 if (browserDetails.version < 38) { |
| 2538 // .urls is not supported in FF < 38. |
| 2539 // create RTCIceServers with a single url. |
| 2540 if (pcConfig && pcConfig.iceServers) { |
| 2541 var newIceServers = []; |
| 2542 for (var i = 0; i < pcConfig.iceServers.length; i++) { |
| 2543 var server = pcConfig.iceServers[i]; |
| 2544 if (server.hasOwnProperty('urls')) { |
| 2545 for (var j = 0; j < server.urls.length; j++) { |
| 2546 var newServer = { |
| 2547 url: server.urls[j] |
| 2548 }; |
| 2549 if (server.urls[j].indexOf('turn') === 0) { |
| 2550 newServer.username = server.username; |
| 2551 newServer.credential = server.credential; |
| 2552 } |
| 2553 newIceServers.push(newServer); |
| 2554 } |
| 2555 } else { |
| 2556 newIceServers.push(pcConfig.iceServers[i]); |
| 2557 } |
| 2558 } |
| 2559 pcConfig.iceServers = newIceServers; |
| 2560 } |
| 2561 } |
| 2562 return new mozRTCPeerConnection(pcConfig, pcConstraints); |
| 2563 }; |
| 2564 window.RTCPeerConnection.prototype = mozRTCPeerConnection.prototype; |
| 2565 |
| 2566 // wrap static methods. Currently just generateCertificate. |
| 2567 if (mozRTCPeerConnection.generateCertificate) { |
| 2568 Object.defineProperty(window.RTCPeerConnection, 'generateCertificate', { |
| 2569 get: function() { |
| 2570 return mozRTCPeerConnection.generateCertificate; |
| 2571 } |
| 2572 }); |
| 2573 } |
| 2574 |
| 2575 window.RTCSessionDescription = mozRTCSessionDescription; |
| 2576 window.RTCIceCandidate = mozRTCIceCandidate; |
| 2577 } |
| 2578 |
| 2579 // shim away need for obsolete RTCIceCandidate/RTCSessionDescription. |
| 2580 ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate'] |
| 2581 .forEach(function(method) { |
| 2582 var nativeMethod = RTCPeerConnection.prototype[method]; |
| 2583 RTCPeerConnection.prototype[method] = function() { |
| 2584 arguments[0] = new ((method === 'addIceCandidate') ? |
| 2585 RTCIceCandidate : RTCSessionDescription)(arguments[0]); |
| 2586 return nativeMethod.apply(this, arguments); |
| 2587 }; |
| 2588 }); |
| 2589 |
| 2590 // support for addIceCandidate(null or undefined) |
| 2591 var nativeAddIceCandidate = |
| 2592 RTCPeerConnection.prototype.addIceCandidate; |
| 2593 RTCPeerConnection.prototype.addIceCandidate = function() { |
| 2594 if (!arguments[0]) { |
| 2595 if (arguments[1]) { |
| 2596 arguments[1].apply(null); |
| 2597 } |
| 2598 return Promise.resolve(); |
| 2599 } |
| 2600 return nativeAddIceCandidate.apply(this, arguments); |
| 2601 }; |
| 2602 |
| 2603 // shim getStats with maplike support |
| 2604 var makeMapStats = function(stats) { |
| 2605 var map = new Map(); |
| 2606 Object.keys(stats).forEach(function(key) { |
| 2607 map.set(key, stats[key]); |
| 2608 map[key] = stats[key]; |
| 2609 }); |
| 2610 return map; |
| 2611 }; |
| 2612 |
| 2613 var modernStatsTypes = { |
| 2614 inboundrtp: 'inbound-rtp', |
| 2615 outboundrtp: 'outbound-rtp', |
| 2616 candidatepair: 'candidate-pair', |
| 2617 localcandidate: 'local-candidate', |
| 2618 remotecandidate: 'remote-candidate' |
| 2619 }; |
| 2620 |
| 2621 var nativeGetStats = RTCPeerConnection.prototype.getStats; |
| 2622 RTCPeerConnection.prototype.getStats = function(selector, onSucc, onErr) { |
| 2623 return nativeGetStats.apply(this, [selector || null]) |
| 2624 .then(function(stats) { |
| 2625 if (browserDetails.version < 48) { |
| 2626 stats = makeMapStats(stats); |
| 2627 } |
| 2628 if (browserDetails.version < 53 && !onSucc) { |
| 2629 // Shim only promise getStats with spec-hyphens in type names |
| 2630 // Leave callback version alone; misc old uses of forEach before Map |
| 2631 try { |
| 2632 stats.forEach(function(stat) { |
| 2633 stat.type = modernStatsTypes[stat.type] || stat.type; |
| 2634 }); |
| 2635 } catch (e) { |
| 2636 if (e.name !== 'TypeError') { |
| 2637 throw e; |
| 2638 } |
| 2639 // Avoid TypeError: "type" is read-only, in old versions. 34-43ish |
| 2640 stats.forEach(function(stat, i) { |
| 2641 stats.set(i, Object.assign({}, stat, { |
| 2642 type: modernStatsTypes[stat.type] || stat.type |
| 2643 })); |
| 2644 }); |
| 2645 } |
| 2646 } |
| 2647 return stats; |
| 2648 }) |
| 2649 .then(onSucc, onErr); |
| 2650 }; |
| 2651 } |
| 2652 }; |
| 2653 |
| 2654 // Expose public methods. |
| 2655 module.exports = { |
| 2656 shimOnTrack: firefoxShim.shimOnTrack, |
| 2657 shimSourceObject: firefoxShim.shimSourceObject, |
| 2658 shimPeerConnection: firefoxShim.shimPeerConnection, |
| 2659 shimGetUserMedia: require('./getusermedia') |
| 2660 }; |
| 2661 |
| 2662 },{"../utils":10,"./getusermedia":8}],8:[function(require,module,exports){ |
| 2663 /* |
| 2664 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 2665 * |
| 2666 * Use of this source code is governed by a BSD-style license |
| 2667 * that can be found in the LICENSE file in the root of the source |
| 2668 * tree. |
| 2669 */ |
| 2670 /* eslint-env node */ |
| 2671 'use strict'; |
| 2672 |
| 2673 var logging = require('../utils').log; |
| 2674 var browserDetails = require('../utils').browserDetails; |
| 2675 |
| 2676 // Expose public methods. |
| 2677 module.exports = function() { |
| 2678 var shimError_ = function(e) { |
| 2679 return { |
| 2680 name: { |
| 2681 SecurityError: 'NotAllowedError', |
| 2682 PermissionDeniedError: 'NotAllowedError' |
| 2683 }[e.name] || e.name, |
| 2684 message: { |
| 2685 'The operation is insecure.': 'The request is not allowed by the ' + |
| 2686 'user agent or the platform in the current context.' |
| 2687 }[e.message] || e.message, |
| 2688 constraint: e.constraint, |
| 2689 toString: function() { |
| 2690 return this.name + (this.message && ': ') + this.message; |
| 2691 } |
| 2692 }; |
| 2693 }; |
| 2694 |
| 2695 // getUserMedia constraints shim. |
| 2696 var getUserMedia_ = function(constraints, onSuccess, onError) { |
| 2697 var constraintsToFF37_ = function(c) { |
| 2698 if (typeof c !== 'object' || c.require) { |
| 2699 return c; |
| 2700 } |
| 2701 var require = []; |
| 2702 Object.keys(c).forEach(function(key) { |
| 2703 if (key === 'require' || key === 'advanced' || key === 'mediaSource') { |
| 2704 return; |
| 2705 } |
| 2706 var r = c[key] = (typeof c[key] === 'object') ? |
| 2707 c[key] : {ideal: c[key]}; |
| 2708 if (r.min !== undefined || |
| 2709 r.max !== undefined || r.exact !== undefined) { |
| 2710 require.push(key); |
| 2711 } |
| 2712 if (r.exact !== undefined) { |
| 2713 if (typeof r.exact === 'number') { |
| 2714 r. min = r.max = r.exact; |
| 2715 } else { |
| 2716 c[key] = r.exact; |
| 2717 } |
| 2718 delete r.exact; |
| 2719 } |
| 2720 if (r.ideal !== undefined) { |
| 2721 c.advanced = c.advanced || []; |
| 2722 var oc = {}; |
| 2723 if (typeof r.ideal === 'number') { |
| 2724 oc[key] = {min: r.ideal, max: r.ideal}; |
| 2725 } else { |
| 2726 oc[key] = r.ideal; |
| 2727 } |
| 2728 c.advanced.push(oc); |
| 2729 delete r.ideal; |
| 2730 if (!Object.keys(r).length) { |
| 2731 delete c[key]; |
| 2732 } |
| 2733 } |
| 2734 }); |
| 2735 if (require.length) { |
| 2736 c.require = require; |
| 2737 } |
| 2738 return c; |
| 2739 }; |
| 2740 constraints = JSON.parse(JSON.stringify(constraints)); |
| 2741 if (browserDetails.version < 38) { |
| 2742 logging('spec: ' + JSON.stringify(constraints)); |
| 2743 if (constraints.audio) { |
| 2744 constraints.audio = constraintsToFF37_(constraints.audio); |
| 2745 } |
| 2746 if (constraints.video) { |
| 2747 constraints.video = constraintsToFF37_(constraints.video); |
| 2748 } |
| 2749 logging('ff37: ' + JSON.stringify(constraints)); |
| 2750 } |
| 2751 return navigator.mozGetUserMedia(constraints, onSuccess, function(e) { |
| 2752 onError(shimError_(e)); |
| 2753 }); |
| 2754 }; |
| 2755 |
| 2756 // Returns the result of getUserMedia as a Promise. |
| 2757 var getUserMediaPromise_ = function(constraints) { |
| 2758 return new Promise(function(resolve, reject) { |
| 2759 getUserMedia_(constraints, resolve, reject); |
| 2760 }); |
| 2761 }; |
| 2762 |
| 2763 // Shim for mediaDevices on older versions. |
| 2764 if (!navigator.mediaDevices) { |
| 2765 navigator.mediaDevices = {getUserMedia: getUserMediaPromise_, |
| 2766 addEventListener: function() { }, |
| 2767 removeEventListener: function() { } |
| 2768 }; |
| 2769 } |
| 2770 navigator.mediaDevices.enumerateDevices = |
| 2771 navigator.mediaDevices.enumerateDevices || function() { |
| 2772 return new Promise(function(resolve) { |
| 2773 var infos = [ |
| 2774 {kind: 'audioinput', deviceId: 'default', label: '', groupId: ''}, |
| 2775 {kind: 'videoinput', deviceId: 'default', label: '', groupId: ''} |
| 2776 ]; |
| 2777 resolve(infos); |
| 2778 }); |
| 2779 }; |
| 2780 |
| 2781 if (browserDetails.version < 41) { |
| 2782 // Work around http://bugzil.la/1169665 |
| 2783 var orgEnumerateDevices = |
| 2784 navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices); |
| 2785 navigator.mediaDevices.enumerateDevices = function() { |
| 2786 return orgEnumerateDevices().then(undefined, function(e) { |
| 2787 if (e.name === 'NotFoundError') { |
| 2788 return []; |
| 2789 } |
| 2790 throw e; |
| 2791 }); |
| 2792 }; |
| 2793 } |
| 2794 if (browserDetails.version < 49) { |
| 2795 var origGetUserMedia = navigator.mediaDevices.getUserMedia. |
| 2796 bind(navigator.mediaDevices); |
| 2797 navigator.mediaDevices.getUserMedia = function(c) { |
| 2798 return origGetUserMedia(c).then(function(stream) { |
| 2799 // Work around https://bugzil.la/802326 |
| 2800 if (c.audio && !stream.getAudioTracks().length || |
| 2801 c.video && !stream.getVideoTracks().length) { |
| 2802 stream.getTracks().forEach(function(track) { |
| 2803 track.stop(); |
| 2804 }); |
| 2805 throw new DOMException('The object can not be found here.', |
| 2806 'NotFoundError'); |
| 2807 } |
| 2808 return stream; |
| 2809 }, function(e) { |
| 2810 return Promise.reject(shimError_(e)); |
| 2811 }); |
| 2812 }; |
| 2813 } |
| 2814 navigator.getUserMedia = function(constraints, onSuccess, onError) { |
| 2815 if (browserDetails.version < 44) { |
| 2816 return getUserMedia_(constraints, onSuccess, onError); |
| 2817 } |
| 2818 // Replace Firefox 44+'s deprecation warning with unprefixed version. |
| 2819 console.warn('navigator.getUserMedia has been replaced by ' + |
| 2820 'navigator.mediaDevices.getUserMedia'); |
| 2821 navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError); |
| 2822 }; |
| 2823 }; |
| 2824 |
| 2825 },{"../utils":10}],9:[function(require,module,exports){ |
| 2826 /* |
| 2827 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 2828 * |
| 2829 * Use of this source code is governed by a BSD-style license |
| 2830 * that can be found in the LICENSE file in the root of the source |
| 2831 * tree. |
| 2832 */ |
| 2833 'use strict'; |
| 2834 var safariShim = { |
| 2835 // TODO: DrAlex, should be here, double check against LayoutTests |
| 2836 // shimOnTrack: function() { }, |
| 2837 |
| 2838 // TODO: once the back-end for the mac port is done, add. |
| 2839 // TODO: check for webkitGTK+ |
| 2840 // shimPeerConnection: function() { }, |
| 2841 |
| 2842 shimGetUserMedia: function() { |
| 2843 if (!navigator.getUserMedia) { |
| 2844 if (navigator.webkitGetUserMedia) { |
| 2845 navigator.getUserMedia = navigator.webkitGetUserMedia.bind(navigator); |
| 2846 } else if (navigator.mediaDevices && |
| 2847 navigator.mediaDevices.getUserMedia) { |
| 2848 navigator.getUserMedia = function(constraints, cb, errcb) { |
| 2849 navigator.mediaDevices.getUserMedia(constraints) |
| 2850 .then(cb, errcb); |
| 2851 }.bind(navigator); |
| 2852 } |
| 2853 } |
| 2854 } |
| 2855 }; |
| 2856 |
| 2857 // Expose public methods. |
| 2858 module.exports = { |
| 2859 shimGetUserMedia: safariShim.shimGetUserMedia |
| 2860 // TODO |
| 2861 // shimOnTrack: safariShim.shimOnTrack, |
| 2862 // shimPeerConnection: safariShim.shimPeerConnection |
| 2863 }; |
| 2864 |
| 2865 },{}],10:[function(require,module,exports){ |
| 2866 /* |
| 2867 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 2868 * |
| 2869 * Use of this source code is governed by a BSD-style license |
| 2870 * that can be found in the LICENSE file in the root of the source |
| 2871 * tree. |
| 2872 */ |
| 2873 /* eslint-env node */ |
| 2874 'use strict'; |
| 2875 |
| 2876 var logDisabled_ = true; |
| 2877 |
| 2878 // Utility methods. |
| 2879 var utils = { |
| 2880 disableLog: function(bool) { |
| 2881 if (typeof bool !== 'boolean') { |
| 2882 return new Error('Argument type: ' + typeof bool + |
| 2883 '. Please use a boolean.'); |
| 2884 } |
| 2885 logDisabled_ = bool; |
| 2886 return (bool) ? 'adapter.js logging disabled' : |
| 2887 'adapter.js logging enabled'; |
| 2888 }, |
| 2889 |
| 2890 log: function() { |
| 2891 if (typeof window === 'object') { |
| 2892 if (logDisabled_) { |
| 2893 return; |
| 2894 } |
| 2895 if (typeof console !== 'undefined' && typeof console.log === 'function') { |
| 2896 console.log.apply(console, arguments); |
| 2897 } |
| 2898 } |
| 2899 }, |
| 2900 |
| 2901 /** |
| 2902 * Extract browser version out of the provided user agent string. |
| 2903 * |
| 2904 * @param {!string} uastring userAgent string. |
| 2905 * @param {!string} expr Regular expression used as match criteria. |
| 2906 * @param {!number} pos position in the version string to be returned. |
| 2907 * @return {!number} browser version. |
| 2908 */ |
| 2909 extractVersion: function(uastring, expr, pos) { |
| 2910 var match = uastring.match(expr); |
| 2911 return match && match.length >= pos && parseInt(match[pos], 10); |
| 2912 }, |
| 2913 |
| 2914 /** |
| 2915 * Browser detector. |
| 2916 * |
| 2917 * @return {object} result containing browser and version |
| 2918 * properties. |
| 2919 */ |
| 2920 detectBrowser: function() { |
| 2921 // Returned result object. |
| 2922 var result = {}; |
| 2923 result.browser = null; |
| 2924 result.version = null; |
| 2925 |
| 2926 // Fail early if it's not a browser |
| 2927 if (typeof window === 'undefined' || !window.navigator) { |
| 2928 result.browser = 'Not a browser.'; |
| 2929 return result; |
| 2930 } |
| 2931 |
| 2932 // Firefox. |
| 2933 if (navigator.mozGetUserMedia) { |
| 2934 result.browser = 'firefox'; |
| 2935 result.version = this.extractVersion(navigator.userAgent, |
| 2936 /Firefox\/(\d+)\./, 1); |
| 2937 } else if (navigator.webkitGetUserMedia) { |
| 2938 // Chrome, Chromium, Webview, Opera, all use the chrome shim for now |
| 2939 if (window.webkitRTCPeerConnection) { |
| 2940 result.browser = 'chrome'; |
| 2941 result.version = this.extractVersion(navigator.userAgent, |
| 2942 /Chrom(e|ium)\/(\d+)\./, 2); |
| 2943 } else { // Safari (in an unpublished version) or unknown webkit-based. |
| 2944 if (navigator.userAgent.match(/Version\/(\d+).(\d+)/)) { |
| 2945 result.browser = 'safari'; |
| 2946 result.version = this.extractVersion(navigator.userAgent, |
| 2947 /AppleWebKit\/(\d+)\./, 1); |
| 2948 } else { // unknown webkit-based browser. |
| 2949 result.browser = 'Unsupported webkit-based browser ' + |
| 2950 'with GUM support but no WebRTC support.'; |
| 2951 return result; |
| 2952 } |
| 2953 } |
| 2954 } else if (navigator.mediaDevices && |
| 2955 navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) { // Edge. |
| 2956 result.browser = 'edge'; |
| 2957 result.version = this.extractVersion(navigator.userAgent, |
| 2958 /Edge\/(\d+).(\d+)$/, 2); |
| 2959 } else if (navigator.mediaDevices && |
| 2960 navigator.userAgent.match(/AppleWebKit\/(\d+)\./)) { |
| 2961 // Safari, with webkitGetUserMedia removed. |
| 2962 result.browser = 'safari'; |
| 2963 result.version = this.extractVersion(navigator.userAgent, |
| 2964 /AppleWebKit\/(\d+)\./, 1); |
| 2965 } else { // Default fallthrough: not supported. |
| 2966 result.browser = 'Not a supported browser.'; |
| 2967 return result; |
| 2968 } |
| 2969 |
| 2970 return result; |
| 2971 }, |
| 2972 |
| 2973 // shimCreateObjectURL must be called before shimSourceObject to avoid loop. |
| 2974 |
| 2975 shimCreateObjectURL: function() { |
| 2976 if (!(typeof window === 'object' && window.HTMLMediaElement && |
| 2977 'srcObject' in window.HTMLMediaElement.prototype)) { |
| 2978 // Only shim CreateObjectURL using srcObject if srcObject exists. |
| 2979 return undefined; |
| 2980 } |
| 2981 |
| 2982 var nativeCreateObjectURL = URL.createObjectURL.bind(URL); |
| 2983 var nativeRevokeObjectURL = URL.revokeObjectURL.bind(URL); |
| 2984 var streams = new Map(), newId = 0; |
| 2985 |
| 2986 URL.createObjectURL = function(stream) { |
| 2987 if ('getTracks' in stream) { |
| 2988 var url = 'polyblob:' + (++newId); |
| 2989 streams.set(url, stream); |
| 2990 console.log('URL.createObjectURL(stream) is deprecated! ' + |
| 2991 'Use elem.srcObject = stream instead!'); |
| 2992 return url; |
| 2993 } |
| 2994 return nativeCreateObjectURL(stream); |
| 2995 }; |
| 2996 URL.revokeObjectURL = function(url) { |
| 2997 nativeRevokeObjectURL(url); |
| 2998 streams.delete(url); |
| 2999 }; |
| 3000 |
| 3001 var dsc = Object.getOwnPropertyDescriptor(window.HTMLMediaElement.prototype, |
| 3002 'src'); |
| 3003 Object.defineProperty(window.HTMLMediaElement.prototype, 'src', { |
| 3004 get: function() { |
| 3005 return dsc.get.apply(this); |
| 3006 }, |
| 3007 set: function(url) { |
| 3008 this.srcObject = streams.get(url) || null; |
| 3009 return dsc.set.apply(this, [url]); |
| 3010 } |
| 3011 }); |
| 3012 |
| 3013 var nativeSetAttribute = HTMLMediaElement.prototype.setAttribute; |
| 3014 HTMLMediaElement.prototype.setAttribute = function() { |
| 3015 if (arguments.length === 2 && |
| 3016 ('' + arguments[0]).toLowerCase() === 'src') { |
| 3017 this.srcObject = streams.get(arguments[1]) || null; |
| 3018 } |
| 3019 return nativeSetAttribute.apply(this, arguments); |
| 3020 }; |
| 3021 } |
| 3022 }; |
| 3023 |
| 3024 // Export. |
| 3025 module.exports = { |
| 3026 log: utils.log, |
| 3027 disableLog: utils.disableLog, |
| 3028 browserDetails: utils.detectBrowser(), |
| 3029 extractVersion: utils.extractVersion, |
| 3030 shimCreateObjectURL: utils.shimCreateObjectURL, |
| 3031 detectBrowser: utils.detectBrowser.bind(utils) |
| 3032 }; |
| 3033 |
| 3034 },{}]},{},[2])(2) |
| 3035 }); |
OLD | NEW |