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, 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");
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());
37 KUrl url = KUrl(xml.attribute("resource"));
38 int out = xml.attribute("out").toInt();
40 setDuration(GenTime(out, KdenliveSettings::project_fps()));
42 out = xml.attribute("duration").toInt();
43 if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
45 if (m_name.isEmpty()) m_name = url.fileName();
47 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 uint DocClipBase::getId() const {
109 void DocClipBase::setId(const uint &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 const QString DocClipBase::getProperty(const QString prop) const {
143 return m_properties.value(prop);
146 void DocClipBase::setDuration(GenTime dur) {
148 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
151 const GenTime &DocClipBase::duration() const {
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());
163 bool DocClipBase::hasFileSize() const {
169 QDomElement DocClipBase::toXML() const {
172 QDomElement clip = doc.createElement("producer");
174 QMapIterator<QString, QString> i(m_properties);
175 while (i.hasNext()) {
177 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
179 //doc.appendChild(clip);
180 //kDebug()<<"/// CLIP XML: "<<doc.toString();
184 DocClipBase *DocClipBase::
185 createClip(KdenliveDoc *doc, const QDomElement & element) {
186 DocClipBase *clip = 0;
188 QDomNode node = element;
190 if (element.tagName() != "kdenliveclip") {
192 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
196 QDomNode n = element.firstChild();
198 while (!n.isNull()) {
199 QDomElement e = n.toElement();
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);
208 QDomText text = n.toText();
209 if (!text.isNull()) {
210 description = text.nodeValue();
217 kWarning() << "DocClipBase::createClip() unable to create clip";
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);
228 void DocClipBase::setAudioThumbCreated(bool isDone) {
229 m_audioThumbCreated = isDone;
233 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
234 m_thumbnail = pixmap;
237 const QPixmap & DocClipBase::thumbnail() const {
241 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
242 kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
243 audioFrameChache = data;
244 m_audioThumbCreated = true;
248 QList < GenTime > DocClipBase::snapMarkers() const {
249 QList < GenTime > markers;
251 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
252 markers.append(m_snapMarkers[count].time());
258 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
259 return m_snapMarkers;
262 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
263 m_snapMarkers = markers;
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)
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";
277 CommentedTime t(time, comment);
278 m_snapMarkers.insert(it, t);
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)
289 if (it != m_snapMarkers.end()) {
290 (*it).setComment(comment);
292 kError() << "trying to edit Snap Marker that does not already exists";
296 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
297 QString result = i18n("Marker");
298 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
300 while (itt != m_snapMarkers.end()) {
301 if ((*itt).time() == time)
306 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
307 result = (*itt).comment();
308 m_snapMarkers.erase(itt);
314 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
315 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
317 while (itt != m_snapMarkers.end()) {
318 if ((*itt).time() == time)
326 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
328 for (it = 0; it < m_snapMarkers.count(); it++) {
329 if (m_snapMarkers[it].time() >= currTime)
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();
338 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
340 for (it = 0; it < m_snapMarkers.count(); it++) {
341 if (m_snapMarkers[it].time() > currTime)
344 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
348 QString DocClipBase::markerComment(GenTime t) {
349 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
351 while (itt != m_snapMarkers.end()) {
352 if ((*itt).time() == t)
353 return (*itt).comment();
356 return QString::null;
359 void DocClipBase::setProducer(Mlt::Producer *producer) {
360 m_clipProducer = producer;
361 if (m_thumbProd) m_thumbProd->setProducer(producer);
364 Mlt::Producer *DocClipBase::producer() {
365 return m_clipProducer;
368 void DocClipBase::setProperties(QMap <QString, QString> properties) {
369 // changing clip type is not allowed
370 properties.remove("type");
371 QMapIterator<QString, QString> i(properties);
372 while (i.hasNext()) {
374 m_properties.insert(i.key(), i.value());
375 if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
376 else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
380 void DocClipBase::setProperty(QString key, QString value) {
381 m_properties.insert(key, value);
382 if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
383 else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
386 QMap <QString, QString> DocClipBase::properties() const {
390 void DocClipBase::slotGetAudioThumbs() {
391 if (m_thumbProd == NULL) return;
392 if (m_audioThumbCreated) {
393 if (m_audioTimer != NULL)
394 m_audioTimer->stop();
396 if (m_audioTimer != NULL)
397 m_audioTimer->start(2000);
398 double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
399 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);