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.

JitsiTrackError.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var JitsiTrackErrors = require("./JitsiTrackErrors");
  2. var TRACK_ERROR_TO_MESSAGE_MAP = {};
  3. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.UNSUPPORTED_RESOLUTION]
  4. = "Video resolution is not supported: ";
  5. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED]
  6. = "Firefox extension is not installed";
  7. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR]
  8. = "Failed to install Chrome extension";
  9. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED]
  10. = "User canceled Chrome's screen sharing prompt";
  11. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR]
  12. = "Unknown error from Chrome extension";
  13. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL]
  14. = "Generic getUserMedia error";
  15. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.PERMISSION_DENIED]
  16. = "User denied permission to use device(s): ";
  17. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NOT_FOUND]
  18. = "Requested device(s) was/were not found: ";
  19. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CONSTRAINT_FAILED]
  20. = "Constraint could not be satisfied: ";
  21. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_IS_DISPOSED]
  22. = "Track has been already disposed";
  23. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_NO_STREAM_FOUND]
  24. = "Track does not have an associated Media Stream";
  25. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS]
  26. = "Track mute/unmute process is currently in progress";
  27. /**
  28. * Object representing error that happened to a JitsiTrack. Can represent
  29. * various types of errors. For error descriptions (@see JitsiTrackErrors).
  30. * @constructor
  31. * @extends Error
  32. * @param {Object|string} error - error object or error name
  33. * @param {Object|string} (options) - getUserMedia constraints object or error
  34. * message
  35. * @param {('audio'|'video'|'desktop'|'screen'|'audiooutput')[]} (devices) -
  36. * list of getUserMedia requested devices
  37. */
  38. function JitsiTrackError(error, options, devices) {
  39. if (typeof error === "object" && typeof error.name !== "undefined") {
  40. /**
  41. * Additional information about original getUserMedia error
  42. * and constraints.
  43. * @type {{
  44. * error: Object,
  45. * constraints: Object,
  46. * devices: Array.<'audio'|'video'|'desktop'|'screen'>
  47. * }}
  48. */
  49. this.gum = {
  50. error: error,
  51. constraints: options,
  52. devices: devices && Array.isArray(devices)
  53. ? devices.slice(0)
  54. : undefined
  55. };
  56. switch (error.name) {
  57. case "PermissionDeniedError":
  58. case "SecurityError":
  59. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  60. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  61. JitsiTrackErrors.PERMISSION_DENIED]
  62. + (this.gum.devices || []).join(", ");
  63. break;
  64. case "NotFoundError":
  65. this.name = JitsiTrackErrors.NOT_FOUND;
  66. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  67. JitsiTrackErrors.NOT_FOUND]
  68. + (this.gum.devices || []).join(", ");
  69. break;
  70. case "ConstraintNotSatisfiedError":
  71. case "OverconstrainedError":
  72. var constraintName = error.constraintName;
  73. if (options && options.video
  74. && (devices || []).indexOf('video') > -1
  75. &&
  76. (constraintName === "minWidth" ||
  77. constraintName === "maxWidth" ||
  78. constraintName === "minHeight" ||
  79. constraintName === "maxHeight" ||
  80. constraintName === "width" ||
  81. constraintName === "height")) {
  82. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  83. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  84. JitsiTrackErrors.UNSUPPORTED_RESOLUTION] +
  85. getResolutionFromFailedConstraint(constraintName,
  86. options);
  87. } else {
  88. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  89. this.message = TRACK_ERROR_TO_MESSAGE_MAP[
  90. JitsiTrackErrors.CONSTRAINT_FAILED] +
  91. error.constraintName;
  92. }
  93. break;
  94. default:
  95. this.name = JitsiTrackErrors.GENERAL;
  96. this.message = error.message ||
  97. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL];
  98. break;
  99. }
  100. } else if (typeof error === "string") {
  101. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  102. this.name = error;
  103. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  104. } else {
  105. // this is some generic error that do not fit any of our pre-defined
  106. // errors, so don't give it any specific name, just store message
  107. this.message = error;
  108. }
  109. } else {
  110. throw new Error("Invalid arguments");
  111. }
  112. this.stack = error.stack || (new Error()).stack;
  113. }
  114. JitsiTrackError.prototype = Object.create(Error.prototype);
  115. JitsiTrackError.prototype.constructor = JitsiTrackError;
  116. /**
  117. * Gets failed resolution constraint from corresponding object.
  118. * @param {string} failedConstraintName
  119. * @param {Object} constraints
  120. * @returns {string|number}
  121. */
  122. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  123. if (constraints && constraints.video && constraints.video.mandatory) {
  124. if (failedConstraintName === "width") {
  125. return constraints.video.mandatory.minWidth;
  126. } else if (failedConstraintName === "height") {
  127. return constraints.video.mandatory.minHeight;
  128. } else {
  129. return constraints.video.mandatory[failedConstraintName] || "";
  130. }
  131. }
  132. return "";
  133. }
  134. module.exports = JitsiTrackError;