]> git.sesse.net Git - kdenlive/blob - src/renderer.h
Fix freeze when making fast play / pause
[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 #include "mlt/framework/mlt_types.h"
36
37 #include <kurl.h>
38
39 #include <qdom.h>
40 #include <qstring.h>
41 #include <qmap.h>
42 #include <QList>
43 #include <QEvent>
44
45
46 class QTimer;
47 class QPixmap;
48
49 namespace Mlt
50 {
51 class Consumer;
52 class Playlist;
53 class Tractor;
54 class Transition;
55 class Frame;
56 class Field;
57 class Producer;
58 class Filter;
59 class Profile;
60 class Service;
61 };
62
63 class MltErrorEvent : public QEvent
64 {
65 public:
66     MltErrorEvent(QString message) : QEvent(QEvent::User), m_message(message) {}
67     QString message() const {
68         return m_message;
69     }
70
71 private:
72     QString m_message;
73 };
74
75
76 class Render: public AbstractRender
77 {
78 Q_OBJECT public:
79
80     enum FailStates { OK = 0,
81                       APP_NOEXIST
82                     };
83     /** @brief Build a MLT Renderer
84      *  @param rendererName A unique identifier for this renderer
85      *  @param winid The parent widget identifier (required for SDL display). Set to 0 for OpenGL rendering
86      *  @param profile The MLT profile used for the renderer (default one will be used if empty). */
87     Render(const QString & rendererName, int winid, QString profile = QString(), QWidget *parent = 0);
88
89     /** @brief Destroy the MLT Renderer. */
90     virtual ~Render();
91
92     /** @brief Seeks the renderer clip to the given time. */
93     void seek(GenTime time);
94     void seek(int time);
95     void seekToFrameDiff(int diff);
96     int m_isBlocked;
97
98     QPixmap getImageThumbnail(KUrl url, int width, int height);
99
100     /** @brief Sets the current MLT producer playlist.
101      * @param list The xml describing the playlist
102      * @param position (optional) time to seek to */
103     int setSceneList(QDomDocument list, int position = 0);
104
105     /** @brief Sets the current MLT producer playlist.
106      * @param list new playlist
107      * @param position (optional) time to seek to
108      * @return 0 when it has success, different from 0 otherwise
109      *
110      * Creates the producer from the text playlist. */
111     int setSceneList(QString playlist, int position = 0);
112     int setProducer(Mlt::Producer *producer, int position);
113
114     /** @brief Get the current MLT producer playlist.
115      * @return A string describing the playlist */
116     const QString sceneList();
117     bool saveSceneList(QString path, QDomElement kdenliveData = QDomElement());
118
119     /** @brief Tells the renderer to play the scene at the specified speed,
120      * @param speed speed to play the scene to
121      *
122      * The speed is relative to normal playback, e.g. 1.0 is normal speed, 0.0
123      * is paused, -1.0 means play backwards. It does not specify start/stop */
124     void play(double speed);
125     void switchPlay(bool play);
126     void pause();
127
128     /** @brief Stops playing.
129      * @param startTime time to seek to */
130     void stop(const GenTime & startTime);
131     int volume() const;
132
133     QImage extractFrame(int frame_position, QString path = QString(), int width = -1, int height = -1);
134
135     /** @brief Plays the scene starting from a specific time.
136      * @param startTime time to start playing the scene from */
137     void play(const GenTime & startTime);
138     void playZone(const GenTime & startTime, const GenTime & stopTime);
139     void loopZone(const GenTime & startTime, const GenTime & stopTime);
140
141     void saveZone(KUrl url, QString desc, QPoint zone);
142
143     /** @brief Returns the name of the renderer. */
144     const QString & rendererName() const;
145
146     /** @brief Returns the speed at which the renderer is currently playing.
147      *
148      * It returns 0.0 when the renderer is not playing anything. */
149     double playSpeed();
150
151     /** @brief Returns the current seek position of the renderer. */
152     GenTime seekPosition() const;
153     int seekFramePosition() const;
154
155     void emitFrameUpdated(Mlt::Frame&);
156     void emitFrameNumber(double position);
157     void emitConsumerStopped();
158
159     /** @brief Returns the aspect ratio of the consumer. */
160     double consumerRatio() const;
161
162     void doRefresh();
163
164     /** @brief Saves current producer frame as an image. */
165     void exportCurrentFrame(KUrl url, bool notify);
166
167     /** @brief Turns on or off on screen display. */
168     void refreshDisplay();
169     int resetProfile(const QString profileName);
170     double fps() const;
171
172     /** @brief Returns the width of a frame for this profile. */
173     int frameRenderWidth() const;
174     /** @brief Returns the display width of a frame for this profile. */
175     int renderWidth() const;
176     /** @brief Returns the height of a frame for this profile. */
177     int renderHeight() const;
178
179     /** @brief Returns display aspect ratio. */
180     double dar() const;
181     /** @brief Returns sample aspect ratio. */
182     double sar() const;
183
184     /*
185      * Playlist manipulation.
186      */
187     Mlt::Producer *checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element);
188     int mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite = false, bool push = false);
189     bool mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod);
190     void mltCutClip(int track, GenTime position);
191     void mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime duration, const GenTime timeOffset);
192     int mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart);
193
194     /** @brief Returns the duration/length of @param track as reported by the track producer. */
195     int mltTrackDuration(int track);
196
197     bool mltResizeClipEnd(ItemInfo info, GenTime clipDuration);
198     bool mltResizeClipStart(ItemInfo info, GenTime diff);
199     bool mltResizeClipCrop(ItemInfo info, GenTime diff);
200     bool mltMoveClip(int startTrack, int endTrack, GenTime pos, GenTime moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
201     bool mltMoveClip(int startTrack, int endTrack, int pos, int moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
202     bool mltRemoveClip(int track, GenTime position);
203
204     /** @brief Deletes an effect from a clip in MLT's playlist. */
205     bool mltRemoveEffect(int track, GenTime position, QString index, bool updateIndex, bool doRefresh = true);
206     bool mltRemoveTrackEffect(int track, QString index, bool updateIndex);
207
208     /** @brief Adds an effect to a clip in MLT's playlist. */
209     bool mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh = true);
210     bool mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh);
211     bool mltAddTrackEffect(int track, EffectsParameterList params);
212
213     /** @brief Edits an effect parameters in MLT's playlist. */
214     bool mltEditEffect(int track, GenTime position, EffectsParameterList params);
215     bool mltEditTrackEffect(int track, EffectsParameterList params);
216
217     /** @brief Updates the "kdenlive_ix" (index) value of an effect. */
218     void mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos);
219
220     /** @brief Changes the order of effects in MLT's playlist.
221      *
222      * It switches effects from oldPos and newPos, updating the "kdenlive_ix"
223      * (index) value. */
224     void mltMoveEffect(int track, GenTime position, int oldPos, int newPos);
225     void mltMoveTrackEffect(int track, int oldPos, int newPos);
226
227     /** @brief Enables/disables audio/video in a track. */
228     void mltChangeTrackState(int track, bool mute, bool blind);
229     bool mltMoveTransition(QString type, int startTrack,  int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut);
230     bool mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
231     void mltDeleteTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
232     void mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force = false);
233     void mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml);
234     void mltAddClipTransparency(ItemInfo info, int transitiontrack, int id);
235     void mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id);
236     void mltDeleteTransparency(int pos, int track, int id);
237     void mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id);
238     void mltInsertTrack(int ix, bool videoTrack);
239     void mltDeleteTrack(int ix);
240     bool mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod);
241     void mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track);
242     Mlt::Producer *invalidProducer(const QString &id);
243
244     /** @brief Changes the speed of a clip in MLT's playlist.
245      *
246      * It creates a new "framebuffer" producer, which must have its "resource"
247      * property set to "video.mpg?0.6", where "video.mpg" is the path to the
248      * clip and "0.6" is the speed in percentage. The newly created producer
249      * will have its "id" property set to "slowmotion:parentid:speed", where
250      * "parentid" is the id of the original clip in the ClipManager list and
251      * "speed" is the current speed. */
252     int mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double oldspeed, int strobe, Mlt::Producer *prod);
253
254     const QList <Mlt::Producer *> producersList();
255     void updatePreviewSettings();
256     void setDropFrames(bool show);
257     QString updateSceneListFps(double current_fps, double new_fps, QString scene);
258     void showFrame(Mlt::Frame&);
259
260     void showAudio(Mlt::Frame&);
261     /** @brief This property is used to decide if the renderer should send audio data for monitoring. */
262     bool analyseAudio;
263     
264     QList <int> checkTrackSequence(int);
265     void sendFrameUpdate();
266
267     /** @brief Returns a pointer to the main producer. */
268     Mlt::Producer *getProducer();
269
270 private:
271
272     /** @brief The name of this renderer.
273      *
274      * Useful to identify the renderers by what they do - e.g. background
275      * rendering, workspace monitor, etc. */
276     QString m_name;
277     Mlt::Consumer * m_mltConsumer;
278     Mlt::Producer * m_mltProducer;
279     Mlt::Profile *m_mltProfile;
280     double m_framePosition;
281     double m_fps;
282     bool m_externalConsumer;
283
284     /** @brief True if we are playing a zone.
285      *
286      * It's determined by the "in" and "out" properties being temporarily
287      * changed. */
288     bool m_isZoneMode;
289     bool m_isLoopMode;
290     GenTime m_loopStart;
291     int m_originalOut;
292
293     /** @brief True when the monitor is in split view. */
294     bool m_isSplitView;
295
296     Mlt::Producer *m_blackClip;
297     QString m_activeProfile;
298
299     QTimer *m_osdTimer;
300
301     /** @brief A human-readable description of this renderer. */
302     int m_winid;
303
304     void closeMlt();
305     void mltCheckLength(Mlt::Tractor *tractor);
306     void mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest);
307     QMap<QString, QString> mltGetTransitionParamsFromXml(QDomElement xml);
308     QMap<QString, Mlt::Producer *> m_slowmotionProducers;
309
310     /** @brief Build the MLT Consumer object with initial settings.
311      *  @param profileName The MLT profile to use for the consumer */
312     void buildConsumer(const QString profileName);
313     void resetZoneMode();
314     void fillSlowMotionProducers();
315     /** @brief Get the track number of the lowest audible (non muted) audio track
316      *  @param return The track number */
317     int getLowestNonMutedAudioTrack(Mlt::Tractor tractor);
318
319     /** @brief Make sure our audio mixing transitions are applied to the lowest track */
320     void fixAudioMixing(Mlt::Tractor tractor);
321
322 private slots:
323
324     /** @brief Refreshes the monitor display. */
325     void refresh();
326     void slotOsdTimeout();
327     int connectPlaylist();
328     //void initSceneList();
329
330 signals:
331
332     /** @brief The renderer received a reply to a getFileProperties request. */
333     void replyGetFileProperties(const QString &clipId, Mlt::Producer*, const QMap < QString, QString > &, const QMap < QString, QString > &, bool, bool);
334
335     /** @brief The renderer received a reply to a getImage request. */
336     void replyGetImage(const QString &, const QPixmap &);
337
338     /** @brief The renderer stopped, either playing or rendering. */
339     void stopped();
340
341     /** @brief The renderer started playing. */
342     void playing(double);
343
344     /** @brief The renderer started rendering. */
345     void rendering(const GenTime &);
346
347     /** @brief An error occurred within this renderer. */
348     void error(const QString &, const QString &);
349     void durationChanged(int);
350     void rendererPosition(int);
351     void rendererStopped(int);
352     /** @brief The clip is not valid, should be removed from project. */
353     void removeInvalidClip(const QString &, bool replaceProducer);
354     /** @brief The proxy is not valid, should be deleted.
355      *  @param id The original clip's id
356      *  @param durationError Should be set to true if the proxy failed because it has not same length as original clip
357      */
358     void removeInvalidProxy(const QString &id, bool durationError);
359     void refreshDocumentProducers(bool displayRatioChanged);
360
361     /** @brief A frame's image has to be shown.
362      *
363      * Used in Mac OS X. */
364     void showImageSignal(QImage);
365     void showAudioSignal(const QByteArray);
366
367 public slots:
368
369     /** @brief Starts the consumer. */
370     void start();
371
372     /** @brief Stops the consumer. */
373     void stop();
374     int getLength();
375
376     /** @brief Checks if the file is readable by MLT. */
377     bool isValid(KUrl url);
378
379     /** @brief Requests the file properties for the specified URL.
380         @param xml The xml parameters for the clip
381         @param clipId The clip Id string
382         @param imageHeight The height (in pixels) of the returned thumbnail (height of a treewidgetitem in projectlist)
383         @param replaceProducer If true, the MLT producer will be recreated
384         @param selectClip If true, clip item will be selected in project view
385      * Upon return, the result will be emitted via replyGetFileProperties().
386      * Wraps the VEML command of the same name. */
387     void getFileProperties(const QDomElement xml, const QString &clipId, int imageHeight, bool replaceProducer = true, bool selectClip = false);
388
389     void exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime);
390     void mltSavePlaylist();
391     void slotSplitView(bool doit);
392     void slotSwitchFullscreen();
393     void slotSetVolume(int volume);
394     void seekToFrame(int pos);
395 };
396
397 #endif