modified lib-jitsi-meet dev repo
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JitsiMeetJS.js 16KB

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