From 634f211ae69c6fa327d410527b00d3f7d00126e7 Mon Sep 17 00:00:00 2001 From: Samuel Mira Date: Wed, 20 Oct 2021 15:21:09 +0300 Subject: Fix crashes on qandroidaudiodecoder Running the auto tests in android can fail and it could lead to crashes and a pending QThread. This patch is for fixing those issues. Fixes: QTBUG-97647 Pick-to: 6.2 Change-Id: I578f199127519e4620abcb0dac36a1603d8d77bd Reviewed-by: Lars Knoll --- .../platform/android/audio/qandroidaudiodecoder.cpp | 18 +++++++++++++++--- .../platform/android/audio/qandroidaudiodecoder_p.h | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/multimedia/platform/android/audio/qandroidaudiodecoder.cpp b/src/multimedia/platform/android/audio/qandroidaudiodecoder.cpp index 718492118..d6d284504 100644 --- a/src/multimedia/platform/android/audio/qandroidaudiodecoder.cpp +++ b/src/multimedia/platform/android/audio/qandroidaudiodecoder.cpp @@ -81,6 +81,9 @@ Decoder::~Decoder() void Decoder::stop() { + if (!m_codec) + return; + const media_status_t err = AMediaCodec_stop(m_codec); if (err != AMEDIA_OK) qCWarning(adLogger) << "stop() error: " << err; @@ -165,6 +168,11 @@ void Decoder::doDecode() createDecoder(); + if (!m_codec) { + emit error(QAudioDecoder::ResourceError, tr("Audio Decoder could not be created.")); + return; + } + media_status_t status = AMediaCodec_configure(m_codec, m_format, nullptr /* surface */, nullptr /* crypto */, 0); @@ -298,9 +306,9 @@ void QAndroidAudioDecoder::start() setIsDecoding(true); m_position = -1; - QThread *threadDecoder = new QThread(this); - m_decoder->moveToThread(threadDecoder); - threadDecoder->start(); + m_threadDecoder = new QThread(this); + m_decoder->moveToThread(m_threadDecoder); + m_threadDecoder->start(); decode(); } @@ -311,6 +319,9 @@ void QAndroidAudioDecoder::stop() m_decoder->stop(); + if (m_threadDecoder && m_threadDecoder->isRunning()) + m_threadDecoder->exit(); + QMutexLocker locker(&m_buffersMutex); m_position = -1; m_audioBuffer.clear(); @@ -369,6 +380,7 @@ void QAndroidAudioDecoder::durationChanged(qint64 duration) void QAndroidAudioDecoder::error(const QAudioDecoder::Error err, const QString &errorString) { + stop(); emit QPlatformAudioDecoder::error(err, errorString); } diff --git a/src/multimedia/platform/android/audio/qandroidaudiodecoder_p.h b/src/multimedia/platform/android/audio/qandroidaudiodecoder_p.h index d6e1bf32e..efb7cdc24 100644 --- a/src/multimedia/platform/android/audio/qandroidaudiodecoder_p.h +++ b/src/multimedia/platform/android/audio/qandroidaudiodecoder_p.h @@ -144,6 +144,8 @@ private: int m_buffersAvailable = 0; QByteArray m_deviceBuffer; + + QThread *m_threadDecoder; }; QT_END_NAMESPACE -- cgit v1.2.3