]> git.sesse.net Git - kdenlive/blob - renderer/renderjob.cpp
Use QLatin1String
[kdenlive] / renderer / renderjob.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "renderjob.h"
22
23 #include <stdlib.h> // for getenv, posix portable to Windows: http://msdn.microsoft.com/en-us/library/tehxacec(VS.80).aspx
24
25 #include <QtDBus>
26 #include <QFile>
27 #include <QThread>
28 #include <QStringList>
29
30
31 // Can't believe I need to do this to sleep.
32 class SleepThread : QThread
33 {
34 public:
35     virtual void run() {}
36     static void msleep(unsigned long msecs) {
37         QThread::msleep(msecs);
38     }
39 };
40
41
42 RenderJob::RenderJob(bool erase, bool usekuiserver, int pid, const QString& renderer, const QString& profile, const QString& rendermodule, const QString& player, const QString& scenelist, const QString& dest, const QStringList& preargs, const QStringList& args, int in, int out) :
43     QObject(),
44     m_jobUiserver(NULL),
45     m_kdenliveinterface(NULL),
46     m_usekuiserver(usekuiserver),
47     m_enablelog(false),
48     m_pid(pid)
49 {
50     m_scenelist = scenelist;
51     m_dest = dest;
52     m_player = player;
53     m_progress = 0;
54     m_erase = erase;
55     m_renderProcess = new QProcess;
56     
57     // Disable VDPAU so that rendering will work even if there is a Kdenlive instance using VDPAU
58 #if QT_VERSION >= 0x040600
59     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
60     env.insert(QLatin1String("MLT_NO_VDPAU"), QLatin1String("1"));
61     m_renderProcess->setProcessEnvironment(env);
62 #else
63     QStringList env = QProcess::systemEnvironment();
64     env << QLatin1String("MLT_NO_VDPAU=1");
65     m_renderProcess->setEnvironment(env);
66 #endif
67
68     m_prog = renderer;
69     m_args << scenelist;
70     if (in != -1) m_args << QLatin1String("in=") + QString::number(in);
71     if (out != -1) m_args << QLatin1String("out=") + QString::number(out);
72
73     m_args << preargs;
74     //qDebug()<<"PRE ARGS: "<<preargs;
75     if (scenelist.startsWith(QLatin1String("consumer:"))) {
76         // Use MLT's producer_consumer, needs a different syntax for profile:
77         m_args << QLatin1String("profile=") + profile;
78     } else m_args << QLatin1String("-profile") << profile;
79     m_args << QLatin1String("-consumer") << rendermodule + QLatin1Char(':') + m_dest << QLatin1String("progress=1") << args;
80
81     m_dualpass = false;
82     if (args.contains(QLatin1String("pass=1"))) m_dualpass = true;
83
84     connect(m_renderProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotCheckProcess(QProcess::ProcessState)));
85     m_renderProcess->setReadChannel(QProcess::StandardError);
86
87     m_enablelog = (getenv("KDENLIVE_RENDER_LOG") != NULL);
88     if (m_enablelog) {
89         // Create a log of every render process.
90         m_logfile.setAutoRemove(false);
91         m_logfile.setFileTemplate(QDir::tempPath() + QLatin1String("/kdenlive_render.log.XXXXXXXX"));
92         if (m_logfile.open()) {
93             qDebug() << "Writing render log to " << m_logfile.fileName();
94
95         } else {
96             qDebug() << "Unable to log to " << m_logfile.fileName();
97         }
98         m_logstream.setDevice(&m_logfile);
99         QString tmplist = scenelist;
100         if (tmplist.contains(QLatin1String("consumer:"))) {
101             const QStringList tl = tmplist.split(QLatin1String("consumer:"));
102             if (tl.count() == 2) {
103                 tmplist = tl[1];
104             }
105         }
106         m_logstream << "Log starting. Dumping contents of " << tmplist << endl;
107         QFile file(tmplist);
108         if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
109             m_logstream << "Unable to read contents of " << tmplist << endl;
110         } else {
111             m_logstream.flush();
112             QTextStream in(&file);
113             m_logstream << in.readAll() << endl;
114         }
115     }
116 }
117
118
119 RenderJob::~RenderJob()
120 {
121     delete m_renderProcess;
122     if (m_enablelog) {
123         m_logfile.close();
124     }
125 }
126
127 void RenderJob::setLocale(const QString &locale)
128 {
129 #if QT_VERSION >= 0x040600
130     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
131     env.insert(QLatin1String("LC_NUMERIC"), locale);
132     m_renderProcess->setProcessEnvironment(env);
133 #else
134     QStringList env = QProcess::systemEnvironment();
135     env << QString::fromLatin1("LC_NUMERIC=%1").arg(locale);
136     m_renderProcess->setEnvironment(env);
137 #endif
138 }
139
140 void RenderJob::slotAbort(const QString& url)
141 {
142     if (m_dest == url) slotAbort();
143 }
144
145 void RenderJob::slotAbort()
146 {
147     qDebug() << "Kdenlive-render: JOB ABORTED BY USER...";
148     m_renderProcess->kill();
149
150     if (m_kdenliveinterface) {
151         m_dbusargs[1] = -3;
152         m_dbusargs.append(QString());
153         m_kdenliveinterface->callWithArgumentList(QDBus::NoBlock, QLatin1String("setRenderingFinished"), m_dbusargs);
154     }
155     if (m_jobUiserver) m_jobUiserver->call(QLatin1String("terminate"), QString());
156     if (m_erase) {
157         QFile f(m_scenelist);
158         f.remove();
159     }
160     QFile f(m_dest);
161     f.remove();
162     if (m_enablelog) {
163         m_logstream << "Job aborted by user" << endl;
164         m_logstream.flush();
165         m_logfile.close();
166     }
167     qApp->quit();
168 }
169
170 void RenderJob::receivedStderr()
171 {
172     QString result = QString::fromLocal8Bit(m_renderProcess->readAllStandardError()).simplified();
173     //fprintf(stderr, "* * * *RENDER LG: %s\n", result.toUtf8().data());
174     if (!result.startsWith(QLatin1String("Current Frame"))) m_errorMessage.append(result + QLatin1String("<br>"));
175     else {
176         if (m_enablelog) m_logstream << "ReceivedStderr from melt: " << result << endl;
177         result = result.section(QLatin1Char(' '), -1);
178         int pro = result.toInt();
179         if (pro < 0 || pro > 100) return;
180         if (pro > m_progress) {
181             m_progress = pro;
182             if (m_args.contains(QLatin1String("pass=1"))) {
183                 m_progress /= 2.0;
184             } else if (m_args.contains(QLatin1String("pass=2"))) {
185                 m_progress = 50 + m_progress / 2.0;
186             }
187             if (m_kdenliveinterface) {
188                 if (!m_kdenliveinterface->isValid()) {
189                     delete m_kdenliveinterface;
190                     m_kdenliveinterface = NULL;
191                     // qDebug() << "BROKEN COMMUNICATION WITH KDENLIVE";
192                 } else {
193                     m_dbusargs[1] = m_progress;
194                     m_kdenliveinterface->callWithArgumentList(QDBus::NoBlock, QLatin1String("setRenderingProgress"), m_dbusargs);
195                 }
196             } else if (pro % 5 == 0) {
197                 // Try to restart communication with Kdenlive every 5 percents
198                 // qDebug() << "TRYING TO RESTART COMMUNICATION WITH KDENLIVE";
199                 initKdenliveDbusInterface();
200             }
201
202             if (m_jobUiserver) {
203                 m_jobUiserver->call(QLatin1String("setPercent"), (uint) m_progress);
204                 /*int seconds = m_startTime.secsTo(QTime::currentTime());
205                 seconds = seconds * (100 - m_progress) / m_progress;
206                 m_jobUiserver->call("setDescriptionField", (uint) 1, tr("Remaining time"), QTime().addSecs(seconds).toString("hh:mm:ss"));*/
207             }
208         }
209     }
210 }
211
212 void RenderJob::start()
213 {
214     QDBusConnectionInterface* interface = QDBusConnection::sessionBus().interface();
215     if (interface && m_usekuiserver) {
216         if (!interface->isServiceRegistered(QLatin1String("org.kde.JobViewServer"))) {
217             qDebug() << "No org.kde.JobViewServer registered, trying to start kuiserver";
218             if (m_enablelog) m_logstream << "No org.kde.JobViewServer registered, trying to start kuiserver";
219             if (QProcess::startDetached(QLatin1String("kuiserver"))) {
220                 qDebug() << "Started kuiserver";
221                 if (m_enablelog) m_logstream << "Started kuiserver";
222                 // Give it a couple of seconds to start
223                 QTime t;
224                 t.start();
225                 while (!interface->isServiceRegistered(QLatin1String("org.kde.JobViewServer")) && t.elapsed() < 3000) {
226                     SleepThread::msleep(100);   //Sleep 100 ms
227                 }
228             } else {
229                 qDebug() << "Failed to start kuiserver";
230                 if (m_enablelog) m_logstream << "Failed to start kuiserver";
231             }
232         }
233
234         if (interface->isServiceRegistered(QLatin1String("org.kde.JobViewServer"))) {
235             QDBusInterface kuiserver(QLatin1String("org.kde.JobViewServer"), QLatin1String("/JobViewServer"), QLatin1String("org.kde.JobViewServer"));
236             QDBusReply<QDBusObjectPath> objectPath = kuiserver.call(QLatin1String("requestView"),QLatin1String("Kdenlive"), QLatin1String("kdenlive"), 0x0001);
237             QString reply = ((QDBusObjectPath) objectPath).path();
238
239             // Use of the KDE JobViewServer is an ugly hack, it is not reliable
240             QString dbusView = QLatin1String("org.kde.JobViewV2");
241             m_jobUiserver = new QDBusInterface(QLatin1String("org.kde.JobViewServer"), reply, dbusView);
242             if (!m_jobUiserver || !m_jobUiserver->isValid()) {
243                 dbusView = QLatin1String("org.kde.JobView");
244                 m_jobUiserver = new QDBusInterface(QLatin1String("org.kde.JobViewServer"), reply, dbusView);
245             }
246                 
247             if (m_jobUiserver && m_jobUiserver->isValid()) {
248                 m_startTime = QTime::currentTime();
249                 if (!m_args.contains(QLatin1String("pass=2")))
250                     m_jobUiserver->call(QLatin1String("setPercent"), (uint) 0);
251                 //m_jobUiserver->call("setInfoMessage", tr("Rendering %1").arg(QFileInfo(m_dest).fileName()));
252                 m_jobUiserver->call(QLatin1String("setDescriptionField"), (uint) 0, tr("Rendering"), m_dest);
253                 QDBusConnection::sessionBus().connect(QLatin1String("org.kde.JobViewServer"), reply, dbusView, QLatin1String("cancelRequested"), this, SLOT(slotAbort()));
254             }
255         }
256     }
257
258     initKdenliveDbusInterface();
259     
260     // Make sure the destination file is writable
261     QFile checkDestination(m_dest);
262     if (!checkDestination.open(QIODevice::WriteOnly)) {
263         slotIsOver(QProcess::NormalExit, false);
264     }
265     checkDestination.close();
266
267     // Because of the logging, we connect to stderr in all cases.
268     connect(m_renderProcess, SIGNAL(readyReadStandardError()), this, SLOT(receivedStderr()));
269     m_renderProcess->start(m_prog, m_args);
270     if (m_enablelog) m_logstream << "Started render process: " << m_prog << " " << m_args.join(QLatin1String(" ")) << endl;
271     qDebug() << "Started render process: " << m_prog << " " << m_args.join(QLatin1String(" "));
272 }
273
274
275 void RenderJob::initKdenliveDbusInterface()
276 {
277     QString kdenliveId;
278     QDBusConnection connection = QDBusConnection::sessionBus();
279     QDBusConnectionInterface* ibus = connection.interface();
280     kdenliveId = QString::fromLatin1("org.kde.kdenlive-%1").arg(m_pid);
281     if (!ibus->isServiceRegistered(kdenliveId))
282     {
283         kdenliveId.clear();
284         const QStringList services = ibus->registeredServiceNames();
285         foreach(const QString & service, services) {
286             if (!service.startsWith(QLatin1String("org.kde.kdenlive")))
287                 continue;
288             kdenliveId = service;
289             break;
290         }
291     }
292     m_dbusargs.clear();
293     if (kdenliveId.isEmpty()) return;
294     m_kdenliveinterface = new QDBusInterface(kdenliveId,
295             QLatin1String("/MainWindow"),
296             QLatin1String("org.kdenlive.MainWindow"),
297             connection,
298             this);
299
300     if (m_kdenliveinterface) {
301         m_dbusargs.append(m_dest);
302         m_dbusargs.append((int) 0);
303         if (!m_args.contains(QLatin1String("pass=2")))
304             m_kdenliveinterface->callWithArgumentList(QDBus::NoBlock, QLatin1String("setRenderingProgress"), m_dbusargs);
305         connect(m_kdenliveinterface, SIGNAL(abortRenderJob(QString)),
306                 this, SLOT(slotAbort(QString)));
307     }
308 }
309
310
311 void RenderJob::slotCheckProcess(QProcess::ProcessState state)
312 {
313     if (state == QProcess::NotRunning) slotIsOver(m_renderProcess->exitStatus());
314 }
315
316
317
318 void RenderJob::slotIsOver(QProcess::ExitStatus status, bool isWritable)
319 {
320     if (m_jobUiserver) m_jobUiserver->call(QLatin1String("terminate"), QString());
321     if (!isWritable) {
322         QString error = tr("Cannot write to %1, check the permissions.").arg(m_dest);
323         if (m_kdenliveinterface) {
324             m_dbusargs[1] = (int) - 2;
325             m_dbusargs.append(error);
326             m_kdenliveinterface->callWithArgumentList(QDBus::NoBlock, QLatin1String("setRenderingFinished"), m_dbusargs);
327         }
328         QStringList args;
329         args << QLatin1String("--error") << error;
330         if (m_enablelog) m_logstream << error << endl;
331         qDebug() << error;
332         QProcess::startDetached(QLatin1String("kdialog"), args);
333         qApp->quit();
334     }
335     if (m_erase) {
336         QFile f(m_scenelist);
337         f.remove();
338     }
339     if (status == QProcess::CrashExit || m_renderProcess->error() != QProcess::UnknownError) {
340         // rendering crashed
341         if (m_kdenliveinterface) {
342             m_dbusargs[1] = (int) - 2;
343             m_dbusargs.append(m_errorMessage);
344             m_kdenliveinterface->callWithArgumentList(QDBus::NoBlock, QLatin1String("setRenderingFinished"), m_dbusargs);
345         }
346         QStringList args;
347         QString error = tr("Rendering of %1 aborted, resulting video will probably be corrupted.").arg(m_dest);
348         args << QLatin1String("--error") << error;
349         if (m_enablelog) m_logstream << error << endl;
350         qDebug() << error;
351         QProcess::startDetached(QLatin1String("kdialog"), args);
352         qApp->quit();
353     } else {
354         if (!m_dualpass && m_kdenliveinterface) {
355             m_dbusargs[1] = (int) - 1;
356             m_dbusargs.append(QString());
357             m_kdenliveinterface->callWithArgumentList(QDBus::NoBlock, QLatin1String("setRenderingFinished"), m_dbusargs);
358         }
359         QDBusConnectionInterface* interface = QDBusConnection::sessionBus().interface();
360         if (!m_dualpass && interface && interface->isServiceRegistered(QLatin1String("org.kde.knotify"))) {
361             QDBusMessage m = QDBusMessage::createMethodCall(QLatin1String("org.kde.knotify"),
362                              QLatin1String("/Notify"),
363                              QLatin1String("org.kde.KNotify"),
364                              QLatin1String("event"));
365             int seconds = m_startTime.secsTo(QTime::currentTime());
366             QList<QVariant> args;
367             args.append(QLatin1String("RenderFinished"));   // action name
368             args.append(QLatin1String("kdenlive"));   // app name
369             args.append(QVariantList());   // contexts
370             args.append(tr("Rendering of %1 finished in %2").arg(m_dest, QTime(0, 0, seconds).toString(QLatin1String("hh:mm:ss"))));   // body
371             args.append(QByteArray());   // app icon
372             QStringList actionList;
373             args.append(actionList);   // actions
374             qlonglong wid = 0;
375             args.append(wid);   // win id
376
377             m.setArguments(args);
378             QDBusMessage replyMsg = QDBusConnection::sessionBus().call(m);
379         }
380         if (m_enablelog) m_logstream << "Rendering of " << m_dest << " finished" << endl;
381         qDebug() << "Rendering of " << m_dest << " finished";
382         if (!m_dualpass && m_player != QLatin1String("-")) {
383             if (m_enablelog) m_logstream << "Starting player" << endl;
384             QStringList args;
385             args << m_dest;
386             QProcess::startDetached(m_player, args);
387         }
388         if (m_dualpass) {
389             emit renderingFinished();
390             deleteLater();
391         } else qApp->quit();
392     }
393 }
394
395 #include "renderjob.moc"