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

JitsiTrackError.js 6.5KB

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