CuteLogger
Fast and simple logging solution for Qt based applications
mltcontroller.h
1 /*
2  * Copyright (c) 2011-2021 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);
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 isClip() const;
96  bool isSeekableClip();
97  bool isPlaylist() const;
98  bool isMultitrack() const;
99  bool isImageProducer(Service* service) const;
100  bool isFileProducer(Service* service) const;
101  void rewind(bool forceChangeDirection);
102  void fastForward(bool forceChangeDirection);
103  void previous(int currentPosition);
104  void next(int currentPosition);
105  void setIn(int);
106  void setOut(int);
107  void restart(const QString& xml = "");
108  void resetURL();
109  QImage image(Frame *frame, int width, int height);
110  QImage image(Mlt::Producer& producer, int frameNumber, int width, int height);
111  void updateAvformatCaching(int trackCount);
112  bool isAudioFilter(const QString& name);
113  int realTime() const;
114  void setImageDurationFromDefault(Service* service) const;
115  void setDurationFromDefault(Producer* service) const;
116  void lockCreationTime(Producer* producer) const;
117  Producer* setupNewProducer(Producer* newProducer) const;
118  QUuid uuid(Mlt::Properties &properties) const;
119  void setUuid(Mlt::Properties &properties, QUuid uid) const;
120  QUuid ensureHasUuid(Mlt::Properties& properties) const;
121  static void copyFilters(Mlt::Producer& fromProducer, Mlt::Producer& toProducer, bool fromClipboard = false, bool includeDisabled = true);
122  void copyFilters(Mlt::Producer* producer = nullptr);
123  void pasteFilters(Mlt::Producer* producer = nullptr, Mlt::Producer* fromProducer = nullptr);
124  static void adjustFilters(Mlt::Producer& producer, int startIndex = 0);
125  static void adjustFilter(Mlt::Filter* filter, int in, int out, int inDelta, int outDelta);
126  static void adjustClipFilters(Mlt::Producer& producer, int in, int out, int inDelta, int outDelta);
127  bool hasFiltersOnClipboard() const {
128  return m_filtersClipboard->is_valid() && m_filtersClipboard->filter_count() > 0;
129  }
130  QString filtersClipboardXML() {
131  return XML(m_filtersClipboard.get());
132  }
133 
134  int audioChannels() const {
135  return m_audioChannels;
136  }
137  Mlt::Repository* repository() const {
138  return m_repo;
139  }
140  Mlt::Profile& profile() {
141  return m_profile;
142  }
143  Mlt::Profile& previewProfile() {
144  return m_previewProfile;
145  }
146  Mlt::Producer* producer() const {
147  return m_producer.data();
148  }
149  Mlt::Consumer* consumer() const {
150  return m_consumer.data();
151  }
152  const QString& URL() const {
153  return m_url;
154  }
155  const TransportControllable* transportControl() const {
156  return &m_transportControl;
157  }
158  Mlt::Producer* savedProducer() const {
159  return m_savedProducer.data();
160  }
161  void setSavedProducer(Mlt::Producer* producer);
162  static Mlt::Filter* getFilter(const QString& name, Mlt::Service* service);
163  QString projectFolder() const { return m_projectFolder; }
164  void setProjectFolder(const QString& folderName);
165  QChar decimalPoint();
166  static void resetLocale();
167  static int filterIn(Mlt::Playlist&playlist, int clipIndex);
168  static int filterOut(Mlt::Playlist&playlist, int clipIndex);
169  void setPreviewScale(int scale);
170  void updatePreviewProfile();
171  static void purgeMemoryPool();
172  static bool fullRange(Mlt::Producer& producer);
173  static bool isMltXml(const QString& s) {
174  return s.contains("<mlt ");
175  }
176  bool blockRefresh(bool block);
177 
178  class RefreshBlocker
179  {
180  public:
181  RefreshBlocker()
182  {
183  singleton().blockRefresh(true);
184  }
185  ~RefreshBlocker()
186  {
187  singleton().blockRefresh(false);
188  }
189  };
190 
191 protected:
192  Mlt::Repository* m_repo;
193  QScopedPointer<Mlt::Producer> m_producer;
194  QScopedPointer<Mlt::FilteredConsumer> m_consumer;
195 
196 private:
197  Mlt::Profile m_profile;
198  Mlt::Profile m_previewProfile;
199  int m_audioChannels{2};
200  QScopedPointer<Mlt::Filter> m_jackFilter;
201  QString m_url;
202  double m_volume{1.0};
203  TransportControl m_transportControl;
204  QScopedPointer<Mlt::Producer> m_savedProducer;
205  QScopedPointer<Mlt::Producer> m_filtersClipboard;
206  unsigned m_skipJackEvents{0};
207  QString m_projectFolder;
208  QMutex m_saveXmlMutex;
209  bool m_blockRefresh;
210 
211  static void on_jack_started(mlt_properties owner, void* object, mlt_event_data data);
212  void onJackStarted(int position);
213  static void on_jack_stopped(mlt_properties owner, void* object, mlt_event_data data);
214  void onJackStopped(int position);
215  void stopJack();
216  void initFiltersClipboard();
217 };
218 
219 } // namespace
220 
221 #define MLT Mlt::Controller::singleton()
222 
223 #endif // MLTCONTROLLER_H