/* * VideoKit * Copyright © 2025 Yusuf Olokoba. All Rights Reserved. */ #nullable enable namespace VideoKit.Clocks { using System.Runtime.CompilerServices; /// /// Clock that produces timestamps spaced at a fixed interval. /// This clock is useful for enforcing a fixed framerate in a recording. /// public sealed class FixedClock : IClock { #region --Client API-- /// /// Interval between consecutive timestamps generated by the clock in seconds. /// public readonly double interval; /// /// Current timestamp in nanoseconds. /// The very first value reported by this property will always be zero. /// public long timestamp { [MethodImpl(MethodImplOptions.Synchronized)] get => (long)((autoTick ? ticks++ : ticks) * interval * 1e+9); } /// /// Create a fixed clock for a given framerate. /// /// Desired framerate for clock's timestamps. /// Optional. If true, the clock will tick when its `Timestamp` is accessed. public FixedClock (float framerate, bool autoTick = true) { this.interval = 1.0 / framerate; this.ticks = 0L; this.autoTick = autoTick; } /// /// Advance the clock by its time interval. /// [MethodImpl(MethodImplOptions.Synchronized)] public void Tick () => ticks++; #endregion #region --Operations-- private readonly bool autoTick; private long ticks; #endregion } }