OSDN Git Service

Added preliminary NVEncC encoding support.
[x264-launcher/x264-launcher.git] / src / thread_binaries.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "thread_binaries.h"
23
24 #include <QLibrary>
25 #include <QEventLoop>
26 #include <QTimer>
27 #include <QMutexLocker>
28 #include <QApplication>
29 #include <QProcess>
30 #include <QDir>
31
32 //Internal
33 #include "global.h"
34 #include "model_sysinfo.h"
35 #include "win_updater.h"
36 #include "encoder_factory.h"
37 #include "source_factory.h"
38
39 //MUtils
40 #include <MUtils/Global.h>
41 #include <MUtils/OSSupport.h>
42
43 //Static
44 QMutex BinariesCheckThread::m_binLock;
45 QScopedPointer<QFile> BinariesCheckThread::m_binPath[MAX_BINARIES];
46
47 //Whatever
48 #define NEXT(X) ((*reinterpret_cast<int*>(&(X)))++)
49 #define SHFL(X) ((*reinterpret_cast<int*>(&(X))) <<= 1)
50
51 //External
52 QString AVS_CHECK_BINARY(const SysinfoModel *sysinfo, const bool& x64);
53
54 //-------------------------------------
55 // External API
56 //-------------------------------------
57
58 bool BinariesCheckThread::check(SysinfoModel *sysinfo)
59 {
60         QMutexLocker lock(&m_binLock);
61
62         QEventLoop loop;
63         BinariesCheckThread thread(sysinfo);
64
65         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
66
67         connect(&thread, SIGNAL(finished()), &loop, SLOT(quit()));
68         connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
69         
70         thread.start();
71         QTimer::singleShot(30000, &loop, SLOT(quit()));
72         
73         qDebug("Binaries checker thread has been created, please wait...");
74         loop.exec(QEventLoop::ExcludeUserInputEvents);
75         qDebug("Binaries checker thread finished.");
76
77         QApplication::restoreOverrideCursor();
78
79         if(!thread.wait(1000))
80         {
81                 qWarning("Binaries checker thread encountered timeout -> probably deadlock!");
82                 thread.terminate();
83                 thread.wait();
84                 return false;
85         }
86
87         if(thread.getException())
88         {
89                 qWarning("Binaries checker thread encountered an exception !!!");
90                 return false;
91         }
92         
93         return thread.getSuccess();
94 }
95
96 //-------------------------------------
97 // Thread class
98 //-------------------------------------
99
100 BinariesCheckThread::BinariesCheckThread(const SysinfoModel *const sysinfo)
101 :
102         m_sysinfo(sysinfo)
103 {
104         m_success = m_exception = false;
105 }
106
107 BinariesCheckThread::~BinariesCheckThread(void)
108 {
109 }
110
111 void BinariesCheckThread::run(void)
112 {
113         m_success = m_exception = false;
114         checkBinaries1(m_success, m_sysinfo, &m_exception);
115 }
116
117 void BinariesCheckThread::checkBinaries1(volatile bool &success, const SysinfoModel *const sysinfo, volatile bool *exception)
118 {
119         __try
120         {
121                 checkBinaries2(success, sysinfo, exception);
122         }
123         __except(1)
124         {
125                 *exception = true;
126                 qWarning("Unhandled exception error in binaries checker thread !!!");
127         }
128 }
129
130 void BinariesCheckThread::checkBinaries2(volatile bool &success, const SysinfoModel *const sysinfo, volatile bool *exception)
131 {
132         try
133         {
134                 return checkBinaries3(success, sysinfo);
135         }
136         catch(...)
137         {
138                 *exception = true;
139                 qWarning("Binaries checker initializdation raised an C++ exception!");
140         }
141 }
142
143 void BinariesCheckThread::checkBinaries3(volatile bool &success, const SysinfoModel *const sysinfo)
144 {
145         success = true;
146
147         //Create list of all required binary files
148         typedef QPair<QString, bool> FileEntry;
149         QList<FileEntry> binFiles;
150         for(OptionsModel::EncType encdr = OptionsModel::EncType_MIN; encdr <= OptionsModel::EncType_MAX; NEXT(encdr))
151         {
152                 const AbstractEncoderInfo &encInfo = EncoderFactory::getEncoderInfo(encdr);
153                 const QFlags<OptionsModel::EncVariant> variants = encInfo.getVariants();
154                 for(OptionsModel::EncArch arch = OptionsModel::EncArch_x86_32; arch <= OptionsModel::EncArch_x86_64; NEXT(arch))
155                 {
156                         for(OptionsModel::EncVariant varnt = OptionsModel::EncVariant_MIN; varnt <= OptionsModel::EncVariant_MAX; SHFL(varnt))
157                         {
158                                 if(variants.testFlag(varnt))
159                                 {
160                                         binFiles << qMakePair(encInfo.getBinaryPath(sysinfo, arch, varnt), false);
161                                         const QStringList dependencies = encInfo.getDependencies(sysinfo, arch, varnt);
162                                         if (!dependencies.empty())
163                                         {
164                                                 for (QStringList::ConstIterator iter = dependencies.constBegin(); iter != dependencies.constEnd(); iter++)
165                                                 {
166                                                         binFiles << qMakePair(*iter, true);
167                                                 }
168                                         }
169                                 }
170                         }
171                 }
172         }
173         for(int i = 0; i < 2; i++)
174         {
175                 binFiles << qMakePair(SourceFactory::getSourceInfo(SourceFactory::SourceType_AVS).getBinaryPath(sysinfo, bool(i)), false);
176                 binFiles << qMakePair(AVS_CHECK_BINARY(sysinfo, bool(i)), false);
177         }
178         for(size_t i = 0; UpdaterDialog::BINARIES[i].name; i++)
179         {
180                 if(UpdaterDialog::BINARIES[i].exec)
181                 {
182                         binFiles << qMakePair(QString("%1/toolset/common/%2").arg(sysinfo->getAppPath(), QString::fromLatin1(UpdaterDialog::BINARIES[i].name)), false);
183                 }
184         }
185
186         //Actually validate the binaries
187         size_t currentFile = 0;
188         for(QList<FileEntry>::ConstIterator iter = binFiles.constBegin(); iter != binFiles.constEnd(); iter++)
189         {
190                 QScopedPointer<QFile> file(new QFile(iter->first));
191                 qDebug("%s", MUTILS_UTF8(file->fileName()));
192
193                 if(file->open(QIODevice::ReadOnly))
194                 {
195                         if(!(iter->second || MUtils::OS::is_executable_file(file->fileName())))
196                         {
197                                 success = false;
198                                 qWarning("Required tool does NOT look like a valid Win32/Win64 binary:\n%s\n", MUTILS_UTF8(file->fileName()));
199                                 return;
200                         }
201                         if(currentFile < MAX_BINARIES)
202                         {
203                                 m_binPath[currentFile++].reset(file.take());
204                                 continue;
205                         }
206                         qFatal("Current binary file exceeds max. number of binaries!");
207                 }
208                 else
209                 {
210                         success = false;
211                         qWarning("Required tool could not be found or access denied:\n%s\n", MUTILS_UTF8(file->fileName()));
212                         return;
213                 }
214         }
215 }