Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

JitsiMeetJS.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  6. var JitsiConnectionEvents = require("./JitsiConnectionEvents");
  7. var JitsiMediaDevicesEvents = require('./JitsiMediaDevicesEvents');
  8. var JitsiConnectionErrors = require("./JitsiConnectionErrors");
  9. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  10. var JitsiTrackEvents = require("./JitsiTrackEvents");
  11. var JitsiTrackErrors = require("./JitsiTrackErrors");
  12. var JitsiTrackError = require("./JitsiTrackError");
  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. var transcriberHolder = require("./modules/transcription/transcriberHolder");
  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(var 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. },
  75. errors: {
  76. conference: JitsiConferenceErrors,
  77. connection: JitsiConnectionErrors,
  78. recorder: JitsiRecorderErrors,
  79. track: JitsiTrackErrors
  80. },
  81. errorTypes: {
  82. JitsiTrackError: JitsiTrackError
  83. },
  84. logLevels: Logger.levels,
  85. mediaDevices: JitsiMediaDevices,
  86. analytics: null,
  87. transcriberHolder: transcriberHolder,
  88. init: function (options) {
  89. var 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), options);
  189. if(!RTC.options.disableAudioLevels)
  190. for(var i = 0; i < tracks.length; i++) {
  191. var 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. return tracks;
  204. }).catch(function (error) {
  205. promiseFulfilled = true;
  206. if(error.name === JitsiTrackErrors.UNSUPPORTED_RESOLUTION) {
  207. var oldResolution = options.resolution || '360',
  208. newResolution = getLowerResolution(oldResolution);
  209. if (newResolution !== null) {
  210. options.resolution = newResolution;
  211. logger.debug("Retry createLocalTracks with resolution",
  212. newResolution);
  213. Statistics.analytics.sendEvent(
  214. "getUserMedia.fail.resolution." + oldResolution);
  215. return LibJitsiMeet.createLocalTracks(options);
  216. }
  217. }
  218. if (JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED ===
  219. error.name) {
  220. // User cancelled action is not really an error, so only
  221. // log it as an event to avoid having conference classified
  222. // as partially failed
  223. var logObject = {
  224. id: "chrome_extension_user_canceled",
  225. message: error.message
  226. };
  227. Statistics.sendLog(JSON.stringify(logObject));
  228. Statistics.analytics.sendEvent(
  229. "getUserMedia.userCancel.extensionInstall");
  230. } else {
  231. // Report gUM failed to the stats
  232. Statistics.sendGetUserMediaFailed(error);
  233. }
  234. window.connectionTimes["obtainPermissions.end"] =
  235. window.performance.now();
  236. Statistics.analytics.sendEvent(
  237. addDeviceTypeToAnalyticsEvent(
  238. "getUserMedia.failed", options) + '.' + error.name,
  239. options);
  240. return Promise.reject(error);
  241. }.bind(this));
  242. },
  243. /**
  244. * Checks if its possible to enumerate available cameras/micropones.
  245. * @returns {Promise<boolean>} a Promise which will be resolved only once
  246. * the WebRTC stack is ready, either with true if the device listing is
  247. * available available or with false otherwise.
  248. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
  249. */
  250. isDeviceListAvailable: function () {
  251. logger.warn('This method is deprecated, use ' +
  252. 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
  253. return this.mediaDevices.isDeviceListAvailable();
  254. },
  255. /**
  256. * Returns true if changing the input (camera / microphone) or output
  257. * (audio) device is supported and false if not.
  258. * @params {string} [deviceType] - type of device to change. Default is
  259. * undefined or 'input', 'output' - for audio output device change.
  260. * @returns {boolean} true if available, false otherwise.
  261. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
  262. */
  263. isDeviceChangeAvailable: function (deviceType) {
  264. logger.warn('This method is deprecated, use ' +
  265. 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
  266. return this.mediaDevices.isDeviceChangeAvailable(deviceType);
  267. },
  268. /**
  269. * Executes callback with list of media devices connected.
  270. * @param {function} callback
  271. * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
  272. */
  273. enumerateDevices: function (callback) {
  274. logger.warn('This method is deprecated, use ' +
  275. 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
  276. this.mediaDevices.enumerateDevices(callback);
  277. },
  278. /**
  279. * @returns function that can be used to be attached to window.onerror and
  280. * if options.enableWindowOnErrorHandler is enabled returns
  281. * the function used by the lib.
  282. * (function(message, source, lineno, colno, error)).
  283. */
  284. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  285. logger.error(
  286. 'UnhandledError: ' + message,
  287. 'Script: ' + source,
  288. 'Line: ' + lineno,
  289. 'Column: ' + colno,
  290. 'StackTrace: ', error);
  291. Statistics.reportGlobalError(error);
  292. },
  293. /**
  294. * Represents a hub/namespace for utility functionality which may be of
  295. * interest to LibJitsiMeet clients.
  296. */
  297. util: {
  298. ScriptUtil: ScriptUtil,
  299. RTCUIHelper: RTCUIHelper,
  300. AuthUtil: AuthUtil
  301. }
  302. };
  303. //Setups the promise object.
  304. window.Promise = window.Promise || require("es6-promise").Promise;
  305. module.exports = LibJitsiMeet;