Home/docs/kernel/Clock and scheduling

Clock and scheduling

Clock & Scheduling

Audio output is the master clock. The presentation target of each video frame is quantized to vsync and aligned to the audio clock; there is no separate display clock between audio and video. When the buffer runs low, the kernel explicitly enters the buffering state, the clock freezes, and it re-anchors once data recovers.

Audio master clock#

Every audio output backend pushes data into a shared f32 PCM ring buffer (capacity 192,000 frames; on overflow the oldest data is dropped). The playback clock is based on media time and derives the current time from the anchor point and the playback rate.

On every render_tick, the Presenter reads a snapshot of the audio clock and syncs it to the playback thread. Three parameters govern clock correction:

ParameterDefaultDescription
deadband5 msJitter smaller than this value is not corrected.
Maximum single correction5 msThe upper limit of each correction, to avoid visible jumps.
Snapshot threshold250 msA deviation above this value counts as the clock having drifted off track, and the clock is re-anchored directly.

The reference time is the media time plus the duration of queued audio (an audio lead of 120 ms), so video is presented ahead of the end of the audio queue. When the playback rate is not 1, audio clock synchronization is skipped and SoundTouch changes the audio speed (SEQUENCE 25 ms).

vsync-quantized scheduling#

The presentation target of each video frame is quantized to the vsync interval (16,666,667 ns by default, which is 60 Hz). For each frame, the scheduler makes one of three decisions:

  • Present — The target time falls within this vsync cycle, so the frame is presented immediately.
  • Wait — The frame is earlier than the presentation window, so it waits for the next cycle.
  • Drop — The frame is later than the presentation window, so it is skipped to reduce accumulated latency.

At most 5 consecutive frames can be dropped. Once it reaches that limit, the scheduler stops dropping and returns to Wait, so the screen is never left without output for long.

The clock is frozen while paused. A seek decodes and presents the keyframe at the requested position while the media clock stays put.

Buffering state#

When audio or video input runs short, playback enters an explicit buffering state: the playback clock freezes and stops advancing. Once data recovers, the clock is re-anchored at the recovered media time. The host observes the state change through the BufferingChanged event.

The clock does not advance during buffering so that recovery does not cause a long run of dropped frames or a black screen.

Diagnostics#

Environment variables control trace output for the clock and playback, which is written to /tmp/erika_playback_trace.log:

Environment variableOutput
ERIKA_CLOCK_TRACEClock correction and anchor events.
ERIKA_PLAYBACK_TRACEPlayback state machine events.
ERIKA_DANMAKU_TRACEDanmaku plan generation events.

On Android, diagnostic output is emitted through logcat with the Erika tag. The debug HUD computes fps from frame count deltas, and you can control it with set_debug_hud_enabled.

iOS display target time#

Since 0.2.1, the iOS Flutter plugin uses CADisplayLink.targetTimestamp as the display target rather than the time the native queue actually runs. The refresh interval follows the display callback, so a 120 Hz screen is not scheduled at a fixed 60 Hz.

The display callback coalesces backlogged ticks through a mailbox. erika_presenter_render_tick_with_timing samples video, subtitles, and danmaku at the same target time. After a pause or seek, it does not advance or re-anchor the playback clock.

liberika v0.3.8 Section Kernel License MPL-2.0