From 7880a523a782fdfe9e0846a598024df04d2fffa2 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 6 Feb 2022 12:08:04 +0100 Subject: [PATCH] SDRPlayV3: prevent segfault when SDRPlay library fails to initialize --- .../samplesource/sdrplayv3/sdrplayv3input.cpp | 2 +- .../sdrplayv3/sdrplayv3thread.cpp | 34 ++++++++++++------- .../samplesource/sdrplayv3/sdrplayv3thread.h | 1 + 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/plugins/samplesource/sdrplayv3/sdrplayv3input.cpp b/plugins/samplesource/sdrplayv3/sdrplayv3input.cpp index 7b91c4b9d..2d9fa6433 100644 --- a/plugins/samplesource/sdrplayv3/sdrplayv3input.cpp +++ b/plugins/samplesource/sdrplayv3/sdrplayv3input.cpp @@ -146,7 +146,7 @@ bool SDRPlayV3Input::start() m_sdrPlayThread->setFcPos((int) m_settings.m_fcPos); m_sdrPlayThread->startWork(); - m_running = true; + m_running = m_sdrPlayThread->isRunning(); applySettings(m_settings, true, true); return true; diff --git a/plugins/samplesource/sdrplayv3/sdrplayv3thread.cpp b/plugins/samplesource/sdrplayv3/sdrplayv3thread.cpp index e5d830ffb..6861757fe 100644 --- a/plugins/samplesource/sdrplayv3/sdrplayv3thread.cpp +++ b/plugins/samplesource/sdrplayv3/sdrplayv3thread.cpp @@ -44,10 +44,27 @@ SDRPlayV3Thread::~SDRPlayV3Thread() void SDRPlayV3Thread::startWork() { + sdrplay_api_ErrT err; + sdrplay_api_CallbackFnsT cbFns; + + cbFns.StreamACbFn = &SDRPlayV3Thread::callbackHelper; + cbFns.StreamBCbFn = &SDRPlayV3Thread::callbackHelper; + cbFns.EventCbFn = &SDRPlayV3Thread::eventCallback; + + if ((err = sdrplay_api_Init(m_dev->dev, &cbFns, this)) != sdrplay_api_Success) + { + qCritical() << "SDRPlayV3Thread::run: sdrplay_api_Init error: " << sdrplay_api_GetErrorString(err); + m_running = false; + return; + } + m_startWaitMutex.lock(); start(); - while(!m_running) + + while(!m_running) { m_startWaiter.wait(&m_startWaitMutex, 100); + } + m_startWaitMutex.unlock(); } @@ -58,9 +75,12 @@ void SDRPlayV3Thread::stopWork() if (m_running) { m_running = false; - if ((err = sdrplay_api_Uninit(m_dev->dev)) != sdrplay_api_Success) + + if ((err = sdrplay_api_Uninit(m_dev->dev)) != sdrplay_api_Success) { qWarning() << "SDRPlayV3Thread::callbackHelper: sdrplay_api_Uninit error: " << sdrplay_api_GetErrorString(err); + } } + wait(); } @@ -94,16 +114,6 @@ bool SDRPlayV3Thread::waitForRfChanged() // Don't really need a thread here - just using same structure as other plugins void SDRPlayV3Thread::run() { - sdrplay_api_ErrT err; - sdrplay_api_CallbackFnsT cbFns; - - cbFns.StreamACbFn = &SDRPlayV3Thread::callbackHelper; - cbFns.StreamBCbFn = &SDRPlayV3Thread::callbackHelper; - cbFns.EventCbFn = &SDRPlayV3Thread::eventCallback; - - if ((err = sdrplay_api_Init(m_dev->dev, &cbFns, this)) != sdrplay_api_Success) - qCritical() << "SDRPlayV3Thread::run: sdrplay_api_Init error: " << sdrplay_api_GetErrorString(err); - m_running = true; m_startWaiter.wakeAll(); } diff --git a/plugins/samplesource/sdrplayv3/sdrplayv3thread.h b/plugins/samplesource/sdrplayv3/sdrplayv3thread.h index 308a6ac66..2637263e6 100644 --- a/plugins/samplesource/sdrplayv3/sdrplayv3thread.h +++ b/plugins/samplesource/sdrplayv3/sdrplayv3thread.h @@ -37,6 +37,7 @@ public: void startWork(); void stopWork(); + bool isRunning() const { return m_running; } void setSamplerate(int samplerate); void setLog2Decimation(unsigned int log2_decim); void setFcPos(int fcPos);