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