System Architecture

Process Architecture

The system runs as multiple cooperating processes communicating through shared memory:

  Camera 0 ──> capture.exe 0 ──┐
  Camera 1 ──> capture.exe 1 ──┼──> Shared Memory (16GB) ──> videogen.exe ──> viewer.exe
  Camera 2 ──> capture.exe 2 ──┘         ^                        |
                                          |                        v
                               motion.exe 0/1/2              Output Files
                               (reads feeds,                   (H.264 MP4)
                                writes flags)

  controller.exe: launches and monitors all processes

Shared Memory Layout

All processes access a single large shared memory region (~16GB) containing:

Control Region

Feed Data (3 feeds x 3 channels x 520 frames)

Each feed has 3 channels (active, latest, ready) with 520-frame circular buffers. Each frame contains:

Output Data (2 output feeds x 520 frames)

Double-buffered output for the viewer. While one buffer is being written by videogen, the viewer reads the other.

Memory Totals

ComponentCalculationSize
Feed data3 feeds x 3 channels x 520 frames x 2.3MB~10.8 GB
Output data2 feeds x 520 frames x 2.3MB~2.4 GB
Control/metadataHeaders, indices, flags~50 MB
Total~13.3 GB

IPC Mechanism

Processes coordinate through 20 named Windows synchronization objects:

Named Mutexes

Protect concurrent access to shared data:

MutexPurpose
0Current frame index update
1Source thread frame posting wait
2Channel check/increment
3, 4, 5Next-write index for feeds 0, 1, 2
6, 7, 8Motion detection signal for feeds 0, 1, 2
9Output feed and frame count
10Frames-this-channel counter
11Motion info: min type2 frame
12Motion info: movie start
13Motion info: movie end
14Frame builder broadcast
15Index of last frame built
16, 17Viewer shared memory access

Named Events

Data Flow

  1. Capture: Reads YUV422 from USB, converts to YUV420, crops to 540 lines, writes to circular buffer, signals motion
  2. Motion: Frame differencing, adaptive threshold, binary map, flood-fill blob detection, blob merge, trigger evaluation. Writes results to frame headers.
  3. VideoGen: On motion trigger, merges two feeds into composite frame (1920x1080), pipes to FFmpeg for H.264 encoding, optionally uploads
  4. Viewer: Copies frames from output buffer to local memory, renders with SDL3, draws grid line overlays, calculates distance at cursor

Frame Format: YUV420

All frame data is stored as YUV420 planar:

This format halves the data compared to YUV422 and is native to H.264 encoding.

Circular Buffer

Each feed channel has a 520-frame circular buffer (~8.7 seconds at 60fps). The next_write index advances modulo 520. Readers track their own position and must stay within the buffer window or they'll read stale data.

Feed Merge Compositing

Output frames combine two camera feeds vertically:

Display TypeTop 540 linesBottom 540 linesWhen Used
Type 0/1Feed 1 (middle)Feed 0 (top of hill)Jumper in top/middle of hill
Type 2Feed 1 (middle)Feed 2 (bottom of hill)Jumper past top of hill

Design Principles

See Also