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