Skip to content

Commit

Permalink
Ensure a jack client is initialized when calling get_time
Browse files Browse the repository at this point in the history
  • Loading branch information
wmedrano committed Sep 9, 2024
1 parent 1daee27 commit c32c009
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/client/client_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions src/client/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 8 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,15 @@ mod transport;
/// Properties
mod properties;

static TIME_CLIENT: std::sync::LazyLock<Client> = 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()
}
4 changes: 3 additions & 1 deletion src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c32c009

Please sign in to comment.