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 62KB

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