modified lib-jitsi-meet dev repo
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiMeetJS.js 9.2KB

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