Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

strophe.jingle.adapter.js 24KB

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