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>
69 struct requestClipInfo {
75 bool operator==(const requestClipInfo &a)
77 return clipId == a.clipId;
81 class MltErrorEvent : public QEvent
84 MltErrorEvent(QString message) : QEvent(QEvent::User), m_message(message) {}
85 QString message() const {
94 class Render: public AbstractRender
98 enum FailStates { OK = 0,
101 /** @brief Build a MLT Renderer
102 * @param rendererName A unique identifier for this renderer
103 * @param winid The parent widget identifier (required for SDL display). Set to 0 for OpenGL rendering
104 * @param profile The MLT profile used for the renderer (default one will be used if empty). */
105 Render(Kdenlive::MONITORID rendererName, int winid, QString profile = QString(), QWidget *parent = 0);
107 /** @brief Destroy the MLT Renderer. */
110 /** @brief Seeks the renderer clip to the given time. */
111 void seek(GenTime time);
113 void seekToFrameDiff(int diff);
115 QPixmap getImageThumbnail(KUrl url, int width, int height);
117 /** @brief Sets the current MLT producer playlist.
118 * @param list The xml describing the playlist
119 * @param position (optional) time to seek to */
120 int setSceneList(QDomDocument list, int position = 0);
122 /** @brief Sets the current MLT producer playlist.
123 * @param list new playlist
124 * @param position (optional) time to seek to
125 * @return 0 when it has success, different from 0 otherwise
127 * Creates the producer from the text playlist. */
128 int setSceneList(QString playlist, int position = 0);
129 int setProducer(Mlt::Producer *producer, int position);
131 /** @brief Get the current MLT producer playlist.
132 * @return A string describing the playlist */
133 const QString sceneList();
134 bool saveSceneList(QString path, QDomElement kdenliveData = QDomElement());
136 /** @brief Tells the renderer to play the scene at the specified speed,
137 * @param speed speed to play the scene to
139 * The speed is relative to normal playback, e.g. 1.0 is normal speed, 0.0
140 * is paused, -1.0 means play backwards. It does not specify start/stop */
141 void play(double speed);
142 void switchPlay(bool play);
145 /** @brief Stops playing.
146 * @param startTime time to seek to */
147 void stop(const GenTime &startTime);
150 QImage extractFrame(int frame_position, QString path = QString(), int width = -1, int height = -1);
152 /** @brief Plays the scene starting from a specific time.
153 * @param startTime time to start playing the scene from */
154 void play(const GenTime & startTime);
155 void playZone(const GenTime & startTime, const GenTime & stopTime);
156 void loopZone(const GenTime & startTime, const GenTime & stopTime);
158 void saveZone(KUrl url, QString desc, QPoint zone);
160 /** @brief Save a clip in timeline to an xml playlist. */
161 bool saveClip(int track, GenTime position, KUrl url, QString desc = QString());
163 /** @brief Return true if we are currently playing */
164 bool isPlaying() const;
166 /** @brief Returns the speed at which the renderer is currently playing.
168 * It returns 0.0 when the renderer is not playing anything. */
169 double playSpeed() const;
171 /** @brief Returns the current seek position of the renderer. */
172 GenTime seekPosition() const;
173 int seekFramePosition() const;
175 void emitFrameUpdated(Mlt::Frame&);
176 void emitFrameNumber();
177 void emitConsumerStopped(bool forcePause = false);
179 /** @brief Returns the aspect ratio of the consumer. */
180 double consumerRatio() const;
182 /** @brief Saves current producer frame as an image. */
183 void exportCurrentFrame(KUrl url, bool notify);
185 /** @brief Change the Mlt PROFILE
186 * @param profileName The MLT profile name
187 * @param dropSceneList If true, the current playlist will be deleted
188 * @return true if the profile was changed
190 int resetProfile(const QString& profileName, bool dropSceneList = false);
191 /** @brief Returns true if the render uses profileName as current profile. */
192 bool hasProfile(const QString& profileName) const;
195 /** @brief Returns the width of a frame for this profile. */
196 int frameRenderWidth() const;
197 /** @brief Returns the display width of a frame for this profile. */
198 int renderWidth() const;
199 /** @brief Returns the height of a frame for this profile. */
200 int renderHeight() const;
202 /** @brief Returns display aspect ratio. */
204 /** @brief Returns sample aspect ratio. */
206 /** @brief If monitor is active, refresh it. */
207 void refreshIfActive();
208 /** @brief Start the MLT monitor consumer. */
209 void startConsumer();
212 * Playlist manipulation.
214 Mlt::Producer *checkSlowMotionProducer(Mlt::Producer *prod, QDomElement element);
215 int mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite = false, bool push = false);
216 bool mltUpdateClip(Mlt::Tractor *tractor, ItemInfo info, QDomElement element, Mlt::Producer *prod);
217 bool mltCutClip(int track, GenTime position);
218 void mltInsertSpace(QMap <int, int> trackClipStartList, QMap <int, int> trackTransitionStartList, int track, const GenTime &duration, const GenTime &timeOffset);
219 int mltGetSpaceLength(const GenTime &pos, int track, bool fromBlankStart);
221 /** @brief Returns the duration/length of @param track as reported by the track producer. */
222 int mltTrackDuration(int track);
224 bool mltResizeClipEnd(ItemInfo info, GenTime clipDuration, bool refresh = true);
225 bool mltResizeClipStart(ItemInfo info, GenTime diff);
226 bool mltResizeClipCrop(ItemInfo info, GenTime newCropStart);
227 bool mltMoveClip(int startTrack, int endTrack, GenTime pos, GenTime moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
228 bool mltMoveClip(int startTrack, int endTrack, int pos, int moveStart, Mlt::Producer *prod, bool overwrite = false, bool insert = false);
229 bool mltRemoveClip(int track, GenTime position);
231 /** @brief Deletes an effect from a clip in MLT's playlist. */
232 bool mltRemoveEffect(int track, GenTime position, int index, bool updateIndex, bool doRefresh = true);
233 bool mltRemoveTrackEffect(int track, int index, bool updateIndex);
235 /** @brief Adds an effect to a clip in MLT's playlist. */
236 bool mltAddEffect(int track, GenTime position, EffectsParameterList params, bool doRefresh = true);
237 bool addFilterToService(Mlt::Service service, EffectsParameterList params, int duration);
238 bool mltAddEffect(Mlt::Service service, EffectsParameterList params, int duration, bool doRefresh);
239 bool mltAddTrackEffect(int track, EffectsParameterList params);
241 /** @brief Enable / disable clip effects.
242 * @param track The track where the clip is
243 * @param position The start position of the clip
244 * @param effectIndexes The list of effect indexes to enable / disable
245 * @param disable True if effects should be disabled, false otherwise */
246 bool mltEnableEffects(int track, GenTime position, QList <int> effectIndexes, bool disable);
247 /** @brief Enable / disable track effects.
248 * @param track The track where the effect is
249 * @param effectIndexes The list of effect indexes to enable / disable
250 * @param disable True if effects should be disabled, false otherwise */
251 bool mltEnableTrackEffects(int track, QList <int> effectIndexes, bool disable);
253 /** @brief Edits an effect parameters in MLT's playlist. */
254 bool mltEditEffect(int track, GenTime position, EffectsParameterList params);
255 bool mltEditTrackEffect(int track, EffectsParameterList params);
257 /** @brief Updates the "kdenlive_ix" (index) value of an effect. */
258 void mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos);
260 /** @brief Changes the order of effects in MLT's playlist.
262 * It switches effects from oldPos and newPos, updating the "kdenlive_ix"
264 void mltMoveEffect(int track, GenTime position, int oldPos, int newPos);
265 void mltMoveTrackEffect(int track, int oldPos, int newPos);
267 /** @brief Enables/disables audio/video in a track. */
268 void mltChangeTrackState(int track, bool mute, bool blind);
269 bool mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut);
270 bool mltAddTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
271 void mltDeleteTransition(QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool refresh = true);
272 void mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml, bool force = false);
273 void mltUpdateTransitionParams(QString type, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml);
274 void mltAddClipTransparency(ItemInfo info, int transitiontrack, int id);
275 void mltMoveTransparency(int startTime, int endTime, int startTrack, int endTrack, int id);
276 void mltDeleteTransparency(int pos, int track, int id);
277 void mltResizeTransparency(int oldStart, int newStart, int newEnd, int track, int id);
278 QList <TransitionInfo> mltInsertTrack(int ix, bool videoTrack);
279 void mltDeleteTrack(int ix);
280 bool mltUpdateClipProducer(Mlt::Tractor *tractor, int track, int pos, Mlt::Producer *prod);
281 void mltPlantTransition(Mlt::Field *field, Mlt::Transition &tr, int a_track, int b_track);
282 Mlt::Producer *invalidProducer(const QString &id);
284 /** @brief Changes the speed of a clip in MLT's playlist.
286 * It creates a new "framebuffer" producer, which must have its "resource"
287 * property set to "video.mpg?0.6", where "video.mpg" is the path to the
288 * clip and "0.6" is the speed in percentage. The newly created producer
289 * will have its "id" property set to "slowmotion:parentid:speed", where
290 * "parentid" is the id of the original clip in the ClipManager list and
291 * "speed" is the current speed. */
292 int mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double oldspeed, int strobe, Mlt::Producer *prod);
294 const QList <Mlt::Producer *> producersList();
295 void updatePreviewSettings();
296 void setDropFrames(bool show);
297 QString updateSceneListFps(double current_fps, double new_fps, QString scene);
299 void showAudio(Mlt::Frame&);
301 QList <int> checkTrackSequence(int);
302 void sendFrameUpdate();
304 /** @brief Returns a pointer to the main producer. */
305 Mlt::Producer *getProducer();
306 /** @brief Returns the number of clips to process (When requesting clip info). */
307 int processingItems();
308 /** @brief Processing of this clip is over, producer was set on clip, remove from list. */
309 void processingDone(const QString &id);
310 /** @brief Force processing of clip with selected id. */
311 void forceProcessing(const QString &id);
312 /** @brief Are we currently processing clip with selected id. */
313 bool isProcessing(const QString &id);
315 /** @brief Requests the file properties for the specified URL (will be put in a queue list)
316 @param xml The xml parameters for the clip
317 @param clipId The clip Id string
318 @param imageHeight The height (in pixels) of the returned thumbnail (height of a treewidgetitem in projectlist)
319 @param replaceProducer If true, the MLT producer will be recreated */
320 void getFileProperties(const QDomElement &xml, const QString &clipId, int imageHeight, bool replaceProducer = true);
322 /** @brief Lock the MLT service */
323 Mlt::Tractor *lockService();
324 /** @brief Unlock the MLT service */
325 void unlockService(Mlt::Tractor *tractor);
326 const QString activeClipId();
327 /** @brief Fill a combobox with the found blackmagic devices */
328 static bool getBlackMagicDeviceList(KComboBox *devicelist, bool force = false);
329 static bool getBlackMagicOutputDeviceList(KComboBox *devicelist, bool force = false);
330 /** @brief Frame rendering is handeled by Kdenlive, don't show video through SDL display */
331 void disablePreview(bool disable);
332 /** @brief Get current seek pos requested of SEEK_INACTIVE if we are not currently seeking */
333 int requestedSeekPosition;
334 /** @brief Get current seek pos requested of current producer pos if not seeking */
335 int getCurrentSeekPosition() const;
336 /** @brief Create a producer from url and load it in the monitor */
337 void loadUrl(const QString &url);
339 QSemaphore showFrameSemaphore;
340 bool externalConsumer;
343 static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
344 static void consumer_gl_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr);
348 /** @brief The name of this renderer.
350 * Useful to identify the renderers by what they do - e.g. background
351 * rendering, workspace monitor, etc. */
352 Kdenlive::MONITORID m_name;
353 Mlt::Consumer * m_mltConsumer;
354 Mlt::Producer * m_mltProducer;
355 Mlt::Profile *m_mltProfile;
356 Mlt::Event *m_showFrameEvent;
357 Mlt::Event *m_pauseEvent;
360 /** @brief True if we are playing a zone.
362 * It's determined by the "in" and "out" properties being temporarily
368 /** @brief True when the monitor is in split view. */
371 Mlt::Producer *m_blackClip;
372 QString m_activeProfile;
375 QTimer m_refreshTimer;
379 /** @brief A human-readable description of this renderer. */
383 QFuture <void> m_infoThread;
384 QList <requestClipInfo> m_requestList;
388 void mltCheckLength(Mlt::Tractor *tractor);
389 void mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest);
390 QMap<QString, QString> mltGetTransitionParamsFromXml(QDomElement xml);
391 QMap<QString, Mlt::Producer *> m_slowmotionProducers;
392 /** @brief The ids of the clips that are currently being loaded for info query */
393 QStringList m_processingClipId;
395 /** @brief Build the MLT Consumer object with initial settings.
396 * @param profileName The MLT profile to use for the consumer */
397 void buildConsumer(const QString& profileName);
398 void resetZoneMode();
399 void fillSlowMotionProducers();
400 /** @brief Get the track number of the lowest audible (non muted) audio track
401 * @param return The track number */
402 int getLowestNonMutedAudioTrack(Mlt::Tractor tractor);
404 /** @brief Make sure our audio mixing transitions are applied to the lowest track */
405 void fixAudioMixing(Mlt::Tractor tractor);
406 /** @brief Make sure we inform MLT if we need a lot of threads for avformat producer */
407 void checkMaxThreads();
411 /** @brief Refreshes the monitor display. */
413 void slotOsdTimeout();
414 /** @brief Process the clip info requests (in a separate thread). */
415 void processFileProperties();
416 /** @brief A clip with multiple video streams was found, ask what to do. */
417 void slotMultiStreamProducerFound(const QString path, QList<int> audio_list, QList<int> video_list, stringMap data);
418 void showFrame(Mlt::Frame *);
419 void slotCheckSeeking();
423 /** @brief The renderer received a reply to a getFileProperties request. */
424 void replyGetFileProperties(const QString &clipId, Mlt::Producer*, const stringMap &, const stringMap &, bool replaceProducer);
426 /** @brief The renderer received a reply to a getImage request. */
427 void replyGetImage(const QString &, const QString &, int, int);
428 void replyGetImage(const QString &, const QImage &);
430 /** @brief The renderer stopped, either playing or rendering. */
433 /** @brief The renderer started playing. */
434 void playing(double);
436 /** @brief The renderer started rendering. */
437 void rendering(const GenTime &);
439 /** @brief An error occurred within this renderer. */
440 void error(const QString &, const QString &);
441 void durationChanged(int);
442 void rendererPosition(int);
443 void rendererStopped(int);
444 /** @brief The clip is not valid, should be removed from project. */
445 void removeInvalidClip(const QString &, bool replaceProducer);
446 /** @brief The proxy is not valid, should be deleted.
447 * @param id The original clip's id
448 * @param durationError Should be set to true if the proxy failed because it has not same length as original clip
450 void removeInvalidProxy(const QString &id, bool durationError);
451 void refreshDocumentProducers(bool displayRatioChanged, bool fpsChanged);
452 /** @brief A proxy clip is missing, ask for creation. */
453 void requestProxy(QString);
454 /** @brief A multiple stream clip was found. */
455 void multiStreamFound(const QString &,QList<int>,QList<int>,stringMap data);
458 /** @brief A frame's image has to be shown.
460 * Used in Mac OS X. */
461 void showImageSignal(QImage);
462 void showAudioSignal(const QVector<double> &);
463 void addClip(const KUrl &, stringMap);
465 void mltFrameReceived(Mlt::Frame *);
469 /** @brief Starts the consumer. */
472 /** @brief Stops the consumer. */
476 /** @brief Checks if the file is readable by MLT. */
477 bool isValid(KUrl url);
479 void exportFileToFirewire(QString srcFileName, int port, GenTime startTime, GenTime endTime);
480 void mltSavePlaylist();
481 void slotSplitView(bool doit);
482 void slotSwitchFullscreen();
483 void slotSetVolume(int volume);
484 void seekToFrame(int pos);
485 /** @brief Starts a timer to query for a refresh. */