You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strophe.jingle.adapter.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. function TraceablePeerConnection(ice_config, constraints) {
  2. var self = this;
  3. var RTCPeerconnection = navigator.mozGetUserMedia ? mozRTCPeerConnection : webkitRTCPeerConnection;
  4. this.peerconnection = new RTCPeerconnection(ice_config, constraints);
  5. this.updateLog = [];
  6. this.stats = {};
  7. this.statsinterval = null;
  8. this.maxstats = 0; // limit to 300 values, i.e. 5 minutes; set to 0 to disable
  9. /**
  10. * Array of ssrcs that will be added on next modifySources call.
  11. * @type {Array}
  12. */
  13. this.addssrc = [];
  14. /**
  15. * Array of ssrcs that will be added on next modifySources call.
  16. * @type {Array}
  17. */
  18. this.removessrc = [];
  19. /**
  20. * Pending operation that will be done during modifySources call.
  21. * Currently 'mute'/'unmute' operations are supported.
  22. *
  23. * @type {String}
  24. */
  25. this.pendingop = null;
  26. /**
  27. * Flag indicates that peer connection stream have changed and modifySources should proceed.
  28. * @type {boolean}
  29. */
  30. this.switchstreams = false;
  31. // override as desired
  32. this.trace = function (what, info) {
  33. //console.warn('WTRACE', what, info);
  34. self.updateLog.push({
  35. time: new Date(),
  36. type: what,
  37. value: info || ""
  38. });
  39. };
  40. this.onicecandidate = null;
  41. this.peerconnection.onicecandidate = function (event) {
  42. self.trace('onicecandidate', JSON.stringify(event.candidate, null, ' '));
  43. if (self.onicecandidate !== null) {
  44. self.onicecandidate(event);
  45. }
  46. };
  47. this.onaddstream = null;
  48. this.peerconnection.onaddstream = function (event) {
  49. self.trace('onaddstream', event.stream.id);
  50. if (self.onaddstream !== null) {
  51. self.onaddstream(event);
  52. }
  53. };
  54. this.onremovestream = null;
  55. this.peerconnection.onremovestream = function (event) {
  56. self.trace('onremovestream', event.stream.id);
  57. if (self.onremovestream !== null) {
  58. self.onremovestream(event);
  59. }
  60. };
  61. this.onsignalingstatechange = null;
  62. this.peerconnection.onsignalingstatechange = function (event) {
  63. self.trace('onsignalingstatechange', self.signalingState);
  64. if (self.onsignalingstatechange !== null) {
  65. self.onsignalingstatechange(event);
  66. }
  67. };
  68. this.oniceconnectionstatechange = null;
  69. this.peerconnection.oniceconnectionstatechange = function (event) {
  70. self.trace('oniceconnectionstatechange', self.iceConnectionState);
  71. if (self.oniceconnectionstatechange !== null) {
  72. self.oniceconnectionstatechange(event);
  73. }
  74. };
  75. this.onnegotiationneeded = null;
  76. this.peerconnection.onnegotiationneeded = function (event) {
  77. self.trace('onnegotiationneeded');
  78. if (self.onnegotiationneeded !== null) {
  79. self.onnegotiationneeded(event);
  80. }
  81. };
  82. self.ondatachannel = null;
  83. this.peerconnection.ondatachannel = function (event) {
  84. self.trace('ondatachannel', event);
  85. if (self.ondatachannel !== null) {
  86. self.ondatachannel(event);
  87. }
  88. };
  89. if (!navigator.mozGetUserMedia && this.maxstats) {
  90. this.statsinterval = window.setInterval(function() {
  91. self.peerconnection.getStats(function(stats) {
  92. var results = stats.result();
  93. for (var i = 0; i < results.length; ++i) {
  94. //console.log(results[i].type, results[i].id, results[i].names())
  95. var now = new Date();
  96. results[i].names().forEach(function (name) {
  97. var id = results[i].id + '-' + name;
  98. if (!self.stats[id]) {
  99. self.stats[id] = {
  100. startTime: now,
  101. endTime: now,
  102. values: [],
  103. times: []
  104. };
  105. }
  106. self.stats[id].values.push(results[i].stat(name));
  107. self.stats[id].times.push(now.getTime());
  108. if (self.stats[id].values.length > self.maxstats) {
  109. self.stats[id].values.shift();
  110. self.stats[id].times.shift();
  111. }
  112. self.stats[id].endTime = now;
  113. });
  114. }
  115. });
  116. }, 1000);
  117. }
  118. };
  119. dumpSDP = function(description) {
  120. return 'type: ' + description.type + '\r\n' + description.sdp;
  121. }
  122. if (TraceablePeerConnection.prototype.__defineGetter__ !== undefined) {
  123. TraceablePeerConnection.prototype.__defineGetter__('signalingState', function() { return this.peerconnection.signalingState; });
  124. TraceablePeerConnection.prototype.__defineGetter__('iceConnectionState', function() { return this.peerconnection.iceConnectionState; });
  125. TraceablePeerConnection.prototype.__defineGetter__('localDescription', function() {
  126. var simulcast = new Simulcast();
  127. var publicLocalDescription = simulcast.makeLocalDescriptionPublic(this.peerconnection.localDescription);
  128. return publicLocalDescription;
  129. });
  130. TraceablePeerConnection.prototype.__defineGetter__('remoteDescription', function() { return this.peerconnection.remoteDescription; });
  131. }
  132. TraceablePeerConnection.prototype.addStream = function (stream) {
  133. this.trace('addStream', stream.id);
  134. this.peerconnection.addStream(stream);
  135. };
  136. TraceablePeerConnection.prototype.removeStream = function (stream) {
  137. this.trace('removeStream', stream.id);
  138. this.peerconnection.removeStream(stream);
  139. };
  140. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  141. this.trace('createDataChannel', label, opts);
  142. return this.peerconnection.createDataChannel(label, opts);
  143. };
  144. TraceablePeerConnection.prototype.setLocalDescription = function (description, successCallback, failureCallback) {
  145. var self = this;
  146. var simulcast = new Simulcast();
  147. description = simulcast.transformLocalDescription(description);
  148. this.trace('setLocalDescription', dumpSDP(description));
  149. this.peerconnection.setLocalDescription(description,
  150. function () {
  151. self.trace('setLocalDescriptionOnSuccess');
  152. successCallback();
  153. },
  154. function (err) {
  155. self.trace('setLocalDescriptionOnFailure', err);
  156. failureCallback(err);
  157. }
  158. );
  159. /*
  160. if (this.statsinterval === null && this.maxstats > 0) {
  161. // start gathering stats
  162. }
  163. */
  164. };
  165. TraceablePeerConnection.prototype.setRemoteDescription = function (description, successCallback, failureCallback) {
  166. var self = this;
  167. var simulcast = new Simulcast();
  168. description = simulcast.transformRemoteDescription(description);
  169. this.trace('setRemoteDescription', dumpSDP(description));
  170. this.peerconnection.setRemoteDescription(description,
  171. function () {
  172. self.trace('setRemoteDescriptionOnSuccess');
  173. successCallback();
  174. },
  175. function (err) {
  176. self.trace('setRemoteDescriptionOnFailure', err);
  177. failureCallback(err);
  178. }
  179. );
  180. /*
  181. if (this.statsinterval === null && this.maxstats > 0) {
  182. // start gathering stats
  183. }
  184. */
  185. };
  186. TraceablePeerConnection.prototype.hardMuteVideo = function (muted) {
  187. this.pendingop = muted ? 'mute' : 'unmute';
  188. };
  189. TraceablePeerConnection.prototype.enqueueAddSsrc = function(channel, ssrcLines) {
  190. if (!this.addssrc[channel]) {
  191. this.addssrc[channel] = '';
  192. }
  193. this.addssrc[channel] += ssrcLines;
  194. }
  195. TraceablePeerConnection.prototype.addSource = function (elem) {
  196. console.log('addssrc', new Date().getTime());
  197. console.log('ice', this.iceConnectionState);
  198. var sdp = new SDP(this.remoteDescription.sdp);
  199. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  200. var self = this;
  201. $(elem).each(function (idx, content) {
  202. var name = $(content).attr('name');
  203. var lines = '';
  204. tmp = $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  205. var semantics = this.getAttribute('semantics');
  206. var ssrcs = $(this).find('>source').map(function () {
  207. return this.getAttribute('ssrc');
  208. }).get();
  209. if (ssrcs.length != 0) {
  210. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  211. }
  212. });
  213. tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  214. tmp.each(function () {
  215. var ssrc = $(this).attr('ssrc');
  216. if(mySdp.containsSSRC(ssrc)){
  217. /**
  218. * This happens when multiple participants change their streams at the same time and
  219. * ColibriFocus.modifySources have to wait for stable state. In the meantime multiple
  220. * addssrc are scheduled for update IQ. See
  221. */
  222. console.warn("Got add stream request for my own ssrc: "+ssrc);
  223. return;
  224. }
  225. $(this).find('>parameter').each(function () {
  226. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  227. if ($(this).attr('value') && $(this).attr('value').length)
  228. lines += ':' + $(this).attr('value');
  229. lines += '\r\n';
  230. });
  231. });
  232. sdp.media.forEach(function(media, idx) {
  233. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  234. return;
  235. sdp.media[idx] += lines;
  236. self.enqueueAddSsrc(idx, lines);
  237. });
  238. sdp.raw = sdp.session + sdp.media.join('');
  239. });
  240. };
  241. TraceablePeerConnection.prototype.enqueueRemoveSsrc = function(channel, ssrcLines) {
  242. if (!this.removessrc[channel]){
  243. this.removessrc[channel] = '';
  244. }
  245. this.removessrc[channel] += ssrcLines;
  246. }
  247. TraceablePeerConnection.prototype.removeSource = function (elem) {
  248. console.log('removessrc', new Date().getTime());
  249. console.log('ice', this.iceConnectionState);
  250. var sdp = new SDP(this.remoteDescription.sdp);
  251. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  252. var self = this;
  253. $(elem).each(function (idx, content) {
  254. var name = $(content).attr('name');
  255. var lines = '';
  256. tmp = $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  257. var semantics = this.getAttribute('semantics');
  258. var ssrcs = $(this).find('>source').map(function () {
  259. return this.getAttribute('ssrc');
  260. }).get();
  261. if (ssrcs.length != 0) {
  262. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  263. }
  264. });
  265. tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  266. tmp.each(function () {
  267. var ssrc = $(this).attr('ssrc');
  268. // This should never happen, but can be useful for bug detection
  269. if(mySdp.containsSSRC(ssrc)){
  270. console.error("Got remove stream request for my own ssrc: "+ssrc);
  271. return;
  272. }
  273. $(this).find('>parameter').each(function () {
  274. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  275. if ($(this).attr('value') && $(this).attr('value').length)
  276. lines += ':' + $(this).attr('value');
  277. lines += '\r\n';
  278. });
  279. });
  280. sdp.media.forEach(function(media, idx) {
  281. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  282. return;
  283. sdp.media[idx] += lines;
  284. self.enqueueRemoveSsrc(idx, lines);
  285. });
  286. sdp.raw = sdp.session + sdp.media.join('');
  287. });
  288. };
  289. TraceablePeerConnection.prototype.modifySources = function(successCallback) {
  290. var self = this;
  291. if (this.signalingState == 'closed') return;
  292. if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null || this.switchstreams)){
  293. // There is nothing to do since scheduled job might have been executed by another succeeding call
  294. if(successCallback){
  295. successCallback();
  296. }
  297. return;
  298. }
  299. // FIXME: this is a big hack
  300. // https://code.google.com/p/webrtc/issues/detail?id=2688
  301. if (!(this.signalingState == 'stable' && this.iceConnectionState == 'connected')) {
  302. console.warn('modifySources not yet', this.signalingState, this.iceConnectionState);
  303. this.wait = true;
  304. window.setTimeout(function() { self.modifySources(successCallback); }, 250);
  305. return;
  306. }
  307. if (this.wait) {
  308. window.setTimeout(function() { self.modifySources(successCallback); }, 2500);
  309. this.wait = false;
  310. return;
  311. }
  312. // Reset switch streams flag
  313. this.switchstreams = false;
  314. var sdp = new SDP(this.remoteDescription.sdp);
  315. // add sources
  316. this.addssrc.forEach(function(lines, idx) {
  317. sdp.media[idx] += lines;
  318. });
  319. this.addssrc = [];
  320. // remove sources
  321. this.removessrc.forEach(function(lines, idx) {
  322. lines = lines.split('\r\n');
  323. lines.pop(); // remove empty last element;
  324. lines.forEach(function(line) {
  325. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  326. });
  327. });
  328. this.removessrc = [];
  329. sdp.raw = sdp.session + sdp.media.join('');
  330. this.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  331. function() {
  332. if(self.signalingState == 'closed') {
  333. console.error("createAnswer attempt on closed state");
  334. return;
  335. }
  336. self.createAnswer(
  337. function(modifiedAnswer) {
  338. // change video direction, see https://github.com/jitsi/jitmeet/issues/41
  339. if (self.pendingop !== null) {
  340. var sdp = new SDP(modifiedAnswer.sdp);
  341. if (sdp.media.length > 1) {
  342. switch(self.pendingop) {
  343. case 'mute':
  344. sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
  345. break;
  346. case 'unmute':
  347. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  348. break;
  349. }
  350. sdp.raw = sdp.session + sdp.media.join('');
  351. modifiedAnswer.sdp = sdp.raw;
  352. }
  353. self.pendingop = null;
  354. }
  355. // FIXME: pushing down an answer while ice connection state
  356. // is still checking is bad...
  357. //console.log(self.peerconnection.iceConnectionState);
  358. // trying to work around another chrome bug
  359. //modifiedAnswer.sdp = modifiedAnswer.sdp.replace(/a=setup:active/g, 'a=setup:actpass');
  360. self.setLocalDescription(modifiedAnswer,
  361. function() {
  362. //console.log('modified setLocalDescription ok');
  363. if(successCallback){
  364. successCallback();
  365. }
  366. },
  367. function(error) {
  368. console.error('modified setLocalDescription failed', error);
  369. }
  370. );
  371. },
  372. function(error) {
  373. console.error('modified answer failed', error);
  374. }
  375. );
  376. },
  377. function(error) {
  378. console.error('modify failed', error);
  379. }
  380. );
  381. };
  382. TraceablePeerConnection.prototype.close = function () {
  383. this.trace('stop');
  384. if (this.statsinterval !== null) {
  385. window.clearInterval(this.statsinterval);
  386. this.statsinterval = null;
  387. }
  388. this.peerconnection.close();
  389. };
  390. TraceablePeerConnection.prototype.createOffer = function (successCallback, failureCallback, constraints) {
  391. var self = this;
  392. this.trace('createOffer', JSON.stringify(constraints, null, ' '));
  393. this.peerconnection.createOffer(
  394. function (offer) {
  395. self.trace('createOfferOnSuccess', dumpSDP(offer));
  396. successCallback(offer);
  397. },
  398. function(err) {
  399. self.trace('createOfferOnFailure', err);
  400. failureCallback(err);
  401. },
  402. constraints
  403. );
  404. };
  405. TraceablePeerConnection.prototype.createAnswer = function (successCallback, failureCallback, constraints) {
  406. var self = this;
  407. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  408. this.peerconnection.createAnswer(
  409. function (answer) {
  410. var simulcast = new Simulcast();
  411. answer = simulcast.transformAnswer(answer);
  412. self.trace('createAnswerOnSuccess', dumpSDP(answer));
  413. successCallback(answer);
  414. },
  415. function(err) {
  416. self.trace('createAnswerOnFailure', err);
  417. failureCallback(err);
  418. },
  419. constraints
  420. );
  421. };
  422. TraceablePeerConnection.prototype.addIceCandidate = function (candidate, successCallback, failureCallback) {
  423. var self = this;
  424. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  425. this.peerconnection.addIceCandidate(candidate);
  426. /* maybe later
  427. this.peerconnection.addIceCandidate(candidate,
  428. function () {
  429. self.trace('addIceCandidateOnSuccess');
  430. successCallback();
  431. },
  432. function (err) {
  433. self.trace('addIceCandidateOnFailure', err);
  434. failureCallback(err);
  435. }
  436. );
  437. */
  438. };
  439. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  440. if (navigator.mozGetUserMedia) {
  441. // ignore for now...
  442. } else {
  443. this.peerconnection.getStats(callback);
  444. }
  445. };
  446. // mozilla chrome compat layer -- very similar to adapter.js
  447. function setupRTC() {
  448. var RTC = null;
  449. if (navigator.mozGetUserMedia) {
  450. console.log('This appears to be Firefox');
  451. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  452. if (version >= 22) {
  453. RTC = {
  454. peerconnection: mozRTCPeerConnection,
  455. browser: 'firefox',
  456. getUserMedia: navigator.mozGetUserMedia.bind(navigator),
  457. attachMediaStream: function (element, stream) {
  458. element[0].mozSrcObject = stream;
  459. element[0].play();
  460. },
  461. pc_constraints: {}
  462. };
  463. if (!MediaStream.prototype.getVideoTracks)
  464. MediaStream.prototype.getVideoTracks = function () { return []; };
  465. if (!MediaStream.prototype.getAudioTracks)
  466. MediaStream.prototype.getAudioTracks = function () { return []; };
  467. RTCSessionDescription = mozRTCSessionDescription;
  468. RTCIceCandidate = mozRTCIceCandidate;
  469. }
  470. } else if (navigator.webkitGetUserMedia) {
  471. console.log('This appears to be Chrome');
  472. RTC = {
  473. peerconnection: webkitRTCPeerConnection,
  474. browser: 'chrome',
  475. getUserMedia: navigator.webkitGetUserMedia.bind(navigator),
  476. attachMediaStream: function (element, stream) {
  477. element.attr('src', webkitURL.createObjectURL(stream));
  478. },
  479. // DTLS should now be enabled by default but..
  480. pc_constraints: {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]}
  481. };
  482. if (navigator.userAgent.indexOf('Android') != -1) {
  483. RTC.pc_constraints = {}; // disable DTLS on Android
  484. }
  485. if (!webkitMediaStream.prototype.getVideoTracks) {
  486. webkitMediaStream.prototype.getVideoTracks = function () {
  487. return this.videoTracks;
  488. };
  489. }
  490. if (!webkitMediaStream.prototype.getAudioTracks) {
  491. webkitMediaStream.prototype.getAudioTracks = function () {
  492. return this.audioTracks;
  493. };
  494. }
  495. }
  496. if (RTC === null) {
  497. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  498. }
  499. return RTC;
  500. }
  501. function getUserMediaWithConstraints(um, success_callback, failure_callback, resolution, bandwidth, fps, desktopStream) {
  502. var constraints = {audio: false, video: false};
  503. if (um.indexOf('video') >= 0) {
  504. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  505. }
  506. if (um.indexOf('audio') >= 0) {
  507. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  508. }
  509. if (um.indexOf('screen') >= 0) {
  510. constraints.video = {
  511. mandatory: {
  512. chromeMediaSource: "screen",
  513. googLeakyBucket: true,
  514. maxWidth: window.screen.width,
  515. maxHeight: window.screen.height,
  516. maxFrameRate: 3
  517. },
  518. optional: []
  519. };
  520. }
  521. if (um.indexOf('desktop') >= 0) {
  522. constraints.video = {
  523. mandatory: {
  524. chromeMediaSource: "desktop",
  525. chromeMediaSourceId: desktopStream,
  526. googLeakyBucket: true,
  527. maxWidth: window.screen.width,
  528. maxHeight: window.screen.height,
  529. maxFrameRate: 3
  530. },
  531. optional: []
  532. }
  533. }
  534. if (constraints.audio) {
  535. // if it is good enough for hangouts...
  536. constraints.audio.optional.push(
  537. {googEchoCancellation: true},
  538. {googAutoGainControl: true},
  539. {googNoiseSupression: true},
  540. {googHighpassFilter: true},
  541. {googNoisesuppression2: true},
  542. {googEchoCancellation2: true},
  543. {googAutoGainControl2: true}
  544. );
  545. }
  546. if (constraints.video) {
  547. constraints.video.optional.push(
  548. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  549. );
  550. if (um.indexOf('video') >= 0) {
  551. constraints.video.optional.push(
  552. {googLeakyBucket: true}
  553. );
  554. }
  555. }
  556. // Check if we are running on Android device
  557. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  558. if (resolution && !constraints.video || isAndroid) {
  559. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  560. }
  561. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  562. switch (resolution) {
  563. // 16:9 first
  564. case '1080':
  565. case 'fullhd':
  566. constraints.video.mandatory.minWidth = 1920;
  567. constraints.video.mandatory.minHeight = 1080;
  568. break;
  569. case '720':
  570. case 'hd':
  571. constraints.video.mandatory.minWidth = 1280;
  572. constraints.video.mandatory.minHeight = 720;
  573. break;
  574. case '360':
  575. constraints.video.mandatory.minWidth = 640;
  576. constraints.video.mandatory.minHeight = 360;
  577. break;
  578. case '180':
  579. constraints.video.mandatory.minWidth = 320;
  580. constraints.video.mandatory.minHeight = 180;
  581. break;
  582. // 4:3
  583. case '960':
  584. constraints.video.mandatory.minWidth = 960;
  585. constraints.video.mandatory.minHeight = 720;
  586. break;
  587. case '640':
  588. case 'vga':
  589. constraints.video.mandatory.minWidth = 640;
  590. constraints.video.mandatory.minHeight = 480;
  591. break;
  592. case '320':
  593. constraints.video.mandatory.minWidth = 320;
  594. constraints.video.mandatory.minHeight = 240;
  595. break;
  596. default:
  597. if (isAndroid) {
  598. constraints.video.mandatory.minWidth = 320;
  599. constraints.video.mandatory.minHeight = 240;
  600. constraints.video.mandatory.maxFrameRate = 15;
  601. }
  602. break;
  603. }
  604. if (constraints.video.mandatory.minWidth)
  605. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  606. if (constraints.video.mandatory.minHeight)
  607. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  608. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  609. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  610. constraints.video.optional.push({bandwidth: bandwidth});
  611. }
  612. if (fps) { // for some cameras it might be necessary to request 30fps
  613. // so they choose 30fps mjpg over 10fps yuy2
  614. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  615. constraints.video.mandatory.minFrameRate = fps;
  616. }
  617. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  618. try {
  619. if (config.enableSimulcast
  620. && constraints.video
  621. && constraints.video.chromeMediaSource !== 'screen'
  622. && constraints.video.chromeMediaSource !== 'desktop'
  623. && !isAndroid
  624. // We currently do not support FF, as it doesn't have multistream support.
  625. && !isFF) {
  626. var simulcast = new Simulcast();
  627. simulcast.getUserMedia(constraints, function (stream) {
  628. console.log('onUserMediaSuccess');
  629. success_callback(stream);
  630. },
  631. function (error) {
  632. console.warn('Failed to get access to local media. Error ', error);
  633. if (failure_callback) {
  634. failure_callback(error);
  635. }
  636. });
  637. } else {
  638. RTC.getUserMedia(constraints,
  639. function (stream) {
  640. console.log('onUserMediaSuccess');
  641. success_callback(stream);
  642. },
  643. function (error) {
  644. console.warn('Failed to get access to local media. Error ', error);
  645. if (failure_callback) {
  646. failure_callback(error);
  647. }
  648. });
  649. }
  650. } catch (e) {
  651. console.error('GUM failed: ', e);
  652. if(failure_callback) {
  653. failure_callback(e);
  654. }
  655. }
  656. }