Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

strophe.jingle.adapter.js 26KB

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