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