Keyboard shortcuts

Press ← or → to go to the previous or next page

Press S or / to search the docs

Press ? to show this help

Press Esc to hide this help

Guides

Stream audio

A broadcast does not need video. Audio alone suits a voice channel, an intercom, a radio stream, or a microphone on a device without a camera. It uses little bandwidth and CPU, since there is no video to encode. The audio is Opus, and each audio track travels on its own QUIC streams, as in every broadcast.

In short

irl publish --video none
irl watch --no-video <TICKET>
use iroh_live::{Node, layout::LiveLayout, media::{AudioSource, LocalBroadcast, audio, audio_encoding}};
async fn run(node: &Node, layout: &LiveLayout) -> Result<(), Box<dyn std::error::Error>> {
// No `set_video` call, so the broadcast carries audio alone.
let broadcast: LocalBroadcast = layout.publish(node, "voice")?;
let microphone = AudioSource::microphone(audio::capture::Config::default()).await?;
broadcast.set_audio(microphone, audio_encoding::voice())?;
Ok(())
}

With irl

Publish the default microphone with no video:

irl publish --video none

The command prints a ticket. To pick another microphone, list the devices and pass an id. Without a microphone, publish a test tone:

irl devices
irl publish --video none --audio mic:<id>
irl publish --video none --audio test

On another machine, play the ticket through the default speaker:

irl watch --no-video <TICKET>

--no-video opens no window, so this also works on a machine without a display, such as a Raspberry Pi with a speaker. --audio-output <id> picks another output device.

An audio file can take the place of the microphone. :loop starts it again when it ends:

irl publish --video none --audio file:music.mp3:loop

irl reads WAV, MP3 and MP4 files. On macOS, --audio system publishes everything the machine plays. The irl reference lists every audio source.

Publish from Rust

A broadcast that never gets a video source carries audio alone. This program publishes the default microphone as voice and prints the ticket. With a file path as its argument, it publishes that file instead. The programs on this page start from the Rust quickstart, but AudioSource::file needs the file feature, which full leaves out: features = ["full", "file"].

use iroh::protocol::Router;
use iroh_live::{
    EndpointConfig, Node,
    layout::LiveLayout,
    media::{AudioSource, FileEnd, LocalBroadcast, audio, audio_encoding},
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let endpoint = EndpointConfig::from_env()?.bind().await?;
    let layout = LiveLayout::default();
    let node = Node::new(endpoint.clone(), layout.config());
    let router = node.mount(Router::builder(endpoint)).spawn();

    // No `set_video` call, so the broadcast carries audio alone.
    let broadcast: LocalBroadcast = layout.publish(&node, "voice")?;
    match std::env::args().nth(1) {
        Some(path) => {
            let file = AudioSource::file(path, FileEnd::Restart).await?;
            broadcast.set_audio(file, audio_encoding::music())?;
        }
        None => {
            let microphone = AudioSource::microphone(audio::capture::Config::default()).await?;
            broadcast.set_audio(microphone, audio_encoding::voice())?;
        }
    }
    println!("{}", layout.ticket(node.id(), "voice")?);

    tokio::signal::ctrl_c().await?;
    router.shutdown().await?;
    Ok(())
}

audio_encoding::voice() suits speech, and audio_encoding::music() suits music. On macOS, the system asks for permission to use the microphone the first time the program runs.

Listen from Rust

A player subscribes to audio only when its configuration asks for it. This program plays the ticket through the default speaker until you press Ctrl+C:

use iroh_live::{
    BroadcastTicket, EndpointConfig, Node, NodeConfig,
    media::{AudioMode, PlayerConfig, SubscriptionExt, audio::playback::Engine},
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let ticket: BroadcastTicket = std::env::args()
        .nth(1)
        .ok_or("usage: listen <ticket>")?
        .parse()?;
    let endpoint = EndpointConfig::from_env()?.bind().await?;
    let node = Node::new(endpoint.clone(), NodeConfig::default());

    let subscription = node.subscribe_ticket(&ticket).await?;
    // Opens the default speaker. Every player on one engine is mixed.
    let engine = Engine::open(Default::default()).await?;
    let config = PlayerConfig::default().with_audio(AudioMode::Play(engine));
    // Plays while `_player` lives.
    let _player = subscription.remote_broadcast().play(config)?;

    tokio::signal::ctrl_c().await?;
    node.shutdown().await;
    endpoint.close().await;
    Ok(())
}

One Engine mixes every player that uses it, so a program that listens to several broadcasts opens the speaker once. A program that processes the audio itself, without a speaker, uses AudioMode::Decode and reads Player::audio_frames().

Many listeners and browsers

A publisher serves each direct listener itself. For a large audience, or for listeners in browsers, publish through a media relay. The steps are the same as for video.

Where to go next