1 /***************************************************************************
2 krender.h - description
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)
8 ***************************************************************************/
10 /***************************************************************************
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. *
17 ***************************************************************************/
21 * @brief Client side of the interface to a renderer.
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.
33 #include "definitions.h"
34 #include "widgets/abstractmonitor.h"
36 #include <mlt/framework/mlt_types.h>
40 #include <QtXml/qdom.h>
71 struct requestClipInfo {
77 bool operator==(const requestClipInfo &a)
79 return clipId == a.clipId;
83 class MltErrorEvent : public QEvent
86 MltErrorEvent(const QString &message)
87 : QEvent(QEvent::User),
93 QString message() const {
102 class Render: public AbstractRender
106 enum FailStates { OK = 0,
109 /** @brief Build a MLT Renderer
110 * @param rendererName A unique identifier for this renderer
111 * @param winid The parent widget identifier (required for SDL display). Set to 0 for OpenGL rendering
112 * @param profile The MLT profile used for the renderer (default one will be used if empty). */
113 Render(Kdenlive::MonitorId rendererName, int winid, QString profile = QString(), QWidget *parent = 0, QGLWidget *mainGLContext = 0);
115 /** @brief Destroy the MLT Renderer. */
118 /** @brief Seeks the renderer clip to the given time. */
119 void seek(const GenTime &time);
121 void seekToFrameDiff(int diff);
123 QPixmap getImageThumbnail(const KUrl &url, int width, int height);
125 /** @brief Sets the current MLT producer playlist.
126 * @param list The xml describing the playlist
127 * @param position (optional) time to seek to */
128 int setSceneList(const QDomDocument &list, int position = 0);
130 /** @brief Sets the current MLT producer playlist.
131 * @param list new playlist
132 * @param position (optional) time to seek to
133 * @return 0 when it has success, different from 0 otherwise
135 * Creates the producer from the text playlist. */
136 int setSceneList(QString playlist, int position = 0);
137 int setProducer(Mlt::Producer *producer, int position);
139 /** @brief Get the current MLT producer playlist.
140 * @return A string describing the playlist */
141 const QString sceneList();
142 bool saveSceneList(QString path, QDomElement kdenliveData = QDomElement());
144 /** @brief Tells the renderer to play the scene at the specified speed,
145 * @param speed speed to play the scene to
147 * The speed is relative to normal playback, e.g. 1.0 is normal speed, 0.0
148 * is paused, -1.0 means play backwards. It does not specify start/stop */
149 void play(double speed);
150 void switchPlay(bool play);
153 /** @brief Stops playing.
154 * @param startTime time to seek to */
155 void stop(const GenTime &startTime);
158 QImage extractFrame(int frame_position, const QString &path = QString(), int width = -1, int height = -1);
160 /** @brief Plays the scene starting from a specific time.
161 * @param startTime time to start playing the scene from */
162 void play(const GenTime & startTime);
163 void playZone(const GenTime & startTime, const GenTime & stopTime);
164 void loopZone(const GenTime & startTime, const GenTime & stopTime);
166 void saveZone(KUrl url, QString desc, QPoint zone);
168 /** @brief Save a clip in timeline to an xml playlist. */
169 bool saveClip(int track, const GenTime &position, const KUrl &url, const QString &desc = QString());
171 /** @brief Return true if we are currently playing */
172 bool isPlaying() const;
174 /** @brief Returns the speed at which the renderer is currently playing.
176 * It returns 0.0 when the renderer is not playing anything. */
177 double playSpeed() const;
179 /** @brief Returns the current seek position of the renderer. */
180 GenTime seekPosition() const;
181 int seekFramePosition() const;
183 void emitFrameUpdated(Mlt::Frame&);
184 void emitFrameNumber();
185 void emitConsumerStopped(bool forcePause = false);
187 /** @brief Returns the aspect ratio of the consumer. */
188 double consumerRatio() const;
190 /** @brief Saves current producer frame as an image. */
191 void exportCurrentFrame(const KUrl &url, bool notify);
193 /** @brief Change the Mlt PROFILE
194 * @param profileName The MLT profile name
195 * @param dropSceneList If true, the current playlist will be deleted
196 * @return true if the profile was changed
198 int resetProfile(const QString& profileName, bool dropSceneList = false);
199 /** @brief Returns true if the render uses profileName as current profile. */
200 bool hasProfile(const QString& profileName) const;
203 /** @brief Returns the width of a frame for this profile. */
204 int frameRenderWidth() const;
205 /** @brief Returns the display width of a frame for this profile. */
206 int renderWidth() const;
207 /** @brief Returns the height of a frame for this profile. */
208 int renderHeight() const;
210 /** @brief Returns display aspect ratio. */
212 /** @brief Returns sample aspect ratio. */
214 /** @brief If monitor is active, refresh it. */
215 void refreshIfActive();
216 /** @brief Start the MLT monitor consumer. */
217 void startConsumer();
220 * Playlist manipulation.
222 Mlt::Producer *checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element);
223 int mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite = false, bool push = false);
224 bool mltUpdateClip(Mlt::Tractor *tractor, ItemInfo info, QDomElement element, Mlt::Producer *prod);
225 bool mltCutClip(int track, const GenTime &position);
226 void mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime &duration, const GenTime &timeOffset);
227 int mltGetSpaceLength(const GenTime &pos, int track, bool fromBlankStart);
229 /** @brief Returns the duration/length of @param track as reported by the track producer. */
230 int mltTrackDuration(int track);
232 bool mltResizeClipEnd(ItemInfo info, GenTime clipDuration, bool refresh = true);
233 bool mltResizeClipStart(ItemInfo info, GenTime diff);
234 bool mltResizeClipCrop(ItemInfo info, GenTime newCropStart);
235 bool mltMoveClip(int startTrack, int endTrack, GenTime pos, GenTime moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
236 bool mltMoveClip(int startTrack, int endTrack, int pos, int moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
237 bool mltRemoveClip(int track, GenTime position);
239 /** @brief Deletes an effect from a clip in MLT's playlist. */
240 bool mltRemoveEffect(int track, GenTime position, int index, bool updateIndex, bool doRefresh = true);
241 bool mltRemoveTrackEffect(int track, int index, bool updateIndex);
243 /** @brief Adds an effect to a clip in MLT's playlist. */
244 bool mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh = true);
245 bool addFilterToService(Mlt::Service service, EffectsParameterList params, int duration);
246 bool mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh);
247 bool mltAddTrackEffect(int track, EffectsParameterList params);
249 /** @brief Enable / disable clip effects.
250 * @param track The track where the clip is
251 * @param position The start position of the clip
252 * @param effectIndexes The list of effect indexes to enable / disable
253 * @param disable True if effects should be disabled, false otherwise */
254 bool mltEnableEffects(int track, const GenTime &position, const QList<int> &effectIndexes, bool disable);
255 /** @brief Enable / disable track effects.
256 * @param track The track where the effect is
257 * @param effectIndexes The list of effect indexes to enable / disable
258 * @param disable True if effects should be disabled, false otherwise */
259 bool mltEnableTrackEffects(int track, const QList<int> &effectIndexes, bool disable);
261 /** @brief Edits an effect parameters in MLT's playlist. */
262 bool mltEditEffect(int track, const GenTime &position, EffectsParameterList params);
263 bool mltEditTrackEffect(int track, EffectsParameterList params);
265 /** @brief Updates the "kdenlive_ix" (index) value of an effect. */
266 void mltUpdateEffectPosition(int track, const GenTime &position, int oldPos, int newPos);
268 /** @brief Changes the order of effects in MLT's playlist.
270 * It switches effects from oldPos and newPos, updating the "kdenlive_ix"
272 void mltMoveEffect(int track, const GenTime &position, int oldPos, int newPos);
273 void mltMoveTrackEffect(int track, int oldPos, int newPos);
275 /** @brief Enables/disables audio/video in a track. */
276 void mltChangeTrackState(int track, bool mute, bool blind);
277 bool mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut);
278 bool mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
279 void mltDeleteTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
280 void mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force = false);
281 void mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml);
282 void mltAddClipTransparency(ItemInfo info, int transitiontrack, int id);
283 void mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id);
284 void mltDeleteTransparency(int pos, int track, int id);
285 void mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id);
286 QList <TransitionInfo> mltInsertTrack(int ix, bool videoTrack);
287 void mltDeleteTrack(int ix);
288 bool mltUpdateClipProducer(Mlt::Tractor *tractor, int track, int pos, Mlt::Producer *prod);
289 void mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track);
290 Mlt::Producer *invalidProducer(const QString &id);
292 /** @brief Changes the speed of a clip in MLT's playlist.
294 * It creates a new "framebuffer" producer, which must have its "resource"
295 * property set to "video.mpg?0.6", where "video.mpg" is the path to the
296 * clip and "0.6" is the speed in percentage. The newly created producer
297 * will have its "id" property set to "slowmotion:parentid:speed", where
298 * "parentid" is the id of the original clip in the ClipManager list and
299 * "speed" is the current speed. */
300 int mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double oldspeed, int strobe, Mlt::Producer *prod);
302 const QList <Mlt::Producer *> producersList();
303 void updatePreviewSettings();
304 void setDropFrames(bool show);
305 /** @brief Sets an MLT consumer property. */
306 void setConsumerProperty(const QString &name, const QString &value);
307 QString updateSceneListFps(double current_fps, double new_fps, const QString &scene);
309 void showAudio(Mlt::Frame&);
311 QList <int> checkTrackSequence(int);
312 void sendFrameUpdate();
314 /** @brief Returns a pointer to the main producer. */
315 Mlt::Producer *getProducer();
316 /** @brief Returns the number of clips to process (When requesting clip info). */
317 int processingItems();
318 /** @brief Processing of this clip is over, producer was set on clip, remove from list. */
319 void processingDone(const QString &id);
320 /** @brief Force processing of clip with selected id. */
321 void forceProcessing(const QString &id);
322 /** @brief Are we currently processing clip with selected id. */
323 bool isProcessing(const QString &id);
325 /** @brief Requests the file properties for the specified URL (will be put in a queue list)
326 @param xml The xml parameters for the clip
327 @param clipId The clip Id string
328 @param imageHeight The height (in pixels) of the returned thumbnail (height of a treewidgetitem in projectlist)
329 @param replaceProducer If true, the MLT producer will be recreated */
330 void getFileProperties(const QDomElement &xml, const QString &clipId, int imageHeight, bool replaceProducer = true);
332 /** @brief Lock the MLT service */
333 Mlt::Tractor *lockService();
334 /** @brief Unlock the MLT service */
335 void unlockService(Mlt::Tractor *tractor);
336 const QString activeClipId();
337 /** @brief Fill a combobox with the found blackmagic devices */
338 static bool getBlackMagicDeviceList(KComboBox *devicelist, bool force = false);
339 static bool getBlackMagicOutputDeviceList(KComboBox *devicelist, bool force = false);
340 /** @brief Frame rendering is handeled by Kdenlive, don't show video through SDL display */
341 void disablePreview(bool disable);
342 /** @brief Get current seek pos requested of SEEK_INACTIVE if we are not currently seeking */
343 int requestedSeekPosition;
344 /** @brief Get current seek pos requested of current producer pos if not seeking */
345 int getCurrentSeekPosition() const;
346 /** @brief Create a producer from url and load it in the monitor */
347 void loadUrl(const QString &url);
348 /** @brief Check if the installed FFmpeg / Libav supports x11grab */
349 static bool checkX11Grab();
351 /** @brief Ask to set this monitor as active */
352 void setActiveMonitor();
354 QSemaphore showFrameSemaphore;
355 bool externalConsumer;
358 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
359 static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
360 static void consumer_thread_started(mlt_consumer, Render * self, mlt_frame frame_ptr);
361 static void consumer_thread_stopped(mlt_consumer, Render * self, mlt_frame frame_ptr);
365 /** @brief The name of this renderer.
367 * Useful to identify the renderers by what they do - e.g. background
368 * rendering, workspace monitor, etc. */
369 Kdenlive::MonitorId m_name;
370 Mlt::Consumer * m_mltConsumer;
371 Mlt::Producer * m_mltProducer;
372 Mlt::Profile *m_mltProfile;
373 Mlt::Event *m_showFrameEvent;
374 Mlt::Event *m_consumerThreadStartedEvent;
375 Mlt::Event *m_consumerThreadStoppedEvent;
376 Mlt::Event *m_pauseEvent;
379 /** @brief True if we are playing a zone.
381 * It's determined by the "in" and "out" properties being temporarily
387 /** @brief True when the monitor is in split view. */
390 Mlt::Producer *m_blackClip;
391 QString m_activeProfile;
394 QTimer m_refreshTimer;
398 /** @brief A human-readable description of this renderer. */
402 QFuture <void> m_infoThread;
403 QList <requestClipInfo> m_requestList;
405 /** @brief True if this monitor is active. */
407 QGLWidget *m_mainGLContext;
408 QGLWidget *m_GLContext;
409 QMap<pthread_t, QGLWidget *> m_renderThreadGLContexts;
410 Mlt::Filter* m_glslManager;
413 void mltCheckLength(Mlt::Tractor *tractor);
414 void mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest);
415 QMap<QString, QString> mltGetTransitionParamsFromXml(const QDomElement &xml);
416 QMap<QString, Mlt::Producer *> m_slowmotionProducers;
417 /** @brief The ids of the clips that are currently being loaded for info query */
418 QStringList m_processingClipId;
420 /** @brief Build the MLT Consumer object with initial settings.
421 * @param profileName The MLT profile to use for the consumer */
422 void buildConsumer(const QString& profileName);
423 void resetZoneMode();
424 void fillSlowMotionProducers();
425 /** @brief Get the track number of the lowest audible (non muted) audio track
426 * @param return The track number */
427 int getLowestNonMutedAudioTrack(Mlt::Tractor tractor);
429 /** @brief Make sure our audio mixing transitions are applied to the lowest track */
430 void fixAudioMixing(Mlt::Tractor tractor);
431 /** @brief Make sure we inform MLT if we need a lot of threads for avformat producer */
432 void checkMaxThreads();
433 /** @brief Clone serialisable properties only */
434 void cloneProperties(Mlt::Properties &dest, Mlt::Properties &source);
438 /** @brief Refreshes the monitor display. */
440 void slotOsdTimeout();
441 /** @brief Process the clip info requests (in a separate thread). */
442 void processFileProperties();
443 /** @brief A clip with multiple video streams was found, ask what to do. */
444 void slotMultiStreamProducerFound(const QString &path, QList<int> audio_list, QList<int> video_list, stringMap data);
445 void showFrame(Mlt::Frame *);
446 void slotCheckSeeking();
450 /** @brief The renderer received a reply to a getFileProperties request. */
451 void replyGetFileProperties(const QString &clipId, Mlt::Producer*, const stringMap &, const stringMap &, bool replaceProducer);
453 /** @brief The renderer received a reply to a getImage request. */
454 void replyGetImage(const QString &, const QString &, int, int);
455 void replyGetImage(const QString &, const QImage &);
457 /** @brief The renderer stopped, either playing or rendering. */
460 /** @brief The renderer started playing. */
461 void playing(double);
463 /** @brief The renderer started rendering. */
464 void rendering(const GenTime &);
466 /** @brief An error occurred within this renderer. */
467 void error(const QString &, const QString &);
468 void durationChanged(int);
469 void rendererPosition(int);
470 void rendererStopped(int);
471 /** @brief The clip is not valid, should be removed from project. */
472 void removeInvalidClip(const QString &, bool replaceProducer);
473 /** @brief The proxy is not valid, should be deleted.
474 * @param id The original clip's id
475 * @param durationError Should be set to true if the proxy failed because it has not same length as original clip
477 void removeInvalidProxy(const QString &id, bool durationError);
478 void refreshDocumentProducers(bool displayRatioChanged, bool fpsChanged);
479 /** @brief A proxy clip is missing, ask for creation. */
480 void requestProxy(QString);
481 /** @brief A multiple stream clip was found. */
482 void multiStreamFound(const QString &,QList<int>,QList<int>,stringMap data);
485 /** @brief A frame's image has to be shown.
487 * Used in Mac OS X. */
488 void showImageSignal(QImage);
489 void showImageSignal(GLuint);
490 void showAudioSignal(const QVector<double> &);
491 void addClip(const KUrl &, stringMap);
493 /** @brief Activate current monitor. */
494 void activateMonitor(Kdenlive::MonitorId);
495 void mltFrameReceived(Mlt::Frame *);
499 /** @brief Starts the consumer. */
502 /** @brief Stops the consumer. */
506 /** @brief Checks if the file is readable by MLT. */
507 bool isValid(const KUrl &url);
509 void exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime);
510 void mltSavePlaylist();
511 void slotSplitView(bool doit);
512 void slotSwitchFullscreen();
513 void slotSetVolume(int volume);
514 void seekToFrame(int pos);
515 /** @brief Starts a timer to query for a refresh. */