diff --git a/src/ogg/ogg-muxer.ts b/src/ogg/ogg-muxer.ts index a198f80..9fdb58b 100644 --- a/src/ogg/ogg-muxer.ts +++ b/src/ogg/ogg-muxer.ts @@ -55,6 +55,8 @@ export class OggMuxer extends Muxer { this.format = format; this.writer = output._writer; + + this.writer.ensureMonotonicity = true; // Ogg is always monotonically written! } async start() { diff --git a/src/target.ts b/src/target.ts index 90059b3..09f7b1d 100644 --- a/src/target.ts +++ b/src/target.ts @@ -83,8 +83,8 @@ export class StreamTarget extends Target { if (options.chunked !== undefined && typeof options.chunked !== 'boolean') { throw new TypeError('options.chunked, when provided, must be a boolean.'); } - if (options.chunkSize !== undefined && (!Number.isInteger(options.chunkSize) || options.chunkSize <= 0)) { - throw new TypeError('options.chunkSize, when provided, must be a positive integer.'); + if (options.chunkSize !== undefined && (!Number.isInteger(options.chunkSize) || options.chunkSize < 1024)) { + throw new TypeError('options.chunkSize, when provided, must be an integer and not smaller than 1024.'); } this._writable = writable; diff --git a/src/writer.ts b/src/writer.ts index ae7fd1e..1e69d75 100644 --- a/src/writer.ts +++ b/src/writer.ts @@ -305,10 +305,6 @@ export class StreamTargetWriter extends Writer { } } - if (this.ensureMonotonicity && chunk.start !== this.lastFlushEnd) { - throw new Error('Internal error: Monotonicity violation.'); - } - if (this.writer.desiredSize !== null && this.writer.desiredSize <= 0) { await this.writer.ready; // Allow the writer to apply backpressure } @@ -318,15 +314,19 @@ export class StreamTargetWriter extends Writer { this.writeDataIntoChunks(chunk.data, chunk.start); this.tryToFlushChunks(); } else { + if (this.ensureMonotonicity && chunk.start !== this.lastFlushEnd) { + throw new Error('Internal error: Monotonicity violation.'); + } + // Write out the data immediately void this.writer.write({ type: 'write', data: chunk.data, position: chunk.start, }); - } - this.lastFlushEnd = chunk.start + chunk.data.byteLength; + this.lastFlushEnd = chunk.start + chunk.data.byteLength; + } } this.sections.length = 0; @@ -420,11 +420,18 @@ export class StreamTargetWriter extends Writer { if (!chunk.shouldFlush && !force) continue; for (const section of chunk.written) { + const position = chunk.start + section.start; + if (this.ensureMonotonicity && position !== this.lastFlushEnd) { + throw new Error('Internal error: Monotonicity violation.'); + } + void this.writer.write({ type: 'write', data: chunk.data.subarray(section.start, section.end), - position: chunk.start + section.start, + position, }); + + this.lastFlushEnd = chunk.start + section.end; } this.chunks.splice(i--, 1);