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