window-remux.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. #include "window-remux.hpp"
  15. #include "obs-app.hpp"
  16. #include <QCloseEvent>
  17. #include <QDirIterator>
  18. #include <QFileDialog>
  19. #include <QItemDelegate>
  20. #include <QLineEdit>
  21. #include <QMessageBox>
  22. #include <QMimeData>
  23. #include <QPainter>
  24. #include <QPushButton>
  25. #include <QStandardItemModel>
  26. #include <QStyledItemDelegate>
  27. #include <QToolButton>
  28. #include <QTimer>
  29. #include "qt-wrappers.hpp"
  30. #include <memory>
  31. #include <cmath>
  32. using namespace std;
  33. enum RemuxEntryColumn {
  34. State,
  35. InputPath,
  36. OutputPath,
  37. Count
  38. };
  39. enum RemuxEntryRole {
  40. EntryStateRole = Qt::UserRole,
  41. NewPathsToProcessRole
  42. };
  43. /**********************************************************
  44. Delegate - Presents cells in the grid.
  45. **********************************************************/
  46. class RemuxEntryPathItemDelegate : public QStyledItemDelegate {
  47. public:
  48. RemuxEntryPathItemDelegate(bool isOutput, const QString &defaultPath)
  49. : QStyledItemDelegate(),
  50. isOutput(isOutput),
  51. defaultPath(defaultPath)
  52. {
  53. }
  54. virtual QWidget *createEditor(QWidget *parent,
  55. const QStyleOptionViewItem & /* option */,
  56. const QModelIndex &index) const override
  57. {
  58. RemuxEntryState state = index.model()
  59. ->index(index.row(), RemuxEntryColumn::State)
  60. .data(RemuxEntryRole::EntryStateRole)
  61. .value<RemuxEntryState>();
  62. if (state == RemuxEntryState::Pending ||
  63. state == RemuxEntryState::InProgress) {
  64. // Never allow modification of rows that are
  65. // in progress.
  66. return Q_NULLPTR;
  67. } else if (isOutput && state != RemuxEntryState::Ready) {
  68. // Do not allow modification of output rows
  69. // that aren't associated with a valid input.
  70. return Q_NULLPTR;
  71. } else if (!isOutput && state == RemuxEntryState::Complete) {
  72. // Don't allow modification of rows that are
  73. // already complete.
  74. return Q_NULLPTR;
  75. } else {
  76. QSizePolicy buttonSizePolicy(
  77. QSizePolicy::Policy::Minimum,
  78. QSizePolicy::Policy::Expanding,
  79. QSizePolicy::ControlType::PushButton);
  80. QWidget *container = new QWidget(parent);
  81. auto browseCallback = [this, container]()
  82. {
  83. const_cast<RemuxEntryPathItemDelegate *>(this)
  84. ->handleBrowse(container);
  85. };
  86. auto clearCallback = [this, container]()
  87. {
  88. const_cast<RemuxEntryPathItemDelegate *>(this)
  89. ->handleClear(container);
  90. };
  91. QHBoxLayout *layout = new QHBoxLayout();
  92. layout->setMargin(0);
  93. layout->setSpacing(0);
  94. QLineEdit *text = new QLineEdit();
  95. text->setObjectName(QStringLiteral("text"));
  96. text->setSizePolicy(QSizePolicy(
  97. QSizePolicy::Policy::Expanding,
  98. QSizePolicy::Policy::Expanding,
  99. QSizePolicy::ControlType::LineEdit));
  100. layout->addWidget(text);
  101. QToolButton *browseButton = new QToolButton();
  102. browseButton->setText("...");
  103. browseButton->setSizePolicy(buttonSizePolicy);
  104. layout->addWidget(browseButton);
  105. container->connect(browseButton, &QToolButton::clicked,
  106. browseCallback);
  107. // The "clear" button is not shown in output cells
  108. // or the insertion point's input cell.
  109. if (!isOutput && state != RemuxEntryState::Empty) {
  110. QToolButton *clearButton = new QToolButton();
  111. clearButton->setText("X");
  112. clearButton->setSizePolicy(buttonSizePolicy);
  113. layout->addWidget(clearButton);
  114. container->connect(clearButton,
  115. &QToolButton::clicked,
  116. clearCallback);
  117. }
  118. container->setLayout(layout);
  119. container->setFocusProxy(text);
  120. return container;
  121. }
  122. }
  123. virtual void setEditorData(QWidget *editor, const QModelIndex &index)
  124. const override
  125. {
  126. QLineEdit *text = editor->findChild<QLineEdit *>();
  127. text->setText(index.data().toString());
  128. editor->setProperty(PATH_LIST_PROP, QVariant());
  129. }
  130. virtual void setModelData(QWidget *editor,
  131. QAbstractItemModel *model,
  132. const QModelIndex &index) const override
  133. {
  134. // We use the PATH_LIST_PROP property to pass a list of
  135. // path strings from the editor widget into the model's
  136. // NewPathsToProcessRole. This is only used when paths
  137. // are selected through the "browse" or "delete" buttons
  138. // in the editor. If the user enters new text in the
  139. // text box, we simply pass that text on to the model
  140. // as normal text data in the default role.
  141. QVariant pathListProp = editor->property(PATH_LIST_PROP);
  142. if (pathListProp.isValid()) {
  143. QStringList list = editor->property(PATH_LIST_PROP)
  144. .toStringList();
  145. if (isOutput) {
  146. if (list.size() > 0)
  147. model->setData(index, list);
  148. } else
  149. model->setData(index, list,
  150. RemuxEntryRole::NewPathsToProcessRole);
  151. } else {
  152. QLineEdit *lineEdit = editor->findChild<QLineEdit *>();
  153. model->setData(index, lineEdit->text());
  154. }
  155. }
  156. virtual void paint(QPainter *painter,
  157. const QStyleOptionViewItem &option,
  158. const QModelIndex &index) const override
  159. {
  160. RemuxEntryState state = index.model()
  161. ->index(index.row(), RemuxEntryColumn::State)
  162. .data(RemuxEntryRole::EntryStateRole)
  163. .value<RemuxEntryState>();
  164. QStyleOptionViewItem localOption = option;
  165. initStyleOption(&localOption, index);
  166. if (isOutput) {
  167. if (state != Ready) {
  168. QColor background = localOption.palette
  169. .color(QPalette::ColorGroup::Disabled,
  170. QPalette::ColorRole::Background);
  171. localOption.backgroundBrush = QBrush(background);
  172. }
  173. }
  174. QApplication::style()->drawControl(QStyle::CE_ItemViewItem,
  175. &localOption, painter);
  176. }
  177. private:
  178. bool isOutput;
  179. QString defaultPath;
  180. const char *PATH_LIST_PROP = "pathList";
  181. void handleBrowse(QWidget *container)
  182. {
  183. QString OutputPattern =
  184. "(*.mp4 *.flv *.mov *.mkv *.ts *.m3u8)";
  185. QString InputPattern =
  186. "(*.flv *.mov *.mkv *.ts *.m3u8)";
  187. QLineEdit *text = container->findChild<QLineEdit *>();
  188. QString currentPath = text->text();
  189. if (currentPath.isEmpty())
  190. currentPath = defaultPath;
  191. bool isSet = false;
  192. if (isOutput) {
  193. QString newPath = QFileDialog::getSaveFileName(
  194. container, QTStr("Remux.SelectTarget"),
  195. currentPath, OutputPattern);
  196. if (!newPath.isEmpty()) {
  197. container->setProperty(PATH_LIST_PROP,
  198. QStringList() << newPath);
  199. isSet = true;
  200. }
  201. } else {
  202. QStringList paths = QFileDialog::getOpenFileNames(
  203. container,
  204. QTStr("Remux.SelectRecording"),
  205. currentPath,
  206. QTStr("Remux.OBSRecording")
  207. + QString(" ") + InputPattern);
  208. if (!paths.empty()) {
  209. container->setProperty(PATH_LIST_PROP, paths);
  210. isSet = true;
  211. }
  212. }
  213. if (isSet)
  214. emit commitData(container);
  215. }
  216. void handleClear(QWidget *container)
  217. {
  218. // An empty string list will indicate that the entry is being
  219. // blanked and should be deleted.
  220. container->setProperty(PATH_LIST_PROP, QStringList());
  221. emit commitData(container);
  222. }
  223. };
  224. /**********************************************************
  225. Model - Manages the queue's data
  226. **********************************************************/
  227. int RemuxQueueModel::rowCount(const QModelIndex &) const
  228. {
  229. return queue.length() + (isProcessing ? 0 : 1);
  230. }
  231. int RemuxQueueModel::columnCount(const QModelIndex &) const
  232. {
  233. return RemuxEntryColumn::Count;
  234. }
  235. QVariant RemuxQueueModel::data(const QModelIndex &index, int role) const
  236. {
  237. QVariant result = QVariant();
  238. if (index.row() >= queue.length()) {
  239. return QVariant();
  240. } else if (role == Qt::DisplayRole) {
  241. switch (index.column()) {
  242. case RemuxEntryColumn::InputPath:
  243. result = queue[index.row()].sourcePath;
  244. break;
  245. case RemuxEntryColumn::OutputPath:
  246. result = queue[index.row()].targetPath;
  247. break;
  248. }
  249. } else if (role == Qt::DecorationRole &&
  250. index.column() == RemuxEntryColumn::State) {
  251. result = getIcon(queue[index.row()].state);
  252. } else if (role == RemuxEntryRole::EntryStateRole) {
  253. result = queue[index.row()].state;
  254. }
  255. return result;
  256. }
  257. QVariant RemuxQueueModel::headerData(int section, Qt::Orientation orientation,
  258. int role) const
  259. {
  260. QVariant result = QVariant();
  261. if (role == Qt::DisplayRole &&
  262. orientation == Qt::Orientation::Horizontal) {
  263. switch (section) {
  264. case RemuxEntryColumn::State:
  265. result = QString();
  266. break;
  267. case RemuxEntryColumn::InputPath:
  268. result = QTStr("Remux.SourceFile");
  269. break;
  270. case RemuxEntryColumn::OutputPath:
  271. result = QTStr("Remux.TargetFile");
  272. break;
  273. }
  274. }
  275. return result;
  276. }
  277. Qt::ItemFlags RemuxQueueModel::flags(const QModelIndex &index) const
  278. {
  279. Qt::ItemFlags flags = QAbstractTableModel::flags(index);
  280. if (index.column() == RemuxEntryColumn::InputPath) {
  281. flags |= Qt::ItemIsEditable;
  282. } else if (index.column() == RemuxEntryColumn::OutputPath &&
  283. index.row() != queue.length()) {
  284. flags |= Qt::ItemIsEditable;
  285. }
  286. return flags;
  287. }
  288. bool RemuxQueueModel::setData(const QModelIndex &index, const QVariant &value,
  289. int role)
  290. {
  291. bool success = false;
  292. if (role == RemuxEntryRole::NewPathsToProcessRole) {
  293. QStringList pathList = value.toStringList();
  294. if (pathList.size() == 0) {
  295. if (index.row() < queue.size()) {
  296. beginRemoveRows(QModelIndex(), index.row(),
  297. index.row());
  298. queue.removeAt(index.row());
  299. endRemoveRows();
  300. }
  301. } else {
  302. if (pathList.size() > 1 && index.row() < queue.length()) {
  303. queue[index.row()].sourcePath = pathList[0];
  304. checkInputPath(index.row());
  305. pathList.removeAt(0);
  306. success = true;
  307. }
  308. if (pathList.size() > 0) {
  309. int row = index.row();
  310. int lastRow = row + pathList.size() - 1;
  311. beginInsertRows(QModelIndex(), row, lastRow);
  312. for (QString path : pathList) {
  313. RemuxQueueEntry entry;
  314. entry.sourcePath = path;
  315. queue.insert(row, entry);
  316. row++;
  317. }
  318. endInsertRows();
  319. for (row = index.row(); row <= lastRow; row++) {
  320. checkInputPath(row);
  321. }
  322. success = true;
  323. }
  324. }
  325. } else if (index.row() == queue.length()) {
  326. QString path = value.toString();
  327. if (!path.isEmpty()) {
  328. RemuxQueueEntry entry;
  329. entry.sourcePath = path;
  330. beginInsertRows(QModelIndex(), queue.length() + 1,
  331. queue.length() + 1);
  332. queue.append(entry);
  333. endInsertRows();
  334. checkInputPath(index.row());
  335. success = true;
  336. }
  337. } else {
  338. QString path = value.toString();
  339. if (path.isEmpty()) {
  340. if (index.column() == RemuxEntryColumn::InputPath) {
  341. beginRemoveRows(QModelIndex(), index.row(),
  342. index.row());
  343. queue.removeAt(index.row());
  344. endRemoveRows();
  345. }
  346. } else {
  347. switch (index.column()) {
  348. case RemuxEntryColumn::InputPath:
  349. queue[index.row()].sourcePath = value.toString();
  350. checkInputPath(index.row());
  351. success = true;
  352. break;
  353. case RemuxEntryColumn::OutputPath:
  354. queue[index.row()].targetPath = value.toString();
  355. emit dataChanged(index, index);
  356. success = true;
  357. break;
  358. }
  359. }
  360. }
  361. return success;
  362. }
  363. QVariant RemuxQueueModel::getIcon(RemuxEntryState state)
  364. {
  365. QVariant icon;
  366. QStyle *style = QApplication::style();
  367. switch (state) {
  368. case RemuxEntryState::Complete:
  369. icon = style->standardIcon(
  370. QStyle::SP_DialogApplyButton);
  371. break;
  372. case RemuxEntryState::InProgress:
  373. icon = style->standardIcon(
  374. QStyle::SP_ArrowRight);
  375. break;
  376. case RemuxEntryState::Error:
  377. icon = style->standardIcon(
  378. QStyle::SP_DialogCancelButton);
  379. break;
  380. case RemuxEntryState::InvalidPath:
  381. icon = style->standardIcon(
  382. QStyle::SP_MessageBoxWarning);
  383. break;
  384. }
  385. return icon;
  386. }
  387. void RemuxQueueModel::checkInputPath(int row)
  388. {
  389. RemuxQueueEntry &entry = queue[row];
  390. if (entry.sourcePath.isEmpty()) {
  391. entry.state = RemuxEntryState::Empty;
  392. } else {
  393. QFileInfo fileInfo(entry.sourcePath);
  394. if (fileInfo.exists())
  395. entry.state = RemuxEntryState::Ready;
  396. else
  397. entry.state = RemuxEntryState::InvalidPath;
  398. if (entry.state == RemuxEntryState::Ready)
  399. entry.targetPath = fileInfo.path() + QDir::separator()
  400. + fileInfo.baseName() + ".mp4";
  401. }
  402. if (entry.state == RemuxEntryState::Ready && isProcessing)
  403. entry.state = RemuxEntryState::Pending;
  404. emit dataChanged(index(row, 0), index(row, RemuxEntryColumn::Count));
  405. }
  406. QFileInfoList RemuxQueueModel::checkForOverwrites() const
  407. {
  408. QFileInfoList list;
  409. for (const RemuxQueueEntry &entry : queue) {
  410. if (entry.state == RemuxEntryState::Ready) {
  411. QFileInfo fileInfo(entry.targetPath);
  412. if (fileInfo.exists()) {
  413. list.append(fileInfo);
  414. }
  415. }
  416. }
  417. return list;
  418. }
  419. bool RemuxQueueModel::checkForErrors() const
  420. {
  421. bool hasErrors = false;
  422. for (const RemuxQueueEntry &entry : queue) {
  423. if (entry.state == RemuxEntryState::Error) {
  424. hasErrors = true;
  425. break;
  426. }
  427. }
  428. return hasErrors;
  429. }
  430. void RemuxQueueModel::clearAll()
  431. {
  432. beginRemoveRows(QModelIndex(), 0, queue.size() - 1);
  433. queue.clear();
  434. endRemoveRows();
  435. }
  436. void RemuxQueueModel::clearFinished()
  437. {
  438. int index = 0;
  439. for (index = 0; index < queue.size(); index++) {
  440. const RemuxQueueEntry &entry = queue[index];
  441. if (entry.state == RemuxEntryState::Complete) {
  442. beginRemoveRows(QModelIndex(), index, index);
  443. queue.removeAt(index);
  444. endRemoveRows();
  445. index--;
  446. }
  447. }
  448. }
  449. bool RemuxQueueModel::canClearFinished() const
  450. {
  451. bool canClearFinished = false;
  452. for (const RemuxQueueEntry &entry : queue)
  453. if (entry.state == RemuxEntryState::Complete) {
  454. canClearFinished = true;
  455. break;
  456. }
  457. return canClearFinished;
  458. }
  459. void RemuxQueueModel::beginProcessing()
  460. {
  461. for (RemuxQueueEntry &entry : queue)
  462. if (entry.state == RemuxEntryState::Ready)
  463. entry.state = RemuxEntryState::Pending;
  464. // Signal that the insertion point no longer exists.
  465. beginRemoveRows(QModelIndex(), queue.length(), queue.length());
  466. endRemoveRows();
  467. isProcessing = true;
  468. emit dataChanged(index(0, RemuxEntryColumn::State),
  469. index(queue.length(), RemuxEntryColumn::State));
  470. }
  471. void RemuxQueueModel::endProcessing()
  472. {
  473. for (RemuxQueueEntry &entry : queue) {
  474. if (entry.state == RemuxEntryState::Pending) {
  475. entry.state = RemuxEntryState::Ready;
  476. }
  477. }
  478. // Signal that the insertion point exists again.
  479. if (!autoRemux) {
  480. beginInsertRows(QModelIndex(), queue.length(), queue.length());
  481. endInsertRows();
  482. }
  483. isProcessing = false;
  484. emit dataChanged(index(0, RemuxEntryColumn::State),
  485. index(queue.length(), RemuxEntryColumn::State));
  486. }
  487. bool RemuxQueueModel::beginNextEntry(QString &inputPath, QString &outputPath)
  488. {
  489. bool anyStarted = false;
  490. for (int row = 0; row < queue.length(); row++) {
  491. RemuxQueueEntry &entry = queue[row];
  492. if (entry.state == RemuxEntryState::Pending) {
  493. entry.state = RemuxEntryState::InProgress;
  494. inputPath = entry.sourcePath;
  495. outputPath = entry.targetPath;
  496. QModelIndex index = this->index(row,
  497. RemuxEntryColumn::State);
  498. emit dataChanged(index, index);
  499. anyStarted = true;
  500. break;
  501. }
  502. }
  503. return anyStarted;
  504. }
  505. void RemuxQueueModel::finishEntry(bool success)
  506. {
  507. for (int row = 0; row < queue.length(); row++) {
  508. RemuxQueueEntry &entry = queue[row];
  509. if (entry.state == RemuxEntryState::InProgress) {
  510. if (success)
  511. entry.state = RemuxEntryState::Complete;
  512. else
  513. entry.state = RemuxEntryState::Error;
  514. QModelIndex index = this->index(row,
  515. RemuxEntryColumn::State);
  516. emit dataChanged(index, index);
  517. break;
  518. }
  519. }
  520. }
  521. /**********************************************************
  522. The actual remux window implementation
  523. **********************************************************/
  524. OBSRemux::OBSRemux(const char *path, QWidget *parent, bool autoRemux_)
  525. : QDialog (parent),
  526. queueModel(new RemuxQueueModel),
  527. worker (new RemuxWorker()),
  528. ui (new Ui::OBSRemux),
  529. recPath (path),
  530. autoRemux (autoRemux_)
  531. {
  532. setAcceptDrops(true);
  533. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  534. ui->setupUi(this);
  535. ui->progressBar->setVisible(false);
  536. ui->buttonBox->button(QDialogButtonBox::Ok)->
  537. setEnabled(false);
  538. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->
  539. setEnabled(false);
  540. if (autoRemux) {
  541. resize(280, 40);
  542. ui->tableView->hide();
  543. ui->buttonBox->hide();
  544. ui->label->hide();
  545. }
  546. ui->progressBar->setMinimum(0);
  547. ui->progressBar->setMaximum(1000);
  548. ui->progressBar->setValue(0);
  549. ui->tableView->setModel(queueModel);
  550. ui->tableView->setItemDelegateForColumn(RemuxEntryColumn::InputPath,
  551. new RemuxEntryPathItemDelegate(false, recPath));
  552. ui->tableView->setItemDelegateForColumn(RemuxEntryColumn::OutputPath,
  553. new RemuxEntryPathItemDelegate(true, recPath));
  554. ui->tableView->horizontalHeader()->setSectionResizeMode(
  555. QHeaderView::ResizeMode::Stretch);
  556. ui->tableView->horizontalHeader()->setSectionResizeMode(
  557. RemuxEntryColumn::State,
  558. QHeaderView::ResizeMode::Fixed);
  559. ui->tableView->setEditTriggers(
  560. QAbstractItemView::EditTrigger::CurrentChanged);
  561. installEventFilter(CreateShortcutFilter());
  562. ui->buttonBox->button(QDialogButtonBox::Ok)->
  563. setText(QTStr("Remux.Remux"));
  564. ui->buttonBox->button(QDialogButtonBox::Reset)->
  565. setText(QTStr("Remux.ClearFinished"));
  566. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->
  567. setText(QTStr("Remux.ClearAll"));
  568. ui->buttonBox->button(QDialogButtonBox::Reset)->
  569. setDisabled(true);
  570. connect(ui->buttonBox->button(QDialogButtonBox::Ok),
  571. SIGNAL(clicked()), this, SLOT(beginRemux()));
  572. connect(ui->buttonBox->button(QDialogButtonBox::Reset),
  573. SIGNAL(clicked()), this, SLOT(clearFinished()));
  574. connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults),
  575. SIGNAL(clicked()), this, SLOT(clearAll()));
  576. connect(ui->buttonBox->button(QDialogButtonBox::Close),
  577. SIGNAL(clicked()), this, SLOT(close()));
  578. worker->moveToThread(&remuxer);
  579. remuxer.start();
  580. //gcc-4.8 can't use QPointer<RemuxWorker> below
  581. RemuxWorker *worker_ = worker;
  582. connect(worker_, &RemuxWorker::updateProgress,
  583. this, &OBSRemux::updateProgress);
  584. connect(&remuxer, &QThread::finished, worker_, &QObject::deleteLater);
  585. connect(worker_, &RemuxWorker::remuxFinished,
  586. this, &OBSRemux::remuxFinished);
  587. connect(this, &OBSRemux::remux, worker_, &RemuxWorker::remux);
  588. // Guessing the GCC bug mentioned above would also affect
  589. // QPointer<RemuxQueueModel>? Unsure.
  590. RemuxQueueModel *queueModel_ = queueModel;
  591. connect(queueModel_,
  592. SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  593. this,
  594. SLOT(rowCountChanged(const QModelIndex &, int, int)));
  595. connect(queueModel_,
  596. SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
  597. this,
  598. SLOT(rowCountChanged(const QModelIndex &, int, int)));
  599. QModelIndex index = queueModel->createIndex(0, 1);
  600. QMetaObject::invokeMethod(ui->tableView,
  601. "setCurrentIndex", Qt::QueuedConnection,
  602. Q_ARG(const QModelIndex &, index));
  603. }
  604. bool OBSRemux::stopRemux()
  605. {
  606. if (!worker->isWorking)
  607. return true;
  608. // By locking the worker thread's mutex, we ensure that its
  609. // update poll will be blocked as long as we're in here with
  610. // the popup open.
  611. QMutexLocker lock(&worker->updateMutex);
  612. bool exit = false;
  613. if (QMessageBox::critical(nullptr,
  614. QTStr("Remux.ExitUnfinishedTitle"),
  615. QTStr("Remux.ExitUnfinished"),
  616. QMessageBox::Yes | QMessageBox::No,
  617. QMessageBox::No) ==
  618. QMessageBox::Yes) {
  619. exit = true;
  620. }
  621. if (exit) {
  622. // Inform the worker it should no longer be
  623. // working. It will interrupt accordingly in
  624. // its next update callback.
  625. worker->isWorking = false;
  626. }
  627. return exit;
  628. }
  629. OBSRemux::~OBSRemux()
  630. {
  631. stopRemux();
  632. remuxer.quit();
  633. remuxer.wait();
  634. }
  635. void OBSRemux::rowCountChanged(const QModelIndex &, int, int)
  636. {
  637. // See if there are still any rows ready to remux. Change
  638. // the state of the "go" button accordingly.
  639. // There must be more than one row, since there will always be
  640. // at least one row for the empty insertion point.
  641. if (queueModel->rowCount() > 1) {
  642. ui->buttonBox->button(QDialogButtonBox::Ok)->
  643. setEnabled(true);
  644. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->
  645. setEnabled(true);
  646. ui->buttonBox->button(QDialogButtonBox::Reset)->
  647. setEnabled(queueModel->canClearFinished());
  648. } else {
  649. ui->buttonBox->button(QDialogButtonBox::Ok)->
  650. setEnabled(false);
  651. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->
  652. setEnabled(false);
  653. ui->buttonBox->button(QDialogButtonBox::Reset)->
  654. setEnabled(false);
  655. }
  656. }
  657. void OBSRemux::dropEvent(QDropEvent *ev)
  658. {
  659. QStringList urlList;
  660. for (QUrl url : ev->mimeData()->urls()) {
  661. QFileInfo fileInfo(url.toLocalFile());
  662. if (fileInfo.isDir()) {
  663. QStringList directoryFilter;
  664. directoryFilter <<
  665. "*.flv" <<
  666. "*.mp4" <<
  667. "*.mov" <<
  668. "*.mkv" <<
  669. "*.ts" <<
  670. "*.m3u8";
  671. QDirIterator dirIter(fileInfo.absoluteFilePath(),
  672. directoryFilter, QDir::Files,
  673. QDirIterator::Subdirectories);
  674. while (dirIter.hasNext()) {
  675. urlList.append(dirIter.next());
  676. }
  677. } else {
  678. urlList.append(fileInfo.canonicalFilePath());
  679. }
  680. }
  681. if (urlList.empty()) {
  682. QMessageBox::information(nullptr,
  683. QTStr("Remux.NoFilesAddedTitle"),
  684. QTStr("Remux.NoFilesAdded"), QMessageBox::Ok);
  685. } else if (!autoRemux) {
  686. QModelIndex insertIndex = queueModel->index(
  687. queueModel->rowCount() - 1,
  688. RemuxEntryColumn::InputPath);
  689. queueModel->setData(insertIndex, urlList,
  690. RemuxEntryRole::NewPathsToProcessRole);
  691. }
  692. }
  693. void OBSRemux::dragEnterEvent(QDragEnterEvent *ev)
  694. {
  695. if (ev->mimeData()->hasUrls() && !worker->isWorking)
  696. ev->accept();
  697. }
  698. void OBSRemux::beginRemux()
  699. {
  700. if (worker->isWorking) {
  701. stopRemux();
  702. return;
  703. }
  704. bool proceedWithRemux = true;
  705. QFileInfoList overwriteFiles = queueModel->checkForOverwrites();
  706. if (!overwriteFiles.empty()) {
  707. QString message = QTStr("Remux.FileExists");
  708. message += "\n\n";
  709. for (QFileInfo fileInfo : overwriteFiles)
  710. message += fileInfo.canonicalFilePath() + "\n";
  711. if (OBSMessageBox::question(this,
  712. QTStr("Remux.FileExistsTitle"), message)
  713. != QMessageBox::Yes)
  714. proceedWithRemux = false;
  715. }
  716. if (!proceedWithRemux)
  717. return;
  718. // Set all jobs to "pending" first.
  719. queueModel->beginProcessing();
  720. ui->progressBar->setVisible(true);
  721. ui->buttonBox->button(QDialogButtonBox::Ok)->
  722. setText(QTStr("Remux.Stop"));
  723. setAcceptDrops(false);
  724. remuxNextEntry();
  725. }
  726. void OBSRemux::AutoRemux(QString inFile, QString outFile)
  727. {
  728. if (inFile != "" && outFile != "" && autoRemux) {
  729. emit remux(inFile, outFile);
  730. autoRemuxFile = inFile;
  731. }
  732. }
  733. void OBSRemux::remuxNextEntry()
  734. {
  735. worker->lastProgress = 0.f;
  736. QString inputPath, outputPath;
  737. if (queueModel->beginNextEntry(inputPath, outputPath)) {
  738. emit remux(inputPath, outputPath);
  739. } else {
  740. queueModel->autoRemux = autoRemux;
  741. queueModel->endProcessing();
  742. if (!autoRemux) {
  743. OBSMessageBox::information(this,
  744. QTStr("Remux.FinishedTitle"),
  745. queueModel->checkForErrors()
  746. ? QTStr("Remux.FinishedError")
  747. : QTStr("Remux.Finished"));
  748. }
  749. ui->progressBar->setVisible(autoRemux);
  750. ui->buttonBox->button(QDialogButtonBox::Ok)->
  751. setText(QTStr("Remux.Remux"));
  752. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->
  753. setEnabled(true);
  754. ui->buttonBox->button(QDialogButtonBox::Reset)->
  755. setEnabled(queueModel->canClearFinished());
  756. setAcceptDrops(true);
  757. }
  758. }
  759. void OBSRemux::closeEvent(QCloseEvent *event)
  760. {
  761. if (!stopRemux())
  762. event->ignore();
  763. else
  764. QDialog::closeEvent(event);
  765. }
  766. void OBSRemux::reject()
  767. {
  768. if (!stopRemux())
  769. return;
  770. QDialog::reject();
  771. }
  772. void OBSRemux::updateProgress(float percent)
  773. {
  774. ui->progressBar->setValue(percent * 10);
  775. }
  776. void OBSRemux::remuxFinished(bool success)
  777. {
  778. ui->buttonBox->button(QDialogButtonBox::Ok)->
  779. setEnabled(true);
  780. queueModel->finishEntry(success);
  781. if (autoRemux && autoRemuxFile != "") {
  782. QFile::remove(autoRemuxFile);
  783. QTimer::singleShot(3000, this, SLOT(close()));
  784. }
  785. remuxNextEntry();
  786. }
  787. void OBSRemux::clearFinished()
  788. {
  789. queueModel->clearFinished();
  790. }
  791. void OBSRemux::clearAll()
  792. {
  793. queueModel->clearAll();
  794. }
  795. /**********************************************************
  796. Worker thread - Executes the libobs remux operation as a
  797. background process.
  798. **********************************************************/
  799. void RemuxWorker::UpdateProgress(float percent)
  800. {
  801. if (abs(lastProgress - percent) < 0.1f)
  802. return;
  803. emit updateProgress(percent);
  804. lastProgress = percent;
  805. }
  806. void RemuxWorker::remux(const QString &source, const QString &target)
  807. {
  808. isWorking = true;
  809. auto callback = [](void *data, float percent)
  810. {
  811. RemuxWorker *rw = static_cast<RemuxWorker*>(data);
  812. QMutexLocker lock(&rw->updateMutex);
  813. rw->UpdateProgress(percent);
  814. return rw->isWorking;
  815. };
  816. bool stopped = false;
  817. bool success = false;
  818. media_remux_job_t mr_job = nullptr;
  819. if (media_remux_job_create(&mr_job,
  820. QT_TO_UTF8(source),
  821. QT_TO_UTF8(target))) {
  822. success = media_remux_job_process(mr_job, callback,
  823. this);
  824. media_remux_job_destroy(mr_job);
  825. stopped = !isWorking;
  826. }
  827. isWorking = false;
  828. emit remuxFinished(!stopped && success);
  829. }