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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. JitsiConnection: JitsiConnection,
  37. events: {
  38. conference: JitsiConferenceEvents,
  39. connection: JitsiConnectionEvents,
  40. track: JitsiTrackEvents
  41. },
  42. errors: {
  43. conference: JitsiConferenceErrors,
  44. connection: JitsiConnectionErrors,
  45. track: JitsiTrackErrors
  46. },
  47. logLevels: Logger.levels,
  48. /**
  49. * Array of functions that will receive the GUM error.
  50. */
  51. _gumFailedHandler: [],
  52. init: function (options) {
  53. Statistics.audioLevelsEnabled = !options.disableAudioLevels || true;
  54. if (options.enableWindowOnErrorHandler) {
  55. // if an old handler exists also fire its events
  56. var oldOnErrorHandler = window.onerror;
  57. window.onerror = function (message, source, lineno, colno, error) {
  58. JitsiMeetJS.getGlobalOnErrorHandler(
  59. message, source, lineno, colno, error);
  60. if(oldOnErrorHandler)
  61. oldOnErrorHandler(message, source, lineno, colno, error);
  62. }
  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. */
  132. isDeviceListAvailable: function () {
  133. return RTC.isDeviceListAvailable();
  134. },
  135. /**
  136. * Returns true if changing the camera / microphone device is supported and
  137. * false if not.
  138. * @returns {boolean} true if available, false otherwise.
  139. */
  140. isDeviceChangeAvailable: function () {
  141. return RTC.isDeviceChangeAvailable();
  142. },
  143. enumerateDevices: function (callback) {
  144. RTC.enumerateDevices(callback);
  145. },
  146. /**
  147. * Array of functions that will receive the unhandled errors.
  148. */
  149. _globalOnErrorHandler: [],
  150. /**
  151. * @returns function that can be used to be attached to window.onerror and
  152. * if options.enableWindowOnErrorHandler is enabled returns
  153. * the function used by the lib.
  154. * (function(message, source, lineno, colno, error)).
  155. */
  156. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  157. console.error(
  158. 'UnhandledError: ' + message,
  159. 'Script: ' + source,
  160. 'Line: ' + lineno,
  161. 'Column: ' + colno,
  162. 'StackTrace: ', error);
  163. JitsiMeetJS._globalOnErrorHandler.forEach(function (handler) {
  164. handler(error);
  165. });
  166. if(!JitsiMeetJS._globalOnErrorHandler.length){
  167. Statistics.sendUnhandledError(error);
  168. }
  169. },
  170. /**
  171. * Represents a hub/namespace for utility functionality which may be of
  172. * interest to LibJitsiMeet clients.
  173. */
  174. util: {
  175. ScriptUtil: ScriptUtil,
  176. RTCUIHelper: RTCUIHelper
  177. }
  178. };
  179. //Setups the promise object.
  180. window.Promise = window.Promise || require("es6-promise").Promise;
  181. module.exports = LibJitsiMeet;