]> git.sesse.net Git - kdenlive/blobdiff - src/docclipbase.cpp
Clean up timecode handling, improves:
[kdenlive] / src / docclipbase.cpp
index 228af6e756385cd7900d2ec768ef5be42f7290ab..f22410f1d237f091fea6640e1f10407919507f5d 100644 (file)
 #include "kthumb.h"
 #include "clipmanager.h"
 
+#include <KIO/NetAccess>
 #include <KDebug>
 
 #include <QCryptographicHash>
 
 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
-        audioFrameChache(),
-        m_description(),
+        QObject(),
+        m_audioFrameCache(),
         m_refcount(0),
         m_baseTrackProducers(),
         m_audioTrackProducers(),
@@ -46,13 +47,14 @@ DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QStrin
         m_thumbProd(NULL),
         m_audioThumbCreated(false),
         m_id(id),
+        m_placeHolder(xml.hasAttribute("placeholder")),
         m_properties()
 {
     int type = xml.attribute("type").toInt();
     m_clipType = (CLIPTYPE) type;
-
+    if (m_placeHolder) xml.removeAttribute("placeholder");
     QDomNamedNodeMap attributes = xml.attributes();
-    for (unsigned int i = 0; i < attributes.count(); i++) {
+    for (int i = 0; i < attributes.count(); i++) {
         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
     }
 
@@ -90,15 +92,20 @@ DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QStrin
 
 DocClipBase::~DocClipBase()
 {
-    if (m_thumbProd) {
-        delete m_thumbProd;
-    }
+    kDebug() << "CLIP " << m_id << " DELETED******************************";
+    delete m_thumbProd;
     if (m_audioTimer) {
         m_audioTimer->stop();
         delete m_audioTimer;
     }
+    /*kDebug() <<" * * *CNT "<<m_baseTrackProducers.count();
+    if (m_baseTrackProducers.count() > 0) kDebug()<<"YOYO: "<<m_baseTrackProducers.at(0)->get_out()<<", CUT: "<<m_baseTrackProducers.at(0)->is_cut();*/
     qDeleteAll(m_baseTrackProducers);
     m_baseTrackProducers.clear();
+    qDeleteAll(m_audioTrackProducers);
+    m_audioTrackProducers.clear();
+    delete m_videoOnlyProducer;
+    m_videoOnlyProducer = NULL;
 }
 
 void DocClipBase::setZone(QPoint zone)
@@ -132,7 +139,7 @@ void DocClipBase::slotClearAudioCache()
 {
     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
     if (m_audioTimer != NULL) m_audioTimer->stop();
-    audioFrameChache.clear();
+    m_audioFrameCache.clear();
     m_audioThumbCreated = false;
 }
 
@@ -226,8 +233,9 @@ const GenTime &DocClipBase::duration() const
 const GenTime DocClipBase::maxDuration() const
 {
     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
-        const GenTime dur(15000, KdenliveSettings::project_fps());
-        return dur;
+        /*const GenTime dur(15000, KdenliveSettings::project_fps());
+        return dur;*/
+        return GenTime();
     }
     return m_duration;
 }
@@ -250,7 +258,7 @@ QDomElement DocClipBase::toXML() const
         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
     }
     doc.appendChild(clip);
-    //kDebug()<<"/// CLIP XML: "<<doc.toString();
+    //kDebug() << "/// CLIP XML: " << doc.toString();
     return doc.documentElement();
 }
 
@@ -274,7 +282,7 @@ const QPixmap & DocClipBase::thumbnail() const
 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
 {
     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
-    audioFrameChache = data;
+    m_audioFrameCache = data;
     m_audioThumbCreated = true;
     emit gotAudioData();
 }
@@ -283,7 +291,7 @@ QList < GenTime > DocClipBase::snapMarkers() const
 {
     QList < GenTime > markers;
 
-    for (uint count = 0; count < m_snapMarkers.count(); ++count) {
+    for (int count = 0; count < m_snapMarkers.count(); ++count) {
         markers.append(m_snapMarkers[count].time());
     }
 
@@ -405,11 +413,19 @@ void DocClipBase::deleteProducers()
     qDeleteAll(m_baseTrackProducers);
     m_baseTrackProducers.clear();
     if (m_thumbProd) m_thumbProd->clearProducer();
+    qDeleteAll(m_audioTrackProducers);
+    m_audioTrackProducers.clear();
+    delete m_videoOnlyProducer;
+    m_videoOnlyProducer = NULL;
 }
 
-void DocClipBase::setProducer(Mlt::Producer *producer)
+void DocClipBase::setProducer(Mlt::Producer *producer, bool reset)
 {
     if (producer == NULL) return;
+    if (reset) {
+        // Clear all previous producers
+        deleteProducers();
+    }
     QString id = producer->get("id");
     if (id.contains('_')) {
         // this is a subtrack producer, insert it at correct place
@@ -441,7 +457,7 @@ void DocClipBase::setProducer(Mlt::Producer *producer)
     }
     //m_clipProducer = producer;
     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
-    if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
+    if (m_thumbProd && (reset || !m_thumbProd->hasProducer())) m_thumbProd->setProducer(producer);
 }
 
 Mlt::Producer *DocClipBase::audioProducer(int track)
@@ -506,8 +522,16 @@ Mlt::Producer *DocClipBase::producer(int track)
         int i;
         for (i = 0; i < m_baseTrackProducers.count(); i++)
             if (m_baseTrackProducers.at(i) != NULL) break;
+
         if (i >= m_baseTrackProducers.count()) return NULL;
-        m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
+
+        if (KIO::NetAccess::exists(KUrl(m_baseTrackProducers.at(i)->get("resource")), KIO::NetAccess::SourceSide, 0))
+            m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
+        else { // special case for placeholder clips
+            m_baseTrackProducers[track] = NULL;
+            return NULL;
+        }
+
         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
@@ -531,6 +555,14 @@ void DocClipBase::setProducerProperty(const char *name, int data)
     }
 }
 
+void DocClipBase::setProducerProperty(const char *name, double data)
+{
+    for (int i = 0; i < m_baseTrackProducers.count(); i++) {
+        if (m_baseTrackProducers.at(i) != NULL)
+            m_baseTrackProducers[i]->set(name, data);
+    }
+}
+
 void DocClipBase::setProducerProperty(const char *name, const char *data)
 {
     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
@@ -570,14 +602,14 @@ void DocClipBase::slotRefreshProducer()
             int ct = 0;
             Mlt::Filter *filter = clipService.filter(ct);
             while (filter) {
-                if (filter->get("mlt_service") == "luma") {
+                if (strcmp(filter->get("mlt_service"), "luma") == 0) {
                     break;
                 }
                 ct++;
                 filter = clipService.filter(ct);
             }
 
-            if (filter && filter->get("mlt_service") == "luma") {
+            if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
                 filter->set("period", getProperty("ttl").toInt() - 1);
                 filter->set("luma.out", getProperty("luma_duration").toInt());
                 QString resource = getProperty("luma_file");
@@ -609,7 +641,7 @@ void DocClipBase::slotRefreshProducer()
             int ct = 0;
             Mlt::Filter *filter = clipService.filter(0);
             while (filter) {
-                if (filter->get("mlt_service") == "luma") {
+                if (strcmp(filter->get("mlt_service"), "luma") == 0) {
                     clipService.detach(*filter);
                 } else ct++;
                 filter = clipService.filter(ct);
@@ -671,7 +703,6 @@ void DocClipBase::getFileHash(const QString url)
         file.close();
         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
         m_properties.insert("file_hash", QString(fileHash.toHex()));
-        //kDebug() << file.fileName() << file.size() << fileHash.toHex();
     }
 }
 
@@ -684,6 +715,29 @@ QString DocClipBase::getClipHash() const
     return hash;
 }
 
+// static
+QString DocClipBase::getHash(const QString &path)
+{
+    QFile file(path);
+    if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
+        QByteArray fileData;
+        QByteArray fileHash;
+        /*
+               * 1 MB = 1 second per 450 files (or faster)
+               * 10 MB = 9 seconds per 450 files (or faster)
+               */
+        if (file.size() > 1000000*2) {
+            fileData = file.read(1000000);
+            if (file.seek(file.size() - 1000000))
+                fileData.append(file.readAll());
+        } else
+            fileData = file.readAll();
+        file.close();
+        return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
+    }
+    return QString();
+}
+
 void DocClipBase::refreshThumbUrl()
 {
     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
@@ -701,7 +755,15 @@ void DocClipBase::setProperty(const QString &key, const QString &value)
         char *tmp = (char *) qstrdup(value.toUtf8().data());
         setProducerProperty("colour", tmp);
         delete[] tmp;
+    } else if (key == "templatetext") {
+        char *tmp = (char *) qstrdup(value.toUtf8().data());
+        setProducerProperty("templatetext", tmp);
+        delete[] tmp;
+        setProducerProperty("force_reload", 1);
     } else if (key == "xmldata") {
+        char *tmp = (char *) qstrdup(value.toUtf8().data());
+        setProducerProperty("xmldata", tmp);
+        delete[] tmp;
         setProducerProperty("force_reload", 1);
     } else if (key == "force_aspect_ratio") {
         if (value.isEmpty()) {
@@ -749,5 +811,8 @@ bool DocClipBase::slotGetAudioThumbs()
     return true;
 }
 
-
+bool DocClipBase::isPlaceHolder() const
+{
+    return m_placeHolder;
+}