Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RTCUtils.js 31KB

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