projectclip.cpp 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
Copyright (C) 2012  Till Theato <root@ttill.de>
Copyright (C) 2014  Jean-Baptiste Mardelle <jb@kdenlive.org>
This file is part of Kdenlive. See www.kdenlive.org.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy 
defined in Section 14 of version 3 of the license.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "projectclip.h"
#include "projectfolder.h"
#include "bin.h"

#include <QDomElement>
#include <QFile>
#include <QDebug>
#include <QCryptographicHash>

#include <KLocalizedString>

#include <mlt++/Mlt.h>


ProjectClip::ProjectClip(const QString &id, Mlt::Producer *producer, ProjectFolder* parent) :
38
    AbstractProjectItem(AbstractProjectItem::ClipItem, id, parent),
39 40 41 42 43 44 45 46 47 48 49 50 51
    m_producer(producer)
{
    m_properties = QMap <QString, QString> ();
    QString resource = producer->get("resource");
    if (!resource.isEmpty()) {
        m_url = QUrl::fromLocalFile(resource);
        if (m_url.isValid()) {
            m_name = m_url.fileName();
        }
    }
    m_duration = producer->get_length_time(mlt_time_smpte_df);

    getFileHash();
52
    setParent(parent);
53 54 55
}

ProjectClip::ProjectClip(const QDomElement& description, ProjectFolder* parent) :
56
    AbstractProjectItem(AbstractProjectItem::ClipItem, description, parent)
57 58 59 60 61 62 63 64
    , m_producer(NULL)
{
    Q_ASSERT(description.hasAttribute("id"));
    m_properties = QMap <QString, QString> ();
    m_url = QUrl::fromLocalFile(getXmlProperty(description, "resource"));
    m_name = m_url.fileName();
    if (description.hasAttribute("zone"))
	m_zone = QPoint(description.attribute("zone").section(':', 0, 0).toInt(), description.attribute("zone").section(':', 1, 1).toInt());
65
    setParent(parent);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}


ProjectClip::~ProjectClip()
{
    // Cancel all clip jobs so that we don't crash trying to access the deleted clip
    bin()->discardJobs(m_id);
    delete m_producer;
}

QString ProjectClip::getXmlProperty(const QDomElement &producer, const QString &propertyName)
{
    QString value;
    QDomNodeList props = producer.elementsByTagName("property");
    for (int i = 0; i < props.count(); ++i) {
        if (props.at(i).toElement().attribute("name") == propertyName) {
            value = props.at(i).firstChild().nodeValue();
            break;
        }
    }
    return value;
}

ClipType ProjectClip::clipType() const
{
    //TODO: store in properties
    if (m_producer == NULL) return Unknown;
    QString service = m_producer->get("mlt_service");
    if (service == "avformat" || service == "avformat-novalidate") {
	return AV;
    }
    if (service == "qimage" || service == "pixbuf") {
	return Image;
    }
    if (service == "color") {
	return Color;
    }
}

ProjectClip* ProjectClip::clip(const QString &id)
{
    if (id == m_id) {
        return this;
    }
    return NULL;
}

113 114 115 116 117
ProjectFolder* ProjectClip::folder(const QString &id)
{
    return NULL;
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
ProjectClip* ProjectClip::clipAt(int ix)
{
    if (ix == index()) {
        return this;
    }
    return NULL;
}

bool ProjectClip::hasUrl() const
{
    return m_url.isValid();
}

QUrl ProjectClip::url() const
{
    return m_url;
}

bool ProjectClip::hasLimitedDuration() const
{
    //TODO: should be false for color and image clips
    return m_hasLimitedDuration;
}

int ProjectClip::duration() const
{
    if (m_producer) {
	return m_producer->get_playtime();
    }
    return -1;
}

QString ProjectClip::serializeClip()
{
    /*Mlt::Consumer *consumer = bin()->project()->xmlConsumer();
    consumer->connect(*m_baseProducer);
    consumer->run();
    return QString::fromUtf8(consumer->get("kdenlive_clip"));*/
    return QString();
}

void ProjectClip::reloadProducer()
{
    QDomDocument doc;
    QDomElement xml = toXml(doc);
    bin()->reloadProducer(m_id, doc.documentElement());
}

void ProjectClip::setCurrent(bool current, bool notify)
{
    AbstractProjectItem::setCurrent(current, notify);
    if (current && m_producer) {
        bin()->openProducer(m_id, producer());
    }
}

QDomElement ProjectClip::toXml(QDomDocument& document)
{
    QDomElement prod = document.createElement("producer");
    document.appendChild(prod);
    QDomElement prop = document.createElement("property");
    prop.setAttribute("name", "resource");
    QString path = m_properties.value("proxy");
    if (path.length() < 2) {
        // No proxy
        path = m_url.toLocalFile();
    }
    QDomText value = document.createTextNode(path);
    prop.appendChild(value);
    prod.appendChild(prop);
    prod.setAttribute("id", m_id);
    return prod;
}

QPixmap ProjectClip::thumbnail(bool force)
{
/*    if ((force || m_thumbnail.isNull()) && m_baseProducer) {
	int width = 80 * bin()->project()->displayRatio();
	if (width % 2 == 1) width++;
	bin()->project()->monitorManager()->requestThumbnails(m_id, QList <int>() << 0);
    }
    */
    return m_thumbnail;
}

void ProjectClip::setThumbnail(QImage img)
{
    m_thumbnail = roundedPixmap(QPixmap::fromImage(img));
    if (hasProxy() && !m_thumbnail.isNull()) {
        // Overlay proxy icon
        QPainter p(&m_thumbnail);
        QColor c(220, 220, 10, 200);
        QRect r(0, 0, m_thumbnail.height() / 2.5, m_thumbnail.height() / 2.5);
        p.fillRect(r, c);
        QFont font = p.font();
        font.setPixelSize(r.height());
        font.setBold(true);
        p.setFont(font);
        p.setPen(Qt::black);
        p.drawText(r, Qt::AlignCenter, i18nc("The first letter of Proxy, used as abbreviation", "P"));
    }
    bin()->emitItemUpdated(this);
}

void ProjectClip::setProducer(Mlt::Producer *producer, bool replaceProducer)
{
    if (!replaceProducer && m_producer) {
        qDebug()<<"// RECIEVED PRODUCER BUT WE ALREADY HAVE ONE\n----------";
        return;
    }
    if (m_producer) {
        delete m_producer;
    }
    m_producer = producer;
    m_duration = producer->get_length_time(mlt_time_smpte_df);
    bin()->emitItemUpdated(this);
    getFileHash();
}

Mlt::Producer *ProjectClip::producer()
{
    if (!m_producer) return NULL;
    return new Mlt::Producer(*m_producer);
}

bool ProjectClip::hasProducer() const
{
    return m_producer!= NULL;
}

248
QMap <QString, QString> ProjectClip::properties()
249
{
250 251 252 253 254 255 256 257 258 259 260 261 262 263
    //TODO: move into its own class that creates its own widget (reuse clipproperties)
    QMap <QString, QString> result;
    if (m_producer) {
        mlt_properties props = m_producer->get_properties();
        QString key = "video_index";
        int video_index = mlt_properties_get_int(props, key.toUtf8().constData());
        QString codecKey = "meta.media." + QString::number(video_index) + ".codec.long_name";
        result.insert(i18n("Video codec"), mlt_properties_get(props, codecKey.toUtf8().constData()));
        key = "audio_index";
        int audio_index = mlt_properties_get_int(props, key.toUtf8().constData());
        codecKey = "meta.media." + QString::number(audio_index) + ".codec.long_name";
        result.insert(i18n("Audio codec"), mlt_properties_get(props, codecKey.toUtf8().constData()));
    }
    return result;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
}

void ProjectClip::setZone(const QPoint &zone)
{
    m_zone = zone;
}

QPoint ProjectClip::zone() const
{
    return m_zone;
}

void ProjectClip::addMarker(int position)
{
    m_markers << position;
    //bin()->markersUpdated(m_id, m_markers);
}

void ProjectClip::removeMarker(int position)
{
    m_markers.removeAll(position);
    //bin()->markersUpdated(m_id, m_markers);
}

void ProjectClip::setProducerProperty(const char *name, int data)
{
    if (m_producer) {
	m_producer->set(name, data);
	//TODO: also set property on all track producers
    }
}

void ProjectClip::setProducerProperty(const char *name, double data)
{
    if (m_producer) {
	m_producer->set(name, data);
	//TODO: also set property on all track producers
    }
}

void ProjectClip::setProducerProperty(const char *name, const char *data)
{
    if (m_producer) {
	m_producer->set(name, data);
	//TODO: also set property on all track producers
    }
}


QString ProjectClip::getProducerProperty(const QString &key)
{
    QString value;
    if (m_producer) {
	value = m_producer->get(key.toUtf8().constData());
    }
    return value;
}

const QString ProjectClip::hash()
{
    if (!m_properties.contains("file_hash")) getFileHash();
    return m_properties.value("file_hash");
}

void ProjectClip::getFileHash()
{
    if (clipType() == SlideShow) return;    
    QFile file(m_url.toLocalFile());
    qDebug()<<"// CREATE FILE HASH FOR CLIP";
    if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
        QByteArray fileData;
        QByteArray fileHash;
        ////qDebug() << "SETTING HASH of" << value;
        m_properties.insert("file_size", QString::number(file.size()));
        /*
               * 1 MB = 1 second per 450 files (or faster)
               * 10 MB = 9 seconds per 450 files (or faster)
               */
        if (file.size() > 2000000) {
            fileData = file.read(1000000);
            if (file.seek(file.size() - 1000000))
                fileData.append(file.readAll());
        } else
            fileData = file.readAll();
        file.close();
        fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
        m_properties.insert("file_hash", QString(fileHash.toHex()));
    }
}

const QString ProjectClip::getProperty(const QString &key) const
{
    return m_properties.value(key);
}

bool ProjectClip::hasProxy() const
{
    QString proxy = m_properties.value("proxy");
    if (proxy.isEmpty() || proxy == "-") return false;
    return true;
}

void ProjectClip::setProperties(QMap <QString, QString> properties)
{
    QMapIterator<QString, QString> i(properties);
    bool refreshProducer = false;
    QStringList keys;
    keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
    QString oldProxy = m_properties.value("proxy");
    while (i.hasNext()) {
        i.next();
        setProducerProperty(i.key().toUtf8().data(), i.value().toUtf8().data());
	m_properties.insert(i.key(), i.value());
        if (clipType() == SlideShow && keys.contains(i.key())) refreshProducer = true;
    }
    if (properties.contains("proxy")) {
	qDebug()<<"/// CLIP UPDATE, ASK PROXY";
        QString value = properties.value("proxy");
        // If value is "-", that means user manually disabled proxy on this clip
        if (value.isEmpty() || value == "-") {
            // reset proxy
            if (bin()->hasPendingJob(m_id, AbstractClipJob::PROXYJOB)) {
                bin()->discardJobs(m_id, AbstractClipJob::PROXYJOB);
            }
            else reloadProducer();
        }
        else {
	    bin()->startJob(m_id, AbstractClipJob::PROXYJOB);
        }
    }
    //if (refreshProducer) slotRefreshProducer();
}

void ProjectClip::setJobStatus(AbstractClipJob::JOBTYPE jobType, ClipJobStatus status, int progress, const QString &statusMessage)
{
    m_jobType = jobType;
    if (progress > 0) {
	m_jobProgress = progress;
    }
    else {
	m_jobProgress = status;
	if ((status == JobAborted || status == JobCrashed  || status == JobDone) || !statusMessage.isEmpty()) {
	    m_jobMessage = statusMessage;
	}
    }
    bin()->emitItemUpdated(this);
}