meltjob.cpp 6.82 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 38 39 40 41 42 43 44 45
/***************************************************************************
 *                                                                         *
 *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@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) any later version.                                   *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

#include "meltjob.h"
#include "kdenlivesettings.h"
#include "kdenlivedoc.h"

#include <KDebug>
#include <KLocale>

#include <mlt++/Mlt.h>


static void consumer_frame_render(mlt_consumer, MeltJob * self, mlt_frame /*frame_ptr*/)
{
    // detect if the producer has finished playing. Is there a better way to do it?
    self->emitFrameNumber();
}

MeltJob::MeltJob(CLIPTYPE cType, const QString &id, QStringList parameters) : AbstractClipJob(MLTJOB, cType, id, parameters), 
    m_producer(NULL),
    m_profile(NULL),
    m_consumer(NULL),
    m_length(0)
{
    jobStatus = JOBWAITING;
    m_params = parameters;
    description = i18n("Process clip");
46 47
    QString consum = m_params.at(5);
    if (consum.contains(":")) m_dest = consum.section(":", 1);
48 49 50 51 52 53 54 55 56 57 58 59 60 61
}

void MeltJob::setProducer(Mlt::Producer *producer)
{
    m_producer = producer;
}

void MeltJob::startJob()
{
    if (!m_producer) {
        m_errorMessage.append(i18n("No producer for this clip."));
        setStatus(JOBCRASHED);
        return;
    }
62 63
    int in = m_params.takeFirst().toInt();
    int out = m_params.takeFirst().toInt();
64
    QString producerParams =m_params.takeFirst(); 
65 66 67
    QString filter = m_params.takeFirst();
    QString filterParams = m_params.takeFirst();
    QString consumer = m_params.takeFirst();
68 69
    kDebug()<<"consumer: "<<consumer;
    if (consumer.contains(":")) m_dest = consumer.section(":", 1);
70
    QString consumerParams = m_params.takeFirst();
71 72 73 74 75 76 77 78
    
    // optional params
    QString properties;
    if (!m_params.isEmpty()) properties = m_params.takeFirst();
    int startPos = -1;
    if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
    int track = -1;
    if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
79 80 81 82
    QString finalFilter;
    if (!m_params.isEmpty()) finalFilter = m_params.takeFirst();
    else finalFilter = filter;

83
    if (out != -1 && out <= in) {
84 85 86 87
        m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
        setStatus(JOBCRASHED);
        return;
    }
88
    
89
    m_profile = m_producer->profile();
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    Mlt::Producer *prod;
    if (out == -1) {
        QString url = QString::fromUtf8(m_producer->get("resource"));
        prod = new Mlt::Producer(*m_profile,  url.toUtf8().constData());
    }
    else 
        prod = m_producer->cut(in, out);
    QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
    foreach(QString data, list) {
        if (data.contains('=')) {
            prod->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
        }
    }

    if (consumer.contains(":")) {
        m_consumer = new Mlt::Consumer(*m_profile, consumer.section(":", 0, 0).toUtf8().constData(), consumer.section(":", 1).toUtf8().constData());
    }
    else {
        m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
    }
111 112 113 114 115 116
    if (!m_consumer || !m_consumer->is_valid()) {
        m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
        setStatus(JOBCRASHED);
        return;
    }

117 118 119
    //m_consumer->set("terminate_on_pause", 1 );
    //m_consumer->set("eof", "pause" );
    m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
120

121
    list = consumerParams.split(' ', QString::SkipEmptyParts);
122 123
    foreach(QString data, list) {
        if (data.contains('=')) {
124
            kDebug()<<"// filter con: "<<data;
125 126 127 128 129 130 131 132
            m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
        }
    }
    
    Mlt::Filter mltFilter(*m_profile, filter.toUtf8().data());
    list = filterParams.split(' ', QString::SkipEmptyParts);
    foreach(QString data, list) {
        if (data.contains('=')) {
133
            kDebug()<<"// filter p: "<<data;
134 135 136
            mltFilter.set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
        }
    }
137 138
    Mlt::Playlist playlist;
    playlist.append(*prod);
139
    m_length = prod->get_length();
140
    m_consumer->connect(playlist);
141 142
    prod->set_speed(0);
    prod->seek(0);
143
    prod->attach(mltFilter);
144 145
    m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
    m_consumer->start();
146
    prod->set_speed(1);
147 148 149 150 151
    while (jobStatus != JOBABORTED && !m_consumer->is_stopped()) {
        
    }
    m_consumer->stop();
    QStringList wanted = properties.split(',', QString::SkipEmptyParts);
152
    stringMap jobResults;
153 154
    foreach(const QString key, wanted) {
        QString value = mltFilter.get(key.toUtf8().constData());
155
        jobResults.insert(key, value);
156
    }
157
    if (!jobResults.isEmpty()) emit gotFilterJobResults(m_clipId, startPos, track, finalFilter, jobResults);
158 159
    setStatus(JOBDONE);
    delete m_consumer;
160
    delete prod;
161 162 163 164 165 166 167 168 169 170
    return;
}


MeltJob::~MeltJob()
{
}

const QString MeltJob::destination() const
{
171
    return m_dest;
172 173 174 175 176 177 178 179 180 181 182 183 184
}

stringMap MeltJob::cancelProperties()
{
    QMap <QString, QString> props;
    return props;
}

const QString MeltJob::statusMessage()
{
    QString statusInfo;
    switch (jobStatus) {
        case JOBWORKING:
185
            statusInfo = description;
186 187
            break;
        case JOBWAITING:
188
            statusInfo = i18n("Waiting to process clip");
189 190 191 192 193 194 195 196 197 198 199 200
            break;
        default:
            break;
    }
    return statusInfo;
}

void MeltJob::emitFrameNumber()
{
    if (m_consumer && m_length > 0) {
        emit jobProgress(m_clipId, (int) (100 * m_consumer->position() / m_length), jobType);
    }
201 202
}