Skip to content

Software path: VT decode session invalidated by screen lock is never recovered (black video after wake) #739

Closed

Description

@ijuniorfu

What happened?

Summary

On iOS, when an HEVC source plays on the software path (SoftwarePlaybackHost + HardwareVideoDecoder) and the screen is locked and woken, the system invalidates the VTDecompressionSession. VTDecompressionSessionDecodeFrame then returns kVTInvalidSessionErr (-12903) for every remaining packet. HardwareVideoDecoder only logs the error and keeps feeding the dead session, so video never comes back.

For a source with no audio track it is worse. The background audio-only mode drops every video packet with nothing pacing the demux loop, so the demuxer reads to EOF within moments. The host then parks the clock as end of media. After wake the session looks finished and the screen stays black.

Environment

  • AetherEngine: 7.34.0
  • Device / iOS:
  • backgroundPlaybackEnabled = true (default), PiP not active

Steps to reproduce

Case A: HEVC source with audio

  1. Play a 4K HEVC MKV that routes to the software path (dispatch: codec=173 → software, selected HardwareVideoDecoder (VT HEVC)). The test file was 3840x1610 with DTS, E-AC-3 and AAC tracks.
  2. Lock the screen during playback, then wake it.

Case B: HEVC source without audio (video-only MP4)

  1. Play a 30 s 4K HEVC MP4 with no audio stream (audio delivery = noAudioInSource).
  2. Lock the screen a few seconds in, then wake it.

Expected

Video resumes at (or near) the playhead after wake. Audio, if any, stays in place.

Actual

Case A: every video decode after the lock fails:

[SWDiag] clk=1.60 dclk=1.00 rate=1.00 aLead=4.02 ... status=rendering
<<<< DecompressionSessionRemote >>>> signalled err=-12903 at <>:2002
[HardwareVideoDecoder] decode error -12903 at pts=2017
[HardwareVideoDecoder] decode error -12903 at pts=2050
...

Case B: the decode errors run to the end of the file, then end of media is declared where the picture froze. In the first run, the clip was 30 s long and the clock parked at 4.97 s. The screen is black after wake (isReadyForDisplay=false):

[HardwareVideoDecoder] decode error -12903 at pts=6096000
... (one line per packet until EOF) ...
[AudioOutput] pause at t=4.967
[SWHost] end of media: clock parked at 4.967s
[SWHost] layer.isReadyForDisplay=false after 255 frames

Even with the session rebuilt (local experiment, see below), Case B still ends early, because the packets were already dropped in the background:

[HardwareVideoDecoder] recovered at keyframe pts=18000000 after dropping 132 packet(s)
[SWHost] end of media: clock parked at 12.244s

Root cause

There are three independent problems.

  1. HardwareVideoDecoder ignores session invalidation. In decode(packet:epoch:), a non-noErr status from VTDecompressionSessionDecodeFrame is only logged. kVTInvalidSessionErr is permanent for that session; every later call fails the same way. Nothing tells the host, and nothing rebuilds the session.

  2. The foreground return reuses the stale session. SoftwarePlaybackHost.exitBackgroundAudioOnly() flushes the decoder and renderer and resumes feeding video into the same VTDecompressionSession. In audio-only mode no video is decoded in the background, so nothing notices that the lock invalidated the session until the first packet after wake fails.

  3. Background audio-only on a source with no audio. AetherEngine.backgroundAction(...) returns .enterSoftwareAudioOnly whenever a software host is kept alive without PiP. In the demux loop's video branch, backgroundAudioOnly() frees every video packet and returns immediately. Only the audio branch paces on aOut.isReadyForMoreMediaData. With no audio stream, nothing paces the loop at all, so it hits EOF and parkClockAtEndOfMedia() runs while the app is in the background.

The lifecycle also left no trace in the log, because enterBackgroundAudioOnly() and exitBackgroundAudioOnly() emit nothing. That made these reports hard to tie to the lock and wake.

Proposed fix

1. Decoder reports, host recovers

  • HardwareVideoDecoder: on kVTInvalidSessionErr, mark the session lost, log once, and fire a new onSessionLost callback. Drop every later packet instead of calling VT.
  • Add HardwareVideoDecoder.rebuildSession() -> Bool. It invalidates the old session and creates a new one from the stored format description and pixel buffer attributes, reusing the existing refCon box so all callbacks and settings carry over. It bumps the feed epoch and drops packets until the next keyframe. Factor session creation into one helper shared with open() so the two cannot drift apart.
  • SoftwarePlaybackHost, on onSessionLost (hopped to the main actor):
    • In the foreground: rebuildSession(), then seek(to: currentTime). The demuxer repositions to the keyframe before the playhead and the existing skip threshold hides the pre-roll. The picture returns on the right frame, with no freeze until the next keyframe, and audio keeps its place. Live sources without a rewind ring only rebuild and pick up at the next keyframe.
    • In the background, or when VT refuses the rebuild: record the recovery as owed and do it on the foreground return.

2. Rebuild the session on every foreground return

In exitBackgroundAudioOnly(), rebuild the hardware session unconditionally before clearing backgroundAudioOnly. It costs a few milliseconds and covers the invalidation that nothing observed while video was dropped.

3. Do not enter audio-only when there is no audio

Pass the software host's audio state into backgroundAction, for example softwareHostHasAudio: softwareHost?.audioDelivery == .decoded. When it is false and PiP is not active, return .teardownVideo instead of .enterSoftwareAudioOnly. The session is then torn down like a paused one (state = .paused) and the host's existing foreground reload (reloadAtCurrentPosition()) restores it at the frame it left. An alternative is to make the video branch pace on the clock while backgroundAudioOnly() is set and the source has no audio.

4. Log the lifecycle

Log entering and leaving audio-only, and log session loss, rebuild and reposition, so field logs show what triggered a recovery.

Alternative considered

Rebuilding inside HardwareVideoDecoder on the first -12903 and resuming at the next keyframe works and is self-contained. But it leaves the picture frozen for up to one GOP, it cannot recover packets the host already dropped (Case B above), and the decoder cannot tell whether the app is in the foreground. Recovery at the host level avoids all three problems.

Steps to reproduce

none

AetherEngine version or commit SHA

7.34.0

Host app

Custom / my own integration

Platform

iOS

OS version

macOS26.6.2

Device / chip

Mac mini m4

Playback path

Not sure / not a playback bug

Source media (for playback bugs)

No response

Error codes / log lines


Anything else

No response

Activity

  1. added
    bug
    on Oct 11, 2026
  2. superuser404notfound commented on Oct 12, 2026

    @superuser404notfound
    Owner

    Thanks for the thorough write-up. The root-cause analysis was right on all three counts, and the fix in #750 follows the proposal closely:

    • HardwareVideoDecoder marks the session lost on kVTInvalidSessionErr, stops feeding VT, and fires onSessionLost once. rebuildSession() goes through one session factory shared with open().
    • In the foreground the host rebuilds and seeks to currentTime. In the background, or if VT refuses, the recovery is owed to the foreground return. exitBackgroundAudioOnly() now rebuilds unconditionally.
    • backgroundAction takes softwareHostHasAudio, so a SW host without decoded audio is released like a paused session instead of entering audio-only (your Case B).
    • Entering and leaving audio-only, the loss, the rebuild and the reposition are logged now.

    To reproduce it without a device, I added aetherctl bgaudio --lose-session. It invalidates the session in place, which yields the same -12903, and video stayed at +0 frames before the fix. I haven't run an actual iOS screen lock yet. If you can try the branch (fix/739-vt-session-loss) on your Case A and Case B files before it is released, that would be the real confirmation.

  3. ijuniorfu commented on Oct 12, 2026

    @ijuniorfu
    Author

    Tested; the issue has been resolved.

  4. superuser404notfound commented on Oct 12, 2026

    @superuser404notfound
    Owner

    Merged into main (3149b39), so the fix branch is gone. Please test against main instead. It will be in the next release.

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