OSDN Git Service

Refactored all decoder classes to use awaitProcess() from Tool_Abstract base class.
authorLoRd_MuldeR <mulder2@gmx.de>
Sun, 10 Dec 2017 17:53:47 +0000 (18:53 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Sun, 10 Dec 2017 17:53:47 +0000 (18:53 +0100)
18 files changed:
src/Config.h
src/Decoder_AAC.cpp
src/Decoder_AC3.cpp
src/Decoder_ADPCM.cpp
src/Decoder_ALAC.cpp
src/Decoder_Avisynth.cpp
src/Decoder_FLAC.cpp
src/Decoder_MAC.cpp
src/Decoder_MP3.cpp
src/Decoder_Musepack.cpp
src/Decoder_Opus.cpp
src/Decoder_Speex.cpp
src/Decoder_TTA.cpp
src/Decoder_Vorbis.cpp
src/Decoder_WMA.cpp
src/Decoder_WavPack.cpp
src/Tool_Abstract.cpp
src/Tool_Abstract.h

index 3571869..f9a42a2 100644 (file)
@@ -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
 
 ///////////////////////////////////////////////////////////////////////////////
index bb55e99..6a65395 100644 (file)
@@ -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, &regExp](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)
index 26eee17..51bffa5 100644 (file)
@@ -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, &regExp](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)
index a7d42d0..a1487fc 100644 (file)
@@ -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, &regExp](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)
index 14ecff8..19523d2 100644 (file)
@@ -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, &regExp](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<double>(intVal[0]) + (static_cast<double>(intVal[1]) / 10.0));
+                               if (newProgress > prevProgress)
                                {
-                                       int progress = qRound(static_cast<double>(intVal[0]) + (static_cast<double>(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)
index bac63ba..ee119cc 100644 (file)
@@ -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, &regExp](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)
index bf6022e..a426a20 100644 (file)
@@ -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, &regExp](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)
index d7ad04b..02135ec 100644 (file)
@@ -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, &regExp](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)
index 9c3d9a8..220a1cd 100644 (file)
@@ -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, &regExp](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<double>(values[0]) / static_cast<double>(total)) * 100.0);
+                                       if (newProgress > prevProgress)
                                        {
-                                               const int newProgress = qRound((static_cast<double>(values[0]) / static_cast<double>(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)
index 3ea3126..c11ee97 100644 (file)
@@ -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, &regExp](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)
index 3fe5034..73a9e9e 100644 (file)
@@ -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, &regExp](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)
index f19248d..73a44ee 100644 (file)
@@ -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, &regExp](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)
index 55c4f09..8e4d318 100644 (file)
@@ -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, &regExp](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)
index 85a4e2e..78ea56d 100644 (file)
@@ -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, &regExp](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)
index 1551b33..87177fe 100644 (file)
@@ -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, &regExp](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)
index 25639f7..81173a8 100644 (file)
@@ -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, &regExp](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)
index 142e648..66989fc 100644 (file)
@@ -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<bool(const QString &text)> &&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)
index 1b8650f..0d93af3 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <MUtils\Global.h>
 #include <QObject>
+#include <functional>
 
 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<bool(const QString &text)> &&handler, int *const exitCode = NULL);
+
 private:
        static QScopedPointer<MUtils::JobObject> s_jobObjectInstance;
        static QScopedPointer<QElapsedTimer>     s_startProcessTimer;