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

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