您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RTCUtils.js 63KB

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