Skip to content

AudioPlaybackHost: Master Clock continues advancing after end-of-media #694

Closed

Description

@ijuniorfu

What happened?

Problem

After audio playback reaches the end of the media, AudioPlaybackHost reports the playback as ended, but the AudioOutput master clock may continue running.

As a result, AudioOutput.currentTime continues increasing after playback has already finished.

This causes the reported playback position to exceed the actual end position of the media.

Current Behavior

The playback pipeline correctly detects end-of-media after:

  1. The decoder reaches EOF.
  2. Remaining decoded audio is drained.
  3. Pending audio samples are enqueued.
  4. The playback clock catches up with the end of the last enqueued audio.

However, after onEnd() is triggered, the synchronizer's master clock may still have a playback rate of 1.0.

Therefore:

Playback reaches EOF
        ↓
Remaining audio is drained
        ↓
All pending samples are rendered
        ↓
Master clock reaches lastEnqueuedEnd
        ↓
onEnd()
        ↓
Master clock is still running
        ↓
currentTime continues increasing

Root Cause

AudioOutput.currentTime is derived from the AVSampleBufferRenderSynchronizer master clock.

The end-of-media path in AudioPlaybackHost waits for the synchronizer to reach the end of the last enqueued audio:

lastEnqueuedEnd

but does not necessarily stop or park the synchronizer afterward.

As a result, the synchronizer continues advancing even though playback has already reached the end.

Expected Behavior

Once the end of the last enqueued audio has been reached, the master clock should be parked at that exact position and its playback rate should be set to 0.

Expected lifecycle:

Decoder EOF
    ↓
Drain decoder
    ↓
Enqueue remaining samples
    ↓
Wait for synchronizer to reach lastEnqueuedEnd
    ↓
Park master clock at lastEnqueuedEnd
    ↓
Set clock rate to 0
    ↓
Call onEnd()

After this point:

audioOutput.currentTime

should remain stable instead of continuing to increase.

Recommended Fix

In the end-of-media path of AudioPlaybackHost, after the synchronizer has caught up with lastEnqueuedEnd, explicitly park the master clock:

if let aOut = audioOutput,
   lastEnqueuedEnd.isFinite,
   lastEnqueuedEnd >= 0 {
    let endTime = CMTime(
        seconds: lastEnqueuedEnd,
        preferredTimescale: 90000
    )

    aOut.seekClock(to: endTime, rate: 0)

    EngineLog.emit(
        "[AudioHost] end-of-media: park clock at "
        + String(format: "%.3f", lastEnqueuedEnd)
        + "s",
        category: .swPlayback
    )
}

onEnd()
return false

The important part is:

aOut.seekClock(to: endTime, rate: 0)

This both positions the clock at the actual end of the queued audio and stops it from advancing further.

Why lastEnqueuedEnd Should Be Used

The clock should not be stopped immediately when the decoder reports EOF.

Decoder EOF only means that no more packets are available. There may still be decoded audio samples waiting in the renderer.

Using lastEnqueuedEnd ensures that:

  • all decoded audio can finish playing;
  • the master clock reaches the actual end of the queued audio;
  • currentTime does not stop prematurely;
  • the final playback position remains stable.

Approaches to Avoid

Do not clamp currentTime

For example:

min(currentTime, duration)

This hides the underlying clock lifecycle problem instead of fixing it.

Do not replace the synchronizer clock with a software timer

AVSampleBufferRenderSynchronizer should remain the source of truth for audio playback timing.

Replacing it with CACurrentMediaTime() or another software clock could introduce synchronization and seeking issues.

Do not stop the clock immediately at decoder EOF

Stopping the clock at the moment FFmpeg reports EOF can truncate audio that has already been decoded and queued.

The clock should only be parked after the renderer has caught up with the final queued audio.

Expected Result

After end-of-media:

currentTime → lastEnqueuedEnd
rate        → 0

For example:

[AudioHost] end-of-media: park clock at 1.000s

After this point, repeated reads of:

audioOutput.currentTime

should continue returning approximately:

1.000
1.000
1.000
1.000

rather than:

1.000
1.050
1.100
1.150
...

Acceptance Criteria

  • AudioPlaybackHost detects end-of-media normally.
  • All already-decoded audio is allowed to finish rendering.
  • The synchronizer reaches lastEnqueuedEnd.
  • The master clock is parked at lastEnqueuedEnd.
  • The master clock rate becomes 0.
  • AudioOutput.currentTime remains stable after playback ends.
  • Normal playback, seeking, pause/resume, and rate changes are unaffected.
  • No changes to the underlying decoder or audio format handling are required.

Scope

The fix should be limited to the end-of-media clock lifecycle in:

AudioPlaybackHost.swift

No changes to the decoder implementation should be necessary.

Steps to reproduce

none

AetherEngine version or commit SHA

7ac28fa

Host app

Custom / my own integration

Platform

iOS

OS version

iOS26

Device / chip

iPhone16

Playback path

Native AVPlayer

Source media (for playback bugs)

No response

Error codes / log lines


Anything else

No response

Activity

  1. added
    bug
    on Oct 3, 2026
  2. superuser404notfound commented on Oct 4, 2026

    @superuser404notfound
    Owner

    Thanks for the precise report, the analysis is right. AudioPlaybackHost signalled end of media and left the synchronizer at rate 1, so currentTime kept growing. The software host got a park for exactly this in #374; the audio-only host never did.

    Fix is in PR #698:

    • After the playthrough wait, the clock is parked once the audio still queued has played out (the wait releases up to 0.25 s before the last enqueued sample, so parking right there would cut the tail, as the report points out for decoder EOF).
    • The published rate goes to 0 with it.
    • play() after the end no longer restarts the parked clock as a stalled one; a seek clears the park.

    One deviation from the proposed patch: the park is a pause() at the clock's own position after the tail has played, not seekClock(to: lastEnqueuedEnd, rate: 0). By then the clock sits on the last sample either way, and it keeps both hosts on the same implementation.

    Covered by an end-to-end test (1 s WAV through the real host): it fails on main with the clock at rate 1.0 after the end, and passes with the fix, position at 1.0 s and standing still.

    This will ship in the next release after 7.27.0. A check on the iPhone integration once that is out would be appreciated.

  3. added a commit that references this issue on Oct 4, 2026
  4. superuser404notfound commented on Oct 4, 2026

    @superuser404notfound
    Owner

    Released in 7.27.1 (d9a0ce87, fix commit 04343a6a). Leaving this open until it is confirmed on the iPhone integration.

  5. superuser404notfound commented on Oct 5, 2026

    @superuser404notfound
    Owner

    A follow-up is in 7.27.2 (fb3b6327): when the deferred end-of-media park only gets the main actor late, the clock used to stop wherever it stood by then (1.149 s on a 1.0 s source in CI). It is now put back on the last sample. 7.27.2 is the version to confirm against.

  6. added a commit that references this issue on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions