modified lib-jitsi-meet dev repo
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.

JitsiMeetJS.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* global __filename */
  2. import { createGetUserMediaEvent } from './service/statistics/AnalyticsEvents';
  3. import AuthUtil from './modules/util/AuthUtil';
  4. import * as ConnectionQualityEvents
  5. from './service/connectivity/ConnectionQualityEvents';
  6. import GlobalOnErrorHandler from './modules/util/GlobalOnErrorHandler';
  7. import * as JitsiConferenceErrors from './JitsiConferenceErrors';
  8. import * as JitsiConferenceEvents from './JitsiConferenceEvents';
  9. import JitsiConnection from './JitsiConnection';
  10. import * as JitsiConnectionErrors from './JitsiConnectionErrors';
  11. import * as JitsiConnectionEvents from './JitsiConnectionEvents';
  12. import JitsiMediaDevices from './JitsiMediaDevices';
  13. import * as JitsiMediaDevicesEvents from './JitsiMediaDevicesEvents';
  14. import JitsiRecorderErrors from './JitsiRecorderErrors';
  15. import JitsiTrackError from './JitsiTrackError';
  16. import * as JitsiTrackErrors from './JitsiTrackErrors';
  17. import * as JitsiTrackEvents from './JitsiTrackEvents';
  18. import * as JitsiTranscriptionStatus from './JitsiTranscriptionStatus';
  19. import LocalStatsCollector from './modules/statistics/LocalStatsCollector';
  20. import Recording from './modules/xmpp/recording';
  21. import Logger from 'jitsi-meet-logger';
  22. import * as MediaType from './service/RTC/MediaType';
  23. import Resolutions from './service/RTC/Resolutions';
  24. import { ParticipantConnectionStatus }
  25. from './modules/connectivity/ParticipantConnectionStatus';
  26. import RTC from './modules/RTC/RTC';
  27. import browser from './modules/browser';
  28. import RTCUIHelper from './modules/RTC/RTCUIHelper';
  29. import ScriptUtil from './modules/util/ScriptUtil';
  30. import Statistics from './modules/statistics/statistics';
  31. import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
  32. const logger = Logger.getLogger(__filename);
  33. // The amount of time to wait until firing
  34. // JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN event
  35. const USER_MEDIA_PERMISSION_PROMPT_TIMEOUT = 1000;
  36. /**
  37. *
  38. * @param resolution
  39. */
  40. function getLowerResolution(resolution) {
  41. if (!Resolutions[resolution]) {
  42. return null;
  43. }
  44. const order = Resolutions[resolution].order;
  45. let res = null;
  46. let resName = null;
  47. Object.keys(Resolutions).forEach(r => {
  48. const value = Resolutions[r];
  49. if (!res || (res.order < value.order && value.order < order)) {
  50. resName = r;
  51. res = value;
  52. }
  53. });
  54. return resName;
  55. }
  56. /**
  57. * Extracts from an 'options' objects with a specific format
  58. * (TODO what IS the format?) the attributes which are to be logged in analytics
  59. * events.
  60. *
  61. * @param options gum options (???)
  62. * @returns {*} the attributes to attach to analytics events.
  63. */
  64. function getAnalyticsAttributesFromOptions(options) {
  65. const attributes = {
  66. 'audio_requested':
  67. options.devices.includes('audio'),
  68. 'video_requested':
  69. options.devices.includes('video'),
  70. 'screen_sharing_requested':
  71. options.devices.includes('desktop')
  72. };
  73. if (attributes.video_requested) {
  74. attributes.resolution = options.resolution;
  75. }
  76. return attributes;
  77. }
  78. /**
  79. * The public API of the Jitsi Meet library (a.k.a. JitsiMeetJS).
  80. */
  81. export default {
  82. version: '{#COMMIT_HASH#}',
  83. JitsiConnection,
  84. constants: {
  85. participantConnectionStatus: ParticipantConnectionStatus,
  86. recordingStatus: Recording.status,
  87. recordingTypes: Recording.types,
  88. sipVideoGW: VideoSIPGWConstants,
  89. transcriptionStatus: JitsiTranscriptionStatus
  90. },
  91. events: {
  92. conference: JitsiConferenceEvents,
  93. connection: JitsiConnectionEvents,
  94. track: JitsiTrackEvents,
  95. mediaDevices: JitsiMediaDevicesEvents,
  96. connectionQuality: ConnectionQualityEvents
  97. },
  98. errors: {
  99. conference: JitsiConferenceErrors,
  100. connection: JitsiConnectionErrors,
  101. recorder: JitsiRecorderErrors,
  102. track: JitsiTrackErrors
  103. },
  104. errorTypes: {
  105. JitsiTrackError
  106. },
  107. logLevels: Logger.levels,
  108. mediaDevices: JitsiMediaDevices,
  109. analytics: Statistics.analytics,
  110. init(options) {
  111. Statistics.init(options);
  112. // Initialize global window.connectionTimes
  113. // FIXME do not use 'window'
  114. if (!window.connectionTimes) {
  115. window.connectionTimes = {};
  116. }
  117. if (options.enableAnalyticsLogging !== true) {
  118. this.analytics.dispose();
  119. }
  120. if (options.enableWindowOnErrorHandler) {
  121. GlobalOnErrorHandler.addHandler(
  122. this.getGlobalOnErrorHandler.bind(this));
  123. }
  124. // Log deployment-specific information, if available.
  125. // Defined outside the application by individual deployments
  126. const aprops = options.deploymentInfo;
  127. if (aprops && Object.keys(aprops).length > 0) {
  128. const logObject = {};
  129. for (const attr in aprops) {
  130. if (aprops.hasOwnProperty(attr)) {
  131. logObject[attr] = aprops[attr];
  132. }
  133. }
  134. logObject.id = 'deployment_info';
  135. Statistics.sendLog(JSON.stringify(logObject));
  136. }
  137. if (this.version) {
  138. const logObject = {
  139. id: 'component_version',
  140. component: 'lib-jitsi-meet',
  141. version: this.version
  142. };
  143. Statistics.sendLog(JSON.stringify(logObject));
  144. }
  145. return RTC.init(options || {});
  146. },
  147. /**
  148. * Returns whether the desktop sharing is enabled or not.
  149. * @returns {boolean}
  150. */
  151. isDesktopSharingEnabled() {
  152. return RTC.isDesktopSharingEnabled();
  153. },
  154. setLogLevel(level) {
  155. Logger.setLogLevel(level);
  156. },
  157. /**
  158. * Sets the log level to the <tt>Logger</tt> instance with given id.
  159. * @param {Logger.levels} level the logging level to be set
  160. * @param {string} id the logger id to which new logging level will be set.
  161. * Usually it's the name of the JavaScript source file including the path
  162. * ex. "modules/xmpp/ChatRoom.js"
  163. */
  164. setLogLevelById(level, id) {
  165. Logger.setLogLevelById(level, id);
  166. },
  167. /**
  168. * Registers new global logger transport to the library logging framework.
  169. * @param globalTransport
  170. * @see Logger.addGlobalTransport
  171. */
  172. addGlobalLogTransport(globalTransport) {
  173. Logger.addGlobalTransport(globalTransport);
  174. },
  175. /**
  176. * Removes global logging transport from the library logging framework.
  177. * @param globalTransport
  178. * @see Logger.removeGlobalTransport
  179. */
  180. removeGlobalLogTransport(globalTransport) {
  181. Logger.removeGlobalTransport(globalTransport);
  182. },
  183. /**
  184. * Creates the media tracks and returns them trough the callback.
  185. * @param options Object with properties / settings specifying the tracks
  186. * which should be created. should be created or some additional
  187. * configurations about resolution for example.
  188. * @param {Array} options.devices the devices that will be requested
  189. * @param {string} options.resolution resolution constraints
  190. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with
  191. * the following structure {stream: the Media Stream, type: "audio" or
  192. * "video", videoType: "camera" or "desktop"} will be returned trough the
  193. * Promise, otherwise JitsiTrack objects will be returned.
  194. * @param {string} options.cameraDeviceId
  195. * @param {string} options.micDeviceId
  196. * @param {object} options.desktopSharingExtensionExternalInstallation -
  197. * enables external installation process for desktop sharing extension if
  198. * the inline installation is not posible. The following properties should
  199. * be provided:
  200. * @param {intiger} interval - the interval (in ms) for
  201. * checking whether the desktop sharing extension is installed or not
  202. * @param {Function} checkAgain - returns boolean. While checkAgain()==true
  203. * createLocalTracks will wait and check on every "interval" ms for the
  204. * extension. If the desktop extension is not install and checkAgain()==true
  205. * createLocalTracks will finish with rejected Promise.
  206. * @param {Function} listener - The listener will be called to notify the
  207. * user of lib-jitsi-meet that createLocalTracks is starting external
  208. * extension installation process.
  209. * NOTE: If the inline installation process is not possible and external
  210. * installation is enabled the listener property will be called to notify
  211. * the start of external installation process. After that createLocalTracks
  212. * will start to check for the extension on every interval ms until the
  213. * plugin is installed or until checkAgain return false. If the extension
  214. * is found createLocalTracks will try to get the desktop sharing track and
  215. * will finish the execution. If checkAgain returns false, createLocalTracks
  216. * will finish the execution with rejected Promise.
  217. *
  218. * @param {boolean} (firePermissionPromptIsShownEvent) - if event
  219. * JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN should be fired
  220. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>}
  221. * A promise that returns an array of created JitsiTracks if resolved,
  222. * or a JitsiConferenceError if rejected.
  223. */
  224. createLocalTracks(options = {}, firePermissionPromptIsShownEvent) {
  225. let promiseFulfilled = false;
  226. if (firePermissionPromptIsShownEvent === true) {
  227. window.setTimeout(() => {
  228. if (!promiseFulfilled) {
  229. JitsiMediaDevices.emitEvent(
  230. JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN,
  231. browser.getName());
  232. }
  233. }, USER_MEDIA_PERMISSION_PROMPT_TIMEOUT);
  234. }
  235. if (!window.connectionTimes) {
  236. window.connectionTimes = {};
  237. }
  238. window.connectionTimes['obtainPermissions.start']
  239. = window.performance.now();
  240. return RTC.obtainAudioAndVideoPermissions(options)
  241. .then(tracks => {
  242. promiseFulfilled = true;
  243. window.connectionTimes['obtainPermissions.end']
  244. = window.performance.now();
  245. Statistics.sendAnalytics(
  246. createGetUserMediaEvent(
  247. 'success',
  248. getAnalyticsAttributesFromOptions(options)));
  249. if (!RTC.options.disableAudioLevels) {
  250. for (let i = 0; i < tracks.length; i++) {
  251. const track = tracks[i];
  252. const mStream = track.getOriginalStream();
  253. if (track.getType() === MediaType.AUDIO) {
  254. Statistics.startLocalStats(mStream,
  255. track.setAudioLevel.bind(track));
  256. track.addEventListener(
  257. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  258. () => {
  259. Statistics.stopLocalStats(mStream);
  260. });
  261. }
  262. }
  263. }
  264. // set real device ids
  265. const currentlyAvailableMediaDevices
  266. = RTC.getCurrentlyAvailableMediaDevices();
  267. if (currentlyAvailableMediaDevices) {
  268. for (let i = 0; i < tracks.length; i++) {
  269. const track = tracks[i];
  270. track._setRealDeviceIdFromDeviceList(
  271. currentlyAvailableMediaDevices);
  272. }
  273. }
  274. return tracks;
  275. })
  276. .catch(error => {
  277. promiseFulfilled = true;
  278. if (error.name === JitsiTrackErrors.UNSUPPORTED_RESOLUTION
  279. && !browser.usesNewGumFlow()) {
  280. const oldResolution = options.resolution || '720';
  281. const newResolution = getLowerResolution(oldResolution);
  282. if (newResolution !== null) {
  283. options.resolution = newResolution;
  284. logger.debug(
  285. 'Retry createLocalTracks with resolution',
  286. newResolution);
  287. Statistics.sendAnalytics(createGetUserMediaEvent(
  288. 'warning',
  289. {
  290. 'old_resolution': oldResolution,
  291. 'new_resolution': newResolution,
  292. reason: 'unsupported resolution'
  293. }));
  294. return this.createLocalTracks(options);
  295. }
  296. }
  297. if (error.name
  298. === JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED) {
  299. // User cancelled action is not really an error, so only
  300. // log it as an event to avoid having conference classified
  301. // as partially failed
  302. const logObject = {
  303. id: 'chrome_extension_user_canceled',
  304. message: error.message
  305. };
  306. Statistics.sendLog(JSON.stringify(logObject));
  307. Statistics.sendAnalytics(
  308. createGetUserMediaEvent(
  309. 'warning',
  310. {
  311. reason: 'extension install user canceled'
  312. }));
  313. } else if (error.name === JitsiTrackErrors.NOT_FOUND) {
  314. // logs not found devices with just application log to cs
  315. const logObject = {
  316. id: 'usermedia_missing_device',
  317. status: error.gum.devices
  318. };
  319. Statistics.sendLog(JSON.stringify(logObject));
  320. const attributes
  321. = getAnalyticsAttributesFromOptions(options);
  322. attributes.reason = 'device not found';
  323. attributes.devices = error.gum.devices.join('.');
  324. Statistics.sendAnalytics(
  325. createGetUserMediaEvent('error', attributes));
  326. } else {
  327. // Report gUM failed to the stats
  328. Statistics.sendGetUserMediaFailed(error);
  329. const attributes
  330. = getAnalyticsAttributesFromOptions(options);
  331. attributes.reason = error.name;
  332. Statistics.sendAnalytics(
  333. createGetUserMediaEvent('error', attributes));
  334. }
  335. window.connectionTimes['obtainPermissions.end']
  336. = window.performance.now();
  337. return Promise.reject(error);
  338. });
  339. },
  340. /**
  341. * Checks if its possible to enumerate available cameras/microphones.
  342. * @returns {Promise<boolean>} a Promise which will be resolved only once
  343. * the WebRTC stack is ready, either with true if the device listing is
  344. * available available or with false otherwise.
  345. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
  346. */
  347. isDeviceListAvailable() {
  348. logger.warn('This method is deprecated, use '
  349. + 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
  350. return this.mediaDevices.isDeviceListAvailable();
  351. },
  352. /**
  353. * Returns true if changing the input (camera / microphone) or output
  354. * (audio) device is supported and false if not.
  355. * @params {string} [deviceType] - type of device to change. Default is
  356. * undefined or 'input', 'output' - for audio output device change.
  357. * @returns {boolean} true if available, false otherwise.
  358. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
  359. */
  360. isDeviceChangeAvailable(deviceType) {
  361. logger.warn('This method is deprecated, use '
  362. + 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
  363. return this.mediaDevices.isDeviceChangeAvailable(deviceType);
  364. },
  365. /**
  366. * Checks if the current environment supports having multiple audio
  367. * input devices in use simultaneously.
  368. *
  369. * @returns {boolean} True if multiple audio input devices can be used.
  370. */
  371. isMultipleAudioInputSupported() {
  372. return this.mediaDevices.isMultipleAudioInputSupported();
  373. },
  374. /**
  375. * Checks if local tracks can collect stats and collection is enabled.
  376. *
  377. * @param {boolean} True if stats are being collected for local tracks.
  378. */
  379. isCollectingLocalStats() {
  380. return Statistics.audioLevelsEnabled
  381. && LocalStatsCollector.isLocalStatsSupported();
  382. },
  383. /**
  384. * Executes callback with list of media devices connected.
  385. * @param {function} callback
  386. * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
  387. */
  388. enumerateDevices(callback) {
  389. logger.warn('This method is deprecated, use '
  390. + 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
  391. this.mediaDevices.enumerateDevices(callback);
  392. },
  393. /* eslint-disable max-params */
  394. /**
  395. * @returns function that can be used to be attached to window.onerror and
  396. * if options.enableWindowOnErrorHandler is enabled returns
  397. * the function used by the lib.
  398. * (function(message, source, lineno, colno, error)).
  399. */
  400. getGlobalOnErrorHandler(message, source, lineno, colno, error) {
  401. logger.error(
  402. `UnhandledError: ${message}`,
  403. `Script: ${source}`,
  404. `Line: ${lineno}`,
  405. `Column: ${colno}`,
  406. 'StackTrace: ', error);
  407. Statistics.reportGlobalError(error);
  408. },
  409. /* eslint-enable max-params */
  410. /**
  411. * Represents a hub/namespace for utility functionality which may be of
  412. * interest to lib-jitsi-meet clients.
  413. */
  414. util: {
  415. AuthUtil,
  416. RTCUIHelper,
  417. ScriptUtil,
  418. browser
  419. }
  420. };