OSDN Git Service

Implemented multi-threading for file analyzer. Now multiple files can be analyzed...
[lamexp/LameXP.git] / src / Thread_Initialization.cpp
index f2a1e37..f037ce6 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 #include <QLibrary>
 #include <QResource>
 #include <QTime>
+#include <QTextStream>
+#include <QRunnable>
+#include <QThreadPool>
+#include <QMutex>
 
 /* helper macros */
 #define PRINT_CPU_TYPE(X) case X: qDebug("Selected CPU is: " #X)
-static const double g_allowedExtractDelay = 10.0;
+static const double g_allowedExtractDelay = 12.0;
+
+////////////////////////////////////////////////////////////
+// ExtractorTask class
+////////////////////////////////////////////////////////////
+
+class ExtractorTask : public QRunnable
+{
+public:
+       ExtractorTask(const QDir &appDir, const QString &toolName, const QString &toolShortName, const QByteArray &toolHash, const unsigned int toolVersion)
+       :
+               QRunnable(), m_appDir(appDir), m_toolName(toolName), m_toolShortName(toolShortName), m_toolHash(toolHash), m_toolVersion(toolVersion)
+       {
+               /* Nothing to do */
+       }
+
+       static void clearFlags(void)
+       {
+               s_bAbort = s_bCustom = false;
+               s_errMsg[0] = '\0';
+       }
+
+       static bool getAbort(void) { return s_bAbort; }
+       static bool getCustom(void) { return s_bCustom; }
+       static char *const getError(void) { return s_errMsg; }
+
+protected:
+       void run(void)
+       {
+               try
+               {
+                       LockedFile *lockedFile = NULL;
+                       unsigned int version = m_toolVersion;
+                       
+                       if(!s_bAbort)
+                       {
+                               QFileInfo customTool(QString("%1/tools/%2/%3").arg(m_appDir.canonicalPath(), QString::number(lamexp_version_build()), m_toolShortName));
+                               if(customTool.exists() && customTool.isFile())
+                               {
+                                       qDebug("Setting up file: %s <- %s", m_toolShortName.toLatin1().constData(), m_appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
+                                       lockedFile = new LockedFile(customTool.canonicalFilePath()); version = UINT_MAX; s_bCustom = true;
+                               }
+                               else
+                               {
+                                       qDebug("Extracting file: %s -> %s", m_toolName.toLatin1().constData(), m_toolShortName.toLatin1().constData());
+                                       lockedFile = new LockedFile(QString(":/tools/%1").arg(m_toolName), QString("%1/lamexp_%2").arg(lamexp_temp_folder2(), m_toolShortName), m_toolHash);
+                               }
+
+                               if(lockedFile)
+                               {
+                                       QMutexLocker lock(&s_mutex);
+                                       lamexp_register_tool(m_toolShortName, lockedFile, version);
+                               }
+                       }
+               }
+               catch(char *errorMsg)
+               {
+                       qWarning("At least one of the required tools could not be initialized:\n%s", errorMsg);
+                       if(s_mutex.tryLock())
+                       {
+                               if(!s_bAbort) { s_bAbort = true; strncpy_s(s_errMsg, 1024, errorMsg, _TRUNCATE); }
+                               s_mutex.unlock();
+                       }
+               }
+       }
+
+private:
+       const QDir m_appDir;
+       const QString m_toolName;
+       const QString m_toolShortName;
+       const QByteArray m_toolHash;
+       const unsigned int m_toolVersion;
+
+       static volatile bool s_bAbort;
+       static volatile bool s_bCustom;
+       static QMutex s_mutex;
+       static char s_errMsg[1024];
+};
+
+volatile bool ExtractorTask::s_bAbort = false;
+volatile bool ExtractorTask::s_bCustom = false;
+char ExtractorTask::s_errMsg[1024] = {'\0'};
+QMutex ExtractorTask::s_mutex;
 
 ////////////////////////////////////////////////////////////
 // Constructor
@@ -60,7 +146,6 @@ InitializationThread::InitializationThread(const lamexp_cpu_t *cpuFeatures)
 void InitializationThread::run()
 {
        m_bSuccess = false;
-       bool bCustom = false;
        delay();
 
        //CPU type selection
@@ -74,13 +159,14 @@ void InitializationThread::run()
                cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_GEN : CPU_TYPE_X86_GEN;
        }
 
-       //Hack to disable x64 on the Windows 8 Developer Preview
+       //Hack to disable x64 on Wine, as x64 binaries won't run under Wine (tested with Wine 1.4 under Ubuntu 12.04 x64)
        if(cpuSupport & CPU_TYPE_X64_ALL)
        {
-               DWORD osVerNo = lamexp_get_os_version();
-               if((HIWORD(osVerNo) == 6) && (LOWORD(osVerNo) == 2))
+               //DWORD osVerNo = lamexp_get_os_version();
+               //if((HIWORD(osVerNo) == 6) && (LOWORD(osVerNo) == 2))
+               if(lamexp_detect_wine())
                {
-                       qWarning("Windows 8 (x64) developer preview detected. Going to disable all x64 support!\n");
+                       qWarning("Running under Wine on a 64-Bit system. Going to disable all x64 support!\n");
                        cpuSupport = (cpuSupport == CPU_TYPE_X64_SSE) ? CPU_TYPE_X86_SSE : CPU_TYPE_X86_GEN;
                }
        }
@@ -124,6 +210,15 @@ void InitializationThread::run()
        QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
        QDir appDir = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
 
+       QThreadPool *pool = new QThreadPool();
+       int idealThreadCount = QThread::idealThreadCount();
+       if(idealThreadCount > 0)
+       {
+               pool->setMaxThreadCount(idealThreadCount * 2);
+       }
+       
+       ExtractorTask::clearFlags();
+
        QTime timer;
        timer.start();
        
@@ -147,20 +242,8 @@ void InitializationThread::run()
                        
                        if(toolCpuType & cpuSupport)
                        {
-                               QFileInfo customTool(QString("%1/tools/%2/%3").arg(appDir.canonicalPath(), QString::number(lamexp_version_build()), toolShortName));
-                               if(customTool.exists() && customTool.isFile())
-                               {
-                                       bCustom = true;
-                                       qDebug("Setting up file: %s <- %s", toolShortName.toLatin1().constData(), appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
-                                       LockedFile *lockedFile = new LockedFile(customTool.canonicalFilePath());
-                                       lamexp_register_tool(toolShortName, lockedFile, UINT_MAX);
-                               }
-                               else
-                               {
-                                       qDebug("Extracting file: %s -> %s", toolName.toLatin1().constData(),  toolShortName.toLatin1().constData());
-                                       LockedFile *lockedFile = new LockedFile(QString(":/tools/%1").arg(toolName), QString("%1/tool_%2").arg(lamexp_temp_folder2(), toolShortName), toolHash);
-                                       lamexp_register_tool(toolShortName, lockedFile, toolVersion);
-                               }
+                               pool->start(new ExtractorTask(appDir, toolName, toolShortName, toolHash, toolVersion));
+                               QThread::yieldCurrentThread();
                        }
                }
                catch(char *errorMsg)
@@ -169,14 +252,25 @@ void InitializationThread::run()
                        return;
                }
        }
-       
+
+       //Wait for extrator threads to finish
+       pool->waitForDone();
+       LAMEXP_DELETE(pool);
+
+       //Make sure all files were extracted correctly
+       if(ExtractorTask::getAbort())
+       {
+               qFatal("At least one of the required tools could not be initialized:\n%s", ExtractorTask::getError());
+               return;
+       }
+
        //Make sure all files were extracted
        if(!mapChecksum.isEmpty())
        {
                qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(mapChecksum.keys().first()).toLatin1().constData());
                return;
        }
-       
+
        qDebug("All extracted.\n");
 
        //Clean-up
@@ -185,7 +279,7 @@ void InitializationThread::run()
        mapCpuType.clear();
        
        //Using any custom tools?
-       if(bCustom)
+       if(ExtractorTask::getCustom())
        {
                qWarning("Warning: Using custom tools, you might encounter unexpected problems!\n");
        }
@@ -245,33 +339,43 @@ void InitializationThread::initTranslations(void)
        while(!qmFiles.isEmpty())
        {
                QString langId, langName;
-               unsigned int systemId = 0;
+               unsigned int systemId = 0, country = 0;
                QString qmFile = qmFiles.takeFirst();
                
                QRegExp langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive);
                if(langIdExp.indexIn(qmFile) >= 0)
                {
                        langId = langIdExp.cap(1).toLower();
+                       QResource langRes = QResource(QString(":/localization/%1.txt").arg(qmFile));
+                       if(langRes.isValid() && langRes.size() > 0)
+                       {
+                               QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char*>(langRes.data()), langRes.size());
+                               QTextStream stream(&data, QIODevice::ReadOnly);
+                               stream.setAutoDetectUnicode(false); stream.setCodec("UTF-8");
+                               while(!stream.atEnd())
+                               {
+                                       QStringList langInfo = stream.readLine().simplified().split(",", QString::SkipEmptyParts);
+                                       if(langInfo.count() == 3)
+                                       {
+                                               systemId = langInfo.at(0).trimmed().toUInt();
+                                               country  = langInfo.at(1).trimmed().toUInt();
+                                               langName = langInfo.at(2).trimmed();
+                                               break;
+                                       }
+                               }
+                       }
                }
 
-               QResource langRes = (QString(":/localization/%1.txt").arg(qmFile));
-               if(langRes.isValid() && langRes.size() > 0)
+               if(!(langId.isEmpty() || langName.isEmpty() || systemId == 0))
                {
-                       QStringList langInfo = QString::fromUtf8(reinterpret_cast<const char*>(langRes.data()), langRes.size()).simplified().split(",", QString::SkipEmptyParts);
-                       if(langInfo.count() == 2)
+                       if(lamexp_translation_register(langId, qmFile, langName, systemId, country))
                        {
-                               systemId = langInfo.at(0).toUInt();
-                               langName = langInfo.at(1);
+                               qDebug("Registering translation: %s = %s (%u) [%u]", qmFile.toUtf8().constData(), langName.toUtf8().constData(), systemId, country);
+                       }
+                       else
+                       {
+                               qWarning("Failed to register: %s", qmFile.toLatin1().constData());
                        }
-               }
-               
-               if(lamexp_translation_register(langId, qmFile, langName, systemId))
-               {
-                       qDebug("Registering translation: %s = %s (%u)", qmFile.toUtf8().constData(), langName.toUtf8().constData(), systemId);
-               }
-               else
-               {
-                       qWarning("Failed to register: %s", qmFile.toLatin1().constData());
                }
        }
 
@@ -484,17 +588,15 @@ void InitializationThread::initFhgAac(void)
 void InitializationThread::initQAac(void)
 {
        const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
-       
-       QFileInfo qaacFileInfo[4];
+
+       QFileInfo qaacFileInfo[2];
        qaacFileInfo[0] = QFileInfo(QString("%1/qaac.exe").arg(appPath));
        qaacFileInfo[1] = QFileInfo(QString("%1/libsoxrate.dll").arg(appPath));
-       qaacFileInfo[2] = QFileInfo(QString("%1/msvcp100.dll").arg(appPath));
-       qaacFileInfo[3] = QFileInfo(QString("%1/msvcr100.dll").arg(appPath));
        
        bool qaacFilesFound = true;
-       for(int i = 0; i < 4; i++)      { if(!qaacFileInfo[i].exists()) qaacFilesFound = false; }
+       for(int i = 0; i < 2; i++)      { if(!qaacFileInfo[i].exists()) qaacFilesFound = false; }
 
-       //Lock the FhgAacEnc binaries
+       //Lock the QAAC binaries
        if(!qaacFilesFound)
        {
                qDebug("QAAC binaries not found -> QAAC support will be disabled!\n");
@@ -503,27 +605,27 @@ void InitializationThread::initQAac(void)
 
        qDebug("Found QAAC encoder:\n%s\n", qaacFileInfo[0].canonicalFilePath().toUtf8().constData());
 
-       LockedFile *qaacBin[4];
-       for(int i = 0; i < 4; i++) qaacBin[i] = NULL;
+       LockedFile *qaacBin[2];
+       for(int i = 0; i < 2; i++) qaacBin[i] = NULL;
 
        try
        {
-               for(int i = 0; i < 4; i++)
+               for(int i = 0; i < 2; i++)
                {
                        qaacBin[i] = new LockedFile(qaacFileInfo[i].canonicalFilePath());
                }
        }
        catch(...)
        {
-               for(int i = 0; i < 4; i++) LAMEXP_DELETE(qaacBin[i]);
-               qWarning("Failed to get excluive lock to FhgAacEnc binary -> FhgAacEnc support will be disabled!");
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               qWarning("Failed to get excluive lock to QAAC binary -> QAAC support will be disabled!");
                return;
        }
 
        QProcess process;
        process.setProcessChannelMode(QProcess::MergedChannels);
        process.setReadChannel(QProcess::StandardOutput);
-       process.start(qaacFileInfo[0].canonicalFilePath(), QStringList());
+       process.start(qaacFileInfo[0].canonicalFilePath(), QStringList() << "--check");
 
        if(!process.waitForStarted())
        {
@@ -531,12 +633,14 @@ void InitializationThread::initQAac(void)
                qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
                process.kill();
                process.waitForFinished(-1);
-               for(int i = 0; i < 4; i++) LAMEXP_DELETE(qaacBin[i]);
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
                return;
        }
 
        QRegExp qaacEncSig("qaac (\\d)\\.(\\d)(\\d)", Qt::CaseInsensitive);
+       QRegExp coreEncSig("CoreAudioToolbox (\\d)\\.(\\d)\\.(\\d)\\.(\\d)", Qt::CaseInsensitive);
        unsigned int qaacVersion = 0;
+       unsigned int coreVersion = 0;
 
        while(process.state() != QProcess::NotRunning)
        {
@@ -546,7 +650,7 @@ void InitializationThread::initQAac(void)
                        qWarning("QAAC process time out -> killing!");
                        process.kill();
                        process.waitForFinished(-1);
-                       for(int i = 0; i < 4; i++) LAMEXP_DELETE(qaacBin[i]);
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
                        return;
                }
                while(process.bytesAvailable() > 0)
@@ -564,26 +668,52 @@ void InitializationThread::initQAac(void)
                                        qaacVersion = (qBound(0U, tmp[0], 9U) * 100) + (qBound(0U, tmp[1], 9U) * 10) + qBound(0U, tmp[2], 9U);
                                }
                        }
+                       if(coreEncSig.lastIndexIn(line) >= 0)
+                       {
+                               unsigned int tmp[4] = {0, 0, 0, 0};
+                               bool ok[4] = {false, false, false, false};
+                               tmp[0] = coreEncSig.cap(1).toUInt(&ok[0]);
+                               tmp[1] = coreEncSig.cap(2).toUInt(&ok[1]);
+                               tmp[2] = coreEncSig.cap(3).toUInt(&ok[2]);
+                               tmp[3] = coreEncSig.cap(4).toUInt(&ok[3]);
+                               if(ok[0] && ok[1] && ok[2] && ok[3])
+                               {
+                                       coreVersion = (qBound(0U, tmp[0], 9U) * 1000) + (qBound(0U, tmp[1], 9U) * 100) + (qBound(0U, tmp[2], 9U) * 10) + qBound(0U, tmp[3], 9U);
+                               }
+                       }
                }
        }
 
+       //qDebug("qaac %d, CoreAudioToolbox %d", qaacVersion, coreVersion);
+
        if(!(qaacVersion > 0))
        {
                qWarning("QAAC version couldn't be determined -> QAAC support will be disabled!");
-               for(int i = 0; i < 4; i++) LAMEXP_DELETE(qaacBin[i]);
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
                return;
        }
        else if(qaacVersion < lamexp_toolver_qaacenc())
        {
-               qWarning("QAAC version is too much outdated -> QAAC support will be disabled!");
-               for(int i = 0; i < 4; i++) LAMEXP_DELETE(qaacBin[i]);
+               qWarning("QAAC version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.??", qaacVersion, "N/A").toLatin1().constData());
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
                return;
        }
 
-       for(int i = 0; i < 4; i++)
+       if(!(coreVersion > 0))
        {
-               lamexp_register_tool(qaacFileInfo[i].fileName(), qaacBin[i], qaacVersion);
+               qWarning("CoreAudioToolbox version couldn't be determined -> QAAC support will be disabled!");
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               return;
        }
+       else if(coreVersion < lamexp_toolver_coreaudio())
+       {
+               qWarning("CoreAudioToolbox version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.?.?.?", coreVersion, "N/A").toLatin1().constData());
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               return;
+       }
+
+       lamexp_register_tool(qaacFileInfo[0].fileName(), qaacBin[0], qaacVersion);
+       lamexp_register_tool(qaacFileInfo[1].fileName(), qaacBin[1], qaacVersion);
 }
 
 void InitializationThread::selfTest(void)
@@ -613,7 +743,7 @@ void InitializationThread::selfTest(void)
                                qDebug("%02i -> %s", ++n, g_lamexp_tools[i].pcName);
                        }
                }
-               if(n != 24)
+               if(n != 25)
                {
                        qFatal("Tool count mismatch !!!");
                }