]> git.sesse.net Git - kdenlive/blob - src/renderer.cpp
timeline move fixes
[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         if (frame->is_valid()) {
376             filePropertyMap["fps"] =
377                 QString::number(mlt_producer_get_fps( producer.get_producer() ));
378             filePropertyMap["width"] =
379                 QString::number(frame->get_int("width"));
380             filePropertyMap["height"] =
381                 QString::number(frame->get_int("height"));
382             filePropertyMap["frequency"] =
383                 QString::number(frame->get_int("frequency"));
384             filePropertyMap["channels"] =
385                 QString::number(frame->get_int("channels"));
386
387         // Retrieve audio / video codec name
388
389         // Fetch the video_context
390         AVFormatContext *context = (AVFormatContext *) mlt_properties_get_data( properties, "video_context", NULL );
391         if (context != NULL) {
392                 // Get the video_index
393                 int index = mlt_properties_get_int( properties, "video_index" );
394                 filePropertyMap["videocodec"] = context->streams[ index ]->codec->codec->name;
395         }
396         context = (AVFormatContext *) mlt_properties_get_data( properties, "audio_context", NULL );
397         if (context != NULL) {
398                 // Get the video_index
399                 int index = mlt_properties_get_int( properties, "audio_index" );
400                 filePropertyMap["audiocodec"] = context->streams[ index ]->codec->codec->name;
401         }
402
403
404
405             // metadata
406
407             mlt_properties metadata = mlt_properties_new( );
408             mlt_properties_pass( metadata, properties, "meta.attr." );
409             int count = mlt_properties_count( metadata );
410             for ( int i = 0; i < count; i ++ )
411             {
412                 QString name = mlt_properties_get_name( metadata, i );
413                 QString value = QString::fromUtf8(mlt_properties_get_value( metadata, i ));
414                 if (name.endsWith("markup") && !value.isEmpty())
415                         metadataPropertyMap[ name.section(".", 0, -2) ] = value;
416             }
417
418             if (frame->get_int("test_image") == 0) {
419                 if (url.path().endsWith(".westley") || url.path().endsWith(".kdenlive")) {
420                     filePropertyMap["type"] = "playlist";
421                     metadataPropertyMap["comment"] = QString::fromUtf8(mlt_properties_get( MLT_SERVICE_PROPERTIES( producer.get_service() ), "title"));
422                 }
423                 else if (frame->get_int("test_audio") == 0)
424                     filePropertyMap["type"] = "av";
425                 else
426                     filePropertyMap["type"] = "video";
427
428                 // Generate thumbnail for this frame
429                 QPixmap pixmap = frameThumbnail(frame, width, height, true);
430
431                 emit replyGetImage(clipId, 0, pixmap, width, height);
432
433             } else if (frame->get_int("test_audio") == 0) {
434                 QPixmap pixmap(KStandardDirs::locate("appdata", "graphics/music.png"));
435                 emit replyGetImage(clipId, 0, pixmap, width, height);
436                 filePropertyMap["type"] = "audio";
437             }
438         }
439         emit replyGetFileProperties(clipId, filePropertyMap, metadataPropertyMap);
440         kDebug()<<"REquested fuile info for: "<<url.path();
441         delete frame;
442 }
443
444 QDomDocument Render::sceneList() const
445 {
446     return m_sceneList;
447 }
448
449 /** Create the producer from the Westley QDomDocument */
450 void Render::initSceneList()
451 {
452     kDebug()<<"--------  INIT SCENE LIST ------_";
453     QDomDocument doc;
454     QDomElement westley = doc.createElement("westley");
455     doc.appendChild(westley);
456     QDomElement prod = doc.createElement("producer");
457     prod.setAttribute("resource", "colour");
458     prod.setAttribute("colour", "red");
459     prod.setAttribute("id", "black");
460     prod.setAttribute("in", "0");
461     prod.setAttribute("out", "0");
462
463     QDomElement tractor = doc.createElement("tractor");
464     QDomElement multitrack = doc.createElement("multitrack"); 
465
466     QDomElement playlist1 = doc.createElement("playlist");
467     playlist1.appendChild(prod);
468     multitrack.appendChild(playlist1);
469     QDomElement playlist2 = doc.createElement("playlist");
470     multitrack.appendChild(playlist2);
471     QDomElement playlist3 = doc.createElement("playlist");
472     multitrack.appendChild(playlist3);
473     QDomElement playlist4 = doc.createElement("playlist");
474     multitrack.appendChild(playlist4);
475     QDomElement playlist5 = doc.createElement("playlist");
476     multitrack.appendChild(playlist5);
477     tractor.appendChild(multitrack);
478     westley.appendChild(tractor);
479     // kDebug()<<doc.toString();
480 /*
481    QString tmp = QString("<westley><producer resource=\"colour\" colour=\"red\" id=\"red\" /><tractor><multitrack><playlist></playlist><playlist></playlist><playlist /><playlist /><playlist></playlist></multitrack></tractor></westley>");*/
482     setSceneList(doc, 0);
483 }
484
485
486 /** Create the producer from the Westley QDomDocument */
487 void Render::setSceneList(QDomDocument list, int position)
488 {
489     if (m_winid == -1) return;
490     m_generateScenelist = true;
491
492     kWarning()<<"//////  RENDER, SET SCENE LIST";
493
494
495 /*
496     if (!clip.is_valid()) {
497         kWarning()<<" ++++ WARNING, UNABLE TO CREATE MLT PRODUCER";
498         m_generateScenelist = false;
499         return;
500     }*/
501
502     if (m_mltConsumer) {
503         m_mltConsumer->set("refresh", 0);
504         if (!m_mltConsumer->is_stopped()) {
505         //emitConsumerStopped();
506         m_mltConsumer->stop();
507         }
508     }
509
510     if (m_mltProducer) {
511         m_mltProducer->set_speed(0.0);
512
513         //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
514
515         delete m_mltProducer;
516         m_mltProducer = NULL;
517         emit stopped();
518     }
519
520     char *tmp = decodedString(list.toString());
521     //Mlt::Producer clip(profile, "westley-xml", tmp);
522
523     m_mltProducer = new Mlt::Producer(*m_mltProfile, "westley-xml", tmp);
524     delete[] tmp;
525     //m_mltProducer->optimise();
526     if (position != 0) m_mltProducer->seek(position);
527
528     /*if (KdenliveSettings::osdtimecode()) {
529                 // Attach filter for on screen display of timecode
530                 delete m_osdInfo;
531                 QString attr = "attr_check";
532                 mlt_filter filter = mlt_factory_filter( "data_feed", (char*) attr.ascii() );
533                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_fezzik", 1 );
534                 mlt_producer_attach( m_mltProducer->get_producer(), filter );
535                 mlt_filter_close( filter );
536
537                 m_osdInfo = new Mlt::Filter("data_show");
538                 tmp = decodedString(m_osdProfile);
539                 m_osdInfo->set("resource", tmp);
540                 delete[] tmp;
541                 mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
542                 mlt_properties_set_int( properties, "meta.attr.timecode", 1);
543                 mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
544                 m_osdInfo->set("dynamic", "1");
545
546                 if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
547         } else {
548                 m_osdInfo->set("dynamic", "0");
549         }*/
550
551         m_fps = m_mltProducer->get_fps();
552
553         emit playListDuration(m_mltProducer->get_playtime());
554         //m_connectTimer->start( 500 );
555         connectPlaylist();
556         m_generateScenelist = false;
557   
558 }
559
560 const double Render::fps() const
561 {
562     return m_fps;
563 }
564
565 void Render::connectPlaylist() {
566
567         m_connectTimer->stop();
568         m_mltConsumer->connect(*m_mltProducer);
569         m_mltProducer->set_speed(0.0);
570         m_mltConsumer->start();
571         //refresh();
572 /*
573         if (m_mltConsumer->start() == -1) {
574                 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."));
575                 m_mltConsumer = NULL;
576         }
577         else {
578             refresh();
579         }*/
580 }
581
582 void Render::refreshDisplay() {
583
584         if (!m_mltProducer) return;
585         m_mltConsumer->set("refresh", 0);
586
587         mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
588         /*if (KdenliveSettings::osdtimecode()) {
589             mlt_properties_set_int( properties, "meta.attr.timecode", 1);
590             mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
591             m_osdInfo->set("dynamic", "1");
592             m_mltProducer->attach(*m_osdInfo);
593         }
594         else {
595             m_mltProducer->detach(*m_osdInfo);
596             m_osdInfo->set("dynamic", "0");
597         }*/
598         refresh();
599 }
600
601 void Render::setVolume(double volume)
602 {
603     if (!m_mltConsumer || !m_mltProducer) return;
604     /*osdTimer->stop();
605         m_mltConsumer->set("refresh", 0);
606     // Attach filter for on screen display of timecode
607     mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
608     mlt_properties_set_double( properties, "meta.volume", volume );
609     mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
610     mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
611
612     if (!KdenliveSettings::osdtimecode()) {
613         m_mltProducer->detach(*m_osdInfo);
614         mlt_properties_set_int( properties, "meta.attr.timecode", 0);
615         if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
616     }*/
617     refresh();
618     osdTimer->setSingleShot(2500 );
619 }
620
621 void Render::slotOsdTimeout()
622 {
623     mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
624     mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
625     mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
626     //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
627     refresh();
628 }
629
630 void Render::start()
631 {
632     kDebug()<<"-----  STARTING MONITOR: "<<m_name;
633     if (m_winid == -1) {
634     kDebug()<<"-----  BROKEN MONITOR: "<<m_name<<", RESTART";
635         return;
636     }
637
638     if (m_mltConsumer->is_stopped()) {
639     kDebug()<<"-----  MONITOR: "<<m_name<<" WAS STOPPED";
640         if (m_mltConsumer->start() == -1) {
641             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."));
642             m_mltConsumer = NULL;
643             return;
644         }
645         else {
646           kDebug()<<"-----  MONITOR: "<<m_name<<" REFRESH";
647                 refresh();
648         }
649     }
650     m_isBlocked = false;
651 }
652
653 void Render::clear()
654 {
655     if (m_mltConsumer) {
656         m_mltConsumer->set("refresh", 0);
657         if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
658     }
659
660     if (m_mltProducer) {
661         //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
662         m_mltProducer->set_speed(0.0);
663         delete m_mltProducer;
664         m_mltProducer = NULL;
665         emit stopped();
666     }
667 }
668
669 void Render::stop()
670 {
671     if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
672         kDebug()<<"/////////////   RENDER STOPPED: "<<m_name;
673         m_mltConsumer->set("refresh", 0);
674         m_mltConsumer->stop();
675     }
676     kDebug()<<"/////////////   RENDER STOP2-------";
677     m_isBlocked = true;
678
679     if (m_mltProducer) {
680         m_mltProducer->set_speed(0.0);
681         m_mltProducer->set("out", m_mltProducer->get_length() - 1);
682     }
683     kDebug()<<"/////////////   RENDER STOP3-------";
684 }
685
686 void Render::stop(const GenTime & startTime)
687 {
688     kDebug()<<"/////////////   RENDER STOP-------2";
689     if (m_mltProducer) {
690         m_mltProducer->set_speed(0.0);
691         m_mltProducer->seek((int) startTime.frames(m_fps));
692     }
693     m_mltConsumer->purge();
694 }
695
696 void Render::switchPlay()
697 {
698     if (!m_mltProducer)
699         return;
700     if (m_mltProducer->get_speed() == 0.0) m_mltProducer->set_speed(1.0);
701     else {
702       m_mltProducer->set_speed(0.0);
703       kDebug()<<"// POSITON: "<<m_framePosition;
704       m_mltProducer->seek((int) m_framePosition);
705     }
706
707     /*if (speed == 0.0) {
708         m_mltProducer->seek((int) m_framePosition + 1);
709         m_mltConsumer->purge();
710     }*/
711     refresh();
712 }
713
714 void Render::play(double speed)
715 {
716     if (!m_mltProducer)
717         return;
718     if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
719     m_mltProducer->set_speed(speed);
720     /*if (speed == 0.0) {
721         m_mltProducer->seek((int) m_framePosition + 1);
722         m_mltConsumer->purge();
723     }*/
724     refresh();
725 }
726
727 void Render::play(double speed, const GenTime & startTime)
728 {
729     kDebug()<<"/////////////   RENDER PLAY2-------"<<speed;
730     if (!m_mltProducer)
731         return;
732     //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
733     //if (speed == 0.0) m_mltConsumer->set("refresh", 0);
734     m_mltProducer->set_speed(speed);
735     m_mltProducer->seek((int) (startTime.frames(m_fps)));
736     //m_mltConsumer->purge();
737     //refresh();
738 }
739
740 void Render::play(double speed, const GenTime & startTime,
741     const GenTime & stopTime)
742 {
743     kDebug()<<"/////////////   RENDER PLAY3-------"<<speed;
744     if (!m_mltProducer)
745         return;
746     m_mltProducer->set("out", stopTime.frames(m_fps));
747     m_mltProducer->seek((int) (startTime.frames(m_fps)));
748     m_mltConsumer->purge();
749     m_mltProducer->set_speed(speed);
750     refresh();
751 }
752
753 void Render::render(const KUrl & url)
754 {
755     QDomDocument doc;
756     QDomElement elem = doc.createElement("render");
757     elem.setAttribute("filename", url.path());
758     doc.appendChild(elem);
759 }
760
761 void Render::sendSeekCommand(GenTime time)
762 {
763     if (!m_mltProducer)
764         return;
765     //kDebug()<<"//////////  KDENLIVE SEEK: "<<(int) (time.frames(m_fps));
766     m_mltProducer->seek((int) (time.frames(m_fps)));
767     refresh();
768 }
769
770 void Render::seekToFrame(int pos)
771 {
772     if (!m_mltProducer)
773         return;
774     //kDebug()<<"//////////  KDENLIVE SEEK: "<<(int) (time.frames(m_fps));
775     m_mltProducer->seek(pos);
776     refresh();
777 }
778
779 void Render::askForRefresh()
780 {
781     // Use a Timer so that we don't refresh too much
782     refreshTimer->start( 200 );
783 }
784
785 void Render::doRefresh()
786 {
787     // Use a Timer so that we don't refresh too much
788     refresh();
789 }
790
791 void Render::refresh()
792 {
793     if (!m_mltProducer || m_isBlocked)
794         return;
795     refreshTimer->stop();
796     if (m_mltConsumer) {
797         m_mltConsumer->set("refresh", 1);
798     }
799 }
800
801 /** Sets the description of this renderer to desc. */
802 void Render::setDescription(const QString & description)
803 {
804     m_description = description;
805 }
806
807 /** Returns the description of this renderer */
808 QString Render::description()
809 {
810     return m_description;
811 }
812
813
814 double Render::playSpeed()
815 {
816     if (m_mltProducer) return m_mltProducer->get_speed();
817     return 0.0;
818 }
819
820 const GenTime & Render::seekPosition() const
821 {
822     if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
823     else return GenTime();
824 }
825
826
827 const QString & Render::rendererName() const
828 {
829     return m_name;
830 }
831
832
833 void Render::emitFrameNumber(double position)
834 {
835       //kDebug()<<"// POSITON: "<<m_framePosition;
836         if (m_generateScenelist) return;
837         m_framePosition = position;
838         emit rendererPosition((int) position);
839         //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent( GenTime((int) position, m_fps), m_monitorId));
840 }
841
842 void Render::emitConsumerStopped()
843 {
844     // This is used to know when the playing stopped
845     if (m_mltProducer && !m_generateScenelist) {
846         double pos = m_mltProducer->position();
847         emit rendererStopped((int) pos);
848         //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent(GenTime((int) pos, m_fps), m_monitorId + 100));
849         //new QCustomEvent(10002));
850     }
851 }
852
853
854
855 void Render::exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime)
856 {
857 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
858 }
859
860
861 void Render::exportCurrentFrame(KUrl url, bool notify) {
862     if (!m_mltProducer) {
863         KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
864         return;
865     }
866
867     int height = 1080;//KdenliveSettings::defaultheight();
868     int width = 1940; //KdenliveSettings::displaywidth();
869
870     QPixmap pix(width, height);
871     Mlt::Filter m_convert(*m_mltProfile, "avcolour_space");
872     m_convert.set("forced", mlt_image_rgb24a);
873     m_mltProducer->attach(m_convert);
874     Mlt::Frame * frame = m_mltProducer->get_frame();
875     m_mltProducer->detach(m_convert);
876     if (frame) {
877         pix = frameThumbnail(frame, width, height);
878         delete frame;
879     }
880     pix.save(url.path(), "PNG");
881     //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
882 }
883
884 /**     MLT PLAYLIST DIRECT MANIPULATON         **/
885
886
887 void Render::mltCheckLength()
888 {
889     //kDebug()<<"checking track length: "<<track<<"..........";
890     Mlt::Service service(m_mltProducer->get_service());
891     Mlt::Tractor tractor(service);
892
893     int trackNb = tractor.count( );
894     double duration = 0;
895     double trackDuration;
896     if (trackNb == 1) {
897         Mlt::Producer trackProducer(tractor.track(0));
898         Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
899         duration = Mlt::Producer(trackPlaylist.get_producer()).get_playtime() - 1;
900         m_mltProducer->set("out", duration);
901         emit playListDuration(duration);
902         return;
903     }
904     while (trackNb > 1) {
905         Mlt::Producer trackProducer(tractor.track(trackNb - 1));
906         Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
907         trackDuration = Mlt::Producer(trackPlaylist.get_producer()).get_playtime() - 1;
908
909         kDebug()<<" / / /DURATON FOR TRACK "<<trackNb - 1<<" = "<<trackDuration;
910         if (trackDuration > duration) duration = trackDuration;
911         trackNb--;
912     }
913
914     Mlt::Producer blackTrackProducer(tractor.track(0));
915     Mlt::Playlist blackTrackPlaylist(( mlt_playlist ) blackTrackProducer.get_service());
916     double blackDuration = Mlt::Producer(blackTrackPlaylist.get_producer()).get_playtime() - 1;
917         kDebug()<<" / / /DURATON FOR TRACK 0 = "<<blackDuration;
918     if (blackDuration != duration) {
919         blackTrackPlaylist.remove_region( 0, blackDuration );
920         int i = 0;
921         int dur = duration;
922         QDomDocument doc;
923         QDomElement black = doc.createElement("producer");
924         black.setAttribute("mlt_service", "colour");
925         black.setAttribute("colour", "black");
926         black.setAttribute("in", "0");
927         black.setAttribute("out", "13999");
928         while (dur > 14000) { // <producer mlt_service=\"colour\" colour=\"black\" in=\"0\" out=\"13999\" />
929             mltInsertClip(0, GenTime(i * 14000, m_fps), black);
930             dur = dur - 14000;
931             i++;
932         }
933         black.setAttribute("out", QString::number(dur));
934         mltInsertClip(0, GenTime(), black);
935
936         m_mltProducer->set("out", duration);
937         emit playListDuration(duration);
938     }
939 }
940
941
942 void Render::mltInsertClip(int track, GenTime position, QDomElement element)
943 {
944     if (!m_mltProducer) {
945         kDebug()<<"PLAYLIST NOT INITIALISED //////";
946         return;
947     }
948     Mlt::Producer parentProd(m_mltProducer->parent());
949     if (parentProd.get_producer() == NULL) {
950         kDebug()<<"PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
951         return;
952     }
953     m_isBlocked = true;
954     Mlt::Service service(parentProd.get_service());
955     Mlt::Tractor tractor(service);
956
957     QDomDocument doc;
958     doc.appendChild(doc.importNode(element, true));
959     QString resource = doc.toString();
960     kDebug()<<"///////  ADDING CLIP TMLNE: "<<resource<<" ON TRACK: "<<track;
961     Mlt::Producer trackProducer(tractor.track(track));
962     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
963     char *tmp = decodedString(resource);
964     Mlt::Producer clip(*m_mltProfile, "westley-xml", tmp);
965     //clip.set_in_and_out(in.frames(m_fps), out.frames(m_fps));
966     delete[] tmp;
967
968     trackPlaylist.insert_at(position.frames(m_fps), clip, 1);
969     tractor.multitrack()->refresh();
970     tractor.refresh();
971     if (track != 0) mltCheckLength();
972     m_isBlocked = false;
973 }
974
975 void Render::mltCutClip(int track, GenTime position)
976 {
977     m_isBlocked = true;
978     Mlt::Service service(m_mltProducer->parent().get_service());
979     Mlt::Tractor tractor(service);
980     Mlt::Producer trackProducer(tractor.track(track));
981     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
982     trackPlaylist.split_at(position.frames(m_fps));
983     trackPlaylist.consolidate_blanks(0);
984     kDebug()<<"/ / / /CUTTING CLIP AT: "<<position.frames(m_fps);
985     m_isBlocked = false;
986 }
987
988
989 void Render::mltRemoveClip(int track, GenTime position)
990 {
991     m_isBlocked = true;
992     Mlt::Service service(m_mltProducer->parent().get_service());
993     Mlt::Tractor tractor(service);
994     Mlt::Producer trackProducer(tractor.track(track));
995     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
996     int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
997     //trackPlaylist.remove(clipIndex);
998     trackPlaylist.replace_with_blank(clipIndex);
999     trackPlaylist.consolidate_blanks(0);
1000     if (track != 0) mltCheckLength();
1001     //emit durationChanged();
1002     m_isBlocked = false;
1003 }
1004
1005 void Render::mltRemoveEffect(int track, GenTime position, QString tag, int index)
1006 {
1007
1008     Mlt::Service service(m_mltProducer->parent().get_service());
1009
1010     Mlt::Tractor tractor(service);
1011     Mlt::Producer trackProducer(tractor.track(track));
1012     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1013     //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1014     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1015     if (!clip) {
1016         kDebug()<<" / / / CANNOT FIND CLIP TO REMOVE EFFECT";
1017         return;
1018     }
1019     m_isBlocked = true;
1020     Mlt::Service clipService(clip->get_service());
1021
1022     if (tag.startsWith("ladspa")) tag = "ladspa";
1023
1024     if (index == -1) {
1025         int ct = 0;
1026         Mlt::Filter *filter = clipService.filter( ct );
1027         while (filter) {
1028             if (filter->get("mlt_service") == tag) {// && filter->get("kdenlive_id") == id) {
1029                 clipService.detach(*filter);
1030                 kDebug()<<" / / / DLEETED EFFECT: "<<ct;
1031             }
1032             else ct++;
1033             filter = clipService.filter( ct );
1034         }
1035     }
1036     else {
1037         Mlt::Filter *filter = clipService.filter( index );
1038         if (filter && filter->get("mlt_service") == tag /*&& filter->get("kdenlive_id") == id*/) clipService.detach(*filter);
1039         else {
1040             kDebug()<<"WARINIG, FILTER "<<tag<<" NOT FOUND!!!!!";
1041         }
1042     }
1043     m_isBlocked = false;
1044     refresh();
1045 }
1046
1047
1048 void Render::mltAddEffect(int track, GenTime position, QString tag, QMap <QString, QString> args)
1049 {
1050     Mlt::Service service(m_mltProducer->parent().get_service());
1051     Mlt::Tractor tractor(service);
1052     Mlt::Producer trackProducer(tractor.track(track));
1053     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1054
1055     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1056
1057     if (!clip) {
1058         kDebug()<<"**********  CANNOT FIND CLIP TO APPLY EFFECT-----------";
1059         return;
1060     }
1061     Mlt::Service clipService(clip->get_service());
1062     m_isBlocked = true;
1063     // create filter
1064     //kDebug()<<" / / INSERTING EFFECT: "<<id;
1065     if (tag.startsWith("ladspa")) tag = "ladspa";
1066     char *filterId = decodedString(tag);
1067     Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterId);
1068     filter->set("kdenlive_id", filterId);
1069
1070     QMap<QString, QString>::Iterator it;
1071     QString keyFrameNumber = "#0";
1072
1073     for ( it = args.begin(); it != args.end(); ++it ) {
1074     //kDebug()<<" / / INSERTING EFFECT ARGS: "<<it.key()<<": "<<it.data();
1075         QString key;
1076         QString currentKeyFrameNumber;
1077         if (it.key().startsWith("#")) {
1078             currentKeyFrameNumber = it.key().section(":", 0, 0);
1079             if (currentKeyFrameNumber != keyFrameNumber) {
1080                 // attach filter to the clip
1081                 clipService.attach(*filter);
1082                 filter = new Mlt::Filter(*m_mltProfile, filterId);
1083                 filter->set("kdenlive_id", filterId);
1084                 keyFrameNumber = currentKeyFrameNumber;
1085             }
1086             key = it.key().section(":", 1);
1087         }
1088         else key = it.key();
1089         char *name = decodedString(key);
1090         char *value = decodedString(it.value());
1091         filter->set(name, value);
1092         delete[] name;
1093         delete[] value;
1094     }
1095     // attach filter to the clip
1096     clipService.attach(*filter);
1097     delete[] filterId;
1098     m_isBlocked = false;
1099     refresh();
1100
1101 }
1102
1103 void Render::mltEditEffect(int track, GenTime position, int index, QString id, QString tag, QMap <QString, QString> args)
1104 {
1105     QMap<QString, QString>::Iterator it = args.begin();
1106     if (it.key().startsWith("#") || tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle") {
1107         // This is a keyframe effect, to edit it, we remove it and re-add it.
1108         mltRemoveEffect(track, position, tag, -1);
1109         mltAddEffect(track, position, tag, args);
1110         return;
1111     }
1112     m_isBlocked = true;
1113     // create filter
1114     Mlt::Service service(m_mltProducer->parent().get_service());
1115
1116     Mlt::Tractor tractor(service);
1117     Mlt::Producer trackProducer(tractor.track(track));
1118     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1119     //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1120     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1121     if (!clip) {
1122       kDebug()<<"WARINIG, CANNOT FIND CLIP ON track: "<<track<<", AT POS: "<<position.frames(m_fps);
1123       m_isBlocked = false;
1124       return;
1125     }
1126     Mlt::Service clipService(clip->get_service());
1127     Mlt::Filter *filter = clipService.filter( index );
1128
1129
1130     if (!filter || filter->get("mlt_service") != tag) {
1131         kDebug()<<"WARINIG, FILTER NOT FOUND!!!!!";
1132         int index = 0;
1133         filter = clipService.filter( index );
1134         while (filter) {
1135             if (filter->get("mlt_service") == tag && filter->get("kdenlive_id") == id) break;
1136             index++;
1137             filter = clipService.filter( index );
1138         }
1139     }
1140     if (!filter) {
1141         kDebug()<<"WARINIG, FILTER "<<id<<" NOT FOUND!!!!!";
1142         m_isBlocked = false;
1143         return;
1144     }
1145
1146     for ( it = args.begin(); it != args.end(); ++it ) {
1147     kDebug()<<" / / INSERTING EFFECT ARGS: "<<it.key()<<": "<<it.value();
1148         char *name = decodedString(it.key());
1149         char *value = decodedString(it.value());
1150         filter->set(name, value);
1151         delete[] name;
1152         delete[] value;
1153     }
1154     m_isBlocked = false;
1155     refresh();
1156 }
1157
1158 void Render::mltResizeClipEnd(int track, GenTime pos, GenTime in, GenTime out)
1159 {
1160     m_isBlocked = true;
1161     Mlt::Service service(m_mltProducer->parent().get_service());
1162
1163     Mlt::Tractor tractor(service);
1164     Mlt::Producer trackProducer(tractor.track(track));
1165     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1166     if (trackPlaylist.is_blank_at(pos.frames(m_fps) + 1)) 
1167         kDebug()<<"////////  ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
1168     int clipIndex = trackPlaylist.get_clip_index_at(pos.frames(m_fps) + 1);
1169
1170     int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
1171     int newDuration = out.frames(m_fps) - 1;
1172
1173     kDebug()<<" ** RESIZING CLIP END:" << clipIndex << " on track:"<< track <<", mid pos: "<<pos.frames(25)<<", in: "<<in.frames(25)<<", out: "<<out.frames(25)<<", PREVIOUS duration: "<<previousDuration;
1174     trackPlaylist.resize_clip(clipIndex, in.frames(m_fps), newDuration);
1175     trackPlaylist.consolidate_blanks(0);
1176     if (previousDuration < newDuration) {
1177         // clip was made longer, trim next blank if there is one.
1178         if (trackPlaylist.is_blank(clipIndex + 1)) {
1179             trackPlaylist.split(clipIndex + 1, newDuration - previousDuration);
1180             trackPlaylist.remove(clipIndex + 1);
1181         }
1182     }
1183     else trackPlaylist.insert_blank(clipIndex + 1, previousDuration - newDuration - 1);
1184
1185     trackPlaylist.consolidate_blanks(0);
1186     tractor.multitrack()->refresh();
1187     tractor.refresh();
1188     if (track != 0) mltCheckLength();
1189     m_isBlocked = false;
1190 }
1191
1192 void Render::mltChangeTrackState(int track, bool mute, bool blind)
1193 {
1194     Mlt::Service service(m_mltProducer->parent().get_service());
1195
1196     Mlt::Tractor tractor(service);
1197     Mlt::Producer trackProducer(tractor.track(track));
1198     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1199     if (mute) {
1200         if (blind) trackProducer.set("hide", 3);
1201         else trackProducer.set("hide", 2);
1202     }
1203     else if (blind) {
1204         trackProducer.set("hide", 1);
1205     }
1206     else {
1207         trackProducer.set("hide", 0);
1208     }
1209     tractor.multitrack()->refresh();
1210     tractor.refresh();
1211     refresh();
1212 }
1213
1214 void Render::mltResizeClipStart(int track, GenTime pos, GenTime moveEnd, GenTime moveStart, GenTime in, GenTime out)
1215 {
1216     m_isBlocked = true;
1217     Mlt::Service service(m_mltProducer->parent().get_service());
1218     int moveFrame = (moveEnd - moveStart).frames(m_fps);
1219
1220     Mlt::Tractor tractor(service);
1221     Mlt::Producer trackProducer(tractor.track(track));
1222     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1223     if (trackPlaylist.is_blank_at(pos.frames(m_fps) - 1)) 
1224         kDebug()<<"////////  ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
1225     int clipIndex = trackPlaylist.get_clip_index_at(pos.frames(m_fps) - 1);
1226     kDebug()<<" ** RESIZING CLIP START:" << clipIndex << " on track:"<< track <<", mid pos: "<<pos.frames(25)<<", moving: "<<moveFrame<<", in: "<<in.frames(25)<<", out: "<<out.frames(25);
1227
1228     trackPlaylist.resize_clip(clipIndex, in.frames(m_fps), out.frames(m_fps));
1229     if (moveFrame > 0) trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
1230     else {
1231         int midpos = moveStart.frames(m_fps) - 1; //+ (moveFrame / 2)
1232         int blankIndex = trackPlaylist.get_clip_index_at(midpos);
1233         int blankLength = trackPlaylist.clip_length(blankIndex);
1234
1235         kDebug()<<" + resizing blank: "<<blankIndex<<", Mid: "<<midpos<<", Length: "<<blankLength<< ", SIZE DIFF: "<<moveFrame;
1236
1237         
1238         if (blankLength + moveFrame == 0) trackPlaylist.remove(blankIndex);
1239         else trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame -1);
1240     }
1241     trackPlaylist.consolidate_blanks(0);
1242     m_isBlocked = false;
1243     kDebug()<<"-----------------\n"<<"CLIP 0: "<<trackPlaylist.clip_start(0)<<", LENGT: "<<trackPlaylist.clip_length(0);
1244     kDebug()<<"CLIP 1: "<<trackPlaylist.clip_start(1)<<", LENGT: "<<trackPlaylist.clip_length(1);
1245     kDebug()<<"CLIP 2: "<<trackPlaylist.clip_start(2)<<", LENGT: "<<trackPlaylist.clip_length(2);
1246     kDebug()<<"CLIP 3: "<<trackPlaylist.clip_start(3)<<", LENGT: "<<trackPlaylist.clip_length(3);
1247    kDebug()<<"CLIP 4: "<<trackPlaylist.clip_start(4)<<", LENGT: "<<trackPlaylist.clip_length(4);
1248 }
1249
1250 void Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd)
1251 {
1252     mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps));
1253 }
1254
1255
1256 void Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd)
1257 {
1258     m_isBlocked = true;
1259     //m_mltConsumer->set("refresh", 0);
1260     Mlt::Service service(m_mltProducer->parent().get_service());
1261
1262     Mlt::Tractor tractor(service);
1263     Mlt::Producer trackProducer(tractor.track(startTrack));
1264     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1265     int clipIndex = trackPlaylist.get_clip_index_at(moveStart + 1);
1266     mlt_field field = mlt_tractor_field(tractor.get_tractor());
1267     mlt_multitrack multitrack = mlt_field_multitrack(field); //mlt_tractor_multitrack(tractor.get_tractor());
1268     kDebug()<<" --  CURRENT MULTIOTRACK HAS: "<<mlt_multitrack_count(multitrack)<<" tracks";;
1269     mlt_service multiprod = mlt_multitrack_service( multitrack );
1270
1271     Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
1272     trackPlaylist.consolidate_blanks(0);
1273     mlt_events_block( MLT_PRODUCER_PROPERTIES(trackProducer.get_producer()), NULL );
1274
1275     if (endTrack == startTrack) {
1276         if (!trackPlaylist.is_blank_at(moveEnd)) {
1277             kWarning()<<"// ERROR, CLIP COLLISION----------";
1278             int ix = trackPlaylist.get_clip_index_at(moveEnd);
1279                 kDebug()<<"BAD CLIP STARTS AT: "<<trackPlaylist.clip_start(ix)<<", LENGT: "<<trackPlaylist.clip_length(ix);
1280         }
1281         trackPlaylist.insert_at(moveEnd, clipProducer, 1);
1282         trackPlaylist.consolidate_blanks(0);
1283     }
1284     else {
1285         trackPlaylist.consolidate_blanks(0);
1286         Mlt::Producer destTrackProducer(tractor.track(endTrack));
1287         Mlt::Playlist destTrackPlaylist(( mlt_playlist ) destTrackProducer.get_service());
1288         destTrackPlaylist.consolidate_blanks(1);
1289         destTrackPlaylist.insert_at(moveEnd, clipProducer, 1);
1290         destTrackPlaylist.consolidate_blanks(0);
1291     }
1292
1293     mltCheckLength();
1294     mlt_events_unblock( MLT_PRODUCER_PROPERTIES(trackProducer.get_producer()), NULL );
1295     m_isBlocked = false;
1296     m_mltConsumer->set("refresh", 1);
1297 }
1298
1299 void Render::mltMoveTransition(QString type, int startTrack, int trackOffset, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
1300 {
1301     m_isBlocked = true;
1302     m_mltConsumer->set("refresh", 0);
1303     mlt_service serv = m_mltProducer->parent().get_service();
1304
1305     mlt_service nextservice = mlt_service_get_producer(serv);
1306     mlt_properties properties = MLT_SERVICE_PROPERTIES( nextservice );
1307     QString mlt_type = mlt_properties_get( properties, "mlt_type" );
1308     QString resource = mlt_properties_get( properties, "mlt_service");
1309     int old_pos = (oldIn.frames(m_fps) + oldOut.frames(m_fps)) / 2;
1310
1311     int new_in = newIn.frames(m_fps);
1312     int new_out = newOut.frames(m_fps) - 1;
1313     while (mlt_type == "transition") {
1314         mlt_transition tr = (mlt_transition) nextservice;
1315         int currentTrack = mlt_transition_get_b_track(tr);
1316         int currentIn = (int) mlt_transition_get_in(tr);
1317         int currentOut = (int) mlt_transition_get_out(tr);
1318         kDebug()<<"// FOUND EXISTING TRANS, IN: "<<currentIn<<", OUT: "<<currentOut<<", TRACK: "<<currentTrack;
1319         //kDebug()<<"// LOOKING FOR IN: "<<old_in<<", OUT: "<<old_out;
1320         kDebug()<<"// OLD IN: "<<oldIn.frames(m_fps)<<" // OLD OUT: "<<oldOut.frames(m_fps)<<", TRACK: "<<startTrack<<", MID POS: "<<old_pos;
1321         if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
1322             mlt_transition_set_in_and_out(tr, new_in, new_out);
1323             if (trackOffset != 0) {
1324                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( tr );
1325                 mlt_properties_set_int( properties, "a_track", mlt_transition_get_a_track(tr) + trackOffset );
1326                 mlt_properties_set_int( properties, "b_track", mlt_transition_get_b_track(tr) + trackOffset );
1327             }
1328             break;
1329         }
1330         nextservice = mlt_service_producer(nextservice);
1331         properties = MLT_SERVICE_PROPERTIES( nextservice );
1332         mlt_type = mlt_properties_get( properties, "mlt_type" );
1333         resource = mlt_properties_get( properties, "mlt_service" );
1334     }
1335     m_isBlocked = false;
1336 }
1337
1338 void Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QMap <QString, QString> args)
1339 {
1340     m_isBlocked = true;
1341     Mlt::Service service(m_mltProducer->parent().get_service());
1342
1343     Mlt::Tractor tractor(service);
1344     Mlt::Field *field = tractor.field();
1345     char *transId = decodedString(tag);
1346     Mlt::Transition *transition = new Mlt::Transition(*m_mltProfile, transId);
1347     transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps));
1348     QMap<QString, QString>::Iterator it;
1349     QString key;
1350
1351     kDebug()<<" ------  ADDING TRANSITION PARAMs: "<<args.count();
1352
1353     for ( it = args.begin(); it != args.end(); ++it ) {
1354         key = it.key();
1355         char *name = decodedString(key);
1356         char *value = decodedString(it.value());
1357         transition->set(name, value);
1358         kDebug()<<" ------  ADDING TRANS PARAM: "<<name<<": "<<value;
1359         //filter->set("kdenlive_id", id);
1360         delete[] name;
1361         delete[] value;
1362     }
1363     // attach filter to the clip
1364     field->plant_transition(*transition, a_track, b_track);
1365     delete[] transId;
1366     m_isBlocked = false;
1367     refresh();
1368
1369 }
1370
1371 void Render::mltSavePlaylist()
1372 {
1373     kWarning()<<"// UPDATING PLAYLIST TO DISK++++++++++++++++";
1374     Mlt::Consumer *fileConsumer = new Mlt::Consumer(*m_mltProfile, "westley");
1375     fileConsumer->set("resource", "/home/one/playlist.xml");
1376
1377     Mlt::Service service(m_mltProducer->get_service());
1378     Mlt::Tractor tractor(service);
1379
1380     fileConsumer->connect(service);
1381     fileConsumer->start();
1382
1383 }
1384
1385 #include "renderer.moc"