Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RTCUtils.js 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /* global
  2. __filename,
  3. MediaStreamTrack,
  4. RTCIceCandidate: true,
  5. RTCPeerConnection,
  6. RTCSessionDescription: true
  7. */
  8. import { AVAILABLE_DEVICE } from '../../service/statistics/AnalyticsEvents';
  9. import CameraFacingMode from '../../service/RTC/CameraFacingMode';
  10. import EventEmitter from 'events';
  11. import { getLogger } from 'jitsi-meet-logger';
  12. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  13. import JitsiTrackError from '../../JitsiTrackError';
  14. import Listenable from '../util/Listenable';
  15. import * as MediaType from '../../service/RTC/MediaType';
  16. import Resolutions from '../../service/RTC/Resolutions';
  17. import browser from '../browser';
  18. import RTCEvents from '../../service/RTC/RTCEvents';
  19. import ortcRTCPeerConnection from './ortc/RTCPeerConnection';
  20. import screenObtainer from './ScreenObtainer';
  21. import SDPUtil from '../xmpp/SDPUtil';
  22. import Statistics from '../statistics/statistics';
  23. import VideoType from '../../service/RTC/VideoType';
  24. const logger = getLogger(__filename);
  25. // Require adapter only for certain browsers. This is being done for
  26. // react-native, which has its own shims, and while browsers are being migrated
  27. // over to use adapter's shims.
  28. if (browser.usesAdapter()) {
  29. require('webrtc-adapter');
  30. }
  31. const eventEmitter = new EventEmitter();
  32. const AVAILABLE_DEVICES_POLL_INTERVAL_TIME = 3000; // ms
  33. /**
  34. * Default resolution to obtain for video tracks if no resolution is specified.
  35. * This default is used for old gum flow only, as new gum flow uses
  36. * {@link DEFAULT_CONSTRAINTS}.
  37. */
  38. const OLD_GUM_DEFAULT_RESOLUTION = 720;
  39. /**
  40. * Default devices to obtain when no specific devices are specified. This
  41. * default is used for old gum flow only.
  42. */
  43. const OLD_GUM_DEFAULT_DEVICES = [ 'audio', 'video' ];
  44. /**
  45. * Default MediaStreamConstraints to use for calls to getUserMedia.
  46. *
  47. * @private
  48. */
  49. const DEFAULT_CONSTRAINTS = {
  50. video: {
  51. aspectRatio: 16 / 9,
  52. height: {
  53. ideal: 720,
  54. max: 720,
  55. min: 240
  56. }
  57. }
  58. };
  59. /**
  60. * The default frame rate for Screen Sharing.
  61. */
  62. const SS_DEFAULT_FRAME_RATE = 5;
  63. // Currently audio output device change is supported only in Chrome and
  64. // default output always has 'default' device ID
  65. let audioOutputDeviceId = 'default'; // default device
  66. // whether user has explicitly set a device to use
  67. let audioOutputChanged = false;
  68. // Disables all audio processing
  69. let disableAP = false;
  70. // Disables Acoustic Echo Cancellation
  71. let disableAEC = false;
  72. // Disables Noise Suppression
  73. let disableNS = false;
  74. // Disables Automatic Gain Control
  75. let disableAGC = false;
  76. // Disables Highpass Filter
  77. let disableHPF = false;
  78. const featureDetectionAudioEl = document.createElement('audio');
  79. const isAudioOutputDeviceChangeAvailable
  80. = typeof featureDetectionAudioEl.setSinkId !== 'undefined';
  81. let availableDevices;
  82. let availableDevicesPollTimer;
  83. /**
  84. * Initialize wrapper function for enumerating devices.
  85. * TODO: remove this, it should no longer be needed.
  86. *
  87. * @returns {?Function}
  88. */
  89. function initEnumerateDevicesWithCallback() {
  90. if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
  91. return callback => {
  92. navigator.mediaDevices.enumerateDevices()
  93. .then(callback, () => callback([]));
  94. };
  95. }
  96. }
  97. /**
  98. *
  99. * @param constraints
  100. * @param isNewStyleConstraintsSupported
  101. * @param resolution
  102. */
  103. function setResolutionConstraints(
  104. constraints,
  105. isNewStyleConstraintsSupported,
  106. resolution) {
  107. if (Resolutions[resolution]) {
  108. if (isNewStyleConstraintsSupported) {
  109. constraints.video.width = {
  110. ideal: Resolutions[resolution].width
  111. };
  112. constraints.video.height = {
  113. ideal: Resolutions[resolution].height
  114. };
  115. }
  116. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  117. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  118. }
  119. if (constraints.video.mandatory.minWidth) {
  120. constraints.video.mandatory.maxWidth
  121. = constraints.video.mandatory.minWidth;
  122. }
  123. if (constraints.video.mandatory.minHeight) {
  124. constraints.video.mandatory.maxHeight
  125. = constraints.video.mandatory.minHeight;
  126. }
  127. }
  128. /**
  129. * @param {string[]} um required user media types
  130. *
  131. * @param {Object} [options={}] optional parameters
  132. * @param {string} options.resolution
  133. * @param {number} options.bandwidth
  134. * @param {number} options.fps
  135. * @param {string} options.desktopStream
  136. * @param {string} options.cameraDeviceId
  137. * @param {string} options.micDeviceId
  138. * @param {CameraFacingMode} options.facingMode
  139. * @param {bool} firefox_fake_device
  140. * @param {Object} options.frameRate - used only for dekstop sharing.
  141. * @param {Object} options.frameRate.min - Minimum fps
  142. * @param {Object} options.frameRate.max - Maximum fps
  143. */
  144. function getConstraints(um, options = {}) {
  145. const constraints = {
  146. audio: false,
  147. video: false
  148. };
  149. // Don't mix new and old style settings for Chromium as this leads
  150. // to TypeError in new Chromium versions. @see
  151. // https://bugs.chromium.org/p/chromium/issues/detail?id=614716
  152. // This is a temporary solution, in future we will fully split old and
  153. // new style constraints when new versions of Chromium and Firefox will
  154. // have stable support of new constraints format. For more information
  155. // @see https://github.com/jitsi/lib-jitsi-meet/pull/136
  156. const isNewStyleConstraintsSupported
  157. = browser.isFirefox()
  158. || browser.isEdge()
  159. || browser.isReactNative();
  160. if (um.indexOf('video') >= 0) {
  161. // same behaviour as true
  162. constraints.video = { mandatory: {},
  163. optional: [] };
  164. if (options.cameraDeviceId) {
  165. if (isNewStyleConstraintsSupported) {
  166. // New style of setting device id.
  167. constraints.video.deviceId = options.cameraDeviceId;
  168. }
  169. // Old style.
  170. constraints.video.mandatory.sourceId = options.cameraDeviceId;
  171. } else {
  172. // Prefer the front i.e. user-facing camera (to the back i.e.
  173. // environment-facing camera, for example).
  174. // TODO: Maybe use "exact" syntax if options.facingMode is defined,
  175. // but this probably needs to be decided when updating other
  176. // constraints, as we currently don't use "exact" syntax anywhere.
  177. const facingMode = options.facingMode || CameraFacingMode.USER;
  178. if (isNewStyleConstraintsSupported) {
  179. constraints.video.facingMode = facingMode;
  180. }
  181. constraints.video.optional.push({
  182. facingMode
  183. });
  184. }
  185. if (options.minFps || options.maxFps || options.fps) {
  186. // for some cameras it might be necessary to request 30fps
  187. // so they choose 30fps mjpg over 10fps yuy2
  188. if (options.minFps || options.fps) {
  189. // Fall back to options.fps for backwards compatibility
  190. options.minFps = options.minFps || options.fps;
  191. constraints.video.mandatory.minFrameRate = options.minFps;
  192. }
  193. if (options.maxFps) {
  194. constraints.video.mandatory.maxFrameRate = options.maxFps;
  195. }
  196. }
  197. setResolutionConstraints(
  198. constraints, isNewStyleConstraintsSupported, options.resolution);
  199. }
  200. if (um.indexOf('audio') >= 0) {
  201. if (browser.isReactNative()) {
  202. // The react-native-webrtc project that we're currently using
  203. // expects the audio constraint to be a boolean.
  204. constraints.audio = true;
  205. } else if (browser.isFirefox()) {
  206. if (options.micDeviceId) {
  207. constraints.audio = {
  208. mandatory: {},
  209. deviceId: options.micDeviceId, // new style
  210. optional: [ {
  211. sourceId: options.micDeviceId // old style
  212. } ] };
  213. } else {
  214. constraints.audio = true;
  215. }
  216. } else {
  217. // same behaviour as true
  218. constraints.audio = { mandatory: {},
  219. optional: [] };
  220. if (options.micDeviceId) {
  221. if (isNewStyleConstraintsSupported) {
  222. // New style of setting device id.
  223. constraints.audio.deviceId = options.micDeviceId;
  224. }
  225. // Old style.
  226. constraints.audio.optional.push({
  227. sourceId: options.micDeviceId
  228. });
  229. }
  230. // if it is good enough for hangouts...
  231. constraints.audio.optional.push(
  232. { echoCancellation: !disableAEC && !disableAP },
  233. { googEchoCancellation: !disableAEC && !disableAP },
  234. { googAutoGainControl: !disableAGC && !disableAP },
  235. { googNoiseSuppression: !disableNS && !disableAP },
  236. { googHighpassFilter: !disableHPF && !disableAP },
  237. { googNoiseSuppression2: !disableNS && !disableAP },
  238. { googEchoCancellation2: !disableAEC && !disableAP },
  239. { googAutoGainControl2: !disableAGC && !disableAP }
  240. );
  241. }
  242. }
  243. if (um.indexOf('screen') >= 0) {
  244. if (browser.isChrome()) {
  245. constraints.video = {
  246. mandatory: getSSConstraints({
  247. ...options,
  248. source: 'screen'
  249. }),
  250. optional: []
  251. };
  252. } else if (browser.isFirefox()) {
  253. constraints.video = {
  254. mozMediaSource: 'window',
  255. mediaSource: 'window',
  256. frameRate: options.frameRate || {
  257. min: SS_DEFAULT_FRAME_RATE,
  258. max: SS_DEFAULT_FRAME_RATE
  259. }
  260. };
  261. } else {
  262. const errmsg
  263. = '\'screen\' WebRTC media source is supported only in Chrome'
  264. + ' and Firefox';
  265. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  266. logger.error(errmsg);
  267. }
  268. }
  269. if (um.indexOf('desktop') >= 0) {
  270. constraints.video = {
  271. mandatory: getSSConstraints({
  272. ...options,
  273. source: 'desktop'
  274. }),
  275. optional: []
  276. };
  277. }
  278. if (options.bandwidth) {
  279. if (!constraints.video) {
  280. // same behaviour as true
  281. constraints.video = { mandatory: {},
  282. optional: [] };
  283. }
  284. constraints.video.optional.push({ bandwidth: options.bandwidth });
  285. }
  286. // we turn audio for both audio and video tracks, the fake audio & video
  287. // seems to work only when enabled in one getUserMedia call, we cannot get
  288. // fake audio separate by fake video this later can be a problem with some
  289. // of the tests
  290. if (browser.isFirefox() && options.firefox_fake_device) {
  291. // seems to be fixed now, removing this experimental fix, as having
  292. // multiple audio tracks brake the tests
  293. // constraints.audio = true;
  294. constraints.fake = true;
  295. }
  296. return constraints;
  297. }
  298. /**
  299. * Creates a constraints object to be passed into a call to getUserMedia.
  300. *
  301. * @param {Array} um - An array of user media types to get. The accepted
  302. * types are "video", "audio", and "desktop."
  303. * @param {Object} options - Various values to be added to the constraints.
  304. * @param {string} options.cameraDeviceId - The device id for the video
  305. * capture device to get video from.
  306. * @param {Object} options.constraints - Default constraints object to use
  307. * as a base for the returned constraints.
  308. * @param {Object} options.desktopStream - The desktop source id from which
  309. * to capture a desktop sharing video.
  310. * @param {string} options.facingMode - Which direction the camera is
  311. * pointing to.
  312. * @param {string} options.micDeviceId - The device id for the audio capture
  313. * device to get audio from.
  314. * @param {Object} options.frameRate - used only for dekstop sharing.
  315. * @param {Object} options.frameRate.min - Minimum fps
  316. * @param {Object} options.frameRate.max - Maximum fps
  317. * @private
  318. * @returns {Object}
  319. */
  320. function newGetConstraints(um = [], options = {}) {
  321. // Create a deep copy of the constraints to avoid any modification of
  322. // the passed in constraints object.
  323. const constraints = JSON.parse(JSON.stringify(
  324. options.constraints || DEFAULT_CONSTRAINTS));
  325. if (um.indexOf('video') >= 0) {
  326. if (!constraints.video) {
  327. constraints.video = {};
  328. }
  329. if (options.cameraDeviceId) {
  330. constraints.video.deviceId = options.cameraDeviceId;
  331. } else {
  332. const facingMode = options.facingMode || CameraFacingMode.USER;
  333. constraints.video.facingMode = facingMode;
  334. }
  335. } else {
  336. constraints.video = false;
  337. }
  338. if (um.indexOf('audio') >= 0) {
  339. if (!constraints.audio || typeof constraints.audio === 'boolean') {
  340. constraints.audio = {};
  341. }
  342. // NOTE(brian): the new-style ('advanced' instead of 'optional')
  343. // doesn't seem to carry through the googXXX constraints
  344. // Changing back to 'optional' here (even with video using
  345. // the 'advanced' style) allows them to be passed through
  346. // but also requires the device id to capture to be set in optional
  347. // as sourceId otherwise the constraints are considered malformed.
  348. if (!constraints.audio.optional) {
  349. constraints.audio.optional = [];
  350. }
  351. constraints.audio.optional.push(
  352. { sourceId: options.micDeviceId },
  353. { echoCancellation: !disableAEC && !disableAP },
  354. { googEchoCancellation: !disableAEC && !disableAP },
  355. { googAutoGainControl: !disableAGC && !disableAP },
  356. { googNoiseSuppression: !disableNS && !disableAP },
  357. { googHighpassFilter: !disableHPF && !disableAP },
  358. { googNoiseSuppression2: !disableNS && !disableAP },
  359. { googEchoCancellation2: !disableAEC && !disableAP },
  360. { googAutoGainControl2: !disableAGC && !disableAP }
  361. );
  362. } else {
  363. constraints.audio = false;
  364. }
  365. if (um.indexOf('desktop') >= 0) {
  366. if (!constraints.video || typeof constraints.video === 'boolean') {
  367. constraints.video = {};
  368. }
  369. constraints.video = {
  370. mandatory: getSSConstraints({
  371. ...options,
  372. source: 'desktop'
  373. })
  374. };
  375. }
  376. return constraints;
  377. }
  378. /**
  379. * Generates GUM constraints for screen sharing.
  380. *
  381. * @param {Object} options - The options passed to
  382. * <tt>obtainAudioAndVideoPermissions</tt>.
  383. * @returns {Object} - GUM constraints.
  384. *
  385. * TODO: Currently only the new GUM flow and Chrome is using the method. We
  386. * should make it work for all use cases.
  387. */
  388. function getSSConstraints(options = {}) {
  389. const {
  390. desktopStream,
  391. frameRate = {
  392. min: SS_DEFAULT_FRAME_RATE,
  393. max: SS_DEFAULT_FRAME_RATE
  394. }
  395. } = options;
  396. const { max, min } = frameRate;
  397. const constraints = {
  398. chromeMediaSource: options.source,
  399. maxWidth: window.screen.width,
  400. maxHeight: window.screen.height
  401. };
  402. if (typeof min === 'number') {
  403. constraints.minFrameRate = min;
  404. }
  405. if (typeof max === 'number') {
  406. constraints.maxFrameRate = max;
  407. }
  408. if (typeof desktopStream !== 'undefined') {
  409. constraints.chromeMediaSourceId = desktopStream;
  410. }
  411. return constraints;
  412. }
  413. /**
  414. * Generates constraints for screen sharing when using getDisplayMedia.
  415. * The constraints(MediaTrackConstraints) are applied to the resulting track.
  416. *
  417. * @returns {Object} - MediaTrackConstraints constraints.
  418. */
  419. function getTrackSSConstraints(options = {}) {
  420. // we used to set height and width in the constraints, but this can lead
  421. // to inconsistencies if the browser is on a lower resolution screen
  422. // and we share a screen with bigger resolution, so they are now not set
  423. const constraints = {
  424. frameRate: SS_DEFAULT_FRAME_RATE
  425. };
  426. const { desktopSharingFrameRate } = options;
  427. if (desktopSharingFrameRate && desktopSharingFrameRate.max) {
  428. constraints.frameRate = desktopSharingFrameRate.max;
  429. }
  430. return constraints;
  431. }
  432. /**
  433. * Updates the granted permissions based on the options we requested and the
  434. * streams we received.
  435. * @param um the options we requested to getUserMedia.
  436. * @param stream the stream we received from calling getUserMedia.
  437. */
  438. function updateGrantedPermissions(um, stream) {
  439. const audioTracksReceived
  440. = Boolean(stream) && stream.getAudioTracks().length > 0;
  441. const videoTracksReceived
  442. = Boolean(stream) && stream.getVideoTracks().length > 0;
  443. const grantedPermissions = {};
  444. if (um.indexOf('video') !== -1) {
  445. grantedPermissions.video = videoTracksReceived;
  446. }
  447. if (um.indexOf('audio') !== -1) {
  448. grantedPermissions.audio = audioTracksReceived;
  449. }
  450. eventEmitter.emit(RTCEvents.GRANTED_PERMISSIONS, grantedPermissions);
  451. }
  452. /**
  453. * Checks if new list of available media devices differs from previous one.
  454. * @param {MediaDeviceInfo[]} newDevices - list of new devices.
  455. * @returns {boolean} - true if list is different, false otherwise.
  456. */
  457. function compareAvailableMediaDevices(newDevices) {
  458. if (newDevices.length !== availableDevices.length) {
  459. return true;
  460. }
  461. /* eslint-disable newline-per-chained-call */
  462. return (
  463. newDevices.map(mediaDeviceInfoToJSON).sort().join('')
  464. !== availableDevices
  465. .map(mediaDeviceInfoToJSON).sort().join(''));
  466. /* eslint-enable newline-per-chained-call */
  467. /**
  468. *
  469. * @param info
  470. */
  471. function mediaDeviceInfoToJSON(info) {
  472. return JSON.stringify({
  473. kind: info.kind,
  474. deviceId: info.deviceId,
  475. groupId: info.groupId,
  476. label: info.label,
  477. facing: info.facing
  478. });
  479. }
  480. }
  481. /**
  482. * Sends analytics event with the passed device list.
  483. *
  484. * @param {Array<MediaDeviceInfo>} deviceList - List with info about the
  485. * available devices.
  486. * @returns {void}
  487. */
  488. function sendDeviceListToAnalytics(deviceList) {
  489. const audioInputDeviceCount
  490. = deviceList.filter(d => d.kind === 'audioinput').length;
  491. const audioOutputDeviceCount
  492. = deviceList.filter(d => d.kind === 'audiooutput').length;
  493. const videoInputDeviceCount
  494. = deviceList.filter(d => d.kind === 'videoinput').length;
  495. const videoOutputDeviceCount
  496. = deviceList.filter(d => d.kind === 'videooutput').length;
  497. deviceList.forEach(device => {
  498. const attributes = {
  499. 'audio_input_device_count': audioInputDeviceCount,
  500. 'audio_output_device_count': audioOutputDeviceCount,
  501. 'video_input_device_count': videoInputDeviceCount,
  502. 'video_output_device_count': videoOutputDeviceCount,
  503. 'device_id': device.deviceId,
  504. 'device_group_id': device.groupId,
  505. 'device_kind': device.kind,
  506. 'device_label': device.label
  507. };
  508. Statistics.sendAnalytics(AVAILABLE_DEVICE, attributes);
  509. });
  510. }
  511. /**
  512. * Event handler for the 'devicechange' event.
  513. *
  514. * @param {MediaDeviceInfo[]} devices - list of media devices.
  515. * @emits RTCEvents.DEVICE_LIST_CHANGED
  516. */
  517. function onMediaDevicesListChanged(devicesReceived) {
  518. availableDevices = devicesReceived.slice(0);
  519. logger.info(
  520. 'list of media devices has changed:',
  521. availableDevices);
  522. sendDeviceListToAnalytics(availableDevices);
  523. eventEmitter.emit(RTCEvents.DEVICE_LIST_CHANGED, devicesReceived);
  524. }
  525. /**
  526. * Handles the newly created Media Streams.
  527. * @param streams the new Media Streams
  528. * @param resolution the resolution of the video streams
  529. * @returns {*[]} object that describes the new streams
  530. */
  531. function handleLocalStream(streams, resolution) {
  532. let audioStream, desktopStream, videoStream;
  533. const res = [];
  534. // XXX The function obtainAudioAndVideoPermissions has examined the type of
  535. // the browser, its capabilities, etc. and has taken the decision whether to
  536. // invoke getUserMedia per device (e.g. Firefox) or once for both audio and
  537. // video (e.g. Chrome). In order to not duplicate the logic here, examine
  538. // the specified streams and figure out what we've received based on
  539. // obtainAudioAndVideoPermissions' decision.
  540. if (streams) {
  541. // As mentioned above, certian types of browser (e.g. Chrome) support
  542. // (with a result which meets our requirements expressed bellow) calling
  543. // getUserMedia once for both audio and video.
  544. const audioVideo = streams.audioVideo;
  545. if (audioVideo) {
  546. const audioTracks = audioVideo.getAudioTracks();
  547. if (audioTracks.length) {
  548. audioStream = new MediaStream();
  549. for (let i = 0; i < audioTracks.length; i++) {
  550. audioStream.addTrack(audioTracks[i]);
  551. }
  552. }
  553. const videoTracks = audioVideo.getVideoTracks();
  554. if (videoTracks.length) {
  555. videoStream = new MediaStream();
  556. for (let j = 0; j < videoTracks.length; j++) {
  557. videoStream.addTrack(videoTracks[j]);
  558. }
  559. }
  560. } else {
  561. // On other types of browser (e.g. Firefox) we choose (namely,
  562. // obtainAudioAndVideoPermissions) to call getUserMedia per device
  563. // (type).
  564. audioStream = streams.audio;
  565. videoStream = streams.video;
  566. }
  567. desktopStream = streams.desktop;
  568. }
  569. if (desktopStream) {
  570. const { stream, sourceId, sourceType } = desktopStream;
  571. res.push({
  572. stream,
  573. sourceId,
  574. sourceType,
  575. track: stream.getVideoTracks()[0],
  576. mediaType: MediaType.VIDEO,
  577. videoType: VideoType.DESKTOP
  578. });
  579. }
  580. if (audioStream) {
  581. res.push({
  582. stream: audioStream,
  583. track: audioStream.getAudioTracks()[0],
  584. mediaType: MediaType.AUDIO,
  585. videoType: null
  586. });
  587. }
  588. if (videoStream) {
  589. res.push({
  590. stream: videoStream,
  591. track: videoStream.getVideoTracks()[0],
  592. mediaType: MediaType.VIDEO,
  593. videoType: VideoType.CAMERA,
  594. resolution
  595. });
  596. }
  597. return res;
  598. }
  599. /**
  600. * Represents a default implementation of setting a <tt>MediaStream</tt> as the
  601. * source of a video element that tries to be browser-agnostic through feature
  602. * checking. Note though that it was not completely clear from the predating
  603. * browser-specific implementations what &quot;videoSrc&quot; was because one
  604. * implementation of {@link RTCUtils#getVideoSrc} would return
  605. * <tt>MediaStream</tt> (e.g. Firefox), another a <tt>string</tt> representation
  606. * of the <tt>URL</tt> of the <tt>MediaStream</tt> (e.g. Chrome) and the return
  607. * value was only used by {@link RTCUIHelper#getVideoId} which itself did not
  608. * appear to be used anywhere. Generally, the implementation will try to follow
  609. * the related standards i.e. work with the <tt>srcObject</tt> and <tt>src</tt>
  610. * properties of the specified <tt>element</tt> taking into account vender
  611. * prefixes.
  612. *
  613. * @param element the element whose video source/src is to be set to the
  614. * specified <tt>stream</tt>
  615. * @param {MediaStream} stream the <tt>MediaStream</tt> to set as the video
  616. * source/src of <tt>element</tt>
  617. */
  618. function defaultSetVideoSrc(element, stream) {
  619. // srcObject
  620. let srcObjectPropertyName = 'srcObject';
  621. if (!(srcObjectPropertyName in element)) {
  622. srcObjectPropertyName = 'mozSrcObject';
  623. if (!(srcObjectPropertyName in element)) {
  624. srcObjectPropertyName = null;
  625. }
  626. }
  627. if (srcObjectPropertyName) {
  628. element[srcObjectPropertyName] = stream;
  629. return;
  630. }
  631. // src
  632. let src;
  633. if (stream) {
  634. src = stream.jitsiObjectURL;
  635. // Save the created URL for stream so we can reuse it and not keep
  636. // creating URLs.
  637. if (!src) {
  638. stream.jitsiObjectURL = src = URL.createObjectURL(stream);
  639. }
  640. }
  641. element.src = src || '';
  642. }
  643. /**
  644. *
  645. */
  646. class RTCUtils extends Listenable {
  647. /**
  648. *
  649. */
  650. constructor() {
  651. super(eventEmitter);
  652. }
  653. /**
  654. * Depending on the browser, sets difference instance methods for
  655. * interacting with user media and adds methods to native WebRTC-related
  656. * objects. Also creates an instance variable for peer connection
  657. * constraints.
  658. *
  659. * @param {Object} options
  660. * @returns {void}
  661. */
  662. init(options = {}) {
  663. if (typeof options.disableAEC === 'boolean') {
  664. disableAEC = options.disableAEC;
  665. logger.info(`Disable AEC: ${disableAEC}`);
  666. }
  667. if (typeof options.disableNS === 'boolean') {
  668. disableNS = options.disableNS;
  669. logger.info(`Disable NS: ${disableNS}`);
  670. }
  671. if (typeof options.disableAP === 'boolean') {
  672. disableAP = options.disableAP;
  673. logger.info(`Disable AP: ${disableAP}`);
  674. }
  675. if (typeof options.disableAGC === 'boolean') {
  676. disableAGC = options.disableAGC;
  677. logger.info(`Disable AGC: ${disableAGC}`);
  678. }
  679. if (typeof options.disableHPF === 'boolean') {
  680. disableHPF = options.disableHPF;
  681. logger.info(`Disable HPF: ${disableHPF}`);
  682. }
  683. availableDevices = undefined;
  684. window.clearInterval(availableDevicesPollTimer);
  685. availableDevicesPollTimer = undefined;
  686. this.enumerateDevices = initEnumerateDevicesWithCallback();
  687. if (browser.usesNewGumFlow()) {
  688. this.RTCPeerConnectionType = RTCPeerConnection;
  689. this.attachMediaStream
  690. = wrapAttachMediaStream((element, stream) => {
  691. if (element) {
  692. element.srcObject = stream;
  693. }
  694. });
  695. this.getStreamID = ({ id }) => id;
  696. this.getTrackID = ({ id }) => id;
  697. } else if (browser.isChromiumBased() // this is chrome < 61
  698. || browser.isReactNative()) {
  699. this.RTCPeerConnectionType = RTCPeerConnection;
  700. this.attachMediaStream
  701. = wrapAttachMediaStream((element, stream) => {
  702. defaultSetVideoSrc(element, stream);
  703. return element;
  704. });
  705. this.getStreamID = function({ id }) {
  706. // A. MediaStreams from FF endpoints have the characters '{' and
  707. // '}' that make jQuery choke.
  708. // B. The react-native-webrtc implementation that we use at the
  709. // time of this writing returns a number for the id of
  710. // MediaStream. Let's just say that a number contains no special
  711. // characters.
  712. return (
  713. typeof id === 'number'
  714. ? id
  715. : SDPUtil.filterSpecialChars(id));
  716. };
  717. this.getTrackID = ({ id }) => id;
  718. if (!MediaStream.prototype.getVideoTracks) {
  719. MediaStream.prototype.getVideoTracks = function() {
  720. return this.videoTracks;
  721. };
  722. }
  723. if (!MediaStream.prototype.getAudioTracks) {
  724. MediaStream.prototype.getAudioTracks = function() {
  725. return this.audioTracks;
  726. };
  727. }
  728. } else if (browser.isEdge()) {
  729. this.RTCPeerConnectionType = ortcRTCPeerConnection;
  730. this.attachMediaStream
  731. = wrapAttachMediaStream((element, stream) => {
  732. defaultSetVideoSrc(element, stream);
  733. });
  734. // ORTC does not generate remote MediaStreams so those are
  735. // manually created by the ORTC shim. This means that their
  736. // id (internally generated) does not match the stream id
  737. // signaled into the remote SDP. Therefore, the shim adds a
  738. // custom jitsiRemoteId property with the original stream id.
  739. this.getStreamID = function(stream) {
  740. const id = stream.jitsiRemoteId || stream.id;
  741. return SDPUtil.filterSpecialChars(id);
  742. };
  743. // Remote MediaStreamTracks generated by ORTC (within a
  744. // RTCRtpReceiver) have an internally/random id which does not match
  745. // the track id signaled in the remote SDP. The shim adds a custom
  746. // jitsi-id property with the original track id.
  747. this.getTrackID = track => track.jitsiRemoteId || track.id;
  748. } else {
  749. const message = 'Endpoint does not appear to be WebRTC-capable';
  750. logger.error(message);
  751. throw new Error(message);
  752. }
  753. this._initPCConstraints(options);
  754. screenObtainer.init(
  755. options,
  756. this.getUserMediaWithConstraints.bind(this));
  757. if (this.isDeviceListAvailable()) {
  758. this.enumerateDevices(ds => {
  759. availableDevices = ds.splice(0);
  760. logger.debug('Available devices: ', availableDevices);
  761. sendDeviceListToAnalytics(availableDevices);
  762. eventEmitter.emit(
  763. RTCEvents.DEVICE_LIST_AVAILABLE,
  764. availableDevices);
  765. // Use a shared callback to handle both the devicechange event
  766. // and the polling implementations. This prevents duplication
  767. // and works around a chrome bug (verified to occur on 68) where
  768. // devicechange fires twice in a row, which can cause async post
  769. // devicechange processing to collide.
  770. const updateKnownDevices = () => this.enumerateDevices(pds => {
  771. if (compareAvailableMediaDevices(pds)) {
  772. onMediaDevicesListChanged(pds);
  773. }
  774. });
  775. if (browser.supportsDeviceChangeEvent()) {
  776. navigator.mediaDevices.addEventListener(
  777. 'devicechange',
  778. updateKnownDevices);
  779. } else {
  780. // Periodically poll enumerateDevices() method to check if
  781. // list of media devices has changed.
  782. availableDevicesPollTimer = window.setInterval(
  783. updateKnownDevices,
  784. AVAILABLE_DEVICES_POLL_INTERVAL_TIME);
  785. }
  786. });
  787. }
  788. }
  789. /**
  790. * Creates instance objects for peer connection constraints both for p2p
  791. * and outside of p2p.
  792. *
  793. * @params {Object} options - Configuration for setting RTCUtil's instance
  794. * objects for peer connection constraints.
  795. * @params {boolean} options.useIPv6 - Set to true if IPv6 should be used.
  796. * @params {boolean} options.disableSuspendVideo - Whether or not video
  797. * should become suspended if bandwidth estimation becomes low.
  798. * @params {Object} options.testing - Additional configuration for work in
  799. * development.
  800. * @params {Object} options.testing.forceP2PSuspendVideoRatio - True if
  801. * video should become suspended if bandwidth estimation becomes low while
  802. * in peer to peer connection mode.
  803. */
  804. _initPCConstraints(options) {
  805. if (browser.isFirefox()) {
  806. this.pcConstraints = {};
  807. } else if (browser.isChromiumBased() || browser.isReactNative()) {
  808. this.pcConstraints = { optional: [
  809. { googHighStartBitrate: 0 },
  810. { googPayloadPadding: true },
  811. { googScreencastMinBitrate: 400 },
  812. { googCpuOveruseDetection: true },
  813. { googCpuOveruseEncodeUsage: true },
  814. { googCpuUnderuseThreshold: 55 },
  815. { googCpuOveruseThreshold: 85 }
  816. ] };
  817. if (options.useIPv6) {
  818. // https://code.google.com/p/webrtc/issues/detail?id=2828
  819. this.pcConstraints.optional.push({ googIPv6: true });
  820. }
  821. this.p2pPcConstraints
  822. = JSON.parse(JSON.stringify(this.pcConstraints));
  823. // Allows sending of video to be suspended if the bandwidth
  824. // estimation is too low.
  825. if (!options.disableSuspendVideo) {
  826. this.pcConstraints.optional.push(
  827. { googSuspendBelowMinBitrate: true });
  828. }
  829. // There's no reason not to use this for p2p
  830. this.p2pPcConstraints.optional.push({
  831. googSuspendBelowMinBitrate: true
  832. });
  833. }
  834. this.p2pPcConstraints = this.p2pPcConstraints || this.pcConstraints;
  835. }
  836. /* eslint-disable max-params */
  837. /**
  838. * @param {string[]} um required user media types
  839. * @param {Object} [options] optional parameters
  840. * @param {string} options.resolution
  841. * @param {number} options.bandwidth
  842. * @param {number} options.fps
  843. * @param {string} options.desktopStream
  844. * @param {string} options.cameraDeviceId
  845. * @param {string} options.micDeviceId
  846. * @param {Object} options.frameRate - used only for dekstop sharing.
  847. * @param {Object} options.frameRate.min - Minimum fps
  848. * @param {Object} options.frameRate.max - Maximum fps
  849. * @returns {Promise} Returns a media stream on success or a JitsiTrackError
  850. * on failure.
  851. **/
  852. getUserMediaWithConstraints(um, options = {}) {
  853. const constraints = getConstraints(um, options);
  854. logger.info('Get media constraints', constraints);
  855. return new Promise((resolve, reject) => {
  856. navigator.mediaDevices.getUserMedia(constraints)
  857. .then(stream => {
  858. logger.log('onUserMediaSuccess');
  859. updateGrantedPermissions(um, stream);
  860. resolve(stream);
  861. })
  862. .catch(error => {
  863. logger.warn('Failed to get access to local media. '
  864. + ` ${error} ${constraints} `);
  865. updateGrantedPermissions(um, undefined);
  866. reject(new JitsiTrackError(error, constraints, um));
  867. });
  868. });
  869. }
  870. /**
  871. * Acquires a media stream via getUserMedia that
  872. * matches the given constraints
  873. *
  874. * @param {array} umDevices which devices to acquire (e.g. audio, video)
  875. * @param {Object} constraints - Stream specifications to use.
  876. * @returns {Promise}
  877. */
  878. _newGetUserMediaWithConstraints(umDevices, constraints = {}) {
  879. return new Promise((resolve, reject) => {
  880. navigator.mediaDevices.getUserMedia(constraints)
  881. .then(stream => {
  882. logger.log('onUserMediaSuccess');
  883. updateGrantedPermissions(umDevices, stream);
  884. resolve(stream);
  885. })
  886. .catch(error => {
  887. logger.warn('Failed to get access to local media. '
  888. + ` ${error} ${constraints} `);
  889. updateGrantedPermissions(umDevices, undefined);
  890. reject(new JitsiTrackError(error, constraints, umDevices));
  891. });
  892. });
  893. }
  894. /**
  895. * Acquire a display stream via the screenObtainer. This requires extra
  896. * logic compared to use screenObtainer versus normal device capture logic
  897. * in RTCUtils#_newGetUserMediaWithConstraints.
  898. *
  899. * @param {Object} options
  900. * @param {Object} options.desktopSharingExtensionExternalInstallation
  901. * @param {string[]} options.desktopSharingSources
  902. * @param {Object} options.desktopSharingFrameRate
  903. * @param {Object} options.desktopSharingFrameRate.min - Minimum fps
  904. * @param {Object} options.desktopSharingFrameRate.max - Maximum fps
  905. * @returns {Promise} A promise which will be resolved with an object which
  906. * contains the acquired display stream. If desktop sharing is not supported
  907. * then a rejected promise will be returned.
  908. */
  909. _newGetDesktopMedia(options) {
  910. if (!screenObtainer.isSupported() || !browser.supportsVideo()) {
  911. return Promise.reject(
  912. new Error('Desktop sharing is not supported!'));
  913. }
  914. return new Promise((resolve, reject) => {
  915. screenObtainer.obtainStream(
  916. this._parseDesktopSharingOptions(options),
  917. stream => {
  918. resolve(stream);
  919. },
  920. error => {
  921. reject(error);
  922. });
  923. });
  924. }
  925. /* eslint-enable max-params */
  926. /**
  927. * Creates the local MediaStreams.
  928. * @param {Object} [options] optional parameters
  929. * @param {Array} options.devices the devices that will be requested
  930. * @param {string} options.resolution resolution constraints
  931. * @param {string} options.cameraDeviceId
  932. * @param {string} options.micDeviceId
  933. * @param {Object} options.desktopSharingFrameRate
  934. * @param {Object} options.desktopSharingFrameRate.min - Minimum fps
  935. * @param {Object} options.desktopSharingFrameRate.max - Maximum fps
  936. * @returns {*} Promise object that will receive the new JitsiTracks
  937. */
  938. obtainAudioAndVideoPermissions(options = {}) {
  939. options.devices = options.devices || [ ...OLD_GUM_DEFAULT_DEVICES ];
  940. options.resolution = options.resolution || OLD_GUM_DEFAULT_RESOLUTION;
  941. const requestingDesktop = options.devices.includes('desktop');
  942. if (requestingDesktop && !screenObtainer.isSupported()) {
  943. return Promise.reject(
  944. new Error('Desktop sharing is not supported!'));
  945. }
  946. let gumPromise;
  947. if (browser.supportsMediaStreamConstructor()) {
  948. gumPromise = this._getAudioAndVideoStreams(options);
  949. } else {
  950. // If the MediaStream constructor is not supported, then get tracks
  951. // in separate GUM calls in order to keep different tracks separate.
  952. gumPromise = this._getAudioAndVideoStreamsSeparately(options);
  953. }
  954. return gumPromise.then(streams =>
  955. handleLocalStream(streams, options.resolution));
  956. }
  957. /**
  958. * Performs one call to getUserMedia for audio and/or video and another call
  959. * for desktop.
  960. *
  961. * @param {Object} options - An object describing how the gUM request should
  962. * be executed. See {@link obtainAudioAndVideoPermissions} for full options.
  963. * @returns {*} Promise object that will receive the new JitsiTracks on
  964. * success or a JitsiTrackError on failure.
  965. */
  966. _getAudioAndVideoStreams(options) {
  967. const requestingDesktop = options.devices.includes('desktop');
  968. options.devices = options.devices.filter(device =>
  969. device !== 'desktop');
  970. const gumPromise = options.devices.length
  971. ? this.getUserMediaWithConstraints(options.devices, options)
  972. : Promise.resolve(null);
  973. return gumPromise
  974. .then(avStream => {
  975. // If any requested devices are missing, call gum again in
  976. // an attempt to obtain the actual error. For example, the
  977. // requested video device is missing or permission was
  978. // denied.
  979. const missingTracks
  980. = this._getMissingTracks(options.devices, avStream);
  981. if (missingTracks.length) {
  982. this.stopMediaStream(avStream);
  983. return this.getUserMediaWithConstraints(
  984. missingTracks, options)
  985. // GUM has already failed earlier and this success
  986. // handling should not be reached.
  987. .then(() => Promise.reject(new JitsiTrackError(
  988. { name: 'UnknownError' },
  989. getConstraints(options.devices, options),
  990. missingTracks)));
  991. }
  992. return avStream;
  993. })
  994. .then(audioVideo => {
  995. if (!requestingDesktop) {
  996. return { audioVideo };
  997. }
  998. if (options.desktopSharingSourceDevice) {
  999. this.stopMediaStream(audioVideo);
  1000. throw new Error('Using a camera as screenshare source is'
  1001. + 'not supported on this browser.');
  1002. }
  1003. return new Promise((resolve, reject) => {
  1004. screenObtainer.obtainStream(
  1005. this._parseDesktopSharingOptions(options),
  1006. desktop => resolve({
  1007. audioVideo,
  1008. desktop
  1009. }),
  1010. error => {
  1011. if (audioVideo) {
  1012. this.stopMediaStream(audioVideo);
  1013. }
  1014. reject(error);
  1015. });
  1016. });
  1017. });
  1018. }
  1019. /**
  1020. * Private utility for determining if the passed in MediaStream contains
  1021. * tracks of the type(s) specified in the requested devices.
  1022. *
  1023. * @param {string[]} requestedDevices - The track types that are expected to
  1024. * be includes in the stream.
  1025. * @param {MediaStream} stream - The MediaStream to check if it has the
  1026. * expected track types.
  1027. * @returns {string[]} An array of string with the missing track types. The
  1028. * array will be empty if all requestedDevices are found in the stream.
  1029. */
  1030. _getMissingTracks(requestedDevices = [], stream) {
  1031. const missingDevices = [];
  1032. const audioDeviceRequested = requestedDevices.includes('audio');
  1033. const audioTracksReceived
  1034. = stream && stream.getAudioTracks().length > 0;
  1035. if (audioDeviceRequested && !audioTracksReceived) {
  1036. missingDevices.push('audio');
  1037. }
  1038. const videoDeviceRequested = requestedDevices.includes('video');
  1039. const videoTracksReceived
  1040. = stream && stream.getVideoTracks().length > 0;
  1041. if (videoDeviceRequested && !videoTracksReceived) {
  1042. missingDevices.push('video');
  1043. }
  1044. return missingDevices;
  1045. }
  1046. /**
  1047. * Performs separate getUserMedia calls for audio and video instead of in
  1048. * one call. Will also request desktop if specified.
  1049. *
  1050. * @param {Object} options - An object describing how the gUM request should
  1051. * be executed. See {@link obtainAudioAndVideoPermissions} for full options.
  1052. * @returns {*} Promise object that will receive the new JitsiTracks on
  1053. * success or a JitsiTrackError on failure.
  1054. */
  1055. _getAudioAndVideoStreamsSeparately(options) {
  1056. return new Promise((resolve, reject) => {
  1057. const deviceGUM = {
  1058. audio: (...args) =>
  1059. this.getUserMediaWithConstraints([ 'audio' ], ...args),
  1060. video: (...args) =>
  1061. this.getUserMediaWithConstraints([ 'video' ], ...args),
  1062. desktop: (...args) =>
  1063. screenObtainer.obtainStream(
  1064. this._parseDesktopSharingOptions(options), ...args)
  1065. };
  1066. obtainDevices({
  1067. options,
  1068. streams: {},
  1069. successCallback: resolve,
  1070. errorCallback: reject,
  1071. deviceGUM
  1072. });
  1073. });
  1074. }
  1075. /**
  1076. * Returns an object formatted for specifying desktop sharing parameters.
  1077. *
  1078. * @param {Object} options - Takes in the same options object as
  1079. * {@link obtainAudioAndVideoPermissions}.
  1080. * @returns {Object}
  1081. */
  1082. _parseDesktopSharingOptions(options) {
  1083. return {
  1084. ...options.desktopSharingExtensionExternalInstallation,
  1085. desktopSharingSources: options.desktopSharingSources,
  1086. gumOptions: {
  1087. frameRate: options.desktopSharingFrameRate
  1088. },
  1089. trackOptions: getTrackSSConstraints(options)
  1090. };
  1091. }
  1092. /**
  1093. * Gets streams from specified device types. This function intentionally
  1094. * ignores errors for upstream to catch and handle instead.
  1095. *
  1096. * @param {Object} options - A hash describing what devices to get and
  1097. * relevant constraints.
  1098. * @param {string[]} options.devices - The types of media to capture. Valid
  1099. * values are "desktop", "audio", and "video".
  1100. * @param {Object} options.desktopSharingFrameRate
  1101. * @param {Object} options.desktopSharingFrameRate.min - Minimum fps
  1102. * @param {Object} options.desktopSharingFrameRate.max - Maximum fps
  1103. * @param {String} options.desktopSharingSourceDevice - The device id or
  1104. * label for a video input source that should be used for screensharing.
  1105. * @returns {Promise} The promise, when successful, will return an array of
  1106. * meta data for the requested device type, which includes the stream and
  1107. * track. If an error occurs, it will be deferred to the caller for
  1108. * handling.
  1109. */
  1110. newObtainAudioAndVideoPermissions(options) {
  1111. logger.info('Using the new gUM flow');
  1112. const mediaStreamsMetaData = [];
  1113. // Declare private functions to be used in the promise chain below.
  1114. // These functions are declared in the scope of this function because
  1115. // they are not being used anywhere else, so only this function needs to
  1116. // know about them.
  1117. /**
  1118. * Executes a request for desktop media if specified in options.
  1119. *
  1120. * @returns {Promise}
  1121. */
  1122. const maybeRequestDesktopDevice = function() {
  1123. const umDevices = options.devices || [];
  1124. const isDesktopDeviceRequested
  1125. = umDevices.indexOf('desktop') !== -1;
  1126. if (!isDesktopDeviceRequested) {
  1127. return Promise.resolve();
  1128. }
  1129. const {
  1130. desktopSharingExtensionExternalInstallation,
  1131. desktopSharingSourceDevice,
  1132. desktopSharingSources,
  1133. desktopSharingFrameRate
  1134. } = options;
  1135. // Attempt to use a video input device as a screenshare source if
  1136. // the option is defined.
  1137. if (desktopSharingSourceDevice) {
  1138. const matchingDevice
  1139. = availableDevices && availableDevices.find(device =>
  1140. device.kind === 'videoinput'
  1141. && (device.deviceId === desktopSharingSourceDevice
  1142. || device.label === desktopSharingSourceDevice));
  1143. const requestedDevices = [ 'video' ];
  1144. // Leverage the helper used by {@link _newGetDesktopMedia} to
  1145. // get constraints for the desktop stream.
  1146. const { gumOptions, trackOptions }
  1147. = this._parseDesktopSharingOptions(options);
  1148. // Create a custom constraints object to use exact device
  1149. // matching to make sure there is no fallthrough to another
  1150. // camera device. If a matching device could not be found, try
  1151. // anyways and let the caller handle errors.
  1152. const constraints = {
  1153. video: {
  1154. ...gumOptions,
  1155. deviceId: {
  1156. exact: (matchingDevice && matchingDevice.deviceId)
  1157. || desktopSharingSourceDevice
  1158. }
  1159. }
  1160. };
  1161. return this._newGetUserMediaWithConstraints(
  1162. requestedDevices, constraints)
  1163. .then(stream => {
  1164. const track = stream && stream.getTracks()[0];
  1165. const applyConstrainsPromise
  1166. = track && track.applyConstraints
  1167. ? track.applyConstraints(trackOptions)
  1168. : Promise.resolve();
  1169. return applyConstrainsPromise
  1170. .then(() => {
  1171. return {
  1172. sourceType: 'device',
  1173. stream
  1174. };
  1175. });
  1176. });
  1177. }
  1178. return this._newGetDesktopMedia({
  1179. desktopSharingExtensionExternalInstallation,
  1180. desktopSharingSources,
  1181. desktopSharingFrameRate
  1182. });
  1183. }.bind(this);
  1184. /**
  1185. * Creates a meta data object about the passed in desktopStream and
  1186. * pushes the meta data to the internal array mediaStreamsMetaData to be
  1187. * returned later.
  1188. *
  1189. * @param {MediaStreamTrack} desktopStream - A track for a desktop
  1190. * capture.
  1191. * @returns {void}
  1192. */
  1193. const maybeCreateAndAddDesktopTrack = function(desktopStream) {
  1194. if (!desktopStream) {
  1195. return;
  1196. }
  1197. const { stream, sourceId, sourceType } = desktopStream;
  1198. mediaStreamsMetaData.push({
  1199. stream,
  1200. sourceId,
  1201. sourceType,
  1202. track: stream.getVideoTracks()[0],
  1203. videoType: VideoType.DESKTOP
  1204. });
  1205. };
  1206. /**
  1207. * Executes a request for audio and/or video, as specified in options.
  1208. * By default both audio and video will be captured if options.devices
  1209. * is not defined.
  1210. *
  1211. * @returns {Promise}
  1212. */
  1213. const maybeRequestCaptureDevices = function() {
  1214. const umDevices = options.devices || [ 'audio', 'video' ];
  1215. const requestedCaptureDevices = umDevices.filter(device =>
  1216. device === 'audio'
  1217. || (device === 'video' && browser.supportsVideo()));
  1218. if (!requestedCaptureDevices.length) {
  1219. return Promise.resolve();
  1220. }
  1221. const constraints = newGetConstraints(
  1222. requestedCaptureDevices, options);
  1223. logger.info('Got media constraints: ', constraints);
  1224. return this._newGetUserMediaWithConstraints(
  1225. requestedCaptureDevices, constraints);
  1226. }.bind(this);
  1227. /**
  1228. * Splits the passed in media stream into separate audio and video
  1229. * streams and creates meta data objects for each and pushes them to the
  1230. * internal array mediaStreamsMetaData to be returned later.
  1231. *
  1232. * @param {MediaStreamTrack} avStream - A track for with audio and/or
  1233. * video track.
  1234. * @returns {void}
  1235. */
  1236. const maybeCreateAndAddAVTracks = function(avStream) {
  1237. if (!avStream) {
  1238. return;
  1239. }
  1240. const audioTracks = avStream.getAudioTracks();
  1241. if (audioTracks.length) {
  1242. const audioStream = new MediaStream(audioTracks);
  1243. mediaStreamsMetaData.push({
  1244. stream: audioStream,
  1245. track: audioStream.getAudioTracks()[0]
  1246. });
  1247. }
  1248. const videoTracks = avStream.getVideoTracks();
  1249. if (videoTracks.length) {
  1250. const videoStream = new MediaStream(videoTracks);
  1251. mediaStreamsMetaData.push({
  1252. stream: videoStream,
  1253. track: videoStream.getVideoTracks()[0],
  1254. videoType: VideoType.CAMERA
  1255. });
  1256. }
  1257. };
  1258. return maybeRequestDesktopDevice()
  1259. .then(maybeCreateAndAddDesktopTrack)
  1260. .then(maybeRequestCaptureDevices)
  1261. .then(maybeCreateAndAddAVTracks)
  1262. .then(() => mediaStreamsMetaData)
  1263. .catch(error => {
  1264. mediaStreamsMetaData.forEach(({ stream }) => {
  1265. this.stopMediaStream(stream);
  1266. });
  1267. return Promise.reject(error);
  1268. });
  1269. }
  1270. /**
  1271. * Checks whether it is possible to enumerate available cameras/microphones.
  1272. *
  1273. * @returns {boolean} {@code true} if the device listing is available;
  1274. * {@code false}, otherwise.
  1275. */
  1276. isDeviceListAvailable() {
  1277. return Boolean(
  1278. navigator.mediaDevices
  1279. && navigator.mediaDevices.enumerateDevices);
  1280. }
  1281. /**
  1282. * Returns true if changing the input (camera / microphone) or output
  1283. * (audio) device is supported and false if not.
  1284. * @params {string} [deviceType] - type of device to change. Default is
  1285. * undefined or 'input', 'output' - for audio output device change.
  1286. * @returns {boolean} true if available, false otherwise.
  1287. */
  1288. isDeviceChangeAvailable(deviceType) {
  1289. return deviceType === 'output' || deviceType === 'audiooutput'
  1290. ? isAudioOutputDeviceChangeAvailable
  1291. : browser.isChromiumBased()
  1292. || browser.isFirefox() || browser.isEdge();
  1293. }
  1294. /**
  1295. * A method to handle stopping of the stream.
  1296. * One point to handle the differences in various implementations.
  1297. * @param mediaStream MediaStream object to stop.
  1298. */
  1299. stopMediaStream(mediaStream) {
  1300. mediaStream.getTracks().forEach(track => {
  1301. if (track.stop) {
  1302. track.stop();
  1303. }
  1304. });
  1305. // leave stop for implementation still using it
  1306. if (mediaStream.stop) {
  1307. mediaStream.stop();
  1308. }
  1309. // The MediaStream implementation of the react-native-webrtc project has
  1310. // an explicit release method that is to be invoked in order to release
  1311. // used resources such as memory.
  1312. if (mediaStream.release) {
  1313. mediaStream.release();
  1314. }
  1315. // if we have done createObjectURL, lets clean it
  1316. const url = mediaStream.jitsiObjectURL;
  1317. if (url) {
  1318. delete mediaStream.jitsiObjectURL;
  1319. URL.revokeObjectURL(url);
  1320. }
  1321. }
  1322. /**
  1323. * Returns whether the desktop sharing is enabled or not.
  1324. * @returns {boolean}
  1325. */
  1326. isDesktopSharingEnabled() {
  1327. return screenObtainer.isSupported();
  1328. }
  1329. /**
  1330. * Sets current audio output device.
  1331. * @param {string} deviceId - id of 'audiooutput' device from
  1332. * navigator.mediaDevices.enumerateDevices(), 'default' for default
  1333. * device
  1334. * @returns {Promise} - resolves when audio output is changed, is rejected
  1335. * otherwise
  1336. */
  1337. setAudioOutputDevice(deviceId) {
  1338. if (!this.isDeviceChangeAvailable('output')) {
  1339. return Promise.reject(
  1340. new Error('Audio output device change is not supported'));
  1341. }
  1342. return featureDetectionAudioEl.setSinkId(deviceId)
  1343. .then(() => {
  1344. audioOutputDeviceId = deviceId;
  1345. audioOutputChanged = true;
  1346. logger.log(`Audio output device set to ${deviceId}`);
  1347. eventEmitter.emit(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  1348. deviceId);
  1349. });
  1350. }
  1351. /**
  1352. * Returns currently used audio output device id, '' stands for default
  1353. * device
  1354. * @returns {string}
  1355. */
  1356. getAudioOutputDevice() {
  1357. return audioOutputDeviceId;
  1358. }
  1359. /**
  1360. * Returns list of available media devices if its obtained, otherwise an
  1361. * empty array is returned/
  1362. * @returns {Array} list of available media devices.
  1363. */
  1364. getCurrentlyAvailableMediaDevices() {
  1365. return availableDevices;
  1366. }
  1367. /**
  1368. * Returns event data for device to be reported to stats.
  1369. * @returns {MediaDeviceInfo} device.
  1370. */
  1371. getEventDataForActiveDevice(device) {
  1372. const deviceList = [];
  1373. const deviceData = {
  1374. 'deviceId': device.deviceId,
  1375. 'kind': device.kind,
  1376. 'label': device.label,
  1377. 'groupId': device.groupId
  1378. };
  1379. deviceList.push(deviceData);
  1380. return { deviceList };
  1381. }
  1382. /**
  1383. * Configures the given PeerConnection constraints to either enable or
  1384. * disable (according to the value of the 'enable' parameter) the
  1385. * 'googSuspendBelowMinBitrate' option.
  1386. * @param constraints the constraints on which to operate.
  1387. * @param enable {boolean} whether to enable or disable the suspend video
  1388. * option.
  1389. */
  1390. setSuspendVideo(constraints, enable) {
  1391. if (!constraints.optional) {
  1392. constraints.optional = [];
  1393. }
  1394. // Get rid of all "googSuspendBelowMinBitrate" constraints (we assume
  1395. // that the elements of constraints.optional contain a single property).
  1396. constraints.optional
  1397. = constraints.optional.filter(
  1398. c => !c.hasOwnProperty('googSuspendBelowMinBitrate'));
  1399. if (enable) {
  1400. constraints.optional.push({ googSuspendBelowMinBitrate: 'true' });
  1401. }
  1402. }
  1403. }
  1404. const rtcUtils = new RTCUtils();
  1405. /**
  1406. *
  1407. * @param context Execution context, containing options and callbacks
  1408. */
  1409. function obtainDevices(context) {
  1410. if (!context.options.devices || context.options.devices.length === 0) {
  1411. return context.successCallback(context.streams || {});
  1412. }
  1413. const device = context.options.devices.splice(0, 1);
  1414. context.deviceGUM[device](context.options)
  1415. .then(stream => {
  1416. context.streams = context.streams || {};
  1417. context.streams[device] = stream;
  1418. obtainDevices(context);
  1419. }, error => {
  1420. Object.keys(context.streams).forEach(
  1421. d => rtcUtils.stopMediaStream(context.streams[d]));
  1422. logger.error(
  1423. `failed to obtain ${device} stream - stop`, error);
  1424. context.errorCallback(error);
  1425. });
  1426. }
  1427. /**
  1428. * Wraps original attachMediaStream function to set current audio output device
  1429. * if this is supported.
  1430. * @param {Function} origAttachMediaStream
  1431. * @returns {Function}
  1432. */
  1433. function wrapAttachMediaStream(origAttachMediaStream) {
  1434. return function(element, stream) {
  1435. // eslint-disable-next-line prefer-rest-params
  1436. const res = origAttachMediaStream.apply(rtcUtils, arguments);
  1437. if (stream
  1438. && rtcUtils.isDeviceChangeAvailable('output')
  1439. && stream.getAudioTracks
  1440. && stream.getAudioTracks().length
  1441. // we skip setting audio output if there was no explicit change
  1442. && audioOutputChanged) {
  1443. element.setSinkId(rtcUtils.getAudioOutputDevice())
  1444. .catch(function(ex) {
  1445. const err
  1446. = new JitsiTrackError(ex, null, [ 'audiooutput' ]);
  1447. GlobalOnErrorHandler.callUnhandledRejectionHandler({
  1448. promise: this, // eslint-disable-line no-invalid-this
  1449. reason: err
  1450. });
  1451. logger.warn(
  1452. 'Failed to set audio output device for the element.'
  1453. + ' Default audio output device will be used'
  1454. + ' instead',
  1455. element,
  1456. err);
  1457. });
  1458. }
  1459. return res;
  1460. };
  1461. }
  1462. export default rtcUtils;