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