Skip to content

Open source CapCut alternative.

TypeScript

perf(site): mediabunny counts decoded frames in flight

David Le904a3ed69a38
diff --git a/site/package-lock.json b/site/package-lock.json
index f98fd47c..cbd5bcd9 100644
--- a/site/package-lock.json
+++ b/site/package-lock.json
@@ -47,7 +47,7 @@
"hls.js": "^1.6.16",
"linkedom": "^0.18.12",
"lucide-react": "^1.23.0",
- "mediabunny": "^1.55.1",
+ "mediabunny": "^1.55.5",
"next": "16.2.6",
"node-web-audio-api": "^2.2.0",
"openai": "^6.39.0",
@@ -81,6 +81,7 @@
"dotenv-cli": "^11.0.0",
"eslint": "^9",
"eslint-config-next": "16.2.6",
+ "patch-package": "^8.0.1",
"playwright": "^1.62.1",
"prisma": "^7.8.0",
"tailwindcss": "^4",
@@ -6408,6 +6409,13 @@
"node": ">=14.6"
}
},
+ "node_modules/@yarnpkg/lockfile": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz",
+ "integrity": "sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==",
+ "dev": true,
+ "license": "BSD-2-Clause"
+ },
"node_modules/accepts": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz",
@@ -7494,6 +7502,22 @@
"url": "https://paulmillr.com/funding/"
}
},
+ "node_modules/ci-info": {
+ "version": "3.9.0",
+ "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz",
+ "integrity": "sha512-NIxF55hv4nSqQswkAeiOi1r83xy8JldOFDTWiug55KBu9Jnblncd2U6ViHmYgHf01TPZS77NJBhBMKdWj9HQMQ==",
+ "dev": true,
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/sibiraj-s"
+ }
+ ],
+ "license": "MIT",
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/citty": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/citty/-/citty-0.2.2.tgz",
@@ -9967,6 +9991,16 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/find-yarn-workspace-root": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/find-yarn-workspace-root/-/find-yarn-workspace-root-2.0.0.tgz",
+ "integrity": "sha512-1IMnbjt4KzsQfnhnzNd8wUEgXZ44IzZaZmnLYx7D5FZlaHt2gW20Cri8Q+E/t5tIj4+epTBub+2Zxu/vNILzqQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "micromatch": "^4.0.2"
+ }
+ },
"node_modules/flat-cache": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-4.0.1.tgz",
@@ -11790,6 +11824,26 @@
"integrity": "sha512-fQhoXdcvc3V28x7C7BMs4P5+kNlgUURe2jmUT1T//oBRMDrqy1QPelJimwZGo7Hg9VPV3EQV5Bnq4hbFy2vetA==",
"license": "BSD-2-Clause"
},
+ "node_modules/json-stable-stringify": {
+ "version": "1.3.0",
+ "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.3.0.tgz",
+ "integrity": "sha512-qtYiSSFlwot9XHtF9bD9c7rwKjr+RecWT//ZnPvSmEjpV5mmPOCN4j8UjY5hbjNkOwZ/jQv3J6R1/pL7RwgMsg==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "call-bind": "^1.0.8",
+ "call-bound": "^1.0.4",
+ "isarray": "^2.0.5",
+ "jsonify": "^0.0.1",
+ "object-keys": "^1.1.1"
+ },
+ "engines": {
+ "node": ">= 0.4"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/ljharb"
+ }
+ },
"node_modules/json-stable-stringify-without-jsonify": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz",
@@ -11821,6 +11875,16 @@
"graceful-fs": "^4.1.6"
}
},
+ "node_modules/jsonify": {
+ "version": "0.0.1",
+ "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.1.tgz",
+ "integrity": "sha512-2/Ki0GcmuqSrgFyelQq9M05y7PS0mEwuIzrf3f1fPqkVDVRvZrPZtVSMHxdgo8Aq0sxAOb/cr2aqqA3LeWHVPg==",
+ "dev": true,
+ "license": "Public Domain",
+ "funding": {
+ "url": "https://github.com/sponsors/ljharb"
+ }
+ },
"node_modules/jsx-ast-utils": {
"version": "3.3.5",
"resolved": "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.5.tgz",
@@ -11868,6 +11932,16 @@
"json-buffer": "3.0.1"
}
},
+ "node_modules/klaw-sync": {
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/klaw-sync/-/klaw-sync-6.0.0.tgz",
+ "integrity": "sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "graceful-fs": "^4.1.11"
+ }
+ },
"node_modules/kleur": {
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz",
@@ -12591,9 +12665,9 @@
}
},
"node_modules/mediabunny": {
- "version": "1.55.1",
- "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.55.1.tgz",
- "integrity": "sha512-JDdTEUOw9g6Ey8eWc2Ei63GmRGtGTJoE56SE7CHikwzS8i9xpsWcve4bEiUw47d4j49L3r7MXDFsH/5oZIhjBw==",
+ "version": "1.55.5",
+ "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.55.5.tgz",
+ "integrity": "sha512-m0v6y8FGXiK+HKOc3AZqU+kJPLYsSKaLitmQNQIIHZKIGlTwQ34OF+X6Ul0K8iV4b03oPse10apwuoqbvmKeAA==",
"license": "MPL-2.0",
"workspaces": [
".",
@@ -14219,6 +14293,110 @@
"integrity": "sha512-Njv/59hHaokb/hRUjce3Hdv12wd60MtM9Z5Olmn+nehe0QDAsRtRbJPvJ0Z91TusF0SuZRIvnM+S4l6EIP8leA==",
"license": "MIT"
},
+ "node_modules/patch-package": {
+ "version": "8.0.1",
+ "resolved": "https://registry.npmjs.org/patch-package/-/patch-package-8.0.1.tgz",
+ "integrity": "sha512-VsKRIA8f5uqHQ7NGhwIna6Bx6D9s/1iXlA1hthBVBEbkq+t4kXD0HHt+rJhf/Z+Ci0F/HCB2hvn0qLdLG+Qxlw==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@yarnpkg/lockfile": "^1.1.0",
+ "chalk": "^4.1.2",
+ "ci-info": "^3.7.0",
+ "cross-spawn": "^7.0.3",
+ "find-yarn-workspace-root": "^2.0.0",
+ "fs-extra": "^10.0.0",
+ "json-stable-stringify": "^1.0.2",
+ "klaw-sync": "^6.0.0",
+ "minimist": "^1.2.6",
+ "open": "^7.4.2",
+ "semver": "^7.5.3",
+ "slash": "^2.0.0",
+ "tmp": "^0.2.4",
+ "yaml": "^2.2.2"
+ },
+ "bin": {
+ "patch-package": "index.js"
+ },
+ "engines": {
+ "node": ">=14",
+ "npm": ">5"
+ }
+ },
+ "node_modules/patch-package/node_modules/fs-extra": {
+ "version": "10.1.0",
+ "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz",
+ "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "graceful-fs": "^4.2.0",
+ "jsonfile": "^6.0.1",
+ "universalify": "^2.0.0"
+ },
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/patch-package/node_modules/is-docker": {
+ "version": "2.2.1",
+ "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz",
+ "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==",
+ "dev": true,
+ "license": "MIT",
+ "bin": {
+ "is-docker": "cli.js"
+ },
+ "engines": {
+ "node": ">=8"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
+ "node_modules/patch-package/node_modules/is-wsl": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-2.2.0.tgz",
+ "integrity": "sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "is-docker": "^2.0.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/patch-package/node_modules/open": {
+ "version": "7.4.2",
+ "resolved": "https://registry.npmjs.org/open/-/open-7.4.2.tgz",
+ "integrity": "sha512-MVHddDVweXZF3awtlAS+6pgKLlm/JgxZ90+/NBurBoQctVOOB/zDdVjcyPzQ+0laDGbsWgrRkflI65sQeOgT9Q==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "is-docker": "^2.0.0",
+ "is-wsl": "^2.1.1"
+ },
+ "engines": {
+ "node": ">=8"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
+ "node_modules/patch-package/node_modules/semver": {
+ "version": "7.8.5",
+ "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz",
+ "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==",
+ "dev": true,
+ "license": "ISC",
+ "bin": {
+ "semver": "bin/semver.js"
+ },
+ "engines": {
+ "node": ">=10"
+ }
+ },
"node_modules/path-browserify": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",
@@ -16143,6 +16321,16 @@
"string-split-by": "^1.0.0"
}
},
+ "node_modules/slash": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz",
+ "integrity": "sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=6"
+ }
+ },
"node_modules/socket.io": {
"version": "4.8.3",
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-4.8.3.tgz",
@@ -16852,6 +17040,16 @@
"integrity": "sha512-uiHN8PIB1VmWyS98eZYja4xzlYqeFZVjb4OuYlJQnZAuJhMw4PbKQOKgHKhBdJR3FE/t5mUQ1Kd80++B+qhD1Q==",
"license": "MIT"
},
+ "node_modules/tmp": {
+ "version": "0.2.7",
+ "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.7.tgz",
+ "integrity": "sha512-e0votIpp4Uo2AJYSzVHV6xCcawuiez3DzqDAbrTc3YxBkplN6e+dM13ZeIcZnDg/QpSuU2zfZ3rzwY8ukEnaXw==",
+ "dev": true,
+ "license": "MIT",
+ "engines": {
+ "node": ">=14.14"
+ }
+ },
"node_modules/to-regex-range": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
diff --git a/site/package.json b/site/package.json
index 1e9d64a2..dbedd649 100644
--- a/site/package.json
+++ b/site/package.json
@@ -12,7 +12,7 @@
"build": "next build",
"start": "next start",
"lint": "eslint",
- "postinstall": "prisma generate && node scripts/copy-mediapipe-assets.mjs && node scripts/fetch-cut-fonts.mjs",
+ "postinstall": "patch-package && node scripts/check-mediabunny-patch.mjs && prisma generate && node scripts/copy-mediapipe-assets.mjs && node scripts/fetch-cut-fonts.mjs",
"db:generate": "prisma generate",
"db:pull": "prisma db pull",
"prisma:dev": "dotenv -e .env.development -- npx prisma migrate dev",
@@ -68,7 +68,7 @@
"hls.js": "^1.6.16",
"linkedom": "^0.18.12",
"lucide-react": "^1.23.0",
- "mediabunny": "^1.55.1",
+ "mediabunny": "^1.55.5",
"next": "16.2.6",
"node-web-audio-api": "^2.2.0",
"openai": "^6.39.0",
@@ -102,6 +102,7 @@
"dotenv-cli": "^11.0.0",
"eslint": "^9",
"eslint-config-next": "16.2.6",
+ "patch-package": "^8.0.1",
"playwright": "^1.62.1",
"prisma": "^7.8.0",
"tailwindcss": "^4",
diff --git a/site/patches/README.md b/site/patches/README.md
new file mode 100644
index 00000000..71662fa8
--- /dev/null
+++ b/site/patches/README.md
@@ -0,0 +1,28 @@
+# Dependency patches
+
+Applied by `patch-package` in `postinstall`. Each patch names the exact
+version it was made against; `scripts/check-mediabunny-patch.mjs` stops the
+install when the installed version drifts from the patch, so a bump always
+comes with a look at whether the patch is still needed.
+
+## mediabunny: decoded frames in flight
+
+mediabunny's decode pump throttles on WebCodecs' `decodeQueueSize`, which
+drains as soon as a packet reaches the platform decoder, and it starts every
+stream with a 40-packet head start. Frames the decoder is still holding count
+as nothing, so a stream whose reader pauses — the Cut preview with its ring
+full — ends up with about 40 decoded full-size frames waiting in mediabunny's
+sample queue. At 4K that is around a gigabyte per stream, held in the decoder
+process (VTDecoderXPCService on a Mac, the GPU process elsewhere).
+
+The patch gives `VideoDecoderWrapper` an `inFlight` count (packets submitted
+minus frames output, reset on flush), reports it as the decoder's queue size,
+and lowers the head start to 16. The same six edits land in the ES module and
+both bundles.
+
+Delete this patch when upstream's pump counts frames in flight itself: check
+`computeMaxQueueSize` and the pump loops around `getDecodeQueueSize()` in
+`dist/modules/src/media-sink.js` of the new version. Removing it means
+deleting the patch file, this section, the check script, and `patch-package`
+from `postinstall` and `devDependencies`, then running the perf eval's
+playback bucket (`npm run eval:cut-perf -- --bucket playback`).
diff --git a/site/patches/mediabunny+1.55.5.patch b/site/patches/mediabunny+1.55.5.patch
new file mode 100644
index 00000000..049da81b
--- /dev/null
+++ b/site/patches/mediabunny+1.55.5.patch
@@ -0,0 +1,175 @@
+diff --git a/node_modules/mediabunny/dist/bundles/mediabunny.cjs b/node_modules/mediabunny/dist/bundles/mediabunny.cjs
+index 3c514ad..66faf6a 100644
+--- a/node_modules/mediabunny/dist/bundles/mediabunny.cjs
++++ b/node_modules/mediabunny/dist/bundles/mediabunny.cjs
+@@ -23941,7 +23941,7 @@ var Mediabunny = (() => {
+ }
+ };
+ var computeMaxQueueSize = (decodedSampleQueueSize) => {
+- return decodedSampleQueueSize === 0 ? 40 : 8;
++ return decodedSampleQueueSize === 0 ? 16 : 8;
+ };
+ var VideoDecoderWrapper = class extends DecoderWrapper {
+ constructor(onSample, onError, codec, decoderConfig, rotation, timeResolution) {
+@@ -23954,6 +23954,7 @@ var Mediabunny = (() => {
+ this.customDecoder = null;
+ this.customDecoderCallSerializer = new CallSerializer();
+ this.customDecoderQueueSize = 0;
++ this.inFlight = 0; // Packets sent to the decoder whose frames have not come back yet.
+ this.inputTimestamps = [];
+ // Timestamps input into the decoder, sorted.
+ this.sampleQueue = [];
+@@ -24031,6 +24032,7 @@ var Mediabunny = (() => {
+ const stack = new Error("Decoding error").stack;
+ this.decoder = new VideoDecoder({
+ output: (frame) => {
++ this.inFlight = Math.max(0, this.inFlight - 1);
+ try {
+ colorHandler(frame);
+ } catch (error) {
+@@ -24050,10 +24052,7 @@ var Mediabunny = (() => {
+ return this.customDecoderQueueSize;
+ } else {
+ assert(this.decoder);
+- return Math.max(
+- this.decoder.decodeQueueSize,
+- this.alphaDecoder?.decodeQueueSize ?? 0
+- );
++ return Math.max(this.inFlight, this.alphaDecoder?.decodeQueueSize ?? 0);
+ }
+ }
+ decode(packet) {
+@@ -24098,6 +24097,7 @@ var Mediabunny = (() => {
+ }
+ }
+ }
++ this.inFlight++;
+ this.decoder.decode(packet.toEncodedVideoChunk());
+ this.decodeAlphaData(packet);
+ }
+@@ -24265,6 +24265,7 @@ var Mediabunny = (() => {
+ }
+ this.sampleQueue.length = 0;
+ }
++ this.inFlight = 0;
+ this.currentPacketIndex = 0;
+ this.raslSkipped = false;
+ }
+diff --git a/node_modules/mediabunny/dist/bundles/mediabunny.mjs b/node_modules/mediabunny/dist/bundles/mediabunny.mjs
+index 9a62e92..5abfb37 100644
+--- a/node_modules/mediabunny/dist/bundles/mediabunny.mjs
++++ b/node_modules/mediabunny/dist/bundles/mediabunny.mjs
+@@ -23784,7 +23784,7 @@ var BaseMediaSampleSink = class {
+ }
+ };
+ var computeMaxQueueSize = (decodedSampleQueueSize) => {
+- return decodedSampleQueueSize === 0 ? 40 : 8;
++ return decodedSampleQueueSize === 0 ? 16 : 8;
+ };
+ var VideoDecoderWrapper = class extends DecoderWrapper {
+ constructor(onSample, onError, codec, decoderConfig, rotation, timeResolution) {
+@@ -23797,6 +23797,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {
+ this.customDecoder = null;
+ this.customDecoderCallSerializer = new CallSerializer();
+ this.customDecoderQueueSize = 0;
++ this.inFlight = 0; // Packets sent to the decoder whose frames have not come back yet.
+ this.inputTimestamps = [];
+ // Timestamps input into the decoder, sorted.
+ this.sampleQueue = [];
+@@ -23874,6 +23875,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {
+ const stack = new Error("Decoding error").stack;
+ this.decoder = new VideoDecoder({
+ output: (frame) => {
++ this.inFlight = Math.max(0, this.inFlight - 1);
+ try {
+ colorHandler(frame);
+ } catch (error) {
+@@ -23893,10 +23895,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {
+ return this.customDecoderQueueSize;
+ } else {
+ assert(this.decoder);
+- return Math.max(
+- this.decoder.decodeQueueSize,
+- this.alphaDecoder?.decodeQueueSize ?? 0
+- );
++ return Math.max(this.inFlight, this.alphaDecoder?.decodeQueueSize ?? 0);
+ }
+ }
+ decode(packet) {
+@@ -23941,6 +23940,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {
+ }
+ }
+ }
++ this.inFlight++;
+ this.decoder.decode(packet.toEncodedVideoChunk());
+ this.decodeAlphaData(packet);
+ }
+@@ -24108,6 +24108,7 @@ var VideoDecoderWrapper = class extends DecoderWrapper {
+ }
+ this.sampleQueue.length = 0;
+ }
++ this.inFlight = 0;
+ this.currentPacketIndex = 0;
+ this.raslSkipped = false;
+ }
+diff --git a/node_modules/mediabunny/dist/modules/src/media-sink.js b/node_modules/mediabunny/dist/modules/src/media-sink.js
+index da1cc3a..28c15d3 100644
+--- a/node_modules/mediabunny/dist/modules/src/media-sink.js
++++ b/node_modules/mediabunny/dist/modules/src/media-sink.js
+@@ -650,10 +650,11 @@ export class BaseMediaSampleSink {
+ }
+ }
+ const computeMaxQueueSize = (decodedSampleQueueSize) => {
+- // If we have decoded samples lying around, limit the total queue size to a small value (decoded samples can use up
+- // a lot of memory). If not, we're fine with a much bigger queue of encoded packets waiting to be decoded. In fact,
+- // some decoders only start flushing out decoded chunks when the packet queue is large enough.
+- return decodedSampleQueueSize === 0 ? 40 : 8;
++ // Decoded frames are large, and a video decoder holds every packet it has been handed until the frame comes out. So
++ // the pump counts frames in flight together with the decoded ones waiting, and keeps the total small: enough of a
++ // head start for a decoder that reorders frames, and no more, so a consumer that stops reading pins a bounded
++ // number of decoded frames.
++ return decodedSampleQueueSize === 0 ? 16 : 8;
+ };
+ class VideoDecoderWrapper extends DecoderWrapper {
+ constructor(onSample, onError, codec, decoderConfig, rotation, timeResolution) {
+@@ -666,6 +667,7 @@ class VideoDecoderWrapper extends DecoderWrapper {
+ this.customDecoder = null;
+ this.customDecoderCallSerializer = new CallSerializer();
+ this.customDecoderQueueSize = 0;
++ this.inFlight = 0; // Packets sent to the decoder whose frames have not come back yet.
+ this.inputTimestamps = []; // Timestamps input into the decoder, sorted.
+ this.sampleQueue = []; // Safari-specific thing, check usage.
+ this.currentPacketIndex = 0;
+@@ -756,6 +758,7 @@ class VideoDecoderWrapper extends DecoderWrapper {
+ const stack = new Error('Decoding error').stack;
+ this.decoder = new VideoDecoder({
+ output: (frame) => {
++ this.inFlight = Math.max(0, this.inFlight - 1);
+ try {
+ colorHandler(frame);
+ }
+@@ -777,7 +780,7 @@ class VideoDecoderWrapper extends DecoderWrapper {
+ }
+ else {
+ assert(this.decoder);
+- return Math.max(this.decoder.decodeQueueSize, this.alphaDecoder?.decodeQueueSize ?? 0);
++ return Math.max(this.inFlight, this.alphaDecoder?.decodeQueueSize ?? 0);
+ }
+ }
+ decode(packet) {
+@@ -835,6 +838,7 @@ class VideoDecoderWrapper extends DecoderWrapper {
+ }
+ }
+ }
++ this.inFlight++;
+ this.decoder.decode(packet.toEncodedVideoChunk());
+ this.decodeAlphaData(packet);
+ }
+@@ -1042,6 +1046,7 @@ class VideoDecoderWrapper extends DecoderWrapper {
+ }
+ this.sampleQueue.length = 0;
+ }
++ this.inFlight = 0;
+ this.currentPacketIndex = 0;
+ this.raslSkipped = false;
+ }
diff --git a/site/scripts/check-mediabunny-patch.mjs b/site/scripts/check-mediabunny-patch.mjs
new file mode 100644
index 00000000..abe464a5
--- /dev/null
+++ b/site/scripts/check-mediabunny-patch.mjs
@@ -0,0 +1,29 @@
+// Runs in postinstall, after patch-package. The patch in patches/ bounds how
+// many decoded frames mediabunny's decode pump keeps in flight; it belongs to
+// one mediabunny version and retires when upstream carries the fix. A bump
+// stops the install here until someone has looked.
+import { readdirSync, readFileSync } from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+const site = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
+const patches = readdirSync(path.join(site, "patches")).filter((f) => /^mediabunny\+.*\.patch$/.test(f));
+const installed = JSON.parse(readFileSync(path.join(site, "node_modules/mediabunny/package.json"), "utf8")).version;
+const sink = readFileSync(path.join(site, "node_modules/mediabunny/dist/modules/src/media-sink.js"), "utf8");
+
+const patched = patches.map((f) => f.slice("mediabunny+".length, -".patch".length));
+if (patched.length === 1 && patched[0] === installed && sink.includes("this.inFlight++")) process.exit(0);
+
+console.error(`
+mediabunny is ${installed}; the decode-pump patch in site/patches/ is for ${patched.join(", ") || "no version"}.
+
+Open node_modules/mediabunny/dist/modules/src/media-sink.js and read computeMaxQueueSize and
+the pump loops that call getDecodeQueueSize().
+ Upstream now counts frames in flight (submitted minus output) and starts with a small
+ head start: delete site/patches/mediabunny+*.patch, remove this script and patch-package
+ from postinstall and devDependencies, and run the perf eval's playback bucket.
+ Upstream still throttles on decodeQueueSize with a 40-packet head start: re-apply the
+ patch's six edits to the module and both bundles, run \`npx patch-package mediabunny\`,
+ and commit the new patch file.
+`);
+process.exit(1);