From d7796f6d6a5d0f858151fa6f561fd0cf610c41a0 Mon Sep 17 00:00:00 2001 From: vsonnier Date: Sun, 10 Mar 2019 18:11:34 +0100 Subject: [PATCH] Re-read sample rate and MTU AFTER stream activation to recompute CubicSDR buffers --- src/sdr/SoapySDRThread.cpp | 64 +++++++++++++------------------------- src/sdr/SoapySDRThread.h | 4 +-- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/sdr/SoapySDRThread.cpp b/src/sdr/SoapySDRThread.cpp index 9772c90c..3782a6bb 100644 --- a/src/sdr/SoapySDRThread.cpp +++ b/src/sdr/SoapySDRThread.cpp @@ -103,33 +103,7 @@ bool SDRThread::init() { //2. Set sample rate: device->setSampleRate(SOAPY_SDR_RX, 0, sampleRate.load()); - //2.2 Device-specific workarounds: - // TODO: explore bandwidth setting option to see if this is necessary for others - if (device->getDriverKey() == "bladeRF") { - device->setBandwidth(SOAPY_SDR_RX, 0, sampleRate.load()); - } - - //3. Re-read MTU and build Cubic buffers: - int streamMTU = device->getStreamMTU(stream); - mtuElems.store(streamMTU); - - numChannels.store(getOptimalChannelCount(sampleRate.load())); - numElems.store(getOptimalElementCount(sampleRate.load(), TARGET_DISPLAY_FPS)); - - //fallback if mtuElems was wrong - if (!mtuElems.load()) { - mtuElems.store(numElems.load()); - std::cout << "SDRThread::init(): Device Stream MTU is broken, use " << mtuElems.load() << "instead..." << std::endl << std::flush; - } else { - std::cout << "SDRThread::init(): Device Stream set to MTU: " << mtuElems.load() << std::endl << std::flush; - } - - overflowBuffer.data.resize(mtuElems.load()); - - buffs[0] = ::malloc(mtuElems.load() * 4 * sizeof(float)); - numOverflow = 0; - - //3.2 Store Stream-specific parameters to current Device + //3. Store Stream-specific parameters to current Device deviceInfo.load()->setStreamArgs(currentStreamArgs); deviceConfig.load()->setStreamOpts(currentStreamArgs); @@ -177,14 +151,14 @@ bool SDRThread::init() { setting_value_changed.store(false); } //leave lock guard scope - - updateSettings(); - + wxGetApp().sdrThreadNotify(SDRThread::SDR_THREAD_INITIALIZED, std::string("Device Initialized.")); - //5. Activate stream: + //5. Activate stream: (through update settings) wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, std::string("Activating stream.")); - device->activateStream(stream); + + rate_changed.store(true); + updateSettings(); //rebuild menu now that settings are really been applied. wxGetApp().notifyMainUIOfDeviceChange(true); @@ -195,8 +169,13 @@ bool SDRThread::init() { void SDRThread::deinit() { device->deactivateStream(stream); device->closeStream(stream); + + if (buffs[0] != nullptr) { + ::free(buffs[0]); + buffs[0] = nullptr; + } + stream = nullptr; - ::free(buffs[0]); } void SDRThread::assureBufferMinSize(SDRThreadIQData * dataOut, size_t minSize) { @@ -470,34 +449,35 @@ void SDRThread::updateSettings() { device->setBandwidth(SOAPY_SDR_RX, 0, sampleRate.load()); } - //3. Re-do Cubic buffers : - //re-read current sample rate: + //3. Re-activate stream: + device->activateStream(stream); + + //4. Re-do Cubic buffers : + //re-read current sample rate and MTU: sampleRate.store(device->getSampleRate(SOAPY_SDR_RX, 0)); numChannels.store(getOptimalChannelCount(sampleRate.load())); numElems.store(getOptimalElementCount(sampleRate.load(), TARGET_DISPLAY_FPS)); //read (new) MTU size: int streamMTU = device->getStreamMTU(stream); - mtuElems.store(streamMTU); //fallback if mtuElems was wrong if (!mtuElems.load()) { mtuElems.store(numElems.load()); - std::cout << "SDRThread::updateSettings(): Device Stream MTU is broken, use " << mtuElems.load() << "instead..." << std::endl << std::flush; + std::cout << "SDRThread: Device Stream MTU is broken, use " << mtuElems.load() << " instead..." << std::endl << std::flush; } else { - std::cout << "SDRThread::updateSettings(): Device Stream changing to MTU: " << mtuElems.load() << std::endl << std::flush; + std::cout << "SDRThread : Device Stream set to MTU: " << mtuElems.load() << std::endl << std::flush; } overflowBuffer.data.resize(mtuElems.load()); - ::free(buffs[0]); + if (buffs[0] != nullptr) { + ::free(buffs[0]); + } buffs[0] = ::malloc(mtuElems.load() * 4 * sizeof(float)); //clear overflow buffer numOverflow = 0; - //4. Re-activate stream: - device->activateStream(stream); - // rate_changed.store(false); doUpdate = true; diff --git a/src/sdr/SoapySDRThread.h b/src/sdr/SoapySDRThread.h index c72c5a9d..b767c8a2 100644 --- a/src/sdr/SoapySDRThread.h +++ b/src/sdr/SoapySDRThread.h @@ -106,9 +106,9 @@ class SDRThread : public IOThread { void updateSettings(); SoapySDR::Kwargs combineArgs(SoapySDR::Kwargs a, SoapySDR::Kwargs b); - SoapySDR::Stream *stream; + SoapySDR::Stream *stream = nullptr; SoapySDR::Device *device; - void *buffs[1]; + void *buffs[1] = { nullptr }; ReBuffer buffers; SDRThreadIQData overflowBuffer; int numOverflow;