# `MobWebrtc.Audio`
[🔗](https://github.com/GenericJam/mob_webrtc/blob/main/lib/mob_webrtc/audio.ex#L1)

Opus audio between the device's audio stream and a peer's RTP.

    # Send a 440 Hz tone and play what the other side sends:
    {:ok, track} = MobWebrtc.add_track(peer, :audio)
    {:ok, audio} =
      MobWebrtc.Audio.start_link(peer: peer, send_track: track,
                                 receive_track: remote_track, source: {:tone, 440})

Options:

  * `:peer` (required).
  * `:send_track`: a local audio track to send on. Sending also needs a
    `:source`.
  * `:source`: `:mic` (the microphone, through a duplex stream with echo
    cancellation; needs the microphone permission) or `{:tone, hz}` (a sine
    instead of the microphone, so the receiver can prove what it heard).
    Omitted: nothing is sent.
  * `:muted`: start muted (default `false`; see `set_muted/2`).
  * `:receive_track`: a remote audio track to play. It is routed to this
    process (`MobWebrtc.route/3`). Any other audio track routed here plays
    too, e.g. another peer's in a loopback.
  * `:bitrate`: Opus bits per second (default 32 000).
  * `:mic_preset`: the input preset for `source: :mic` (`MobAudioIo.open/3`'s
    `:preset`; default `:voice`, which brings echo cancellation and noise
    suppression). `:performance` or `:unprocessed` capture with less delay
    and no echo cancellation, so a speaker near the microphone feeds back.

The audio never passes through the BEAM as PCM: `mob_webrtc_audio_nif` is a
`mob_audio_io` processor (`"mob_webrtc_audio"`). With `source: :mic` this
process opens a `:duplex` stream (`preset: :voice` unless `:mic_preset`
says otherwise); otherwise an `:output`
stream, which needs no permission (a tone is paced by the output's clock).
The NIF hands back one Opus packet per 20 ms, which goes out as RTP (48 kHz
timestamps, sequence numbers of its own); each received RTP payload is
decoded into the stream's playout buffer.

The NIF's state is one per device (one encoder, one playout buffer), so
this process is too: it registers as `MobWebrtc.Audio`, and a second
`start_link/1` answers `{:error, {:already_started, pid}}`.

`start_link/1` answers `{:error, :nif_not_loaded}` off the device and
`{:error, reason}` when the stream doesn't open (`MobAudioIo.open/3`'s
reasons, e.g. `:permission`); a caller doesn't exit with it.

`stats/1` is the NIF's readback (`mob_webrtc_audio_nif:audio_stats/0`:
packets encoded and decoded, frames played, underruns, the playout
buffer's depth (`playout_ms`, `playout_min_ms` over the last second, held
near `playout_target_ms`, which follows the arrival jitter (`jitter_ms`)
over the last 30 s; `drained_frames` were dropped to get it there),
and the level and dominant frequency of the last second received) plus the
stream's `MobAudioIo.status/1` and this process's RTP counters. The NIF's
counters start from zero when this process opens.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `set_muted`

```elixir
@spec set_muted(pid(), boolean()) :: :ok
```

Mutes or unmutes what this process sends. Muted, every 20 ms packet still
goes out, as an Opus frame of silence, with the same sequence numbers and
timestamps it would have had: the receiver's playout sees no gap or jump,
and the source (the microphone, or a tone) stays open, so unmuting is
instant. Receiving is unaffected.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

# `stats`

```elixir
@spec stats(pid()) :: map() | {:error, term()}
```

The NIF's counters merged with the stream's state and the RTP counters.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
