WebRTC peer connections on a Mob device, with the same calls on iOS, Android and the host.
The protocol stack is ex_webrtc, with
DTLS (OpenSSL, through ex_dtls) and SRTP (libsrtp2, through ex_libsrtp)
linked into the app as static NIFs. There is no media engine: the app
supplies encoded frames as RTP packets and gets the remote side's RTP back.
That fits one-way streaming (a screen or camera to a viewer, e.g. the H264
mob_screencast produces). Two-way calls want echo cancellation and
adaptive playout, which this plugin doesn't have.
Support
| iOS | Android | Host (mix test) | |
|---|---|---|---|
| Peers, ICE, DTLS-SRTP, RTP in and out | ✓ | ✓ | ✓ with host NIFs, or MobWebrtc.Engine.Fake |
Data channels (ex_sctp, static Rust NIF) | ✓ | ✓ arm64 | ✓, or MobWebrtc.Engine.Fake |
| TURN / STUN | pass :ice_servers | pass :ice_servers | pass :ice_servers |
MobWebrtc.SelfTest has passed on the iOS simulator, an arm64 Android
emulator, an armv7 Android phone and an iPhone; mob's
decisions/2026-11-27-webrtc-engine-split.md has the details.
Signaling
The plugin doesn't carry offers, answers or candidates between peers; the
app does, over whatever channel it has. Descriptions and candidates are
JSON-shaped maps with string keys (%{"type" => "offer", "sdp" => sdp},
%{"candidate" => …, "sdpMid" => …, "sdpMLineIndex" => …}), the shapes a
browser's RTCPeerConnection produces and accepts.
{:ok, peer} = MobWebrtc.start_peer()
{:ok, track} = MobWebrtc.add_track(peer, :video)
{:ok, offer} = MobWebrtc.create_offer(peer)
:ok = MobWebrtc.set_local_description(peer, offer)
# send offer to the remote side; when its answer arrives:
:ok = MobWebrtc.set_remote_description(peer, answer)That trickles: each {:ice_candidate, c} goes across on its own, into
add_ice_candidate/2. Without trickle, wait after set_local_description/2
for {:ice_gathering_state, :complete} (or a deadline), then send
local_description/1, which carries every candidate gathered so far, as
the one offer or answer; the remote side needs no add_ice_candidate/2.
Messages
The peer's owner (the caller of start_peer/1, or :owner) receives
{:mob_webrtc, peer, event}:
{:ice_candidate, candidate}: a local candidate to send to the remote side.{:ice_gathering_state, state}::new,:gatheringor:complete;:completeonce every local candidate is inlocal_description/1.{:connection_state, state}::new,:connecting,:connected,:disconnected,:failedor:closed.{:track, %{id: id, kind: :audio | :video}}: the remote side added a track.{:rtp, track_id, rid, %ExRTP.Packet{}}: a packet on a remote track.{:keyframe_request, track_id}: the remote side asked for a keyframe (RTCP PLI) on one of this peer's tracks.{:data_channel, %{ref: ref, label: label}}: the remote side opened a data channel, before any{:data_channel_state, …}for it.{:data_channel_state, ref, state}: a channel is:connecting,:open,:closingor:closed. A channelcreate_data_channel/3made starts:connectingand reports:openonce the connection is up.{:data, ref, binary}: a message on a data channel.
A peer closes when its owner exits. Monitor the peer to learn when it stops.
Errors
Calls return {:error, reason}, where reason comes from the engine
(ex_webrtc's atoms, e.g. :invalid_state), is :invalid_description or
:invalid_candidate for a malformed map from signaling, or is
{:native, reason} from start_peer/1 when the NIFs didn't load.
status/0 says which.
Summary
Functions
Adds a local track to send on. Returns its id for send_rtp/3.
Opens a data channel to the remote side, ordered and reliable unless
opts say otherwise: ordered: false, max_retransmits: n or
max_packet_life_time: ms (one of the two), protocol: string. A channel
made before the offer is negotiated in it; the remote side learns of it
with {:data_channel, %{ref: ref, label: label}}, and this side's owner
gets {:data_channel_state, ref, :open} when it can carry data.
The engine's statistics for this peer (ex_webrtc's get_stats/1 map).
The local description as it stands, with every local candidate gathered so
far: what to send when not trickling candidates (see Signaling).
{:error, :no_local_description} before set_local_description/2.
Asks the remote sender of a received track for a keyframe (RTCP PLI).
Sends the RTP of a remote track to sink as {:mob_webrtc, peer, {:rtp, …}}
instead of to the owner, so media doesn't pass through a screen. Until a
track is routed its RTP goes to the owner. MobWebrtc.Video and
MobWebrtc.Audio route themselves.
Sends one message on an open data channel, as text (:string, the
default) or bytes (:binary), at most 65,536 bytes.
Sends one H264 access unit (Annex-B, as mob_screencast delivers it) on a
local video track: packetized per RFC 6184 (FU-A for NAL units over 1,200
bytes), sequence numbers kept per track, timestamp_ms on the 90 kHz RTP
clock. Start the peer with video_codec: :h264 so the remote side can't
negotiate a codec these packets aren't.
Sends one RTP packet on a local track. The engine sets the negotiated payload type and SSRC; the caller owns sequence numbers and timestamps.
Starts a peer connection.
A readback for agents and dist RPC: whether the NIFs loaded, whether the shared DTLS certificate is ready, and every open peer with its connection state and per-track packet, byte and frame counts (a frame is a packet with the RTP marker bit set, the end of a video frame).
A MobWebrtc.VideoView element showing a remote video track. Options:
:id (atom or string, unique per screen; the same id goes to
MobWebrtc.Video), :width, :height (points/dp).
Types
@type candidate() :: MobWebrtc.Engine.candidate()
@type channel_ref() :: MobWebrtc.Engine.channel_ref()
@type channel_state() :: :connecting | :open | :closing | :closed
@type connection_state() ::
:new | :connecting | :connected | :disconnected | :failed | :closed
@type description() :: MobWebrtc.Engine.description()
@type event() :: {:ice_candidate, candidate()} | {:connection_state, connection_state()} | {:ice_gathering_state, ice_gathering_state()} | {:track, %{id: track_id(), kind: :audio | :video}} | {:rtp, track_id(), String.t() | nil, ExRTP.Packet.t()} | {:keyframe_request, track_id()} | {:data_channel, %{ref: channel_ref(), label: String.t()}} | {:data_channel_state, channel_ref(), channel_state()} | {:data, channel_ref(), binary()}
@type ice_gathering_state() :: :new | :gathering | :complete
@type peer() :: pid()
@type track_id() :: MobWebrtc.Engine.track_id()
Functions
Adds a local track to send on. Returns its id for send_rtp/3.
On the answering side, called after set_remote_description/2 with the
offer, it takes the offered transceiver of that kind that has no track yet,
so the answer is sendrecv on the offer's m-line: one connection carries
audio both ways.
@spec close(peer()) :: :ok
@spec create_answer(peer()) :: {:ok, description()} | {:error, term()}
@spec create_data_channel(peer(), String.t(), keyword()) :: {:ok, channel_ref()} | {:error, term()}
Opens a data channel to the remote side, ordered and reliable unless
opts say otherwise: ordered: false, max_retransmits: n or
max_packet_life_time: ms (one of the two), protocol: string. A channel
made before the offer is negotiated in it; the remote side learns of it
with {:data_channel, %{ref: ref, label: label}}, and this side's owner
gets {:data_channel_state, ref, :open} when it can carry data.
@spec create_offer(peer()) :: {:ok, description()} | {:error, term()}
The engine's statistics for this peer (ex_webrtc's get_stats/1 map).
@spec local_description(peer()) :: {:ok, description()} | {:error, :no_local_description}
The local description as it stands, with every local candidate gathered so
far: what to send when not trickling candidates (see Signaling).
{:error, :no_local_description} before set_local_description/2.
Asks the remote sender of a received track for a keyframe (RTCP PLI).
Sends the RTP of a remote track to sink as {:mob_webrtc, peer, {:rtp, …}}
instead of to the owner, so media doesn't pass through a screen. Until a
track is routed its RTP goes to the owner. MobWebrtc.Video and
MobWebrtc.Audio route themselves.
@spec send_data(peer(), channel_ref(), binary(), :string | :binary) :: :ok | {:error, term()}
Sends one message on an open data channel, as text (:string, the
default) or bytes (:binary), at most 65,536 bytes.
Errors: {:error, :not_open} before the channel's :open, or once the
connection is :failed or :closed; {:error, :unknown_channel} for a
ref this peer never had or a channel that has closed (a closed channel is
forgotten); {:error, :too_large} over 65,536 bytes. The check is best
effort: a send racing the remote side's close can still be dropped, with
only a log line from ex_webrtc.
@spec send_h264(peer(), track_id(), binary(), non_neg_integer()) :: :ok
Sends one H264 access unit (Annex-B, as mob_screencast delivers it) on a
local video track: packetized per RFC 6184 (FU-A for NAL units over 1,200
bytes), sequence numbers kept per track, timestamp_ms on the 90 kHz RTP
clock. Start the peer with video_codec: :h264 so the remote side can't
negotiate a codec these packets aren't.
@spec send_rtp(peer(), track_id(), ExRTP.Packet.t()) :: :ok
Sends one RTP packet on a local track. The engine sets the negotiated payload type and SSRC; the caller owns sequence numbers and timestamps.
@spec set_local_description(peer(), description()) :: :ok | {:error, term()}
@spec set_remote_description(peer(), description()) :: :ok | {:error, term()}
Starts a peer connection.
Options:
:owner: the process that receives the peer's messages (default: the caller).:ice_servers: e.g.[%{urls: "stun:stun.l.google.com:19302"}](default[]).:video_codec::h264to offer and accept only H264 in packetization mode 1, whatsend_h264/4sends (default: the engine's full list).:engine: aMobWebrtc.Engine(default:config :mob_webrtc, :engine, elseMobWebrtc.Engine.ExWebRTC).
@spec status() :: map()
A readback for agents and dist RPC: whether the NIFs loaded, whether the shared DTLS certificate is ready, and every open peer with its connection state and per-track packet, byte and frame counts (a frame is a packet with the RTP marker bit set, the end of a video frame).
A MobWebrtc.VideoView element showing a remote video track. Options:
:id (atom or string, unique per screen; the same id goes to
MobWebrtc.Video), :width, :height (points/dp).