You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTCUtils.js 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. import { getLogger } from '@jitsi/logger';
  2. import EventEmitter from 'events';
  3. import clonedeep from 'lodash.clonedeep';
  4. import JitsiTrackError from '../../JitsiTrackError';
  5. import * as JitsiTrackErrors from '../../JitsiTrackErrors';
  6. import CameraFacingMode from '../../service/RTC/CameraFacingMode';
  7. import RTCEvents from '../../service/RTC/RTCEvents';
  8. import Resolutions from '../../service/RTC/Resolutions';
  9. import { VideoType } from '../../service/RTC/VideoType';
  10. import { AVAILABLE_DEVICE } from '../../service/statistics/AnalyticsEvents';
  11. import browser from '../browser';
  12. import SDPUtil from '../sdp/SDPUtil';
  13. import Statistics from '../statistics/statistics';
  14. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  15. import Listenable from '../util/Listenable';
  16. import screenObtainer from './ScreenObtainer';
  17. const logger = getLogger(__filename);
  18. // Require adapter only for certain browsers. This is being done for
  19. // react-native, which has its own shims, and while browsers are being migrated
  20. // over to use adapter's shims.
  21. if (browser.usesAdapter()) {
  22. require('webrtc-adapter');
  23. }
  24. const eventEmitter = new EventEmitter();
  25. const AVAILABLE_DEVICES_POLL_INTERVAL_TIME = 3000; // ms
  26. /**
  27. * Default MediaStreamConstraints to use for calls to getUserMedia.
  28. *
  29. * @private
  30. */
  31. const DEFAULT_CONSTRAINTS = {
  32. video: {
  33. height: {
  34. ideal: 720,
  35. max: 720,
  36. min: 180
  37. },
  38. width: {
  39. ideal: 1280,
  40. max: 1280,
  41. min: 320
  42. }
  43. }
  44. };
  45. // Currently audio output device change is supported only in Chrome and
  46. // default output always has 'default' device ID
  47. let audioOutputDeviceId = 'default'; // default device
  48. // whether user has explicitly set a device to use
  49. let audioOutputChanged = false;
  50. // Disables all audio processing
  51. let disableAP = false;
  52. // Disables Acoustic Echo Cancellation
  53. let disableAEC = false;
  54. // Disables Noise Suppression
  55. let disableNS = false;
  56. // Disables Automatic Gain Control
  57. let disableAGC = false;
  58. // Enables stereo.
  59. let stereo = null;
  60. const featureDetectionAudioEl = document.createElement('audio');
  61. const isAudioOutputDeviceChangeAvailable
  62. = typeof featureDetectionAudioEl.setSinkId !== 'undefined';
  63. let availableDevices = [];
  64. let availableDevicesPollTimer;
  65. /**
  66. * An empty function.
  67. */
  68. function emptyFuncton() {
  69. // no-op
  70. }
  71. /**
  72. * Creates a constraints object to be passed into a call to getUserMedia.
  73. *
  74. * @param {Array} um - An array of user media types to get. The accepted types are "video", "audio", and "desktop."
  75. * @param {Object} options - Various values to be added to the constraints.
  76. * @param {string} options.cameraDeviceId - The device id for the video capture device to get video from.
  77. * @param {Object} options.constraints - Default constraints object to use as a base for the returned constraints.
  78. * @param {Object} options.desktopStream - The desktop source id from which to capture a desktop sharing video.
  79. * @param {string} options.facingMode - Which direction the camera is pointing to (applicable on mobile)
  80. * @param {string} options.micDeviceId - The device id for the audio capture device to get audio from.
  81. * @private
  82. * @returns {Object}
  83. */
  84. function getConstraints(um = [], options = {}) {
  85. // Create a deep copy of the constraints to avoid any modification of
  86. // the passed in constraints object.
  87. const constraints = clonedeep(options.constraints || DEFAULT_CONSTRAINTS);
  88. if (um.indexOf('video') >= 0) {
  89. // The "resolution" option is a shortcut and takes precendence.
  90. if (Resolutions[options.resolution]) {
  91. const r = Resolutions[options.resolution];
  92. constraints.video.height = { ideal: r.height };
  93. constraints.video.width = { ideal: r.width };
  94. }
  95. if (!constraints.video) {
  96. constraints.video = {};
  97. }
  98. // Override the constraints on Safari because of the following webkit bug.
  99. // https://bugs.webkit.org/show_bug.cgi?id=210932
  100. // Camera doesn't start on older macOS versions if min/max constraints are specified.
  101. // TODO: remove this hack when the bug fix is available on Mojave, Sierra and High Sierra.
  102. if (browser.isWebKitBased()) {
  103. if (constraints.video.height && constraints.video.height.ideal) {
  104. constraints.video.height = { ideal: constraints.video.height.ideal };
  105. } else {
  106. logger.warn('Ideal camera height missing, camera may not start properly');
  107. }
  108. if (constraints.video.width && constraints.video.width.ideal) {
  109. constraints.video.width = { ideal: constraints.video.width.ideal };
  110. } else {
  111. logger.warn('Ideal camera width missing, camera may not start properly');
  112. }
  113. }
  114. if (options.cameraDeviceId) {
  115. constraints.video.deviceId = options.cameraDeviceId;
  116. } else {
  117. const facingMode = options.facingMode || CameraFacingMode.USER;
  118. constraints.video.facingMode = facingMode;
  119. }
  120. } else {
  121. constraints.video = false;
  122. }
  123. if (um.indexOf('audio') >= 0) {
  124. if (!constraints.audio || typeof constraints.audio === 'boolean') {
  125. constraints.audio = {};
  126. }
  127. constraints.audio = {
  128. autoGainControl: !disableAGC && !disableAP,
  129. deviceId: options.micDeviceId,
  130. echoCancellation: !disableAEC && !disableAP,
  131. noiseSuppression: !disableNS && !disableAP
  132. };
  133. if (stereo) {
  134. Object.assign(constraints.audio, { channelCount: 2 });
  135. }
  136. } else {
  137. constraints.audio = false;
  138. }
  139. return constraints;
  140. }
  141. /**
  142. * Updates the granted permissions based on the options we requested and the
  143. * streams we received.
  144. * @param um the options we requested to getUserMedia.
  145. * @param stream the stream we received from calling getUserMedia.
  146. */
  147. function updateGrantedPermissions(um, stream) {
  148. const audioTracksReceived
  149. = Boolean(stream) && stream.getAudioTracks().length > 0;
  150. const videoTracksReceived
  151. = Boolean(stream) && stream.getVideoTracks().length > 0;
  152. const grantedPermissions = {};
  153. if (um.indexOf('video') !== -1) {
  154. grantedPermissions.video = videoTracksReceived;
  155. }
  156. if (um.indexOf('audio') !== -1) {
  157. grantedPermissions.audio = audioTracksReceived;
  158. }
  159. eventEmitter.emit(RTCEvents.PERMISSIONS_CHANGED, grantedPermissions);
  160. }
  161. /**
  162. * Checks if new list of available media devices differs from previous one.
  163. * @param {MediaDeviceInfo[]} newDevices - list of new devices.
  164. * @returns {boolean} - true if list is different, false otherwise.
  165. */
  166. function compareAvailableMediaDevices(newDevices) {
  167. if (newDevices.length !== availableDevices.length) {
  168. return true;
  169. }
  170. /* eslint-disable newline-per-chained-call */
  171. return (
  172. newDevices.map(mediaDeviceInfoToJSON).sort().join('')
  173. !== availableDevices
  174. .map(mediaDeviceInfoToJSON).sort().join(''));
  175. /* eslint-enable newline-per-chained-call */
  176. /**
  177. *
  178. * @param info
  179. */
  180. function mediaDeviceInfoToJSON(info) {
  181. return JSON.stringify({
  182. kind: info.kind,
  183. deviceId: info.deviceId,
  184. groupId: info.groupId,
  185. label: info.label,
  186. facing: info.facing
  187. });
  188. }
  189. }
  190. /**
  191. * Sends analytics event with the passed device list.
  192. *
  193. * @param {Array<MediaDeviceInfo>} deviceList - List with info about the
  194. * available devices.
  195. * @returns {void}
  196. */
  197. function sendDeviceListToAnalytics(deviceList) {
  198. const audioInputDeviceCount
  199. = deviceList.filter(d => d.kind === 'audioinput').length;
  200. const audioOutputDeviceCount
  201. = deviceList.filter(d => d.kind === 'audiooutput').length;
  202. const videoInputDeviceCount
  203. = deviceList.filter(d => d.kind === 'videoinput').length;
  204. const videoOutputDeviceCount
  205. = deviceList.filter(d => d.kind === 'videooutput').length;
  206. deviceList.forEach(device => {
  207. const attributes = {
  208. 'audio_input_device_count': audioInputDeviceCount,
  209. 'audio_output_device_count': audioOutputDeviceCount,
  210. 'video_input_device_count': videoInputDeviceCount,
  211. 'video_output_device_count': videoOutputDeviceCount,
  212. 'device_id': device.deviceId,
  213. 'device_group_id': device.groupId,
  214. 'device_kind': device.kind,
  215. 'device_label': device.label
  216. };
  217. Statistics.sendAnalytics(AVAILABLE_DEVICE, attributes);
  218. });
  219. }
  220. /**
  221. * Update known devices.
  222. *
  223. * @param {Array<Object>} pds - The new devices.
  224. * @returns {void}
  225. *
  226. * NOTE: Use this function as a shared callback to handle both the devicechange event and the polling implementations.
  227. * This prevents duplication and works around a chrome bug (verified to occur on 68) where devicechange fires twice in
  228. * a row, which can cause async post devicechange processing to collide.
  229. */
  230. function updateKnownDevices(pds) {
  231. if (compareAvailableMediaDevices(pds)) {
  232. onMediaDevicesListChanged(pds);
  233. }
  234. }
  235. /**
  236. * Event handler for the 'devicechange' event.
  237. *
  238. * @param {MediaDeviceInfo[]} devices - list of media devices.
  239. * @emits RTCEvents.DEVICE_LIST_CHANGED
  240. */
  241. function onMediaDevicesListChanged(devicesReceived) {
  242. availableDevices = devicesReceived.slice(0);
  243. logger.info('list of media devices has changed:', availableDevices);
  244. sendDeviceListToAnalytics(availableDevices);
  245. // Used by tracks to update the real device id before the consumer of lib-jitsi-meet receives the new device list.
  246. eventEmitter.emit(RTCEvents.DEVICE_LIST_WILL_CHANGE, availableDevices);
  247. eventEmitter.emit(RTCEvents.DEVICE_LIST_CHANGED, availableDevices);
  248. }
  249. /**
  250. *
  251. */
  252. class RTCUtils extends Listenable {
  253. /**
  254. *
  255. */
  256. constructor() {
  257. super(eventEmitter);
  258. }
  259. /**
  260. * Depending on the browser, sets difference instance methods for
  261. * interacting with user media and adds methods to native WebRTC-related
  262. * objects. Also creates an instance variable for peer connection
  263. * constraints.
  264. *
  265. * @param {Object} options
  266. * @returns {void}
  267. */
  268. init(options = {}) {
  269. if (typeof options.disableAEC === 'boolean') {
  270. disableAEC = options.disableAEC;
  271. logger.info(`Disable AEC: ${disableAEC}`);
  272. }
  273. if (typeof options.disableNS === 'boolean') {
  274. disableNS = options.disableNS;
  275. logger.info(`Disable NS: ${disableNS}`);
  276. }
  277. if (typeof options.disableAP === 'boolean') {
  278. disableAP = options.disableAP;
  279. logger.info(`Disable AP: ${disableAP}`);
  280. }
  281. if (typeof options.disableAGC === 'boolean') {
  282. disableAGC = options.disableAGC;
  283. logger.info(`Disable AGC: ${disableAGC}`);
  284. }
  285. if (typeof options.audioQuality?.stereo === 'boolean') {
  286. stereo = options.audioQuality.stereo;
  287. logger.info(`Stereo: ${stereo}`);
  288. }
  289. window.clearInterval(availableDevicesPollTimer);
  290. availableDevicesPollTimer = undefined;
  291. if (browser.isReactNative()) {
  292. this.RTCPeerConnectionType = RTCPeerConnection;
  293. this.attachMediaStream = undefined; // Unused on React Native.
  294. this.getStreamID = function({ id }) {
  295. // The react-native-webrtc implementation that we use at the
  296. // time of this writing returns a number for the id of
  297. // MediaStream. Let's just say that a number contains no special
  298. // characters.
  299. return (
  300. typeof id === 'number'
  301. ? id
  302. : SDPUtil.filterSpecialChars(id));
  303. };
  304. this.getTrackID = ({ id }) => id;
  305. } else {
  306. this.RTCPeerConnectionType = RTCPeerConnection;
  307. this.attachMediaStream
  308. = wrapAttachMediaStream((element, stream) => {
  309. if (element) {
  310. element.srcObject = stream;
  311. }
  312. });
  313. this.getStreamID = ({ id }) => id;
  314. this.getTrackID = ({ id }) => id;
  315. }
  316. this.pcConstraints = browser.isChromiumBased() || browser.isReactNative()
  317. ? { optional: [
  318. { googScreencastMinBitrate: 100 },
  319. { googCpuOveruseDetection: true }
  320. ] }
  321. : {};
  322. screenObtainer.init(options);
  323. if (this.isDeviceListAvailable()) {
  324. this.enumerateDevices(ds => {
  325. availableDevices = ds.slice(0);
  326. logger.debug('Available devices: ', availableDevices);
  327. sendDeviceListToAnalytics(availableDevices);
  328. eventEmitter.emit(
  329. RTCEvents.DEVICE_LIST_AVAILABLE,
  330. availableDevices);
  331. if (browser.supportsDeviceChangeEvent()) {
  332. navigator.mediaDevices.addEventListener(
  333. 'devicechange',
  334. () => this.enumerateDevices(emptyFuncton));
  335. } else {
  336. // Periodically poll enumerateDevices() method to check if
  337. // list of media devices has changed.
  338. availableDevicesPollTimer = window.setInterval(
  339. () => this.enumerateDevices(emptyFuncton),
  340. AVAILABLE_DEVICES_POLL_INTERVAL_TIME);
  341. }
  342. });
  343. }
  344. }
  345. /**
  346. *
  347. * @param {Function} callback
  348. */
  349. enumerateDevices(callback) {
  350. navigator.mediaDevices.enumerateDevices()
  351. .then(devices => {
  352. updateKnownDevices(devices);
  353. callback(devices);
  354. })
  355. .catch(error => {
  356. logger.warn(`Failed to enumerate devices. ${error}`);
  357. updateKnownDevices([]);
  358. callback([]);
  359. });
  360. }
  361. /**
  362. * Acquires a media stream via getUserMedia that
  363. * matches the given constraints
  364. *
  365. * @param {array} umDevices which devices to acquire (e.g. audio, video)
  366. * @param {Object} constraints - Stream specifications to use.
  367. * @param {number} timeout - The timeout in ms for GUM.
  368. * @returns {Promise}
  369. */
  370. _getUserMedia(umDevices, constraints = {}, timeout = 0) {
  371. return new Promise((resolve, reject) => {
  372. let gumTimeout, timeoutExpired = false;
  373. if (typeof timeout === 'number' && !isNaN(timeout) && timeout > 0) {
  374. gumTimeout = setTimeout(() => {
  375. timeoutExpired = true;
  376. gumTimeout = undefined;
  377. reject(new JitsiTrackError(JitsiTrackErrors.TIMEOUT));
  378. }, timeout);
  379. }
  380. navigator.mediaDevices.getUserMedia(constraints)
  381. .then(stream => {
  382. logger.log('onUserMediaSuccess');
  383. updateGrantedPermissions(umDevices, stream);
  384. if (!timeoutExpired) {
  385. if (typeof gumTimeout !== 'undefined') {
  386. clearTimeout(gumTimeout);
  387. }
  388. resolve(stream);
  389. }
  390. })
  391. .catch(error => {
  392. logger.warn(`Failed to get access to local media. ${error} ${JSON.stringify(constraints)}`);
  393. const jitsiError = new JitsiTrackError(error, constraints, umDevices);
  394. if (!timeoutExpired) {
  395. if (typeof gumTimeout !== 'undefined') {
  396. clearTimeout(gumTimeout);
  397. }
  398. reject(jitsiError);
  399. }
  400. if (jitsiError.name === JitsiTrackErrors.PERMISSION_DENIED) {
  401. updateGrantedPermissions(umDevices, undefined);
  402. }
  403. // else {
  404. // Probably the error is not caused by the lack of permissions and we don't need to update them.
  405. // }
  406. });
  407. });
  408. }
  409. /**
  410. * Acquire a display stream via the screenObtainer. This requires extra
  411. * logic compared to use screenObtainer versus normal device capture logic
  412. * in RTCUtils#_getUserMedia.
  413. *
  414. * @returns {Promise} A promise which will be resolved with an object which
  415. * contains the acquired display stream. If desktop sharing is not supported
  416. * then a rejected promise will be returned.
  417. */
  418. _getDesktopMedia() {
  419. if (!screenObtainer.isSupported()) {
  420. return Promise.reject(new Error('Desktop sharing is not supported!'));
  421. }
  422. return new Promise((resolve, reject) => {
  423. screenObtainer.obtainStream(
  424. stream => {
  425. resolve(stream);
  426. },
  427. error => {
  428. reject(error);
  429. });
  430. });
  431. }
  432. /**
  433. * Private utility for determining if the passed in MediaStream contains
  434. * tracks of the type(s) specified in the requested devices.
  435. *
  436. * @param {string[]} requestedDevices - The track types that are expected to
  437. * be includes in the stream.
  438. * @param {MediaStream} stream - The MediaStream to check if it has the
  439. * expected track types.
  440. * @returns {string[]} An array of string with the missing track types. The
  441. * array will be empty if all requestedDevices are found in the stream.
  442. */
  443. _getMissingTracks(requestedDevices = [], stream) {
  444. const missingDevices = [];
  445. const audioDeviceRequested = requestedDevices.includes('audio');
  446. const audioTracksReceived
  447. = stream && stream.getAudioTracks().length > 0;
  448. if (audioDeviceRequested && !audioTracksReceived) {
  449. missingDevices.push('audio');
  450. }
  451. const videoDeviceRequested = requestedDevices.includes('video');
  452. const videoTracksReceived
  453. = stream && stream.getVideoTracks().length > 0;
  454. if (videoDeviceRequested && !videoTracksReceived) {
  455. missingDevices.push('video');
  456. }
  457. return missingDevices;
  458. }
  459. /**
  460. * Gets streams from specified device types. This function intentionally
  461. * ignores errors for upstream to catch and handle instead.
  462. *
  463. * @param {Object} options - A hash describing what devices to get and
  464. * relevant constraints.
  465. * @param {string[]} options.devices - The types of media to capture. Valid
  466. * values are "desktop", "audio", and "video".
  467. * @param {Object} options.desktopSharingFrameRate
  468. * @param {Object} options.desktopSharingFrameRate.min - Minimum fps
  469. * @param {Object} options.desktopSharingFrameRate.max - Maximum fps
  470. * @param {String} options.desktopSharingSourceDevice - The device id or
  471. * label for a video input source that should be used for screensharing.
  472. * @returns {Promise} The promise, when successful, will return an array of
  473. * meta data for the requested device type, which includes the stream and
  474. * track. If an error occurs, it will be deferred to the caller for
  475. * handling.
  476. */
  477. obtainAudioAndVideoPermissions(options) {
  478. const {
  479. timeout,
  480. ...otherOptions
  481. } = options;
  482. const mediaStreamsMetaData = [];
  483. // Declare private functions to be used in the promise chain below.
  484. // These functions are declared in the scope of this function because
  485. // they are not being used anywhere else, so only this function needs to
  486. // know about them.
  487. /**
  488. * Executes a request for desktop media if specified in options.
  489. *
  490. * @returns {Promise}
  491. */
  492. const maybeRequestDesktopDevice = function() {
  493. const umDevices = otherOptions.devices || [];
  494. const isDesktopDeviceRequested
  495. = umDevices.indexOf('desktop') !== -1;
  496. if (!isDesktopDeviceRequested) {
  497. return Promise.resolve();
  498. }
  499. const {
  500. desktopSharingSourceDevice
  501. } = otherOptions;
  502. // Attempt to use a video input device as a screenshare source if
  503. // the option is defined.
  504. if (desktopSharingSourceDevice) {
  505. const matchingDevice
  506. = availableDevices && availableDevices.find(device =>
  507. device.kind === 'videoinput'
  508. && (device.deviceId === desktopSharingSourceDevice
  509. || device.label === desktopSharingSourceDevice));
  510. if (!matchingDevice) {
  511. return Promise.reject(new JitsiTrackError(
  512. { name: 'ConstraintNotSatisfiedError' },
  513. {},
  514. [ desktopSharingSourceDevice ]
  515. ));
  516. }
  517. const requestedDevices = [ 'video' ];
  518. const constraints = {
  519. video: {
  520. deviceId: matchingDevice.deviceId
  521. // frameRate is omited here on purpose since this is a device that we'll pretend is a screen.
  522. }
  523. };
  524. return this._getUserMedia(requestedDevices, constraints, timeout)
  525. .then(stream => {
  526. return {
  527. sourceType: 'device',
  528. stream
  529. };
  530. });
  531. }
  532. return this._getDesktopMedia();
  533. }.bind(this);
  534. /**
  535. * Creates a meta data object about the passed in desktopStream and
  536. * pushes the meta data to the internal array mediaStreamsMetaData to be
  537. * returned later.
  538. *
  539. * @param {MediaStreamTrack} desktopStream - A track for a desktop
  540. * capture.
  541. * @returns {void}
  542. */
  543. const maybeCreateAndAddDesktopTrack = function(desktopStream) {
  544. if (!desktopStream) {
  545. return;
  546. }
  547. const { stream, sourceId, sourceType } = desktopStream;
  548. const desktopAudioTracks = stream.getAudioTracks();
  549. if (desktopAudioTracks.length) {
  550. const desktopAudioStream = new MediaStream(desktopAudioTracks);
  551. mediaStreamsMetaData.push({
  552. stream: desktopAudioStream,
  553. sourceId,
  554. sourceType,
  555. track: desktopAudioStream.getAudioTracks()[0]
  556. });
  557. }
  558. const desktopVideoTracks = stream.getVideoTracks();
  559. if (desktopVideoTracks.length) {
  560. const desktopVideoStream = new MediaStream(desktopVideoTracks);
  561. mediaStreamsMetaData.push({
  562. stream: desktopVideoStream,
  563. sourceId,
  564. sourceType,
  565. track: desktopVideoStream.getVideoTracks()[0],
  566. videoType: VideoType.DESKTOP
  567. });
  568. }
  569. };
  570. /**
  571. * Executes a request for audio and/or video, as specified in options.
  572. * By default both audio and video will be captured if options.devices
  573. * is not defined.
  574. *
  575. * @returns {Promise}
  576. */
  577. const maybeRequestCaptureDevices = function() {
  578. const umDevices = otherOptions.devices || [ 'audio', 'video' ];
  579. const requestedCaptureDevices = umDevices.filter(device => device === 'audio' || device === 'video');
  580. if (!requestedCaptureDevices.length) {
  581. return Promise.resolve();
  582. }
  583. const constraints = getConstraints(requestedCaptureDevices, otherOptions);
  584. logger.info('Got media constraints: ', JSON.stringify(constraints));
  585. return this._getUserMedia(requestedCaptureDevices, constraints, timeout);
  586. }.bind(this);
  587. /**
  588. * Splits the passed in media stream into separate audio and video
  589. * streams and creates meta data objects for each and pushes them to the
  590. * internal array mediaStreamsMetaData to be returned later.
  591. *
  592. * @param {MediaStreamTrack} avStream - A track for with audio and/or
  593. * video track.
  594. * @returns {void}
  595. */
  596. const maybeCreateAndAddAVTracks = function(avStream) {
  597. if (!avStream) {
  598. return;
  599. }
  600. const audioTracks = avStream.getAudioTracks();
  601. if (audioTracks.length) {
  602. const audioStream = new MediaStream(audioTracks);
  603. mediaStreamsMetaData.push({
  604. stream: audioStream,
  605. track: audioStream.getAudioTracks()[0],
  606. effects: otherOptions.effects
  607. });
  608. }
  609. const videoTracks = avStream.getVideoTracks();
  610. if (videoTracks.length) {
  611. const videoStream = new MediaStream(videoTracks);
  612. mediaStreamsMetaData.push({
  613. stream: videoStream,
  614. track: videoStream.getVideoTracks()[0],
  615. videoType: VideoType.CAMERA,
  616. effects: otherOptions.effects
  617. });
  618. }
  619. };
  620. return maybeRequestDesktopDevice()
  621. .then(maybeCreateAndAddDesktopTrack)
  622. .then(maybeRequestCaptureDevices)
  623. .then(maybeCreateAndAddAVTracks)
  624. .then(() => mediaStreamsMetaData)
  625. .catch(error => {
  626. mediaStreamsMetaData.forEach(({ stream }) => {
  627. this.stopMediaStream(stream);
  628. });
  629. return Promise.reject(error);
  630. });
  631. }
  632. /**
  633. * Checks whether it is possible to enumerate available cameras/microphones.
  634. *
  635. * @returns {boolean} {@code true} if the device listing is available;
  636. * {@code false}, otherwise.
  637. */
  638. isDeviceListAvailable() {
  639. return Boolean(
  640. navigator.mediaDevices
  641. && navigator.mediaDevices.enumerateDevices);
  642. }
  643. /**
  644. * Returns true if changing the input (camera / microphone) or output
  645. * (audio) device is supported and false if not.
  646. * @params {string} [deviceType] - type of device to change. Default is
  647. * undefined or 'input', 'output' - for audio output device change.
  648. * @returns {boolean} true if available, false otherwise.
  649. */
  650. isDeviceChangeAvailable(deviceType) {
  651. if (deviceType === 'output' || deviceType === 'audiooutput') {
  652. return isAudioOutputDeviceChangeAvailable;
  653. }
  654. return true;
  655. }
  656. /**
  657. * A method to handle stopping of the stream.
  658. * One point to handle the differences in various implementations.
  659. * @param mediaStream MediaStream object to stop.
  660. */
  661. stopMediaStream(mediaStream) {
  662. if (!mediaStream) {
  663. return;
  664. }
  665. mediaStream.getTracks().forEach(track => {
  666. if (track.stop) {
  667. track.stop();
  668. }
  669. });
  670. // leave stop for implementation still using it
  671. if (mediaStream.stop) {
  672. mediaStream.stop();
  673. }
  674. // The MediaStream implementation of the react-native-webrtc project has
  675. // an explicit release method that is to be invoked in order to release
  676. // used resources such as memory.
  677. if (mediaStream.release) {
  678. mediaStream.release();
  679. }
  680. }
  681. /**
  682. * Returns whether the desktop sharing is enabled or not.
  683. * @returns {boolean}
  684. */
  685. isDesktopSharingEnabled() {
  686. return screenObtainer.isSupported();
  687. }
  688. /**
  689. * Sets current audio output device.
  690. * @param {string} deviceId - id of 'audiooutput' device from
  691. * navigator.mediaDevices.enumerateDevices(), 'default' for default
  692. * device
  693. * @returns {Promise} - resolves when audio output is changed, is rejected
  694. * otherwise
  695. */
  696. setAudioOutputDevice(deviceId) {
  697. if (!this.isDeviceChangeAvailable('output')) {
  698. return Promise.reject(
  699. new Error('Audio output device change is not supported'));
  700. }
  701. return featureDetectionAudioEl.setSinkId(deviceId)
  702. .then(() => {
  703. audioOutputDeviceId = deviceId;
  704. audioOutputChanged = true;
  705. logger.log(`Audio output device set to ${deviceId}`);
  706. eventEmitter.emit(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  707. deviceId);
  708. });
  709. }
  710. /**
  711. * Sets the capture frame rate for desktop tracks.
  712. *
  713. * @param {number} maxFps - max fps to be used as the capture frame rate.
  714. * @returns {void}
  715. */
  716. setDesktopSharingFrameRate(maxFps) {
  717. screenObtainer.setDesktopSharingFrameRate(maxFps);
  718. }
  719. /**
  720. * Returns currently used audio output device id, '' stands for default
  721. * device
  722. * @returns {string}
  723. */
  724. getAudioOutputDevice() {
  725. return audioOutputDeviceId;
  726. }
  727. /**
  728. * Returns list of available media devices if its obtained, otherwise an
  729. * empty array is returned/
  730. * @returns {Array} list of available media devices.
  731. */
  732. getCurrentlyAvailableMediaDevices() {
  733. return availableDevices;
  734. }
  735. /**
  736. * Returns whether available devices have permissions granted
  737. * @returns {Boolean}
  738. */
  739. arePermissionsGrantedForAvailableDevices() {
  740. return availableDevices.some(device => Boolean(device.label));
  741. }
  742. /**
  743. * Returns event data for device to be reported to stats.
  744. * @returns {MediaDeviceInfo} device.
  745. */
  746. getEventDataForActiveDevice(device) {
  747. const deviceList = [];
  748. const deviceData = {
  749. 'deviceId': device.deviceId,
  750. 'kind': device.kind,
  751. 'label': device.label,
  752. 'groupId': device.groupId
  753. };
  754. deviceList.push(deviceData);
  755. return { deviceList };
  756. }
  757. /**
  758. * Configures the given PeerConnection constraints to either enable or
  759. * disable (according to the value of the 'enable' parameter) the
  760. * 'googSuspendBelowMinBitrate' option.
  761. * @param constraints the constraints on which to operate.
  762. * @param enable {boolean} whether to enable or disable the suspend video
  763. * option.
  764. */
  765. setSuspendVideo(constraints, enable) {
  766. if (!constraints.optional) {
  767. constraints.optional = [];
  768. }
  769. // Get rid of all "googSuspendBelowMinBitrate" constraints (we assume
  770. // that the elements of constraints.optional contain a single property).
  771. constraints.optional
  772. = constraints.optional.filter(
  773. c => !c.hasOwnProperty('googSuspendBelowMinBitrate'));
  774. if (enable) {
  775. constraints.optional.push({ googSuspendBelowMinBitrate: 'true' });
  776. }
  777. }
  778. }
  779. const rtcUtils = new RTCUtils();
  780. /**
  781. * Wraps original attachMediaStream function to set current audio output device
  782. * if this is supported.
  783. * @param {Function} origAttachMediaStream
  784. * @returns {Function}
  785. */
  786. function wrapAttachMediaStream(origAttachMediaStream) {
  787. return function(element, stream) {
  788. // eslint-disable-next-line prefer-rest-params
  789. const res = origAttachMediaStream.apply(rtcUtils, arguments);
  790. if (stream
  791. && rtcUtils.isDeviceChangeAvailable('output')
  792. && stream.getAudioTracks
  793. && stream.getAudioTracks().length
  794. // we skip setting audio output if there was no explicit change
  795. && audioOutputChanged) {
  796. element.setSinkId(rtcUtils.getAudioOutputDevice())
  797. .catch(function(ex) {
  798. const err
  799. = new JitsiTrackError(ex, null, [ 'audiooutput' ]);
  800. GlobalOnErrorHandler.callUnhandledRejectionHandler({
  801. promise: this, // eslint-disable-line no-invalid-this
  802. reason: err
  803. });
  804. logger.warn(
  805. 'Failed to set audio output device for the element.'
  806. + ' Default audio output device will be used'
  807. + ' instead',
  808. element,
  809. err);
  810. });
  811. }
  812. return res;
  813. };
  814. }
  815. export default rtcUtils;