From 7288244a279135a43be1a71d1b9ebe9e7f57bbb9 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 10 Dec 2017 18:53:47 +0100 Subject: [PATCH] Refactored all decoder classes to use awaitProcess() from Tool_Abstract base class. --- src/Config.h | 2 +- src/Decoder_AAC.cpp | 60 ++++++++----------------------------- src/Decoder_AC3.cpp | 68 +++++++++++------------------------------ src/Decoder_ADPCM.cpp | 60 ++++++++----------------------------- src/Decoder_ALAC.cpp | 70 ++++++++----------------------------------- src/Decoder_Avisynth.cpp | 62 +++++++++----------------------------- src/Decoder_FLAC.cpp | 60 ++++++++----------------------------- src/Decoder_MAC.cpp | 59 +++++++----------------------------- src/Decoder_MP3.cpp | 74 ++++++++++----------------------------------- src/Decoder_Musepack.cpp | 59 +++++++----------------------------- src/Decoder_Opus.cpp | 61 +++++++------------------------------ src/Decoder_Speex.cpp | 52 ++++---------------------------- src/Decoder_TTA.cpp | 60 ++++++++----------------------------- src/Decoder_Vorbis.cpp | 61 +++++++------------------------------ src/Decoder_WMA.cpp | 60 ++++++++----------------------------- src/Decoder_WavPack.cpp | 59 +++++++----------------------------- src/Tool_Abstract.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Tool_Abstract.h | 20 ++++++++++--- 18 files changed, 277 insertions(+), 748 deletions(-) diff --git a/src/Config.h b/src/Config.h index 3571869a..f9a42a2b 100644 --- a/src/Config.h +++ b/src/Config.h @@ -35,7 +35,7 @@ #define VER_LAMEXP_MINOR_LO 6 #define VER_LAMEXP_TYPE Beta #define VER_LAMEXP_PATCH 1 -#define VER_LAMEXP_BUILD 2066 +#define VER_LAMEXP_BUILD 2068 #define VER_LAMEXP_CONFG 2002 /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Decoder_AAC.cpp b/src/Decoder_AAC.cpp index bb55e999..6a653952 100644 --- a/src/Decoder_AAC.cpp +++ b/src/Decoder_AAC.cpp @@ -60,62 +60,28 @@ bool AACDecoder::decode(const QString &sourceFile, const QString &outputFile, QA return false; } - bool bTimeout = false; - bool bAborted = false; - + int prevProgress = -1; QRegExp regExp("\\[(\\d+)%\\]\\s+decoding\\s+"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("FAAD process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok) emit statusUpdated(progress); - } - else if(!text.isEmpty()) - { - emit messageLogged(text); + if (newProgress > prevProgress) + { + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); + } } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool AACDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_AC3.cpp b/src/Decoder_AC3.cpp index 26eee171..51bffa5d 100644 --- a/src/Decoder_AC3.cpp +++ b/src/Decoder_AC3.cpp @@ -53,69 +53,37 @@ bool AC3Decoder::decode(const QString &sourceFile, const QString &outputFile, QA QStringList args; args << QDir::toNativeSeparators(sourceFile); - args << "-i" << "-w" << QDir::toNativeSeparators(outputFile); + args << "-w" << QDir::toNativeSeparators(outputFile); if(!startProcess(process, m_binary, args)) { return false; } - bool bTimeout = false; - bool bAborted = false; + int prevProgress = -1; + QRegExp regExp("\\b\\s*(\\d+)\\.(\\d+)?%(\\s+)Frames", Qt::CaseInsensitive); - QRegExp regExp("\\b(\\s*)(\\d+)\\.(\\d+)%(\\s+)Frames"); - - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("Valdec process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) - { - bool ok = false; - int progress = regExp.cap(2).toInt(&ok); - if(ok) emit statusUpdated(progress); - } - else if(!text.isEmpty()) + qWarning("Found! [\"%s\"]", MUTILS_UTF8(regExp.cap(1))); + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - emit messageLogged(text); + qWarning("newProgress: %d", newProgress); + if (newProgress > prevProgress) + { + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); + } } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } - - return true; + }); + + return (result == RESULT_SUCCESS); } bool AC3Decoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_ADPCM.cpp b/src/Decoder_ADPCM.cpp index a7d42d01..a1487fc1 100644 --- a/src/Decoder_ADPCM.cpp +++ b/src/Decoder_ADPCM.cpp @@ -62,62 +62,28 @@ bool ADPCMDecoder::decode(const QString &sourceFile, const QString &outputFile, return false; } - bool bTimeout = false; - bool bAborted = false; - + int prevProgress = -1; QRegExp regExp("In:(\\d+)(\\.\\d+)*%"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("Sox process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) - { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok) emit statusUpdated(progress); - } - else if(!text.isEmpty()) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - emit messageLogged(text); + if (newProgress > prevProgress) + { + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); + } } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool ADPCMDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_ALAC.cpp b/src/Decoder_ALAC.cpp index 14ecff8a..19523d28 100644 --- a/src/Decoder_ALAC.cpp +++ b/src/Decoder_ALAC.cpp @@ -62,75 +62,29 @@ bool ALACDecoder::decode(const QString &sourceFile, const QString &outputFile, Q return false; } - bool bTimeout = false; - bool bAborted = false; int prevProgress = -1; - - //The ALAC Decoder doesn't actually send any status updates :-[ - //emit statusUpdated(20 + (QUuid::createUuid().data1 % 60)); QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("ALAC process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + qint32 intVal[2]; + if (MUtils::regexp_parse_int32(regExp, intVal, 2)) { - bool ok[2] = {false, false}; - int intVal[2] = {0, 0}; - intVal[0] = regExp.cap(1).toInt(&ok[0]); - intVal[1] = regExp.cap(2).toInt(&ok[1]); - if(ok[0] && ok[1]) + const int newProgress = qRound(static_cast(intVal[0]) + (static_cast(intVal[1]) / 10.0)); + if (newProgress > prevProgress) { - int progress = qRound(static_cast(intVal[0]) + (static_cast(intVal[1]) / 10.0)); - if(progress > prevProgress) - { - emit statusUpdated(progress); - prevProgress = qMin(progress + 2, 99); - } + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); } } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } - - return true; + }); + + return (result == RESULT_SUCCESS); } bool ALACDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_Avisynth.cpp b/src/Decoder_Avisynth.cpp index bac63bac..ee119ccd 100644 --- a/src/Decoder_Avisynth.cpp +++ b/src/Decoder_Avisynth.cpp @@ -61,62 +61,28 @@ bool AvisynthDecoder::decode(const QString &sourceFile, const QString &outputFil return false; } - bool bTimeout = false; - bool bAborted = false; + int prevProgress = -1; + QRegExp regExp("\\d+/\\d+ \\[(\\d+)%\\]"); - QRegExp regExp("(\\d+)/(\\d+) \\[(\\d+)%\\]"); - - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("AVS2WAV process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) - { - bool ok = false; - int progress = regExp.cap(3).toInt(&ok); - if(ok) emit statusUpdated(progress); - } - else if(!text.isEmpty()) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - emit messageLogged(text); + if (newProgress > prevProgress) + { + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); + } } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool AvisynthDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_FLAC.cpp b/src/Decoder_FLAC.cpp index bf6022eb..a426a20f 100644 --- a/src/Decoder_FLAC.cpp +++ b/src/Decoder_FLAC.cpp @@ -61,62 +61,28 @@ bool FLACDecoder::decode(const QString &sourceFile, const QString &outputFile, Q return false; } - bool bTimeout = false; - bool bAborted = false; - + int prevProgress = -1; QRegExp regExp("\\b(\\d+)% complete"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("FLAC process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine().replace('\b', char(0x20)); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) - { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok) emit statusUpdated(progress); - } - else if(!text.isEmpty()) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - emit messageLogged(text); + if (newProgress > prevProgress) + { + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); + } } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool FLACDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_MAC.cpp b/src/Decoder_MAC.cpp index d7ad04b5..02135ecf 100644 --- a/src/Decoder_MAC.cpp +++ b/src/Decoder_MAC.cpp @@ -61,67 +61,28 @@ bool MACDecoder::decode(const QString &sourceFile, const QString &outputFile, QA return false; } - bool bTimeout = false; - bool bAborted = false; int prevProgress = -1; - QRegExp regExp("Progress: (\\d+).(\\d+)%"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("MAC process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok && (progress > prevProgress)) + if (newProgress > prevProgress) { - emit statusUpdated(progress); - prevProgress = qMin(progress + 2, 99); + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); } } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool MACDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_MP3.cpp b/src/Decoder_MP3.cpp index 9c3d9a8a..220a1cde 100644 --- a/src/Decoder_MP3.cpp +++ b/src/Decoder_MP3.cpp @@ -43,7 +43,7 @@ MP3Decoder::MP3Decoder(void) : m_binary(lamexp_tools_lookup("mpg123.exe")) { - if(m_binary.isEmpty()) + if (m_binary.isEmpty()) { MUTILS_THROW("Error initializing MPG123 decoder. Tool 'mpg123.exe' is not registred!"); } @@ -61,80 +61,38 @@ bool MP3Decoder::decode(const QString &sourceFile, const QString &outputFile, QA args << "-v" << "--utf8" << "-w" << QDir::toNativeSeparators(outputFile); args << QDir::toNativeSeparators(sourceFile); - if(!startProcess(process, m_binary, args)) + if (!startProcess(process, m_binary, args)) { return false; } - bool bTimeout = false; - bool bAborted = false; int prevProgress = -1; - - //QRegExp regExp("\\b\\d+\\+\\d+\\s+(\\d+):(\\d+)\\.(\\d+)\\+(\\d+):(\\d+)\\.(\\d+)\\b"); QRegExp regExp("[_=>]\\s+(\\d+)\\+(\\d+)\\s+"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("mpg123 process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + quint32 values[2]; + if (MUtils::regexp_parse_uint32(regExp, values, 2)) { - quint32 values[2]; - if (MUtils::regexp_parse_uint32(regExp, values, 2)) + const quint32 total = values[0] + values[1]; + if ((total >= 512U) && (values[0] >= 256U)) { - const quint32 total = values[0] + values[1]; - if ((total >= 512U) && (values[0] >= 256U)) + const int newProgress = qRound((static_cast(values[0]) / static_cast(total)) * 100.0); + if (newProgress > prevProgress) { - const int newProgress = qRound((static_cast(values[0]) / static_cast(total)) * 100.0); - if (newProgress > prevProgress) - { - emit statusUpdated(newProgress); - prevProgress = qMin(newProgress + 2, 99); - } + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); } } } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS) - { return false; - } - - return true; + }); + + return (result == RESULT_SUCCESS); } bool MP3Decoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_Musepack.cpp b/src/Decoder_Musepack.cpp index 3ea31263..c11ee975 100644 --- a/src/Decoder_Musepack.cpp +++ b/src/Decoder_Musepack.cpp @@ -62,67 +62,28 @@ bool MusepackDecoder::decode(const QString &sourceFile, const QString &outputFil return false; } - bool bTimeout = false; - bool bAborted = false; int prevProgress = -1; - QRegExp regExp("Decoding progress: (\\d+)\\.(\\d+)%"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("MpcDec process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok && (progress > prevProgress)) + if (newProgress > prevProgress) { - emit statusUpdated(progress); - prevProgress = qMin(progress + 2, 99); + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); } } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool MusepackDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_Opus.cpp b/src/Decoder_Opus.cpp index 3fe50345..73a9e9ef 100644 --- a/src/Decoder_Opus.cpp +++ b/src/Decoder_Opus.cpp @@ -68,67 +68,28 @@ bool OpusDecoder::decode(const QString &sourceFile, const QString &outputFile, Q return false; } - bool bTimeout = false; - bool bAborted = false; int prevProgress = -1; - QRegExp regExp("\\((\\d+)%\\)"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("opusdec process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok && (progress > prevProgress)) + if (newProgress > prevProgress) { - emit statusUpdated(progress); - prevProgress = qMin(progress + 2, 99); + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); } } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS) - { return false; - } - - return true; + }); + + return (result == RESULT_SUCCESS); } bool OpusDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_Speex.cpp b/src/Decoder_Speex.cpp index f19248d2..73a44eef 100644 --- a/src/Decoder_Speex.cpp +++ b/src/Decoder_Speex.cpp @@ -61,60 +61,18 @@ bool SpeexDecoder::decode(const QString &sourceFile, const QString &outputFile, return false; } - bool bTimeout = false; - bool bAborted = false; - QRegExp regExp("Working\\.\\.\\. (.)"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) + if (regExp.lastIndexIn(text) >= 0) { - process.kill(); - qWarning("SpeexDec process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) - { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) - { - /* qDebug("Status: %s", regExp.cap(1).toLatin1().constData()); */ - } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool SpeexDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_TTA.cpp b/src/Decoder_TTA.cpp index 55c4f091..8e4d3183 100644 --- a/src/Decoder_TTA.cpp +++ b/src/Decoder_TTA.cpp @@ -62,62 +62,28 @@ bool TTADecoder::decode(const QString &sourceFile, const QString &outputFile, QA return false; } - bool bTimeout = false; - bool bAborted = false; - + int prevProgress = -1; QRegExp regExp("Progress: (\\d+)%"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("TTAEnc process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) - { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok) emit statusUpdated(progress); - } - else if(!text.isEmpty()) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - emit messageLogged(text); + if (newProgress > prevProgress) + { + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); + } } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool TTADecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_Vorbis.cpp b/src/Decoder_Vorbis.cpp index 85a4e2e3..78ea56d6 100644 --- a/src/Decoder_Vorbis.cpp +++ b/src/Decoder_Vorbis.cpp @@ -60,67 +60,28 @@ bool VorbisDecoder::decode(const QString &sourceFile, const QString &outputFile, return false; } - bool bTimeout = false; - bool bAborted = false; int prevProgress = -1; + QRegExp regExp("\\s+(\\d+)% decoded."); - QRegExp regExp(" (\\d+)% decoded."); - - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("OggDec process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress)) { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok && (progress > prevProgress)) + if (newProgress > prevProgress) { - emit statusUpdated(progress); - prevProgress = qMin(progress + 2, 99); + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); } } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool VorbisDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_WMA.cpp b/src/Decoder_WMA.cpp index 1551b33f..87177fe4 100644 --- a/src/Decoder_WMA.cpp +++ b/src/Decoder_WMA.cpp @@ -62,62 +62,28 @@ bool WMADecoder::decode(const QString &sourceFile, const QString &outputFile, QA return false; } - bool bTimeout = false; - bool bAborted = false; - + int prevProgress = -1; QRegExp regExp("\\[(\\d+)\\.(\\d+)%\\]"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("wma2wav process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) - { - bool ok = false; - int progress = regExp.cap(1).toInt(&ok); - if(ok) emit statusUpdated(progress); - } - else if(!text.isEmpty()) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress, 2U)) { - emit messageLogged(text); + if (newProgress > prevProgress) + { + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); + } } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool WMADecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Decoder_WavPack.cpp b/src/Decoder_WavPack.cpp index 25639f71..81173a83 100644 --- a/src/Decoder_WavPack.cpp +++ b/src/Decoder_WavPack.cpp @@ -61,67 +61,28 @@ bool WavPackDecoder::decode(const QString &sourceFile, const QString &outputFile return false; } - bool bTimeout = false; - bool bAborted = false; int prevProgress = -1; - QRegExp regExp("(\\s|\b)(\\d+)%\\s+done"); - while(process.state() != QProcess::NotRunning) + const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, ®Exp](const QString &text) { - if(checkFlag(abortFlag)) - { - process.kill(); - bAborted = true; - emit messageLogged("\nABORTED BY USER !!!"); - break; - } - process.waitForReadyRead(m_processTimeoutInterval); - if(!process.bytesAvailable() && process.state() == QProcess::Running) - { - process.kill(); - qWarning("WvUnpack process timed out <-- killing!"); - emit messageLogged("\nPROCESS TIMEOUT !!!"); - bTimeout = true; - break; - } - while(process.bytesAvailable() > 0) + if (regExp.lastIndexIn(text) >= 0) { - QByteArray line = process.readLine(); - QString text = QString::fromUtf8(line.constData()).simplified(); - if(regExp.lastIndexIn(text) >= 0) + qint32 newProgress; + if (MUtils::regexp_parse_int32(regExp, newProgress, 2U)) { - bool ok = false; - int progress = regExp.cap(2).toInt(&ok); - if(ok && (progress > prevProgress)) + if (newProgress > prevProgress) { - emit statusUpdated(progress); - prevProgress = qMin(progress + 2, 99); + emit statusUpdated(newProgress); + prevProgress = qMin(newProgress + 2, 99); } } - else if(!text.isEmpty()) - { - emit messageLogged(text); - } + return true; } - } - - process.waitForFinished(); - if(process.state() != QProcess::NotRunning) - { - process.kill(); - process.waitForFinished(-1); - } - - emit statusUpdated(100); - emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); - - if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS || QFileInfo(outputFile).size() == 0) - { return false; - } + }); - return true; + return (result == RESULT_SUCCESS); } bool WavPackDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion) diff --git a/src/Tool_Abstract.cpp b/src/Tool_Abstract.cpp index 142e6486..66989fc0 100644 --- a/src/Tool_Abstract.cpp +++ b/src/Tool_Abstract.cpp @@ -157,6 +157,84 @@ bool AbstractTool::startProcess(QProcess &process, const QString &program, const } /* +* Wait for process to terminate while processing its output +*/ +AbstractTool::result_t AbstractTool::awaitProcess(QProcess &process, QAtomicInt &abortFlag, std::function &&handler, int *const exitCode) +{ + bool bTimeout = false; + bool bAborted = false; + + QString lastText; + + while (process.state() != QProcess::NotRunning) + { + if (checkFlag(abortFlag)) + { + process.kill(); + bAborted = true; + emit messageLogged("\nABORTED BY USER !!!"); + break; + } + + process.waitForReadyRead(m_processTimeoutInterval); + if (!process.bytesAvailable() && process.state() == QProcess::Running) + { + process.kill(); + qWarning("Tool process timed out <-- killing!"); + emit messageLogged("\nPROCESS TIMEOUT !!!"); + bTimeout = true; + break; + } + + while (process.bytesAvailable() > 0) + { + QByteArray line = process.readLine(); + if (line.size() > 0) + { + static const char REPALCE_CHARS[3] = { '\r', '\b', '\t' }; + for (size_t i = 0; i < MUTILS_ARR2LEN(REPALCE_CHARS); ++i) + { + line.replace(REPALCE_CHARS[i], char(0x20)); + } + const QString text = QString::fromUtf8(line.constData()).simplified(); + if (!text.isEmpty()) + { + if (!handler(text)) + { + if (text.compare(lastText, Qt::CaseInsensitive) != 0) + { + emit messageLogged(lastText = text); + } + } + } + } + } + } + + process.waitForFinished(); + if (process.state() != QProcess::NotRunning) + { + process.kill(); + process.waitForFinished(-1); + } + + if (exitCode) + { + *exitCode = process.exitCode(); + } + + emit statusUpdated(100); + emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode())); + + if (bAborted || bTimeout || (process.exitCode() != EXIT_SUCCESS)) + { + return bAborted ? RESULT_ABORTED : (bTimeout ? RESULT_TIMEOUT : RESULT_FAILURE); + } + + return RESULT_SUCCESS; +} + +/* * Convert program arguments to single string */ QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments) diff --git a/src/Tool_Abstract.h b/src/Tool_Abstract.h index 1b8650f3..0d93af39 100644 --- a/src/Tool_Abstract.h +++ b/src/Tool_Abstract.h @@ -24,6 +24,7 @@ #include #include +#include class QMutex; class QProcess; @@ -42,21 +43,32 @@ public: AbstractTool(void); ~AbstractTool(void); - bool startProcess(QProcess &process, const QString &program, const QStringList &args, const QString &workingDir = QString()); - static QString commandline2string(const QString &program, const QStringList &arguments); - signals: void statusUpdated(int progress); void messageLogged(const QString &line); protected: static const int m_processTimeoutInterval = 600000; - + + typedef enum + { + RESULT_ABORTED = -2, + RESULT_TIMEOUT = -1, + RESULT_FAILURE = 0, + RESULT_SUCCESS = 1 + } + result_t; + static __forceinline bool checkFlag(QAtomicInt &flag) { return MUTILS_BOOLIFY(flag); } + static QString commandline2string(const QString &program, const QStringList &arguments); + + bool startProcess(QProcess &process, const QString &program, const QStringList &args, const QString &workingDir = QString()); + result_t awaitProcess(QProcess &process, QAtomicInt &abortFlag, std::function &&handler, int *const exitCode = NULL); + private: static QScopedPointer s_jobObjectInstance; static QScopedPointer s_startProcessTimer; -- 2.11.4.GIT