Skip to main content

vector_core/calls/
platform.rs

1//! What a client supplies for calls to run on it: the Iroh endpoint, the audio
2//! devices, the video link, and a few platform extras. Everything else (the state
3//! machine, the signalling, the video transport) is shared.
4
5use std::any::Any;
6use std::sync::{Arc, OnceLock};
7
8use bytes::Bytes;
9use iroh::endpoint::Connection;
10use iroh::{Endpoint, EndpointAddr};
11use tokio::sync::mpsc;
12
13use super::share::ShareInput;
14use super::stats::MediaStats;
15
16/// One video link, split: what goes to the side that decodes and paints, and what
17/// comes from the side that captures and encodes. Messages are `calls::link`'s.
18pub struct LinkConn {
19    pub to_web: mpsc::Sender<Bytes>,
20    pub from_web: mpsc::Receiver<Bytes>,
21}
22
23/// How awake a call needs the machine.
24#[derive(Clone, Copy, PartialEq, Eq, Debug)]
25pub enum AwakeLevel {
26    /// No idle sleep; the display may go dark.
27    System,
28    /// The display stays on too: there is video.
29    Display,
30}
31
32/// What the audio of a connected call starts with.
33pub struct AudioStart {
34    pub volume: f32,
35    pub share_volume: f32,
36    /// The call's counters, kept across a restart: fresh ones read as a peer gone quiet.
37    pub stats: Option<Arc<MediaStats>>,
38    pub share: Arc<ShareInput>,
39}
40
41/// A running audio engine; dropping it stops it.
42pub trait CallAudio: Send {
43    fn stats(&self) -> Arc<MediaStats>;
44    fn set_muted(&self, on: bool);
45    /// False when the call went ahead without a microphone, so it can only be heard muted.
46    fn has_mic(&self) -> bool {
47        true
48    }
49    /// Listener-side gains, 1.0 is unity.
50    fn set_volume(&self, volume: f32);
51    fn set_share_volume(&self, volume: f32);
52}
53
54#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
55#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
56pub trait CallPlatform: Send + Sync + 'static {
57    /// The endpoint calls ride. It accepts `wire::CALL_ALPN` and hands those
58    /// connections to `session::on_incoming`.
59    async fn endpoint(&self) -> Result<Endpoint, String>;
60    /// Our address, encoded, as a peer should dial it.
61    async fn local_addr(&self) -> Result<String, String>;
62    /// A peer's address as this platform may dial it; None if unusable.
63    fn decode_addr(&self, addr: &str) -> Option<EndpointAddr>;
64    /// Microphone and speaker for a connected call.
65    async fn start_audio(&self, conn: Connection, start: AudioStart) -> Result<Box<dyn CallAudio>, String>;
66    /// The media engine for a voice Channel's call; without one, the call runs presence-only.
67    async fn start_voice(&self) -> Result<Box<dyn super::voice::VoiceMedia>, String> {
68        Err("Voice isn't available on this platform yet".into())
69    }
70    /// Where new video links go: the call's video track while one runs, else nowhere.
71    fn set_video_taker(&self, taker: Option<mpsc::Sender<LinkConn>>);
72    /// A hold on the machine for as long as the returned value lives.
73    fn hold_awake(&self, _level: AwakeLevel) -> Option<Box<dyn Any + Send>> {
74        None
75    }
76    /// The call's sound routed as a phone call's for as long as the returned value lives: to
77    /// the ear, or `out_loud` (a video call, a voice Channel). Taken before the audio opens.
78    fn hold_call_audio(&self, _out_loud: bool) -> Option<Box<dyn Any + Send>> {
79        None
80    }
81    /// A call this side took down after it rang or ran.
82    fn ended(&self) {}
83    /// Captures a shared screen's sound itself, for a picker that gave none.
84    fn share_audio_native(&self) -> bool {
85        false
86    }
87    async fn start_share_audio(&self, _share: Arc<ShareInput>) -> Result<(), String> {
88        Err("Not available on this platform".into())
89    }
90    async fn stop_share_audio(&self) {}
91    /// False once the system has refused screen capture for good.
92    fn share_audio_permitted(&self) -> bool {
93        true
94    }
95}
96
97static PLATFORM: OnceLock<Arc<dyn CallPlatform>> = OnceLock::new();
98
99/// Installed once at startup; calls are refused until it is.
100pub fn install(platform: Arc<dyn CallPlatform>) {
101    let _ = PLATFORM.set(platform);
102}
103
104/// Whether `platform` is the one installed: `install` keeps the first it is given.
105pub fn installed_is(platform: &Arc<dyn CallPlatform>) -> bool {
106    PLATFORM.get().is_some_and(|p| std::ptr::eq(Arc::as_ptr(p) as *const (), Arc::as_ptr(platform) as *const ()))
107}
108
109pub(crate) fn get() -> Result<Arc<dyn CallPlatform>, String> {
110    PLATFORM.get().cloned().ok_or_else(|| "Calls are not available".to_string())
111}