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

RTCUtils.js 31KB

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