]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
* Fix crash when using razor tool while playing
[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, const QString &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), m_properties(QMap <QString, QString> ()) {
27     int type = xml.attribute("type").toInt();
28     m_clipType = (CLIPTYPE) type;
29     m_name = xml.attribute("name");
30
31     QDomNamedNodeMap attributes = xml.attributes();
32     for (unsigned int i = 0; i < attributes.count(); i++) {
33         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
34     }
35
36     KUrl url = KUrl(xml.attribute("resource"));
37     int out = xml.attribute("out").toInt();
38     if (out != 0) {
39         setDuration(GenTime(out, KdenliveSettings::project_fps()));
40     } else {
41         out = xml.attribute("duration").toInt();
42         if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
43     }
44     if (m_name.isEmpty()) m_name = url.fileName();
45
46     //if (!url.isEmpty() && QFile::exists(url.path()))
47     {
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 const QString &DocClipBase::getId() const {
106     return m_id;
107 }
108
109 void DocClipBase::setId(const QString &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 bool DocClipBase::isTransparent() const {
143     return (m_properties.value("transparency") == "1");
144 }
145
146 const QString DocClipBase::getProperty(const QString prop) const {
147     return m_properties.value(prop);
148 }
149
150 void DocClipBase::setDuration(GenTime dur) {
151     m_duration = dur;
152     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
153 }
154
155 const GenTime &DocClipBase::duration() const {
156     return m_duration;
157 }
158
159 const GenTime &DocClipBase::maxDuration() const {
160     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
161         const GenTime dur(10000, KdenliveSettings::project_fps());
162         return dur;
163     }
164     return m_duration;
165 }
166
167 bool DocClipBase::hasFileSize() const {
168     return true;
169 }
170
171
172 // virtual
173 QDomElement DocClipBase::toXML() const {
174     QDomDocument doc;
175
176     QDomElement clip = doc.createElement("producer");
177
178     QMapIterator<QString, QString> i(m_properties);
179     while (i.hasNext()) {
180         i.next();
181         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
182     }
183     //doc.appendChild(clip);
184     //kDebug()<<"/// CLIP XML: "<<doc.toString();
185     return clip;
186 }
187
188 DocClipBase *DocClipBase::
189 createClip(KdenliveDoc *doc, const QDomElement & element) {
190     DocClipBase *clip = 0;
191     QString description;
192     QDomNode node = element;
193     node.normalize();
194     if (element.tagName() != "kdenliveclip") {
195         kWarning() <<
196         "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
197         return 0;
198     }
199
200     QDomNode n = element.firstChild();
201
202     while (!n.isNull()) {
203         QDomElement e = n.toElement();
204         if (!e.isNull()) {
205             QString tagName = e.tagName();
206             if (e.tagName() == "avfile") {
207                 // clip = DocClipAVFile::createClip(e);
208             } else if (e.tagName() == "DocTrackBaseList") {
209                 // clip = DocClipProject::createClip(doc, e);
210             }
211         } else {
212             QDomText text = n.toText();
213             if (!text.isNull()) {
214                 description = text.nodeValue();
215             }
216         }
217
218         n = n.nextSibling();
219     }
220     if (clip == 0) {
221         kWarning() << "DocClipBase::createClip() unable to create clip";
222     } else {
223         // setup DocClipBase specifics of the clip.
224         QMap <QString, QString> props;
225         props.insert("description", description);
226         clip->setProperties(props);
227         clip->setAudioThumbCreated(false);
228     }
229     return clip;
230 }
231
232 void DocClipBase::setAudioThumbCreated(bool isDone) {
233     m_audioThumbCreated = isDone;
234 }
235
236
237 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
238     m_thumbnail = pixmap;
239 }
240
241 const QPixmap & DocClipBase::thumbnail() const {
242     return m_thumbnail;
243 }
244
245 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
246     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
247     audioFrameChache = data;
248     m_audioThumbCreated = true;
249     emit gotAudioData();
250 }
251
252 QList < GenTime > DocClipBase::snapMarkers() const {
253     QList < GenTime > markers;
254
255     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
256         markers.append(m_snapMarkers[count].time());
257     }
258
259     return markers;
260 }
261
262 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
263     return m_snapMarkers;
264 }
265
266 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
267     m_snapMarkers = markers;
268 }
269
270 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
271     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
272     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
273         if ((*it).time() >= time)
274             break;
275     }
276
277     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
278         (*it).setComment(comment);
279         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
280     } else {
281         CommentedTime t(time, comment);
282         m_snapMarkers.insert(it, t);
283     }
284
285 }
286
287 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
288     QList < CommentedTime >::Iterator it;
289     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
290         if ((*it).time() == time)
291             break;
292     }
293     if (it != m_snapMarkers.end()) {
294         (*it).setComment(comment);
295     } else {
296         kError() << "trying to edit Snap Marker that does not already exists";
297     }
298 }
299
300 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
301     QString result = i18n("Marker");
302     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
303
304     while (itt != m_snapMarkers.end()) {
305         if ((*itt).time() == time)
306             break;
307         ++itt;
308     }
309
310     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
311         result = (*itt).comment();
312         m_snapMarkers.erase(itt);
313     }
314     return result;
315 }
316
317
318 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
319     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
320
321     while (itt != m_snapMarkers.end()) {
322         if ((*itt).time() == time)
323             return time;
324         ++itt;
325     }
326
327     return GenTime(0.0);
328 }
329
330 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
331     int it;
332     for (it = 0; it < m_snapMarkers.count(); it++) {
333         if (m_snapMarkers[it].time() >= currTime)
334             break;
335     }
336     if (it == 0) return GenTime();
337     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
338         return m_snapMarkers[it].time();
339     else return m_snapMarkers[it-1].time();
340 }
341
342 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
343     int it;
344     for (it = 0; it < m_snapMarkers.count(); it++) {
345         if (m_snapMarkers[it].time() > currTime)
346             break;
347     }
348     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
349     return duration();
350 }
351
352 QString DocClipBase::markerComment(GenTime t) {
353     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
354
355     while (itt != m_snapMarkers.end()) {
356         if ((*itt).time() == t)
357             return (*itt).comment();
358         ++itt;
359     }
360     return QString::null;
361 }
362
363 void DocClipBase::setProducer(Mlt::Producer *producer) {
364     if (producer == NULL) return;
365     m_clipProducer = producer;
366     m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
367     if (m_thumbProd) m_thumbProd->setProducer(producer);
368 }
369
370 Mlt::Producer *DocClipBase::producer() {
371     return m_clipProducer;
372 }
373
374 void DocClipBase::slotRefreshProducer() {
375     if (m_clipProducer == NULL) return;
376     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
377     if (m_clipType == SLIDESHOW) {
378         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
379                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
380                delete[] tmp;
381         delete m_clipProducer;
382         m_clipProducer = new Mlt::Producer(producer.get_producer());
383         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
384         m_clipProducer->set("ttl", getProperty("ttl").toInt());
385         //m_clipProducer->set("id", getProperty("id"));
386         if (getProperty("fade") == "1") {
387             // we want a fade filter effect
388             kDebug() << "////////////   FADE WANTED";
389             Mlt::Service clipService(m_clipProducer->get_service());
390             int ct = 0;
391             Mlt::Filter *filter = clipService.filter(ct);
392             while (filter) {
393                 if (filter->get("mlt_service") == "luma") {
394                     break;
395                 }
396                 ct++;
397                 filter = clipService.filter(ct);
398             }
399
400             if (filter && filter->get("mlt_service") == "luma") {
401                 filter->set("period", getProperty("ttl").toInt() - 1);
402                 filter->set("luma.out", getProperty("luma_duration").toInt());
403                 QString resource = getProperty("luma_file");
404                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
405                 filter->set("luma.resource", tmp);
406                 delete[] tmp;
407                 if (getProperty("softness") != QString()) {
408                     int soft = getProperty("softness").toInt();
409                     filter->set("luma.softness", (double) soft / 100.0);
410                 }
411             } else {
412                 // filter does not exist, create it...
413                 Mlt::Filter *filter = new Mlt::Filter(*(m_clipProducer->profile()), "luma");
414                 filter->set("period", getProperty("ttl").toInt() - 1);
415                 filter->set("luma.out", getProperty("luma_duration").toInt());
416                 QString resource = getProperty("luma_file");
417                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
418                 filter->set("luma.resource", tmp);
419                 delete[] tmp;
420                 if (getProperty("softness") != QString()) {
421                     int soft = getProperty("softness").toInt();
422                     filter->set("luma.softness", (double) soft / 100.0);
423                 }
424                 clipService.attach(*filter);
425             }
426         } else {
427             kDebug() << "////////////   FADE NOT WANTED!!!";
428             Mlt::Service clipService(m_clipProducer->get_service());
429             int ct = 0;
430             Mlt::Filter *filter = clipService.filter(0);
431             while (filter) {
432                 if (filter->get("mlt_service") == "luma") {
433                     clipService.detach(*filter);
434                 } else ct++;
435                 filter = clipService.filter(ct);
436             }
437         }
438     }
439 }
440
441 void DocClipBase::setProperties(QMap <QString, QString> properties) {
442     // changing clip type is not allowed
443     properties.remove("type");
444     QMapIterator<QString, QString> i(properties);
445     bool refreshProducer = false;
446     QStringList keys;
447     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
448     while (i.hasNext()) {
449         i.next();
450         m_properties.insert(i.key(), i.value());
451         if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
452         else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
453         else if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
454         else if (i.key() == "transparency") m_clipProducer->set("transparency", i.value().toInt());
455         else if (i.key() == "colour") {
456             char *tmp = (char *) qstrdup(i.value().toUtf8().data());
457             m_clipProducer->set("colour", tmp);
458             delete[] tmp;
459         } else if (i.key() == "xmldata") {
460             m_clipProducer->set("force_reload", 1);
461         }
462     }
463     if (refreshProducer) slotRefreshProducer();
464 }
465
466 void DocClipBase::clearProperty(const QString &key) {
467     m_properties.remove(key);
468 }
469
470 void DocClipBase::setProperty(const QString &key, const QString &value) {
471     m_properties.insert(key, value);
472     if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
473     else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
474     else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
475     else if (key == "colour") {
476         char *tmp = (char *) qstrdup(value.toUtf8().data());
477         m_clipProducer->set("colour", tmp);
478         delete[] tmp;
479     } else if (key == "xmldata") {
480         m_clipProducer->set("force_reload", 1);
481     }
482 }
483
484 QMap <QString, QString> DocClipBase::properties() const {
485     return m_properties;
486 }
487
488 void DocClipBase::slotGetAudioThumbs() {
489     if (m_thumbProd == NULL) return;
490     if (m_audioThumbCreated) {
491         if (m_audioTimer != NULL)
492             m_audioTimer->stop();
493     } else {
494         if (m_audioTimer != NULL)
495             m_audioTimer->start(2000);
496         double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
497         m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
498     }
499 }
500
501
502