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.2KB

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