mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 02:43:48 +02:00
* Add BufferTarget.onFinalize option for awaitable async action on finalize The `finalized` event fires synchronously and its return value is ignored, making it unsuitable for use cases where the muxer should wait (e.g. uploading the buffer to S3, R2, or other object stores that require a known Content-Length and therefore can't stream via StreamTarget). Adds a new `BufferTargetOptions` type with an `onFinalize` callback that the muxer awaits before resolving. Matches the existing callback pattern used by `HlsOutputFormatOptions.onSegment`, `onMaster`, etc. When used with PathedTarget, this provides proper backpressure: the next segment won't start being produced until the previous one has finished uploading, keeping memory bounded regardless of video length. - Adds `BufferTargetOptions` type, exported from the package root - `BufferTarget` constructor now accepts optional options (backward compatible) - `_finalize()` awaits `onFinalize` before emitting the `finalized` event - Adds runtime validation for non-function callbacks - Updates "Upload to a server" docs in writing-hls.md with S3 pattern - Documents `onFinalize` in writing-media-files.md BufferTarget section - Adds tests for callback invocation, async awaiting, backward compat, and validation * Add ConcurrentRunner, add OutputOptions.onFinalize, adjust docs accordingly, clean up tests * give me more control baby * forgot to tell about it * polish guide and js docs * vanilagy says remove this, docs will reveal it --------- Co-authored-by: Vanilagy <[email protected]>
37 lines
933 B
TypeScript
37 lines
933 B
TypeScript
import { expect, test } from 'vitest';
|
|
import { Output } from '../../src/output.js';
|
|
import { MkvOutputFormat } from '../../src/output-format.js';
|
|
import { BufferTarget } from '../../src/target.js';
|
|
import { EncodedVideoPacketSource } from '../../src/media-source.js';
|
|
import { EncodedPacket } from '../../src/packet.js';
|
|
|
|
test('Output, onFinalize', async () => {
|
|
let callCount = 0;
|
|
const output = new Output({
|
|
format: new MkvOutputFormat(),
|
|
target: new BufferTarget(),
|
|
onFinalize: async () => {
|
|
await new Promise(resolve => setTimeout(resolve, 200));
|
|
|
|
callCount++;
|
|
},
|
|
});
|
|
|
|
const source = new EncodedVideoPacketSource('avc');
|
|
output.addVideoTrack(source);
|
|
|
|
await output.start();
|
|
|
|
await source.add(new EncodedPacket(new Uint8Array(1024), 'key', 0, 0.5), {
|
|
decoderConfig: {
|
|
codec: 'avc1.640028',
|
|
codedWidth: 1920,
|
|
codedHeight: 1080,
|
|
},
|
|
});
|
|
|
|
await output.finalize();
|
|
|
|
expect(callCount).toBe(1);
|
|
});
|