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