From 88a6cf4e4aaa58229b9b07d66c2abd264dfbc2f1 Mon Sep 17 00:00:00 2001
From: Mladen Milinkovic <maxrd2@smoothware.net>
Date: Fri, 17 Jun 2022 21:34:20 +0200
Subject: [PATCH] Fix deadlock/crash while changing audio stream on
 paused/stopped media #71

---
 src/videoplayer/backend/audiodecoder.cpp | 2 +-
 src/videoplayer/backend/decoder.cpp      | 1 +
 src/videoplayer/backend/ffplayer.cpp     | 2 ++
 src/videoplayer/videoplayer.cpp          | 2 ++
 4 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/videoplayer/backend/audiodecoder.cpp b/src/videoplayer/backend/audiodecoder.cpp
index d3d13e78..9c7ee1fc 100644
--- a/src/videoplayer/backend/audiodecoder.cpp
+++ b/src/videoplayer/backend/audiodecoder.cpp
@@ -499,7 +499,7 @@ AudioDecoder::run()
 			// bytes needed for 100ms of audio
 			const ALint hwMinBytes = m_vs->audClk.speed() * m_fmtTgt.bytesPerSec * .100;
 
-			while(!m_vs->abortRequested) {
+			while(!m_vs->abortRequested && !isInterruptionRequested()) {
 				ALint hwBufOffset = 0;
 				alGetSourcei(m_alSrc, AL_BYTE_OFFSET, &hwBufOffset);
 				if(!std::isnan(af->pts)) {
diff --git a/src/videoplayer/backend/decoder.cpp b/src/videoplayer/backend/decoder.cpp
index 79b1ad7a..8a69d918 100644
--- a/src/videoplayer/backend/decoder.cpp
+++ b/src/videoplayer/backend/decoder.cpp
@@ -155,6 +155,7 @@ Decoder::abort()
 	m_queue->abort();
 	if(m_frameQueue)
 		m_frameQueue->signal();
+	requestInterruption();
 	wait();
 	m_queue->flush();
 }
diff --git a/src/videoplayer/backend/ffplayer.cpp b/src/videoplayer/backend/ffplayer.cpp
index 88288285..7b26e4a4 100644
--- a/src/videoplayer/backend/ffplayer.cpp
+++ b/src/videoplayer/backend/ffplayer.cpp
@@ -288,6 +288,8 @@ FFPlayer::activeAudioStream()
 void
 FFPlayer::activeAudioStream(int streamIndex)
 {
+	if(!m_vs)
+		return;
 	streamIndex = streamIndex < 0 ? -1 : m_vs->demuxer->absoluteStreamIndex(AVMEDIA_TYPE_AUDIO, streamIndex);
 	m_vs->demuxer->selectStream(AVMEDIA_TYPE_AUDIO, streamIndex);
 }
diff --git a/src/videoplayer/videoplayer.cpp b/src/videoplayer/videoplayer.cpp
index 0307ce7d..a42babe5 100644
--- a/src/videoplayer/videoplayer.cpp
+++ b/src/videoplayer/videoplayer.cpp
@@ -301,6 +301,8 @@ VideoPlayer::setupNotifications()
 
 	//connect(m_player, &FFPlayer::videoStreamsChanged, this, [this](const QStringList &streams){});
 	connect(m_player, &FFPlayer::audioStreamsChanged, this, [this](const QStringList &streams){
+		if(m_activeAudioStream >= 0)
+			m_player->activeAudioStream(m_activeAudioStream);
 		emit audioStreamsChanged(m_audioStreams = streams);
 		emit activeAudioStreamChanged(m_activeAudioStream = m_player->activeAudioStream());
 	});
-- 
GitLab

