jack 0.13.5

Real time audio and midi with JACK.
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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use jack_sys as j;
use std::{
    ffi,
    panic::catch_unwind,
    sync::atomic::{AtomicBool, Ordering},
};

use crate::{Client, ClientStatus, Control, Error, Frames, PortId, ProcessScope};

/// Specifies callbacks for JACK.
pub trait NotificationHandler: Send {
    /// Called just once after the creation of the thread in which all other
    /// callbacks will be
    /// handled.
    ///
    /// It does not need to be suitable for real-time execution.
    fn thread_init(&self, _: &Client) {}

    /// Called when the JACK server shuts down the client thread. The function must be written as if
    /// it were an asynchronous POSIX signal handler --- use only async-safe functions, and remember
    /// that it is executed from another thread. A typical funcion might set a flag or write to a
    /// pipe so that the rest of the application knows that the JACK client thread has shut down.
    ///
    /// # Safety
    /// See <https://man7.org/linux/man-pages/man7/signal-safety.7.html> for details about
    /// what is legal in an async-signal-safe callback.
    unsafe fn shutdown(&mut self, _status: ClientStatus, _reason: &str) {}

    /// Called whenever "freewheel" mode is entered or leaving.
    fn freewheel(&mut self, _: &Client, _is_freewheel_enabled: bool) {}

    /// Called whenever the system sample rate changes.
    fn sample_rate(&mut self, _: &Client, _srate: Frames) -> Control {
        Control::Continue
    }

    /// Called whenever a client is registered or unregistered
    fn client_registration(&mut self, _: &Client, _name: &str, _is_registered: bool) {}

    /// Called whenever a port is registered or unregistered
    fn port_registration(&mut self, _: &Client, _port_id: PortId, _is_registered: bool) {}

    /// Called whenever a port is renamed.
    fn port_rename(
        &mut self,
        _: &Client,
        _port_id: PortId,
        _old_name: &str,
        _new_name: &str,
    ) -> Control {
        Control::Continue
    }

    /// Called whenever ports are connected/disconnected to/from each other.
    fn ports_connected(
        &mut self,
        _: &Client,
        _port_id_a: PortId,
        _port_id_b: PortId,
        _are_connected: bool,
    ) {
    }

    /// Called whenever the processing graph is reordered.
    fn graph_reorder(&mut self, _: &Client) -> Control {
        Control::Continue
    }

    /// Called whenever an xrun occurs.
    ///
    /// An xrun is a buffer under or over run, which means some data has been
    /// missed.
    fn xrun(&mut self, _: &Client) -> Control {
        Control::Continue
    }
}

/// Specifies real-time processing.
pub trait ProcessHandler: Send {
    /// Indicates whether or not this process handler represents a
    /// slow-sync client
    const SLOW_SYNC: bool = false;

    /// Called whenever there is work to be done.
    ///
    /// It needs to be suitable for real-time execution. That means that it
    /// cannot call functions
    /// that might block for a long time. This includes all I/O functions
    /// (disk, TTY, network),
    /// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select,
    /// pthread_join,
    /// pthread_cond_wait, etc, etc.
    ///
    /// Should return `Control::Continue` on success, and
    /// `Control::Quit` on error.
    fn process(&mut self, _: &Client, _process_scope: &ProcessScope) -> Control;

    /// Called whenever the size of the buffer that will be passed to `process`
    /// is about to change, and once before the first call to `process`.
    ///
    /// It is called on the same thread as `process`, but as an exception, does
    /// not need to be suitable for real-time execution, so it is allowed to
    /// allocate new buffers to accomodate the buffer size for example.
    fn buffer_size(&mut self, _: &Client, _size: Frames) -> Control {
        Control::Continue
    }

    /// For slow-sync clients, called periodically when the transport position
    /// is changed.  The transport will not start rolling until all clients
    /// indicate they are ready, or a timeout occurs.
    ///
    /// It should return `false` until the handler is ready process audio.
    ///
    /// Ignored unless Self::SLOW_SYNC == true.
    fn sync(
        &mut self,
        _: &Client,
        _state: crate::TransportState,
        _pos: &crate::TransportPosition,
    ) -> bool {
        true
    }
}

unsafe extern "C" fn thread_init_callback<N, P>(data: *mut libc::c_void)
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return;
        };
        ctx.notification.thread_init(&ctx.client);
    });
    if let Err(err) = res {
        if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
            ctx.mark_invalid(true)
        }
        eprintln!("{err:?}");
        std::mem::forget(err);
    }
}

unsafe extern "C" fn shutdown<N, P>(
    code: j::jack_status_t,
    reason: *const libc::c_char,
    data: *mut libc::c_void,
) where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return;
        };
        let cstr = ffi::CStr::from_ptr(reason);
        let reason_str = cstr.to_str().unwrap_or("Failed to interpret error.");
        ctx.notification.shutdown(
            ClientStatus::from_bits(code).unwrap_or_else(ClientStatus::empty),
            reason_str,
        );
    });
    if let Err(err) = res {
        if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
            ctx.mark_invalid(true)
        }
        eprintln!("{err:?}");
        std::mem::forget(err);
    }
}

unsafe extern "C" fn process<N, P>(n_frames: Frames, data: *mut libc::c_void) -> libc::c_int
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return Control::Quit;
        };
        let scope = ProcessScope::from_raw(n_frames, ctx.client.raw());
        let c = ctx.process.process(&ctx.client, &scope);
        if c == Control::Quit {
            ctx.mark_invalid(false);
        }
        c
    });
    match res {
        Ok(res) => res.to_ffi(),
        Err(err) => {
            if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
                ctx.mark_invalid(true)
            }
            eprintln!("JACK process callback panicked.\n{err:?}");
            std::mem::forget(err);
            Control::Quit.to_ffi()
        }
    }
}

unsafe extern "C" fn sync<N, P>(
    state: jack_sys::jack_transport_state_t,
    pos: *mut jack_sys::jack_position_t,
    data: *mut libc::c_void,
) -> libc::c_int
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return false;
        };
        let is_ready = ctx.process.sync(
            &ctx.client,
            crate::Transport::state_from_ffi(state),
            &*(pos as *mut crate::TransportPosition),
        );
        if !is_ready {
            ctx.mark_invalid(false);
        }
        is_ready
    });
    match res {
        Ok(true) => 1,
        Ok(false) => 0,
        Err(err) => {
            if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
                ctx.mark_invalid(true)
            }
            eprintln!("{err:?}");
            std::mem::forget(err);
            0
        }
    }
}

unsafe extern "C" fn freewheel<N, P>(starting: libc::c_int, data: *mut libc::c_void)
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return;
        };
        let is_starting = !matches!(starting, 0);
        ctx.notification.freewheel(&ctx.client, is_starting);
    });
    if let Err(err) = res {
        if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
            ctx.mark_invalid(true)
        }
        eprintln!("{err:?}");
        std::mem::forget(err);
    }
}

unsafe extern "C" fn buffer_size<N, P>(n_frames: Frames, data: *mut libc::c_void) -> libc::c_int
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return Control::Quit;
        };
        let c = ctx.process.buffer_size(&ctx.client, n_frames);
        if c == Control::Quit {
            ctx.mark_invalid(false);
        }
        c
    });
    match res {
        Ok(c) => c.to_ffi(),
        Err(err) => {
            if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
                ctx.mark_invalid(true)
            }
            eprintln!("{err:?}");
            std::mem::forget(err);
            Control::Quit.to_ffi()
        }
    }
}

unsafe extern "C" fn sample_rate<N, P>(n_frames: Frames, data: *mut libc::c_void) -> libc::c_int
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return Control::Quit;
        };
        let c = ctx.notification.sample_rate(&ctx.client, n_frames);
        if c == Control::Quit {
            ctx.mark_invalid(false);
        }
        c
    });
    match res {
        Ok(c) => c.to_ffi(),
        Err(err) => {
            if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
                ctx.mark_invalid(true)
            }
            eprintln!("{err:?}");
            std::mem::forget(err);
            Control::Quit.to_ffi()
        }
    }
}

unsafe extern "C" fn client_registration<N, P>(
    name: *const libc::c_char,
    register: libc::c_int,
    data: *mut libc::c_void,
) where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return;
        };
        let name = ffi::CStr::from_ptr(name).to_str().unwrap();
        let register = !matches!(register, 0);
        ctx.notification
            .client_registration(&ctx.client, name, register);
    });
    if let Err(err) = res {
        if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
            ctx.mark_invalid(true)
        }
        eprintln!("{err:?}");
        std::mem::forget(err);
    }
}

unsafe extern "C" fn port_registration<N, P>(
    port_id: PortId,
    register: libc::c_int,
    data: *mut libc::c_void,
) where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return;
        };
        let register = !matches!(register, 0);
        ctx.notification
            .port_registration(&ctx.client, port_id, register);
    });
    if let Err(err) = res {
        if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
            ctx.mark_invalid(true)
        }
        eprintln!("{err:?}");
        std::mem::forget(err);
    }
}

#[allow(dead_code)] // TODO: remove once it can be registered
unsafe extern "C" fn port_rename<N, P>(
    port_id: PortId,
    old_name: *const libc::c_char,
    new_name: *const libc::c_char,
    data: *mut libc::c_void,
) -> libc::c_int
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return Control::Quit;
        };
        let old_name = ffi::CStr::from_ptr(old_name).to_str().unwrap();
        let new_name = ffi::CStr::from_ptr(new_name).to_str().unwrap();
        let c = ctx
            .notification
            .port_rename(&ctx.client, port_id, old_name, new_name);
        if c == Control::Quit {
            ctx.mark_invalid(false);
        }
        c
    });
    match res {
        Ok(c) => c.to_ffi(),
        Err(err) => {
            if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
                ctx.mark_invalid(true)
            }
            eprintln!("{err:?}");
            std::mem::forget(err);
            Control::Quit.to_ffi()
        }
    }
}

unsafe extern "C" fn port_connect<N, P>(
    port_id_a: PortId,
    port_id_b: PortId,
    connect: libc::c_int,
    data: *mut libc::c_void,
) where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return;
        };
        let are_connected = !matches!(connect, 0);
        ctx.notification
            .ports_connected(&ctx.client, port_id_a, port_id_b, are_connected);
    });
    if let Err(err) = res {
        if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
            ctx.mark_invalid(true)
        }
        eprintln!("{err:?}");
        std::mem::forget(err);
    }
}

unsafe extern "C" fn graph_order<N, P>(data: *mut libc::c_void) -> libc::c_int
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return Control::Quit;
        };
        let c = ctx.notification.graph_reorder(&ctx.client);
        if c == Control::Quit {
            ctx.mark_invalid(false);
        }
        c
    });
    match res {
        Ok(c) => c.to_ffi(),
        Err(err) => {
            if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
                ctx.mark_invalid(true)
            }
            eprintln!("{err:?}");
            std::mem::forget(err);
            Control::Quit.to_ffi()
        }
    }
}

unsafe extern "C" fn xrun<N, P>(data: *mut libc::c_void) -> libc::c_int
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    let res = catch_unwind(|| {
        let Some(ctx) = CallbackContext::<N, P>::from_raw(data) else {
            return Control::Quit;
        };
        let c = ctx.notification.xrun(&ctx.client);
        if c == Control::Quit {
            ctx.mark_invalid(false);
        }
        c
    });
    match res {
        Ok(c) => c.to_ffi(),
        Err(err) => {
            if let Some(ctx) = CallbackContext::<N, P>::from_raw(data) {
                ctx.mark_invalid(true)
            }
            eprintln!("{err:?}");
            std::mem::forget(err);
            Control::Quit.to_ffi()
        }
    }
}

/// Unsafe ffi wrapper that clears the callbacks registered to `client`.
///
/// This is mostly for use within the jack crate itself.
///
/// Returns `Err(Error::CallbackDeregistrationError)` on failure.
///
/// # Unsafe
///
/// * Uses ffi calls, be careful.
///
/// # TODO
///
/// * Implement correctly. Freezes on my system. Maybe this makes sense now? it doesn't disturb my program
pub unsafe fn clear_callbacks(client: *mut j::jack_client_t) -> Result<(), Error> {
    j::jack_set_thread_init_callback(client, None, std::ptr::null_mut());
    j::jack_set_process_callback(client, None, std::ptr::null_mut());
    Ok(())
}

/// The information used by JACK to process data.
pub struct CallbackContext<N, P> {
    /// The underlying JACK client.
    pub client: Client,
    /// The handler for notifications.
    pub notification: N,
    /// The handler for processing.
    pub process: P,
    /// True if the callback is valid for callbacks.
    ///
    /// This becomes false after quit an event that causes processing to quit.
    pub is_valid_for_callback: AtomicBool,
    /// True if the callback has panicked.
    pub has_panic: AtomicBool,
}

impl<N, P> CallbackContext<N, P>
where
    N: 'static + Send + Sync + NotificationHandler,
    P: 'static + Send + ProcessHandler,
{
    pub unsafe fn from_raw<'a>(ptr: *mut libc::c_void) -> Option<&'a mut CallbackContext<N, P>> {
        debug_assert!(!ptr.is_null());
        let obj_ptr = ptr as *mut CallbackContext<N, P>;
        let obj_ref = &mut *obj_ptr;
        if obj_ref.is_valid_for_callback.load(Ordering::Relaxed) {
            Some(obj_ref)
        } else {
            None
        }
    }

    /// Mark the callback context as invalid.
    ///
    /// This usually happens after a panic.
    #[cold]
    pub fn mark_invalid(&mut self, did_panic: bool) {
        self.is_valid_for_callback.store(true, Ordering::Relaxed);
        self.has_panic.store(did_panic, Ordering::Relaxed);
    }

    fn raw(b: &mut Box<Self>) -> *mut libc::c_void {
        let ptr: *mut Self = b.as_mut();
        ptr as *mut libc::c_void
    }

    /// Registers methods from `handler` to be used by JACK with `client`.
    ///
    /// This is mostly for use within the jack crate itself.
    ///
    /// Returns `Ok(handler_ptr)` on success, or
    /// `Err(Error::CallbackRegistrationError)` on failure.
    ///
    /// `handler_ptr` here is a pointer to a heap-allocated pair `(T, *mut
    /// j::jack_client_t)`.
    ///
    /// Registers `handler` with JACK. All JACK calls to `client` will be handled by
    /// `handler`. `handler` is consumed, but it is not deallocated. `handler`
    /// should be manually
    /// deallocated when JACK will no longer make calls to it, such as when
    /// registering new callbacks
    /// with the same client, or dropping the client.
    ///
    /// # TODO
    ///
    /// * Handled failed registrations
    /// * Fix `jack_set_port_rename_callback`
    ///
    /// # Unsafe
    ///
    /// * makes ffi calls
    /// * `handler` will not be automatically deallocated.
    pub unsafe fn register_callbacks(b: &mut Box<Self>) -> Result<(), Error> {
        let data_ptr = CallbackContext::raw(b);
        let client = b.client.raw();
        j::jack_set_thread_init_callback(client, Some(thread_init_callback::<N, P>), data_ptr);
        j::jack_on_info_shutdown(client, Some(shutdown::<N, P>), data_ptr);
        j::jack_set_process_callback(client, Some(process::<N, P>), data_ptr);
        if P::SLOW_SYNC {
            j::jack_set_sync_callback(client, Some(sync::<N, P>), data_ptr);
        }
        j::jack_set_freewheel_callback(client, Some(freewheel::<N, P>), data_ptr);
        j::jack_set_buffer_size_callback(client, Some(buffer_size::<N, P>), data_ptr);
        j::jack_set_sample_rate_callback(client, Some(sample_rate::<N, P>), data_ptr);
        j::jack_set_client_registration_callback(
            client,
            Some(client_registration::<N, P>),
            data_ptr,
        );
        j::jack_set_port_registration_callback(client, Some(port_registration::<N, P>), data_ptr);
        // doesn't compile for testing since it is a weak export
        // j::jack_set_port_rename_callback(client, Some(port_rename::<N, P), data_ptr);
        j::jack_set_port_connect_callback(client, Some(port_connect::<N, P>), data_ptr);
        j::jack_set_graph_order_callback(client, Some(graph_order::<N, P>), data_ptr);
        j::jack_set_xrun_callback(client, Some(xrun::<N, P>), data_ptr);
        Ok(())
    }
}