Beam Bots Logo

bb_camera_webrtc

CI License: Apache 2.0 Hex version badge Hexdocs badge REUSE status Ask DeepWiki

Streams Beam Bots cameras to browsers over WebRTC, encoding each camera once however many people are watching. It is what bb_liveview uses for its camera previews, and the piece to reach for if you are putting a camera in a web page of your own.

signaling = Membrane.WebRTC.Signaling.new()
{:ok, ref} = BB.Camera.WebRTC.watch(MyRobot, :eye, signaling)

How it works

The first viewer of a camera starts a Membrane pipeline for it — the camera's raw frames from BB.Camera.Membrane.Source, an H.264 encoder tuned for latency, and a WebRTC sink per viewer — and the last viewer to leave stops it, which releases the camera. A second viewer costs network, not CPU.

Measured over Tailscale at 640x480 and 30fps, from a frame being made on the robot to it being shown in the browser: 68ms median, 66–69ms in all. That is a best case — the test frames were flat and compress to nothing — but it is the pipeline's own cost: a real camera adds its sensor's delay, and a real scene takes longer to encode and send.

Requirements

Installation

def deps do
  [
    {:bb_camera_webrtc, "~> 0.1.0"},

    # Membrane's `ratio` pins `decimal` below what `bb` wants, and below the
    # versions that fix its DoS advisories. `ratio` treats `decimal` as
    # optional, so overriding is safe. Mix only honours this from your own
    # `mix.exs`.
    {:decimal, "~> 3.0", override: true}
  ]
end

In the browser

assets/player.js is a Phoenix LiveView hook that plays the stream in a <video> element. Bundle it with your own assets:

import { createPlayerHook } from "../../deps/bb_camera_webrtc/assets/player.js";
const Hooks = { CameraPlayer: createPlayerHook() };

Licence

Apache 2.0. See LICENSES.