From 8a2d0fe56b8db0a4723078109a02881dff26f67f Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Fri, 3 Feb 2023 02:38:08 +0100 Subject: [PATCH] Recorder bugfix + rewrote RTL-TCP source to use new network lib --- decoder_modules/radio/src/radio_module.h | 2 +- misc_modules/recorder/src/main.cpp | 16 +- source_modules/rtl_tcp_source/src/main.cpp | 250 +++++++----------- .../rtl_tcp_source/src/rtltcp_client.h | 200 -------------- 4 files changed, 106 insertions(+), 362 deletions(-) delete mode 100644 source_modules/rtl_tcp_source/src/rtltcp_client.h diff --git a/decoder_modules/radio/src/radio_module.h b/decoder_modules/radio/src/radio_module.h index 8e38ee39..8600cf0a 100644 --- a/decoder_modules/radio/src/radio_module.h +++ b/decoder_modules/radio/src/radio_module.h @@ -426,7 +426,7 @@ private: // Configure noise blanker nb.setRate(500.0 / ifSamplerate); setNBLevel(nbLevel); - setNBEnabled(nbAllowed&& nbEnabled); + setNBEnabled(nbAllowed && nbEnabled); // Configure FM IF Noise Reduction setIFNRPreset((selectedDemodID == RADIO_DEMOD_NFM) ? ifnrPresets[fmIFPresetId] : IFNR_PRESET_BROADCAST); diff --git a/misc_modules/recorder/src/main.cpp b/misc_modules/recorder/src/main.cpp index 96203469..e9ca8bed 100644 --- a/misc_modules/recorder/src/main.cpp +++ b/misc_modules/recorder/src/main.cpp @@ -95,11 +95,11 @@ public: splitter.init(&volume.out); splitter.bindStream(&meterStream); meter.init(&meterStream); - s2m.init(NULL); + s2m.init(&stereoStream); // Init sinks basebandSink.init(NULL, complexHandler, this); - stereoSink.init(NULL, stereoHandler, this); + stereoSink.init(&stereoStream, stereoHandler, this); monoSink.init(&s2m.out, monoHandler, this); gui::menu.registerEntry(name, menuHandler, this); @@ -178,17 +178,15 @@ public: // Open audio stream or baseband if (recMode == RECORDER_MODE_AUDIO) { - // TODO: Select the stereo to mono converter if needed - stereoStream = sigpath::sinkManager.bindStream(selectedStreamName); + // Start correct path depending on if (stereo) { - stereoSink.setInput(stereoStream); stereoSink.start(); } else { - s2m.setInput(stereoStream); s2m.start(); monoSink.start(); } + splitter.bindStream(&stereoStream); } else { // Create and bind IQ stream @@ -207,11 +205,11 @@ public: // Close audio stream or baseband if (recMode == RECORDER_MODE_AUDIO) { - // NOTE: Has to be done before the unbind since the stream is deleted... + splitter.unbindStream(&stereoStream); monoSink.stop(); stereoSink.stop(); s2m.stop(); - sigpath::sinkManager.unbindStream(selectedStreamName, stereoStream); + } else { // Unbind and destroy IQ stream @@ -535,7 +533,7 @@ private: wav::Writer writer; std::recursive_mutex recMtx; dsp::stream* basebandStream; - dsp::stream* stereoStream; + dsp::stream stereoStream; dsp::sink::Handler basebandSink; dsp::sink::Handler stereoSink; dsp::sink::Handler monoSink; diff --git a/source_modules/rtl_tcp_source/src/main.cpp b/source_modules/rtl_tcp_source/src/main.cpp index feddb1c8..05df8168 100644 --- a/source_modules/rtl_tcp_source/src/main.cpp +++ b/source_modules/rtl_tcp_source/src/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -7,6 +7,7 @@ #include #include #include +#include #define CONCAT(a, b) ((std::string(a) + b).c_str()) @@ -14,86 +15,73 @@ SDRPP_MOD_INFO{ /* Name: */ "rtl_tcp_source", /* Description: */ "RTL-TCP source module for SDR++", /* Author: */ "Ryzerth", - /* Version: */ 0, 1, 0, + /* Version: */ 1, 1, 0, /* Max instances */ 1 }; ConfigManager config; -const double sampleRates[] = { - 250000, - 1024000, - 1536000, - 1792000, - 1920000, - 2048000, - 2160000, - 2400000, - 2560000, - 2880000, - 3200000 -}; - -const char* sampleRatesTxt[] = { - "250KHz", - "1.024MHz", - "1.536MHz", - "1.792MHz", - "1.92MHz", - "2.048MHz", - "2.16MHz", - "2.4MHz", - "2.56MHz", - "2.88MHz", - "3.2MHz" -}; - -#define SAMPLE_RATE_COUNT (sizeof(sampleRates) / sizeof(double)) - class RTLTCPSourceModule : public ModuleManager::Instance { public: RTLTCPSourceModule(std::string name) { this->name = name; - sampleRate = 2400000.0; + // Define samplerates + samplerates.define(250e3, "250KHz", 250e3); + samplerates.define(1.024e6, "1.024MHz", 1.024e6); + samplerates.define(1.536e6, "1.536MHz", 1.536e6); + samplerates.define(1.792e6, "1.792MHz", 1.792e6); + samplerates.define(1.92e6, "1.92MHz", 1.92e6); + samplerates.define(2.048e6, "2.048MHz", 2.048e6); + samplerates.define(2.16e6, "2.16MHz", 2.16e6); + samplerates.define(2.4e6, "2.4MHz", 2.4e6); + samplerates.define(2.56e6, "2.56MHz", 2.56e6); + samplerates.define(2.88e6, "2.88MHz", 2.88e6); + samplerates.define(3.2e6, "3.2MHz", 3.2e6); - int _24id = 0; - for (int i = 0; i < SAMPLE_RATE_COUNT; i++) { - if (sampleRates[i] == 2400000) { _24id = i; } - srTxt += sampleRatesTxt[i]; - srTxt += '\0'; - } - srId = 7; + // Define direct sampling modes + directSamplingModes.define(0, "Disabled", 0); + directSamplingModes.define(1, "I branch", 1); + directSamplingModes.define(2, "Q branch", 2); + // Select the default samplerate instead of id 0 + srId = samplerates.valueId(2.4e6); + + // Load config config.acquire(); - std::string hostStr = config.conf["host"]; - port = config.conf["port"]; - double wantedSr = config.conf["sampleRate"]; - directSamplingMode = config.conf["directSamplingMode"]; - ppm = config.conf["ppm"]; - rtlAGC = config.conf["rtlAGC"]; - tunerAGC = config.conf["tunerAGC"]; - gain = std::clamp(config.conf["gainIndex"], 0, 28); - biasTee = config.conf["biasTee"]; - offsetTuning = config.conf["offsetTuning"]; - hostStr = hostStr.substr(0, 1023); - strcpy(ip, hostStr.c_str()); + if (config.conf.contains("host")) { + std::string hostStr = config.conf["host"]; + strcpy(ip, hostStr.c_str()); + } + if (config.conf.contains("port")) { + port = config.conf["port"]; + } + if (config.conf.contains("sampleRate")) { + double sr = config.conf["sampleRate"]; + if (samplerates.keyExists(sr)) { srId = samplerates.keyId(sr); } + } + if (config.conf.contains("directSamplingMode")) { + int mode = config.conf["directSamplingMode"]; + if (directSamplingModes.keyExists(mode)) { directSamplingId = directSamplingModes.keyId(mode); } + } + if (config.conf.contains("ppm")) { + ppm = config.conf["ppm"]; + } + if (config.conf.contains("gainIndex")) { + gain = config.conf["gainIndex"]; + } + if (config.conf.contains("biasTee")) { + biasTee = config.conf["biasTee"]; + } + if (config.conf.contains("offsetTuning")) { + offsetTuning = config.conf["offsetTuning"]; + } config.release(); - bool found = false; - for (int i = 0; i < SAMPLE_RATE_COUNT; i++) { - if (sampleRates[i] == wantedSr) { - found = true; - srId = i; - sampleRate = sampleRates[i]; - break; - } - } - if (!found) { - srId = _24id; - sampleRate = sampleRates[_24id]; - } + // Update samplerate + sampleRate = samplerates[srId]; + // Register source handler.ctx = this; handler.selectHandler = menuSelected; handler.deselectHandler = menuDeselected; @@ -139,48 +127,48 @@ private: static void start(void* ctx) { RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx; if (_this->running) { return; } - if (!_this->client.connectToRTL(_this->ip, _this->port)) { - spdlog::error("Could not connect to {0}:{1}", _this->ip, _this->port); + + // Connect to the server + try { + _this->client = rtltcp::connect(&_this->stream, _this->ip, _this->port); + } + catch (std::exception e) { + spdlog::error("Could connect to RTL-TCP server: {0}", e.what()); return; } - spdlog::warn("Setting sample rate to {0}", _this->sampleRate); - _this->client.setFrequency(_this->freq); - _this->client.setSampleRate(_this->sampleRate); - _this->client.setPPM(_this->ppm); - _this->client.setDirectSampling(_this->directSamplingMode); - _this->client.setAGCMode(_this->rtlAGC); - _this->client.setBiasTee(_this->biasTee); - _this->client.setOffsetTuning(_this->offsetTuning); + + // Sync settings + _this->client->setFrequency(_this->freq); + _this->client->setSampleRate(_this->sampleRate); + _this->client->setPPM(_this->ppm); + _this->client->setDirectSampling(_this->directSamplingId); + _this->client->setAGCMode(_this->rtlAGC); + _this->client->setBiasTee(_this->biasTee); + _this->client->setOffsetTuning(_this->offsetTuning); if (_this->tunerAGC) { - _this->client.setGainMode(0); + _this->client->setGainMode(0); } else { - _this->client.setGainMode(1); - - // Setting it twice because for some reason it refuses to do it on the first time - _this->client.setGainIndex(_this->gain); + _this->client->setGainMode(1); + _this->client->setGainIndex(_this->gain); } _this->running = true; - _this->workerThread = std::thread(worker, _this); spdlog::info("RTLTCPSourceModule '{0}': Start!", _this->name); } static void stop(void* ctx) { RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx; if (!_this->running) { return; } + _this->client->close(); _this->running = false; - _this->stream.stopWriter(); - _this->workerThread.join(); - _this->stream.clearWriteStop(); - _this->client.disconnect(); spdlog::info("RTLTCPSourceModule '{0}': Stop!", _this->name); } static void tune(double freq, void* ctx) { RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx; if (_this->running) { - _this->client.setFrequency(freq); + _this->client->setFrequency(freq); } _this->freq = freq; spdlog::info("RTLTCPSourceModule '{0}': Tune: {1}!", _this->name, freq); @@ -205,8 +193,8 @@ private: } SmGui::FillWidth(); - if (SmGui::Combo(CONCAT("##_rtltcp_sr_", _this->name), &_this->srId, _this->srTxt.c_str())) { - _this->sampleRate = sampleRates[_this->srId]; + if (SmGui::Combo(CONCAT("##_rtltcp_sr_", _this->name), &_this->srId, _this->samplerates.txt)) { + _this->sampleRate = _this->samplerates[_this->srId]; core::setInputSampleRate(_this->sampleRate); config.acquire(); config.conf["sampleRate"] = _this->sampleRate; @@ -217,13 +205,13 @@ private: SmGui::LeftLabel("Direct Sampling"); SmGui::FillWidth(); - if (SmGui::Combo(CONCAT("##_rtltcp_ds_", _this->name), &_this->directSamplingMode, "Disabled\0I branch\0Q branch\0")) { + if (SmGui::Combo(CONCAT("##_rtltcp_ds_", _this->name), &_this->directSamplingId, "Disabled\0I branch\0Q branch\0")) { if (_this->running) { - _this->client.setDirectSampling(_this->directSamplingMode); - _this->client.setGainIndex(_this->gain); + _this->client->setDirectSampling(_this->directSamplingId); + _this->client->setGainIndex(_this->gain); } config.acquire(); - config.conf["directSamplingMode"] = _this->directSamplingMode; + config.conf["directSamplingMode"] = _this->directSamplingId; config.release(true); } @@ -231,7 +219,7 @@ private: SmGui::FillWidth(); if (SmGui::InputInt(CONCAT("##_rtltcp_ppm_", _this->name), &_this->ppm, 1, 10)) { if (_this->running) { - _this->client.setPPM(_this->ppm); + _this->client->setPPM(_this->ppm); } config.acquire(); config.conf["ppm"] = _this->ppm; @@ -243,7 +231,7 @@ private: SmGui::FillWidth(); if (SmGui::SliderInt(CONCAT("##_gain_select_", _this->name), &_this->gain, 0, 28, SmGui::FMT_STR_NONE)) { if (_this->running) { - _this->client.setGainIndex(_this->gain); + _this->client->setGainIndex(_this->gain); } config.acquire(); config.conf["gainIndex"] = _this->gain; @@ -253,7 +241,7 @@ private: if (SmGui::Checkbox(CONCAT("Bias-T##_biast_select_", _this->name), &_this->biasTee)) { if (_this->running) { - _this->client.setBiasTee(_this->biasTee); + _this->client->setBiasTee(_this->biasTee); } config.acquire(); config.conf["biasTee"] = _this->biasTee; @@ -262,7 +250,7 @@ private: if (SmGui::Checkbox(CONCAT("Offset Tuning##_biast_select_", _this->name), &_this->offsetTuning)) { if (_this->running) { - _this->client.setOffsetTuning(_this->offsetTuning); + _this->client->setOffsetTuning(_this->offsetTuning); } config.acquire(); config.conf["offsetTuning"] = _this->offsetTuning; @@ -271,9 +259,9 @@ private: if (SmGui::Checkbox("RTL AGC", &_this->rtlAGC)) { if (_this->running) { - _this->client.setAGCMode(_this->rtlAGC); + _this->client->setAGCMode(_this->rtlAGC); if (!_this->rtlAGC) { - _this->client.setGainIndex(_this->gain); + _this->client->setGainIndex(_this->gain); } } config.acquire(); @@ -284,9 +272,9 @@ private: SmGui::ForceSync(); if (SmGui::Checkbox("Tuner AGC", &_this->tunerAGC)) { if (_this->running) { - _this->client.setGainMode(!_this->tunerAGC); + _this->client->setGainMode(!_this->tunerAGC); if (!_this->tunerAGC) { - _this->client.setGainIndex(_this->gain); + _this->client->setGainIndex(_this->gain); } } config.acquire(); @@ -295,77 +283,35 @@ private: } } - static void worker(void* ctx) { - RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx; - int blockSize = _this->sampleRate / 200.0f; - uint8_t* inBuf = new uint8_t[blockSize * 2]; - - while (true) { - // Read samples here - _this->client.receiveData(inBuf, blockSize * 2); - for (int i = 0; i < blockSize; i++) { - _this->stream.writeBuf[i].re = ((double)inBuf[i * 2] - 128.0) / 128.0; - _this->stream.writeBuf[i].im = ((double)inBuf[(i * 2) + 1] - 128.0) / 128.0; - } - if (!_this->stream.swap(blockSize)) { break; }; - } - - delete[] inBuf; - } - std::string name; bool enabled = true; dsp::stream stream; double sampleRate; SourceManager::SourceHandler handler; std::thread workerThread; - RTLTCPClient client; + std::shared_ptr client; bool running = false; double freq; + char ip[1024] = "localhost"; int port = 1234; - int gain = 0; - int ppm = 0; - bool rtlAGC = false; - bool tunerAGC = false; - int directSamplingMode = 0; int srId = 0; + int directSamplingId = 0; + int ppm = 0; + int gain = 0; bool biasTee = false; bool offsetTuning = false; + bool rtlAGC = false; + bool tunerAGC = false; - std::string srTxt = ""; + OptionList samplerates; + OptionList directSamplingModes; }; MOD_EXPORT void _INIT_() { config.setPath(core::args["root"].s() + "/rtl_tcp_config.json"); - json defConf; - defConf["host"] = "localhost"; - defConf["port"] = 1234; - defConf["sampleRate"] = 2400000.0; - defConf["directSamplingMode"] = 0; - defConf["ppm"] = 0; - defConf["rtlAGC"] = false; - defConf["tunerAGC"] = false; - defConf["gainIndex"] = 0; - defConf["biasTee"] = false; - defConf["offsetTuning"] = false; - config.load(defConf); + config.load(json({})); config.enableAutoSave(); - - config.acquire(); - if (!config.conf.contains("biasTee")) { - config.conf["biasTee"] = false; - } - if (!config.conf.contains("offsetTuning")) { - config.conf["offsetTuning"] = false; - } - if (!config.conf.contains("ppm")) { - config.conf["ppm"] = 0; - } - if (!config.conf.contains("sampleRate")) { - config.conf["sampleRate"] = 2400000.0; - } - config.release(true); } MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) { diff --git a/source_modules/rtl_tcp_source/src/rtltcp_client.h b/source_modules/rtl_tcp_source/src/rtltcp_client.h deleted file mode 100644 index cdae214f..00000000 --- a/source_modules/rtl_tcp_source/src/rtltcp_client.h +++ /dev/null @@ -1,200 +0,0 @@ -#pragma once -#include -#include -#include - -#ifdef _WIN32 -#include -#include -#else -#include -#include -#include -#include -#include -#include -#endif - -#ifdef _WIN32 -#define __attribute__(x) -#pragma pack(push, 1) -#endif -struct command_t { - unsigned char cmd; - unsigned int param; -} __attribute__((packed)); -#ifdef _WIN32 -#pragma pack(pop) -#endif - -class RTLTCPClient { -public: - RTLTCPClient() { - } - - bool connectToRTL(char* host, uint16_t port) { - if (connected) { - return true; - } - -#ifdef _WIN32 - struct addrinfo* result = NULL; - struct addrinfo* ptr = NULL; - struct addrinfo hints; - - WSADATA wsaData; - WSAStartup(MAKEWORD(2, 2), &wsaData); - - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - - char buf[128]; - sprintf(buf, "%hu", port); - - int iResult = getaddrinfo(host, buf, &hints, &result); - if (iResult != 0) { - // TODO: log error - printf("\n%s\n", gai_strerror(iResult)); - WSACleanup(); - return false; - } - ptr = result; - - sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); - - if (sock == INVALID_SOCKET) { - // TODO: log error - printf("B"); - freeaddrinfo(result); - WSACleanup(); - return false; - } - - iResult = connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen); - if (iResult == SOCKET_ERROR) { - printf("C"); - closesocket(sock); - freeaddrinfo(result); - WSACleanup(); - return false; - } - freeaddrinfo(result); -#else - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) { - // TODO: Log error - return false; - } - struct hostent* server = gethostbyname(host); - struct sockaddr_in serv_addr; - bzero(&serv_addr, sizeof(struct sockaddr_in)); - serv_addr.sin_family = AF_INET; - bcopy((char*)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length); - serv_addr.sin_port = htons(port); - if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { - // TODO: log error - return false; - } -#endif - - connected = true; - - printf("Connected"); - - return true; - } - - void disconnect() { - if (!connected) { - return; - } -#ifdef _WIN32 - closesocket(sock); - WSACleanup(); -#else - close(sockfd); -#endif - connected = false; - } - - // struct command_t { - // uint8_t cmd; - // uint32_t arg; - // }; - - void sendCommand(uint8_t command, uint32_t param) { - command_t cmd; - cmd.cmd = command; - cmd.param = htonl(param); -#ifdef _WIN32 - send(sock, (char*)&cmd, sizeof(command_t), 0); -#else - (void)write(sockfd, &cmd, sizeof(command_t)); -#endif - } - - void receiveData(uint8_t* buf, size_t count) { - int received = 0; - int ret = 0; - while (received < count) { -#ifdef _WIN32 - ret = recv(sock, (char*)&buf[received], count - received, 0); -#else - ret = read(sockfd, &buf[received], count - received); -#endif - if (ret <= 0) { return; } - received += ret; - } - } - - void setFrequency(double freq) { - sendCommand(1, freq); - } - - void setSampleRate(double sr) { - sendCommand(2, sr); - } - - void setGainMode(int mode) { - sendCommand(3, mode); - } - - void setGain(double gain) { - sendCommand(4, gain); - } - - void setPPM(int ppm) { - sendCommand(5, (uint32_t)ppm); - } - - void setAGCMode(int mode) { - sendCommand(8, mode); - } - - void setDirectSampling(int mode) { - sendCommand(9, mode); - } - - void setOffsetTuning(bool enabled) { - sendCommand(10, enabled); - } - - void setGainIndex(int index) { - sendCommand(13, index); - } - - void setBiasTee(bool enabled) { - sendCommand(14, enabled); - } - -private: -#ifdef _WIN32 - SOCKET sock; -#else - int sockfd; -#endif - - bool connected = false; -}; \ No newline at end of file