-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(capture): stop losing the recording when finalization is interrupted #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iOSDevSK
wants to merge
1
commit into
webadderallorg:main
Choose a base branch
from
iOSDevSK:fix/screencapturekit-finalize-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { mkdtemp, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import path from "node:path"; | ||
| import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
|
|
||
| import { classifyMp4Layout, isUnfinalizedMp4, readMp4TopLevelBoxes } from "./mp4Integrity"; | ||
|
|
||
| function box(type: string, size: number, payload = 0): Buffer { | ||
| const header = Buffer.alloc(8 + payload); | ||
| header.writeUInt32BE(size, 0); | ||
| header.write(type, 4, "latin1"); | ||
| return header; | ||
| } | ||
|
|
||
| describe("classifyMp4Layout", () => { | ||
| it("treats a file with a moov index as finalized", () => { | ||
| expect( | ||
| classifyMp4Layout( | ||
| [ | ||
| { type: "ftyp", size: 28 }, | ||
| { type: "mdat", size: 3809329 }, | ||
| { type: "moov", size: 1634 }, | ||
| ], | ||
| 3810991, | ||
| ), | ||
| ).toBe("finalized"); | ||
| }); | ||
|
|
||
| it("flags the interrupted-writer signature as unfinalized", () => { | ||
| expect( | ||
| classifyMp4Layout( | ||
| [ | ||
| { type: "ftyp", size: 28 }, | ||
| { type: "wide", size: 8 }, | ||
| { type: "mdat", size: 0 }, | ||
| ], | ||
| 45989367, | ||
| ), | ||
| ).toBe("unfinalized"); | ||
| }); | ||
|
|
||
| it("keeps a moov-bearing file finalized even when mdat runs to the end", () => { | ||
| expect( | ||
| classifyMp4Layout( | ||
| [ | ||
| { type: "ftyp", size: 28 }, | ||
| { type: "moov", size: 1634 }, | ||
| { type: "mdat", size: 0 }, | ||
| ], | ||
| 4096, | ||
| ), | ||
| ).toBe("finalized"); | ||
| }); | ||
|
|
||
| it("does not guess when the layout is unfamiliar", () => { | ||
| expect(classifyMp4Layout([{ type: "ftyp", size: 28 }], 28)).toBe("unknown"); | ||
| expect( | ||
| classifyMp4Layout( | ||
| [ | ||
| { type: "ftyp", size: 28 }, | ||
| { type: "moof", size: 512 }, | ||
| ], | ||
| 540, | ||
| ), | ||
| ).toBe("unknown"); | ||
| }); | ||
|
|
||
| it("does not classify an empty or unreadable box table", () => { | ||
| expect(classifyMp4Layout([], 1024)).toBe("unknown"); | ||
| expect(classifyMp4Layout([{ type: "mdat", size: 0 }], 0)).toBe("unknown"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("readMp4TopLevelBoxes / isUnfinalizedMp4", () => { | ||
| let dir: string; | ||
|
|
||
| beforeAll(async () => { | ||
| dir = await mkdtemp(path.join(tmpdir(), "recordly-mp4-")); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await rm(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("walks the box table of a finalized file", async () => { | ||
| const file = path.join(dir, "finalized.mp4"); | ||
| await writeFile( | ||
| file, | ||
| Buffer.concat([box("ftyp", 16, 8), box("mdat", 24, 16), box("moov", 16, 8)]), | ||
| ); | ||
|
|
||
| expect(await readMp4TopLevelBoxes(file)).toEqual([ | ||
| { type: "ftyp", size: 16 }, | ||
| { type: "mdat", size: 24 }, | ||
| { type: "moov", size: 16 }, | ||
| ]); | ||
| expect(await isUnfinalizedMp4(file)).toBe(false); | ||
| }); | ||
|
|
||
| it("stops at an open-ended mdat and reports the file as unfinalized", async () => { | ||
| const file = path.join(dir, "interrupted.mp4"); | ||
| await writeFile( | ||
| file, | ||
| Buffer.concat([ | ||
| box("ftyp", 16, 8), | ||
| box("wide", 8), | ||
| box("mdat", 0), | ||
| Buffer.alloc(4096, 7), | ||
| ]), | ||
| ); | ||
|
|
||
| expect(await readMp4TopLevelBoxes(file)).toEqual([ | ||
| { type: "ftyp", size: 16 }, | ||
| { type: "wide", size: 8 }, | ||
| { type: "mdat", size: 0 }, | ||
| ]); | ||
| expect(await isUnfinalizedMp4(file)).toBe(true); | ||
| }); | ||
|
|
||
| it("reports missing files as not-unfinalized so callers keep their fallback", async () => { | ||
| expect(await isUnfinalizedMp4(path.join(dir, "nope.mp4"))).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import fs from "node:fs/promises"; | ||
|
|
||
| export interface Mp4Box { | ||
| type: string; | ||
| size: number; | ||
| } | ||
|
|
||
| export type Mp4Layout = "finalized" | "unfinalized" | "unknown"; | ||
|
|
||
| /** | ||
| * An MP4 written by AVAssetWriter (the macOS capture helper) or by the Windows | ||
| * capture helper only becomes playable once the writer finalizes it: the `mdat` | ||
| * gets its real size and a `moov` index is appended. A helper that dies | ||
| * mid-recording leaves `ftyp` + an open-ended `mdat` and no `moov` — bytes on | ||
| * disk that no player can open. | ||
| * | ||
| * Classification is deliberately conservative: only the exact interrupted-writer | ||
| * signature is reported as `unfinalized`. Anything unexpected is `unknown`, so | ||
| * callers keep their existing behaviour rather than rejecting a file that might | ||
| * be perfectly fine. | ||
| */ | ||
| export function classifyMp4Layout(boxes: Mp4Box[], fileSize: number): Mp4Layout { | ||
| if (boxes.length === 0 || fileSize <= 0) { | ||
| return "unknown"; | ||
| } | ||
|
|
||
| if (boxes.some((box) => box.type === "moov")) { | ||
| return "finalized"; | ||
| } | ||
|
|
||
| const lastBox = boxes[boxes.length - 1]; | ||
| if (lastBox.type === "mdat" && lastBox.size === 0) { | ||
| // size 0 means "this box runs to the end of the file" — the placeholder a | ||
| // writer patches on finish. | ||
| return "unfinalized"; | ||
| } | ||
|
|
||
| return "unknown"; | ||
| } | ||
|
|
||
| /** | ||
| * Reads the top-level box table without loading the file: each header is 8 bytes | ||
| * and points at the next one, so even a multi-gigabyte capture costs a handful of | ||
| * reads. | ||
| */ | ||
| export async function readMp4TopLevelBoxes(filePath: string, maxBoxes = 32): Promise<Mp4Box[]> { | ||
| const handle = await fs.open(filePath, "r"); | ||
| try { | ||
| const { size } = await handle.stat(); | ||
| const boxes: Mp4Box[] = []; | ||
| const header = Buffer.alloc(16); | ||
| let offset = 0; | ||
|
|
||
| while (offset < size && boxes.length < maxBoxes) { | ||
| const { bytesRead } = await handle.read(header, 0, 16, offset); | ||
| if (bytesRead < 8) { | ||
| break; | ||
| } | ||
|
|
||
| const declaredSize = header.readUInt32BE(0); | ||
| const type = header.toString("latin1", 4, 8); | ||
| boxes.push({ type, size: declaredSize }); | ||
|
|
||
| let boxSize = declaredSize; | ||
| if (declaredSize === 1) { | ||
| if (bytesRead < 16) { | ||
| break; | ||
| } | ||
| boxSize = Number(header.readBigUInt64BE(8)); | ||
| } else if (declaredSize === 0) { | ||
| // Runs to end of file — nothing can follow it. | ||
| break; | ||
| } | ||
|
|
||
| if (boxSize < 8) { | ||
| break; | ||
| } | ||
| offset += boxSize; | ||
| } | ||
|
|
||
| return boxes; | ||
| } finally { | ||
| await handle.close(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * True only when the file is positively identified as a capture that was never | ||
| * finalized. Unreadable or unusual files return false so callers fall back to | ||
| * their previous behaviour. | ||
| */ | ||
| export async function isUnfinalizedMp4(filePath: string): Promise<boolean> { | ||
| try { | ||
| const boxes = await readMp4TopLevelBoxes(filePath); | ||
| const { size } = await fs.stat(filePath); | ||
| return classifyMp4Layout(boxes, size) === "unfinalized"; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Return the interrupted-recording failure to the caller.
This branch returns
null, which makesstop-native-screen-recordingreport “No native screen recording is active” when the helper already produced an unfinalized file. The dedicated recovery IPC handler similarly reports that no recoverable output exists.Return
{ success: false, message, unfinalizedPath: candidatePath }from this branch. This preserves the failed-recording state and the raw path without passing the file to muxing or the editor.🧰 Tools
🪛 ast-grep (0.45.0)
[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import type { ChildProcessWithoutNullStreams } from "node:child_process";
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
(detect-child-process-typescript)
🤖 Prompt for AI Agents