File indexing completed on 2022-11-29 19:13:37

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(int itemType, int itemId);
0191     int getItemPosition(const ObjectId &id);
0192     int getItemIn(const ObjectId &id);
0193     int getItemTrack(const ObjectId &id);
0194     int getItemDuration(const ObjectId &id);
0195     QSize getItemFrameSize(const ObjectId &id);
0196     /** @brief Returns the capabilities of a clip: AudioOnly, VideoOnly or Disabled if both are allowed */
0197     PlaylistState::ClipState getItemState(const ObjectId &id);
0198     /** @brief Get a list of video track names with indexes */
0199     QMap<int, QString> getTrackNames(bool videoOnly);
0200     /** @brief Returns the composition A track (MLT index / Track id) */
0201     QPair<int, int> getCompositionATrack(int cid) const;
0202     void setCompositionATrack(int cid, int aTrack);
0203     /** @brief Return true if composition's a_track is automatic (no forced track)
0204      */
0205     bool compositionAutoTrack(int cid) const;
0206     std::shared_ptr<DocUndoStack> undoStack();
0207     double getClipSpeed(int id) const;
0208     /** @brief Mark an item as invalid for timeline preview */
0209     void invalidateItem(ObjectId itemId);
0210     void invalidateRange(QPair<int, int>range);
0211     void prepareShutdown();
0212     /** the keyframe model changed (effect added, deleted, active effect changed), inform timeline */
0213     void updateItemKeyframes(ObjectId id);
0214     /** A fade for clip id changed, update timeline */
0215     void updateItemModel(ObjectId id, const QString &service);
0216     /** Show / hide keyframes for a timeline clip */
0217     void showClipKeyframes(ObjectId id, bool enable);
0218     Mlt::Profile *thumbProfile();
0219     /** @brief Returns the current project duration */
0220     int projectDuration() const;
0221     /** @brief Returns true if current project has some rendered timeline preview  */
0222     bool hasTimelinePreview() const;
0223     /** @brief Returns monitor position  */
0224     int getMonitorPosition(Kdenlive::MonitorId id = Kdenlive::ProjectMonitor) const;
0225     /** @brief Handles audio and video capture **/
0226     void startMediaCapture(int tid, bool, bool);
0227     void stopMediaCapture(int tid, bool, bool);
0228     QStringList getAudioCaptureDevices();
0229     int getMediaCaptureState();
0230     bool isMediaMonitoring() const;
0231     bool isMediaCapturing() const;
0232     MediaCapture *getAudioDevice();
0233     /** @brief Returns Project Folder name for capture output location */
0234     QString getProjectFolderName(bool folderForAudio = false);
0235     /** @brief Returns a timeline clip's bin id */
0236     QString getTimelineClipBinId(int cid);
0237     /** @brief Returns all track ids in timeline */
0238     std::unordered_set<QString> getAllTimelineTracksId();
0239     /** @brief Returns a frame duration from a timecode */
0240     int getDurationFromString(const QString &time);
0241     /** @brief An error occurred within a filter, inform user */
0242     void processInvalidFilter(const QString &service, const QString &id, const QString &message);
0243     /** @brief Update current project's tags */
0244     void updateProjectTags(int previousCount, const QMap <int, QStringList> &tags);
0245     /** @brief Returns the project profile */
0246     Mlt::Profile *getProjectProfile();
0247     /** @brief Returns the consumer profile, that will be scaled
0248      *  according to preview settings. Should only be used on the consumer */
0249     Mlt::Profile &getMonitorProfile();
0250     /** @brief Returns a copy of current timeline's master playlist */
0251     std::unique_ptr<Mlt::Producer> getMasterProducerInstance();
0252     /** @brief Returns a copy of a track's playlist */
0253     std::unique_ptr<Mlt::Producer> getTrackProducerInstance(int tid);
0254     /** @brief Returns the undo stack index (position). */
0255     int undoIndex() const;
0256     /** @brief Enable / disable monitor multitrack view. Returns false if multitrack was not previously enabled */
0257     bool enableMultiTrack(bool enable);
0258     /** @brief Returns number of audio channels for this project. */
0259     int audioChannels();
0260     /** @brief Add guides in the project. */
0261     void addGuides(const QList <int> &guides);
0262     /** @brief Temporarily un/plug a list of clips in timeline. */
0263     void temporaryUnplug(const QList<int> &clipIds, bool hide);
0264     /** @brief Returns the current doc's subtitle model. */
0265     std::shared_ptr<SubtitleModel> getSubtitleModel(bool enforce = false);
0266     /** @brief Transcode a video file. */
0267     void transcodeFile(const QString &url);
0268     /** @brief Display key binding info in statusbar. */
0269     void setWidgetKeyBinding(const QString &mess = QString());
0270     KSharedDataCache audioThumbCache;
0271     /* @brief The thread job pool for clip jobs, allowing to set a max number of concurrent jobs */
0272     TaskManager taskManager;
0273     /** @brief The number of clip load jobs changed */
0274     void loadingClips(int);
0275     /** @brief Resize current mix item */
0276     void resizeMix(int cid, int duration, MixAlignment align, int leftFrames = -1);
0277     /** @brief Get Mix cut pos (the duration of the mix on the right clip) */
0278     int getMixCutPos(int cid) const;
0279     /** @brief Get alignment info for a mix item */
0280     MixAlignment getMixAlign(int cid) const;
0281     /** @brief Closing current document, do some cleanup */
0282     void cleanup();
0283 #if KNEWSTUFF_VERSION < QT_VERSION_CHECK(5, 98, 0)
0284     /** @brief Instantiates a "Get Hot New Stuff" dialog.
0285      * @param configFile configuration file for KNewStuff
0286      * @return number of installed items */
0287     int getNewStuff(const QString &configFile);
0288 #endif
0289     /** @brief Get the frame size of the clip above a composition */
0290     const QSize getCompositionSizeOnTrack(const ObjectId &id);
0291     void loadTimelinePreview(const QString &chunks, const QString &dirty, int enablePreview, Mlt::Playlist &playlist);
0292     /** @brief Returns true if the audio mixer widget is visible */
0293     bool audioMixerVisible{false};
0294     QString packageType() { return m_packageType; };
0295     /** @brief Start / stop audio capture */
0296     void switchCapture();
0297     /** @brief A list of markers type categories {marker type, {color, category name}} */
0298     struct MarkerCategory
0299     {
0300         QColor color;
0301         QString displayName;
0302     };
0303     QMap<int, MarkerCategory> markerTypes;
0304 
0305 private:
0306     explicit Core(const QString &packageType);
0307     static std::unique_ptr<Core> m_self;
0308 
0309     /** @brief Makes sure Qt's locale and system locale settings match. */
0310     void initLocale();
0311 
0312     MainWindow *m_mainWindow{nullptr};
0313     ProjectManager *m_projectManager{nullptr};
0314     MonitorManager *m_monitorManager{nullptr};
0315     std::shared_ptr<ProjectItemModel> m_projectItemModel;
0316     LibraryWidget *m_library{nullptr};
0317     SubtitleEdit *m_subtitleWidget{nullptr};
0318     TextBasedEdit *m_textEditWidget{nullptr};
0319     GuidesList *m_guidesList{nullptr};
0320     TimeRemap *m_timeRemapWidget{nullptr};
0321     MixerManager *m_mixerWidget{nullptr};
0322 
0323     /** @brief Current project's profile path */
0324     QString m_currentProfile;
0325 
0326     QString m_profile;
0327     QString m_packageType;
0328     Timecode m_timecode;
0329     std::unique_ptr<Mlt::Profile> m_thumbProfile;
0330     /** @brief Mlt profile used in the consumer 's monitors */
0331     Mlt::Profile m_monitorProfile;
0332     /** @brief Mlt profile used to build the project's clips */
0333     std::unique_ptr<Mlt::Profile> m_projectProfile;
0334     bool m_guiConstructed = false;
0335     /** @brief Check that the profile is valid (width is a multiple of 8 and height a multiple of 2 */
0336     void checkProfileValidity();
0337     std::unique_ptr<MediaCapture> m_capture;
0338     QUrl m_mediaCaptureFile;
0339     QMutex m_thumbProfileMutex;
0340 
0341 public slots:
0342     /** @brief Trigger (launch) an action by its actionCollection name */
0343     void triggerAction(const QString &name);
0344     /** @brief Get an action's descriptive text by its actionCollection name */
0345     const QString actionText(const QString &name);
0346     /** @brief display a user info/warning message in the project bin */
0347     void displayBinMessage(const QString &text, int type, const QList<QAction *> &actions = QList<QAction *>(), bool showClose = false, BinMessage::BinCategory messageCategory = BinMessage::BinCategory::NoMessage);
0348     void displayBinLogMessage(const QString &text, int type, const QString logInfo);
0349     /** @brief Create small thumbnails for luma used in compositions */
0350     void buildLumaThumbs(const QStringList &values);
0351     /** @brief Try to find a display name for the given filename.
0352      *  This is espacally helpfull for mlt's dynamically created luma files without thumb (luma01.pgm, luma02.pgm,...),
0353      *  but also for others as it makes the visible name translatable.
0354      *  @return The name that fits to the filename or if none is found the filename it self
0355      */
0356     const QString nameForLumaFile(const QString &filename);
0357     /** @brief Set current project modified. */
0358     void setDocumentModified();
0359     /** @brief Show currently selected effect zone in timeline ruler. */
0360     void showEffectZone(ObjectId id, QPair <int, int>inOut, bool checked);
0361     void updateMasterZones();
0362     /** @brief Open the proxies test dialog. */
0363     void testProxies();
0364     /** @brief Refresh the monitor profile when project profile changes. */
0365     void updateMonitorProfile();
0366     /** @brief Add a new Bin Widget. */
0367     void addBin(const QString &id = QString());
0368     /** @brief Transcode a bin clip video. */
0369     void transcodeFriendlyFile(const QString &binId, bool checkProfile);
0370     /** @brief Reset audio  monitoring volume and channels. */
0371     void resetAudioMonitoring();
0372     /** @brief Start audio recording (after countdown). */
0373     void startRecording();
0374     /** @brief Show or hide track head audio rec controls. */
0375     void monitorAudio(int tid, bool monitor);
0376 
0377 signals:
0378     void coreIsReady();
0379     void updateLibraryPath();
0380     //void updateMonitorProfile();
0381     /** @brief Call config dialog on a selected page / tab */
0382     void showConfigDialog(int, int);
0383     void finalizeRecording(const QString &captureFile);
0384     void autoScrollChanged();
0385     /** @brief Send a message to splash screen if still displayed */
0386     void loadingMessageUpdated(const QString &, int progress = 0, int max = -1);
0387     /** @brief Opening finished, close splash screen */
0388     void closeSplash();
0389     /** @brief Trigger an update of the speech models list */
0390     void voskModelUpdate(const QStringList models);
0391     /** @brief Update current effect zone */
0392     void updateEffectZone(const QPoint p, bool withUndo);
0393     /** @brief The effect stask is about to be deleted, disconnect everything */
0394     void disconnectEffectStack();
0395     /** @brief Add a time remap effect to clip and show keyframes dialog */
0396     void remapClip(int cid);
0397     /** @brief A monitor property changed, check if we need to reset */
0398     void monitorProfileUpdated();
0399     /** @brief Color theme changed, process refresh */
0400     void updatePalette();
0401     /** @brief Emitted when a clip is resized (to handle clip monitor inserted zones) */
0402     void clipInstanceResized(const QString &binId);
0403     /** @brief Contains the project audio levels */
0404     void audioLevelsAvailable(const QVector<double>& levels);
0405     /** @brief A frame was displayed in monitor, update audio mixer */
0406     void updateMixerLevels(int pos);
0407     /** @brief Audio recording was started or stopped*/
0408     void switchTimelineRecord(bool on);
0409     /** @brief Launch audio recording on track tid*/
0410     void recordAudio(int tid, bool record);
0411     /** @brief Inform widgets that the project profile (and possibly fps) changed */
0412     void updateProjectTimecode();
0413     /** @brief Visible guide categories changed, reload snaps in timeline */
0414     void refreshActiveGuides();
0415     /** @brief The default marker category was changed, update guides list button */
0416     void updateDefaultMarkerCategory();
0417 };