pipewire-native 0.1.4

A Rust implementation of the PipeWire client library
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (c) 2025 Asymptotic Inc.
// SPDX-FileCopyrightText: Copyright (c) 2025 Sanchayan Maity

use pipewire_native_spa as spa;

use spa::flags;
use spa::interface::r#loop::LoopUtilsSource;
use spa::{emit_hook, hook::HookList};

use std::os::fd::RawFd;
use std::pin::Pin;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};

use crate::{debug, default_topic, log, new_refcounted, properties::Properties, refcounted, trace};
use crate::{HookId, GLOBAL_SUPPORT};

default_topic!(log::topic::MAIN_LOOP);

/// Main loop events
pub struct MainLoopEvents {
    /// The main loop was destroyed.
    pub destroy: Option<Box<dyn FnMut() + Send>>,
}

#[derive(Clone)]
pub(crate) struct LoopSupport {
    #[allow(dead_code)]
    handle: Arc<Box<dyn spa::interface::plugin::Handle + Send + Sync>>,
    pub(crate) loop_: Arc<Pin<Box<spa::interface::r#loop::LoopImpl>>>,
    pub(crate) loop_utils: Arc<Pin<Box<spa::interface::r#loop::LoopUtilsImpl>>>,
    pub(crate) loop_control: Arc<Pin<Box<spa::interface::r#loop::LoopControlImpl>>>,
}

/// An event source on the mainloop.
pub struct Source {
    inner: Pin<Box<LoopUtilsSource>>,
}

impl Source {
    /// Mask representing current set of even types the source is interested in.
    pub fn mask(&self) -> spa::flags::Io {
        self.inner.mask
    }

    fn from_loop_utils(source: Pin<Box<LoopUtilsSource>>) -> Self {
        Source { inner: source }
    }
}

/// Callback for events triggered by [MainLoop::signal_event]
pub type SourceEventFn = spa::interface::r#loop::SourceEventFn;
/// Callback for events triggered by [MainLoop] idle events.
pub type SourceIdleFn = spa::interface::r#loop::SourceIdleFn;
/// Callback for events triggered by [MainLoop] I/O events.
pub type SourceIoFn = spa::interface::r#loop::SourceIoFn;
/// Callback for events triggered by signals on the [MainLoop].
pub type SourceSignalFn = spa::interface::r#loop::SourceSignalFn;
/// Callback for timer events on the [MainLoop].
pub type SourceTimerFn = spa::interface::r#loop::SourceTimerFn;

refcounted! {
    /// An event loop implementation. This event loop is used for communication with PipeWire, and
    /// provides a number of primitives for integration with applications and any other event loops
    /// or event sources they might have.
    ///
    /// Many of these primitives are quite low-level, and it is also possible to use the main loop
    /// only for PipeWire-related events. This can be done by creating the [MainLoop] and invoking
    /// [MainLoop::run] on a separate thread. In this case, it is important to make sure that any
    /// access to common data structures is protected from concurrent access. The [MainLoop::lock]
    /// and [MainLoop::unlock] methods provide such a mechanism.
    pub struct MainLoop {
        support: LoopSupport,
        // This is an atomic because it is hard to convince Rust that this will only be mutated on one
        // thread (i.e. the one on which run() is called
        running: AtomicBool,
        name: String,
        hooks: Arc<Mutex<HookList<MainLoopEvents>>>,
    }
}

impl MainLoop {
    /// Create a new main loop.
    pub fn new(props: &Properties) -> Option<MainLoop> {
        let l = InnerMainLoop::new(props)?;

        debug!("Creating main loop");

        Some(MainLoop {
            inner: new_refcounted(l),
        })
    }

    pub(crate) fn set_running(&self) -> std::io::Result<()> {
        if self
            .inner
            .running
            .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
            .is_err()
        {
            Err(std::io::Error::from(std::io::ErrorKind::AlreadyExists))
        } else {
            Ok(())
        }
    }

    pub(crate) fn run_once(&self) -> std::io::Result<i32> {
        if !self.inner.running.load(Ordering::Relaxed) {
            return Err(std::io::Error::from(std::io::ErrorKind::NotConnected));
        }

        self.inner.support.loop_control.enter();

        let res = self
            .inner
            .support
            .loop_control
            .iterate(Some(std::time::Duration::MAX));

        self.inner.support.loop_control.leave();

        res
    }

    /// Run the main loop. This takes over executation of the current thread until [MainLoop::quit]
    /// is invoked.
    pub fn run(&self) {
        if self
            .inner
            .running
            .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
            .is_err()
        {
            return;
        }

        self.inner.support.loop_control.enter();

        while self.inner.running.load(Ordering::Relaxed) {
            if let Err(res) = self
                .inner
                .support
                .loop_control
                .iterate(Some(std::time::Duration::MAX))
            {
                if res.kind() == std::io::ErrorKind::Interrupted {
                    continue;
                }
            }
        }

        self.inner.support.loop_control.leave();
    }

    /// Terminate execution of the main loop.
    pub fn quit(&self) {
        debug!("quit");

        let this = self.clone();

        let stop = move |_block: bool, _seq: u32, _data: &[u8]| {
            this.inner.running.store(false, Ordering::Relaxed);
            0
        };

        let _ = self
            .inner
            .support
            .loop_
            .invoke(1, &[], false, Box::new(stop));
    }

    /// Add a listener for main loop events.
    pub fn add_listener(&self, events: MainLoopEvents) -> HookId {
        self.inner.hooks.lock().unwrap().append(events)
    }

    /// Remove a set of event listeners.
    pub fn remove_listener(&self, hook_id: HookId) {
        self.inner.hooks.lock().unwrap().remove(hook_id);
    }

    // Loop control methods
    // TODO: decide how we want to expose these, if at all
    #[doc(hidden)]
    pub fn get_fd(&self) -> RawFd {
        self.inner.support.loop_control.get_fd() as RawFd
    }

    #[doc(hidden)]
    pub fn enter(&self) {
        trace!("enter");
        self.inner.support.loop_control.enter()
    }

    #[doc(hidden)]
    pub fn leave(&self) {
        trace!("leave");
        self.inner.support.loop_control.leave()
    }

    /// Run one iteration of the loop, returning if no events occurred in `timeout` time. A value
    /// of `None` signifies an infinite timeout. Returns the number of iterations performed.
    pub fn iterate(&self, timeout: Option<std::time::Duration>) -> std::io::Result<i32> {
        trace!("iterate");
        self.inner.support.loop_control.iterate(timeout)
    }

    #[doc(hidden)]
    pub fn check(&self) -> std::io::Result<i32> {
        self.inner.support.loop_control.check()
    }

    /// Take the main loop lock, preventing it from running until the lock is released. The main
    /// loop takes this lock in each of its iterations.
    pub fn lock(&self) -> std::io::Result<()> {
        trace!("lock");
        self.inner.support.loop_control.lock()?;
        Ok(())
    }

    /// Release the main loop lock, allowing it to run again.
    pub fn unlock(&self) -> std::io::Result<()> {
        trace!("unlock");
        self.inner.support.loop_control.unlock()?;
        Ok(())
    }

    /// Get the time corresponding to the specified timeout, which can be used with
    /// [MainLoop::wait].
    pub fn get_time(&self, timeout: std::time::Duration) -> std::io::Result<libc::timespec> {
        self.inner.support.loop_control.get_time(timeout)
    }

    /// Wait until the specified time or until signalled.
    pub fn wait(&self, abstime: &libc::timespec) -> std::io::Result<()> {
        debug!("wait");
        self.inner.support.loop_control.wait(abstime)?;
        Ok(())
    }

    /// Signal any threads waiting with [MainLoop::wait] to wake up.
    pub fn signal(&self, wait_for_accept: bool) -> std::io::Result<()> {
        debug!("signal");
        self.inner.support.loop_control.signal(wait_for_accept)?;
        Ok(())
    }

    /// Acknowledge a [MainLoop::signal] with `wait_for_accept: true`, allowing that thread to
    /// proceed.
    pub fn accept(&self) -> std::io::Result<()> {
        debug!("accept");
        self.inner.support.loop_control.accept()?;
        Ok(())
    }

    /// Add an I/O event source using the given `fd`.
    pub fn add_io(
        &self,
        fd: RawFd,
        mask: flags::Io,
        close: bool,
        func: Box<SourceIoFn>,
    ) -> Option<Source> {
        self.inner
            .support
            .loop_utils
            .add_io(fd, mask, close, func)
            .map(Source::from_loop_utils)
    }

    /// Update the set of events of interest (given by `mask`).
    pub fn update_io(&self, source: &mut Source, mask: flags::Io) -> std::io::Result<i32> {
        self.inner
            .support
            .loop_utils
            .update_io(&mut source.inner, mask)
    }

    /// Add an idle event source (called after each iteration of the main loop).
    pub fn add_idle(&self, enabled: bool, func: Box<SourceIdleFn>) -> Option<Source> {
        self.inner
            .support
            .loop_utils
            .add_idle(enabled, func)
            .map(Source::from_loop_utils)
    }

    /// Enable or disable an idle event source.
    pub fn enable_idle(&self, source: &mut Source, enabled: bool) -> std::io::Result<i32> {
        debug!("idle {enabled}");
        self.inner
            .support
            .loop_utils
            .enable_idle(&mut source.inner, enabled)
    }

    /// Add a source which can be triggered using [Self::signal_event].
    pub fn add_event(&self, func: Box<SourceEventFn>) -> Option<Source> {
        self.inner
            .support
            .loop_utils
            .add_event(func)
            .map(Source::from_loop_utils)
    }

    /// Signal a source added with [Self::add_event].
    pub fn signal_event(&self, source: &mut Source) -> std::io::Result<i32> {
        self.inner
            .support
            .loop_utils
            .signal_event(&mut source.inner)
    }

    /// Add a timer source.
    pub fn add_timer(&self, func: Box<SourceTimerFn>) -> Option<Source> {
        self.inner
            .support
            .loop_utils
            .add_timer(func)
            .map(Source::from_loop_utils)
    }

    /// Update the timer interval of a timer source.
    pub fn update_timer(
        &self,
        source: &mut Source,
        value: &libc::timespec,
        interval: Option<&libc::timespec>,
        absolute: bool,
    ) -> std::io::Result<i32> {
        self.inner
            .support
            .loop_utils
            .update_timer(&mut source.inner, value, interval, absolute)
    }

    /// Add a source triggered by UNIX signals.
    pub fn add_signal(&self, signal_number: i32, func: Box<SourceSignalFn>) -> Option<Source> {
        self.inner
            .support
            .loop_utils
            .add_signal(signal_number, func)
            .map(Source::from_loop_utils)
    }

    /// Remove and destroy an event source.
    pub fn destroy_source(&self, source: Source) {
        self.inner.support.loop_utils.destroy_source(source.inner)
    }

    /// Set the main loop name.
    pub fn set_name(&mut self, name: &str) {
        debug!("main loop name {name}");
        if let Some(inner) = Arc::get_mut(&mut self.inner) {
            inner.name = name.to_string()
        }
    }

    pub(crate) fn support(&self) -> LoopSupport {
        self.inner.support.clone()
    }
}

impl Drop for InnerMainLoop {
    fn drop(&mut self) {
        self.destroy();
    }
}

impl InnerMainLoop {
    pub fn new(props: &Properties) -> Option<InnerMainLoop> {
        let support = GLOBAL_SUPPORT
            .get()
            .expect("Global support should be initialised");

        let handle = support
            .load_spa_handle(None, spa::interface::plugin::LOOP_FACTORY, None)
            .ok()?;

        let loop_ = handle.get_interface(spa::interface::LOOP).and_then(|i| {
            Arc::new(Box::into_pin(i))
                .downcast_arc_pin_box::<spa::interface::r#loop::LoopImpl>()
                .ok()
        })?;
        let loop_utils = handle
            .get_interface(spa::interface::LOOP_UTILS)
            .and_then(|i| {
                Arc::new(Box::into_pin(i))
                    .downcast_arc_pin_box::<spa::interface::r#loop::LoopUtilsImpl>()
                    .ok()
            })?;
        let loop_control = handle
            .get_interface(spa::interface::LOOP_CONTROL)
            .and_then(|i| {
                Arc::new(Box::into_pin(i))
                    .downcast_arc_pin_box::<spa::interface::r#loop::LoopControlImpl>()
                    .ok()
            })?;

        let name = if let Some(n) = props.get("loop.name") {
            n.to_string()
        } else {
            "main.loop".to_string()
        };

        Some(InnerMainLoop {
            support: LoopSupport {
                handle: Arc::new(handle),
                loop_,
                loop_utils,
                loop_control,
            },
            running: AtomicBool::new(false),
            name,
            hooks: HookList::new(),
        })
    }

    fn destroy(&self) {
        emit_hook!(self.hooks, destroy);
    }
}