window-remux.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 <QItemDelegate>
  19. #include <QLineEdit>
  20. #include <QMessageBox>
  21. #include <QMimeData>
  22. #include <QPainter>
  23. #include <QPushButton>
  24. #include <QStandardItemModel>
  25. #include <QStyledItemDelegate>
  26. #include <QToolButton>
  27. #include <QTimer>
  28. #include "qt-wrappers.hpp"
  29. #include "window-basic-main.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->setContentsMargins(0, 0, 0, 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. QObject::connect(text, SIGNAL(editingFinished()), this,
  95. SLOT(updateText()));
  96. QToolButton *browseButton = new QToolButton();
  97. browseButton->setText("...");
  98. browseButton->setSizePolicy(buttonSizePolicy);
  99. layout->addWidget(browseButton);
  100. container->connect(browseButton, &QToolButton::clicked,
  101. browseCallback);
  102. // The "clear" button is not shown in output cells
  103. // or the insertion point's input cell.
  104. if (!isOutput && state != RemuxEntryState::Empty) {
  105. QToolButton *clearButton = new QToolButton();
  106. clearButton->setText("X");
  107. clearButton->setSizePolicy(buttonSizePolicy);
  108. layout->addWidget(clearButton);
  109. container->connect(clearButton, &QToolButton::clicked,
  110. clearCallback);
  111. }
  112. container->setLayout(layout);
  113. container->setFocusProxy(text);
  114. return container;
  115. }
  116. }
  117. void RemuxEntryPathItemDelegate::setEditorData(QWidget *editor,
  118. const QModelIndex &index) const
  119. {
  120. QLineEdit *text = editor->findChild<QLineEdit *>();
  121. text->setText(index.data().toString());
  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::Window);
  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 = SaveFile(container,
  183. QTStr("Remux.SelectTarget"),
  184. currentPath, OutputPattern);
  185. if (!newPath.isEmpty()) {
  186. container->setProperty(PATH_LIST_PROP,
  187. QStringList() << newPath);
  188. isSet = true;
  189. }
  190. } else {
  191. QStringList paths = OpenFiles(
  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. #ifdef __APPLE__
  200. // TODO: Revisit when QTBUG-42661 is fixed
  201. container->window()->raise();
  202. #endif
  203. }
  204. if (isSet)
  205. emit commitData(container);
  206. }
  207. void RemuxEntryPathItemDelegate::handleClear(QWidget *container)
  208. {
  209. // An empty string list will indicate that the entry is being
  210. // blanked and should be deleted.
  211. container->setProperty(PATH_LIST_PROP, QStringList());
  212. emit commitData(container);
  213. }
  214. void RemuxEntryPathItemDelegate::updateText()
  215. {
  216. QLineEdit *lineEdit = dynamic_cast<QLineEdit *>(sender());
  217. QWidget *editor = lineEdit->parentWidget();
  218. emit commitData(editor);
  219. }
  220. /**********************************************************
  221. Model - Manages the queue's data
  222. **********************************************************/
  223. int RemuxQueueModel::rowCount(const QModelIndex &) const
  224. {
  225. return queue.length() + (isProcessing ? 0 : 1);
  226. }
  227. int RemuxQueueModel::columnCount(const QModelIndex &) const
  228. {
  229. return RemuxEntryColumn::Count;
  230. }
  231. QVariant RemuxQueueModel::data(const QModelIndex &index, int role) const
  232. {
  233. QVariant result = QVariant();
  234. if (index.row() >= queue.length()) {
  235. return QVariant();
  236. } else if (role == Qt::DisplayRole) {
  237. switch (index.column()) {
  238. case RemuxEntryColumn::InputPath:
  239. result = queue[index.row()].sourcePath;
  240. break;
  241. case RemuxEntryColumn::OutputPath:
  242. result = queue[index.row()].targetPath;
  243. break;
  244. }
  245. } else if (role == Qt::DecorationRole &&
  246. index.column() == RemuxEntryColumn::State) {
  247. result = getIcon(queue[index.row()].state);
  248. } else if (role == RemuxEntryRole::EntryStateRole) {
  249. result = queue[index.row()].state;
  250. }
  251. return result;
  252. }
  253. QVariant RemuxQueueModel::headerData(int section, Qt::Orientation orientation,
  254. int role) const
  255. {
  256. QVariant result = QVariant();
  257. if (role == Qt::DisplayRole &&
  258. orientation == Qt::Orientation::Horizontal) {
  259. switch (section) {
  260. case RemuxEntryColumn::State:
  261. result = QString();
  262. break;
  263. case RemuxEntryColumn::InputPath:
  264. result = QTStr("Remux.SourceFile");
  265. break;
  266. case RemuxEntryColumn::OutputPath:
  267. result = QTStr("Remux.TargetFile");
  268. break;
  269. }
  270. }
  271. return result;
  272. }
  273. Qt::ItemFlags RemuxQueueModel::flags(const QModelIndex &index) const
  274. {
  275. Qt::ItemFlags flags = QAbstractTableModel::flags(index);
  276. if (index.column() == RemuxEntryColumn::InputPath) {
  277. flags |= Qt::ItemIsEditable;
  278. } else if (index.column() == RemuxEntryColumn::OutputPath &&
  279. index.row() != queue.length()) {
  280. flags |= Qt::ItemIsEditable;
  281. }
  282. return flags;
  283. }
  284. bool RemuxQueueModel::setData(const QModelIndex &index, const QVariant &value,
  285. int role)
  286. {
  287. bool success = false;
  288. if (role == RemuxEntryRole::NewPathsToProcessRole) {
  289. QStringList pathList = value.toStringList();
  290. if (pathList.size() == 0) {
  291. if (index.row() < queue.size()) {
  292. beginRemoveRows(QModelIndex(), index.row(),
  293. index.row());
  294. queue.removeAt(index.row());
  295. endRemoveRows();
  296. }
  297. } else {
  298. if (pathList.size() >= 1 &&
  299. index.row() < queue.length()) {
  300. queue[index.row()].sourcePath = pathList[0];
  301. checkInputPath(index.row());
  302. pathList.removeAt(0);
  303. success = true;
  304. }
  305. if (pathList.size() > 0) {
  306. int row = index.row();
  307. int lastRow = row + pathList.size() - 1;
  308. beginInsertRows(QModelIndex(), row, lastRow);
  309. for (QString path : pathList) {
  310. RemuxQueueEntry entry;
  311. entry.sourcePath = path;
  312. entry.state = RemuxEntryState::Empty;
  313. queue.insert(row, entry);
  314. row++;
  315. }
  316. endInsertRows();
  317. for (row = index.row(); row <= lastRow; row++) {
  318. checkInputPath(row);
  319. }
  320. success = true;
  321. }
  322. }
  323. } else if (index.row() == queue.length()) {
  324. QString path = value.toString();
  325. if (!path.isEmpty()) {
  326. RemuxQueueEntry entry;
  327. entry.sourcePath = path;
  328. entry.state = RemuxEntryState::Empty;
  329. beginInsertRows(QModelIndex(), queue.length() + 1,
  330. queue.length() + 1);
  331. queue.append(entry);
  332. endInsertRows();
  333. checkInputPath(index.row());
  334. success = true;
  335. }
  336. } else {
  337. QString path = value.toString();
  338. if (path.isEmpty()) {
  339. if (index.column() == RemuxEntryColumn::InputPath) {
  340. beginRemoveRows(QModelIndex(), index.row(),
  341. index.row());
  342. queue.removeAt(index.row());
  343. endRemoveRows();
  344. }
  345. } else {
  346. switch (index.column()) {
  347. case RemuxEntryColumn::InputPath:
  348. queue[index.row()].sourcePath =
  349. value.toString();
  350. checkInputPath(index.row());
  351. success = true;
  352. break;
  353. case RemuxEntryColumn::OutputPath:
  354. queue[index.row()].targetPath =
  355. value.toString();
  356. emit dataChanged(index, index);
  357. success = true;
  358. break;
  359. }
  360. }
  361. }
  362. return success;
  363. }
  364. QVariant RemuxQueueModel::getIcon(RemuxEntryState state)
  365. {
  366. QVariant icon;
  367. QStyle *style = QApplication::style();
  368. switch (state) {
  369. case RemuxEntryState::Complete:
  370. icon = style->standardIcon(QStyle::SP_DialogApplyButton);
  371. break;
  372. case RemuxEntryState::InProgress:
  373. icon = style->standardIcon(QStyle::SP_ArrowRight);
  374. break;
  375. case RemuxEntryState::Error:
  376. icon = style->standardIcon(QStyle::SP_DialogCancelButton);
  377. break;
  378. case RemuxEntryState::InvalidPath:
  379. icon = style->standardIcon(QStyle::SP_MessageBoxWarning);
  380. break;
  381. default:
  382. break;
  383. }
  384. return icon;
  385. }
  386. void RemuxQueueModel::checkInputPath(int row)
  387. {
  388. RemuxQueueEntry &entry = queue[row];
  389. if (entry.sourcePath.isEmpty()) {
  390. entry.state = RemuxEntryState::Empty;
  391. } else {
  392. entry.sourcePath = QDir::toNativeSeparators(entry.sourcePath);
  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 = QDir::toNativeSeparators(
  400. fileInfo.path() + QDir::separator() +
  401. fileInfo.completeBaseName() + ".mp4");
  402. }
  403. if (entry.state == RemuxEntryState::Ready && isProcessing)
  404. entry.state = RemuxEntryState::Pending;
  405. emit dataChanged(index(row, 0), index(row, RemuxEntryColumn::Count));
  406. }
  407. QFileInfoList RemuxQueueModel::checkForOverwrites() const
  408. {
  409. QFileInfoList list;
  410. for (const RemuxQueueEntry &entry : queue) {
  411. if (entry.state == RemuxEntryState::Ready) {
  412. QFileInfo fileInfo(entry.targetPath);
  413. if (fileInfo.exists()) {
  414. list.append(fileInfo);
  415. }
  416. }
  417. }
  418. return list;
  419. }
  420. bool RemuxQueueModel::checkForErrors() const
  421. {
  422. bool hasErrors = false;
  423. for (const RemuxQueueEntry &entry : queue) {
  424. if (entry.state == RemuxEntryState::Error) {
  425. hasErrors = true;
  426. break;
  427. }
  428. }
  429. return hasErrors;
  430. }
  431. void RemuxQueueModel::clearAll()
  432. {
  433. beginRemoveRows(QModelIndex(), 0, queue.size() - 1);
  434. queue.clear();
  435. endRemoveRows();
  436. }
  437. void RemuxQueueModel::clearFinished()
  438. {
  439. int index = 0;
  440. for (index = 0; index < queue.size(); index++) {
  441. const RemuxQueueEntry &entry = queue[index];
  442. if (entry.state == RemuxEntryState::Complete) {
  443. beginRemoveRows(QModelIndex(), index, index);
  444. queue.removeAt(index);
  445. endRemoveRows();
  446. index--;
  447. }
  448. }
  449. }
  450. bool RemuxQueueModel::canClearFinished() const
  451. {
  452. bool canClearFinished = false;
  453. for (const RemuxQueueEntry &entry : queue)
  454. if (entry.state == RemuxEntryState::Complete) {
  455. canClearFinished = true;
  456. break;
  457. }
  458. return canClearFinished;
  459. }
  460. void RemuxQueueModel::beginProcessing()
  461. {
  462. for (RemuxQueueEntry &entry : queue)
  463. if (entry.state == RemuxEntryState::Ready)
  464. entry.state = RemuxEntryState::Pending;
  465. // Signal that the insertion point no longer exists.
  466. beginRemoveRows(QModelIndex(), queue.length(), queue.length());
  467. endRemoveRows();
  468. isProcessing = true;
  469. emit dataChanged(index(0, RemuxEntryColumn::State),
  470. index(queue.length(), RemuxEntryColumn::State));
  471. }
  472. void RemuxQueueModel::endProcessing()
  473. {
  474. for (RemuxQueueEntry &entry : queue) {
  475. if (entry.state == RemuxEntryState::Pending) {
  476. entry.state = RemuxEntryState::Ready;
  477. }
  478. }
  479. // Signal that the insertion point exists again.
  480. if (!autoRemux) {
  481. beginInsertRows(QModelIndex(), queue.length(), queue.length());
  482. endInsertRows();
  483. }
  484. isProcessing = false;
  485. emit dataChanged(index(0, RemuxEntryColumn::State),
  486. index(queue.length(), RemuxEntryColumn::State));
  487. }
  488. bool RemuxQueueModel::beginNextEntry(QString &inputPath, QString &outputPath)
  489. {
  490. bool anyStarted = false;
  491. for (int row = 0; row < queue.length(); row++) {
  492. RemuxQueueEntry &entry = queue[row];
  493. if (entry.state == RemuxEntryState::Pending) {
  494. entry.state = RemuxEntryState::InProgress;
  495. inputPath = entry.sourcePath;
  496. outputPath = entry.targetPath;
  497. QModelIndex index =
  498. this->index(row, RemuxEntryColumn::State);
  499. emit dataChanged(index, index);
  500. anyStarted = true;
  501. break;
  502. }
  503. }
  504. return anyStarted;
  505. }
  506. void RemuxQueueModel::finishEntry(bool success)
  507. {
  508. for (int row = 0; row < queue.length(); row++) {
  509. RemuxQueueEntry &entry = queue[row];
  510. if (entry.state == RemuxEntryState::InProgress) {
  511. if (success)
  512. entry.state = RemuxEntryState::Complete;
  513. else
  514. entry.state = RemuxEntryState::Error;
  515. QModelIndex index =
  516. this->index(row, RemuxEntryColumn::State);
  517. emit dataChanged(index, index);
  518. break;
  519. }
  520. }
  521. }
  522. /**********************************************************
  523. The actual remux window implementation
  524. **********************************************************/
  525. OBSRemux::OBSRemux(const char *path, QWidget *parent, bool autoRemux_)
  526. : QDialog(parent),
  527. queueModel(new RemuxQueueModel),
  528. worker(new RemuxWorker()),
  529. ui(new Ui::OBSRemux),
  530. recPath(path),
  531. autoRemux(autoRemux_)
  532. {
  533. setAcceptDrops(true);
  534. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  535. ui->setupUi(this);
  536. ui->progressBar->setVisible(false);
  537. ui->buttonBox->button(QDialogButtonBox::Ok)->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(
  551. RemuxEntryColumn::InputPath,
  552. new RemuxEntryPathItemDelegate(false, recPath));
  553. ui->tableView->setItemDelegateForColumn(
  554. RemuxEntryColumn::OutputPath,
  555. new RemuxEntryPathItemDelegate(true, recPath));
  556. ui->tableView->horizontalHeader()->setSectionResizeMode(
  557. QHeaderView::ResizeMode::Stretch);
  558. ui->tableView->horizontalHeader()->setSectionResizeMode(
  559. RemuxEntryColumn::State, QHeaderView::ResizeMode::Fixed);
  560. ui->tableView->setEditTriggers(
  561. QAbstractItemView::EditTrigger::CurrentChanged);
  562. ui->tableView->setTextElideMode(Qt::ElideMiddle);
  563. ui->tableView->setWordWrap(false);
  564. installEventFilter(CreateShortcutFilter());
  565. ui->buttonBox->button(QDialogButtonBox::Ok)
  566. ->setText(QTStr("Remux.Remux"));
  567. ui->buttonBox->button(QDialogButtonBox::Reset)
  568. ->setText(QTStr("Remux.ClearFinished"));
  569. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  570. ->setText(QTStr("Remux.ClearAll"));
  571. ui->buttonBox->button(QDialogButtonBox::Reset)->setDisabled(true);
  572. connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
  573. this, SLOT(beginRemux()));
  574. connect(ui->buttonBox->button(QDialogButtonBox::Reset),
  575. SIGNAL(clicked()), this, SLOT(clearFinished()));
  576. connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults),
  577. SIGNAL(clicked()), this, SLOT(clearAll()));
  578. connect(ui->buttonBox->button(QDialogButtonBox::Close),
  579. SIGNAL(clicked()), this, SLOT(close()));
  580. worker->moveToThread(&remuxer);
  581. remuxer.start();
  582. //gcc-4.8 can't use QPointer<RemuxWorker> below
  583. RemuxWorker *worker_ = worker;
  584. connect(worker_, &RemuxWorker::updateProgress, this,
  585. &OBSRemux::updateProgress);
  586. connect(&remuxer, &QThread::finished, worker_, &QObject::deleteLater);
  587. connect(worker_, &RemuxWorker::remuxFinished, this,
  588. &OBSRemux::remuxFinished);
  589. connect(this, &OBSRemux::remux, worker_, &RemuxWorker::remux);
  590. // Guessing the GCC bug mentioned above would also affect
  591. // QPointer<RemuxQueueModel>? Unsure.
  592. RemuxQueueModel *queueModel_ = queueModel;
  593. connect(queueModel_,
  594. SIGNAL(rowsInserted(const QModelIndex &, int, int)), this,
  595. SLOT(rowCountChanged(const QModelIndex &, int, int)));
  596. connect(queueModel_, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
  597. this, SLOT(rowCountChanged(const QModelIndex &, int, int)));
  598. QModelIndex index = queueModel->createIndex(0, 1);
  599. QMetaObject::invokeMethod(ui->tableView, "setCurrentIndex",
  600. Qt::QueuedConnection,
  601. Q_ARG(const QModelIndex &, index));
  602. }
  603. bool OBSRemux::stopRemux()
  604. {
  605. if (!worker->isWorking)
  606. return true;
  607. // By locking the worker thread's mutex, we ensure that its
  608. // update poll will be blocked as long as we're in here with
  609. // the popup open.
  610. QMutexLocker lock(&worker->updateMutex);
  611. bool exit = false;
  612. if (QMessageBox::critical(nullptr, QTStr("Remux.ExitUnfinishedTitle"),
  613. QTStr("Remux.ExitUnfinished"),
  614. QMessageBox::Yes | QMessageBox::No,
  615. QMessageBox::No) == QMessageBox::Yes) {
  616. exit = true;
  617. }
  618. if (exit) {
  619. // Inform the worker it should no longer be
  620. // working. It will interrupt accordingly in
  621. // its next update callback.
  622. worker->isWorking = false;
  623. }
  624. return exit;
  625. }
  626. OBSRemux::~OBSRemux()
  627. {
  628. stopRemux();
  629. remuxer.quit();
  630. remuxer.wait();
  631. }
  632. void OBSRemux::rowCountChanged(const QModelIndex &, int, int)
  633. {
  634. // See if there are still any rows ready to remux. Change
  635. // the state of the "go" button accordingly.
  636. // There must be more than one row, since there will always be
  637. // at least one row for the empty insertion point.
  638. if (queueModel->rowCount() > 1) {
  639. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
  640. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  641. ->setEnabled(true);
  642. ui->buttonBox->button(QDialogButtonBox::Reset)
  643. ->setEnabled(queueModel->canClearFinished());
  644. } else {
  645. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
  646. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  647. ->setEnabled(false);
  648. ui->buttonBox->button(QDialogButtonBox::Reset)
  649. ->setEnabled(false);
  650. }
  651. }
  652. void OBSRemux::dropEvent(QDropEvent *ev)
  653. {
  654. QStringList urlList;
  655. for (QUrl url : ev->mimeData()->urls()) {
  656. QFileInfo fileInfo(url.toLocalFile());
  657. if (fileInfo.isDir()) {
  658. QStringList directoryFilter;
  659. directoryFilter << "*.flv"
  660. << "*.mp4"
  661. << "*.mov"
  662. << "*.mkv"
  663. << "*.ts"
  664. << "*.m3u8";
  665. QDirIterator dirIter(fileInfo.absoluteFilePath(),
  666. directoryFilter, QDir::Files,
  667. QDirIterator::Subdirectories);
  668. while (dirIter.hasNext()) {
  669. urlList.append(dirIter.next());
  670. }
  671. } else {
  672. urlList.append(fileInfo.canonicalFilePath());
  673. }
  674. }
  675. if (urlList.empty()) {
  676. QMessageBox::information(nullptr,
  677. QTStr("Remux.NoFilesAddedTitle"),
  678. QTStr("Remux.NoFilesAdded"),
  679. QMessageBox::Ok);
  680. } else if (!autoRemux) {
  681. QModelIndex insertIndex =
  682. queueModel->index(queueModel->rowCount() - 1,
  683. RemuxEntryColumn::InputPath);
  684. queueModel->setData(insertIndex, urlList,
  685. RemuxEntryRole::NewPathsToProcessRole);
  686. }
  687. }
  688. void OBSRemux::dragEnterEvent(QDragEnterEvent *ev)
  689. {
  690. if (ev->mimeData()->hasUrls() && !worker->isWorking)
  691. ev->accept();
  692. }
  693. void OBSRemux::beginRemux()
  694. {
  695. if (worker->isWorking) {
  696. stopRemux();
  697. return;
  698. }
  699. bool proceedWithRemux = true;
  700. QFileInfoList overwriteFiles = queueModel->checkForOverwrites();
  701. if (!overwriteFiles.empty()) {
  702. QString message = QTStr("Remux.FileExists");
  703. message += "\n\n";
  704. for (QFileInfo fileInfo : overwriteFiles)
  705. message += fileInfo.canonicalFilePath() + "\n";
  706. if (OBSMessageBox::question(this,
  707. QTStr("Remux.FileExistsTitle"),
  708. message) != QMessageBox::Yes)
  709. proceedWithRemux = false;
  710. }
  711. if (!proceedWithRemux)
  712. return;
  713. // Set all jobs to "pending" first.
  714. queueModel->beginProcessing();
  715. ui->progressBar->setVisible(true);
  716. ui->buttonBox->button(QDialogButtonBox::Ok)
  717. ->setText(QTStr("Remux.Stop"));
  718. setAcceptDrops(false);
  719. remuxNextEntry();
  720. }
  721. void OBSRemux::AutoRemux(QString inFile, QString outFile)
  722. {
  723. if (inFile != "" && outFile != "" && autoRemux) {
  724. ui->progressBar->setVisible(true);
  725. emit remux(inFile, outFile);
  726. autoRemuxFile = outFile;
  727. }
  728. }
  729. void OBSRemux::remuxNextEntry()
  730. {
  731. worker->lastProgress = 0.f;
  732. QString inputPath, outputPath;
  733. if (queueModel->beginNextEntry(inputPath, outputPath)) {
  734. emit remux(inputPath, outputPath);
  735. } else {
  736. queueModel->autoRemux = autoRemux;
  737. queueModel->endProcessing();
  738. if (!autoRemux) {
  739. OBSMessageBox::information(
  740. this, QTStr("Remux.FinishedTitle"),
  741. queueModel->checkForErrors()
  742. ? QTStr("Remux.FinishedError")
  743. : QTStr("Remux.Finished"));
  744. }
  745. ui->progressBar->setVisible(autoRemux);
  746. ui->buttonBox->button(QDialogButtonBox::Ok)
  747. ->setText(QTStr("Remux.Remux"));
  748. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  749. ->setEnabled(true);
  750. ui->buttonBox->button(QDialogButtonBox::Reset)
  751. ->setEnabled(queueModel->canClearFinished());
  752. setAcceptDrops(true);
  753. }
  754. }
  755. void OBSRemux::closeEvent(QCloseEvent *event)
  756. {
  757. if (!stopRemux())
  758. event->ignore();
  759. else
  760. QDialog::closeEvent(event);
  761. }
  762. void OBSRemux::reject()
  763. {
  764. if (!stopRemux())
  765. return;
  766. QDialog::reject();
  767. }
  768. void OBSRemux::updateProgress(float percent)
  769. {
  770. ui->progressBar->setValue(percent * 10);
  771. }
  772. void OBSRemux::remuxFinished(bool success)
  773. {
  774. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
  775. queueModel->finishEntry(success);
  776. if (autoRemux && autoRemuxFile != "") {
  777. QTimer::singleShot(3000, this, SLOT(close()));
  778. OBSBasic *main = OBSBasic::Get();
  779. main->ShowStatusBarMessage(
  780. QTStr("Basic.StatusBar.AutoRemuxedTo")
  781. .arg(autoRemuxFile));
  782. }
  783. remuxNextEntry();
  784. }
  785. void OBSRemux::clearFinished()
  786. {
  787. queueModel->clearFinished();
  788. }
  789. void OBSRemux::clearAll()
  790. {
  791. queueModel->clearAll();
  792. }
  793. /**********************************************************
  794. Worker thread - Executes the libobs remux operation as a
  795. background process.
  796. **********************************************************/
  797. void RemuxWorker::UpdateProgress(float percent)
  798. {
  799. if (abs(lastProgress - percent) < 0.1f)
  800. return;
  801. emit updateProgress(percent);
  802. lastProgress = percent;
  803. }
  804. void RemuxWorker::remux(const QString &source, const QString &target)
  805. {
  806. isWorking = true;
  807. auto callback = [](void *data, float percent) {
  808. RemuxWorker *rw = static_cast<RemuxWorker *>(data);
  809. QMutexLocker lock(&rw->updateMutex);
  810. rw->UpdateProgress(percent);
  811. return rw->isWorking;
  812. };
  813. bool stopped = false;
  814. bool success = false;
  815. media_remux_job_t mr_job = nullptr;
  816. if (media_remux_job_create(&mr_job, QT_TO_UTF8(source),
  817. QT_TO_UTF8(target))) {
  818. success = media_remux_job_process(mr_job, callback, this);
  819. media_remux_job_destroy(mr_job);
  820. stopped = !isWorking;
  821. }
  822. isWorking = false;
  823. emit remuxFinished(!stopped && success);
  824. }