]> git.sesse.net Git - kdenlive/blob - src/renderer.cpp
2586f078f621383ad03b7763989a37caaa485923
[kdenlive] / src / renderer.cpp
1 /***************************************************************************
2                         krender.cpp  -  description
3                            -------------------
4   begin                : Fri Nov 22 2002
5   copyright            : (C) 2002 by Jason Wood
6   email                : jasonwood@blueyonder.co.uk
7   copyright            : (C) 2005 Lucio Flavio Correa
8   email                : lucio.correa@gmail.com
9   copyright            : (C) Marco Gittler
10   email                : g.marco@freenet.de
11   copyright            : (C) 2006 Jean-Baptiste Mardelle
12   email                : jb@ader.ch
13
14 ***************************************************************************/
15
16 /***************************************************************************
17  *                                                                         *
18  *   This program is free software; you can redistribute it and/or modify  *
19  *   it under the terms of the GNU General Public License as published by  *
20  *   the Free Software Foundation; either version 2 of the License, or     *
21  *   (at your option) any later version.                                   *
22  *                                                                         *
23  ***************************************************************************/
24
25 // ffmpeg Header files
26
27 extern "C" {
28 #include <ffmpeg/avformat.h>
29 }
30 #include <QTimer>
31 #include <QDir>
32 #include <QApplication>
33 #include <QPainter>
34
35 #include <KDebug>
36 #include <KStandardDirs>
37 #include <KMessageBox>
38 #include <KLocale>
39
40 #include "renderer.h"
41 #include "kdenlivesettings.h"
42
43 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr)
44 {
45     // detect if the producer has finished playing. Is there a better way to do it ?
46     //if (self->isBlocked) return;
47     if (mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame_ptr ), "_speed" ) == 0.0) {
48         self->emitConsumerStopped();
49     }
50     else 
51     {
52         self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
53     }
54 }
55
56 Render::Render(const QString & rendererName, int winid, int extid, QWidget *parent):QObject(parent), m_name(rendererName), m_mltConsumer(NULL), m_mltProducer(NULL), m_mltTextProducer(NULL), m_sceneList(QDomDocument()), m_winid(-1), m_framePosition(0), m_generateScenelist(false), m_isBlocked(true)
57 {
58     kDebug()<<"//////////  USING PROFILE: "<<qstrdup(KdenliveSettings::current_profile().toUtf8());
59     m_mltProfile = new Mlt::Profile((char*) qstrdup(KdenliveSettings::current_profile().toUtf8()));
60     refreshTimer = new QTimer( this );
61     connect( refreshTimer, SIGNAL(timeout()), this, SLOT( refresh()) );
62
63     m_connectTimer = new QTimer( this );
64     connect( m_connectTimer, SIGNAL(timeout()), this, SLOT(connectPlaylist()) );
65
66     if (rendererName == "project") m_monitorId = 10000;
67     else m_monitorId = 10001;
68     osdTimer = new QTimer( this );
69     connect( osdTimer, SIGNAL(timeout()), this, SLOT(slotOsdTimeout()) );
70
71     m_osdProfile =   KStandardDirs::locate("data", "kdenlive/profiles/metadata.properties");
72     //if (rendererName == "clip")
73     {
74     //Mlt::Consumer *consumer = new Mlt::Consumer( profile , "sdl_preview");
75     m_mltConsumer = new Mlt::Consumer( *m_mltProfile , "sdl_preview");//consumer;
76     m_mltConsumer->set("resize", 1);
77     m_mltConsumer->set("window_id", winid);
78     m_mltConsumer->set("terminate_on_pause", 1);
79     m_externalwinid = extid;
80     m_winid = winid;
81     m_mltConsumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_show);
82     Mlt::Producer *producer = new Mlt::Producer( *m_mltProfile , "westley-xml", "<westley><playlist><producer mlt_service=\"colour\" colour=\"blue\" /></playlist></westley>");
83     m_mltProducer = producer;
84     m_mltConsumer->connect(*m_mltProducer);
85     m_mltProducer->set_speed(0.0);
86     //m_mltConsumer->start();
87     //refresh();
88     //initSceneList();
89     }
90     /*m_osdInfo = new Mlt::Filter("data_show");
91     char *tmp = decodedString(m_osdProfile);
92     m_osdInfo->set("resource", tmp);
93     delete[] tmp;*/
94     //      Does it do anything usefull? I mean, RenderThread doesn't do anything useful at the moment
95     //      (except being cpu hungry :)
96
97     /*      if(!s_renderThread) {
98     s_renderThread = new RenderThread;
99     s_renderThread->start();
100     } */
101 }
102
103 Render::~Render()
104 {
105     closeMlt();
106 }
107
108
109 void Render::closeMlt()
110 {
111     delete m_connectTimer;
112     delete osdTimer;
113     delete refreshTimer;
114     if (m_mltConsumer)
115         delete m_mltConsumer;
116     if (m_mltProducer)
117         delete m_mltProducer;
118     //delete m_osdInfo;
119 }
120
121
122
123 int Render::resetRendererProfile(char * profile)
124 {
125     if (!m_mltConsumer) return 0;
126     if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
127     m_mltConsumer->set("refresh", 0);
128     //m_mltConsumer->set("profile", profile);
129     kDebug()<<" + + RESET CONSUMER WITH PROFILE: "<<profile;
130     mlt_properties properties = MLT_CONSUMER_PROPERTIES( m_mltConsumer->get_consumer() );
131 //    apply_profile_properties( m_profile, m_mltConsumer->get_consumer(), properties );
132     refresh();
133     return 1;
134 }
135
136 /** Wraps the VEML command of the same name; Seeks the renderer clip to the given time. */
137 void Render::seek(GenTime time)
138 {
139     sendSeekCommand(time);
140     //emit positionChanged(time);
141 }
142
143 //static
144 char *Render::decodedString(QString str)
145 {
146     /*QCString fn = QFile::encodeName(str);
147     char *t = new char[fn.length() + 1];
148     strcpy(t, (const char *)fn);*/
149
150   return qstrdup( str.toUtf8().data() ); //toLatin1
151 }
152
153 //static
154 QPixmap Render::frameThumbnail(Mlt::Frame *frame, int width, int height, bool border)
155 {
156     QPixmap pix(width, height);
157
158     mlt_image_format format = mlt_image_rgb24a;
159     uint8_t *thumb = frame->get_image(format, width, height);
160     QImage image(thumb, width, height, QImage::Format_ARGB32);
161   
162     if (!image.isNull()) {
163       pix = pix.fromImage(image);
164       if (border) {
165         QPainter painter(&pix);
166         painter.drawRect(0, 0, width - 1, height - 1);
167       }
168     }
169     else pix.fill(Qt::black);
170     return pix;
171 }
172
173
174 QPixmap Render::extractFrame(int frame_position, int width, int height)
175 {
176     QPixmap pix(width, height);
177     if (!m_mltProducer) {
178         pix.fill(Qt::black);
179         return pix;
180     }
181     Mlt::Producer *mlt_producer = m_mltProducer->cut(frame_position, frame_position + 1);
182     Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
183     m_convert.set("forced", mlt_image_rgb24a);
184     mlt_producer->attach(m_convert);
185     Mlt::Frame *frame = mlt_producer->get_frame();
186
187     if (frame) {
188         pix = frameThumbnail(frame, width, height);
189         delete frame;
190     }
191     else pix.fill(Qt::black);
192     delete mlt_producer;
193     return pix;
194 }
195
196 QPixmap Render::getImageThumbnail(KUrl url, int width, int height)
197 {
198     QImage im;
199     QPixmap pixmap;
200     if (url.fileName().startsWith(".all.")) {  //  check for slideshow
201             QString fileType = url.fileName().right(3);
202             QStringList more;
203             QStringList::Iterator it;
204
205             QDir dir( url.directory() );
206             more = dir.entryList( QDir::Files );
207  
208             for ( it = more.begin() ; it != more.end() ; ++it ) {
209                 if ((*it).endsWith("."+fileType, Qt::CaseInsensitive)) {
210                         im.load(url.directory() + "/" + *it);
211                         break;
212                 }
213             }
214     }
215     else im.load(url.path());
216     //pixmap = im.scaled(width, height);
217     return pixmap;
218 }
219
220 //static
221 QPixmap Render::getVideoThumbnail(char *profile, QString file, int frame_position, int width, int height)
222 {
223     QPixmap pix(width, height);
224     char *tmp = decodedString(file);
225     Mlt::Profile *prof = new Mlt::Profile(profile);
226     Mlt::Producer m_producer(*prof, tmp);
227     delete[] tmp;
228     if (m_producer.is_blank()) {
229         pix.fill(Qt::black);
230         return pix;
231     }
232
233     Mlt::Filter m_convert(*prof, "avcolour_space");
234     m_convert.set("forced", mlt_image_rgb24a);
235     m_producer.attach(m_convert);
236     m_producer.seek(frame_position);
237     Mlt::Frame * frame = m_producer.get_frame();
238     if (frame) {
239         pix = frameThumbnail(frame, width, height, true);
240         delete frame;
241     }
242     if (prof) delete prof;
243     return pix;
244 }
245
246 /*
247 void Render::getImage(KUrl url, int frame_position, QPoint size)
248 {
249     char *tmp = decodedString(url.path());
250     Mlt::Producer m_producer(tmp);
251     delete[] tmp;
252     if (m_producer.is_blank()) {
253         return;
254     }
255     Mlt::Filter m_convert("avcolour_space");
256     m_convert.set("forced", mlt_image_rgb24a);
257     m_producer.attach(m_convert);
258     m_producer.seek(frame_position);
259
260     Mlt::Frame * frame = m_producer.get_frame();
261
262     if (frame) {
263         QPixmap pix = frameThumbnail(frame, size.x(), size.y(), true);
264         delete frame;
265         emit replyGetImage(url, frame_position, pix, size.x(), size.y());
266     }
267 }*/
268
269 /* Create thumbnail for color */
270 /*void Render::getImage(int id, QString color, QPoint size)
271 {
272     QPixmap pixmap(size.x() - 2, size.y() - 2);
273     color = color.replace(0, 2, "#");
274     color = color.left(7);
275     pixmap.fill(QColor(color));
276     QPixmap result(size.x(), size.y());
277     result.fill(Qt::black);
278     //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
279     emit replyGetImage(id, result, size.x(), size.y());
280
281 }*/
282
283 /* Create thumbnail for image */
284 /*void Render::getImage(KUrl url, QPoint size)
285 {
286     QImage im;
287     QPixmap pixmap;
288     if (url.fileName().startsWith(".all.")) {  //  check for slideshow
289             QString fileType = url.fileName().right(3);
290             QStringList more;
291             QStringList::Iterator it;
292
293             QDir dir( url.directory() );
294             more = dir.entryList( QDir::Files );
295             for ( it = more.begin() ; it != more.end() ; ++it ) {
296                 if ((*it).endsWith("."+fileType, Qt::CaseInsensitive)) {
297                         if (!im.load(url.directory() + "/" + *it))
298                             kDebug()<<"++ ERROR LOADIN IMAGE: "<<url.directory() + "/" + *it;
299                         break;
300                 }
301             }
302     }
303     else im.load(url.path());
304
305     //pixmap = im.smoothScale(size.x() - 2, size.y() - 2);
306     QPixmap result(size.x(), size.y());
307     result.fill(Qt::black);
308     //copyBlt(&result, 1, 1, &pixmap, 0, 0, size.x() - 2, size.y() - 2);
309     emit replyGetImage(url, 1, result, size.x(), size.y());
310 }*/
311
312
313 double Render::consumerRatio() const
314 {
315     if (!m_mltConsumer) return 1.0;
316     return (m_mltConsumer->get_double("aspect_ratio_num")/m_mltConsumer->get_double("aspect_ratio_den"));
317 }
318
319
320 int Render::getLength()
321 {
322
323     if (m_mltProducer) 
324     {
325         // kDebug()<<"//////  LENGTH: "<<mlt_producer_get_playtime(m_mltProducer->get_producer());
326         return mlt_producer_get_playtime(m_mltProducer->get_producer());
327     }
328     return 0;
329 }
330
331 bool Render::isValid(KUrl url)
332 {
333     char *tmp = decodedString(url.path());
334     Mlt::Producer producer(*m_mltProfile, tmp);
335     delete[] tmp;
336     if (producer.is_blank())
337         return false;
338
339     return true;
340 }
341
342 void Render::getFileProperties(const QDomElement &xml, int clipId)
343 {
344         int height = 40;
345         int width = height * 16/9.0; //KdenliveSettings::displayratio();
346         QDomDocument doc;
347         QDomElement westley = doc.createElement("westley");
348         doc.appendChild(westley);
349         westley.appendChild(doc.importNode(xml, true));
350         kDebug()<<"////////////\n"<<doc.toString()<<"////////////////\n";
351         char *tmp = decodedString(doc.toString());
352
353         Mlt::Producer producer(*m_mltProfile, "westley-xml", tmp);
354         delete[] tmp;
355
356         if (producer.is_blank()) {
357             return;
358         }
359         int frameNumber = xml.attribute( "frame_thumbnail", 0).toInt();
360         if ( frameNumber != 0 ) producer.seek( frameNumber );
361         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer.get_producer() );
362
363         QMap < QString, QString > filePropertyMap;
364         QMap < QString, QString > metadataPropertyMap;
365
366         KUrl url = xml.attribute("resource", QString::null);
367         filePropertyMap["filename"] = url.path();
368         filePropertyMap["duration"] = QString::number(producer.get_playtime());
369         kDebug()<<"///////  PRODUCER: "<<url.path()<<" IS: "<<producer.get_playtime();
370         Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
371         m_convert.set("forced", mlt_image_rgb24a);
372         producer.attach(m_convert);
373         Mlt::Frame * frame = producer.get_frame();
374
375         filePropertyMap["fps"] =
376             QString::number(mlt_producer_get_fps( producer.get_producer() ));
377
378         if (frame && frame->is_valid()) {
379             filePropertyMap["width"] =
380                 QString::number(frame->get_int("width"));
381             filePropertyMap["height"] =
382                 QString::number(frame->get_int("height"));
383             filePropertyMap["frequency"] =
384                 QString::number(frame->get_int("frequency"));
385             filePropertyMap["channels"] =
386                 QString::number(frame->get_int("channels"));
387
388             if (frame->get_int("test_image") == 0) {
389                 if (url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
390                     filePropertyMap["type"] = "playlist";
391                     metadataPropertyMap["comment"] = QString::fromUtf8(mlt_properties_get( MLT_SERVICE_PROPERTIES( producer.get_service() ), "title"));
392                 }
393                 else if (frame->get_int("test_audio") == 0)
394                     filePropertyMap["type"] = "av";
395                 else
396                     filePropertyMap["type"] = "video";
397
398                 // Generate thumbnail for this frame
399                 QPixmap pixmap = frameThumbnail(frame, width, height, true);
400
401                 emit replyGetImage(clipId, 0, pixmap, width, height);
402
403             } else if (frame->get_int("test_audio") == 0) {
404                 QPixmap pixmap(KStandardDirs::locate("appdata", "graphics/music.png"));
405                 emit replyGetImage(clipId, 0, pixmap, width, height);
406                 filePropertyMap["type"] = "audio";
407             }
408         }
409
410         // Retrieve audio / video codec name
411
412         // Fetch the video_context
413 #if 0 //until the reason for the chrashs is found
414         AVFormatContext *context = (AVFormatContext *) mlt_properties_get_data( properties, "video_context", NULL );
415         if (context != NULL) {
416                 // Get the video_index
417                 int index = mlt_properties_get_int( properties, "video_index" );
418                 if (context->streams && context->streams [index] && context->streams[ index ]->codec && context->streams[ index ]->codec->codec->name )
419                         filePropertyMap["videocodec"] = context->streams[ index ]->codec->codec->name;
420         }
421         context = (AVFormatContext *) mlt_properties_get_data( properties, "audio_context", NULL );
422         if (context != NULL) {
423                 // Get the video_index
424                 int index = mlt_properties_get_int( properties, "audio_index" );
425                 if (context->streams && context->streams [index] && context->streams[ index ]->codec && context->streams[ index ]->codec->codec->name )
426                         filePropertyMap["audiocodec"] = context->streams[ index ]->codec->codec->name;
427         }
428 #endif
429
430
431             // metadata
432
433             mlt_properties metadata = mlt_properties_new( );
434             mlt_properties_pass( metadata, properties, "meta.attr." );
435             int count = mlt_properties_count( metadata );
436             for ( int i = 0; i < count; i ++ )
437             {
438                 QString name = mlt_properties_get_name( metadata, i );
439                 QString value = QString::fromUtf8(mlt_properties_get_value( metadata, i ));
440                 if (name.endsWith("markup") && !value.isEmpty())
441                         metadataPropertyMap[ name.section(".", 0, -2) ] = value;
442             }
443
444         emit replyGetFileProperties(clipId, filePropertyMap, metadataPropertyMap);
445         kDebug()<<"REquested fuile info for: "<<url.path();
446         if (frame) delete frame;
447 }
448
449 QDomDocument Render::sceneList() const
450 {
451     return m_sceneList;
452 }
453
454 /** Create the producer from the Westley QDomDocument */
455 void Render::initSceneList()
456 {
457     kDebug()<<"--------  INIT SCENE LIST ------_";
458     QDomDocument doc;
459     QDomElement westley = doc.createElement("westley");
460     doc.appendChild(westley);
461     QDomElement prod = doc.createElement("producer");
462     prod.setAttribute("resource", "colour");
463     prod.setAttribute("colour", "red");
464     prod.setAttribute("id", "black");
465     prod.setAttribute("in", "0");
466     prod.setAttribute("out", "0");
467
468     QDomElement tractor = doc.createElement("tractor");
469     QDomElement multitrack = doc.createElement("multitrack"); 
470
471     QDomElement playlist1 = doc.createElement("playlist");
472     playlist1.appendChild(prod);
473     multitrack.appendChild(playlist1);
474     QDomElement playlist2 = doc.createElement("playlist");
475     multitrack.appendChild(playlist2);
476     QDomElement playlist3 = doc.createElement("playlist");
477     multitrack.appendChild(playlist3);
478     QDomElement playlist4 = doc.createElement("playlist");
479     multitrack.appendChild(playlist4);
480     QDomElement playlist5 = doc.createElement("playlist");
481     multitrack.appendChild(playlist5);
482     tractor.appendChild(multitrack);
483     westley.appendChild(tractor);
484     // kDebug()<<doc.toString();
485 /*
486    QString tmp = QString("<westley><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></westley>");*/
487     setSceneList(doc, 0);
488 }
489
490
491 /** Create the producer from the Westley QDomDocument */
492 void Render::setSceneList(QDomDocument list, int position)
493 {
494     if (m_winid == -1) return;
495     m_generateScenelist = true;
496
497     kWarning()<<"//////  RENDER, SET SCENE LIST";
498
499
500 /*
501     if (!clip.is_valid()) {
502         kWarning()<<" ++++ WARNING, UNABLE TO CREATE MLT PRODUCER";
503         m_generateScenelist = false;
504         return;
505     }*/
506
507     if (m_mltConsumer) {
508         m_mltConsumer->set("refresh", 0);
509         if (!m_mltConsumer->is_stopped()) {
510         //emitConsumerStopped();
511         m_mltConsumer->stop();
512         }
513     }
514
515     if (m_mltProducer) {
516         m_mltProducer->set_speed(0.0);
517
518         //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
519
520         delete m_mltProducer;
521         m_mltProducer = NULL;
522         emit stopped();
523     }
524
525     char *tmp = decodedString(list.toString());
526     //Mlt::Producer clip(profile, "westley-xml", tmp);
527
528     m_mltProducer = new Mlt::Producer(*m_mltProfile, "westley-xml", tmp);
529     delete[] tmp;
530     //m_mltProducer->optimise();
531     if (position != 0) m_mltProducer->seek(position);
532
533     /*if (KdenliveSettings::osdtimecode()) {
534                 // Attach filter for on screen display of timecode
535                 delete m_osdInfo;
536                 QString attr = "attr_check";
537                 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
538                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik", 1 );
539                 mlt_producer_attach( m_mltProducer->get_producer(), filter );
540                 mlt_filter_close( filter );
541
542                 m_osdInfo = new Mlt::Filter("data_show");
543                 tmp = decodedString(m_osdProfile);
544                 m_osdInfo->set("resource", tmp);
545                 delete[] tmp;
546                 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
547                 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
548                 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
549                 m_osdInfo->set("dynamic", "1");
550
551                 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
552         } else {
553                 m_osdInfo->set("dynamic", "0");
554         }*/
555
556         m_fps = m_mltProducer->get_fps();
557
558         emit durationChanged(m_mltProducer->get_playtime());
559         //m_connectTimer->start( 500 );
560         connectPlaylist();
561         m_generateScenelist = false;
562   
563 }
564
565 const double Render::fps() const
566 {
567     return m_fps;
568 }
569
570 void Render::connectPlaylist() {
571
572         m_connectTimer->stop();
573         m_mltConsumer->connect(*m_mltProducer);
574         m_mltProducer->set_speed(0.0);
575         m_mltConsumer->start();
576         //refresh();
577 /*
578         if (m_mltConsumer->start() == -1) {
579                 KMessageBox::error(qApp->activeWindow(), i18n("Could not create the video preview window.\nThere is something wrong with your Kdenlive install or your driver settings, please fix it."));
580                 m_mltConsumer = NULL;
581         }
582         else {
583             refresh();
584         }*/
585 }
586
587 void Render::refreshDisplay() {
588
589         if (!m_mltProducer) return;
590         m_mltConsumer->set("refresh", 0);
591
592         mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
593         /*if (KdenliveSettings::osdtimecode()) {
594             mlt_properties_set_int( properties, "meta.attr.timecode", 1);
595             mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
596             m_osdInfo->set("dynamic", "1");
597             m_mltProducer->attach(*m_osdInfo);
598         }
599         else {
600             m_mltProducer->detach(*m_osdInfo);
601             m_osdInfo->set("dynamic", "0");
602         }*/
603         refresh();
604 }
605
606 void Render::setVolume(double volume)
607 {
608     if (!m_mltConsumer || !m_mltProducer) return;
609     /*osdTimer->stop();
610         m_mltConsumer->set("refresh", 0);
611     // Attach filter for on screen display of timecode
612     mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
613     mlt_properties_set_double( properties, "meta.volume", volume );
614     mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
615     mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
616
617     if (!KdenliveSettings::osdtimecode()) {
618         m_mltProducer->detach(*m_osdInfo);
619         mlt_properties_set_int( properties, "meta.attr.timecode", 0);
620         if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
621     }*/
622     refresh();
623     osdTimer->setSingleShot(2500 );
624 }
625
626 void Render::slotOsdTimeout()
627 {
628     mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
629     mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
630     mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
631     //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
632     refresh();
633 }
634
635 void Render::start()
636 {
637     kDebug()<<"-----  STARTING MONITOR: "<<m_name;
638     if (m_winid == -1) {
639     kDebug()<<"-----  BROKEN MONITOR: "<<m_name<<", RESTART";
640         return;
641     }
642
643     if (m_mltConsumer->is_stopped()) {
644     kDebug()<<"-----  MONITOR: "<<m_name<<" WAS STOPPED";
645         if (m_mltConsumer->start() == -1) {
646             KMessageBox::error(qApp->activeWindow(), i18n("Could not create the video preview window.\nThere is something wrong with your Kdenlive install or your driver settings, please fix it."));
647             m_mltConsumer = NULL;
648             return;
649         }
650         else {
651           kDebug()<<"-----  MONITOR: "<<m_name<<" REFRESH";
652                 refresh();
653         }
654     }
655     m_isBlocked = false;
656 }
657
658 void Render::clear()
659 {
660     if (m_mltConsumer) {
661         m_mltConsumer->set("refresh", 0);
662         if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
663     }
664
665     if (m_mltProducer) {
666         //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
667         m_mltProducer->set_speed(0.0);
668         delete m_mltProducer;
669         m_mltProducer = NULL;
670         emit stopped();
671     }
672 }
673
674 void Render::stop()
675 {
676     if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
677         kDebug()<<"/////////////   RENDER STOPPED: "<<m_name;
678         m_mltConsumer->set("refresh", 0);
679         m_mltConsumer->stop();
680     }
681     kDebug()<<"/////////////   RENDER STOP2-------";
682     m_isBlocked = true;
683
684     if (m_mltProducer) {
685         m_mltProducer->set_speed(0.0);
686         m_mltProducer->set("out", m_mltProducer->get_length() - 1);
687     }
688     kDebug()<<"/////////////   RENDER STOP3-------";
689 }
690
691 void Render::stop(const GenTime & startTime)
692 {
693     kDebug()<<"/////////////   RENDER STOP-------2";
694     if (m_mltProducer) {
695         m_mltProducer->set_speed(0.0);
696         m_mltProducer->seek((int) startTime.frames(m_fps));
697     }
698     m_mltConsumer->purge();
699 }
700
701 void Render::switchPlay()
702 {
703     if (!m_mltProducer)
704         return;
705     if (m_mltProducer->get_speed() == 0.0) m_mltProducer->set_speed(1.0);
706     else {
707       m_mltProducer->set_speed(0.0);
708       kDebug()<<"// POSITON: "<<m_framePosition;
709       m_mltProducer->seek((int) m_framePosition);
710     }
711
712     /*if (speed == 0.0) {
713         m_mltProducer->seek((int) m_framePosition + 1);
714         m_mltConsumer->purge();
715     }*/
716     refresh();
717 }
718
719 void Render::play(double speed)
720 {
721     if (!m_mltProducer)
722         return;
723     if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
724     m_mltProducer->set_speed(speed);
725     /*if (speed == 0.0) {
726         m_mltProducer->seek((int) m_framePosition + 1);
727         m_mltConsumer->purge();
728     }*/
729     refresh();
730 }
731
732 void Render::play(double speed, const GenTime & startTime)
733 {
734     kDebug()<<"/////////////   RENDER PLAY2-------"<<speed;
735     if (!m_mltProducer)
736         return;
737     //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
738     //if (speed == 0.0) m_mltConsumer->set("refresh", 0);
739     m_mltProducer->set_speed(speed);
740     m_mltProducer->seek((int) (startTime.frames(m_fps)));
741     //m_mltConsumer->purge();
742     //refresh();
743 }
744
745 void Render::play(double speed, const GenTime & startTime,
746     const GenTime & stopTime)
747 {
748     kDebug()<<"/////////////   RENDER PLAY3-------"<<speed;
749     if (!m_mltProducer)
750         return;
751     m_mltProducer->set("out", stopTime.frames(m_fps));
752     m_mltProducer->seek((int) (startTime.frames(m_fps)));
753     m_mltConsumer->purge();
754     m_mltProducer->set_speed(speed);
755     refresh();
756 }
757
758 void Render::render(const KUrl & url)
759 {
760     QDomDocument doc;
761     QDomElement elem = doc.createElement("render");
762     elem.setAttribute("filename", url.path());
763     doc.appendChild(elem);
764 }
765
766 void Render::sendSeekCommand(GenTime time)
767 {
768     if (!m_mltProducer)
769         return;
770     //kDebug()<<"//////////  KDENLIVE SEEK: "<<(int) (time.frames(m_fps));
771     m_mltProducer->seek((int) (time.frames(m_fps)));
772     refresh();
773 }
774
775 void Render::seekToFrame(int pos)
776 {
777     if (!m_mltProducer)
778         return;
779     //kDebug()<<"//////////  KDENLIVE SEEK: "<<(int) (time.frames(m_fps));
780     m_mltProducer->seek(pos);
781     refresh();
782 }
783
784 void Render::askForRefresh()
785 {
786     // Use a Timer so that we don't refresh too much
787     refreshTimer->start( 200 );
788 }
789
790 void Render::doRefresh()
791 {
792     // Use a Timer so that we don't refresh too much
793     refresh();
794 }
795
796 void Render::refresh()
797 {
798     if (!m_mltProducer || m_isBlocked)
799         return;
800     refreshTimer->stop();
801     if (m_mltConsumer) {
802         m_mltConsumer->set("refresh", 1);
803     }
804 }
805
806 /** Sets the description of this renderer to desc. */
807 void Render::setDescription(const QString & description)
808 {
809     m_description = description;
810 }
811
812 /** Returns the description of this renderer */
813 QString Render::description()
814 {
815     return m_description;
816 }
817
818
819 double Render::playSpeed()
820 {
821     if (m_mltProducer) return m_mltProducer->get_speed();
822     return 0.0;
823 }
824
825 const GenTime & Render::seekPosition() const
826 {
827     if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
828     else return GenTime();
829 }
830
831
832 const QString & Render::rendererName() const
833 {
834     return m_name;
835 }
836
837
838 void Render::emitFrameNumber(double position)
839 {
840       //kDebug()<<"// POSITON: "<<m_framePosition;
841         if (m_generateScenelist) return;
842         m_framePosition = position;
843         emit rendererPosition((int) position);
844         //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent( GenTime((int) position, m_fps), m_monitorId));
845 }
846
847 void Render::emitConsumerStopped()
848 {
849     // This is used to know when the playing stopped
850     if (m_mltProducer && !m_generateScenelist) {
851         double pos = m_mltProducer->position();
852         emit rendererStopped((int) pos);
853         //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent(GenTime((int) pos, m_fps), m_monitorId + 100));
854         //new QCustomEvent(10002));
855     }
856 }
857
858
859
860 void Render::exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime)
861 {
862 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
863 }
864
865
866 void Render::exportCurrentFrame(KUrl url, bool notify) {
867     if (!m_mltProducer) {
868         KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
869         return;
870     }
871
872     int height = 1080;//KdenliveSettings::defaultheight();
873     int width = 1940; //KdenliveSettings::displaywidth();
874
875     QPixmap pix(width, height);
876     Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
877     m_convert.set("forced", mlt_image_rgb24a);
878     m_mltProducer->attach(m_convert);
879     Mlt::Frame * frame = m_mltProducer->get_frame();
880     m_mltProducer->detach(m_convert);
881     if (frame) {
882         pix = frameThumbnail(frame, width, height);
883         delete frame;
884     }
885     pix.save(url.path(), "PNG");
886     //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
887 }
888
889 /**     MLT PLAYLIST DIRECT MANIPULATON         **/
890
891
892 void Render::mltCheckLength()
893 {
894     //kDebug()<<"checking track length: "<<track<<"..........";
895     Mlt::Service service(m_mltProducer->get_service());
896     Mlt::Tractor tractor(service);
897
898     int trackNb = tractor.count( );
899     double duration = 0;
900     double trackDuration;
901     if (trackNb == 1) {
902         Mlt::Producer trackProducer(tractor.track(0));
903         Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
904         duration = Mlt::Producer(trackPlaylist.get_producer()).get_playtime() - 1;
905         m_mltProducer->set("out", duration);
906         emit durationChanged(duration);
907         return;
908     }
909     while (trackNb > 1) {
910         Mlt::Producer trackProducer(tractor.track(trackNb - 1));
911         Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
912         trackDuration = Mlt::Producer(trackPlaylist.get_producer()).get_playtime() - 1;
913
914         kDebug()<<" / / /DURATON FOR TRACK "<<trackNb - 1<<" = "<<trackDuration;
915         if (trackDuration > duration) duration = trackDuration;
916         trackNb--;
917     }
918
919     Mlt::Producer blackTrackProducer(tractor.track(0));
920     Mlt::Playlist blackTrackPlaylist(( mlt_playlist ) blackTrackProducer.get_service());
921     double blackDuration = Mlt::Producer(blackTrackPlaylist.get_producer()).get_playtime() - 1;
922         kDebug()<<" / / /DURATON FOR TRACK 0 = "<<blackDuration;
923     if (blackDuration != duration) {
924         blackTrackPlaylist.remove_region( 0, blackDuration );
925         int i = 0;
926         int dur = duration;
927         QDomDocument doc;
928         QDomElement black = doc.createElement("producer");
929         black.setAttribute("mlt_service", "colour");
930         black.setAttribute("colour", "black");
931         black.setAttribute("in", "0");
932         black.setAttribute("out", "13999");
933         while (dur > 14000) { // <producer mlt_service=\"colour\" colour=\"black\" in=\"0\" out=\"13999\" />
934             mltInsertClip(0, GenTime(i * 14000, m_fps), black);
935             dur = dur - 14000;
936             i++;
937         }
938         black.setAttribute("out", QString::number(dur));
939         mltInsertClip(0, GenTime(), black);
940
941         m_mltProducer->set("out", duration);
942         emit durationChanged(duration);
943     }
944 }
945
946
947 void Render::mltInsertClip(int track, GenTime position, QDomElement element)
948 {
949     if (!m_mltProducer) {
950         kDebug()<<"PLAYLIST NOT INITIALISED //////";
951         return;
952     }
953     Mlt::Producer parentProd(m_mltProducer->parent());
954     if (parentProd.get_producer() == NULL) {
955         kDebug()<<"PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
956         return;
957     }
958     m_isBlocked = true;
959     Mlt::Service service(parentProd.get_service());
960     Mlt::Tractor tractor(service);
961
962     QDomDocument doc;
963     doc.appendChild(doc.importNode(element, true));
964     QString resource = doc.toString();
965     kDebug()<<"///////  ADDING CLIP TMLNE: "<<resource<<" ON TRACK: "<<track;
966     Mlt::Producer trackProducer(tractor.track(track));
967     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
968     char *tmp = decodedString(resource);
969     Mlt::Producer clip(*m_mltProfile, "westley-xml", tmp);
970     //clip.set_in_and_out(in.frames(m_fps), out.frames(m_fps));
971     delete[] tmp;
972
973     trackPlaylist.insert_at(position.frames(m_fps), clip, 1);
974     tractor.multitrack()->refresh();
975     tractor.refresh();
976     if (track != 0) mltCheckLength();
977     m_isBlocked = false;
978 }
979
980 void Render::mltCutClip(int track, GenTime position)
981 {
982     m_isBlocked = true;
983     Mlt::Service service(m_mltProducer->parent().get_service());
984     Mlt::Tractor tractor(service);
985     Mlt::Producer trackProducer(tractor.track(track));
986     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
987     trackPlaylist.split_at(position.frames(m_fps));
988     trackPlaylist.consolidate_blanks(0);
989     kDebug()<<"/ / / /CUTTING CLIP AT: "<<position.frames(m_fps);
990     m_isBlocked = false;
991 }
992
993
994 void Render::mltRemoveClip(int track, GenTime position)
995 {
996     m_isBlocked = true;
997     Mlt::Service service(m_mltProducer->parent().get_service());
998     Mlt::Tractor tractor(service);
999     Mlt::Producer trackProducer(tractor.track(track));
1000     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1001     int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1002     //trackPlaylist.remove(clipIndex);
1003     trackPlaylist.replace_with_blank(clipIndex);
1004     trackPlaylist.consolidate_blanks(0);
1005     if (track != 0) mltCheckLength();
1006     //emit durationChanged();
1007     m_isBlocked = false;
1008 }
1009
1010 void Render::mltRemoveEffect(int track, GenTime position, QString index, bool doRefresh)
1011 {
1012
1013     Mlt::Service service(m_mltProducer->parent().get_service());
1014
1015     Mlt::Tractor tractor(service);
1016     Mlt::Producer trackProducer(tractor.track(track));
1017     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1018     //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1019     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1020     if (!clip) {
1021         kDebug()<<" / / / CANNOT FIND CLIP TO REMOVE EFFECT";
1022         return;
1023     }
1024     m_isBlocked = true;
1025     Mlt::Service clipService(clip->get_service());
1026
1027 //    if (tag.startsWith("ladspa")) tag = "ladspa";
1028
1029     int ct = 0;
1030         Mlt::Filter *filter = clipService.filter( ct );
1031         while (filter) {
1032             if (index == "-1" || filter->get("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
1033                 clipService.detach(*filter);
1034                 kDebug()<<" / / / DLEETED EFFECT: "<<ct;
1035             }
1036             else ct++;
1037             filter = clipService.filter( ct );
1038         }
1039     m_isBlocked = false;
1040     if (doRefresh) refresh();
1041 }
1042
1043
1044 void Render::mltAddEffect(int track, GenTime position, QMap <QString, QString> args, bool doRefresh)
1045 {
1046     Mlt::Service service(m_mltProducer->parent().get_service());
1047     Mlt::Tractor tractor(service);
1048     Mlt::Producer trackProducer(tractor.track(track));
1049     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1050
1051     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1052
1053     if (!clip) {
1054         kDebug()<<"**********  CANNOT FIND CLIP TO APPLY EFFECT-----------";
1055         return;
1056     }
1057     Mlt::Service clipService(clip->get_service());
1058     m_isBlocked = true;
1059     // create filter
1060     QString tag = args.value("tag");
1061     //kDebug()<<" / / INSERTING EFFECT: "<<id;
1062     if (tag.startsWith("ladspa")) tag = "ladspa";
1063     char *filterId = decodedString(tag);
1064     Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterId);
1065     if (filter && filter->is_valid())
1066       filter->set("kdenlive_id", filterId);
1067     else {
1068       kDebug() << "filter is NULL";
1069       return;
1070     }
1071
1072     QMap<QString, QString>::Iterator it;
1073     QString keyFrameNumber = "#0";
1074
1075     for ( it = args.begin(); it != args.end(); ++it ) {
1076     //kDebug()<<" / / INSERTING EFFECT ARGS: "<<it.key()<<": "<<it.data();
1077         QString key;
1078         QString currentKeyFrameNumber;
1079         if (it.key().startsWith("#")) {
1080             currentKeyFrameNumber = it.key().section(":", 0, 0);
1081             if (currentKeyFrameNumber != keyFrameNumber) {
1082                 // attach filter to the clip
1083                 clipService.attach(*filter);
1084                 filter = new Mlt::Filter(*m_mltProfile, filterId);
1085                 filter->set("kdenlive_id", filterId);
1086                 keyFrameNumber = currentKeyFrameNumber;
1087             }
1088             key = it.key().section(":", 1);
1089         }
1090         else key = it.key();
1091         char *name = decodedString(key);
1092         char *value = decodedString(it.value());
1093         filter->set(name, value);
1094         delete[] name;
1095         delete[] value;
1096     }
1097     // attach filter to the clip
1098     clipService.attach(*filter);
1099     delete[] filterId;
1100     m_isBlocked = false;
1101     if (doRefresh) refresh();
1102
1103 }
1104
1105 void Render::mltEditEffect(int track, GenTime position, QMap <QString, QString> args)
1106 {
1107     QString index = args.value("kdenlive_ix");
1108     QString tag =  args.value("tag");
1109     QMap<QString, QString>::Iterator it = args.begin();
1110     if (it.key().startsWith("#") || tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle") {
1111         // This is a keyframe effect, to edit it, we remove it and re-add it.
1112         mltRemoveEffect(track, position,index);
1113         mltAddEffect(track, position, args);
1114         return;
1115     }
1116     m_isBlocked = true;
1117     // create filter
1118     Mlt::Service service(m_mltProducer->parent().get_service());
1119
1120     Mlt::Tractor tractor(service);
1121     Mlt::Producer trackProducer(tractor.track(track));
1122     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1123     //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1124     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1125     if (!clip) {
1126       kDebug()<<"WARINIG, CANNOT FIND CLIP ON track: "<<track<<", AT POS: "<<position.frames(m_fps);
1127       m_isBlocked = false;
1128       return;
1129     }
1130
1131     Mlt::Service clipService(clip->get_service());
1132
1133 //    if (tag.startsWith("ladspa")) tag = "ladspa";
1134
1135     int ct = 0;
1136     Mlt::Filter *filter = clipService.filter( ct );
1137     while (filter) {
1138         if (filter->get("kdenlive_ix") == index) {
1139           break;
1140         }
1141         ct++;
1142         filter = clipService.filter( ct );
1143     }
1144
1145
1146     if (!filter) {
1147         kDebug()<<"WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT!!!!!";
1148         mltAddEffect(track, position, args);
1149         m_isBlocked = false;
1150         return;
1151     }
1152
1153     for ( it = args.begin(); it != args.end(); ++it ) {
1154     kDebug()<<" / / EDITING EFFECT ARGS: "<<it.key()<<": "<<it.value();
1155         char *name = decodedString(it.key());
1156         char *value = decodedString(it.value());
1157         filter->set(name, value);
1158         delete[] name;
1159         delete[] value;
1160     }
1161     m_isBlocked = false;
1162     refresh();
1163 }
1164
1165 void Render::mltResizeClipEnd(int track, GenTime pos, GenTime in, GenTime out)
1166 {
1167     m_isBlocked = true;
1168     Mlt::Service service(m_mltProducer->parent().get_service());
1169
1170     Mlt::Tractor tractor(service);
1171     Mlt::Producer trackProducer(tractor.track(track));
1172     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1173     if (trackPlaylist.is_blank_at(pos.frames(m_fps) + 1)) 
1174         kDebug()<<"////////  ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
1175     int clipIndex = trackPlaylist.get_clip_index_at(pos.frames(m_fps) + 1);
1176
1177     int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
1178     int newDuration = out.frames(m_fps) - 1;
1179
1180     kDebug()<<" ** RESIZING CLIP END:" << clipIndex << " on track:"<< track <<", mid pos: "<<pos.frames(25)<<", in: "<<in.frames(25)<<", out: "<<out.frames(25)<<", PREVIOUS duration: "<<previousDuration;
1181     trackPlaylist.resize_clip(clipIndex, in.frames(m_fps), newDuration);
1182     trackPlaylist.consolidate_blanks(0);
1183     if (previousDuration < newDuration) {
1184         // clip was made longer, trim next blank if there is one.
1185         if (trackPlaylist.is_blank(clipIndex + 1)) {
1186             trackPlaylist.split(clipIndex + 1, newDuration - previousDuration);
1187             trackPlaylist.remove(clipIndex + 1);
1188         }
1189     }
1190     else trackPlaylist.insert_blank(clipIndex + 1, previousDuration - newDuration - 1);
1191
1192     trackPlaylist.consolidate_blanks(0);
1193     tractor.multitrack()->refresh();
1194     tractor.refresh();
1195     if (track != 0) mltCheckLength();
1196     m_isBlocked = false;
1197 }
1198
1199 void Render::mltChangeTrackState(int track, bool mute, bool blind)
1200 {
1201     Mlt::Service service(m_mltProducer->parent().get_service());
1202
1203     Mlt::Tractor tractor(service);
1204     Mlt::Producer trackProducer(tractor.track(track));
1205     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1206     if (mute) {
1207         if (blind) trackProducer.set("hide", 3);
1208         else trackProducer.set("hide", 2);
1209     }
1210     else if (blind) {
1211         trackProducer.set("hide", 1);
1212     }
1213     else {
1214         trackProducer.set("hide", 0);
1215     }
1216     tractor.multitrack()->refresh();
1217     tractor.refresh();
1218     refresh();
1219 }
1220
1221 void Render::mltResizeClipStart(int track, GenTime pos, GenTime moveEnd, GenTime moveStart, GenTime in, GenTime out)
1222 {
1223     m_isBlocked = true;
1224     Mlt::Service service(m_mltProducer->parent().get_service());
1225     int moveFrame = (moveEnd - moveStart).frames(m_fps);
1226
1227     Mlt::Tractor tractor(service);
1228     Mlt::Producer trackProducer(tractor.track(track));
1229     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1230     if (trackPlaylist.is_blank_at(pos.frames(m_fps) - 1)) 
1231         kDebug()<<"////////  ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
1232     int clipIndex = trackPlaylist.get_clip_index_at(pos.frames(m_fps) - 1);
1233     kDebug()<<" ** RESIZING CLIP START:" << clipIndex << " on track:"<< track <<", mid pos: "<<pos.frames(25)<<", moving: "<<moveFrame<<", in: "<<in.frames(25)<<", out: "<<out.frames(25);
1234
1235     trackPlaylist.resize_clip(clipIndex, in.frames(m_fps), out.frames(m_fps));
1236     if (moveFrame > 0) trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
1237     else {
1238         int midpos = moveStart.frames(m_fps) - 1; //+ (moveFrame / 2)
1239         int blankIndex = trackPlaylist.get_clip_index_at(midpos);
1240         int blankLength = trackPlaylist.clip_length(blankIndex);
1241
1242         kDebug()<<" + resizing blank: "<<blankIndex<<", Mid: "<<midpos<<", Length: "<<blankLength<< ", SIZE DIFF: "<<moveFrame;
1243
1244         
1245         if (blankLength + moveFrame == 0) trackPlaylist.remove(blankIndex);
1246         else trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame -1);
1247     }
1248     trackPlaylist.consolidate_blanks(0);
1249     m_isBlocked = false;
1250     kDebug()<<"-----------------\n"<<"CLIP 0: "<<trackPlaylist.clip_start(0)<<", LENGT: "<<trackPlaylist.clip_length(0);
1251     kDebug()<<"CLIP 1: "<<trackPlaylist.clip_start(1)<<", LENGT: "<<trackPlaylist.clip_length(1);
1252     kDebug()<<"CLIP 2: "<<trackPlaylist.clip_start(2)<<", LENGT: "<<trackPlaylist.clip_length(2);
1253     kDebug()<<"CLIP 3: "<<trackPlaylist.clip_start(3)<<", LENGT: "<<trackPlaylist.clip_length(3);
1254    kDebug()<<"CLIP 4: "<<trackPlaylist.clip_start(4)<<", LENGT: "<<trackPlaylist.clip_length(4);
1255 }
1256
1257 void Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd)
1258 {
1259     mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps));
1260 }
1261
1262
1263 void Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd)
1264 {
1265     m_isBlocked = true;
1266     //m_mltConsumer->set("refresh", 0);
1267     Mlt::Service service(m_mltProducer->parent().get_service());
1268
1269     Mlt::Tractor tractor(service);
1270     Mlt::Producer trackProducer(tractor.track(startTrack));
1271     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1272     int clipIndex = trackPlaylist.get_clip_index_at(moveStart + 1);
1273     mlt_field field = mlt_tractor_field(tractor.get_tractor());
1274     mlt_multitrack multitrack = mlt_field_multitrack(field); //mlt_tractor_multitrack(tractor.get_tractor());
1275     kDebug()<<" --  CURRENT MULTIOTRACK HAS: "<<mlt_multitrack_count(multitrack)<<" tracks";;
1276     mlt_service multiprod = mlt_multitrack_service( multitrack );
1277
1278     Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
1279     trackPlaylist.consolidate_blanks(0);
1280     mlt_events_block( MLT_PRODUCER_PROPERTIES(trackProducer.get_producer()), NULL );
1281
1282     if (endTrack == startTrack) {
1283         if (!trackPlaylist.is_blank_at(moveEnd)) {
1284             kWarning()<<"// ERROR, CLIP COLLISION----------";
1285             int ix = trackPlaylist.get_clip_index_at(moveEnd);
1286                 kDebug()<<"BAD CLIP STARTS AT: "<<trackPlaylist.clip_start(ix)<<", LENGT: "<<trackPlaylist.clip_length(ix);
1287         }
1288         trackPlaylist.insert_at(moveEnd, clipProducer, 1);
1289         trackPlaylist.consolidate_blanks(0);
1290     }
1291     else {
1292         trackPlaylist.consolidate_blanks(0);
1293         Mlt::Producer destTrackProducer(tractor.track(endTrack));
1294         Mlt::Playlist destTrackPlaylist(( mlt_playlist ) destTrackProducer.get_service());
1295         destTrackPlaylist.consolidate_blanks(1);
1296         destTrackPlaylist.insert_at(moveEnd, clipProducer, 1);
1297         destTrackPlaylist.consolidate_blanks(0);
1298     }
1299
1300     mltCheckLength();
1301     mlt_events_unblock( MLT_PRODUCER_PROPERTIES(trackProducer.get_producer()), NULL );
1302     m_isBlocked = false;
1303     m_mltConsumer->set("refresh", 1);
1304 }
1305
1306 void Render::mltMoveTransition(QString type, int startTrack, int trackOffset, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
1307 {
1308     m_isBlocked = true;
1309     m_mltConsumer->set("refresh", 0);
1310     mlt_service serv = m_mltProducer->parent().get_service();
1311
1312     mlt_service nextservice = mlt_service_get_producer(serv);
1313     mlt_properties properties = MLT_SERVICE_PROPERTIES( nextservice );
1314     QString mlt_type = mlt_properties_get( properties, "mlt_type" );
1315     QString resource = mlt_properties_get( properties, "mlt_service");
1316     int old_pos = (oldIn.frames(m_fps) + oldOut.frames(m_fps)) / 2;
1317
1318     int new_in = newIn.frames(m_fps);
1319     int new_out = newOut.frames(m_fps) - 1;
1320     while (mlt_type == "transition") {
1321         mlt_transition tr = (mlt_transition) nextservice;
1322         int currentTrack = mlt_transition_get_b_track(tr);
1323         int currentIn = (int) mlt_transition_get_in(tr);
1324         int currentOut = (int) mlt_transition_get_out(tr);
1325         kDebug()<<"// FOUND EXISTING TRANS, IN: "<<currentIn<<", OUT: "<<currentOut<<", TRACK: "<<currentTrack;
1326         //kDebug()<<"// LOOKING FOR IN: "<<old_in<<", OUT: "<<old_out;
1327         kDebug()<<"// OLD IN: "<<oldIn.frames(m_fps)<<" // OLD OUT: "<<oldOut.frames(m_fps)<<", TRACK: "<<startTrack<<", MID POS: "<<old_pos;
1328         if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
1329             mlt_transition_set_in_and_out(tr, new_in, new_out);
1330             if (trackOffset != 0) {
1331                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( tr );
1332                 mlt_properties_set_int( properties, "a_track", mlt_transition_get_a_track(tr) + trackOffset );
1333                 mlt_properties_set_int( properties, "b_track", mlt_transition_get_b_track(tr) + trackOffset );
1334             }
1335             break;
1336         }
1337         nextservice = mlt_service_producer(nextservice);
1338         properties = MLT_SERVICE_PROPERTIES( nextservice );
1339         mlt_type = mlt_properties_get( properties, "mlt_type" );
1340         resource = mlt_properties_get( properties, "mlt_service" );
1341     }
1342     m_isBlocked = false;
1343 }
1344
1345 void Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QMap <QString, QString> args)
1346 {
1347     m_isBlocked = true;
1348     Mlt::Service service(m_mltProducer->parent().get_service());
1349
1350     Mlt::Tractor tractor(service);
1351     Mlt::Field *field = tractor.field();
1352     char *transId = decodedString(tag);
1353     Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, transId);
1354     transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps));
1355     QMap<QString, QString>::Iterator it;
1356     QString key;
1357
1358     kDebug()<<" ------  ADDING TRANSITION PARAMs: "<<args.count();
1359
1360     for ( it = args.begin(); it != args.end(); ++it ) {
1361         key = it.key();
1362         char *name = decodedString(key);
1363         char *value = decodedString(it.value());
1364         transition->set(name, value);
1365         kDebug()<<" ------  ADDING TRANS PARAM: "<<name<<": "<<value;
1366         //filter->set("kdenlive_id", id);
1367         delete[] name;
1368         delete[] value;
1369     }
1370     // attach filter to the clip
1371     field->plant_transition(*transition, a_track, b_track);
1372     delete[] transId;
1373     m_isBlocked = false;
1374     refresh();
1375
1376 }
1377
1378 void Render::mltSavePlaylist()
1379 {
1380     kWarning()<<"// UPDATING PLAYLIST TO DISK++++++++++++++++";
1381     Mlt::Consumer *fileConsumer = new Mlt::Consumer(*m_mltProfile, "westley");
1382     fileConsumer->set("resource", "/home/one/playlist.xml");
1383
1384     Mlt::Service service(m_mltProducer->get_service());
1385     Mlt::Tractor tractor(service);
1386
1387     fileConsumer->connect(service);
1388     fileConsumer->start();
1389
1390 }
1391
1392 #include "renderer.moc"