]> git.sesse.net Git - kdenlive/blob - src/renderer.cpp
a9081928c81cec058f96399af903c3b3f682964e
[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         emit playListDuration( (int) m_mltProducer->get_playtime());
597         m_connectTimer->start( 500 );
598         m_generateScenelist = false;
599   
600 }
601
602 const double Render::fps() const
603 {
604     return m_fps;
605 }
606
607 void Render::connectPlaylist() {
608         kDebug()<<"**************  CONNECTING PLAYLIST";
609         m_connectTimer->stop();
610         if (m_mltConsumer->start() == -1) {
611                 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."));
612                 m_mltConsumer = NULL;
613         }
614         else {
615             m_mltConsumer->connect(*m_mltProducer);
616             m_mltProducer->set_speed(0.0);
617             refresh();
618         }
619 }
620
621 void Render::refreshDisplay() {
622
623         if (!m_mltProducer) return;
624         m_mltConsumer->set("refresh", 0);
625
626         mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
627         /*if (KdenliveSettings::osdtimecode()) {
628             mlt_properties_set_int( properties, "meta.attr.timecode", 1);
629             mlt_properties_set( properties, "meta.attr.timecode.markup", "#timecode#");
630             m_osdInfo->set("dynamic", "1");
631             m_mltProducer->attach(*m_osdInfo);
632         }
633         else {
634             m_mltProducer->detach(*m_osdInfo);
635             m_osdInfo->set("dynamic", "0");
636         }*/
637         refresh();
638 }
639
640 void Render::setVolume(double volume)
641 {
642     if (!m_mltConsumer || !m_mltProducer) return;
643     /*osdTimer->stop();
644         m_mltConsumer->set("refresh", 0);
645     // Attach filter for on screen display of timecode
646     mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
647     mlt_properties_set_double( properties, "meta.volume", volume );
648     mlt_properties_set_int( properties, "meta.attr.osdvolume", 1);
649     mlt_properties_set( properties, "meta.attr.osdvolume.markup", i18n("Volume: ") + QString::number(volume * 100));
650
651     if (!KdenliveSettings::osdtimecode()) {
652         m_mltProducer->detach(*m_osdInfo);
653         mlt_properties_set_int( properties, "meta.attr.timecode", 0);
654         if (m_mltProducer->attach(*m_osdInfo) == 1) kDebug()<<"////// error attaching filter";
655     }*/
656     refresh();
657     osdTimer->setSingleShot(2500 );
658 }
659
660 void Render::slotOsdTimeout()
661 {
662     mlt_properties properties = MLT_PRODUCER_PROPERTIES(m_mltProducer->get_producer());
663     mlt_properties_set_int(properties, "meta.attr.osdvolume", 0);
664     mlt_properties_set(properties, "meta.attr.osdvolume.markup", NULL);
665     //if (!KdenliveSettings::osdtimecode()) m_mltProducer->detach(*m_osdInfo);
666     refresh();
667 }
668
669 void Render::start()
670 {
671     if (!m_mltConsumer || m_winid == -1) {
672         restartConsumer();
673         return;
674     }
675
676     if (m_mltConsumer->is_stopped()) {
677         if (m_mltConsumer->start() == -1) {
678             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."));
679             m_mltConsumer = NULL;
680             return;
681         }
682         else {
683                 refresh();
684         }
685     }
686     isBlocked = false;
687 }
688
689 void Render::clear()
690 {
691     if (m_mltConsumer) {
692         m_mltConsumer->set("refresh", 0);
693         if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
694     }
695
696     if (m_mltProducer) {
697         //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
698         m_mltProducer->set_speed(0.0);
699         delete m_mltProducer;
700         m_mltProducer = NULL;
701         emit stopped();
702     }
703 }
704
705 void Render::stop()
706 {
707     kDebug()<<"/////////////   RENDER STOP-------";
708     if (m_mltConsumer && !m_mltConsumer->is_stopped()) {
709         m_mltConsumer->set("refresh", 0);
710         m_mltConsumer->stop();
711     }
712
713     isBlocked = true;
714
715     if (m_mltProducer) {
716         m_mltProducer->set_speed(0.0);
717         m_mltProducer->set("out", m_mltProducer->get_length() - 1);
718     }
719 }
720
721 void Render::stop(const GenTime & startTime)
722 {
723     kDebug()<<"/////////////   RENDER STOP-------2";
724     if (m_mltProducer) {
725         m_mltProducer->set_speed(0.0);
726         m_mltProducer->seek((int) startTime.frames(m_fps));
727     }
728     m_mltConsumer->purge();
729 }
730
731 void Render::switchPlay()
732 {
733     if (!m_mltProducer)
734         return;
735     if (m_mltProducer->get_speed() == 0.0) m_mltProducer->set_speed(1.0);
736     else {
737       m_mltProducer->set_speed(0.0);
738       kDebug()<<"// POSITON: "<<m_framePosition;
739       m_mltProducer->seek((int) m_framePosition);
740     }
741
742     /*if (speed == 0.0) {
743         m_mltProducer->seek((int) m_framePosition + 1);
744         m_mltConsumer->purge();
745     }*/
746     refresh();
747 }
748
749 void Render::play(double speed)
750 {
751     if (!m_mltProducer)
752         return;
753     if (speed == 0.0) m_mltProducer->set("out", m_mltProducer->get_length() - 1);
754     m_mltProducer->set_speed(speed);
755     /*if (speed == 0.0) {
756         m_mltProducer->seek((int) m_framePosition + 1);
757         m_mltConsumer->purge();
758     }*/
759     refresh();
760 }
761
762 void Render::play(double speed, const GenTime & startTime)
763 {
764     kDebug()<<"/////////////   RENDER PLAY2-------"<<speed;
765     if (!m_mltProducer)
766         return;
767     //m_mltProducer->set("out", m_mltProducer->get_length() - 1);
768     //if (speed == 0.0) m_mltConsumer->set("refresh", 0);
769     m_mltProducer->set_speed(speed);
770     m_mltProducer->seek((int) (startTime.frames(m_fps)));
771     //m_mltConsumer->purge();
772     //refresh();
773 }
774
775 void Render::play(double speed, const GenTime & startTime,
776     const GenTime & stopTime)
777 {
778     kDebug()<<"/////////////   RENDER PLAY3-------"<<speed;
779     if (!m_mltProducer)
780         return;
781     m_mltProducer->set("out", stopTime.frames(m_fps));
782     m_mltProducer->seek((int) (startTime.frames(m_fps)));
783     m_mltConsumer->purge();
784     m_mltProducer->set_speed(speed);
785     refresh();
786 }
787
788 void Render::render(const KUrl & url)
789 {
790     QDomDocument doc;
791     QDomElement elem = doc.createElement("render");
792     elem.setAttribute("filename", url.path());
793     doc.appendChild(elem);
794 }
795
796 void Render::sendSeekCommand(GenTime time)
797 {
798     if (!m_mltProducer)
799         return;
800     //kDebug()<<"//////////  KDENLIVE SEEK: "<<(int) (time.frames(m_fps));
801     m_mltProducer->seek((int) (time.frames(m_fps)));
802     refresh();
803 }
804
805 void Render::seekToFrame(int pos)
806 {
807     if (!m_mltProducer)
808         return;
809     //kDebug()<<"//////////  KDENLIVE SEEK: "<<(int) (time.frames(m_fps));
810     m_mltProducer->seek(pos);
811     refresh();
812 }
813
814 void Render::askForRefresh()
815 {
816     // Use a Timer so that we don't refresh too much
817     refreshTimer->start( 200 );
818 }
819
820 void Render::refresh()
821 {
822     if (!m_mltProducer)
823         return;
824     refreshTimer->stop();
825     if (m_mltConsumer) {
826         m_mltConsumer->set("refresh", 1);
827     }
828 }
829
830 /** Sets the description of this renderer to desc. */
831 void Render::setDescription(const QString & description)
832 {
833     m_description = description;
834 }
835
836 /** Returns the description of this renderer */
837 QString Render::description()
838 {
839     return m_description;
840 }
841
842
843 double Render::playSpeed()
844 {
845     if (m_mltProducer) return m_mltProducer->get_speed();
846     return 0.0;
847 }
848
849 const GenTime & Render::seekPosition() const
850 {
851     if (m_mltProducer) return GenTime((int) m_mltProducer->position(), m_fps);
852     else return GenTime();
853 }
854
855
856 const QString & Render::rendererName() const
857 {
858     return m_name;
859 }
860
861
862 void Render::emitFrameNumber(double position)
863 {
864       //kDebug()<<"// POSITON: "<<m_framePosition;
865         if (m_generateScenelist) return;
866         m_framePosition = position;
867         emit rendererPosition((int) position);
868         //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent( GenTime((int) position, m_fps), m_monitorId));
869 }
870
871 void Render::emitConsumerStopped()
872 {
873     // This is used to know when the playing stopped
874     if (m_mltProducer && !m_generateScenelist) {
875         double pos = m_mltProducer->position();
876         emit rendererStopped((int) pos);
877         //if (qApp->activeWindow()) QApplication::postEvent(qApp->activeWindow(), new PositionChangeEvent(GenTime((int) pos, m_fps), m_monitorId + 100));
878         //new QCustomEvent(10002));
879     }
880 }
881
882
883
884 void Render::exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime)
885 {
886 KMessageBox::sorry(0, i18n("Firewire is not enabled on your system.\n Please install Libiec61883 and recompile Kdenlive"));
887 }
888
889
890 void Render::exportCurrentFrame(KUrl url, bool notify) {
891     if (!m_mltProducer) {
892         KMessageBox::sorry(qApp->activeWindow(), i18n("There is no clip, cannot extract frame."));
893         return;
894     }
895
896     int height = 1080;//KdenliveSettings::defaultheight();
897     int width = 1940; //KdenliveSettings::displaywidth();
898
899     QPixmap pix(width, height);
900
901     Mlt::Filter m_convert("avcolour_space");
902     m_convert.set("forced", mlt_image_rgb24a);
903     m_mltProducer->attach(m_convert);
904     Mlt::Frame * frame = m_mltProducer->get_frame();
905     m_mltProducer->detach(m_convert);
906     if (frame) {
907         pix = frameThumbnail(frame, width, height);
908         delete frame;
909     }
910     pix.save(url.path(), "PNG");
911     //if (notify) QApplication::postEvent(qApp->activeWindow(), new UrlEvent(url, 10003));
912 }
913
914 /**     MLT PLAYLIST DIRECT MANIPULATON         **/
915
916
917 void Render::mltCheckLength()
918 {
919     //kDebug()<<"checking track length: "<<track<<"..........";
920     Mlt::Service service(m_mltProducer->get_service());
921     Mlt::Tractor tractor(service);
922
923     int trackNb = tractor.count( );
924     double duration = 0;
925     double trackDuration;
926     if (trackNb == 1) {
927         Mlt::Producer trackProducer(tractor.track(0));
928         Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
929         duration = Mlt::Producer(trackPlaylist.get_producer()).get_playtime() - 1;
930         m_mltProducer->set("out", duration);
931         emit durationChanged();
932         return;
933     }
934     while (trackNb > 1) {
935         Mlt::Producer trackProducer(tractor.track(trackNb - 1));
936         Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
937         trackDuration = Mlt::Producer(trackPlaylist.get_producer()).get_playtime() - 1;
938
939         kDebug()<<" / / /DURATON FOR TRACK "<<trackNb - 1<<" = "<<trackDuration;
940         if (trackDuration > duration) duration = trackDuration;
941         trackNb--;
942     }
943
944     Mlt::Producer blackTrackProducer(tractor.track(0));
945     Mlt::Playlist blackTrackPlaylist(( mlt_playlist ) blackTrackProducer.get_service());
946     double blackDuration = Mlt::Producer(blackTrackPlaylist.get_producer()).get_playtime() - 1;
947         kDebug()<<" / / /DURATON FOR TRACK 0 = "<<blackDuration;
948     if (blackDuration != duration) {
949         blackTrackPlaylist.remove_region( 0, blackDuration );
950         int i = 0;
951         int dur = duration;
952         
953         while (dur > 14000) { // <producer mlt_service=\"colour\" colour=\"black\" in=\"0\" out=\"13999\" />
954             mltInsertClip(0, GenTime(i * 14000, m_fps), QString("<westley><producer mlt_service=\"colour\" colour=\"black\" in=\"0\" out=\"13999\" /></westley>"));
955             dur = dur - 14000;
956             i++;
957         }
958
959         mltInsertClip(0, GenTime(), QString("<westley><producer mlt_service=\"colour\" colour=\"black\" in=\"0\" out=\"" + QString::number(dur) + "\" /></westley>"));
960
961         m_mltProducer->set("out", duration);
962         emit durationChanged();
963     }
964 }
965
966
967 void Render::mltInsertClip(int track, GenTime position, QString resource)
968 {
969     if (!m_mltProducer) {
970         kDebug()<<"PLAYLIST NOT INITIALISED //////";
971         return;
972     }
973     Mlt::Producer parentProd(m_mltProducer->parent());
974     if (parentProd.get_producer() == NULL) {
975         kDebug()<<"PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
976         return;
977     }
978     Mlt::Service service(parentProd.get_service());
979     Mlt::Tractor tractor(service);
980
981     Mlt::Producer trackProducer(tractor.track(track));
982     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
983     char *tmp = decodedString(resource);
984     Mlt::Producer clip("westley-xml", tmp);
985     //clip.set_in_and_out(in.frames(m_fps), out.frames(m_fps));
986     delete[] tmp;
987
988     trackPlaylist.insert_at(position.frames(m_fps), clip, 1);
989     tractor.multitrack()->refresh();
990     tractor.refresh();
991     if (track != 0) mltCheckLength();
992     double duration = Mlt::Producer(trackPlaylist.get_producer()).get_playtime();
993     kDebug()<<"// +  +INSERTING CLIP: "<<resource<<" AT: "<<position.frames(m_fps)<<" on track: "<<track<<", DURATION: "<<duration;
994
995
996 }
997
998 void Render::mltCutClip(int track, GenTime position)
999 {
1000     Mlt::Service service(m_mltProducer->parent().get_service());
1001     if (service.type() == playlist_type) kDebug()<<"// PLAYLIST TYPE";
1002     if (service.type() == tractor_type) kDebug()<<"// TRACOT TYPE";
1003     if (service.type() == multitrack_type) kDebug()<<"// MULTITRACK TYPE";
1004     if (service.type() == producer_type) kDebug()<<"// PROD TYPE";
1005
1006     Mlt::Tractor tractor(service);
1007     Mlt::Producer trackProducer(tractor.track(track));
1008     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1009     trackPlaylist.split_at(position.frames(m_fps));
1010     trackPlaylist.consolidate_blanks(0);
1011     kDebug()<<"/ / / /CUTTING CLIP AT: "<<position.frames(m_fps);
1012 }
1013
1014
1015 void Render::mltRemoveClip(int track, GenTime position)
1016 {
1017     Mlt::Service service(m_mltProducer->parent().get_service());
1018     if (service.type() == playlist_type) kDebug()<<"// PLAYLIST TYPE";
1019     if (service.type() == tractor_type) kDebug()<<"// TRACOT TYPE";
1020     if (service.type() == multitrack_type) kDebug()<<"// MULTITRACK TYPE";
1021     if (service.type() == producer_type) kDebug()<<"// PROD TYPE";
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     //trackPlaylist.remove(clipIndex);
1028     trackPlaylist.replace_with_blank(clipIndex);
1029     trackPlaylist.consolidate_blanks(0);
1030     if (track != 0) mltCheckLength();
1031     //emit durationChanged();
1032 }
1033
1034 void Render::mltRemoveEffect(int track, GenTime position, QString id, QString tag, int index)
1035 {
1036     Mlt::Service service(m_mltProducer->parent().get_service());
1037
1038     Mlt::Tractor tractor(service);
1039     Mlt::Producer trackProducer(tractor.track(track));
1040     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1041     //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1042     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1043     if (!clip) {
1044         kDebug()<<" / / / CANNOT FIND CLIP TO REMOVE EFFECT";
1045         return;
1046     }
1047     Mlt::Service clipService(clip->get_service());
1048
1049     if (tag.startsWith("ladspa")) tag = "ladspa";
1050
1051     if (index == -1) {
1052         int ct = 0;
1053         Mlt::Filter *filter = clipService.filter( ct );
1054         while (filter) {
1055             if (filter->get("mlt_service") == tag && filter->get("kdenlive_id") == id) {
1056                 clipService.detach(*filter);
1057                 kDebug()<<" / / / DLEETED EFFECT: "<<ct;
1058             }
1059             else ct++;
1060             filter = clipService.filter( ct );
1061         }
1062     }
1063     else {
1064         Mlt::Filter *filter = clipService.filter( index );
1065         if (filter && filter->get("mlt_service") == tag && filter->get("kdenlive_id") == id) clipService.detach(*filter);
1066         else {
1067             kDebug()<<"WARINIG, FILTER "<<id<<" NOT FOUND!!!!!";
1068         }
1069     }
1070     refresh();
1071 }
1072
1073
1074 void Render::mltAddEffect(int track, GenTime position, QString id, QString tag, QMap <QString, QString> args)
1075 {
1076     Mlt::Service service(m_mltProducer->parent().get_service());
1077
1078     Mlt::Tractor tractor(service);
1079     Mlt::Producer trackProducer(tractor.track(track));
1080     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1081
1082     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1083
1084     if (!clip) {
1085         kDebug()<<"**********  CANNOT FIND CLIP TO APPLY EFFECT-----------";
1086         return;
1087     }
1088     Mlt::Service clipService(clip->get_service());
1089
1090     // create filter
1091     kDebug()<<" / / INSERTING EFFECT: "<<id;
1092     if (tag.startsWith("ladspa")) tag = "ladspa";
1093     char *filterId = decodedString(tag);
1094     Mlt::Filter *filter = new Mlt::Filter(filterId);
1095     filter->set("kdenlive_id", filterId);
1096
1097     QMap<QString, QString>::Iterator it;
1098     QString keyFrameNumber = "#0";
1099
1100     for ( it = args.begin(); it != args.end(); ++it ) {
1101     //kDebug()<<" / / INSERTING EFFECT ARGS: "<<it.key()<<": "<<it.data();
1102         QString key;
1103         QString currentKeyFrameNumber;
1104         if (it.key().startsWith("#")) {
1105             currentKeyFrameNumber = it.key().section(":", 0, 0);
1106             if (currentKeyFrameNumber != keyFrameNumber) {
1107                 // attach filter to the clip
1108                 clipService.attach(*filter);
1109                 filter = new Mlt::Filter(filterId);
1110                 filter->set("kdenlive_id", filterId);
1111                 keyFrameNumber = currentKeyFrameNumber;
1112             }
1113             key = it.key().section(":", 1);
1114         }
1115         else key = it.key();
1116         char *name = decodedString(key);
1117         char *value = decodedString(it.value());
1118         filter->set(name, value);
1119         delete[] name;
1120         delete[] value;
1121     }
1122     // attach filter to the clip
1123     clipService.attach(*filter);
1124     delete[] filterId;
1125     refresh();
1126
1127 }
1128
1129 void Render::mltEditEffect(int track, GenTime position, int index, QString id, QString tag, QMap <QString, QString> args)
1130 {
1131     QMap<QString, QString>::Iterator it = args.begin();
1132     if (it.key().startsWith("#") || tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle") {
1133         // This is a keyframe effect, to edit it, we remove it and re-add it.
1134         mltRemoveEffect(track, position, id, tag, -1);
1135         mltAddEffect(track, position, id, tag, args);
1136         return;
1137     }
1138
1139     // create filter
1140     Mlt::Service service(m_mltProducer->parent().get_service());
1141
1142     Mlt::Tractor tractor(service);
1143     Mlt::Producer trackProducer(tractor.track(track));
1144     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1145     //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
1146     Mlt::Producer *clip = trackPlaylist.get_clip_at(position.frames(m_fps));
1147     Mlt::Service clipService(clip->get_service());
1148     Mlt::Filter *filter = clipService.filter( index );
1149
1150
1151     if (!filter || filter->get("mlt_service") != tag) {
1152         kDebug()<<"WARINIG, FILTER NOT FOUND!!!!!";
1153         int index = 0;
1154         filter = clipService.filter( index );
1155         while (filter) {
1156             if (filter->get("mlt_service") == tag && filter->get("kdenlive_id") == id) break;
1157             index++;
1158             filter = clipService.filter( index );
1159         }
1160     }
1161     if (!filter) {
1162         kDebug()<<"WARINIG, FILTER "<<id<<" NOT FOUND!!!!!";
1163         return;
1164     }
1165
1166     for ( it = args.begin(); it != args.end(); ++it ) {
1167     kDebug()<<" / / INSERTING EFFECT ARGS: "<<it.key()<<": "<<it.value();
1168         char *name = decodedString(it.key());
1169         char *value = decodedString(it.value());
1170         filter->set(name, value);
1171         delete[] name;
1172         delete[] value;
1173     }
1174     refresh();
1175 }
1176
1177 void Render::mltResizeClipEnd(int track, GenTime pos, GenTime in, GenTime out)
1178 {
1179     Mlt::Service service(m_mltProducer->parent().get_service());
1180
1181     Mlt::Tractor tractor(service);
1182     Mlt::Producer trackProducer(tractor.track(track));
1183     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1184     if (trackPlaylist.is_blank_at(pos.frames(m_fps) + 1)) 
1185         kDebug()<<"////////  ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
1186     int clipIndex = trackPlaylist.get_clip_index_at(pos.frames(m_fps) + 1);
1187
1188     int previousDuration = trackPlaylist.clip_length(clipIndex) - 1;
1189     int newDuration = out.frames(m_fps) - 1;
1190
1191     kDebug()<<" ** RESIZING CLIP END:" << clipIndex << " on track:"<< track <<", mid pos: "<<pos.frames(25)<<", in: "<<in.frames(25)<<", out: "<<out.frames(25)<<", PREVIOUS duration: "<<previousDuration;
1192     trackPlaylist.resize_clip(clipIndex, in.frames(m_fps), newDuration);
1193     trackPlaylist.consolidate_blanks(0);
1194     if (previousDuration < newDuration) {
1195         // clip was made longer, trim next blank if there is one.
1196         if (trackPlaylist.is_blank(clipIndex + 1)) {
1197             trackPlaylist.split(clipIndex + 1, newDuration - previousDuration);
1198             trackPlaylist.remove(clipIndex + 1);
1199         }
1200     }
1201     else trackPlaylist.insert_blank(clipIndex + 1, previousDuration - newDuration - 1);
1202
1203     trackPlaylist.consolidate_blanks(0);
1204     tractor.multitrack()->refresh();
1205     tractor.refresh();
1206     if (track != 0) mltCheckLength();
1207
1208 }
1209
1210 void Render::mltChangeTrackState(int track, bool mute, bool blind)
1211 {
1212     Mlt::Service service(m_mltProducer->parent().get_service());
1213
1214     Mlt::Tractor tractor(service);
1215     Mlt::Producer trackProducer(tractor.track(track));
1216     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1217     if (mute) {
1218         if (blind) trackProducer.set("hide", 3);
1219         else trackProducer.set("hide", 2);
1220     }
1221     else if (blind) {
1222         trackProducer.set("hide", 1);
1223     }
1224     else {
1225         trackProducer.set("hide", 0);
1226     }
1227     tractor.multitrack()->refresh();
1228     tractor.refresh();
1229     refresh();
1230 }
1231
1232 void Render::mltResizeClipStart(int track, GenTime pos, GenTime moveEnd, GenTime moveStart, GenTime in, GenTime out)
1233 {
1234     Mlt::Service service(m_mltProducer->parent().get_service());
1235     if (service.type() == playlist_type) kDebug()<<"// PLAYLIST TYPE";
1236     if (service.type() == tractor_type) kDebug()<<"// TRACOT TYPE";
1237     if (service.type() == multitrack_type) kDebug()<<"// MULTITRACK TYPE";
1238     if (service.type() == producer_type) kDebug()<<"// PROD TYPE";
1239
1240     int moveFrame = (moveEnd - moveStart).frames(m_fps);
1241
1242     Mlt::Tractor tractor(service);
1243     Mlt::Producer trackProducer(tractor.track(track));
1244     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1245     if (trackPlaylist.is_blank_at(pos.frames(m_fps) - 1)) 
1246         kDebug()<<"////////  ERROR RSIZING BLANK CLIP!!!!!!!!!!!";
1247     int clipIndex = trackPlaylist.get_clip_index_at(pos.frames(m_fps) - 1);
1248     kDebug()<<" ** RESIZING CLIP START:" << clipIndex << " on track:"<< track <<", mid pos: "<<pos.frames(25)<<", moving: "<<moveFrame<<", in: "<<in.frames(25)<<", out: "<<out.frames(25);
1249
1250     trackPlaylist.resize_clip(clipIndex, in.frames(m_fps), out.frames(m_fps));
1251     if (moveFrame > 0) trackPlaylist.insert_blank(clipIndex, moveFrame - 1);
1252     else {
1253         int midpos = moveStart.frames(m_fps) - 1; //+ (moveFrame / 2)
1254         int blankIndex = trackPlaylist.get_clip_index_at(midpos);
1255         int blankLength = trackPlaylist.clip_length(blankIndex);
1256
1257         kDebug()<<" + resizing blank: "<<blankIndex<<", Mid: "<<midpos<<", Length: "<<blankLength<< ", SIZE DIFF: "<<moveFrame;
1258
1259         
1260         if (blankLength + moveFrame == 0) trackPlaylist.remove(blankIndex);
1261         else trackPlaylist.resize_clip(blankIndex, 0, blankLength + moveFrame -1);
1262     }
1263     trackPlaylist.consolidate_blanks(0);
1264
1265     kDebug()<<"-----------------\n"<<"CLIP 0: "<<trackPlaylist.clip_start(0)<<", LENGT: "<<trackPlaylist.clip_length(0);
1266     kDebug()<<"CLIP 1: "<<trackPlaylist.clip_start(1)<<", LENGT: "<<trackPlaylist.clip_length(1);
1267     kDebug()<<"CLIP 2: "<<trackPlaylist.clip_start(2)<<", LENGT: "<<trackPlaylist.clip_length(2);
1268     kDebug()<<"CLIP 3: "<<trackPlaylist.clip_start(3)<<", LENGT: "<<trackPlaylist.clip_length(3);
1269    kDebug()<<"CLIP 4: "<<trackPlaylist.clip_start(4)<<", LENGT: "<<trackPlaylist.clip_length(4);
1270 }
1271
1272 void Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd)
1273 {
1274     mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps));
1275 }
1276
1277
1278 void Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd)
1279 {
1280     m_mltConsumer->set("refresh", 0);
1281     Mlt::Service service(m_mltProducer->parent().get_service());
1282
1283     Mlt::Tractor tractor(service);
1284     Mlt::Producer trackProducer(tractor.track(startTrack));
1285     Mlt::Playlist trackPlaylist(( mlt_playlist ) trackProducer.get_service());
1286     int clipIndex = trackPlaylist.get_clip_index_at(moveStart + 1);
1287
1288
1289     mlt_field field = mlt_tractor_field(tractor.get_tractor());
1290
1291     mlt_multitrack multitrack = mlt_field_multitrack(field); //mlt_tractor_multitrack(tractor.get_tractor());
1292     kDebug()<<" --  CURRENT MULTIOTRACK HAS: "<<mlt_multitrack_count(multitrack)<<" tracks";;
1293     mlt_service multiprod = mlt_multitrack_service( multitrack );
1294
1295     Mlt::Producer clipProducer(trackPlaylist.replace_with_blank(clipIndex));
1296     trackPlaylist.consolidate_blanks(0);
1297     mlt_events_block( MLT_PRODUCER_PROPERTIES(clipProducer.get_producer()), NULL );
1298
1299     if (endTrack == startTrack) {
1300         if (!trackPlaylist.is_blank_at(moveEnd)) {
1301             kWarning()<<"// ERROR, CLIP COLLISION----------";
1302             int ix = trackPlaylist.get_clip_index_at(moveEnd);
1303                 kDebug()<<"BAD CLIP STARTS AT: "<<trackPlaylist.clip_start(ix)<<", LENGT: "<<trackPlaylist.clip_length(ix);
1304         }
1305         trackPlaylist.insert_at(moveEnd, clipProducer, 1);
1306         trackPlaylist.consolidate_blanks(0);
1307     }
1308     else {
1309         trackPlaylist.consolidate_blanks(0);
1310         Mlt::Producer destTrackProducer(tractor.track(endTrack));
1311         Mlt::Playlist destTrackPlaylist(( mlt_playlist ) destTrackProducer.get_service());
1312         destTrackPlaylist.consolidate_blanks(1);
1313         destTrackPlaylist.insert_at(moveEnd, clipProducer, 1);
1314         destTrackPlaylist.consolidate_blanks(0);
1315     }
1316
1317     mltCheckLength();
1318     mlt_events_unblock( MLT_PRODUCER_PROPERTIES(clipProducer.get_producer()), NULL );
1319 }
1320
1321 void Render::mltMoveTransition(QString type, int startTrack, int trackOffset, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
1322 {
1323     m_mltConsumer->set("refresh", 0);
1324     mlt_service serv = m_mltProducer->parent().get_service();
1325
1326     mlt_service nextservice = mlt_service_get_producer(serv);
1327     mlt_properties properties = MLT_SERVICE_PROPERTIES( nextservice );
1328     QString mlt_type = mlt_properties_get( properties, "mlt_type" );
1329     QString resource = mlt_properties_get( properties, "mlt_service");
1330     int old_pos = (oldIn.frames(m_fps) + oldOut.frames(m_fps)) / 2;
1331
1332     int new_in = newIn.frames(m_fps);
1333     int new_out = newOut.frames(m_fps) - 1;
1334     while (mlt_type == "transition") {
1335         mlt_transition tr = (mlt_transition) nextservice;
1336         int currentTrack = mlt_transition_get_b_track(tr);
1337         int currentIn = (int) mlt_transition_get_in(tr);
1338         int currentOut = (int) mlt_transition_get_out(tr);
1339         kDebug()<<"// FOUND EXISTING TRANS, IN: "<<currentIn<<", OUT: "<<currentOut<<", TRACK: "<<currentTrack;
1340         //kDebug()<<"// LOOKING FOR IN: "<<old_in<<", OUT: "<<old_out;
1341         kDebug()<<"// OLD IN: "<<oldIn.frames(m_fps)<<" // OLD OUT: "<<oldOut.frames(m_fps)<<", TRACK: "<<startTrack<<", MID POS: "<<old_pos;
1342         if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
1343             mlt_transition_set_in_and_out(tr, new_in, new_out);
1344             if (trackOffset != 0) {
1345                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( tr );
1346                 mlt_properties_set_int( properties, "a_track", mlt_transition_get_a_track(tr) + trackOffset );
1347                 mlt_properties_set_int( properties, "b_track", mlt_transition_get_b_track(tr) + trackOffset );
1348             }
1349             break;
1350         }
1351         nextservice = mlt_service_producer(nextservice);
1352         properties = MLT_SERVICE_PROPERTIES( nextservice );
1353         mlt_type = mlt_properties_get( properties, "mlt_type" );
1354         resource = mlt_properties_get( properties, "mlt_service" );
1355     }
1356 }
1357
1358 void Render::mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QMap <QString, QString> args)
1359 {
1360     Mlt::Service service(m_mltProducer->parent().get_service());
1361
1362     Mlt::Tractor tractor(service);
1363     Mlt::Field *field = tractor.field();
1364
1365     char *transId = decodedString(tag);
1366     Mlt::Transition *transition = new Mlt::Transition(transId);
1367     transition->set_in_and_out((int) in.frames(m_fps), (int) out.frames(m_fps));
1368     QMap<QString, QString>::Iterator it;
1369     QString key;
1370
1371     kDebug()<<" ------  ADDING TRANSITION PARAMs: "<<args.count();
1372
1373     for ( it = args.begin(); it != args.end(); ++it ) {
1374         key = it.key();
1375         char *name = decodedString(key);
1376         char *value = decodedString(it.value());
1377         transition->set(name, value);
1378         kDebug()<<" ------  ADDING TRANS PARAM: "<<name<<": "<<value;
1379         //filter->set("kdenlive_id", id);
1380         delete[] name;
1381         delete[] value;
1382     }
1383     // attach filter to the clip
1384     field->plant_transition(*transition, a_track, b_track);
1385     delete[] transId;
1386     refresh();
1387
1388 }
1389
1390 void Render::mltSavePlaylist()
1391 {
1392     kWarning()<<"// UPDATING PLAYLIST TO DISK++++++++++++++++";
1393     Mlt::Consumer *fileConsumer = new Mlt::Consumer("westley");
1394     fileConsumer->set("resource", "/home/one/playlist.xml");
1395
1396     Mlt::Service service(m_mltProducer->get_service());
1397     Mlt::Tractor tractor(service);
1398
1399     fileConsumer->connect(service);
1400     fileConsumer->start();
1401
1402 }
1403
1404 #include "renderer.moc"