RemuxWorker.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /******************************************************************************
  2. Copyright (C) 2014 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <QFileInfo>
  16. #include <QMutex>
  17. #include <QPointer>
  18. #include <QThread>
  19. #include <QStyledItemDelegate>
  20. #include <memory>
  21. #include "ui_OBSRemux.h"
  22. #include <media-io/media-remux.h>
  23. #include <util/threading.h>
  24. class RemuxQueueModel;
  25. class RemuxWorker;
  26. enum RemuxEntryState { Empty, Ready, Pending, InProgress, Complete, InvalidPath, Error };
  27. Q_DECLARE_METATYPE(RemuxEntryState);
  28. class OBSRemux : public QDialog {
  29. Q_OBJECT
  30. QPointer<RemuxQueueModel> queueModel;
  31. QThread remuxer;
  32. QPointer<RemuxWorker> worker;
  33. std::unique_ptr<Ui::OBSRemux> ui;
  34. const char *recPath;
  35. virtual void closeEvent(QCloseEvent *event) override;
  36. virtual void reject() override;
  37. bool autoRemux;
  38. QString autoRemuxFile;
  39. public:
  40. explicit OBSRemux(const char *recPath, QWidget *parent = nullptr, bool autoRemux = false);
  41. virtual ~OBSRemux() override;
  42. using job_t = std::shared_ptr<struct media_remux_job>;
  43. void AutoRemux(QString inFile, QString outFile);
  44. protected:
  45. virtual void dropEvent(QDropEvent *ev) override;
  46. virtual void dragEnterEvent(QDragEnterEvent *ev) override;
  47. void remuxNextEntry();
  48. private slots:
  49. void rowCountChanged(const QModelIndex &parent, int first, int last);
  50. public slots:
  51. void updateProgress(float percent);
  52. void remuxFinished(bool success);
  53. void beginRemux();
  54. bool stopRemux();
  55. void clearFinished();
  56. void clearAll();
  57. signals:
  58. void remux(const QString &source, const QString &target);
  59. };
  60. class RemuxQueueModel : public QAbstractTableModel {
  61. Q_OBJECT
  62. friend class OBSRemux;
  63. public:
  64. RemuxQueueModel(QObject *parent = 0) : QAbstractTableModel(parent), isProcessing(false) {}
  65. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  66. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  67. QVariant data(const QModelIndex &index, int role) const;
  68. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  69. Qt::ItemFlags flags(const QModelIndex &index) const;
  70. bool setData(const QModelIndex &index, const QVariant &value, int role);
  71. QFileInfoList checkForOverwrites() const;
  72. bool checkForErrors() const;
  73. void beginProcessing();
  74. void endProcessing();
  75. bool beginNextEntry(QString &inputPath, QString &outputPath);
  76. void finishEntry(bool success);
  77. bool canClearFinished() const;
  78. void clearFinished();
  79. void clearAll();
  80. bool autoRemux = false;
  81. private:
  82. struct RemuxQueueEntry {
  83. RemuxEntryState state;
  84. QString sourcePath;
  85. QString targetPath;
  86. };
  87. QList<RemuxQueueEntry> queue;
  88. bool isProcessing;
  89. static QVariant getIcon(RemuxEntryState state);
  90. void checkInputPath(int row);
  91. };
  92. class RemuxWorker : public QObject {
  93. Q_OBJECT
  94. QMutex updateMutex;
  95. bool isWorking;
  96. float lastProgress;
  97. void UpdateProgress(float percent);
  98. explicit RemuxWorker() : isWorking(false) {}
  99. virtual ~RemuxWorker(){};
  100. private slots:
  101. void remux(const QString &source, const QString &target);
  102. signals:
  103. void updateProgress(float percent);
  104. void remuxFinished(bool success);
  105. friend class OBSRemux;
  106. };
  107. class RemuxEntryPathItemDelegate : public QStyledItemDelegate {
  108. Q_OBJECT
  109. public:
  110. RemuxEntryPathItemDelegate(bool isOutput, const QString &defaultPath);
  111. virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  112. const QModelIndex &index) const override;
  113. virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  114. virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  115. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
  116. const QModelIndex &index) const override;
  117. private:
  118. bool isOutput;
  119. QString defaultPath;
  120. const char *PATH_LIST_PROP = "pathList";
  121. void handleBrowse(QWidget *container);
  122. void handleClear(QWidget *container);
  123. private slots:
  124. void updateText();
  125. };