modified lib-jitsi-meet dev repo
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RTCUtils.js 32KB

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