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

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