Open source CapCut alternative.
TypeScript
fix(site): close a disposed input's queued frames [rebuild]
David Lea9d180469089
diff --git a/site/patches/README.md b/site/patches/README.mdindex a9aa380a..6deeaaa2 100644--- a/site/patches/README.md+++ b/site/patches/README.md@@ -14,9 +14,9 @@ about 40 decoded frames, around a gigabyte per stream at 4K.The patch gives `VideoDecoderWrapper` an `inFlight` count (packets submittedminus frames output, reset on flush), reports it as the decoder's queue size,-and lowers the head start to 16. A read that ends in an error wakes the parked-pump, so the failed decoder closes and frees its frames. The edits land in the-ES module and all three bundles.+and lowers the head start to 16. A read that fails, or whose input is disposed,+wakes the parked pump, which closes its decoder and queued frames. The edits+land in the ES module and all three bundles.Delete this patch when upstream's pump counts frames in flight itself: check`computeMaxQueueSize` and the pump loops around `getDecodeQueueSize()` indiff --git a/site/patches/mediabunny+1.61.0.patch b/site/patches/mediabunny+1.61.0.patchindex 62c1ab90..5d261ade 100644--- a/site/patches/mediabunny+1.61.0.patch+++ b/site/patches/mediabunny+1.61.0.patch@@ -1,5 +1,5 @@diff --git a/node_modules/mediabunny/dist/bundles/mediabunny.cjs b/node_modules/mediabunny/dist/bundles/mediabunny.cjs-index f984c84..aa1fb83 100644+index f984c84..cf6c6e2 100644--- a/node_modules/mediabunny/dist/bundles/mediabunny.cjs+++ b/node_modules/mediabunny/dist/bundles/mediabunny.cjs@@ -12381,6 +12381,21 @@ var Mediabunny = (() => {@@ -24,7 +24,39 @@ index f984c84..aa1fb83 100644}}return;-@@ -24437,6 +24452,9 @@ var Mediabunny = (() => {+@@ -24351,13 +24366,17 @@ var Mediabunny = (() => {+ verifyKeyPackets: true,+ metadataOnly: false+ };++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+ if (sample.timestamp >= endTimestamp) {+ ended = true;+ }+- if (ended) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (ended || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -24422,6 +24441,12 @@ var Mediabunny = (() => {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -24437,6 +24462,9 @@ var Mediabunny = (() => {terminated = true;ended = true;closeSamples();@@ -34,7 +66,7 @@ index f984c84..aa1fb83 100644throw new InputDisposedError();} else if (terminated) {return { value: void 0, done: true };-@@ -24444,6 +24462,9 @@ var Mediabunny = (() => {+@@ -24444,6 +24472,9 @@ var Mediabunny = (() => {terminated = true;ended = true;closeSamples();@@ -44,7 +76,50 @@ index f984c84..aa1fb83 100644throw outOfBandError;} else if (sampleQueue.length > 0) {const value = sampleQueue.shift();-@@ -24608,12 +24629,18 @@ var Mediabunny = (() => {+@@ -24495,10 +24526,14 @@ var Mediabunny = (() => {+ verifyKeyPackets: true,+ metadataOnly: false+ };++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+- if (terminated) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (terminated || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -24532,11 +24567,11 @@ var Mediabunny = (() => {+ decoder.decode(currentPacket);+ while (currentPacket.sequenceNumber < maxSequenceNumber) {+ const maxQueueSize = computeMaxQueueSize(sampleQueue.length);+- while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated) {++ while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated && !this._track.input._disposed) {+ ({ promise: queueDequeue, resolve: onQueueDequeue } = promiseWithResolvers());+ await queueDequeue;+ }+- if (terminated) {++ if (terminated || this._track.input._disposed) {+ break;+ }+ const nextPacket = await packetSink.getNextPacket(currentPacket, retrievalOptions);+@@ -24595,6 +24630,12 @@ var Mediabunny = (() => {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -24608,12 +24649,18 @@ var Mediabunny = (() => {if (track.input._disposed) {terminated = true;closeSamples();@@ -63,7 +138,7 @@ index f984c84..aa1fb83 100644throw outOfBandError;} else if (sampleQueue.length > 0) {const value = sampleQueue.shift();-@@ -24644,11 +24671,13 @@ var Mediabunny = (() => {+@@ -24644,11 +24691,13 @@ var Mediabunny = (() => {}};var computeMaxQueueSize = (decodedSampleQueueSize) => {@@ -79,7 +154,7 @@ index f984c84..aa1fb83 100644this.codec = codec;this.decoderConfig = decoderConfig;this.rotation = rotation;-@@ -24658,6 +24687,13 @@ var Mediabunny = (() => {+@@ -24658,6 +24707,13 @@ var Mediabunny = (() => {this.customDecoder = null;this.customDecoderCallSerializer = new CallSerializer();this.customDecoderQueueSize = 0;@@ -93,7 +168,7 @@ index f984c84..aa1fb83 100644this.inputTimestamps = [];// Timestamps input into the decoder, sorted.this.sampleQueue = [];-@@ -24689,6 +24725,17 @@ var Mediabunny = (() => {+@@ -24689,6 +24745,17 @@ var Mediabunny = (() => {if (!(sample instanceof VideoSample)) {throw new TypeError("The argument passed to onSample must be a VideoSample.");}@@ -111,7 +186,7 @@ index f984c84..aa1fb83 100644this.finalizeAndEmitSample(sample);};this.customDecoder.onError = (error) => {-@@ -24744,6 +24791,7 @@ var Mediabunny = (() => {+@@ -24744,6 +24811,7 @@ var Mediabunny = (() => {const stack = new Error("Decoding error").stack;this.decoder = new VideoDecoder({output: (frame) => {@@ -119,7 +194,7 @@ index f984c84..aa1fb83 100644try {colorHandler(frame);} catch (error) {-@@ -24760,13 +24808,10 @@ var Mediabunny = (() => {+@@ -24760,13 +24828,10 @@ var Mediabunny = (() => {}getDecodeQueueSize() {if (this.customDecoder) {@@ -135,7 +210,7 @@ index f984c84..aa1fb83 100644}}decode(packet) {-@@ -24824,6 +24869,13 @@ var Mediabunny = (() => {+@@ -24824,6 +24889,13 @@ var Mediabunny = (() => {}}}@@ -149,7 +224,7 @@ index f984c84..aa1fb83 100644this.decoder.decode(packet.toEncodedVideoChunk());this.decodeAlphaData(packet);}-@@ -24940,6 +24992,49 @@ var Mediabunny = (() => {+@@ -24940,6 +25012,49 @@ var Mediabunny = (() => {sample.setFlip(this.flip);this.onSample(sample);}@@ -199,7 +274,7 @@ index f984c84..aa1fb83 100644async mergeAlpha(color, alpha) {const resolver = promiseWithResolvers();this.mergeAlphaPromises.push(resolver.promise);-@@ -24968,6 +25063,11 @@ var Mediabunny = (() => {+@@ -24968,6 +25083,11 @@ var Mediabunny = (() => {async flush() {if (this.customDecoder) {await this.customDecoderCallSerializer.call(() => this.customDecoder.flush());@@ -211,7 +286,7 @@ index f984c84..aa1fb83 100644} else {assert(this.decoder);await Promise.all([-@@ -24992,6 +25092,7 @@ var Mediabunny = (() => {+@@ -24992,6 +25112,7 @@ var Mediabunny = (() => {}this.sampleQueue.length = 0;}@@ -219,7 +294,7 @@ index f984c84..aa1fb83 100644this.currentPacketIndex = 0;this.raslSkipped = false;}-@@ -25248,6 +25349,18 @@ var Mediabunny = (() => {+@@ -25248,6 +25369,18 @@ var Mediabunny = (() => {if (decoderOptions.optimizeForLatency !== void 0 && typeof decoderOptions.optimizeForLatency !== "boolean") {throw new TypeError("decoderOptions.optimizeForLatency, when provided, must be a boolean.");}@@ -238,7 +313,7 @@ index f984c84..aa1fb83 100644};var VideoSampleSink = class extends BaseMediaSampleSink {/** Creates a new {@link VideoSampleSink} for the given {@link InputVideoTrack}. */-@@ -25276,12 +25389,19 @@ var Mediabunny = (() => {+@@ -25276,12 +25409,19 @@ var Mediabunny = (() => {let decoderConfig = await this._track.getDecoderConfig();const timeResolution = await this._track.getTimeResolution();assert(codec && decoderConfig);@@ -260,8 +335,19 @@ index f984c84..aa1fb83 100644}/** @internal */_createPacketSink() {+@@ -27342,6 +27482,10 @@ var Mediabunny = (() => {+ return;+ }+ this._disposed = true;++ // Wake the decode pumps parked on this input (Donkey patch): each unwinds and closes what it queued.++ for (const wake of this._disposeWakers ?? []) {++ wake();++ }+ for (const ref of this._sourceRefs) {+ ref.free();+ }diff --git a/node_modules/mediabunny/dist/bundles/mediabunny.mjs b/node_modules/mediabunny/dist/bundles/mediabunny.mjs-index e675b1f..693fc15 100644+index e675b1f..b12529a 100644--- a/node_modules/mediabunny/dist/bundles/mediabunny.mjs+++ b/node_modules/mediabunny/dist/bundles/mediabunny.mjs@@ -12224,6 +12224,21 @@ var Mp3Demuxer = class extends Demuxer {@@ -286,7 +372,39 @@ index e675b1f..693fc15 100644}}return;-@@ -24280,6 +24295,9 @@ var BaseMediaSampleSink = class {+@@ -24194,13 +24209,17 @@ var BaseMediaSampleSink = class {+ verifyKeyPackets: true,+ metadataOnly: false+ };++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+ if (sample.timestamp >= endTimestamp) {+ ended = true;+ }+- if (ended) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (ended || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -24265,6 +24284,12 @@ var BaseMediaSampleSink = class {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -24280,6 +24305,9 @@ var BaseMediaSampleSink = class {terminated = true;ended = true;closeSamples();@@ -296,7 +414,7 @@ index e675b1f..693fc15 100644throw new InputDisposedError();} else if (terminated) {return { value: void 0, done: true };-@@ -24287,6 +24305,9 @@ var BaseMediaSampleSink = class {+@@ -24287,6 +24315,9 @@ var BaseMediaSampleSink = class {terminated = true;ended = true;closeSamples();@@ -306,7 +424,50 @@ index e675b1f..693fc15 100644throw outOfBandError;} else if (sampleQueue.length > 0) {const value = sampleQueue.shift();-@@ -24451,12 +24472,18 @@ var BaseMediaSampleSink = class {+@@ -24338,10 +24369,14 @@ var BaseMediaSampleSink = class {+ verifyKeyPackets: true,+ metadataOnly: false+ };++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+- if (terminated) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (terminated || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -24375,11 +24410,11 @@ var BaseMediaSampleSink = class {+ decoder.decode(currentPacket);+ while (currentPacket.sequenceNumber < maxSequenceNumber) {+ const maxQueueSize = computeMaxQueueSize(sampleQueue.length);+- while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated) {++ while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated && !this._track.input._disposed) {+ ({ promise: queueDequeue, resolve: onQueueDequeue } = promiseWithResolvers());+ await queueDequeue;+ }+- if (terminated) {++ if (terminated || this._track.input._disposed) {+ break;+ }+ const nextPacket = await packetSink.getNextPacket(currentPacket, retrievalOptions);+@@ -24438,6 +24473,12 @@ var BaseMediaSampleSink = class {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -24451,12 +24492,18 @@ var BaseMediaSampleSink = class {if (track.input._disposed) {terminated = true;closeSamples();@@ -325,7 +486,7 @@ index e675b1f..693fc15 100644throw outOfBandError;} else if (sampleQueue.length > 0) {const value = sampleQueue.shift();-@@ -24487,11 +24514,13 @@ var BaseMediaSampleSink = class {+@@ -24487,11 +24534,13 @@ var BaseMediaSampleSink = class {}};var computeMaxQueueSize = (decodedSampleQueueSize) => {@@ -341,7 +502,7 @@ index e675b1f..693fc15 100644this.codec = codec;this.decoderConfig = decoderConfig;this.rotation = rotation;-@@ -24501,6 +24530,13 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24501,6 +24550,13 @@ var VideoDecoderWrapper = class extends DecoderWrapper {this.customDecoder = null;this.customDecoderCallSerializer = new CallSerializer();this.customDecoderQueueSize = 0;@@ -355,7 +516,7 @@ index e675b1f..693fc15 100644this.inputTimestamps = [];// Timestamps input into the decoder, sorted.this.sampleQueue = [];-@@ -24532,6 +24568,17 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24532,6 +24588,17 @@ var VideoDecoderWrapper = class extends DecoderWrapper {if (!(sample instanceof VideoSample)) {throw new TypeError("The argument passed to onSample must be a VideoSample.");}@@ -373,7 +534,7 @@ index e675b1f..693fc15 100644this.finalizeAndEmitSample(sample);};this.customDecoder.onError = (error) => {-@@ -24587,6 +24634,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24587,6 +24654,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {const stack = new Error("Decoding error").stack;this.decoder = new VideoDecoder({output: (frame) => {@@ -381,7 +542,7 @@ index e675b1f..693fc15 100644try {colorHandler(frame);} catch (error) {-@@ -24603,13 +24651,10 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24603,13 +24671,10 @@ var VideoDecoderWrapper = class extends DecoderWrapper {}getDecodeQueueSize() {if (this.customDecoder) {@@ -397,7 +558,7 @@ index e675b1f..693fc15 100644}}decode(packet) {-@@ -24667,6 +24712,13 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24667,6 +24732,13 @@ var VideoDecoderWrapper = class extends DecoderWrapper {}}}@@ -411,7 +572,7 @@ index e675b1f..693fc15 100644this.decoder.decode(packet.toEncodedVideoChunk());this.decodeAlphaData(packet);}-@@ -24783,6 +24835,49 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24783,6 +24855,49 @@ var VideoDecoderWrapper = class extends DecoderWrapper {sample.setFlip(this.flip);this.onSample(sample);}@@ -461,7 +622,7 @@ index e675b1f..693fc15 100644async mergeAlpha(color, alpha) {const resolver = promiseWithResolvers();this.mergeAlphaPromises.push(resolver.promise);-@@ -24811,6 +24906,11 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24811,6 +24926,11 @@ var VideoDecoderWrapper = class extends DecoderWrapper {async flush() {if (this.customDecoder) {await this.customDecoderCallSerializer.call(() => this.customDecoder.flush());@@ -473,7 +634,7 @@ index e675b1f..693fc15 100644} else {assert(this.decoder);await Promise.all([-@@ -24835,6 +24935,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {+@@ -24835,6 +24955,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {}this.sampleQueue.length = 0;}@@ -481,7 +642,7 @@ index e675b1f..693fc15 100644this.currentPacketIndex = 0;this.raslSkipped = false;}-@@ -25091,6 +25192,18 @@ var validateVideoSinkDecoderOptions = (decoderOptions) => {+@@ -25091,6 +25212,18 @@ var validateVideoSinkDecoderOptions = (decoderOptions) => {if (decoderOptions.optimizeForLatency !== void 0 && typeof decoderOptions.optimizeForLatency !== "boolean") {throw new TypeError("decoderOptions.optimizeForLatency, when provided, must be a boolean.");}@@ -500,7 +661,7 @@ index e675b1f..693fc15 100644};var VideoSampleSink = class extends BaseMediaSampleSink {/** Creates a new {@link VideoSampleSink} for the given {@link InputVideoTrack}. */-@@ -25119,12 +25232,19 @@ var VideoSampleSink = class extends BaseMediaSampleSink {+@@ -25119,12 +25252,19 @@ var VideoSampleSink = class extends BaseMediaSampleSink {let decoderConfig = await this._track.getDecoderConfig();const timeResolution = await this._track.getTimeResolution();assert(codec && decoderConfig);@@ -522,8 +683,19 @@ index e675b1f..693fc15 100644}/** @internal */_createPacketSink() {+@@ -27185,6 +27325,10 @@ var Input = class _Input extends EventEmitter {+ return;+ }+ this._disposed = true;++ // Wake the decode pumps parked on this input (Donkey patch): each unwinds and closes what it queued.++ for (const wake of this._disposeWakers ?? []) {++ wake();++ }+ for (const ref of this._sourceRefs) {+ ref.free();+ }diff --git a/node_modules/mediabunny/dist/bundles/mediabunny.node.cjs b/node_modules/mediabunny/dist/bundles/mediabunny.node.cjs-index c828e19..a1c28cc 100644+index c828e19..26b13d6 100644--- a/node_modules/mediabunny/dist/bundles/mediabunny.node.cjs+++ b/node_modules/mediabunny/dist/bundles/mediabunny.node.cjs@@ -12378,6 +12378,21 @@ var Mediabunny = (() => {@@ -548,7 +720,39 @@ index c828e19..a1c28cc 100644}}return;-@@ -24440,6 +24455,9 @@ var Mediabunny = (() => {+@@ -24354,13 +24369,17 @@ var Mediabunny = (() => {+ verifyKeyPackets: true,+ metadataOnly: false+ };++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+ if (sample.timestamp >= endTimestamp) {+ ended = true;+ }+- if (ended) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (ended || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -24425,6 +24444,12 @@ var Mediabunny = (() => {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -24440,6 +24465,9 @@ var Mediabunny = (() => {terminated = true;ended = true;closeSamples();@@ -558,7 +762,7 @@ index c828e19..a1c28cc 100644throw new InputDisposedError();} else if (terminated) {return { value: void 0, done: true };-@@ -24447,6 +24465,9 @@ var Mediabunny = (() => {+@@ -24447,6 +24475,9 @@ var Mediabunny = (() => {terminated = true;ended = true;closeSamples();@@ -568,7 +772,50 @@ index c828e19..a1c28cc 100644throw outOfBandError;} else if (sampleQueue.length > 0) {const value = sampleQueue.shift();-@@ -24611,12 +24632,18 @@ var Mediabunny = (() => {+@@ -24498,10 +24529,14 @@ var Mediabunny = (() => {+ verifyKeyPackets: true,+ metadataOnly: false+ };++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+- if (terminated) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (terminated || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -24535,11 +24570,11 @@ var Mediabunny = (() => {+ decoder.decode(currentPacket);+ while (currentPacket.sequenceNumber < maxSequenceNumber) {+ const maxQueueSize = computeMaxQueueSize(sampleQueue.length);+- while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated) {++ while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated && !this._track.input._disposed) {+ ({ promise: queueDequeue, resolve: onQueueDequeue } = promiseWithResolvers());+ await queueDequeue;+ }+- if (terminated) {++ if (terminated || this._track.input._disposed) {+ break;+ }+ const nextPacket = await packetSink.getNextPacket(currentPacket, retrievalOptions);+@@ -24598,6 +24633,12 @@ var Mediabunny = (() => {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -24611,12 +24652,18 @@ var Mediabunny = (() => {if (track.input._disposed) {terminated = true;closeSamples();@@ -587,7 +834,7 @@ index c828e19..a1c28cc 100644throw outOfBandError;} else if (sampleQueue.length > 0) {const value = sampleQueue.shift();-@@ -24650,8 +24677,10 @@ var Mediabunny = (() => {+@@ -24650,8 +24697,10 @@ var Mediabunny = (() => {return decodedSampleQueueSize === 0 ? 40 : 8;};var VideoDecoderWrapper = class extends DecoderWrapper {@@ -599,7 +846,7 @@ index c828e19..a1c28cc 100644this.codec = codec;this.decoderConfig = decoderConfig;this.rotation = rotation;-@@ -24661,6 +24690,12 @@ var Mediabunny = (() => {+@@ -24661,6 +24710,12 @@ var Mediabunny = (() => {this.customDecoder = null;this.customDecoderCallSerializer = new CallSerializer();this.customDecoderQueueSize = 0;@@ -612,7 +859,7 @@ index c828e19..a1c28cc 100644this.inputTimestamps = [];// Timestamps input into the decoder, sorted.this.sampleQueue = [];-@@ -24692,6 +24727,17 @@ var Mediabunny = (() => {+@@ -24692,6 +24747,17 @@ var Mediabunny = (() => {if (!(sample instanceof VideoSample)) {throw new TypeError("The argument passed to onSample must be a VideoSample.");}@@ -630,7 +877,7 @@ index c828e19..a1c28cc 100644this.finalizeAndEmitSample(sample);};this.customDecoder.onError = (error) => {-@@ -24763,7 +24809,7 @@ var Mediabunny = (() => {+@@ -24763,7 +24829,7 @@ var Mediabunny = (() => {}getDecodeQueueSize() {if (this.customDecoder) {@@ -639,7 +886,7 @@ index c828e19..a1c28cc 100644} else {assert(this.decoder);return Math.max(-@@ -24827,6 +24873,12 @@ var Mediabunny = (() => {+@@ -24827,6 +24893,12 @@ var Mediabunny = (() => {}}}@@ -652,7 +899,7 @@ index c828e19..a1c28cc 100644this.decoder.decode(packet.toEncodedVideoChunk());this.decodeAlphaData(packet);}-@@ -24943,6 +24995,49 @@ var Mediabunny = (() => {+@@ -24943,6 +25015,49 @@ var Mediabunny = (() => {sample.setFlip(this.flip);this.onSample(sample);}@@ -702,7 +949,7 @@ index c828e19..a1c28cc 100644async mergeAlpha(color, alpha) {const resolver = promiseWithResolvers();this.mergeAlphaPromises.push(resolver.promise);-@@ -24971,6 +25066,11 @@ var Mediabunny = (() => {+@@ -24971,6 +25086,11 @@ var Mediabunny = (() => {async flush() {if (this.customDecoder) {await this.customDecoderCallSerializer.call(() => this.customDecoder.flush());@@ -714,7 +961,7 @@ index c828e19..a1c28cc 100644} else {assert(this.decoder);await Promise.all([-@@ -25251,6 +25351,18 @@ var Mediabunny = (() => {+@@ -25251,6 +25371,18 @@ var Mediabunny = (() => {if (decoderOptions.optimizeForLatency !== void 0 && typeof decoderOptions.optimizeForLatency !== "boolean") {throw new TypeError("decoderOptions.optimizeForLatency, when provided, must be a boolean.");}@@ -733,7 +980,7 @@ index c828e19..a1c28cc 100644};var VideoSampleSink = class extends BaseMediaSampleSink {/** Creates a new {@link VideoSampleSink} for the given {@link InputVideoTrack}. */-@@ -25279,12 +25391,19 @@ var Mediabunny = (() => {+@@ -25279,12 +25411,19 @@ var Mediabunny = (() => {let decoderConfig = await this._track.getDecoderConfig();const timeResolution = await this._track.getTimeResolution();assert(codec && decoderConfig);@@ -755,6 +1002,17 @@ index c828e19..a1c28cc 100644}/** @internal */_createPacketSink() {+@@ -27345,6 +27484,10 @@ var Mediabunny = (() => {+ return;+ }+ this._disposed = true;++ // Wake the decode pumps parked on this input (Donkey patch): each unwinds and closes what it queued.++ for (const wake of this._disposeWakers ?? []) {++ wake();++ }+ for (const ref of this._sourceRefs) {+ ref.free();+ }diff --git a/node_modules/mediabunny/dist/mediabunny.d.ts b/node_modules/mediabunny/dist/mediabunny.d.tsindex 1307312..1a2027e 100644--- a/node_modules/mediabunny/dist/mediabunny.d.ts@@ -781,6 +1039,21 @@ index 1307312..1a2027e 100644};/**+diff --git a/node_modules/mediabunny/dist/modules/src/input.js b/node_modules/mediabunny/dist/modules/src/input.js+index d4bec8f..8043f02 100644+--- a/node_modules/mediabunny/dist/modules/src/input.js++++ b/node_modules/mediabunny/dist/modules/src/input.js+@@ -354,6 +354,10 @@ export class Input extends EventEmitter {+ return;+ }+ this._disposed = true;++ // Wake the decode pumps parked on this input (Donkey patch): each unwinds and closes what it queued.++ for (const wake of this._disposeWakers ?? []) {++ wake();++ }+ for (const ref of this._sourceRefs) {+ ref.free();+ }diff --git a/node_modules/mediabunny/dist/modules/src/media-sink.d.ts b/node_modules/mediabunny/dist/modules/src/media-sink.d.tsindex 056c51c..2463224 100644--- a/node_modules/mediabunny/dist/modules/src/media-sink.d.ts@@ -808,7 +1081,7 @@ index 056c51c..2463224 100644/*** A sink that retrieves decoded video samples (video frames) from a video track.diff --git a/node_modules/mediabunny/dist/modules/src/media-sink.js b/node_modules/mediabunny/dist/modules/src/media-sink.js-index 9465e3b..b814d42 100644+index 9465e3b..4b77ae1 100644--- a/node_modules/mediabunny/dist/modules/src/media-sink.js+++ b/node_modules/mediabunny/dist/modules/src/media-sink.js@@ -13,7 +13,7 @@ import { InputAudioTrack, InputTrack, InputVideoTrack } from './input-track.js';@@ -820,7 +1093,39 @@ index 9465e3b..b814d42 100644const validatePacketRetrievalOptions = (options) => {if (!options || typeof options !== 'object') {throw new TypeError('options must be an object.');-@@ -425,6 +425,9 @@ export class BaseMediaSampleSink {+@@ -324,13 +324,17 @@ export class BaseMediaSampleSink {+ metadataOnly: false,+ };+ // The following is the "pump" process that keeps pumping packets into the decoder++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+ if (sample.timestamp >= endTimestamp) {+ ended = true;+ }+- if (ended) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (ended || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -407,6 +411,12 @@ export class BaseMediaSampleSink {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -425,6 +435,9 @@ export class BaseMediaSampleSink {terminated = true;ended = true;closeSamples();@@ -830,7 +1135,7 @@ index 9465e3b..b814d42 100644throw new InputDisposedError();}else if (terminated) {-@@ -434,6 +437,9 @@ export class BaseMediaSampleSink {+@@ -434,6 +447,9 @@ export class BaseMediaSampleSink {terminated = true;ended = true;closeSamples();@@ -840,7 +1145,50 @@ index 9465e3b..b814d42 100644throw outOfBandError;}else if (sampleQueue.length > 0) {-@@ -621,6 +627,9 @@ export class BaseMediaSampleSink {+@@ -492,10 +508,14 @@ export class BaseMediaSampleSink {+ metadataOnly: false,+ };+ // The following is the "pump" process that keeps pumping packets into the decoder++ // Wake this pump if its input is disposed while it is parked on a full queue (Donkey patch).++ const wakeOnDispose = () => onQueueDequeue();++ (this._track.input._disposeWakers ??= new Set()).add(wakeOnDispose);+ (async () => {+ decoder = await this._createDecoder((sample) => {+ onQueueDequeue();+- if (terminated) {++ // A sample decoded after the input was disposed has no reader left (Donkey patch).++ if (terminated || this._track.input._disposed) {+ sample.close();+ return;+ }+@@ -536,11 +556,11 @@ export class BaseMediaSampleSink {+ decoder.decode(currentPacket);+ while (currentPacket.sequenceNumber < maxSequenceNumber) {+ const maxQueueSize = computeMaxQueueSize(sampleQueue.length);+- while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated) {++ while (sampleQueue.length + decoder.getDecodeQueueSize() > maxQueueSize && !terminated && !this._track.input._disposed) {+ ({ promise: queueDequeue, resolve: onQueueDequeue } = promiseWithResolvers());+ await queueDequeue;+ }+- if (terminated) {++ if (terminated || this._track.input._disposed) {+ break;+ }+ const nextPacket = await packetSink.getNextPacket(currentPacket, retrievalOptions);+@@ -605,6 +625,12 @@ export class BaseMediaSampleSink {+ }+ }).finally(() => {+ decoder?.close();++ this._track.input._disposeWakers?.delete(wakeOnDispose);++ // A pump that ends on a disposed input closes what it queued (Donkey patch): a reader that was idle++ // when its input went away never calls next() again, so the samples would wait for the garbage collector.++ if (this._track.input._disposed) {++ closeSamples();++ }+ });+ const track = this._track;+ const closeSamples = () => {+@@ -621,6 +647,9 @@ export class BaseMediaSampleSink {// nothing can ever close.terminated = true;closeSamples();@@ -850,7 +1198,7 @@ index 9465e3b..b814d42 100644throw new InputDisposedError();}else if (terminated) {-@@ -629,6 +638,9 @@ export class BaseMediaSampleSink {+@@ -629,6 +658,9 @@ export class BaseMediaSampleSink {else if (hasOutOfBandError) {terminated = true;closeSamples();@@ -860,7 +1208,7 @@ index 9465e3b..b814d42 100644throw outOfBandError;}else if (sampleQueue.length > 0) {-@@ -662,14 +674,17 @@ export class BaseMediaSampleSink {+@@ -662,14 +694,17 @@ export class BaseMediaSampleSink {}}const computeMaxQueueSize = (decodedSampleQueueSize) => {@@ -883,7 +1231,7 @@ index 9465e3b..b814d42 100644this.codec = codec;this.decoderConfig = decoderConfig;this.rotation = rotation;-@@ -679,6 +694,13 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -679,6 +714,13 @@ class VideoDecoderWrapper extends DecoderWrapper {this.customDecoder = null;this.customDecoderCallSerializer = new CallSerializer();this.customDecoderQueueSize = 0;@@ -897,7 +1245,7 @@ index 9465e3b..b814d42 100644this.inputTimestamps = []; // Timestamps input into the decoder, sorted.this.sampleQueue = []; // Safari-specific thing, check usage.this.currentPacketIndex = 0;-@@ -710,6 +732,20 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -710,6 +752,20 @@ class VideoDecoderWrapper extends DecoderWrapper {if (!(sample instanceof VideoSample)) {throw new TypeError('The argument passed to onSample must be a VideoSample.');}@@ -918,7 +1266,7 @@ index 9465e3b..b814d42 100644this.finalizeAndEmitSample(sample);};// @ts-expect-error It's technically readonly-@@ -780,6 +816,7 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -780,6 +836,7 @@ class VideoDecoderWrapper extends DecoderWrapper {const stack = new Error('Decoding error').stack;this.decoder = new VideoDecoder({output: (frame) => {@@ -926,7 +1274,7 @@ index 9465e3b..b814d42 100644try {colorHandler(frame);}-@@ -797,11 +834,11 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -797,11 +854,11 @@ class VideoDecoderWrapper extends DecoderWrapper {}getDecodeQueueSize() {if (this.customDecoder) {@@ -940,7 +1288,7 @@ index 9465e3b..b814d42 100644}}decode(packet) {-@@ -873,6 +910,13 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -873,6 +930,13 @@ class VideoDecoderWrapper extends DecoderWrapper {}}}@@ -954,7 +1302,7 @@ index 9465e3b..b814d42 100644this.decoder.decode(packet.toEncodedVideoChunk());this.decodeAlphaData(packet);}-@@ -1020,6 +1064,49 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -1020,6 +1084,49 @@ class VideoDecoderWrapper extends DecoderWrapper {sample.setFlip(this.flip);this.onSample(sample);}@@ -1004,7 +1352,7 @@ index 9465e3b..b814d42 100644async mergeAlpha(color, alpha) {const resolver = promiseWithResolvers();this.mergeAlphaPromises.push(resolver.promise);-@@ -1056,6 +1143,11 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -1056,6 +1163,11 @@ class VideoDecoderWrapper extends DecoderWrapper {async flush() {if (this.customDecoder) {await this.customDecoderCallSerializer.call(() => this.customDecoder.flush());@@ -1016,7 +1364,7 @@ index 9465e3b..b814d42 100644}else {assert(this.decoder);-@@ -1081,6 +1173,7 @@ class VideoDecoderWrapper extends DecoderWrapper {+@@ -1081,6 +1193,7 @@ class VideoDecoderWrapper extends DecoderWrapper {}this.sampleQueue.length = 0;}@@ -1024,7 +1372,7 @@ index 9465e3b..b814d42 100644this.currentPacketIndex = 0;this.raslSkipped = false;}-@@ -1358,6 +1451,18 @@ const validateVideoSinkDecoderOptions = (decoderOptions) => {+@@ -1358,6 +1471,18 @@ const validateVideoSinkDecoderOptions = (decoderOptions) => {if (decoderOptions.optimizeForLatency !== undefined && typeof decoderOptions.optimizeForLatency !== 'boolean') {throw new TypeError('decoderOptions.optimizeForLatency, when provided, must be a boolean.');}@@ -1043,7 +1391,7 @@ index 9465e3b..b814d42 100644};/*** A sink that retrieves decoded video samples (video frames) from a video track.-@@ -1390,12 +1495,19 @@ export class VideoSampleSink extends BaseMediaSampleSink {+@@ -1390,12 +1515,19 @@ export class VideoSampleSink extends BaseMediaSampleSink {let decoderConfig = await this._track.getDecoderConfig();const timeResolution = await this._track.getTimeResolution();assert(codec && decoderConfig);diff --git a/site/src/cut/lib/decodePump.test.ts b/site/src/cut/lib/decodePump.test.tsindex e3d273b8..de0702b4 100644--- a/site/src/cut/lib/decodePump.test.ts+++ b/site/src/cut/lib/decodePump.test.ts@@ -16,13 +16,14 @@ import {// mediabunny's decode pump, as patched (patches/mediabunny+1.61.0.patch):// what happens to a decoder that fails while the reader has stopped pulling.-const CLIP = join(dirname(fileURLToPath(import.meta.url)), "../../../public/cut-starter/media/ai-watercolor-and-loose-black-ink-sketch-il-1.mp4");+const CLIP = join(dirname(fileURLToPath(import.meta.url)), "../../../public/cut-starter/media/recap-01-6a6c904b.mp4");const bytes = readFileSync(CLIP);-// Registered decoders are process-wide and permanent, so this one answers-// only while a case here runs.+// Registered decoders are process-wide and permanent, so each answers only+// while a case of its own runs.let active = false;let closed = 0;+let steady = false;/** A decoder that hands back a frame per packet and fails out of band a* moment after its fourth: by then the pump has filled the queue and is@@ -59,10 +60,43 @@ class FailingDecoder extends CustomVideoDecoder {}registerDecoder(FailingDecoder);+/** Every frame a SteadyDecoder made, to check each was closed. */+const made: VideoSample[] = [];++/** A decoder that hands back a frame per packet and never fails. */+class SteadyDecoder extends CustomVideoDecoder {+ static override supports() {+ return steady;+ }++ init() {}++ decode(packet: EncodedPacket) {+ const sample = new VideoSample(new Uint8Array(16), {+ format: "RGBX",+ codedWidth: 2,+ codedHeight: 2,+ timestamp: packet.timestamp,+ duration: packet.duration,+ });+ made.push(sample);+ this.onSample(sample);+ }++ flush() {}++ close() {}+}+registerDecoder(SteadyDecoder);+afterAll(() => {active = false;+ steady = false;});+/** Whether mediabunny has let a sample's frame go. */+const isClosed = (sample: VideoSample) => (sample as unknown as { _closed: boolean })._closed;+const settle = () => new Promise((r) => setTimeout(r, 60));describe("decode pump", () => {@@ -94,4 +128,29 @@ describe("decode pump", () => {}});}++ for (const [name, read] of Object.entries(reads)) {+ test(`an input disposed under an idle reader closes what the pump queued: ${name}`, async () => {+ steady = true;+ made.length = 0;+ const input = new Input({ source: new BlobSource(new Blob([bytes])), formats: [MP4] });+ try {+ const track = await input.getPrimaryVideoTrack();+ const samples = read(new VideoSampleSink(track!));++ // One frame read, then the reader goes idle and never asks again+ // while the pump fills the queue and parks.+ (await samples.next()).value?.close();+ await settle();+ expect(made.length).toBeGreaterThan(1);++ input.dispose();+ await settle();++ expect(made.filter((sample) => !isClosed(sample))).toEqual([]);+ } finally {+ steady = false;+ }+ });+ }});