1 /**************************1*************************************************
2 DocClipBase.cpp - description
4 begin : Fri Apr 12 2002
5 copyright : (C) 2002 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
20 #include "kdenlivesettings.h"
21 #include "docclipbase.h"
23 #include "clipmanager.h"
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");
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());
36 KUrl url = KUrl(xml.attribute("resource"));
37 int out = xml.attribute("out").toInt();
39 setDuration(GenTime(out, KdenliveSettings::project_fps()));
41 out = xml.attribute("duration").toInt();
42 if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
44 if (m_name.isEmpty()) m_name = url.fileName();
46 //if (!url.isEmpty() && QFile::exists(url.path()))
48 m_thumbProd = new KThumb(clipManager, url);
49 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
51 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
54 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
55 DocClipBase::operator=(clip);
57 m_clipType = clip.clipType();
59 m_duration = clip.duration();
60 m_audioThumbCreated = clip.audioThumbCreated();
61 m_properties = clip.properties();
65 DocClipBase::~DocClipBase() {
66 if (m_thumbProd) delete m_thumbProd;
67 if (m_clipProducer) delete m_clipProducer;
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()));
77 void DocClipBase::slotRequestAudioThumbs() {
78 emit getAudioThumbs();
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;
88 KThumb *DocClipBase::thumbProducer() {
92 bool DocClipBase::audioThumbCreated() const {
93 return m_audioThumbCreated;
96 void DocClipBase::setName(const QString name) {
100 const QString & DocClipBase::name() const {
105 const QString &DocClipBase::getId() const {
109 void DocClipBase::setId(const QString &newId) {
113 const CLIPTYPE & DocClipBase::clipType() const {
117 void DocClipBase::setClipType(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();
124 KUrl DocClipBase::fileURL() const {
125 QString res = m_properties.value("resource");
126 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
130 void DocClipBase::setClipThumbFrame(const uint &ix) {
131 m_properties.insert("thumbnail", QString::number((int) ix));
134 uint DocClipBase::getClipThumbFrame() const {
135 return (uint) m_properties.value("thumbnail").toInt();
138 const QString DocClipBase::description() const {
139 return m_properties.value("description");
142 bool DocClipBase::isTransparent() const {
143 return (m_properties.value("transparency") == "1");
146 const QString DocClipBase::getProperty(const QString prop) const {
147 return m_properties.value(prop);
150 void DocClipBase::setDuration(GenTime dur) {
152 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
155 const GenTime &DocClipBase::duration() const {
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());
167 bool DocClipBase::hasFileSize() const {
173 QDomElement DocClipBase::toXML() const {
176 QDomElement clip = doc.createElement("producer");
178 QMapIterator<QString, QString> i(m_properties);
179 while (i.hasNext()) {
181 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
183 //doc.appendChild(clip);
184 //kDebug()<<"/// CLIP XML: "<<doc.toString();
188 DocClipBase *DocClipBase::
189 createClip(KdenliveDoc *doc, const QDomElement & element) {
190 DocClipBase *clip = 0;
192 QDomNode node = element;
194 if (element.tagName() != "kdenliveclip") {
196 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
200 QDomNode n = element.firstChild();
202 while (!n.isNull()) {
203 QDomElement e = n.toElement();
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);
212 QDomText text = n.toText();
213 if (!text.isNull()) {
214 description = text.nodeValue();
221 kWarning() << "DocClipBase::createClip() unable to create clip";
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);
232 void DocClipBase::setAudioThumbCreated(bool isDone) {
233 m_audioThumbCreated = isDone;
237 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
238 m_thumbnail = pixmap;
241 const QPixmap & DocClipBase::thumbnail() const {
245 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
246 kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
247 audioFrameChache = data;
248 m_audioThumbCreated = true;
252 QList < GenTime > DocClipBase::snapMarkers() const {
253 QList < GenTime > markers;
255 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
256 markers.append(m_snapMarkers[count].time());
262 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
263 return m_snapMarkers;
266 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
267 m_snapMarkers = markers;
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)
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";
281 CommentedTime t(time, comment);
282 m_snapMarkers.insert(it, t);
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)
293 if (it != m_snapMarkers.end()) {
294 (*it).setComment(comment);
296 kError() << "trying to edit Snap Marker that does not already exists";
300 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
301 QString result = i18n("Marker");
302 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
304 while (itt != m_snapMarkers.end()) {
305 if ((*itt).time() == time)
310 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
311 result = (*itt).comment();
312 m_snapMarkers.erase(itt);
318 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
319 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
321 while (itt != m_snapMarkers.end()) {
322 if ((*itt).time() == time)
330 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
332 for (it = 0; it < m_snapMarkers.count(); it++) {
333 if (m_snapMarkers[it].time() >= currTime)
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();
342 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
344 for (it = 0; it < m_snapMarkers.count(); it++) {
345 if (m_snapMarkers[it].time() > currTime)
348 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
352 QString DocClipBase::markerComment(GenTime t) {
353 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
355 while (itt != m_snapMarkers.end()) {
356 if ((*itt).time() == t)
357 return (*itt).comment();
360 return QString::null;
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);
370 Mlt::Producer *DocClipBase::producer() {
371 return m_clipProducer;
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);
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());
391 Mlt::Filter *filter = clipService.filter(ct);
393 if (filter->get("mlt_service") == "luma") {
397 filter = clipService.filter(ct);
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);
407 if (getProperty("softness") != QString()) {
408 int soft = getProperty("softness").toInt();
409 filter->set("luma.softness", (double) soft / 100.0);
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);
420 if (getProperty("softness") != QString()) {
421 int soft = getProperty("softness").toInt();
422 filter->set("luma.softness", (double) soft / 100.0);
424 clipService.attach(*filter);
427 kDebug() << "//////////// FADE NOT WANTED!!!";
428 Mlt::Service clipService(m_clipProducer->get_service());
430 Mlt::Filter *filter = clipService.filter(0);
432 if (filter->get("mlt_service") == "luma") {
433 clipService.detach(*filter);
435 filter = clipService.filter(ct);
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;
447 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
448 while (i.hasNext()) {
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);
459 } else if (i.key() == "xmldata") {
460 m_clipProducer->set("force_reload", 1);
463 if (refreshProducer) slotRefreshProducer();
466 void DocClipBase::setProperty(QString key, QString value) {
467 m_properties.insert(key, value);
468 if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
469 else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
470 else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
471 else if (key == "colour") {
472 char *tmp = (char *) qstrdup(value.toUtf8().data());
473 m_clipProducer->set("colour", tmp);
475 } else if (key == "xmldata") {
476 m_clipProducer->set("force_reload", 1);
480 QMap <QString, QString> DocClipBase::properties() const {
484 void DocClipBase::slotGetAudioThumbs() {
485 if (m_thumbProd == NULL) return;
486 if (m_audioThumbCreated) {
487 if (m_audioTimer != NULL)
488 m_audioTimer->stop();
490 if (m_audioTimer != NULL)
491 m_audioTimer->start(2000);
492 double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
493 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);