diff --git a/src/client/client_impl.rs b/src/client/client_impl.rs index 1a2e0755..a01d5e02 100644 --- a/src/client/client_impl.rs +++ b/src/client/client_impl.rs @@ -97,6 +97,17 @@ impl Client { AsyncClient::new(self, notification_handler, process_handler) } + /// Return JACK's current system time in microseconds, using the JACK clock + /// source. + /// + /// Note: Although attached a `Client` method, this should use the same time clock as all + /// clients. + pub fn time(&self) -> Time { + // Despite not needing a ptr to the client, this function often segfaults if a client has + // not been initialized. + unsafe { jack_sys::jack_get_time() } + } + /// The sample rate of the JACK system, as set by the user when jackd was /// started. pub fn sample_rate(&self) -> usize { diff --git a/src/client/test.rs b/src/client/test.rs index adb67129..3eb10f75 100644 --- a/src/client/test.rs +++ b/src/client/test.rs @@ -6,6 +6,20 @@ fn open_test_client(name: &str) -> (Client, ClientStatus) { Client::new(name, ClientOptions::NO_START_SERVER).unwrap() } +#[test] +fn time_can_get_time() { + open_test_client("tcgt").0.time(); +} + +#[test] +fn time_is_monotonically_increasing() { + let c = open_test_client("tcgt").0; + let initial_t = c.time(); + std::thread::sleep(std::time::Duration::from_millis(100)); + let later_t = c.time(); + assert!(initial_t < later_t); +} + #[test] fn client_valid_client_name_size() { assert!(*CLIENT_NAME_SIZE > 0); diff --git a/src/lib.rs b/src/lib.rs index 5c290a09..f7ca0b20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,27 +80,15 @@ mod transport; /// Properties mod properties; +static TIME_CLIENT: std::sync::LazyLock = std::sync::LazyLock::new(|| { + Client::new("deprecated_get_time", ClientOptions::NO_START_SERVER) + .unwrap() + .0 +}); + /// Return JACK's current system time in microseconds, using the JACK clock /// source. +#[deprecated = "Prefer using Client::time. get_time will be eventually be removed and it requires an extra client initialization."] pub fn get_time() -> primitive_types::Time { - unsafe { jack_sys::jack_get_time() } -} - -#[cfg(test)] -mod test { - use super::*; - use std::{thread, time}; - - #[test] - fn time_can_get_time() { - get_time(); - } - - #[test] - fn time_is_monotonically_increasing() { - let initial_t = get_time(); - thread::sleep(time::Duration::from_millis(100)); - let later_t = get_time(); - assert!(initial_t < later_t); - } + TIME_CLIENT.time() } diff --git a/src/properties.rs b/src/properties.rs index 898dc0b0..f56fb931 100644 --- a/src/properties.rs +++ b/src/properties.rs @@ -61,7 +61,9 @@ mod metadata { } /// A piece of Metadata on a Jack `subject`: either a port or a client. - /// See the JACK Metadata API [description](https://jackaudio.org/metadata/) and [documentation](https://jackaudio.org/api/group__Metadata.html) and for more info. + /// + /// See the JACK Metadata API [description](https://jackaudio.org/metadata/) and + /// [documentation](https://jackaudio.org/api/group__Metadata.html) and for more info. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Property { value: String,