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