Make samples disposable

This commit is contained in:
Vanilagy
2025-09-24 21:41:52 +02:00
parent fa4e064345
commit a043f410fd
4 changed files with 25 additions and 8 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ Here's a long list of stuff this library does:
- Converting media files
- Hardware-accelerated decoding & encoding (via the WebCodecs API)
- Support for multiple video, audio and subtitle tracks
- Read & write support for many container formats (.mp4, .mov, .webm, .mkv, .mp3, .wav, .ogg, .aac, .flac), including variations such as MP4 with Fast Start, fragmented MP4, or streamable Matroska
- Read & write support for many container formats (.mp4, .mov, .webm, .mkv, .mp3, .wav, .ogg, .aac, .flac), including variations such as MP4 with Fast Start, fragmented MP4, streamable Matroska, transparent WebM, etc.
- Support for 25 different codecs
- Lazy, optimized, on-demand file reading
- Input and output streaming, arbitrary file size support
+3 -5
View File
@@ -8,10 +8,12 @@
import { Demuxer } from './demuxer';
import { InputFormat } from './input-format';
import { assert } from './misc';
import { assert, polyfillSymbolDispose } from './misc';
import { Reader } from './reader';
import { Source } from './source';
polyfillSymbolDispose();
/**
* The options for creating an Input object.
* @group Input files & tracks
@@ -24,10 +26,6 @@ export type InputOptions<S extends Source = Source> = {
source: S;
};
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
// @ts-expect-error Readonly
Symbol.dispose ??= Symbol('Symbol.dispose');
/**
* Represents an input media file. This is the root object from which all media read operations start.
* @group Input files & tracks
+6
View File
@@ -780,3 +780,9 @@ export const uint8ArraysAreEqual = (a: Uint8Array, b: Uint8Array) => {
return true;
};
export const polyfillSymbolDispose = () => {
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
// @ts-expect-error Readonly
Symbol.dispose ??= Symbol('Symbol.dispose');
};
+15 -2
View File
@@ -16,8 +16,11 @@ import {
toUint8Array,
SetRequired,
isFirefox,
polyfillSymbolDispose,
} from './misc';
polyfillSymbolDispose();
/**
* Metadata used for VideoSample initialization.
* @group Samples
@@ -49,7 +52,7 @@ export type VideoSampleInit = {
* @group Samples
* @public
*/
export class VideoSample {
export class VideoSample implements Disposable {
/** @internal */
_data!: VideoFrame | OffscreenCanvas | Uint8Array | null;
/** @internal */
@@ -736,6 +739,11 @@ export class VideoSample {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
(this.duration as number) = newDuration;
}
/** Calls `.close()`. */
[Symbol.dispose]() {
this.close();
}
}
const isVideoFrame = (x: unknown): x is VideoFrame => {
@@ -840,7 +848,7 @@ export type AudioSampleCopyToOptions = {
* @group Samples
* @public
*/
export class AudioSample {
export class AudioSample implements Disposable {
/** @internal */
_data: AudioData | Uint8Array;
/** @internal */
@@ -1276,6 +1284,11 @@ export class AudioSample {
(this.timestamp as number) = newTimestamp;
}
/** Calls `.close()`. */
[Symbol.dispose]() {
this.close();
}
/** @internal */
static* _fromAudioBuffer(audioBuffer: AudioBuffer, timestamp: number) {
if (!(audioBuffer instanceof AudioBuffer)) {