CuteLogger
Fast and simple logging solution for Qt based applications
mltcontroller.h
1 /*
2  * Copyright (c) 2011-2024 Meltytech, LLC
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MLTCONTROLLER_H
19 #define MLTCONTROLLER_H
20 
21 #include "transportcontrol.h"
22 
23 #include <Mlt.h>
24 #include <QImage>
25 #include <QMutex>
26 #include <QScopedPointer>
27 #include <QString>
28 #include <QTemporaryFile>
29 #include <QUuid>
30 
31 #define MLT_LC_CATEGORY LC_ALL
32 #define MLT_LC_NAME "LC_ALL"
33 
34 #if LIBMLT_VERSION_INT >= ((7 << 16) + (19 << 8))
35 #define kAudioIndexProperty "astream"
36 #define kVideoIndexProperty "vstream"
37 #else
38 #define kAudioIndexProperty "audio_index"
39 #define kVideoIndexProperty "video_index"
40 #endif
41 
42 namespace Mlt {
43 
44 const int kMaxImageDurationSecs = 3600 * 4;
45 extern const QString XmlMimeType;
46 
47 class TransportControl : public TransportControllable
48 {
49  Q_OBJECT
50 public slots:
51  void play(double speed = 1.0) override;
52  void pause(int position = -1) override;
53  void stop() override;
54  void seek(int position) override;
55  void rewind(bool forceChangeDirection) override;
56  void fastForward(bool forceChangeDirection) override;
57  void previous(int currentPosition) override;
58  void next(int currentPosition) override;
59  void setIn(int) override;
60  void setOut(int) override;
61 };
62 
63 class Controller
64 {
65 protected:
66  Controller();
67  virtual int reconfigure(bool isMulti) = 0;
68 
69 public:
70  enum {
71  FILTER_INDEX_ALL = -1,
72  FILTER_INDEX_ENABLED = -2,
73  };
74 
75  static Controller &singleton(QObject *parent = nullptr);
76  virtual ~Controller();
77  static void destroy();
78 
79  virtual QObject *videoWidget() = 0;
80  virtual int setProducer(Mlt::Producer *, bool isMulti = false);
81  virtual int open(const QString &url, const QString &urlToSave, bool skipConvert = false);
82  bool openXML(const QString &filename);
83  virtual void close();
84  virtual int displayWidth() const = 0;
85  virtual int displayHeight() const = 0;
86 
87  void closeConsumer();
88  virtual void play(double speed = 1.0);
89  bool isPaused() const;
90  virtual void pause(int position = -1);
91  void stop();
92  bool enableJack(bool enable = true);
93  void setVolume(double volume, bool muteOnPause = true);
94  double volume() const;
95  void onWindowResize();
96  virtual void seek(int position);
97  virtual void refreshConsumer(bool scrubAudio = false);
98  bool saveXML(const QString &filename,
99  Service *service = nullptr,
100  bool withRelativePaths = true,
101  QTemporaryFile *tempFile = nullptr,
102  bool proxy = false,
103  QString projectNote = QString());
104  QString XML(Service *service = nullptr, bool withProfile = false, bool withMetadata = true);
105  int consumerChanged();
106  void setProfile(const QString &profile_name);
107  void setAudioChannels(int audioChannels);
108  QString resource() const;
109  bool isSeekable(Mlt::Producer *p = nullptr) const;
110  bool isLiveProducer(Mlt::Producer *p = nullptr) const;
111  bool isClip() const;
112  bool isClosedClip(Producer *producer = nullptr) const;
113  bool isSeekableClip();
114  bool isPlaylist() const;
115  bool isMultitrack() const;
116  bool isImageProducer(Service *service) const;
117  bool isFileProducer(Service *service) const;
118  void rewind(bool forceChangeDirection);
119  void fastForward(bool forceChangeDirection);
120  void previous(int currentPosition);
121  void next(int currentPosition);
122  void setIn(int);
123  void setOut(int);
124  void fixLengthProperties(Service &service);
125  void restart(const QString &xml = "");
126  void resetURL();
127  QImage image(Frame *frame, int width, int height);
128  QImage image(Mlt::Producer &producer, int frameNumber, int width, int height);
129  void updateAvformatCaching(int trackCount);
130  bool isAudioFilter(const QString &name);
131  int realTime() const;
132  void setImageDurationFromDefault(Service *service) const;
133  void setDurationFromDefault(Producer *service) const;
134  void lockCreationTime(Producer *producer) const;
135  Producer *setupNewProducer(Producer *newProducer) const;
136  QUuid uuid(Mlt::Properties &properties) const;
137  void setUuid(Mlt::Properties &properties, QUuid uid) const;
138  QUuid ensureHasUuid(Mlt::Properties &properties) const;
139  static void copyFilters(Mlt::Producer &fromProducer,
140  Mlt::Producer &toProducer,
141  bool fromClipboard = false,
142  int filterIndex = FILTER_INDEX_ENABLED);
143  void copyFilters(Mlt::Producer *producer = nullptr, int filterIndex = FILTER_INDEX_ENABLED);
144  void pasteFilters(Mlt::Producer *producer = nullptr, Mlt::Producer *fromProducer = nullptr);
145  static void adjustFilters(Mlt::Producer &producer, int startIndex = 0);
146  static void adjustFilter(
147  Mlt::Filter *filter, int in, int out, int inDelta, int outDelta, int keyframeDelta);
148  static void adjustClipFilters(
149  Mlt::Producer &producer, int in, int out, int inDelta, int outDelta, int keyframeDelta);
150  bool hasFiltersOnClipboard() const
151  {
152  return m_filtersClipboard->is_valid() && m_filtersClipboard->filter_count() > 0;
153  }
154  QString filtersClipboardXML() { return XML(m_filtersClipboard.get()); }
155 
156  int audioChannels() const { return m_audioChannels; }
157  Mlt::Repository *repository() const { return m_repo; }
158  Mlt::Profile &profile() { return m_profile; }
159  Mlt::Profile &previewProfile() { return m_previewProfile; }
160  Mlt::Producer *producer() const { return m_producer.data(); }
161  Mlt::Consumer *consumer() const { return m_consumer.data(); }
162  const QString &URL() const { return m_url; }
163  const TransportControllable *transportControl() const { return &m_transportControl; }
164  Mlt::Producer *savedProducer() const { return m_savedProducer.data(); }
165  void setSavedProducer(Mlt::Producer *producer);
166  static Mlt::Filter *getFilter(const QString &name, Mlt::Service *service);
167  QString projectFolder() const { return m_projectFolder; }
168  void setProjectFolder(const QString &folderName);
169  QChar decimalPoint();
170  static void resetLocale();
171  static int filterIn(Mlt::Playlist &playlist, int clipIndex);
172  static int filterOut(Mlt::Playlist &playlist, int clipIndex);
173  void setPreviewScale(int scale);
174  void updatePreviewProfile();
175  static void purgeMemoryPool();
176  static bool fullRange(Mlt::Producer &producer);
177  static bool isMltXml(const QString &s) { return s.contains("<mlt "); }
178  static bool isTrackProducer(Mlt::Producer &producer);
179  static int checkFile(const QString &path);
180  bool blockRefresh(bool block);
181 
182  class RefreshBlocker
183  {
184  public:
185  RefreshBlocker() { singleton().blockRefresh(true); }
186  ~RefreshBlocker() { singleton().blockRefresh(false); }
187  };
188 
189 protected:
190  Mlt::Repository *m_repo;
191  QScopedPointer<Mlt::Producer> m_producer;
192  QScopedPointer<Mlt::FilteredConsumer> m_consumer;
193 
194 private:
195  Mlt::Profile m_profile;
196  Mlt::Profile m_previewProfile;
197  int m_audioChannels{2};
198  QScopedPointer<Mlt::Filter> m_jackFilter;
199  QString m_url;
200  double m_volume{1.0};
201  TransportControl m_transportControl;
202  QScopedPointer<Mlt::Producer> m_savedProducer;
203  QScopedPointer<Mlt::Producer> m_filtersClipboard;
204  unsigned m_skipJackEvents{0};
205  QString m_projectFolder;
206  QMutex m_saveXmlMutex;
207  bool m_blockRefresh;
208 
209  static void on_jack_started(mlt_properties owner, void *object, mlt_event_data data);
210  void onJackStarted(int position);
211  static void on_jack_stopped(mlt_properties owner, void *object, mlt_event_data data);
212  void onJackStopped(int position);
213  void stopJack();
214  void initFiltersClipboard();
215 };
216 
217 } // namespace Mlt
218 
219 #define MLT Mlt::Controller::singleton()
220 
221 #endif // MLTCONTROLLER_H