2019-11-20 00:11:30 -05:00
|
|
|
/**
|
|
|
|
* 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);
|
2019-11-22 14:20:05 -05:00
|
|
|
connect(worker, &Worker::ready, this, &Decoder::processReady);
|
|
|
|
connect(worker, &Worker::error, this, &Decoder::processError);
|
|
|
|
connect(worker, &Worker::finished, this, &Decoder::processFinished);
|
2019-11-20 00:11:30 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-11-22 14:20:05 -05:00
|
|
|
//
|
2019-11-22 15:00:06 -05:00
|
|
|
void Decoder::processError(int errorCode, QString errorString){
|
|
|
|
if(JS8_DEBUG_DECODE) qDebug() << "decoder process error" << errorCode << errorString;
|
|
|
|
emit error(errorCode, errorString);
|
2019-11-22 14:20:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-11-22 15:00:06 -05:00
|
|
|
void Decoder::processFinished(int exitCode, int statusCode, QString errorString){
|
|
|
|
if(JS8_DEBUG_DECODE) qDebug() << "decoder process finished" << exitCode << statusCode << errorString;
|
|
|
|
emit finished(exitCode, statusCode, errorString);
|
2019-11-22 14:20:05 -05:00
|
|
|
}
|
|
|
|
|
2019-11-20 00:11:30 -05:00
|
|
|
////////////////////////////////////////
|
|
|
|
//////////////// 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),
|
2019-11-22 14:20:05 -05:00
|
|
|
[this, proc] (QProcess::ProcessError errorCode) {
|
2019-11-22 15:00:06 -05:00
|
|
|
emit error(int(errorCode), proc->errorString());
|
2019-11-20 00:11:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
connect(proc, static_cast<void (QProcess::*) (int, QProcess::ExitStatus)> (&QProcess::finished),
|
|
|
|
[this, proc] (int exitCode, QProcess::ExitStatus status) {
|
2019-11-22 15:00:06 -05:00
|
|
|
emit finished(exitCode, int(status), QString{proc->readAllStandardError()});
|
2019-11-20 00:11:30 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|