1 // Copyright (c) Charles J. Cliffe
2 // SPDX-License-Identifier: GPL-2.0+
3 
4 #pragma once
5 
6 #include <queue>
7 #include <vector>
8 #include <memory>
9 #include "liquid/liquid.h"
10 #include "AudioThread.h"
11 #include "ThreadBlockingQueue.h"
12 #include "CubicSDRDefs.h"
13 #include "Modem.h"
14 
15 class DemodulatorWorkerThreadResult {
16 public:
17     enum class Type {
18         DEMOD_WORKER_THREAD_RESULT_NULL, DEMOD_WORKER_THREAD_RESULT_FILTERS
19     };
20 
DemodulatorWorkerThreadResult()21     DemodulatorWorkerThreadResult() :
22             cmd(DemodulatorWorkerThreadResult::Type::DEMOD_WORKER_THREAD_RESULT_NULL), iqResampler(nullptr), iqResampleRatio(0), sampleRate(0), bandwidth(0), modemKit(nullptr) {
23 
24     }
25 
DemodulatorWorkerThreadResult(DemodulatorWorkerThreadResult::Type cmd)26     explicit DemodulatorWorkerThreadResult(DemodulatorWorkerThreadResult::Type cmd) :
27             DemodulatorWorkerThreadResult() {
28         this->cmd = cmd;
29     }
30 
31     DemodulatorWorkerThreadResult::Type cmd;
32 
33     msresamp_crcf iqResampler;
34     double iqResampleRatio;
35 
36     long long sampleRate;
37     unsigned int bandwidth;
38     Modem *modem{};
39     ModemKit *modemKit;
40     std::string modemType;
41     std::string modemName;
42 };
43 
44 class DemodulatorWorkerThreadCommand {
45 public:
46     enum class Type {
47         DEMOD_WORKER_THREAD_CMD_NULL, DEMOD_WORKER_THREAD_CMD_BUILD_FILTERS, DEMOD_WORKER_THREAD_CMD_MAKE_DEMOD
48     };
49 
DemodulatorWorkerThreadCommand()50     DemodulatorWorkerThreadCommand() :
51             cmd(DemodulatorWorkerThreadCommand::Type::DEMOD_WORKER_THREAD_CMD_NULL), frequency(0), sampleRate(0), bandwidth(0), audioSampleRate(0) {
52 
53     }
54 
DemodulatorWorkerThreadCommand(DemodulatorWorkerThreadCommand::Type cmd)55     explicit DemodulatorWorkerThreadCommand(DemodulatorWorkerThreadCommand::Type cmd) :
56             cmd(cmd), frequency(0), sampleRate(0), bandwidth(0), audioSampleRate(0) {
57 
58     }
59 
60     DemodulatorWorkerThreadCommand::Type cmd;
61 
62     long long frequency;
63     long long sampleRate;
64     unsigned int bandwidth;
65     unsigned int audioSampleRate;
66     std::string demodType;
67     ModemSettings settings;
68 };
69 
70 typedef ThreadBlockingQueue<DemodulatorWorkerThreadCommand> DemodulatorThreadWorkerCommandQueue;
71 typedef ThreadBlockingQueue<DemodulatorWorkerThreadResult> DemodulatorThreadWorkerResultQueue;
72 
73 typedef std::shared_ptr<DemodulatorThreadWorkerCommandQueue> DemodulatorThreadWorkerCommandQueuePtr;
74 typedef std::shared_ptr<DemodulatorThreadWorkerResultQueue> DemodulatorThreadWorkerResultQueuePtr;
75 
76 class DemodulatorWorkerThread : public IOThread {
77 public:
78 
79     DemodulatorWorkerThread();
80     ~DemodulatorWorkerThread() override;
81 
82     void run() override;
83 
setCommandQueue(DemodulatorThreadWorkerCommandQueuePtr tQueue)84     void setCommandQueue(DemodulatorThreadWorkerCommandQueuePtr tQueue) {
85         commandQueue = tQueue;
86     }
87 
setResultQueue(DemodulatorThreadWorkerResultQueuePtr tQueue)88     void setResultQueue(DemodulatorThreadWorkerResultQueuePtr tQueue) {
89         resultQueue = tQueue;
90     }
91 
92     void terminate() override;
93 
94 protected:
95 
96     DemodulatorThreadWorkerCommandQueuePtr commandQueue;
97     DemodulatorThreadWorkerResultQueuePtr resultQueue;
98     Modem *cModem;
99     ModemKit *cModemKit;
100     std::string cModemType;
101     std::string cModemName;
102 };
103