MobWebrtc (mob_webrtc v0.1.0)

Copy Markdown View Source

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

iOSAndroidHost (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 / STUNpass :ice_serverspass :ice_serverspass :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, :gathering or :complete; :complete once every local candidate is in local_description/1.
  • {:connection_state, state}: :new, :connecting, :connected, :disconnected, :failed or :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, :closing or :closed. A channel create_data_channel/3 made starts :connecting and reports :open once 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

candidate()

@type candidate() :: MobWebrtc.Engine.candidate()

channel_ref()

@type channel_ref() :: MobWebrtc.Engine.channel_ref()

channel_state()

@type channel_state() :: :connecting | :open | :closing | :closed

connection_state()

@type connection_state() ::
  :new | :connecting | :connected | :disconnected | :failed | :closed

description()

@type description() :: MobWebrtc.Engine.description()

event()

@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()}

ice_gathering_state()

@type ice_gathering_state() :: :new | :gathering | :complete

peer()

@type peer() :: pid()

track_id()

@type track_id() :: MobWebrtc.Engine.track_id()

Functions

add_ice_candidate(peer, candidate)

@spec add_ice_candidate(peer(), candidate()) :: :ok | {:error, term()}

add_track(peer, kind)

@spec add_track(peer(), :audio | :video) :: {:ok, track_id()} | {:error, term()}

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.

close(peer)

@spec close(peer()) :: :ok

create_answer(peer)

@spec create_answer(peer()) :: {:ok, description()} | {:error, term()}

create_data_channel(peer, label, opts \\ [])

@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.

create_offer(peer)

@spec create_offer(peer()) :: {:ok, description()} | {:error, term()}

get_stats(peer)

@spec get_stats(peer()) :: map()

The engine's statistics for this peer (ex_webrtc's get_stats/1 map).

local_description(peer)

@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.

request_keyframe(peer, track_id)

@spec request_keyframe(peer(), track_id()) :: :ok

Asks the remote sender of a received track for a keyframe (RTCP PLI).

route(peer, track_id, sink)

@spec route(peer(), track_id(), pid()) :: :ok

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.

send_data(peer, ref, data, type \\ :string)

@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.

send_h264(peer, track_id, access_unit, timestamp_ms)

@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.

send_rtp(peer, track_id, packet)

@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.

set_local_description(peer, description)

@spec set_local_description(peer(), description()) :: :ok | {:error, term()}

set_remote_description(peer, description)

@spec set_remote_description(peer(), description()) :: :ok | {:error, term()}

start_peer(opts \\ [])

@spec start_peer(keyword()) :: {:ok, peer()} | {: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: :h264 to offer and accept only H264 in packetization mode 1, what send_h264/4 sends (default: the engine's full list).
  • :engine: a MobWebrtc.Engine (default: config :mob_webrtc, :engine, else MobWebrtc.Engine.ExWebRTC).

status()

@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).

video_view(opts)

@spec video_view(keyword()) :: map()

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).