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())) {
47 m_thumbProd = new KThumb(clipManager, url);
48 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
50 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
53 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
54 DocClipBase::operator=(clip);
56 m_clipType = clip.clipType();
58 m_duration = clip.duration();
59 m_audioThumbCreated = clip.audioThumbCreated();
60 m_properties = clip.properties();
64 DocClipBase::~DocClipBase() {
65 if (m_thumbProd) delete m_thumbProd;
66 if (m_clipProducer) delete m_clipProducer;
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()));
76 void DocClipBase::slotRequestAudioThumbs() {
77 emit getAudioThumbs();
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;
87 KThumb *DocClipBase::thumbProducer() {
91 bool DocClipBase::audioThumbCreated() const {
92 return m_audioThumbCreated;
95 void DocClipBase::setName(const QString name) {
99 const QString & DocClipBase::name() const {
104 const QString &DocClipBase::getId() const {
108 void DocClipBase::setId(const QString &newId) {
112 const CLIPTYPE & DocClipBase::clipType() const {
116 void DocClipBase::setClipType(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();
123 KUrl DocClipBase::fileURL() const {
124 QString res = m_properties.value("resource");
125 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
129 void DocClipBase::setClipThumbFrame(const uint &ix) {
130 m_properties.insert("thumbnail", QString::number((int) ix));
133 uint DocClipBase::getClipThumbFrame() const {
134 return (uint) m_properties.value("thumbnail").toInt();
137 const QString DocClipBase::description() const {
138 return m_properties.value("description");
141 bool DocClipBase::isTransparent() const {
142 return (m_properties.value("transparency") == "1");
145 const QString DocClipBase::getProperty(const QString prop) const {
146 return m_properties.value(prop);
149 void DocClipBase::setDuration(GenTime dur) {
151 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
154 const GenTime &DocClipBase::duration() const {
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());
166 bool DocClipBase::hasFileSize() const {
172 QDomElement DocClipBase::toXML() const {
175 QDomElement clip = doc.createElement("producer");
177 QMapIterator<QString, QString> i(m_properties);
178 while (i.hasNext()) {
180 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
182 //doc.appendChild(clip);
183 //kDebug()<<"/// CLIP XML: "<<doc.toString();
187 DocClipBase *DocClipBase::
188 createClip(KdenliveDoc *doc, const QDomElement & element) {
189 DocClipBase *clip = 0;
191 QDomNode node = element;
193 if (element.tagName() != "kdenliveclip") {
195 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
199 QDomNode n = element.firstChild();
201 while (!n.isNull()) {
202 QDomElement e = n.toElement();
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);
211 QDomText text = n.toText();
212 if (!text.isNull()) {
213 description = text.nodeValue();
220 kWarning() << "DocClipBase::createClip() unable to create clip";
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);
231 void DocClipBase::setAudioThumbCreated(bool isDone) {
232 m_audioThumbCreated = isDone;
236 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
237 m_thumbnail = pixmap;
240 const QPixmap & DocClipBase::thumbnail() const {
244 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
245 kDebug() << "CLIPBASE RECIEDVEDÂ AUDIOÂ DATA*********************************************";
246 audioFrameChache = data;
247 m_audioThumbCreated = true;
251 QList < GenTime > DocClipBase::snapMarkers() const {
252 QList < GenTime > markers;
254 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
255 markers.append(m_snapMarkers[count].time());
261 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
262 return m_snapMarkers;
265 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
266 m_snapMarkers = markers;
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)
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";
280 CommentedTime t(time, comment);
281 m_snapMarkers.insert(it, t);
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)
292 if (it != m_snapMarkers.end()) {
293 (*it).setComment(comment);
295 kError() << "trying to edit Snap Marker that does not already exists";
299 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
300 QString result = i18n("Marker");
301 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
303 while (itt != m_snapMarkers.end()) {
304 if ((*itt).time() == time)
309 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
310 result = (*itt).comment();
311 m_snapMarkers.erase(itt);
317 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
318 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
320 while (itt != m_snapMarkers.end()) {
321 if ((*itt).time() == time)
329 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
331 for (it = 0; it < m_snapMarkers.count(); it++) {
332 if (m_snapMarkers[it].time() >= currTime)
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();
341 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
343 for (it = 0; it < m_snapMarkers.count(); it++) {
344 if (m_snapMarkers[it].time() > currTime)
347 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
351 QString DocClipBase::markerComment(GenTime t) {
352 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
354 while (itt != m_snapMarkers.end()) {
355 if ((*itt).time() == t)
356 return (*itt).comment();
359 return QString::null;
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);
368 Mlt::Producer *DocClipBase::producer() {
369 return m_clipProducer;
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);
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());
389 Mlt::Filter *filter = clipService.filter(ct);
391 if (filter->get("mlt_service") == "luma") {
395 filter = clipService.filter(ct);
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);
405 if (getProperty("softness") != QString()) {
406 int soft = getProperty("softness").toInt();
407 filter->set("luma.softness", (double) soft / 100.0);
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);
418 if (getProperty("softness") != QString()) {
419 int soft = getProperty("softness").toInt();
420 filter->set("luma.softness", (double) soft / 100.0);
422 clipService.attach(*filter);
425 kDebug() << "//////////// FADE NOT WANTED!!!";
426 Mlt::Service clipService(m_clipProducer->get_service());
428 Mlt::Filter *filter = clipService.filter(0);
430 if (filter->get("mlt_service") == "luma") {
431 clipService.detach(*filter);
433 filter = clipService.filter(ct);
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;
445 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
446 while (i.hasNext()) {
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());
454 if (refreshProducer) slotRefreshProducer();
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());
464 QMap <QString, QString> DocClipBase::properties() const {
468 void DocClipBase::slotGetAudioThumbs() {
469 if (m_thumbProd == NULL) return;
470 if (m_audioThumbCreated) {
471 if (m_audioTimer != NULL)
472 m_audioTimer->stop();
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);