1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * SPDX-License-Identifier: MIT
 */

//! Bindings for PipeWire [Core API](https://docs.pipewire.org/group__api__pw__core.html)
//!
use std::ffi::{CStr, CString};
use std::ops::{AddAssign, SubAssign};
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::Mutex;
use std::time::Duration;

use spa_sys::spa_support;

use crate::core_api::loop_::LoopRef;
use crate::core_api::main_loop::{MainLoop, MainLoopRef};
use crate::spa::dict::DictRef;
use crate::spa::handle::HandleRef;
use crate::spa::loop_::AsLoopRef;
use crate::spa::pod::object::param_port_config::Direction;
use crate::spa::support::SupportRef;
use crate::wrapper::{RawWrapper, Wrapper};
use crate::{error, i32_as_void_result, spa, SPA_ID_INVALID};

pub mod client;
pub mod context;
pub mod core;
pub mod device;
pub mod factory;
pub mod link;
pub mod loop_;
pub mod main_loop;
pub mod node;
pub mod permissions;
pub mod port;
pub mod properties;
pub mod proxy;
pub mod registry;
pub mod type_info;

pub const PW_ID_ANY: u32 = SPA_ID_INVALID;

static mut INSTANCES: Mutex<usize> = Mutex::new(0);

/// PipeWire structure
#[derive(Debug)]
pub struct PipeWire {}

impl PipeWire {
    /// Init the pipewire, can be called several times.
    ///
    /// # Arguments
    ///
    /// * `args` - arguments
    ///
    /// Returns PipeWire struct
    ///
    /// # Examples
    ///
    /// ```
    /// use pipewire_wrapper::core_api::PipeWire;
    /// let pipe_wire = PipeWire::init(&Vec::default());
    /// ```
    pub fn init(args: &Vec<&CStr>) -> PipeWire {
        unsafe {
            let argc = &mut (args.len() as i32) as *mut ::std::os::raw::c_int;
            let argv = args.as_ptr() as *mut *mut *mut ::std::os::raw::c_char;
            {
                let mut instances = INSTANCES.lock().unwrap(); // todo try to recover with decrement after (mutex_unpoison #96469)
                pw_sys::pw_init(argc, argv);
                instances.add_assign(1);
            }
        }
        PipeWire {}
    }

    /// Check if a debug category is enabled.
    /// Debugging categories can be enabled by using the PIPEWIRE_DEBUG environment variable.
    ///
    /// # Arguments
    ///
    /// * `name` - the name of the category to check
    pub fn debug_is_category_enabled(&self, name: &CString) -> bool {
        unsafe { pw_sys::pw_debug_is_category_enabled(name.as_ptr()) }
    }

    /// Application name.
    pub fn get_application_name(&self) -> Option<&CStr> {
        unsafe {
            pw_sys::pw_get_application_name()
                .as_ref()
                .map(|ptr| CStr::from_ptr(ptr))
        }
    }

    /// Program name.
    pub fn get_prgname(&self) -> Option<&CStr> {
        unsafe {
            pw_sys::pw_get_prgname()
                .as_ref()
                .map(|ptr| CStr::from_ptr(ptr))
        }
    }

    /// User name
    pub fn get_user_name(&self) -> Option<&CStr> {
        unsafe {
            pw_sys::pw_get_user_name()
                .as_ref()
                .map(|ptr| CStr::from_ptr(ptr))
        }
    }

    /// Host name
    pub fn get_host_name(&self) -> Option<&CStr> {
        unsafe {
            pw_sys::pw_get_host_name()
                .as_ref()
                .map(|ptr| CStr::from_ptr(ptr))
        }
    }

    /// Client name
    pub fn get_client_name(&self) -> Option<&CStr> {
        unsafe {
            pw_sys::pw_get_client_name()
                .as_ref()
                .map(|ptr| CStr::from_ptr(ptr))
        }
    }

    /// Is PipeWire running on Valgrind
    pub fn in_valgrind(&self) -> bool {
        unsafe { pw_sys::pw_in_valgrind() }
    }

    /// Check option switch, i.e. in-valgrind, no-color, no-config, do-dlclose
    pub fn check_option(&self, option: &CStr, value: &CStr) -> bool {
        unsafe { pw_sys::pw_check_option(option.as_ptr(), value.as_ptr()) }
    }

    /// Get reversed [Direction]
    pub fn direction_reverse(direction: &Direction) -> Direction {
        Direction::from_raw(unsafe { pw_sys::pw_direction_reverse(*direction.as_raw()) })
    }

    /// Set domain
    pub fn set_domain(&self, domain: &CStr) -> crate::Result<()> {
        unsafe { i32_as_void_result(pw_sys::pw_set_domain(domain.as_ptr())) }
    }

    /// Domain
    pub fn get_domain(&self) -> Option<&CStr> {
        unsafe {
            pw_sys::pw_get_domain()
                .as_ref()
                .map(|ptr| CStr::from_ptr(ptr))
        }
    }

    /// Get support list
    pub fn get_spa_support(&self, max_support_elements: usize) -> Vec<SupportRef> {
        let mut support_vec: Vec<SupportRef> = Vec::with_capacity(max_support_elements);
        let support_count = unsafe {
            pw_sys::pw_get_support(
                support_vec.as_mut_ptr() as *mut spa_support,
                max_support_elements as u32,
            )
        };
        support_vec.truncate(support_count as usize);
        support_vec
    }

    /// Load SPA handle
    pub fn load_spa_handle(
        &self,
        lib: &CStr,
        factory_name: &CStr,
        info: &DictRef,
        support: Vec<SupportRef>,
    ) -> Option<&HandleRef> {
        let handle = unsafe {
            pw_sys::pw_load_spa_handle(
                lib.as_ptr(),
                factory_name.as_ptr(),
                info.as_raw_ptr(),
                support.len() as u32,
                support.as_ptr() as *const spa_sys::spa_support,
            )
        };
        unsafe { (handle as *mut HandleRef).as_ref() }
    }

    /// Unload handle
    pub fn unload_spa_handle(&self, handle: &HandleRef) -> crate::Result<()> {
        unsafe { i32_as_void_result(pw_sys::pw_unload_spa_handle(handle.as_raw_ptr())) }
    }
}

impl Drop for PipeWire {
    fn drop(&mut self) {
        unsafe {
            let mut instances = INSTANCES.lock().unwrap();
            if *instances == 0 {
                pw_sys::pw_deinit();
            } else {
                instances.sub_assign(1);
            }
        }
    }
}

impl Default for PipeWire {
    fn default() -> Self {
        PipeWire::init(&Vec::default())
    }
}