rill-io 0.5.0-beta.4

Audio I/O backends for Rill - CPAL, ALSA, PipeWire, JACK
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! PipeWire backend for Linux
//!
//! Uses `pipewire` (0.9) with `MainLoopRc` / `ContextRc` / `StreamBox`.
//! Output writes directly to the PW DMA buffer via OutputWindow (no ring buffer).
//! Input still uses IoRingBuffer.
//!
//! `run()` — blocking: initializes PW, creates context/core/streams,
//! enters mainloop iterate loop. Exits when `running` becomes false.
//! No `std::thread`, `std::sync`.

use std::fmt;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;

use pipewire as pw;
use pw::properties::properties;
use pw::spa;
use pw::spa::sys as spa_sys;

use crate::buffer::IoRingBuffer;
use crate::config::AudioConfig;
use crate::error::{IoError, IoResult};
use crate::output_window::{OutputSlot, OutputWindow};
use crate::PwBuffers;
use rill_core::io::IoBackend;

/// Maximum stereo block in samples (4096 frames × 2 channels).
const MAX_BLOCK_SAMPLES: usize = 8192;

/// Callback slot.
#[derive(Copy, Clone)]
struct CbSlot(usize);

impl CbSlot {
    fn new() -> Self {
        Self(Box::into_raw(Box::new(None::<Box<dyn Fn(f32)>>)) as usize)
    }
    unsafe fn set(&self, cb: Box<dyn Fn(f32)>) {
        (*(self.0 as *mut Option<Box<dyn Fn(f32)>>)) = Some(cb);
    }
    unsafe fn call(&self, sr: f32) {
        if let Some(ref cb) = *(self.0 as *mut Option<Box<dyn Fn(f32)>>) {
            cb(sr);
        }
    }
    unsafe fn drop_box(&self) {
        drop(Box::from_raw(self.0 as *mut Option<Box<dyn Fn(f32)>>));
    }
}

// PipewireBackend
// ============================================================================

/// Direct capture data — set by capture callback, read by generate().
/// Valid only during process_cb.call() on the PW RT thread.
/// PipeWire audio backend — processes audio via PW stream callbacks,
/// output goes directly to PW DMA buffer through `OutputWindow`.
pub struct PipewireBackend {
    config: AudioConfig,
    input_buffer: Arc<IoRingBuffer>,
    process_cb: CbSlot,
    xruns: Arc<AtomicU32>,
    running: Arc<AtomicBool>,
    output_slot: OutputSlot,
    negotiated_input_channels: Arc<AtomicU32>,
    negotiated_input_rate: Arc<AtomicU32>,
}

impl fmt::Debug for PipewireBackend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PipewireBackend")
            .field("config", &self.config)
            .finish()
    }
}

impl PipewireBackend {
    /// Create a new PipeWire backend.
    pub fn new(config: AudioConfig) -> IoResult<Self> {
        if !cfg!(target_os = "linux") {
            return Err(IoError::Unsupported(
                "PipeWire is only available on Linux".into(),
            ));
        }

        Ok(Self {
            input_buffer: Arc::new(IoRingBuffer::new(
                (config.buffer_size * config.input_channels.max(1) * 32) as usize,
            )),
            config,
            process_cb: CbSlot::new(),
            xruns: Arc::new(AtomicU32::new(0)),
            running: Arc::new(AtomicBool::new(false)),
            output_slot: OutputSlot::new(),
            negotiated_input_channels: Arc::new(AtomicU32::new(0)),
            negotiated_input_rate: Arc::new(AtomicU32::new(0)),
        })
    }

    /// Return the negotiated sample rate from PipeWire, or 0 if not yet negotiated.
    pub fn negotiated_rate(&self) -> u32 {
        self.negotiated_input_rate.load(Ordering::Relaxed)
    }

    /// Return the negotiated channel count from PipeWire, or 0 if not yet negotiated.
    pub fn negotiated_channels(&self) -> u32 {
        self.negotiated_input_channels.load(Ordering::Relaxed)
    }

    /// Return shared ring buffers for injection into AudioInput/AudioOutput.
    pub fn rings(&self) -> Arc<PwBuffers> {
        Arc::new(PwBuffers {
            input: self.input_buffer.clone(),
            output: Arc::new(IoRingBuffer::new(0)),
        })
    }
}

// ============================================================================
// IoBackend impl
// ============================================================================

impl IoBackend<f32> for PipewireBackend {
    fn set_process_callback(&self, cb: Box<dyn Fn(f32)>) {
        unsafe {
            self.process_cb.set(cb);
        }
    }

    fn read(&self, channels: &mut [&mut [f32]]) -> usize {
        let frames = channels.first().map(|c| c.len()).unwrap_or(0);
        if frames == 0 {
            return 0;
        }
        let out_ch = {
            let c = self.negotiated_input_channels.load(Ordering::Relaxed);
            if c > 0 {
                c as usize
            } else {
                self.config.input_channels.max(1) as usize
            }
        };
        let mut temp = [0.0f32; MAX_BLOCK_SAMPLES];
        let max_s = frames.saturating_mul(out_ch).min(MAX_BLOCK_SAMPLES);
        let n_read = self.input_buffer.read(&mut temp[..max_s]);
        let frames_out = n_read / out_ch;
        let out = frames_out.min(frames);
        if out_ch >= 2 {
            for i in 0..out {
                if let Some(c) = channels.get_mut(0) {
                    c[i] = temp[i * out_ch];
                }
                if let Some(c) = channels.get_mut(1) {
                    c[i] = temp[i * out_ch + 1];
                }
            }
        } else {
            for i in 0..out {
                if let Some(c) = channels.get_mut(0) {
                    c[i] = temp[i];
                }
                if let Some(c) = channels.get_mut(1) {
                    c[i] = temp[i];
                }
            }
        }
        out
    }

    fn write(&self, channels: &[&[f32]]) -> usize {
        let nch = channels.len();
        if nch == 0 {
            return 0;
        }
        let frames = channels[0].len();
        if let Some(win) = unsafe { self.output_slot.as_mut() } {
            let cap = win.capacity().min(frames * nch);
            let dst = win.as_mut_slice();
            for i in 0..frames {
                for ch in 0..nch {
                    dst[i * nch + ch] = channels[ch][i];
                }
            }
            cap / nch
        } else {
            0
        }
    }

    fn run(&self, running: Arc<AtomicBool>) -> Result<(), String> {
        let process_cb = self.process_cb;
        let oslot = self.output_slot.clone();
        let ibuf = self.input_buffer.clone();
        let xruns = self.xruns.clone();
        let sample_rate = self.config.sample_rate;
        let out_channels = self.config.output_channels;
        let in_channels = self.config.input_channels;
        let out_device = self.config.output_device.clone();
        let in_device = self.config.input_device.clone();

        pw::init();

        let mainloop =
            pw::main_loop::MainLoopRc::new(None).map_err(|e| format!("PW MainLoopRc::new: {e}"))?;
        let context = pw::context::ContextRc::new(&mainloop, None)
            .map_err(|e| format!("PW ContextRc::new: {e}"))?;
        let core = context
            .connect_rc(None)
            .map_err(|e| format!("PW core.connect_rc: {e}"))?;

        // Output stream and listener — alive for the duration of run()
        let _out_stream;
        let _out_listener;
        let ml = mainloop.clone();
        let ml2 = ml.clone();
        let running2 = running.clone();

        if out_channels > 0 {
            let out_chan = out_channels;
            let buf_frames = self.config.buffer_size as usize;
            let chunk_frames = buf_frames;
            let chunk_bytes = chunk_frames * out_chan as usize * 4;
            let out_node = out_device.as_deref().unwrap_or("rill-output");
            let out_desc = format!("Rill Audio Output ({out_node})");
            let mut out_props = properties! {
                *pw::keys::MEDIA_TYPE => "Audio",
                *pw::keys::MEDIA_ROLE => "Music",
                *pw::keys::MEDIA_CATEGORY => "Playback",
                *pw::keys::NODE_NAME => out_node,
                *pw::keys::NODE_DESCRIPTION => out_desc.as_str(),
            };
            out_props.insert("audio.channels", out_chan.to_string());

            let stream =
                pw::stream::StreamBox::new(&core, &format!("{out_node}-output"), out_props)
                    .map_err(|e| format!("PW StreamBox output: {e}"))?;

            let out_running = running.clone();
            let out_ml = ml.clone();
            let out_sr = sample_rate;
            let listener = stream
                .add_local_listener_with_user_data(())
                .process(move |s, _| {
                    if !out_running.load(Ordering::Acquire) {
                        out_ml.quit();
                        return;
                    }
                    let mut buf = match s.dequeue_buffer() {
                        Some(b) => b,
                        None => return,
                    };
                    let datas = buf.datas_mut();
                    if datas.is_empty() {
                        return;
                    }
                    let data = &mut datas[0];
                    let slice = match data.data() {
                        Some(s) => s,
                        None => return,
                    };
                    let stride = out_chan as usize * 4;
                    let n_frames = slice.len() / stride;
                    let mut offset = 0usize;
                    while offset + chunk_bytes <= slice.len() {
                        let chunk = &mut slice[offset..offset + chunk_bytes];
                        unsafe {
                            oslot.set(OutputWindow::new(
                                chunk.as_mut_ptr() as *mut f32,
                                chunk_frames * out_chan as usize,
                            ));
                            process_cb.call(out_sr as f32);
                            oslot.clear();
                        }
                        offset += chunk_bytes;
                    }
                    if offset < slice.len() {
                        slice[offset..].fill(0);
                    }
                    let ck = data.chunk_mut();
                    *ck.offset_mut() = 0;
                    *ck.stride_mut() = stride as i32;
                    *ck.size_mut() = (stride * n_frames) as u32;
                })
                .register()
                .map_err(|e| format!("PW output listener: {e}"))?;
            _out_listener = Some(listener);

            let mut audio_info = spa::param::audio::AudioInfoRaw::new();
            audio_info.set_format(spa::param::audio::AudioFormat::F32LE);
            audio_info.set_rate(sample_rate);
            audio_info.set_channels(out_chan);
            let mut position = [0; spa::param::audio::MAX_CHANNELS];
            if out_chan >= 1 {
                position[0] = spa_sys::SPA_AUDIO_CHANNEL_FL;
            }
            if out_chan >= 2 {
                position[1] = spa_sys::SPA_AUDIO_CHANNEL_FR;
            }
            audio_info.set_position(position);

            let params_bytes: Vec<u8> = spa::pod::serialize::PodSerializer::serialize(
                std::io::Cursor::new(Vec::new()),
                &spa::pod::Value::Object(spa::pod::Object {
                    type_: spa_sys::SPA_TYPE_OBJECT_Format,
                    id: spa_sys::SPA_PARAM_EnumFormat,
                    properties: audio_info.into(),
                }),
            )
            .unwrap()
            .0
            .into_inner();
            let mut out_params = [spa::pod::Pod::from_bytes(&params_bytes).unwrap()];

            if let Err(e) = stream.connect(
                spa::utils::Direction::Output,
                None,
                pw::stream::StreamFlags::AUTOCONNECT
                    | pw::stream::StreamFlags::MAP_BUFFERS
                    | pw::stream::StreamFlags::RT_PROCESS,
                &mut out_params,
            ) {
                return Err(format!("PW output connect: {e}"));
            }

            _out_stream = Some(stream);
        } else {
            _out_listener = None;
            _out_stream = None;
        }

        self.running.store(true, Ordering::Release);

        // ── Input stream ────────────────────────────────────────────────────
        let in_node = in_device.as_deref().unwrap_or("rill-input");
        let in_desc = format!("Rill Audio Input ({in_node})");
        let mut in_props = properties! {
            *pw::keys::MEDIA_TYPE => "Audio",
            *pw::keys::MEDIA_ROLE => "Music",
            *pw::keys::MEDIA_CATEGORY => "Capture",
            *pw::keys::NODE_NAME => in_node,
            *pw::keys::NODE_DESCRIPTION => in_desc.as_str(),
        };
        in_props.insert("audio.channels", in_channels.to_string());
        in_props.insert(
            *pw::keys::NODE_LATENCY,
            format!("{}/{}", self.config.buffer_size, sample_rate),
        );

        let in_stream =
            match pw::stream::StreamBox::new(&core, &format!("{in_node}-input"), in_props) {
                Ok(s) => Some(s),
                Err(e) => {
                    log::warn!("PW StreamBox input: {e} — capture disabled");
                    None
                }
            };

        // Input listener — MUST live until end of run() or PW stops calling it.
        let _in_listener;

        if let Some(ref in_st) = in_stream {
            let mut in_ai = spa::param::audio::AudioInfoRaw::new();
            in_ai.set_format(spa::param::audio::AudioFormat::F32LE);
            // Don't set rate/channels — let PW negotiate.

            let in_params_bytes: Vec<u8> = spa::pod::serialize::PodSerializer::serialize(
                std::io::Cursor::new(Vec::new()),
                &spa::pod::Value::Object(spa::pod::Object {
                    type_: spa_sys::SPA_TYPE_OBJECT_Format,
                    id: spa_sys::SPA_PARAM_EnumFormat,
                    properties: in_ai.into(),
                }),
            )
            .unwrap()
            .0
            .into_inner();
            let mut in_params = [spa::pod::Pod::from_bytes(&in_params_bytes).unwrap()];

            let buf_frames = self.config.buffer_size as usize;
            let nch_fmt = self.negotiated_input_channels.clone();
            let nrate_fmt = self.negotiated_input_rate.clone();
            let nch_proc = self.negotiated_input_channels.clone();
            let nrate_proc = self.negotiated_input_rate.clone();

            let listener = in_st
                .add_local_listener_with_user_data(())
                .param_changed(move |_stream, _data, id, param| {
                    if id == spa_sys::SPA_PARAM_Format {
                        if let Some(param) = param {
                            let mut ai = spa::param::audio::AudioInfoRaw::new();
                            if ai.parse(param).is_ok() {
                                nch_fmt.store(ai.channels(), std::sync::atomic::Ordering::Relaxed);
                                nrate_fmt.store(ai.rate(), std::sync::atomic::Ordering::Relaxed);
                            }
                        }
                    }
                })
                .process(move |stream, _| {
                    if !running2.load(Ordering::Acquire) {
                        ml2.quit();
                        return;
                    }
                    let mut buf = match stream.dequeue_buffer() {
                        Some(b) => b,
                        None => {
                            xruns.fetch_add(1, Ordering::Relaxed);
                            return;
                        }
                    };
                    let datas = buf.datas_mut();
                    if datas.is_empty() {
                        return;
                    }
                    let data = &mut datas[0];

                    let actual_channels = {
                        let c = nch_proc.load(std::sync::atomic::Ordering::Relaxed);
                        if c > 0 {
                            c as usize
                        } else {
                            in_channels as usize
                        }
                    };

                    let (_chunk_offset, chunk_size) = {
                        let ck = data.chunk_mut();
                        (*ck.offset_mut(), *ck.size_mut())
                    };

                    if chunk_size == 0 {
                        return;
                    }

                    let slice = match data.data() {
                        Some(s) => s,
                        None => return,
                    };
                    let stride = actual_channels * 4;
                    let n_samp = (chunk_size as usize / stride) * actual_channels;
                    let len = n_samp.min(MAX_BLOCK_SAMPLES);
                    let mut temp = [0.0f32; MAX_BLOCK_SAMPLES];
                    for (i, item) in temp.iter_mut().enumerate().take(len) {
                        let off = i * 4;
                        if off + 4 <= slice.len() {
                            let mut bytes = [0u8; 4];
                            bytes.copy_from_slice(&slice[off..off + 4]);
                            *item = f32::from_le_bytes(bytes);
                        }
                    }
                    let block_samps = buf_frames * actual_channels;
                    ibuf.write(&temp[..len]);
                    if out_channels == 0 {
                        while ibuf.len() >= block_samps {
                            unsafe {
                                let sr =
                                    nrate_proc.load(std::sync::atomic::Ordering::Relaxed) as f32;
                                process_cb.call(if sr > 0.0 { sr } else { sample_rate as f32 });
                            }
                        }
                    }
                })
                .register()
                .map_err(|e| format!("PW input listener: {e}"))?;
            _in_listener = Some(listener);

            if let Err(e) = in_st.connect(
                spa::utils::Direction::Input,
                None,
                pw::stream::StreamFlags::AUTOCONNECT
                    | pw::stream::StreamFlags::MAP_BUFFERS
                    | pw::stream::StreamFlags::DRIVER,
                &mut in_params,
            ) {
                log::warn!("PW input connect: disabled — {e}");
            }
        } else {
            _in_listener = None;
        }

        // ── PW event loop ───────────────────────────────────────────────────
        // No own loop — run() blocks the thread, events are
        // handled by PW. Callback checks running and calls quit.
        mainloop.run();

        if let Some(ref s) = in_stream {
            let _ = s.disconnect();
        }

        Ok(())
    }

    fn stop(&self) -> Result<(), String> {
        self.running.store(false, Ordering::Release);
        Ok(())
    }
}

impl Drop for PipewireBackend {
    fn drop(&mut self) {
        self.running.store(false, Ordering::Release);
        unsafe {
            self.process_cb.drop_box();
        }
    }
}