File indexing completed on 2022-10-04 15:37:18

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