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.

RTCUtils.js 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* global config, require, attachMediaStream, getUserMedia,
  2. RTCPeerConnection, RTCSessionDescription, RTCIceCandidate, MediaStreamTrack,
  3. mozRTCPeerConnection, mozRTCSessionDescription, mozRTCIceCandidate,
  4. webkitRTCPeerConnection, webkitMediaStream, webkitURL
  5. */
  6. /* jshint -W101 */
  7. var logger = require("jitsi-meet-logger").getLogger(__filename);
  8. var RTCBrowserType = require("./RTCBrowserType");
  9. var Resolutions = require("../../service/RTC/Resolutions");
  10. var RTCEvents = require("../../service/RTC/RTCEvents");
  11. var AdapterJS = require("./adapter.screenshare");
  12. var SDPUtil = require("../xmpp/SDPUtil");
  13. var EventEmitter = require("events");
  14. var screenObtainer = require("./ScreenObtainer");
  15. var JitsiTrackErrors = require("../../JitsiTrackErrors");
  16. var MediaType = require("../../service/RTC/MediaType");
  17. var eventEmitter = new EventEmitter();
  18. var devices = {
  19. audio: true,
  20. video: true
  21. };
  22. var rtcReady = false;
  23. function setResolutionConstraints(constraints, resolution) {
  24. var isAndroid = RTCBrowserType.isAndroid();
  25. if (Resolutions[resolution]) {
  26. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  27. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  28. }
  29. else if (isAndroid) {
  30. // FIXME can't remember if the purpose of this was to always request
  31. // low resolution on Android ? if yes it should be moved up front
  32. constraints.video.mandatory.minWidth = 320;
  33. constraints.video.mandatory.minHeight = 180;
  34. constraints.video.mandatory.maxFrameRate = 15;
  35. }
  36. if (constraints.video.mandatory.minWidth)
  37. constraints.video.mandatory.maxWidth =
  38. constraints.video.mandatory.minWidth;
  39. if (constraints.video.mandatory.minHeight)
  40. constraints.video.mandatory.maxHeight =
  41. constraints.video.mandatory.minHeight;
  42. }
  43. /**
  44. * @param {string[]} um required user media types
  45. *
  46. * @param {Object} [options={}] optional parameters
  47. * @param {string} options.resolution
  48. * @param {number} options.bandwidth
  49. * @param {number} options.fps
  50. * @param {string} options.desktopStream
  51. * @param {string} options.cameraDeviceId
  52. * @param {string} options.micDeviceId
  53. * @param {bool} firefox_fake_device
  54. */
  55. function getConstraints(um, options) {
  56. var constraints = {audio: false, video: false};
  57. if (um.indexOf('video') >= 0) {
  58. // same behaviour as true
  59. constraints.video = { mandatory: {}, optional: [] };
  60. if (options.cameraDeviceId) {
  61. // new style of settings device id (FF only)
  62. constraints.video.deviceId = options.cameraDeviceId;
  63. // old style
  64. constraints.video.optional.push({
  65. sourceId: options.cameraDeviceId
  66. });
  67. }
  68. constraints.video.optional.push({ googLeakyBucket: true });
  69. setResolutionConstraints(constraints, options.resolution);
  70. }
  71. if (um.indexOf('audio') >= 0) {
  72. if (!RTCBrowserType.isFirefox()) {
  73. // same behaviour as true
  74. constraints.audio = { mandatory: {}, optional: []};
  75. if (options.micDeviceId) {
  76. // new style of settings device id (FF only)
  77. constraints.audio.deviceId = options.micDeviceId;
  78. // old style
  79. constraints.audio.optional.push({
  80. sourceId: options.micDeviceId
  81. });
  82. }
  83. // if it is good enough for hangouts...
  84. constraints.audio.optional.push(
  85. {googEchoCancellation: true},
  86. {googAutoGainControl: true},
  87. {googNoiseSupression: true},
  88. {googHighpassFilter: true},
  89. {googNoisesuppression2: true},
  90. {googEchoCancellation2: true},
  91. {googAutoGainControl2: true}
  92. );
  93. } else {
  94. if (options.micDeviceId) {
  95. constraints.audio = {
  96. mandatory: {},
  97. deviceId: options.micDeviceId, // new style
  98. optional: [{
  99. sourceId: options.micDeviceId // old style
  100. }]};
  101. } else {
  102. constraints.audio = true;
  103. }
  104. }
  105. }
  106. if (um.indexOf('screen') >= 0) {
  107. if (RTCBrowserType.isChrome()) {
  108. constraints.video = {
  109. mandatory: {
  110. chromeMediaSource: "screen",
  111. googLeakyBucket: true,
  112. maxWidth: window.screen.width,
  113. maxHeight: window.screen.height,
  114. maxFrameRate: 3
  115. },
  116. optional: []
  117. };
  118. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  119. constraints.video = {
  120. optional: [
  121. {
  122. sourceId: AdapterJS.WebRTCPlugin.plugin.screensharingKey
  123. }
  124. ]
  125. };
  126. } else if (RTCBrowserType.isFirefox()) {
  127. constraints.video = {
  128. mozMediaSource: "window",
  129. mediaSource: "window"
  130. };
  131. } else {
  132. logger.error(
  133. "'screen' WebRTC media source is supported only in Chrome" +
  134. " and with Temasys plugin");
  135. }
  136. }
  137. if (um.indexOf('desktop') >= 0) {
  138. constraints.video = {
  139. mandatory: {
  140. chromeMediaSource: "desktop",
  141. chromeMediaSourceId: options.desktopStream,
  142. googLeakyBucket: true,
  143. maxWidth: window.screen.width,
  144. maxHeight: window.screen.height,
  145. maxFrameRate: 3
  146. },
  147. optional: []
  148. };
  149. }
  150. if (options.bandwidth) {
  151. if (!constraints.video) {
  152. //same behaviour as true
  153. constraints.video = {mandatory: {}, optional: []};
  154. }
  155. constraints.video.optional.push({bandwidth: options.bandwidth});
  156. }
  157. if (options.fps) {
  158. // for some cameras it might be necessary to request 30fps
  159. // so they choose 30fps mjpg over 10fps yuy2
  160. if (!constraints.video) {
  161. // same behaviour as true;
  162. constraints.video = {mandatory: {}, optional: []};
  163. }
  164. constraints.video.mandatory.minFrameRate = options.fps;
  165. }
  166. // we turn audio for both audio and video tracks, the fake audio & video seems to work
  167. // only when enabled in one getUserMedia call, we cannot get fake audio separate by fake video
  168. // this later can be a problem with some of the tests
  169. if(RTCBrowserType.isFirefox() && options.firefox_fake_device)
  170. {
  171. // seems to be fixed now, removing this experimental fix, as having
  172. // multiple audio tracks brake the tests
  173. //constraints.audio = true;
  174. constraints.fake = true;
  175. }
  176. return constraints;
  177. }
  178. function setAvailableDevices(um, available) {
  179. if (um.indexOf("video") != -1) {
  180. devices.video = available;
  181. }
  182. if (um.indexOf("audio") != -1) {
  183. devices.audio = available;
  184. }
  185. eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, devices);
  186. }
  187. // In case of IE we continue from 'onReady' callback
  188. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  189. // once it is initialized.
  190. function onReady (options, GUM) {
  191. rtcReady = true;
  192. eventEmitter.emit(RTCEvents.RTC_READY, true);
  193. screenObtainer.init(options, GUM);
  194. }
  195. /**
  196. * Apply function with arguments if function exists.
  197. * Do nothing if function not provided.
  198. * @param {function} [fn] function to apply
  199. * @param {Array} [args=[]] arguments for function
  200. */
  201. function maybeApply(fn, args) {
  202. if (fn) {
  203. fn.apply(null, args || []);
  204. }
  205. }
  206. var getUserMediaStatus = {
  207. initialized: false,
  208. callbacks: []
  209. };
  210. /**
  211. * Wrap `getUserMedia` to allow others to know if it was executed at least
  212. * once or not. Wrapper function uses `getUserMediaStatus` object.
  213. * @param {Function} getUserMedia native function
  214. * @returns {Function} wrapped function
  215. */
  216. function wrapGetUserMedia(getUserMedia) {
  217. return function (constraints, successCallback, errorCallback) {
  218. getUserMedia(constraints, function (stream) {
  219. maybeApply(successCallback, [stream]);
  220. if (!getUserMediaStatus.initialized) {
  221. getUserMediaStatus.initialized = true;
  222. getUserMediaStatus.callbacks.forEach(function (callback) {
  223. callback();
  224. });
  225. getUserMediaStatus.callbacks.length = 0;
  226. }
  227. }, function (error) {
  228. maybeApply(errorCallback, [error]);
  229. });
  230. };
  231. }
  232. /**
  233. * Create stub device which equals to auto selected device.
  234. * @param {string} kind if that should be `audio` or `video` device
  235. * @returns {Object} stub device description in `enumerateDevices` format
  236. */
  237. function createAutoDeviceInfo(kind) {
  238. return {
  239. facing: null,
  240. label: 'Auto',
  241. kind: kind,
  242. deviceId: '',
  243. groupId: ''
  244. };
  245. }
  246. /**
  247. * Execute function after getUserMedia was executed at least once.
  248. * @param {Function} callback function to execute after getUserMedia
  249. */
  250. function afterUserMediaInitialized(callback) {
  251. if (getUserMediaStatus.initialized) {
  252. callback();
  253. } else {
  254. getUserMediaStatus.callbacks.push(callback);
  255. }
  256. }
  257. /**
  258. * Wrapper function which makes enumerateDevices to wait
  259. * until someone executes getUserMedia first time.
  260. * @param {Function} enumerateDevices native function
  261. * @returns {Funtion} wrapped function
  262. */
  263. function wrapEnumerateDevices(enumerateDevices) {
  264. return function (callback) {
  265. // enumerate devices only after initial getUserMedia
  266. afterUserMediaInitialized(function () {
  267. enumerateDevices().then(function (devices) {
  268. //add auto devices
  269. devices.unshift(
  270. createAutoDeviceInfo('audioinput'),
  271. createAutoDeviceInfo('videoinput')
  272. );
  273. callback(devices);
  274. }, function (err) {
  275. console.error('cannot enumerate devices: ', err);
  276. // return only auto devices
  277. callback([createAutoDeviceInfo('audioinput'),
  278. createAutoDeviceInfo('videoinput')]);
  279. });
  280. });
  281. };
  282. }
  283. /**
  284. * Use old MediaStreamTrack to get devices list and
  285. * convert it to enumerateDevices format.
  286. * @param {Function} callback function to call when received devices list.
  287. */
  288. function enumerateDevicesThroughMediaStreamTrack (callback) {
  289. MediaStreamTrack.getSources(function (sources) {
  290. var devices = sources.map(function (source) {
  291. var kind = (source.kind || '').toLowerCase();
  292. return {
  293. facing: source.facing || null,
  294. label: source.label,
  295. kind: kind ? kind + 'input': null,
  296. deviceId: source.id,
  297. groupId: source.groupId || null
  298. };
  299. });
  300. //add auto devices
  301. devices.unshift(
  302. createAutoDeviceInfo('audioinput'),
  303. createAutoDeviceInfo('videoinput')
  304. );
  305. callback(devices);
  306. });
  307. }
  308. function obtainDevices(options) {
  309. if(!options.devices || options.devices.length === 0) {
  310. return options.successCallback(options.streams || {});
  311. }
  312. var device = options.devices.splice(0, 1);
  313. var devices = [];
  314. devices.push(device);
  315. options.deviceGUM[device](function (stream) {
  316. options.streams = options.streams || {};
  317. options.streams[device] = stream;
  318. obtainDevices(options);
  319. },
  320. function (error) {
  321. Object.keys(options.streams).forEach(function(device) {
  322. RTCUtils.stopMediaStream(options.streams[device]);
  323. });
  324. logger.error(
  325. "failed to obtain " + device + " stream - stop", error);
  326. options.errorCallback(JitsiTrackErrors.parseError(error, devices));
  327. });
  328. }
  329. /**
  330. * Handles the newly created Media Streams.
  331. * @param streams the new Media Streams
  332. * @param resolution the resolution of the video streams
  333. * @returns {*[]} object that describes the new streams
  334. */
  335. function handleLocalStream(streams, resolution) {
  336. var audioStream, videoStream, desktopStream, res = [];
  337. // If this is FF, the stream parameter is *not* a MediaStream object, it's
  338. // an object with two properties: audioStream, videoStream.
  339. if (window.webkitMediaStream) {
  340. var audioVideo = streams.audioVideo;
  341. if (audioVideo) {
  342. var audioTracks = audioVideo.getAudioTracks();
  343. if(audioTracks.length) {
  344. audioStream = new webkitMediaStream();
  345. for (var i = 0; i < audioTracks.length; i++) {
  346. audioStream.addTrack(audioTracks[i]);
  347. }
  348. }
  349. var videoTracks = audioVideo.getVideoTracks();
  350. if(videoTracks.length) {
  351. videoStream = new webkitMediaStream();
  352. for (var j = 0; j < videoTracks.length; j++) {
  353. videoStream.addTrack(videoTracks[j]);
  354. }
  355. }
  356. }
  357. if (streams && streams.desktopStream)
  358. desktopStream = streams.desktopStream;
  359. }
  360. else if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed()) { // Firefox and Temasys plugin
  361. if (streams && streams.audio)
  362. audioStream = streams.audio;
  363. if (streams && streams.video)
  364. videoStream = streams.video;
  365. if(streams && streams.desktop)
  366. desktopStream = streams.desktop;
  367. }
  368. if (desktopStream)
  369. res.push({stream: desktopStream,
  370. type: MediaType.VIDEO, videoType: "desktop"});
  371. if(audioStream)
  372. res.push({stream: audioStream, type: MediaType.AUDIO, videoType: null});
  373. if(videoStream)
  374. res.push({stream: videoStream, type: MediaType.VIDEO, videoType: "camera",
  375. resolution: resolution});
  376. return res;
  377. }
  378. //Options parameter is to pass config options. Currently uses only "useIPv6".
  379. var RTCUtils = {
  380. init: function (options) {
  381. return new Promise(function(resolve, reject) {
  382. if (RTCBrowserType.isFirefox()) {
  383. var FFversion = RTCBrowserType.getFirefoxVersion();
  384. if (FFversion < 40) {
  385. logger.error(
  386. "Firefox version too old: " + FFversion +
  387. ". Required >= 40.");
  388. reject(new Error("Firefox version too old: " + FFversion +
  389. ". Required >= 40."));
  390. return;
  391. }
  392. this.peerconnection = mozRTCPeerConnection;
  393. this.getUserMedia = wrapGetUserMedia(navigator.mozGetUserMedia.bind(navigator));
  394. this.enumerateDevices = wrapEnumerateDevices(
  395. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  396. );
  397. this.pc_constraints = {};
  398. this.attachMediaStream = function (element, stream) {
  399. // srcObject is being standardized and FF will eventually
  400. // support that unprefixed. FF also supports the
  401. // "element.src = URL.createObjectURL(...)" combo, but that
  402. // will be deprecated in favour of srcObject.
  403. //
  404. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  405. // https://github.com/webrtc/samples/issues/302
  406. if (!element)
  407. return;
  408. element.mozSrcObject = stream;
  409. element.play();
  410. return element;
  411. };
  412. this.getStreamID = function (stream) {
  413. var id = stream.id;
  414. if (!id) {
  415. var tracks = stream.getVideoTracks();
  416. if (!tracks || tracks.length === 0) {
  417. tracks = stream.getAudioTracks();
  418. }
  419. id = tracks[0].id;
  420. }
  421. return SDPUtil.filter_special_chars(id);
  422. };
  423. this.getVideoSrc = function (element) {
  424. if (!element)
  425. return null;
  426. return element.mozSrcObject;
  427. };
  428. this.setVideoSrc = function (element, src) {
  429. if (element)
  430. element.mozSrcObject = src;
  431. };
  432. RTCSessionDescription = mozRTCSessionDescription;
  433. RTCIceCandidate = mozRTCIceCandidate;
  434. } else if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera() || RTCBrowserType.isNWJS()) {
  435. this.peerconnection = webkitRTCPeerConnection;
  436. var getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  437. if (navigator.mediaDevices) {
  438. this.getUserMedia = wrapGetUserMedia(getUserMedia);
  439. this.enumerateDevices = wrapEnumerateDevices(
  440. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  441. );
  442. } else {
  443. this.getUserMedia = getUserMedia;
  444. this.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  445. }
  446. this.attachMediaStream = function (element, stream) {
  447. // saves the created url for the stream, so we can reuse it
  448. // and not keep creating urls
  449. if (!stream.jitsiObjectURL) {
  450. stream.jitsiObjectURL
  451. = webkitURL.createObjectURL(stream);
  452. }
  453. element.src = stream.jitsiObjectURL;
  454. return element;
  455. };
  456. this.getStreamID = function (stream) {
  457. // streams from FF endpoints have the characters '{' and '}'
  458. // that make jQuery choke.
  459. return SDPUtil.filter_special_chars(stream.id);
  460. };
  461. this.getVideoSrc = function (element) {
  462. if (!element)
  463. return null;
  464. return element.getAttribute("src");
  465. };
  466. this.setVideoSrc = function (element, src) {
  467. if (!src) {
  468. src = '';
  469. }
  470. if (element)
  471. element.setAttribute("src", src);
  472. };
  473. // DTLS should now be enabled by default but..
  474. this.pc_constraints = {'optional': [
  475. {'DtlsSrtpKeyAgreement': 'true'}
  476. ]};
  477. if (options.useIPv6) {
  478. // https://code.google.com/p/webrtc/issues/detail?id=2828
  479. this.pc_constraints.optional.push({googIPv6: true});
  480. }
  481. if (RTCBrowserType.isAndroid()) {
  482. this.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. // Detect IE/Safari
  496. else if (RTCBrowserType.isTemasysPluginUsed()) {
  497. //AdapterJS.WebRTCPlugin.setLogLevel(
  498. // AdapterJS.WebRTCPlugin.PLUGIN_LOG_LEVELS.VERBOSE);
  499. var self = this;
  500. AdapterJS.webRTCReady(function (isPlugin) {
  501. self.peerconnection = RTCPeerConnection;
  502. self.getUserMedia = window.getUserMedia;
  503. self.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  504. self.attachMediaStream = function (element, stream) {
  505. if (stream.id === "dummyAudio" || stream.id === "dummyVideo") {
  506. return;
  507. }
  508. var isVideoStream = !!stream.getVideoTracks().length;
  509. if (isVideoStream && !$(element).is(':visible')) {
  510. throw new Error('video element must be visible to attach video stream');
  511. }
  512. return attachMediaStream(element, stream);
  513. };
  514. self.getStreamID = function (stream) {
  515. var id = SDPUtil.filter_special_chars(stream.label);
  516. return id;
  517. };
  518. self.getVideoSrc = function (element) {
  519. if (!element) {
  520. logger.warn("Attempt to get video SRC of null element");
  521. return null;
  522. }
  523. var children = element.children;
  524. for (var i = 0; i !== children.length; ++i) {
  525. if (children[i].name === 'streamId') {
  526. return children[i].value;
  527. }
  528. }
  529. //logger.info(element.id + " SRC: " + src);
  530. return null;
  531. };
  532. self.setVideoSrc = function (element, src) {
  533. //logger.info("Set video src: ", element, src);
  534. if (!src) {
  535. attachMediaStream(element, null);
  536. } else {
  537. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  538. var stream
  539. = AdapterJS.WebRTCPlugin.plugin
  540. .getStreamWithId(
  541. AdapterJS.WebRTCPlugin.pageId, src);
  542. attachMediaStream(element, stream);
  543. }
  544. };
  545. onReady(options, self.getUserMediaWithConstraints);
  546. resolve();
  547. });
  548. } else {
  549. try {
  550. logger.error('Browser does not appear to be WebRTC-capable');
  551. } catch (e) {
  552. }
  553. reject('Browser does not appear to be WebRTC-capable');
  554. return;
  555. }
  556. // Call onReady() if Temasys plugin is not used
  557. if (!RTCBrowserType.isTemasysPluginUsed()) {
  558. onReady(options, this.getUserMediaWithConstraints);
  559. resolve();
  560. }
  561. }.bind(this));
  562. },
  563. /**
  564. * @param {string[]} um required user media types
  565. * @param {function} success_callback
  566. * @param {Function} failure_callback
  567. * @param {Object} [options] optional parameters
  568. * @param {string} options.resolution
  569. * @param {number} options.bandwidth
  570. * @param {number} options.fps
  571. * @param {string} options.desktopStream
  572. * @param {string} options.cameraDeviceId
  573. * @param {string} options.micDeviceId
  574. **/
  575. getUserMediaWithConstraints: function ( um, success_callback, failure_callback, options) {
  576. options = options || {};
  577. var resolution = options.resolution;
  578. var constraints = getConstraints(um, options);
  579. logger.info("Get media constraints", constraints);
  580. try {
  581. this.getUserMedia(constraints,
  582. function (stream) {
  583. logger.log('onUserMediaSuccess');
  584. setAvailableDevices(um, true);
  585. success_callback(stream);
  586. },
  587. function (error) {
  588. setAvailableDevices(um, false);
  589. logger.warn('Failed to get access to local media. Error ',
  590. error, constraints);
  591. if (failure_callback) {
  592. failure_callback(error, resolution);
  593. }
  594. });
  595. } catch (e) {
  596. logger.error('GUM failed: ', e);
  597. if (failure_callback) {
  598. failure_callback(e);
  599. }
  600. }
  601. },
  602. /**
  603. * Creates the local MediaStreams.
  604. * @param {Object} [options] optional parameters
  605. * @param {Array} options.devices the devices that will be requested
  606. * @param {string} options.resolution resolution constraints
  607. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  608. * type: "audio" or "video", videoType: "camera" or "desktop"}
  609. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  610. * @param {string} options.cameraDeviceId
  611. * @param {string} options.micDeviceId
  612. * @returns {*} Promise object that will receive the new JitsiTracks
  613. */
  614. obtainAudioAndVideoPermissions: function (options) {
  615. var self = this;
  616. options = options || {};
  617. return new Promise(function (resolve, reject) {
  618. var successCallback = function (stream) {
  619. resolve(handleLocalStream(stream, options.resolution));
  620. };
  621. options.devices = options.devices || ['audio', 'video'];
  622. if(!screenObtainer.isSupported()
  623. && options.devices.indexOf("desktop") !== -1){
  624. reject(new Error("Desktop sharing is not supported!"));
  625. }
  626. if (RTCBrowserType.isFirefox() ||
  627. RTCBrowserType.isTemasysPluginUsed()) {
  628. var GUM = function (device, s, e) {
  629. this.getUserMediaWithConstraints(device, s, e, options);
  630. };
  631. var deviceGUM = {
  632. "audio": GUM.bind(self, ["audio"]),
  633. "video": GUM.bind(self, ["video"])
  634. };
  635. if(screenObtainer.isSupported()){
  636. deviceGUM["desktop"] = screenObtainer.obtainStream.bind(
  637. screenObtainer);
  638. }
  639. // With FF/IE we can't split the stream into audio and video because FF
  640. // doesn't support media stream constructors. So, we need to get the
  641. // audio stream separately from the video stream using two distinct GUM
  642. // calls. Not very user friendly :-( but we don't have many other
  643. // options neither.
  644. //
  645. // Note that we pack those 2 streams in a single object and pass it to
  646. // the successCallback method.
  647. obtainDevices({
  648. devices: options.devices,
  649. streams: [],
  650. successCallback: successCallback,
  651. errorCallback: reject,
  652. deviceGUM: deviceGUM
  653. });
  654. } else {
  655. var hasDesktop = options.devices.indexOf('desktop') > -1;
  656. if (hasDesktop) {
  657. options.devices.splice(options.devices.indexOf("desktop"), 1);
  658. }
  659. options.resolution = options.resolution || '360';
  660. if(options.devices.length) {
  661. this.getUserMediaWithConstraints(
  662. options.devices,
  663. function (stream) {
  664. if((options.devices.indexOf("audio") !== -1 &&
  665. !stream.getAudioTracks().length) ||
  666. (options.devices.indexOf("video") !== -1 &&
  667. !stream.getVideoTracks().length))
  668. {
  669. self.stopMediaStream(stream);
  670. reject(JitsiTrackErrors.parseError(
  671. new Error("Unable to get the audio and " +
  672. "video tracks."),
  673. options.devices));
  674. return;
  675. }
  676. if(hasDesktop) {
  677. screenObtainer.obtainStream(
  678. function (desktopStream) {
  679. successCallback({audioVideo: stream,
  680. desktopStream: desktopStream});
  681. }, function (error) {
  682. self.stopMediaStream(stream);
  683. reject(
  684. JitsiTrackErrors.parseError(error,
  685. options.devices));
  686. });
  687. } else {
  688. successCallback({audioVideo: stream});
  689. }
  690. },
  691. function (error) {
  692. reject(JitsiTrackErrors.parseError(error,
  693. options.devices));
  694. },
  695. options);
  696. } else if (hasDesktop) {
  697. screenObtainer.obtainStream(
  698. function (stream) {
  699. successCallback({desktopStream: stream});
  700. }, function (error) {
  701. reject(
  702. JitsiTrackErrors.parseError(error,
  703. ["desktop"]));
  704. });
  705. }
  706. }
  707. }.bind(this));
  708. },
  709. addListener: function (eventType, listener) {
  710. eventEmitter.on(eventType, listener);
  711. },
  712. removeListener: function (eventType, listener) {
  713. eventEmitter.removeListener(eventType, listener);
  714. },
  715. getDeviceAvailability: function () {
  716. return devices;
  717. },
  718. isRTCReady: function () {
  719. return rtcReady;
  720. },
  721. /**
  722. * Checks if its possible to enumerate available cameras/micropones.
  723. * @returns {boolean} true if available, false otherwise.
  724. */
  725. isDeviceListAvailable: function () {
  726. var isEnumerateDevicesAvailable
  727. = navigator.mediaDevices && navigator.mediaDevices.enumerateDevices;
  728. if (isEnumerateDevicesAvailable) {
  729. return true;
  730. }
  731. return (MediaStreamTrack && MediaStreamTrack.getSources)? true : false;
  732. },
  733. /**
  734. * Returns true if changing the camera / microphone device is supported and
  735. * false if not.
  736. */
  737. isDeviceChangeAvailable: function () {
  738. return RTCBrowserType.isChrome() ||
  739. RTCBrowserType.isFirefox() ||
  740. RTCBrowserType.isOpera() ||
  741. RTCBrowserType.isTemasysPluginUsed();
  742. },
  743. /**
  744. * A method to handle stopping of the stream.
  745. * One point to handle the differences in various implementations.
  746. * @param mediaStream MediaStream object to stop.
  747. */
  748. stopMediaStream: function (mediaStream) {
  749. mediaStream.getTracks().forEach(function (track) {
  750. // stop() not supported with IE
  751. if (!RTCBrowserType.isTemasysPluginUsed() && track.stop) {
  752. track.stop();
  753. }
  754. });
  755. // leave stop for implementation still using it
  756. if (mediaStream.stop) {
  757. mediaStream.stop();
  758. }
  759. // if we have done createObjectURL, lets clean it
  760. if (mediaStream.jitsiObjectURL) {
  761. webkitURL.revokeObjectURL(mediaStream.jitsiObjectURL);
  762. }
  763. },
  764. /**
  765. * Returns whether the desktop sharing is enabled or not.
  766. * @returns {boolean}
  767. */
  768. isDesktopSharingEnabled: function () {
  769. return screenObtainer.isSupported();
  770. }
  771. };
  772. module.exports = RTCUtils;