Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JitsiMeetJS.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var AuthUtil = require("./modules/util/AuthUtil");
  3. var JitsiConnection = require("./JitsiConnection");
  4. var JitsiMediaDevices = require("./JitsiMediaDevices");
  5. import * as JitsiConferenceErrors from "./JitsiConferenceErrors";
  6. import * as JitsiConferenceEvents from "./JitsiConferenceEvents";
  7. import * as JitsiConnectionErrors from "./JitsiConnectionErrors";
  8. import * as JitsiConnectionEvents from "./JitsiConnectionEvents";
  9. import * as JitsiMediaDevicesEvents from "./JitsiMediaDevicesEvents";
  10. import JitsiTrackError from "./JitsiTrackError";
  11. import * as JitsiTrackErrors from "./JitsiTrackErrors";
  12. import * as JitsiTrackEvents from "./JitsiTrackEvents";
  13. var JitsiRecorderErrors = require("./JitsiRecorderErrors");
  14. var Logger = require("jitsi-meet-logger");
  15. var MediaType = require("./service/RTC/MediaType");
  16. var RTC = require("./modules/RTC/RTC");
  17. var RTCUIHelper = require("./modules/RTC/RTCUIHelper");
  18. var Statistics = require("./modules/statistics/statistics");
  19. var Resolutions = require("./service/RTC/Resolutions");
  20. var ScriptUtil = require("./modules/util/ScriptUtil");
  21. var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
  22. var RTCBrowserType = require("./modules/RTC/RTCBrowserType");
  23. // The amount of time to wait until firing
  24. // JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN event
  25. var USER_MEDIA_PERMISSION_PROMPT_TIMEOUT = 500;
  26. function getLowerResolution(resolution) {
  27. if(!Resolutions[resolution])
  28. return null;
  29. var order = Resolutions[resolution].order;
  30. var res = null;
  31. var resName = null;
  32. for(let i in Resolutions) {
  33. var tmp = Resolutions[i];
  34. if (!res || (res.order < tmp.order && tmp.order < order)) {
  35. resName = i;
  36. res = tmp;
  37. }
  38. }
  39. return resName;
  40. }
  41. /**
  42. * Checks the available devices in options and concatenate the data to the
  43. * name, which will be used as analytics event name. Adds resolution for the
  44. * devices.
  45. * @param name name of event
  46. * @param options gum options
  47. * @returns {*}
  48. */
  49. function addDeviceTypeToAnalyticsEvent(name, options) {
  50. if (options.devices.indexOf("audio") !== -1) {
  51. name += ".audio";
  52. }
  53. if (options.devices.indexOf("desktop") !== -1) {
  54. name += ".desktop";
  55. }
  56. if (options.devices.indexOf("video") !== -1) {
  57. // we have video add resolution
  58. name += ".video." + options.resolution;
  59. }
  60. return name;
  61. }
  62. /**
  63. * Namespace for the interface of Jitsi Meet Library.
  64. */
  65. var LibJitsiMeet = {
  66. version: '{#COMMIT_HASH#}',
  67. JitsiConnection: JitsiConnection,
  68. events: {
  69. conference: JitsiConferenceEvents,
  70. connection: JitsiConnectionEvents,
  71. track: JitsiTrackEvents,
  72. mediaDevices: JitsiMediaDevicesEvents
  73. },
  74. errors: {
  75. conference: JitsiConferenceErrors,
  76. connection: JitsiConnectionErrors,
  77. recorder: JitsiRecorderErrors,
  78. track: JitsiTrackErrors
  79. },
  80. errorTypes: {
  81. JitsiTrackError: JitsiTrackError
  82. },
  83. logLevels: Logger.levels,
  84. mediaDevices: JitsiMediaDevices,
  85. analytics: null,
  86. init: function (options) {
  87. let logObject, attr;
  88. Statistics.init(options);
  89. this.analytics = Statistics.analytics;
  90. if (options.enableWindowOnErrorHandler) {
  91. GlobalOnErrorHandler.addHandler(
  92. this.getGlobalOnErrorHandler.bind(this));
  93. }
  94. // Log deployment-specific information, if available.
  95. if (window.jitsiRegionInfo
  96. && Object.keys(window.jitsiRegionInfo).length > 0) {
  97. logObject = {};
  98. for (attr in window.jitsiRegionInfo) {
  99. if (window.jitsiRegionInfo.hasOwnProperty(attr)) {
  100. logObject[attr] = window.jitsiRegionInfo[attr];
  101. }
  102. }
  103. logObject.id = "deployment_info";
  104. Statistics.sendLog(JSON.stringify(logObject));
  105. }
  106. if(this.version) {
  107. logObject = {
  108. id: "component_version",
  109. component: "lib-jitsi-meet",
  110. version: this.version
  111. };
  112. Statistics.sendLog(JSON.stringify(logObject));
  113. }
  114. return RTC.init(options || {});
  115. },
  116. /**
  117. * Returns whether the desktop sharing is enabled or not.
  118. * @returns {boolean}
  119. */
  120. isDesktopSharingEnabled: function () {
  121. return RTC.isDesktopSharingEnabled();
  122. },
  123. setLogLevel: function (level) {
  124. Logger.setLogLevel(level);
  125. },
  126. /**
  127. * Creates the media tracks and returns them trough the callback.
  128. * @param options Object with properties / settings specifying the tracks which should be created.
  129. * should be created or some additional configurations about resolution for example.
  130. * @param {Array} options.devices the devices that will be requested
  131. * @param {string} options.resolution resolution constraints
  132. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  133. * type: "audio" or "video", videoType: "camera" or "desktop"}
  134. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  135. * @param {string} options.cameraDeviceId
  136. * @param {string} options.micDeviceId
  137. * @param {object} options.desktopSharingExtensionExternalInstallation -
  138. * enables external installation process for desktop sharing extension if
  139. * the inline installation is not posible. The following properties should
  140. * be provided:
  141. * @param {intiger} interval - the interval (in ms) for
  142. * checking whether the desktop sharing extension is installed or not
  143. * @param {Function} checkAgain - returns boolean. While checkAgain()==true
  144. * createLocalTracks will wait and check on every "interval" ms for the
  145. * extension. If the desktop extension is not install and checkAgain()==true
  146. * createLocalTracks will finish with rejected Promise.
  147. * @param {Function} listener - The listener will be called to notify the
  148. * user of lib-jitsi-meet that createLocalTracks is starting external
  149. * extension installation process.
  150. * NOTE: If the inline installation process is not possible and external
  151. * installation is enabled the listener property will be called to notify
  152. * the start of external installation process. After that createLocalTracks
  153. * will start to check for the extension on every interval ms until the
  154. * plugin is installed or until checkAgain return false. If the extension
  155. * is found createLocalTracks will try to get the desktop sharing track and
  156. * will finish the execution. If checkAgain returns false, createLocalTracks
  157. * will finish the execution with rejected Promise.
  158. *
  159. * @param {boolean} (firePermissionPromptIsShownEvent) - if event
  160. * JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN should be fired
  161. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>}
  162. * A promise that returns an array of created JitsiTracks if resolved,
  163. * or a JitsiConferenceError if rejected.
  164. */
  165. createLocalTracks: function (options, firePermissionPromptIsShownEvent) {
  166. var promiseFulfilled = false;
  167. if (firePermissionPromptIsShownEvent === true) {
  168. window.setTimeout(function () {
  169. if (!promiseFulfilled) {
  170. JitsiMediaDevices.emitEvent(
  171. JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN,
  172. RTCBrowserType.getBrowserName());
  173. }
  174. }, USER_MEDIA_PERMISSION_PROMPT_TIMEOUT);
  175. }
  176. if(!window.connectionTimes)
  177. window.connectionTimes = {};
  178. window.connectionTimes["obtainPermissions.start"] =
  179. window.performance.now();
  180. return RTC.obtainAudioAndVideoPermissions(options || {})
  181. .then(function(tracks) {
  182. promiseFulfilled = true;
  183. window.connectionTimes["obtainPermissions.end"] =
  184. window.performance.now();
  185. Statistics.analytics.sendEvent(addDeviceTypeToAnalyticsEvent(
  186. "getUserMedia.success", options), options);
  187. if(!RTC.options.disableAudioLevels)
  188. for(let i = 0; i < tracks.length; i++) {
  189. const track = tracks[i];
  190. var mStream = track.getOriginalStream();
  191. if(track.getType() === MediaType.AUDIO){
  192. Statistics.startLocalStats(mStream,
  193. track.setAudioLevel.bind(track));
  194. track.addEventListener(
  195. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  196. function(){
  197. Statistics.stopLocalStats(mStream);
  198. });
  199. }
  200. }
  201. // set real device ids
  202. var currentlyAvailableMediaDevices
  203. = RTC.getCurrentlyAvailableMediaDevices();
  204. if (currentlyAvailableMediaDevices) {
  205. for(let i = 0; i < tracks.length; i++) {
  206. const track = tracks[i];
  207. track._setRealDeviceIdFromDeviceList(
  208. currentlyAvailableMediaDevices);
  209. }
  210. }
  211. return tracks;
  212. }).catch(function (error) {
  213. promiseFulfilled = true;
  214. if(error.name === JitsiTrackErrors.UNSUPPORTED_RESOLUTION) {
  215. var oldResolution = options.resolution || '360',
  216. newResolution = getLowerResolution(oldResolution);
  217. if (newResolution !== null) {
  218. options.resolution = newResolution;
  219. logger.debug("Retry createLocalTracks with resolution",
  220. newResolution);
  221. Statistics.analytics.sendEvent(
  222. "getUserMedia.fail.resolution." + oldResolution);
  223. return LibJitsiMeet.createLocalTracks(options);
  224. }
  225. }
  226. if (JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED ===
  227. error.name) {
  228. // User cancelled action is not really an error, so only
  229. // log it as an event to avoid having conference classified
  230. // as partially failed
  231. const logObject = {
  232. id: "chrome_extension_user_canceled",
  233. message: error.message
  234. };
  235. Statistics.sendLog(JSON.stringify(logObject));
  236. Statistics.analytics.sendEvent(
  237. "getUserMedia.userCancel.extensionInstall");
  238. } else if (JitsiTrackErrors.NOT_FOUND === error.name) {
  239. // logs not found devices with just application log to cs
  240. const logObject = {
  241. id: "usermedia_missing_device",
  242. status: error.gum.devices
  243. };
  244. Statistics.sendLog(JSON.stringify(logObject));
  245. Statistics.analytics.sendEvent(
  246. "getUserMedia.deviceNotFound."
  247. + error.gum.devices.join('.'));
  248. } else {
  249. // Report gUM failed to the stats
  250. Statistics.sendGetUserMediaFailed(error);
  251. Statistics.analytics.sendEvent(
  252. addDeviceTypeToAnalyticsEvent(
  253. "getUserMedia.failed", options) + '.' + error.name,
  254. options);
  255. }
  256. window.connectionTimes["obtainPermissions.end"] =
  257. window.performance.now();
  258. return Promise.reject(error);
  259. }.bind(this));
  260. },
  261. /**
  262. * Checks if its possible to enumerate available cameras/micropones.
  263. * @returns {Promise<boolean>} a Promise which will be resolved only once
  264. * the WebRTC stack is ready, either with true if the device listing is
  265. * available available or with false otherwise.
  266. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
  267. */
  268. isDeviceListAvailable: function () {
  269. logger.warn('This method is deprecated, use ' +
  270. 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
  271. return this.mediaDevices.isDeviceListAvailable();
  272. },
  273. /**
  274. * Returns true if changing the input (camera / microphone) or output
  275. * (audio) device is supported and false if not.
  276. * @params {string} [deviceType] - type of device to change. Default is
  277. * undefined or 'input', 'output' - for audio output device change.
  278. * @returns {boolean} true if available, false otherwise.
  279. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
  280. */
  281. isDeviceChangeAvailable: function (deviceType) {
  282. logger.warn('This method is deprecated, use ' +
  283. 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
  284. return this.mediaDevices.isDeviceChangeAvailable(deviceType);
  285. },
  286. /**
  287. * Executes callback with list of media devices connected.
  288. * @param {function} callback
  289. * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
  290. */
  291. enumerateDevices: function (callback) {
  292. logger.warn('This method is deprecated, use ' +
  293. 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
  294. this.mediaDevices.enumerateDevices(callback);
  295. },
  296. /**
  297. * @returns function that can be used to be attached to window.onerror and
  298. * if options.enableWindowOnErrorHandler is enabled returns
  299. * the function used by the lib.
  300. * (function(message, source, lineno, colno, error)).
  301. */
  302. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  303. logger.error(
  304. 'UnhandledError: ' + message,
  305. 'Script: ' + source,
  306. 'Line: ' + lineno,
  307. 'Column: ' + colno,
  308. 'StackTrace: ', error);
  309. Statistics.reportGlobalError(error);
  310. },
  311. /**
  312. * Represents a hub/namespace for utility functionality which may be of
  313. * interest to LibJitsiMeet clients.
  314. */
  315. util: {
  316. ScriptUtil: ScriptUtil,
  317. RTCUIHelper: RTCUIHelper,
  318. AuthUtil: AuthUtil
  319. }
  320. };
  321. module.exports = LibJitsiMeet;