Skip to content

The free and privacy-friendly screen recorder with no limits 🎥

Faster recording starts, better encoder recovery on contended hardware, smoother stop flow + drawing persistence, and diag tooling

Alyssa X74e3a0d4e5c5
diff --git a/package-lock.json b/package-lock.json
index c49c768..0f75a4c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "screenity",
- "version": "4.4.0",
+ "version": "4.4.7",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "screenity",
- "version": "4.4.0",
+ "version": "4.4.7",
"hasInstallScript": true,
"dependencies": {
"@babel/plugin-transform-react-jsx": "^7.27.1",
@@ -31,12 +31,12 @@
"concurrently": "^8.2.0",
"dotenv": "^17.0.1",
"driver.js": "^1.3.6",
- "fabric": "^5.3.0",
+ "fabric": "^6.9.1",
"fix-webm-duration": "^1.0.5",
"gif.js": "^0.2.0",
"jszip": "^3.10.1",
"localforage": "^1.10.0",
- "mediabunny": "^1.40.1",
+ "mediabunny": "^1.45.3",
"patch-package": "^8.0.0",
"plyr": "3.7.8",
"plyr-react": "^5.3.0",
@@ -1951,6 +1951,7 @@
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.11.tgz",
"integrity": "sha512-Yhlar6v9WQgUp/He7BdgzOz8lqMQ8sU+jkCq7Wx8Myc5YFJLbEe7lgui/V7G1qB1DJykHSGwreceSaD60Y0PUQ==",
+ "license": "BSD-3-Clause",
"optional": true,
"dependencies": {
"detect-libc": "^2.0.0",
@@ -1968,9 +1969,10 @@
}
},
"node_modules/@mapbox/node-pre-gyp/node_modules/semver": {
- "version": "7.7.3",
- "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.3.tgz",
- "integrity": "sha512-SdsKMrI9TdgjdweUSR9MweHA4EJ8YxHn8DFaDisvhVlUOe4BF1tLD7GAj0lIqWVl+dPb/rExr0Btby5loQm20Q==",
+ "version": "7.8.0",
+ "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.0.tgz",
+ "integrity": "sha512-AcM7dV/5ul4EekoQ29Agm5vri8JNqRyj39o0qpX6vDF2GZrtutZl5RwgD1XnZjiTAfncsJhMI48QQH3sN87YNA==",
+ "license": "ISC",
"optional": true,
"bin": {
"semver": "bin/semver.js"
@@ -4032,9 +4034,10 @@
}
},
"node_modules/@tootallnate/once": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",
- "integrity": "sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A==",
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.1.tgz",
+ "integrity": "sha512-HqmEUIGRJ5fSXchkVgR5F7qn48bDBzv0kWj/Kfu5e6uci4UlEeng4331LnBkWffb++Ei3FOVLxo8JJWMFBDMeQ==",
+ "license": "MIT",
"optional": true,
"engines": {
"node": ">= 10"
@@ -4551,6 +4554,7 @@
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
"integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==",
+ "license": "ISC",
"optional": true
},
"node_modules/accepts": {
@@ -4587,25 +4591,14 @@
}
},
"node_modules/acorn-globals": {
- "version": "6.0.0",
- "resolved": "https://registry.npmjs.org/acorn-globals/-/acorn-globals-6.0.0.tgz",
- "integrity": "sha512-ZQl7LOWaF5ePqqcX4hLuv/bLXYQNfNWw2c0/yX/TsPRKamzHcTGQnlCjHT3TsmkOUVEPS3crCxiPfdzE/Trlhg==",
+ "version": "7.0.1",
+ "resolved": "https://registry.npmjs.org/acorn-globals/-/acorn-globals-7.0.1.tgz",
+ "integrity": "sha512-umOSDSDrfHbTNPuNpC2NSnnA3LUrqpevPb4T9jRx4MagXNS0rs+gwiTcAvqCRmsD6utzsrzNt+ebm00SNWiC3Q==",
+ "license": "MIT",
"optional": true,
"dependencies": {
- "acorn": "^7.1.1",
- "acorn-walk": "^7.1.1"
- }
- },
- "node_modules/acorn-globals/node_modules/acorn": {
- "version": "7.4.1",
- "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.4.1.tgz",
- "integrity": "sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==",
- "optional": true,
- "bin": {
- "acorn": "bin/acorn"
- },
- "engines": {
- "node": ">=0.4.0"
+ "acorn": "^8.1.0",
+ "acorn-walk": "^8.0.2"
}
},
"node_modules/acorn-import-phases": {
@@ -4621,10 +4614,14 @@
}
},
"node_modules/acorn-walk": {
- "version": "7.2.0",
- "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-7.2.0.tgz",
- "integrity": "sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA==",
+ "version": "8.3.5",
+ "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.5.tgz",
+ "integrity": "sha512-HEHNfbars9v4pgpW6SO1KSPkfoS0xVOM/9UzkJltjlsHZmJasxg8aXkuZa7SMf8vKGIBhpUsPluQSqhJFCqebw==",
+ "license": "MIT",
"optional": true,
+ "dependencies": {
+ "acorn": "^8.11.0"
+ },
"engines": {
"node": ">=0.4.0"
}
@@ -4747,6 +4744,7 @@
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/aproba/-/aproba-2.1.0.tgz",
"integrity": "sha512-tLIEcj5GuR2RSTnxNKdkK0dJ/GrC7P38sUkiDmDuHfsHmbagTFAxDVIBltoklXEVIQ/f14IL8IMJ5pn9Hez1Ew==",
+ "license": "ISC",
"optional": true
},
"node_modules/are-we-there-yet": {
@@ -4754,6 +4752,7 @@
"resolved": "https://registry.npmjs.org/are-we-there-yet/-/are-we-there-yet-2.0.0.tgz",
"integrity": "sha512-Ci/qENmwHnsYo9xKIcUJN5LeDKdJ6R1Z1j9V/J5wyq8nh/mYPEpIKJbBZXtZjG04HiK7zV/p6Vs9952MrMeUIw==",
"deprecated": "This package is no longer supported.",
+ "license": "ISC",
"optional": true,
"dependencies": {
"delegates": "^1.0.0",
@@ -4767,6 +4766,7 @@
"version": "3.6.2",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz",
"integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"inherits": "^2.0.3",
@@ -5059,12 +5059,6 @@
"node": ">=8"
}
},
- "node_modules/browser-process-hrtime": {
- "version": "1.0.0",
- "resolved": "https://registry.npmjs.org/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz",
- "integrity": "sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==",
- "optional": true
- },
"node_modules/browserslist": {
"version": "4.28.1",
"resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.28.1.tgz",
@@ -5466,6 +5460,7 @@
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/color-support/-/color-support-1.1.3.tgz",
"integrity": "sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==",
+ "license": "ISC",
"optional": true,
"bin": {
"color-support": "bin.js"
@@ -5614,6 +5609,7 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz",
"integrity": "sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==",
+ "license": "ISC",
"optional": true
},
"node_modules/content-disposition": {
@@ -5953,12 +5949,14 @@
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/cssom/-/cssom-0.5.0.tgz",
"integrity": "sha512-iKuQcq+NdHqlAcwUY0o/HL69XQrUaQdMjmStJ8JFmUaiiQErlhrmuigkg/CU4E2J0IyUKUrMAgl36TvN67MqTw==",
+ "license": "MIT",
"optional": true
},
"node_modules/cssstyle": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/cssstyle/-/cssstyle-2.3.0.tgz",
"integrity": "sha512-AZL67abkUzIuvcHqk7c09cezpGNcxUxU4Ioi/05xHk4DQeTkWmGYftIE6ctU6AEt+Gn4n1lDStOtj7FKycP71A==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"cssom": "~0.3.6"
@@ -5971,6 +5969,7 @@
"version": "0.3.8",
"resolved": "https://registry.npmjs.org/cssom/-/cssom-0.3.8.tgz",
"integrity": "sha512-b0tGHbfegbhPJpxpiBPU2sCkigAqtM9O121le6bbOlgyV+NyGyCmVfJ6QW9eRjz8CpNfWEOYBIMIGRYkLwsIYg==",
+ "license": "MIT",
"optional": true
},
"node_modules/csstype": {
@@ -5988,6 +5987,7 @@
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/data-urls/-/data-urls-3.0.2.tgz",
"integrity": "sha512-Jy/tj3ldjZJo63sVAvg6LHt2mHvl4V6AgRAmNDtLdm7faqtsx+aJG42rsyCo9JCoRVKwPFzKlIPx3DIibwSIaQ==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"abab": "^2.0.6",
@@ -5998,19 +5998,6 @@
"node": ">=12"
}
},
- "node_modules/data-urls/node_modules/whatwg-url": {
- "version": "11.0.0",
- "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz",
- "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==",
- "optional": true,
- "dependencies": {
- "tr46": "^3.0.0",
- "webidl-conversions": "^7.0.0"
- },
- "engines": {
- "node": ">=12"
- }
- },
"node_modules/date-fns": {
"version": "2.30.0",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz",
@@ -6046,12 +6033,14 @@
"version": "10.6.0",
"resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.6.0.tgz",
"integrity": "sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==",
+ "license": "MIT",
"optional": true
},
"node_modules/decompress-response": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-4.2.1.tgz",
"integrity": "sha512-jOSne2qbyE+/r8G1VU+G/82LBs2Fs4LAsTiLSHOCOMZQl2OKZ6i8i4IyHemTe+/yIXOtTcRQMzPcgyhoFlqPkw==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"mimic-response": "^2.0.0"
@@ -6109,6 +6098,7 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz",
"integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==",
+ "license": "MIT",
"optional": true
},
"node_modules/depd": {
@@ -6134,6 +6124,7 @@
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz",
"integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==",
+ "license": "Apache-2.0",
"optional": true,
"engines": {
"node": ">=8"
@@ -6214,6 +6205,7 @@
"resolved": "https://registry.npmjs.org/domexception/-/domexception-4.0.0.tgz",
"integrity": "sha512-A2is4PLG+eeSfoTMA95/s4pvAoSo2mKtiM5jlHkAVewmiO8ISFTFKZjH7UAM1Atli/OT/7JHOrJRJiMKUZKYBw==",
"deprecated": "Use your platform's native DOMException instead",
+ "license": "MIT",
"optional": true,
"dependencies": {
"webidl-conversions": "^7.0.0"
@@ -6495,6 +6487,7 @@
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/escodegen/-/escodegen-2.1.0.tgz",
"integrity": "sha512-2NlIDTwUWJN0mRPQOdtQBzbUHvdGY2P1VXSyU83Q3xKxM7WHX2Ql8dKq782Q9TgQUNOLEzEYu9bzLNj1q88I5w==",
+ "license": "BSD-2-Clause",
"optional": true,
"dependencies": {
"esprima": "^4.0.1",
@@ -6538,6 +6531,7 @@
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
+ "license": "BSD-2-Clause",
"optional": true,
"bin": {
"esparse": "bin/esparse.js",
@@ -6705,15 +6699,16 @@
]
},
"node_modules/fabric": {
- "version": "5.5.2",
- "resolved": "https://registry.npmjs.org/fabric/-/fabric-5.5.2.tgz",
- "integrity": "sha512-krvnxxyhslNBAkG0SpxPxFVsD6YD2mTJByyPxV92E1BxXtjXEcWsTSJpzX8+2Jprvs7oOLYFAjHO3o1ZvjoBrQ==",
+ "version": "6.9.1",
+ "resolved": "https://registry.npmjs.org/fabric/-/fabric-6.9.1.tgz",
+ "integrity": "sha512-TqG08Xbt4rtlPsXgCjSUcZz/RsyEP57Qo21nCVRkw7zz9nR0co4SLkL9Q/zQh3tC1Yxap6M5jKFHUKV6SgPovg==",
+ "license": "MIT",
"engines": {
- "node": ">=14.0.0"
+ "node": ">=16.20.0"
},
"optionalDependencies": {
- "canvas": "^2.8.0",
- "jsdom": "^19.0.0"
+ "canvas": "^2.11.2",
+ "jsdom": "^20.0.1"
}
},
"node_modules/fast-deep-equal": {
@@ -7019,6 +7014,7 @@
"resolved": "https://registry.npmjs.org/gauge/-/gauge-3.0.2.tgz",
"integrity": "sha512-+5J6MS/5XksCuXq++uFRsnUd7Ovu1XenbeuIuNRJxYWjgQbPuFhT14lAvsWfqfAmnwluf1OwMjz39HjfLPci0Q==",
"deprecated": "This package is no longer supported.",
+ "license": "ISC",
"optional": true,
"dependencies": {
"aproba": "^1.0.3 || ^2.0.0",
@@ -7303,6 +7299,7 @@
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz",
"integrity": "sha512-8Rf9Y83NBReMnx0gFzA8JImQACstCYWUplepDa9xprwwtmgEZUF0h/i5xSA625zB/I37EtrswSST6OXxwaaIJQ==",
+ "license": "ISC",
"optional": true
},
"node_modules/hashery": {
@@ -7361,6 +7358,7 @@
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-3.0.0.tgz",
"integrity": "sha512-oWv4T4yJ52iKrufjnyZPkrN0CH3QnrUqdB6In1g5Fe1mia8GmF36gnfNySxoZtxD5+NmYw1EElVXiBk93UeskA==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"whatwg-encoding": "^2.0.0"
@@ -7536,6 +7534,7 @@
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-5.0.0.tgz",
"integrity": "sha512-n2hY8YdoRE1i7r6M0w9DIw5GgZN0G25P8zLCRQ8rjXtTU3vsNFBI/vWK/UIeE6g5MUUz6avwAPXmL6Fy9D/90w==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"@tootallnate/once": "2",
@@ -7963,6 +7962,7 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz",
"integrity": "sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==",
+ "license": "MIT",
"optional": true
},
"node_modules/is-running": {
@@ -8046,41 +8046,41 @@
}
},
"node_modules/jsdom": {
- "version": "19.0.0",
- "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-19.0.0.tgz",
- "integrity": "sha512-RYAyjCbxy/vri/CfnjUWJQQtZ3LKlLnDqj+9XLNnJPgEGeirZs3hllKR20re8LUZ6o1b1X4Jat+Qd26zmP41+A==",
+ "version": "20.0.3",
+ "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-20.0.3.tgz",
+ "integrity": "sha512-SYhBvTh89tTfCD/CRdSOm13mOBa42iTaTyfyEWBdKcGdPxPtLFBXuHR8XHb33YNYaP+lLbmSvBTsnoesCNJEsQ==",
+ "license": "MIT",
"optional": true,
"dependencies": {
- "abab": "^2.0.5",
- "acorn": "^8.5.0",
- "acorn-globals": "^6.0.0",
+ "abab": "^2.0.6",
+ "acorn": "^8.8.1",
+ "acorn-globals": "^7.0.0",
"cssom": "^0.5.0",
"cssstyle": "^2.3.0",
- "data-urls": "^3.0.1",
- "decimal.js": "^10.3.1",
+ "data-urls": "^3.0.2",
+ "decimal.js": "^10.4.2",
"domexception": "^4.0.0",
"escodegen": "^2.0.0",
"form-data": "^4.0.0",
"html-encoding-sniffer": "^3.0.0",
"http-proxy-agent": "^5.0.0",
- "https-proxy-agent": "^5.0.0",
+ "https-proxy-agent": "^5.0.1",
"is-potential-custom-element-name": "^1.0.1",
- "nwsapi": "^2.2.0",
- "parse5": "6.0.1",
- "saxes": "^5.0.1",
+ "nwsapi": "^2.2.2",
+ "parse5": "^7.1.1",
+ "saxes": "^6.0.0",
"symbol-tree": "^3.2.4",
- "tough-cookie": "^4.0.0",
- "w3c-hr-time": "^1.0.2",
- "w3c-xmlserializer": "^3.0.0",
+ "tough-cookie": "^4.1.2",
+ "w3c-xmlserializer": "^4.0.0",
"webidl-conversions": "^7.0.0",
"whatwg-encoding": "^2.0.0",
"whatwg-mimetype": "^3.0.0",
- "whatwg-url": "^10.0.0",
- "ws": "^8.2.3",
+ "whatwg-url": "^11.0.0",
+ "ws": "^8.11.0",
"xml-name-validator": "^4.0.0"
},
"engines": {
- "node": ">=12"
+ "node": ">=14"
},
"peerDependencies": {
"canvas": "^2.5.0"
@@ -8091,6 +8091,32 @@
}
}
},
+ "node_modules/jsdom/node_modules/entities": {
+ "version": "6.0.1",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-6.0.1.tgz",
+ "integrity": "sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g==",
+ "license": "BSD-2-Clause",
+ "optional": true,
+ "engines": {
+ "node": ">=0.12"
+ },
+ "funding": {
+ "url": "https://github.com/fb55/entities?sponsor=1"
+ }
+ },
+ "node_modules/jsdom/node_modules/parse5": {
+ "version": "7.3.0",
+ "resolved": "https://registry.npmjs.org/parse5/-/parse5-7.3.0.tgz",
+ "integrity": "sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw==",
+ "license": "MIT",
+ "optional": true,
+ "dependencies": {
+ "entities": "^6.0.0"
+ },
+ "funding": {
+ "url": "https://github.com/inikulin/parse5?sponsor=1"
+ }
+ },
"node_modules/jsesc": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz",
@@ -8356,6 +8382,7 @@
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz",
"integrity": "sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"semver": "^6.0.0"
@@ -8403,11 +8430,12 @@
}
},
"node_modules/mediabunny": {
- "version": "1.40.1",
- "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.40.1.tgz",
- "integrity": "sha512-HU/stGzAkdWaJIly6ypbUVgAUvT9kt39DIg0IaErR7/1fwtTmgUYs4i8uEPYcgcjPjbB9gtBmUXOLnXi6J2LDw==",
+ "version": "1.45.3",
+ "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.45.3.tgz",
+ "integrity": "sha512-GUCPYjR+5olLM7DRmupCXCmZkkSrKHVl1gyW2RztpObqLfrix19kWGf/9WgWzDW0g49DvfGXpl+zfArCx5HDMQ==",
"license": "MPL-2.0",
"workspaces": [
+ ".",
"packages/*"
],
"dependencies": {
@@ -8532,6 +8560,7 @@
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-2.1.0.tgz",
"integrity": "sha512-wXqjST+SLt7R009ySCglWBCFpjUygmCIfD790/kVbiGmUgfYGuB14PiTd5DwVxSV4NcYHjzMkoj5LjQZwTQLEA==",
+ "license": "MIT",
"optional": true,
"engines": {
"node": ">=8"
@@ -8633,9 +8662,10 @@
}
},
"node_modules/nan": {
- "version": "2.23.0",
- "resolved": "https://registry.npmjs.org/nan/-/nan-2.23.0.tgz",
- "integrity": "sha512-1UxuyYGdoQHcGg87Lkqm3FzefucTa0NAiOcuRsDmysep3c1LVCRK2krrUDafMWtjSG04htvAmvg96+SDknOmgQ==",
+ "version": "2.27.0",
+ "resolved": "https://registry.npmjs.org/nan/-/nan-2.27.0.tgz",
+ "integrity": "sha512-hC+0LidcL3XE4rp1C4H54KujgXKzbfyTngZTwBByQxsOxCEKZT0MPQ4hOKUH2jU1OYstqdDH4onyHPDzcV0XdQ==",
+ "license": "MIT",
"optional": true
},
"node_modules/nanoid": {
@@ -8688,9 +8718,10 @@
"optional": true
},
"node_modules/node-fetch": {
- "version": "2.6.13",
- "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.13.tgz",
- "integrity": "sha512-StxNAxh15zr77QvvkmveSQ8uCQ4+v5FkvNTj0OESmiHu+VRi/gXArXtkWMElOsOUNLtUEvI4yS+rdtOHZTwlQA==",
+ "version": "2.7.0",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
+ "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"whatwg-url": "^5.0.0"
@@ -8711,18 +8742,21 @@
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
+ "license": "MIT",
"optional": true
},
"node_modules/node-fetch/node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
+ "license": "BSD-2-Clause",
"optional": true
},
"node_modules/node-fetch/node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"tr46": "~0.0.3",
@@ -8749,6 +8783,7 @@
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",
"integrity": "sha512-Tbj67rffqceeLpcRXrT7vKAN8CwfPeIBgM7E6iBkmKLV7bEMwpGgYLGv0jACUsECaa/vuxP0IjEont6umdMgtQ==",
+ "license": "ISC",
"optional": true,
"dependencies": {
"abbrev": "1"
@@ -8786,6 +8821,7 @@
"resolved": "https://registry.npmjs.org/npmlog/-/npmlog-5.0.1.tgz",
"integrity": "sha512-AqZtDUWOMKs1G/8lwylVjrdYgqA4d9nu8hc+0gzRxlDb1I10+FHBGMXs6aiQHFdCUUlqH99MUMuLfzWDNDtfxw==",
"deprecated": "This package is no longer supported.",
+ "license": "ISC",
"optional": true,
"dependencies": {
"are-we-there-yet": "^2.0.0",
@@ -8807,9 +8843,10 @@
}
},
"node_modules/nwsapi": {
- "version": "2.2.22",
- "resolved": "https://registry.npmjs.org/nwsapi/-/nwsapi-2.2.22.tgz",
- "integrity": "sha512-ujSMe1OWVn55euT1ihwCI1ZcAaAU3nxUiDwfDQldc51ZXaB9m2AyOn6/jh1BLe2t/G8xd6uKG1UBF2aZJeg2SQ==",
+ "version": "2.2.23",
+ "resolved": "https://registry.npmjs.org/nwsapi/-/nwsapi-2.2.23.tgz",
+ "integrity": "sha512-7wfH4sLbt4M0gCDzGE6vzQBo0bfTKjU7Sfpqy/7gs1qBfYz2vEJH6vXcBKpO3+6Yu1telwd0t9HpyOoLEQQbIQ==",
+ "license": "MIT",
"optional": true
},
"node_modules/object-assign": {
@@ -9037,7 +9074,7 @@
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/parse5/-/parse5-6.0.1.tgz",
"integrity": "sha512-Ofn/CTFzRGTTxwpNEs9PP93gXShHcTq255nzRYSKe8AkVpZY7e1fpmTfOyoIvjP5HG7Z2ZM7VS9PPhQGW2pOpw==",
- "devOptional": true
+ "dev": true
},
"node_modules/parseurl": {
"version": "1.3.3",
@@ -9513,6 +9550,7 @@
"version": "1.15.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.15.0.tgz",
"integrity": "sha512-JZd3gMVBAVQkSs6HdNZo9Sdo0LNcQeMNP3CozBJb3JYC/QUYZTnKxP+f8oWRX4rHP5EurWxqAHTSwUCjlNKa1w==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"punycode": "^2.3.1"
@@ -9578,6 +9616,7 @@
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
"integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==",
+ "license": "MIT",
"optional": true
},
"node_modules/queue-microtask": {
@@ -10288,15 +10327,16 @@
}
},
"node_modules/saxes": {
- "version": "5.0.1",
- "resolved": "https://registry.npmjs.org/saxes/-/saxes-5.0.1.tgz",
- "integrity": "sha512-5LBh1Tls8c9xgGjw3QrMwETmTMVk0oFgvrFSvWx62llR2hcEInrKNZ2GZCCuuy2lvWrdl5jhbpeqc5hRYKFOcw==",
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/saxes/-/saxes-6.0.0.tgz",
+ "integrity": "sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==",
+ "license": "ISC",
"optional": true,
"dependencies": {
"xmlchars": "^2.2.0"
},
"engines": {
- "node": ">=10"
+ "node": ">=v12.22.7"
}
},
"node_modules/scheduler": {
@@ -10532,6 +10572,7 @@
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
"integrity": "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==",
+ "license": "ISC",
"optional": true
},
"node_modules/set-function-length": {
@@ -10703,12 +10744,14 @@
"url": "https://feross.org/support"
}
],
+ "license": "MIT",
"optional": true
},
"node_modules/simple-get": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/simple-get/-/simple-get-3.1.1.tgz",
"integrity": "sha512-CQ5LTKGfCpvE1K0n2us+kuMPbk/q0EKl82s4aheV9oXjFEz6W/Y7oQFVJuU6QG77hRT4Ghb5RURteF5vnWjupA==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"decompress-response": "^4.2.0",
@@ -11290,6 +11333,7 @@
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz",
"integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==",
+ "license": "MIT",
"optional": true
},
"node_modules/table": {
@@ -11439,6 +11483,7 @@
"version": "4.1.4",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.4.tgz",
"integrity": "sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==",
+ "license": "BSD-3-Clause",
"optional": true,
"dependencies": {
"psl": "^1.1.33",
@@ -11454,6 +11499,7 @@
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz",
"integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==",
+ "license": "MIT",
"optional": true,
"engines": {
"node": ">= 4.0.0"
@@ -11463,6 +11509,7 @@
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-3.0.0.tgz",
"integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"punycode": "^2.1.1"
@@ -11668,6 +11715,7 @@
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz",
"integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"querystringify": "^2.1.1",
@@ -11758,26 +11806,17 @@
"node": ">= 0.8"
}
},
- "node_modules/w3c-hr-time": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz",
- "integrity": "sha512-z8P5DvDNjKDoFIHK7q8r8lackT6l+jo/Ye3HOle7l9nICP9lf1Ci25fy9vHd0JOWewkIFzXIEig3TdKT7JQ5fQ==",
- "deprecated": "Use your platform's native performance.now() and performance.timeOrigin.",
- "optional": true,
- "dependencies": {
- "browser-process-hrtime": "^1.0.0"
- }
- },
"node_modules/w3c-xmlserializer": {
- "version": "3.0.0",
- "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-3.0.0.tgz",
- "integrity": "sha512-3WFqGEgSXIyGhOmAFtlicJNMjEps8b1MG31NCA0/vOF9+nKMUW1ckhi9cnNHmf88Rzw5V+dwIwsm2C7X8k9aQg==",
+ "version": "4.0.0",
+ "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-4.0.0.tgz",
+ "integrity": "sha512-d+BFHzbiCx6zGfz0HyQ6Rg69w9k19nviJspaj4yNscGjrHu94sVP+aRm75yEbCh+r2/yR+7q6hux9LVtbuTGBw==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"xml-name-validator": "^4.0.0"
},
"engines": {
- "node": ">=12"
+ "node": ">=14"
}
},
"node_modules/watchpack": {
@@ -11812,6 +11851,7 @@
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
"integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==",
+ "license": "BSD-2-Clause",
"optional": true,
"engines": {
"node": ">=12"
@@ -12116,6 +12156,8 @@
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-2.0.0.tgz",
"integrity": "sha512-p41ogyeMUrw3jWclHWTQg1k05DSVXPLcVxRTYsXUk+ZooOCZLcoYgPZ/HL/D/N+uQPOtcp1me1WhBEaX02mhWg==",
+ "deprecated": "Use @exodus/bytes instead for a more spec-conformant and faster implementation",
+ "license": "MIT",
"optional": true,
"dependencies": {
"iconv-lite": "0.6.3"
@@ -12128,15 +12170,17 @@
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz",
"integrity": "sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==",
+ "license": "MIT",
"optional": true,
"engines": {
"node": ">=12"
}
},
"node_modules/whatwg-url": {
- "version": "10.0.0",
- "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-10.0.0.tgz",
- "integrity": "sha512-CLxxCmdUby142H5FZzn4D8ikO1cmypvXVQktsgosNy4a4BHrDHeciBBGZhb0bNoR5/MltoCatso+vFjjGx8t0w==",
+ "version": "11.0.0",
+ "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz",
+ "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==",
+ "license": "MIT",
"optional": true,
"dependencies": {
"tr46": "^3.0.0",
@@ -12164,6 +12208,7 @@
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.5.tgz",
"integrity": "sha512-eDMORYaPNZ4sQIuuYPDHdQvf4gyCF9rEEV/yPxGfwPkRodwEgiMUUXTx/dex+Me0wxx53S+NgUHaP7y3MGlDmg==",
+ "license": "ISC",
"optional": true,
"dependencies": {
"string-width": "^1.0.2 || 2 || 3 || 4"
@@ -12249,6 +12294,7 @@
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-4.0.0.tgz",
"integrity": "sha512-ICP2e+jsHvAj2E2lIHxa5tjXRlKDJo4IdvPvCXbXQGdzSfmSpNVyIKMvoZHjDY9DP0zV17iI85o90vRFXNccRw==",
+ "license": "Apache-2.0",
"optional": true,
"engines": {
"node": ">=12"
@@ -12258,6 +12304,7 @@
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz",
"integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==",
+ "license": "MIT",
"optional": true
},
"node_modules/y18n": {
diff --git a/package.json b/package.json
index 9fd5316..7dd1db1 100755
--- a/package.json
+++ b/package.json
@@ -1,10 +1,12 @@
{
"name": "screenity",
- "version": "4.4.7",
+ "version": "4.4.8",
"description": "The free and privacy-friendly screen recorder with no sign in needed.",
"scripts": {
- "build": "cross-env NODE_ENV=development node utils/build.js",
+ "build": "cross-env NODE_ENV=production node utils/build.js",
+ "build:dev": "cross-env NODE_ENV=development node utils/build.js",
"build:prod": "cross-env NODE_ENV=production node utils/build.js",
+ "build:local": "cross-env NODE_ENV=production SCREENITY_USE_LOCAL_ENV=1 node utils/build.js",
"build:release": "cross-env NODE_ENV=production SCREENITY_SKIP_ENV=true node utils/build.js",
"build:bs": "cross-env NODE_ENV=production SCREENITY_BS_BUILD=1 node utils/build.js",
"start": "cross-env NODE_ENV=development node utils/server.js",
@@ -53,12 +55,12 @@
"concurrently": "^8.2.0",
"dotenv": "^17.0.1",
"driver.js": "^1.3.6",
- "fabric": "^5.3.0",
+ "fabric": "^6.9.1",
"fix-webm-duration": "^1.0.5",
"gif.js": "^0.2.0",
"jszip": "^3.10.1",
"localforage": "^1.10.0",
- "mediabunny": "^1.40.1",
+ "mediabunny": "^1.45.3",
"patch-package": "^8.0.0",
"plyr": "3.7.8",
"plyr-react": "^5.3.0",
diff --git a/src/manifest.json b/src/manifest.json
index 9da944e..bd3a09c 100755
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -3,7 +3,7 @@
"name": "__MSG_extName__",
"description": "__MSG_extDesc__",
"default_locale": "en",
- "version": "4.4.7",
+ "version": "4.4.8",
"background": {
"service_worker": "background.bundle.js"
},
diff --git a/src/media/fastRecorderGate.ts b/src/media/fastRecorderGate.ts
index fd9dc0d..2279068 100644
--- a/src/media/fastRecorderGate.ts
+++ b/src/media/fastRecorderGate.ts
@@ -146,7 +146,236 @@ export const markFastRecorderFailure = async (
}
};
+// Round-trip 4 synthetic frames through a real VideoEncoder to
+// confirm it emits chunks. isConfigSupported() only validates shape
+// and misses the "28-byte ftyp" failure. 30 frames would also catch
+// first-keyframe-only bugs, but ~900ms/frame under pressure = 27s of
+// blocking; runtime watchdogs handle that. 1.5s flush cap: any chunk
+// landed = healthy-but-slow, zero chunks = no-output.
+const PROBE_FRAME_COUNT = 4;
+const PROBE_WALL_CLOCK_CAP_MS = 1500;
+const verifyEncoderProducesOutput = async (
+ config: VideoEncoderConfig
+): Promise<{ ok: boolean; reason?: string; chunks: number; ms: number }> => {
+ const started = Date.now();
+ if (
+ typeof VideoEncoder === "undefined" ||
+ typeof VideoFrame === "undefined" ||
+ typeof OffscreenCanvas === "undefined"
+ ) {
+ // Can't run the round-trip in this context; report it as
+ // unverified rather than penalizing the probe.
+ return { ok: true, reason: "unverified", chunks: 0, ms: 0 };
+ }
+ let chunks = 0;
+ let encoderError: string | null = null;
+ let encoder: VideoEncoder | null = null;
+ const frames: VideoFrame[] = [];
+ const width = Number(config.width) || 1280;
+ const height = Number(config.height) || 720;
+ try {
+ encoder = new VideoEncoder({
+ output: () => {
+ chunks += 1;
+ },
+ error: (e: any) => {
+ encoderError = String(e?.message || e);
+ },
+ });
+ encoder.configure(config);
+ const canvas = new OffscreenCanvas(width, height);
+ // getContext('2d') returns the union OffscreenRenderingContext; cast
+ // narrows to the only variant we use so strict TS (build:bs) accepts
+ // fillStyle/fillRect without complaining about ImageBitmapRenderingContext.
+ const ctx = canvas.getContext("2d") as OffscreenCanvasRenderingContext2D | null;
+ const frameDurationUs = Math.round(1_000_000 / 30);
+ for (let i = 0; i < PROBE_FRAME_COUNT; i += 1) {
+ if (ctx) {
+ ctx.fillStyle = i % 2 === 0 ? "#1a1a1a" : "#e8e8e8";
+ ctx.fillRect(0, 0, width, height);
+ }
+ const frame = new VideoFrame(canvas, {
+ timestamp: i * frameDurationUs,
+ duration: frameDurationUs,
+ });
+ frames.push(frame);
+ encoder.encode(frame, { keyFrame: i === 0 });
+ }
+ // Wall-clock-bounded flush. A slow but healthy encoder can drain
+ // for tens of seconds under system memory/GPU pressure; we don't
+ // make the user wait. If timeout hits, the chunks counter has
+ // whatever the encoder produced; we'll classify below.
+ let flushTimedOut = false;
+ let flushTimer: ReturnType<typeof setTimeout> | null = null;
+ try {
+ await Promise.race([
+ encoder.flush(),
+ new Promise<void>((_, reject) => {
+ flushTimer = setTimeout(() => {
+ flushTimedOut = true;
+ reject(new Error("flush-timeout"));
+ }, PROBE_WALL_CLOCK_CAP_MS);
+ }),
+ ]);
+ } catch (err: any) {
+ // If we timed out and at least one chunk landed, that's an OK
+ // signal; encoder is producing output, just slow. Don't bubble
+ // an error in that case.
+ if (!flushTimedOut || chunks === 0) {
+ encoderError = encoderError || String(err?.message || err);
+ }
+ } finally {
+ if (flushTimer) clearTimeout(flushTimer);
+ }
+ } catch (err: any) {
+ encoderError = encoderError || String(err?.message || err);
+ } finally {
+ for (const f of frames) {
+ try {
+ f.close();
+ } catch {}
+ }
+ try {
+ encoder?.close();
+ } catch {}
+ }
+ const ms = Date.now() - started;
+ if (encoderError) return { ok: false, reason: "error", chunks, ms };
+ // The only firm fail signal is zero output. With the small frame
+ // count + bounded flush, "too-few-chunks" heuristics produce false
+ // negatives (a slow but healthy encoder might only land 1 chunk
+ // within the cap). Runtime watchdogs in WebCodecsRecorder catch
+ // the rest.
+ if (chunks === 0) return { ok: false, reason: "no-output", chunks, ms };
+ return { ok: true, chunks, ms };
+};
+
+// Round-trip 1s of synthetic audio through a real AudioEncoder. Same
+// purpose as the video probe: isConfigSupported says "yes" but the
+// encoder might still fail at first encode under platform-specific
+// constraints (Opus 48k internal mismatch, AAC channel layout issues,
+// etc). Cheap; ~10 AudioData inputs at 4800 samples each.
+const verifyAudioEncoderProducesOutput = async (
+ config: AudioEncoderConfig
+): Promise<{ ok: boolean; reason?: string; chunks: number; ms: number }> => {
+ const started = Date.now();
+ if (typeof AudioEncoder === "undefined" || typeof AudioData === "undefined") {
+ return { ok: true, reason: "unverified", chunks: 0, ms: 0 };
+ }
+ let chunks = 0;
+ let encoderError: string | null = null;
+ let encoder: AudioEncoder | null = null;
+ const sampleRate = config.sampleRate || 48000;
+ const channels = config.numberOfChannels || 2;
+ // 10 chunks × 4800 samples = ~1s at 48kHz.
+ const CHUNKS = 10;
+ const SAMPLES_PER_CHUNK = Math.round(sampleRate / 10);
+ try {
+ encoder = new AudioEncoder({
+ output: () => {
+ chunks += 1;
+ },
+ error: (e: any) => {
+ encoderError = String(e?.message || e);
+ },
+ });
+ encoder.configure(config);
+ // Interleaved f32 silence; zero buffer keeps it deterministic and
+ // doesn't allocate per-channel.
+ const data = new Float32Array(SAMPLES_PER_CHUNK * channels);
+ for (let i = 0; i < CHUNKS; i += 1) {
+ const audioData = new AudioData({
+ format: "f32",
+ sampleRate,
+ numberOfFrames: SAMPLES_PER_CHUNK,
+ numberOfChannels: channels,
+ timestamp: Math.round((i * SAMPLES_PER_CHUNK * 1_000_000) / sampleRate),
+ data,
+ });
+ try {
+ encoder.encode(audioData);
+ } finally {
+ audioData.close();
+ }
+ }
+ await encoder.flush();
+ } catch (err: any) {
+ encoderError = encoderError || String(err?.message || err);
+ } finally {
+ try {
+ encoder?.close();
+ } catch {}
+ }
+ const ms = Date.now() - started;
+ if (encoderError) return { ok: false, reason: "error", chunks, ms };
+ if (chunks === 0) return { ok: false, reason: "no-output", chunks, ms };
+ return { ok: true, chunks, ms };
+};
+
+// Probe cache TTL. Probe is 250-350ms warm, seconds cold; re-running
+// every start showed up as countdown-to-record delay. Key is
+// { userAgent + GATE_VERSION }. ok=false isn't cached so a driver
+// recovery re-probes immediately.
+const PROBE_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
+// Short window for failed probes: coalesces back-to-back calls during
+// one startup without masking driver recovery.
+const PROBE_FAILURE_CACHE_TTL_MS = 60 * 1000;
+let _probeInMemory: FastRecorderProbeResult | null = null;
+let _probeInMemoryAt = 0;
+let _probeInFlight: Promise<FastRecorderProbeResult> | null = null;
+
+const tryReadCachedProbe = async (): Promise<FastRecorderProbeResult | null> => {
+ if (_probeInMemory) {
+ if (_probeInMemory.ok) return _probeInMemory;
+ if (Date.now() - _probeInMemoryAt < PROBE_FAILURE_CACHE_TTL_MS) {
+ return _probeInMemory;
+ }
+ }
+ try {
+ const ua = typeof navigator !== "undefined" ? navigator.userAgent : "";
+ const { [STORAGE_KEYS.probe]: cached } = await chrome.storage.local.get([
+ STORAGE_KEYS.probe,
+ ]);
+ if (
+ cached &&
+ cached.ok === true &&
+ typeof cached.at === "number" &&
+ Date.now() - cached.at < PROBE_CACHE_TTL_MS &&
+ cached.details?.userAgent === ua &&
+ cached.details?.gateVersion === GATE_VERSION
+ ) {
+ _probeInMemory = cached;
+ return cached;
+ }
+ } catch {}
+ return null;
+};
+
export const probeFastRecorderSupport = async (): Promise<FastRecorderProbeResult> => {
+ const cached = await tryReadCachedProbe();
+ if (cached) return cached;
+ if (_probeInFlight) return _probeInFlight;
+ _probeInFlight = _probeFastRecorderSupportUncached().finally(() => {
+ _probeInFlight = null;
+ });
+ const result = await _probeInFlight;
+ // Cache both success and failure in memory. Success rides the long
+ // TTL via storage; failure rides the short in-memory TTL only so a
+ // hardware/driver fix is rediscovered within a minute.
+ _probeInMemory = result;
+ _probeInMemoryAt = Date.now();
+ return result;
+};
+
+// Best-effort pre-warm: kicks the probe asynchronously so the result is
+// ready in memory by the time preflight needs it. Safe to call multiple
+// times; coalesces via _probeInFlight.
+export const prewarmFastRecorderProbe = (): void => {
+ if (_probeInMemory || _probeInFlight) return;
+ void probeFastRecorderSupport().catch(() => {});
+};
+
+const _probeFastRecorderSupportUncached = async (): Promise<FastRecorderProbeResult> => {
try {
debugLog("probe start", GATE_VERSION, Date.now());
const reasons: string[] = [];
@@ -321,6 +550,7 @@ export const probeFastRecorderSupport = async (): Promise<FastRecorderProbeResul
const isLinux = /Linux/i.test(ua);
details.userAgent = ua;
details.isLinux = isLinux;
+ details.gateVersion = GATE_VERSION;
if (isLinux && playableCodecs.length === 0) {
reasons.push("linux-missing-codecs");
@@ -394,14 +624,100 @@ export const probeFastRecorderSupport = async (): Promise<FastRecorderProbeResul
if (containerKind === "mp4") details.containerKind = "mp4";
- const ok = reasons.length === 0;
+ // Verify the selected encoder actually produces output, not just
+ // that the config is "supported". This is the proactive guard:
+ // a machine whose encoder emits zero frames is caught here and
+ // routed to MediaRecorder, instead of the user losing a recording.
+ if (details.selectedVideoConfig) {
+ // Retry once on transient encoder errors. VTDecoderXPCService
+ // (macOS), NVIDIA driver hand-offs (Windows), intel-VAAPI
+ // (Linux) all reject the first configure() right after another
+ // encoder ran, then succeed on the retry. Without this, a one-
+ // off "error" reason stickied MediaRecorder for the session.
+ // "no-output" is a real HW bug and does NOT retry.
+ let encodeCheck = await verifyEncoderProducesOutput(
+ details.selectedVideoConfig as VideoEncoderConfig
+ );
+ if (!encodeCheck.ok && encodeCheck.reason === "error") {
+ await new Promise((r) => setTimeout(r, 200));
+ const retry = await verifyEncoderProducesOutput(
+ details.selectedVideoConfig as VideoEncoderConfig
+ );
+ details.encodeRoundTripRetry = retry;
+ if (retry.ok) {
+ encodeCheck = retry;
+ }
+ }
+ details.encodeRoundTrip = encodeCheck;
+ if (!encodeCheck.ok) {
+ reasons.push(`video-encode-${encodeCheck.reason}`);
+ }
+ }
+
+ // Audio round-trip guard. Catches Opus 48k mismatch and AAC
+ // platform configure-time bugs that isConfigSupported misses.
+ // Doesn't block video; just tags the audio reason separately.
+ if (details.audioConfig && (details as any).audioConfigSupported) {
+ const audioEncodeCheck = await verifyAudioEncoderProducesOutput(
+ details.audioConfig as AudioEncoderConfig
+ );
+ details.audioEncodeRoundTrip = audioEncodeCheck;
+ if (!audioEncodeCheck.ok) {
+ reasons.push(`audio-encode-${audioEncodeCheck.reason}`);
+ }
+ }
+
+ let ok = reasons.length === 0;
const at = Date.now();
+
+ // Optimistic override: if the only failures are transient encode
+ // errors and a clean probe ran in the last 7 days, trust it.
+ // In-session swap to MediaRecorder is wired up; the cost of a
+ // false-negative IDB recording is higher than a WebCodecs retry.
+ if (!ok) {
+ const onlyTransientReasons = reasons.every((r) =>
+ r === "video-encode-error" || r === "audio-encode-error"
+ );
+ if (onlyTransientReasons) {
+ try {
+ const ua = typeof navigator !== "undefined" ? navigator.userAgent : "";
+ const { [STORAGE_KEYS.probe]: prior } =
+ await chrome.storage.local.get([STORAGE_KEYS.probe]);
+ const PROBE_TRUST_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
+ if (
+ prior &&
+ prior.ok === true &&
+ typeof prior.at === "number" &&
+ Date.now() - prior.at < PROBE_TRUST_WINDOW_MS &&
+ prior.details?.userAgent === ua &&
+ prior.details?.gateVersion === GATE_VERSION
+ ) {
+ details.transientFailureOverridden = true;
+ details.transientReasons = reasons.slice();
+ ok = true;
+ }
+ } catch {}
+ }
+ }
+
debugLog("probe result", { ok, reasons, details, at });
try {
- await chrome.storage.local.set({
- [STORAGE_KEYS.probe]: { ok, reasons, details, at },
- });
+ if (ok) {
+ // Refresh the storage probe on success: longest TTL applies.
+ await chrome.storage.local.set({
+ [STORAGE_KEYS.probe]: { ok, reasons, details, at },
+ });
+ } else {
+ // Don't overwrite a known-good cached probe with a failure
+ // (one bad VTDecoder run shouldn't wipe a week of confidence).
+ // But also persist the failure separately so the next start
+ // can short-circuit a re-probe for the in-memory TTL window
+ // even if the SW restarted between recordings.
+ await chrome.storage.local.set({
+ fastRecorderProbeLastFailure: { reasons, details, at },
+ });
+ }
} catch {}
return { ok, reasons, details, at };
diff --git a/src/pages/Background/alarms/handleAlarm.js b/src/pages/Background/alarms/handleAlarm.js
index 3c116a4..604e71e 100644
--- a/src/pages/Background/alarms/handleAlarm.js
+++ b/src/pages/Background/alarms/handleAlarm.js
@@ -7,6 +7,7 @@ import { diagEvent } from "../../utils/diagnosticLog";
import { lifecycle } from "../../utils/lifecycleLog";
import { chunksStore } from "../recording/chunkHandler";
import { emitRecordingTelemetry } from "../recording/emitRecordingTelemetry";
+import { markFastRecorderFailure } from "../../../media/fastRecorderGate";
import {
CLOUD_LOCAL_PLAYBACK_KEY,
CLOUD_LOCAL_PLAYBACK_EVENT_KEY,
@@ -105,7 +106,7 @@ export const handleAlarm = async (alarm) => {
lastChunkAt: null,
});
// Backstop: the session is over. Remove any recorder tab still
- // alive — a final guard if both the recorder's own abandonment
+ // alive; a final guard if both the recorder's own abandonment
// listener and the teardown sweep somehow missed it. Gated on no
// active or in-flight recording so a fresh start is never killed.
if (!snap.recording && !snap.pendingRecording) {
@@ -231,6 +232,24 @@ export const handleAlarm = async (alarm) => {
const { recording } = await chrome.storage.local.get(["recording"]);
if (recording) {
diagEvent("error", { note: "first-chunk-watchdog-fired" });
+ // If the fast (WebCodecs) recorder was in use, a missing first chunk
+ // is almost always a silently-failed WebCodecs encoder (the
+ // "28-byte ftyp" stall). Sticky-disable it now so the user's retry
+ // falls back to MediaRecorder and succeeds, instead of hitting
+ // WebCodecs again and failing identically. (markFastRecorderFailure
+ // ignores transient stream errors via its own pattern list.)
+ try {
+ const { fastRecorderInUse } = await chrome.storage.local.get([
+ "fastRecorderInUse",
+ ]);
+ if (fastRecorderInUse) {
+ await markFastRecorderFailure("webcodecs-no-first-chunk", {
+ error: "WebCodecs produced no first chunk within 8s (watchdog)",
+ });
+ }
+ } catch (err) {
+ console.warn("[Screenity][BG] watchdog sticky-disable failed", err);
+ }
// 1500ms cap so a wedged tab can't delay the user-facing error
let snapshot = null;
let snapshotError = null;
diff --git a/src/pages/Background/auth/loginWithWebsite.js b/src/pages/Background/auth/loginWithWebsite.js
index c750072..212e192 100644
--- a/src/pages/Background/auth/loginWithWebsite.js
+++ b/src/pages/Background/auth/loginWithWebsite.js
@@ -30,21 +30,26 @@ export const loginWithWebsite = async (_depth = 0) => {
return { authenticated: false, instantMode: false };
}
- // cleared by handle-login when user re-initiates login
- const { stayLoggedOut } = await chrome.storage.local.get(["stayLoggedOut"]);
- if (stayLoggedOut) {
- return { authenticated: false, instantMode: false };
- }
-
- const { screenityToken, lastAuthCheck, instantMode } =
+ // Single storage IPC for the initial branch decision. The three
+ // separate gets here added ~30-60ms of serial round-trips on the
+ // start critical path; consolidating doesn't change semantics but
+ // shaves a few ms off every recording start.
+ const { stayLoggedOut, screenityToken, lastAuthCheck, instantMode } =
await chrome.storage.local.get([
+ "stayLoggedOut",
"screenityToken",
"lastAuthCheck",
"instantMode",
]);
+ if (stayLoggedOut) {
+ return { authenticated: false, instantMode: false };
+ }
+
if (!screenityToken) {
- await chrome.storage.local.set({ isLoggedIn: false });
+ // Don't block the start path on this write; it's just a UI flag
+ // that will get re-read on next mount.
+ chrome.storage.local.set({ isLoggedIn: false });
return { authenticated: false, instantMode: false };
}
diff --git a/src/pages/Background/lifecycleObserver.js b/src/pages/Background/lifecycleObserver.js
index 4b7b7b7..350ea7f 100644
--- a/src/pages/Background/lifecycleObserver.js
+++ b/src/pages/Background/lifecycleObserver.js
@@ -55,5 +55,22 @@ export const initLifecycleObserver = () => {
to: summarizeValue(newValue),
});
}
+
+ // Reactive icon sync. Anchors the action icon to storage.recording
+ // so it can never drift out of sync when an imperative
+ // chrome.action.setIcon call is missed (e.g. a stop path
+ // interrupted by SW teardown). Imperative setIcon calls in the
+ // start/stop sites still run; this is the safety net.
+ if (changes.recording && changes.recording.oldValue !== changes.recording.newValue) {
+ try {
+ chrome.action.setIcon({
+ path: changes.recording.newValue
+ ? "assets/recording-logo.png"
+ : "assets/icon-34.png",
+ });
+ } catch (err) {
+ console.warn("[Screenity][BG] reactive setIcon failed:", err);
+ }
+ }
});
};
diff --git a/src/pages/Background/listeners/onInstalledListener.js b/src/pages/Background/listeners/onInstalledListener.js
index 62c1b8b..85391a9 100644
--- a/src/pages/Background/listeners/onInstalledListener.js
+++ b/src/pages/Background/listeners/onInstalledListener.js
@@ -74,8 +74,12 @@ export const onInstalledListener = () => {
removeTab(backupTab);
}
- // update only; manifest auto-injects on page load. install would double-mount React on dev.
- if (details.reason === "update") {
+ // Backfill content scripts into already-open tabs. manifest
+ // content_scripts only auto-inject on future loads; without this,
+ // a fresh install / update can't record on tabs that were already
+ // open. The `install` gate causes React double-mount under dev
+ // HMR; accepted tradeoff vs the prod break.
+ if (details.reason === "install" || details.reason === "update") {
executeScripts();
}
diff --git a/src/pages/Background/listeners/onTabRemovedListener.js b/src/pages/Background/listeners/onTabRemovedListener.js
index 8dfe9e8..4449ba7 100644
--- a/src/pages/Background/listeners/onTabRemovedListener.js
+++ b/src/pages/Background/listeners/onTabRemovedListener.js
@@ -90,6 +90,25 @@ export const onTabRemovedListener = () => {
if (recorderSession && recorderSession.status === "recording") {
diagEvent("crash", { reason: "recorder-owner-tab-removed", tabId });
+ // Same multi-preserve guard as below; don't nuke
+ // multiProjectId when the user discarded scene N (N>1) of a
+ // multi-recording. The already-saved scenes' project must
+ // survive so the user's "Done" click reaches it.
+ const {
+ multiMode: preservedMultiMode,
+ multiSceneCount: preservedSceneCount,
+ multiProjectId: preservedProjectId,
+ multiLastSceneId: preservedLastSceneId,
+ } = await chrome.storage.local.get([
+ "multiMode",
+ "multiSceneCount",
+ "multiProjectId",
+ "multiLastSceneId",
+ ]);
+ const hasSavedMultiScenes =
+ preservedMultiMode &&
+ Number(preservedSceneCount) > 0 &&
+ preservedProjectId;
await chrome.storage.local.set({
recorderSession: {
...recorderSession,
@@ -97,10 +116,19 @@ export const onTabRemovedListener = () => {
crashedAt: Date.now(),
},
recording: false,
- multiMode: false,
- multiSceneCount: 0,
- multiProjectId: null,
- multiLastSceneId: null,
+ ...(hasSavedMultiScenes
+ ? {
+ multiMode: preservedMultiMode,
+ multiSceneCount: preservedSceneCount,
+ multiProjectId: preservedProjectId,
+ multiLastSceneId: preservedLastSceneId,
+ }
+ : {
+ multiMode: false,
+ multiSceneCount: 0,
+ multiProjectId: null,
+ multiLastSceneId: null,
+ }),
});
endDiagSession("crashed");
}
@@ -137,6 +165,27 @@ export const onTabRemovedListener = () => {
diagEvent("crash", { reason: "recorder-tab-closed", tabId });
endDiagSession("crashed");
console.error("Recorder tab was closed during recording!");
+ // Preserve multi-project state when scenes are already saved.
+ // Without this, discarding scene N (N>1) wiped multiProjectId
+ // because the cloudrecorder tab close races BG's
+ // `recording:false` write and we'd see recorderSession.status
+ // still "recording". The user would then click "Done" on their
+ // scene 1 and hit "No project ID for multi recording".
+ const {
+ multiMode: preservedMultiMode,
+ multiSceneCount: preservedSceneCount,
+ multiProjectId: preservedProjectId,
+ multiLastSceneId: preservedLastSceneId,
+ } = await chrome.storage.local.get([
+ "multiMode",
+ "multiSceneCount",
+ "multiProjectId",
+ "multiLastSceneId",
+ ]);
+ const hasSavedMultiScenes =
+ preservedMultiMode &&
+ Number(preservedSceneCount) > 0 &&
+ preservedProjectId;
chrome.storage.local.set({
recording: false,
recordingTab: null,
@@ -146,10 +195,19 @@ export const onTabRemovedListener = () => {
recorderSession: recorderSession
? { ...recorderSession, status: "crashed", crashedAt: Date.now() }
: null,
- multiMode: false,
- multiSceneCount: 0,
- multiProjectId: null,
- multiLastSceneId: null,
+ ...(hasSavedMultiScenes
+ ? {
+ multiMode: preservedMultiMode,
+ multiSceneCount: preservedSceneCount,
+ multiProjectId: preservedProjectId,
+ multiLastSceneId: preservedLastSceneId,
+ }
+ : {
+ multiMode: false,
+ multiSceneCount: 0,
+ multiProjectId: null,
+ multiLastSceneId: null,
+ }),
});
chrome.action.setIcon({ path: "assets/icon-34.png" });
}
diff --git a/src/pages/Background/messaging/handlers.js b/src/pages/Background/messaging/handlers.js
index 5fd6338..898df6d 100644
--- a/src/pages/Background/messaging/handlers.js
+++ b/src/pages/Background/messaging/handlers.js
@@ -1,3 +1,4 @@
+import JSZip from "jszip";
import { registerMessage } from "../../../messaging/messageRouter";
import { perfMark, perfSpan } from "../../utils/perfMarks";
import {
@@ -86,7 +87,7 @@ const API_BASE = process.env.SCREENITY_API_BASE_URL;
const APP_BASE = process.env.SCREENITY_APP_BASE;
// Flip a project to isPublic:true after recording finishes. v2 removed
-// the publish system, so isPublic is the only access gate — without
+// the publish system, so isPublic is the only access gate; without
// this PATCH the share URL we copy to clipboard would 404 for viewers.
// Fire-and-forget; clipboard/toast UX is identical on success or failure.
const markProjectPublic = async (projectId) => {
@@ -109,6 +110,11 @@ const markProjectPublic = async (projectId) => {
const CLOUD_FEATURES_ENABLED =
process.env.SCREENITY_ENABLE_CLOUD_FEATURES === "true";
const DEBUG_POSTSTOP = false;
+// Gate for the start-flow / stop-flow / offscreen-diag console mirrors.
+// Off in prod by default; set globalThis.SCREENITY_DEBUG_RECORDER for support.
+const DEBUG_FLOW =
+ process.env.NODE_ENV !== "production" ||
+ (typeof globalThis !== "undefined" && !!globalThis.SCREENITY_DEBUG_RECORDER);
const STOP_RECORDING_TAB_DEBOUNCE_MS = 1200;
const CLOUD_LOCAL_PLAYBACK_MAX_BYTES = 250 * 1024 * 1024;
const CLOUD_LOCAL_PLAYBACK_MAX_CHUNKS = 4000;
@@ -809,35 +815,107 @@ export const setupHandlers = () => {
return { ok: false, error: err?.message || String(err) };
}
});
- // Forward a scene-create payload to the editor tab, which does the
- // POST itself (same-origin cookie auth). Sidesteps the MV3 SW-fetch
- // hang we hit when the cloud recorder tab tears down right after
- // dispatching the request. The editor confirms via reply message.
+ // CloudRecorder mirrors each step of its stop→finalize→close
+ // sequence here so the timeline survives the tab's window.close().
+ // Read the BG service worker console (chrome://extensions → service
+ // worker → inspect) to see the full sequence end-to-end.
+ registerMessage("stop-flow-tick", (message) => {
+ if (!DEBUG_FLOW) return { ok: true };
+ const t = message?.t ?? "?";
+ const label = message?.label || "?";
+ const extra = message?.extra || {};
+ console.warn(`[stop-flow T+${t}ms] ${label}`, extra);
+ return { ok: true };
+ });
+
+ // Mirror of start-flow events from the cloud recorder tab. Same
+ // rationale as stop-flow-tick: visible in BG console even when the
+ // recorder tab closed before the user could read it.
+ registerMessage("start-flow-tick", (message) => {
+ if (!DEBUG_FLOW) return { ok: true };
+ const event = message?.event || "?";
+ const data = message?.data || {};
+ const ts = message?.ts ? new Date(message.ts).toISOString().slice(11, 23) : "??:??:??";
+ console.warn(`[start-flow ${ts}] ${event}`, data);
+ return { ok: true };
+ });
+
+ // Forward scene-create direct from BG; the editor-tab proxy added
+ // ~3-6s of cold-start. Uses getPlatformInfo() as the SW heartbeat.
+ // Falls back to editor-tab proxy on non-transient failure.
registerMessage("forward-create-scene", async (message) => {
const { projectId, payload } = message || {};
if (!projectId || !payload) {
return { ok: false, error: "missing-projectId-or-payload" };
}
+ if (!API_BASE) {
+ return { ok: false, error: "no-api-base" };
+ }
+
+ const ping = () => {
+ try {
+ chrome.runtime.getPlatformInfo(() => void chrome.runtime.lastError);
+ } catch {}
+ };
+ ping();
+ const keepAlive = setInterval(ping, 10_000);
+
+ try {
+ const { screenityToken } = await chrome.storage.local.get([
+ "screenityToken",
+ ]);
+ const headers = { "Content-Type": "application/json" };
+ if (screenityToken) headers.Authorization = `Bearer ${screenityToken}`;
+ // No keepalive:true (MV3 SW fetches with it can hang forever).
+ // 1s abort then fall through to editor-tab proxy; healthy is
+ // <500ms, >1s means BG-direct is wedged.
+ const controller = new AbortController();
+ const abortTimer = setTimeout(() => controller.abort(), 1_000);
+ let res;
+ try {
+ res = await fetch(`${API_BASE}/videos/${projectId}/scenes/`, {
+ method: "POST",
+ headers,
+ body: JSON.stringify(payload),
+ signal: controller.signal,
+ });
+ } finally {
+ clearTimeout(abortTimer);
+ }
+ const text = await res.text();
+ let body = null;
+ try {
+ body = text ? JSON.parse(text) : null;
+ } catch {}
+ if (res.ok) {
+ return { ok: true, status: res.status, body, text };
+ }
+ // Fall through to editor-tab proxy on non-2xx; same-origin
+ // cookie auth is a different code path and may succeed where
+ // bearer-auth doesn't (e.g. token rotation).
+ console.warn(
+ `[forward-create-scene] BG POST returned ${res.status}; falling back to editor-tab proxy`,
+ );
+ } catch (err) {
+ console.warn(
+ `[forward-create-scene] BG POST threw, falling back to editor-tab proxy:`,
+ err?.message || err,
+ );
+ } finally {
+ clearInterval(keepAlive);
+ }
+
+ // Editor-tab proxy fallback (the original path).
let validated = await getValidatedEditorTab({
expectedProjectId: projectId,
expectedKind: "editor",
reason: "forward-create-scene",
});
- let openedTabId = null;
if (!validated.ok || !validated.tab?.id) {
- // No editor tab; happens in multi-mode between scenes. Open one
- // in the background as a same-origin proxy: same-origin POST is
- // immune to the MV3 SW-fetch lifecycle hang we hit when posting
- // bearer-auth from the SW alone. The tab stays open to serve
- // subsequent scenes; finish-multi-recording will reuse/focus it.
const targetUrl = `${process.env.SCREENITY_APP_BASE}/editor/${projectId}/edit?load=true`;
try {
- const tab = await chrome.tabs.create({
- url: targetUrl,
- active: false,
- });
+ const tab = await chrome.tabs.create({ url: targetUrl, active: false });
if (tab?.id) {
- openedTabId = tab.id;
await setEditorTabReference({
tabId: tab.id,
tabUrl: targetUrl,
@@ -859,34 +937,20 @@ export const setupHandlers = () => {
const requestId =
(typeof crypto !== "undefined" && crypto.randomUUID?.()) ||
`scene-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`;
- // Editor tab was just opened by prepare-open-editor; the content
- // script may not have mounted yet. Retry with backoff until it can
- // receive messages, capped at ~10s total.
const backoffsMs = [0, 200, 400, 800, 1200, 1600, 2000, 2400, 2800];
let lastErr = null;
for (const wait of backoffsMs) {
- if (wait > 0) {
- await new Promise((r) => setTimeout(r, wait));
- }
+ if (wait > 0) await new Promise((r) => setTimeout(r, wait));
try {
const reply = await chrome.tabs.sendMessage(
validated.tab.id,
- {
- type: "proxy-create-scene",
- projectId,
- requestId,
- payload,
- },
- // Target top frame only: content script also runs in any
- // sub-iframes (manifest matches <all_urls>), and a postMessage
- // from a sub-iframe doesn't bubble to the editor's top window.
+ { type: "proxy-create-scene", projectId, requestId, payload },
{ frameId: 0 },
);
return reply || { ok: false, error: "no-reply-from-editor" };
} catch (err) {
lastErr = err?.message || String(err);
if (!/Receiving end does not exist|Could not establish/i.test(lastErr)) {
- // Different error; don't keep retrying.
break;
}
}
@@ -939,7 +1003,14 @@ export const setupHandlers = () => {
});
registerMessage("offscreen-diag", async (message) => {
- console.warn("[Screenity][OffscreenDiag]", message.source, message.payload);
+ if (!DEBUG_FLOW) return { ok: true };
+ let payloadStr;
+ try {
+ payloadStr = JSON.stringify(message.payload);
+ } catch {
+ payloadStr = String(message.payload);
+ }
+ console.warn("[Screenity][OffscreenDiag]", message.source, payloadStr);
return { ok: true };
});
registerMessage("offscreen-ready", async () => {
@@ -1158,7 +1229,11 @@ export const setupHandlers = () => {
}
diagEvent("countdown-finished", { skipped: false });
const decisionAt = Date.now();
- await chrome.storage.local.set({
+ // Don't block startAfterCountdown on the diagnostic write; the
+ // Recorder doesn't read countdownFinishedAt until well after stream
+ // setup (~hundreds of ms later), so fire-and-forget shaves a
+ // storage IPC off the countdown→record critical path.
+ chrome.storage.local.set({
countdownFinishedAt: message?.endedAt || decisionAt,
lastCountdownFinishedDecision: {
ts: decisionAt,
@@ -2036,14 +2111,18 @@ export const setupHandlers = () => {
return;
}
let messageTab = null;
+ let activeProjectId = null;
if (message.multiMode) {
messageTab = (await getCurrentTab())?.id || null;
+ activeProjectId = (await chrome.storage.local.get(["projectId"]))
+ .projectId;
} else {
const { projectId, instantMode } = await chrome.storage.local.get([
"projectId",
"instantMode",
]);
+ activeProjectId = projectId;
const targetUrl = getEditorTargetUrl({
projectId,
instantMode: Boolean(instantMode),
@@ -2058,9 +2137,14 @@ export const setupHandlers = () => {
}
if (messageTab) {
+ // Editor listeners (VideoEditorPage + EditorLayout) gate this
+ // message on projectId matching the active project. Without
+ // it they silently drop the event and the SceneLoader never
+ // surfaces during the upload window.
await sendMessageTab(messageTab, {
type: "update-project-loading",
multiMode: message.multiMode,
+ projectId: activeProjectId || null,
}).catch((err) =>
console.warn(
"[Screenity][BG] Failed to send update-project-loading",
@@ -2585,4 +2669,78 @@ export const setupHandlers = () => {
registerMessage("cancel-first-chunk-watchdog", async () => {
await chrome.alarms.clear(FIRST_CHUNK_WATCHDOG_ALARM).catch(() => {});
});
+
+ // Zip in BG so jszip stays out of contentScript.bundle.js (~100KB).
+ registerMessage("make-zip", async (message) => {
+ try {
+ const files = (message && message.files) || {};
+ const filename =
+ (message && typeof message.filename === "string"
+ ? message.filename
+ : null) || "screenity-bundle.zip";
+ const zip = new JSZip();
+ for (const [name, content] of Object.entries(files)) {
+ if (typeof content === "string") {
+ zip.file(name, content);
+ } else if (content instanceof Uint8Array) {
+ zip.file(name, content);
+ } else if (content && typeof content === "object") {
+ // Convenience: stringify plain objects so callers can pass
+ // structured payloads directly.
+ zip.file(name, JSON.stringify(content));
+ }
+ }
+ // base64 because ArrayBuffer/Uint8Array structured-clone is
+ // unreliable across MV3 SW → content (lands as empty {}).
+ const base64 = await zip.generateAsync({ type: "base64" });
+ return { ok: true, base64, filename };
+ } catch (err) {
+ console.warn("[make-zip] failed", err);
+ return {
+ ok: false,
+ error: String((err && err.message) || err).slice(0, 200),
+ };
+ }
+ });
+
+ // Receive perf entries from page contexts at pagehide; routed via
+ // BG so the storage IPC completes (a dying page racing storage.set
+ // drops the last few marks). Cloud upload telemetry routes here too.
+ const UPLOAD_TELEMETRY_KEY = "cloudUploadTelemetryEvents";
+ const MAX_UPLOAD_TELEMETRY_EVENTS = 200;
+ registerMessage("cloud-telemetry-event", async (message) => {
+ try {
+ const event = message?.event;
+ if (!event || typeof event !== "object") return { ok: false };
+ const existing = await chrome.storage.local.get([UPLOAD_TELEMETRY_KEY]);
+ const current = Array.isArray(existing?.[UPLOAD_TELEMETRY_KEY])
+ ? existing[UPLOAD_TELEMETRY_KEY]
+ : [];
+ const next = [...current, event].slice(-MAX_UPLOAD_TELEMETRY_EVENTS);
+ await chrome.storage.local.set({
+ [UPLOAD_TELEMETRY_KEY]: next,
+ lastUploadTelemetryEvent: event,
+ });
+ return { ok: true };
+ } catch (err) {
+ // Best-effort: telemetry isn't user-facing functionality.
+ return { ok: false, error: String(err?.message || err).slice(0, 200) };
+ }
+ });
+
+ registerMessage("perf-forward", async (message) => {
+ try {
+ const ctx = typeof message?.ctx === "string" ? message.ctx : "unknown";
+ const entries = Array.isArray(message?.entries) ? message.entries : [];
+ if (!entries.length) return;
+ const key = `perfTimeline.${ctx}`;
+ const r = await chrome.storage.local.get([key]);
+ const cur = Array.isArray(r[key]) ? r[key] : [];
+ for (const e of entries) cur.push(e);
+ while (cur.length > 300) cur.shift();
+ await chrome.storage.local.set({ [key]: cur });
+ } catch {
+ // Best-effort; perf data isn't user-facing functionality.
+ }
+ });
};
diff --git a/src/pages/Background/recording/openRecorderTab.js b/src/pages/Background/recording/openRecorderTab.js
index 3c3dacd..ea767fe 100644
--- a/src/pages/Background/recording/openRecorderTab.js
+++ b/src/pages/Background/recording/openRecorderTab.js
@@ -76,23 +76,25 @@ const openRecorderTab = async (
const finalUrl = isRegion ? `${recorderUrl}?tab=true` : recorderUrl;
// Close leftover setup.html tabs to prevent them stealing focus from
- // the editor after recording stops. Skip the active source tab: a
- // freshly-installed user can click Record directly from setup.html,
- // and closing the source tab mid-acquisition fires REC_START_SOURCE_TAB_GONE.
- try {
- const setupUrl = chrome.runtime.getURL("setup.html");
- const allTabs = await chrome.tabs.query({});
- const stale = allTabs.filter(
- (t) =>
- t.id != null &&
- t.id !== activeTab?.id &&
- t.url &&
- t.url.startsWith(setupUrl),
- );
- if (stale.length > 0) {
- await chrome.tabs.remove(stale.map((t) => t.id));
- }
- } catch {}
+ // the editor after recording stops. Fire-and-forget; the tab cleanup
+ // doesn't need to gate tabs.create below, and chrome.tabs.query({})
+ // can take 50-100ms on a tab-heavy browser.
+ (async () => {
+ try {
+ const setupUrl = chrome.runtime.getURL("setup.html");
+ const allTabs = await chrome.tabs.query({});
+ const stale = allTabs.filter(
+ (t) =>
+ t.id != null &&
+ t.id !== activeTab?.id &&
+ t.url &&
+ t.url.startsWith(setupUrl),
+ );
+ if (stale.length > 0) {
+ await chrome.tabs.remove(stale.map((t) => t.id));
+ }
+ } catch {}
+ })();
const endTabCreate = perfSpan("BG.openRecorderTab tabs.create");
const tab = await chrome.tabs.create({
@@ -192,7 +194,7 @@ const openRecorderTab = async (
// app has mounted its message listener, so the first send is
// routinely lost. It carries isTab/tabID for region recordings, so
// retry until the recorder is listening. (streaming-data no longer
- // depends on `loaded` — the recorder pulls it on mount.)
+ // depends on `loaded`; the recorder pulls it on mount.)
const loadedMsg = {
type: "loaded",
request: request,
@@ -205,6 +207,20 @@ const openRecorderTab = async (
}
: {}),
};
+ console.warn(
+ "[Screenity][openRecorderTab] loadedMsg",
+ JSON.stringify({
+ isRegion,
+ camera,
+ activeTabId: activeTab.id,
+ isPlayground,
+ requestRegion: !!request?.region,
+ requestCustomRegion: !!request?.customRegion,
+ requestRecordingType: request?.recordingType || null,
+ loadedHasIsTab: !!loadedMsg.isTab,
+ loadedTabID: loadedMsg.tabID || null,
+ }),
+ );
(async () => {
const backoffMs = [0, 200, 500, 1000, 2000];
for (let attempt = 0; attempt < backoffMs.length; attempt += 1) {
diff --git a/src/pages/Background/recording/recordingHelpers.js b/src/pages/Background/recording/recordingHelpers.js
index e6abc92..dcbc294 100644
--- a/src/pages/Background/recording/recordingHelpers.js
+++ b/src/pages/Background/recording/recordingHelpers.js
@@ -184,10 +184,12 @@ export const handleRecordingError = async (request) => {
]);
const preserveMultiProject =
Boolean(multiMode) && Number(multiSceneCount) > 0;
- // sceneId/sceneIdStatus/pendingSceneIndex must clear with projectId. Otherwise a
- // retry attempt inherits a sceneId tied to a project that no longer exists, and
- // the cloudrecorder reaches startRecording with projectId=null while sceneIdStatus
- // still says "recording". See the no-project-id failure pattern in diagnostics.
+ // Clear sceneId/sceneIdStatus/pendingSceneIndex when projectId
+ // clears; a retry inheriting a stale sceneId reaches the
+ // cloudrecorder with projectId=null and sceneIdStatus="recording".
+ // pendingSceneIndex must clear to `[]`, not null: it's consumed
+ // via destructuring defaults that don't fire for null, and a null
+ // value crashes the next .includes() in CloudRecorder.
const multiState = preserveMultiProject
? {}
: {
@@ -200,11 +202,15 @@ export const handleRecordingError = async (request) => {
activeSceneId: null,
sceneId: null,
sceneIdStatus: null,
- pendingSceneIndex: null,
+ pendingSceneIndex: [],
};
await chrome.storage.local.set({
recording: false,
+ // Clear pendingRecording at stop. countdownEverShown is per-tab
+ // React state, so a tab that wasn't the recorder reads stale
+ // pendingRecording:true and the "Preparing…" loader sticks.
+ pendingRecording: false,
recordingUiTabId: null,
tabRecordedID: null,
offscreen: false,
@@ -350,7 +356,7 @@ export const handleGetStreamingData = async () => {
// after the push retry loop.
void pushStreamingData(dataStr);
// Returned to a `get-streaming-data` pull as the direct response. This
- // is the robust delivery path — it does not depend on the recorder
+ // is the robust delivery path; it does not depend on the recorder
// tab's onMessage listener being registered yet, nor on `recordingTab`
// routing being correct.
return { ok: true, data: dataStr };
diff --git a/src/pages/Background/recording/restartRecording.js b/src/pages/Background/recording/restartRecording.js
index 0b1d34c..1d60cc6 100644
--- a/src/pages/Background/recording/restartRecording.js
+++ b/src/pages/Background/recording/restartRecording.js
@@ -63,11 +63,31 @@ export const handleRestart = async (message = {}, sender = null) => {
// reset before round-trip so a mid-flight alarm can't fire tier-3 against stale keys
await resetWatchdogState();
+ // Snapshot multi state before restart, re-pin after. Several
+ // paths during restart (offscreen discard cascade, watchdogs)
+ // write multiMode:false because they miss the recording-in-progress
+ // signals; without re-pin, restarting scene N (N>1) wipes everything.
+ const multiSnapshot = await chrome.storage.local.get([
+ "multiMode",
+ "multiSceneCount",
+ "multiProjectId",
+ "multiLastSceneId",
+ ]);
await chrome.storage.local.set({
restarting: true,
...(sourceTabId != null
? { activeTab: sourceTabId, recordingUiTabId: sourceTabId }
: {}),
+ // Re-pin in the same batch so anything reading immediately sees
+ // the preserved state.
+ ...(multiSnapshot.multiMode
+ ? {
+ multiMode: multiSnapshot.multiMode,
+ multiSceneCount: multiSnapshot.multiSceneCount,
+ multiProjectId: multiSnapshot.multiProjectId,
+ multiLastSceneId: multiSnapshot.multiLastSceneId,
+ }
+ : {}),
});
await persistRestartFlow("requested", {
attemptId,
@@ -113,6 +133,38 @@ export const handleRestart = async (message = {}, sender = null) => {
recorderState: response?.recorderState || null,
});
await resetActiveTabRestart({ sourceTabId });
+ // Second re-pin after the round-trip. If any handler reachable
+ // during the cloudrecorder dismissRecording → discardOffscreen
+ // path cleared the multi keys, restore them so the upcoming
+ // stop's preserveMultiProject check sees the correct count.
+ if (multiSnapshot.multiMode) {
+ try {
+ const afterRoundTrip = await chrome.storage.local.get([
+ "multiMode",
+ "multiSceneCount",
+ "multiProjectId",
+ ]);
+ const lostMulti =
+ !afterRoundTrip.multiMode ||
+ (multiSnapshot.multiSceneCount > 0 &&
+ !(afterRoundTrip.multiSceneCount > 0)) ||
+ (multiSnapshot.multiProjectId &&
+ !afterRoundTrip.multiProjectId);
+ if (lostMulti) {
+ await chrome.storage.local.set({
+ multiMode: multiSnapshot.multiMode,
+ multiSceneCount: multiSnapshot.multiSceneCount,
+ multiProjectId: multiSnapshot.multiProjectId,
+ multiLastSceneId: multiSnapshot.multiLastSceneId,
+ });
+ lifecycle("BG.restart", "multi-state-restored", {
+ attemptId,
+ before: multiSnapshot,
+ afterRoundTrip,
+ });
+ }
+ } catch {}
+ }
await persistRestartFlow("countdown-dispatched", { attemptId, sourceTabId });
lifecycle("BG.restart", "completed", { attemptId, sourceTabId });
diagEvent("restart-completed", { attemptId });
diff --git a/src/pages/Background/recording/startRecording.js b/src/pages/Background/recording/startRecording.js
index 8d3b452..9640bf1 100644
--- a/src/pages/Background/recording/startRecording.js
+++ b/src/pages/Background/recording/startRecording.js
@@ -75,33 +75,38 @@ export const startRecording = async (caller = "unknown") => {
const _startRecordingInner = async (caller) => {
const recordingAttemptId = makeRecordingAttemptId();
- // snapshot residual state to spot leaked flags from prior recordings
- try {
- const residual = await chrome.storage.local.get([
- "recordingTab",
- "recordingUiTabId",
- "tabRecordedID",
- "sandboxTab",
- "region",
- "customRegion",
- "recordingType",
- "recording",
- "pendingRecording",
- "restarting",
- "offscreen",
- "useWebCodecsRecorder",
- "fastRecorderInUse",
- "backup",
- "backupSetup",
- "backupTab",
- "memoryError",
- ]);
- lifecycle("BG.startRecording", "session-boundary", {
- caller,
- attemptId: recordingAttemptId,
- residual,
- });
- } catch {}
+ // Diagnostic-only snapshot of residual state; runs async so it
+ // doesn't block the actual start path. ~17-key storage read can
+ // cost 30-80ms on a contended SW and there's no consumer that
+ // needs it before we send "start-recording" to the recorder.
+ (async () => {
+ try {
+ const residual = await chrome.storage.local.get([
+ "recordingTab",
+ "recordingUiTabId",
+ "tabRecordedID",
+ "sandboxTab",
+ "region",
+ "customRegion",
+ "recordingType",
+ "recording",
+ "pendingRecording",
+ "restarting",
+ "offscreen",
+ "useWebCodecsRecorder",
+ "fastRecorderInUse",
+ "backup",
+ "backupSetup",
+ "backupTab",
+ "memoryError",
+ ]);
+ lifecycle("BG.startRecording", "session-boundary", {
+ caller,
+ attemptId: recordingAttemptId,
+ residual,
+ });
+ } catch {}
+ })();
let stack = null;
try {
stack = new Error().stack?.split("\n").slice(1, 6).join("\n") || null;
@@ -114,26 +119,57 @@ const _startRecordingInner = async (caller) => {
recordingStartingAt: Date.now(),
});
- // Close any prior editor tab. The OPFS writer wipes the previous
- // recording's file when a new one starts, so leaving the old editor
- // open with stale backing data is worse than closing it. The
- // editor-force-close message clears its beforeunload prompt first so
- // the user doesn't get a confirm dialog. The recordingStartingAt guard
- // above keeps the cascading cleanup off the new recorder tab.
+ // One batched read for every flag this function consumes below. The
+ // original code did six separate awaited gets (sandboxTab,
+ // recordingToScene+multiMode, activeTab+recordingUiTabId, customRegion,
+ // recordingType); under any storage contention that pile up to
+ // 150-300ms of pure round-trip overhead on the countdown→record path.
+ const _startReads = chrome.storage.local.get([
+ "sandboxTab",
+ "recordingToScene",
+ "multiMode",
+ "activeTab",
+ "recordingUiTabId",
+ "customRegion",
+ "recordingType",
+ ]);
+
+ // Close every prior editor tab. OPFS wipes the previous recording
+ // when a new one starts, so a stale editor on old backing data is
+ // worse than closing it. URL-based sweep (not cached `sandboxTab`)
+ // because sandboxTab only tracks the LAST editor; consecutive
+ // recordings would orphan earlier ones. recordingStartingAt above
+ // keeps the cascading close from sweeping the new recorder tab.
try {
- const { sandboxTab: priorSandboxTab } = await chrome.storage.local.get([
- "sandboxTab",
- ]);
- if (Number.isInteger(priorSandboxTab)) {
- try {
- await Promise.race([
- chrome.tabs.sendMessage(priorSandboxTab, {
- type: "editor-force-close",
- }),
- new Promise((r) => setTimeout(r, 250)),
- ]);
- } catch {}
- chrome.tabs.remove(priorSandboxTab).catch(() => {});
+ const editorUrls = [
+ chrome.runtime.getURL("editor.html"),
+ chrome.runtime.getURL("editorwebcodecs.html"),
+ chrome.runtime.getURL("editorviewer.html"),
+ ];
+ const allTabs = await chrome.tabs.query({});
+ const editorTabs = allTabs.filter(
+ (t) =>
+ t.id != null &&
+ t.url &&
+ editorUrls.some((prefix) => t.url.startsWith(prefix)),
+ );
+ if (editorTabs.length > 0) {
+ // Best-effort: tell each editor to clear its beforeunload.
+ // 100ms global race cap; we don't await per-tab so a single
+ // unresponsive tab can't block start.
+ await Promise.race([
+ Promise.allSettled(
+ editorTabs.map((t) =>
+ chrome.tabs.sendMessage(t.id, { type: "editor-force-close" }),
+ ),
+ ),
+ new Promise((r) => setTimeout(r, 100)),
+ ]);
+ // Fire all removes in parallel; failures are silent (tab may
+ // have closed in the interim).
+ for (const t of editorTabs) {
+ chrome.tabs.remove(t.id).catch(() => {});
+ }
}
} catch {}
@@ -162,43 +198,36 @@ const _startRecordingInner = async (caller) => {
lastStartRecordingCaller: { caller, stack, ts: Date.now() },
});
- // A standalone (non-scene, non-multi) recording must not inherit a sceneId
- // from a previous recording. The cloudrecorder reads chrome.storage sceneId
- // at start; a leaked value tags the new recording's telemetry and upload
- // sessions with the prior recording's identity (the back-to-back sceneId
- // leak). Scene and multi-scene recordings legitimately carry sceneId across
- // the boundary. NOTE: only sceneId/sceneIdStatus are cleared — never
- // pendingSceneIndex: it is an array consumed via a `= []` destructuring
- // default (upsertPendingScene), and a default only covers `undefined`, not
- // `null`. Setting it null makes `pendingSceneIndex.includes()` throw and
- // crashes the next recorder. Its own upsert/remove lifecycle manages it.
+ // Clear leaked sceneId from a prior standalone recording; the
+ // cloudrecorder reads storage at start and a stale value tags the
+ // new recording's telemetry. Only clear sceneId/sceneIdStatus, NOT
+ // pendingSceneIndex: it's an array with a `= []` destructuring
+ // default that only covers `undefined`; setting it null makes
+ // `.includes()` throw. Its own upsert/remove lifecycle owns it.
+ const {
+ recordingToScene,
+ multiMode,
+ activeTab,
+ recordingUiTabId,
+ customRegion,
+ recordingType,
+ } = await _startReads;
try {
- const { recordingToScene, multiMode } = await chrome.storage.local.get([
- "recordingToScene",
- "multiMode",
- ]);
if (!recordingToScene && !multiMode) {
- await chrome.storage.local.set({
+ // Fire-and-forget: the clear doesn't gate the recorder-start
+ // message below; the recorder reads sceneId at its own preflight.
+ chrome.storage.local.set({
sceneId: null,
sceneIdStatus: null,
});
}
} catch {}
-
- const { activeTab, recordingUiTabId } = await chrome.storage.local.get([
- "activeTab",
- "recordingUiTabId",
- ]);
if (recordingUiTabId != null) {
chrome.storage.local.set({ recordingUiTabId });
} else if (activeTab != null) {
chrome.storage.local.set({ recordingUiTabId: activeTab });
}
- const { customRegion } = await chrome.storage.local.get(["customRegion"]);
-
- const { recordingType } = await chrome.storage.local.get(["recordingType"]);
-
if (recordingType === "region" || recordingType === "tab") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
diff --git a/src/pages/Background/recording/stopRecording.js b/src/pages/Background/recording/stopRecording.js
index 108fd90..85845e5 100644
--- a/src/pages/Background/recording/stopRecording.js
+++ b/src/pages/Background/recording/stopRecording.js
@@ -52,17 +52,53 @@ const markEditorStartFailed = async (tabId, errorCode, why) => {
} catch {}
};
+// Bound lock acquisition: storage.get + storage.set both block on
+// contended IPC and can stall multi-second on many-tab Chrome. The
+// lock suppresses duplicate stops racing with editor-open; under
+// contention fail-open beats making the user wait. Worst case on
+// fail-open is one extra editor tab.
+const LOCK_ACQUIRE_TIMEOUT_MS = 500;
const acquirePostStopEditorLock = async (recordingId = null) => {
- const { postStopEditorOpening } = await chrome.storage.local.get([
- "postStopEditorOpening",
- ]);
- if (postStopEditorOpening) return false;
- await chrome.storage.local.set({
- postStopEditorOpening: true,
- postStopEditorOpened: true,
- postStopRecordingId: recordingId,
+ const timeoutSentinel = Symbol("acquire-lock-timeout");
+ let timer = null;
+ const timeoutPromise = new Promise((resolve) => {
+ timer = setTimeout(() => resolve(timeoutSentinel), LOCK_ACQUIRE_TIMEOUT_MS);
});
- return true;
+ try {
+ const result = await Promise.race([
+ (async () => {
+ const { postStopEditorOpening } = await chrome.storage.local.get([
+ "postStopEditorOpening",
+ ]);
+ if (postStopEditorOpening) return false;
+ await chrome.storage.local.set({
+ postStopEditorOpening: true,
+ postStopEditorOpened: true,
+ postStopRecordingId: recordingId,
+ });
+ return true;
+ })(),
+ timeoutPromise,
+ ]);
+ if (result === timeoutSentinel) {
+ console.warn(
+ "[Screenity][BG] acquirePostStopEditorLock timed out; failing open",
+ );
+ // Fire-and-forget the writes so the next stop sees the lock
+ // (without us blocking on it).
+ chrome.storage.local
+ .set({
+ postStopEditorOpening: true,
+ postStopEditorOpened: true,
+ postStopRecordingId: recordingId,
+ })
+ .catch(() => {});
+ return true;
+ }
+ return result;
+ } finally {
+ if (timer) clearTimeout(timer);
+ }
};
const releasePostStopEditorLock = async (overrides = {}) => {
@@ -536,7 +572,7 @@ export const handleStopRecordingTab = async (request) => {
const safetyTimer = setTimeout(() => {
if (!settled) {
settled = true;
- console.warn("[Screenity][BG] Editor tab load timed out — releasing lock");
+ console.warn("[Screenity][BG] Editor tab load timed out; releasing lock");
diagEvent("editor-open-timeout", { tabId: tab.id, type: "editorwebcodecs" });
releasePostStopEditorLock();
markEditorStartFailed(
@@ -595,7 +631,7 @@ export const handleStopRecordingTab = async (request) => {
const safetyTimer = setTimeout(() => {
if (!settled) {
settled = true;
- console.warn("[Screenity][BG] Editor tab load timed out — releasing lock");
+ console.warn("[Screenity][BG] Editor tab load timed out; releasing lock");
diagEvent("editor-open-timeout", { tabId: tab.id, type: editorUrl });
releasePostStopEditorLock({ postStopRecordingId: null });
markEditorStartFailed(
diff --git a/src/pages/Background/tabManagement/resetActiveTab.js b/src/pages/Background/tabManagement/resetActiveTab.js
index 8d8b779..6725f95 100644
--- a/src/pages/Background/tabManagement/resetActiveTab.js
+++ b/src/pages/Background/tabManagement/resetActiveTab.js
@@ -32,26 +32,24 @@ export const restartActiveTab = async (message = {}) => {
export const resetActiveTab = async (forceRestart = false, message = {}) => {
perfMark("BG.resetActiveTab.enter", { forceRestart });
- const { activeTab } = await chrome.storage.local.get(["activeTab"]);
- const { surface } = await chrome.storage.local.get(["surface"]);
+ // One storage IPC + the active-tab query in parallel. Two sequential
+ // gets here were costing ~60-80ms; the read of activeTab/surface and
+ // the active-tab query are independent so they should overlap.
+ const [{ activeTab, surface }, [currentTab]] = await Promise.all([
+ chrome.storage.local.get(["activeTab", "surface"]),
+ chrome.tabs.query({ active: true, currentWindow: true }),
+ ]);
if (forceRestart) {
return restartActiveTab(message);
}
- const [currentTab] = await chrome.tabs.query({
- active: true,
- currentWindow: true,
- });
-
- const shouldFocusTab =
- surface !== "browser" ||
- (activeTab && (await isRestrictedDomain(activeTab)));
-
if (activeTab) {
try {
let tab = null;
try {
+ // Single chrome.tabs.get (was 2 via isRestrictedDomain).
+ // Saves ~30-60ms on the start path.
tab = await chrome.tabs.get(activeTab);
} catch (err) {
// source tab closed between picker-confirm and ready-to-record
@@ -72,23 +70,41 @@ export const resetActiveTab = async (forceRestart = false, message = {}) => {
return;
}
+ // Decide focus from the already-fetched tab; no second
+ // chrome.tabs.get inside isRestrictedDomain.
+ let restricted = false;
+ try {
+ const url = new URL(tab.url || "");
+ restricted =
+ url.hostname.includes("google.com") ||
+ url.protocol === "chrome:" ||
+ url.protocol === "chrome-extension:" ||
+ url.protocol === "about:";
+ } catch {}
+ const shouldFocusTab = surface !== "browser" || restricted;
+
if (shouldFocusTab) {
const endFocus = perfSpan("BG.resetActiveTab focus-back-to-tab");
- await chrome.windows.update(tab.windowId, { focused: true });
- await chrome.tabs.update(activeTab, {
- active: true,
- selected: true,
- highlighted: true,
- });
- await focusTab(activeTab);
+ // Three focus ops in parallel. Chrome resolves them in the same
+ // tick anyway and Promise.all halves the wall-clock cost.
+ await Promise.all([
+ chrome.windows.update(tab.windowId, { focused: true }),
+ chrome.tabs.update(activeTab, {
+ active: true,
+ selected: true,
+ highlighted: true,
+ }),
+ focusTab(activeTab),
+ ]);
endFocus();
}
// currentTab fallback would hit the pinned recorder tab when surface==="browser"
const targetTabId = activeTab || currentTab?.id;
- // awaited so the write lands before sendMessageTab triggers the content-side write
- await traceStep("readyToRecordSent", {
+ // traceStep is a storage write for the diagnostic startflow trace -
+ // not load-bearing for the message send below, so fire-and-forget.
+ traceStep("readyToRecordSent", {
routing: {
targetTabId,
activeTab,
diff --git a/src/pages/Camera/utils/backgroundUtils.js b/src/pages/Camera/utils/backgroundUtils.js
index 966ca2a..50e6e2f 100644
--- a/src/pages/Camera/utils/backgroundUtils.js
+++ b/src/pages/Camera/utils/backgroundUtils.js
@@ -1,7 +1,14 @@
-import {
- FilesetResolver,
- ImageSegmenter,
-} from "@mediapipe/tasks-vision";
+// Lazy-load @mediapipe/tasks-vision; ~133KB of code only needed when
+// the user enables background effects on the camera. Eager import was
+// adding the cost to every camera-bubble mount even for users who
+// never touch effects.
+let _mpPromise = null;
+const loadMediapipe = () => {
+ if (!_mpPromise) {
+ _mpPromise = import("@mediapipe/tasks-vision");
+ }
+ return _mpPromise;
+};
// Reused across frames to avoid GC pressure.
let _frameCanvas = null;
@@ -63,6 +70,7 @@ const withTimeout = (promise, ms, label) =>
]);
const tryCreateSegmenter = async (vision, delegate) => {
+ const { ImageSegmenter } = await loadMediapipe();
return ImageSegmenter.createFromOptions(vision, {
baseOptions: {
modelAssetPath: "./assets/mediapipeVision/selfie_segmenter.tflite",
@@ -76,6 +84,7 @@ const tryCreateSegmenter = async (vision, delegate) => {
export const loadSegmentationModel = async () => {
try {
+ const { FilesetResolver } = await loadMediapipe();
const vision = await withTimeout(
FilesetResolver.forVisionTasks("./assets/mediapipeVision"),
MODEL_LOAD_TIMEOUT_MS,
diff --git a/src/pages/Camera/utils/cameraUtils.js b/src/pages/Camera/utils/cameraUtils.js
index 02237d9..5ef6512 100644
--- a/src/pages/Camera/utils/cameraUtils.js
+++ b/src/pages/Camera/utils/cameraUtils.js
@@ -147,22 +147,13 @@ export const getCameraStream = async (
const video = videoRef.current;
video.srcObject = stream;
- // Virtual cameras (OBS Virtual Camera, mmhmm, Snap Camera, etc.)
- // trip up the naive `video.onloadedmetadata = ...` pattern in three
- // ways:
- // 1. Sync race: assigning srcObject can fire `loadedmetadata`
- // synchronously when the stream is already cached/active. By
- // the time we set `onloadedmetadata`, the event has already
- // fired and we hang forever.
- // 2. Track muted: virtual cams often start with the video track
- // `muted=true` (the source app hasn't focused yet); they fire
- // `unmute` on the track before metadata.
- // 3. Never fires: some virtual cams never fire `loadedmetadata`
- // until they receive their first real frame, which can take
- // arbitrarily long if the source app is paused.
- //
- // Check readyState, addEventListener (no clobber), listen on metadata
- // and unmute, cap the wait at 12s so virtual cams don't hang the spinner.
+ // Virtual cams (OBS, mmhmm, Snap, etc.) break the naive
+ // `onloadedmetadata = ...` pattern three ways: loadedmetadata can
+ // fire synchronously on srcObject assign, the track can start
+ // muted (fires `unmute` before metadata), and some virtual cams
+ // never fire loadedmetadata until first frame. Check readyState
+ // up front, listen via addEventListener on both metadata and
+ // unmute, cap at 12s so paused source apps don't hang the spinner.
const VIRTUAL_CAM_LOADED_TIMEOUT_MS = 12000;
await new Promise((resolve) => {
let settled = false;
@@ -384,6 +375,13 @@ export const surfaceHandler = async (request, videoRef) => {
const isSubscribed = request.subscribed || false;
const instantMode = request.instantMode || false;
+ // PiP rule: enter PiP when the bubble needs to be visible to the
+ // capture (free+monitor, pro+instant+monitor) or as a draggable
+ // preview when the camera is already a separate cloud track
+ // (pro+non-instant+non-monitor). Skip PiP when the on-page bubble
+ // is captured naturally (pro+instant+non-monitor) or would imply
+ // the camera's part of the screen recording when it isn't
+ // (pro+non-instant+monitor).
const shouldEnterPip =
(request.surface === "monitor" && (!isSubscribed || instantMode)) ||
(request.surface !== "monitor" && isSubscribed && !instantMode);
diff --git a/src/pages/CloudRecorder/CloudRecorder.jsx b/src/pages/CloudRecorder/CloudRecorder.jsx
index bb1cba6..a8b776e 100644
--- a/src/pages/CloudRecorder/CloudRecorder.jsx
+++ b/src/pages/CloudRecorder/CloudRecorder.jsx
@@ -31,8 +31,15 @@ localforage.config({
});
const API_BASE = process.env.SCREENITY_API_BASE_URL;
+// Enable the start-flow logs unconditionally for dev builds so the
+// startup timeline is visible in the cloudrecorder tab console without
+// needing to set `window.SCREENITY_DEBUG_RECORDER = true` first. Prod
+// builds keep the original flag.
const DEBUG_START_FLOW =
- typeof window !== "undefined" ? !!window.SCREENITY_DEBUG_RECORDER : false;
+ typeof window !== "undefined"
+ ? !!window.SCREENITY_DEBUG_RECORDER ||
+ process.env.NODE_ENV !== "production"
+ : false;
const SCREEN_CHUNK_MEMORY_WINDOW = 8;
const CAMERA_CHUNK_MEMORY_WINDOW = 8;
const AUDIO_CHUNK_MEMORY_WINDOW = 8;
@@ -99,6 +106,10 @@ const CloudRecorder = () => {
const [finalizeFailure, setFinalizeFailure] = useState(null);
const finalizeFailureRef = useRef(null);
const finalizeContextRef = useRef(null);
+ // Timing trace for the stop→finalize→scene-create→close pipeline.
+ // Populated when stopRecording fires; ticked at each milestone so
+ // dead time is visible in the console.
+ const stopFlowRef = useRef(null);
const simulateFinalizeFailureConsumedRef = useRef(false);
const [retryingFinalize, setRetryingFinalize] = useState(false);
@@ -349,7 +360,20 @@ const CloudRecorder = () => {
const logStartFlow = (event, data = {}) => {
if (!DEBUG_START_FLOW) return;
const payload = { ts: Date.now(), event, ...data };
- console.info("[Screenity][StartFlow]", payload);
+ // console.warn (not console.info); Terser drop_console removes
+ // info/log/debug in prod builds.
+ console.warn("[Screenity][StartFlow]", payload);
+ // Mirror to BG so the timeline is visible even if the cloud
+ // recorder tab closes mid-sequence (e.g. an unhandled error
+ // tearing the tab down before the user can read the console).
+ try {
+ chrome.runtime.sendMessage({
+ type: "start-flow-tick",
+ event,
+ data,
+ ts: payload.ts,
+ });
+ } catch {}
try {
const update = {
startFlowDebug: {
@@ -450,20 +474,38 @@ const CloudRecorder = () => {
return telemetryRuntimeRef.current;
};
+ // Route telemetry writes through BG. window.close() races the
+ // storage IPC when written from this tab and the event drops; BG
+ // outlives the tab. Counter so the pre-close flush can await sends.
+ const inflightTelemetryWritesRef = useRef(new Set());
const appendUploadTelemetryEvent = async (eventPayload) => {
+ const p = (async () => {
+ try {
+ await chrome.runtime.sendMessage({
+ type: "cloud-telemetry-event",
+ event: eventPayload,
+ });
+ } catch (err) {
+ console.warn("Failed to forward upload telemetry event:", err);
+ }
+ })();
+ inflightTelemetryWritesRef.current.add(p);
+ p.finally(() => inflightTelemetryWritesRef.current.delete(p));
+ return p;
+ };
+
+ // Called by the close paths just before window.close() so any
+ // event emitted in the last microtask actually lands in BG storage.
+ const flushInflightTelemetry = async () => {
+ const writes = Array.from(inflightTelemetryWritesRef.current);
+ if (writes.length === 0) return;
try {
- const existing = await chrome.storage.local.get([UPLOAD_TELEMETRY_KEY]);
- const current = Array.isArray(existing?.[UPLOAD_TELEMETRY_KEY])
- ? existing[UPLOAD_TELEMETRY_KEY]
- : [];
- const next = [...current, eventPayload].slice(-MAX_UPLOAD_TELEMETRY_EVENTS);
- await chrome.storage.local.set({
- [UPLOAD_TELEMETRY_KEY]: next,
- lastUploadTelemetryEvent: eventPayload,
- });
- } catch (err) {
- console.warn("Failed to persist upload telemetry event:", err);
- }
+ await Promise.race([
+ Promise.allSettled(writes),
+ // Hard cap so a wedged BG SW can't block tab close forever.
+ new Promise((r) => setTimeout(r, 1500)),
+ ]);
+ } catch {}
};
const resolveUploadTelemetryToken = async () => {
@@ -841,9 +883,12 @@ const CloudRecorder = () => {
if (!sceneId) return;
const indexKey = "pendingSceneIndex";
const sceneKey = `pendingScene:${sceneId}`;
- const { pendingSceneIndex = [] } = await chrome.storage.local.get([
- indexKey,
- ]);
+ const raw = await chrome.storage.local.get([indexKey]);
+ // Destructure default doesn't trigger when the stored value is
+ // explicitly null (vs. undefined); a previous teardown path could
+ // have set it to null and the next .includes() throws. Coerce
+ // defensively.
+ const pendingSceneIndex = Array.isArray(raw?.[indexKey]) ? raw[indexKey] : [];
const nextIndex = pendingSceneIndex.includes(sceneId)
? pendingSceneIndex
: [...pendingSceneIndex, sceneId];
@@ -857,9 +902,8 @@ const CloudRecorder = () => {
if (!sceneId) return;
const indexKey = "pendingSceneIndex";
const sceneKey = `pendingScene:${sceneId}`;
- const { pendingSceneIndex = [] } = await chrome.storage.local.get([
- indexKey,
- ]);
+ const raw = await chrome.storage.local.get([indexKey]);
+ const pendingSceneIndex = Array.isArray(raw?.[indexKey]) ? raw[indexKey] : [];
const nextIndex = pendingSceneIndex.filter((id) => id !== sceneId);
await chrome.storage.local.remove([sceneKey]);
await chrome.storage.local.set({ [indexKey]: nextIndex });
@@ -926,6 +970,8 @@ const CloudRecorder = () => {
screenMediaId,
cameraMediaId,
audioMediaId,
+ durations = null,
+ dimensions = null,
}) => {
if (!projectId || !sceneId) return { ok: false, error: "missing-ids" };
const mediaIds = [screenMediaId, cameraMediaId, audioMediaId].filter(
@@ -944,6 +990,10 @@ const CloudRecorder = () => {
screenMediaId: screenMediaId || null,
cameraMediaId: cameraMediaId || null,
audioMediaId: audioMediaId || null,
+ // Server stamps these onto media docs so scenes land with the
+ // real length instead of waiting for Bunny's encoded webhook.
+ durations: durations || undefined,
+ dimensions: dimensions || undefined,
}),
});
if (!res.ok) {
@@ -973,6 +1023,13 @@ const CloudRecorder = () => {
oscillator.start();
keepAliveOscillator.current = oscillator;
}
+ // AudioContext starts suspended without a user gesture; without
+ // resume() the oscillator emits nothing and Chrome throttles
+ // the tab (camera + screen drop to ~5fps for the first 15s).
+ const aCtx = keepAliveAudioCtx.current;
+ if (aCtx && aCtx.state !== "running" && typeof aCtx.resume === "function") {
+ aCtx.resume().catch(() => {});
+ }
} catch (err) {
console.warn("[CloudRecorder] keepalive: audio layer failed:", err);
}
@@ -1760,16 +1817,10 @@ const CloudRecorder = () => {
// Re-probe HW slots + sticky-disabled state on the next recording
// (encoders chosen per session, not per component-mount).
resetEncoderProbeCache();
- // Reap the OPFS session directory once chunks have been cleared by the
- // various callers' .clear() calls. Awaited because the success path
- // calls window.close() right after finalize, and a fire-and-forget
- // delete races with tab teardown, leaving half-deleted dirs across
- // sessions and confuse the orphan reaper.
- //
- // Skipped when a local-playback offer is still holding the screen
- // chunks: the editor reads them via cloud-local-playback-read-chunk
- // and clears+destroys via cloud-local-playback-clear when done. The
- // offer's TTL alarm cleans up if the editor never gets to it.
+ // Reap the OPFS session dir; awaited because window.close()
+ // follows and fire-and-forget delete races teardown. Skipped
+ // when a local-playback offer is still holding chunks (editor
+ // clears via cloud-local-playback-clear; TTL alarm is the safety).
if (usedOpfs && opfsSessionId && !keepOpfsSession) {
try {
await destroySessionDir(opfsSessionId);
@@ -2248,9 +2299,10 @@ const CloudRecorder = () => {
const recoverPendingScenes = useCallback(async () => {
try {
- const { pendingSceneIndex = [] } = await chrome.storage.local.get([
- "pendingSceneIndex",
- ]);
+ const raw = await chrome.storage.local.get(["pendingSceneIndex"]);
+ const pendingSceneIndex = Array.isArray(raw?.pendingSceneIndex)
+ ? raw.pendingSceneIndex
+ : [];
if (!pendingSceneIndex.length) return;
for (const sceneId of pendingSceneIndex) {
@@ -2707,6 +2759,7 @@ const CloudRecorder = () => {
uploadMetaRef.current = null;
isInit.current = false;
+ await flushInflightTelemetry();
if (!IS_IFRAME_CONTEXT) {
try {
window.close();
@@ -2824,12 +2877,16 @@ const CloudRecorder = () => {
}
};
- // Decide encoder kind (and therefore container) per track BEFORE
- // constructing the uploaders. The TUS Upload-Metadata `filetype` is
- // set at upload-create time and isn't re-declarable on PATCH, so the
- // uploader needs to know the right container up front. The plan is
- // cached; chooseTrackEncoder during startRecording reads the same
- // probe result and returns the same kind.
+ // Stashed init options; all three /api/bunny/videos POSTs fire
+ // in parallel below. Partners are discovered by shared sceneId
+ // server-side, so camera/audio no longer need screen's mediaId.
+ let screenInitOpts = null;
+ let cameraInitOpts = null;
+ let audioInitOpts = null;
+
+ // Pick encoder/container per track before constructing uploaders.
+ // TUS Upload-Metadata filetype is fixed at create time and can't
+ // be re-declared on PATCH. Plan is cached.
const screenSettings = screenStream.current?.getVideoTracks?.()?.[0]
?.getSettings?.() || {};
const cameraSettings = cameraStream.current?.getVideoTracks?.()?.[0]
@@ -2847,9 +2904,14 @@ const CloudRecorder = () => {
const { inspectTrackPlan } = await import(
"./encoder/chooseEncoder"
);
- const screenPlan = await inspectTrackPlan({ track: "screen", probeOptions });
- const cameraPlan = await inspectTrackPlan({ track: "camera", probeOptions });
- const audioPlan = await inspectTrackPlan({ track: "audio" });
+ // Parallel: probeHwSlots inside chooseEncoder is memoized
+ // (_hwSlotsPromise) so concurrent calls share the same probe;
+ // the three inspectTrackPlan calls don't conflict.
+ const [screenPlan, cameraPlan, audioPlan] = await Promise.all([
+ inspectTrackPlan({ track: "screen", probeOptions }),
+ inspectTrackPlan({ track: "camera", probeOptions }),
+ inspectTrackPlan({ track: "audio" }),
+ ]);
encoderKinds.screen = screenPlan.kind;
encoderKinds.camera = cameraPlan.kind;
encoderKinds.audio = audioPlan.kind;
@@ -2943,21 +3005,15 @@ const CloudRecorder = () => {
height = downscaled.height;
}
}
- await screenUploader.current.initialize(projectId, {
+ // Stash options; fire all 3 inits in parallel below.
+ screenInitOpts = {
title: "Screen Recording",
type: "screen",
width,
height,
sceneId,
sessionId,
- });
- logDebugEvent("uploader-ready", {
- type: "screen",
- projectId,
- sceneId,
- mediaId: screenUploader.current?.getMeta()?.mediaId || null,
- videoId: screenUploader.current?.getMeta()?.videoId || null,
- });
+ };
}
if (cameraStream.current) {
@@ -2999,30 +3055,24 @@ const CloudRecorder = () => {
height = downscaled.height;
}
}
- await cameraUploader.current.initialize(projectId, {
+ // Omit linkedMediaId; server groups partners by shared sceneId
+ // in usedIn now. Lifts the serial wait on screen's mediaId
+ // (~3s dev, 500ms-1s prod).
+ cameraInitOpts = {
title: "Camera Recording",
type: "camera",
- linkedMediaId: screenUploader.current?.getMeta()?.mediaId || null,
+ linkedMediaId: null,
width,
height,
sceneId,
sessionId,
- });
- logDebugEvent("uploader-ready", {
- type: "camera",
- projectId,
- sceneId,
- mediaId: cameraUploader.current?.getMeta()?.mediaId || null,
- videoId: cameraUploader.current?.getMeta()?.videoId || null,
- });
+ };
}
const { micActive } = await chrome.storage.local.get(["micActive"]);
- // Only spin up the audio TUS uploader when the user actually has
- // mic on. The cloudrecorder always attaches a mic stream (then
- // gain-mutes it when micActive=false) for system-audio mixing
- // routing, so a non-empty getAudioTracks() isn't a strong signal
- // on its own.
+ // Only init the audio uploader when mic is actually on; a non-
+ // empty getAudioTracks() isn't enough since we attach a muted
+ // mic stream for system-audio mixing.
if (micActive === true && rawMicStream.current?.getAudioTracks?.().length) {
// Audio uploader failures are non-fatal; recording proceeds
// without an audio track if init fails (mic supplementary).
@@ -3044,23 +3094,16 @@ const CloudRecorder = () => {
onTelemetry: onUploaderTelemetry("audio"),
onStateChange: onUploaderStateChange("audio"),
});
- await audioUploader.current.initialize(projectId, {
+ audioInitOpts = {
title: "Audio Recording",
type: "audio",
- linkedMediaId:
- screenUploader.current?.getMeta()?.mediaId ||
- cameraUploader.current?.getMeta()?.mediaId ||
- null,
+ // See comment above for cameraInitOpts; sceneId-based
+ // partner discovery makes linkedMediaId optional and lets
+ // this fire in parallel with the screen + camera inits.
+ linkedMediaId: null,
sceneId,
sessionId,
- });
- logDebugEvent("uploader-ready", {
- type: "audio",
- projectId,
- sceneId,
- mediaId: audioUploader.current?.getMeta()?.mediaId || null,
- videoId: audioUploader.current?.getMeta()?.videoId || null,
- });
+ };
} catch (audioErr) {
console.warn(
"⚠️ Audio uploader init failed; recording will continue without an audio track:",
@@ -3073,6 +3116,67 @@ const CloudRecorder = () => {
}
}
+ // All 3 TUS inits in parallel. No serial dependency anymore -
+ // sceneId-based partner discovery on the server side replaces
+ // the old screen.mediaId → linkedMediaId chain.
+ const parallelInits = [];
+ if (screenUploader.current && screenInitOpts) {
+ parallelInits.push(
+ screenUploader.current
+ .initialize(projectId, screenInitOpts)
+ .then(() => {
+ logDebugEvent("uploader-ready", {
+ type: "screen",
+ projectId,
+ sceneId,
+ mediaId: screenUploader.current?.getMeta()?.mediaId || null,
+ videoId: screenUploader.current?.getMeta()?.videoId || null,
+ });
+ }),
+ );
+ }
+ if (cameraUploader.current && cameraInitOpts) {
+ parallelInits.push(
+ cameraUploader.current
+ .initialize(projectId, cameraInitOpts)
+ .then(() => {
+ logDebugEvent("uploader-ready", {
+ type: "camera",
+ projectId,
+ sceneId,
+ mediaId: cameraUploader.current?.getMeta()?.mediaId || null,
+ videoId: cameraUploader.current?.getMeta()?.videoId || null,
+ });
+ }),
+ );
+ }
+ if (audioUploader.current && audioInitOpts) {
+ parallelInits.push(
+ audioUploader.current
+ .initialize(projectId, audioInitOpts)
+ .then(() => {
+ logDebugEvent("uploader-ready", {
+ type: "audio",
+ projectId,
+ sceneId,
+ mediaId: audioUploader.current?.getMeta()?.mediaId || null,
+ videoId: audioUploader.current?.getMeta()?.videoId || null,
+ });
+ })
+ .catch((audioErr) => {
+ console.warn(
+ "⚠️ Audio uploader init failed; recording will continue without an audio track:",
+ audioErr,
+ );
+ logDebugEvent("audio-uploader-init-failed", {
+ error: audioErr?.message || String(audioErr),
+ });
+ audioUploader.current = null;
+ }),
+ );
+ }
+ if (parallelInits.length) await Promise.all(parallelInits);
+
await setPipelineState("uploaders-ready", {
projectId,
sceneId,
@@ -3099,11 +3203,9 @@ const CloudRecorder = () => {
const createMediaRecorder = (stream, options, onDataAvailable) => {
try {
const recorder = new MediaRecorder(stream, options);
- // Track in-flight write() promises so stopAllRecorders can drain them
- // before finalize() runs. Without this, a late ondataavailable that
- // fires after onstop (or whose write() promise hasn't resolved yet)
- // can race finalize and get rejected by the isFinalizing guard,
- // silently truncating the upload.
+ // Track in-flight write() promises so stopAllRecorders can
+ // drain them before finalize(); a late ondataavailable would
+ // otherwise race finalize and silently truncate the upload.
recorder._pendingWrites = new Set();
recorder.ondataavailable = (event) => {
@@ -3163,14 +3265,58 @@ const CloudRecorder = () => {
return;
}
+ // Pre-flight OPFS quota; otherwise we start and checkMaxMemory
+ // stops us within ~1s of the first chunk.
+ try {
+ const { quota = 0, usage = 0 } = await navigator.storage.estimate();
+ const headroom = Math.max(0, quota - usage);
+ const minQuota = 25 * 1024 * 1024;
+ if (quota > 0 && quota < minQuota) {
+ void emitUploadTelemetry("upload_storage_pressure", {
+ severity: "preflight-quota-too-low",
+ quota,
+ usage,
+ headroom,
+ });
+ sendRecordingError(
+ "Not enough browser storage available to record. Free up disk space and try again.",
+ );
+ logStartFlow("recording_error", {
+ reason: "preflight-low-quota",
+ quota,
+ headroom,
+ });
+ return;
+ }
+ if (headroom > 0 && headroom < STORAGE_CRITICAL_HEADROOM_BYTES) {
+ void emitUploadTelemetry("upload_storage_pressure", {
+ severity: "preflight-critical-headroom",
+ quota,
+ usage,
+ headroom,
+ });
+ sendRecordingError(
+ "Your browser is almost out of storage. Free up space and try again to avoid losing the recording.",
+ );
+ logStartFlow("recording_error", {
+ reason: "preflight-critical-headroom",
+ headroom,
+ });
+ return;
+ }
+ } catch (err) {
+ // Best-effort: if the API throws, fall through and let the
+ // during-recording checkMaxMemory pick it up. Don't block recording
+ // on a failed estimate.
+ console.warn("[CloudRecorder] preflight storage estimate failed:", err);
+ }
+
const { projectId } = await chrome.storage.local.get(["projectId"]);
if (!projectId) {
- // Diagnostic: capture co-resident state so we can see how the cloudrecorder
- // reached startRecording without a project. The most common path is a BG
- // recording-error firing during attempt N (which clears projectId) followed
- // by a retry that reaches us via `start-recording-tab` without going back
- // through openRecorderTab → createVideoProject.
+ // Diagnostic for how we reached startRecording without a project.
+ // Most common path: BG recording-error clears projectId, then a
+ // retry hits start-recording-tab without re-creating it.
let danglingState = null;
try {
danglingState = await chrome.storage.local.get([
@@ -3311,12 +3457,10 @@ const CloudRecorder = () => {
hasMic: Boolean(micStream.current),
});
if (screenStream.current) {
- // micStream is always attached when a mic device exists (it's the
- // AudioContext mix point for system audio too) and gain-muted to 0
- // when micActive=false, so stream-track presence isn't reliable.
- // audioIntent comes from BG's getStreamingData payload, which reads
- // before the content-script ContentState's fresh-state auto-default
- // can race-flip micActive false back to true.
+ // Stream-track presence isn't reliable: micStream is always
+ // attached when a mic exists (the mix point for system audio)
+ // and gain-muted when micActive=false. audioIntent reads from
+ // BG before contentState's fresh-state auto-default can race.
const { micActive, systemAudio } = audioIntent.current;
const screenHasAudio = micActive === true || systemAudio === true;
const stream = screenHasAudio
@@ -4050,7 +4194,7 @@ const CloudRecorder = () => {
) {
unloadGuardRef.current.stopTriggeredFromUnload = true;
emitAbandonedOnUnloadOnce("pagehide");
- console.warn("⚠️ Recorder page unloading — finalizing");
+ console.warn("⚠️ Recorder page unloading; finalizing");
navigator.sendBeacon?.(
`${API_BASE}/log/recorder-unload`,
JSON.stringify({ reason: "pagehide", ts: Date.now() }),
@@ -4065,6 +4209,37 @@ const CloudRecorder = () => {
return () => window.removeEventListener("pagehide", onHide);
}, []);
+ // Prewarm the WebCodecs hw-slot probe on mount so it's resolved by
+ // the time inspectTrackPlan runs (saves 300-500ms cold-start).
+ useEffect(() => {
+ if (IS_OFFSCREEN_HOST) return undefined;
+ let cancelled = false;
+ (async () => {
+ try {
+ const { inspectTrackPlan } = await import("./encoder/chooseEncoder");
+ if (cancelled) return;
+ // Default probe options; the real probe is shape-invariant
+ // because chooseEncoder caches the first promise it created
+ // regardless of probeOptions.
+ await inspectTrackPlan({
+ track: "screen",
+ probeOptions: {
+ screenWidth: 1920,
+ screenHeight: 1080,
+ cameraWidth: 1280,
+ cameraHeight: 720,
+ framerate: 30,
+ },
+ });
+ } catch {
+ // Best-effort prewarm; the real init still runs the probe.
+ }
+ })();
+ return () => {
+ cancelled = true;
+ };
+ }, []);
+
useEffect(() => {
const onVisibilityChange = () => {
if (document.visibilityState === "hidden") {
@@ -4399,21 +4574,50 @@ const CloudRecorder = () => {
domain: recordedTabDomain || null,
};
- // Forward to the editor tab via BG; editor performs the POST itself
- // (same-origin cookie auth, no CORS, no SW lifecycle race). MV3 SW
- // fetches hang indefinitely when dispatched from the cloud recorder
- // during its post-stop teardown; the editor's tab outlives it.
- let swRes;
- try {
- swRes = await chrome.runtime.sendMessage({
- type: "forward-create-scene",
- projectId,
- payload,
+ // Scene-create via BG (POSTs directly, editor-tab proxy fallback).
+ // 30s cap because keepalive doesn't keep the JS context alive.
+ stopFlowRef.current?.tick?.("scene-create-dispatched", { sceneId });
+ const sendOnce = () =>
+ new Promise((resolve) => {
+ let settled = false;
+ const timer = setTimeout(() => {
+ if (settled) return;
+ settled = true;
+ resolve({ ok: false, error: "bg-timeout" });
+ }, 30_000);
+ chrome.runtime
+ .sendMessage({ type: "forward-create-scene", projectId, payload })
+ .then((reply) => {
+ if (settled) return;
+ settled = true;
+ clearTimeout(timer);
+ resolve(reply || { ok: false, error: "no-bg-response" });
+ })
+ .catch((err) => {
+ if (settled) return;
+ settled = true;
+ clearTimeout(timer);
+ resolve({ ok: false, error: err?.message || String(err) });
+ });
});
- } catch (err) {
- swRes = { ok: false, error: err?.message || String(err) };
+ let swRes = await sendOnce();
+ // One retry if BG didn't respond (SW was restarting). Any other
+ // error is forwarded as-is; the outer block already has the
+ // /recover-scene fallback path that catches it.
+ if (
+ !swRes?.ok &&
+ (swRes?.error === "no-bg-response" || swRes?.error === "bg-timeout")
+ ) {
+ stopFlowRef.current?.tick?.("scene-create-retry", {
+ reason: swRes?.error,
+ });
+ swRes = await sendOnce();
}
- if (!swRes) swRes = { ok: false, error: "no-bg-response" };
+ stopFlowRef.current?.tick?.("scene-create-replied", {
+ ok: !!swRes?.ok,
+ status: swRes?.status,
+ error: swRes?.error,
+ });
const res = {
ok: !!swRes?.ok,
status: swRes?.status ?? 0,
@@ -4429,6 +4633,11 @@ const CloudRecorder = () => {
screenMediaId: uploadMeta.screen?.mediaId || null,
cameraMediaId: uploadMeta.camera?.mediaId || null,
audioMediaId: uploadMeta.audio?.mediaId || null,
+ // Pass the duration + capture dims so the server can stamp
+ // them onto the media docs (and therefore the scene) right
+ // away, instead of waiting for the Bunny encoded webhook.
+ durations: payload?.durations || null,
+ dimensions: payload?.dimensions || null,
});
if (recoverResult.ok) {
@@ -4468,24 +4677,45 @@ const CloudRecorder = () => {
throw new Error(`Failed to create scene: ${errorText}`);
}
} else {
- await setSceneCreateStatus(sceneId, "created");
- // No ensureMediaLinked call here: /api/bunny/videos already
- // stamped each media's `usedIn` at TUS init time, and the
- // /scenes/ POST that just succeeded already cleared
- // `recoveryState`. PATCH was a no-op + a post-stop hang risk
- // in the dying cloudrecorder tab.
- await removePendingScene(sceneId);
- await markSceneComplete(sceneId);
- await setPipelineState("scene-created", {
+ // Dispatch editor-ready right after the 201; housekeeping
+ // (setSceneCreateStatus / removePendingScene / etc.) runs in
+ // parallel below since nothing in the handoff reads it.
+ sceneOutcome = "created";
+ shouldIncrementMultiSceneCount = true;
+
+ if (multiMode && shouldIncrementMultiSceneCount) {
+ // Multi needs count + lastSceneId persisted before
+ // reopen-popup-multi fires; that handler reads them.
+ // reads them. Keep this awaited path for multi.
+ await chrome.storage.local.set({
+ multiSceneCount: multiSceneCount + 1,
+ multiLastSceneId: sceneId,
+ });
+ chrome.runtime.sendMessage({
+ type: "reopen-popup-multi",
+ });
+ }
+
+ sendEditorReady({
projectId,
sceneId,
+ recordingToScene,
+ multiMode,
});
- logDebugEvent("scene-create-complete", {
- projectId,
- sceneId,
+
+ // Housekeeping in parallel (editor's already moving).
+ await setSceneCreateStatus(sceneId, "created");
+ Promise.all([
+ removePendingScene(sceneId).catch(() => {}),
+ markSceneComplete(sceneId).catch(() => {}),
+ setPipelineState("scene-created", { projectId, sceneId }).catch(
+ () => {},
+ ),
+ chrome.storage.local.remove("clickEvents").catch(() => {}),
+ ]).then(() => {
+ logDebugEvent("scene-create-complete", { projectId, sceneId });
});
- sceneOutcome = "created";
- shouldIncrementMultiSceneCount = true;
+ return { created: true };
}
}
@@ -4532,6 +4762,32 @@ const CloudRecorder = () => {
isFinishing.current = true;
clearPendingStart();
+ // Timing trace for stop → scene-create → close. T+0 stop pressed,
+ // recorders drained, last TUS chunk, /scenes dispatched,
+ // /scenes replied, tab close requested. Same shape as
+ // startFlowTrace but for the post-stop pipeline.
+ stopFlowRef.current = { startedAt: performance.now(), steps: [] };
+ const stopTick = (label, extra = {}) => {
+ const t = Math.round(performance.now() - stopFlowRef.current.startedAt);
+ if (DEBUG_START_FLOW) {
+ console.warn(`[stop-flow T+${t}ms] ${label}`, extra);
+ }
+ stopFlowRef.current.steps.push({ t, label, extra });
+ // Mirror to BG service worker so the timeline survives this
+ // tab's window.close(). BG console (chrome://extensions →
+ // service worker) keeps the full sequence visible.
+ try {
+ chrome.runtime.sendMessage({
+ type: "stop-flow-tick",
+ t,
+ label,
+ extra,
+ });
+ } catch {}
+ };
+ stopFlowRef.current.tick = stopTick;
+ stopTick("stop-pressed", { reason, shouldFinalize });
+
if (DEBUG_START_FLOW) {
console.debug("[Screenity] stopRecording invoked", {
reason,
@@ -4608,6 +4864,7 @@ const CloudRecorder = () => {
stopAllIntervals();
await stopAllRecorders();
+ stopFlowRef.current?.tick?.("recorders-drained");
const { sceneId } = await chrome.storage.local.get(["sceneId"]);
const uploadMeta = {
@@ -4671,15 +4928,17 @@ const CloudRecorder = () => {
}
const settledResults = await Promise.allSettled(finalizeCalls);
+ stopFlowRef.current?.tick?.("video-uploaders-finalized", {
+ count: finalizeCalls.length,
+ rejected: settledResults.filter((r) => r.status === "rejected").length,
+ });
const rejectedResults = settledResults.filter(
(result) => result.status === "rejected",
);
- // Audio is supplementary: a dead mic mid-recording shouldn't block
- // scene creation. Finalize separately, never feed into
- // incompleteUploaders. On failure, clear uploadMeta.audio so the
- // scene isn't wired to a half-uploaded audio doc (which would
- // break playback and 400 the transcription queue precondition).
+ // Audio is supplementary; a dead mic shouldn't block scene
+ // creation. Finalize separately; on failure, clear uploadMeta.audio
+ // so the scene isn't wired to a half-uploaded doc.
if (audioUploader.current) {
let audioFinalizeOk = true;
try {
@@ -4936,6 +5195,12 @@ const CloudRecorder = () => {
mediaId: uploadMeta.screen?.mediaId || uploadMeta.camera?.mediaId || null,
});
+ // Flush any pending telemetry IPCs to BG so events landed by
+ // the previous emit calls don't get torn off by window.close()
+ // below. 1.5s hard cap.
+ await flushInflightTelemetry();
+ stopFlowRef.current?.tick?.("tab-closing");
+
if (!IS_IFRAME_CONTEXT) {
try {
window.close();
@@ -4991,7 +5256,10 @@ const CloudRecorder = () => {
});
// Close anyway to prevent stuck tabs; delay so user sees the error.
- setTimeout(() => {
+ setTimeout(async () => {
+ // Same flush as the success path; error telemetry is
+ // exactly what we DON'T want to lose, so wait for BG writes.
+ await flushInflightTelemetry();
if (!IS_IFRAME_CONTEXT) {
try {
window.close();
@@ -5199,7 +5467,10 @@ const CloudRecorder = () => {
bindScreenTrack(screenStream.current.getVideoTracks()[0]);
} catch (err) {
if (isUserCaptureCancel(err)) {
- sendRecordingError("User cancelled stream selection", true);
+ sendRecordingError(
+ `User cancelled stream selection [region-iframe gDM ${err?.name || "?"}]`,
+ true,
+ );
return;
}
sendRecordingError("Failed to access region stream: " + err.message);
@@ -5230,7 +5501,7 @@ const CloudRecorder = () => {
);
} catch (err) {
console.warn(
- "⚠️ Camera permission denied — continuing without camera:",
+ "⚠️ Camera permission denied; continuing without camera:",
err,
);
cameraStream.current = null;
@@ -5381,6 +5652,37 @@ const CloudRecorder = () => {
);
console.log("[CloudRecorder] offscreen getDisplayMedia OK");
} else {
+ if (!id) {
+ console.error(
+ "[CloudRecorder] gUM path reached with no streamId; dispatch bug",
+ {
+ isTab: isTab.current,
+ tabID: tabID.current,
+ IS_OFFSCREEN_HOST,
+ IS_IFRAME_CONTEXT,
+ recordingType: recordingType.current,
+ useDisplayMedia,
+ prewarmedStream: !!prewarmedStream,
+ },
+ );
+ chrome.runtime
+ .sendMessage({
+ type: "offscreen-diag",
+ source: "gUM-no-streamId",
+ payload: {
+ isTab: !!isTab.current,
+ tabIDpresent: !!tabID.current,
+ IS_OFFSCREEN_HOST,
+ IS_IFRAME_CONTEXT,
+ recordingType: recordingType.current,
+ },
+ })
+ .catch(() => {});
+ sendRecordingError(
+ "No screen stream id available [gUM-no-streamId]",
+ );
+ return;
+ }
console.log("[CloudRecorder] desktop getUserMedia constraints", {
audioEnabled: !!desktopConstraints.audio,
videoMandatory: desktopConstraints.video?.mandatory,
@@ -5510,7 +5812,10 @@ const CloudRecorder = () => {
bindScreenTrack(screenStream.current.getVideoTracks()[0]);
} catch (fallbackErr) {
if (isUserCaptureCancel(fallbackErr)) {
- sendRecordingError("User cancelled stream selection", true);
+ sendRecordingError(
+ `User cancelled stream selection [offscreen-tab gDM fallback ${fallbackErr?.name || "?"}]`,
+ true,
+ );
return;
}
sendRecordingError(
@@ -5519,7 +5824,10 @@ const CloudRecorder = () => {
return;
}
} else if (isUserCaptureCancel(err)) {
- sendRecordingError("User cancelled stream selection", true);
+ sendRecordingError(
+ `User cancelled stream selection [tab-streamId-consume ${err?.name || "?"}]`,
+ true,
+ );
return;
} else {
sendRecordingError("Failed to access screen stream: " + err.message);
@@ -5551,7 +5859,7 @@ const CloudRecorder = () => {
);
} catch (err) {
console.warn(
- "⚠️ Camera permission denied — continuing without camera:",
+ "⚠️ Camera permission denied; continuing without camera:",
err,
);
cameraStream.current = null;
@@ -5600,7 +5908,15 @@ const CloudRecorder = () => {
}
} catch {}
- aCtx.current = new AudioContext();
+ // Force 48 kHz: Opus is fixed at 48k, AAC accepts it natively.
+ // System default (often 44.1k) makes the encoder mismatch the
+ // AudioData stream and throw on first encode.
+ try {
+ aCtx.current = new AudioContext({ sampleRate: 48000 });
+ } catch {
+ // Some platforms reject hint sampleRates; fall back to default.
+ aCtx.current = new AudioContext();
+ }
destination.current = aCtx.current.createMediaStreamDestination();
if (micStream.current?.getAudioTracks().length) {
@@ -5693,6 +6009,12 @@ const CloudRecorder = () => {
multiMode: Boolean(multiMode),
});
+ // Run countdown + uploader-init in parallel. maybeStartRecording
+ // polls uploadersInitialized.current at countdown-end (200ms ×
+ // 75). Saves ~2-3s vs awaiting init before the countdown.
+ traceStep("resetActiveTabSent");
+ chrome.runtime.sendMessage({ type: "reset-active-tab" });
+
uploadersInitialized.current = await initializeUploaders();
if (!uploadersInitialized.current) {
throw new Error("Failed to initialize uploaders");
@@ -5706,8 +6028,6 @@ const CloudRecorder = () => {
prewarmRef.current = startPrewarm(screenStream.current);
preloadWebCodecsModules();
}
- traceStep("resetActiveTabSent");
- chrome.runtime.sendMessage({ type: "reset-active-tab" });
if (pendingStartRef.current) {
maybeStartRecording("uploaders-ready");
}
@@ -5738,10 +6058,12 @@ const CloudRecorder = () => {
}
try {
- const permissions = await navigator.permissions.query({ name: "camera" });
- const permissions2 = await navigator.permissions.query({
- name: "microphone",
- });
+ // Parallel; they're independent queries against different
+ // PermissionDescriptors and each typically takes ~50-100ms.
+ const [permissions, permissions2] = await Promise.all([
+ navigator.permissions.query({ name: "camera" }),
+ navigator.permissions.query({ name: "microphone" }),
+ ]);
if (isTab.current) {
// Wait for getStreamID across all recordingTypes: an earlier guard skipped
@@ -5758,17 +6080,53 @@ const CloudRecorder = () => {
}
}
+ const _dispatchPayload = {
+ recordingType: data.recordingType,
+ isTab: !!isTab.current,
+ tabIDpresent: !!tabID.current,
+ tabIDprefix: String(tabID.current || "").slice(0, 16),
+ IS_OFFSCREEN_HOST,
+ IS_IFRAME_CONTEXT,
+ tabPreferred: !!tabPreferred.current,
+ recordingTabId: recordingTabId.current || null,
+ };
+ if (DEBUG_START_FLOW) {
+ console.warn("[CloudRecorder][start-dispatch]", _dispatchPayload);
+ chrome.runtime
+ .sendMessage({
+ type: "offscreen-diag",
+ source: "start-dispatch",
+ payload: _dispatchPayload,
+ })
+ .catch(() => {});
+ }
+ const _diagBranch = (branch) => {
+ chrome.runtime
+ .sendMessage({
+ type: "offscreen-diag",
+ source: "start-dispatch-branch",
+ payload: { branch },
+ })
+ .catch(() => {});
+ };
if (data.recordingType === "camera") {
+ _diagBranch("camera");
startStream(data, null, permissions, permissions2);
} else if (IS_OFFSCREEN_HOST && isTab.current && tabID.current) {
// Pre-acquired tab streamId from action-icon click; picker fallback in catch.
+ _diagBranch("offscreen-pre-acquired-tab");
console.log("[CloudRecorder][offscreen] using pre-acquired tab streamId");
startStream(data, tabID.current, permissions, permissions2);
} else if (IS_OFFSCREEN_HOST) {
+ _diagBranch("offscreen-useDisplayMedia");
startStream(data, null, permissions, permissions2, {
useDisplayMedia: true,
});
- } else if (!isTab.current && (data.recordingType != "region" || tabPreferred.current)) {
+ } else if (
+ !isTab.current &&
+ (data.recordingType != "region" || tabPreferred.current)
+ ) {
+ _diagBranch("desktop-picker");
// Desktop picker path: non-tab mode without region, or tabPreferred forced
// isTab=false (playground) so there's no pre-obtained streamId.
{
@@ -5776,8 +6134,22 @@ const CloudRecorder = () => {
["screen", "window", "tab", "audio"],
null,
(streamId) => {
+ chrome.runtime
+ .sendMessage({
+ type: "offscreen-diag",
+ source: "desktop-picker-callback",
+ payload: {
+ streamIdPresent: !!streamId,
+ streamIdPrefix: String(streamId || "").slice(0, 16),
+ lastError: chrome.runtime.lastError?.message || null,
+ },
+ })
+ .catch(() => {});
if (!streamId) {
- sendRecordingError("User cancelled stream selection", true);
+ sendRecordingError(
+ "User cancelled stream selection [desktop-picker null streamId]",
+ true,
+ );
} else {
startStream(data, streamId, permissions, permissions2);
}
@@ -5785,6 +6157,7 @@ const CloudRecorder = () => {
);
}
} else {
+ _diagBranch("final-else-tabID");
startStream(data, tabID.current, permissions, permissions2);
}
} catch (err) {
@@ -6102,12 +6475,9 @@ const CloudRecorder = () => {
} else if (request.type === "dismiss-recording") {
if (!isInit.current) return;
const dismissReason = request.reason || "dismiss-recording-msg";
- // Back-to-back cross-talk guard: a dismiss issued for a previous
- // recording can reach this (newer) recorder because dismiss-recording
- // is routed by a single global recordingTab pointer. If the dismiss
- // provably targets a different project than the one this recorder is
- // actively capturing, ignore it — honoring it would discard a healthy
- // recording. Unknown ids (no session yet / legacy sender) are honored.
+ // Cross-talk guard: a dismiss for a previous recording can
+ // reach this recorder via the global recordingTab pointer.
+ // Ignore when projectIds disagree; unknown ids are honored.
const myProjectId = recorderSession.current?.projectId || null;
const targetProjectId = request.projectId || null;
if (myProjectId && targetProjectId && myProjectId !== targetProjectId) {
diff --git a/src/pages/CloudRecorder/bunnyTusUploader.js b/src/pages/CloudRecorder/bunnyTusUploader.js
index 5c82371..5530bfc 100644
--- a/src/pages/CloudRecorder/bunnyTusUploader.js
+++ b/src/pages/CloudRecorder/bunnyTusUploader.js
@@ -776,6 +776,18 @@ export default class BunnyTusUploader {
const data = await res.json();
this.videoId = data.videoId;
this.mediaId = data.mediaId;
+ // The server now inlines the TUS upload signature in this
+ // response (data.tusAuth). When present, stash it so the
+ // immediate-next refreshTusAuth() can skip its GET round-trip.
+ // Saves ~300-1000ms per uploader in dev. Falls back to the
+ // GET path when tusAuth is missing (older server / resume).
+ if (data?.tusAuth?.signature && data.tusAuth.expires && data.tusAuth.libraryId) {
+ this._inlinedTusAuth = {
+ signature: data.tusAuth.signature,
+ expires: data.tusAuth.expires,
+ libraryId: data.tusAuth.libraryId,
+ };
+ }
await this.persistVideoMap({
projectId,
sceneId,
@@ -796,7 +808,34 @@ export default class BunnyTusUploader {
}
this.journalKey = this.journalKey || this.getJournalKey(this.mediaId);
- await this.refreshTusAuth();
+ try {
+ await this.refreshTusAuth();
+ } catch (refreshErr) {
+ // 404 from tus-auth means the Bunny video is gone (orphan
+ // reaper, manual cleanup) but local resume state lingered.
+ // Clear it so the retry creates a fresh video instead of
+ // looping on identical 404s.
+ if (refreshErr?.status === 404) {
+ this.debugLog("Clearing stale resume state after tus-auth 404", {
+ mediaId: String(this.mediaId || ""),
+ videoId: String(this.videoId || ""),
+ });
+ try {
+ await this.clearUploadJournal();
+ } catch {}
+ try {
+ const mapKey = this.getVideoMapKey(
+ this.projectId,
+ this.sceneId,
+ this.metadata?.type,
+ );
+ if (mapKey) {
+ await chrome.storage.local.remove([mapKey]);
+ }
+ } catch {}
+ }
+ throw refreshErr;
+ }
if (this.uploadUrl) {
const serverOffset = await this.getServerOffset();
@@ -839,6 +878,25 @@ export default class BunnyTusUploader {
}
async refreshTusAuth() {
+ // Fast path: /api/bunny/videos inlines the TUS signature on the
+ // create response. Use it once to skip a round-trip; subsequent
+ // calls (expiry, journal resume) fall through to the GET.
+ if (this._inlinedTusAuth) {
+ const { signature, expires, libraryId } = this._inlinedTusAuth;
+ this._inlinedTusAuth = null;
+ // Only use it if it's still well within its validity window.
+ // generateBunnyUploadSignature returns `expires` as a unix
+ // seconds timestamp; require at least 30s of headroom.
+ const nowSec = Math.floor(Date.now() / 1000);
+ if (typeof expires === "number" && expires - nowSec > 30) {
+ this.signature = signature;
+ this.expires = expires;
+ this.libraryId = libraryId;
+ this.scheduleJournalPersist();
+ return;
+ }
+ }
+
const token = this.userToken || (await chrome.storage.local.get(["screenityToken"]).then(r => r.screenityToken));
const headers = token ? { Authorization: `Bearer ${token}` } : {};
const url = `${API_BASE}/bunny/videos/tus-auth?videoId=${this.videoId}`;
@@ -887,12 +945,15 @@ export default class BunnyTusUploader {
await this._sleepWithJitter(500 * Math.pow(2, attempt));
}
- // Build a structured error whose message matches the outer transient regex
- // (/network/i) for 5xx/blip cases, and stays non-transient for 401/403.
+ // Classify so the outer retry loop's /network/i regex matches
+ // 5xx (transient) but not 401/403 (auth) or 404 (missing). 404
+ // means the backing video is gone; retrying never recovers it.
const classification =
lastStatus === 400 || lastStatus === 401 || lastStatus === 403
? "unauthorized"
- : "network";
+ : lastStatus === 404
+ ? "missing"
+ : "network";
const err = new Error(
`Failed to refresh TUS auth (${classification} ${lastStatus ?? "offline"})`,
);
diff --git a/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js b/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js
index 1c5c3ec..53d1c1a 100644
--- a/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js
+++ b/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js
@@ -1,9 +1,6 @@
// MediaRecorder-shaped wrapper around WebCodecsRecorder so Cloud's
-// existing recorder lifecycle (start/stop/pause/resume, ondataavailable,
-// _pendingWrites drain) drives WebCodecs without a refactor. Muxer
-// fragments come out as fMP4 Blobs through ondataavailable; uploader
-// just sees Blobs. `state` mirrors the MediaRecorder string so existing
-// comparisons in CloudRecorder.jsx still work.
+// existing lifecycle drives WebCodecs without a refactor. fMP4
+// fragments come out as Blobs via ondataavailable.
import { WebCodecsRecorder } from "../../Recorder/webcodecs/WebCodecsRecorder";
@@ -24,6 +21,9 @@ export class WebCodecsTrackRecorder {
options.videoBitsPerSecond || 16_000_000;
this.audioBitsPerSecond = options.audioBitsPerSecond || 128_000;
this.enableAudio = options.enableAudio !== false; // default true
+ // Used by cloud's camera path on macOS to force a software h264
+ // encoder, sidestepping VideoToolbox's per-process HW-slot serialization.
+ this.preferSoftware = Boolean(options.preferSoftware);
// MediaRecorder-shape fields used by callers.
this._state = "inactive";
this.ondataavailable = null;
@@ -109,13 +109,10 @@ export class WebCodecsTrackRecorder {
enableAudio: this.enableAudio,
videoBitrate: this.videoBitsPerSecond,
audioBitrate: this.audioBitsPerSecond,
- // Cap encoded resolution at 1080p. Without this, retina screen
- // captures (often 2200x1440 or higher) exceed the AVC Level 4.2
- // max coded area (~2.2 MP) and the encoder throws NotSupportedError
- // on the first frame, leaving Bunny with only the 28-byte ftyp init.
- // 1080p matches Bunny Stream's tier ceiling, so users can't play
- // back at higher than 1080p anyway. WebCodecsRecorder downscales
- // via its resize canvas; aspect ratio is preserved.
+ preferSoftware: this.preferSoftware,
+ // Cap at 1080p; retina captures exceed AVC L4.2 (~2.2MP) and
+ // throw on first frame, leaving Bunny with the 28-byte init.
+ // Bunny tier caps at 1080p anyway. WCR downscales via canvas.
width: 1920,
height: 1080,
});
diff --git a/src/pages/CloudRecorder/encoder/chooseEncoder.js b/src/pages/CloudRecorder/encoder/chooseEncoder.js
index cb4fba7..285a3d7 100644
--- a/src/pages/CloudRecorder/encoder/chooseEncoder.js
+++ b/src/pages/CloudRecorder/encoder/chooseEncoder.js
@@ -11,15 +11,10 @@ import {
import { WebCodecsTrackRecorder } from "./WebCodecsTrackRecorder";
import { probeHwSlots } from "./hwSlotProbe";
-// Mirrors WebCodecsRecorder's resize-canvas dimension math (Recorder/
-// webcodecs/WebCodecsRecorder.js ~line 199-209). Cloud calls this so
-// the per-track uploader + persisted scene.{screen,camera} record the
-// actual encoded dimensions instead of the source track's native
-// dimensions, which can be retina-scale (e.g. 2200x1440) on macOS
-// while the encoded video is capped at 1080p.
-//
-// MediaRecorder paths don't downscale (they record at native), so
-// callers must skip this when kind === "mediarecorder".
+// Mirrors WebCodecsRecorder's resize-canvas math. Cloud uploaders
+// and scene.{screen,camera} need encoded dims, not the source
+// track's native (retina-scale on macOS while encode caps at 1080p).
+// MediaRecorder records native, so callers skip this for it.
export const WEBCODECS_CAP_WIDTH = 1920;
export const WEBCODECS_CAP_HEIGHT = 1080;
const HARD_CAP = 3840;
@@ -123,12 +118,19 @@ export const inspectTrackPlan = async ({ track, probeOptions }) => {
reason: "screen-hw-camera-mr-mode",
};
}
+ // Camera on WebCodecs but with software encoder so it doesn't fight
+ // the screen's HW slot. Same MP4 h264 container as the dual-hw mode,
+ // just a different backend selection inside WebCodecsRecorder.
+ const cameraPreferSoftware =
+ track === "camera" &&
+ hwSlots.summary.mode === "dual-webcodecs-camera-sw";
return {
kind: "webcodecs",
container: "video/mp4",
codec: "avc1.4D0028",
hwSlots: hwSlots.summary,
- reason: "ok",
+ cameraPreferSoftware,
+ reason: cameraPreferSoftware ? "dual-webcodecs-camera-sw-mode" : "ok",
};
};
@@ -159,6 +161,7 @@ export const chooseTrackEncoder = async ({
videoBitsPerSecond,
audioBitsPerSecond,
enableAudio,
+ preferSoftware: Boolean(plan.cameraPreferSoftware),
});
recorder.ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
@@ -186,6 +189,7 @@ export const chooseTrackEncoder = async ({
// gate's marker function distinguishes via its own pattern list.
void markFastRecorderFailure(`cloud-${track}-runtime`, {
error: String(err?.message || err),
+ detail: err?.detail || null,
track,
});
};
diff --git a/src/pages/CloudRecorder/encoder/hwSlotProbe.js b/src/pages/CloudRecorder/encoder/hwSlotProbe.js
index 331359a..bec9d90 100644
--- a/src/pages/CloudRecorder/encoder/hwSlotProbe.js
+++ b/src/pages/CloudRecorder/encoder/hwSlotProbe.js
@@ -1,23 +1,14 @@
-// probes HW H.264 slot availability via VideoEncoder.isConfigSupported.
-// screen + camera each want a slot. Intel Quick Sync usually has 2,
-// Apple Silicon many more, Linux often 0-1, VMs often 0.
-// returned separately so chooseEncoder can fall the camera back to
-// MediaRecorder/VP9 when only the screen slot is free (mixing HW H.264
-// for screen + SW H.264 for camera trashes the main thread).
-
-// Probe at a conservative bitrate baseline (4 Mbps). The real encoder
-// runs later at the desired bitrate; isConfigSupported gates codec /
-// profile / dimensions / HW availability, and some HW reports false on
-// aggressive bitrates that it actually handles fine in practice.
+// Probes HW H.264 slot availability via isConfigSupported. Screen +
+// camera each want a slot; returned separately so chooseEncoder can
+// fall camera back to MediaRecorder/VP9 when only one slot is free.
+
+// 4 Mbps baseline; real encoder uses the configured bitrate later.
const PROBE_BITRATE = 4_000_000;
-// Codec candidates ordered by preference. Trying multiple lets the probe
-// succeed on machines where the highest profile isn't supported but a
-// lower one (e.g. Baseline L3.0) is. Mirrors fastRecorderGate's approach.
-// avc1.64002A = High Profile L4.2 (up to 1080p high bitrate)
-// avc1.4D0028 = Main L4.0 (up to 1080p)
-// avc1.4D401F = Main L3.1 (up to 720p high bitrate)
-// avc1.42E01E = Baseline L3.0 (universal compatibility)
+// Candidates ordered High → Baseline so machines without the top
+// profile still probe successfully.
+// 64002A = High L4.2, 4D0028 = Main L4.0,
+// 4D401F = Main L3.1, 42E01E = Baseline L3.0
const CODEC_CANDIDATES = [
"avc1.64002A",
"avc1.4D0028",
@@ -38,11 +29,9 @@ const buildVideoConfig = ({ codec, width, height, framerate, hwOption }) => ({
hardwareAcceleration: hwOption,
});
-// Try (codec × hw-option) combinations until one reports supported. A
-// machine that supports plain H.264 software but not hardware still gets
-// WebCodecs (better encoder reliability than MediaRecorder VP9, which is
-// also software). The "all-mr" fallback only triggers when every codec
-// + hw option fails.
+// Try codec × hw-option until one reports supported. SW-only H.264
+// still gets WebCodecs (more reliable than MediaRecorder VP9, also SW).
+// "all-mr" triggers only when every combo fails.
const probeOne = async (label, { width, height, framerate }) => {
if (typeof VideoEncoder === "undefined") {
return { label, supported: false, reason: "VideoEncoder unavailable" };
@@ -85,17 +74,144 @@ const probeOne = async (label, { width, height, framerate }) => {
return { label, supported: false, attempts };
};
+// Two-encoder concurrency probe. isConfigSupported reports each
+// encoder independently; both can return supported on a one-slot HW
+// (macOS especially), where the second silently falls back to
+// software and runs at ~4fps. Open two real encoders and check both
+// emit at rate; if the second is >2x slower, force camera onto
+// MediaRecorder. 30 frames (~1s) because 8 missed it (keyframe
+// buffering hides asymmetry until ~500ms in). 2s cap, memoized.
+const CONCURRENT_FRAME_COUNT = 30;
+const CONCURRENT_TIMEOUT_MS = 2500;
+
+const probeConcurrentHw = async ({ codec, framerate }) => {
+ if (
+ typeof VideoEncoder === "undefined" ||
+ typeof VideoFrame === "undefined" ||
+ typeof OffscreenCanvas === "undefined"
+ ) {
+ return { ran: false, reason: "no-webcodecs" };
+ }
+ const width = 1280;
+ const height = 720;
+ const config = {
+ codec,
+ width,
+ height,
+ bitrate: PROBE_BITRATE,
+ framerate,
+ bitrateMode: "constant",
+ latencyMode: "realtime",
+ hardwareAcceleration: "prefer-hardware",
+ };
+ const makeEncoder = () => {
+ let chunks = 0;
+ let error = null;
+ const encoder = new VideoEncoder({
+ output: () => {
+ chunks += 1;
+ },
+ error: (err) => {
+ error = String(err?.message || err);
+ },
+ });
+ try {
+ encoder.configure(config);
+ } catch (err) {
+ try {
+ encoder.close();
+ } catch {}
+ return null;
+ }
+ return {
+ encoder,
+ getChunks: () => chunks,
+ getError: () => error,
+ close: () => {
+ try {
+ encoder.close();
+ } catch {}
+ },
+ };
+ };
+
+ const a = makeEncoder();
+ const b = makeEncoder();
+ if (!a || !b) {
+ a?.close();
+ b?.close();
+ return { ran: false, reason: "configure-failed" };
+ }
+
+ const canvas = new OffscreenCanvas(width, height);
+ const ctx = canvas.getContext("2d");
+ if (!ctx) {
+ a.close();
+ b.close();
+ return { ran: false, reason: "no-2d-context" };
+ }
+ const started =
+ typeof performance !== "undefined" ? performance.now() : Date.now();
+ for (let i = 0; i < CONCURRENT_FRAME_COUNT; i++) {
+ ctx.fillStyle = `hsl(${(i * 31) % 360}, 50%, 40%)`;
+ ctx.fillRect(0, 0, width, height);
+ const ts = Math.round((i * 1_000_000) / framerate);
+ const dur = Math.round(1_000_000 / framerate);
+ try {
+ const frameA = new VideoFrame(canvas, {
+ timestamp: ts,
+ duration: dur,
+ });
+ a.encoder.encode(frameA, { keyFrame: i === 0 });
+ frameA.close();
+ } catch {}
+ try {
+ const frameB = new VideoFrame(canvas, {
+ timestamp: ts,
+ duration: dur,
+ });
+ b.encoder.encode(frameB, { keyFrame: i === 0 });
+ frameB.close();
+ } catch {}
+ }
+ try {
+ await Promise.race([
+ Promise.allSettled([a.encoder.flush(), b.encoder.flush()]),
+ new Promise((r) => setTimeout(r, CONCURRENT_TIMEOUT_MS)),
+ ]);
+ } catch {}
+ const elapsed =
+ (typeof performance !== "undefined" ? performance.now() : Date.now()) -
+ started;
+ const aChunks = a.getChunks();
+ const bChunks = b.getChunks();
+ a.close();
+ b.close();
+ // Healthy dual-HW: both encoders emit close to the input frame count.
+ // We require at least 4 chunks from each (allowing for encoder
+ // buffering of the last keyframe close-out). A 2x ratio is the
+ // tripwire: anything worse means the second encoder is starved.
+ const minPerEncoder = 4;
+ const concurrent =
+ aChunks >= minPerEncoder &&
+ bChunks >= minPerEncoder &&
+ Math.max(aChunks, bChunks) / Math.max(1, Math.min(aChunks, bChunks)) < 2;
+ return {
+ ran: true,
+ concurrent,
+ aChunks,
+ bChunks,
+ elapsedMs: Math.round(elapsed),
+ };
+};
+
export const probeHwSlots = async ({
framerate = 30,
} = {}) => {
- // Probe at a universal 720p baseline regardless of the actual stream
- // dimensions. isConfigSupported is a feasibility check for this codec
- // family on this hardware. modern HW that can encode 720p H.264
- // can encode any resolution we'd realistically capture (Bunny caps at
- // 1080p tier anyway). Probing at the actual resolution is risky:
- // getSettings() can return inaccurate values during track warmup, and
- // some Chromium builds (Playwright's headless variant) reject 1080p
- // configs even though the real encoder handles them once started.
+ // Probe at 720p baseline; modern HW that encodes 720p H.264 handles
+ // anything we'd capture. Probing at the real resolution is flaky:
+ // getSettings() lies during track warmup, and headless Chromium
+ // rejects 1080p that the real encoder accepts once started.
const screenProbe = await probeOne("screen", {
width: 1280,
height: 720,
@@ -106,17 +222,71 @@ export const probeHwSlots = async ({
height: 720,
framerate,
});
+
+ // Second stage: both probes can claim supported while only one HW
+ // slot exists per process (macOS especially). Diag showed camera
+ // at 4fps SW fallback in this case.
+ let concurrentProbe = null;
+ if (screenProbe.supported && cameraProbe.supported) {
+ const codec =
+ screenProbe.configResolved?.codec ||
+ cameraProbe.configResolved?.codec ||
+ CODEC_CANDIDATES[0];
+ try {
+ concurrentProbe = await probeConcurrentHw({ codec, framerate });
+ } catch {
+ concurrentProbe = { ran: false, reason: "exception" };
+ }
+ }
+
+ // macOS VideoToolbox h264 serializes per process; a second concurrent
+ // prefer-hardware silently falls back to SW at unpredictable fps.
+ // Route camera to explicit SW (below) to avoid contending the slot.
+ const ua = typeof navigator !== "undefined" ? navigator.userAgent : "";
+ const isMacUA = /Mac OS X|Macintosh/i.test(ua);
+
+ // cameraHw=false still records via WebCodecs SW (same h264 MP4).
+ // Only flip to MediaRecorder when isConfigSupported itself fails.
+ const cameraHwAvailable =
+ cameraProbe.supported &&
+ (!concurrentProbe || !concurrentProbe.ran || concurrentProbe.concurrent);
+
+ // Force SW h264 for camera on macOS or when the concurrent probe
+ // showed HW starvation. Same h264 MP4 output, just SW backend.
+ const cameraPreferSoftware =
+ isMacUA ||
+ Boolean(concurrentProbe?.ran && concurrentProbe.concurrent === false);
+
+ // cameraHw is "viable for WebCodecs encoding". Software h264 still
+ // counts; server only cares about the bitstream, not the backend.
+ // The only way cameraHw becomes false is if isConfigSupported itself
+ // rejected every codec candidate at the individual probe step.
+ const cameraHwViable = cameraProbe.supported;
+
return {
screen: screenProbe,
camera: cameraProbe,
+ concurrentProbe,
summary: {
screenHw: screenProbe.supported,
- cameraHw: cameraProbe.supported,
- // Both supported = dual-hw. Only screen = degrade camera. Neither
- // (or only camera, which we don't optimize for) = all-mr.
+ cameraHw: cameraHwViable,
+ cameraHwAvailable,
+ cameraHwAdvertised: cameraProbe.supported,
+ cameraPreferSoftware,
+ concurrent: concurrentProbe?.concurrent ?? null,
+ concurrentRan: Boolean(concurrentProbe?.ran),
+ isMacUA,
+ // - dual-hw → both encoders WebCodecs, both HW
+ // - dual-webcodecs-camera-sw → both encoders WebCodecs, camera SW
+ // (Mac or HW-contended hardware)
+ // - screen-hw-camera-mr → only screen can use h264; camera
+ // falls back to MediaRecorder VP9
+ // - all-mr → no WebCodecs h264 support at all
mode: screenProbe.supported
- ? cameraProbe.supported
- ? "dual-hw"
+ ? cameraHwViable
+ ? cameraPreferSoftware
+ ? "dual-webcodecs-camera-sw"
+ : "dual-hw"
: "screen-hw-camera-mr"
: "all-mr",
},
diff --git a/src/pages/Content/Wrapper.jsx b/src/pages/Content/Wrapper.jsx
index 09e1906..684a271 100644
--- a/src/pages/Content/Wrapper.jsx
+++ b/src/pages/Content/Wrapper.jsx
@@ -1,9 +1,13 @@
-import React, { useContext, useRef, useEffect } from "react";
+import React, { useContext, useRef, useEffect, useState } from "react";
import PopupContainer from "./popup/PopupContainer";
import Toolbar from "./toolbar/Toolbar";
import Camera from "./camera/Camera";
import CameraOnly from "./camera-only/CameraOnly";
+// Static import (fabric is ~313KB). React.lazy can't dynamic-import in
+// content scripts on strict-CSP pages: host script-src blocks the
+// chunk insertion even with web_accessible_resources. Verified
+// ChunkLoadError on google.com.
import Canvas from "./canvas/Canvas";
import Countdown from "./countdown/Countdown";
import Modal from "./modal/Modal";
@@ -121,6 +125,80 @@ const Wrapper = () => {
contentStateRef.current = contentState;
}, [contentState]);
+ // Delayed loader: only show after 800ms in a wait window
+ // (pre-countdown setup or post-stop finalize), never during
+ // countdown or active recording. Skip the flash when start
+ // resolves in <500ms.
+ const LOADER_DELAY_MS = 800;
+ const inPreCountdownWait =
+ Boolean(contentState.pendingRecording) &&
+ !contentState.countdownActive &&
+ !contentState.isCountdownVisible &&
+ !contentState.recording &&
+ // Latched after first countdown appearance. Prevents re-show in
+ // the countdown-end → recording-true gap (capture is already
+ // live by then for region/desktop).
+ !contentState.countdownEverShown;
+ // Gate post-stop loader on encoderActive=false. Encoder can keep
+ // capturing several seconds after stop click on contended Chrome
+ // (seen up to 9s in diag); without this gate the loader gets baked
+ // into the recorded video.
+ const inPostStopWait =
+ Boolean(contentState.finalizingRecording) &&
+ contentState.encoderActive === false;
+ // Restart wait: streams are reused, no picker pop, so the
+ // visibility-hidden gate below never fires. Without this flag the
+ // toolbar would just freeze.
+ const inRestartWait = Boolean(contentState.restartingRecording);
+ const waitActive = inPreCountdownWait || inPostStopWait || inRestartWait;
+ const [showLoader, setShowLoader] = useState(false);
+ useEffect(() => {
+ if (!waitActive) {
+ setShowLoader(false);
+ return;
+ }
+ // Only show after a hide → show round-trip. If the tab never went
+ // hidden, the recorder tab's own loading state is what the user
+ // sees; our overlay would just flash on the source tab.
+ let timeoutId = null;
+ let wasHiddenThisWait = false;
+ const armTimer = () => {
+ if (document.hidden) return;
+ if (!wasHiddenThisWait) return;
+ timeoutId = setTimeout(() => {
+ if (!document.hidden) setShowLoader(true);
+ }, LOADER_DELAY_MS);
+ };
+ const onVisibility = () => {
+ if (document.hidden) {
+ wasHiddenThisWait = true;
+ if (timeoutId) clearTimeout(timeoutId);
+ timeoutId = null;
+ setShowLoader(false);
+ } else if (!timeoutId) {
+ armTimer();
+ }
+ };
+ // Post-stop skips the visibility gate (stop click is on the
+ // active tab; show feedback there immediately after 800ms).
+ if (inPostStopWait) {
+ wasHiddenThisWait = true;
+ }
+ // Same exemption for restart: the click is on the source tab and
+ // the recorder doesn't reopen a picker, so the tab never goes
+ // hidden. Without skipping the gate, the loader wouldn't fire and
+ // the user sees a frozen toolbar for the full restart window.
+ if (inRestartWait) {
+ wasHiddenThisWait = true;
+ }
+ armTimer();
+ document.addEventListener("visibilitychange", onVisibility);
+ return () => {
+ if (timeoutId) clearTimeout(timeoutId);
+ document.removeEventListener("visibilitychange", onVisibility);
+ };
+ }, [waitActive, inPostStopWait, inRestartWait]);
+
useEffect(() => {
if (!parentRef.current) return;
@@ -288,7 +366,12 @@ const Wrapper = () => {
}}
></div>
)}
- <Canvas />
+ {(contentState.drawingMode || contentState.blurMode) && (
+ // Key on multiSceneCount only: fresh fabric per scene,
+ // but drawings persist through the pre-record → record
+ // transition within a scene.
+ <Canvas key={`canvas-${contentState.multiSceneCount || 0}`} />
+ )}
<CursorModes />
<root.div
className="root-container"
@@ -326,9 +409,21 @@ const Wrapper = () => {
{contentState.recordingType === "region" &&
contentState.customRegion && <Region />}
{shadowRef.current && <Modal shadowRef={shadowRef} />}
- {contentState.preparingRecording && (
- <RecordingLoader />
- )}
+ {/*
+ Render-time double-check: even if `showLoader` is true,
+ hide the pre-countdown loader the moment countdownActive
+ / isCountdownVisible flips on. The 800ms loader timer
+ can fire in the same scheduler tick as the
+ ready-to-record handler, producing a one-frame overlap
+ where both the "Processing your recording…" overlay and
+ the countdown render simultaneously. Tying visibility
+ to the live contentState here closes that frame -
+ showLoader still gets cleared by the useEffect cleanup
+ on the next tick, but the user never sees the overlap.
+ */}
+ {showLoader &&
+ !contentState.countdownActive &&
+ !contentState.isCountdownVisible && <RecordingLoader />}
<Countdown />
{contentState.recordingType != "camera" &&
!contentState.onboarding &&
diff --git a/src/pages/Content/canvas/fabricCompat.js b/src/pages/Content/canvas/fabricCompat.js
new file mode 100644
index 0000000..3fe6da1
--- /dev/null
+++ b/src/pages/Content/canvas/fabricCompat.js
@@ -0,0 +1,78 @@
+// Fabric v5 → v6 shim. v6 dropped the `fabric` namespace export, so
+// this re-exports v6's named exports under the legacy `fabric.X` shape
+// our call sites use. Tree-shaking still works.
+import {
+ Canvas,
+ StaticCanvas,
+ ActiveSelection,
+ Circle,
+ Color,
+ Control,
+ Ellipse,
+ FabricImage,
+ FabricObject,
+ FabricText,
+ Gradient,
+ Group,
+ IText,
+ Line,
+ Path,
+ Pattern,
+ PencilBrush,
+ Point,
+ Polygon,
+ Polyline,
+ Rect,
+ Shadow,
+ Textbox,
+ Triangle,
+ classRegistry,
+ config,
+ controlsUtils,
+ filters,
+ loadSVGFromString,
+ loadSVGFromURL,
+ util,
+ version,
+} from "fabric";
+
+// v5-style namespace; only the classes used in src/pages/Content/canvas.
+export const fabric = {
+ Canvas,
+ StaticCanvas,
+ ActiveSelection,
+ Circle,
+ Color,
+ Control,
+ Ellipse,
+ Image: FabricImage,
+ FabricImage,
+ Object: FabricObject,
+ FabricObject,
+ Text: FabricText,
+ FabricText,
+ Gradient,
+ Group,
+ IText,
+ Line,
+ Path,
+ Pattern,
+ PencilBrush,
+ Point,
+ Polygon,
+ Polyline,
+ Rect,
+ Shadow,
+ Textbox,
+ Triangle,
+ classRegistry,
+ config,
+ controlsUtils,
+ filters,
+ loadSVGFromString,
+ loadSVGFromURL,
+ util,
+ version,
+};
+
+export default fabric;
diff --git a/src/pages/Content/canvas/layout/CanvasWrap.jsx b/src/pages/Content/canvas/layout/CanvasWrap.jsx
index a20dfe4..a9d1838 100644
--- a/src/pages/Content/canvas/layout/CanvasWrap.jsx
+++ b/src/pages/Content/canvas/layout/CanvasWrap.jsx
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useContext } from "react";
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
import { contentStateContext } from "../../context/ContentState";
@@ -94,6 +94,12 @@ const CanvasWrap = (props) => {
canvas.isDrawingMode = shouldDraw;
+ // Suppress fabric's marquee rubber-band for any non-select tool
+ // so dragging mid-tool doesn't paint the blue selection box.
+ // Tool modules toggle this too; the central gate closes the
+ // activation-gap window.
+ canvas.selection = !contentState.drawingMode || tool === "select";
+
// Important: when leaving pen/highlighter, clear any in-progress brush stroke
if (!shouldDraw && canvas.freeDrawingBrush) {
const brush = canvas.freeDrawingBrush;
diff --git a/src/pages/Content/canvas/modules/ArrowTool.jsx b/src/pages/Content/canvas/modules/ArrowTool.jsx
index 922a973..5e3cdb0 100644
--- a/src/pages/Content/canvas/modules/ArrowTool.jsx
+++ b/src/pages/Content/canvas/modules/ArrowTool.jsx
@@ -1,4 +1,4 @@
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
const createArrowLine = (x, y, color, strokeWidth) => {
return new fabric.Line([x, y, x, y], {
diff --git a/src/pages/Content/canvas/modules/CustomControls.jsx b/src/pages/Content/canvas/modules/CustomControls.jsx
index 826c2b4..ec6ceed 100644
--- a/src/pages/Content/canvas/modules/CustomControls.jsx
+++ b/src/pages/Content/canvas/modules/CustomControls.jsx
@@ -1,5 +1,5 @@
import React from "react";
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
import {
HandleControl,
@@ -87,6 +87,12 @@ const CustomControls = (canvas) => {
ctx.restore();
}
+ // fabric v6 leaves prototype.controls undefined; initialize it so
+ // the v5 `controls.tl = …` pattern below doesn't throw.
+ if (!fabric.Object.prototype.controls) {
+ fabric.Object.prototype.controls = {};
+ }
+
fabric.Object.prototype.controls.tl = new fabric.Control({
x: -0.5,
y: -0.5,
@@ -178,7 +184,11 @@ const CustomControls = (canvas) => {
render: renderIconRotate,
});
- // Also use same controls for Textbox
+ // Also use same controls for Textbox. v6 has the same undefined-by-
+ // default behavior on Textbox.prototype.controls.
+ if (!fabric.Textbox.prototype.controls) {
+ fabric.Textbox.prototype.controls = {};
+ }
fabric.Textbox.prototype.controls.tl = new fabric.Control({
x: -0.5,
y: -0.5,
diff --git a/src/pages/Content/canvas/modules/History.jsx b/src/pages/Content/canvas/modules/History.jsx
index 41225a7..e7fdf28 100644
--- a/src/pages/Content/canvas/modules/History.jsx
+++ b/src/pages/Content/canvas/modules/History.jsx
@@ -1,4 +1,4 @@
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
const getState = (stateOrRef) =>
stateOrRef && stateOrRef.current ? stateOrRef.current : stateOrRef;
@@ -27,7 +27,13 @@ const undoCanvas = (stateOrRef, setToolSettings) => {
canvas.clear();
canvas.renderAll();
- canvas.loadFromJSON(penultimateItem, () => {
+ // v6 change: loadFromJSON's second arg is a per-object reviver
+ // (called for every deserialized object), NOT the load-complete
+ // callback. The function now returns a Promise that resolves when
+ // loading finishes. v5-style callback usage would either silently
+ // misfire (treating the callback as a reviver) or never run the
+ // post-load cleanup.
+ canvas.loadFromJSON(penultimateItem).then(() => {
canvas.discardActiveObject();
canvas.renderAll();
});
@@ -49,7 +55,8 @@ const redoCanvas = (stateOrRef, setToolSettings) => {
const lastItem = redoStack.pop();
undoStack.push(lastItem);
- canvas.loadFromJSON(lastItem, () => {
+ // v6: loadFromJSON returns a Promise. See undoCanvas comment above.
+ canvas.loadFromJSON(lastItem).then(() => {
canvas.discardActiveObject();
canvas.renderAll();
});
diff --git a/src/pages/Content/canvas/modules/ImageTool.jsx b/src/pages/Content/canvas/modules/ImageTool.jsx
index 66549f6..48f218c 100644
--- a/src/pages/Content/canvas/modules/ImageTool.jsx
+++ b/src/pages/Content/canvas/modules/ImageTool.jsx
@@ -1,4 +1,4 @@
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
const ImageTool = (
canvas,
diff --git a/src/pages/Content/canvas/modules/PenTool.jsx b/src/pages/Content/canvas/modules/PenTool.jsx
index 399a363..9794188 100644
--- a/src/pages/Content/canvas/modules/PenTool.jsx
+++ b/src/pages/Content/canvas/modules/PenTool.jsx
@@ -1,4 +1,4 @@
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
const PenTool = (canvas, contentStateRef, setContentState, saveCanvas) => {
const getState = () => contentStateRef.current;
diff --git a/src/pages/Content/canvas/modules/SelectTool.jsx b/src/pages/Content/canvas/modules/SelectTool.jsx
index d71a5cd..8850afd 100644
--- a/src/pages/Content/canvas/modules/SelectTool.jsx
+++ b/src/pages/Content/canvas/modules/SelectTool.jsx
@@ -1,4 +1,4 @@
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
const SelectTool = (canvas, contentStateRef, setContentState) => {
const getState = () => contentStateRef.current;
diff --git a/src/pages/Content/canvas/modules/ShapeTool.jsx b/src/pages/Content/canvas/modules/ShapeTool.jsx
index 93bd383..9ae4372 100644
--- a/src/pages/Content/canvas/modules/ShapeTool.jsx
+++ b/src/pages/Content/canvas/modules/ShapeTool.jsx
@@ -1,4 +1,4 @@
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
const ShapeTool = (canvas, contentStateRef, setContentState, saveCanvas) => {
const getState = () => contentStateRef.current;
diff --git a/src/pages/Content/canvas/modules/TextTool.jsx b/src/pages/Content/canvas/modules/TextTool.jsx
index 83ce06b..f8245d2 100644
--- a/src/pages/Content/canvas/modules/TextTool.jsx
+++ b/src/pages/Content/canvas/modules/TextTool.jsx
@@ -1,4 +1,4 @@
-import { fabric } from "fabric";
+import { fabric } from "../fabricCompat";
const TextTool = (canvas, contentStateRef, setContentState, saveCanvas) => {
const getState = () => contentStateRef.current;
diff --git a/src/pages/Content/context/ContentState.jsx b/src/pages/Content/context/ContentState.jsx
index bfc8ea9..d3693a0 100644
--- a/src/pages/Content/context/ContentState.jsx
+++ b/src/pages/Content/context/ContentState.jsx
@@ -65,6 +65,13 @@ const ContentState = (props) => {
const tabRecordedIdRef = useRef(null);
const recordingUiTabRef = useRef(null);
const recordingStartTimeRef = useRef(null);
+ // Cached pause state for the timer; refreshed via storage.onChanged.
+ // Lets the tick loop read sync refs instead of awaiting storage
+ // every second (storage IPC can stall multi-second on contended Chrome).
+ const pausedRef = useRef(false);
+ const pausedAtRef = useRef(null);
+ const totalPausedMsRef = useRef(0);
+ const recordingFlagRef = useRef(false);
const timerReadSeqRef = useRef(0);
const lastBeepStartTimeRef = useRef(null);
const recordingBeepTabIdRef = useRef(null);
@@ -239,6 +246,12 @@ const ContentState = (props) => {
recording: false,
time: 0,
paused: false,
+ // Restart-wait flag: shows the RecordingLoader during the
+ // restart gap. The normal pre-countdown loader doesn't fire
+ // here because restart reuses streams (no picker → no
+ // visibility flip). Wrapper.jsx skips its visibility gate
+ // when this is true. Clears on next countdown / recording.
+ restartingRecording: true,
}));
}, 100);
}, []);
@@ -264,41 +277,57 @@ const ContentState = (props) => {
const stopRecording = useCallback(() => {
chrome.runtime.sendMessage({ type: "clear-recording-alarm" });
+ const isMulti = contentStateRef.current.multiMode;
+ // Preserve the user's tool state in multi-mode so they keep their
+ // pen / blur / cursor selection between scenes. Single-mode still
+ // tears them down because the editor takes over after stop.
chrome.storage.local.set({
restarting: false,
tabRecordedID: null,
- drawingMode: false,
- blurMode: false,
- cursorMode: "none",
- cursorEffects: [],
+ ...(isMulti
+ ? {}
+ : {
+ drawingMode: false,
+ blurMode: false,
+ cursorMode: "none",
+ cursorEffects: [],
+ }),
});
- const isMulti = contentStateRef.current.multiMode;
+ // Keep the RecordingLoader up until BG opens the editor. Setting
+ // recording:false here would flash an idle "00:00" toolbar during
+ // the 1-6s finalize window.
setContentState((prevContentState) => ({
...prevContentState,
- recording: false,
+ finalizingRecording: true,
paused: false,
timeWarning: false,
- showExtension: isMulti ? true : false,
- blurMode: false,
- showPopup: true,
- pendingRecording: false,
tabCaptureFrame: false,
pipEnded: false,
- time: 0,
- timer: 0,
- preparingRecording: isMulti ? true : false,
- drawingMode: false,
- blurMode: false,
- toolbarMode: "",
- cursorMode: "none",
- cursorEffects: [],
+ // Preserve tool state in multi so reopen-popup-multi can
+ // carry it to the next scene.
+ ...(isMulti
+ ? {}
+ : {
+ drawingMode: false,
+ blurMode: false,
+ toolbarMode: "",
+ cursorMode: "none",
+ cursorEffects: [],
+ }),
}));
const elements = document.querySelectorAll(".screenity-blur");
elements.forEach((element) => {
element.classList.remove("screenity-blur");
});
- setTimer(0);
perfMark("Content stop-click", { reason: "content-toolbar-stop" });
+ // Play the stop beep on click, not via the recording-state effect.
+ // finalizingRecording keeps contentState.recording true through
+ // finalize / multi-reopen, so the effect-driven beep would fire
+ // seconds late. Always fire on click + always suppress the effect.
+ try {
+ playBeep(stopBeepRef, "assets/sounds/beep.mp3");
+ } catch {}
+ suppressStopBeepRef.current = true;
chrome.runtime.sendMessage(
{ type: "stop-recording-tab", reason: "content-toolbar-stop" },
(res) => {
@@ -313,12 +342,50 @@ const ContentState = (props) => {
}
},
);
+ // Watchdog: clear finalizing if BG never signals editor-open.
+ // 30s is past worst-case (encoder flush is ~15s cap).
+ setTimeout(() => {
+ setContentState((prev) => {
+ if (!prev.finalizingRecording) return prev;
+ const wasMulti = prev.multiMode === true;
+ return {
+ ...prev,
+ finalizingRecording: false,
+ preparingRecording: false,
+ pendingRecording: false,
+ recording: false,
+ paused: false,
+ time: 0,
+ timer: 0,
+ timeWarning: false,
+ tabCaptureFrame: false,
+ pipEnded: false,
+ showExtension: wasMulti ? true : false,
+ showPopup: wasMulti ? true : true,
+ // Preserve tool state in multi-mode (see editor-open cleanup
+ // for the rationale).
+ drawingMode: wasMulti ? prev.drawingMode : false,
+ blurMode: wasMulti ? prev.blurMode : false,
+ toolbarMode: wasMulti ? prev.toolbarMode : "",
+ cursorMode: wasMulti ? prev.cursorMode : "none",
+ cursorEffects: wasMulti ? prev.cursorEffects : [],
+ cameraActive: false,
+ };
+ });
+ setTimer(0);
+ }, 30000);
});
const pauseRecording = useCallback((dismiss) => {
if (contentStateRef.current?.paused) return;
chrome.runtime.sendMessage({ type: "pause-recording-tab" });
+ // Freeze the timer immediately. Recorder's storage write lags
+ // 50-150ms through BG IPC, visible as the timer continuing while
+ // the dismiss-confirm modal is open.
+ pausedRef.current = true;
+ pausedAtRef.current = Date.now();
+
setTimeout(() => {
setContentState((prev) => ({
...prev,
@@ -337,6 +404,17 @@ const ContentState = (props) => {
if (!contentStateRef.current?.paused) return;
chrome.runtime.sendMessage({ type: "resume-recording-tab" });
+ // Mirror Recorder's totalPausedMs locally so the elapsed value
+ // stays stable on unfreeze instead of jumping when storage lands.
+ const now = Date.now();
+ if (pausedAtRef.current) {
+ totalPausedMsRef.current =
+ (totalPausedMsRef.current || 0) +
+ Math.max(0, now - pausedAtRef.current);
+ }
+ pausedRef.current = false;
+ pausedAtRef.current = null;
+
setContentState((prev) => ({
...prev,
paused: false,
@@ -487,6 +565,10 @@ const ContentState = (props) => {
...prev,
pendingRecording: true,
countdownCancelled: false,
+ // Reset the latched loader gate for the new session; the
+ // overlay can show during this session's pre-countdown wait
+ // until the countdown appears, then it's permanently off.
+ countdownEverShown: false,
}));
let permission = false;
@@ -552,6 +634,9 @@ const ContentState = (props) => {
setStartFlowOutcome("error", {
error: canUpload === false ? "storage-limit" : (error || "quota-check-failed"),
});
+ // Explicit storage write; see note on the removed
+ // contentState→storage useEffect.
+ chrome.storage.local.set({ pendingRecording: false });
setContentState((prev) => ({
...prev,
pendingRecording: false,
@@ -584,6 +669,7 @@ const ContentState = (props) => {
true,
);
setStartFlowOutcome("cancelled", { error: "permission-denied" });
+ chrome.storage.local.set({ pendingRecording: false });
setContentState((prevContentState) => ({
...prevContentState,
pendingRecording: false,
@@ -647,6 +733,7 @@ const ContentState = (props) => {
);
}
setStartFlowOutcome("error", { error: "insufficient-memory" });
+ chrome.storage.local.set({ pendingRecording: false });
setContentState((prevContentState) => ({
...prevContentState,
pendingRecording: false,
@@ -706,6 +793,7 @@ const ContentState = (props) => {
},
() => {
setStartFlowOutcome("cancelled", { error: "mic-muted-cancel" });
+ chrome.storage.local.set({ pendingRecording: false });
setContentState((prevContentState) => ({
...prevContentState,
pendingRecording: false,
@@ -727,6 +815,13 @@ const ContentState = (props) => {
);
} else {
perfMark("Content desktop-capture.sent");
+ // Sync recordingType to storage so the cloudrecorder tab reads
+ // the current pick, not a stale value from a prior session.
+ // Mismatch causes the CR dispatch to land in the null-tabID
+ // branch and crash with REC_START_CANCEL.
+ chrome.storage.local.set({
+ recordingType: contentStateRef.current.recordingType || "screen",
+ });
chrome.runtime.sendMessage({
type: "desktop-capture",
region:
@@ -958,6 +1053,24 @@ const ContentState = (props) => {
timer: 0,
processingProgress: 0,
recording: false,
+ // True between stop click and editor-open (or watchdog fire).
+ // Drives the toolbar "Saving recording…" state during finalize.
+ // for 1–6s in the background.
+ finalizingRecording: false,
+ // True between restart click and next countdown / recording.
+ // Drives the RecordingLoader during the restart gap. Cleared by
+ // the storage listener on recording → true (or restarting →
+ // by the countdown handler when the countdown begins.
+ restartingRecording: false,
+ // Mirrors storage.recording even during finalize (when local
+ // contentState.recording stays true for UI continuity). Post-stop
+ // loader gates on !encoderActive so it can't bleed into the video.
+ encoderActive: false,
+ // Latches the pre-countdown loader closed once the countdown
+ // appears, so it can't re-show in the countdown-end → recording=true
+ // gap (capture is already live by then and would burn in).
+ // Reset on every fresh start.
+ countdownEverShown: false,
startRecording: startRecording,
restartRecording: restartRecording,
stopRecording: stopRecording,
@@ -1418,29 +1531,28 @@ const ContentState = (props) => {
}
}, [contentState.openModal]);
- const updateTimerFromStorage = useCallback(async () => {
- const seq = ++timerReadSeqRef.current;
- const { recording, recordingStartTime, paused, pausedAt, totalPausedMs } =
- await chrome.storage.local.get([
- "recording",
- "recordingStartTime",
- "paused",
- "pausedAt",
- "totalPausedMs",
- ]);
- if (seq !== timerReadSeqRef.current) return;
-
- if (!recording || !recordingStartTime) {
+ // Tick the timer from cached refs synchronously. Refs are kept fresh
+ // by the storage.onChanged listener (cheap; only fires when the
+ // values change). The async fallback path runs once at mount and
+ // when refs are uninitialized; the hot loop never awaits storage
+ // again, so a contended chrome.storage IPC layer can't freeze the
+ // timer display.
+ const tickTimerFromRefs = useCallback(() => {
+ const recording = recordingFlagRef.current;
+ const startTime = recordingStartTimeRef.current;
+ if (!recording || !startTime) {
setTimer(0);
return;
}
-
const now = Date.now();
- const basePaused = totalPausedMs || 0;
- const extraPaused = paused && pausedAt ? Math.max(0, now - pausedAt) : 0;
+ const basePaused = totalPausedMsRef.current || 0;
+ const extraPaused =
+ pausedRef.current && pausedAtRef.current
+ ? Math.max(0, now - pausedAtRef.current)
+ : 0;
const elapsedSeconds = Math.max(
0,
- Math.floor((now - recordingStartTime - basePaused - extraPaused) / 1000),
+ Math.floor((now - startTime - basePaused - extraPaused) / 1000),
);
if (contentStateRef.current?.alarm) {
@@ -1449,19 +1561,42 @@ const ContentState = (props) => {
setTimer((prev) => (prev === nextRemaining ? prev : nextRemaining));
return;
}
-
setTimer((prev) => (prev === elapsedSeconds ? prev : elapsedSeconds));
}, []);
+ const updateTimerFromStorage = useCallback(async () => {
+ const seq = ++timerReadSeqRef.current;
+ const { recording, recordingStartTime, paused, pausedAt, totalPausedMs } =
+ await chrome.storage.local.get([
+ "recording",
+ "recordingStartTime",
+ "paused",
+ "pausedAt",
+ "totalPausedMs",
+ ]);
+ if (seq !== timerReadSeqRef.current) return;
+ // Sync the refs so the next tick path is synchronous.
+ recordingFlagRef.current = Boolean(recording);
+ recordingStartTimeRef.current = recordingStartTime || null;
+ pausedRef.current = Boolean(paused);
+ pausedAtRef.current = pausedAt || null;
+ totalPausedMsRef.current = totalPausedMs || 0;
+ tickTimerFromRefs();
+ }, [tickTimerFromRefs]);
+
useEffect(() => {
+ // One-time seed from storage to populate refs, then tick from
+ // refs synchronously. Storage onChanged keeps refs fresh; the
+ // hot tick never awaits IPC again.
updateTimerFromStorage();
- // This runs in every open tab; skip reads on hidden ones.
const interval = setInterval(() => {
if (document.hidden) return;
- updateTimerFromStorage();
+ tickTimerFromRefs();
}, 1000);
const handleVisibility = () => {
if (!document.hidden) {
+ // On visibility change, refresh refs from storage (in case
+ // we missed onChanged events while hidden), then tick.
updateTimerFromStorage();
}
};
@@ -1473,7 +1608,51 @@ const ContentState = (props) => {
document.removeEventListener("visibilitychange", handleVisibility);
window.removeEventListener("focus", handleFocus);
};
- }, [updateTimerFromStorage]);
+ }, [updateTimerFromStorage, tickTimerFromRefs]);
+
+ // Reconcile stale recording-flow state when the tab becomes visible.
+ // Storage onChanged events may not fire reliably on suspended/hidden
+ // tabs, so finalizingRecording can latch true even after the
+ // recording has fully stopped elsewhere. Without this, Wrapper.jsx's
+ // 800ms post-stop loader timer fires on the next hide→show
+ // transition and the "Preparing..." overlay appears.
+ useEffect(() => {
+ const reconcileOnVisible = async () => {
+ if (document.hidden) return;
+ try {
+ const snap = await chrome.storage.local.get([
+ "recording",
+ "restarting",
+ ]);
+ if (snap.recording || snap.restarting) return;
+ setContentState((prev) =>
+ prev.finalizingRecording ||
+ prev.preparingRecording ||
+ prev.pendingRecording ||
+ prev.restartingRecording ||
+ prev.recording
+ ? {
+ ...prev,
+ finalizingRecording: false,
+ preparingRecording: false,
+ pendingRecording: false,
+ restartingRecording: false,
+ recording: false,
+ }
+ : prev,
+ );
+ } catch {}
+ };
+ document.addEventListener("visibilitychange", reconcileOnVisible);
+ window.addEventListener("focus", reconcileOnVisible);
+ // Also run once on mount in case the tab was already visible when
+ // the listener attached (e.g., HMR or late content-script inject).
+ reconcileOnVisible();
+ return () => {
+ document.removeEventListener("visibilitychange", reconcileOnVisible);
+ window.removeEventListener("focus", reconcileOnVisible);
+ };
+ }, []);
useEffect(() => {
const onChanged = (changes, area) => {
@@ -1493,6 +1672,70 @@ const ContentState = (props) => {
recordingStartTimeRef.current =
changes.recordingStartTime.newValue ?? null;
}
+ // Mirror pause/recording state into refs the timer reads
+ // synchronously each tick. Keeps the visual tick correct even
+ // when chrome.storage.local.get blocks on a contended IPC layer.
+ if (changes.recording) {
+ recordingFlagRef.current = Boolean(changes.recording.newValue);
+ // Clear the restart-wait loader as soon as the next
+ // recording starts. Storage flip true → false would also
+ // qualify but that's handled by the cleanup paths that fire
+ // around stop; here we specifically catch the
+ // restart→recording-resumes transition.
+ if (changes.recording.newValue === true) {
+ setContentState((prev) =>
+ prev.restartingRecording
+ ? { ...prev, restartingRecording: false }
+ : prev,
+ );
+ }
+ // Recording true → false: clear any local flow state that
+ // the sandboxTab listener might have missed on a hidden tab
+ // (Chrome suspends bg tabs; sandboxTab listener may not fire
+ // until the user returns). Prevents the stale "Preparing..."
+ // loader showing when the user reopens the popup.
+ if (
+ changes.recording.oldValue === true &&
+ changes.recording.newValue === false
+ ) {
+ setContentState((prev) =>
+ prev.finalizingRecording ||
+ prev.preparingRecording ||
+ prev.pendingRecording ||
+ prev.restartingRecording
+ ? {
+ ...prev,
+ finalizingRecording: false,
+ preparingRecording: false,
+ pendingRecording: false,
+ restartingRecording: false,
+ }
+ : prev,
+ );
+ }
+ }
+ // Same clear when storage.restarting flips false (BG side
+ // wraps up the restart flow). Defensive backstop.
+ if (
+ changes.restarting &&
+ changes.restarting.oldValue === true &&
+ changes.restarting.newValue === false
+ ) {
+ setContentState((prev) =>
+ prev.restartingRecording
+ ? { ...prev, restartingRecording: false }
+ : prev,
+ );
+ }
+ if (changes.paused) {
+ pausedRef.current = Boolean(changes.paused.newValue);
+ }
+ if (changes.pausedAt) {
+ pausedAtRef.current = changes.pausedAt.newValue ?? null;
+ }
+ if (changes.totalPausedMs) {
+ totalPausedMsRef.current = changes.totalPausedMs.newValue ?? 0;
+ }
if (changes.recordingBeepTabId) {
recordingBeepTabIdRef.current =
changes.recordingBeepTabId.newValue ?? null;
@@ -1541,6 +1784,51 @@ const ContentState = (props) => {
cameraActive: Boolean(changes.cameraActive.newValue),
}));
}
+ // sandboxTab appearing means BG opened the editor tab and the
+ // finalize handoff is done; tear down recording UI (toolbar,
+ // camera, drawing/blur/cursor, preparing overlay). Storage-
+ // driven so it fires even on a non-recording tab. Only triggers
+ // for single-scene stop; multi-scene reuses the editor tab and
+ // cleans up via reopen-popup-multi.
+ const sandboxTabAppeared =
+ changes.sandboxTab && changes.sandboxTab.newValue != null;
+ if (sandboxTabAppeared) {
+ const wasMulti = contentStateRef.current?.multiMode === true;
+ setContentState((prev) =>
+ prev.finalizingRecording || prev.recording
+ ? {
+ ...prev,
+ finalizingRecording: false,
+ preparingRecording: false,
+ pendingRecording: false,
+ recording: false,
+ paused: false,
+ time: 0,
+ timer: 0,
+ timeWarning: false,
+ tabCaptureFrame: false,
+ pipEnded: false,
+ showExtension: wasMulti ? true : false,
+ showPopup: wasMulti ? true : true,
+ // Preserve tool state in multi so the user's drawing
+ // setup carries into the next scene; single-mode clears.
+ drawingMode: wasMulti ? prev.drawingMode : false,
+ blurMode: wasMulti ? prev.blurMode : false,
+ toolbarMode: wasMulti ? prev.toolbarMode : "",
+ cursorMode: wasMulti ? prev.cursorMode : "none",
+ cursorEffects: wasMulti ? prev.cursorEffects : [],
+ cameraActive: false,
+ }
+ : prev,
+ );
+ setTimer(0);
+ // Match the old stop handler's DOM cleanup; pre-clear blur
+ // overlays in case they survived the React tree teardown.
+ try {
+ const elements = document.querySelectorAll(".screenity-blur");
+ elements.forEach((el) => el.classList.remove("screenity-blur"));
+ } catch {}
+ }
if (changes.recording) {
const isRecording = Boolean(changes.recording.newValue);
// Read recordingType from this batch; the React ref may lag.
@@ -1590,17 +1878,42 @@ const ContentState = (props) => {
isRecording &&
!contentStateRef.current?.isCountdownVisible &&
!contentStateRef.current?.countdownActive;
- setContentState((prev) => ({
- ...prev,
- recording: isRecording,
- ...(shouldHideCountdown
- ? {
- countdownActive: false,
- isCountdownVisible: false,
- }
- : {}),
- }));
- shouldUpdateTimer = true;
+ // BG flips recording=false ~100ms after stop is clicked. If
+ // we're locally in the finalizing window (user pressed stop,
+ // editor not yet open), suppress the flip; otherwise the
+ // toolbar drops the recording surface and looks idle while
+ // the editor is still being prepared. sandboxTab → newValue
+ // is what clears the finalizing state cleanly.
+ const isLocallyFinalizing =
+ !isRecording &&
+ contentStateRef.current?.finalizingRecording === true;
+ if (isLocallyFinalizing) {
+ // Keep the toolbar-facing `recording` flag true so its
+ // Recording UI persists during finalize, but expose the
+ // BG-acknowledged state via encoderActive so the post-stop
+ // loader can gate on it. Encoder can keep capturing many
+ // seconds after stop on contended Chrome; without the gate
+ // the loader bleeds into the recorded video.
+ setContentState((prev) =>
+ prev.encoderActive !== false
+ ? { ...prev, encoderActive: false }
+ : prev,
+ );
+ shouldUpdateTimer = false;
+ } else {
+ setContentState((prev) => ({
+ ...prev,
+ recording: isRecording,
+ encoderActive: isRecording,
+ ...(shouldHideCountdown
+ ? {
+ countdownActive: false,
+ isCountdownVisible: false,
+ }
+ : {}),
+ }));
+ shouldUpdateTimer = true;
+ }
}
if (changes.cursorEffects) {
const nextEffects = normalizeCursorEffects(
@@ -1672,11 +1985,12 @@ const ContentState = (props) => {
setupHandlers();
}, []);
- useEffect(() => {
- chrome.storage.local.set({
- pendingRecording: contentState.pendingRecording,
- });
- }, [contentState.pendingRecording]);
+ // Storage is one-way (storage → contentState via
+ // chrome.storage.onChanged). Don't mirror back via useEffect:
+ // startStreaming's cleanup branches setContentState false
+ // transiently, and a reverse-sync useEffect would propagate
+ // those false values to storage and flicker the popup button.
+ // Each cleanup branch in startStreaming writes storage itself.
useEffect(() => {
if (!contentState.qualityValue) {
diff --git a/src/pages/Content/context/messaging/handlers.js b/src/pages/Content/context/messaging/handlers.js
index 38c1726..09a0534 100644
--- a/src/pages/Content/context/messaging/handlers.js
+++ b/src/pages/Content/context/messaging/handlers.js
@@ -9,8 +9,6 @@ import { checkAuthStatus } from "../utils/checkAuthStatus";
import { traceStep, setStartFlowOutcome } from "../../../utils/startFlowTrace";
import { perfMark } from "../../../utils/perfMarks";
import { triggerSupportDownload } from "../../../utils/triggerSupportDownload";
-import JSZip from "jszip";
-
const CLOUD_FEATURES_ENABLED =
process.env.SCREENITY_ENABLE_CLOUD_FEATURES === "true";
@@ -434,12 +432,35 @@ export const setupHandlers = () => {
// ignore external pushes to avoid jitter/skips.
});
- registerMessage("toggle-popup", () => {
+ registerMessage("toggle-popup", async () => {
+ // Reconcile stale recording-flow state against storage before
+ // showing the popup. The tab may have been suspended during a
+ // recording handoff and missed the storage events that clear
+ // finalizingRecording / pendingRecording locally; without this,
+ // the post-stop loader briefly renders on reopen.
+ let storageReconcile = null;
+ try {
+ const snap = await chrome.storage.local.get([
+ "recording",
+ "restarting",
+ ]);
+ if (!snap.recording && !snap.restarting) {
+ storageReconcile = {
+ finalizingRecording: false,
+ preparingRecording: false,
+ pendingRecording: false,
+ restartingRecording: false,
+ recording: false,
+ };
+ chrome.storage.local.set({ pendingRecording: false }).catch(() => {});
+ }
+ } catch {}
setContentState((prev) => ({
...prev,
showExtension: !prev.showExtension,
hasOpenedBefore: true,
showPopup: true,
+ ...(storageReconcile || {}),
}));
setTimer(0);
updateFromStorage();
@@ -472,6 +493,12 @@ export const setupHandlers = () => {
countdownActive: true,
isCountdownVisible: true,
countdownCancelled: false,
+ // Latching gate: once the countdown is up, the pre-countdown
+ // loader can never re-show in this session (even during the
+ // brief countdown-end → recording=true storage flip window).
+ // Otherwise the loader bleeds into the captured frame for
+ // region/desktop captures where the stream is already live.
+ countdownEverShown: true,
}));
chrome.runtime.sendMessage({ type: "diag-countdown-started" }).catch(() => {});
} else {
@@ -899,16 +926,32 @@ export const setupHandlers = () => {
fastRecorder: fastRecorderData,
};
- const zip = new JSZip();
- zip.file("troubleshooting.json", JSON.stringify(data));
- const blob = await zip.generateAsync({ type: "blob" });
+ // Hand the zip step off to BG (see Background `make-zip` handler).
+ // No JSZip in content; content collects fields, BG runs JSZip,
+ // content downloads the resulting ArrayBuffer.
+ const filename = "screenity-troubleshooting.zip";
+ const resp = await chrome.runtime.sendMessage({
+ type: "make-zip",
+ files: { "troubleshooting.json": JSON.stringify(data) },
+ filename,
+ });
+ if (!resp?.ok || typeof resp.base64 !== "string") {
+ console.warn("[Screenity] troubleshooting zip failed:", resp?.error);
+ return;
+ }
+ const bin = atob(resp.base64);
+ const bytes = new Uint8Array(bin.length);
+ for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
+ const blob = new Blob([bytes], { type: "application/zip" });
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
- a.download = "screenity-troubleshooting.zip";
+ a.download = filename;
+ document.body.appendChild(a);
a.click();
- window.URL.revokeObjectURL(url);
+ a.remove();
+ setTimeout(() => window.URL.revokeObjectURL(url), 1000);
chrome.runtime.sendMessage({ type: "indexed-db-download" });
};
@@ -970,21 +1013,76 @@ export const setupHandlers = () => {
}));
});
- registerMessage("reopen-popup-multi", (message) => {
+ registerMessage("reopen-popup-multi", async (message) => {
+ // Read multi-state from storage before setContentState so the
+ // popup renders with multiMode/multiSceneCount correct on first
+ // paint. The old fire-and-forget updateFromStorage() ran AFTER
+ // setContentState, which raced and could leave the popup
+ // showing the "Multi recording" switch instead of "Done" (stale
+ // for hundreds of ms, or persistent across tabs).
+ let storedMulti = {};
+ try {
+ storedMulti = await chrome.storage.local.get([
+ "multiMode",
+ "multiSceneCount",
+ "multiProjectId",
+ "multiLastSceneId",
+ "projectId",
+ ]);
+ } catch {}
+ // Multi-scene stop choreography. Scene-create is already done
+ // server-side by now. Order: clear loader + recording state
+ // (including finalizingRecording, or the loader sticks until the
+ // 30s watchdog), toast immediately, popup ~700ms later so the
+ // toast settles first. Read multi state from storage; contentState
+ // can be stale on a tab that didn't run the recording.
+ const isMulti = Boolean(storedMulti.multiMode);
setContentState((prev) => ({
...prev,
showExtension: true,
showPopup: true,
+ finalizingRecording: false,
preparingRecording: false,
+ pendingRecording: false,
+ recording: false,
+ paused: false,
+ time: 0,
+ timer: 0,
+ timeWarning: false,
+ tabCaptureFrame: false,
+ pipEnded: false,
+ // Seed multi-mode + scene count + project id from storage. This
+ // is the field set that gates the popup's "Done" button (uses
+ // multiMode + multiSceneCount > 0); if either is stale, the
+ // user sees the "Multi recording" switch instead of "Done".
+ multiMode: isMulti,
+ multiSceneCount: storedMulti.multiSceneCount || 0,
+ multiProjectId: storedMulti.multiProjectId || null,
+ multiLastSceneId: storedMulti.multiLastSceneId || null,
+ projectId: storedMulti.projectId || prev.projectId,
+ drawingMode: isMulti ? prev.drawingMode : false,
+ blurMode: isMulti ? prev.blurMode : false,
+ toolbarMode: isMulti ? prev.toolbarMode : "",
+ cursorMode: isMulti ? prev.cursorMode : "none",
+ cursorEffects: isMulti ? prev.cursorEffects : [],
+ cameraActive: false,
}));
+ setTimer(0);
+ try {
+ const elements = document.querySelectorAll(".screenity-blur");
+ elements.forEach((el) => el.classList.remove("screenity-blur"));
+ } catch {}
updateFromStorage(false, message.senderId);
- setTimeout(() => {
- const state = getState();
- if (state.openToast) {
- state.openToast(chrome.i18n.getMessage("addedToMultiToast"), () => {});
- }
- }, 1000);
+ // Toast on top; auto-dismisses in 5s.
+ const state = getState();
+ if (state.openToast) {
+ state.openToast(
+ chrome.i18n.getMessage("addedToMultiToast"),
+ () => {},
+ 5000,
+ );
+ }
});
registerMessage("open-popup-project", (message) => {
@@ -1003,9 +1101,11 @@ export const setupHandlers = () => {
setTimeout(() => {
const state = getState();
if (state.openToast) {
+ // Explicit 5s lifetime; see addedToMultiToast above.
state.openToast(
chrome.i18n.getMessage("readyRecordSceneToast"),
() => {},
+ 5000,
);
}
}, 1000);
@@ -1116,8 +1216,14 @@ export const setupHandlers = () => {
}
});
registerMessage("update-project-loading", (message, sender) => {
+ // Editor listeners gate on projectId; drop it and the handoff
+ // loader never surfaces.
window.postMessage(
- { source: "update-project-loading", multiMode: message.multiMode },
+ {
+ source: "update-project-loading",
+ multiMode: message.multiMode,
+ projectId: message.projectId || null,
+ },
"*",
);
diff --git a/src/pages/Content/countdown/Countdown.jsx b/src/pages/Content/countdown/Countdown.jsx
index e231ace..669f745 100644
--- a/src/pages/Content/countdown/Countdown.jsx
+++ b/src/pages/Content/countdown/Countdown.jsx
@@ -13,8 +13,11 @@ const Countdown = () => {
const [isTransforming, setIsTransforming] = useState(false);
const [isRotating, setIsRotating] = useState(false);
- const END_HOLD_MS = 1000;
- const POST_HIDE_START_DELAY_MS = 150;
+ // Post-zero hold so "GO" registers and BG can start the encoder
+ // before the overlay vanishes. 250ms felt snappy but exposed dead
+ // time; 600ms is the sweet spot.
+ const END_HOLD_MS = 600;
+ const POST_HIDE_START_DELAY_MS = 100;
const HIDE_AFTER_END_MS = 0;
const intervalRef = useRef(null);
diff --git a/src/pages/Content/index.jsx b/src/pages/Content/index.jsx
index 56d7a86..b29b62a 100644
--- a/src/pages/Content/index.jsx
+++ b/src/pages/Content/index.jsx
@@ -1,11 +1,13 @@
+// Must be the first import; sets __webpack_public_path__ so dynamic
+// chunks resolve against the extension origin. See publicPath.js.
+import "./publicPath";
+
import React from "react";
import { createRoot } from "react-dom/client";
import Content from "./Content";
-// Idempotency guard: content script is injected via manifest AND re-injected
-// by executeScripts() from onInstalled (every MV3 session start). Without
-// this, both mounts orphan listeners and double-fire side effects (e.g.
-// double start beeps).
+// Idempotency: content script is injected via manifest AND by
+// executeScripts() on session start; both mounts would double-fire.
if (window.__screenityContentBootstrapped) {
} else {
window.__screenityContentBootstrapped = true;
diff --git a/src/pages/Content/popup/layout/RecordingTab.jsx b/src/pages/Content/popup/layout/RecordingTab.jsx
index 023f2ef..5bbb1ff 100644
--- a/src/pages/Content/popup/layout/RecordingTab.jsx
+++ b/src/pages/Content/popup/layout/RecordingTab.jsx
@@ -27,14 +27,15 @@ const RecordingTab = (props) => {
const [tabRecordingDisabled, setTabRecordingDisabled] = useState(false);
const [showModalSoon, setShowModalSoon] = useState(false); // 👈 NEW
+ // On pages that can't do tab/region capture (chrome://, app pages),
+ // swap the visible selection to "screen" but don't persist; the
+ // user's stored preference rehydrates on the next mount elsewhere.
useEffect(() => {
if (tabRecordingDisabled && contentState.recordingType === "region") {
setContentState((prev) => ({
...prev,
recordingType: "screen",
}));
- chrome.storage.local.set({ recordingType: "screen" });
-
contentState.openToast?.(
chrome.i18n.getMessage("tabRecordingDisabledToast"),
4000
@@ -53,8 +54,7 @@ const RecordingTab = (props) => {
...prev,
recordingType: "screen",
}));
- chrome.storage.local.set({ recordingType: "screen" });
-
+ // Same rationale as above; no storage write, just content-state.
contentState.openToast?.(
chrome.i18n.getMessage("tabRecordingDisabledToast"),
4000
diff --git a/src/pages/Content/popup/layout/RecordingType.jsx b/src/pages/Content/popup/layout/RecordingType.jsx
index c22ecbd..5eca760 100644
--- a/src/pages/Content/popup/layout/RecordingType.jsx
+++ b/src/pages/Content/popup/layout/RecordingType.jsx
@@ -286,6 +286,10 @@ const RecordingType = (props) => {
)}
{contentState.isLoggedIn &&
!contentState.recordingToScene &&
+ // Instant mode bakes the whole recording into one non-editable
+ // video, incompatible with multi-scene composition. Hide the
+ // toggle while multi is on.
+ !contentState.multiMode &&
CLOUD_FEATURES_ENABLED && (
<>
<div className="popup-content-divider"></div>
diff --git a/src/pages/Content/popup/layout/SettingsMenu.jsx b/src/pages/Content/popup/layout/SettingsMenu.jsx
index c56df23..5fb91be 100644
--- a/src/pages/Content/popup/layout/SettingsMenu.jsx
+++ b/src/pages/Content/popup/layout/SettingsMenu.jsx
@@ -88,8 +88,10 @@ const SettingsMenu = (props) => {
const a = document.createElement("a");
a.href = url;
a.download = filename;
+ document.body.appendChild(a);
a.click();
- window.URL.revokeObjectURL(url);
+ a.remove();
+ setTimeout(() => window.URL.revokeObjectURL(url), 1000);
} catch (err) {
console.error("[Screenity] Troubleshooting export failed:", err);
}
diff --git a/src/pages/Content/publicPath.js b/src/pages/Content/publicPath.js
new file mode 100644
index 0000000..dd3c87d
--- /dev/null
+++ b/src/pages/Content/publicPath.js
@@ -0,0 +1,8 @@
+// In a content script there's no document.currentScript, so webpack
+// falls back to window.location.origin (the host page) when resolving
+// chunk URLs. Chrome then blocks the cross-origin chunk fetch and
+// every lazy import throws ChunkLoadError. Point it at the extension
+// origin instead. Lives in its own file because imports are hoisted;
+// must run before any module that touches webpack's runtime.
+// eslint-disable-next-line no-undef, camelcase
+__webpack_public_path__ = chrome.runtime.getURL("");
diff --git a/src/pages/Content/shortcuts/Shortcuts.jsx b/src/pages/Content/shortcuts/Shortcuts.jsx
index 7577fb1..81c600f 100644
--- a/src/pages/Content/shortcuts/Shortcuts.jsx
+++ b/src/pages/Content/shortcuts/Shortcuts.jsx
@@ -1,6 +1,8 @@
import React, { useEffect, useContext, useRef } from "react";
import { contentStateContext } from "../context/ContentState";
+// Static import; see Wrapper.jsx for why content scripts can't
+// dynamic-import on strict-CSP pages.
import { undoCanvas, redoCanvas, saveCanvas } from "../canvas/modules/History";
const Shortcuts = ({ shortcuts }) => {
diff --git a/src/pages/Content/toolbar/layout/ToolbarWrap.jsx b/src/pages/Content/toolbar/layout/ToolbarWrap.jsx
index 15f8b4f..76eef84 100644
--- a/src/pages/Content/toolbar/layout/ToolbarWrap.jsx
+++ b/src/pages/Content/toolbar/layout/ToolbarWrap.jsx
@@ -9,6 +9,11 @@ import * as Toolbar from "@radix-ui/react-toolbar";
import { Rnd } from "react-rnd";
+// Drawing toolbar; was previously React.lazy'd to keep fabric out of
+// the content-script hot bundle, but lazy chunks can't be loaded from
+// a content script on strict-CSP pages (host's script-src blocks
+// chrome-extension:// <script> injection even with
+// web_accessible_resources). Static import is the only working option.
import DrawingToolbar from "./DrawingToolbar";
import CursorToolbar from "./CursorToolbar";
import BlurToolbar from "./BlurToolbar";
@@ -476,7 +481,7 @@ const ToolbarWrap = () => {
<ToolTrigger
type="button"
content={chrome.i18n.getMessage("finishRecordingTooltip")}
- disabled={!contentState.recording}
+ disabled={!contentState.recording || contentState.finalizingRecording}
onClick={() => {
contentState.stopRecording();
}}
@@ -494,7 +499,7 @@ const ToolbarWrap = () => {
<ToolTrigger
type="button"
content={chrome.i18n.getMessage("restartRecordingTooltip")}
- disabled={!contentState.recording}
+ disabled={!contentState.recording || contentState.finalizingRecording}
onClick={() => {
contentState.tryRestartRecording();
}}
@@ -505,7 +510,7 @@ const ToolbarWrap = () => {
<ToolTrigger
type="button"
content={chrome.i18n.getMessage("pauseRecordingTooltip")}
- disabled={!contentState.recording}
+ disabled={!contentState.recording || contentState.finalizingRecording}
onClick={() => {
contentState.pauseRecording();
}}
@@ -518,7 +523,7 @@ const ToolbarWrap = () => {
type="button"
resume
content={chrome.i18n.getMessage("resumeRecordingTooltip")}
- disabled={!contentState.recording}
+ disabled={!contentState.recording || contentState.finalizingRecording}
onClick={() => {
contentState.resumeRecording();
}}
@@ -529,7 +534,7 @@ const ToolbarWrap = () => {
<ToolTrigger
type="button"
content={chrome.i18n.getMessage("cancelRecordingTooltip")}
- disabled={!contentState.recording}
+ disabled={!contentState.recording || contentState.finalizingRecording}
onClick={() => {
if (contentState.tryDismissRecording !== undefined) {
contentState.tryDismissRecording();
diff --git a/src/pages/EditorWebCodecs/Sandbox.jsx b/src/pages/EditorWebCodecs/Sandbox.jsx
index 947d149..3bc9b7a 100644
--- a/src/pages/EditorWebCodecs/Sandbox.jsx
+++ b/src/pages/EditorWebCodecs/Sandbox.jsx
@@ -1,16 +1,21 @@
import React, { useEffect, useRef } from "react";
-import addAudioToVideo from "./utils/addAudioToVideo";
-import convertWebmToMp4 from "./utils/convertWebmToMp4";
-import cropVideo from "./utils/cropVideo";
-import cutVideo from "./utils/cutVideo";
-import muteVideo from "./utils/muteVideo";
-import reencodeVideo from "./utils/reencodeVideo";
-import toGIF from "./utils/toGIF";
-import getFrame from "./utils/getFrame";
-import hasAudio from "./utils/hasAudio";
-import convertMp4ToWebm from "./utils/convertMp4ToWebm";
-import blobToArrayBuffer from "./utils/blobToArrayBuffer";
+// Lazy-load each video op so editorwebcodecs.html mounts without
+// pulling the ~630KB mediabunny chunk until the user invokes one.
+const lazyUtil = (importFn) =>
+ (...args) =>
+ importFn().then((m) => m.default(...args));
+const addAudioToVideo = lazyUtil(() => import("./utils/addAudioToVideo"));
+const convertWebmToMp4 = lazyUtil(() => import("./utils/convertWebmToMp4"));
+const cropVideo = lazyUtil(() => import("./utils/cropVideo"));
+const cutVideo = lazyUtil(() => import("./utils/cutVideo"));
+const muteVideo = lazyUtil(() => import("./utils/muteVideo"));
+const reencodeVideo = lazyUtil(() => import("./utils/reencodeVideo"));
+const toGIF = lazyUtil(() => import("./utils/toGIF"));
+const getFrame = lazyUtil(() => import("./utils/getFrame"));
+const hasAudio = lazyUtil(() => import("./utils/hasAudio"));
+const convertMp4ToWebm = lazyUtil(() => import("./utils/convertMp4ToWebm"));
+const blobToArrayBuffer = lazyUtil(() => import("./utils/blobToArrayBuffer"));
const Sandbox = () => {
const iframeRef = useRef(null);
@@ -314,6 +319,29 @@ const Sandbox = () => {
return () => window.removeEventListener("message", handler);
}, []);
+ // Bridge editor-force-close from BG to the sandboxed iframe via
+ // postMessage; sandbox.html has no chrome.runtime access. Also
+ // clear the parent's beforeunload if set.
+ useEffect(() => {
+ const onRuntimeMessage = (message, _sender, sendResponse) => {
+ if (message?.type !== "editor-force-close") return;
+ try {
+ window.onbeforeunload = null;
+ } catch {}
+ try {
+ iframeRef.current?.contentWindow?.postMessage(
+ { type: "editor-force-close" },
+ "*",
+ );
+ } catch {}
+ try {
+ sendResponse?.({ ok: true });
+ } catch {}
+ };
+ chrome.runtime.onMessage.addListener(onRuntimeMessage);
+ return () => chrome.runtime.onMessage.removeListener(onRuntimeMessage);
+ }, []);
+
useEffect(() => {
const storageHandler = (changes, areaName) => {
if (areaName !== "local") return;
diff --git a/src/pages/Permissions/Permissions.jsx b/src/pages/Permissions/Permissions.jsx
index b0f6c9c..e494ef1 100644
--- a/src/pages/Permissions/Permissions.jsx
+++ b/src/pages/Permissions/Permissions.jsx
@@ -44,7 +44,8 @@ const Recorder = () => {
checkPermissions();
};
- // If the permissions are granted, enumerate devices
+ // If `Permissions.query()` reports either granted, take the fast
+ // path and enumerate.
if (
cameraPermission.state === "granted" ||
microphonePermission.state === "granted"
@@ -53,17 +54,36 @@ const Recorder = () => {
cameraPermission.state === "granted",
microphonePermission.state === "granted"
);
- } else {
- // Post message to parent window
+ return;
+ }
+
+ // permissions.query() asks the iframe's own origin and reports
+ // "prompt"/"denied" even when getUserMedia would succeed via
+ // the parent's `allow="camera *; microphone *"`. Probe directly.
+ try {
+ const probeStream = await navigator.mediaDevices.getUserMedia({
+ audio: true,
+ video: true,
+ });
+ // It worked; clean up the probe tracks immediately. The
+ // subsequent enumerateDevices() call will request its own
+ // stream when it actually needs the data; this probe was just
+ // a permission test.
+ probeStream.getTracks().forEach((t) => t.stop());
+ enumerateDevices(true, true);
+ return;
+ } catch (probeErr) {
+ // Real permission failure (NotAllowedError, NotFoundError if
+ // no devices, NotReadableError if device in use). Surface
+ // the modal.
window.parent.postMessage(
{
type: "screenity-permissions",
success: false,
- error: err.name,
+ error: probeErr?.name || "unknown",
},
"*"
);
- // sendResponse({ success: false, error: err.name });
}
} catch (err) {
enumerateDevices();
diff --git a/src/pages/Recorder/Recorder.jsx b/src/pages/Recorder/Recorder.jsx
index 28eb5c3..29cb521 100644
--- a/src/pages/Recorder/Recorder.jsx
+++ b/src/pages/Recorder/Recorder.jsx
@@ -9,6 +9,10 @@ import {
preloadWebCodecsModules,
} from "./webcodecs/WebCodecsRecorder";
import { startPrewarm, stopPrewarm } from "./streamWarmup";
+import {
+ startEncoderPrewarm,
+ closeActiveEncoderPrewarm,
+} from "./encoderPrewarm";
import { getUserMediaWithFallback } from "../utils/mediaDeviceFallback";
import { IS_OFFSCREEN_HOST } from "../utils/recordingHost";
import { chooseWriter } from "./recorderStorage/chooseWriter";
@@ -256,7 +260,7 @@ const Recorder = () => {
const streamingDataRetryTimer = useRef(null);
// Apply streaming-data once. Shared by the `get-streaming-data` pull
- // response and the `streaming-data` push message — whichever arrives
+ // response and the `streaming-data` push message; whichever arrives
// first wins; the rest is deduped via streamingDataReceivedAt.
const applyStreamingData = (dataStr) => {
if (streamingDataReceivedAt.current != null) {
@@ -279,11 +283,9 @@ const Recorder = () => {
}
};
- // Pull get-streaming-data from the SW. The SW answers the pull *directly*
- // with the payload (response.data), so delivery does not depend on the
- // tab's onMessage listener being registered yet, nor on the SW's
- // separate push landing in a timing window. An initial sendMessage
- // during SW wake-up can fail with "Receiving end does not exist"; retry.
+ // Pull get-streaming-data; the SW answers directly so we don't
+ // depend on a push landing in a timing window. Retry the initial
+ // call which can fail during SW wake-up.
const requestStreamingDataWithRetry = (attempt = 0) => {
if (streamingDataReceivedAt.current != null) return;
const maxAttempts = 6;
@@ -315,7 +317,7 @@ const Recorder = () => {
retryIfNeeded();
return;
}
- // SW answered the pull directly with the payload — robust path.
+ // SW answered the pull directly with the payload; robust path.
if (response && response.data) {
slLog("get-streaming-data-response-applied", { attempt });
applyStreamingData(response.data);
@@ -455,11 +457,8 @@ const Recorder = () => {
}
}
- // Tab keep-alive: Chrome throttles/freezes background tabs after ~5 minutes of
- // inactivity. Multi-layered signals stack against the freeze heuristic.
- // recorder.html runs an inline keepalive bootstrap (see Recorder/index.html)
- // so throttling can't kick in before React mounts; we reuse those resources
- // to avoid a duplicate AudioContext or second lock holder.
+ // Reuse the keepalive bootstrap from recorder.html (audio/locks/
+ // mediaSession) instead of duplicating its resources.
const startTabKeepAlive = () => {
if (IS_OFFSCREEN_HOST) return;
@@ -473,6 +472,12 @@ const Recorder = () => {
perfMark("Recorder keepalive.inline-state", {
hasInline: Boolean(inlineKA),
hasAudioCtx: Boolean(inlineKA?.audioCtx),
+ // ctx.state should be "running" for Chrome's audio-playing
+ // heuristic to count this tab. If it's "suspended" we know the
+ // autoplay block is what's letting freeze through.
+ audioCtxState: inlineKA?.audioCtx?.state || null,
+ hasSilentAudio: Boolean(inlineKA?.silentAudio),
+ silentAudioPaused: inlineKA?.silentAudio?.paused ?? null,
hasLock: Boolean(inlineKA?.lockAbort),
hasMediaSession: Boolean(inlineKA?.mediaSession),
inlineStartedAt: inlineKA?.startedAt || null,
@@ -507,10 +512,20 @@ const Recorder = () => {
keepAliveOscillator.current = oscillator;
}
}
+ // Resume if suspended; happens when autoplay policy blocks the
+ // initial start. Without this the oscillator isn't producing
+ // output and Chrome's audio-playing heuristic doesn't count this
+ // tab, leaving it eligible for background-tab freezing.
+ const aCtx = keepAliveAudioCtx.current;
+ if (aCtx && aCtx.state !== "running" && typeof aCtx.resume === "function") {
+ aCtx.resume().catch(() => {});
+ }
} catch (err) {
debugWarn("keepalive: audio layer failed:", err);
audioPath = "error";
}
+ // REVERTED: the silent <audio> looping data-URL caused tab freezes
+ // and browser-wide sluggishness. Tracking is in [feedback_silent_audio_freeze].
endAudio({ path: audioPath });
const endLocks = perfSpan("Recorder keepalive.locks");
@@ -637,11 +652,8 @@ const Recorder = () => {
*/
const startSessionHeartbeat = () => {
if (sessionHeartbeat.current) clearInterval(sessionHeartbeat.current);
- // For WebCodecs: the muxer only emits a single chunk at finalize, so
- // handleChunk can't drive the recording-stall watchdog heartbeat during
- // the recording. Drive it from here instead, writes lastChunkAt every
- // 10s, plus firstChunkAt on the first tick (also overwrites any stale
- // values left from a prior session that hadn't been cleaned up yet).
+ // WebCodecs muxer only emits at finalize, so drive the stall
+ // watchdog heartbeat from here (lastChunkAt every 10s).
let firstHeartbeat = true;
const tick = () => {
if (!isRecording.current) return;
@@ -754,6 +766,16 @@ const Recorder = () => {
}
try {
+ // E2E hook: force chunksStore.setItem to throw QuotaExceededError on
+ // the next save so tests can exercise the in-recorder quota catch.
+ // One-shot; fires on whichever index hits next.
+ const g = /** @type {any} */ (globalThis);
+ if (g.__screenityForceChunkSaveQuotaError && !g.__screenityForceChunkSaveQuotaError_fired) {
+ g.__screenityForceChunkSaveQuotaError_fired = true;
+ const forced = new Error("Forced chunk-save quota error for testing");
+ forced.name = "QuotaExceededError";
+ throw forced;
+ }
await chunksStore.setItem(`chunk_${i}`, {
index: i,
chunk: e.data,
@@ -771,6 +793,12 @@ const Recorder = () => {
}
} catch (err) {
debugError("Failed to save chunk, aborting recording", err);
+ const name = err?.name || err?.errorName || "";
+ const msg = String(err?.message || err || "").toLowerCase();
+ const looksLikeQuotaError =
+ name === "QuotaExceededError" ||
+ msg.includes("quota") ||
+ msg.includes("disk");
if (!lowStorageAbort.current) {
chrome.runtime.sendMessage({
type: "show-toast",
@@ -779,6 +807,7 @@ const Recorder = () => {
});
}
lowStorageAbort.current = true;
+ const stopReason = looksLikeQuotaError ? "low-storage" : "chunk-save-failed";
chrome.storage.local.set({
recording: false,
restarting: false,
@@ -786,8 +815,16 @@ const Recorder = () => {
memoryError: true,
lowStorageAbortAt: Date.now(),
lowStorageAbortChunks: index.current,
+ // Surfaces the distinction between a QuotaExceededError catch and
+ // a generic chunk-save failure so consumers (and tests) can see
+ // which classification fired without intercepting messages.
+ lastRecorderStopReason: stopReason,
+ lastRecorderStopAt: Date.now(),
+ });
+ requestStop(stopReason, {
+ memoryError: true,
+ savedChunks: savedCount.current,
});
- requestStop("chunk-save-failed", { memoryError: true, savedChunks: savedCount.current });
return false;
}
@@ -933,6 +970,9 @@ const Recorder = () => {
startTabKeepAlive();
perfMark("Recorder mount.startTabKeepAlive-done");
} catch (err) {}
+ // No prewarmFastRecorderProbe here: parallel with tabCapture
+ // setup it caused VTDecoder contention. Probe runs in preflight
+ // only; storage cache keeps subsequent recordings instant.
}, []);
// Grace before acting on a recording=false flag flip: a normal stop
@@ -940,11 +980,10 @@ const Recorder = () => {
// this tab, so wait that window out before declaring abandonment.
const ABANDON_GRACE_MS = 1500;
- // The session was torn down (recording flipped false) without this tab
- // being told to stop. Halt capture and discard quietly — the user has
- // already seen the failure, so resurrecting an editor would confuse.
- // Without this the MediaRecorder runs until the tab is closed by hand
- // (the ~8h orphan, support code SCR-AKJJ).
+ // Session torn down without this tab being told to stop. Halt
+ // capture quietly; resurrecting the editor after the user already
+ // saw the failure would confuse. Otherwise MediaRecorder runs
+ // until the tab's manually closed.
const abortAbandonedRecording = (detail) => {
if (abandonHandled.current) return;
abandonHandled.current = true;
@@ -986,7 +1025,7 @@ const Recorder = () => {
} catch {}
});
// dismissRecording aborts the chunk writer, clears timing state and
- // closes the tab — the same quiet teardown path as a user dismiss.
+ // closes the tab; the same quiet teardown path as a user dismiss.
dismissRecording().catch(() => {});
};
@@ -1191,14 +1230,11 @@ const Recorder = () => {
startTabKeepAlive();
- // Fire-and-forget prewarm cancel: awaiting it leaves a gap with no
- // reader on the track, which macOS interprets as "no demand" and puts
- // capture back to sleep. Re-spinning takes 5s under load and the first
- // encoded frame freezes on screen for that long. Not awaiting lets
- // WebCodecsRecorder.start() open its own processor while the prewarm
- // reader is still releasing, so the OS sees continuous demand.
- // keepClear / keepWriter pass the in-flight clear and pre-opened writer
- // to the preflight code below.
+ // Fire-and-forget. Awaiting leaves a no-reader gap on the track,
+ // which macOS reads as "no demand" and puts capture to sleep
+ // (5s re-spin under load, frozen first frame). Not awaiting lets
+ // WebCodecsRecorder.start() open its processor while the prewarm
+ // reader releases, so the OS sees continuous demand.
endPrewarm({ keepClear: true, keepWriter: true });
recordingStartTime.current = Date.now();
@@ -1321,20 +1357,17 @@ const Recorder = () => {
});
endActiveSet();
- // Decide encoder BEFORE opening the writer. The previous code used an
- // optimistic guess (willLikelyUseFast) based on user pref + sticky state
- // only; when the actual probe disagreed (e.g. Linux Chrome with broken
- // VideoEncoder coverage), MediaRecorder ran but bytes had already started
- // flowing into the OPFS writer that was opened against the guess. The
- // sandboxed editor.html then couldn't read OPFS. Backend choice is now
- // a function of shouldUseFast, the ground truth.
- const { useWebCodecsRecorder } = await chrome.storage.local.get([
- "useWebCodecsRecorder",
+ // Decide encoder before opening the writer; the optimistic
+ // guess could disagree with the probe and leave bytes the editor
+ // can't read. Parallel probe + storage reads drop preflight from
+ // ~340ms to <50ms on cache hit.
+ const [userSettingRaw, stickyState, probeResult] = await Promise.all([
+ chrome.storage.local.get(["useWebCodecsRecorder"]),
+ getFastRecorderStickyState(),
+ probeFastRecorderSupport(),
]);
// Default-on: undefined means enabled; only explicit `false` opts out.
- const userSetting = useWebCodecsRecorder === false ? false : true;
- const stickyState = await getFastRecorderStickyState();
- const probeResult = await probeFastRecorderSupport();
+ const userSetting = userSettingRaw.useWebCodecsRecorder === false ? false : true;
const shouldUseFast = shouldUseFastRecorder(
userSetting,
probeResult,
@@ -1574,6 +1607,19 @@ const Recorder = () => {
}
try {
+ // E2E hook: same shape as the MediaRecorder branch's hook so a
+ // single test can exercise both paths' quota classification.
+ // One-shot; fires on whichever chunk hits next.
+ const g = /** @type {any} */ (globalThis);
+ if (
+ g.__screenityForceWebCodecsChunkSaveQuotaError &&
+ !g.__screenityForceWebCodecsChunkSaveQuotaError_fired
+ ) {
+ g.__screenityForceWebCodecsChunkSaveQuotaError_fired = true;
+ const forced = new Error("Forced WebCodecs chunk-save quota for testing");
+ forced.name = "QuotaExceededError";
+ throw forced;
+ }
const i = index.current;
if (chunkWriter.current) {
await chunkWriter.current.write({
@@ -1786,6 +1832,13 @@ const Recorder = () => {
audioBitsPerSecond,
});
+ // Close prewarm before opening the real encoder; some macOS
+ // configs only allow one VTDecoder per process. The OS
+ // service stays warm for several seconds after close.
+ try {
+ await closeActiveEncoderPrewarm();
+ } catch {}
+
recorder.current = new WebCodecsRecorder(liveStream.current, {
width,
height,
@@ -1796,14 +1849,28 @@ const Recorder = () => {
videoEncoderConfig: selectedVideoConfig,
containerKind,
debug: DEBUG_RECORDER,
- onFinalized: async () => {
- debug("WebCodecsRecorder onFinalized()");
+ onFinalized: async (payload) => {
+ perfMark("Recorder.onFinalized.enter");
+ debug("WebCodecsRecorder onFinalized()", payload);
+ if (payload?.swRetry) {
+ try {
+ await chrome.storage.local.set({
+ lastWebCodecsSwRetry: {
+ ...payload.swRetry,
+ at: Date.now(),
+ },
+ });
+ } catch {}
+ }
+ const endDrain = perfSpan("Recorder.onFinalized.waitForDrain");
await waitForDrain();
try {
await webcodecsWriteChain;
} catch {}
+ endDrain();
// Close before validating: OPFS holds an exclusive handle until close.
if (chunkWriter.current) {
+ const endClose = perfSpan("Recorder.onFinalized.chunkWriter.close");
try {
const closeResult = await chunkWriter.current.close();
// Only overwrite chunkBackendRef when close returns one; otherwise
@@ -1832,11 +1899,17 @@ const Recorder = () => {
}
chunkWriter.current = null;
chunkWriterBackend.current = null;
+ endClose();
}
- await updateFreeFinalizeStatus("chunks_ready", 95);
+ // Fire-and-forget; Chrome throttles storage IPC on bg
+ // tabs (~10s) and this is just the finalize progress bar.
+ void updateFreeFinalizeStatus("chunks_ready", 95);
let validation = null;
+ const endValidate = perfSpan("Recorder.onFinalized.validate");
try {
+ const endRebuild = perfSpan("Recorder.onFinalized.rebuildBlob");
const blob = await rebuildBlobFromChunks();
+ endRebuild({ bytes: blob?.size ?? 0 });
validation = await validateFastRecorderOutputBlob(blob, {
minBytes: 64 * 1024,
// Multi-GB demuxer scans on slow disks need ~15s.
@@ -1856,6 +1929,7 @@ const Recorder = () => {
details: { error: String(err) },
};
}
+ endValidate({ ok: !!(validation && validation.ok) });
if (validation && !validation.ok) {
await markFastRecorderFailure("validation-failed", validation);
@@ -1875,7 +1949,7 @@ const Recorder = () => {
fastRecorderValidationFailed: hardFail,
fastRecorderValidation: validation,
});
- await updateFreeFinalizeStatus(
+ void updateFreeFinalizeStatus(
"failed",
100,
validation.reasons || "validation-failed",
@@ -1906,12 +1980,14 @@ const Recorder = () => {
fastRecorderValidation: validation,
});
}
- await chrome.storage.local.set({
+ // Fire-and-forget; editor reads this seconds after tab
+ // boot, awaiting hit Chrome's bg-tab IPC throttle (~10s).
+ chrome.storage.local.set({
lastRecordingBackendRef: chunkBackendRef.current || {
backend: "idb",
},
});
- await updateFreeFinalizeStatus("ready", 100);
+ void updateFreeFinalizeStatus("ready", 100);
if (!sentLast.current) {
sentLast.current = true;
isFinishing.current = false;
@@ -1938,13 +2014,25 @@ const Recorder = () => {
debugError("WebCodecsRecorder error", err);
const errStr = String(err);
const transient = isTransientFastRecorderError(errStr);
- markFastRecorderFailure("webcodecs-error", { error: errStr });
+ // Prefer the recorder's structured failure code (e.g.
+ // webcodecs-zero-frames, webcodecs-no-first-chunk) so the
+ // sticky-disable reason and diagnostics name the real cause
+ // instead of a generic "webcodecs-error".
+ const failureCode =
+ typeof err?.code === "string" && err.code
+ ? err.code
+ : "webcodecs-error";
+ markFastRecorderFailure(failureCode, {
+ error: errStr,
+ detail: err?.detail || null,
+ });
// Transient stream errors don't disable WebCodecs.
const persisted = {
lastWebCodecsFailureAt: Date.now(),
lastWebCodecsFailureCode: transient
? "webcodecs-transient"
- : "webcodecs-error",
+ : failureCode,
+ lastWebCodecsFailureDetail: err?.detail || null,
};
if (!transient) persisted.useWebCodecsRecorder = false;
chrome.storage.local.set(persisted);
@@ -2063,7 +2151,7 @@ const Recorder = () => {
recorder.current = null;
// Abort the OPFS writer opened for the WebCodecs path. The recursive
// startRecording() will pick IDB (userSetting=false now), but it
- // also nulls chunkWriter.current without closing — leaving the OPFS
+ // also nulls chunkWriter.current without closing; leaving the OPFS
// file handle dangling.
if (chunkWriter.current) {
try {
@@ -2699,6 +2787,12 @@ const Recorder = () => {
.catch(() => {});
}
}
+ // Tear down the encoder prewarm too; if endPrewarm runs because
+ // the user cancelled the start flow, the prewarm encoder would
+ // otherwise sit ticking its keep-alive every 800ms.
+ try {
+ await closeActiveEncoderPrewarm();
+ } catch {}
await stopPrewarm(ctrl);
}
@@ -2766,12 +2860,14 @@ const Recorder = () => {
resetGateState();
isFinishing.current = true;
isRecording.current = false;
- await updateFreeFinalizeStatus("stopping", 0);
+ // Don't block stop() on telemetry IPCs; diag showed 9.5s of
+ // sequential awaits under contention, delaying editor-open.
+ void updateFreeFinalizeStatus("stopping", 0);
stopSessionHeartbeat();
stopRecordingTick();
persistSessionState("stopping");
- await setRecordingTimingState({
+ void setRecordingTimingState({
recording: false,
paused: false,
recordingStartTime: null,
@@ -2786,11 +2882,11 @@ const Recorder = () => {
recorder.current instanceof WebCodecsRecorder
) {
debug("Stopping WebCodecsRecorder");
- await updateFreeFinalizeStatus("finalizing", 20);
+ void updateFreeFinalizeStatus("finalizing", 20);
await recorder.current.stop();
} else if (recorder.current instanceof MediaRecorder) {
debug("Stopping MediaRecorder");
- await updateFreeFinalizeStatus("finalizing", 20);
+ void updateFreeFinalizeStatus("finalizing", 20);
try {
recorder.current.requestData();
} catch {}
@@ -3629,6 +3725,39 @@ const Recorder = () => {
beginPrewarm(liveStream.current);
+ // Open a synthetic encoder so the OS service is warm by the time
+ // the real encoder opens. Closed in preflight; failure no-ops.
+ try {
+ const vTrack = liveStream.current?.getVideoTracks?.()[0];
+ const settings = vTrack?.getSettings?.() || {};
+ const w = Number(settings.width) || 0;
+ const h = Number(settings.height) || 0;
+ if (w > 0 && h > 0) {
+ const probeData = await chrome.storage.local.get([
+ "fastRecorderProbe",
+ ]);
+ const probeConfig =
+ probeData?.fastRecorderProbe?.details?.selectedVideoConfig || null;
+ // Prefer the codec the probe selected (matches what WCR will
+ // pick). Fall back to a Main-profile h264 that's broadly
+ // supported by VTDecoder.
+ const codec = probeConfig?.codec || "avc1.4D401F";
+ const bitrate =
+ Number(probeConfig?.bitrate) || 4_000_000;
+ const framerate =
+ Number(settings.frameRate) ||
+ Number(probeConfig?.framerate) ||
+ 30;
+ void startEncoderPrewarm({
+ width: w,
+ height: h,
+ codec,
+ bitrate,
+ framerate,
+ });
+ }
+ } catch {}
+
slLog("warmUp-done");
perfMark("Recorder reset-active-tab.sent");
chrome.runtime.sendMessage({ type: "reset-active-tab" });
@@ -3670,7 +3799,13 @@ const Recorder = () => {
if (data.recordingType === "camera") {
debug("Streaming camera recording");
startStream(data, null, null, permissions, permissions2);
- } else if (!isTab.current) {
+ } else if (
+ // Region is always direct tab capture; never show the
+ // picker. Old !isTab.current check raced isTab=true from the
+ // "loaded" message and could fall through to chooseDesktopMedia.
+ !isTab.current &&
+ data.recordingType !== "region"
+ ) {
let captureTypes = ["screen", "window", "tab", "audio"];
if (tabPreferred.current) {
captureTypes = ["tab", "screen", "window", "audio"];
@@ -3941,7 +4076,7 @@ const Recorder = () => {
recordedTabId.current = null;
}
// streaming-data is pulled on mount (see the mount effect), not
- // here — `loaded` can be lost to the cold-start race and must not
+ // here; `loaded` can be lost to the cold-start race and must not
// be the sole trigger for the pull.
} else if (request.type === "streaming-data") {
// Push path; the pull response usually wins. applyStreamingData
@@ -4225,14 +4360,10 @@ const Recorder = () => {
};
}, []);
- // Pull streaming-data as soon as the recorder is mounted — do not wait
- // for the SW's `loaded` push, which is fire-and-forget at tab
- // status:complete and is routinely lost to the cold-start race (the
- // SW→tab handoff failure, support codes SCR-AKJJ / SCR-8LC2). The pull
- // is answered directly in its own sendMessage response, so it does not
- // depend on a push landing in a timing window. The 15s watchdog is
- // armed here too, independent of `loaded`, so a lost `loaded` can't
- // leave the recorder hanging without an error path.
+ // Pull streaming-data on mount instead of waiting for the SW's
+ // `loaded` push, which is fire-and-forget and routinely lost to
+ // cold-start. 15s watchdog independent of `loaded` so a lost push
+ // can't hang the recorder.
useEffect(() => {
loadedAt.current = Date.now();
requestStreamingDataWithRetry();
diff --git a/src/pages/Recorder/encoderPrewarm.js b/src/pages/Recorder/encoderPrewarm.js
new file mode 100644
index 0000000..af6f96b
--- /dev/null
+++ b/src/pages/Recorder/encoderPrewarm.js
@@ -0,0 +1,195 @@
+// Encoder pre-warm. OS encode services (VTDecoderXPCService etc) need
+// a few frames before the HW pipeline locks in; diag showed real
+// recordings' first ~30 frames running at 6-9fps before settling at
+// 30fps. Opens a VideoEncoder with the real config, bursts synthetic
+// frames, closes. Fire-and-forget; failure no-ops.
+
+import { perfMark, perfSpan } from "../utils/perfMarks";
+
+let _activeWarmer = null;
+
+const computeFramerate = (config) => {
+ const fps = Number(config?.framerate);
+ if (!Number.isFinite(fps) || fps <= 0 || fps > 240) return 30;
+ return Math.round(fps);
+};
+
+const synthesizeAndEncode = (encoder, canvas, ctx, index, framerate, keyFrame) => {
+ // Vary content per frame so the encoder exercises the pipeline
+ // instead of emitting empty deltas.
+ const hue = (index * 17) % 360;
+ ctx.fillStyle = `hsl(${hue}, 50%, 35%)`;
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+ ctx.fillStyle = `hsl(${(hue + 180) % 360}, 50%, 60%)`;
+ ctx.fillRect(
+ (index % 8) * (canvas.width / 8),
+ (index % 6) * (canvas.height / 6),
+ canvas.width / 8,
+ canvas.height / 6,
+ );
+ const tsUs = Math.round((index * 1_000_000) / framerate);
+ const frame = new VideoFrame(canvas, {
+ timestamp: tsUs,
+ duration: Math.round(1_000_000 / framerate),
+ });
+ try {
+ encoder.encode(frame, { keyFrame });
+ } finally {
+ frame.close();
+ }
+};
+
+/**
+ * Start a prewarm encoder that opens, encodes a few frames, and
+ * stays open. Returns a handle with .close(). Idempotent: a second
+ * call while one is active returns the active handle.
+ *
+ * @param {object} cfg
+ * @param {number} cfg.width
+ * @param {number} cfg.height
+ * @param {string} cfg.codec e.g. "avc1.64002A"
+ * @param {number} [cfg.bitrate]
+ * @param {number} [cfg.framerate]
+ * @returns {Promise<{close: () => Promise<void>, ms: number, chunks: number, ok: boolean}>}
+ */
+export const startEncoderPrewarm = async (cfg) => {
+ if (_activeWarmer) return _activeWarmer.handle;
+ if (typeof VideoEncoder === "undefined" || typeof VideoFrame === "undefined" || typeof OffscreenCanvas === "undefined") {
+ perfMark("Recorder.encoderPrewarm.skipped", { reason: "no-webcodecs" });
+ return null;
+ }
+ if (!cfg || !cfg.width || !cfg.height || !cfg.codec) {
+ perfMark("Recorder.encoderPrewarm.skipped", { reason: "no-config" });
+ return null;
+ }
+
+ const endStart = perfSpan("Recorder.encoderPrewarm.start", {
+ w: cfg.width,
+ h: cfg.height,
+ codec: cfg.codec,
+ });
+
+ const state = {
+ chunks: 0,
+ encoder: null,
+ canvas: null,
+ ctx: null,
+ closed: false,
+ framerate: computeFramerate(cfg),
+ keepAliveTimer: null,
+ keepAliveIndex: 0,
+ };
+ _activeWarmer = state;
+
+ const handle = {
+ /** Close the prewarm encoder. Safe to call multiple times. */
+ close: async () => {
+ if (state.closed) return;
+ state.closed = true;
+ if (state.keepAliveTimer) {
+ clearInterval(state.keepAliveTimer);
+ state.keepAliveTimer = null;
+ }
+ if (state.encoder) {
+ try {
+ // No flush; the goal was OS-side warmup, not the chunks.
+ state.encoder.close();
+ } catch {}
+ }
+ _activeWarmer = null;
+ perfMark("Recorder.encoderPrewarm.closed", { chunks: state.chunks });
+ },
+ get chunks() {
+ return state.chunks;
+ },
+ get ok() {
+ return state.encoder != null && state.chunks > 0;
+ },
+ };
+ state.handle = handle;
+
+ try {
+ state.encoder = new VideoEncoder({
+ output: () => {
+ state.chunks += 1;
+ },
+ error: (err) => {
+ perfMark("Recorder.encoderPrewarm.encoderError", {
+ msg: String(err?.message || err).slice(0, 120),
+ });
+ },
+ });
+ state.encoder.configure({
+ codec: cfg.codec,
+ width: cfg.width,
+ height: cfg.height,
+ bitrate: Math.max(500_000, Number(cfg.bitrate) || 4_000_000),
+ framerate: state.framerate,
+ bitrateMode: "constant",
+ latencyMode: "realtime",
+ hardwareAcceleration: "prefer-hardware",
+ });
+ state.canvas = new OffscreenCanvas(cfg.width, cfg.height);
+ state.ctx = state.canvas.getContext("2d");
+ if (!state.ctx) {
+ throw new Error("no-canvas-2d-context");
+ }
+
+ // Burst 6 frames (1 keyframe + 5 deltas) then close. No
+ // keep-alive ticker: an 800ms timer on the background tab
+ // triggers Chrome's intensive throttling, which makes storage
+ // IPCs take 11-19s and wedges start. OS encode service stays
+ // warm for a few seconds, covers most countdown windows.
+ const BURST_COUNT = 6;
+ for (let i = 0; i < BURST_COUNT; i++) {
+ if (state.closed) return handle;
+ synthesizeAndEncode(state.encoder, state.canvas, state.ctx, i, state.framerate, i === 0);
+ }
+ // Flush + close fire-and-forget so the caller isn't blocked.
+ try {
+ state.encoder.flush().catch(() => {});
+ } catch {}
+ try {
+ state.encoder.close();
+ } catch {}
+ state.encoder = null;
+ state.closed = true;
+ _activeWarmer = null;
+
+ endStart({ chunks: state.chunks, ok: true });
+ perfMark("Recorder.encoderPrewarm.closed", { chunks: state.chunks });
+ perfMark("Recorder.encoderPrewarm.ready", {
+ framerate: state.framerate,
+ width: cfg.width,
+ height: cfg.height,
+ });
+ } catch (err) {
+ endStart({ ok: false, error: String(err?.message || err).slice(0, 100) });
+ perfMark("Recorder.encoderPrewarm.failed", {
+ msg: String(err?.message || err).slice(0, 120),
+ });
+ if (state.encoder) {
+ try {
+ state.encoder.close();
+ } catch {}
+ state.encoder = null;
+ }
+ _activeWarmer = null;
+ return null;
+ }
+
+ return handle;
+};
+
+/** Returns the active prewarm handle if one exists, else null. */
+export const getActiveEncoderPrewarm = () => _activeWarmer?.handle || null;
+
+/** Synchronously close any active prewarm. Safe at any time. */
+export const closeActiveEncoderPrewarm = async () => {
+ const h = getActiveEncoderPrewarm();
+ if (h) {
+ try {
+ await h.close();
+ } catch {}
+ }
+};
diff --git a/src/pages/Recorder/recorderKeepalive.js b/src/pages/Recorder/recorderKeepalive.js
index da2db01..b7f9ccc 100644
--- a/src/pages/Recorder/recorderKeepalive.js
+++ b/src/pages/Recorder/recorderKeepalive.js
@@ -1,10 +1,9 @@
-// keep-alive bootstrap, loaded from recorder.html before the main bundle.
-// region recordings open the tab with active:false, so chrome throttles
-// JS within ~1s and stretches bundle parse from ~300ms to 3-4.5s.
-// three signals (audio oscillator, navigator.locks, mediaSession) make
-// the tab look active before throttling kicks in.
-// external because MV3 CSP blocks inline <script>. handles stashed on
-// window.__SCREENITY_KEEPALIVE so React-side startTabKeepAlive() reuses.
+// Keep-alive bootstrap. Loaded from recorder.html before the main
+// bundle. Region recordings open the tab with active:false so Chrome
+// throttles JS within ~1s and bundle parse stretches from 300ms to
+// 3-4.5s. Audio oscillator + navigator.locks + mediaSession make the
+// tab look active before throttling. Handles on window.__SCREENITY_KEEPALIVE
+// so React's startTabKeepAlive() reuses them.
(function () {
try {
var KA = (window.__SCREENITY_KEEPALIVE =
@@ -14,8 +13,7 @@
performance.mark("screenity-keepalive-start");
} catch (e) {}
- // Silent ultrasonic sine wave; counts as "playing audio" to Chrome's
- // freeze heuristic (looks at AudioContext.state + node wiring).
+ // Silent ultrasonic sine; counts as "playing audio" to Chrome.
try {
var Ctx = window.AudioContext || window.webkitAudioContext;
if (Ctx && !KA.audioCtx) {
@@ -28,12 +26,18 @@
osc.connect(gain);
gain.connect(ctx.destination);
osc.start();
+ // AudioContext starts suspended without a user gesture; without
+ // resume() the oscillator emits nothing and Chrome doesn't
+ // count it (diag showed 6.5s mid-recording freezes).
+ if (ctx.state !== "running") {
+ ctx.resume().catch(function () {});
+ }
KA.audioCtx = ctx;
KA.oscillator = osc;
}
} catch (e) {}
- // Holding an exclusive lock signals "doing work".
+ // Hold an exclusive lock; signals "doing work" to Chrome.
try {
if (navigator.locks && !KA.lockAbort) {
var ac = new AbortController();
@@ -50,7 +54,6 @@
}
} catch (e) {}
- // Belt + braces with the audio oscillator above.
try {
if (navigator.mediaSession && !KA.mediaSession) {
if (typeof window.MediaMetadata === "function") {
diff --git a/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js b/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js
index c8ee634..e78e5cf 100644
--- a/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js
+++ b/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js
@@ -1,10 +1,7 @@
-// main-thread ChunkWriter delegating to a worker that holds a
-// FileSystemSyncAccessHandle. requestId correlates post/response and
-// awaiting each write is the backpressure.
-// on worker crash / timeout / quota: pending writes reject with a tagged
-// error, writer marks dead, fileName kept so callers can find partial
-// content. recorder stops and opens the editor on the partial; backends
-// don't swap mid-recording (would orphan written chunks).
+// Main-thread ChunkWriter delegating to a worker that holds a
+// FileSystemSyncAccessHandle. On worker crash/timeout/quota, pending
+// writes reject and the writer marks dead; recorder stops and opens
+// the editor on the partial (no mid-recording backend swap).
const WORKER_URL = () =>
chrome.runtime.getURL("recorderopfsworker.bundle.js");
@@ -186,22 +183,38 @@ export class OpfsChunkWriter {
chunkCount: this._chunkCount,
timeoutMs: closeTimeoutMs,
});
+ const tBeforeReq = (typeof performance !== "undefined" ? performance.now() : Date.now());
try {
const resp = await this._request({ type: "close" }, closeTimeoutMs);
+ const tAfterReq = (typeof performance !== "undefined" ? performance.now() : Date.now());
this._byteSize = resp.byteSize ?? this._byteSize;
this._chunkCount = resp.chunkCount ?? this._chunkCount;
this._fileName = resp.fileName || this._fileName;
+ // Surface per-phase timings so a diag can attribute the 10s
+ // close-stall we've seen on macOS: flush vs handle.close vs
+ // worker round-trip overhead.
+ try {
+ const { perfMark } = await import("../../../utils/perfMarks");
+ perfMark("OPFSWriter.close.detail", {
+ byteSize: this._byteSize,
+ chunkCount: this._chunkCount,
+ requestRoundTripMs: Math.round(tAfterReq - tBeforeReq),
+ workerFlushMs: resp.timings?.flushMs ?? null,
+ workerGetSizeMs: resp.timings?.getSizeMs ?? null,
+ workerCloseHandleMs: resp.timings?.closeHandleMs ?? null,
+ });
+ } catch {}
opfsDiag("close-ok", {
byteSize: this._byteSize,
chunkCount: this._chunkCount,
});
} finally {
- // Always set the finalized flag, even if close() timed out or
- // threw. Writing it only on success left the editor's wait-finalize
- // loop spinning 60s on slow disks (AV scan, hung worker).
+ // Fire-and-forget; the storage.set under SW contention added 2s
+ // to close() and delayed the editor open. The editor reads this
+ // key seconds later, so async lands in time.
if (this._fileName) {
try {
- await chrome.storage.local.set({
+ chrome.storage.local.set({
lastRecordingFinalizedFileName: this._fileName,
});
} catch {}
diff --git a/src/pages/Recorder/recorderStorage/opfs/writerWorker.js b/src/pages/Recorder/recorderStorage/opfs/writerWorker.js
index fc0387e..39cdac3 100644
--- a/src/pages/Recorder/recorderStorage/opfs/writerWorker.js
+++ b/src/pages/Recorder/recorderStorage/opfs/writerWorker.js
@@ -71,15 +71,31 @@ const closeFile = () => {
if (!syncHandle) {
return { byteSize: offset, chunkCount, fileName };
}
+ // Granular timing; diag showed close taking ~10s on one user's
+ // macOS recording. Without splitting flush vs close we can't tell
+ // whether the cost is fsync or the handle release itself.
+ const tFlushStart = (typeof performance !== "undefined" ? performance.now() : Date.now());
try {
syncHandle.flush();
} catch {}
+ const tFlushEnd = (typeof performance !== "undefined" ? performance.now() : Date.now());
const byteSize = typeof syncHandle.getSize === "function" ? syncHandle.getSize() : offset;
+ const tGetSizeEnd = (typeof performance !== "undefined" ? performance.now() : Date.now());
syncHandle.close();
+ const tCloseEnd = (typeof performance !== "undefined" ? performance.now() : Date.now());
syncHandle = null;
closed = true;
devLog("close", { byteSize, chunkCount, fileName });
- return { byteSize, chunkCount, fileName };
+ return {
+ byteSize,
+ chunkCount,
+ fileName,
+ timings: {
+ flushMs: Math.round(tFlushEnd - tFlushStart),
+ getSizeMs: Math.round(tGetSizeEnd - tFlushEnd),
+ closeHandleMs: Math.round(tCloseEnd - tGetSizeEnd),
+ },
+ };
};
const abortFile = async () => {
@@ -175,6 +191,7 @@ self.onmessage = (e) => {
byteSize: result.byteSize,
chunkCount: result.chunkCount,
fileName: result.fileName,
+ timings: result.timings,
});
} catch (err) {
post({
diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
index b1e570f..ba4229c 100644
--- a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
@@ -106,6 +106,89 @@ export class WebCodecsRecorder {
this._stopping = false;
this._videoLoopPromise = null;
this._audioLoopPromise = null;
+
+ // Zero-frame recordings must error, not ship a header-only file.
+ // _failureReported dedupes the zero-frame and first-chunk-watchdog
+ // races.
+ this._failureReported = false;
+ this._videoLoopError = null;
+ this._firstChunkSeen = false;
+ this._firstChunkWatchdog = null;
+ this._firstChunkWatchdogMs = Number.isFinite(options.firstChunkWatchdogMs)
+ ? options.firstChunkWatchdogMs
+ : 8000;
+
+ // Mid-stream watchdog: encoder goes silent after producing chunks
+ // (HW reclaim, hang). Triggers graceful finalize on the partial.
+ this._lastChunkAt = null;
+ this._midStreamWatchdog = null;
+ this._midStreamWatchdogMs = Number.isFinite(options.midStreamWatchdogMs)
+ ? options.midStreamWatchdogMs
+ : 15000;
+
+ // Last structured failure code that fired via _reportFailure; passed
+ // to onFinalized so the consumer can distinguish a clean stop, a
+ // mid-stream stall salvage, and other failure shapes.
+ this._lastFailureCode = null;
+
+ // Backpressure: feeding faster than the encoder drains grows the
+ // queue unbounded and chunks stop emitting (28-byte failure).
+ // Past threshold, drop the next frame and force a keyframe.
+ this._encoderMaxQueueSize = Number.isFinite(options.encoderMaxQueueSize)
+ ? options.encoderMaxQueueSize
+ : 4;
+ this._forceNextKeyframe = false;
+ this._droppedForBackpressureCount = 0;
+ // Audio: same pattern, deeper queue. Frames are smaller / more
+ // frequent (~10-25ms vs 33ms video at 30fps).
+ this._audioEncoderMaxQueueSize = Number.isFinite(options.audioEncoderMaxQueueSize)
+ ? options.audioEncoderMaxQueueSize
+ : 10;
+ this._droppedAudioForBackpressureCount = 0;
+ // Peak queue depth; leading indicator for backpressure tuning.
+ // Drops tell us when the encoder failed; this tells us when it
+ // almost did.
+ this._peakVideoEncodeQueueSize = 0;
+ this._peakAudioEncodeQueueSize = 0;
+
+ // Encoder reclaim recovery. Chrome reclaims idle HW slots; we
+ // rebuild with the same config. Capped to surface real failures.
+ this._maxEncoderReclaims = Number.isFinite(options.maxEncoderReclaims)
+ ? options.maxEncoderReclaims
+ : 3;
+ this._encoderReclaimCount = 0;
+ this._audioEncoderReclaimCount = 0;
+ // Active configs preserved for rebuild after reclaim. Updated when
+ // a SW fallback rebuilds the encoder so the next reclaim uses the
+ // config that actually worked, not the originally-attempted HW one.
+ this._activeVideoConfig = null;
+ this._activeVideoCodecLabel = null;
+ this._activeAudioConfig = null;
+ // Handlers preserved so rebuild reuses the closures that wire
+ // output → muxer, first-chunk watchdog, decoderConfig surfacing.
+ this._videoEncoderOutputHandler = null;
+ this._videoEncoderErrorHandler = null;
+ this._audioEncoderOutputHandler = null;
+ this._audioEncoderErrorHandler = null;
+
+ // Visibility-change tracking so _reportFailure can correlate
+ // encoder reclaims with recent backgrounding. Counts only; the
+ // engine doesn't pause/resume on visibility.
+ this._visibilityChangeCount = 0;
+ this._lastVisibilityChangeAt = null;
+ this._lastVisibilityState = null;
+ this._visibilityChangeListener = null;
+
+ // devicechange tracking so _reportFailure captures BT/USB unplugs
+ // and default-mic switches (the common cause of audio sample-rate
+ // mismatches). Engine doesn't rebuild; consumer decides policy.
+ this._deviceChangeCount = 0;
+ this._lastDeviceChangeAt = null;
+ this._deviceChangeListener = null;
+
+ // One-shot: surfaces SPS+PPS bytes once for OPFS persistence so a
+ // tab-kill recovery can reconstruct MP4 without the muxer.
+ this._decoderConfigEmitted = false;
}
async _probeRealResolution() {
@@ -147,6 +230,30 @@ export class WebCodecsRecorder {
this.videoTimestampOffsetUs = null;
this.firstVideoFrame = undefined;
this._stopping = false;
+ this._failureReported = false;
+ this._videoLoopError = null;
+ this._firstChunkSeen = false;
+ this._lastChunkAt = null;
+ this._lastFailureCode = null;
+ this._forceNextKeyframe = false;
+ this._droppedForBackpressureCount = 0;
+ this._droppedAudioForBackpressureCount = 0;
+ this._peakVideoEncodeQueueSize = 0;
+ this._peakAudioEncodeQueueSize = 0;
+ this._encoderReclaimCount = 0;
+ this._audioEncoderReclaimCount = 0;
+ this._activeVideoConfig = null;
+ this._activeVideoCodecLabel = null;
+ this._activeAudioConfig = null;
+ this._visibilityChangeCount = 0;
+ this._lastVisibilityChangeAt = null;
+ this._lastVisibilityState =
+ typeof document !== "undefined" ? document.visibilityState : null;
+ this._deviceChangeCount = 0;
+ this._lastDeviceChangeAt = null;
+ this._attachVisibilityListener();
+ this._attachDeviceChangeListener();
+ this._decoderConfigEmitted = false;
this._videoFrameIndex = 0;
this._videoStartUs = null;
this._frameDurationUs = null;
@@ -366,10 +473,8 @@ export class WebCodecsRecorder {
this._startAlignTimer = null;
if (!this.running) return;
- // E2E hook: simulate a runtime encoder failure before any chunk
- // lands so tests can exercise the in-session swap to
- // MediaRecorder. Resolves start() with success first so the
- // caller treats this as a normal post-start error.
+ // E2E hook: simulate runtime encoder failure for the
+ // in-session MediaRecorder swap test path.
if (globalThis.__screenityForceWebCodecsError) {
if (this._startResolve) {
this._startResolve(true);
@@ -383,6 +488,25 @@ export class WebCodecsRecorder {
return;
}
+ // E2E hook: fire `ended` on the video track mid-recording
+ // (simulates monitor unplug / Stop-sharing click).
+ const endAfterMs = Number(
+ (globalThis).__screenityForceTrackEndAfterMs,
+ );
+ if (
+ this.videoTrack &&
+ Number.isFinite(endAfterMs) &&
+ endAfterMs > 0
+ ) {
+ this._forcedTrackEndTimer = setTimeout(() => {
+ try {
+ this.videoTrack.dispatchEvent(new Event("ended"));
+ } catch (err) {
+ this.warn("[WCR] forced track end dispatch failed", err);
+ }
+ }, endAfterMs);
+ }
+
// Launch loops together so t=0 aligns.
this._videoLoopPromise = this.readVideoLoop();
if (this.audioReader && this.options.enableAudio) {
@@ -390,6 +514,10 @@ export class WebCodecsRecorder {
this._audioLoopPromise = this.readAudioLoop();
}
+ // No-first-chunk watchdog. Surfaces the silent encoder
+ // stall so the caller can swap to MediaRecorder.
+ this._armFirstChunkWatchdog();
+
if (this._startResolve) {
this._startResolve(true);
this._startResolve = null;
@@ -425,6 +553,7 @@ export class WebCodecsRecorder {
async stop() {
this.log("[WCR] stop()", this.frameCount);
+ perfMark("WCR.stop.enter", { frameCount: this.frameCount });
if (this._stopping) {
this.log("[WCR] stop() already in progress");
@@ -442,6 +571,12 @@ export class WebCodecsRecorder {
clearTimeout(this._startAlignTimer);
this._startAlignTimer = null;
}
+ if (this._forcedTrackEndTimer) {
+ clearTimeout(this._forcedTrackEndTimer);
+ this._forcedTrackEndTimer = null;
+ }
+ this._clearFirstChunkWatchdog();
+ this._clearMidStreamStallWatchdog();
// Soft stop: let loops finish current work.
this.running = false;
@@ -463,25 +598,43 @@ export class WebCodecsRecorder {
this.err("[WCR] Error while waiting for loops:", err);
}
- // First flush: drain queued audio/video so audioSamplesWritten is a
- // reliable basis for final track duration.
- try {
- if (this.videoEncoder && this.videoEncoder.state !== "closed") {
- await this.videoEncoder.flush();
- }
- if (this.audioEncoder && this.audioEncoder.state !== "closed") {
- await this.audioEncoder.flush();
+ // Drain queued a/v so audioSamplesWritten is reliable for the
+ // final track duration. 15s cap (webcodecs#723: flush can take
+ // 5-10s, HW glitches hang indefinitely); muxer has its own.
+ const flushBounded = async (encoder, kind) => {
+ if (!encoder || encoder.state === "closed") return;
+ const endSpan = perfSpan(`WCR.stop.${kind}.flush`);
+ const startedAt = performance.now();
+ let timed = null;
+ try {
+ await Promise.race([
+ encoder.flush(),
+ new Promise((_, reject) => {
+ timed = setTimeout(
+ () => reject(new Error(`${kind}-flush-timeout`)),
+ 15000,
+ );
+ }),
+ ]);
+ } finally {
+ if (timed) clearTimeout(timed);
}
+ const ms = Math.round(performance.now() - startedAt);
+ if (kind === "video") this._videoFlushMs = ms;
+ else this._audioFlushMs = ms;
+ endSpan({ ms });
+ };
+ try {
+ await flushBounded(this.videoEncoder, "video");
+ await flushBounded(this.audioEncoder, "audio");
} catch (err) {
this.err("[WCR] flush error:", err);
this.options.onError?.(err);
}
- // Encode trailing hold frames to cover the final audio end. Without
- // these, audio extends past the last video sample and players render a
- // trailing black frame. Multiple frames (not one long-duration frame):
- // encoders don't always preserve VideoFrame.duration on the output
- // chunk, so a single 150ms hold may get written with duration=0.
+ // Trailing hold frames so audio doesn't outlast video and render
+ // a black tail. Multiple short frames, not one long one: encoders
+ // can drop VideoFrame.duration on the output chunk.
if (
this.videoEncoder &&
this.videoEncoder.state !== "closed" &&
@@ -519,7 +672,22 @@ export class WebCodecsRecorder {
hold.close();
this._videoFrameIndex += 1;
}
- await this.videoEncoder.flush();
+ // 5s cap on trailing-hold flush; some Windows drivers never
+ // resolve flush(). Muxer finalize still ships mdat'd chunks.
+ let holdTimed = null;
+ try {
+ await Promise.race([
+ this.videoEncoder.flush(),
+ new Promise((_, reject) => {
+ holdTimed = setTimeout(
+ () => reject(new Error("trailing-hold-flush-timeout")),
+ 5000,
+ );
+ }),
+ ]);
+ } finally {
+ if (holdTimed) clearTimeout(holdTimed);
+ }
this.log("[WCR] trailing hold frames encoded", {
holdStartUs,
audioEndUs,
@@ -530,12 +698,12 @@ export class WebCodecsRecorder {
}
}
- // Finalize with hard timeout: abrupt stream end (Chrome "Stop sharing"
- // tearing down the video track) can hang output.finalize(). Fragmented
- // MP4 + StreamTarget means most of the file is already in chunksStore;
- // a hung finalize costs at most ~1s. flushPending() preserves what we have.
+ // Hard timeout on finalize: abrupt stream end can hang it. Most
+ // of the file is already in chunksStore (fragmented MP4);
+ // flushPending() preserves the rest.
let muxerFinalizeOk = false;
if (this.muxer) {
+ const endFinalize = perfSpan("WCR.stop.muxer.finalize");
try {
await Promise.race([
this.muxer.finalize().then(() => {
@@ -548,7 +716,9 @@ export class WebCodecsRecorder {
),
),
]);
+ endFinalize({ ok: true });
} catch (err) {
+ endFinalize({ ok: false, err: String(err?.message || err).slice(0, 80) });
this.err("[WCR] muxer.finalize:", err);
this.options.onError?.(err);
try {
@@ -561,13 +731,40 @@ export class WebCodecsRecorder {
// stuck-at-90% bug on stream-end. Recorder layer decides next steps
// (validate chunks, mark failed, send video-ready).
if (this.options.onFinalized) {
+ const endOnFinalized = perfSpan("WCR.stop.onFinalized.callback");
try {
- await this.options.onFinalized({ muxerFinalizeOk });
+ // Integrity signal: muxerFinalizeOk says whether moov was
+ // written, framesEncoded confirms real content,
+ // lastFailureCode names the cause on salvaged partials.
+ await this.options.onFinalized({
+ muxerFinalizeOk,
+ framesEncoded: this.frameCount,
+ firstChunkSeen: this._firstChunkSeen,
+ droppedForBackpressure: {
+ video: this._droppedForBackpressureCount,
+ audio: this._droppedAudioForBackpressureCount,
+ },
+ peakEncodeQueueSize: {
+ video: this._peakVideoEncodeQueueSize,
+ audio: this._peakAudioEncodeQueueSize,
+ },
+ flushMs: {
+ video: this._videoFlushMs ?? null,
+ audio: this._audioFlushMs ?? null,
+ },
+ swRetry: this._didSwRetry
+ ? { reason: this._swRetryReason || null }
+ : null,
+ lastFailureCode: this._lastFailureCode,
+ });
+ endOnFinalized({ ok: true });
} catch (err) {
+ endOnFinalized({ ok: false, err: String(err?.message || err).slice(0, 80) });
this.err("[WCR] onFinalized threw:", err);
}
}
+ perfMark("WCR.stop.exit");
this.cleanup();
if (this.options.onStop) {
@@ -592,11 +789,19 @@ export class WebCodecsRecorder {
this.paused = false;
this.justResumed = true;
this.muxer?.setPausedOffset(this.totalPausedDurationUs);
+ // Reset the mid-stream watchdog's last-chunk timestamp so it doesn't
+ // fire on the stale pre-pause value before chunks resume flowing.
+ this._lastChunkAt = performance.now();
}
cleanup() {
this.log("[WCR] cleanup");
+ this._clearFirstChunkWatchdog();
+ this._clearMidStreamStallWatchdog();
+ this._detachVisibilityListener();
+ this._detachDeviceChangeListener();
+
try {
this.videoReader?.releaseLock();
} catch {}
@@ -645,21 +850,258 @@ export class WebCodecsRecorder {
this.resizeCtx = null;
this._startPromise = null;
this._startResolve = null;
+ this._firstChunkSeen = false;
+ this._videoLoopError = null;
+ this._lastChunkAt = null;
+ this._lastFailureCode = null;
this._videoLoopPromise = null;
this._audioLoopPromise = null;
}
+ // Fire onError exactly once for a detected recorder failure. The
+ // zero-frame loop exit and the first-chunk watchdog can both race to
+ // report the same stall; the guard keeps the caller's swap handler
+ // from running twice.
+ _reportFailure(code, originalError) {
+ if (this._failureReported) return;
+ this._failureReported = true;
+ const err =
+ originalError instanceof Error
+ ? originalError
+ : new Error(String(originalError || code));
+ if (!err.code) err.code = code;
+ this._lastFailureCode = err.code;
+ // Diagnostic detail (no PII): distinguishes an encoder that was fed
+ // frames but emitted nothing (firstChunkSeen false, framesEncoded > 0)
+ // from a capture track that delivered no frames at all
+ // (framesEncoded 0). This is the signal that pins encoder vs track.
+ if (!err.detail) {
+ err.detail = {
+ framesEncoded: this.frameCount,
+ firstChunkSeen: this._firstChunkSeen,
+ droppedForBackpressure: {
+ video: this._droppedForBackpressureCount,
+ audio: this._droppedAudioForBackpressureCount,
+ },
+ peakEncodeQueueSize: {
+ video: this._peakVideoEncodeQueueSize,
+ audio: this._peakAudioEncodeQueueSize,
+ },
+ flushMs: {
+ video: this._videoFlushMs ?? null,
+ audio: this._audioFlushMs ?? null,
+ },
+ swRetry: this._didSwRetry
+ ? { reason: this._swRetryReason || null }
+ : null,
+ videoLoopError: this._videoLoopError
+ ? String(this._videoLoopError?.message || this._videoLoopError)
+ : null,
+ // Visibility transitions during the recording. Reclaim errors
+ // Failures often correlate with the tab being backgrounded.
+ visibility: {
+ changes: this._visibilityChangeCount,
+ lastChangeAt: this._lastVisibilityChangeAt,
+ lastState: this._lastVisibilityState,
+ stateNow:
+ typeof document !== "undefined" ? document.visibilityState : null,
+ },
+ reclaims: {
+ video: this._encoderReclaimCount,
+ audio: this._audioEncoderReclaimCount,
+ },
+ deviceChanges: this._deviceChangeCount || 0,
+ };
+ }
+ try {
+ this.options.onError?.(err);
+ } catch (cbErr) {
+ this.err("[WCR] onError callback threw:", cbErr);
+ }
+ }
+
+ _attachVisibilityListener() {
+ if (typeof document === "undefined" || this._visibilityChangeListener) {
+ return;
+ }
+ this._visibilityChangeListener = () => {
+ this._visibilityChangeCount += 1;
+ this._lastVisibilityChangeAt = Date.now();
+ this._lastVisibilityState = document.visibilityState;
+ this.log("[WCR] visibilitychange", {
+ state: document.visibilityState,
+ count: this._visibilityChangeCount,
+ });
+ };
+ try {
+ document.addEventListener(
+ "visibilitychange",
+ this._visibilityChangeListener,
+ );
+ } catch {
+ this._visibilityChangeListener = null;
+ }
+ }
+
+ _detachVisibilityListener() {
+ if (typeof document === "undefined" || !this._visibilityChangeListener) {
+ return;
+ }
+ try {
+ document.removeEventListener(
+ "visibilitychange",
+ this._visibilityChangeListener,
+ );
+ } catch {}
+ this._visibilityChangeListener = null;
+ }
+
+ _attachDeviceChangeListener() {
+ if (
+ typeof navigator === "undefined" ||
+ !navigator.mediaDevices ||
+ typeof navigator.mediaDevices.addEventListener !== "function" ||
+ this._deviceChangeListener
+ ) {
+ return;
+ }
+ this._deviceChangeListener = () => {
+ this._deviceChangeCount += 1;
+ this._lastDeviceChangeAt = Date.now();
+ this.log("[WCR] devicechange", { count: this._deviceChangeCount });
+ };
+ try {
+ navigator.mediaDevices.addEventListener(
+ "devicechange",
+ this._deviceChangeListener,
+ );
+ } catch {
+ this._deviceChangeListener = null;
+ }
+ }
+
+ _detachDeviceChangeListener() {
+ if (
+ typeof navigator === "undefined" ||
+ !navigator.mediaDevices ||
+ !this._deviceChangeListener
+ ) {
+ return;
+ }
+ try {
+ navigator.mediaDevices.removeEventListener(
+ "devicechange",
+ this._deviceChangeListener,
+ );
+ } catch {}
+ this._deviceChangeListener = null;
+ }
+
+ _armFirstChunkWatchdog() {
+ this._clearFirstChunkWatchdog();
+ if (this._firstChunkSeen) return;
+ this._firstChunkWatchdog = setTimeout(() => {
+ this._firstChunkWatchdog = null;
+ if (!this.running || this._stopping || this._firstChunkSeen) return;
+ this.warn(
+ `[WCR] no encoded video chunk within ${this._firstChunkWatchdogMs}ms`,
+ );
+ this._reportFailure(
+ "webcodecs-no-first-chunk",
+ new Error(
+ `WebCodecs produced no encoded video within ${this._firstChunkWatchdogMs}ms`,
+ ),
+ );
+ }, this._firstChunkWatchdogMs);
+ }
+
+ _clearFirstChunkWatchdog() {
+ if (this._firstChunkWatchdog) {
+ clearTimeout(this._firstChunkWatchdog);
+ this._firstChunkWatchdog = null;
+ }
+ }
+
+ // Mid-stream stall watchdog. If the encoder goes silent past the
+ // window (HW reclaim, hang, OS pressure), fire onError and stop the
+ // loop so finalize ships what's already encoded.
+ _armMidStreamStallWatchdog() {
+ this._clearMidStreamStallWatchdog();
+ this._lastChunkAt = performance.now();
+ const tickMs = Math.max(2000, Math.floor(this._midStreamWatchdogMs / 3));
+ this._midStreamWatchdog = setInterval(() => {
+ if (
+ !this.running ||
+ this._stopping ||
+ this.paused ||
+ this._failureReported ||
+ this._lastChunkAt == null
+ ) {
+ return;
+ }
+ const sinceLastChunkMs = performance.now() - this._lastChunkAt;
+ if (sinceLastChunkMs > this._midStreamWatchdogMs) {
+ this.warn(
+ `[WCR] no encoded video chunk for ${Math.round(sinceLastChunkMs)}ms mid-recording`,
+ );
+ this._reportFailure(
+ "webcodecs-mid-stream-stall",
+ new Error(
+ `WebCodecs produced no chunks for ${Math.round(sinceLastChunkMs)}ms after frame ${this.frameCount}`,
+ ),
+ );
+ // Graceful stop: exit the run-loop so the normal finalize path
+ // packages whatever was encoded into a playable file.
+ this.running = false;
+ this._clearMidStreamStallWatchdog();
+ }
+ }, tickMs);
+ }
+
+ _clearMidStreamStallWatchdog() {
+ if (this._midStreamWatchdog) {
+ clearInterval(this._midStreamWatchdog);
+ this._midStreamWatchdog = null;
+ }
+ }
+
async initVideoEncoder(config, codecLabel) {
this.log("[WCR] initVideoEncoder()", codecLabel);
- this.videoEncoder = new VideoEncoder({
- output: (chunk, meta) => {
- // decoderConfig.codec can overstate the level vs the SPS (Chrome
- // reports avc1.64002A when the bitstream is L4.0 / 0x28). Parse
- // from the avcC description when possible, fall back to the
- // reported codec. avcC bytes: [0] version, [1] profile,
- // [2] profile_compat, [3] level.
+ // H.264 needs even dimensions; defend at the encoder boundary in
+ // case a caller skips the normalization.
+ if (typeof config?.width === "number" && config.width % 2 !== 0) {
+ this.warn("[WCR] forcing video width to even", {
+ from: config.width,
+ to: config.width - 1,
+ });
+ config = { ...config, width: config.width - 1 };
+ }
+ if (typeof config?.height === "number" && config.height % 2 !== 0) {
+ this.warn("[WCR] forcing video height to even", {
+ from: config.height,
+ to: config.height - 1,
+ });
+ config = { ...config, height: config.height - 1 };
+ }
+
+ // Extract output/error callbacks so the prefer-software retry below
+ // can rebuild the VideoEncoder with the same handlers.
+ const videoEncoderOutput = (chunk, meta) => {
+ // First encoded video chunk: the recording is producing real
+ // bytes, so disarm the no-first-chunk watchdog and arm the
+ // mid-stream stall watchdog instead.
+ if (!this._firstChunkSeen) {
+ this._firstChunkSeen = true;
+ this._clearFirstChunkWatchdog();
+ this._armMidStreamStallWatchdog();
+ }
+ // Refresh the mid-stream watchdog's timestamp on every chunk so
+ // it only fires when output genuinely stops.
+ this._lastChunkAt = performance.now();
+ // decoderConfig.codec can overstate level vs SPS; parse from
+ // avcC ([1]=profile, [2]=compat, [3]=level) when possible.
if (!this.actualVideoCodec) {
const desc = meta?.decoderConfig?.description;
const reported = meta?.decoderConfig?.codec;
@@ -681,42 +1123,171 @@ export class WebCodecsRecorder {
this.actualVideoCodec = reported;
}
}
+
+ // Chrome WebCodecs bug: AVCDecoderConfigurationRecord can have
+ // bit 0/1 set in constraint_set_flags, which some decoders
+ // reject. Zero the byte in place (AVC only).
+ // ref: github.com/WebAV-Tech/WebAV/issues/203
+ const codecForFix = this.actualVideoCodec || meta?.decoderConfig?.codec;
+ const descForFix = meta?.decoderConfig?.description;
+ if (
+ descForFix &&
+ typeof codecForFix === "string" &&
+ codecForFix.startsWith("avc")
+ ) {
+ const u8 = descForFix instanceof Uint8Array
+ ? descForFix
+ : new Uint8Array(
+ descForFix instanceof ArrayBuffer
+ ? descForFix
+ : descForFix.buffer,
+ descForFix instanceof ArrayBuffer ? 0 : descForFix.byteOffset,
+ descForFix.byteLength,
+ );
+ if (u8.length >= 3 && (u8[2] & 0x03) !== 0) {
+ u8[2] = 0;
+ }
+ }
+
+ // Surface the decoder config once. Persisting it alongside
+ // OPFS chunks unlocks chunks-only recovery when the muxer
+ // never finalizes (tab killed mid-recording).
+ if (
+ !this._decoderConfigEmitted &&
+ typeof this.options.onDecoderConfig === "function"
+ ) {
+ const desc = meta?.decoderConfig?.description;
+ const codec = this.actualVideoCodec || meta?.decoderConfig?.codec;
+ if (desc && codec) {
+ this._decoderConfigEmitted = true;
+ try {
+ this.options.onDecoderConfig({
+ description: desc,
+ codec,
+ container: this.containerKind,
+ width: this.targetWidth,
+ height: this.targetHeight,
+ });
+ } catch (cbErr) {
+ this.warn("[WCR] onDecoderConfig callback threw:", cbErr);
+ }
+ }
+ }
+
this.muxer.addVideoChunk(chunk, meta);
- },
- error: (err) => {
- this.err("[WCR] VideoEncoder error:", err);
- this.options.onError?.(err);
- },
+ };
+ const videoEncoderError = (err) => {
+ this._handleVideoEncoderError(err);
+ };
+ this._videoEncoderOutputHandler = videoEncoderOutput;
+ this._videoEncoderErrorHandler = videoEncoderError;
+ this._activeVideoCodecLabel = codecLabel;
+ this.videoEncoder = new VideoEncoder({
+ output: videoEncoderOutput,
+ error: videoEncoderError,
});
- // configure() can throw sync on invalid configs (Chrome removing a
- // codec profile, validation changes). Tag with codec context for triage.
+ // configure() can throw sync. On QuotaExceeded or generic failure
+ // with prefer-hardware, retry once with prefer-software (Teams/
+ // Zoom contention, NVIDIA 3-encoder cap). HW → SW → MR.
+ const tryConfigureWith = (cfg) => {
+ // E2E hook: force one HW configure() to throw QuotaExceededError so
+ // tests can exercise the prefer-software retry path. Mirrors
+ // __screenityForceZeroFrames; one-shot.
+ const hwPref =
+ cfg.hardwareAcceleration === "prefer-hardware" ||
+ !cfg.hardwareAcceleration;
+ const g = /** @type {any} */ (globalThis);
+ if (
+ hwPref &&
+ g.__screenityForceConfigureHwQuotaError &&
+ !g.__screenityForceConfigureHwQuotaError_fired
+ ) {
+ g.__screenityForceConfigureHwQuotaError_fired = true;
+ const forced = new Error(
+ "Forced HW configure quota error for testing",
+ );
+ forced.name = "QuotaExceededError";
+ throw forced;
+ }
+ this.videoEncoder.configure(cfg);
+ };
try {
- this.videoEncoder.configure(config);
+ tryConfigureWith(config);
} catch (err) {
+ const isHwPreferred =
+ config.hardwareAcceleration === "prefer-hardware" ||
+ !config.hardwareAcceleration;
+ const errMsg = String(err?.message || err || "");
+ const isContentionError =
+ err?.name === "QuotaExceededError" ||
+ /quota|too many|in use|already/i.test(errMsg);
+ if (isHwPreferred && !this._didSwRetry) {
+ this._didSwRetry = true;
+ this._swRetryReason = isContentionError
+ ? "encoder-quota"
+ : "configure-failed";
+ this.warn(
+ "[WCR] HW VideoEncoder.configure failed, retrying prefer-software",
+ { reason: this._swRetryReason, codec: codecLabel, message: errMsg },
+ );
+ // Rebuild the encoder: configure() on a closed/errored encoder
+ // throws InvalidStateError on subsequent calls.
+ try {
+ this.videoEncoder.close();
+ } catch {}
+ this.videoEncoder = new VideoEncoder({
+ output: videoEncoderOutput,
+ error: videoEncoderError,
+ });
+ const swConfig = { ...config, hardwareAcceleration: "prefer-software" };
+ try {
+ tryConfigureWith(swConfig);
+ this.log("[WCR] VideoEncoder configured (prefer-software fallback)");
+ this._activeVideoConfig = swConfig;
+ return;
+ } catch (swErr) {
+ const tagged = new Error(
+ `VideoEncoder.configure failed for ${codecLabel} on both HW and SW: ${swErr?.message || swErr}`,
+ );
+ tagged.cause = swErr;
+ tagged.codec = codecLabel;
+ tagged.code = "video-encoder-configure-failed";
+ tagged.swRetryReason = this._swRetryReason;
+ throw tagged;
+ }
+ }
const tagged = new Error(
- `VideoEncoder.configure failed for ${codecLabel}: ${err?.message || err}`,
+ `VideoEncoder.configure failed for ${codecLabel}: ${errMsg}`,
);
tagged.cause = err;
tagged.codec = codecLabel;
tagged.code = "video-encoder-configure-failed";
throw tagged;
}
+ this._activeVideoConfig = config;
this.log("[WCR] VideoEncoder configured");
}
async initAudioEncoder(config) {
if (!config) return;
+ const audioEncoderOutput = (chunk, meta) => {
+ if (this.debug) this.log("[WCR] AUDIO");
+ // Chrome occasionally emits AAC chunks with a broken
+ // AudioSpecificConfig (under 2 bytes or audioObjectType=0).
+ // Synthesize a correct 2-byte ASC when invalid.
+ const fixed = this._maybeFixAacDescription(meta);
+ this.muxer.addAudioChunk(chunk, fixed || meta);
+ };
+ const audioEncoderError = (err) => {
+ this._handleAudioEncoderError(err);
+ };
+ this._audioEncoderOutputHandler = audioEncoderOutput;
+ this._audioEncoderErrorHandler = audioEncoderError;
this.audioEncoder = new AudioEncoder({
- output: (chunk, meta) => {
- if (this.debug) this.log("[WCR] AUDIO");
- this.muxer.addAudioChunk(chunk, meta);
- },
- error: (err) => {
- this.err("[WCR] AudioEncoder error:", err);
- this.options.onError?.(err);
- },
+ output: audioEncoderOutput,
+ error: audioEncoderError,
});
try {
@@ -729,6 +1300,207 @@ export class WebCodecsRecorder {
tagged.code = "audio-encoder-configure-failed";
throw tagged;
}
+ this._activeAudioConfig = config;
+ }
+
+ // Patch AAC meta when description is missing/short/audioObjectType=0.
+ // Returns null when no fix is needed. First-chunk only in practice.
+ _maybeFixAacDescription(meta) {
+ const dc = meta?.decoderConfig;
+ if (!dc) return null;
+ const codec = dc.codec || this._activeAudioConfig?.codec || "";
+ if (typeof codec !== "string" || !/^mp4a\.40\./.test(codec)) return null;
+ const desc = dc.description;
+ let invalid = false;
+ let u8 = null;
+ if (!desc) {
+ invalid = true;
+ } else {
+ u8 =
+ desc instanceof Uint8Array
+ ? desc
+ : new Uint8Array(
+ desc instanceof ArrayBuffer ? desc : desc.buffer,
+ desc instanceof ArrayBuffer ? 0 : desc.byteOffset,
+ desc.byteLength,
+ );
+ if (u8.byteLength < 2) {
+ invalid = true;
+ } else {
+ const objectType = (u8[0] >> 3) & 0x1f;
+ if (objectType === 0) invalid = true;
+ }
+ }
+ if (!invalid) return null;
+
+ const objectTypeFromCodec = Number(codec.split(".")[2]);
+ const sampleRate =
+ dc.sampleRate ||
+ this._activeAudioConfig?.sampleRate ||
+ this.audioSampleRate ||
+ 48000;
+ const channels =
+ dc.numberOfChannels ||
+ this._activeAudioConfig?.numberOfChannels ||
+ this.audioChannelCount ||
+ 2;
+ const synthesized = this._synthesizeAacDescription(
+ Number.isFinite(objectTypeFromCodec) && objectTypeFromCodec > 0
+ ? objectTypeFromCodec
+ : 2,
+ sampleRate,
+ channels,
+ );
+ this.warn("[WCR] rewrote invalid AAC AudioSpecificConfig", {
+ had: u8 ? Array.from(u8).map((b) => b.toString(16)) : null,
+ synthesized: Array.from(synthesized).map((b) => b.toString(16)),
+ });
+ return { ...meta, decoderConfig: { ...dc, description: synthesized } };
+ }
+
+ // Pack an AAC ASC: 5b objectType + 4b sampleRateIdx + 4b channels
+ // + 3b pad = 2 bytes. Common case only (objectType < 32, standard
+ // sample rates). AAC-LC 48k stereo = [0x11, 0x90].
+ _synthesizeAacDescription(objectType, sampleRate, numberOfChannels) {
+ const AAC_SAMPLE_RATES = [
+ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000,
+ 11025, 8000, 7350,
+ ];
+ let freqIndex = AAC_SAMPLE_RATES.indexOf(sampleRate);
+ if (freqIndex === -1) freqIndex = 3; // 48000
+ const channelConfig = numberOfChannels === 8 ? 7 : numberOfChannels;
+ const byte0 =
+ ((objectType & 0x1f) << 3) | ((freqIndex >> 1) & 0x07);
+ const byte1 = ((freqIndex & 0x01) << 7) | ((channelConfig & 0x0f) << 3);
+ return new Uint8Array([byte0, byte1]);
+ }
+
+ // Handle VideoEncoder.error: rebuild on HW reclaim (capped by
+ // _maxEncoderReclaims), or one HW→SW rebuild for pre-first-chunk
+ // async errors. Otherwise surface to onError.
+ _handleVideoEncoderError(err) {
+ const msg = String(err?.message || err || "");
+ const isReclaim =
+ /codec\s+reclaimed|reclaimed\s+due\s+to\s+inactivity/i.test(msg);
+ const canRecover =
+ this.running &&
+ !this._stopping &&
+ this._activeVideoConfig &&
+ this._videoEncoderOutputHandler &&
+ this._videoEncoderErrorHandler;
+
+ if (
+ isReclaim &&
+ canRecover &&
+ this._encoderReclaimCount < this._maxEncoderReclaims
+ ) {
+ this._encoderReclaimCount += 1;
+ this.warn("[WCR] VideoEncoder reclaimed, rebuilding", {
+ attempt: this._encoderReclaimCount,
+ max: this._maxEncoderReclaims,
+ codec: this._activeVideoCodecLabel,
+ message: msg,
+ });
+ if (this._rebuildVideoEncoder(this._activeVideoConfig)) {
+ this._forceNextKeyframe = true;
+ return;
+ }
+ }
+
+ const preFirstChunk = !this._firstChunkSeen;
+ const isHwPreferred =
+ this._activeVideoConfig &&
+ (this._activeVideoConfig.hardwareAcceleration === "prefer-hardware" ||
+ !this._activeVideoConfig.hardwareAcceleration);
+ if (preFirstChunk && canRecover && isHwPreferred && !this._didSwRetry) {
+ this._didSwRetry = true;
+ this._swRetryReason = "encoder-error-pre-first-chunk";
+ this.warn(
+ "[WCR] VideoEncoder errored before first chunk, retrying prefer-software",
+ { codec: this._activeVideoCodecLabel, message: msg },
+ );
+ const swConfig = {
+ ...this._activeVideoConfig,
+ hardwareAcceleration: "prefer-software",
+ };
+ if (this._rebuildVideoEncoder(swConfig)) {
+ this._forceNextKeyframe = true;
+ return;
+ }
+ }
+
+ this.err("[WCR] VideoEncoder error:", err);
+ this.options.onError?.(err);
+ }
+
+ // Audio reclaim recovery. Less critical than video (audio glitches are
+ // usually preferable to losing the recording) but the same pattern
+ // applies; Chrome can reclaim the AudioEncoder slot too. No keyframe
+ // concept for audio; rebuild and continue.
+ _handleAudioEncoderError(err) {
+ const msg = String(err?.message || err || "");
+ const isReclaim =
+ /codec\s+reclaimed|reclaimed\s+due\s+to\s+inactivity/i.test(msg);
+ if (
+ isReclaim &&
+ this.running &&
+ !this._stopping &&
+ this._activeAudioConfig &&
+ this._audioEncoderOutputHandler &&
+ this._audioEncoderErrorHandler &&
+ this._audioEncoderReclaimCount < this._maxEncoderReclaims
+ ) {
+ this._audioEncoderReclaimCount += 1;
+ this.warn("[WCR] AudioEncoder reclaimed, rebuilding", {
+ attempt: this._audioEncoderReclaimCount,
+ max: this._maxEncoderReclaims,
+ message: msg,
+ });
+ if (this._rebuildAudioEncoder(this._activeAudioConfig)) {
+ return;
+ }
+ }
+
+ this.err("[WCR] AudioEncoder error:", err);
+ this.options.onError?.(err);
+ }
+
+ // Best-effort close + fresh construct/configure with the same
+ // handlers. The video loop re-reads this.videoEncoder each tick.
+ _rebuildVideoEncoder(config) {
+ try {
+ this.videoEncoder?.close();
+ } catch {}
+ try {
+ this.videoEncoder = new VideoEncoder({
+ output: this._videoEncoderOutputHandler,
+ error: this._videoEncoderErrorHandler,
+ });
+ this.videoEncoder.configure(config);
+ this._activeVideoConfig = config;
+ return true;
+ } catch (rebuildErr) {
+ this.err("[WCR] VideoEncoder rebuild failed:", rebuildErr);
+ return false;
+ }
+ }
+
+ _rebuildAudioEncoder(config) {
+ try {
+ this.audioEncoder?.close();
+ } catch {}
+ try {
+ this.audioEncoder = new AudioEncoder({
+ output: this._audioEncoderOutputHandler,
+ error: this._audioEncoderErrorHandler,
+ });
+ this.audioEncoder.configure(config);
+ this._activeAudioConfig = config;
+ return true;
+ } catch (rebuildErr) {
+ this.err("[WCR] AudioEncoder rebuild failed:", rebuildErr);
+ return false;
+ }
}
async prepareAudioEncoderConfig() {
@@ -788,21 +1560,41 @@ export class WebCodecsRecorder {
latencyMode: "realtime",
};
+ // Bias to software when the caller asks (cloud camera on macOS:
+ // VideoToolbox serializes h264 encoders per-process and a second
+ // prefer-hardware silently falls back at unpredictable fps).
+ // Forcing SW gives a reliable 30fps instead of contended 5fps.
+ const preferSoftware = Boolean(this.options?.preferSoftware);
const candidates =
this.containerKind === "webm"
- ? [
- { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-hardware" },
- { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-software" },
- { codec: "vp8", containerCodec: "vp8", hw: "prefer-software" },
- ]
- : [
- { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-hardware" },
- { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-hardware" },
- { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-hardware" },
- { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-software" },
- { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-software" },
- { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-software" },
- ];
+ ? preferSoftware
+ ? [
+ { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-software" },
+ { codec: "vp8", containerCodec: "vp8", hw: "prefer-software" },
+ { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-hardware" },
+ ]
+ : [
+ { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-hardware" },
+ { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-software" },
+ { codec: "vp8", containerCodec: "vp8", hw: "prefer-software" },
+ ]
+ : preferSoftware
+ ? [
+ { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-software" },
+ { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-software" },
+ { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-software" },
+ { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-hardware" },
+ { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-hardware" },
+ { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-hardware" },
+ ]
+ : [
+ { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-hardware" },
+ { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-hardware" },
+ { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-hardware" },
+ { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-software" },
+ { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-software" },
+ { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-software" },
+ ];
for (const c of candidates) {
const config = { ...base, codec: c.codec, hardwareAcceleration: c.hw };
@@ -853,35 +1645,82 @@ export class WebCodecsRecorder {
try {
while (this.running) {
+ // E2E hook: simulate a silent stall (zero encoded frames, no
+ // throw). Thrown-error hooks don't cover this failure mode.
+ if (globalThis.__screenityForceZeroFrames) break;
+
if (this.paused) {
await new Promise((r) => setTimeout(r, 10));
continue;
}
- const { value: frame, done } = await this.videoReader
- .read()
- .catch(() => ({ done: true }));
-
- if (done || !frame) break;
-
- if (frame.codedWidth === 0 || frame.codedHeight === 0) {
- this.warn("[WCR] zero-size frame");
- this.options.onError?.({ type: "video-lost" });
- }
-
- ensureResizeCanvas();
+ // Static-frame fallback. macOS ScreenCaptureKit throttles to
+ // frames-on-change on a static screen, so the track stops
+ // emitting and the stall watchdog would misfire. Reuse the
+ // last canvas on timeout to keep chunks flowing.
+ const STATIC_FRAME_FALLBACK_MS = 1500;
+ const readResult = await Promise.race([
+ this.videoReader
+ .read()
+ .then((v) => ({ ...v, isSyntheticTimeout: false }))
+ .catch(() => ({
+ done: true,
+ value: null,
+ isSyntheticTimeout: false,
+ })),
+ new Promise((resolve) =>
+ setTimeout(
+ () =>
+ resolve({
+ done: false,
+ value: null,
+ isSyntheticTimeout: true,
+ }),
+ STATIC_FRAME_FALLBACK_MS,
+ ),
+ ),
+ ]);
- this.resizeCtx.drawImage(
- frame,
- 0,
- 0,
- this.targetWidth,
- this.targetHeight
- );
+ let isSynthetic = false;
+ let frame = null;
+ if (readResult.isSyntheticTimeout) {
+ // Track stalled; only synthesize if we've drawn a frame
+ // already (else first-chunk watchdog handles no-frames).
+ if (!this._firstChunkSeen || !this.resizeCanvas) {
+ continue;
+ }
+ isSynthetic = true;
+ } else {
+ if (readResult.done || !readResult.value) break;
+ frame = readResult.value;
+
+ if (frame.codedWidth === 0 || frame.codedHeight === 0) {
+ // A zero-size frame can't be drawn or encoded. Skip it
+ // instead of feeding a 0x0 frame into the encoder. If
+ // every frame is zero-size, no chunk ever lands and the
+ // first-chunk watchdog surfaces the failure.
+ this.warn("[WCR] zero-size frame, skipping");
+ frame.close();
+ continue;
+ }
- if (!this._videoStartUs) {
- this._videoStartUs = performance.now() * 1000;
- perfMark("WCR.firstSourceFrame", { w: frame.codedWidth, h: frame.codedHeight });
+ ensureResizeCanvas();
+
+ this.resizeCtx.drawImage(
+ frame,
+ 0,
+ 0,
+ this.targetWidth,
+ this.targetHeight,
+ );
+
+ if (!this._videoStartUs) {
+ this._videoStartUs = performance.now() * 1000;
+ perfMark("WCR.firstSourceFrame", {
+ w: frame.codedWidth,
+ h: frame.codedHeight,
+ });
+ }
}
const nowUs = performance.now() * 1000;
@@ -894,7 +1733,7 @@ export class WebCodecsRecorder {
// Drop frames ahead of wall-clock.
if (targetIndex < this._videoFrameIndex) {
- frame.close();
+ frame?.close();
continue;
}
@@ -913,6 +1752,24 @@ export class WebCodecsRecorder {
const endIndex = targetIndex;
for (let i = startIndex; i <= endIndex; i += 1) {
const tsUs = i * frameDurationUs;
+
+ // Track the peak observed queue depth as a leading indicator
+ //; useful for tuning the threshold from real telemetry.
+ const vQueueSize = this.videoEncoder.encodeQueueSize;
+ if (vQueueSize > this._peakVideoEncodeQueueSize) {
+ this._peakVideoEncodeQueueSize = vQueueSize;
+ }
+
+ // Backpressure: drop the frame and force a keyframe next so
+ // the stream stays decodable. An unbounded queue can stop
+ // emitting chunks entirely.
+ if (vQueueSize > this._encoderMaxQueueSize) {
+ this._droppedForBackpressureCount += 1;
+ this._forceNextKeyframe = true;
+ this._videoFrameIndex = i + 1;
+ continue;
+ }
+
const resized = new VideoFrame(this.resizeCanvas, {
timestamp: tsUs,
duration: frameDurationUs,
@@ -921,8 +1778,12 @@ export class WebCodecsRecorder {
const keyFrame =
i === 0 ||
(this.justResumed && i === startIndex) ||
+ this._forceNextKeyframe === true ||
i - this._lastKeyFrameIndex >= this._keyFrameIntervalFrames;
- if (keyFrame) this._lastKeyFrameIndex = i;
+ if (keyFrame) {
+ this._lastKeyFrameIndex = i;
+ this._forceNextKeyframe = false;
+ }
if (this.justResumed && i === startIndex) {
this.justResumed = false;
}
@@ -939,26 +1800,54 @@ export class WebCodecsRecorder {
targetIndex,
});
}
+ // Diag snapshot every 30 frames. encodeQueueSize > 10 means
+ // the encoder can't keep up (the "first seconds sluggish"
+ // signature).
+ if (this.frameCount > 0 && this.frameCount % 30 === 0) {
+ perfMark("WCR.frame.progress", {
+ frame: this.frameCount,
+ videoQ: this.videoEncoder?.encodeQueueSize ?? -1,
+ audioQ: this.audioEncoder?.encodeQueueSize ?? -1,
+ });
+ }
this.frameCount++;
this._videoFrameIndex = i + 1;
resized.close();
}
- frame.close();
+ // Only close a real source frame; the synthetic path reuses
+ // the existing resize-canvas content and has no incoming frame.
+ if (frame) frame.close();
}
} catch (err) {
this.err("[WCR] video loop error:", err);
+ // Keep the error so the zero-frame branch below can attach the
+ // real cause (e.g. a synchronous encode() throw) to onError.
+ this._videoLoopError = err;
}
this.log("[WCR] video loop exit", this.frameCount, "running=", this.running);
- // Loop exited while still running: video source died. Stop the audio
- // loop so audio doesn't extend past last video frame. Don't fire
- // onError, partial recording is valid.
+ // Loop exited while running; stop audio so it doesn't outlast video.
if (this.running) {
- this.warn(
- "[WCR] video loop exited early while running; stopping audio loop",
- );
this.running = false;
+ if (this.frameCount === 0) {
+ // Zero frames encoded means there is no recording; only the
+ // muxer header would reach the uploader (the "28-byte ftyp"
+ // stall). This is NOT a valid partial recording: surface it so
+ // the caller swaps to MediaRecorder.
+ this.warn("[WCR] video loop exited with zero frames encoded");
+ this._reportFailure(
+ "webcodecs-zero-frames",
+ this._videoLoopError ||
+ new Error("WebCodecs video loop produced zero frames"),
+ );
+ } else {
+ // Some frames encoded: a real, if short, partial recording
+ // exists. Keep it; finalize() ships what we have.
+ this.warn(
+ "[WCR] video loop exited early while running; partial recording kept",
+ );
+ }
}
}
@@ -981,6 +1870,21 @@ export class WebCodecsRecorder {
);
const durUs = Math.round((frames * 1_000_000) / sampleRate);
+ // Track peak audio encode-queue depth (leading indicator).
+ const aQueueSize = this.audioEncoder.encodeQueueSize;
+ if (aQueueSize > this._peakAudioEncodeQueueSize) {
+ this._peakAudioEncodeQueueSize = aQueueSize;
+ }
+
+ // Backpressure mirror of the video path. Advance the sample
+ // counter even on a drop so subsequent timestamps stay aligned
+ // (brief silence gap, beats audio drifting out of sync).
+ if (aQueueSize > this._audioEncoderMaxQueueSize) {
+ this._droppedAudioForBackpressureCount += 1;
+ this.audioSamplesWritten += frames;
+ return;
+ }
+
this.audioEncoder.encode(audioData, {
timestamp: tsUs,
});
@@ -996,10 +1900,8 @@ export class WebCodecsRecorder {
}
};
- // Windows WASAPI loopback stops yielding frames when system audio is
- // silent, leaving audioReader.read() pending forever while video keeps
- // recording. Pad with silence so the audio track stays aligned to the
- // full video duration.
+ // Windows WASAPI loopback stops yielding on silent system audio,
+ // leaving audioReader.read() pending. Pad with silence.
const SILENCE_TIMEOUT_MS = 500;
const SILENCE_CHUNK_MS = 500;
let paddedSilenceCount = 0;
diff --git a/src/pages/Sandbox/Sandbox.jsx b/src/pages/Sandbox/Sandbox.jsx
index c2bbb52..ae4cd5c 100644
--- a/src/pages/Sandbox/Sandbox.jsx
+++ b/src/pages/Sandbox/Sandbox.jsx
@@ -1,8 +1,10 @@
import "./styles/edit/_VideoPlayer.scss";
import "./styles/global/_app.scss";
-import React, { useEffect, useRef, useContext } from "react";
-import Editor from "./layout/editor/Editor";
+import React, { useEffect, useRef, useContext, Suspense, lazy } from "react";
+// Editor (trim/cut/timeline UI) only mounts when user enters edit mode.
+// Initial open is "player" mode; defer Editor + its TrimUI dependencies.
+const Editor = lazy(() => import("./layout/editor/Editor"));
import Player from "./layout/player/Player";
import Modal from "./components/global/Modal";
import Toast from "./components/global/Toast";
@@ -270,7 +272,11 @@ const Sandbox = () => {
<video></video>
{contentState.ffmpeg &&
contentState.ready &&
- contentState.mode === "edit" && <Editor />}
+ contentState.mode === "edit" && (
+ <Suspense fallback={null}>
+ <Editor />
+ </Suspense>
+ )}
{contentState.mode != "edit" && contentState.ready && <Player />}
{!contentState.ready && (
<div className="wrap">
diff --git a/src/pages/Sandbox/context/ContentState.jsx b/src/pages/Sandbox/context/ContentState.jsx
index 1db55e4..dd8bb6e 100644
--- a/src/pages/Sandbox/context/ContentState.jsx
+++ b/src/pages/Sandbox/context/ContentState.jsx
@@ -25,15 +25,15 @@ import { diagForward } from "../../utils/diagForward";
import { perfMark, perfSpan } from "../../utils/perfMarks";
import { triggerSupportDownload } from "../../utils/triggerSupportDownload";
import { chooseReader } from "../recorderStorage/chooseReader";
-import {
- Input,
- Output,
- BlobSource,
- BufferTarget,
- Mp4OutputFormat,
- ALL_FORMATS,
- Conversion,
-} from "mediabunny";
+// mediabunny is ~630KB (the muxer/demuxer pipeline). Only used in
+// export/remux/conversion functions that fire on user action, not on
+// editor mount. Lazy-loading shifts that parse cost off the editor's
+// cold-load critical path. Cache promise; all call sites are async.
+let _mbPromise = null;
+const loadMb = () => {
+ if (!_mbPromise) _mbPromise = import("mediabunny");
+ return _mbPromise;
+};
localforage.config({
driver: localforage.INDEXEDDB,
@@ -493,7 +493,7 @@ const ContentState = (props) => {
const timestamp = formatLocalTimestamp(recordingMeta.startedAt);
setContentState((prevState) => ({
...prevState,
- title: `${baseTitle} — ${timestamp}`,
+ title: `${baseTitle}; ${timestamp}`,
recordingMeta,
}));
chrome.storage.local.remove(["recordingMeta"]);
@@ -1224,12 +1224,9 @@ const ContentState = (props) => {
prev.finalizingRecording ? { ...prev, finalizingRecording: false } : prev,
);
await reader.close().catch(() => {});
- // Use the OPFS-backed Blob on the critical path. The previous
- // arrayBuffer copy was defensive against mtime/deletion but
- // could take 30-60s on slow disks for 100MB+ files (the "stuck
- // at 90%" reports). Materialize in the background instead, so a
- // later recording that deletes the OPFS file doesn't strand the
- // editor.
+ // OPFS Blob on the critical path; arrayBuffer copy was 30-60s
+ // on slow disks for 100MB+ ("stuck at 90%"). Materialize in
+ // the background.
const blob = rawBlob;
if (rawBlob) {
diagForward("sandbox-opfs-materialize-deferred", {
@@ -1947,6 +1944,15 @@ const ContentState = (props) => {
}, []);
const onMessage = async (event) => {
+ // editor-force-close from the editorwebcodecs.html parent.
+ // sandbox.html can't use chrome.runtime, so accept it via
+ // postMessage and clear beforeunload to avoid the prompt.
+ if (event.data?.type === "editor-force-close") {
+ try {
+ window.onbeforeunload = null;
+ } catch {}
+ return;
+ }
if (event.data.type === "recording-error-from-parent") {
const payload = event.data.payload || {};
// suppress when this editor already loaded; parent forwards every error
@@ -2772,6 +2778,15 @@ const ContentState = (props) => {
// BufferTarget is required: fastStart:false patches the mdat size with a
// positioned write at the end, which a stream-to-blob pipe would drop.
const remuxFragmentedToStandardMp4 = async (fragmentedBlob, onProgress) => {
+ const {
+ Input,
+ Output,
+ BlobSource,
+ BufferTarget,
+ Mp4OutputFormat,
+ ALL_FORMATS,
+ Conversion,
+ } = await loadMb();
const input = new Input({
formats: ALL_FORMATS,
source: new BlobSource(fragmentedBlob),
@@ -3092,7 +3107,7 @@ const ContentState = (props) => {
const downloadGIF = async () => {
// ref: rapid clicks fire before state propagates
const latest = contentStateRef.current || contentState;
- // Don't gate on isFfmpegRunning — it leaks from background poll
+ // Don't gate on isFfmpegRunning; it leaks from background poll
// handlers and would intermittently swallow the click. downloadingGIF
// is the real lock (mirrors the `download` MP4 path).
if (latest.downloadingGIF || latest.downloading) {
diff --git a/src/pages/Sandbox/layout/player/Content.js b/src/pages/Sandbox/layout/player/Content.js
index ad07ef8..bf71ace 100644
--- a/src/pages/Sandbox/layout/player/Content.js
+++ b/src/pages/Sandbox/layout/player/Content.js
@@ -1,9 +1,14 @@
-import React, { useContext, useState } from "react";
+import React, { useContext, useState, Suspense, lazy } from "react";
import styles from "../../styles/player/_Content.module.scss";
// Components
import VideoPlayer from "../../components/player/VideoPlayer";
-import CropperWrap from "../../components/editor/CropperWrap";
+// Cropper drags in react-advanced-cropper (~218KB) and is only rendered
+// when the user opens the crop tool. Lazy-load so it doesn't eat the
+// editor's initial parse budget on every recording-stop.
+const CropperWrap = lazy(() =>
+ import("../../components/editor/CropperWrap"),
+);
import HelpButton from "../../components/player/HelpButton";
import ProBanner from "../../components/global/ProBanner";
@@ -17,7 +22,11 @@ const Content = () => {
<div className={styles.wrap}>
{contentState.mode === "audio" && <VideoPlayer />}
{contentState.mode === "player" && <VideoPlayer />}
- {contentState.mode === "crop" && <CropperWrap />}
+ {contentState.mode === "crop" && (
+ <Suspense fallback={null}>
+ <CropperWrap />
+ </Suspense>
+ )}
</div>
{contentState.mode === "crop" && <HelpButton />}
{contentState.bannerSupport && <ProBanner />}
diff --git a/src/pages/Sandbox/layout/player/Player.js b/src/pages/Sandbox/layout/player/Player.js
index 385b676..70ffa86 100644
--- a/src/pages/Sandbox/layout/player/Player.js
+++ b/src/pages/Sandbox/layout/player/Player.js
@@ -1,9 +1,12 @@
-import React, { useContext, useState } from "react";
+import React, { useContext, useState, Suspense, lazy } from "react";
// Components
import PlayerNav from "./PlayerNav";
-import CropNav from "../editor/CropNav";
-import AudioNav from "../editor/AudioNav";
+// Crop and Audio nav only render in their respective modes; player
+// mode is the initial state on editor open. Lazy-loading avoids
+// pulling their trees into the editor's cold-load critical path.
+const CropNav = lazy(() => import("../editor/CropNav"));
+const AudioNav = lazy(() => import("../editor/AudioNav"));
import RightPanel from "./RightPanel";
import Content from "./Content";
@@ -17,9 +20,17 @@ const Player = () => {
return (
<div className={styles.layout}>
- {contentState.mode === "crop" && <CropNav />}
+ {contentState.mode === "crop" && (
+ <Suspense fallback={null}>
+ <CropNav />
+ </Suspense>
+ )}
{contentState.mode === "player" && <PlayerNav />}
- {contentState.mode === "audio" && <AudioNav />}
+ {contentState.mode === "audio" && (
+ <Suspense fallback={null}>
+ <AudioNav />
+ </Suspense>
+ )}
<div className={styles.content}>
<Content />
<RightPanel />
diff --git a/src/pages/utils/buildDiagnosticZip.js b/src/pages/utils/buildDiagnosticZip.js
index 9509e38..db155ac 100644
--- a/src/pages/utils/buildDiagnosticZip.js
+++ b/src/pages/utils/buildDiagnosticZip.js
@@ -1,13 +1,10 @@
-/**
- * Builds a diagnostic ZIP for troubleshooting. Returns { blob, filename }.
- * Used by the popup settings menu and the editor "Get help" button.
- */
-import JSZip from "jszip";
+// Builds a diagnostic ZIP. Returns { blob, filename }. JSZip lives in
+// the BG (make-zip handler) so the ~100 KB dep doesn't ship into every
+// content script.
import { getStartFlowTrace } from "./startFlowTrace";
-// Last line of defense before the diag zip leaves the user's machine:
-// strip user-home paths and URL query strings/fragments (recording IDs,
-// signed-URL tokens, OAuth tokens).
+// Strip user-home paths and URL query/fragments (recording IDs,
+// signed-URL tokens) before the zip leaves the machine.
const PII_REPLACEMENTS = [
[/\/Users\/[^/\s"]+/g, "/Users/[redacted]"],
[/\/home\/[^/\s"]+/g, "/home/[redacted]"],
@@ -55,6 +52,17 @@ const FAST_RECORDER_KEYS = [
"useWebCodecsRecorder",
"lastWebCodecsFailureAt",
"lastWebCodecsFailureCode",
+ // Detailed WebCodecs failure payload (framesEncoded, firstChunkSeen,
+ // queue/flush stats). Key for diagnosing zero-frame / 28-byte-ftyp bugs.
+ "lastWebCodecsFailureDetail",
+ // HW→SW retry succeeded mid-session (Teams/Zoom/NVIDIA contention).
+ "lastWebCodecsSwRetry",
+ // Stop classification: separates low-storage from generic chunk-save-failed.
+ "lastRecorderStopReason",
+ "lastRecorderStopAt",
+ // Mid-stream track-end (monitor unplug, tab close, "Stop sharing").
+ // Includes savedChunks/duration/label/readyState to reconstruct from zip.
+ "lastTrackEndEvent",
"lastFailedValidation",
"webcodecsConstructSnapshot",
"recorderStartTimings",
@@ -89,8 +97,7 @@ export const buildDiagnosticZip = async ({
),
),
new Promise((resolve) =>
- // perfMarks writes per-context (perfTimeline.BG, etc) to avoid
- // write races; read all and merge by timestamp.
+ // Merge per-context perfTimeline.* keys by timestamp.
chrome.storage.local.get(null, (all) => {
const merged = [];
for (const k of Object.keys(all || {})) {
@@ -104,7 +111,6 @@ export const buildDiagnosticZip = async ({
}),
),
new Promise((resolve) =>
- // CloudRecorder writes locally before posting; captures state when offline.
chrome.storage.local.get(["cloudUploadTelemetryEvents"], (r) =>
resolve(
Array.isArray(r?.cloudUploadTelemetryEvents)
@@ -119,43 +125,33 @@ export const buildDiagnosticZip = async ({
const now = new Date();
const ts = now.toISOString().replace(/[:.]/g, "-").slice(0, 19);
- const zip = new JSZip();
-
- zip.file(
- "manifest.json",
- JSON.stringify({
- extensionVersion: manifestVersion,
- schemaVersion: 1,
- exportedAt: now.toISOString(),
- chromeVersion: /Chrome\/([\d.]+)/.exec(userAgent)?.[1] || null,
- source,
- }),
- );
-
- zip.file(
- "environment.json",
- JSON.stringify({
- userAgent,
- platformInfo,
- screen: {
- width: window.screen.availWidth,
- height: window.screen.availHeight,
- devicePixelRatio: window.devicePixelRatio,
- },
- deviceMemory: navigator.deviceMemory || null,
- }),
- );
-
- zip.file(
- "config.json",
- JSON.stringify({
- fastRecorder: fastRecorderData,
- ...extraConfig,
- }),
- );
+ const files = {};
+
+ files["manifest.json"] = JSON.stringify({
+ extensionVersion: manifestVersion,
+ schemaVersion: 1,
+ exportedAt: now.toISOString(),
+ chromeVersion: /Chrome\/([\d.]+)/.exec(userAgent)?.[1] || null,
+ source,
+ });
+
+ files["environment.json"] = JSON.stringify({
+ userAgent,
+ platformInfo,
+ screen: {
+ width: window.screen.availWidth,
+ height: window.screen.availHeight,
+ devicePixelRatio: window.devicePixelRatio,
+ },
+ deviceMemory: navigator.deviceMemory || null,
+ });
+
+ files["config.json"] = JSON.stringify({
+ fastRecorder: fastRecorderData,
+ ...extraConfig,
+ });
if (diagData?.log) {
- // structuredClone avoids the stringification round-trip on large logs.
const annotated =
typeof structuredClone === "function"
? structuredClone(diagData.log)
@@ -176,41 +172,53 @@ export const buildDiagnosticZip = async ({
if (hints.length > 0) session.hints = hints;
}
}
- zip.file("sessions.json", JSON.stringify(redactPii(annotated)));
+ files["sessions.json"] = JSON.stringify(redactPii(annotated));
}
if (diagData?.errors) {
- zip.file("errors.json", JSON.stringify(redactPii(diagData.errors)));
+ files["errors.json"] = JSON.stringify(redactPii(diagData.errors));
}
if (diagData?.flags) {
- zip.file("storage-flags.json", JSON.stringify(redactPii(diagData.flags)));
+ files["storage-flags.json"] = JSON.stringify(redactPii(diagData.flags));
}
try {
const trace = await getStartFlowTrace();
if (trace) {
- zip.file("start-flow-trace.json", JSON.stringify(trace));
+ files["start-flow-trace.json"] = JSON.stringify(trace);
}
} catch {}
if (Array.isArray(lifecycleData) && lifecycleData.length > 0) {
- zip.file("lifecycle-log.json", JSON.stringify(lifecycleData));
+ files["lifecycle-log.json"] = JSON.stringify(lifecycleData);
}
if (Array.isArray(perfTimeline) && perfTimeline.length > 0) {
- zip.file("perf-timeline.json", JSON.stringify(perfTimeline));
+ files["perf-timeline.json"] = JSON.stringify(perfTimeline);
}
if (Array.isArray(uploadTelemetry) && uploadTelemetry.length > 0) {
- zip.file(
- "upload-telemetry.json",
- JSON.stringify(redactPii(uploadTelemetry)),
- );
+ files["upload-telemetry.json"] = JSON.stringify(redactPii(uploadTelemetry));
}
- const blob = await zip.generateAsync({ type: "blob" });
const filename = `screenity-diagnostics-${ts}.zip`;
- return { blob, filename };
+ const resp = await chrome.runtime.sendMessage({
+ type: "make-zip",
+ files,
+ filename,
+ });
+ if (!resp?.ok || typeof resp.base64 !== "string") {
+ throw new Error(
+ `make-zip failed: ${resp?.error || "no response from background"}`,
+ );
+ }
+ // base64 over the message channel; structured-clone of ArrayBuffer
+ // is unreliable across MV3 contexts.
+ const bin = atob(resp.base64);
+ const bytes = new Uint8Array(bin.length);
+ for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
+ const blob = new Blob([bytes], { type: "application/zip" });
+ return { blob, filename: resp.filename || filename };
};
diff --git a/src/pages/utils/diagnosticLog.js b/src/pages/utils/diagnosticLog.js
index 16962ad..fd08792 100644
--- a/src/pages/utils/diagnosticLog.js
+++ b/src/pages/utils/diagnosticLog.js
@@ -176,6 +176,20 @@ export const getErrorSnapshot = async () => {
"fastRecorderDisabledAt",
"memoryError",
"recordingAttemptId",
+ // WebCodecs failure/retry telemetry; the rich payload that turns
+ // "my recording failed" into a precise diagnosis (zero-frame vs
+ // configure-failed vs flush-timeout vs HW-encoder-quota).
+ "lastWebCodecsFailureCode",
+ "lastWebCodecsFailureDetail",
+ "lastWebCodecsSwRetry",
+ "lastWebCodecsFailureAt",
+ // Recorder-level stop classification + mid-stream source-end
+ // diagnostic. Cover the non-WebCodecs and "encoder ran fine but
+ // source disappeared" failure shapes.
+ "lastRecorderStopReason",
+ "lastRecorderStopAt",
+ "lastTrackEndEvent",
+ "lastTrackEndedEvent",
];
try {
return await chrome.storage.local.get(keys);
diff --git a/src/pages/utils/perfMarks.js b/src/pages/utils/perfMarks.js
index ba72fc8..aa01d22 100644
--- a/src/pages/utils/perfMarks.js
+++ b/src/pages/utils/perfMarks.js
@@ -1,8 +1,9 @@
-// dev-only perf instrumentation; DCE'd to a no-op in prod via DefinePlugin.
-// usage: perfMark("name", { ... }) or const end = perfSpan("name"); end({ ... })
-// read via console "[perf]" lines, dumpPerf() in BG devtools, or perf-timeline.json.
-
+// Perf instrumentation; enabled in prod too (300-event-per-context
+// cap, 250ms debounced flush) so user diag zips have the full
+// stop→editor timing. Labels are hardcoded; payloads are caller ints/
+// strings. Console "[perf]" lines stay dev-only.
const DEV = process.env.SCREENITY_DEV_MODE === "true";
+const ENABLED = true;
const noop = () => {};
const noopSpan = () => () => {};
@@ -106,14 +107,16 @@ const buildDevImpl = () => {
}
}
localBuf.push(entry);
- try {
- const tag = delta !== null ? `+${delta}ms` : "";
- if (meta) {
- console.debug("[perf]", CTX, label, tag, meta);
- } else {
- console.debug("[perf]", CTX, label, tag);
- }
- } catch {}
+ if (DEV) {
+ try {
+ const tag = delta !== null ? `+${delta}ms` : "";
+ if (meta) {
+ console.debug("[perf]", CTX, label, tag, meta);
+ } else {
+ console.debug("[perf]", CTX, label, tag);
+ }
+ } catch {}
+ }
scheduleFlush();
};
@@ -222,6 +225,54 @@ const buildDevImpl = () => {
refreshSessionId();
+ // Flush localBuf on pagehide so the last ~250ms of marks survive
+ // tab teardown. Also forward to BG since pagehide storage.set is
+ // racey; BG outlives the tab and appends to perfTimeline.<ctx>.
+ const forwardBatchToBg = (batch) => {
+ if (!batch || !batch.length) return;
+ try {
+ if (
+ typeof chrome !== "undefined" &&
+ chrome.runtime &&
+ typeof chrome.runtime.sendMessage === "function"
+ ) {
+ // Fire-and-forget; the SW receives and persists. Wrapped because
+ // sendMessage rejects on no-receiver, which we don't care about
+ // (BG SW may be momentarily asleep on a perf-only message).
+ chrome.runtime
+ .sendMessage({ type: "perf-forward", ctx: CTX, entries: batch })
+ .catch(() => {});
+ }
+ } catch {}
+ };
+ try {
+ if (
+ typeof self !== "undefined" &&
+ typeof self.addEventListener === "function" &&
+ CTX !== "BG"
+ ) {
+ const finalFlush = () => {
+ if (pendingFlush) {
+ clearTimeout(pendingFlush);
+ pendingFlush = null;
+ }
+ // Take a copy of the buffer for BG forwarding *before* flushNow
+ // drains it; otherwise the local in-flight write and the BG
+ // forward compete and one loses.
+ if (localBuf.length) {
+ forwardBatchToBg(localBuf.slice());
+ }
+ flushNow();
+ };
+ self.addEventListener("pagehide", finalFlush);
+ if (typeof document !== "undefined" && document.addEventListener) {
+ document.addEventListener("visibilitychange", () => {
+ if (document.visibilityState === "hidden") finalFlush();
+ });
+ }
+ }
+ } catch {}
+
return {
perfMark: perfMarkImpl,
perfSpan: perfSpanImpl,
@@ -230,9 +281,9 @@ const buildDevImpl = () => {
};
};
-const impl = DEV ? buildDevImpl() : null;
+const impl = ENABLED ? buildDevImpl() : null;
-export const perfMark = DEV ? impl.perfMark : noop;
-export const perfSpan = DEV ? impl.perfSpan : noopSpan;
-export const perfReset = DEV ? impl.perfReset : noop;
-export const getPerfTimeline = DEV ? impl.getPerfTimeline : noopAsyncArr;
+export const perfMark = ENABLED ? impl.perfMark : noop;
+export const perfSpan = ENABLED ? impl.perfSpan : noopSpan;
+export const perfReset = ENABLED ? impl.perfReset : noop;
+export const getPerfTimeline = ENABLED ? impl.getPerfTimeline : noopAsyncArr;
diff --git a/src/pages/utils/startFlowTrace.js b/src/pages/utils/startFlowTrace.js
index 8db18e1..74fbaa4 100644
--- a/src/pages/utils/startFlowTrace.js
+++ b/src/pages/utils/startFlowTrace.js
@@ -1,13 +1,13 @@
-/**
- * Start-flow trace for recording diagnostics.
- *
- * Single bounded object in chrome.storage.local ("startFlowTrace"),
- * overwritten each attempt. No URLs, no page content, no user identity
- * beyond isPro boolean. Error text is truncated and URL-stripped.
- */
+// Start-flow trace for recording diagnostics. One bounded object in
+// chrome.storage.local, overwritten each attempt. No URLs, no page
+// content, no identity beyond isPro.
const STORAGE_KEY = "startFlowTrace";
const MAX_ERR_LEN = 120;
+// Off in prod by default; set globalThis.SCREENITY_DEBUG_RECORDER for support.
+const DEBUG_FLOW =
+ process.env.NODE_ENV !== "production" ||
+ (typeof globalThis !== "undefined" && !!globalThis.SCREENITY_DEBUG_RECORDER);
const sanitize = (str) => {
if (!str) return null;
@@ -66,13 +66,14 @@ export const initStartFlowTrace = async (attemptId, config = {}) => {
/** Write a timestamp checkpoint. Merges extra fields without overwriting others. */
export const traceStep = async (stepName, extra = {}) => {
+ const now = Date.now();
try {
const res = await chrome.storage.local.get(STORAGE_KEY);
const trace = res?.[STORAGE_KEY];
if (!trace) return;
if (trace.t && stepName in trace.t) {
- trace.t[stepName] = Date.now();
+ trace.t[stepName] = now;
}
// Merge extra fields (surface, routing, etc.)
@@ -85,6 +86,29 @@ export const traceStep = async (stepName, extra = {}) => {
}
await chrome.storage.local.set({ [STORAGE_KEY]: trace });
+
+ // Mirror every tick to BG so the unified timeline is visible on
+ // the SW console even when the originating tab is gone. Uses
+ // console.warn because Terser strips log/info/debug in prod.
+ const base = trace.t?.startStreaming;
+ const elapsedMs = base ? now - base : 0;
+ if (DEBUG_FLOW) {
+ try {
+ // eslint-disable-next-line no-console
+ console.warn(
+ `[start-flow T+${elapsedMs}ms] ${stepName}`,
+ Object.keys(extra).length ? extra : "",
+ );
+ } catch {}
+ }
+ try {
+ chrome.runtime.sendMessage({
+ type: "start-flow-tick",
+ event: stepName,
+ data: { ...extra, elapsedMs },
+ ts: now,
+ });
+ } catch {}
} catch {
// best effort
}
diff --git a/src/pages/utils/triggerSupportDownload.js b/src/pages/utils/triggerSupportDownload.js
index 4abc976..5da2282 100644
--- a/src/pages/utils/triggerSupportDownload.js
+++ b/src/pages/utils/triggerSupportDownload.js
@@ -1,7 +1,7 @@
import { buildDiagnosticZip } from "./buildDiagnosticZip";
-// Anchor-tag download of the diag zip so the user can attach it to the
-// Tally form that opens right after. Fire-and-forget.
+// Anchor-tag download of the diag zip; user attaches it to the Tally
+// form that opens right after.
export const triggerSupportDownload = async (opts = {}) => {
try {
const { blob, filename } = await buildDiagnosticZip(opts);
diff --git a/webpack.config.js b/webpack.config.js
index f7f9d8c..f2b44ee 100755
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -25,7 +25,11 @@ const ASSET_PATH = process.env.ASSET_PATH || "/";
if (process.env.SCREENITY_SKIP_ENV) {
// open-source release build, no dotenv
-} else if (isDev) {
+} else if (isDev || process.env.SCREENITY_USE_LOCAL_ENV === "1") {
+ // SCREENITY_USE_LOCAL_ENV=1 lets you do a NODE_ENV=production
+ // (minified, fast) build that still points at localhost; useful
+ // for testing login/auth flows against a local dev server while
+ // keeping the small prod-style bundle.
require("dotenv").config({ path: ".env.local" });
} else {
require("dotenv").config({ path: ".env.production" });
@@ -157,12 +161,13 @@ const htmlPlugins = Object.keys(entryPoints)
"index.html"
);
- // recorder needs keepalive injected BEFORE its main bundle so audio +
- // locks + mediaSession signals are live before the heavy parse hits,
- // otherwise hidden-tab throttling kicks in. manual sort is required:
- // auto sort flips the order based on the chunk graph.
- const isRecorder = entryName === "recorder";
- const chunks = isRecorder
+ // Inject keepalive before the main bundle so audio/locks/mediaSession
+ // signals are live before heavy parse; otherwise hidden-tab throttling
+ // drops encoders to ~5fps for the first 15s. Manual sort because auto
+ // sort flips order based on the chunk graph.
+ const needsKeepalive =
+ entryName === "recorder" || entryName === "cloudrecorder";
+ const chunks = needsKeepalive
? ["recorderkeepalive", entryName]
: [entryName];
@@ -171,7 +176,7 @@ const htmlPlugins = Object.keys(entryPoints)
filename: `${entryName}.html`,
chunks,
cache: true,
- ...(isRecorder ? { chunksSortMode: "manual" } : {}),
+ ...(needsKeepalive ? { chunksSortMode: "manual" } : {}),
};
// Add favicon only for backup page
@@ -278,7 +283,7 @@ const config = {
"react-dom": path.resolve("./node_modules/react-dom"),
"react/jsx-runtime": path.resolve("./node_modules/react/jsx-runtime"),
},
- // Code extensions first — image/font extensions are only needed for explicit imports with extensions
+ // Code extensions first; image/font extensions are only needed for explicit imports with extensions
extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
},
plugins: [
@@ -321,8 +326,14 @@ const config = {
...JSON.parse(content.toString()),
};
- // Strip dev-only origins from production builds.
- if (!isDev && manifest.externally_connectable?.matches) {
+ // Strip dev-only origins from prod builds. The
+ // SCREENITY_USE_LOCAL_ENV escape hatch keeps them in for
+ // build:local (prod-optimized bundle on localhost APIs).
+ if (
+ !isDev &&
+ process.env.SCREENITY_USE_LOCAL_ENV !== "1" &&
+ manifest.externally_connectable?.matches
+ ) {
manifest.externally_connectable.matches =
manifest.externally_connectable.matches.filter(
(m) =>
@@ -369,14 +380,30 @@ if (isDev) {
minimizer: [
new TerserPlugin({
extractComments: false,
+ // Parallelize across CPU cores. Default is os.cpus().length-1
+ // but being explicit makes the intent clear.
+ parallel: true,
terserOptions: {
+ ecma: 2020,
compress: {
+ ecma: 2020,
+ // Two compress passes catches more dead code than one
+ // (DCE during pass 1 enables further inlining in pass 2).
+ // Adds ~10-20% to build time, shaves 3-7% off bundle size.
+ passes: 2,
// strip log/debug/info in prod, keep warn/error for support.
- // drop_console array form removes calls even in callbacks
- // where pure_funcs alone wouldn't.
+ // Array form catches calls inside callbacks too.
drop_console: ["log", "debug", "info"],
pure_funcs: ["console.log", "console.debug", "console.info"],
},
+ mangle: {
+ // Off; would mangle _-prefixed properties only. Measure
+ // before flipping.
+ },
+ format: {
+ // Drop all comments, including @preserve from deps.
+ comments: false,
+ },
},
}),
],