js8call/Decoder.cpp

164 lines
3.8 KiB
C++
Raw Normal View History

/**
* This file is part of JS8Call.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* (C) 2019 Jordan Sherer <kn4crd@gmail.com> - All Rights Reserved
*
**/
#include "Decoder.h"
#include "commons.h"
#include <QTimer>
Decoder::Decoder(QObject *parent):
QObject(parent)
{
}
Decoder::~Decoder(){
}
//
void Decoder::lock(){
// NOOP
}
//
void Decoder::unlock(){
// NOOP
}
//
Worker* Decoder::createWorker(){
auto worker = new Worker();
worker->moveToThread(&m_thread);
connect(&m_thread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Decoder::startWorker, worker, &Worker::start);
connect(this, &Decoder::quitWorker, worker, &Worker::quit);
connect(worker, &Worker::ready, this, &Decoder::ready);
return worker;
}
//
void Decoder::start(QThread::Priority priority){
m_thread.start(priority);
}
//
void Decoder::quit(){
m_thread.quit();
}
//
bool Decoder::wait(){
return m_thread.wait();
}
//
void Decoder::processStart(QString path, QStringList args){
if(m_worker.isNull()){
m_worker = createWorker();
}
emit startWorker(path, args);
}
//
void Decoder::processReady(QByteArray t){
emit ready(t);
}
//
void Decoder::processQuit(){
emit quitWorker();
}
////////////////////////////////////////
//////////////// WORKER ////////////////
////////////////////////////////////////
//
Worker::~Worker(){
}
//
void Worker::setProcess(QProcess *proc, int msecs){
if(!m_proc.isNull()){
bool b = m_proc->waitForFinished(msecs);
if(!b) m_proc->close();
m_proc.reset();
}
if(proc){
m_proc.reset(proc);
}
}
//
void Worker::start(QString path, QStringList args){
if(JS8_DEBUG_DECODE) qDebug() << "decoder process starting...";
auto proc = new QProcess(this);
connect(proc, &QProcess::readyReadStandardOutput,
[this, proc](){
while(proc->canReadLine()){
emit ready(proc->readLine());
}
});
connect(proc, static_cast<void (QProcess::*) (QProcess::ProcessError)> (&QProcess::error),
[this, proc] (QProcess::ProcessError e) {
emit error();
});
connect(proc, static_cast<void (QProcess::*) (int, QProcess::ExitStatus)> (&QProcess::finished),
[this, proc] (int exitCode, QProcess::ExitStatus status) {
emit finished();
});
auto watcher = new QTimer(this);
connect(proc, static_cast<void (QProcess::*) (int, QProcess::ExitStatus)> (&QProcess::finished),
[this, watcher] (int /*exitCode*/, QProcess::ExitStatus /*status*/) {
watcher->stop();
});
connect(watcher, &QTimer::timeout,
[this, proc](){
if(JS8_DEBUG_DECODE) qDebug() << "decode process" << proc->state() << "can readline?" << proc->canReadLine();
});
watcher->setInterval(500);
watcher->start();
QProcessEnvironment env {QProcessEnvironment::systemEnvironment ()};
env.insert ("OMP_STACKSIZE", "4M");
proc->setProcessEnvironment (env);
proc->start(path, args, QIODevice::ReadWrite | QIODevice::Unbuffered);
setProcess(proc);
}
//
void Worker::quit(){
setProcess(nullptr);
}