123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- /* global __filename */
-
- import { createGetUserMediaEvent } from './service/statistics/AnalyticsEvents';
- import AuthUtil from './modules/util/AuthUtil';
- import * as ConnectionQualityEvents
- from './service/connectivity/ConnectionQualityEvents';
- import GlobalOnErrorHandler from './modules/util/GlobalOnErrorHandler';
- import * as JitsiConferenceErrors from './JitsiConferenceErrors';
- import * as JitsiConferenceEvents from './JitsiConferenceEvents';
- import JitsiConnection from './JitsiConnection';
- import * as JitsiConnectionErrors from './JitsiConnectionErrors';
- import * as JitsiConnectionEvents from './JitsiConnectionEvents';
- import JitsiMediaDevices from './JitsiMediaDevices';
- import * as JitsiMediaDevicesEvents from './JitsiMediaDevicesEvents';
- import JitsiTrackError from './JitsiTrackError';
- import * as JitsiTrackErrors from './JitsiTrackErrors';
- import * as JitsiTrackEvents from './JitsiTrackEvents';
- import * as JitsiTranscriptionStatus from './JitsiTranscriptionStatus';
- import LocalStatsCollector from './modules/statistics/LocalStatsCollector';
- import Logger from 'jitsi-meet-logger';
- import * as MediaType from './service/RTC/MediaType';
- import Resolutions from './service/RTC/Resolutions';
- import { ParticipantConnectionStatus }
- from './modules/connectivity/ParticipantConnectionStatus';
- import RTC from './modules/RTC/RTC';
- import browser from './modules/browser';
- import ScriptUtil from './modules/util/ScriptUtil';
- import recordingConstants from './modules/recording/recordingConstants';
- import Statistics from './modules/statistics/statistics';
- import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
-
- const logger = Logger.getLogger(__filename);
-
- /**
- * The amount of time to wait until firing
- * {@link JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN} event.
- */
- const USER_MEDIA_PERMISSION_PROMPT_TIMEOUT = 1000;
-
- /**
- * Gets the next lowest desirable resolution to try for a camera. If the given
- * resolution is already the lowest acceptable resolution, returns {@code null}.
- *
- * @param resolution the current resolution
- * @return the next lowest resolution from the given one, or {@code null} if it
- * is already the lowest acceptable resolution.
- */
- function getLowerResolution(resolution) {
- if (!Resolutions[resolution]) {
- return null;
- }
- const order = Resolutions[resolution].order;
- let res = null;
- let resName = null;
-
- Object.keys(Resolutions).forEach(r => {
- const value = Resolutions[r];
-
- if (!res || (res.order < value.order && value.order < order)) {
- resName = r;
- res = value;
- }
- });
-
- if (resName === resolution) {
- resName = null;
- }
-
- return resName;
- }
-
- /**
- * Extracts from an 'options' objects with a specific format (TODO what IS the
- * format?) the attributes which are to be logged in analytics events.
- *
- * @param options gum options (???)
- * @returns {*} the attributes to attach to analytics events.
- */
- function getAnalyticsAttributesFromOptions(options) {
- const attributes = {
- 'audio_requested':
- options.devices.includes('audio'),
- 'video_requested':
- options.devices.includes('video'),
- 'screen_sharing_requested':
- options.devices.includes('desktop')
- };
-
- if (attributes.video_requested) {
- attributes.resolution = options.resolution;
- }
-
- return attributes;
- }
-
- /**
- * Tries to deal with the following problem: {@code JitsiMeetJS} is not only
- * this module, it's also a global (i.e. attached to {@code window}) namespace
- * for all globals of the projects in the Jitsi Meet family. If lib-jitsi-meet
- * is loaded through an HTML {@code script} tag, {@code JitsiMeetJS} will
- * automatically be attached to {@code window} by webpack. Unfortunately,
- * webpack's source code does not check whether the global variable has already
- * been assigned and overwrites it. Which is OK for the module
- * {@code JitsiMeetJS} but is not OK for the namespace {@code JitsiMeetJS}
- * because it may already contain the values of other projects in the Jitsi Meet
- * family. The solution offered here works around webpack by merging all
- * existing values of the namespace {@code JitsiMeetJS} into the module
- * {@code JitsiMeetJS}.
- *
- * @param {Object} module - The module {@code JitsiMeetJS} (which will be
- * exported and may be attached to {@code window} by webpack later on).
- * @private
- * @returns {Object} - A {@code JitsiMeetJS} module which contains all existing
- * value of the namespace {@code JitsiMeetJS} (if any).
- */
- function _mergeNamespaceAndModule(module) {
- return (
- typeof window.JitsiMeetJS === 'object'
- ? Object.assign({}, window.JitsiMeetJS, module)
- : module);
- }
-
- /**
- * The public API of the Jitsi Meet library (a.k.a. {@code JitsiMeetJS}).
- */
- export default _mergeNamespaceAndModule({
-
- version: '{#COMMIT_HASH#}',
-
- JitsiConnection,
- constants: {
- participantConnectionStatus: ParticipantConnectionStatus,
- recording: recordingConstants,
- sipVideoGW: VideoSIPGWConstants,
- transcriptionStatus: JitsiTranscriptionStatus
- },
- events: {
- conference: JitsiConferenceEvents,
- connection: JitsiConnectionEvents,
- track: JitsiTrackEvents,
- mediaDevices: JitsiMediaDevicesEvents,
- connectionQuality: ConnectionQualityEvents
- },
- errors: {
- conference: JitsiConferenceErrors,
- connection: JitsiConnectionErrors,
- track: JitsiTrackErrors
- },
- errorTypes: {
- JitsiTrackError
- },
- logLevels: Logger.levels,
- mediaDevices: JitsiMediaDevices,
- analytics: Statistics.analytics,
- init(options) {
- Statistics.init(options);
-
- // Initialize global window.connectionTimes
- // FIXME do not use 'window'
- if (!window.connectionTimes) {
- window.connectionTimes = {};
- }
-
- if (options.enableAnalyticsLogging !== true) {
- logger.warn('Analytics disabled, disposing.');
- this.analytics.dispose();
- }
-
- if (options.enableWindowOnErrorHandler) {
- GlobalOnErrorHandler.addHandler(
- this.getGlobalOnErrorHandler.bind(this));
- }
-
- // Log deployment-specific information, if available. Defined outside
- // the application by individual deployments
- const aprops = options.deploymentInfo;
-
- if (aprops && Object.keys(aprops).length > 0) {
- const logObject = {};
-
- for (const attr in aprops) {
- if (aprops.hasOwnProperty(attr)) {
- logObject[attr] = aprops[attr];
- }
- }
-
- logObject.id = 'deployment_info';
- Statistics.sendLog(JSON.stringify(logObject));
- }
-
- if (this.version) {
- const logObject = {
- id: 'component_version',
- component: 'lib-jitsi-meet',
- version: this.version
- };
-
- Statistics.sendLog(JSON.stringify(logObject));
- }
-
- return RTC.init(options || {});
- },
-
- /**
- * Returns whether the desktop sharing is enabled or not.
- *
- * @returns {boolean}
- */
- isDesktopSharingEnabled() {
- return RTC.isDesktopSharingEnabled();
- },
- setLogLevel(level) {
- Logger.setLogLevel(level);
- },
-
- /**
- * Sets the log level to the <tt>Logger</tt> instance with given id.
- *
- * @param {Logger.levels} level the logging level to be set
- * @param {string} id the logger id to which new logging level will be set.
- * Usually it's the name of the JavaScript source file including the path
- * ex. "modules/xmpp/ChatRoom.js"
- */
- setLogLevelById(level, id) {
- Logger.setLogLevelById(level, id);
- },
-
- /**
- * Registers new global logger transport to the library logging framework.
- *
- * @param globalTransport
- * @see Logger.addGlobalTransport
- */
- addGlobalLogTransport(globalTransport) {
- Logger.addGlobalTransport(globalTransport);
- },
-
- /**
- * Removes global logging transport from the library logging framework.
- *
- * @param globalTransport
- * @see Logger.removeGlobalTransport
- */
- removeGlobalLogTransport(globalTransport) {
- Logger.removeGlobalTransport(globalTransport);
- },
-
- /**
- * Creates the media tracks and returns them trough the callback.
- *
- * @param options Object with properties / settings specifying the tracks
- * which should be created. should be created or some additional
- * configurations about resolution for example.
- * @param {Array} options.devices the devices that will be requested
- * @param {string} options.resolution resolution constraints
- * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with
- * the following structure {stream: the Media Stream, type: "audio" or
- * "video", videoType: "camera" or "desktop"} will be returned trough the
- * Promise, otherwise JitsiTrack objects will be returned.
- * @param {string} options.cameraDeviceId
- * @param {string} options.micDeviceId
- * @param {object} options.desktopSharingExtensionExternalInstallation -
- * enables external installation process for desktop sharing extension if
- * the inline installation is not posible. The following properties should
- * be provided:
- * @param {intiger} interval - the interval (in ms) for
- * checking whether the desktop sharing extension is installed or not
- * @param {Function} checkAgain - returns boolean. While checkAgain()==true
- * createLocalTracks will wait and check on every "interval" ms for the
- * extension. If the desktop extension is not install and checkAgain()==true
- * createLocalTracks will finish with rejected Promise.
- * @param {Function} listener - The listener will be called to notify the
- * user of lib-jitsi-meet that createLocalTracks is starting external
- * extension installation process.
- * NOTE: If the inline installation process is not possible and external
- * installation is enabled the listener property will be called to notify
- * the start of external installation process. After that createLocalTracks
- * will start to check for the extension on every interval ms until the
- * plugin is installed or until checkAgain return false. If the extension
- * is found createLocalTracks will try to get the desktop sharing track and
- * will finish the execution. If checkAgain returns false, createLocalTracks
- * will finish the execution with rejected Promise.
- *
- * @param {boolean} (firePermissionPromptIsShownEvent) - if event
- * JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN should be fired
- * @param originalOptions - internal use only, to be able to store the
- * originally requested options.
- * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>} A promise
- * that returns an array of created JitsiTracks if resolved, or a
- * JitsiConferenceError if rejected.
- */
- createLocalTracks(
- options = {}, firePermissionPromptIsShownEvent, originalOptions) {
- let promiseFulfilled = false;
-
- if (firePermissionPromptIsShownEvent === true) {
- window.setTimeout(() => {
- if (!promiseFulfilled) {
- JitsiMediaDevices.emitEvent(
- JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN,
- browser.getName());
- }
- }, USER_MEDIA_PERMISSION_PROMPT_TIMEOUT);
- }
-
- if (!window.connectionTimes) {
- window.connectionTimes = {};
- }
- window.connectionTimes['obtainPermissions.start']
- = window.performance.now();
-
- return RTC.obtainAudioAndVideoPermissions(options)
- .then(tracks => {
- promiseFulfilled = true;
-
- window.connectionTimes['obtainPermissions.end']
- = window.performance.now();
-
- Statistics.sendAnalytics(
- createGetUserMediaEvent(
- 'success',
- getAnalyticsAttributesFromOptions(options)));
-
- if (!RTC.options.disableAudioLevels) {
- for (let i = 0; i < tracks.length; i++) {
- const track = tracks[i];
- const mStream = track.getOriginalStream();
-
- if (track.getType() === MediaType.AUDIO) {
- Statistics.startLocalStats(mStream,
- track.setAudioLevel.bind(track));
- track.addEventListener(
- JitsiTrackEvents.LOCAL_TRACK_STOPPED,
- () => {
- Statistics.stopLocalStats(mStream);
- });
- }
- }
- }
-
- // set real device ids
- const currentlyAvailableMediaDevices
- = RTC.getCurrentlyAvailableMediaDevices();
-
- if (currentlyAvailableMediaDevices) {
- for (let i = 0; i < tracks.length; i++) {
- const track = tracks[i];
-
- track._setRealDeviceIdFromDeviceList(
- currentlyAvailableMediaDevices);
- }
- }
-
- return tracks;
- })
- .catch(error => {
- promiseFulfilled = true;
-
- if (error.name === JitsiTrackErrors.UNSUPPORTED_RESOLUTION
- && !browser.usesNewGumFlow()) {
- const oldResolution = options.resolution || '720';
- const newResolution = getLowerResolution(oldResolution);
-
- if (newResolution !== null) {
- options.resolution = newResolution;
-
- logger.debug(
- 'Retry createLocalTracks with resolution',
- newResolution);
-
- Statistics.sendAnalytics(createGetUserMediaEvent(
- 'warning',
- {
- 'old_resolution': oldResolution,
- 'new_resolution': newResolution,
- reason: 'unsupported resolution'
- }));
-
- return this.createLocalTracks(
- options,
- undefined,
- originalOptions || Object.assign({}, options));
- }
-
- // We tried everything. If there is a mandatory device id,
- // remove it and let gum find a device to use.
- if (originalOptions
- && error.gum.constraints
- && error.gum.constraints.video
- && error.gum.constraints.video.mandatory
- && error.gum.constraints.video.mandatory.sourceId) {
- originalOptions.cameraDeviceId = undefined;
-
- return this.createLocalTracks(originalOptions);
- }
- }
-
- if (error.name
- === JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED) {
- // User cancelled action is not really an error, so only
- // log it as an event to avoid having conference classified
- // as partially failed
- const logObject = {
- id: 'chrome_extension_user_canceled',
- message: error.message
- };
-
- Statistics.sendLog(JSON.stringify(logObject));
-
- Statistics.sendAnalytics(
- createGetUserMediaEvent(
- 'warning',
- {
- reason: 'extension install user canceled'
- }));
- } else if (error.name === JitsiTrackErrors.NOT_FOUND) {
- // logs not found devices with just application log to cs
- const logObject = {
- id: 'usermedia_missing_device',
- status: error.gum.devices
- };
-
- Statistics.sendLog(JSON.stringify(logObject));
-
- const attributes
- = getAnalyticsAttributesFromOptions(options);
-
- attributes.reason = 'device not found';
- attributes.devices = error.gum.devices.join('.');
- Statistics.sendAnalytics(
- createGetUserMediaEvent('error', attributes));
- } else {
- // Report gUM failed to the stats
- Statistics.sendGetUserMediaFailed(error);
-
- const attributes
- = getAnalyticsAttributesFromOptions(options);
-
- attributes.reason = error.name;
- Statistics.sendAnalytics(
- createGetUserMediaEvent('error', attributes));
- }
-
- window.connectionTimes['obtainPermissions.end']
- = window.performance.now();
-
- return Promise.reject(error);
- });
- },
-
- /**
- * Checks if its possible to enumerate available cameras/microphones.
- *
- * @returns {Promise<boolean>} a Promise which will be resolved only once
- * the WebRTC stack is ready, either with true if the device listing is
- * available available or with false otherwise.
- * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
- */
- isDeviceListAvailable() {
- logger.warn('This method is deprecated, use '
- + 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
-
- return this.mediaDevices.isDeviceListAvailable();
- },
-
- /**
- * Returns true if changing the input (camera / microphone) or output
- * (audio) device is supported and false if not.
- *
- * @param {string} [deviceType] - type of device to change. Default is
- * {@code undefined} or 'input', 'output' - for audio output device change.
- * @returns {boolean} {@code true} if available; {@code false}, otherwise.
- * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
- */
- isDeviceChangeAvailable(deviceType) {
- logger.warn('This method is deprecated, use '
- + 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
-
- return this.mediaDevices.isDeviceChangeAvailable(deviceType);
- },
-
-
- /**
- * Checks if the current environment supports having multiple audio
- * input devices in use simultaneously.
- *
- * @returns {boolean} True if multiple audio input devices can be used.
- */
- isMultipleAudioInputSupported() {
- return this.mediaDevices.isMultipleAudioInputSupported();
- },
-
- /**
- * Checks if local tracks can collect stats and collection is enabled.
- *
- * @param {boolean} True if stats are being collected for local tracks.
- */
- isCollectingLocalStats() {
- return Statistics.audioLevelsEnabled
- && LocalStatsCollector.isLocalStatsSupported();
- },
-
- /**
- * Executes callback with list of media devices connected.
- *
- * @param {function} callback
- * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
- */
- enumerateDevices(callback) {
- logger.warn('This method is deprecated, use '
- + 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
- this.mediaDevices.enumerateDevices(callback);
- },
-
- /* eslint-disable max-params */
-
- /**
- * @returns function that can be used to be attached to window.onerror and
- * if options.enableWindowOnErrorHandler is enabled returns
- * the function used by the lib.
- * (function(message, source, lineno, colno, error)).
- */
- getGlobalOnErrorHandler(message, source, lineno, colno, error) {
- logger.error(
- `UnhandledError: ${message}`,
- `Script: ${source}`,
- `Line: ${lineno}`,
- `Column: ${colno}`,
- 'StackTrace: ', error);
- Statistics.reportGlobalError(error);
- },
-
- /* eslint-enable max-params */
-
- /**
- * Represents a hub/namespace for utility functionality which may be of
- * interest to lib-jitsi-meet clients.
- */
- util: {
- AuthUtil,
- ScriptUtil,
- browser
- }
- });
|