Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

JitsiMeetJS.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 RTC = require("./modules/RTC/RTC");
  11. var RTCUIHelper = require("./modules/RTC/RTCUIHelper");
  12. var Statistics = require("./modules/statistics/statistics");
  13. var Resolutions = require("./service/RTC/Resolutions");
  14. var ScriptUtil = require("./modules/util/ScriptUtil");
  15. function getLowerResolution(resolution) {
  16. if(!Resolutions[resolution])
  17. return null;
  18. var order = Resolutions[resolution].order;
  19. var res = null;
  20. var resName = null;
  21. for(var i in Resolutions) {
  22. var tmp = Resolutions[i];
  23. if (!res || (res.order < tmp.order && tmp.order < order)) {
  24. resName = i;
  25. res = tmp;
  26. }
  27. }
  28. return resName;
  29. }
  30. /**
  31. * Namespace for the interface of Jitsi Meet Library.
  32. */
  33. var LibJitsiMeet = {
  34. version: '{#COMMIT_HASH#}',
  35. JitsiConnection: JitsiConnection,
  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. JitsiMeetJS.getGlobalOnErrorHandler(
  58. message, source, lineno, colno, error);
  59. if(oldOnErrorHandler)
  60. oldOnErrorHandler(message, source, lineno, colno, error);
  61. }
  62. }
  63. return RTC.init(options || {});
  64. },
  65. /**
  66. * Returns whether the desktop sharing is enabled or not.
  67. * @returns {boolean}
  68. */
  69. isDesktopSharingEnabled: function () {
  70. return RTC.isDesktopSharingEnabled();
  71. },
  72. setLogLevel: function (level) {
  73. Logger.setLogLevel(level);
  74. },
  75. /**
  76. * Creates the media tracks and returns them trough the callback.
  77. * @param options Object with properties / settings specifying the tracks which should be created.
  78. * should be created or some additional configurations about resolution for example.
  79. * @param {Array} options.devices the devices that will be requested
  80. * @param {string} options.resolution resolution constraints
  81. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  82. * type: "audio" or "video", videoType: "camera" or "desktop"}
  83. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  84. * @param {string} options.cameraDeviceId
  85. * @param {string} options.micDeviceId
  86. * @returns {Promise.<{Array.<JitsiTrack>}, JitsiConferenceError>}
  87. * A promise that returns an array of created JitsiTracks if resolved,
  88. * or a JitsiConferenceError if rejected.
  89. */
  90. createLocalTracks: function (options) {
  91. return RTC.obtainAudioAndVideoPermissions(options || {}).then(
  92. function(tracks) {
  93. if(!RTC.options.disableAudioLevels)
  94. for(var i = 0; i < tracks.length; i++) {
  95. var track = tracks[i];
  96. var mStream = track.getOriginalStream();
  97. if(track.getType() === "audio"){
  98. Statistics.startLocalStats(mStream,
  99. track.setAudioLevel.bind(track));
  100. track.addEventListener(
  101. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  102. function(){
  103. Statistics.stopLocalStats(mStream);
  104. });
  105. }
  106. }
  107. return tracks;
  108. }).catch(function (error) {
  109. this._gumFailedHandler.forEach(function (handler) {
  110. handler(error);
  111. });
  112. if(!this._gumFailedHandler.length)
  113. Statistics.sendGetUserMediaFailed(error);
  114. if(error === JitsiTrackErrors.UNSUPPORTED_RESOLUTION) {
  115. var oldResolution = options.resolution || '360';
  116. var newResolution = getLowerResolution(oldResolution);
  117. if(newResolution === null)
  118. return Promise.reject(error);
  119. options.resolution = newResolution;
  120. logger.debug("Retry createLocalTracks with resolution",
  121. newResolution);
  122. return LibJitsiMeet.createLocalTracks(options);
  123. }
  124. return Promise.reject(error);
  125. }.bind(this));
  126. },
  127. /**
  128. * Checks if its possible to enumerate available cameras/micropones.
  129. * @returns {boolean} true if available, false otherwise.
  130. */
  131. isDeviceListAvailable: function () {
  132. return RTC.isDeviceListAvailable();
  133. },
  134. /**
  135. * Returns true if changing the camera / microphone device is supported and
  136. * false if not.
  137. * @returns {boolean} true if available, false otherwise.
  138. */
  139. isDeviceChangeAvailable: function () {
  140. return RTC.isDeviceChangeAvailable();
  141. },
  142. enumerateDevices: function (callback) {
  143. RTC.enumerateDevices(callback);
  144. },
  145. /**
  146. * Array of functions that will receive the unhandled errors.
  147. */
  148. _globalOnErrorHandler: [],
  149. /**
  150. * @returns function that can be used to be attached to window.onerror and
  151. * if options.enableWindowOnErrorHandler is enabled returns
  152. * the function used by the lib.
  153. * (function(message, source, lineno, colno, error)).
  154. */
  155. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  156. console.error(
  157. 'UnhandledError: ' + message,
  158. 'Script: ' + source,
  159. 'Line: ' + lineno,
  160. 'Column: ' + colno,
  161. 'StackTrace: ', error);
  162. JitsiMeetJS._globalOnErrorHandler.forEach(function (handler) {
  163. handler(error);
  164. });
  165. if(!JitsiMeetJS._globalOnErrorHandler.length){
  166. Statistics.sendUnhandledError(error);
  167. }
  168. },
  169. /**
  170. * Represents a hub/namespace for utility functionality which may be of
  171. * interest to LibJitsiMeet clients.
  172. */
  173. util: {
  174. ScriptUtil: ScriptUtil,
  175. RTCUIHelper: RTCUIHelper
  176. }
  177. };
  178. //Setups the promise object.
  179. window.Promise = window.Promise || require("es6-promise").Promise;
  180. module.exports = LibJitsiMeet;