# Voice

> Join voice channels, send and receive audio, and play music with an audio player; end-to-end encrypted.

Voice in Zaun is a mesh of WebRTC connections, one per other participant (up to 10 per channel).
Every audio frame is **end-to-end encrypted** on top of WebRTC's own encryption: each sender seals
its Opus frames with SFrame (RFC 9605) under a key that reaches the others only wrapped with X-Wing
(a post-quantum hybrid) and signed with its member key. Keys change whenever someone joins or
leaves. The host relays signalling and keys and **never hears audio**.

## Join and talk

**Rust:**

```rust
use std::time::Duration;
use zaun_client::VoiceEvent;

let voice = community.channel_by_name("lounge").expect("a voice channel").join()?;
voice.send_audio(&opus_frame); // one 20 ms Opus frame
if let Some(VoiceEvent::Audio { member, frame }) = voice.next_event(Duration::from_millis(100)) {
    play(member, &frame);
}
```

Rust uses str0m and reaches the host's TURN relay over UDP, TCP or TLS (`turns:`, TLS 1.3 with the
post-quantum X25519MLKEM768 key exchange only). Your app encodes and plays Opus, or uses the built-in
`AudioPlayer` below.

**Browser:**

```js
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const voice = await channel.join({ track: stream.getAudioTracks()[0] });
voice.on("track", ({ member, stream }) => playStream(member, stream));
```

Browsers use their own WebRTC with an encoded transform (`RTCRtpScriptTransform` in Firefox,
Safari and Chromium; Chromium's older encoded streams as a fallback). Pages with a
Content-Security-Policy need `worker-src blob:`.

**Node.js / Bun:**

```js
const voice = await channel.join();
const { finished } = voice.play("song.opus"); // an Ogg Opus file, in real time
voice.on("audio", ({ member, frame }) => decode(member, frame));
await finished;
```

Node.js and Bun use werift (`npm install werift@0.27`). `voice.sendAudio(frame)` sends one Opus
frame.

## Playing music (Rust)

`AudioPlayer` plays resources into every voice session
subscribed to it, on absolute 20 ms deadlines so the stream keeps real time.

```rust
use zaun_client::{AudioPlayer, AudioPlayerEvent, AudioResource};

let voice = community.channel_by_name("music").expect("a voice channel").join()?;
let player = AudioPlayer::new()?;
voice.subscribe(&player);
player.play(AudioResource::ogg_file("intro.opus")?);                              // no transcoding
player.enqueue(AudioResource::ffmpeg_file("song.flac", Default::default())?);   // anything ffmpeg reads
for event in player.events() {
    if let AudioPlayerEvent::TrackEnd { .. } = event {
        // pick the next song
    }
}
```

A player with nobody able to hear it (no subscriber, or everyone muted) pauses itself, and resumes
when someone can hear it again.

## Privacy

Media goes only through the host's TURN relay unless both the host and your app allow a direct
connection (`allow_direct` / `allowDirect`), since a direct connection shows your IP address to the
other participants. Each member's key is **pinned** the first time the client sees it; if a host
later lists another key for that member, the participant is skipped with a `KeyChanged` event, and
`member.fingerprint()` gives people something to compare out of band.
