Bubble encoder errors to consumer

This commit is contained in:
David Payr
2024-12-29 12:58:24 +01:00
parent f70142e25b
commit 49b36f735f
7 changed files with 46 additions and 40 deletions
+10 -3
View File
@@ -113,6 +113,7 @@ export type VideoCodecConfig = {
codec: VideoCodec;
bitrate: number;
latencyMode?: VideoEncoderConfig['latencyMode'];
onEncodingError?: (error: Error) => void;
};
const validateVideoCodecConfig = (config: VideoCodecConfig) => {
@@ -128,6 +129,9 @@ const validateVideoCodecConfig = (config: VideoCodecConfig) => {
if (config.latencyMode !== undefined && !['quality', 'realtime'].includes(config.latencyMode)) {
throw new TypeError('config.latencyMode, when provided, must be \'quality\' or \'realtime\'.');
}
if (config.onEncodingError !== undefined && typeof config.onEncodingError !== 'function') {
throw new TypeError('config.onEncodingError, when provided, must be a function.');
}
};
class VideoEncoderWrapper {
@@ -186,7 +190,7 @@ class VideoEncoderWrapper {
this.encoder = new VideoEncoder({
output: (chunk, meta) => void this.muxer!.addEncodedVideoChunk(this.source._connectedTrack!, chunk, meta),
error: error => console.error('Video encode error:', error),
error: this.codecConfig.onEncodingError ?? (error => console.error('VideoEncoder error:', error)),
});
this.encoder.configure({
@@ -382,6 +386,7 @@ export class EncodedAudioChunkSource extends AudioSource {
export type AudioCodecConfig = {
codec: AudioCodec;
bitrate: number;
onEncodingError?: (error: Error) => void;
};
const validateAudioCodecConfig = (config: AudioCodecConfig) => {
@@ -394,6 +399,9 @@ const validateAudioCodecConfig = (config: AudioCodecConfig) => {
if (!Number.isInteger(config.bitrate) || config.bitrate <= 0) {
throw new TypeError('config.bitrate must be a positive integer.');
}
if (config.onEncodingError !== undefined && typeof config.onEncodingError !== 'function') {
throw new TypeError('config.onEncodingError, when provided, must be a function.');
}
};
class AudioEncoderWrapper {
@@ -445,7 +453,7 @@ class AudioEncoderWrapper {
this.encoder = new AudioEncoder({
output: (chunk, meta) => void this.muxer!.addEncodedAudioChunk(this.source._connectedTrack!, chunk, meta),
error: error => console.error('Audio encode error:', error),
error: this.codecConfig.onEncodingError ?? (error => console.error('AudioEncoder error:', error)),
});
this.encoder.configure({
@@ -629,7 +637,6 @@ export class TextSubtitleSource extends SubtitleSource {
codec,
output: (cue, metadata) =>
this._connectedTrack?.output._muxer.addSubtitleCue(this._connectedTrack, cue, metadata),
error: error => console.error('Subtitle parse error:', error),
});
}
+2 -7
View File
@@ -18,7 +18,6 @@ export type SubtitleMetadata = {
type SubtitleParserOptions = {
codec: 'webvtt';
output: (cue: SubtitleCue, metadata: SubtitleMetadata) => unknown;
error: (error: Error) => unknown;
};
const cueBlockHeaderRegex = /(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g;
@@ -42,18 +41,14 @@ export class SubtitleParser {
if (!this.preambleText) {
if (!preambleStartRegex.test(text)) {
const error = new Error('WebVTT preamble incorrect.');
this.options.error(error);
throw error;
throw new Error('WebVTT preamble incorrect.');
}
match = cueBlockHeaderRegex.exec(text);
const preamble = text.slice(0, match?.index ?? text.length).trimEnd();
if (!preamble) {
const error = new Error('No WebVTT preamble provided.');
this.options.error(error);
throw error;
throw new Error('No WebVTT preamble provided.');
}
this.preambleText = preamble;