2012-01-28 18:55:40 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Simple x264 Launcher
|
|
|
|
// 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
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "thread_encode.h"
|
2012-01-29 19:14:46 +01:00
|
|
|
|
2012-01-28 19:59:04 +01:00
|
|
|
#include "global.h"
|
2012-01-29 19:14:46 +01:00
|
|
|
#include "model_options.h"
|
|
|
|
|
|
|
|
#include <QDate>
|
|
|
|
#include <QTime>
|
2012-01-29 21:31:09 +01:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QLibrary>
|
2012-01-28 18:55:40 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
/*
|
|
|
|
* Win32 API definitions
|
|
|
|
*/
|
|
|
|
typedef HANDLE (WINAPI *CreateJobObjectFun)(__in_opt LPSECURITY_ATTRIBUTES lpJobAttributes, __in_opt LPCSTR lpName);
|
|
|
|
typedef BOOL (WINAPI *SetInformationJobObjectFun)(__in HANDLE hJob, __in JOBOBJECTINFOCLASS JobObjectInformationClass, __in_bcount(cbJobObjectInformationLength) LPVOID lpJobObjectInformation, __in DWORD cbJobObjectInformationLength);
|
|
|
|
typedef BOOL (WINAPI *AssignProcessToJobObjectFun)(__in HANDLE hJob, __in HANDLE hProcess);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Static vars
|
|
|
|
*/
|
|
|
|
QMutex EncodeThread::m_mutex_startProcess;
|
|
|
|
HANDLE EncodeThread::m_handle_jobObject = NULL;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructor & Destructor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir)
|
2012-01-28 18:55:40 +01:00
|
|
|
:
|
2012-01-29 15:57:23 +01:00
|
|
|
m_jobId(QUuid::createUuid()),
|
|
|
|
m_sourceFileName(sourceFileName),
|
2012-01-29 19:14:46 +01:00
|
|
|
m_outputFileName(outputFileName),
|
2012-01-29 21:31:09 +01:00
|
|
|
m_options(new OptionsModel(*options)),
|
|
|
|
m_binDir(binDir)
|
2012-01-28 18:55:40 +01:00
|
|
|
{
|
2012-01-28 23:24:41 +01:00
|
|
|
m_abort = false;
|
2012-01-28 18:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EncodeThread::~EncodeThread(void)
|
|
|
|
{
|
2012-01-29 19:14:46 +01:00
|
|
|
X264_DELETE(m_options);
|
2012-01-28 18:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Thread entry point
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void EncodeThread::run(void)
|
|
|
|
{
|
2012-01-28 19:59:04 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
encode();
|
|
|
|
}
|
|
|
|
catch(char *msg)
|
|
|
|
{
|
|
|
|
emit messageLogged(m_jobId, QString("EXCEPTION ERROR: ").append(QString::fromLatin1(msg)));
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
emit messageLogged(m_jobId, QString("EXCEPTION ERROR !!!"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Encode functions
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-01-28 19:59:04 +01:00
|
|
|
void EncodeThread::encode(void)
|
|
|
|
{
|
2012-01-29 21:31:09 +01:00
|
|
|
Sleep(500);
|
2012-01-28 19:59:04 +01:00
|
|
|
|
2012-01-29 19:14:46 +01:00
|
|
|
//Print some basic info
|
|
|
|
log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
|
|
|
|
log(tr("Source file: %1").arg(m_sourceFileName));
|
|
|
|
log(tr("Output file: %1").arg(m_outputFileName));
|
|
|
|
log(tr("\n[Encoder Options]"));
|
|
|
|
log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
|
|
|
|
log(tr("Preset: %1").arg(m_options->preset()));
|
|
|
|
log(tr("Tuning: %1").arg(m_options->tune()));
|
|
|
|
log(tr("Profile: %1").arg(m_options->profile()));
|
|
|
|
log(tr("Custom: %1").arg(m_options->custom().isEmpty() ? tr("(None)") : m_options->custom()));
|
2012-01-29 21:31:09 +01:00
|
|
|
|
|
|
|
//Detect source info
|
2012-01-29 19:14:46 +01:00
|
|
|
log(tr("\n[Input Properties]"));
|
2012-01-29 21:31:09 +01:00
|
|
|
log(tr("Not implemented yet, sorry ;-)\n"));
|
|
|
|
|
|
|
|
QStringList cmdLine;
|
|
|
|
QProcess process;
|
|
|
|
|
|
|
|
cmdLine = buildCommandLine();
|
2012-01-29 19:14:46 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
log("Creating process:");
|
|
|
|
if(!startProcess(process, QString("%1/x264.exe").arg(m_binDir), cmdLine))
|
2012-01-28 19:59:04 +01:00
|
|
|
{
|
2012-01-29 21:31:09 +01:00
|
|
|
emit statusChanged(m_jobId, JobStatus_Failed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit statusChanged(m_jobId, JobStatus_Running);
|
|
|
|
QRegExp regExp("\\[(\\d+)\\.\\d+%\\].+frames");
|
2012-01-28 23:24:41 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
bool bTimeout = false;
|
|
|
|
bool bAborted = false;
|
2012-01-28 23:24:41 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
while(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
2012-01-28 23:24:41 +01:00
|
|
|
if(m_abort)
|
|
|
|
{
|
2012-01-29 21:31:09 +01:00
|
|
|
process.kill();
|
|
|
|
bAborted = true;
|
|
|
|
log("\nABORTED BY USER !!!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
process.waitForReadyRead(m_processTimeoutInterval);
|
|
|
|
if(!process.bytesAvailable() && process.state() == QProcess::Running)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
qWarning("x264 process timed out <-- killing!");
|
|
|
|
log("\nPROCESS TIMEOUT !!!");
|
|
|
|
bTimeout = true;
|
|
|
|
break;
|
2012-01-28 23:24:41 +01:00
|
|
|
}
|
2012-01-29 21:31:09 +01:00
|
|
|
while(process.bytesAvailable() > 0)
|
|
|
|
{
|
|
|
|
QByteArray line = process.readLine();
|
|
|
|
QString text = QString::fromUtf8(line.constData()).simplified();
|
|
|
|
if(regExp.lastIndexIn(text) >= 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
unsigned int progress = regExp.cap(1).toUInt(&ok);
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
emit progressChanged(m_jobId, progress);
|
|
|
|
emit detailsChanged(m_jobId, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!text.isEmpty())
|
|
|
|
{
|
|
|
|
log(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.waitForFinished();
|
|
|
|
if(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
|
|
|
|
{
|
|
|
|
emit statusChanged(m_jobId, JobStatus_Failed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit progressChanged(m_jobId, 100);
|
|
|
|
emit statusChanged(m_jobId, JobStatus_Completed);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList EncodeThread::buildCommandLine(void)
|
|
|
|
{
|
|
|
|
QStringList cmdLine;
|
|
|
|
|
|
|
|
cmdLine << "--crf" << QString::number(m_options->quantizer());
|
|
|
|
|
|
|
|
if(m_options->tune().compare("none", Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
cmdLine << "--tune" << m_options->tune().toLower();
|
2012-01-28 19:59:04 +01:00
|
|
|
}
|
2012-01-29 21:31:09 +01:00
|
|
|
|
|
|
|
cmdLine << "--preset" << m_options->preset().toLower();
|
|
|
|
cmdLine << "--output" << m_outputFileName;
|
|
|
|
cmdLine << m_sourceFileName;
|
2012-01-28 19:59:04 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
return cmdLine;
|
|
|
|
}
|
2012-01-28 19:59:04 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Misc functions
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args)
|
|
|
|
{
|
|
|
|
static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL;
|
|
|
|
|
|
|
|
QMutexLocker lock(&m_mutex_startProcess);
|
|
|
|
log(commandline2string(program, args) + "\n");
|
|
|
|
|
|
|
|
if(!AssignProcessToJobObjectPtr)
|
2012-01-29 00:57:47 +01:00
|
|
|
{
|
2012-01-29 21:31:09 +01:00
|
|
|
QLibrary Kernel32Lib("kernel32.dll");
|
|
|
|
AssignProcessToJobObjectPtr = (AssignProcessToJobObjectFun) Kernel32Lib.resolve("AssignProcessToJobObject");
|
|
|
|
}
|
2012-01-29 00:57:47 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
|
|
|
process.start(program, args);
|
|
|
|
|
|
|
|
if(process.waitForStarted())
|
|
|
|
{
|
|
|
|
|
|
|
|
if(AssignProcessToJobObjectPtr)
|
2012-01-29 00:57:47 +01:00
|
|
|
{
|
2012-01-29 21:31:09 +01:00
|
|
|
AssignProcessToJobObjectPtr(m_handle_jobObject, process.pid()->hProcess);
|
2012-01-29 00:57:47 +01:00
|
|
|
}
|
2012-01-29 21:31:09 +01:00
|
|
|
if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
|
2012-01-29 00:57:47 +01:00
|
|
|
{
|
2012-01-29 21:31:09 +01:00
|
|
|
SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
|
2012-01-29 00:57:47 +01:00
|
|
|
}
|
2012-01-29 21:31:09 +01:00
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
return true;
|
2012-01-29 00:57:47 +01:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
log("Process creation has failed :-(");
|
|
|
|
QString errorMsg= process.errorString().trimmed();
|
|
|
|
if(!errorMsg.isEmpty()) log(errorMsg);
|
2012-01-29 00:57:47 +01:00
|
|
|
|
2012-01-29 21:31:09 +01:00
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
|
|
|
|
{
|
|
|
|
QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
|
|
|
|
|
|
|
|
for(int i = 0; i < arguments.count(); i++)
|
|
|
|
{
|
|
|
|
commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return commandline;
|
2012-01-28 18:55:40 +01:00
|
|
|
}
|