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