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

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