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

Advanced

Open and switch sources

LocalBroadcast::set_spec opens the common sources by name, as Publish a camera, screen or file shows. This page covers the cases it leaves out: a device config of your own, a source that changes while the broadcast runs, frames that your application makes, and a file published without a decoder.

Open a device yourself

Pass a source to set_video or set_audio together with its encoding:

use iroh_live::media::{AudioSource, LocalBroadcast, VideoEncoding, VideoSource, audio, audio_encoding};
async fn run(broadcast: &LocalBroadcast) -> Result<(), Box<dyn std::error::Error>> {
broadcast.set_video(VideoSource::screen().await?, VideoEncoding::default())?;
let microphone = AudioSource::microphone(audio::capture::Config::default()).await?;
broadcast.set_audio(microphone, audio_encoding::voice())?;
Ok(())
}

VideoSource::camera and VideoSource::screen open a camera or the main screen at its default mode. VideoSource::capture takes a full capture::Config instead, and screen_source() returns the capture source that screen opens, for a config of your own. Each one opens the device and fails if it cannot. On Linux with the pipewire feature, the default camera is PipeWire’s, which other applications can share, and the default display is the screen the user picks in the desktop portal. capture.width, capture.height and capture.framerate are hints, and the device picks its nearest mode.

AudioSource::microphone only checks that the device exists. The microphone opens once set_audio runs, and a failure then shows in LocalBroadcast::status() as a failed audio slot.

Ask for a size and a frame rate

set_spec_with takes a VideoOpenOptions with the size and frame rate to ask the device for:

use iroh_live::media::{LocalBroadcast, MediaEncoding, spec::{MediaSpec, VideoOpenOptions}, video};
async fn run(broadcast: &LocalBroadcast) -> Result<(), Box<dyn std::error::Error>> {
let options = VideoOpenOptions::default()
    .with_size(video::Size::new(1280, 720))
    .with_framerate(video::FrameRate::new(30, 1).expect("a valid rate"));
broadcast
    .set_spec_with(&MediaSpec::camera(), &options, MediaEncoding::default())
    .await?;
Ok(())
}

Change the source while live

set_video, set_audio and set_spec replace the current source of a running broadcast, and viewers keep their subscription. The broadcast keeps the sources it encodes: video_source() and audio_source() hand them back, for example to draw a preview from the frames of video_source(), so the application does not have to track them itself.

To switch cameras behind one source, VideoSource::switchable returns a VideoSwitch that swaps what feeds it. VideoSwitch::open_spec opens a source by its specifier, which suits a source picker:

use iroh_live::media::{
    VideoSwitch,
    spec::{VideoOpenOptions, VideoSourceSpec},
};

fn pick(switch: &VideoSwitch, choice: &str) -> Result<(), Box<dyn std::error::Error>> {
    let spec: VideoSourceSpec = choice.parse()?;
    // Opens in the background; `switch.status()` reports how it went.
    switch.open_spec(spec, VideoOpenOptions::default());
    Ok(())
}

VideoSourceSpec::open opens one source by its specifier without a switch.

Push your own frames

VideoSource::push(format) and AudioSource::push(format) take frames that the application makes. A video producer can call FrameSender::request_keyframe() before a push to make that frame a keyframe on every rendition.

Publish a file without a decoder

By default, a file: source publishes the file’s H.264 and AAC as they are stored. This passthrough costs no CPU and keeps the file’s quality, but it has limits:

  • It publishes one rendition at the file’s size and bitrate, and nothing adapts the bitrate to the path.
  • Keyframes come only where the file has them, so a new viewer waits up to one GOP for a picture, and a viewer’s request for a keyframe goes unanswered.
  • There are no decoded pictures for a preview.

--reencode and MediaSpec::with_reencode(true) remove these limits, at the cost of a decode and an encode. In irl, every flag that only an encoder acts on turns it on, such as --renditions, --codec or --bitrate, and so does --preview. --no-reencode insists on passthrough and fails on such a flag instead.

Both ways take H.264 video and AAC audio only. A passed-through file publishes whichever of its tracks is H.264 or AAC, and leaves the other out with a warning. A file without video publishes its audio alone.

For finer control, MediaFile::open reads the file’s index, and info() reports its codecs, size, rate and GOP. LocalBroadcast::set_encoded_file publishes the file’s H.264 and AAC as they are, and the file’s video and audio decode it into sources for set_video and set_audio. Every stream of one file plays against one clock, so audio and video stay in sync and loop together. The publish-file example in iroh-live/examples shows both ways.

Other sources

rpicam_h264 publishes the Raspberry Pi camera’s own H.264, as Publish from a Raspberry Pi describes.

Where to go next