Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiMeetJS.js 15KB

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