Make PlaybackEngine thread-safe to fix FFmpeg segfault

Video and audio threads both pull from the same engine, racing on the
MPS demuxer file pointer and feeding corrupted H.264 data to FFmpeg (which
segfaults). Synchronize the pull methods and wait for the audio thread
before closing the engine.
This commit is contained in:
2026-09-15 23:47:41 -04:00
parent 0a16d3d3ec
commit 3ded804b94
2 changed files with 10 additions and 5 deletions
@@ -195,7 +195,7 @@ public final class PlayerApp extends Application {
try (UmdIsoReader iso = new UmdIsoReader(Path.of(isoPath));
PlaybackEngine engine = PlaybackEngine.open(iso, streamName, 0)) {
startAudio(engine);
Thread audio = startAudio(engine);
awaitClock();
long basePts = clockStartPts;
long baseNanos = clockStartNanos;
@@ -219,6 +219,10 @@ public final class PlayerApp extends Application {
sleepUntil(targetNanos);
}
// Wait for the audio thread to finish draining before the
// engine (and its decoders) is closed by this try block.
audio.join(2000);
MoviePlayer mp = moviePlayer;
if (mp != null) {
mp.onPlayListEnd(playListNumber);
@@ -232,7 +236,7 @@ public final class PlayerApp extends Application {
}
/** Plays decoded PCM through the system audio line, acting as the master A/V clock. */
private void startAudio(PlaybackEngine engine) {
private Thread startAudio(PlaybackEngine engine) {
Thread audio = new Thread(() -> {
SourceDataLine line = null;
try {
@@ -264,6 +268,7 @@ public final class PlayerApp extends Application {
}, "audio-player");
audio.setDaemon(true);
audio.start();
return audio;
}
private static SourceDataLine openLine() throws Exception {