pub trait ClockSource: Send {
// Required methods
fn ptp_now_ns(&self) -> Result<i64>;
fn sample_to_ptp(&self, sample_idx: i64) -> i64;
fn ptp_to_sample(&self, ptp_ns: i64) -> i64;
}Expand description
A source of PTP-aligned time that can map between sample indices and timestamps.
ClockSource is the single contract a host uses to timestamp audio samples.
Concrete implementations (an NtpClock fallback or a PtpClock) own a
ClockAnchor and a backing time reference.
§Threading
Implementations are Send but not real-time-safe in general:
ClockSource::ptp_now_ns may block (polling a daemon, reading a device)
and must run off the RT audio thread. The pure-conversion methods
ClockSource::sample_to_ptp and ClockSource::ptp_to_sample are
allocation-free and safe to call from the RT thread.
§Example
A minimal anchor-only clock (no live time source) implementing the trait:
use audio_clock_bsd::{ClockAnchor, ClockError, ClockSource, Result};
struct AnchorClock {
anchor: ClockAnchor,
}
impl ClockSource for AnchorClock {
fn ptp_now_ns(&self) -> Result<i64> {
// A real clock would read its backing time source here.
Err(ClockError::Unavailable("no live source".into()))
}
fn sample_to_ptp(&self, sample_idx: i64) -> i64 {
self.anchor.sample_to_ptp_checked(sample_idx).unwrap_or(i64::MIN)
}
fn ptp_to_sample(&self, ptp_ns: i64) -> i64 {
self.anchor.ptp_to_sample_checked(ptp_ns).unwrap_or(i64::MIN)
}
}Required Methods§
Sourcefn ptp_now_ns(&self) -> Result<i64>
fn ptp_now_ns(&self) -> Result<i64>
Returns the current PTP timestamp in nanoseconds since the epoch.
This may block (daemon poll, device read) — call from a worker thread.
§Errors
ClockError::Unavailablewhen no time source is present.ClockError::NotSyncedwhen the source exists but has not locked.
Sourcefn sample_to_ptp(&self, sample_idx: i64) -> i64
fn sample_to_ptp(&self, sample_idx: i64) -> i64
Converts a sample index to a PTP timestamp in nanoseconds.
Pure arithmetic over the clock’s anchor — allocation-free, RT-safe.
Implementations MUST NOT panic; on internal overflow they return a
sentinel (i64::MIN) so the RT path is never aborted.
Sourcefn ptp_to_sample(&self, ptp_ns: i64) -> i64
fn ptp_to_sample(&self, ptp_ns: i64) -> i64
Converts a PTP timestamp in nanoseconds to a sample index.
Pure arithmetic over the clock’s anchor — allocation-free, RT-safe.
Implementations MUST NOT panic; on internal overflow they return a
sentinel (i64::MIN).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".