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