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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 publicLocalDescription = simulcast.reverseTransformLocalDescription(this.peerconnection.localDescription);
  127. return publicLocalDescription;
  128. });
  129. TraceablePeerConnection.prototype.__defineGetter__('remoteDescription', function() {
  130. var publicRemoteDescription = simulcast.reverseTransformRemoteDescription(this.peerconnection.remoteDescription);
  131. return publicRemoteDescription;
  132. });
  133. }
  134. TraceablePeerConnection.prototype.addStream = function (stream) {
  135. this.trace('addStream', stream.id);
  136. this.peerconnection.addStream(stream);
  137. };
  138. TraceablePeerConnection.prototype.removeStream = function (stream) {
  139. this.trace('removeStream', stream.id);
  140. this.peerconnection.removeStream(stream);
  141. };
  142. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  143. this.trace('createDataChannel', label, opts);
  144. return this.peerconnection.createDataChannel(label, opts);
  145. };
  146. TraceablePeerConnection.prototype.setLocalDescription = function (description, successCallback, failureCallback) {
  147. var self = this;
  148. description = simulcast.transformLocalDescription(description);
  149. this.trace('setLocalDescription', dumpSDP(description));
  150. this.peerconnection.setLocalDescription(description,
  151. function () {
  152. self.trace('setLocalDescriptionOnSuccess');
  153. successCallback();
  154. },
  155. function (err) {
  156. self.trace('setLocalDescriptionOnFailure', err);
  157. failureCallback(err);
  158. }
  159. );
  160. /*
  161. if (this.statsinterval === null && this.maxstats > 0) {
  162. // start gathering stats
  163. }
  164. */
  165. };
  166. TraceablePeerConnection.prototype.setRemoteDescription = function (description, successCallback, failureCallback) {
  167. var self = this;
  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. answer = simulcast.transformAnswer(answer);
  411. self.trace('createAnswerOnSuccess', dumpSDP(answer));
  412. successCallback(answer);
  413. },
  414. function(err) {
  415. self.trace('createAnswerOnFailure', err);
  416. failureCallback(err);
  417. },
  418. constraints
  419. );
  420. };
  421. TraceablePeerConnection.prototype.addIceCandidate = function (candidate, successCallback, failureCallback) {
  422. var self = this;
  423. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  424. this.peerconnection.addIceCandidate(candidate);
  425. /* maybe later
  426. this.peerconnection.addIceCandidate(candidate,
  427. function () {
  428. self.trace('addIceCandidateOnSuccess');
  429. successCallback();
  430. },
  431. function (err) {
  432. self.trace('addIceCandidateOnFailure', err);
  433. failureCallback(err);
  434. }
  435. );
  436. */
  437. };
  438. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  439. if (navigator.mozGetUserMedia) {
  440. // ignore for now...
  441. } else {
  442. this.peerconnection.getStats(callback);
  443. }
  444. };
  445. // mozilla chrome compat layer -- very similar to adapter.js
  446. function setupRTC() {
  447. var RTC = null;
  448. if (navigator.mozGetUserMedia) {
  449. console.log('This appears to be Firefox');
  450. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  451. if (version >= 22) {
  452. RTC = {
  453. peerconnection: mozRTCPeerConnection,
  454. browser: 'firefox',
  455. getUserMedia: navigator.mozGetUserMedia.bind(navigator),
  456. attachMediaStream: function (element, stream) {
  457. element[0].mozSrcObject = stream;
  458. element[0].play();
  459. },
  460. pc_constraints: {}
  461. };
  462. if (!MediaStream.prototype.getVideoTracks)
  463. MediaStream.prototype.getVideoTracks = function () { return []; };
  464. if (!MediaStream.prototype.getAudioTracks)
  465. MediaStream.prototype.getAudioTracks = function () { return []; };
  466. RTCSessionDescription = mozRTCSessionDescription;
  467. RTCIceCandidate = mozRTCIceCandidate;
  468. }
  469. } else if (navigator.webkitGetUserMedia) {
  470. console.log('This appears to be Chrome');
  471. RTC = {
  472. peerconnection: webkitRTCPeerConnection,
  473. browser: 'chrome',
  474. getUserMedia: navigator.webkitGetUserMedia.bind(navigator),
  475. attachMediaStream: function (element, stream) {
  476. element.attr('src', webkitURL.createObjectURL(stream));
  477. },
  478. // DTLS should now be enabled by default but..
  479. pc_constraints: {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]}
  480. };
  481. if (navigator.userAgent.indexOf('Android') != -1) {
  482. RTC.pc_constraints = {}; // disable DTLS on Android
  483. }
  484. if (!webkitMediaStream.prototype.getVideoTracks) {
  485. webkitMediaStream.prototype.getVideoTracks = function () {
  486. return this.videoTracks;
  487. };
  488. }
  489. if (!webkitMediaStream.prototype.getAudioTracks) {
  490. webkitMediaStream.prototype.getAudioTracks = function () {
  491. return this.audioTracks;
  492. };
  493. }
  494. }
  495. if (RTC === null) {
  496. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  497. }
  498. return RTC;
  499. }
  500. function getUserMediaWithConstraints(um, success_callback, failure_callback, resolution, bandwidth, fps, desktopStream) {
  501. var constraints = {audio: false, video: false};
  502. if (um.indexOf('video') >= 0) {
  503. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  504. }
  505. if (um.indexOf('audio') >= 0) {
  506. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  507. }
  508. if (um.indexOf('screen') >= 0) {
  509. constraints.video = {
  510. mandatory: {
  511. chromeMediaSource: "screen",
  512. googLeakyBucket: true,
  513. maxWidth: window.screen.width,
  514. maxHeight: window.screen.height,
  515. maxFrameRate: 3
  516. },
  517. optional: []
  518. };
  519. }
  520. if (um.indexOf('desktop') >= 0) {
  521. constraints.video = {
  522. mandatory: {
  523. chromeMediaSource: "desktop",
  524. chromeMediaSourceId: desktopStream,
  525. googLeakyBucket: true,
  526. maxWidth: window.screen.width,
  527. maxHeight: window.screen.height,
  528. maxFrameRate: 3
  529. },
  530. optional: []
  531. }
  532. }
  533. if (constraints.audio) {
  534. // if it is good enough for hangouts...
  535. constraints.audio.optional.push(
  536. {googEchoCancellation: true},
  537. {googAutoGainControl: true},
  538. {googNoiseSupression: true},
  539. {googHighpassFilter: true},
  540. {googNoisesuppression2: true},
  541. {googEchoCancellation2: true},
  542. {googAutoGainControl2: true}
  543. );
  544. }
  545. if (constraints.video) {
  546. constraints.video.optional.push(
  547. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  548. );
  549. if (um.indexOf('video') >= 0) {
  550. constraints.video.optional.push(
  551. {googLeakyBucket: true}
  552. );
  553. }
  554. }
  555. // Check if we are running on Android device
  556. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  557. if (resolution && !constraints.video || isAndroid) {
  558. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  559. }
  560. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  561. switch (resolution) {
  562. // 16:9 first
  563. case '1080':
  564. case 'fullhd':
  565. constraints.video.mandatory.minWidth = 1920;
  566. constraints.video.mandatory.minHeight = 1080;
  567. break;
  568. case '720':
  569. case 'hd':
  570. constraints.video.mandatory.minWidth = 1280;
  571. constraints.video.mandatory.minHeight = 720;
  572. break;
  573. case '360':
  574. constraints.video.mandatory.minWidth = 640;
  575. constraints.video.mandatory.minHeight = 360;
  576. break;
  577. case '180':
  578. constraints.video.mandatory.minWidth = 320;
  579. constraints.video.mandatory.minHeight = 180;
  580. break;
  581. // 4:3
  582. case '960':
  583. constraints.video.mandatory.minWidth = 960;
  584. constraints.video.mandatory.minHeight = 720;
  585. break;
  586. case '640':
  587. case 'vga':
  588. constraints.video.mandatory.minWidth = 640;
  589. constraints.video.mandatory.minHeight = 480;
  590. break;
  591. case '320':
  592. constraints.video.mandatory.minWidth = 320;
  593. constraints.video.mandatory.minHeight = 240;
  594. break;
  595. default:
  596. if (isAndroid) {
  597. constraints.video.mandatory.minWidth = 320;
  598. constraints.video.mandatory.minHeight = 240;
  599. constraints.video.mandatory.maxFrameRate = 15;
  600. }
  601. break;
  602. }
  603. if (constraints.video.mandatory.minWidth)
  604. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  605. if (constraints.video.mandatory.minHeight)
  606. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  607. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  608. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  609. constraints.video.optional.push({bandwidth: bandwidth});
  610. }
  611. if (fps) { // for some cameras it might be necessary to request 30fps
  612. // so they choose 30fps mjpg over 10fps yuy2
  613. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  614. constraints.video.mandatory.minFrameRate = fps;
  615. }
  616. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  617. try {
  618. if (config.enableSimulcast
  619. && constraints.video
  620. && constraints.video.chromeMediaSource !== 'screen'
  621. && constraints.video.chromeMediaSource !== 'desktop'
  622. && !isAndroid
  623. // We currently do not support FF, as it doesn't have multistream support.
  624. && !isFF) {
  625. simulcast.getUserMedia(constraints, function (stream) {
  626. console.log('onUserMediaSuccess');
  627. success_callback(stream);
  628. },
  629. function (error) {
  630. console.warn('Failed to get access to local media. Error ', error);
  631. if (failure_callback) {
  632. failure_callback(error);
  633. }
  634. });
  635. } else {
  636. RTC.getUserMedia(constraints,
  637. function (stream) {
  638. console.log('onUserMediaSuccess');
  639. success_callback(stream);
  640. },
  641. function (error) {
  642. console.warn('Failed to get access to local media. Error ', error);
  643. if (failure_callback) {
  644. failure_callback(error);
  645. }
  646. });
  647. }
  648. } catch (e) {
  649. console.error('GUM failed: ', e);
  650. if(failure_callback) {
  651. failure_callback(e);
  652. }
  653. }
  654. }