Skip to main content

Options

Struct Options 

Source
#[non_exhaustive]
pub struct Options { /* private fields */ }
Expand description

The settings of a command: receivers, the stream’s shape, the browser receiver, storage and callbacks. Fields a command does not use are ignored. Cloning shares the callbacks.

use tvpipe::{Options, PixelFormat};
let options = Options::new()
    .to("Living Room")
    .video(1280, 720, 60, PixelFormat::Rgba)
    .audio(48_000, 2);

Implementations§

Source§

impl Options

Source

pub fn new() -> Options

No receivers yet, defaults everywhere.

Examples found in repository?
examples/stream.rs (line 27)
22fn main() -> tvpipe::Result<()> {
23    let mut targets: Vec<String> = std::env::args().skip(1).collect();
24    if targets.is_empty() {
25        targets.push("browser".into());
26    }
27    let mut options = Options::new()
28        .video(W as u32, H as u32, FPS as u32, PixelFormat::Rgba)
29        .audio(RATE, 2)
30        .player(PlayerOptions::new().port(0))
31        // Pairing with an Apple TV that needs it, on the terminal.
32        .on_pin(|req| {
33            eprint!("PIN shown on {}: ", req.name);
34            let mut line = String::new();
35            std::io::stdin().read_line(&mut line).ok()?;
36            Some(line.trim().to_owned())
37        });
38    for t in &targets {
39        options = options.to(t.as_str());
40    }
41    let mut s = tvpipe::stream(options)?;
42    if let Some(url) = s.player_url() {
43        println!("watch at {url}");
44    }
45
46    let mut frame = vec![0u8; W * H * 4];
47    let mut pcm = Vec::new();
48    let mut phase = 0.0f32;
49    let mut sent = 0u64;
50    for n in 0u64.. {
51        let pts = Duration::from_micros(n * 1_000_000 / FPS);
52        // Pace by the stream's clock: a game would render at its own rate.
53        if let Some(wait) = pts.checked_sub(s.elapsed()) {
54            std::thread::sleep(wait);
55        }
56        draw(&mut frame, n);
57        if s.write_video(&frame, pts)? == Written::Queued {
58            sent += 1;
59        }
60        tone(&mut pcm, &mut phase, (RATE as u64 / FPS) as usize);
61        s.write_audio(&pcm, pts)?;
62        for e in s.poll_events() {
63            match e {
64                Event::Receiver(r) => println!("{}: {}", r.name, r.state),
65                Event::Stats(st) if st.elapsed_ms / 1000 % 5 == 0 => {
66                    println!("{} s, {sent} frames queued", st.elapsed_ms / 1000)
67                }
68                _ => {}
69            }
70        }
71    }
72    s.finish()?;
73    Ok(())
74}
Source

pub fn to(self, target: impl Into<String>) -> Self

A receiver (repeatable): a name from discover ("Living Room"), forced to a protocol ("airplay:Living Room", "cast:Office"), an address ("192.168.1.20", "cast:10.0.0.5"), or "browser" for a browser tab (PlayerOptions).

Examples found in repository?
examples/stream.rs (line 39)
22fn main() -> tvpipe::Result<()> {
23    let mut targets: Vec<String> = std::env::args().skip(1).collect();
24    if targets.is_empty() {
25        targets.push("browser".into());
26    }
27    let mut options = Options::new()
28        .video(W as u32, H as u32, FPS as u32, PixelFormat::Rgba)
29        .audio(RATE, 2)
30        .player(PlayerOptions::new().port(0))
31        // Pairing with an Apple TV that needs it, on the terminal.
32        .on_pin(|req| {
33            eprint!("PIN shown on {}: ", req.name);
34            let mut line = String::new();
35            std::io::stdin().read_line(&mut line).ok()?;
36            Some(line.trim().to_owned())
37        });
38    for t in &targets {
39        options = options.to(t.as_str());
40    }
41    let mut s = tvpipe::stream(options)?;
42    if let Some(url) = s.player_url() {
43        println!("watch at {url}");
44    }
45
46    let mut frame = vec![0u8; W * H * 4];
47    let mut pcm = Vec::new();
48    let mut phase = 0.0f32;
49    let mut sent = 0u64;
50    for n in 0u64.. {
51        let pts = Duration::from_micros(n * 1_000_000 / FPS);
52        // Pace by the stream's clock: a game would render at its own rate.
53        if let Some(wait) = pts.checked_sub(s.elapsed()) {
54            std::thread::sleep(wait);
55        }
56        draw(&mut frame, n);
57        if s.write_video(&frame, pts)? == Written::Queued {
58            sent += 1;
59        }
60        tone(&mut pcm, &mut phase, (RATE as u64 / FPS) as usize);
61        s.write_audio(&pcm, pts)?;
62        for e in s.poll_events() {
63            match e {
64                Event::Receiver(r) => println!("{}: {}", r.name, r.state),
65                Event::Stats(st) if st.elapsed_ms / 1000 % 5 == 0 => {
66                    println!("{} s, {sent} frames queued", st.elapsed_ms / 1000)
67                }
68                _ => {}
69            }
70        }
71    }
72    s.finish()?;
73    Ok(())
74}
Source

pub fn to_device(self, device: &DeviceInfo) -> Self

A receiver from discover: Cast when it plays video over Cast, else AirPlay.

Source

pub fn video( self, width: u32, height: u32, fps: u32, format: PixelFormat, ) -> Self

Raw frames of this size, rate and layout go to the built-in H.264 encoder (Stream::write_video).

Examples found in repository?
examples/stream.rs (line 28)
22fn main() -> tvpipe::Result<()> {
23    let mut targets: Vec<String> = std::env::args().skip(1).collect();
24    if targets.is_empty() {
25        targets.push("browser".into());
26    }
27    let mut options = Options::new()
28        .video(W as u32, H as u32, FPS as u32, PixelFormat::Rgba)
29        .audio(RATE, 2)
30        .player(PlayerOptions::new().port(0))
31        // Pairing with an Apple TV that needs it, on the terminal.
32        .on_pin(|req| {
33            eprint!("PIN shown on {}: ", req.name);
34            let mut line = String::new();
35            std::io::stdin().read_line(&mut line).ok()?;
36            Some(line.trim().to_owned())
37        });
38    for t in &targets {
39        options = options.to(t.as_str());
40    }
41    let mut s = tvpipe::stream(options)?;
42    if let Some(url) = s.player_url() {
43        println!("watch at {url}");
44    }
45
46    let mut frame = vec![0u8; W * H * 4];
47    let mut pcm = Vec::new();
48    let mut phase = 0.0f32;
49    let mut sent = 0u64;
50    for n in 0u64.. {
51        let pts = Duration::from_micros(n * 1_000_000 / FPS);
52        // Pace by the stream's clock: a game would render at its own rate.
53        if let Some(wait) = pts.checked_sub(s.elapsed()) {
54            std::thread::sleep(wait);
55        }
56        draw(&mut frame, n);
57        if s.write_video(&frame, pts)? == Written::Queued {
58            sent += 1;
59        }
60        tone(&mut pcm, &mut phase, (RATE as u64 / FPS) as usize);
61        s.write_audio(&pcm, pts)?;
62        for e in s.poll_events() {
63            match e {
64                Event::Receiver(r) => println!("{}: {}", r.name, r.state),
65                Event::Stats(st) if st.elapsed_ms / 1000 % 5 == 0 => {
66                    println!("{} s, {sent} frames queued", st.elapsed_ms / 1000)
67                }
68                _ => {}
69            }
70        }
71    }
72    s.finish()?;
73    Ok(())
74}
Source

pub fn h264(self, width: u32, height: u32, fps: u32) -> Self

The program’s own H.264 of this size and rate (Stream::write_h264).

Source

pub fn audio(self, sample_rate: u32, channels: u8) -> Self

Interleaved PCM at this rate and channel count (Stream::write_audio).

Examples found in repository?
examples/stream.rs (line 29)
22fn main() -> tvpipe::Result<()> {
23    let mut targets: Vec<String> = std::env::args().skip(1).collect();
24    if targets.is_empty() {
25        targets.push("browser".into());
26    }
27    let mut options = Options::new()
28        .video(W as u32, H as u32, FPS as u32, PixelFormat::Rgba)
29        .audio(RATE, 2)
30        .player(PlayerOptions::new().port(0))
31        // Pairing with an Apple TV that needs it, on the terminal.
32        .on_pin(|req| {
33            eprint!("PIN shown on {}: ", req.name);
34            let mut line = String::new();
35            std::io::stdin().read_line(&mut line).ok()?;
36            Some(line.trim().to_owned())
37        });
38    for t in &targets {
39        options = options.to(t.as_str());
40    }
41    let mut s = tvpipe::stream(options)?;
42    if let Some(url) = s.player_url() {
43        println!("watch at {url}");
44    }
45
46    let mut frame = vec![0u8; W * H * 4];
47    let mut pcm = Vec::new();
48    let mut phase = 0.0f32;
49    let mut sent = 0u64;
50    for n in 0u64.. {
51        let pts = Duration::from_micros(n * 1_000_000 / FPS);
52        // Pace by the stream's clock: a game would render at its own rate.
53        if let Some(wait) = pts.checked_sub(s.elapsed()) {
54            std::thread::sleep(wait);
55        }
56        draw(&mut frame, n);
57        if s.write_video(&frame, pts)? == Written::Queued {
58            sent += 1;
59        }
60        tone(&mut pcm, &mut phase, (RATE as u64 / FPS) as usize);
61        s.write_audio(&pcm, pts)?;
62        for e in s.poll_events() {
63            match e {
64                Event::Receiver(r) => println!("{}: {}", r.name, r.state),
65                Event::Stats(st) if st.elapsed_ms / 1000 % 5 == 0 => {
66                    println!("{} s, {sent} frames queued", st.elapsed_ms / 1000)
67                }
68                _ => {}
69            }
70        }
71    }
72    s.finish()?;
73    Ok(())
74}
Source

pub fn size(self, width: u32, height: u32) -> Self

Caps the output size (default: the input’s, at most 1920x1080).

Source

pub fn fps(self, fps: u32) -> Self

The output frame rate (default: the input’s, at most 60).

Source

pub fn bitrate_kbps(self, kbps: u32) -> Self

The video bitrate (default: from the size and rate).

Source

pub fn latency(self, latency: Duration) -> Self

The playout delay every receiver presents frames with (5–2000 ms; default: 300 ms with Cast, AirPlay’s automatic leads, 100 ms for browser tabs).

Source

pub fn reconnect(self, window: Duration) -> Self

How long a receiver whose connection was lost is reconnected (default 120 s; zero: never).

Source

pub fn encoder_threads(self, threads: usize) -> Self

Threads of the built-in encoder (0, the default: one per core, up to its slice limit).

Source

pub fn encoder(self, encoder: Encoder) -> Self

Which encoder makes the video for play (default: the best ffmpeg has).

Source

pub fn ffmpeg_path(self, path: impl Into<PathBuf>) -> Self

The ffmpeg to use (default: PATH, or next to the program).

Source

pub fn no_audio(self) -> Self

Video only.

Source

pub fn looped(self, looped: bool) -> Self

Starts a file over at its end.

Source

pub fn realtime(self, realtime: bool) -> Self

Paces a file at its timestamps (default for files) or reads it as fast as it comes (default for live inputs).

Source

pub fn player(self, player: PlayerOptions) -> Self

The browser receiver’s settings.

Examples found in repository?
examples/stream.rs (line 30)
22fn main() -> tvpipe::Result<()> {
23    let mut targets: Vec<String> = std::env::args().skip(1).collect();
24    if targets.is_empty() {
25        targets.push("browser".into());
26    }
27    let mut options = Options::new()
28        .video(W as u32, H as u32, FPS as u32, PixelFormat::Rgba)
29        .audio(RATE, 2)
30        .player(PlayerOptions::new().port(0))
31        // Pairing with an Apple TV that needs it, on the terminal.
32        .on_pin(|req| {
33            eprint!("PIN shown on {}: ", req.name);
34            let mut line = String::new();
35            std::io::stdin().read_line(&mut line).ok()?;
36            Some(line.trim().to_owned())
37        });
38    for t in &targets {
39        options = options.to(t.as_str());
40    }
41    let mut s = tvpipe::stream(options)?;
42    if let Some(url) = s.player_url() {
43        println!("watch at {url}");
44    }
45
46    let mut frame = vec![0u8; W * H * 4];
47    let mut pcm = Vec::new();
48    let mut phase = 0.0f32;
49    let mut sent = 0u64;
50    for n in 0u64.. {
51        let pts = Duration::from_micros(n * 1_000_000 / FPS);
52        // Pace by the stream's clock: a game would render at its own rate.
53        if let Some(wait) = pts.checked_sub(s.elapsed()) {
54            std::thread::sleep(wait);
55        }
56        draw(&mut frame, n);
57        if s.write_video(&frame, pts)? == Written::Queued {
58            sent += 1;
59        }
60        tone(&mut pcm, &mut phase, (RATE as u64 / FPS) as usize);
61        s.write_audio(&pcm, pts)?;
62        for e in s.poll_events() {
63            match e {
64                Event::Receiver(r) => println!("{}: {}", r.name, r.state),
65                Event::Stats(st) if st.elapsed_ms / 1000 % 5 == 0 => {
66                    println!("{} s, {sent} frames queued", st.elapsed_ms / 1000)
67                }
68                _ => {}
69            }
70        }
71    }
72    s.finish()?;
73    Ok(())
74}
Source

pub fn storage(self, storage: Storage) -> Self

Where pairings and the player token are kept (default: Storage::default_app).

Source

pub fn on_pin( self, f: impl FnMut(&PinRequest) -> Option<String> + Send + 'static, ) -> Self

Pairs an AirPlay receiver that needs it during stream/play: f gets the request and returns the PIN the TV shows (None: give up).

Examples found in repository?
examples/stream.rs (lines 32-37)
22fn main() -> tvpipe::Result<()> {
23    let mut targets: Vec<String> = std::env::args().skip(1).collect();
24    if targets.is_empty() {
25        targets.push("browser".into());
26    }
27    let mut options = Options::new()
28        .video(W as u32, H as u32, FPS as u32, PixelFormat::Rgba)
29        .audio(RATE, 2)
30        .player(PlayerOptions::new().port(0))
31        // Pairing with an Apple TV that needs it, on the terminal.
32        .on_pin(|req| {
33            eprint!("PIN shown on {}: ", req.name);
34            let mut line = String::new();
35            std::io::stdin().read_line(&mut line).ok()?;
36            Some(line.trim().to_owned())
37        });
38    for t in &targets {
39        options = options.to(t.as_str());
40    }
41    let mut s = tvpipe::stream(options)?;
42    if let Some(url) = s.player_url() {
43        println!("watch at {url}");
44    }
45
46    let mut frame = vec![0u8; W * H * 4];
47    let mut pcm = Vec::new();
48    let mut phase = 0.0f32;
49    let mut sent = 0u64;
50    for n in 0u64.. {
51        let pts = Duration::from_micros(n * 1_000_000 / FPS);
52        // Pace by the stream's clock: a game would render at its own rate.
53        if let Some(wait) = pts.checked_sub(s.elapsed()) {
54            std::thread::sleep(wait);
55        }
56        draw(&mut frame, n);
57        if s.write_video(&frame, pts)? == Written::Queued {
58            sent += 1;
59        }
60        tone(&mut pcm, &mut phase, (RATE as u64 / FPS) as usize);
61        s.write_audio(&pcm, pts)?;
62        for e in s.poll_events() {
63            match e {
64                Event::Receiver(r) => println!("{}: {}", r.name, r.state),
65                Event::Stats(st) if st.elapsed_ms / 1000 % 5 == 0 => {
66                    println!("{} s, {sent} frames queued", st.elapsed_ms / 1000)
67                }
68                _ => {}
69            }
70        }
71    }
72    s.finish()?;
73    Ok(())
74}
Source

pub fn on_event(self, f: impl FnMut(&Event) + Send + 'static) -> Self

Calls f for every event, on a thread of the session’s own (so a slow callback never stalls the media threads).

Source

pub fn approve_remote( self, f: impl Fn(&str, &dyn Fn() -> bool) -> bool + Send + Sync + 'static, ) -> Self

share with a public link: decides whether a capture from over the tunnel may reach the receivers, given its description; the second argument says the question is moot. Without it, remote shares are refused.

Source

pub fn share_tunnel(self, provider: TunnelProvider) -> Self

share without a URL: also serves the share page on a public link (tunnel), so someone elsewhere can share to the receivers.

Source

pub fn open_share_page(self, open: bool) -> Self

share without a URL: opens the share page with the OS’s opener.

Source

pub fn visible_browser(self, visible: bool) -> Self

share URL (headless): show tvpipe’s browser window instead of running it headless.

Source

pub fn browser_path(self, path: impl Into<PathBuf>) -> Self

share URL: the browser to start (default: the installed one).

Trait Implementations§

Source§

impl Clone for Options

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Options

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Options

Source§

fn default() -> Options

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.