]> git.sesse.net Git - kdenlive/blob - src/projecttree/meltjob.cpp
Fix scene detection jobs not running concurrently and possible crash: http://kdenlive...
[kdenlive] / src / projecttree / meltjob.cpp
1 /***************************************************************************
2  *                                                                         *
3  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
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     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21 #include "meltjob.h"
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
24
25 #include <KDebug>
26 #include <KUrl>
27 #include <KLocale>
28
29 #include <mlt++/Mlt.h>
30
31
32 static void consumer_frame_render(mlt_consumer, MeltJob * self, mlt_frame frame_ptr)
33 {
34     Mlt::Frame frame(frame_ptr);
35     self->emitFrameNumber((int) frame.get_position());
36 }
37
38 MeltJob::MeltJob(CLIPTYPE cType, const QString &id, QStringList parameters,  QMap <QString, QString>extraParams) : AbstractClipJob(MLTJOB, cType, id, parameters),
39     addClipToProject(0),
40     m_consumer(NULL),
41     m_producer(NULL),
42     m_profile(NULL),
43     m_filter(NULL),
44     m_showFrameEvent(NULL),
45     m_length(0),
46     m_extra(extraParams)
47 {
48     m_jobStatus = JOBWAITING;
49     m_params = parameters;
50     description = i18n("Process clip");
51     QString consum = m_params.at(5);
52     if (consum.contains(':')) m_dest = consum.section(':', 1);
53 }
54
55 void MeltJob::setProducer(Mlt::Producer *producer, KUrl url)
56 {
57     m_url = QString::fromUtf8(producer->get("resource"));
58     if (m_url == "<playlist>" || m_url == "<tractor>" || m_url == "<producer>")
59         m_url == url.path();
60 }
61
62 void MeltJob::startJob()
63 {
64     if (m_url.isEmpty()) {
65         m_errorMessage.append(i18n("No producer for this clip."));
66         setStatus(JOBCRASHED);
67         return;
68     }
69     int in = m_params.takeFirst().toInt();
70     if (in > 0 && !m_extra.contains("offset")) m_extra.insert("offset", QString::number(in));
71     int out = m_params.takeFirst().toInt();
72     QString producerParams =m_params.takeFirst(); 
73     QString filter = m_params.takeFirst();
74     QString filterParams = m_params.takeFirst();
75     QString consumer = m_params.takeFirst();
76     if (consumer.contains(':')) m_dest = consumer.section(':', 1);
77     QString consumerParams = m_params.takeFirst();
78     
79     // optional params
80     int startPos = -1;
81     if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
82     int track = -1;
83     if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
84     if (!m_extra.contains("finalfilter")) m_extra.insert("finalfilter", filter);
85
86     if (out != -1 && out <= in) {
87         m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
88         setStatus(JOBCRASHED);
89         return;
90     }
91     if (m_extra.contains("producer_profile")) {
92         m_profile = new Mlt::Profile;
93         m_profile->set_explicit(false);
94     }
95     else {
96         m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
97     }
98     if (m_extra.contains("resize_profile")) {   
99         m_profile->set_height(m_extra.value("resize_profile").toInt());
100         m_profile->set_width(m_profile->height() * m_profile->sar());
101     }
102     if (out == -1) {
103         m_producer = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
104         if (m_producer) m_length = m_producer->get_length();
105     }
106     else {
107         Mlt::Producer *tmp = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
108         if (tmp) m_producer = tmp->cut(in, out);
109         delete tmp;
110         if (m_producer) m_length = m_producer->get_playtime();
111     }
112     if (!m_producer || !m_producer->is_valid()) {
113         return;
114         m_errorMessage.append(i18n("Invalid clip"));
115         setStatus(JOBCRASHED);
116     }
117     if (m_extra.contains("producer_profile")) {
118         m_profile->from_producer(*m_producer);
119         m_profile->set_explicit(true);
120     }
121     QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
122     foreach(const QString &data, list) {
123         if (data.contains('=')) {
124             m_producer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
125         }
126     }
127     if (consumer.contains(":")) {
128         m_consumer = new Mlt::Consumer(*m_profile, consumer.section(':', 0, 0).toUtf8().constData(), consumer.section(':', 1).toUtf8().constData());
129     }
130     else {
131         m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
132     }
133     if (!m_consumer || !m_consumer->is_valid()) {
134         m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
135         setStatus(JOBCRASHED);
136         return;
137     }
138
139     //m_consumer->set("terminate_on_pause", 1 );
140     //m_consumer->set("eof", "pause" );
141     m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
142
143
144     list = consumerParams.split(' ', QString::SkipEmptyParts);
145     foreach(const QString &data, list) {
146         if (data.contains('=')) {
147             kDebug()<<"// filter con: "<<data;
148             m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
149         }
150     }
151     
152     m_filter = new Mlt::Filter(*m_profile, filter.toUtf8().data());
153     if (!m_filter || !m_filter->is_valid()) {
154         m_errorMessage = i18n("Filter %1 crashed", filter);
155         setStatus(JOBCRASHED);
156         return;
157     }
158     list = filterParams.split(' ', QString::SkipEmptyParts);
159     foreach(const QString &data, list) {
160         if (data.contains('=')) {
161             kDebug()<<"// filter p: "<<data;
162             m_filter->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
163         }
164     }
165     Mlt::Tractor tractor;
166     Mlt::Playlist playlist;
167     playlist.append(*m_producer);
168     tractor.set_track(playlist, 0);
169     m_consumer->connect(tractor);
170     m_producer->set_speed(0);
171     m_producer->seek(0);
172     m_producer->attach(*m_filter);
173     m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
174     m_producer->set_speed(1);
175     m_consumer->run();
176     
177     QMap <QString, QString> jobResults;
178     if (m_jobStatus != JOBABORTED && m_extra.contains("key")) {
179         QString result = m_filter->get(m_extra.value("key").toUtf8().constData());
180         jobResults.insert(m_extra.value("key"), result);
181     }
182     if (!jobResults.isEmpty() && m_jobStatus != JOBABORTED) {
183         m_jobStatus = JOBDONE;
184         emit gotFilterJobResults(m_clipId, startPos, track, jobResults, m_extra);
185     }
186     if (m_jobStatus == JOBABORTED) m_jobStatus = JOBDONE;
187 }
188
189
190 MeltJob::~MeltJob()
191 {
192     if (m_showFrameEvent) delete m_showFrameEvent;
193     if (m_filter) delete m_filter;
194     if (m_producer) delete m_producer;
195     if (m_consumer) delete m_consumer;
196     if (m_profile) delete m_profile;
197 }
198
199 const QString MeltJob::destination() const
200 {
201     return m_dest;
202 }
203
204 stringMap MeltJob::cancelProperties()
205 {
206     QMap <QString, QString> props;
207     return props;
208 }
209
210 const QString MeltJob::statusMessage()
211 {
212     QString statusInfo;
213     switch (m_jobStatus) {
214         case JOBWORKING:
215             statusInfo = description;
216             break;
217         case JOBWAITING:
218             statusInfo = i18n("Waiting to process clip");
219             break;
220         default:
221             break;
222     }
223     return statusInfo;
224 }
225
226 void MeltJob::emitFrameNumber(int pos)
227 {
228     if (m_length > 0) {
229         emit jobProgress(m_clipId, (int) (100 * pos / m_length), jobType);
230     }
231 }
232
233 bool MeltJob::isProjectFilter() const
234 {
235     return m_extra.contains("projecttreefilter");
236 }
237
238 void MeltJob::setStatus(CLIPJOBSTATUS status)
239 {
240     m_jobStatus = status;
241     if (status == JOBABORTED && m_consumer) m_consumer->stop();
242 }
243