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