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