您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiMeetJS.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. }.bind(this);
  62. // if an old handler exists also fire its events
  63. var oldOnUnhandledRejection = window.onunhandledrejection;
  64. window.onunhandledrejection = function(event) {
  65. this.getGlobalOnErrorHandler(
  66. null, null, null, null, event.reason);
  67. if(oldOnUnhandledRejection)
  68. oldOnUnhandledRejection(event);
  69. }.bind(this);
  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 input (camera / microphone) or output
  144. * (audio) device is supported and false if not.
  145. * @params {string} [deviceType] - type of device to change. Default is
  146. * undefined or 'input', 'output' - for audio output device change.
  147. * @returns {boolean} true if available, false otherwise.
  148. */
  149. isDeviceChangeAvailable: function (deviceType) {
  150. return RTC.isDeviceChangeAvailable(deviceType);
  151. },
  152. /**
  153. * Returns currently used audio output device id, '' stands for default
  154. * device
  155. * @returns {string}
  156. */
  157. getAudioOutputDevice: function () {
  158. return RTC.getAudioOutputDevice();
  159. },
  160. /**
  161. * Sets current audio output device.
  162. * @param {string} deviceId - id of 'audiooutput' device from
  163. * navigator.mediaDevices.enumerateDevices(), '' is for default device
  164. * @returns {Promise} - resolves when audio output is changed, is rejected
  165. * otherwise
  166. */
  167. setAudioOutputDevice: function (deviceId) {
  168. return RTC.setAudioOutputDevice(deviceId);
  169. },
  170. enumerateDevices: function (callback) {
  171. RTC.enumerateDevices(callback);
  172. },
  173. /**
  174. * Array of functions that will receive the unhandled errors.
  175. */
  176. _globalOnErrorHandler: [],
  177. /**
  178. * @returns function that can be used to be attached to window.onerror and
  179. * if options.enableWindowOnErrorHandler is enabled returns
  180. * the function used by the lib.
  181. * (function(message, source, lineno, colno, error)).
  182. */
  183. getGlobalOnErrorHandler: function (message, source, lineno, colno, error) {
  184. console.error(
  185. 'UnhandledError: ' + message,
  186. 'Script: ' + source,
  187. 'Line: ' + lineno,
  188. 'Column: ' + colno,
  189. 'StackTrace: ', error);
  190. var globalOnErrorHandler = this._globalOnErrorHandler;
  191. if (globalOnErrorHandler.length) {
  192. globalOnErrorHandler.forEach(function (handler) {
  193. handler(error);
  194. });
  195. } else {
  196. Statistics.sendUnhandledError(error);
  197. }
  198. },
  199. /**
  200. * Represents a hub/namespace for utility functionality which may be of
  201. * interest to LibJitsiMeet clients.
  202. */
  203. util: {
  204. ScriptUtil: ScriptUtil,
  205. RTCUIHelper: RTCUIHelper
  206. }
  207. };
  208. // XXX JitsiConnection or the instances it initializes and is associated with
  209. // (e.g. JitsiConference) may need a reference to LibJitsiMeet (aka
  210. // JitsiMeetJS). An approach could be to declare LibJitsiMeet global (which is
  211. // what we do in Jitsi Meet) but that could be seen as not such a cool decision
  212. // certainly looks even worse within the lib-jitsi-meet library itself. That's
  213. // why the decision is to provide LibJitsiMeet as a parameter of
  214. // JitsiConnection.
  215. LibJitsiMeet.JitsiConnection = JitsiConnection.bind(null, LibJitsiMeet);
  216. //Setups the promise object.
  217. window.Promise = window.Promise || require("es6-promise").Promise;
  218. module.exports = LibJitsiMeet;