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