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