File indexing completed on 2024-06-16 04:16:17

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Dmitrii Utkin <loentar@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-only
0005  */
0006 
0007 #ifndef RECORDER_WRITER_H
0008 #define RECORDER_WRITER_H
0009 
0010 #include "recorder_format.h"
0011 
0012 #include <QObject>
0013 #include <QMutex>
0014 
0015 struct RecorderExportSettings;
0016 class RecorderConfig;
0017 class KisCanvas2;
0018 class QDir;
0019 
0020 struct RecorderWriterSettings
0021 {
0022     QString outputDirectory;
0023     RecorderFormat format{RecorderFormat::JPEG};
0024     int quality;
0025     int compression;
0026     int resolution;
0027     double captureInterval;
0028     bool recordIsolateLayerMode;
0029     bool realTimeCaptureMode;
0030 };
0031 
0032 // This class is used to provide mechanism ro sync the number of available
0033 // and used recording threads between the GUI and the RecorderWriterManager
0034 class ThreadCounter : public QObject
0035 {
0036     Q_OBJECT
0037 public:
0038     ThreadCounter() = default;
0039     ThreadCounter(const ThreadCounter&) = delete;
0040     ThreadCounter(ThreadCounter&&) = delete;
0041     ThreadCounter& operator=(const ThreadCounter&) = delete;
0042     ThreadCounter& operator=(ThreadCounter&&) = delete;
0043 
0044     bool set(int value);
0045     void setAndNotify(int value);
0046     unsigned int get() const;
0047 
0048     bool setUsed(int value);
0049     void setUsedAndNotify(int value);
0050     void incUsedAndNotify();
0051     void decUsedAndNotify();
0052     unsigned int getUsed() const;
0053 
0054 Q_SIGNALS:
0055     void notifyValueChange(bool valueWasIncreased);
0056     void notifyInUseChange(bool valueWasIncreased);
0057 
0058 private:
0059     bool setUsedImpl(int value);
0060 
0061 private:
0062     unsigned int threads;
0063     unsigned int inUse;
0064     QMutex inUseMutex;
0065 };
0066 
0067 class RecorderWriter : public QObject
0068 {
0069     Q_OBJECT
0070 public:
0071     RecorderWriter(
0072         unsigned int i,
0073         QPointer<KisCanvas2> c,
0074         const RecorderWriterSettings& s,
0075         const QDir& d);
0076     ~RecorderWriter();
0077 
0078     RecorderWriter() = delete;
0079     RecorderWriter(const RecorderWriter&) = delete;
0080     RecorderWriter(RecorderWriter&&) = delete;
0081     RecorderWriter& operator=(const RecorderWriter&) = delete;
0082     RecorderWriter& operator=(RecorderWriter&&) = delete;
0083 
0084 Q_SIGNALS:
0085     void capturingDone(int writerId, bool success);
0086 
0087 public Q_SLOTS:
0088     void onCaptureImage(int writerId, int index);
0089 
0090 private:
0091     class Private;
0092     Private *const d;
0093     unsigned int id;
0094 };
0095 
0096 class RecorderWriterManager : public QObject
0097 {
0098     Q_OBJECT
0099 public:
0100     explicit RecorderWriterManager(const RecorderExportSettings &es);
0101     ~RecorderWriterManager();
0102 
0103     RecorderWriterManager() = delete;
0104     RecorderWriterManager(const RecorderWriterManager&) = delete;
0105     RecorderWriterManager(RecorderWriterManager&&) = delete;
0106     RecorderWriterManager& operator=(const RecorderWriterManager&) = delete;
0107     RecorderWriterManager& operator=(RecorderWriterManager&&) = delete;
0108 
0109     // stops current recording
0110     void setCanvas(QPointer<KisCanvas2> canvas);
0111     // stops current recording
0112     void setup(const RecorderWriterSettings &settings);
0113 
0114     void start();
0115     bool stop();
0116 
0117     void setEnabled(bool enabled);
0118 
0119 Q_SIGNALS:
0120     void started();
0121     void stopped();
0122     void frameWriteFailed();
0123     void lowPerformanceWarning();
0124     void recorderStopWarning();
0125 
0126     void startCapturing(int writerId, int index);
0127 
0128 private Q_SLOTS:
0129     void onTimer();
0130     void onCapturingDone(int workerId, bool success);
0131     void onImageModified();
0132     void onToolChanged(const QString &toolId);
0133 
0134 public:
0135     ThreadCounter recorderThreads{};
0136 
0137 private:
0138     class Private;
0139     Private *const d;
0140     const RecorderExportSettings &exporterSettings;
0141 
0142 };
0143 
0144 #endif // RECORDER_WRITER_H