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