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

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