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,
protocol,
proxy::{HasProxy, Proxy},
proxy_object_invoke, refcounted,
types::{self, params::ParamBuilder},
HookId, Id, Refcounted,
};
refcounted! {
pub struct Port {
proxy: RwLock<Option<Proxy<Port>>>,
methods: Arc<Mutex<PortMethods<Port>>>,
hooks: Arc<Mutex<spa::hook::HookList<PortEvents>>>,
}
}
#[allow(clippy::type_complexity)]
pub(crate) struct PortMethods<T: HasProxy + Refcounted> {
pub(crate) subscribe_params:
Box<dyn FnMut(&Proxy<T>, &[spa::param::ParamType]) -> std::io::Result<()>>,
pub(crate) enum_params: Box<
dyn FnMut(
&Proxy<T>,
u32,
Option<spa::param::ParamType>,
u32,
u32,
Option<ParamBuilder>,
) -> std::io::Result<()>,
>,
}
bitflags! {
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PortChangeMask : u32 {
const PROPS = (1 << 0);
const PARAMS = (1 << 1);
}
}
#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, macros::EnumU32)]
pub enum PortDirection {
Input,
Output,
}
pub struct PortInfo<'a> {
pub id: Id,
pub direction: PortDirection,
pub mask: PortChangeMask,
pub props: &'a Properties,
pub params: &'a [(spa::param::ParamType, spa::param::ParamInfoFlags)],
}
#[allow(clippy::type_complexity)]
#[derive(Default)]
pub struct PortEvents {
pub info: Option<Box<dyn FnMut(&PortInfo<'_>) + Send>>,
pub param:
Option<Box<dyn FnMut(u32, spa::param::ParamType, u32, u32, &spa::pod::RawPodOwned) + Send>>,
}
impl HasProxy for Port {
fn type_(&self) -> types::ObjectType {
types::interface::PORT
}
fn version(&self) -> u32 {
3
}
fn proxy(&self) -> Proxy<Self> {
self.inner
.proxy
.read()
.unwrap()
.as_ref()
.expect("Port proxy should be initialised on creation")
.clone()
}
}
impl Port {
pub(crate) fn new(core: &Core) -> Self {
let this = Self {
inner: new_refcounted(InnerPort::new(core)),
};
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: PortEvents) -> 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 fn subscribe_params(&self, ids: &[spa::param::ParamType]) -> std::io::Result<()> {
let proxy = self.proxy();
proxy_object_invoke!(proxy, subscribe_params, ids)
}
pub fn enum_params(
&self,
seq: u32,
id: Option<spa::param::ParamType>,
start: u32,
num: u32,
filter: Option<ParamBuilder>,
) -> std::io::Result<()> {
let proxy = self.proxy();
proxy_object_invoke!(proxy, enum_params, seq, id, start, num, filter)
}
pub(crate) fn methods(&self) -> Arc<Mutex<PortMethods<Port>>> {
self.inner.methods.clone()
}
pub(crate) fn events(&self) -> Arc<Mutex<spa::hook::HookList<PortEvents>>> {
self.inner.hooks.clone()
}
}
impl InnerPort {
fn new(core: &Core) -> Self {
Self {
proxy: RwLock::new(None),
methods: Arc::new(Mutex::new(protocol::marshal::port::Methods::marshal(
core.connection(),
))),
hooks: spa::hook::HookList::new(),
}
}
}