# `BB.LiveView.Camera`
[🔗](https://github.com/beam-bots/bb_liveview/blob/main/lib/bb/live_view/camera.ex#L5)

The slice of `bb_camera` and `bb_camera_webrtc` the dashboard consumes, and
the lookups built on it.

A camera is an ordinary `BB.Sensor` callback module that also implements
`BB.Camera`. Video never travels over BB PubSub or the LiveView socket —
PubSub carries only lifecycle events (capture begin/end) and periodic
capture telemetry, and the socket carries only the WebRTC signalling that
sets up a stream. The pixels go from the robot to the browser over WebRTC.

## Watching

`watch/3` starts streaming one camera to one browser on behalf of the calling
process, and returns a `t:watch/0`. The stream then sends the caller
messages whose shape is the implementation's business. Pass each one to
`stream_message?/1` to learn whether it belongs to a stream, and to
`stream_event/2` to learn what it means for a given watch:

  * `{:signal, message}` — signalling bound for the browser, `message`
    being a JSON-shaped map (`%{"type" => "sdp_offer", "data" => ...}`);
  * `:ended` — the camera stopped, the browser's connection failed, or the
    pipeline went down. Watching again starts a fresh stream;
  * `nil` — the message belongs to some other watch.

`signal/2` hands the browser's replies back, and `unwatch/1` stops a stream,
as does the caller exiting.

## Annotations

An estimator nested under a camera publishes beneath the camera's topic, and
a payload that implements `bb_camera`'s `BB.Camera.Annotation` protocol is
drawn over the camera's video as a layer. `annotation/1` asks the
implementation whether a payload is one, and what to draw, as plain data -
so nothing here knows any payload type, or `bb_camera`'s structs.
`time_to_live/3` says how much longer a layer has, measured from the
message's capture time.

## Optional dependencies

Neither `bb_camera` nor `bb_camera_webrtc` is required. Every call goes
through `implementation/0`, which is `BB.LiveView.Camera.BBCamera` by
default and reaches both packages only via `apply/3` behind
`Code.ensure_loaded?/1`. With no camera package installed `discover/1`
answers `[]` for every robot, which is the whole truth: a robot cannot
declare a camera without the package, because `use BB.Camera` would not
compile. With `bb_camera` but no `bb_camera_webrtc` cameras are listed but
not previewable.

Point the seam elsewhere to supply a different source — the tests drive the
dashboard with a double this way:

    config :bb_liveview, :camera, MyCameraSource

# `annotation`

```elixir
@type annotation() :: %{
  width: pos_integer(),
  height: pos_integer(),
  lifetime: non_neg_integer() | :infinity,
  primitives: [map()]
}
```

A layer to draw over a camera's video, as data a browser can be handed.

Coordinates are pixels of a `width` by `height` frame, with the origin at
the top-left corner of the top-left pixel. `lifetime` is in nanoseconds.
Each primitive is a map with a `"kind"` of `"points"`, `"circle"` or
`"text"` and the fields of `bb_camera`'s primitive of that name, points as
`[x, y]` lists.

# `camera`

```elixir
@type camera() :: %{
  name: atom(),
  path: [atom()],
  topic: [atom()],
  module: module(),
  attached_to: {:link, atom()} | {:joint, atom()} | :robot,
  status: :capturing | :running | :declared,
  stream: stream() | nil,
  previewable?: boolean()
}
```

What the dashboard is allowed to know about a camera.

Narrower than `BB.Camera.Info` on purpose. `Info` already omits the DSL
options a camera was declared with — device paths, URLs, credentials — and
this drops the driver-supplied `metadata` map too, so nothing a driver author
chose to stash can reach a browser by being rendered accidentally.

# `stream`

```elixir
@type stream() :: %{
  format: atom() | {:raw, atom()},
  width: pos_integer(),
  height: pos_integer(),
  frame_rate: float() | nil
}
```

# `stream_event`

```elixir
@type stream_event() :: {:signal, map()} | :ended | nil
```

What a stream's message means for one watch.

# `watch`

```elixir
@type watch() :: term()
```

A stream being watched. Opaque: only the implementation looks inside.

# `annotation`

```elixir
@callback annotation(payload :: struct()) :: annotation() | nil
```

The layer to draw for a message payload, or `nil` if it isn't drawn.

# `available?`

```elixir
@callback available?() :: boolean()
```

Whether a camera implementation is installed.

# `discover`

```elixir
@callback discover(robot :: module()) :: [struct()]
```

Every camera declared by a robot, running or not.

# `signal`

```elixir
@callback signal(watch(), message :: map()) :: :ok
```

Hands a validated signalling message from the browser to a stream.

# `stream_event`

```elixir
@callback stream_event(watch(), message :: term()) :: stream_event()
```

What `message`, received by the watching process, means for `watch`.

# `stream_message?`

```elixir
@callback stream_message?(message :: term()) :: boolean()
```

Whether `message`, received by the watching process, came from a stream.

# `unwatch`

```elixir
@callback unwatch(watch()) :: :ok
```

Stops a stream started by `watch/3` from the same process. Afterwards the
caller receives nothing more about it. Harmless on one that has ended.

# `watch`

```elixir
@callback watch(robot :: module(), camera :: atom(), opts :: keyword()) ::
  {:ok, watch()} | {:error, term()}
```

Starts streaming `camera` to a browser on behalf of the calling process,
which becomes the stream's signalling peer, until it unwatches or exits.

# `webrtc_available?`

```elixir
@callback webrtc_available?() :: boolean()
```

Whether cameras can be streamed to a browser.

# `annotation`

```elixir
@spec annotation(term()) :: annotation() | nil
```

The layer to draw over a camera's video for a message payload, or `nil` for
a payload that isn't drawn - including every payload when no camera package
is installed.

# `available?`

```elixir
@spec available?() :: boolean()
```

Whether a camera implementation is installed.

`false` means no camera package, which the dashboard renders as an
explanation rather than an error.

# `discover`

```elixir
@spec discover(module()) :: [camera()]
```

Every camera declared by `robot_module`, sanitised for display.

Answers `[]` for anything that isn't a robot, and for every robot when no
camera package is installed.

# `implementation`

```elixir
@spec implementation() :: module()
```

The module implementing the camera contract.

# `label`

```elixir
@spec label(camera()) :: String.t()
```

A human-readable name for a camera, including where it is attached.

# `signal`

```elixir
@spec signal(watch(), map()) :: :ok
```

Hands a signalling message from the browser to a stream.

Only an SDP answer or an ICE candidate is passed on. Anything else — the
`null` or empty candidate a browser sends when it has finished gathering, or
a message that isn't signalling at all — is dropped here rather than handed
to a WebRTC sink that would crash on it.

# `stream_event`

```elixir
@spec stream_event(watch(), term()) :: stream_event()
```

What `message`, received by the watching process, means for `watch`.

# `stream_message?`

```elixir
@spec stream_message?(term()) :: boolean()
```

Whether `message`, received by the watching process, came from a camera
stream — so a LiveView can pass it on without knowing what it looks like.

# `time_to_live`

```elixir
@spec time_to_live(non_neg_integer() | :infinity, integer(), integer()) ::
  integer() | :infinity
```

How many milliseconds a layer has left to live, given its lifetime in
nanoseconds, its message's capture time and the time now, both from
`System.monotonic_time(:nanosecond)`.

Zero or less means the layer has already expired. A browser can't compare
its clock with the BEAM's, so this is what it is told, and it counts down
from when it hears.

Both times must come from this node's clock. A message stamped by another
node carries that node's `monotonic_time`, so it has no time to live here,
and the Cameras component drops it before asking.

    iex> BB.LiveView.Camera.time_to_live(250_000_000, 1_000_000_000, 1_100_000_000)
    150

    iex> BB.LiveView.Camera.time_to_live(250_000_000, 1_000_000_000, 1_300_000_000)
    -50

    iex> BB.LiveView.Camera.time_to_live(:infinity, 1_000_000_000, 9_000_000_000)
    :infinity

# `unwatch`

```elixir
@spec unwatch(watch()) :: :ok
```

Stops a stream. Afterwards the caller receives nothing more about it.
Harmless on one that has already ended.

# `watch`

```elixir
@spec watch(module(), atom(), keyword()) :: {:ok, watch()} | {:error, term()}
```

Starts streaming `camera_name` to a browser, on behalf of the calling
process. See "Watching" above.

## Options

  * `:ice_servers` — STUN and TURN servers for browsers that can't reach the
    robot directly. None by default.

# `webrtc_available?`

```elixir
@spec webrtc_available?() :: boolean()
```

Whether cameras can be streamed to a browser — that is, whether
`bb_camera_webrtc` is installed.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
