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>
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Static vars
|
|
|
|
*/
|
2012-04-06 23:15:58 +02:00
|
|
|
quint64 AbstractTool::s_lastLaunchTime = 0ui64;
|
|
|
|
QMutex AbstractTool::s_mutex_startProcess;
|
2013-10-07 00:01:15 +02:00
|
|
|
JobObject *AbstractTool::s_jobObject = NULL;
|
2012-04-06 23:15:58 +02:00
|
|
|
unsigned int AbstractTool::s_jobObjRefCount = 0U;
|
2012-03-01 02:45:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Const
|
|
|
|
*/
|
2013-10-07 00:01:15 +02:00
|
|
|
static const unsigned int START_DELAY = 333; //in milliseconds
|
2012-03-01 02:45:21 +01:00
|
|
|
static const quint64 START_DELAY_NANO = START_DELAY * 1000 * 10; //in 100-nanosecond intervals
|
2010-12-01 23:14:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
AbstractTool::AbstractTool(void)
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
QMutexLocker lock(&s_mutex_startProcess);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
if(s_jobObjRefCount < 1U)
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2013-10-07 00:01:15 +02:00
|
|
|
s_jobObject = new JobObject();
|
|
|
|
s_jobObjRefCount = 1U;
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
2012-03-01 02:45:21 +01:00
|
|
|
else
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
s_jobObjRefCount++;
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
2010-12-05 23:11:03 +01:00
|
|
|
|
|
|
|
m_firstLaunch = true;
|
2010-12-01 23:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
AbstractTool::~AbstractTool(void)
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
QMutexLocker lock(&s_mutex_startProcess);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2012-04-06 23:15:58 +02:00
|
|
|
if(s_jobObjRefCount >= 1U)
|
2012-02-21 22:36:13 +01:00
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
s_jobObjRefCount--;
|
2013-10-07 00:01:15 +02:00
|
|
|
if(s_jobObjRefCount < 1U)
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
MUTILS_DELETE(s_jobObject);
|
2012-03-01 02:45:21 +01:00
|
|
|
}
|
2012-02-21 22:36:13 +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)
|
|
|
|
{
|
2012-04-06 23:15:58 +02:00
|
|
|
QMutexLocker lock(&s_mutex_startProcess);
|
2012-03-01 02:45:21 +01:00
|
|
|
|
2013-10-07 00:01:15 +02:00
|
|
|
if(lamexp_current_file_time() <= s_lastLaunchTime)
|
2012-03-01 02:45:21 +01:00
|
|
|
{
|
2014-11-30 21:32:23 +01:00
|
|
|
MUtils::OS::sleep_ms(START_DELAY);
|
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())
|
|
|
|
{
|
2013-10-07 00:01:15 +02:00
|
|
|
if(s_jobObject)
|
2010-12-01 23:14:47 +01:00
|
|
|
{
|
2013-10-07 00:01:15 +02:00
|
|
|
if(!s_jobObject->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
|
|
|
|
|
|
|
lamexp_change_process_priority(&process, -1);
|
2010-12-01 23:14:47 +01:00
|
|
|
lock.unlock();
|
2010-12-05 23:11:03 +01:00
|
|
|
|
|
|
|
if(m_firstLaunch)
|
|
|
|
{
|
|
|
|
emit statusUpdated(0);
|
|
|
|
m_firstLaunch = false;
|
|
|
|
}
|
|
|
|
|
2013-10-07 00:01:15 +02:00
|
|
|
s_lastLaunchTime = lamexp_current_file_time() + START_DELAY_NANO;
|
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
|
|
|
|
2013-10-07 00:01:15 +02:00
|
|
|
s_lastLaunchTime = lamexp_current_file_time() + START_DELAY_NANO;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|