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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 {boolean} (firePermissionPromptIsShownEvent) - if event
  117. * JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN should be fired
  118. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>}
  119. * A promise that returns an array of created JitsiTracks if resolved,
  120. * or a JitsiConferenceError if rejected.
  121. */
  122. createLocalTracks: function (options, firePermissionPromptIsShownEvent) {
  123. var promiseFulfilled = false;
  124. if (firePermissionPromptIsShownEvent === true) {
  125. window.setTimeout(function () {
  126. if (!promiseFulfilled) {
  127. JitsiMediaDevices.emitEvent(
  128. JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN,
  129. RTCBrowserType.getBrowserName());
  130. }
  131. }, USER_MEDIA_PERMISSION_PROMPT_TIMEOUT);
  132. }
  133. return RTC.obtainAudioAndVideoPermissions(options || {})
  134. .then(function(tracks) {
  135. promiseFulfilled = true;
  136. if(!RTC.options.disableAudioLevels)
  137. for(var i = 0; i < tracks.length; i++) {
  138. var track = tracks[i];
  139. var mStream = track.getOriginalStream();
  140. if(track.getType() === MediaType.AUDIO){
  141. Statistics.startLocalStats(mStream,
  142. track.setAudioLevel.bind(track));
  143. track.addEventListener(
  144. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  145. function(){
  146. Statistics.stopLocalStats(mStream);
  147. });
  148. }
  149. }
  150. return tracks;
  151. }).catch(function (error) {
  152. promiseFulfilled = true;
  153. if(error.name === JitsiTrackErrors.UNSUPPORTED_RESOLUTION) {
  154. var oldResolution = options.resolution || '360',
  155. newResolution = getLowerResolution(oldResolution);
  156. if (newResolution !== null) {
  157. options.resolution = newResolution;
  158. logger.debug("Retry createLocalTracks with resolution",
  159. newResolution);
  160. return LibJitsiMeet.createLocalTracks(options);
  161. }
  162. }
  163. if (JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED ===
  164. error.name) {
  165. // User cancelled action is not really an error, so only
  166. // log it as an event to avoid having conference classified
  167. // as partially failed
  168. var logObject = {
  169. id: "chrome_extension_user_canceled",
  170. message: error.message
  171. };
  172. Statistics.sendLog(JSON.stringify(logObject));
  173. } else {
  174. // Report gUM failed to the stats
  175. Statistics.sendGetUserMediaFailed(error);
  176. }
  177. return Promise.reject(error);
  178. }.bind(this));
  179. },
  180. /**
  181. * Checks if its possible to enumerate available cameras/micropones.
  182. * @returns {Promise<boolean>} a Promise which will be resolved only once
  183. * the WebRTC stack is ready, either with true if the device listing is
  184. * available available or with false otherwise.
  185. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
  186. */
  187. isDeviceListAvailable: function () {
  188. logger.warn('This method is deprecated, use ' +
  189. 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
  190. return this.mediaDevices.isDeviceListAvailable();
  191. },
  192. /**
  193. * Returns true if changing the input (camera / microphone) or output
  194. * (audio) device is supported and false if not.
  195. * @params {string} [deviceType] - type of device to change. Default is
  196. * undefined or 'input', 'output' - for audio output device change.
  197. * @returns {boolean} true if available, false otherwise.
  198. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
  199. */
  200. isDeviceChangeAvailable: function (deviceType) {
  201. logger.warn('This method is deprecated, use ' +
  202. 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
  203. return this.mediaDevices.isDeviceChangeAvailable(deviceType);
  204. },
  205. /**
  206. * Executes callback with list of media devices connected.
  207. * @param {function} callback
  208. * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
  209. */
  210. enumerateDevices: function (callback) {
  211. logger.warn('This method is deprecated, use ' +
  212. 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
  213. this.mediaDevices.enumerateDevices(callback);
  214. },
  215. /**
  216. * @returns function that can be used to be attached to window.onerror and
  217. * if options.enableWindowOnErrorHandler is enabled returns
  218. * the function used by the lib.
  219. * (function(message, source, lineno, colno, error)).
  220. */
  221. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  222. logger.error(
  223. 'UnhandledError: ' + message,
  224. 'Script: ' + source,
  225. 'Line: ' + lineno,
  226. 'Column: ' + colno,
  227. 'StackTrace: ', error);
  228. Statistics.reportGlobalError(error);
  229. },
  230. /**
  231. * Represents a hub/namespace for utility functionality which may be of
  232. * interest to LibJitsiMeet clients.
  233. */
  234. util: {
  235. ScriptUtil: ScriptUtil,
  236. RTCUIHelper: RTCUIHelper,
  237. AuthUtil: AuthUtil
  238. }
  239. };
  240. //Setups the promise object.
  241. window.Promise = window.Promise || require("es6-promise").Promise;
  242. module.exports = LibJitsiMeet;