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