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 // forward declarations
32 class QQuickView;
33 
34 #define MLT_LC_CATEGORY LC_ALL
35 #define MLT_LC_NAME "LC_ALL"
36 
37 #if LIBMLT_VERSION_INT >= ((7 << 16) + (19 << 8))
38 #define kAudioIndexProperty "astream"
39 #define kVideoIndexProperty "vstream"
40 #else
41 #define kAudioIndexProperty "audio_index"
42 #define kVideoIndexProperty "video_index"
43 #endif
44 
45 namespace Mlt {
46 
47 const int kMaxImageDurationSecs = 3600 * 4;
48 extern const QString XmlMimeType;
49 
50 class TransportControl : public TransportControllable
51 {
52  Q_OBJECT
53 public slots:
54  void play(double speed = 1.0) override;
55  void pause(int position = -1) override;
56  void stop() override;
57  void seek(int position) override;
58  void rewind(bool forceChangeDirection) override;
59  void fastForward(bool forceChangeDirection) override;
60  void previous(int currentPosition) override;
61  void next(int currentPosition) override;
62  void setIn(int) override;
63  void setOut(int) override;
64 };
65 
66 class Controller
67 {
68 protected:
69  Controller();
70  virtual int reconfigure(bool isMulti) = 0;
71 
72 public:
73  enum {
74  FILTER_INDEX_ALL = -1,
75  FILTER_INDEX_ENABLED = -2,
76  };
77 
78  static Controller &singleton(QObject *parent = nullptr);
79  virtual ~Controller();
80  static void destroy();
81 
82  virtual QObject *videoWidget() = 0;
83  virtual int setProducer(Mlt::Producer *, bool isMulti = false);
84  virtual int open(const QString &url, const QString &urlToSave, bool skipConvert = false);
85  bool openXML(const QString &filename);
86  virtual void close();
87  virtual int displayWidth() const = 0;
88  virtual int displayHeight() const = 0;
89 
90  void closeConsumer();
91  virtual void play(double speed = 1.0);
92  bool isPaused() const;
93  virtual void pause(int position = -1);
94  void stop();
95  bool enableJack(bool enable = true);
96  void setVolume(double volume, bool muteOnPause = true);
97  double volume() const;
98  void onWindowResize();
99  virtual void seek(int position);
100  virtual void refreshConsumer(bool scrubAudio = false);
101  bool saveXML(const QString &filename,
102  Service *service = nullptr,
103  bool withRelativePaths = true,
104  QTemporaryFile *tempFile = nullptr,
105  bool proxy = false,
106  QString projectNote = QString());
107  QString XML(Service *service = nullptr, bool withProfile = false, bool withMetadata = true);
108  int consumerChanged();
109  void setProfile(const QString &profile_name);
110  void setAudioChannels(int audioChannels);
111  QString resource() const;
112  bool isSeekable(Mlt::Producer *p = nullptr) const;
113  bool isLiveProducer(Mlt::Producer *p = nullptr) const;
114  bool isClip() const;
115  bool isClosedClip(Producer *producer = nullptr) const;
116  bool isSeekableClip();
117  bool isPlaylist() const;
118  bool isMultitrack() const;
119  bool isImageProducer(Service *service) const;
120  bool isFileProducer(Service *service) const;
121  void rewind(bool forceChangeDirection);
122  void fastForward(bool forceChangeDirection);
123  void previous(int currentPosition);
124  void next(int currentPosition);
125  void setIn(int);
126  void setOut(int);
127  void fixLengthProperties(Service &service);
128  void restart(const QString &xml = "");
129  void resetURL();
130  QImage image(Frame *frame, int width, int height);
131  QImage image(Mlt::Producer &producer, int frameNumber, int width, int height);
132  void updateAvformatCaching(int trackCount);
133  bool isAudioFilter(const QString &name);
134  int realTime() const;
135  void setImageDurationFromDefault(Service *service) const;
136  void setDurationFromDefault(Producer *service) const;
137  void lockCreationTime(Producer *producer) const;
138  Producer *setupNewProducer(Producer *newProducer) const;
139  QUuid uuid(Mlt::Properties &properties) const;
140  void setUuid(Mlt::Properties &properties, QUuid uid) const;
141  QUuid ensureHasUuid(Mlt::Properties &properties) const;
142  static void copyFilters(Mlt::Producer &fromProducer,
143  Mlt::Producer &toProducer,
144  bool fromClipboard = false,
145  int filterIndex = FILTER_INDEX_ENABLED);
146  void copyFilters(Mlt::Producer *producer = nullptr, int filterIndex = FILTER_INDEX_ENABLED);
147  void pasteFilters(Mlt::Producer *producer = nullptr, Mlt::Producer *fromProducer = nullptr);
148  static void adjustFilters(Mlt::Producer &producer, int startIndex = 0);
149  static void adjustFilter(
150  Mlt::Filter *filter, int in, int out, int inDelta, int outDelta, int keyframeDelta);
151  static void adjustClipFilters(
152  Mlt::Producer &producer, int in, int out, int inDelta, int outDelta, int keyframeDelta);
153  bool hasFiltersOnClipboard() const
154  {
155  return m_filtersClipboard->is_valid() && m_filtersClipboard->filter_count() > 0;
156  }
157  QString filtersClipboardXML() { return XML(m_filtersClipboard.get()); }
158 
159  int audioChannels() const { return m_audioChannels; }
160  Mlt::Repository *repository() const { return m_repo; }
161  Mlt::Profile &profile() { return m_profile; }
162  Mlt::Profile &previewProfile() { return m_previewProfile; }
163  Mlt::Producer *producer() const { return m_producer.data(); }
164  Mlt::Consumer *consumer() const { return m_consumer.data(); }
165  const QString &URL() const { return m_url; }
166  const TransportControllable *transportControl() const { return &m_transportControl; }
167  Mlt::Producer *savedProducer() const { return m_savedProducer.data(); }
168  void setSavedProducer(Mlt::Producer *producer);
169  static Mlt::Filter *getFilter(const QString &name, Mlt::Service *service);
170  QString projectFolder() const { return m_projectFolder; }
171  void setProjectFolder(const QString &folderName);
172  QChar decimalPoint();
173  static void resetLocale();
174  static int filterIn(Mlt::Playlist &playlist, int clipIndex);
175  static int filterOut(Mlt::Playlist &playlist, int clipIndex);
176  void setPreviewScale(int scale);
177  void updatePreviewProfile();
178  static void purgeMemoryPool();
179  static bool fullRange(Mlt::Producer &producer);
180  static bool isMltXml(const QString &s) { return s.contains("<mlt "); }
181  static bool isTrackProducer(Mlt::Producer &producer);
182  static int checkFile(const QString &path);
183  bool blockRefresh(bool block);
184 
185  class RefreshBlocker
186  {
187  public:
188  RefreshBlocker() { singleton().blockRefresh(true); }
189  ~RefreshBlocker() { singleton().blockRefresh(false); }
190  };
191 
192 protected:
193  Mlt::Repository *m_repo;
194  QScopedPointer<Mlt::Producer> m_producer;
195  QScopedPointer<Mlt::FilteredConsumer> m_consumer;
196 
197 private:
198  Mlt::Profile m_profile;
199  Mlt::Profile m_previewProfile;
200  int m_audioChannels{2};
201  QScopedPointer<Mlt::Filter> m_jackFilter;
202  QString m_url;
203  double m_volume{1.0};
204  TransportControl m_transportControl;
205  QScopedPointer<Mlt::Producer> m_savedProducer;
206  QScopedPointer<Mlt::Producer> m_filtersClipboard;
207  unsigned m_skipJackEvents{0};
208  QString m_projectFolder;
209  QMutex m_saveXmlMutex;
210  bool m_blockRefresh;
211 
212  static void on_jack_started(mlt_properties owner, void *object, mlt_event_data data);
213  void onJackStarted(int position);
214  static void on_jack_stopped(mlt_properties owner, void *object, mlt_event_data data);
215  void onJackStopped(int position);
216  void stopJack();
217  void initFiltersClipboard();
218 };
219 
220 } // namespace Mlt
221 
222 #define MLT Mlt::Controller::singleton()
223 
224 #endif // MLTCONTROLLER_H