]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
slideshow update
[kdenlive] / src / docclipbase.cpp
1 /**************************1*************************************************
2                           DocClipBase.cpp  -  description
3                              -------------------
4     begin                : Fri Apr 12 2002
5     copyright            : (C) 2002 by Jason Wood
6     email                : jasonwood@blueyonder.co.uk
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <KDebug>
19
20 #include "kdenlivesettings.h"
21 #include "docclipbase.h"
22 #include "kthumb.h"
23 #include "clipmanager.h"
24
25 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, uint id):
26         m_id(id), m_description(QString()), m_refcount(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL), m_clipProducer(NULL) {
27     int type = xml.attribute("type").toInt();
28     m_clipType = (CLIPTYPE) type;
29     m_name = xml.attribute("name");
30     m_id = id;
31
32     QDomNamedNodeMap attributes = xml.attributes();
33     for (unsigned int i = 0; i < attributes.count(); i++) {
34         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
35     }
36
37     KUrl url = KUrl(xml.attribute("resource"));
38     int out = xml.attribute("out").toInt();
39     if (out != 0) {
40         setDuration(GenTime(out, KdenliveSettings::project_fps()));
41     } else {
42         out = xml.attribute("duration").toInt();
43         if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
44     }
45     if (m_name.isEmpty()) m_name = url.fileName();
46
47     if (!url.isEmpty() && QFile::exists(url.path())) {
48         m_thumbProd = new KThumb(clipManager, url);
49         if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
50     }
51     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
52 }
53
54 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
55     DocClipBase::operator=(clip);
56     m_id = clip.getId();
57     m_clipType = clip.clipType();
58     m_name = clip.name();
59     m_duration = clip.duration();
60     m_audioThumbCreated = clip.audioThumbCreated();
61     m_properties = clip.properties();
62     return *this;
63 }*/
64
65 DocClipBase::~DocClipBase() {
66     if (m_thumbProd) delete m_thumbProd;
67     if (m_clipProducer) delete m_clipProducer;
68 }
69
70 void DocClipBase::slotCreateAudioTimer() {
71     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
72     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
73     m_audioTimer = new QTimer(this);
74     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
75 }
76
77 void DocClipBase::slotRequestAudioThumbs() {
78     emit getAudioThumbs();
79 }
80
81 void DocClipBase::slotClearAudioCache() {
82     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
83     if (m_audioTimer != NULL) m_audioTimer->stop();
84     audioFrameChache.clear();
85     m_audioThumbCreated = false;
86 }
87
88 KThumb *DocClipBase::thumbProducer() {
89     return m_thumbProd;
90 }
91
92 bool DocClipBase::audioThumbCreated() const {
93     return m_audioThumbCreated;
94 }
95
96 void DocClipBase::setName(const QString name) {
97     m_name = name;
98 }
99
100 const QString & DocClipBase::name() const {
101
102     return m_name;
103 }
104
105 uint DocClipBase::getId() const {
106     return m_id;
107 }
108
109 void DocClipBase::setId(const uint &newId) {
110     m_id = newId;
111 }
112
113 const CLIPTYPE & DocClipBase::clipType() const {
114     return m_clipType;
115 }
116
117 void DocClipBase::setClipType(CLIPTYPE type) {
118     m_clipType = type;
119     m_properties.insert("type", QString::number((int) type));
120     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
121         slotCreateAudioTimer();
122 }
123
124 KUrl DocClipBase::fileURL() const {
125     QString res = m_properties.value("resource");
126     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
127     return KUrl();
128 }
129
130 void DocClipBase::setClipThumbFrame(const uint &ix) {
131     m_properties.insert("thumbnail", QString::number((int) ix));
132 }
133
134 uint DocClipBase::getClipThumbFrame() const {
135     return (uint) m_properties.value("thumbnail").toInt();
136 }
137
138 const QString DocClipBase::description() const {
139     return m_properties.value("description");
140 }
141
142 const QString DocClipBase::getProperty(const QString prop) const {
143     return m_properties.value(prop);
144 }
145
146 void DocClipBase::setDuration(GenTime dur) {
147     m_duration = dur;
148     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
149 }
150
151 const GenTime &DocClipBase::duration() const {
152     return m_duration;
153 }
154
155 const GenTime &DocClipBase::maxDuration() const {
156     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
157         const GenTime dur(10000, KdenliveSettings::project_fps());
158         return dur;
159     }
160     return m_duration;
161 }
162
163 bool DocClipBase::hasFileSize() const {
164     return true;
165 }
166
167
168 // virtual
169 QDomElement DocClipBase::toXML() const {
170     QDomDocument doc;
171
172     QDomElement clip = doc.createElement("producer");
173
174     QMapIterator<QString, QString> i(m_properties);
175     while (i.hasNext()) {
176         i.next();
177         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
178     }
179     //doc.appendChild(clip);
180     //kDebug()<<"/// CLIP XML: "<<doc.toString();
181     return clip;
182 }
183
184 DocClipBase *DocClipBase::
185 createClip(KdenliveDoc *doc, const QDomElement & element) {
186     DocClipBase *clip = 0;
187     QString description;
188     QDomNode node = element;
189     node.normalize();
190     if (element.tagName() != "kdenliveclip") {
191         kWarning() <<
192         "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
193         return 0;
194     }
195
196     QDomNode n = element.firstChild();
197
198     while (!n.isNull()) {
199         QDomElement e = n.toElement();
200         if (!e.isNull()) {
201             QString tagName = e.tagName();
202             if (e.tagName() == "avfile") {
203                 // clip = DocClipAVFile::createClip(e);
204             } else if (e.tagName() == "DocTrackBaseList") {
205                 // clip = DocClipProject::createClip(doc, e);
206             }
207         } else {
208             QDomText text = n.toText();
209             if (!text.isNull()) {
210                 description = text.nodeValue();
211             }
212         }
213
214         n = n.nextSibling();
215     }
216     if (clip == 0) {
217         kWarning() << "DocClipBase::createClip() unable to create clip";
218     } else {
219         // setup DocClipBase specifics of the clip.
220         QMap <QString, QString> props;
221         props.insert("description", description);
222         clip->setProperties(props);
223         clip->setAudioThumbCreated(false);
224     }
225     return clip;
226 }
227
228 void DocClipBase::setAudioThumbCreated(bool isDone) {
229     m_audioThumbCreated = isDone;
230 }
231
232
233 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
234     m_thumbnail = pixmap;
235 }
236
237 const QPixmap & DocClipBase::thumbnail() const {
238     return m_thumbnail;
239 }
240
241 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
242     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
243     audioFrameChache = data;
244     m_audioThumbCreated = true;
245     emit gotAudioData();
246 }
247
248 QList < GenTime > DocClipBase::snapMarkers() const {
249     QList < GenTime > markers;
250
251     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
252         markers.append(m_snapMarkers[count].time());
253     }
254
255     return markers;
256 }
257
258 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
259     return m_snapMarkers;
260 }
261
262 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
263     m_snapMarkers = markers;
264 }
265
266 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
267     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
268     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
269         if ((*it).time() >= time)
270             break;
271     }
272
273     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
274         (*it).setComment(comment);
275         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
276     } else {
277         CommentedTime t(time, comment);
278         m_snapMarkers.insert(it, t);
279     }
280
281 }
282
283 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
284     QList < CommentedTime >::Iterator it;
285     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
286         if ((*it).time() == time)
287             break;
288     }
289     if (it != m_snapMarkers.end()) {
290         (*it).setComment(comment);
291     } else {
292         kError() << "trying to edit Snap Marker that does not already exists";
293     }
294 }
295
296 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
297     QString result = i18n("Marker");
298     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
299
300     while (itt != m_snapMarkers.end()) {
301         if ((*itt).time() == time)
302             break;
303         ++itt;
304     }
305
306     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
307         result = (*itt).comment();
308         m_snapMarkers.erase(itt);
309     }
310     return result;
311 }
312
313
314 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
315     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
316
317     while (itt != m_snapMarkers.end()) {
318         if ((*itt).time() == time)
319             return time;
320         ++itt;
321     }
322
323     return GenTime(0.0);
324 }
325
326 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
327     int it;
328     for (it = 0; it < m_snapMarkers.count(); it++) {
329         if (m_snapMarkers[it].time() >= currTime)
330             break;
331     }
332     if (it == 0) return GenTime();
333     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
334         return m_snapMarkers[it].time();
335     else return m_snapMarkers[it-1].time();
336 }
337
338 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
339     int it;
340     for (it = 0; it < m_snapMarkers.count(); it++) {
341         if (m_snapMarkers[it].time() > currTime)
342             break;
343     }
344     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
345     return duration();
346 }
347
348 QString DocClipBase::markerComment(GenTime t) {
349     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
350
351     while (itt != m_snapMarkers.end()) {
352         if ((*itt).time() == t)
353             return (*itt).comment();
354         ++itt;
355     }
356     return QString::null;
357 }
358
359 void DocClipBase::setProducer(Mlt::Producer *producer) {
360     m_clipProducer = producer;
361     if (m_thumbProd) m_thumbProd->setProducer(producer);
362 }
363
364 Mlt::Producer *DocClipBase::producer() {
365     return m_clipProducer;
366 }
367
368 void DocClipBase::slotRefreshProducer() {
369     if (m_clipProducer == NULL) return;
370     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
371     if (m_clipType == SLIDESHOW) {
372         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
373                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
374                delete[] tmp;
375         delete m_clipProducer;
376         m_clipProducer = new Mlt::Producer(producer.get_producer());
377         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
378         m_clipProducer->set("ttl", getProperty("ttl").toInt());
379         //m_clipProducer->set("id", getProperty("id"));
380         if (getProperty("fade") == "1") {
381             // we want a fade filter effect
382             kDebug() << "////////////   FADE WANTED";
383             Mlt::Service clipService(m_clipProducer->get_service());
384             int ct = 0;
385             Mlt::Filter *filter = clipService.filter(ct);
386             while (filter) {
387                 if (filter->get("mlt_service") == "luma") {
388                     break;
389                 }
390                 ct++;
391                 filter = clipService.filter(ct);
392             }
393
394             if (filter && filter->get("mlt_service") == "luma") {
395                 filter->set("period", getProperty("ttl").toInt() - 1);
396                 filter->set("luma.out", getProperty("luma_duration").toInt());
397                 QString resource = getProperty("luma_file");
398                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
399                 filter->set("luma.resource", tmp);
400                 delete[] tmp;
401                 if (getProperty("softness") != QString()) {
402                     int soft = getProperty("softness").toInt();
403                     filter->set("luma.softness", (double) soft / 100.0);
404                 }
405             } else {
406                 // filter does not exist, create it...
407                 Mlt::Filter *filter = new Mlt::Filter(*(m_clipProducer->profile()), "luma");
408                 filter->set("period", getProperty("ttl").toInt() - 1);
409                 filter->set("luma.out", getProperty("luma_duration").toInt());
410                 QString resource = getProperty("luma_file");
411                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
412                 filter->set("luma.resource", tmp);
413                 delete[] tmp;
414                 if (getProperty("softness") != QString()) {
415                     int soft = getProperty("softness").toInt();
416                     filter->set("luma.softness", (double) soft / 100.0);
417                 }
418                 clipService.attach(*filter);
419             }
420         } else {
421             kDebug() << "////////////   FADE NOT WANTED!!!";
422             Mlt::Service clipService(m_clipProducer->get_service());
423             int ct = 0;
424             Mlt::Filter *filter = clipService.filter(0);
425             while (filter) {
426                 if (filter->get("mlt_service") == "luma") {
427                     clipService.detach(*filter);
428                 } else ct++;
429                 filter = clipService.filter(ct);
430             }
431         }
432     }
433 }
434
435 void DocClipBase::setProperties(QMap <QString, QString> properties) {
436     // changing clip type is not allowed
437     properties.remove("type");
438     QMapIterator<QString, QString> i(properties);
439     bool refreshProducer = false;
440     QStringList keys;
441     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
442     while (i.hasNext()) {
443         i.next();
444         m_properties.insert(i.key(), i.value());
445         if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
446         else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
447         else if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
448     }
449     if (refreshProducer) slotRefreshProducer();
450 }
451
452 void DocClipBase::setProperty(QString key, QString value) {
453     m_properties.insert(key, value);
454     if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
455     else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
456 }
457
458 QMap <QString, QString> DocClipBase::properties() const {
459     return m_properties;
460 }
461
462 void DocClipBase::slotGetAudioThumbs() {
463     if (m_thumbProd == NULL) return;
464     if (m_audioThumbCreated) {
465         if (m_audioTimer != NULL)
466             m_audioTimer->stop();
467     } else {
468         if (m_audioTimer != NULL)
469             m_audioTimer->start(2000);
470         double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
471         m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
472     }
473 }
474
475
476