A server-side participant: a process that hears input tracks and plays output tracks as PCM, with no WebRTC in sight.
defmodule MyApp.DelayEcho do
use Octoroom.Worker, audio: [rate: 16_000, channels: 1]
@impl true
def init(_args, _ctx), do: {:ok, :queue.new()}
@impl true
def handle_audio(_input, pcm, q) do
q = :queue.in(pcm, q)
if :queue.len(q) > 100 do
{{:value, out}, q} = :queue.out(q)
{:ok, q, play: {:default, out}}
else
{:ok, q}
end
end
endA room starts workers from its workers/1 callback (see
Octoroom.Room.worker/3). audio: picks the PCM format the worker sees,
s16le at a rate dividing 48 kHz and 1 or 2 channels (default 48 kHz
stereo, the room's own format). The worker plays audio in the same format
unless output: gives another, for a worker that hears at one rate and
speaks at another:
use Octoroom.Worker, audio: [rate: 16_000, channels: 1, output: [rate: 24_000]]Callbacks return {:ok, state} or {:ok, state, actions}, where actions
is a keyword list of:
play: {output, pcm}: queuepcmon an output track. Outputs are paced to real time, so a worker can hand over audio faster than it plays.:defaultis the worker's first output.flush: output: drop that output's queued, unplayed audio (barge-in). The worker then getshandle_info({:octoroom, :flushed, output, dropped_ms}, state): how many ms of audio were dropped (nil if the output's outlet wasn't running), so what it queued minusdropped_msis what listeners heard, give or take the frame on its way.emit: event: broadcast{:worker_event, worker_id, event}to the room'sscope: :roomsubscribers (seeOctoroom.subscribe/2). Any term; it never reaches clients.notify: {output, type, data}: an event for clients. It goes to the participants subscribed tooutput(an audio or data output;:defaultis the first of either): to their clients as aworker_eventand to theirscope: {:participant, id}subscribers as{:worker_notify, worker_id, output, type, data}, and toscope: :roomsubscribers.typeis a string anddataa map that encodes as JSON; anything else raises.notify: {output, type, data, participant_id}: the same, for one participant only, and not toscope: :roomsubscribers. Dropped if that participant isn't subscribed tooutput. For catching up a participant that just subscribed.presence: %{state: string | nil, attributes: map}: what clients show for this worker, replacing what it set before (missing keys reset). It goes to everyone subscribed to any of its outputs, now and as they subscribe. After a restart,stateis nil until the worker sets it again;attributesare kept.save: term: keeptermin the store (Octoroom.Store) as what this worker would need to carry on, such as its conversation so far. Each save replaces the last. The worker's next start in the same room gets it asctx.saved: after a crash, a change of args, or the node restarting mid-call. It's deleted when the room stops the worker becauseworkers/1no longer lists it, and when the room stops because its last participant left. Keep it small: it's written on every save.
handle_info/2 also receives:
{:octoroom, :inputs, names},{:octoroom, :outputs, names}and{:octoroom, :data, names}when the room changes the worker's inputs, outputs or data outputs; the worker keeps running. A change of module or args restarts it.{:octoroom, :subscribed, participant_id, output}and{:octoroom, :unsubscribed, participant_id, output}as participants start and stop receiving one of its outputs (an audio subscription counts once its pull is open). After a restart, the worker gets:subscribedfor everyone already subscribed.
A worker that crashes is restarted by its room, with backoff. Its output tracks stay published while it restarts, so listeners hear a gap rather than a renegotiation.
Summary
Functions
What worker id in room room_id last saved (the save: action), or
nil. For apps that show what a worker kept, such as a conversation, while
the room is down or before its workers have started again.
Sends message to a running worker's handle_info/2, for example from a
LiveView. Returns :ok, or {:error, :no_worker}.
The pid of worker id in room room_id, or nil.
Types
@type actions() :: [ play: {String.t() | :default, binary()}, flush: String.t() | :default, emit: term(), notify: {String.t() | :default, String.t(), map()} | {String.t() | :default, String.t(), map(), String.t()}, presence: %{ optional(:state) => String.t() | nil, optional(:attributes) => map() }, save: term() ]
@type state() :: term()
@type video_frame() :: %{ data: binary(), mime_type: String.t(), width: pos_integer() | nil, height: pos_integer() | nil, source: String.t(), participant_id: String.t() }
Callbacks
@callback handle_video(input :: String.t(), frame :: video_frame(), state()) :: result()
@callback init(args :: term(), ctx :: Octoroom.Worker.Context.t()) :: result() | {:stop, reason :: term()}
Functions
What worker id in room room_id last saved (the save: action), or
nil. For apps that show what a worker kept, such as a conversation, while
the room is down or before its workers have started again.
Sends message to a running worker's handle_info/2, for example from a
LiveView. Returns :ok, or {:error, :no_worker}.
The pid of worker id in room room_id, or nil.