window-remux.hpp 4.6 KB
Newer Older
P
Palana 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************************************
    Copyright (C) 2014 by Ruwen Hahn <palana@stunned.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#pragma once

20 21
#include <QFileInfo>
#include <QMutex>
P
Palana 已提交
22 23
#include <QPointer>
#include <QThread>
24
#include <QStyledItemDelegate>
P
Palana 已提交
25 26 27 28 29 30
#include <memory>
#include "ui_OBSRemux.h"

#include <media-io/media-remux.h>
#include <util/threading.h>

31
class RemuxQueueModel;
P
Palana 已提交
32 33
class RemuxWorker;

34 35 36 37 38 39 40 41 42 43 44 45
enum RemuxEntryState
{
	Empty,
	Ready,
	Pending,
	InProgress,
	Complete,
	InvalidPath,
	Error
};
Q_DECLARE_METATYPE(RemuxEntryState);

P
Palana 已提交
46 47 48
class OBSRemux : public QDialog {
	Q_OBJECT

49
	QPointer<RemuxQueueModel> queueModel;
P
Palana 已提交
50 51 52 53 54 55 56 57 58 59
	QThread remuxer;
	QPointer<RemuxWorker> worker;

	std::unique_ptr<Ui::OBSRemux> ui;

	const char *recPath;

	virtual void closeEvent(QCloseEvent *event) override;
	virtual void reject() override;

C
cg2121 已提交
60 61 62
	bool autoRemux;
	QString autoRemuxFile;

P
Palana 已提交
63
public:
C
cg2121 已提交
64 65
	explicit OBSRemux(const char *recPath, QWidget *parent = nullptr,
			bool autoRemux = false);
P
Palana 已提交
66 67 68 69
	virtual ~OBSRemux() override;

	using job_t = std::shared_ptr<struct media_remux_job>;

C
cg2121 已提交
70 71
	void AutoRemux(QString inFile, QString outFile);

72 73 74 75 76 77
protected:
	void dropEvent(QDropEvent *ev);
	void dragEnterEvent(QDragEnterEvent *ev);

	void remuxNextEntry();

P
Palana 已提交
78
private slots:
79
	void rowCountChanged(const QModelIndex &parent, int first, int last);
P
Palana 已提交
80 81 82 83

public slots:
	void updateProgress(float percent);
	void remuxFinished(bool success);
84 85 86 87
	void beginRemux();
	bool stopRemux();
	void clearFinished();
	void clearAll();
P
Palana 已提交
88 89

signals:
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	void remux(const QString &source, const QString &target);
};

class RemuxQueueModel : public QAbstractTableModel {
	Q_OBJECT

	friend class OBSRemux;

public:
	RemuxQueueModel(QObject *parent = 0)
		: QAbstractTableModel(parent)
		, isProcessing(false) {}

	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	int columnCount(const QModelIndex &parent = QModelIndex()) const;
	QVariant data(const QModelIndex &index, int role) const;
	QVariant headerData(int section, Qt::Orientation orientation,
			int role = Qt::DisplayRole) const;
	Qt::ItemFlags flags(const QModelIndex &index) const;
	bool setData(const QModelIndex &index, const QVariant &value,
			int role);

	QFileInfoList checkForOverwrites() const;
	bool checkForErrors() const;
	void beginProcessing();
	void endProcessing();
	bool beginNextEntry(QString &inputPath, QString &outputPath);
	void finishEntry(bool success);
	bool canClearFinished() const;
	void clearFinished();
	void clearAll();

C
Clayton Groeneveld 已提交
122 123
	bool autoRemux = false;

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
private:
	struct RemuxQueueEntry
	{
		RemuxEntryState state;

		QString sourcePath;
		QString targetPath;
	};

	QList<RemuxQueueEntry> queue;
	bool isProcessing;

	static QVariant getIcon(RemuxEntryState state);

	void checkInputPath(int row);
P
Palana 已提交
139 140 141 142 143
};

class RemuxWorker : public QObject {
	Q_OBJECT

144 145 146
	QMutex updateMutex;

	bool isWorking;
P
Palana 已提交
147 148 149 150

	float lastProgress;
	void UpdateProgress(float percent);

151 152 153
	explicit RemuxWorker()
		: isWorking(false) { }
	virtual ~RemuxWorker() {};
P
Palana 已提交
154 155

private slots:
156
	void remux(const QString &source, const QString &target);
P
Palana 已提交
157 158 159 160 161 162 163

signals:
	void updateProgress(float percent);
	void remuxFinished(bool success);

	friend class OBSRemux;
};
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

class RemuxEntryPathItemDelegate : public QStyledItemDelegate {
	Q_OBJECT

public:
	RemuxEntryPathItemDelegate(bool isOutput, const QString &defaultPath);

	virtual QWidget *createEditor(QWidget *parent,
		const QStyleOptionViewItem & /* option */,
		const QModelIndex &index) const override;

	virtual void setEditorData(QWidget *editor, const QModelIndex &index)
		const override;
	virtual void setModelData(QWidget *editor,
		QAbstractItemModel *model,
		const QModelIndex &index) const override;
	virtual void paint(QPainter *painter,
		const QStyleOptionViewItem &option,
		const QModelIndex &index) const override;

private:
	bool isOutput;
	QString defaultPath;
	const char *PATH_LIST_PROP = "pathList";

	void handleBrowse(QWidget *container);
	void handleClear(QWidget *container);

192 193
private slots:
	void updateText();
194
};