您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RTCUtils.js 32KB

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