diff --git a/docs/guide/writing-media-files.md b/docs/guide/writing-media-files.md index 384ce9a..5e58081 100644 --- a/docs/guide/writing-media-files.md +++ b/docs/guide/writing-media-files.md @@ -197,7 +197,18 @@ output.state; // => 'pending' | 'started' | 'canceled' | 'finalizing' | 'finaliz ## Output targets -The _output target_ determines where the data created by the `Output` will be written. This library offers two targets: +The _output target_ determines where the data created by the `Output` will be written. This library offers a couple of targets. + +--- + +All targets have an optional `onwrite` callback you can set to monitor which byte regions are being written to: +```ts +target.onwrite = (start, end) => { + // ... +}; +``` + +You can use this to track the size of the output file as it grows. But be warned, this function is chatty and gets called *extremely* frequently. ### `BufferTarget` diff --git a/src/source.ts b/src/source.ts index a262bf6..1b3d41a 100644 --- a/src/source.ts +++ b/src/source.ts @@ -63,7 +63,7 @@ export abstract class Source { return result; } - /** Called each time data is retrieved from the source. Will be called with the retrieved range. */ + /** Called each time data is retrieved from the source. Will be called with the retrieved range (end exclusive). */ onread: ((start: number, end: number) => unknown) | null = null; } diff --git a/src/target.ts b/src/target.ts index 91d9960..03366bd 100644 --- a/src/target.ts +++ b/src/target.ts @@ -19,6 +19,14 @@ export abstract class Target { /** @internal */ abstract _createWriter(): Writer; + + /** + * Called each time data is written to the target. Will be called with the byte range into which data was written. + * + * Use this callback to track the size of the output file as it grows. But be warned, this function is chatty and + * gets called *extremely* often. + */ + onwrite: ((start: number, end: number) => unknown) | null = null; } /** @@ -113,6 +121,6 @@ export class StreamTarget extends Target { export class NullTarget extends Target { /** @internal */ _createWriter() { - return new NullTargetWriter(); + return new NullTargetWriter(this); } } diff --git a/src/writer.ts b/src/writer.ts index 5f2b8f7..30de003 100644 --- a/src/writer.ts +++ b/src/writer.ts @@ -6,7 +6,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { BufferTarget, StreamTarget, StreamTargetChunk } from './target'; +import { BufferTarget, NullTarget, StreamTarget, StreamTargetChunk } from './target'; import { assert } from './misc'; export abstract class Writer { @@ -156,8 +156,9 @@ export class BufferTargetWriter extends Writer { this.ensureSize(this.pos + data.byteLength); this.bytes.set(data, this.pos); - this.pos += data.byteLength; + this.target.onwrite?.(this.pos, this.pos + data.byteLength); + this.pos += data.byteLength; this.maxPos = Math.max(this.maxPos, this.pos); } @@ -250,6 +251,8 @@ export class StreamTargetWriter extends Writer { data: data.slice(), start: this.pos, }); + this.target.onwrite?.(this.pos, this.pos + data.byteLength); + this.pos += data.byteLength; this.lastWriteEnd = Math.max(this.lastWriteEnd, this.pos); @@ -463,8 +466,13 @@ export class StreamTargetWriter extends Writer { export class NullTargetWriter extends Writer { private pos = 0; + constructor(private target: NullTarget) { + super(); + } + write(data: Uint8Array) { this.maybeTrackWrites(data); + this.target.onwrite?.(this.pos, this.pos + data.byteLength); this.pos += data.byteLength; }