1
0

window-remux.cpp 25 KB

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