2010-12-01 23:14:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2014-01-01 17:05:52 +01:00
|
|
|
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
|
2010-12-01 23:14:47 +01:00
|
|
|
//
|
|
|
|
// 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
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2010-12-01 23:14:47 +01:00
|
|
|
//
|
|
|
|
// 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 "Tool_Abstract.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//Internal
|
2011-04-11 21:55:34 +02:00
|
|
|
#include "Global.h"
|
2013-10-07 00:01:15 +02:00
|
|
|
#include "JobObject.h"
|
2011-04-11 21:55:34 +02:00
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
2014-11-30 21:32:23 +01:00
|
|
|
#include <MUtils/OSSupport.h>
|
2014-11-25 02:14:42 +01:00
|
|
|
|
|
|
|
//Qt
|
2010-12-01 23:14:47 +01:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QLibrary>
|
2011-11-22 12:43:32 +01:00
|
|
|
#include <QProcessEnvironment>
|
|
|
|
#include <QDir>
|
2014-12-06 15:54:58 +01:00
|
|
|
#include <QElapsedTimer>
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
/*
|
2014-12-06 15:54:58 +01:00
|
|
|
* Static Objects
|
2010-12-01 23:14:47 +01:00
|
|
|
*/
|
2014-12-06 15:54:58 +01:00
|
|
|
QScopedPointer<JobObject> AbstractTool::s_jobObjectInstance;
|
|
|
|
QScopedPointer<QElapsedTimer> AbstractTool::s_startProcessTimer;
|
2014-12-05 22:39:31 +01:00
|
|
|
|
|
|
|
/*
|
2014-12-06 15:54:58 +01:00
|
|
|
* Synchronization
|
2014-12-05 22:39:31 +01:00
|
|
|
*/
|
2014-12-06 15:54:58 +01:00
|
|
|
QMutex AbstractTool::s_startProcessMutex;
|
|
|
|
QMutex AbstractTool::s_createObjectMutex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ref Counter
|
|
|
|
*/
|
|
|
|
quint64 AbstractTool::s_referenceCounter = 0ui64;
|
2012-03-01 02:45:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Const
|
|
|
|
*/
|
2014-12-06 15:54:58 +01:00
|
|
|
static const qint64 START_DELAY = 64i64; //in milliseconds
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
AbstractTool::AbstractTool(void)
|
2014-12-06 01:10:27 +01:00
|
|
|
:
|
|
|
|
m_firstLaunch(true)
|
|
|
|
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2014-12-06 15:54:58 +01:00
|
|
|
QMutexLocker lock(&s_createObjectMutex);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2014-12-06 15:54:58 +01:00
|
|
|
if(s_referenceCounter++ == 0)
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2014-12-06 15:54:58 +01:00
|
|
|
s_jobObjectInstance.reset(new JobObject());
|
|
|
|
s_startProcessTimer.reset(new QElapsedTimer());
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
AbstractTool::~AbstractTool(void)
|
|
|
|
{
|
2014-12-06 15:54:58 +01:00
|
|
|
QMutexLocker lock(&s_createObjectMutex);
|
2014-12-06 01:10:27 +01:00
|
|
|
|
2014-12-06 15:54:58 +01:00
|
|
|
if(--s_referenceCounter == 0)
|
2014-12-06 01:10:27 +01:00
|
|
|
{
|
2014-12-06 15:54:58 +01:00
|
|
|
s_jobObjectInstance.reset(NULL);
|
|
|
|
s_startProcessTimer.reset(NULL);
|
2014-12-06 01:10:27 +01:00
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize and launch process object
|
|
|
|
*/
|
|
|
|
bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
|
|
|
|
{
|
2014-12-05 22:39:31 +01:00
|
|
|
QMutexLocker lock(&s_startProcessMutex);
|
2014-12-05 23:01:38 +01:00
|
|
|
|
2014-12-06 15:54:58 +01:00
|
|
|
if((!s_startProcessTimer.isNull()) && s_startProcessTimer->isValid())
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
2014-12-06 15:54:58 +01:00
|
|
|
qint64 elapsed = s_startProcessTimer->elapsed();
|
|
|
|
while(elapsed < START_DELAY)
|
|
|
|
{
|
|
|
|
lock.unlock();
|
|
|
|
MUtils::OS::sleep_ms((size_t)(START_DELAY - elapsed));
|
|
|
|
lock.relock();
|
|
|
|
elapsed = s_startProcessTimer->elapsed();
|
|
|
|
}
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
|
|
|
|
2010-12-01 23:14:47 +01:00
|
|
|
emit messageLogged(commandline2string(program, args) + "\n");
|
2014-11-25 02:14:42 +01:00
|
|
|
MUtils::init_process(process, QFileInfo(program).absolutePath());
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
process.start(program, args);
|
|
|
|
|
|
|
|
if(process.waitForStarted())
|
|
|
|
{
|
2014-12-06 15:54:58 +01:00
|
|
|
if(!s_jobObjectInstance.isNull())
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2014-12-06 15:54:58 +01:00
|
|
|
if(!s_jobObjectInstance->addProcessToJob(&process))
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
|
|
|
qWarning("Failed to assign process to job object!");
|
|
|
|
}
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
2013-10-07 00:01:15 +02:00
|
|
|
|
2014-12-04 00:02:42 +01:00
|
|
|
MUtils::OS::change_process_priority(&process, -1);
|
2010-12-05 23:11:03 +01:00
|
|
|
|
|
|
|
if(m_firstLaunch)
|
|
|
|
{
|
|
|
|
emit statusUpdated(0);
|
|
|
|
m_firstLaunch = false;
|
|
|
|
}
|
|
|
|
|
2014-12-06 15:54:58 +01:00
|
|
|
s_startProcessTimer->start();
|
2010-12-01 23:14:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-15 21:32:47 +02:00
|
|
|
emit messageLogged("Process creation has failed :-(");
|
|
|
|
QString errorMsg= process.errorString().trimmed();
|
|
|
|
if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
|
|
|
|
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2014-12-06 15:54:58 +01:00
|
|
|
s_startProcessTimer->start();
|
2010-12-01 23:14:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert program arguments to single string
|
|
|
|
*/
|
|
|
|
QString AbstractTool::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|