Packages

WebRTC peers on Mob devices: ex_webrtc with ex_dtls and ex_libsrtp linked as static NIFs.

Current section

Files

Jump to
mob_webrtc README.md
Raw

README.md

# mob_webrtc

WebRTC peer connections for [Mob](https://hexdocs.pm/mob) apps:
[`ex_webrtc`](https://hexdocs.pm/ex_webrtc) running on the device's BEAM, with
its DTLS (`ex_dtls`, OpenSSL) and SRTP (`ex_libsrtp`, libsrtp2) NIFs linked into
the app statically. One Elixir API on iOS, Android and the host.

Video is RTP in and out: the app hands it encoded frames as RTP packets (the
H264 [`mob_screencast`](https://hexdocs.pm/mob_screencast) produces, for
example) and incoming H264 is decoded into `MobWebrtc.VideoView`. Audio is a
media path of its own: `MobWebrtc.Audio` encodes the microphone with Opus
(through `mob_audio_io`, whose default `:voice` preset is the platform's
echo-cancelling input) and plays the remote side through an adaptive playout
buffer. Data channels carry messages. See the design records in mob,
`decisions/2026-11-27-webrtc-engine-split.md` and
`decisions/2026-11-27-webrtc-media-plane-in-plugin.md`.

## Install

```elixir
# mix.exs
{:mob_webrtc, "~> 0.1"}

# mob.exs (mob_audio_io comes with mob_webrtc; MobWebrtc.Audio plays and records through it)
config :mob, :plugins, [:mob_webrtc, :mob_audio_io]

# mob.exs: ex_sctp's Rustler crate, linked as a static NIF. Every peer needs
# it (ex_webrtc sets up SCTP in each one), not only data channels: without
# this line start_peer/1 answers {:error, {:native, status}}.
config :mob_dev,
  static_nifs: [%{module: :ex_sctp, init: "nif_init", archs: [:all]}]
```

Then `mix mob.plugin.trust mob_webrtc` and `mix mob.deploy --native`.

ex_webrtc compiles its data-channel code only when `ExSCTP` is there at its
compile time: in a project that built ex_webrtc before it had ex_sctp, run
`mix deps.compile ex_webrtc --force` once.

ex_sctp's crate (rustler 0.34) exports its init function as plain
`nif_init`, so it can't share an app with another Rustler crate that does the
same: iOS fails the link with a duplicate symbol and Android could link only
one of them.

The native build needs:

- **mob_dev 0.7.33 or later** in the host app. Both NIFs carry
  unifex-generated code with the same global names, so each is linked into one
  object that exports only its init symbol (`cpp_archive` `isolate_symbols`),
  and the iOS link check has to accept the audio NIF's calls into
  mob_audio_io's C interface (`mai_register`). Older mob_dev fails the link.
- **The prebuilt bundle** (libssl, libsrtp2 and libopus for every target ABI,
  plus headers). mob_dev downloads it from this repo's `native-v0.2.0` release
  and checks the SHA-256 pinned in `priv/mob_plugin.exs`.
  `scripts/build_prebuilt.sh` rebuilds it reproducibly; to build against a
  local bundle while working on the plugin, set
  `MOB_PLUGIN_PREBUILT_DIR=<dir with the tarball>`.
- **OpenSSL for the host build.** `ex_dtls` compiles its host NIF at
  `mix compile` through `pkg-config openssl`.
- **Rust**, with the targets of each platform you build:
  `rustup target add aarch64-apple-ios aarch64-apple-ios-sim
  aarch64-linux-android armv7-linux-androideabi x86_64-linux-android`.

## Use

```elixir
{:ok, peer} = MobWebrtc.start_peer(ice_servers: [%{urls: "stun:stun.l.google.com:19302"}])
{: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 over your signaling channel, then:
:ok = MobWebrtc.set_remote_description(peer, answer)

def handle_info({:mob_webrtc, peer, {:ice_candidate, candidate}}, socket) # -> send to remote
def handle_info({:mob_webrtc, peer, {:connection_state, :connected}}, socket)
def handle_info({:mob_webrtc, peer, {:keyframe_request, track}}, socket)  # e.g. MobScreencast.request_keyframe()

MobWebrtc.send_rtp(peer, track, %ExRTP.Packet{...})
```

Descriptions and candidates are JSON-shaped maps with string keys, the shapes
a browser's `RTCPeerConnection` uses, so signaling can pass them through. The
full message and error list is in `MobWebrtc`'s moduledoc.

## Support

| | iOS | Android | Host |
|--|--|--|--|
| Peers, ICE, DTLS-SRTP, RTP | ✓ | ✓ | ✓ with host NIFs |
| Data channels (`ex_sctp`) | ✓ | ✓ | ✓ |
| Opus audio (`MobWebrtc.Audio`) | ✓ | ✓ | ✗ (`{:error, :nif_not_loaded}`) |
| H264 decode into `MobWebrtc.VideoView` | ✓ (VideoToolbox) | ✓ (MediaCodec) | ✗ |

## For agents

- `MobWebrtc.status/0` over dist: NIF load state, the shared certificate, and
  every peer's connection state and per-track packet, byte and frame counts.
- `MobWebrtc.SelfTest` (`mix mob.selftest`): NIFs loaded, DTLS-SRTP handshake,
  SRTP round trip, and a two-peer loopback over the device's real UDP.
- `MobWebrtc.Audio.stats/1`: Opus packets encoded and decoded, frames played,
  underruns, and the level and dominant frequency (`rx_freq_hz`) of the last
  second received, so a `source: {:tone, hz}` sender can be heard and proved.
- `MobWebrtc.Engine.Fake` for host tests (`start_peer(engine: MobWebrtc.Engine.Fake)`);
  `MobWebrtc.Engine.Fake.emit/2` plays the remote side.
- Logs are prefixed `[mob_webrtc]`.
- `:mob_webrtc_video_nif.video_stats/1` is the decoder's own readback (frames
  in, decoded, dropped, decode errors, decoded size, whether it wants a
  keyframe). `webrtc_spike/viewer/video_push.exs` pushes an H264 file into the
  spike's `VideoTestScreen` over dist and reads it back. Android native views
  get no element frames yet (MOB-534), so the Android view logs where its
  picture landed: `[mob_webrtc] video <id>: surface at x,y wxh px`.

## Repository

- `lib/`, `priv/`: the plugin.
- `webrtc_spike/`: a Mob host app used to build and verify the plugin on devices.
- `scripts/build_prebuilt.sh`: the prebuilt native bundle.
- `str0m_probe/`: a compile check of `str0m` for the mobile targets (beads
  `webrtc-a9t.2`, parked).
- `.beads/`: issue tracker (`bd list`); epic `webrtc-a9t`.