Octoroom.Room behaviour (octoroom v0.1.0)

Copy Markdown View Source

A room module decides who may join a room and who hears whom.

defmodule MyApp.TeamRoom do
  use Octoroom.Room

  @impl true
  def join(_params, socket), do: {:ok, %{user_id: socket.assigns.user_id}}
end

Only join/2 is required. subscriptions/2 defaults to default_subscriptions/2: every other participant's mic, camera and screen share, and every worker output. Both subscriptions/2 and workers/1 must be pure and fast: they run after every room change and read only the %Octoroom.Room{} they're given.

If one raises, throws, exits or returns the wrong shape, the room keeps its last good answer: workers/1 failing leaves the workers as they are, and subscriptions/2 failing for a listener leaves that listener's subscriptions as they are (tracks that stop being published still close). The failure is logged, broadcast as {:error, {:policy, callback}, reason} and emitted as [:octoroom, :policy, :error] telemetry, once each time it starts or changes.

The struct holds the room's id, its participants in join order (each an Octoroom.Participant), its workers (the specs workers/1 last returned) and its assigns.

Publishing

A participant publishes up to one track per source: "mic", "camera", "screen" and "screen_audio", named "<id>/<source>" in the room (mic/1, camera/1, screen/1, screen_audio/1). By default it may publish any of them; a :publish key in the metadata join/2 returns limits it, e.g. %{name: name, publish: ["mic"]} for audio only. The room server refuses any other source. Metadata must be a map, and :publish (or "publish") a list of strings when present; a join with anything else is refused. What a participant publishes right now is in its tracks.

Summary

Callbacks

May this client join, and with what metadata? Runs in the client's channel process.

May sender send worker worker_id a message from its client? Only asked for a sender the worker hears (one of its tracks is among the worker's inputs): others are refused before this runs, so it can only narrow that rule. Without it, everyone the worker hears may. A raise counts as no.

Runs once when the room process starts. Returns the room's assigns. If it raises or returns anything else, the room doesn't start, and the join that started it fails with {:error, {:mount_failed, reason}}.

What should listener hear now?

Which server-side participants should be running now? Each is a worker/3 spec, identified by its id. Octoroom starts and stops workers to match. A spec that comes back with other inputs, outputs or data outputs is updated in place; one with another module or args is restarted.

Functions

Sets a room assign, then reconciles.

A participant's camera track (video).

Every other participant's tracks (mic, camera, screen share and its audio), plus every worker output track. Sources nobody publishes are never pulled, so they cost nothing.

The same audio track, played at value (0.0 to 1.0) in the listener's browser.

Adds the calling process to room room_id as a participant with meta, starting the room if needed. The caller is monitored: when it exits, the participant leaves.

A participant's mic track.

Every participant except participant.

Re-runs the policy, for policies that read state outside the room.

Brings a participant back from its resume data (what the join reply's resume held, signed into a token by the channel): after its connection dropped, after the room server crashed, or after the node restarted.

A participant's screen share (video).

The audio of a participant's screen share, when the browser shares it.

A snapshot of the room: its assigns, and for each participant its metadata, its connection (media), what it publishes (tracks: source => %{status: :pending | :live | :closing, muted: boolean}; mic is the mic's status, or :none) and the tracks it currently hears.

A track by room-scoped name.

Updates a participant's metadata with fun, then reconciles. fun runs in the room server.

A worker spec, for workers/1

Types

t()

@type t() :: %Octoroom.Room{
  assigns: map(),
  id: String.t(),
  participants: [Octoroom.Participant.t()],
  workers: [worker_spec()]
}

worker_spec()

@type worker_spec() :: %{
  id: String.t(),
  module: module(),
  inputs: [String.t()],
  outputs: [String.t()],
  data: [String.t()],
  args: term()
}

Callbacks

join(params, socket)

@callback join(params :: map(), socket :: term()) ::
  {:ok, meta :: map()} | {:error, term()}

May this client join, and with what metadata? Runs in the client's channel process.

message(sender, worker_id, t)

(optional)
@callback message(sender :: Octoroom.Participant.t(), worker_id :: String.t(), t()) ::
  boolean()

May sender send worker worker_id a message from its client? Only asked for a sender the worker hears (one of its tracks is among the worker's inputs): others are refused before this runs, so it can only narrow that rule. Without it, everyone the worker hears may. A raise counts as no.

mount(room_id)

@callback mount(room_id :: String.t()) :: {:ok, map()}

Runs once when the room process starts. Returns the room's assigns. If it raises or returns anything else, the room doesn't start, and the join that started it fails with {:error, {:mount_failed, reason}}.

subscriptions(listener, t)

@callback subscriptions(listener :: Octoroom.Participant.t(), t()) :: [
  Octoroom.Track.t() | String.t()
]

What should listener hear now?

workers(t)

@callback workers(t()) :: [worker_spec()]

Which server-side participants should be running now? Each is a worker/3 spec, identified by its id. Octoroom starts and stops workers to match. A spec that comes back with other inputs, outputs or data outputs is updated in place; one with another module or args is restarted.

Functions

assign(room_id, key, value)

Sets a room assign, then reconciles.

camera(participant)

A participant's camera track (video).

default_subscriptions(listener, room)

Every other participant's tracks (mic, camera, screen share and its audio), plus every worker output track. Sources nobody publishes are never pulled, so they cost nothing.

gain(track, value)

The same audio track, played at value (0.0 to 1.0) in the listener's browser.

join(module, room_id, meta, opts \\ [])

Adds the calling process to room room_id as a participant with meta, starting the room if needed. The caller is monitored: when it exits, the participant leaves.

Returns {:ok, reply, server}, where reply has participant_id, participants, ice_servers, resumed (false) and resume: the data a resume token carries (see resume/4). Metadata that isn't a map, or whose :publish isn't a list of source names, is refused with {:error, {:bad_meta, meta}}.

mic(participant)

A participant's mic track.

others(room, participant)

Every participant except participant.

refresh(room_id)

Re-runs the policy, for policies that read state outside the room.

resume(module, room_id, data, opts \\ [])

Brings a participant back from its resume data (what the join reply's resume held, signed into a token by the channel): after its connection dropped, after the room server crashed, or after the node restarted.

If the room still has the participant, only its process is replaced. If not (the room was restarted), the participant is re-added with the same id and metadata and its Cloudflare session is adopted as it is, so nobody renegotiates. reply.resumed is false when there was nothing to adopt and the client must publish again.

screen(participant)

A participant's screen share (video).

screen_audio(participant)

The audio of a participant's screen share, when the browser shares it.

state(room_id)

A snapshot of the room: its assigns, and for each participant its metadata, its connection (media), what it publishes (tracks: source => %{status: :pending | :live | :closing, muted: boolean}; mic is the mic's status, or :none) and the tracks it currently hears.

track(name)

A track by room-scoped name.

update_meta(room_id, participant_id, fun)

Updates a participant's metadata with fun, then reconciles. fun runs in the room server.

Returns :ok, {:error, :no_participant}, or {:error, reason} when fun raises, throws or exits (reason is the exception, {:throw, value} or {:exit, reason}) or returns something other than a map whose :publish, if present, is a list of source names ({:bad_return, value}). The metadata is then left as it was, and the failure is logged and broadcast as {:error, {:policy, :update_meta}, reason}.

worker(id, module, opts)

A worker spec, for workers/1:

  • inputs: audio tracks the worker hears (mic/1, track/1 or names). An input is connected once its track is live. Workers don't take video yet.
  • outputs: names of the tracks the worker plays. Output names share a namespace with every other track in the room.
  • data: names of data-only outputs, for a worker with something to tell clients but nothing to play (a notetaker, a captioner). They share the same namespace, and subscriptions/2 lists them like tracks, but they carry no media: subscribing to one never touches Cloudflare, and it applies as soon as the participant has joined, before its media is up.
  • args: passed to the worker's init/2.