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