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

RTCUtils.js 30KB

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