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