From 6f020650cc1f08c78fff3e1086481dc1e7ec1fd0 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Wed, 4 Jun 2025 15:39:36 +0200 Subject: [PATCH] Make MediaSource.close return void --- docs/guide/media-sources.md | 4 ++-- .../procedural-generation/procedural-generation.ts | 4 ++-- src/conversion.ts | 10 +++++----- src/media-source.ts | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/guide/media-sources.md b/docs/guide/media-sources.md index 59c313b..43e6941 100644 --- a/docs/guide/media-sources.md +++ b/docs/guide/media-sources.md @@ -19,7 +19,7 @@ await mediaSource.add(...); When you're done using the source, meaning no additional media data will be added, it's best to close the source as soon as possible: ```ts -void mediaSource.close(); +mediaSource.close(); ``` Closing sources manually is _technically_ not required and will happen automatically when finalizing the `Output`. However, if your `Output` has multiple tracks and not all of them finish supplying their data at the same time (for example, adding all audio first and then all video), closing sources early will improve performance and lower memory usage. This is because the `Output` can better "plan ahead", knowing it doesn't have to wait for certain tracks anymore (see [Packet buffering](./writing-overview#packet-buffering)). Therefore, it is good practice to always manually close all media sources as soon as you are done using them. @@ -399,7 +399,7 @@ await textSource.add(text); If you add the entire subtitle file at once, make sure to [close the source](#closing-sources) immediately after: ```ts -void textSource.close(); +textSource.close(); ``` You can also add cues individually in small chunks: diff --git a/examples/procedural-generation/procedural-generation.ts b/examples/procedural-generation/procedural-generation.ts index 719305a..bef6342 100644 --- a/examples/procedural-generation/procedural-generation.ts +++ b/examples/procedural-generation/procedural-generation.ts @@ -128,13 +128,13 @@ const generateVideo = async () => { } // Signal to the output that no more video frames are coming (not necessary, but recommended) - void canvasSource.close(); + canvasSource.close(); // Let's render the audio. Ideally, the audio is rendered before the video (or concurrently to it), but for // simplicity, we're rendering it after we've cranked through all frames. const audioBuffer = await offlineAudioContext.startRendering(); await audioBufferSource.add(audioBuffer); - void audioBufferSource.close(); + audioBufferSource.close(); clearInterval(progressInterval); diff --git a/src/conversion.ts b/src/conversion.ts index b488336..3fb7608 100644 --- a/src/conversion.ts +++ b/src/conversion.ts @@ -499,7 +499,7 @@ export class Conversion { this._reportProgress(track.id, packet.timestamp + packet.duration); } - await source.close(); + source.close(); this._synchronizer.closeTrack(track.id); })()); } else { @@ -593,7 +593,7 @@ export class Conversion { sample.close(); } - await source.close(); + source.close(); this._synchronizer.closeTrack(track.id); })()); } @@ -670,7 +670,7 @@ export class Conversion { this._reportProgress(track.id, packet.timestamp + packet.duration); } - await source.close(); + source.close(); this._synchronizer.closeTrack(track.id); })()); } else { @@ -763,7 +763,7 @@ export class Conversion { sample.close(); } - await source.close(); + source.close(); this._synchronizer.closeTrack(track.id); })()); } @@ -822,7 +822,7 @@ export class Conversion { await resampler.finalize(); - await source.close(); + source.close(); this._synchronizer.closeTrack(track.id); })()); diff --git a/src/media-source.ts b/src/media-source.ts index 009fd7d..be0998e 100644 --- a/src/media-source.ts +++ b/src/media-source.ts @@ -81,7 +81,7 @@ export abstract class MediaSource { */ close() { if (this._closingPromise) { - throw new Error('Source already closed.'); + return; } const connectedTrack = this._connectedTrack; @@ -94,7 +94,7 @@ export abstract class MediaSource { throw new Error('Cannot call close before output has been started.'); } - return this._closingPromise = (async () => { + this._closingPromise = (async () => { await this._flushAndClose(); this._closed = true;