Warning, file /multimedia/kdenlive/src/core.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002 SPDX-FileCopyrightText: 2014 Till Theato <root@ttill.de>
0003 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #pragma once
0007 
0008 #include "definitions.h"
0009 #include "jobs/taskmanager.h"
0010 #include "kdenlivecore_export.h"
0011 #include "undohelper.hpp"
0012 #include "utils/timecode.h"
0013 #include <KSharedDataCache>
0014 #include <QColor>
0015 #include <QMutex>
0016 #include <QObject>
0017 #include <QPoint>
0018 #include <QTextEdit>
0019 #include <QThreadPool>
0020 #include <QUrl>
0021 #include <knewstuff_version.h>
0022 #include <memory>
0023 #include <unordered_set>
0024 
0025 #include <mlt++/MltProfile.h>
0026 #include <mlt++/MltPlaylist.h>
0027 
0028 class Bin;
0029 class DocUndoStack;
0030 class EffectStackModel;
0031 class KdenliveDoc;
0032 class LibraryWidget;
0033 class MainWindow;
0034 class MediaCapture;
0035 class MixerManager;
0036 class Monitor;
0037 class MonitorManager;
0038 class ProfileModel;
0039 class ProjectItemModel;
0040 class ProjectManager;
0041 class SubtitleEdit;
0042 class SubtitleModel;
0043 class TextBasedEdit;
0044 class GuidesList;
0045 class TimeRemap;
0046 
0047 namespace Mlt {
0048     class Repository;
0049     class Producer;
0050 } // namespace Mlt
0051 
0052 #define EXIT_RESTART (42)
0053 #define EXIT_CLEAN_RESTART (43)
0054 #define pCore Core::self()
0055 
0056 /**
0057  * @class Core
0058  * @brief Singleton that provides access to the different parts of Kdenlive
0059  *
0060  * Needs to be initialize before any widgets are created in MainWindow.
0061  * Plugins should be loaded after the widget setup.
0062  */
0063 class /*KDENLIVECORE_EXPORT*/ Core : public QObject
0064 {
0065     Q_OBJECT
0066 
0067 public:
0068     Core(const Core &) = delete;
0069     Core &operator=(const Core &) = delete;
0070     Core(Core &&) = delete;
0071     Core &operator=(Core &&) = delete;
0072 
0073     ~Core() override;
0074 
0075     /**
0076      * @brief Setup the basics of the application, in particular the connection
0077      * with Mlt
0078      * @param MltPath (optional) path to MLT environment
0079      */
0080     static bool build(const QString &packageType, bool testMode = false);
0081 
0082     /**
0083      * @brief Init the GUI part of the app and show the main window
0084      * @param inSandbox does the app run in a sanbox? If yes, we use App path to deduce
0085      * other binaries paths (melt, ffmpeg, etc)
0086      * @param MltPath
0087      * @param Url (optional) file to open
0088      * If Url is present, it will be opened, otherwise, if openlastproject is
0089      * set, latest project will be opened. If no file is open after trying this,
0090      * a default new file will be created.
0091      * @param clipsToLoad
0092      */
0093     void initGUI(bool inSandbox, const QString &MltPath, const QUrl &Url, const QString &clipsToLoad = QString());
0094 
0095     /** @brief Returns a pointer to the singleton object. */
0096     static std::unique_ptr<Core> &self();
0097 
0098     /** @brief Delete the global core instance */
0099     static void clean();
0100 
0101     /** @brief Returns a pointer to the main window. */
0102     MainWindow *window();
0103 
0104     /** @brief Open a file using an external app. */
0105     QString openExternalApp(QString appPath, const QStringList args);
0106 
0107     /** @brief Returns a pointer to the project manager. */
0108     ProjectManager *projectManager();
0109     /** @brief Returns a pointer to the current project. */
0110     KdenliveDoc *currentDoc();
0111     /** @brief Returns project's timecode. */
0112     Timecode timecode() const;
0113     /** @brief Returns a pointer to the monitor manager. */
0114     MonitorManager *monitorManager();
0115     /** @brief Returns a pointer to the view of the project bin. */
0116     Bin *bin();
0117     /** @brief Returns a pointer to the view of the active bin (or main bin on no focus). */
0118     Bin *activeBin();
0119     /** @brief Select a clip in the Bin from its id. */
0120     void selectBinClip(const QString &id, bool activateMonitor = true, int frame = -1, const QPoint &zone = QPoint());
0121     /** @brief Selects an item in the current timeline (clip, composition, subtitle). */
0122     void selectTimelineItem(int id);
0123     /** @brief Returns a pointer to the model of the project bin. */
0124     std::shared_ptr<ProjectItemModel> projectItemModel();
0125     /** @brief Returns a pointer to the library. */
0126     LibraryWidget *library();
0127     /** @brief Returns a pointer to the subtitle edit. */
0128     SubtitleEdit *subtitleWidget();
0129     /** @brief Returns a pointer to the text based editing widget. */
0130     TextBasedEdit *textEditWidget();
0131     /** @brief Returns a pointer to the guides list widget. */
0132     GuidesList *guidesList();
0133     /** @brief Returns a pointer to the time remapping widget. */
0134     TimeRemap *timeRemapWidget();
0135     /** @brief Returns true if clip displayed in remap widget is the bin clip with id clipId. */
0136     bool currentRemap(const QString &clipId);
0137     /** @brief Returns a pointer to the audio mixer. */
0138     MixerManager *mixer();
0139     ToolType::ProjectTool activeTool();
0140 
0141     /** @brief Returns a pointer to MLT's repository */
0142     std::unique_ptr<Mlt::Repository> &getMltRepository();
0143 
0144     /** @brief Returns a pointer to the current profile */
0145     std::unique_ptr<ProfileModel> &getCurrentProfile() const;
0146     const QString &getCurrentProfilePath() const;
0147 
0148     /** @brief Define the active profile
0149      *  @returns true if profile exists, false if not found
0150      */
0151     bool setCurrentProfile(const QString &profilePath);
0152     /** @brief Returns Sample Aspect Ratio of current profile */
0153     double getCurrentSar() const;
0154     /** @brief Returns Display Aspect Ratio of current profile */
0155     double getCurrentDar() const;
0156 
0157     /** @brief Returns frame rate of current profile */
0158     double getCurrentFps() const;
0159 
0160     /** @brief Returns the frame size (width x height) of current profile */
0161     QSize getCurrentFrameSize() const;
0162     /** @brief Returns the frame display size (width x height) of current profile */
0163     QSize getCurrentFrameDisplaySize() const;
0164     /** @brief Request project monitor refresh, and restore previously active monitor */
0165     void refreshProjectMonitorOnce();
0166     /** @brief Request project monitor refresh if current position is inside range*/
0167     void refreshProjectRange(QPair<int, int> range);
0168     /** @brief Request project monitor refresh if referenced item is under cursor */
0169     void refreshProjectItem(const ObjectId &id);
0170     /** @brief Returns a reference to a monitor (clip or project monitor) */
0171     Monitor *getMonitor(int id);
0172     /** @brief Seek a monitor to position */
0173     void seekMonitor(int id, int position);
0174     /** @brief Returns timeline's active track info (position and tag) */
0175     QPair <int,QString> currentTrackInfo() const;
0176     /** @brief This function must be called whenever the profile used changes */
0177     void profileChanged();
0178 
0179     /** @brief Create and push and undo object based on the corresponding functions
0180         Note that if you class permits and requires it, you should use the macro PUSH_UNDO instead*/
0181     void pushUndo(const Fun &undo, const Fun &redo, const QString &text);
0182     void pushUndo(QUndoCommand *command);
0183     /** @brief display a user info/warning message in statusbar */
0184     void displayMessage(const QString &message, MessageType type, int timeout = -1);
0185     /** @brief display timeline selection info in statusbar */
0186     void displaySelectionMessage(const QString &message);
0187     /** @brief Clear asset view if itemId is displayed. */
0188     void clearAssetPanel(int itemId);
0189     /** @brief Returns the effectstack of a given bin clip. */
0190     std::shared_ptr<EffectStackModel> getItemEffectStack(const QUuid &uuid, int itemType, int itemId);
0191     int getItemPosition(const ObjectId &id);
0192     /** @brief Get item in point. */
0193     int getItemIn(const ObjectId &id);
0194     /** @brief Get item in point, possibly from another timeline. */
0195     int getItemIn(const QUuid &uuid, const ObjectId &id);
0196     int getItemTrack(const ObjectId &id);
0197     int getItemDuration(const ObjectId &id);
0198     QSize getItemFrameSize(const ObjectId &id);
0199     /** @brief Returns the capabilities of a clip: AudioOnly, VideoOnly or Disabled if both are allowed */
0200     PlaylistState::ClipState getItemState(const ObjectId &id);
0201     /** @brief Get a list of video track names with indexes */
0202     QMap<int, QString> getTrackNames(bool videoOnly);
0203     /** @brief Returns the composition A track (MLT index / Track id) */
0204     QPair<int, int> getCompositionATrack(int cid) const;
0205     void setCompositionATrack(int cid, int aTrack);
0206     /** @brief Return true if composition's a_track is automatic (no forced track)
0207      */
0208     bool compositionAutoTrack(int cid) const;
0209     std::shared_ptr<DocUndoStack> undoStack();
0210     double getClipSpeed(int id) const;
0211     /** @brief Mark an item as invalid for timeline preview */
0212     void invalidateItem(ObjectId itemId);
0213     void invalidateRange(QPair<int, int>range);
0214     void prepareShutdown();
0215     void finishShutdown();
0216     /** the keyframe model changed (effect added, deleted, active effect changed), inform timeline */
0217     void updateItemKeyframes(ObjectId id);
0218     /** A fade for clip id changed, update timeline */
0219     void updateItemModel(ObjectId id, const QString &service);
0220     /** Show / hide keyframes for a timeline clip */
0221     void showClipKeyframes(ObjectId id, bool enable);
0222     Mlt::Profile *thumbProfile();
0223     /** @brief Returns the current project duration */
0224     int projectDuration() const;
0225     /** @brief Returns true if current project has some rendered timeline preview  */
0226     bool hasTimelinePreview() const;
0227     /** @brief Returns monitor position  */
0228     int getMonitorPosition(Kdenlive::MonitorId id = Kdenlive::ProjectMonitor) const;
0229     /** @brief Handles audio and video capture **/
0230     void startMediaCapture(int tid, bool, bool);
0231     void stopMediaCapture(int tid, bool, bool);
0232     QStringList getAudioCaptureDevices();
0233     int getMediaCaptureState();
0234     bool isMediaMonitoring() const;
0235     bool isMediaCapturing() const;
0236     MediaCapture *getAudioDevice();
0237     /** @brief Returns Project Folder name for capture output location */
0238     QString getProjectFolderName(bool folderForAudio = false);
0239     /** @brief Returns a timeline clip's bin id */
0240     QString getTimelineClipBinId(int cid);
0241     /** @brief Returns all track ids in timeline */
0242     std::unordered_set<QString> getAllTimelineTracksId();
0243     /** @brief Returns a frame duration from a timecode */
0244     int getDurationFromString(const QString &time);
0245     /** @brief An error occurred within a filter, inform user */
0246     void processInvalidFilter(const QString &service, const QString &id, const QString &message);
0247     /** @brief Update current project's tags */
0248     void updateProjectTags(int previousCount, const QMap <int, QStringList> &tags);
0249     /** @brief Returns the project profile */
0250     Mlt::Profile *getProjectProfile();
0251     /** @brief Returns the consumer profile, that will be scaled
0252      *  according to preview settings. Should only be used on the consumer */
0253     Mlt::Profile &getMonitorProfile();
0254     /** @brief Returns a copy of current timeline's master playlist */
0255     std::unique_ptr<Mlt::Producer> getMasterProducerInstance();
0256     /** @brief Returns a copy of a track's playlist */
0257     std::unique_ptr<Mlt::Producer> getTrackProducerInstance(int tid);
0258     /** @brief Returns the undo stack index (position). */
0259     int undoIndex() const;
0260     /** @brief Enable / disable monitor multitrack view. Returns false if multitrack was not previously enabled */
0261     bool enableMultiTrack(bool enable);
0262     /** @brief Returns number of audio channels for this project. */
0263     int audioChannels();
0264     /** @brief Add guides in the project. */
0265     void addGuides(const QList <int> &guides);
0266     /** @brief Temporarily un/plug a list of clips in timeline. */
0267     void temporaryUnplug(const QList<int> &clipIds, bool hide);
0268     /** @brief Transcode a video file. */
0269     void transcodeFile(const QString &url);
0270     /** @brief Display key binding info in statusbar. */
0271     void setWidgetKeyBinding(const QString &mess = QString());
0272     KSharedDataCache audioThumbCache;
0273     /* @brief The thread job pool for clip jobs, allowing to set a max number of concurrent jobs */
0274     TaskManager taskManager;
0275     /** @brief The number of clip load jobs changed */
0276     void loadingClips(int);
0277     /** @brief Resize current mix item */
0278     void resizeMix(int cid, int duration, MixAlignment align, int leftFrames = -1);
0279     /** @brief Get Mix cut pos (the duration of the mix on the right clip) */
0280     int getMixCutPos(int cid) const;
0281     /** @brief Get alignment info for a mix item */
0282     MixAlignment getMixAlign(int cid) const;
0283     /** @brief Closing current document, do some cleanup */
0284     void cleanup();
0285     /** @brief Create the dock widgets */
0286     void buildDocks();
0287 #if KNEWSTUFF_VERSION < QT_VERSION_CHECK(5, 98, 0)
0288     /** @brief Instantiates a "Get Hot New Stuff" dialog.
0289      * @param configFile configuration file for KNewStuff
0290      * @return number of installed items */
0291     int getNewStuff(const QString &configFile);
0292 #endif
0293     /** @brief Get the frame size of the clip above a composition */
0294     const QSize getCompositionSizeOnTrack(const ObjectId &id);
0295     void loadTimelinePreview(const QUuid uuid, const QString &chunks, const QString &dirty, bool enablePreview, Mlt::Playlist &playlist);
0296     /** @brief Returns true if the audio mixer widget is visible */
0297     bool audioMixerVisible{false};
0298     QString packageType() { return m_packageType; };
0299     /** @brief Start / stop audio capture */
0300     void switchCapture();
0301     /** @brief Get the uuid of currently active timeline */
0302     const QUuid currentTimelineId() const;
0303     /** @brief Update a sequence AV info (has audio/video) */
0304     void updateSequenceAVType(const QUuid &uuid);
0305     /** @brief A list of markers type categories {marker type, {color, category name}} */
0306     struct MarkerCategory
0307     {
0308         QColor color;
0309         QString displayName;
0310     };
0311     QMap<int, MarkerCategory> markerTypes;
0312 
0313 private:
0314     explicit Core(const QString &packageType);
0315     static std::unique_ptr<Core> m_self;
0316 
0317     /** @brief Makes sure Qt's locale and system locale settings match. */
0318     void initLocale();
0319 
0320     MainWindow *m_mainWindow{nullptr};
0321     ProjectManager *m_projectManager{nullptr};
0322     MonitorManager *m_monitorManager{nullptr};
0323     std::shared_ptr<ProjectItemModel> m_projectItemModel;
0324     LibraryWidget *m_library{nullptr};
0325     SubtitleEdit *m_subtitleWidget{nullptr};
0326     TextBasedEdit *m_textEditWidget{nullptr};
0327     GuidesList *m_guidesList{nullptr};
0328     TimeRemap *m_timeRemapWidget{nullptr};
0329     MixerManager *m_mixerWidget{nullptr};
0330 
0331     /** @brief Current project's profile path */
0332     QString m_currentProfile;
0333 
0334     QString m_profile;
0335     QString m_packageType;
0336     Timecode m_timecode;
0337     std::unique_ptr<Mlt::Profile> m_thumbProfile;
0338     /** @brief Mlt profile used in the consumer 's monitors */
0339     Mlt::Profile m_monitorProfile;
0340     /** @brief Mlt profile used to build the project's clips */
0341     std::unique_ptr<Mlt::Profile> m_projectProfile;
0342     bool m_guiConstructed = false;
0343     /** @brief Check that the profile is valid (width is a multiple of 8 and height a multiple of 2 */
0344     void checkProfileValidity();
0345     std::unique_ptr<MediaCapture> m_capture;
0346     QUrl m_mediaCaptureFile;
0347     QMutex m_thumbProfileMutex;
0348 
0349 public Q_SLOTS:
0350     /** @brief Trigger (launch) an action by its actionCollection name */
0351     void triggerAction(const QString &name);
0352     /** @brief Get an action's descriptive text by its actionCollection name */
0353     const QString actionText(const QString &name);
0354     /** @brief Add an action to the app's actionCollection */
0355     void addActionToCollection(const QString &name, QAction *action);
0356     /** @brief display a user info/warning message in the project bin */
0357     void displayBinMessage(const QString &text, int type, const QList<QAction *> &actions = QList<QAction *>(), bool showClose = false, BinMessage::BinCategory messageCategory = BinMessage::BinCategory::NoMessage);
0358     void displayBinLogMessage(const QString &text, int type, const QString logInfo);
0359     /** @brief Create small thumbnails for luma used in compositions */
0360     void buildLumaThumbs(const QStringList &values);
0361     /** @brief Try to find a display name for the given filename.
0362      *  This is espacally helpfull for mlt's dynamically created luma files without thumb (luma01.pgm, luma02.pgm,...),
0363      *  but also for others as it makes the visible name translatable.
0364      *  @return The name that fits to the filename or if none is found the filename it self
0365      */
0366     const QString nameForLumaFile(const QString &filename);
0367     /** @brief Set current project modified. */
0368     void setDocumentModified();
0369     /** @brief Show currently selected effect zone in timeline ruler. */
0370     void showEffectZone(ObjectId id, QPair <int, int>inOut, bool checked);
0371     void updateMasterZones();
0372     /** @brief Open the proxies test dialog. */
0373     void testProxies();
0374     /** @brief Refresh the monitor profile when project profile changes. */
0375     void updateMonitorProfile();
0376     /** @brief Add a new Bin Widget. */
0377     void addBin(const QString &id = QString());
0378     /** @brief Transcode a bin clip video. */
0379     void transcodeFriendlyFile(const QString &binId, bool checkProfile);
0380     /** @brief Reset audio  monitoring volume and channels. */
0381     void resetAudioMonitoring();
0382     /** @brief Start audio recording (after countdown). */
0383     void startRecording();
0384     /** @brief Show or hide track head audio rec controls. */
0385     void monitorAudio(int tid, bool monitor);
0386 
0387 Q_SIGNALS:
0388     void coreIsReady();
0389     void updateLibraryPath();
0390     //void updateMonitorProfile();
0391     /** @brief Call config dialog on a selected page / tab */
0392     void showConfigDialog(Kdenlive::ConfigPage, int);
0393     void finalizeRecording(const QString &captureFile);
0394     void autoScrollChanged();
0395     /** @brief Send a message to splash screen if still displayed */
0396     void loadingMessageUpdated(const QString &, int progress = 0, int max = -1);
0397     /** @brief Opening finished, close splash screen */
0398     void closeSplash();
0399     /** @brief Trigger an update of the speech models list */
0400     void voskModelUpdate(const QStringList models);
0401     /** @brief Update current effect zone */
0402     void updateEffectZone(const QPoint p, bool withUndo);
0403     /** @brief The effect stask is about to be deleted, disconnect everything */
0404     void disconnectEffectStack();
0405     /** @brief Add a time remap effect to clip and show keyframes dialog */
0406     void remapClip(int cid);
0407     /** @brief A monitor property changed, check if we need to reset */
0408     void monitorProfileUpdated();
0409     /** @brief Color theme changed, process refresh */
0410     void updatePalette();
0411     /** @brief Emitted when a clip is resized (to handle clip monitor inserted zones) */
0412     void clipInstanceResized(const QString &binId);
0413     /** @brief Contains the project audio levels */
0414     void audioLevelsAvailable(const QVector<double>& levels);
0415     /** @brief A frame was displayed in monitor, update audio mixer */
0416     void updateMixerLevels(int pos);
0417     /** @brief Audio recording was started or stopped*/
0418     void switchTimelineRecord(bool on);
0419     /** @brief Launch audio recording on track tid*/
0420     void recordAudio(int tid, bool record);
0421     /** @brief Inform widgets that the project profile (and possibly fps) changed */
0422     void updateProjectTimecode();
0423     /** @brief Visible guide categories changed, reload snaps in timeline */
0424     void refreshActiveGuides();
0425     /** @brief The default marker category was changed, update guides list button */
0426     void updateDefaultMarkerCategory();
0427     /** @brief The default speech engine was changed */
0428     void speechEngineChanged();
0429 };