mod options;
mod session;
pub(crate) mod stream;
use std::collections::BTreeMap;
use std::sync::mpsc;
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
pub use options::{Input, Options, PixelFormat, PlayerOptions, TunnelProvider};
pub use session::{ReceiverInfo, Session, SessionState, Stopper};
pub use stream::{AudioWriter, ConfigFormat, Frame, FrameBuffer, Stream, Written, H264};
use crate::error::{Error, Result};
use crate::events::{Event, Paired, PinRequest, Unpaired};
use crate::net::discover::DeviceInfo;
use crate::opts::{Command, InputSpec};
use crate::session::GameIo;
fn callback(options: &Options) -> Option<session::Callback> {
let f = options.on_event.clone()?;
Some(Box::new(move |e: &Event| {
(f.lock().unwrap_or_else(|e| e.into_inner()))(e)
}))
}
pub fn discover(timeout: Duration) -> Result<Vec<DeviceInfo>> {
discover_with(&Options::new(), timeout)
}
pub fn discover_with(options: &Options, timeout: Duration) -> Result<Vec<DeviceInfo>> {
let devices = crate::net::discover::discover(timeout, &[])?;
let store = crate::net::discover::stored_pairings(&options.ctx().storage);
Ok(devices
.iter()
.map(|d| DeviceInfo::new(d, store.as_ref()))
.collect())
}
pub struct Discovery {
shared: Arc<DiscoveryShared>,
thread: Option<JoinHandle<()>>,
}
impl std::fmt::Debug for Discovery {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Discovery").finish_non_exhaustive()
}
}
type DiscoveryShared = (Mutex<(Vec<DeviceInfo>, bool)>, Condvar);
const DISCOVERY_ROUND: Duration = Duration::from_secs(3);
impl Discovery {
pub fn start(options: &Options) -> Result<Discovery> {
let shared: Arc<DiscoveryShared> =
Arc::new((Mutex::new((Vec::new(), false)), Condvar::new()));
let s = shared.clone();
let opts = options.clone();
let thread = std::thread::Builder::new()
.name("tvpipe-discovery".into())
.spawn(move || loop {
let found = discover_with(&opts, DISCOVERY_ROUND);
let (lock, cv) = &*s;
let mut g = lock.lock().unwrap_or_else(|e| e.into_inner());
if g.1 {
return;
}
match found {
Ok(list) => {
let mut by_name: BTreeMap<String, DeviceInfo> =
g.0.drain(..).map(|d| (d.name.clone(), d)).collect();
for d in list {
by_name.insert(d.name.clone(), d);
}
g.0 = by_name.into_values().collect();
}
Err(e) => debug!("discovery: {e}"),
}
let (g, _) = cv
.wait_timeout(g, Duration::from_millis(500))
.unwrap_or_else(|e| e.into_inner());
if g.1 {
return;
}
})
.map_err(|e| Error::io("spawn the discovery thread", e))?;
Ok(Discovery {
shared,
thread: Some(thread),
})
}
pub fn devices(&self) -> Vec<DeviceInfo> {
self.shared
.0
.lock()
.unwrap_or_else(|e| e.into_inner())
.0
.clone()
}
}
impl Drop for Discovery {
fn drop(&mut self) {
self.shared.0.lock().unwrap_or_else(|e| e.into_inner()).1 = true;
self.shared.1.notify_all();
drop(self.thread.take());
}
}
#[cfg(feature = "airplay")]
pub fn pair(target: &str, on_pin: impl FnMut(&PinRequest) -> Option<String>) -> Result<Paired> {
pair_with(target, &Options::new(), on_pin)
}
#[cfg(feature = "airplay")]
pub fn pair_with(
target: &str,
options: &Options,
mut on_pin: impl FnMut(&PinRequest) -> Option<String>,
) -> Result<Paired> {
let ctx = options.ctx();
crate::airplay::pair::pair(target, None, &ctx, &mut |req| {
on_pin(req).ok_or_else(|| Error::usage("no PIN entered").with_code("pin-missing"))
})
}
#[cfg(feature = "airplay")]
pub fn unpair(target: &str, options: &Options) -> Result<Vec<Unpaired>> {
crate::airplay::pair::unpair(target, &options.ctx())
}
#[derive(Debug)]
#[non_exhaustive]
pub enum PairingState {
Connecting,
NeedPin(PinRequest),
Done(Paired),
Failed(Error),
}
#[cfg(feature = "airplay")]
pub struct Pairing {
state: Arc<Mutex<PairingState>>,
pin: mpsc::Sender<Option<String>>,
thread: Option<JoinHandle<()>>,
}
#[cfg(feature = "airplay")]
impl std::fmt::Debug for Pairing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Pairing")
.field("state", &*self.lock())
.finish_non_exhaustive()
}
}
#[cfg(feature = "airplay")]
impl Pairing {
pub fn start(target: &str, options: &Options) -> Result<Pairing> {
let state = Arc::new(Mutex::new(PairingState::Connecting));
let (tx, rx) = mpsc::channel::<Option<String>>();
let (s, target, opts) = (state.clone(), target.to_owned(), options.clone());
let thread = std::thread::Builder::new()
.name("tvpipe-pairing".into())
.spawn(move || {
let set = |st: PairingState| *s.lock().unwrap_or_else(|e| e.into_inner()) = st;
let r = pair_with(&target, &opts, |req| {
set(PairingState::NeedPin(req.clone()));
let pin = rx.recv().ok().flatten();
set(PairingState::Connecting);
pin
});
set(match r {
Ok(p) => PairingState::Done(p),
Err(e) => PairingState::Failed(e),
});
})
.map_err(|e| Error::io("spawn the pairing thread", e))?;
Ok(Pairing {
state,
pin: tx,
thread: Some(thread),
})
}
fn lock(&self) -> MutexGuard<'_, PairingState> {
self.state.lock().unwrap_or_else(|e| e.into_inner())
}
pub fn state(&self) -> PairingState {
match &*self.lock() {
PairingState::Connecting => PairingState::Connecting,
PairingState::NeedPin(r) => PairingState::NeedPin(r.clone()),
PairingState::Done(p) => PairingState::Done(p.clone()),
PairingState::Failed(e) => PairingState::Failed(e.clone_lossy()),
}
}
pub fn submit_pin(&self, pin: &str) -> Result<()> {
if !matches!(*self.lock(), PairingState::NeedPin(_)) {
return Err(Error::usage("the pairing does not wait for a PIN"));
}
self.pin
.send(Some(pin.trim().to_owned()))
.map_err(|_| Error::closed("the pairing ended"))
}
pub fn cancel(&self) {
let _ = self.pin.send(None);
}
pub fn wait(mut self) -> Result<Paired> {
if let Some(t) = self.thread.take() {
let _ = t.join();
}
match self.state() {
PairingState::Done(p) => Ok(p),
PairingState::Failed(e) => Err(e),
_ => Err(Error::closed("the pairing ended")),
}
}
}
#[cfg(feature = "airplay")]
impl Drop for Pairing {
fn drop(&mut self) {
self.cancel();
}
}
fn start_session(
options: &Options,
start: impl Fn(&Options) -> Result<Session>,
) -> Result<Session> {
let s = start(options)?;
match s.wait_connected() {
Ok(()) => Ok(s),
Err(e) => {
drop(s);
pair_on_demand(options, e)?;
let s = start(options)?;
s.wait_connected()?;
Ok(s)
}
}
}
fn pair_on_demand(options: &Options, e: Error) -> Result<()> {
#[cfg(feature = "airplay")]
if let (Some("pairing-required"), Some(device), Some(f)) =
(e.code(), e.device(), options.on_pin.clone())
{
info!("{device} needs pairing");
pair_with(device, options, |req| {
(f.lock().unwrap_or_else(|e| e.into_inner()))(req)
})?;
return Ok(());
}
let _ = options;
Err(e)
}
pub fn stream(options: Options) -> Result<Stream> {
let s = Stream::start(options.clone())?;
match s.wait_connected() {
Ok(()) => Ok(s),
Err(e) => {
drop(s);
pair_on_demand(&options, e)?;
let s = Stream::start(options)?;
s.wait_connected()?;
Ok(s)
}
}
}
fn spawn(options: &Options, cmd: Command) -> Result<Session> {
let approve = options.approve_remote.clone();
let io = GameIo {
approve: Box::new(move |what, cancel| approve.as_ref().is_some_and(|f| f(what, cancel))),
..GameIo::none()
};
Session::spawn(cmd, options.ctx(), io, callback(options))
}
pub fn play(input: Input, options: Options) -> Result<Session> {
start_session(&options, |o| {
spawn(o, Command::Play(o.play_options(input.spec())))
})
}
pub fn test_pattern(options: Options) -> Result<Session> {
start_session(&options, |o| {
spawn(
o,
Command::TestPattern(o.play_options(InputSpec::TestPattern)),
)
})
}
pub fn preview(input: Option<Input>, options: Options) -> Result<Session> {
let mut options = options;
options.to = vec![crate::opts::BROWSER_TARGET.to_owned()];
match input {
Some(i) => play(i, options),
None => test_pattern(options),
}
}
pub fn share(url: Option<&str>, options: Options) -> Result<Session> {
let s = spawn(&options, Command::Share(options.share_options(url)))?;
let until = Instant::now() + Duration::from_secs(30);
while url.is_none() && s.share_url().is_none() && Instant::now() < until {
if s.wait_timeout(Duration::from_millis(20)) {
break;
}
}
Ok(s)
}
#[doc(hidden)]
pub fn __spawn_command(
cmd: Command,
ctx: crate::ctx::Ctx,
io: GameIo,
on_event: Option<session::Callback>,
) -> Result<Session> {
Session::spawn(cmd, ctx, io, on_event)
}
#[cfg(all(test, feature = "player", feature = "h264"))]
mod tests;