@svta/common-media-library
Version:
A common library for media playback in JavaScript
64 lines • 2.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebVttTransformer = void 0;
const WebVttParser_js_1 = require("./WebVttParser.js");
const WebVttResultType_js_1 = require("./WebVttResultType.js");
/**
* WebVTT transform stream transformer.
*
* @group WebVTT
*
* @beta
*/
class WebVttTransformer {
/**
* Creates a new WebVTT transformer.
*/
constructor() {
this.results = [];
this.parser = new WebVttParser_js_1.WebVttParser();
this.parser.oncue = cue => this.results.push({ type: WebVttResultType_js_1.WebVttResultType.CUE, data: cue });
this.parser.onregion = region => this.results.push({ type: WebVttResultType_js_1.WebVttResultType.REGION, data: region });
this.parser.onstyle = style => this.results.push({ type: WebVttResultType_js_1.WebVttResultType.STYLE, data: style });
this.parser.ontimestampmap = timestampmap => this.results.push({ type: WebVttResultType_js_1.WebVttResultType.TIMESTAMP_MAP, data: timestampmap });
this.parser.onparsingerror = error => this.results.push({ type: WebVttResultType_js_1.WebVttResultType.ERROR, data: error });
}
enqueueResults(controller) {
// TODO: Should parse errors throw?
for (const result of this.results) {
controller.enqueue(result);
}
this.results = [];
}
/**
* Transforms a chunk of WebVTT data.
*
* @param chunk - The chunk of WebVTT data to transform.
* @param controller - The controller to enqueue the results to.
*/
transform(chunk, controller) {
try {
this.parser.parse(chunk);
this.enqueueResults(controller);
}
catch (error) {
controller.error(error);
}
}
/**
* Flushes the transformer.
*
* @param controller - The controller to enqueue the results to.
*/
flush(controller) {
try {
this.parser.flush();
this.enqueueResults(controller);
}
catch (error) {
controller.error(error);
}
}
}
exports.WebVttTransformer = WebVttTransformer;
//# sourceMappingURL=WebVttTransformer.js.map