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

RTCUtils.js 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /* global $,
  2. __filename,
  3. attachMediaStream,
  4. MediaStreamTrack,
  5. RTCIceCandidate: true,
  6. RTCPeerConnection,
  7. RTCSessionDescription: true,
  8. mozRTCIceCandidate,
  9. mozRTCPeerConnection,
  10. mozRTCSessionDescription,
  11. webkitMediaStream,
  12. webkitRTCPeerConnection,
  13. webkitURL
  14. */
  15. import CameraFacingMode from '../../service/RTC/CameraFacingMode';
  16. import EventEmitter from 'events';
  17. import { getLogger } from 'jitsi-meet-logger';
  18. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  19. import JitsiTrackError from '../../JitsiTrackError';
  20. import Listenable from '../util/Listenable';
  21. import * as MediaType from '../../service/RTC/MediaType';
  22. import Resolutions from '../../service/RTC/Resolutions';
  23. import RTCBrowserType from './RTCBrowserType';
  24. import RTCEvents from '../../service/RTC/RTCEvents';
  25. import ortcRTCPeerConnection from './ortc/RTCPeerConnection';
  26. import screenObtainer from './ScreenObtainer';
  27. import SDPUtil from '../xmpp/SDPUtil';
  28. import VideoType from '../../service/RTC/VideoType';
  29. const logger = getLogger(__filename);
  30. // XXX Don't require Temasys unless it's to be used because it doesn't run on
  31. // React Native, for example.
  32. const AdapterJS
  33. = RTCBrowserType.isTemasysPluginUsed()
  34. ? require('./adapter.screenshare')
  35. : undefined;
  36. const eventEmitter = new EventEmitter();
  37. const AVAILABLE_DEVICES_POLL_INTERVAL_TIME = 3000; // ms
  38. const devices = {
  39. audio: false,
  40. video: false
  41. };
  42. // Currently audio output device change is supported only in Chrome and
  43. // default output always has 'default' device ID
  44. let audioOutputDeviceId = 'default'; // default device
  45. // whether user has explicitly set a device to use
  46. let audioOutputChanged = false;
  47. // Disables all audio processing
  48. let disableAP = false;
  49. // Disables Acoustic Echo Cancellation
  50. let disableAEC = false;
  51. // Disables Noise Suppression
  52. let disableNS = false;
  53. // Disables Automatic Gain Control
  54. let disableAGC = false;
  55. // Disables Highpass Filter
  56. let disableHPF = false;
  57. const featureDetectionAudioEl = document.createElement('audio');
  58. const isAudioOutputDeviceChangeAvailable
  59. = typeof featureDetectionAudioEl.setSinkId !== 'undefined';
  60. let currentlyAvailableMediaDevices;
  61. /**
  62. * "rawEnumerateDevicesWithCallback" will be initialized only after WebRTC is
  63. * ready. Otherwise it is too early to assume that the devices listing is not
  64. * supported.
  65. */
  66. let rawEnumerateDevicesWithCallback;
  67. /**
  68. *
  69. */
  70. function initRawEnumerateDevicesWithCallback() {
  71. rawEnumerateDevicesWithCallback = navigator.mediaDevices
  72. && navigator.mediaDevices.enumerateDevices
  73. ? function(callback) {
  74. navigator.mediaDevices.enumerateDevices().then(
  75. callback,
  76. () => callback([]));
  77. }
  78. // Safari:
  79. // "ReferenceError: Can't find variable: MediaStreamTrack"
  80. // when Temasys plugin is not installed yet, have to delay this call
  81. // until WebRTC is ready.
  82. : MediaStreamTrack && MediaStreamTrack.getSources
  83. ? function(callback) {
  84. MediaStreamTrack.getSources(
  85. sources =>
  86. callback(sources.map(convertMediaStreamTrackSource)));
  87. }
  88. : undefined;
  89. }
  90. // TODO: currently no browser supports 'devicechange' event even in nightly
  91. // builds so no feature/browser detection is used at all. However in future this
  92. // should be changed to some expression. Progress on 'devicechange' event
  93. // implementation for Chrome/Opera/NWJS can be tracked at
  94. // https://bugs.chromium.org/p/chromium/issues/detail?id=388648, for Firefox -
  95. // at https://bugzilla.mozilla.org/show_bug.cgi?id=1152383. More information on
  96. // 'devicechange' event can be found in spec -
  97. // http://w3c.github.io/mediacapture-main/#event-mediadevices-devicechange
  98. // TODO: check MS Edge
  99. const isDeviceChangeEventSupported = false;
  100. let rtcReady = false;
  101. /**
  102. *
  103. * @param constraints
  104. * @param resolution
  105. */
  106. function setResolutionConstraints(
  107. constraints, isNewStyleConstraintsSupported, resolution) {
  108. if (Resolutions[resolution]) {
  109. if (isNewStyleConstraintsSupported) {
  110. constraints.video.width = {
  111. ideal: Resolutions[resolution].width
  112. };
  113. constraints.video.height = {
  114. ideal: Resolutions[resolution].height
  115. };
  116. }
  117. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  118. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  119. }
  120. if (constraints.video.mandatory.minWidth) {
  121. constraints.video.mandatory.maxWidth
  122. = constraints.video.mandatory.minWidth;
  123. }
  124. if (constraints.video.mandatory.minHeight) {
  125. constraints.video.mandatory.maxHeight
  126. = constraints.video.mandatory.minHeight;
  127. }
  128. }
  129. /**
  130. * @param {string[]} um required user media types
  131. *
  132. * @param {Object} [options={}] optional parameters
  133. * @param {string} options.resolution
  134. * @param {number} options.bandwidth
  135. * @param {number} options.fps
  136. * @param {string} options.desktopStream
  137. * @param {string} options.cameraDeviceId
  138. * @param {string} options.micDeviceId
  139. * @param {CameraFacingMode} options.facingMode
  140. * @param {bool} firefox_fake_device
  141. */
  142. function getConstraints(um, options) {
  143. const constraints = { audio: false,
  144. video: false };
  145. // Don't mix new and old style settings for Chromium as this leads
  146. // to TypeError in new Chromium versions. @see
  147. // https://bugs.chromium.org/p/chromium/issues/detail?id=614716
  148. // This is a temporary solution, in future we will fully split old and
  149. // new style constraints when new versions of Chromium and Firefox will
  150. // have stable support of new constraints format. For more information
  151. // @see https://github.com/jitsi/lib-jitsi-meet/pull/136
  152. const isNewStyleConstraintsSupported
  153. = RTCBrowserType.isFirefox()
  154. || RTCBrowserType.isEdge()
  155. || RTCBrowserType.isReactNative()
  156. || RTCBrowserType.isTemasysPluginUsed();
  157. if (um.indexOf('video') >= 0) {
  158. // same behaviour as true
  159. constraints.video = { mandatory: {},
  160. optional: [] };
  161. if (options.cameraDeviceId) {
  162. if (isNewStyleConstraintsSupported) {
  163. // New style of setting device id.
  164. constraints.video.deviceId = options.cameraDeviceId;
  165. }
  166. // Old style.
  167. constraints.video.optional.push({
  168. sourceId: options.cameraDeviceId
  169. });
  170. } else {
  171. // Prefer the front i.e. user-facing camera (to the back i.e.
  172. // environment-facing camera, for example).
  173. // TODO: Maybe use "exact" syntax if options.facingMode is defined,
  174. // but this probably needs to be decided when updating other
  175. // constraints, as we currently don't use "exact" syntax anywhere.
  176. const facingMode = options.facingMode || CameraFacingMode.USER;
  177. if (isNewStyleConstraintsSupported) {
  178. constraints.video.facingMode = facingMode;
  179. }
  180. constraints.video.optional.push({
  181. facingMode
  182. });
  183. }
  184. if (options.minFps || options.maxFps || options.fps) {
  185. // for some cameras it might be necessary to request 30fps
  186. // so they choose 30fps mjpg over 10fps yuy2
  187. if (options.minFps || options.fps) {
  188. // Fall back to options.fps for backwards compatibility
  189. options.minFps = options.minFps || options.fps;
  190. constraints.video.mandatory.minFrameRate = options.minFps;
  191. }
  192. if (options.maxFps) {
  193. constraints.video.mandatory.maxFrameRate = options.maxFps;
  194. }
  195. }
  196. setResolutionConstraints(
  197. constraints, isNewStyleConstraintsSupported, options.resolution);
  198. }
  199. if (um.indexOf('audio') >= 0) {
  200. if (RTCBrowserType.isReactNative()) {
  201. // The react-native-webrtc project that we're currently using
  202. // expects the audio constraint to be a boolean.
  203. constraints.audio = true;
  204. } else if (RTCBrowserType.isFirefox()) {
  205. if (options.micDeviceId) {
  206. constraints.audio = {
  207. mandatory: {},
  208. deviceId: options.micDeviceId, // new style
  209. optional: [ {
  210. sourceId: options.micDeviceId // old style
  211. } ] };
  212. } else {
  213. constraints.audio = true;
  214. }
  215. } else {
  216. // same behaviour as true
  217. constraints.audio = { mandatory: {},
  218. optional: [] };
  219. if (options.micDeviceId) {
  220. if (isNewStyleConstraintsSupported) {
  221. // New style of setting device id.
  222. constraints.audio.deviceId = options.micDeviceId;
  223. }
  224. // Old style.
  225. constraints.audio.optional.push({
  226. sourceId: options.micDeviceId
  227. });
  228. }
  229. // if it is good enough for hangouts...
  230. constraints.audio.optional.push(
  231. { echoCancellation: !disableAP },
  232. { googEchoCancellation: !disableAEC },
  233. { googAutoGainControl: !disableAGC },
  234. { googNoiseSupression: !disableNS },
  235. { googHighpassFilter: !disableHPF },
  236. { googNoiseSuppression2: !disableNS },
  237. { googEchoCancellation2: !disableAEC },
  238. { googAutoGainControl2: !disableAGC }
  239. );
  240. }
  241. }
  242. if (um.indexOf('screen') >= 0) {
  243. if (RTCBrowserType.isChrome()) {
  244. constraints.video = {
  245. mandatory: {
  246. chromeMediaSource: 'screen',
  247. maxWidth: window.screen.width,
  248. maxHeight: window.screen.height,
  249. maxFrameRate: 3
  250. },
  251. optional: []
  252. };
  253. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  254. constraints.video = {
  255. optional: [
  256. {
  257. sourceId: AdapterJS.WebRTCPlugin.plugin.screensharingKey
  258. }
  259. ]
  260. };
  261. } else if (RTCBrowserType.isFirefox()) {
  262. constraints.video = {
  263. mozMediaSource: 'window',
  264. mediaSource: 'window'
  265. };
  266. } else {
  267. const errmsg
  268. = '\'screen\' WebRTC media source is supported only in Chrome'
  269. + ' and with Temasys plugin';
  270. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  271. logger.error(errmsg);
  272. }
  273. }
  274. if (um.indexOf('desktop') >= 0) {
  275. constraints.video = {
  276. mandatory: {
  277. chromeMediaSource: 'desktop',
  278. chromeMediaSourceId: options.desktopStream,
  279. maxWidth: window.screen.width,
  280. maxHeight: window.screen.height,
  281. maxFrameRate: 3
  282. },
  283. optional: []
  284. };
  285. }
  286. if (options.bandwidth) {
  287. if (!constraints.video) {
  288. // same behaviour as true
  289. constraints.video = { mandatory: {},
  290. optional: [] };
  291. }
  292. constraints.video.optional.push({ bandwidth: options.bandwidth });
  293. }
  294. // we turn audio for both audio and video tracks, the fake audio & video
  295. // seems to work only when enabled in one getUserMedia call, we cannot get
  296. // fake audio separate by fake video this later can be a problem with some
  297. // of the tests
  298. if (RTCBrowserType.isFirefox() && options.firefox_fake_device) {
  299. // seems to be fixed now, removing this experimental fix, as having
  300. // multiple audio tracks brake the tests
  301. // constraints.audio = true;
  302. constraints.fake = true;
  303. }
  304. return constraints;
  305. }
  306. /**
  307. * Sets the availbale devices based on the options we requested and the
  308. * streams we received.
  309. * @param um the options we requested to getUserMedia.
  310. * @param stream the stream we received from calling getUserMedia.
  311. */
  312. function setAvailableDevices(um, stream) {
  313. const audioTracksReceived = stream && stream.getAudioTracks().length > 0;
  314. const videoTracksReceived = stream && stream.getVideoTracks().length > 0;
  315. if (um.indexOf('video') !== -1) {
  316. devices.video = videoTracksReceived;
  317. }
  318. if (um.indexOf('audio') !== -1) {
  319. devices.audio = audioTracksReceived;
  320. }
  321. eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, devices);
  322. }
  323. /**
  324. * Checks if new list of available media devices differs from previous one.
  325. * @param {MediaDeviceInfo[]} newDevices - list of new devices.
  326. * @returns {boolean} - true if list is different, false otherwise.
  327. */
  328. function compareAvailableMediaDevices(newDevices) {
  329. if (newDevices.length !== currentlyAvailableMediaDevices.length) {
  330. return true;
  331. }
  332. return (
  333. newDevices
  334. .map(mediaDeviceInfoToJSON)
  335. .sort()
  336. .join('')
  337. !== currentlyAvailableMediaDevices
  338. .map(mediaDeviceInfoToJSON)
  339. .sort()
  340. .join(''));
  341. /**
  342. *
  343. * @param info
  344. */
  345. function mediaDeviceInfoToJSON(info) {
  346. return JSON.stringify({
  347. kind: info.kind,
  348. deviceId: info.deviceId,
  349. groupId: info.groupId,
  350. label: info.label,
  351. facing: info.facing
  352. });
  353. }
  354. }
  355. /**
  356. * Periodically polls enumerateDevices() method to check if list of media
  357. * devices has changed. This is temporary workaround until 'devicechange' event
  358. * will be supported by browsers.
  359. */
  360. function pollForAvailableMediaDevices() {
  361. // Here we use plain navigator.mediaDevices.enumerateDevices instead of
  362. // wrapped because we just need to know the fact the devices changed, labels
  363. // do not matter. This fixes situation when we have no devices initially,
  364. // and then plug in a new one.
  365. if (rawEnumerateDevicesWithCallback) {
  366. rawEnumerateDevicesWithCallback(ds => {
  367. // We don't fire RTCEvents.DEVICE_LIST_CHANGED for the first time
  368. // we call enumerateDevices(). This is the initial step.
  369. if (typeof currentlyAvailableMediaDevices === 'undefined') {
  370. currentlyAvailableMediaDevices = ds.slice(0);
  371. } else if (compareAvailableMediaDevices(ds)) {
  372. onMediaDevicesListChanged(ds);
  373. }
  374. window.setTimeout(pollForAvailableMediaDevices,
  375. AVAILABLE_DEVICES_POLL_INTERVAL_TIME);
  376. });
  377. }
  378. }
  379. /**
  380. * Event handler for the 'devicechange' event.
  381. *
  382. * @param {MediaDeviceInfo[]} devices - list of media devices.
  383. * @emits RTCEvents.DEVICE_LIST_CHANGED
  384. */
  385. function onMediaDevicesListChanged(devicesReceived) {
  386. currentlyAvailableMediaDevices = devicesReceived.slice(0);
  387. logger.info(
  388. 'list of media devices has changed:',
  389. currentlyAvailableMediaDevices);
  390. const videoInputDevices
  391. = currentlyAvailableMediaDevices.filter(d => d.kind === 'videoinput');
  392. const audioInputDevices
  393. = currentlyAvailableMediaDevices.filter(d => d.kind === 'audioinput');
  394. const videoInputDevicesWithEmptyLabels
  395. = videoInputDevices.filter(d => d.label === '');
  396. const audioInputDevicesWithEmptyLabels
  397. = audioInputDevices.filter(d => d.label === '');
  398. if (videoInputDevices.length
  399. && videoInputDevices.length
  400. === videoInputDevicesWithEmptyLabels.length) {
  401. devices.video = false;
  402. }
  403. if (audioInputDevices.length
  404. && audioInputDevices.length
  405. === audioInputDevicesWithEmptyLabels.length) {
  406. devices.audio = false;
  407. }
  408. eventEmitter.emit(RTCEvents.DEVICE_LIST_CHANGED, devicesReceived);
  409. }
  410. /**
  411. * Apply function with arguments if function exists.
  412. * Do nothing if function not provided.
  413. * @param {function} [fn] function to apply
  414. * @param {Array} [args=[]] arguments for function
  415. */
  416. function maybeApply(fn, args) {
  417. fn && fn(...args);
  418. }
  419. /**
  420. * Wrap `getUserMedia` in order to convert between callback and Promise based
  421. * APIs.
  422. * @param {Function} getUserMedia native function
  423. * @returns {Function} wrapped function
  424. */
  425. function wrapGetUserMedia(getUserMedia, usePromises = false) {
  426. let gUM;
  427. if (usePromises) {
  428. gUM = function(constraints, successCallback, errorCallback) {
  429. return getUserMedia(constraints)
  430. .then(stream => {
  431. maybeApply(successCallback, [ stream ]);
  432. return stream;
  433. })
  434. .catch(error => {
  435. maybeApply(errorCallback, [ error ]);
  436. throw error;
  437. });
  438. };
  439. } else {
  440. gUM = function(constraints, successCallback, errorCallback) {
  441. getUserMedia(constraints, stream => {
  442. maybeApply(successCallback, [ stream ]);
  443. }, error => {
  444. maybeApply(errorCallback, [ error ]);
  445. });
  446. };
  447. }
  448. return gUM;
  449. }
  450. /**
  451. * Use old MediaStreamTrack to get devices list and
  452. * convert it to enumerateDevices format.
  453. * @param {Function} callback function to call when received devices list.
  454. */
  455. function enumerateDevicesThroughMediaStreamTrack(callback) {
  456. MediaStreamTrack.getSources(
  457. sources => callback(sources.map(convertMediaStreamTrackSource)));
  458. }
  459. /**
  460. * Converts MediaStreamTrack Source to enumerateDevices format.
  461. * @param {Object} source
  462. */
  463. function convertMediaStreamTrackSource(source) {
  464. const kind = (source.kind || '').toLowerCase();
  465. return {
  466. facing: source.facing || null,
  467. label: source.label,
  468. // theoretically deprecated MediaStreamTrack.getSources should
  469. // not return 'audiooutput' devices but let's handle it in any
  470. // case
  471. kind: kind
  472. ? kind === 'audiooutput' ? kind : `${kind}input`
  473. : null,
  474. deviceId: source.id,
  475. groupId: source.groupId || null
  476. };
  477. }
  478. /**
  479. * Handles the newly created Media Streams.
  480. * @param streams the new Media Streams
  481. * @param resolution the resolution of the video streams
  482. * @returns {*[]} object that describes the new streams
  483. */
  484. function handleLocalStream(streams, resolution) {
  485. let audioStream, desktopStream, videoStream;
  486. const res = [];
  487. // XXX The function obtainAudioAndVideoPermissions has examined the type of
  488. // the browser, its capabilities, etc. and has taken the decision whether to
  489. // invoke getUserMedia per device (e.g. Firefox) or once for both audio and
  490. // video (e.g. Chrome). In order to not duplicate the logic here, examine
  491. // the specified streams and figure out what we've received based on
  492. // obtainAudioAndVideoPermissions' decision.
  493. if (streams) {
  494. // As mentioned above, certian types of browser (e.g. Chrome) support
  495. // (with a result which meets our requirements expressed bellow) calling
  496. // getUserMedia once for both audio and video.
  497. const audioVideo = streams.audioVideo;
  498. if (audioVideo) {
  499. const NativeMediaStream
  500. = window.webkitMediaStream || window.MediaStream;
  501. const audioTracks = audioVideo.getAudioTracks();
  502. if (audioTracks.length) {
  503. // eslint-disable-next-line new-cap
  504. audioStream = new NativeMediaStream();
  505. for (let i = 0; i < audioTracks.length; i++) {
  506. audioStream.addTrack(audioTracks[i]);
  507. }
  508. }
  509. const videoTracks = audioVideo.getVideoTracks();
  510. if (videoTracks.length) {
  511. // eslint-disable-next-line new-cap
  512. videoStream = new NativeMediaStream();
  513. for (let j = 0; j < videoTracks.length; j++) {
  514. videoStream.addTrack(videoTracks[j]);
  515. }
  516. }
  517. } else {
  518. // On other types of browser (e.g. Firefox) we choose (namely,
  519. // obtainAudioAndVideoPermissions) to call getUserMedia per device
  520. // (type).
  521. audioStream = streams.audio;
  522. videoStream = streams.video;
  523. }
  524. desktopStream = streams.desktop;
  525. }
  526. if (desktopStream) {
  527. const { stream, sourceId, sourceType } = desktopStream;
  528. res.push({
  529. stream,
  530. sourceId,
  531. sourceType,
  532. track: stream.getVideoTracks()[0],
  533. mediaType: MediaType.VIDEO,
  534. videoType: VideoType.DESKTOP
  535. });
  536. }
  537. if (audioStream) {
  538. res.push({
  539. stream: audioStream,
  540. track: audioStream.getAudioTracks()[0],
  541. mediaType: MediaType.AUDIO,
  542. videoType: null
  543. });
  544. }
  545. if (videoStream) {
  546. res.push({
  547. stream: videoStream,
  548. track: videoStream.getVideoTracks()[0],
  549. mediaType: MediaType.VIDEO,
  550. videoType: VideoType.CAMERA,
  551. resolution
  552. });
  553. }
  554. return res;
  555. }
  556. /**
  557. * Represents a default implementation of setting a <tt>MediaStream</tt> as the
  558. * source of a video element that tries to be browser-agnostic through feature
  559. * checking. Note though that it was not completely clear from the predating
  560. * browser-specific implementations what &quot;videoSrc&quot; was because one
  561. * implementation of {@link RTCUtils#getVideoSrc} would return
  562. * <tt>MediaStream</tt> (e.g. Firefox), another a <tt>string</tt> representation
  563. * of the <tt>URL</tt> of the <tt>MediaStream</tt> (e.g. Chrome) and the return
  564. * value was only used by {@link RTCUIHelper#getVideoId} which itself did not
  565. * appear to be used anywhere. Generally, the implementation will try to follow
  566. * the related standards i.e. work with the <tt>srcObject</tt> and <tt>src</tt>
  567. * properties of the specified <tt>element</tt> taking into account vender
  568. * prefixes.
  569. *
  570. * @param element the element whose video source/src is to be set to the
  571. * specified <tt>stream</tt>
  572. * @param {MediaStream} stream the <tt>MediaStream</tt> to set as the video
  573. * source/src of <tt>element</tt>
  574. */
  575. function defaultSetVideoSrc(element, stream) {
  576. // srcObject
  577. let srcObjectPropertyName = 'srcObject';
  578. if (!(srcObjectPropertyName in element)) {
  579. srcObjectPropertyName = 'mozSrcObject';
  580. if (!(srcObjectPropertyName in element)) {
  581. srcObjectPropertyName = null;
  582. }
  583. }
  584. if (srcObjectPropertyName) {
  585. element[srcObjectPropertyName] = stream;
  586. return;
  587. }
  588. // src
  589. let src;
  590. if (stream) {
  591. src = stream.jitsiObjectURL;
  592. // Save the created URL for stream so we can reuse it and not keep
  593. // creating URLs.
  594. if (!src) {
  595. stream.jitsiObjectURL
  596. = src
  597. = (URL || webkitURL).createObjectURL(stream);
  598. }
  599. }
  600. element.src = src || '';
  601. }
  602. /**
  603. *
  604. */
  605. class RTCUtils extends Listenable {
  606. /**
  607. *
  608. */
  609. constructor() {
  610. super(eventEmitter);
  611. }
  612. /**
  613. *
  614. * @param options
  615. */
  616. init(options) {
  617. if (typeof options.disableAEC === 'boolean') {
  618. disableAEC = options.disableAEC;
  619. logger.info(`Disable AEC: ${disableAEC}`);
  620. }
  621. if (typeof options.disableNS === 'boolean') {
  622. disableNS = options.disableNS;
  623. logger.info(`Disable NS: ${disableNS}`);
  624. }
  625. if (typeof options.disableAP === 'boolean') {
  626. disableAP = options.disableAP;
  627. logger.info(`Disable AP: ${disableAP}`);
  628. }
  629. if (typeof options.disableAGC === 'boolean') {
  630. disableAGC = options.disableAGC;
  631. logger.info(`Disable AGC: ${disableAGC}`);
  632. }
  633. if (typeof options.disableHPF === 'boolean') {
  634. disableHPF = options.disableHPF;
  635. logger.info(`Disable HPF: ${disableHPF}`);
  636. }
  637. // Initialize rawEnumerateDevicesWithCallback
  638. initRawEnumerateDevicesWithCallback();
  639. return new Promise((resolve, reject) => {
  640. if (RTCBrowserType.isFirefox()) {
  641. const FFversion = RTCBrowserType.getFirefoxVersion();
  642. if (FFversion < 40) {
  643. rejectWithWebRTCNotSupported(
  644. `Firefox version too old: ${FFversion}.`
  645. + ' Required >= 40.',
  646. reject);
  647. return;
  648. }
  649. this.RTCPeerConnectionType = mozRTCPeerConnection;
  650. this.getUserMedia
  651. = wrapGetUserMedia(
  652. navigator.mozGetUserMedia.bind(navigator));
  653. this.enumerateDevices = rawEnumerateDevicesWithCallback;
  654. this.pcConstraints = {};
  655. this.attachMediaStream
  656. = wrapAttachMediaStream((element, stream) => {
  657. // srcObject is being standardized and FF will
  658. // eventually support that unprefixed. FF also supports
  659. // the "element.src = URL.createObjectURL(...)" combo,
  660. // but that will be deprecated in favour of srcObject.
  661. //
  662. // https://groups.google.com/forum/#!topic/
  663. // mozilla.dev.media/pKOiioXonJg
  664. // https://github.com/webrtc/samples/issues/302
  665. if (element) {
  666. defaultSetVideoSrc(element, stream);
  667. if (stream) {
  668. element.play();
  669. }
  670. }
  671. return element;
  672. });
  673. this.getStreamID = function(stream) {
  674. let id = stream.id;
  675. if (!id) {
  676. let tracks = stream.getVideoTracks();
  677. if (!tracks || tracks.length === 0) {
  678. tracks = stream.getAudioTracks();
  679. }
  680. id = tracks[0].id;
  681. }
  682. return SDPUtil.filterSpecialChars(id);
  683. };
  684. this.getTrackID = function(track) {
  685. return track.id;
  686. };
  687. /* eslint-disable no-global-assign, no-native-reassign */
  688. RTCSessionDescription = mozRTCSessionDescription;
  689. RTCIceCandidate = mozRTCIceCandidate;
  690. /* eslint-enable no-global-assign, no-native-reassign */
  691. } else if (RTCBrowserType.isChrome()
  692. || RTCBrowserType.isOpera()
  693. || RTCBrowserType.isNWJS()
  694. || RTCBrowserType.isElectron()
  695. || RTCBrowserType.isReactNative()) {
  696. this.RTCPeerConnectionType = webkitRTCPeerConnection;
  697. const getUserMedia
  698. = navigator.webkitGetUserMedia.bind(navigator);
  699. this.getUserMedia = wrapGetUserMedia(getUserMedia);
  700. this.enumerateDevices = rawEnumerateDevicesWithCallback;
  701. this.attachMediaStream
  702. = wrapAttachMediaStream((element, stream) => {
  703. defaultSetVideoSrc(element, stream);
  704. return element;
  705. });
  706. this.getStreamID = function(stream) {
  707. // A. MediaStreams from FF endpoints have the characters '{'
  708. // and '}' that make jQuery choke.
  709. // B. The react-native-webrtc implementation that we use on
  710. // React Native at the time of this writing returns a number
  711. // for the id of MediaStream. Let's just say that a number
  712. // contains no special characters.
  713. const id = stream.id;
  714. // XXX The return statement is affected by automatic
  715. // semicolon insertion (ASI). No line terminator is allowed
  716. // between the return keyword and the expression.
  717. return (
  718. typeof id === 'number'
  719. ? id
  720. : SDPUtil.filterSpecialChars(id));
  721. };
  722. this.getTrackID = function(track) {
  723. return track.id;
  724. };
  725. this.pcConstraints = { optional: [] };
  726. if (options.useIPv6) {
  727. // https://code.google.com/p/webrtc/issues/detail?id=2828
  728. this.pcConstraints.optional.push({ googIPv6: true });
  729. }
  730. if (!webkitMediaStream.prototype.getVideoTracks) {
  731. webkitMediaStream.prototype.getVideoTracks = function() {
  732. return this.videoTracks;
  733. };
  734. }
  735. if (!webkitMediaStream.prototype.getAudioTracks) {
  736. webkitMediaStream.prototype.getAudioTracks = function() {
  737. return this.audioTracks;
  738. };
  739. }
  740. this.p2pPcConstraints
  741. = JSON.parse(JSON.stringify(this.pcConstraints));
  742. // Allows sending of video to be suspended if the bandwidth
  743. // estimation is too low.
  744. if (!options.disableSuspendVideo) {
  745. this.pcConstraints.optional.push(
  746. { googSuspendBelowMinBitrate: true });
  747. }
  748. // There's no reason not to use this for p2p
  749. this.p2pPcConstraints.optional.push({
  750. googSuspendBelowMinBitrate: true
  751. });
  752. } else if (RTCBrowserType.isEdge()) {
  753. this.RTCPeerConnectionType = ortcRTCPeerConnection;
  754. this.getUserMedia
  755. = wrapGetUserMedia(
  756. navigator.mediaDevices.getUserMedia.bind(
  757. navigator.mediaDevices),
  758. true);
  759. this.enumerateDevices = rawEnumerateDevicesWithCallback;
  760. this.attachMediaStream
  761. = wrapAttachMediaStream((element, stream) => {
  762. defaultSetVideoSrc(element, stream);
  763. return element;
  764. });
  765. // ORTC does not generate remote MediaStreams so those are
  766. // manually created by the ORTC shim. This means that their
  767. // id (internally generated) does not match the stream id
  768. // signaled into the remote SDP. Therefore, the shim adds a
  769. // custom jitsiRemoteId property with the original stream id.
  770. this.getStreamID = function(stream) {
  771. const id = stream.jitsiRemoteId || stream.id;
  772. return SDPUtil.filterSpecialChars(id);
  773. };
  774. // Remote MediaStreamTracks generated by ORTC (within a
  775. // RTCRtpReceiver) have an internally/random id which does not
  776. // match the track id signaled in the remote SDP. The shim adds
  777. // a custom jitsi-id property with the original track id.
  778. this.getTrackID = function(track) {
  779. return track.jitsiRemoteId || track.id;
  780. };
  781. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  782. // Detect IE/Safari
  783. const webRTCReadyCb = () => {
  784. this.RTCPeerConnectionType = RTCPeerConnection;
  785. this.getUserMedia = window.getUserMedia;
  786. this.enumerateDevices
  787. = enumerateDevicesThroughMediaStreamTrack;
  788. this.attachMediaStream
  789. = wrapAttachMediaStream((element, stream) => {
  790. if (stream) {
  791. if (stream.id === 'dummyAudio'
  792. || stream.id === 'dummyVideo') {
  793. return;
  794. }
  795. // The container must be visible in order to
  796. // play or attach the stream when Temasys plugin
  797. // is in use
  798. const containerSel = $(element);
  799. if (RTCBrowserType.isTemasysPluginUsed()
  800. && !containerSel.is(':visible')) {
  801. containerSel.show();
  802. }
  803. const video
  804. = stream.getVideoTracks().length > 0;
  805. if (video && !$(element).is(':visible')) {
  806. throw new Error(
  807. 'video element must be visible to'
  808. + ' attach video stream');
  809. }
  810. }
  811. return attachMediaStream(element, stream);
  812. });
  813. this.getStreamID
  814. = stream => SDPUtil.filterSpecialChars(stream.label);
  815. this.getTrackID
  816. = track => track.id;
  817. onReady(
  818. options,
  819. this.getUserMediaWithConstraints.bind(this));
  820. };
  821. const webRTCReadyPromise
  822. = new Promise(r => AdapterJS.webRTCReady(r));
  823. // Resolve or reject depending on whether the Temasys plugin is
  824. // installed.
  825. AdapterJS.WebRTCPlugin.isPluginInstalled(
  826. AdapterJS.WebRTCPlugin.pluginInfo.prefix,
  827. AdapterJS.WebRTCPlugin.pluginInfo.plugName,
  828. AdapterJS.WebRTCPlugin.pluginInfo.type,
  829. /* installed */ () => {
  830. webRTCReadyPromise.then(() => {
  831. webRTCReadyCb();
  832. resolve();
  833. });
  834. },
  835. /* not installed */ () => {
  836. const error
  837. = new Error('Temasys plugin is not installed');
  838. error.name = 'WEBRTC_NOT_READY';
  839. error.webRTCReadyPromise = webRTCReadyPromise;
  840. reject(error);
  841. });
  842. } else {
  843. rejectWithWebRTCNotSupported(
  844. 'Browser does not appear to be WebRTC-capable',
  845. reject);
  846. return;
  847. }
  848. this.p2pPcConstraints = this.p2pPcConstraints || this.pcConstraints;
  849. // Call onReady() if Temasys plugin is not used
  850. if (!RTCBrowserType.isTemasysPluginUsed()) {
  851. onReady(options, this.getUserMediaWithConstraints.bind(this));
  852. resolve();
  853. }
  854. });
  855. }
  856. /* eslint-disable max-params */
  857. /**
  858. * @param {string[]} um required user media types
  859. * @param {function} successCallback
  860. * @param {Function} failureCallback
  861. * @param {Object} [options] optional parameters
  862. * @param {string} options.resolution
  863. * @param {number} options.bandwidth
  864. * @param {number} options.fps
  865. * @param {string} options.desktopStream
  866. * @param {string} options.cameraDeviceId
  867. * @param {string} options.micDeviceId
  868. **/
  869. getUserMediaWithConstraints(
  870. um,
  871. successCallback,
  872. failureCallback,
  873. options = {}) {
  874. const constraints = getConstraints(um, options);
  875. logger.info('Get media constraints', constraints);
  876. try {
  877. this.getUserMedia(
  878. constraints,
  879. stream => {
  880. logger.log('onUserMediaSuccess');
  881. setAvailableDevices(um, stream);
  882. successCallback(stream);
  883. },
  884. error => {
  885. setAvailableDevices(um, undefined);
  886. logger.warn('Failed to get access to local media. Error ',
  887. error, constraints);
  888. if (failureCallback) {
  889. failureCallback(
  890. new JitsiTrackError(error, constraints, um));
  891. }
  892. });
  893. } catch (e) {
  894. logger.error('GUM failed: ', e);
  895. if (failureCallback) {
  896. failureCallback(new JitsiTrackError(e, constraints, um));
  897. }
  898. }
  899. }
  900. /* eslint-enable max-params */
  901. /**
  902. * Creates the local MediaStreams.
  903. * @param {Object} [options] optional parameters
  904. * @param {Array} options.devices the devices that will be requested
  905. * @param {string} options.resolution resolution constraints
  906. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with
  907. * the following structure {stream: the Media Stream, type: "audio" or
  908. * "video", videoType: "camera" or "desktop"} will be returned trough the
  909. * Promise, otherwise JitsiTrack objects will be returned.
  910. * @param {string} options.cameraDeviceId
  911. * @param {string} options.micDeviceId
  912. * @returns {*} Promise object that will receive the new JitsiTracks
  913. */
  914. obtainAudioAndVideoPermissions(options = {}) {
  915. const self = this;
  916. const dsOptions = {
  917. ...options.desktopSharingExtensionExternalInstallation,
  918. desktopSharingSources: options.desktopSharingSources
  919. };
  920. return new Promise((resolve, reject) => {
  921. const successCallback = function(stream) {
  922. resolve(handleLocalStream(stream, options.resolution));
  923. };
  924. options.devices = options.devices || [ 'audio', 'video' ];
  925. if (!screenObtainer.isSupported()
  926. && options.devices.indexOf('desktop') !== -1) {
  927. reject(new Error('Desktop sharing is not supported!'));
  928. }
  929. if (RTCBrowserType.isFirefox()
  930. // XXX The react-native-webrtc implementation that we
  931. // utilize on React Native at the time of this writing does
  932. // not support the MediaStream constructors defined by
  933. // https://www.w3.org/TR/mediacapture-streams/#constructors
  934. // and instead has a single constructor which expects (an
  935. // NSNumber as) a MediaStream ID.
  936. || RTCBrowserType.isReactNative()
  937. || RTCBrowserType.isTemasysPluginUsed()) {
  938. const GUM = function(device, s, e) {
  939. this.getUserMediaWithConstraints(device, s, e, options);
  940. };
  941. const deviceGUM = {
  942. 'audio': GUM.bind(self, [ 'audio' ]),
  943. 'video': GUM.bind(self, [ 'video' ])
  944. };
  945. if (screenObtainer.isSupported()) {
  946. deviceGUM.desktop = screenObtainer.obtainStream.bind(
  947. screenObtainer,
  948. dsOptions);
  949. }
  950. // With FF/IE we can't split the stream into audio and video
  951. // because FF doesn't support media stream constructors. So, we
  952. // need to get the audio stream separately from the video stream
  953. // using two distinct GUM calls. Not very user friendly :-( but
  954. // we don't have many other options neither.
  955. //
  956. // Note that we pack those 2 streams in a single object and pass
  957. // it to the successCallback method.
  958. obtainDevices({
  959. devices: options.devices,
  960. streams: [],
  961. successCallback,
  962. errorCallback: reject,
  963. deviceGUM
  964. });
  965. } else {
  966. const hasDesktop = options.devices.indexOf('desktop') > -1;
  967. if (hasDesktop) {
  968. options.devices.splice(
  969. options.devices.indexOf('desktop'),
  970. 1);
  971. }
  972. options.resolution = options.resolution || '360';
  973. if (options.devices.length) {
  974. this.getUserMediaWithConstraints(
  975. options.devices,
  976. stream => {
  977. const audioDeviceRequested
  978. = options.devices.indexOf('audio') !== -1;
  979. const videoDeviceRequested
  980. = options.devices.indexOf('video') !== -1;
  981. const audioTracksReceived
  982. = stream.getAudioTracks().length > 0;
  983. const videoTracksReceived
  984. = stream.getVideoTracks().length > 0;
  985. if ((audioDeviceRequested && !audioTracksReceived)
  986. || (videoDeviceRequested
  987. && !videoTracksReceived)) {
  988. self.stopMediaStream(stream);
  989. // We are getting here in case if we requested
  990. // 'audio' or 'video' devices or both, but
  991. // didn't get corresponding MediaStreamTrack in
  992. // response stream. We don't know the reason why
  993. // this happened, so reject with general error.
  994. // eslint-disable-next-line no-shadow
  995. const devices = [];
  996. if (audioDeviceRequested
  997. && !audioTracksReceived) {
  998. devices.push('audio');
  999. }
  1000. if (videoDeviceRequested
  1001. && !videoTracksReceived) {
  1002. devices.push('video');
  1003. }
  1004. // we are missing one of the media we requested
  1005. // in order to get the actual error that caused
  1006. // this missing media we will call one more time
  1007. // getUserMedia so we can obtain the actual
  1008. // error (Example usecases are requesting
  1009. // audio and video and video device is missing
  1010. // or device is denied to be used and chrome is
  1011. // set to not ask for permissions)
  1012. self.getUserMediaWithConstraints(
  1013. devices,
  1014. () => {
  1015. // we already failed to obtain this
  1016. // media, so we are not supposed in any
  1017. // way to receive success for this call
  1018. // any way we will throw an error to be
  1019. // sure the promise will finish
  1020. reject(new JitsiTrackError(
  1021. { name: 'UnknownError' },
  1022. getConstraints(
  1023. options.devices,
  1024. options),
  1025. devices)
  1026. );
  1027. },
  1028. error => {
  1029. // rejects with real error for not
  1030. // obtaining the media
  1031. reject(error);
  1032. }, options);
  1033. return;
  1034. }
  1035. if (hasDesktop) {
  1036. screenObtainer.obtainStream(
  1037. dsOptions,
  1038. desktop => {
  1039. successCallback({ audioVideo: stream,
  1040. desktop });
  1041. }, error => {
  1042. self.stopMediaStream(stream);
  1043. reject(error);
  1044. });
  1045. } else {
  1046. successCallback({ audioVideo: stream });
  1047. }
  1048. },
  1049. error => reject(error),
  1050. options);
  1051. } else if (hasDesktop) {
  1052. screenObtainer.obtainStream(
  1053. dsOptions,
  1054. desktop => successCallback({ desktop }),
  1055. error => reject(error));
  1056. }
  1057. }
  1058. });
  1059. }
  1060. /**
  1061. *
  1062. */
  1063. getDeviceAvailability() {
  1064. return devices;
  1065. }
  1066. /**
  1067. *
  1068. */
  1069. isRTCReady() {
  1070. return rtcReady;
  1071. }
  1072. /**
  1073. *
  1074. */
  1075. _isDeviceListAvailable() {
  1076. if (!rtcReady) {
  1077. throw new Error('WebRTC not ready yet');
  1078. }
  1079. return Boolean(
  1080. (navigator.mediaDevices
  1081. && navigator.mediaDevices.enumerateDevices)
  1082. || (typeof MediaStreamTrack !== 'undefined'
  1083. && MediaStreamTrack.getSources));
  1084. }
  1085. /**
  1086. * Returns a promise which can be used to make sure that the WebRTC stack
  1087. * has been initialized.
  1088. *
  1089. * @returns {Promise} which is resolved only if the WebRTC stack is ready.
  1090. * Note that currently we do not detect stack initialization failure and
  1091. * the promise is never rejected(unless unexpected error occurs).
  1092. */
  1093. onRTCReady() {
  1094. if (rtcReady) {
  1095. return Promise.resolve();
  1096. }
  1097. return new Promise(resolve => {
  1098. const listener = () => {
  1099. eventEmitter.removeListener(RTCEvents.RTC_READY, listener);
  1100. resolve();
  1101. };
  1102. eventEmitter.addListener(RTCEvents.RTC_READY, listener);
  1103. // We have no failed event, so... it either resolves or nothing
  1104. // happens
  1105. });
  1106. }
  1107. /**
  1108. * Checks if its possible to enumerate available cameras/microphones.
  1109. *
  1110. * @returns {Promise<boolean>} a Promise which will be resolved only once
  1111. * the WebRTC stack is ready, either with true if the device listing is
  1112. * available available or with false otherwise.
  1113. */
  1114. isDeviceListAvailable() {
  1115. return this.onRTCReady().then(this._isDeviceListAvailable.bind(this));
  1116. }
  1117. /**
  1118. * Returns true if changing the input (camera / microphone) or output
  1119. * (audio) device is supported and false if not.
  1120. * @params {string} [deviceType] - type of device to change. Default is
  1121. * undefined or 'input', 'output' - for audio output device change.
  1122. * @returns {boolean} true if available, false otherwise.
  1123. */
  1124. isDeviceChangeAvailable(deviceType) {
  1125. return deviceType === 'output' || deviceType === 'audiooutput'
  1126. ? isAudioOutputDeviceChangeAvailable
  1127. : RTCBrowserType.isChrome()
  1128. || RTCBrowserType.isFirefox()
  1129. || RTCBrowserType.isOpera()
  1130. || RTCBrowserType.isTemasysPluginUsed()
  1131. || RTCBrowserType.isNWJS()
  1132. || RTCBrowserType.isElectron()
  1133. || RTCBrowserType.isEdge();
  1134. }
  1135. /**
  1136. * A method to handle stopping of the stream.
  1137. * One point to handle the differences in various implementations.
  1138. * @param mediaStream MediaStream object to stop.
  1139. */
  1140. stopMediaStream(mediaStream) {
  1141. mediaStream.getTracks().forEach(track => {
  1142. // stop() not supported with IE
  1143. if (!RTCBrowserType.isTemasysPluginUsed() && track.stop) {
  1144. track.stop();
  1145. }
  1146. });
  1147. // leave stop for implementation still using it
  1148. if (mediaStream.stop) {
  1149. mediaStream.stop();
  1150. }
  1151. // The MediaStream implementation of the react-native-webrtc project has
  1152. // an explicit release method that is to be invoked in order to release
  1153. // used resources such as memory.
  1154. if (mediaStream.release) {
  1155. mediaStream.release();
  1156. }
  1157. // if we have done createObjectURL, lets clean it
  1158. const url = mediaStream.jitsiObjectURL;
  1159. if (url) {
  1160. delete mediaStream.jitsiObjectURL;
  1161. (URL || webkitURL).revokeObjectURL(url);
  1162. }
  1163. }
  1164. /**
  1165. * Returns whether the desktop sharing is enabled or not.
  1166. * @returns {boolean}
  1167. */
  1168. isDesktopSharingEnabled() {
  1169. return screenObtainer.isSupported();
  1170. }
  1171. /**
  1172. * Sets current audio output device.
  1173. * @param {string} deviceId - id of 'audiooutput' device from
  1174. * navigator.mediaDevices.enumerateDevices(), 'default' for default
  1175. * device
  1176. * @returns {Promise} - resolves when audio output is changed, is rejected
  1177. * otherwise
  1178. */
  1179. setAudioOutputDevice(deviceId) {
  1180. if (!this.isDeviceChangeAvailable('output')) {
  1181. Promise.reject(
  1182. new Error('Audio output device change is not supported'));
  1183. }
  1184. return featureDetectionAudioEl.setSinkId(deviceId)
  1185. .then(() => {
  1186. audioOutputDeviceId = deviceId;
  1187. audioOutputChanged = true;
  1188. logger.log(`Audio output device set to ${deviceId}`);
  1189. eventEmitter.emit(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  1190. deviceId);
  1191. });
  1192. }
  1193. /**
  1194. * Returns currently used audio output device id, '' stands for default
  1195. * device
  1196. * @returns {string}
  1197. */
  1198. getAudioOutputDevice() {
  1199. return audioOutputDeviceId;
  1200. }
  1201. /**
  1202. * Returns list of available media devices if its obtained, otherwise an
  1203. * empty array is returned/
  1204. * @returns {Array} list of available media devices.
  1205. */
  1206. getCurrentlyAvailableMediaDevices() {
  1207. return currentlyAvailableMediaDevices;
  1208. }
  1209. /**
  1210. * Returns event data for device to be reported to stats.
  1211. * @returns {MediaDeviceInfo} device.
  1212. */
  1213. getEventDataForActiveDevice(device) {
  1214. const deviceList = [];
  1215. const deviceData = {
  1216. 'deviceId': device.deviceId,
  1217. 'kind': device.kind,
  1218. 'label': device.label,
  1219. 'groupId': device.groupId
  1220. };
  1221. deviceList.push(deviceData);
  1222. return { deviceList };
  1223. }
  1224. }
  1225. /**
  1226. * Rejects a Promise because WebRTC is not supported.
  1227. *
  1228. * @param {string} errorMessage - The human-readable message of the Error which
  1229. * is the reason for the rejection.
  1230. * @param {Function} reject - The reject function of the Promise.
  1231. * @returns {void}
  1232. */
  1233. function rejectWithWebRTCNotSupported(errorMessage, reject) {
  1234. const error = new Error(errorMessage);
  1235. // WebRTC is not supported either natively or via a known plugin such as
  1236. // Temasys.
  1237. // XXX The Error class already has a property name which is commonly used to
  1238. // detail the represented error in a non-human-readable way (in contrast to
  1239. // the human-readable property message). I explicitly did not want to
  1240. // introduce a new specific property.
  1241. // FIXME None of the existing JitsiXXXErrors seemed to be appropriate
  1242. // recipients of the constant WEBRTC_NOT_SUPPORTED so I explicitly chose to
  1243. // leave it as a magic string at the time of this writing.
  1244. error.name = 'WEBRTC_NOT_SUPPORTED';
  1245. logger.error(errorMessage);
  1246. reject(error);
  1247. }
  1248. const rtcUtils = new RTCUtils();
  1249. /**
  1250. *
  1251. * @param options
  1252. */
  1253. function obtainDevices(options) {
  1254. if (!options.devices || options.devices.length === 0) {
  1255. return options.successCallback(options.streams || {});
  1256. }
  1257. const device = options.devices.splice(0, 1);
  1258. options.deviceGUM[device](
  1259. stream => {
  1260. options.streams = options.streams || {};
  1261. options.streams[device] = stream;
  1262. obtainDevices(options);
  1263. },
  1264. error => {
  1265. Object.keys(options.streams).forEach(
  1266. d => rtcUtils.stopMediaStream(options.streams[d]));
  1267. logger.error(
  1268. `failed to obtain ${device} stream - stop`, error);
  1269. options.errorCallback(error);
  1270. });
  1271. }
  1272. /**
  1273. * In case of IE we continue from 'onReady' callback passed to RTCUtils
  1274. * constructor. It will be invoked by Temasys plugin once it is initialized.
  1275. *
  1276. * @param options
  1277. * @param GUM
  1278. */
  1279. function onReady(options, GUM) {
  1280. rtcReady = true;
  1281. eventEmitter.emit(RTCEvents.RTC_READY, true);
  1282. screenObtainer.init(options, GUM);
  1283. if (rtcUtils.isDeviceListAvailable() && rawEnumerateDevicesWithCallback) {
  1284. rawEnumerateDevicesWithCallback(ds => {
  1285. currentlyAvailableMediaDevices = ds.splice(0);
  1286. eventEmitter.emit(RTCEvents.DEVICE_LIST_AVAILABLE,
  1287. currentlyAvailableMediaDevices);
  1288. if (isDeviceChangeEventSupported) {
  1289. navigator.mediaDevices.addEventListener(
  1290. 'devicechange',
  1291. () => rtcUtils.enumerateDevices(onMediaDevicesListChanged));
  1292. } else {
  1293. pollForAvailableMediaDevices();
  1294. }
  1295. });
  1296. }
  1297. }
  1298. /**
  1299. * Wraps original attachMediaStream function to set current audio output device
  1300. * if this is supported.
  1301. * @param {Function} origAttachMediaStream
  1302. * @returns {Function}
  1303. */
  1304. function wrapAttachMediaStream(origAttachMediaStream) {
  1305. return function(element, stream) {
  1306. // eslint-disable-next-line prefer-rest-params
  1307. const res = origAttachMediaStream.apply(rtcUtils, arguments);
  1308. if (stream
  1309. && rtcUtils.isDeviceChangeAvailable('output')
  1310. && stream.getAudioTracks
  1311. && stream.getAudioTracks().length
  1312. // we skip setting audio output if there was no explicit change
  1313. && audioOutputChanged) {
  1314. element.setSinkId(rtcUtils.getAudioOutputDevice())
  1315. .catch(function(ex) {
  1316. const err
  1317. = new JitsiTrackError(ex, null, [ 'audiooutput' ]);
  1318. GlobalOnErrorHandler.callUnhandledRejectionHandler({
  1319. promise: this, // eslint-disable-line no-invalid-this
  1320. reason: err
  1321. });
  1322. logger.warn('Failed to set audio output device for the '
  1323. + 'element. Default audio output device will be used '
  1324. + 'instead',
  1325. element, err);
  1326. });
  1327. }
  1328. return res;
  1329. };
  1330. }
  1331. export default rtcUtils;