erlang_webrtc

A WebRTC protocol stack in pure Erlang for OTP 29: ICE (full and lite) with TURN and mDNS candidates, DTLS 1.3 and 1.2 with DTLS-SRTP, SRTP/SRTCP, RTP/RTCP with retransmission, feedback, simulcast and congestion control, SCTP over DTLS with data channels, and JSEP negotiation on structured SDP. No NIFs and no runtime dependency beyond OTP (crypto, public_key).

Use it to build servers that talk to browsers and other WebRTC stacks: SFUs, media gateways, SIP to WebRTC bridges, data channel services, test peers. Those applications are not part of the library; it gives you peer connections and leaves signaling, codecs and routing to you.

Status: the transport stack works end to end against Chromium, Firefox, Pion, aiortc, GStreamer, pion/turn, OTP ssl and OpenSSL; see the summary and known gaps at the top of docs/COMPLIANCE.md. What is implemented and tested is tracked requirement by requirement in docs/COMPLIANCE.md; which independent implementations it has been tested against is in docs/INTEROP.md.

Build and test

rebar3 compile
rebar3 eunit            # conformance tests
rebar3 proper           # properties
rebar3 ct --dir test/e2e
make test-interop       # needs the adapters, see docs/TESTING.md
rebar3 dialyzer

Answer a browser offer

You receive an offer from a browser through your signaling channel and answer it.

{ok, Pc} = webrtc:new_peer_connection(#{ice_servers => [#{urls => [<<"stun:stun.l.google.com:19302">>]}]}),
ok = webrtc:set_remote_description(Pc, offer, OfferSdp),
{ok, AnswerSdp} = webrtc:create_answer(Pc, #{}),
ok = webrtc:set_local_description(Pc, answer, AnswerSdp),
%% send AnswerSdp to the browser

Events arrive in the owner process (the caller by default):

loop(Pc) ->
    receive
        {webrtc, Pc, {ice_candidate, Cand, Mid}} ->
            %% send Cand and Mid to the browser
            loop(Pc);
        {webrtc, Pc, {datachannel_open, Id, Label, _Proto, _Opts}} ->
            io:format("channel ~s open~n", [Label]),
            loop(Pc);
        {webrtc, Pc, {channel_message, Id, text, Text}} ->
            ok = webrtc:send_data(Pc, Id, text, <<"echo: ", Text/binary>>),
            loop(Pc);
        {webrtc, Pc, {rtp, Mid, Packet}} ->
            loop(Pc);
        {webrtc, Pc, {connection_state, closed}} ->
            ok;
        {webrtc, Pc, _} ->
            loop(Pc)
    end.

Trickled candidates from the browser:

ok = webrtc:add_ice_candidate(Pc, <<"candidate:1 1 udp 2122260223 192.0.2.10 54321 typ host">>).

Use STUN and TURN servers by URI

You give the servers as a browser would, with stun:, turn: and turns: URIs. Names are resolved when the peer connection starts.

{ok, Pc} = webrtc:new_peer_connection(#{ice_servers => [
    #{urls => [<<"stun:stun.l.google.com:19302">>]},
    #{urls => [<<"turn:turn.example.com?transport=udp">>, <<"turns:turn.example.com">>],
      username => <<"user">>, credential => <<"secret">>}
]}).

Documentation

Start here:

Guides, task by task:

Reference and internals:

Project:

License

Apache 2.0, see LICENSE.