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.ts 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import * as JitsiTrackErrors from './JitsiTrackErrors';
  2. export interface IGumError {
  3. constraint?: string;
  4. constraintName?: string;
  5. message?: string;
  6. name?: string;
  7. stack?: string;
  8. }
  9. export interface IVideoConstraints {
  10. mandatory?: { [key: string]: string | number; };
  11. }
  12. export interface IGumOptions {
  13. video?: IVideoConstraints;
  14. }
  15. export interface IGum {
  16. constraints?: IGumOptions | string;
  17. devices?: ('audio' | 'video' | 'desktop' | 'screen' | 'audiooutput')[];
  18. error: IGumError;
  19. }
  20. export type DeviceType = 'audio' | 'video' | 'desktop' | 'screen' | 'audiooutput';
  21. const TRACK_ERROR_TO_MESSAGE_MAP: { [key: string]: string; } = {
  22. [JitsiTrackErrors.UNSUPPORTED_RESOLUTION]: 'Video resolution is not supported: ',
  23. [JitsiTrackErrors.SCREENSHARING_USER_CANCELED]: 'User canceled screen sharing prompt',
  24. [JitsiTrackErrors.SCREENSHARING_GENERIC_ERROR]: 'Unknown error from screensharing',
  25. [JitsiTrackErrors.SCREENSHARING_NOT_SUPPORTED_ERROR]: 'Not supported',
  26. [JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_ERROR]: 'Unkown error from desktop picker',
  27. [JitsiTrackErrors.ELECTRON_DESKTOP_PICKER_NOT_FOUND]: 'Failed to detect desktop picker',
  28. [JitsiTrackErrors.GENERAL]: 'Generic getUserMedia error',
  29. [JitsiTrackErrors.PERMISSION_DENIED]: 'User denied permission to use device(s): ',
  30. [JitsiTrackErrors.NOT_FOUND]: 'Requested device(s) was/were not found: ',
  31. [JitsiTrackErrors.CONSTRAINT_FAILED]: 'Constraint could not be satisfied: ',
  32. [JitsiTrackErrors.TIMEOUT]: 'Could not start media source. Timeout occurred!',
  33. [JitsiTrackErrors.TRACK_IS_DISPOSED]: 'Track has been already disposed',
  34. [JitsiTrackErrors.TRACK_NO_STREAM_FOUND]: 'Track does not have an associated Media Stream'
  35. };
  36. /**
  37. *
  38. * Represents an error that occurred to a JitsiTrack. Can represent various
  39. * types of errors. For error descriptions (@see JitsiTrackErrors).
  40. *
  41. * @extends Error
  42. */
  43. export default class JitsiTrackError extends Error {
  44. public gum?: IGum;
  45. /**
  46. * @constructor
  47. * @param {IGumError|string} error - error object or error name
  48. * @param {IGumOptions|string} [options] - getUserMedia constraints object or error message
  49. * @param {DeviceType[]} [devices] - list of getUserMedia requested devices
  50. */
  51. constructor(
  52. error: IGumError | string,
  53. options?: IGumOptions | string,
  54. devices?: DeviceType[]
  55. ) {
  56. super();
  57. if (typeof error === 'object' && typeof error.name !== 'undefined') {
  58. /**
  59. * Additional information about original getUserMedia error
  60. * and constraints.
  61. * @type {IGum}
  62. */
  63. this.gum = {
  64. error,
  65. constraints: options,
  66. devices: devices && Array.isArray(devices) ? devices.slice(0) : undefined
  67. };
  68. switch (error.name) {
  69. case 'NotAllowedError':
  70. case 'PermissionDeniedError':
  71. case 'SecurityError':
  72. this.name = JitsiTrackErrors.PERMISSION_DENIED;
  73. this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name] + (this.gum.devices || []).join(', ');
  74. break;
  75. case 'DevicesNotFoundError':
  76. case 'NotFoundError':
  77. this.name = JitsiTrackErrors.NOT_FOUND;
  78. this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name] + (this.gum.devices || []).join(', ');
  79. break;
  80. case 'ConstraintNotSatisfiedError':
  81. case 'OverconstrainedError': {
  82. const constraintName = error.constraintName || error.constraint;
  83. // we treat deviceId as unsupported resolution, as we want to
  84. // retry and finally if everything fails to remove deviceId from
  85. // mandatory constraints
  86. if (typeof options !== 'string'
  87. && options?.video
  88. && (!devices || devices.indexOf('video') > -1)
  89. && (constraintName === 'minWidth'
  90. || constraintName === 'maxWidth'
  91. || constraintName === 'minHeight'
  92. || constraintName === 'maxHeight'
  93. || constraintName === 'width'
  94. || constraintName === 'height'
  95. || constraintName === 'deviceId')) {
  96. this.name = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
  97. this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name]
  98. + this.getResolutionFromFailedConstraint(constraintName, options);
  99. } else {
  100. this.name = JitsiTrackErrors.CONSTRAINT_FAILED;
  101. this.message = TRACK_ERROR_TO_MESSAGE_MAP[this.name] + error.constraintName;
  102. }
  103. break;
  104. }
  105. default:
  106. this.name = JitsiTrackErrors.GENERAL;
  107. this.message = error.message || TRACK_ERROR_TO_MESSAGE_MAP[this.name];
  108. break;
  109. }
  110. } else if (typeof error === 'string') {
  111. if (TRACK_ERROR_TO_MESSAGE_MAP[error]) {
  112. this.name = error;
  113. this.message = typeof options === 'string' ? options : TRACK_ERROR_TO_MESSAGE_MAP[error];
  114. } else {
  115. // this is some generic error that do not fit any of our
  116. // pre-defined errors, so don't give it any specific name, just
  117. // store message
  118. this.message = error;
  119. }
  120. } else {
  121. throw new Error('Invalid arguments');
  122. }
  123. this.stack = typeof error === 'string' ? new Error().stack : error.stack;
  124. }
  125. /**
  126. * Gets failed resolution constraint from corresponding object.
  127. * @param failedConstraintName - The name of the failed constraint
  128. * @param constraints - The constraints object
  129. * @returns The resolution value or empty string
  130. */
  131. private getResolutionFromFailedConstraint(failedConstraintName: string, constraints: IGumOptions): string | number {
  132. if (constraints?.video?.mandatory) {
  133. switch (failedConstraintName) {
  134. case 'width':
  135. return constraints.video.mandatory.minWidth;
  136. case 'height':
  137. return constraints.video.mandatory.minHeight;
  138. default:
  139. return constraints.video.mandatory[failedConstraintName] || '';
  140. }
  141. }
  142. return '';
  143. }
  144. }