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