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

JitsiTrackError.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import * as JitsiTrackErrors from './JitsiTrackErrors';
  2. const 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[
  10. JitsiTrackErrors.CHROME_EXTENSION_USER_GESTURE_REQUIRED]
  11. = 'Failed to install Chrome extension - installations can only be initiated'
  12. + ' by a user gesture.';
  13. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_USER_CANCELED]
  14. = 'User canceled Chrome\'s screen sharing prompt';
  15. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CHROME_EXTENSION_GENERIC_ERROR]
  16. = 'Unknown error from Chrome extension';
  17. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR]
  18. = 'Unkown error from desktop picker';
  19. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND]
  20. = 'Failed to detect desktop picker';
  21. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.GENERAL]
  22. = 'Generic getUserMedia error';
  23. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.PERMISSION_DENIED]
  24. = 'User denied permission to use device(s): ';
  25. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NOT_FOUND]
  26. = 'Requested device(s) was/were not found: ';
  27. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.CONSTRAINT_FAILED]
  28. = 'Constraint could not be satisfied: ';
  29. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_IS_DISPOSED]
  30. = 'Track has been already disposed';
  31. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_NO_STREAM_FOUND]
  32. = 'Track does not have an associated Media Stream';
  33. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.TRACK_MUTE_UNMUTE_IN_PROGRESS]
  34. = 'Track mute/unmute process is currently in progress';
  35. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.NO_DATA_FROM_SOURCE]
  36. = 'The track has stopped receiving data from it\'s source';
  37. // FIXME: Using prototype inheritance because otherwise instanceof is not
  38. // working properly (see https://github.com/babel/babel/issues/3083)
  39. /**
  40. *
  41. * Represents an error that occurred to a JitsiTrack. Can represent various
  42. * types of errors. For error descriptions (@see JitsiTrackErrors).
  43. *
  44. * @extends Error
  45. *
  46. *
  47. * @constructor
  48. * @param {Object|string} error - error object or error name
  49. * @param {Object|string} (options) - getUserMedia constraints object or
  50. * error message
  51. * @param {('audio'|'video'|'desktop'|'screen'|'audiooutput')[]} (devices) -
  52. * list of getUserMedia requested devices
  53. */
  54. function JitsiTrackError(error, options, devices) {
  55. if (typeof error === 'object' && typeof error.name !== 'undefined') {
  56. /**
  57. * Additional information about original getUserMedia error
  58. * and constraints.
  59. * @type {{
  60. * error: Object,
  61. * constraints: Object,
  62. * devices: Array.<'audio'|'video'|'desktop'|'screen'>
  63. * }}
  64. */
  65. this.gum = {
  66. error,
  67. constraints: options,
  68. devices: devices && Array.isArray(devices)
  69. ? devices.slice(0)
  70. : undefined
  71. };
  72. switch (error.name) {
  73. case 'PermissionDeniedError':
  74. case 'SecurityError':
  75. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  76. this.message
  77. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  78. + (this.gum.devices || []).join(', ');
  79. break;
  80. case 'DevicesNotFoundError':
  81. case 'NotFoundError':
  82. this.name = JitsiTrackErrors.NOT_FOUND;
  83. this.message
  84. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  85. + (this.gum.devices || []).join(', ');
  86. break;
  87. case 'ConstraintNotSatisfiedError':
  88. case 'OverconstrainedError': {
  89. const constraintName = error.constraintName || error.constraint;
  90. if (options
  91. && options.video
  92. && (!devices || devices.indexOf('video') > -1)
  93. && (constraintName === 'minWidth'
  94. || constraintName === 'maxWidth'
  95. || constraintName === 'minHeight'
  96. || constraintName === 'maxHeight'
  97. || constraintName === 'width'
  98. || constraintName === 'height')) {
  99. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  100. this.message
  101. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  102. + getResolutionFromFailedConstraint(
  103. constraintName,
  104. options);
  105. } else {
  106. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  107. this.message
  108. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  109. + error.constraintName;
  110. }
  111. break;
  112. }
  113. default:
  114. this.name = JitsiTrackErrors.GENERAL;
  115. this.message
  116. = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
  117. break;
  118. }
  119. } else if (typeof error === 'string') {
  120. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  121. this.name = error;
  122. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  123. } else {
  124. // this is some generic error that do not fit any of our
  125. // pre-defined errors, so don't give it any specific name, just
  126. // store message
  127. this.message = error;
  128. }
  129. } else {
  130. throw new Error('Invalid arguments');
  131. }
  132. this.stack = error.stack || (new Error()).stack;
  133. }
  134. JitsiTrackError.prototype = Object.create(Error.prototype);
  135. JitsiTrackError.prototype.constructor = JitsiTrackError;
  136. /**
  137. * Gets failed resolution constraint from corresponding object.
  138. * @param {string} failedConstraintName
  139. * @param {Object} constraints
  140. * @returns {string|number}
  141. */
  142. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  143. if (constraints && constraints.video && constraints.video.mandatory) {
  144. switch (failedConstraintName) {
  145. case 'width':
  146. return constraints.video.mandatory.minWidth;
  147. case 'height':
  148. return constraints.video.mandatory.minHeight;
  149. default:
  150. return constraints.video.mandatory[failedConstraintName] || '';
  151. }
  152. }
  153. return '';
  154. }
  155. export default JitsiTrackError;