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

JitsiTrackError.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.SCREENSHARING_USER_CANCELED]
  6. = 'User canceled screen sharing prompt';
  7. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.SCREENSHARING_GENERIC_ERROR]
  8. = 'Unknown error from screensharing';
  9. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR]
  10. = 'Unkown error from desktop picker';
  11. TRACK_ERROR_TO_MESSAGE_MAP[JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND]
  12. = 'Failed to detect desktop picker';
  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. // FIXME: Using prototype inheritance because otherwise instanceof is not
  26. // working properly (see https://github.com/babel/babel/issues/3083)
  27. /**
  28. *
  29. * Represents an error that occurred to a JitsiTrack. Can represent various
  30. * types of errors. For error descriptions (@see JitsiTrackErrors).
  31. *
  32. * @extends Error
  33. *
  34. *
  35. * @constructor
  36. * @param {Object|string} error - error object or error name
  37. * @param {Object|string} (options) - getUserMedia constraints object or
  38. * error message
  39. * @param {('audio'|'video'|'desktop'|'screen'|'audiooutput')[]} (devices) -
  40. * list of getUserMedia requested devices
  41. */
  42. function JitsiTrackError(error, options, devices) {
  43. if (typeof error === 'object' && typeof error.name !== 'undefined') {
  44. /**
  45. * Additional information about original getUserMedia error
  46. * and constraints.
  47. * @type {{
  48. * error: Object,
  49. * constraints: Object,
  50. * devices: Array.<'audio'|'video'|'desktop'|'screen'>
  51. * }}
  52. */
  53. this.gum = {
  54. error,
  55. constraints: options,
  56. devices: devices && Array.isArray(devices)
  57. ? devices.slice(0)
  58. : undefined
  59. };
  60. switch (error.name) {
  61. case 'NotAllowedError':
  62. case 'PermissionDeniedError':
  63. case 'SecurityError':
  64. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  65. this.message
  66. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  67. + (this.gum.devices || []).join(', ');
  68. break;
  69. case 'DevicesNotFoundError':
  70. case 'NotFoundError':
  71. this.name = JitsiTrackErrors.NOT_FOUND;
  72. this.message
  73. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  74. + (this.gum.devices || []).join(', ');
  75. break;
  76. case 'ConstraintNotSatisfiedError':
  77. case 'OverconstrainedError': {
  78. const constraintName = error.constraintName || error.constraint;
  79. // we treat deviceId as unsupported resolution, as we want to
  80. // retry and finally if everything fails to remove deviceId from
  81. // mandatory constraints
  82. if (options
  83. && options.video
  84. && (!devices || devices.indexOf('video') > -1)
  85. && (constraintName === 'minWidth'
  86. || constraintName === 'maxWidth'
  87. || constraintName === 'minHeight'
  88. || constraintName === 'maxHeight'
  89. || constraintName === 'width'
  90. || constraintName === 'height'
  91. || constraintName === 'deviceId')) {
  92. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  93. this.message
  94. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  95. + getResolutionFromFailedConstraint(
  96. constraintName,
  97. options);
  98. } else {
  99. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  100. this.message
  101. = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  102. + error.constraintName;
  103. }
  104. break;
  105. }
  106. default:
  107. this.name = JitsiTrackErrors.GENERAL;
  108. this.message
  109. = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
  110. break;
  111. }
  112. } else if (typeof error === 'string') {
  113. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  114. this.name = error;
  115. this.message = options || TRACK_ERROR_TO_MESSAGE_MAP[error];
  116. } else {
  117. // this is some generic error that do not fit any of our
  118. // pre-defined errors, so don't give it any specific name, just
  119. // store message
  120. this.message = error;
  121. }
  122. } else {
  123. throw new Error('Invalid arguments');
  124. }
  125. this.stack = error.stack || (new Error()).stack;
  126. }
  127. JitsiTrackError.prototype = Object.create(Error.prototype);
  128. JitsiTrackError.prototype.constructor = JitsiTrackError;
  129. /**
  130. * Gets failed resolution constraint from corresponding object.
  131. * @param {string} failedConstraintName
  132. * @param {Object} constraints
  133. * @returns {string|number}
  134. */
  135. function getResolutionFromFailedConstraint(failedConstraintName, constraints) {
  136. if (constraints && constraints.video && constraints.video.mandatory) {
  137. switch (failedConstraintName) {
  138. case 'width':
  139. return constraints.video.mandatory.minWidth;
  140. case 'height':
  141. return constraints.video.mandatory.minHeight;
  142. default:
  143. return constraints.video.mandatory[failedConstraintName] || '';
  144. }
  145. }
  146. return '';
  147. }
  148. export default JitsiTrackError;