G.711 (PCMU and PCMA) is the 64 kbit/s telephone codec: 8 kHz, one channel, one byte per sample. Use it to talk to SIP gateways and phones, or as the audio codec that needs no native library. The plugin is pure Erlang.

Negotiate it

erlang_webrtc offers Opus first. To get G.711, offer only that:

Pcmu = [C || #{name := <<"PCMU">>} = C <- webrtc_jsep_caps:default_codecs(audio)],
{ok, Pc} = webrtc:new_peer_connection(#{
    codecs => #{audio => Pcmu},
    frames => true,
    codec_plugins => #{audio => {webrtc_codecs, #{}}}
}).

Send

Give 8 kHz mono samples, 20 ms (160 samples) at a time:

ok = webrtc:send_media(Pc, Mid, #{
    format => s16le,
    sample_rate => 8000,
    channels => 1,
    samples => Pcm
}).

Receive

receive
    {webrtc, Pc, {decoded, Mid, #{sample_rate := 8000, channels := 1, samples := Pcm}}} -> play(Pcm)
end.

Options

OptionDefaultMeaning
ptime20Duration of an encoded frame in milliseconds, at most 120
codec_plugin => {webrtc_codecs, #{ptime => 10}}

Notes

  • Samples at another rate or with two channels are an error: resample before you send.
  • More than ptime of samples in one call gives several frames; fewer gives one shorter frame.
  • bitrate and keyframe are accepted and ignored.
  • webrtc_codecs_g711:compress/2 and expand/2 convert samples without a peer connection.