]> git.sesse.net Git - kdenlive/blob - src/renderer.h
Merge branch 'master' of git://anongit.kde.org/kdenlive
[kdenlive] / src / renderer.h
1 /***************************************************************************
2                          krender.h  -  description
3                             -------------------
4    begin                : Fri Nov 22 2002
5    copyright            : (C) 2002 by Jason Wood (jasonwood@blueyonder.co.uk)
6    copyright            : (C) 2010 by Jean-Baptiste Mardelle (jb@kdenlive.org)
7
8 ***************************************************************************/
9
10 /***************************************************************************
11  *                                                                         *
12  *   This program is free software; you can redistribute it and/or modify  *
13  *   it under the terms of the GNU General Public License as published by  *
14  *   the Free Software Foundation; either version 2 of the License, or     *
15  *   (at your option) any later version.                                   *
16  *                                                                         *
17  ***************************************************************************/
18
19 /**
20  * @class Render
21  * @brief Client side of the interface to a renderer.
22  *
23  * From Kdenlive's point of view, you treat the Render object as the renderer,
24  * and simply use it as if it was local. Calls are asynchronous - you send a
25  * call out, and then receive the return value through the relevant signal that
26  * get's emitted once the call completes.
27  */
28
29 #ifndef RENDERER_H
30 #define RENDERER_H
31
32 #include "gentime.h"
33 #include "definitions.h"
34 #include "abstractmonitor.h"
35
36 #include <mlt/framework/mlt_types.h>
37
38 #include <kurl.h>
39
40 #include <qdom.h>
41 #include <qstring.h>
42 #include <qmap.h>
43 #include <QList>
44 #include <QEvent>
45 #include <QMutex>
46 #include <QFuture>
47 #include <QSemaphore>
48
49 class QTimer;
50 class QPixmap;
51
52 class KComboBox;
53
54 namespace Mlt
55 {
56 class Consumer;
57 class Playlist;
58 class Tractor;
59 class Transition;
60 class Frame;
61 class Field;
62 class Producer;
63 class Filter;
64 class Profile;
65 class Service;
66 class Event;
67 };
68
69 struct requestClipInfo {
70     QDomElement xml;
71     QString clipId;
72     int imageHeight;
73     bool replaceProducer;
74
75 bool operator==(const requestClipInfo &a)
76 {
77     return clipId == a.clipId;
78 }
79 };
80
81 class MltErrorEvent : public QEvent
82 {
83 public:
84     MltErrorEvent(QString message) : QEvent(QEvent::User), m_message(message) {}
85     QString message() const {
86         return m_message;
87     }
88
89 private:
90     QString m_message;
91 };
92
93
94 class Render: public AbstractRender
95 {
96 Q_OBJECT public:
97
98     enum FailStates { OK = 0,
99                       APP_NOEXIST
100                     };
101     /** @brief Build a MLT Renderer
102      *  @param rendererName A unique identifier for this renderer
103      *  @param winid The parent widget identifier (required for SDL display). Set to 0 for OpenGL rendering
104      *  @param profile The MLT profile used for the renderer (default one will be used if empty). */
105     Render(Kdenlive::MONITORID rendererName, int winid, QString profile = QString(), QWidget *parent = 0);
106
107     /** @brief Destroy the MLT Renderer. */
108     virtual ~Render();
109
110     /** @brief Seeks the renderer clip to the given time. */
111     void seek(GenTime time);
112     void seek(int time);
113     void seekToFrameDiff(int diff);
114
115     QPixmap getImageThumbnail(KUrl url, int width, int height);
116
117     /** @brief Sets the current MLT producer playlist.
118      * @param list The xml describing the playlist
119      * @param position (optional) time to seek to */
120     int setSceneList(QDomDocument list, int position = 0);
121
122     /** @brief Sets the current MLT producer playlist.
123      * @param list new playlist
124      * @param position (optional) time to seek to
125      * @return 0 when it has success, different from 0 otherwise
126      *
127      * Creates the producer from the text playlist. */
128     int setSceneList(QString playlist, int position = 0);
129     int setProducer(Mlt::Producer *producer, int position);
130
131     /** @brief Get the current MLT producer playlist.
132      * @return A string describing the playlist */
133     const QString sceneList();
134     bool saveSceneList(QString path, QDomElement kdenliveData = QDomElement());
135
136     /** @brief Tells the renderer to play the scene at the specified speed,
137      * @param speed speed to play the scene to
138      *
139      * The speed is relative to normal playback, e.g. 1.0 is normal speed, 0.0
140      * is paused, -1.0 means play backwards. It does not specify start/stop */
141     void play(double speed);
142     void switchPlay(bool play);
143     void pause();
144
145     /** @brief Stops playing.
146      * @param startTime time to seek to */
147     void stop(const GenTime &startTime);
148     int volume() const;
149
150     QImage extractFrame(int frame_position, QString path = QString(), int width = -1, int height = -1);
151
152     /** @brief Plays the scene starting from a specific time.
153      * @param startTime time to start playing the scene from */
154     void play(const GenTime & startTime);
155     void playZone(const GenTime & startTime, const GenTime & stopTime);
156     void loopZone(const GenTime & startTime, const GenTime & stopTime);
157
158     void saveZone(KUrl url, QString desc, QPoint zone);
159     
160     /** @brief Save a clip in timeline to an xml playlist. */
161     bool saveClip(int track, GenTime position, KUrl url, QString desc = QString());
162
163     /** @brief Return true if we are currently playing */
164     bool isPlaying() const;
165
166     /** @brief Returns the speed at which the renderer is currently playing.
167      *
168      * It returns 0.0 when the renderer is not playing anything. */
169     double playSpeed() const;
170
171     /** @brief Returns the current seek position of the renderer. */
172     GenTime seekPosition() const;
173     int seekFramePosition() const;
174
175     void emitFrameUpdated(Mlt::Frame&);
176     void emitFrameNumber();
177     void emitConsumerStopped(bool forcePause = false);
178
179     /** @brief Returns the aspect ratio of the consumer. */
180     double consumerRatio() const;
181
182     /** @brief Saves current producer frame as an image. */
183     void exportCurrentFrame(KUrl url, bool notify);
184
185     /** @brief Change the Mlt PROFILE
186      * @param profileName The MLT profile name
187      * @param dropSceneList If true, the current playlist will be deleted
188      * @return true if the profile was changed
189      * . */
190     int resetProfile(const QString& profileName, bool dropSceneList = false);
191     /** @brief Returns true if the render uses profileName as current profile. */
192     bool hasProfile(const QString& profileName) const;
193     double fps() const;
194
195     /** @brief Returns the width of a frame for this profile. */
196     int frameRenderWidth() const;
197     /** @brief Returns the display width of a frame for this profile. */
198     int renderWidth() const;
199     /** @brief Returns the height of a frame for this profile. */
200     int renderHeight() const;
201
202     /** @brief Returns display aspect ratio. */
203     double dar() const;
204     /** @brief Returns sample aspect ratio. */
205     double sar() const;
206     /** @brief If monitor is active, refresh it. */
207     void refreshIfActive();
208     /** @brief Start the MLT monitor consumer. */
209     void startConsumer();
210
211     /*
212      * Playlist manipulation.
213      */
214     Mlt::Producer *checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element);
215     int mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite = false, bool push = false);
216     bool mltUpdateClip(Mlt::Tractor *tractor, ItemInfo info, QDomElement element, Mlt::Producer *prod);
217     bool mltCutClip(int track, GenTime position);
218     void mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime &duration, const GenTime &timeOffset);
219     int mltGetSpaceLength(const GenTime &pos, int track, bool fromBlankStart);
220
221     /** @brief Returns the duration/length of @param track as reported by the track producer. */
222     int mltTrackDuration(int track);
223
224     bool mltResizeClipEnd(ItemInfo info, GenTime clipDuration, bool refresh = true);
225     bool mltResizeClipStart(ItemInfo info, GenTime diff);
226     bool mltResizeClipCrop(ItemInfo info, GenTime newCropStart);
227     bool mltMoveClip(int startTrack, int endTrack, GenTime pos, GenTime moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
228     bool mltMoveClip(int startTrack, int endTrack, int pos, int moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
229     bool mltRemoveClip(int track, GenTime position);
230
231     /** @brief Deletes an effect from a clip in MLT's playlist. */
232     bool mltRemoveEffect(int track, GenTime position, int index, bool updateIndex, bool doRefresh = true);
233     bool mltRemoveTrackEffect(int track, int index, bool updateIndex);
234
235     /** @brief Adds an effect to a clip in MLT's playlist. */
236     bool mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh = true);
237     bool addFilterToService(Mlt::Service service, EffectsParameterList params, int duration);
238     bool mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh);
239     bool mltAddTrackEffect(int track, EffectsParameterList params);
240     
241     /** @brief Enable / disable clip effects. 
242      * @param track The track where the clip is
243      * @param position The start position of the clip
244      * @param effectIndexes The list of effect indexes to enable / disable
245      * @param disable True if effects should be disabled, false otherwise */
246     bool mltEnableEffects(int track, GenTime position, QList <int> effectIndexes, bool disable);
247     /** @brief Enable / disable track effects.
248      * @param track The track where the effect is
249      * @param effectIndexes The list of effect indexes to enable / disable
250      * @param disable True if effects should be disabled, false otherwise */
251     bool mltEnableTrackEffects(int track, QList <int> effectIndexes, bool disable);
252
253     /** @brief Edits an effect parameters in MLT's playlist. */
254     bool mltEditEffect(int track, GenTime position, EffectsParameterList params);
255     bool mltEditTrackEffect(int track, EffectsParameterList params);
256
257     /** @brief Updates the "kdenlive_ix" (index) value of an effect. */
258     void mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos);
259
260     /** @brief Changes the order of effects in MLT's playlist.
261      *
262      * It switches effects from oldPos and newPos, updating the "kdenlive_ix"
263      * (index) value. */
264     void mltMoveEffect(int track, GenTime position, int oldPos, int newPos);
265     void mltMoveTrackEffect(int track, int oldPos, int newPos);
266
267     /** @brief Enables/disables audio/video in a track. */
268     void mltChangeTrackState(int track, bool mute, bool blind);
269     bool mltMoveTransition(QString type, int startTrack,  int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut);
270     bool mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
271     void mltDeleteTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
272     void mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force = false);
273     void mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml);
274     void mltAddClipTransparency(ItemInfo info, int transitiontrack, int id);
275     void mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id);
276     void mltDeleteTransparency(int pos, int track, int id);
277     void mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id);
278     QList <TransitionInfo> mltInsertTrack(int ix, bool videoTrack);
279     void mltDeleteTrack(int ix);
280     bool mltUpdateClipProducer(Mlt::Tractor *tractor, int track, int pos, Mlt::Producer *prod);
281     void mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track);
282     Mlt::Producer *invalidProducer(const QString &id);
283
284     /** @brief Changes the speed of a clip in MLT's playlist.
285      *
286      * It creates a new "framebuffer" producer, which must have its "resource"
287      * property set to "video.mpg?0.6", where "video.mpg" is the path to the
288      * clip and "0.6" is the speed in percentage. The newly created producer
289      * will have its "id" property set to "slowmotion:parentid:speed", where
290      * "parentid" is the id of the original clip in the ClipManager list and
291      * "speed" is the current speed. */
292     int mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double oldspeed, int strobe, Mlt::Producer *prod);
293
294     const QList <Mlt::Producer *> producersList();
295     void updatePreviewSettings();
296     void setDropFrames(bool show);
297     QString updateSceneListFps(double current_fps, double new_fps, QString scene);
298
299     void showAudio(Mlt::Frame&);
300     
301     QList <int> checkTrackSequence(int);
302     void sendFrameUpdate();
303
304     /** @brief Returns a pointer to the main producer. */
305     Mlt::Producer *getProducer();
306     /** @brief Returns the number of clips to process (When requesting clip info). */
307     int processingItems();
308     /** @brief Processing of this clip is over, producer was set on clip, remove from list. */
309     void processingDone(const QString &id);
310     /** @brief Force processing of clip with selected id. */
311     void forceProcessing(const QString &id);
312     /** @brief Are we currently processing clip with selected id. */
313     bool isProcessing(const QString &id);
314
315     /** @brief Requests the file properties for the specified URL (will be put in a queue list)
316         @param xml The xml parameters for the clip
317         @param clipId The clip Id string
318         @param imageHeight The height (in pixels) of the returned thumbnail (height of a treewidgetitem in projectlist)
319         @param replaceProducer If true, the MLT producer will be recreated */
320     void getFileProperties(const QDomElement &xml, const QString &clipId, int imageHeight, bool replaceProducer = true);
321
322     /** @brief Lock the MLT service */
323     Mlt::Tractor *lockService();
324     /** @brief Unlock the MLT service */
325     void unlockService(Mlt::Tractor *tractor);
326     const QString activeClipId();
327     /** @brief Fill a combobox with the found blackmagic devices */
328     static bool getBlackMagicDeviceList(KComboBox *devicelist, bool force = false);
329     static bool getBlackMagicOutputDeviceList(KComboBox *devicelist, bool force = false);
330     /** @brief Frame rendering is handeled by Kdenlive, don't show video through SDL display */
331     void disablePreview(bool disable);
332     /** @brief Get current seek pos requested of SEEK_INACTIVE if we are not currently seeking */
333     int requestedSeekPosition;
334     /** @brief Get current seek pos requested of current producer pos if not seeking */
335     int getCurrentSeekPosition() const;
336     /** @brief Create a producer from url and load it in the monitor  */
337     void loadUrl(const QString &url);
338     /** @brief Check if the installed FFmpeg / Libav supports x11grab */
339     static bool checkX11Grab();
340     
341     QSemaphore showFrameSemaphore;
342     bool externalConsumer;
343
344 protected:
345     static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
346     static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
347     
348 private:
349
350     /** @brief The name of this renderer.
351      *
352      * Useful to identify the renderers by what they do - e.g. background
353      * rendering, workspace monitor, etc. */
354     Kdenlive::MONITORID m_name;
355     Mlt::Consumer * m_mltConsumer;
356     Mlt::Producer * m_mltProducer;
357     Mlt::Profile *m_mltProfile;
358     Mlt::Event *m_showFrameEvent;
359     Mlt::Event *m_pauseEvent;
360     double m_fps;
361
362     /** @brief True if we are playing a zone.
363      *
364      * It's determined by the "in" and "out" properties being temporarily
365      * changed. */
366     bool m_isZoneMode;
367     bool m_isLoopMode;
368     GenTime m_loopStart;
369
370     /** @brief True when the monitor is in split view. */
371     bool m_isSplitView;
372
373     Mlt::Producer *m_blackClip;
374     QString m_activeProfile;
375
376     QTimer *m_osdTimer;
377     QTimer m_refreshTimer;
378     QMutex m_mutex;
379     QMutex m_infoMutex;
380
381     /** @brief A human-readable description of this renderer. */
382     int m_winid;
383
384     QLocale m_locale;
385     QFuture <void> m_infoThread;
386     QList <requestClipInfo> m_requestList;
387     bool m_paused;
388
389     void closeMlt();
390     void mltCheckLength(Mlt::Tractor *tractor);
391     void mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest);
392     QMap<QString, QString> mltGetTransitionParamsFromXml(QDomElement xml);
393     QMap<QString, Mlt::Producer *> m_slowmotionProducers;
394     /** @brief The ids of the clips that are currently being loaded for info query */
395     QStringList m_processingClipId;
396
397     /** @brief Build the MLT Consumer object with initial settings.
398      *  @param profileName The MLT profile to use for the consumer */
399     void buildConsumer(const QString& profileName);
400     void resetZoneMode();
401     void fillSlowMotionProducers();
402     /** @brief Get the track number of the lowest audible (non muted) audio track
403      *  @param return The track number */
404     int getLowestNonMutedAudioTrack(Mlt::Tractor tractor);
405
406     /** @brief Make sure our audio mixing transitions are applied to the lowest track */
407     void fixAudioMixing(Mlt::Tractor tractor);
408     /** @brief Make sure we inform MLT if we need a lot of threads for avformat producer */
409     void checkMaxThreads();
410
411 private slots:
412
413     /** @brief Refreshes the monitor display. */
414     void refresh();
415     void slotOsdTimeout();
416     /** @brief Process the clip info requests (in a separate thread). */
417     void processFileProperties();
418     /** @brief A clip with multiple video streams was found, ask what to do. */
419     void slotMultiStreamProducerFound(const QString path, QList<int> audio_list, QList<int> video_list, stringMap data);
420     void showFrame(Mlt::Frame *);
421     void slotCheckSeeking();
422
423 signals:
424
425     /** @brief The renderer received a reply to a getFileProperties request. */
426     void replyGetFileProperties(const QString &clipId, Mlt::Producer*, const stringMap &, const stringMap &, bool replaceProducer);
427
428     /** @brief The renderer received a reply to a getImage request. */
429     void replyGetImage(const QString &, const QString &, int, int);
430     void replyGetImage(const QString &, const QImage &);
431
432     /** @brief The renderer stopped, either playing or rendering. */
433     void stopped();
434
435     /** @brief The renderer started playing. */
436     void playing(double);
437
438     /** @brief The renderer started rendering. */
439     void rendering(const GenTime &);
440
441     /** @brief An error occurred within this renderer. */
442     void error(const QString &, const QString &);
443     void durationChanged(int);
444     void rendererPosition(int);
445     void rendererStopped(int);
446     /** @brief The clip is not valid, should be removed from project. */
447     void removeInvalidClip(const QString &, bool replaceProducer);
448     /** @brief The proxy is not valid, should be deleted.
449      *  @param id The original clip's id
450      *  @param durationError Should be set to true if the proxy failed because it has not same length as original clip
451      */
452     void removeInvalidProxy(const QString &id, bool durationError);
453     void refreshDocumentProducers(bool displayRatioChanged, bool fpsChanged);
454     /** @brief A proxy clip is missing, ask for creation. */
455     void requestProxy(QString);
456     /** @brief A multiple stream clip was found. */
457     void multiStreamFound(const QString &,QList<int>,QList<int>,stringMap data);
458
459
460     /** @brief A frame's image has to be shown.
461      *
462      * Used in Mac OS X. */
463     void showImageSignal(QImage);
464     void showAudioSignal(const QVector<double> &);
465     void addClip(const KUrl &, stringMap);
466     void checkSeeking();
467     void mltFrameReceived(Mlt::Frame *);
468
469 public slots:
470
471     /** @brief Starts the consumer. */
472     void start();
473
474     /** @brief Stops the consumer. */
475     void stop();
476     int getLength();
477
478     /** @brief Checks if the file is readable by MLT. */
479     bool isValid(KUrl url);
480
481     void exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime);
482     void mltSavePlaylist();
483     void slotSplitView(bool doit);
484     void slotSwitchFullscreen();
485     void slotSetVolume(int volume);
486     void seekToFrame(int pos);
487     /** @brief Starts a timer to query for a refresh. */
488     void doRefresh();
489 };
490
491 #endif