use std::sync::{Arc, Mutex, RwLock};
use bitflags::bitflags;
use pipewire_native_macros as macros;
use pipewire_native_spa as spa;
use crate::{
core::Core,
new_refcounted,
properties::Properties,
proxy::{HasProxy, Proxy},
refcounted, types, HookId, Id,
};
refcounted! {
pub struct Link {
proxy: RwLock<Option<Proxy<Link>>>,
hooks: Arc<Mutex<spa::hook::HookList<LinkEvents>>>,
}
}
bitflags! {
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LinkChangeMask : u32 {
const PROPS = (1 << 0);
}
}
#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, macros::EnumU32)]
pub enum LinkState {
Error,
Unlinked,
Init,
Negotiation,
Allocating,
Paused,
Active,
}
pub struct LinkInfo<'a> {
pub id: Id,
pub output_node_id: Id,
pub output_port_id: Id,
pub input_node_id: Id,
pub input_port_id: Id,
pub mask: LinkChangeMask,
pub state: LinkState,
pub error: Option<&'a str>,
pub format: Option<&'a spa::pod::RawPodOwned>,
pub props: &'a Properties,
}
#[allow(clippy::type_complexity)]
#[derive(Default)]
pub struct LinkEvents {
pub info: Option<Box<dyn FnMut(&LinkInfo<'_>) + Send>>,
}
impl HasProxy for Link {
fn type_(&self) -> types::ObjectType {
types::interface::LINK
}
fn version(&self) -> u32 {
3
}
fn proxy(&self) -> Proxy<Self> {
self.inner
.proxy
.read()
.unwrap()
.as_ref()
.expect("Link proxy should be initialised on creation")
.clone()
}
}
impl Link {
pub(crate) fn new(core: &Core) -> Self {
let this = Self {
inner: new_refcounted(InnerLink::new()),
};
let id = core.next_proxy_id();
this.inner
.proxy
.write()
.unwrap()
.replace(Proxy::new(id, &this));
core.add_proxy(&this, id);
this
}
pub fn add_listener(&self, events: LinkEvents) -> HookId {
self.inner.hooks.lock().unwrap().append(events)
}
pub fn remove_listener(&self, hook_id: HookId) {
self.inner.hooks.lock().unwrap().remove(hook_id);
}
pub(crate) fn events(&self) -> Arc<Mutex<spa::hook::HookList<LinkEvents>>> {
self.inner.hooks.clone()
}
}
impl InnerLink {
fn new() -> Self {
Self {
proxy: RwLock::new(None),
hooks: spa::hook::HookList::new(),
}
}
}