Skip to content

fix(sw): recover the VideoToolbox session a screen lock invalidates (#739) - #750

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/739-vt-session-loss
Oct 12, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/739-vt-session-loss

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Addresses #739. Implements the fix the report proposes, all three parts.

What was wrong

  1. HardwareVideoDecoder logged kVTInvalidSessionErr (-12903) per packet and kept feeding the dead session.
  2. exitBackgroundAudioOnly() flushed but reused the same session, which a screen lock had invalidated while nothing decoded video.
  3. backgroundAction sent a SW host without audio into audio-only mode. Only the audio branch paces the demux loop there, so the loop dropped the rest of the file and parked the clock at end of media in the background.

Changes

  • Decoder: on kVTInvalidSessionErr it marks the session lost (under the same hold of lock that fed it), logs once, drops further packets and fires onSessionLost. rebuildSession() recreates the session from the stored format description and attributes through one makeSession factory shared with open(), keeps the refCon box, bumps the feed epoch and gates on the next keyframe.
  • Host: a loss in the foreground is rebuilt and repositioned to the playhead (seek(to: currentTime)), so the picture returns on the audio's frame. Live without a rewind ring only rebuilds. In the background, or when VT refuses, the recovery is owed and paid on the next foreground return. Every return out of audio-only mode rebuilds the session unconditionally.
  • Policy: backgroundAction(... softwareHostHasAudio: ...). A SW host whose audioDelivery is not .decoded and has no PiP window gets .teardownVideo, as a paused session would.
  • Logs: entering/leaving audio-only, loss, rebuild (or refusal), reposition, and the keyframe the decoder recovered at.

Verification

  • aetherctl bgaudio --lose-session (new) invalidates the VT session in place mid-foreground and on entering background. That produces exactly the field error (-12903). On a 1080p HEVC+AAC clip it went from +0 / +0 video frames after the loss and after the foreground return (recovery disabled) to +72 / +166 with the fix:
    [HardwareVideoDecoder] AE#739 session invalidated by the system (-12903) at pts=41984; dropping video until it is rebuilt
    [HardwareVideoDecoder] AE#739 session rebuilt
    [SWHost] AE#739 video session rebuilt, repositioning to the playhead at 2.867s
    [SWHost] background audio-only: dropping video, audio keeps playing
    [SWHost] background audio-only left, video resumes at the next keyframe, hardware session rebuilt=true
    [HardwareVideoDecoder] AE#739 recovered at keyframe pts=98304 after dropping 12 packet(s)
    
  • BackgroundKeepaliveTests: a SW host without audio tears down whether playing or paused, and keeps the PiP case unchanged.
  • swift test: both runners green (4325 swift-testing tests, XCTest suite passed). Builds for generic iOS and tvOS.

Not covered here: an on-device screen lock on iOS. The harness reproduces the invalidation, not the lock itself.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WJcXkGQrMiBPixVEFTSBJS

…739)

On iOS a screen lock invalidates the SW host's VTDecompressionSession.
HardwareVideoDecoder only logged the resulting kVTInvalidSessionErr
(-12903) and kept feeding the dead session, so video never came back;
a source without audio was worse, since background audio-only mode has
nothing to pace the demux loop on and it raced to EOF in the background.

- HardwareVideoDecoder marks the session lost on kVTInvalidSessionErr,
  drops packets instead of calling VT, and fires onSessionLost once.
  rebuildSession() recreates it from the stored format description and
  attributes through the same factory open() uses, bumps the feed epoch
  and resumes at the next keyframe.
- The host rebuilds and repositions to the playhead on a loss seen in
  the foreground (picture returns on the audio's frame), owes it to the
  foreground return in the background or when VT refuses, and rebuilds
  unconditionally on every foreground return out of audio-only mode.
- backgroundAction takes softwareHostHasAudio: a SW host that renders no
  audio is released like a paused session instead of entering audio-only.
- The audio-only lifecycle and the loss/rebuild/reposition are logged.
- aetherctl bgaudio --lose-session invalidates the session mid-foreground
  and on entering background; +0 video frames after both before the fix,
  frames resume after both with it.

Reported and analysed by @ijuniorfu.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WJcXkGQrMiBPixVEFTSBJS
@superuser404notfound
superuser404notfound merged commit 3149b39 into main Oct 12, 2026
4 checks passed
@superuser404notfound
superuser404notfound deleted the fix/739-vt-session-loss branch October 12, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant