Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RTCUtils.js 67KB

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