/*
* VideoKit
* Copyright © 2026 Yusuf Olokoba. All Rights Reserved.
*/
#nullable enable
namespace VideoKit.Clocks {
using System.Runtime.CompilerServices;
using Internal;
using Status = Internal.VideoKit.Status;
///
/// Realtime clock for generating timestamps.
///
public sealed class RealtimeClock : IClock {
#region --Client API--
///
/// Current timestamp in nanoseconds.
/// The very first value reported by this property will always be zero.
///
public long timestamp {
[MethodImpl(MethodImplOptions.Synchronized)]
get => (isPaused ? pauseTime : CurrentTimestamp) - startTime;
}
///
/// Whether the clock is paused.
///
public bool paused {
[MethodImpl(MethodImplOptions.Synchronized)] get => isPaused;
[MethodImpl(MethodImplOptions.Synchronized)]
set {
if (value == isPaused)
return;
if (value)
pauseTime = CurrentTimestamp;
else
startTime += CurrentTimestamp - pauseTime;
isPaused = value;
}
}
///
/// Create a realtime clock.
///
public RealtimeClock() {
this.startTime = CurrentTimestamp;
this.isPaused = false;
this.pauseTime = 0L;
}
#endregion
#region --Operations--
private long startTime;
private bool isPaused;
private long pauseTime;
private static long CurrentTimestamp => VideoKit.GetCurrentTimestamp(out var timestamp)
.Throw() == Status.Ok ? timestamp : 0L;
#endregion
}
}