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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. init: function (options) {
  65. Statistics.audioLevelsEnabled = !options.disableAudioLevels;
  66. if(typeof options.audioLevelsInterval === 'number') {
  67. Statistics.audioLevelsInterval = options.audioLevelsInterval;
  68. }
  69. if (options.enableWindowOnErrorHandler) {
  70. GlobalOnErrorHandler.addHandler(
  71. this.getGlobalOnErrorHandler.bind(this));
  72. }
  73. // Lets send some general stats useful for debugging problems
  74. if (window.jitsiRegionInfo
  75. && Object.keys(window.jitsiRegionInfo).length > 0) {
  76. // remove quotes to make it prettier
  77. Statistics.sendLog(
  78. JSON.stringify(window.jitsiRegionInfo).replace(/\"/g, ""));
  79. }
  80. if(this.version)
  81. Statistics.sendLog("LibJitsiMeet:" + this.version);
  82. return RTC.init(options || {});
  83. },
  84. /**
  85. * Returns whether the desktop sharing is enabled or not.
  86. * @returns {boolean}
  87. */
  88. isDesktopSharingEnabled: function () {
  89. return RTC.isDesktopSharingEnabled();
  90. },
  91. setLogLevel: function (level) {
  92. Logger.setLogLevel(level);
  93. },
  94. /**
  95. * Creates the media tracks and returns them trough the callback.
  96. * @param options Object with properties / settings specifying the tracks which should be created.
  97. * should be created or some additional configurations about resolution for example.
  98. * @param {Array} options.devices the devices that will be requested
  99. * @param {string} options.resolution resolution constraints
  100. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  101. * type: "audio" or "video", videoType: "camera" or "desktop"}
  102. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  103. * @param {string} options.cameraDeviceId
  104. * @param {string} options.micDeviceId
  105. * @param {boolean} (firePermissionPromptIsShownEvent) - if event
  106. * JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN should be fired
  107. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>}
  108. * A promise that returns an array of created JitsiTracks if resolved,
  109. * or a JitsiConferenceError if rejected.
  110. */
  111. createLocalTracks: function (options, firePermissionPromptIsShownEvent) {
  112. var promiseFulfilled = false;
  113. if (firePermissionPromptIsShownEvent === true) {
  114. window.setTimeout(function () {
  115. if (!promiseFulfilled) {
  116. var browser = RTCBrowserType.getBrowserType()
  117. .split('rtc_browser.')[1];
  118. if (RTCBrowserType.isAndroid()) {
  119. browser = 'android';
  120. }
  121. JitsiMediaDevices.emitEvent(
  122. JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN,
  123. browser);
  124. }
  125. }, USER_MEDIA_PERMISSION_PROMPT_TIMEOUT);
  126. }
  127. return RTC.obtainAudioAndVideoPermissions(options || {})
  128. .then(function(tracks) {
  129. promiseFulfilled = true;
  130. if(!RTC.options.disableAudioLevels)
  131. for(var i = 0; i < tracks.length; i++) {
  132. var track = tracks[i];
  133. var mStream = track.getOriginalStream();
  134. if(track.getType() === MediaType.AUDIO){
  135. Statistics.startLocalStats(mStream,
  136. track.setAudioLevel.bind(track));
  137. track.addEventListener(
  138. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  139. function(){
  140. Statistics.stopLocalStats(mStream);
  141. });
  142. }
  143. }
  144. return tracks;
  145. }).catch(function (error) {
  146. promiseFulfilled = true;
  147. if(error.name === JitsiTrackErrors.UNSUPPORTED_RESOLUTION) {
  148. var oldResolution = options.resolution || '360',
  149. newResolution = getLowerResolution(oldResolution);
  150. if (newResolution !== null) {
  151. options.resolution = newResolution;
  152. logger.debug("Retry createLocalTracks with resolution",
  153. newResolution);
  154. return LibJitsiMeet.createLocalTracks(options);
  155. }
  156. }
  157. if (JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED ===
  158. error.name) {
  159. // User cancelled action is not really an error, so only
  160. // log it as an event to avoid having conference classified
  161. // as partially failed
  162. Statistics.sendLog(error.message);
  163. } else {
  164. // Report gUM failed to the stats
  165. Statistics.sendGetUserMediaFailed(error);
  166. }
  167. return Promise.reject(error);
  168. }.bind(this));
  169. },
  170. /**
  171. * Checks if its possible to enumerate available cameras/micropones.
  172. * @returns {boolean} true if available, false otherwise.
  173. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
  174. */
  175. isDeviceListAvailable: function () {
  176. logger.warn('This method is deprecated, use ' +
  177. 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
  178. return this.mediaDevices.isDeviceListAvailable();
  179. },
  180. /**
  181. * Returns true if changing the input (camera / microphone) or output
  182. * (audio) device is supported and false if not.
  183. * @params {string} [deviceType] - type of device to change. Default is
  184. * undefined or 'input', 'output' - for audio output device change.
  185. * @returns {boolean} true if available, false otherwise.
  186. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
  187. */
  188. isDeviceChangeAvailable: function (deviceType) {
  189. logger.warn('This method is deprecated, use ' +
  190. 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
  191. return this.mediaDevices.isDeviceChangeAvailable(deviceType);
  192. },
  193. /**
  194. * Executes callback with list of media devices connected.
  195. * @param {function} callback
  196. * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
  197. */
  198. enumerateDevices: function (callback) {
  199. logger.warn('This method is deprecated, use ' +
  200. 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
  201. this.mediaDevices.enumerateDevices(callback);
  202. },
  203. /**
  204. * @returns function that can be used to be attached to window.onerror and
  205. * if options.enableWindowOnErrorHandler is enabled returns
  206. * the function used by the lib.
  207. * (function(message, source, lineno, colno, error)).
  208. */
  209. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  210. logger.error(
  211. 'UnhandledError: ' + message,
  212. 'Script: ' + source,
  213. 'Line: ' + lineno,
  214. 'Column: ' + colno,
  215. 'StackTrace: ', error);
  216. Statistics.reportGlobalError(error);
  217. },
  218. /**
  219. * Represents a hub/namespace for utility functionality which may be of
  220. * interest to LibJitsiMeet clients.
  221. */
  222. util: {
  223. ScriptUtil: ScriptUtil,
  224. RTCUIHelper: RTCUIHelper,
  225. AuthUtil: AuthUtil
  226. }
  227. };
  228. //Setups the promise object.
  229. window.Promise = window.Promise || require("es6-promise").Promise;
  230. module.exports = LibJitsiMeet;