libspa/param/
mod.rs

1// Copyright The pipewire-rs Contributors.
2// SPDX-License-Identifier: MIT
3
4//! Types for dealing with SPA parameters.
5
6pub mod audio;
7pub mod format;
8pub mod format_utils;
9pub mod video;
10
11use std::ffi::CStr;
12use std::fmt::Debug;
13
14/// Different parameter types that can be queried
15#[derive(Copy, Clone, PartialEq, Eq)]
16pub struct ParamType(pub spa_sys::spa_param_type);
17
18#[allow(non_upper_case_globals)]
19impl ParamType {
20    /// invalid
21    pub const Invalid: Self = Self(spa_sys::SPA_PARAM_Invalid);
22    /// property information as SPA_TYPE_OBJECT_PropInfo
23    pub const PropInfo: Self = Self(spa_sys::SPA_PARAM_PropInfo);
24    /// properties as SPA_TYPE_OBJECT_Props
25    pub const Props: Self = Self(spa_sys::SPA_PARAM_Props);
26    /// available formats as SPA_TYPE_OBJECT_Format
27    pub const EnumFormat: Self = Self(spa_sys::SPA_PARAM_EnumFormat);
28    /// configured format as SPA_TYPE_OBJECT_Format
29    pub const Format: Self = Self(spa_sys::SPA_PARAM_Format);
30    /// buffer configurations as SPA_TYPE_OBJECT_ParamBuffers
31    pub const Buffers: Self = Self(spa_sys::SPA_PARAM_Buffers);
32    /// allowed metadata for buffers as SPA_TYPE_OBJECT_ParamMeta
33    pub const Meta: Self = Self(spa_sys::SPA_PARAM_Meta);
34    /// configurable IO areas as SPA_TYPE_OBJECT_ParamIO
35    pub const IO: Self = Self(spa_sys::SPA_PARAM_IO);
36    /// profile enumeration as SPA_TYPE_OBJECT_ParamProfile
37    pub const EnumProfile: Self = Self(spa_sys::SPA_PARAM_EnumProfile);
38    /// profile configuration as SPA_TYPE_OBJECT_ParamProfile
39    pub const Profile: Self = Self(spa_sys::SPA_PARAM_Profile);
40    /// port configuration enumeration as SPA_TYPE_OBJECT_ParamPortConfig
41    pub const EnumPortConfig: Self = Self(spa_sys::SPA_PARAM_EnumPortConfig);
42    /// port configuration as SPA_TYPE_OBJECT_ParamPortConfig
43    pub const PortConfig: Self = Self(spa_sys::SPA_PARAM_PortConfig);
44    /// routing enumeration as SPA_TYPE_OBJECT_ParamRoute
45    pub const EnumRoute: Self = Self(spa_sys::SPA_PARAM_EnumRoute);
46    /// routing configuration as SPA_TYPE_OBJECT_ParamRoute
47    pub const Route: Self = Self(spa_sys::SPA_PARAM_Route);
48    /// Control parameter, a SPA_TYPE_Sequence
49    pub const Control: Self = Self(spa_sys::SPA_PARAM_Control);
50    /// latency reporting, a SPA_TYPE_OBJECT_ParamLatency
51    pub const Latency: Self = Self(spa_sys::SPA_PARAM_Latency);
52    /// processing latency, a SPA_TYPE_OBJECT_ParamProcessLatency
53    pub const ProcessLatency: Self = Self(spa_sys::SPA_PARAM_ProcessLatency);
54
55    /// Obtain a [`ParamType`] from a raw `spa_param_type` variant.
56    pub fn from_raw(raw: spa_sys::spa_param_type) -> Self {
57        Self(raw)
58    }
59
60    /// Get the raw [`spa_sys::spa_param_type`] representing this `ParamType`.
61    pub fn as_raw(&self) -> spa_sys::spa_param_type {
62        self.0
63    }
64}
65
66impl Debug for ParamType {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        let c_str = unsafe {
69            let c_buf =
70                spa_sys::spa_debug_type_find_short_name(spa_sys::spa_type_param, self.as_raw());
71            if c_buf.is_null() {
72                return f.write_str("Unknown");
73            }
74            CStr::from_ptr(c_buf)
75        };
76        let name = format!("ParamType::{}", c_str.to_string_lossy());
77        f.write_str(&name)
78    }
79}
80
81bitflags::bitflags! {
82    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
83    pub struct ParamInfoFlags: u32 {
84        const SERIAL = 1<<0;
85        const READ   = 1<<1;
86        const WRITE  = 1<<2;
87        const READWRITE = Self::READ.bits() | Self::WRITE.bits();
88    }
89}
90
91/// Information about a parameter
92#[repr(transparent)]
93pub struct ParamInfo(spa_sys::spa_param_info);
94
95impl ParamInfo {
96    pub fn id(&self) -> ParamType {
97        ParamType::from_raw(self.0.id)
98    }
99
100    pub fn flags(&self) -> ParamInfoFlags {
101        ParamInfoFlags::from_bits_truncate(self.0.flags)
102    }
103}
104
105impl Debug for ParamInfo {
106    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107        f.debug_struct("ParamInfo")
108            .field("id", &self.id())
109            .field("flags", &self.flags())
110            .finish()
111    }
112}