You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTCUtils.js 57KB

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