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.

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