maolan 0.2.0

Rust DAW application for recording, editing, routing, automation, export, and plugin hosting
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
#![cfg(test)]

mod scanner;
mod watchdog;

use maolan_plugin_host::events::EventPair;
use maolan_plugin_host::protocol::*;
use maolan_plugin_host::shm::ShmMapping;

use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::sync::atomic::Ordering;
use std::time::Duration;

/// Spawn the standalone `maolan-plugin-host` binary.
///
/// Returns the [`Child`] process handle, the [`ShmMapping`], and the
/// [`EventPair`] used to wake / wait on the host.
/// The caller must later call [`shutdown_host`] to avoid leaking the shm segment.
fn spawn_plugin_host(
    format: &str,
    plugin_path: &str,
    instance_id: &str,
) -> Result<(Child, ShmMapping, EventPair), String> {
    let pid = std::process::id();
    let shm_name = format!("/maolan-{pid}-{instance_id}");

    let mapping = ShmMapping::create(&shm_name, SHM_SIZE)?;
    unsafe {
        init_shm_layout(mapping.as_ptr(), mapping.size());
    }

    let mut events = EventPair::new().map_err(|e| format!("failed to create event pipes: {e}"))?;

    let host_bin = find_plugin_host_binary()
        .ok_or_else(|| "maolan-plugin-host binary not found".to_string())?;

    let mut cmd = Command::new(&host_bin);
    cmd.arg(format)
        .arg(plugin_path)
        .arg(&shm_name)
        .arg(instance_id)
        .arg(events.host_read_fd().to_string())
        .arg(events.host_write_fd().to_string())
        .stdin(Stdio::null())
        .stdout(Stdio::null());

    // In tests, inherit stderr so tracing logs are visible.
    if cfg!(test) {
        cmd.stderr(Stdio::inherit());
    } else {
        cmd.stderr(Stdio::null());
    }

    let child = cmd
        .spawn()
        .map_err(|e| format!("failed to spawn {host_bin:?}: {e}"))?;

    // Close the ends we don't need so the pipe properly signals EOF when the child dies.
    events.close_daw_unused();

    Ok((child, mapping, events))
}

/// Gracefully request shutdown, wait up to `timeout`, then kill if needed.
fn shutdown_host(child: &mut Child, mapping: &ShmMapping, events: &EventPair, timeout: Duration) {
    let header = unsafe { header_mut(mapping.as_ptr()) };
    header.shutdown_request.store(1, Ordering::Release);

    // Wake the host so it sees the shutdown request immediately.
    let _ = events.signal_host();

    let start = std::time::Instant::now();
    loop {
        match child.try_wait() {
            Ok(Some(_)) => break,
            Ok(None) if start.elapsed() >= timeout => {
                let _ = child.kill();
                break;
            }
            _ => std::thread::sleep(Duration::from_millis(10)),
        }
    }

    let _ = ShmMapping::unlink(mapping.name());
}

/// Locate the `maolan-plugin-host` binary relative to the current executable.
fn find_plugin_host_binary() -> Option<PathBuf> {
    let exe = std::env::current_exe().ok()?;
    let dir = exe.parent()?;

    // With a workspace both crates share target/debug/.
    // Test binaries live in target/debug/deps/; regular binaries in target/debug/.
    let mut candidates: Vec<PathBuf> = Vec::new();

    if dir.file_name()? == "deps" {
        let debug_dir = dir.parent()?;
        candidates.push(debug_dir.join("maolan-plugin-host"));

        // Fallback: the binary may have been built independently inside the
        // plugin-host subdirectory before the workspace was introduced.
        if let Some(daw_dir) = debug_dir.parent()?.parent() {
            candidates.push(
                daw_dir
                    .join("plugin-host")
                    .join("target")
                    .join("debug")
                    .join("maolan-plugin-host"),
            );
        }
    } else {
        candidates.push(dir.join("maolan-plugin-host"));
    }

    candidates.into_iter().find(|cand| cand.exists())
}

mod tests {
    use super::*;
    use std::time::Instant;

    #[test]
    fn minimal_ipc_handshake() {
        let instance_id = "test-instance-001";
        let (mut child, mapping, events) =
            spawn_plugin_host("__test__", "__test__", instance_id).unwrap();

        let header = unsafe { header_ref(mapping.as_ptr()) };
        assert!(
            wait_for_ready(header, Duration::from_secs(5)),
            "plugin host did not signal ready within 5 seconds"
        );

        let scratch = unsafe { scratch_ptr(mapping.as_ptr()) };
        let magic = unsafe { std::ptr::read_unaligned(scratch as *const u32) };
        assert_eq!(magic, 0xDEADBEEF, "scratch magic number mismatch");

        shutdown_host(&mut child, &mapping, &events, Duration::from_secs(2));
    }

    #[test]
    fn child_crash_recovery() {
        let instance_id = "test-crash-002";
        let (mut child, mapping, events) =
            spawn_plugin_host("__test__", "__crash__", instance_id).unwrap();

        let header = unsafe { header_ref(mapping.as_ptr()) };
        assert!(
            wait_for_ready(header, Duration::from_secs(5)),
            "plugin host did not signal ready"
        );

        // Host crashes itself with SIGKILL. DAW should survive.
        let status = child.wait().expect("wait should return after crash");
        #[cfg(unix)]
        {
            use std::os::unix::process::ExitStatusExt;
            assert!(
                status.signal() == Some(9), // SIGKILL
                "expected SIGKILL, got {:?}",
                status.signal()
            );
        }

        let _ = ShmMapping::unlink(mapping.name());
        // Receiving the events is fine — the pipe fds are closed by the OS.
        let _ = events;
    }

    #[test]
    fn watchdog_kills_hung_host() {
        let instance_id = "test-hang-003";
        let (mut child, mapping, events) =
            spawn_plugin_host("__test__", "__hang__", instance_id).unwrap();

        let header = unsafe { header_ref(mapping.as_ptr()) };
        assert!(
            wait_for_ready(header, Duration::from_secs(5)),
            "plugin host did not signal ready"
        );

        // The host ignores shutdown_request and sleeps forever.
        // Our shutdown_host gives it 500 ms, then kills.
        shutdown_host(&mut child, &mapping, &events, Duration::from_millis(500));

        let status = child.wait().expect("wait should return after kill");
        assert!(
            !status.success(),
            "host should have been killed, not exited cleanly"
        );
    }

    #[test]
    fn null_plugin_passthrough() {
        let instance_id = "test-null-004";
        let (mut child, mapping, events) =
            spawn_plugin_host("null", "__test__", instance_id).unwrap();

        let header = unsafe { header_ref(mapping.as_ptr()) };
        assert!(
            wait_for_ready(header, Duration::from_secs(5)),
            "plugin host did not signal ready"
        );

        let ptr = mapping.as_ptr();
        let block_size = 256usize;
        let channels = 2usize;

        // Configure the block.
        unsafe {
            let h = header_mut(ptr);
            h.block_size.store(block_size as u32, Ordering::Release);
            h.num_input_channels
                .store(channels as u32, Ordering::Release);
            h.num_output_channels
                .store(channels as u32, Ordering::Release);
            let ts = transport_mut(ptr);
            ts.sample_rate_hz = 48000.0;
        }

        // Write a ramp to each input channel (bus 0).
        for ch in 0..channels {
            let plane = unsafe { audio_channel_ptr(ptr, ch, 0) };
            for s in 0..block_size {
                let value = (s as f32) / (block_size as f32);
                unsafe {
                    std::ptr::write(plane.add(s), value);
                }
            }
        }

        // Wake the host to process the block.
        events.signal_host().expect("signal host should succeed");

        // Wait for completion.
        events
            .wait_host(Duration::from_secs(2))
            .expect("host should complete within 2 seconds");

        // Verify output channels (bus 1) match input.
        for ch in 0..channels {
            let out_plane = unsafe { audio_channel_ptr(ptr, ch, 1) };
            for s in 0..block_size {
                let expected = (s as f32) / (block_size as f32);
                let actual = unsafe { std::ptr::read(out_plane.add(s)) };
                assert!(
                    (actual - expected).abs() < 1e-6,
                    "channel {ch} sample {s}: expected {expected}, got {actual}"
                );
            }
        }

        shutdown_host(&mut child, &mapping, &events, Duration::from_secs(2));
    }

    #[test]
    fn clap_plugin_load_and_process() {
        let plugin_path = "/home/meka/.clap/Maolan.clap";
        if !std::path::Path::new(plugin_path).exists() {
            eprintln!("Skipping CLAP test: {plugin_path} not found");
            return;
        }

        // Use monitoring plugin (simple, 1 param, mostly analysis).
        let plugin_id = "rs.maolan.monitoring";
        let instance_id = "test-clap-005";
        let (mut child, mapping, events) =
            spawn_plugin_host("clap", &format!("{plugin_path}#{plugin_id}"), instance_id).unwrap();

        let header = unsafe { header_ref(mapping.as_ptr()) };
        assert!(
            wait_for_ready(header, Duration::from_secs(5)),
            "plugin host did not signal ready"
        );

        let ptr = mapping.as_ptr();
        let block_size = 256usize;
        let channels = 2usize;

        unsafe {
            let h = header_mut(ptr);
            h.block_size.store(block_size as u32, Ordering::Release);
            h.num_input_channels
                .store(channels as u32, Ordering::Release);
            h.num_output_channels
                .store(channels as u32, Ordering::Release);
        }

        // Write a simple ramp to inputs so we can verify output is valid.
        for ch in 0..channels {
            let plane = unsafe { audio_channel_ptr(ptr, ch, 0) };
            for i in 0..block_size {
                unsafe {
                    *plane.add(i) = (i as f32) / (block_size as f32);
                }
            }
        }

        // Process first block without parameters.
        events.signal_host().expect("signal host should succeed");
        events
            .wait_host(Duration::from_secs(10))
            .expect("host should complete within 10 seconds");

        // Send a parameter change (Mode = 5) before the next block.
        let param_ring = unsafe {
            let buf = param_ring_ptr(ptr);
            let (w, r) = param_indices(ptr);
            maolan_plugin_host::ringbuf::RingBuffer::new(buf, w, r, RING_CAPACITY)
        };
        let param_ev = ParameterEvent {
            param_index: 0, // Mode
            value: 5.0,
            sample_offset: 0,
            event_kind: maolan_plugin_host::protocol::PARAM_EVENT_VALUE,
        };
        assert!(param_ring.push(param_ev), "param ring push should succeed");

        // Process second block — the host should drain the param event.
        events.signal_host().expect("signal host should succeed");
        events
            .wait_host(Duration::from_secs(10))
            .expect("host should complete within 10 seconds");

        // Process a third block for stability.
        events.signal_host().expect("signal host should succeed");
        events
            .wait_host(Duration::from_secs(10))
            .expect("host should complete within 10 seconds");

        // Verify output is finite (no NaN/Inf).
        for ch in 0..channels {
            let plane =
                unsafe { std::slice::from_raw_parts(audio_channel_ptr(ptr, ch, 1), block_size) };
            for (i, &sample) in plane.iter().enumerate() {
                assert!(
                    sample.is_finite(),
                    "output ch={ch} sample={i} is not finite: {sample}"
                );
            }
        }

        tracing::info!("CLAP plugin '{plugin_id}' processed two blocks cleanly");

        shutdown_host(&mut child, &mapping, &events, Duration::from_secs(2));

        // Verify the child exited cleanly (not crashed).
        match child.try_wait() {
            Ok(Some(status)) => {
                assert!(status.success(), "plugin host exited with error: {status}");
            }
            Ok(None) => {
                let _ = child.kill();
                panic!("plugin host did not exit after shutdown");
            }
            Err(e) => panic!("failed to wait for plugin host: {e}"),
        }
    }

    #[test]
    fn scanner_test_plugin() {
        let host_bin = find_plugin_host_binary().expect("maolan-plugin-host binary not found");
        let plugin_path = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/plugin-host/tests/test_passthrough.so"
        );

        let result =
            scanner::scan_plugin_file(&host_bin, "clap", plugin_path, Duration::from_secs(10));
        assert!(result.is_ok(), "scan failed: {:?}", result.err());
        let scan = result.unwrap();
        assert_eq!(scan.plugins.len(), 1);
        assert_eq!(scan.plugins[0].id, "com.maolan.test.passthrough");
    }

    #[test]
    fn scanner_blocklist_crashing_plugin() {
        let host_bin = find_plugin_host_binary().expect("maolan-plugin-host binary not found");
        let plugin_path = "/home/meka/.clap/Maolan.clap";

        if !std::path::Path::new(plugin_path).exists() {
            eprintln!("Skipping blocklist test: {plugin_path} not found");
            return;
        }

        // Use a temp blocklist so we don't pollute the real one.
        let mut blocklist = scanner::Blocklist::default();
        let result = scanner::scan_or_blocklist(
            &host_bin,
            "clap",
            plugin_path,
            &mut blocklist,
            Duration::from_secs(10),
        );
        // With proper clap_version_is_compatible checks and cleanup,
        // the scan now completes without crashing.
        assert!(result.is_some(), "expected scan to succeed");
        assert!(
            !blocklist.contains(plugin_path),
            "plugin should not be blocklisted when scan succeeds"
        );
    }

    #[test]
    fn watchdog_detects_hung_host() {
        let instance_id = "test-watchdog-006";
        let (mut child, mapping, events) =
            spawn_plugin_host("null", "__hang__", instance_id).unwrap();

        let header = unsafe { header_ref(mapping.as_ptr()) };
        assert!(
            wait_for_ready(header, Duration::from_secs(5)),
            "host did not signal ready"
        );

        let mut wd = watchdog::Watchdog::new(Duration::from_millis(200));
        // Host is alive initially.
        assert!(wd.is_alive(header), "host should be alive initially");

        // Wait for the heartbeat to stall (hang process doesn't increment).
        std::thread::sleep(Duration::from_millis(300));
        assert!(!wd.is_alive(header), "watchdog should detect hung host");
        assert_eq!(wd.failure_count, 1);

        shutdown_host(&mut child, &mapping, &events, Duration::from_secs(2));
    }

    #[test]
    fn ipc_latency_benchmark() {
        let instance_id = "test-bench-007";
        let (mut child, mapping, events) =
            spawn_plugin_host("null", "__test__", instance_id).unwrap();

        let header = unsafe { header_ref(mapping.as_ptr()) };
        assert!(
            wait_for_ready(header, Duration::from_secs(5)),
            "host did not signal ready"
        );

        let ptr = mapping.as_ptr();
        let block_size = 256usize;
        let channels = 2usize;

        unsafe {
            let h = header_mut(ptr);
            h.block_size.store(block_size as u32, Ordering::Release);
            h.num_input_channels
                .store(channels as u32, Ordering::Release);
            h.num_output_channels
                .store(channels as u32, Ordering::Release);
        }

        // Warm-up.
        for _ in 0..5 {
            events.signal_host().unwrap();
            events.wait_host(Duration::from_secs(5)).unwrap();
        }

        let iterations = 100;
        let start = Instant::now();
        for _ in 0..iterations {
            events.signal_host().unwrap();
            events.wait_host(Duration::from_secs(5)).unwrap();
        }
        let elapsed = start.elapsed();
        let avg_us = elapsed.as_micros() as f64 / iterations as f64;

        tracing::info!(
            "IPC latency benchmark: {iterations} round-trips in {:.2} ms, avg = {:.2} µs/block",
            elapsed.as_secs_f64() * 1000.0,
            avg_us
        );

        // Expect < 1 ms per round-trip on local machine.
        assert!(
            avg_us < 1000.0,
            "IPC latency too high: {avg_us:.2} µs/block (expected < 1000 µs)"
        );

        shutdown_host(&mut child, &mapping, &events, Duration::from_secs(2));
    }
}