2014-12-11 23:35:06 -05:00
|
|
|
#include "DemodulatorInstance.h"
|
|
|
|
|
|
|
|
DemodulatorInstance::DemodulatorInstance() :
|
2014-12-16 21:30:03 -05:00
|
|
|
t_Demod(NULL), t_PreDemod(NULL), t_Audio(NULL), threadQueueDemod(NULL), demodulatorThread(NULL), terminated(false), audioTerminated(false), demodTerminated(
|
|
|
|
false), preDemodTerminated(false) {
|
2014-12-16 18:27:02 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
label = new std::string("Unnamed");
|
|
|
|
threadQueueDemod = new DemodulatorThreadInputQueue;
|
|
|
|
threadQueuePostDemod = new DemodulatorThreadPostInputQueue;
|
|
|
|
threadQueueCommand = new DemodulatorThreadCommandQueue;
|
|
|
|
threadQueueNotify = new DemodulatorThreadCommandQueue;
|
2014-12-16 18:27:02 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
demodulatorPreThread = new DemodulatorPreThread(threadQueueDemod, threadQueuePostDemod, threadQueueNotify);
|
|
|
|
demodulatorPreThread->setCommandQueue(threadQueueCommand);
|
|
|
|
demodulatorThread = new DemodulatorThread(threadQueuePostDemod, threadQueueNotify);
|
2014-12-16 18:27:02 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
audioInputQueue = new AudioThreadInputQueue;
|
|
|
|
audioThread = new AudioThread(audioInputQueue, threadQueueNotify);
|
2014-12-16 18:27:02 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
demodulatorThread->setAudioInputQueue(audioInputQueue);
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorInstance::~DemodulatorInstance() {
|
2014-12-16 21:30:03 -05:00
|
|
|
delete audioThread;
|
|
|
|
delete demodulatorThread;
|
2014-12-11 23:35:06 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
delete audioInputQueue;
|
|
|
|
delete threadQueueDemod;
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DemodulatorInstance::setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) {
|
2014-12-16 21:30:03 -05:00
|
|
|
demodulatorThread->setVisualOutputQueue(tQueue);
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DemodulatorInstance::run() {
|
2014-12-16 21:30:03 -05:00
|
|
|
t_Audio = new std::thread(&AudioThread::threadMain, audioThread);
|
2014-12-11 23:35:06 -05:00
|
|
|
|
|
|
|
#ifdef __APPLE__ // Already using pthreads, might as well do some custom init..
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_attr_t attr;
|
|
|
|
size_t size;
|
2014-12-11 23:35:06 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setstacksize(&attr, 2048000);
|
|
|
|
pthread_attr_getstacksize(&attr, &size);
|
2014-12-16 20:33:44 -05:00
|
|
|
pthread_attr_setschedpolicy(&attr, SCHED_RR);
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_create(&t_PreDemod, &attr, &DemodulatorPreThread::pthread_helper, demodulatorPreThread);
|
|
|
|
pthread_attr_destroy(&attr);
|
2014-12-11 23:35:06 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setstacksize(&attr, 2048000);
|
|
|
|
pthread_attr_getstacksize(&attr, &size);
|
2014-12-16 20:33:44 -05:00
|
|
|
pthread_attr_setschedpolicy(&attr, SCHED_RR);
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_create(&t_Demod, &attr, &DemodulatorThread::pthread_helper, demodulatorThread);
|
|
|
|
pthread_attr_destroy(&attr);
|
2014-12-16 18:27:02 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
std::cout << "Initialized demodulator stack size of " << size << std::endl;
|
2014-12-11 23:35:06 -05:00
|
|
|
|
|
|
|
#else
|
2014-12-16 21:30:03 -05:00
|
|
|
t_PreDemod = new std::thread(&DemodulatorPreThread::threadMain, demodulatorPreThread);
|
|
|
|
t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread);
|
2014-12-11 23:35:06 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void DemodulatorInstance::updateLabel(int freq) {
|
2014-12-16 21:30:03 -05:00
|
|
|
std::stringstream newLabel;
|
|
|
|
newLabel.precision(3);
|
|
|
|
newLabel << std::fixed << ((float) freq / 1000000.0);
|
|
|
|
setLabel(newLabel.str());
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorThreadCommandQueue *DemodulatorInstance::getCommandQueue() {
|
2014-12-16 21:30:03 -05:00
|
|
|
return threadQueueCommand;
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorThreadParameters &DemodulatorInstance::getParams() {
|
2014-12-16 21:30:03 -05:00
|
|
|
return demodulatorPreThread->getParams();
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DemodulatorInstance::terminate() {
|
2014-12-16 21:30:03 -05:00
|
|
|
std::cout << "Terminating demodulator preprocessor thread.." << std::endl;
|
|
|
|
demodulatorPreThread->terminate();
|
|
|
|
std::cout << "Terminating demodulator thread.." << std::endl;
|
|
|
|
demodulatorThread->terminate();
|
|
|
|
std::cout << "Terminating demodulator audio thread.." << std::endl;
|
|
|
|
audioThread->terminate();
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DemodulatorInstance::getLabel() {
|
2014-12-16 21:30:03 -05:00
|
|
|
return *(label.load());
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DemodulatorInstance::setLabel(std::string labelStr) {
|
2014-12-16 21:30:03 -05:00
|
|
|
std::string *newLabel = new std::string;
|
|
|
|
newLabel->append(labelStr);
|
|
|
|
std::string *oldLabel;
|
|
|
|
oldLabel = label;
|
|
|
|
label = newLabel;
|
|
|
|
delete oldLabel;
|
2014-12-16 18:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DemodulatorInstance::isTerminated() {
|
2014-12-16 21:30:03 -05:00
|
|
|
while (!threadQueueNotify->empty()) {
|
|
|
|
DemodulatorThreadCommand cmd;
|
|
|
|
threadQueueNotify->pop(cmd);
|
|
|
|
|
|
|
|
switch (cmd.cmd) {
|
|
|
|
case DemodulatorThreadCommand::DEMOD_THREAD_CMD_AUDIO_TERMINATED:
|
|
|
|
audioThread = NULL;
|
|
|
|
t_Audio->join();
|
|
|
|
audioTerminated = true;
|
|
|
|
break;
|
|
|
|
case DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_TERMINATED:
|
|
|
|
demodulatorThread = NULL;
|
2014-12-16 18:27:02 -05:00
|
|
|
#ifdef __APPLE__
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_join(t_Demod, NULL);
|
2014-12-16 18:27:02 -05:00
|
|
|
#else
|
2014-12-16 21:30:03 -05:00
|
|
|
t_Demod->join();
|
2014-12-16 18:27:02 -05:00
|
|
|
#endif
|
2014-12-16 21:30:03 -05:00
|
|
|
demodTerminated = true;
|
|
|
|
break;
|
|
|
|
case DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_PREPROCESS_TERMINATED:
|
|
|
|
demodulatorPreThread = NULL;
|
2014-12-16 18:27:02 -05:00
|
|
|
#ifdef __APPLE__
|
2014-12-16 21:30:03 -05:00
|
|
|
pthread_join(t_PreDemod, NULL);
|
2014-12-16 18:27:02 -05:00
|
|
|
#else
|
2014-12-16 21:30:03 -05:00
|
|
|
t_PreDemod->join();
|
2014-12-16 18:27:02 -05:00
|
|
|
#endif
|
2014-12-16 21:30:03 -05:00
|
|
|
preDemodTerminated = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 18:27:02 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
terminated = audioTerminated && demodTerminated && preDemodTerminated;
|
2014-12-16 18:27:02 -05:00
|
|
|
|
2014-12-16 21:30:03 -05:00
|
|
|
return terminated;
|
2014-12-11 23:35:06 -05:00
|
|
|
}
|
2014-12-16 18:27:02 -05:00
|
|
|
|