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

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