vector_core/calls/
platform.rs1use 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
16pub struct LinkConn {
19 pub to_web: mpsc::Sender<Bytes>,
20 pub from_web: mpsc::Receiver<Bytes>,
21}
22
23#[derive(Clone, Copy, PartialEq, Eq, Debug)]
25pub enum AwakeLevel {
26 System,
28 Display,
30}
31
32pub struct AudioStart {
34 pub volume: f32,
35 pub share_volume: f32,
36 pub stats: Option<Arc<MediaStats>>,
38 pub share: Arc<ShareInput>,
39}
40
41pub trait CallAudio: Send {
43 fn stats(&self) -> Arc<MediaStats>;
44 fn set_muted(&self, on: bool);
45 fn has_mic(&self) -> bool {
47 true
48 }
49 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 async fn endpoint(&self) -> Result<Endpoint, String>;
60 async fn local_addr(&self) -> Result<String, String>;
62 fn decode_addr(&self, addr: &str) -> Option<EndpointAddr>;
64 async fn start_audio(&self, conn: Connection, start: AudioStart) -> Result<Box<dyn CallAudio>, String>;
66 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 fn set_video_taker(&self, taker: Option<mpsc::Sender<LinkConn>>);
72 fn hold_awake(&self, _level: AwakeLevel) -> Option<Box<dyn Any + Send>> {
74 None
75 }
76 fn hold_call_audio(&self, _out_loud: bool) -> Option<Box<dyn Any + Send>> {
79 None
80 }
81 fn ended(&self) {}
83 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 fn share_audio_permitted(&self) -> bool {
93 true
94 }
95}
96
97static PLATFORM: OnceLock<Arc<dyn CallPlatform>> = OnceLock::new();
98
99pub fn install(platform: Arc<dyn CallPlatform>) {
101 let _ = PLATFORM.set(platform);
102}
103
104pub 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}