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 "abstractmonitor.h"
36 #include <mlt/framework/mlt_types.h>
70 struct requestClipInfo {
76 bool operator==(const requestClipInfo &a)
78 return clipId == a.clipId;
82 class MltErrorEvent : public QEvent
85 MltErrorEvent(const QString &message)
86 : QEvent(QEvent::User),
92 QString message() const {
101 class Render: public AbstractRender
105 enum FailStates { OK = 0,
108 /** @brief Build a MLT Renderer
109 * @param rendererName A unique identifier for this renderer
110 * @param winid The parent widget identifier (required for SDL display). Set to 0 for OpenGL rendering
111 * @param profile The MLT profile used for the renderer (default one will be used if empty). */
112 Render(Kdenlive::MONITORID rendererName, int winid, QString profile = QString(), QWidget *parent = 0);
114 /** @brief Destroy the MLT Renderer. */
117 /** @brief Seeks the renderer clip to the given time. */
118 void seek(const GenTime &time);
120 void seekToFrameDiff(int diff);
122 QPixmap getImageThumbnail(const KUrl &url, int width, int height);
124 /** @brief Sets the current MLT producer playlist.
125 * @param list The xml describing the playlist
126 * @param position (optional) time to seek to */
127 int setSceneList(const QDomDocument &list, int position = 0);
129 /** @brief Sets the current MLT producer playlist.
130 * @param list new playlist
131 * @param position (optional) time to seek to
132 * @return 0 when it has success, different from 0 otherwise
134 * Creates the producer from the text playlist. */
135 int setSceneList(QString playlist, int position = 0);
136 int setProducer(Mlt::Producer *producer, int position);
138 /** @brief Get the current MLT producer playlist.
139 * @return A string describing the playlist */
140 const QString sceneList();
141 bool saveSceneList(QString path, QDomElement kdenliveData = QDomElement());
143 /** @brief Tells the renderer to play the scene at the specified speed,
144 * @param speed speed to play the scene to
146 * The speed is relative to normal playback, e.g. 1.0 is normal speed, 0.0
147 * is paused, -1.0 means play backwards. It does not specify start/stop */
148 void play(double speed);
149 void switchPlay(bool play);
152 /** @brief Stops playing.
153 * @param startTime time to seek to */
154 void stop(const GenTime &startTime);
157 QImage extractFrame(int frame_position, const QString &path = QString(), int width = -1, int height = -1);
159 /** @brief Plays the scene starting from a specific time.
160 * @param startTime time to start playing the scene from */
161 void play(const GenTime & startTime);
162 void playZone(const GenTime & startTime, const GenTime & stopTime);
163 void loopZone(const GenTime & startTime, const GenTime & stopTime);
165 void saveZone(KUrl url, QString desc, QPoint zone);
167 /** @brief Save a clip in timeline to an xml playlist. */
168 bool saveClip(int track, const GenTime &position, const KUrl &url, const QString &desc = QString());
170 /** @brief Return true if we are currently playing */
171 bool isPlaying() const;
173 /** @brief Returns the speed at which the renderer is currently playing.
175 * It returns 0.0 when the renderer is not playing anything. */
176 double playSpeed() const;
178 /** @brief Returns the current seek position of the renderer. */
179 GenTime seekPosition() const;
180 int seekFramePosition() const;
182 void emitFrameUpdated(Mlt::Frame&);
183 void emitFrameNumber();
184 void emitConsumerStopped(bool forcePause = false);
186 /** @brief Returns the aspect ratio of the consumer. */
187 double consumerRatio() const;
189 /** @brief Saves current producer frame as an image. */
190 void exportCurrentFrame(const KUrl &url, bool notify);
192 /** @brief Change the Mlt PROFILE
193 * @param profileName The MLT profile name
194 * @param dropSceneList If true, the current playlist will be deleted
195 * @return true if the profile was changed
197 int resetProfile(const QString& profileName, bool dropSceneList = false);
198 /** @brief Returns true if the render uses profileName as current profile. */
199 bool hasProfile(const QString& profileName) const;
202 /** @brief Returns the width of a frame for this profile. */
203 int frameRenderWidth() const;
204 /** @brief Returns the display width of a frame for this profile. */
205 int renderWidth() const;
206 /** @brief Returns the height of a frame for this profile. */
207 int renderHeight() const;
209 /** @brief Returns display aspect ratio. */
211 /** @brief Returns sample aspect ratio. */
213 /** @brief If monitor is active, refresh it. */
214 void refreshIfActive();
215 /** @brief Start the MLT monitor consumer. */
216 void startConsumer();
219 * Playlist manipulation.
221 Mlt::Producer *checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element);
222 int mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite = false, bool push = false);
223 bool mltUpdateClip(Mlt::Tractor *tractor, ItemInfo info, QDomElement element, Mlt::Producer *prod);
224 bool mltCutClip(int track, const GenTime &position);
225 void mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime &duration, const GenTime &timeOffset);
226 int mltGetSpaceLength(const GenTime &pos, int track, bool fromBlankStart);
228 /** @brief Returns the duration/length of @param track as reported by the track producer. */
229 int mltTrackDuration(int track);
231 bool mltResizeClipEnd(ItemInfo info, GenTime clipDuration, bool refresh = true);
232 bool mltResizeClipStart(ItemInfo info, GenTime diff);
233 bool mltResizeClipCrop(ItemInfo info, GenTime newCropStart);
234 bool mltMoveClip(int startTrack, int endTrack, GenTime pos, GenTime moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
235 bool mltMoveClip(int startTrack, int endTrack, int pos, int moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
236 bool mltRemoveClip(int track, GenTime position);
238 /** @brief Deletes an effect from a clip in MLT's playlist. */
239 bool mltRemoveEffect(int track, GenTime position, int index, bool updateIndex, bool doRefresh = true);
240 bool mltRemoveTrackEffect(int track, int index, bool updateIndex);
242 /** @brief Adds an effect to a clip in MLT's playlist. */
243 bool mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh = true);
244 bool addFilterToService(Mlt::Service service, EffectsParameterList params, int duration);
245 bool mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh);
246 bool mltAddTrackEffect(int track, EffectsParameterList params);
248 /** @brief Enable / disable clip effects.
249 * @param track The track where the clip is
250 * @param position The start position of the clip
251 * @param effectIndexes The list of effect indexes to enable / disable
252 * @param disable True if effects should be disabled, false otherwise */
253 bool mltEnableEffects(int track, const GenTime &position, const QList<int> &effectIndexes, bool disable);
254 /** @brief Enable / disable track effects.
255 * @param track The track where the effect is
256 * @param effectIndexes The list of effect indexes to enable / disable
257 * @param disable True if effects should be disabled, false otherwise */
258 bool mltEnableTrackEffects(int track, const QList<int> &effectIndexes, bool disable);
260 /** @brief Edits an effect parameters in MLT's playlist. */
261 bool mltEditEffect(int track, const GenTime &position, EffectsParameterList params);
262 bool mltEditTrackEffect(int track, EffectsParameterList params);
264 /** @brief Updates the "kdenlive_ix" (index) value of an effect. */
265 void mltUpdateEffectPosition(int track, const GenTime &position, int oldPos, int newPos);
267 /** @brief Changes the order of effects in MLT's playlist.
269 * It switches effects from oldPos and newPos, updating the "kdenlive_ix"
271 void mltMoveEffect(int track, const GenTime &position, int oldPos, int newPos);
272 void mltMoveTrackEffect(int track, int oldPos, int newPos);
274 /** @brief Enables/disables audio/video in a track. */
275 void mltChangeTrackState(int track, bool mute, bool blind);
276 bool mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut);
277 bool mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
278 void mltDeleteTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
279 void mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force = false);
280 void mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml);
281 void mltAddClipTransparency(ItemInfo info, int transitiontrack, int id);
282 void mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id);
283 void mltDeleteTransparency(int pos, int track, int id);
284 void mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id);
285 QList <TransitionInfo> mltInsertTrack(int ix, bool videoTrack);
286 void mltDeleteTrack(int ix);
287 bool mltUpdateClipProducer(Mlt::Tractor *tractor, int track, int pos, Mlt::Producer *prod);
288 void mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track);
289 Mlt::Producer *invalidProducer(const QString &id);
291 /** @brief Changes the speed of a clip in MLT's playlist.
293 * It creates a new "framebuffer" producer, which must have its "resource"
294 * property set to "video.mpg?0.6", where "video.mpg" is the path to the
295 * clip and "0.6" is the speed in percentage. The newly created producer
296 * will have its "id" property set to "slowmotion:parentid:speed", where
297 * "parentid" is the id of the original clip in the ClipManager list and
298 * "speed" is the current speed. */
299 int mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double oldspeed, int strobe, Mlt::Producer *prod);
301 const QList <Mlt::Producer *> producersList();
302 void updatePreviewSettings();
303 void setDropFrames(bool show);
304 /** @brief Sets an MLT consumer property. */
305 void setConsumerProperty(const QString &name, const QString &value);
306 QString updateSceneListFps(double current_fps, double new_fps, const QString &scene);
308 void showAudio(Mlt::Frame&);
310 QList <int> checkTrackSequence(int);
311 void sendFrameUpdate();
313 /** @brief Returns a pointer to the main producer. */
314 Mlt::Producer *getProducer();
315 /** @brief Returns the number of clips to process (When requesting clip info). */
316 int processingItems();
317 /** @brief Processing of this clip is over, producer was set on clip, remove from list. */
318 void processingDone(const QString &id);
319 /** @brief Force processing of clip with selected id. */
320 void forceProcessing(const QString &id);
321 /** @brief Are we currently processing clip with selected id. */
322 bool isProcessing(const QString &id);
324 /** @brief Requests the file properties for the specified URL (will be put in a queue list)
325 @param xml The xml parameters for the clip
326 @param clipId The clip Id string
327 @param imageHeight The height (in pixels) of the returned thumbnail (height of a treewidgetitem in projectlist)
328 @param replaceProducer If true, the MLT producer will be recreated */
329 void getFileProperties(const QDomElement &xml, const QString &clipId, int imageHeight, bool replaceProducer = true);
331 /** @brief Lock the MLT service */
332 Mlt::Tractor *lockService();
333 /** @brief Unlock the MLT service */
334 void unlockService(Mlt::Tractor *tractor);
335 const QString activeClipId();
336 /** @brief Fill a combobox with the found blackmagic devices */
337 static bool getBlackMagicDeviceList(KComboBox *devicelist, bool force = false);
338 static bool getBlackMagicOutputDeviceList(KComboBox *devicelist, bool force = false);
339 /** @brief Frame rendering is handeled by Kdenlive, don't show video through SDL display */
340 void disablePreview(bool disable);
341 /** @brief Get current seek pos requested of SEEK_INACTIVE if we are not currently seeking */
342 int requestedSeekPosition;
343 /** @brief Get current seek pos requested of current producer pos if not seeking */
344 int getCurrentSeekPosition() const;
345 /** @brief Create a producer from url and load it in the monitor */
346 void loadUrl(const QString &url);
347 /** @brief Check if the installed FFmpeg / Libav supports x11grab */
348 static bool checkX11Grab();
350 /** @brief Ask to set this monitor as active */
351 void setActiveMonitor();
353 QSemaphore showFrameSemaphore;
354 bool externalConsumer;
357 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
358 static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
362 /** @brief The name of this renderer.
364 * Useful to identify the renderers by what they do - e.g. background
365 * rendering, workspace monitor, etc. */
366 Kdenlive::MONITORID m_name;
367 Mlt::Consumer * m_mltConsumer;
368 Mlt::Producer * m_mltProducer;
369 Mlt::Profile *m_mltProfile;
370 Mlt::Event *m_showFrameEvent;
371 Mlt::Event *m_pauseEvent;
374 /** @brief True if we are playing a zone.
376 * It's determined by the "in" and "out" properties being temporarily
382 /** @brief True when the monitor is in split view. */
385 Mlt::Producer *m_blackClip;
386 QString m_activeProfile;
389 QTimer m_refreshTimer;
393 /** @brief A human-readable description of this renderer. */
397 QFuture <void> m_infoThread;
398 QList <requestClipInfo> m_requestList;
400 /** @brief True if this monitor is active. */
404 void mltCheckLength(Mlt::Tractor *tractor);
405 void mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest);
406 QMap<QString, QString> mltGetTransitionParamsFromXml(const QDomElement &xml);
407 QMap<QString, Mlt::Producer *> m_slowmotionProducers;
408 /** @brief The ids of the clips that are currently being loaded for info query */
409 QStringList m_processingClipId;
411 /** @brief Build the MLT Consumer object with initial settings.
412 * @param profileName The MLT profile to use for the consumer */
413 void buildConsumer(const QString& profileName);
414 void resetZoneMode();
415 void fillSlowMotionProducers();
416 /** @brief Get the track number of the lowest audible (non muted) audio track
417 * @param return The track number */
418 int getLowestNonMutedAudioTrack(Mlt::Tractor tractor);
420 /** @brief Make sure our audio mixing transitions are applied to the lowest track */
421 void fixAudioMixing(Mlt::Tractor tractor);
422 /** @brief Make sure we inform MLT if we need a lot of threads for avformat producer */
423 void checkMaxThreads();
424 /** @brief Clone serialisable properties only */
425 void cloneProperties(Mlt::Properties &dest, Mlt::Properties &source);
429 /** @brief Refreshes the monitor display. */
431 void slotOsdTimeout();
432 /** @brief Process the clip info requests (in a separate thread). */
433 void processFileProperties();
434 /** @brief A clip with multiple video streams was found, ask what to do. */
435 void slotMultiStreamProducerFound(const QString &path, QList<int> audio_list, QList<int> video_list, stringMap data);
436 void showFrame(Mlt::Frame *);
437 void slotCheckSeeking();
441 /** @brief The renderer received a reply to a getFileProperties request. */
442 void replyGetFileProperties(const QString &clipId, Mlt::Producer*, const stringMap &, const stringMap &, bool replaceProducer);
444 /** @brief The renderer received a reply to a getImage request. */
445 void replyGetImage(const QString &, const QString &, int, int);
446 void replyGetImage(const QString &, const QImage &);
448 /** @brief The renderer stopped, either playing or rendering. */
451 /** @brief The renderer started playing. */
452 void playing(double);
454 /** @brief The renderer started rendering. */
455 void rendering(const GenTime &);
457 /** @brief An error occurred within this renderer. */
458 void error(const QString &, const QString &);
459 void durationChanged(int);
460 void rendererPosition(int);
461 void rendererStopped(int);
462 /** @brief The clip is not valid, should be removed from project. */
463 void removeInvalidClip(const QString &, bool replaceProducer);
464 /** @brief The proxy is not valid, should be deleted.
465 * @param id The original clip's id
466 * @param durationError Should be set to true if the proxy failed because it has not same length as original clip
468 void removeInvalidProxy(const QString &id, bool durationError);
469 void refreshDocumentProducers(bool displayRatioChanged, bool fpsChanged);
470 /** @brief A proxy clip is missing, ask for creation. */
471 void requestProxy(QString);
472 /** @brief A multiple stream clip was found. */
473 void multiStreamFound(const QString &,QList<int>,QList<int>,stringMap data);
476 /** @brief A frame's image has to be shown.
478 * Used in Mac OS X. */
479 void showImageSignal(QImage);
480 void showAudioSignal(const QVector<double> &);
481 void addClip(const KUrl &, stringMap);
483 /** @brief Activate current monitor. */
484 void activateMonitor(Kdenlive::MONITORID);
485 void mltFrameReceived(Mlt::Frame *);
489 /** @brief Starts the consumer. */
492 /** @brief Stops the consumer. */
496 /** @brief Checks if the file is readable by MLT. */
497 bool isValid(const KUrl &url);
499 void exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime);
500 void mltSavePlaylist();
501 void slotSplitView(bool doit);
502 void slotSwitchFullscreen();
503 void slotSetVolume(int volume);
504 void seekToFrame(int pos);
505 /** @brief Starts a timer to query for a refresh. */