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