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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 JitsiRecorderErrors = require("./JitsiRecorderErrors");
  12. var Logger = require("jitsi-meet-logger");
  13. var MediaType = require("./service/RTC/MediaType");
  14. var RTC = require("./modules/RTC/RTC");
  15. var RTCUIHelper = require("./modules/RTC/RTCUIHelper");
  16. var Statistics = require("./modules/statistics/statistics");
  17. var Resolutions = require("./service/RTC/Resolutions");
  18. var ScriptUtil = require("./modules/util/ScriptUtil");
  19. var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
  20. function getLowerResolution(resolution) {
  21. if(!Resolutions[resolution])
  22. return null;
  23. var order = Resolutions[resolution].order;
  24. var res = null;
  25. var resName = null;
  26. for(var i in Resolutions) {
  27. var tmp = Resolutions[i];
  28. if (!res || (res.order < tmp.order && tmp.order < order)) {
  29. resName = i;
  30. res = tmp;
  31. }
  32. }
  33. return resName;
  34. }
  35. /**
  36. * Namespace for the interface of Jitsi Meet Library.
  37. */
  38. var LibJitsiMeet = {
  39. version: '{#COMMIT_HASH#}',
  40. events: {
  41. conference: JitsiConferenceEvents,
  42. connection: JitsiConnectionEvents,
  43. track: JitsiTrackEvents,
  44. mediaDevices: JitsiMediaDevicesEvents
  45. },
  46. errors: {
  47. conference: JitsiConferenceErrors,
  48. connection: JitsiConnectionErrors,
  49. recorder: JitsiRecorderErrors,
  50. track: JitsiTrackErrors
  51. },
  52. logLevels: Logger.levels,
  53. mediaDevices: JitsiMediaDevices,
  54. /**
  55. * Array of functions that will receive the GUM error.
  56. */
  57. _gumFailedHandler: [],
  58. init: function (options) {
  59. Statistics.audioLevelsEnabled = !options.disableAudioLevels;
  60. if (options.enableWindowOnErrorHandler) {
  61. GlobalOnErrorHandler.addHandler(
  62. this.getGlobalOnErrorHandler.bind(this));
  63. }
  64. return RTC.init(options || {});
  65. },
  66. /**
  67. * Returns whether the desktop sharing is enabled or not.
  68. * @returns {boolean}
  69. */
  70. isDesktopSharingEnabled: function () {
  71. return RTC.isDesktopSharingEnabled();
  72. },
  73. setLogLevel: function (level) {
  74. Logger.setLogLevel(level);
  75. },
  76. /**
  77. * Creates the media tracks and returns them trough the callback.
  78. * @param options Object with properties / settings specifying the tracks which should be created.
  79. * should be created or some additional configurations about resolution for example.
  80. * @param {Array} options.devices the devices that will be requested
  81. * @param {string} options.resolution resolution constraints
  82. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  83. * type: "audio" or "video", videoType: "camera" or "desktop"}
  84. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  85. * @param {string} options.cameraDeviceId
  86. * @param {string} options.micDeviceId
  87. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>}
  88. * A promise that returns an array of created JitsiTracks if resolved,
  89. * or a JitsiConferenceError if rejected.
  90. */
  91. createLocalTracks: function (options) {
  92. return RTC.obtainAudioAndVideoPermissions(options || {}).then(
  93. function(tracks) {
  94. if(!RTC.options.disableAudioLevels)
  95. for(var i = 0; i < tracks.length; i++) {
  96. var track = tracks[i];
  97. var mStream = track.getOriginalStream();
  98. if(track.getType() === MediaType.AUDIO){
  99. Statistics.startLocalStats(mStream,
  100. track.setAudioLevel.bind(track));
  101. track.addEventListener(
  102. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  103. function(){
  104. Statistics.stopLocalStats(mStream);
  105. });
  106. }
  107. }
  108. return tracks;
  109. }).catch(function (error) {
  110. this._gumFailedHandler.forEach(function (handler) {
  111. handler(error);
  112. });
  113. if(!this._gumFailedHandler.length)
  114. Statistics.sendGetUserMediaFailed(error);
  115. if(error === JitsiTrackErrors.UNSUPPORTED_RESOLUTION) {
  116. var oldResolution = options.resolution || '360';
  117. var newResolution = getLowerResolution(oldResolution);
  118. if(newResolution === null)
  119. return Promise.reject(error);
  120. options.resolution = newResolution;
  121. logger.debug("Retry createLocalTracks with resolution",
  122. newResolution);
  123. return LibJitsiMeet.createLocalTracks(options);
  124. }
  125. return Promise.reject(error);
  126. }.bind(this));
  127. },
  128. /**
  129. * Checks if its possible to enumerate available cameras/micropones.
  130. * @returns {boolean} true if available, false otherwise.
  131. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceListAvailable instead
  132. */
  133. isDeviceListAvailable: function () {
  134. logger.warn('This method is deprecated, use ' +
  135. 'JitsiMeetJS.mediaDevices.isDeviceListAvailable instead');
  136. return this.mediaDevices.isDeviceListAvailable();
  137. },
  138. /**
  139. * Returns true if changing the input (camera / microphone) or output
  140. * (audio) device is supported and false if not.
  141. * @params {string} [deviceType] - type of device to change. Default is
  142. * undefined or 'input', 'output' - for audio output device change.
  143. * @returns {boolean} true if available, false otherwise.
  144. * @deprecated use JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead
  145. */
  146. isDeviceChangeAvailable: function (deviceType) {
  147. logger.warn('This method is deprecated, use ' +
  148. 'JitsiMeetJS.mediaDevices.isDeviceChangeAvailable instead');
  149. return this.mediaDevices.isDeviceChangeAvailable(deviceType);
  150. },
  151. /**
  152. * Executes callback with list of media devices connected.
  153. * @param {function} callback
  154. * @deprecated use JitsiMeetJS.mediaDevices.enumerateDevices instead
  155. */
  156. enumerateDevices: function (callback) {
  157. logger.warn('This method is deprecated, use ' +
  158. 'JitsiMeetJS.mediaDevices.enumerateDevices instead');
  159. this.mediaDevices.enumerateDevices(callback);
  160. },
  161. /**
  162. * Array of functions that will receive the unhandled errors.
  163. */
  164. _globalOnErrorHandler: [],
  165. /**
  166. * @returns function that can be used to be attached to window.onerror and
  167. * if options.enableWindowOnErrorHandler is enabled returns
  168. * the function used by the lib.
  169. * (function(message, source, lineno, colno, error)).
  170. */
  171. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  172. logger.error(
  173. 'UnhandledError: ' + message,
  174. 'Script: ' + source,
  175. 'Line: ' + lineno,
  176. 'Column: ' + colno,
  177. 'StackTrace: ', error);
  178. var globalOnErrorHandler = this._globalOnErrorHandler;
  179. if (globalOnErrorHandler.length) {
  180. globalOnErrorHandler.forEach(function (handler) {
  181. handler(error);
  182. });
  183. } else {
  184. Statistics.sendUnhandledError(error);
  185. }
  186. },
  187. /**
  188. * Represents a hub/namespace for utility functionality which may be of
  189. * interest to LibJitsiMeet clients.
  190. */
  191. util: {
  192. ScriptUtil: ScriptUtil,
  193. RTCUIHelper: RTCUIHelper
  194. }
  195. };
  196. // XXX JitsiConnection or the instances it initializes and is associated with
  197. // (e.g. JitsiConference) may need a reference to LibJitsiMeet (aka
  198. // JitsiMeetJS). An approach could be to declare LibJitsiMeet global (which is
  199. // what we do in Jitsi Meet) but that could be seen as not such a cool decision
  200. // certainly looks even worse within the lib-jitsi-meet library itself. That's
  201. // why the decision is to provide LibJitsiMeet as a parameter of
  202. // JitsiConnection.
  203. LibJitsiMeet.JitsiConnection = JitsiConnection.bind(null, LibJitsiMeet);
  204. //Setups the promise object.
  205. window.Promise = window.Promise || require("es6-promise").Promise;
  206. module.exports = LibJitsiMeet;