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

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