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 30KB

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