Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JitsiMeetJS.js 8.7KB

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