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