modified lib-jitsi-meet dev repo
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 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var JitsiConnection = require("./JitsiConnection");
  3. var JitsiMediaDevices = require("./JitsiMediaDevices");
  4. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  5. var JitsiConnectionEvents = require("./JitsiConnectionEvents");
  6. var JitsiMediaDevicesEvents = require('./JitsiMediaDevicesEvents');
  7. var JitsiConnectionErrors = require("./JitsiConnectionErrors");
  8. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  9. var JitsiTrackEvents = require("./JitsiTrackEvents");
  10. var JitsiTrackErrors = require("./JitsiTrackErrors");
  11. var JitsiTrackError = require("./JitsiTrackError");
  12. var JitsiRecorderErrors = require("./JitsiRecorderErrors");
  13. var Logger = require("jitsi-meet-logger");
  14. var MediaType = require("./service/RTC/MediaType");
  15. var RTC = require("./modules/RTC/RTC");
  16. var RTCUIHelper = require("./modules/RTC/RTCUIHelper");
  17. var Statistics = require("./modules/statistics/statistics");
  18. var Resolutions = require("./service/RTC/Resolutions");
  19. var ScriptUtil = require("./modules/util/ScriptUtil");
  20. var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
  21. var RTCBrowserType = require("./modules/RTC/RTCBrowserType");
  22. // The amount of time to wait until firing
  23. // JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN event
  24. var USER_MEDIA_PERMISSION_PROMPT_TIMEOUT = 500;
  25. function getLowerResolution(resolution) {
  26. if(!Resolutions[resolution])
  27. return null;
  28. var order = Resolutions[resolution].order;
  29. var res = null;
  30. var resName = null;
  31. for(var i in Resolutions) {
  32. var tmp = Resolutions[i];
  33. if (!res || (res.order < tmp.order && tmp.order < order)) {
  34. resName = i;
  35. res = tmp;
  36. }
  37. }
  38. return resName;
  39. }
  40. /**
  41. * Namespace for the interface of Jitsi Meet Library.
  42. */
  43. var LibJitsiMeet = {
  44. version: '{#COMMIT_HASH#}',
  45. JitsiConnection: JitsiConnection,
  46. events: {
  47. conference: JitsiConferenceEvents,
  48. connection: JitsiConnectionEvents,
  49. track: JitsiTrackEvents,
  50. mediaDevices: JitsiMediaDevicesEvents
  51. },
  52. errors: {
  53. conference: JitsiConferenceErrors,
  54. connection: JitsiConnectionErrors,
  55. recorder: JitsiRecorderErrors,
  56. track: JitsiTrackErrors
  57. },
  58. errorTypes: {
  59. JitsiTrackError: JitsiTrackError
  60. },
  61. logLevels: Logger.levels,
  62. mediaDevices: JitsiMediaDevices,
  63. init: function (options) {
  64. Statistics.audioLevelsEnabled = !options.disableAudioLevels;
  65. if(typeof options.audioLevelsInterval === 'number') {
  66. Statistics.audioLevelsInterval = options.audioLevelsInterval;
  67. }
  68. if (options.enableWindowOnErrorHandler) {
  69. GlobalOnErrorHandler.addHandler(
  70. this.getGlobalOnErrorHandler.bind(this));
  71. }
  72. // Lets send some general stats useful for debugging problems
  73. if (window.jitsiRegionInfo
  74. && Object.keys(window.jitsiRegionInfo).length > 0) {
  75. // remove quotes to make it prettier
  76. Statistics.sendLog(
  77. JSON.stringify(window.jitsiRegionInfo).replace(/\"/g, ""));
  78. }
  79. if(this.version)
  80. Statistics.sendLog("LibJitsiMeet:" + this.version);
  81. return RTC.init(options || {});
  82. },
  83. /**
  84. * Returns whether the desktop sharing is enabled or not.
  85. * @returns {boolean}
  86. */
  87. isDesktopSharingEnabled: function () {
  88. return RTC.isDesktopSharingEnabled();
  89. },
  90. setLogLevel: function (level) {
  91. Logger.setLogLevel(level);
  92. },
  93. /**
  94. * Creates the media tracks and returns them trough the callback.
  95. * @param options Object with properties / settings specifying the tracks which should be created.
  96. * should be created or some additional configurations about resolution for example.
  97. * @param {Array} options.devices the devices that will be requested
  98. * @param {string} options.resolution resolution constraints
  99. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  100. * type: "audio" or "video", videoType: "camera" or "desktop"}
  101. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  102. * @param {string} options.cameraDeviceId
  103. * @param {string} options.micDeviceId
  104. * @param {boolean} (firePermissionPromptIsShownEvent) - if event
  105. * JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN should be fired
  106. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>}
  107. * A promise that returns an array of created JitsiTracks if resolved,
  108. * or a JitsiConferenceError if rejected.
  109. */
  110. createLocalTracks: function (options, firePermissionPromptIsShownEvent) {
  111. var promiseFulfilled = false;
  112. if (firePermissionPromptIsShownEvent === true) {
  113. window.setTimeout(function () {
  114. if (!promiseFulfilled) {
  115. var browser = RTCBrowserType.getBrowserType()
  116. .split('rtc_browser.')[1];
  117. if (RTCBrowserType.isAndroid()) {
  118. browser = 'android';
  119. }
  120. JitsiMediaDevices.emitEvent(
  121. JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN,
  122. browser);
  123. }
  124. }, USER_MEDIA_PERMISSION_PROMPT_TIMEOUT);
  125. }
  126. return RTC.obtainAudioAndVideoPermissions(options || {})
  127. .then(function(tracks) {
  128. promiseFulfilled = true;
  129. if(!RTC.options.disableAudioLevels)
  130. for(var i = 0; i < tracks.length; i++) {
  131. var track = tracks[i];
  132. var mStream = track.getOriginalStream();
  133. if(track.getType() === MediaType.AUDIO){
  134. Statistics.startLocalStats(mStream,
  135. track.setAudioLevel.bind(track));
  136. track.addEventListener(
  137. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  138. function(){
  139. Statistics.stopLocalStats(mStream);
  140. });
  141. }
  142. }
  143. return tracks;
  144. }).catch(function (error) {
  145. promiseFulfilled = true;
  146. if(error.name === JitsiTrackErrors.UNSUPPORTED_RESOLUTION) {
  147. var oldResolution = options.resolution || '360',
  148. newResolution = getLowerResolution(oldResolution);
  149. if (newResolution !== null) {
  150. options.resolution = newResolution;
  151. logger.debug("Retry createLocalTracks with resolution",
  152. newResolution);
  153. return LibJitsiMeet.createLocalTracks(options);
  154. }
  155. }
  156. if (JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED ===
  157. error.name) {
  158. // User cancelled action is not really an error, so only
  159. // log it as an event to avoid having conference classified
  160. // as partially failed
  161. Statistics.sendLog(error.message);
  162. } else {
  163. // Report gUM failed to the stats
  164. Statistics.sendGetUserMediaFailed(error);
  165. }
  166. return Promise.reject(error);
  167. }.bind(this));
  168. },
  169. /**
  170. * Checks if its possible to enumerate available cameras/micropones.
  171. * @returns {boolean} true if available, false otherwise.
  172. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
  173. */
  174. isDeviceListAvailable: function () {
  175. logger.warn('This method is deprecated, use ' +
  176. 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
  177. return this.mediaDevices.isDeviceListAvailable();
  178. },
  179. /**
  180. * Returns true if changing the input (camera / microphone) or output
  181. * (audio) device is supported and false if not.
  182. * @params {string} [deviceType] - type of device to change. Default is
  183. * undefined or 'input', 'output' - for audio output device change.
  184. * @returns {boolean} true if available, false otherwise.
  185. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
  186. */
  187. isDeviceChangeAvailable: function (deviceType) {
  188. logger.warn('This method is deprecated, use ' +
  189. 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
  190. return this.mediaDevices.isDeviceChangeAvailable(deviceType);
  191. },
  192. /**
  193. * Executes callback with list of media devices connected.
  194. * @param {function} callback
  195. * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
  196. */
  197. enumerateDevices: function (callback) {
  198. logger.warn('This method is deprecated, use ' +
  199. 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
  200. this.mediaDevices.enumerateDevices(callback);
  201. },
  202. /**
  203. * @returns function that can be used to be attached to window.onerror and
  204. * if options.enableWindowOnErrorHandler is enabled returns
  205. * the function used by the lib.
  206. * (function(message, source, lineno, colno, error)).
  207. */
  208. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  209. logger.error(
  210. 'UnhandledError: ' + message,
  211. 'Script: ' + source,
  212. 'Line: ' + lineno,
  213. 'Column: ' + colno,
  214. 'StackTrace: ', error);
  215. Statistics.reportGlobalError(error);
  216. },
  217. /**
  218. * Represents a hub/namespace for utility functionality which may be of
  219. * interest to LibJitsiMeet clients.
  220. */
  221. util: {
  222. ScriptUtil: ScriptUtil,
  223. RTCUIHelper: RTCUIHelper
  224. }
  225. };
  226. //Setups the promise object.
  227. window.Promise = window.Promise || require("es6-promise").Promise;
  228. module.exports = LibJitsiMeet;