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

RTCUtils.js 59KB

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