OSDN Git Service

Some refactoring to allow supporting multiple encoders in the encode thread (far...
[x264-launcher/x264-launcher.git] / src / tool_abstract.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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 "tool_abstract.h"
23
24 #include "global.h"
25 #include "model_options.h"
26 #include "model_preferences.h"
27 #include "model_sysinfo.h"
28 #include "binaries.h"
29 #include "job_object.h"
30
31 #include <QProcess>
32 #include <QMutexLocker>
33 #include <QDir>
34
35 QMutex AbstractTool::s_mutexStartProcess;
36
37 AbstractTool::AbstractTool(volatile bool *abort, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobObject *jobObject)
38 :
39         m_abort(abort),
40         m_options(options),
41         m_sysinfo(sysinfo),
42         m_preferences(preferences),
43         m_jobObject(jobObject)
44 {
45         /*nothing to do here*/
46 }
47
48 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
49 {
50         QMutexLocker lock(&s_mutexStartProcess);
51         log(commandline2string(program, args) + "\n");
52
53         process.setWorkingDirectory(QDir::tempPath());
54
55         if(mergeChannels)
56         {
57                 process.setProcessChannelMode(QProcess::MergedChannels);
58                 process.setReadChannel(QProcess::StandardOutput);
59         }
60         else
61         {
62                 process.setProcessChannelMode(QProcess::SeparateChannels);
63                 process.setReadChannel(QProcess::StandardError);
64         }
65
66         process.start(program, args);
67         
68         if(process.waitForStarted())
69         {
70                 m_jobObject->addProcessToJob(&process);
71                 x264_change_process_priority(&process, m_preferences->getProcessPriority());
72                 lock.unlock();
73                 return true;
74         }
75
76         log("Process creation has failed :-(");
77         QString errorMsg= process.errorString().trimmed();
78         if(!errorMsg.isEmpty()) log(errorMsg);
79
80         process.kill();
81         process.waitForFinished(-1);
82         return false;
83 }
84
85 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
86 {
87         QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
88         
89         for(int i = 0; i < arguments.count(); i++)
90         {
91                 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
92         }
93
94         return commandline;
95 }