Repository Public License
1<!--2ORIX Camera3File: docs/architecture/video-engine.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->78# Video Engine910Status: the writer is tested on the Mac with all three codecs; the full recording flow is tested in the Simulator; **recording from a real camera and microphone has not been tested.**1112## Path of a frame1314```mermaid15sequenceDiagram16 participant C as Camera (data output)17 participant P as FramePipeline18 participant R as VideoRecorder19 participant W as AVAssetWriter20 C->>P: BGRA frame + timestamp21 P->>P: keep as latest (monitor)22 P->>R: appendVideo (every frame, or one per interval)23 R->>W: adaptor.append (if ready, else count a drop)24 Note over C,P: audio buffers follow the same route25 P-->>R: stop26 R->>W: endSession + finishWriting27 R-->>P: result (duration, frames, dropped, size)28```2930## VideoRecorder3132`Packages/ORIXCameraKit/Sources/ORIXImaging/Capture/VideoRecorder.swift`3334| Aspect | Implementation |35|---|---|36| Container | QuickTime `.mov`, movie fragments every 5 s |37| Video | H.264, HEVC or ProRes 422 from BGRA buffers through a pixel-buffer adaptor |38| Bit rate | H.264 and HEVC: `width × height × fps × 0.25` or `× 0.13` as average bit rate, key frame every second. ProRes: not rate-controlled (2.4 bits/pixel is used only to estimate storage) |39| Audio | AAC, 96 kbit/s mono (160 kbit/s if stereo), real-time input |40| Orientation | frames stored as delivered; the track transform carries the rotation |41| Timeline | the session starts at the first video frame; audio before it is ignored |42| Drops | a frame is dropped and **counted** when the encoder is not ready, the append fails, or its timestamp does not advance |43| Retiming | with `timeLapsePlaybackRate`, frame *i* gets the timestamp *i / rate* and audio is discarded |44| Finish | one nominal frame duration is added after the last timestamp; a recording with no frame throws and leaves no file |4546Encoder availability is asked of VideoToolbox (`VTCopyVideoEncoderList`), not assumed from the device model.4748## Why not AVCaptureMovieFileOutput4950`AVCaptureMovieFileOutput` is the simplest way to record on iOS. ORIX Camera uses a video data output and `AVAssetWriter` instead.5152| Reason | Detail |53|---|---|54| One stream for everything | The same BGRA frames feed the monitor (with peaking and zebras drawn on the exact pixels), the histogram and focus analysis, the recorder, and the multi-frame modes. There is one code path to reason about. |55| Retiming | Time-lapse and slow motion are the same recorder with rewritten timestamps. |56| Testability | The writer runs on macOS. Tests record real movies with each codec, decode a frame back, check duration, frame count, audio presence and drop accounting. |57| Crash safety | Movie fragments keep an interrupted recording playable up to the last fragment; the next launch adopts such files into the library. |58| Explicit accounting | Dropped frames are counted by the recorder and reported to the user. |5960**Costs accepted:**6162- **8-bit SDR only.** The pipeline carries 32BGRA. Recording 10-bit HLG or Apple Log needs bi-planar 10-bit buffers through the monitor shader and the writer. Not done in 1.0; the app detects and reports when the camera offers them.63- **More code** than a movie file output, and more responsibility: timestamps, session start, finishing.64- **BGRA conversion** by the capture pipeline has a cost at 4K60 that has not been measured on a device.65- **ProRes from 8-bit BGRA** is valid ProRes 422, but it does not carry more than 8 bits of source precision.6667## Recording flow in the app6869`CameraModel.startRecording`:70711. refuse if free space cannot hold ten seconds at the target rate plus the 200 MB reserve;722. create the recorder in `Incoming/` with the stream's real dimensions, the chosen codec, the measured frame rate and the capture rotation;733. sound is on only with microphone permission and outside time-lapse and slow motion;744. hand the recorder to the pipeline; keep the screen awake.7576`stopRecording` detaches the recorder, finishes it, moves the file into `Media/`, makes a thumbnail, indexes it with its duration and summary, and reports dropped frames or the interruption reason.7778| Kind | Trigger | Retiming |79|---|---|---|80| `video` | Pro Video | none |81| `slowMotion` | Pro Video above 60 fps with "Play back in slow motion" | every frame, 30 fps |82| `timeLapse` | Creative → Time-lapse | one frame per interval, 30 fps |8384## Interruptions8586| Event | Behaviour |87|---|---|88| Session interrupted (call, other app, multitasking, thermal shutdown) | recording stopped and saved, with the reason in the message; a card explains the pause |89| App goes to the background | `suspend()` stops and saves the recording before stopping the session |90| App killed while recording | the fragmented file stays in `Incoming/`; `recoverInterruptedRecordings` adopts it at the next launch if it has a duration |91| System pressure serious or critical | heat badge; a warning at critical; the monitor refreshes at 30 Hz |92| Audio route change | handled by the capture session; no specific code. Untested |9394## Storage estimate9596`ExposureMath.bytesPerSecond` = video bit rate / 8 + 12 000 bytes/s when sound is on. `remainingRecordingTime` divides the free space, minus a 500 MB reserve, by that rate. The format sheet shows megabytes per minute for each codec at the selected format.97