Why frames don't go on PubSub

Copy Markdown View Source

A sensor reading is usually small enough to publish and let subscribers deal with it. Camera frames change the cost of that decision. A 1920 × 1080 I420 frame is about 3 MB; at 30 frames per second that is about 93 MB each second. A fan-out registry has no way to make a slow subscriber stop receiving. Even when the BEAM shares the binary's bytes, queued messages retain those bytes, and a stalled consumer can exhaust the node's memory.

BB.Camera.Hub therefore never publishes a frame on BB.PubSub. Camera PubSub messages describe lifecycle and capture state, without frame bytes in payload metadata or errors. Frames pass through a separate bounded interface. This boundary lets a camera serve an estimator, a preview and a recorder without making each consumer's processing speed everybody else's problem.

One capture, several consumers

A camera has one capture shared by everyone watching it. The hub is a tee with three bounds:

  • At ingress, BB.Camera.Hub.deliver/2 is synchronous. The driver waits until the hub accepts each frame before delivering another. Turning this into a cast, or spawning a task for each delivery, would lose that bound.
  • In storage, the hub holds one frame, the latest. Each new frame replaces it whether or not anybody read the old one.
  • At egress, each consumer has at most one outstanding request. The hub sends a frame only to a consumer with demand, and only a sequence newer than the one that consumer last received in this capture session.

A consumer asking quickly waits for capture; it cannot spin by reading the same frame repeatedly. A consumer asking slowly skips intermediate frames and gets the newest when ready. This bounds delivery while preserving a current view. It deliberately does not promise every captured frame to every consumer; recording every frame needs a design that can sustain the capture rate and bound its own storage.

The backpressure is local to these boundaries. It does not stop a hardware camera producing, and it cannot bound data already accumulating in a port or a native callback's mailbox. The driver must control that part of ingress. Likewise, a consumer that requests again before finishing and queues work elsewhere takes responsibility for that queue.

Demand follows the consumer

BB.Camera.Media shapes demand into a blocking pull interface. The direct subscription API exposes it as messages, and BB.Camera.Membrane.Source translates pipeline demand into hub requests. These are different ways to consume the same capture, with the same newest-only rule.

A sensor-nested estimator uses BB.Camera.EstimatorSource, selected by use BB.Camera. The estimator server requests the next input after completing the previous dispatch. An expensive vision algorithm consequently processes current scenes at the rate it can manage, without accumulating old scenes. Its smaller result messages can be published on PubSub for the rest of the robot to use.

Subscription starts capture when the first consumer arrives; releasing the last subscription stops it. Consumers and hub monitor each other, so a dead consumer releases its subscription and a dead camera is observable to its consumers. Consumers are not linked to the camera: a robot stopping should end a stream rather than kill the process serving it.

Why raw pixels

Newest-only delivery requires frames that can be interpreted independently. Dropping a frame from an interframe codec can also invalidate later frames that refer to it. Drivers decode before delivery, and the hub accepts only raw pixel formats such as {:raw, :I420}. Consumers share that decoding work and can read whichever frame survives without needing earlier ones.

Raw bytes do not describe their own layout. BB.Message.Camera.Frame carries actual dimensions and format, and its validation checks that the tightly packed data has exactly the size those fields imply. A declared resolution that differs from what the device negotiated cannot silently describe the wrong buffer.

Why a frame is still a message

Transport policy belongs to the hub, not to the payload type. A frame is an ordinary BB.Message whose payload is BB.Message.Camera.Frame. Its envelope carries the camera's name as frame_id, the capturing node, and capture time in both monotonic and wall-clock nanoseconds. An estimator can therefore measure input age from the same place it does for every other sensor.

This also lets a frame be serialised wherever a message can. Another subsystem may choose to record, bridge or even publish one; that subsystem owns the bounding. The hub never does so. A special non-message frame type would not make fan-out safe, but would force every consumer to learn another timestamp and validation contract.

Capture time matters even when the frame waits before processing. The hub uses it to snapshot the robot's joint configurations from joint history, leaving forward kinematics to consumers that need it. This is an estimate of pose at capture: it cannot include joint reports still in flight, and falls back to current configuration where history cannot answer. It is not a claim of exact physical synchronisation.