]> git.sesse.net Git - kdenlive/blob - src/projecttree/meltjob.cpp
5592607709d81d5c365a1343bf0d35c5e7ba3abc
[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     // detect if the producer has finished playing. Is there a better way to do it?
35     self->emitFrameNumber();
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_producer(NULL),
41     m_profile(NULL),
42     m_consumer(NULL),
43     m_showFrameEvent(NULL),
44     m_length(0),
45     m_extra(extraParams)
46 {
47     jobStatus = JOBWAITING;
48     m_params = parameters;
49     description = i18n("Process clip");
50     QString consum = m_params.at(5);
51     if (consum.contains(':')) m_dest = consum.section(':', 1);
52 }
53
54 void MeltJob::setProducer(Mlt::Producer *producer, KUrl url)
55 {
56     m_producer = producer;
57     m_url = QString::fromUtf8(m_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_producer) {
65         m_errorMessage.append(i18n("No producer for this clip."));
66         setStatus(JOBCRASHED);
67         return;
68     }
69     int in = m_params.takeFirst().toInt();
70     int out = m_params.takeFirst().toInt();
71     QString producerParams =m_params.takeFirst(); 
72     QString filter = m_params.takeFirst();
73     QString filterParams = m_params.takeFirst();
74     QString consumer = m_params.takeFirst();
75     if (consumer.contains(':')) m_dest = consumer.section(':', 1);
76     QString consumerParams = m_params.takeFirst();
77     
78     // optional params
79     int startPos = -1;
80     if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
81     int track = -1;
82     if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
83     if (!m_extra.contains("finalfilter")) m_extra.insert("finalfilter", filter);
84
85     if (out != -1 && out <= in) {
86         m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
87         setStatus(JOBCRASHED);
88         return;
89     }
90     Mlt::Producer *prod ;
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 (out == -1) {
99         prod = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
100     }
101     else {
102         Mlt::Producer *tmp = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
103         prod = tmp->cut(in, out);
104         delete tmp;
105     }
106     if (m_extra.contains("producer_profile")) {
107         m_profile->from_producer(*prod);
108         m_profile->set_explicit(true);
109     }
110     QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
111     foreach(const QString &data, list) {
112         if (data.contains('=')) {
113             prod->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
114         }
115     }
116     if (consumer.contains(":")) {
117         m_consumer = new Mlt::Consumer(*m_profile, consumer.section(':', 0, 0).toUtf8().constData(), consumer.section(':', 1).toUtf8().constData());
118     }
119     else {
120         m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
121     }
122     if (!m_consumer || !m_consumer->is_valid()) {
123         m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
124         setStatus(JOBCRASHED);
125         return;
126     }
127
128     //m_consumer->set("terminate_on_pause", 1 );
129     //m_consumer->set("eof", "pause" );
130     m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
131
132
133     list = consumerParams.split(' ', QString::SkipEmptyParts);
134     foreach(const QString &data, list) {
135         if (data.contains('=')) {
136             kDebug()<<"// filter con: "<<data;
137             m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
138         }
139     }
140     
141     Mlt::Filter mltFilter(*m_profile, filter.toUtf8().data());
142     list = filterParams.split(' ', QString::SkipEmptyParts);
143     foreach(const QString &data, list) {
144         if (data.contains('=')) {
145             kDebug()<<"// filter p: "<<data;
146             mltFilter.set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
147         }
148     }
149     Mlt::Tractor tractor;
150     Mlt::Playlist playlist;
151     playlist.append(*prod);
152     tractor.set_track(playlist, 0);
153     m_length = prod->get_length();
154     m_consumer->connect(tractor);
155     prod->set_speed(0);
156     prod->seek(0);
157     prod->attach(mltFilter);
158     m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
159     m_consumer->start();
160     prod->set_speed(1);
161     while (jobStatus != JOBABORTED && !m_consumer->is_stopped()) {
162         
163     }
164     m_consumer->stop();
165     QMap <QString, QString> jobResults;
166     if (m_extra.contains("key"))
167         jobResults.insert(m_extra.value("key"), mltFilter.get(m_extra.value("key").toUtf8().constData()));
168     if (!jobResults.isEmpty() && jobStatus != JOBABORTED) emit gotFilterJobResults(m_clipId, startPos, track, jobResults, m_extra);
169     setStatus(JOBDONE);
170     delete m_consumer;
171     delete prod;
172     return;
173 }
174
175
176 MeltJob::~MeltJob()
177 {
178 }
179
180 const QString MeltJob::destination() const
181 {
182     return m_dest;
183 }
184
185 stringMap MeltJob::cancelProperties()
186 {
187     QMap <QString, QString> props;
188     return props;
189 }
190
191 const QString MeltJob::statusMessage()
192 {
193     QString statusInfo;
194     switch (jobStatus) {
195         case JOBWORKING:
196             statusInfo = description;
197             break;
198         case JOBWAITING:
199             statusInfo = i18n("Waiting to process clip");
200             break;
201         default:
202             break;
203     }
204     return statusInfo;
205 }
206
207 void MeltJob::emitFrameNumber()
208 {
209     if (m_consumer && m_length > 0) {
210         emit jobProgress(m_clipId, (int) (100 * m_consumer->position() / m_length), jobType);
211     }
212 }
213
214 bool MeltJob::isProjectFilter() const
215 {
216     return m_extra.contains("projecttreefilter");
217 }
218
219
220