modified lib-jitsi-meet dev repo
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiMeetJS.js 21KB

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