window-remux.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. }
  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. entry.sourcePath = QDir::toNativeSeparators(entry.sourcePath);
  387. QFileInfo fileInfo(entry.sourcePath);
  388. if (fileInfo.exists())
  389. entry.state = RemuxEntryState::Ready;
  390. else
  391. entry.state = RemuxEntryState::InvalidPath;
  392. if (entry.state == RemuxEntryState::Ready)
  393. entry.targetPath = QDir::toNativeSeparators(
  394. fileInfo.path() + QDir::separator() +
  395. fileInfo.completeBaseName() + ".mp4");
  396. }
  397. if (entry.state == RemuxEntryState::Ready && isProcessing)
  398. entry.state = RemuxEntryState::Pending;
  399. emit dataChanged(index(row, 0), index(row, RemuxEntryColumn::Count));
  400. }
  401. QFileInfoList RemuxQueueModel::checkForOverwrites() const
  402. {
  403. QFileInfoList list;
  404. for (const RemuxQueueEntry &entry : queue) {
  405. if (entry.state == RemuxEntryState::Ready) {
  406. QFileInfo fileInfo(entry.targetPath);
  407. if (fileInfo.exists()) {
  408. list.append(fileInfo);
  409. }
  410. }
  411. }
  412. return list;
  413. }
  414. bool RemuxQueueModel::checkForErrors() const
  415. {
  416. bool hasErrors = false;
  417. for (const RemuxQueueEntry &entry : queue) {
  418. if (entry.state == RemuxEntryState::Error) {
  419. hasErrors = true;
  420. break;
  421. }
  422. }
  423. return hasErrors;
  424. }
  425. void RemuxQueueModel::clearAll()
  426. {
  427. beginRemoveRows(QModelIndex(), 0, queue.size() - 1);
  428. queue.clear();
  429. endRemoveRows();
  430. }
  431. void RemuxQueueModel::clearFinished()
  432. {
  433. int index = 0;
  434. for (index = 0; index < queue.size(); index++) {
  435. const RemuxQueueEntry &entry = queue[index];
  436. if (entry.state == RemuxEntryState::Complete) {
  437. beginRemoveRows(QModelIndex(), index, index);
  438. queue.removeAt(index);
  439. endRemoveRows();
  440. index--;
  441. }
  442. }
  443. }
  444. bool RemuxQueueModel::canClearFinished() const
  445. {
  446. bool canClearFinished = false;
  447. for (const RemuxQueueEntry &entry : queue)
  448. if (entry.state == RemuxEntryState::Complete) {
  449. canClearFinished = true;
  450. break;
  451. }
  452. return canClearFinished;
  453. }
  454. void RemuxQueueModel::beginProcessing()
  455. {
  456. for (RemuxQueueEntry &entry : queue)
  457. if (entry.state == RemuxEntryState::Ready)
  458. entry.state = RemuxEntryState::Pending;
  459. // Signal that the insertion point no longer exists.
  460. beginRemoveRows(QModelIndex(), queue.length(), queue.length());
  461. endRemoveRows();
  462. isProcessing = true;
  463. emit dataChanged(index(0, RemuxEntryColumn::State),
  464. index(queue.length(), RemuxEntryColumn::State));
  465. }
  466. void RemuxQueueModel::endProcessing()
  467. {
  468. for (RemuxQueueEntry &entry : queue) {
  469. if (entry.state == RemuxEntryState::Pending) {
  470. entry.state = RemuxEntryState::Ready;
  471. }
  472. }
  473. // Signal that the insertion point exists again.
  474. if (!autoRemux) {
  475. beginInsertRows(QModelIndex(), queue.length(), queue.length());
  476. endInsertRows();
  477. }
  478. isProcessing = false;
  479. emit dataChanged(index(0, RemuxEntryColumn::State),
  480. index(queue.length(), RemuxEntryColumn::State));
  481. }
  482. bool RemuxQueueModel::beginNextEntry(QString &inputPath, QString &outputPath)
  483. {
  484. bool anyStarted = false;
  485. for (int row = 0; row < queue.length(); row++) {
  486. RemuxQueueEntry &entry = queue[row];
  487. if (entry.state == RemuxEntryState::Pending) {
  488. entry.state = RemuxEntryState::InProgress;
  489. inputPath = entry.sourcePath;
  490. outputPath = entry.targetPath;
  491. QModelIndex index =
  492. this->index(row, RemuxEntryColumn::State);
  493. emit dataChanged(index, index);
  494. anyStarted = true;
  495. break;
  496. }
  497. }
  498. return anyStarted;
  499. }
  500. void RemuxQueueModel::finishEntry(bool success)
  501. {
  502. for (int row = 0; row < queue.length(); row++) {
  503. RemuxQueueEntry &entry = queue[row];
  504. if (entry.state == RemuxEntryState::InProgress) {
  505. if (success)
  506. entry.state = RemuxEntryState::Complete;
  507. else
  508. entry.state = RemuxEntryState::Error;
  509. QModelIndex index =
  510. this->index(row, RemuxEntryColumn::State);
  511. emit dataChanged(index, index);
  512. break;
  513. }
  514. }
  515. }
  516. /**********************************************************
  517. The actual remux window implementation
  518. **********************************************************/
  519. OBSRemux::OBSRemux(const char *path, QWidget *parent, bool autoRemux_)
  520. : QDialog(parent),
  521. queueModel(new RemuxQueueModel),
  522. worker(new RemuxWorker()),
  523. ui(new Ui::OBSRemux),
  524. recPath(path),
  525. autoRemux(autoRemux_)
  526. {
  527. setAcceptDrops(true);
  528. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  529. ui->setupUi(this);
  530. ui->progressBar->setVisible(false);
  531. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
  532. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  533. ->setEnabled(false);
  534. if (autoRemux) {
  535. resize(280, 40);
  536. ui->tableView->hide();
  537. ui->buttonBox->hide();
  538. ui->label->hide();
  539. }
  540. ui->progressBar->setMinimum(0);
  541. ui->progressBar->setMaximum(1000);
  542. ui->progressBar->setValue(0);
  543. ui->tableView->setModel(queueModel);
  544. ui->tableView->setItemDelegateForColumn(
  545. RemuxEntryColumn::InputPath,
  546. new RemuxEntryPathItemDelegate(false, recPath));
  547. ui->tableView->setItemDelegateForColumn(
  548. RemuxEntryColumn::OutputPath,
  549. new RemuxEntryPathItemDelegate(true, recPath));
  550. ui->tableView->horizontalHeader()->setSectionResizeMode(
  551. QHeaderView::ResizeMode::Stretch);
  552. ui->tableView->horizontalHeader()->setSectionResizeMode(
  553. RemuxEntryColumn::State, QHeaderView::ResizeMode::Fixed);
  554. ui->tableView->setEditTriggers(
  555. QAbstractItemView::EditTrigger::CurrentChanged);
  556. ui->tableView->setTextElideMode(Qt::ElideMiddle);
  557. ui->tableView->setWordWrap(false);
  558. installEventFilter(CreateShortcutFilter());
  559. ui->buttonBox->button(QDialogButtonBox::Ok)
  560. ->setText(QTStr("Remux.Remux"));
  561. ui->buttonBox->button(QDialogButtonBox::Reset)
  562. ->setText(QTStr("Remux.ClearFinished"));
  563. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  564. ->setText(QTStr("Remux.ClearAll"));
  565. ui->buttonBox->button(QDialogButtonBox::Reset)->setDisabled(true);
  566. connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
  567. this, SLOT(beginRemux()));
  568. connect(ui->buttonBox->button(QDialogButtonBox::Reset),
  569. SIGNAL(clicked()), this, SLOT(clearFinished()));
  570. connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults),
  571. SIGNAL(clicked()), this, SLOT(clearAll()));
  572. connect(ui->buttonBox->button(QDialogButtonBox::Close),
  573. SIGNAL(clicked()), this, SLOT(close()));
  574. worker->moveToThread(&remuxer);
  575. remuxer.start();
  576. //gcc-4.8 can't use QPointer<RemuxWorker> below
  577. RemuxWorker *worker_ = worker;
  578. connect(worker_, &RemuxWorker::updateProgress, this,
  579. &OBSRemux::updateProgress);
  580. connect(&remuxer, &QThread::finished, worker_, &QObject::deleteLater);
  581. connect(worker_, &RemuxWorker::remuxFinished, this,
  582. &OBSRemux::remuxFinished);
  583. connect(this, &OBSRemux::remux, worker_, &RemuxWorker::remux);
  584. // Guessing the GCC bug mentioned above would also affect
  585. // QPointer<RemuxQueueModel>? Unsure.
  586. RemuxQueueModel *queueModel_ = queueModel;
  587. connect(queueModel_,
  588. SIGNAL(rowsInserted(const QModelIndex &, int, int)), this,
  589. SLOT(rowCountChanged(const QModelIndex &, int, int)));
  590. connect(queueModel_, SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
  591. this, SLOT(rowCountChanged(const QModelIndex &, int, int)));
  592. QModelIndex index = queueModel->createIndex(0, 1);
  593. QMetaObject::invokeMethod(ui->tableView, "setCurrentIndex",
  594. Qt::QueuedConnection,
  595. Q_ARG(const QModelIndex &, index));
  596. }
  597. bool OBSRemux::stopRemux()
  598. {
  599. if (!worker->isWorking)
  600. return true;
  601. // By locking the worker thread's mutex, we ensure that its
  602. // update poll will be blocked as long as we're in here with
  603. // the popup open.
  604. QMutexLocker lock(&worker->updateMutex);
  605. bool exit = false;
  606. if (QMessageBox::critical(nullptr, QTStr("Remux.ExitUnfinishedTitle"),
  607. QTStr("Remux.ExitUnfinished"),
  608. QMessageBox::Yes | QMessageBox::No,
  609. QMessageBox::No) == QMessageBox::Yes) {
  610. exit = true;
  611. }
  612. if (exit) {
  613. // Inform the worker it should no longer be
  614. // working. It will interrupt accordingly in
  615. // its next update callback.
  616. worker->isWorking = false;
  617. }
  618. return exit;
  619. }
  620. OBSRemux::~OBSRemux()
  621. {
  622. stopRemux();
  623. remuxer.quit();
  624. remuxer.wait();
  625. }
  626. void OBSRemux::rowCountChanged(const QModelIndex &, int, int)
  627. {
  628. // See if there are still any rows ready to remux. Change
  629. // the state of the "go" button accordingly.
  630. // There must be more than one row, since there will always be
  631. // at least one row for the empty insertion point.
  632. if (queueModel->rowCount() > 1) {
  633. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
  634. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  635. ->setEnabled(true);
  636. ui->buttonBox->button(QDialogButtonBox::Reset)
  637. ->setEnabled(queueModel->canClearFinished());
  638. } else {
  639. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
  640. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  641. ->setEnabled(false);
  642. ui->buttonBox->button(QDialogButtonBox::Reset)
  643. ->setEnabled(false);
  644. }
  645. }
  646. void OBSRemux::dropEvent(QDropEvent *ev)
  647. {
  648. QStringList urlList;
  649. for (QUrl url : ev->mimeData()->urls()) {
  650. QFileInfo fileInfo(url.toLocalFile());
  651. if (fileInfo.isDir()) {
  652. QStringList directoryFilter;
  653. directoryFilter << "*.flv"
  654. << "*.mp4"
  655. << "*.mov"
  656. << "*.mkv"
  657. << "*.ts"
  658. << "*.m3u8";
  659. QDirIterator dirIter(fileInfo.absoluteFilePath(),
  660. directoryFilter, QDir::Files,
  661. QDirIterator::Subdirectories);
  662. while (dirIter.hasNext()) {
  663. urlList.append(dirIter.next());
  664. }
  665. } else {
  666. urlList.append(fileInfo.canonicalFilePath());
  667. }
  668. }
  669. if (urlList.empty()) {
  670. QMessageBox::information(nullptr,
  671. QTStr("Remux.NoFilesAddedTitle"),
  672. QTStr("Remux.NoFilesAdded"),
  673. QMessageBox::Ok);
  674. } else if (!autoRemux) {
  675. QModelIndex insertIndex =
  676. queueModel->index(queueModel->rowCount() - 1,
  677. RemuxEntryColumn::InputPath);
  678. queueModel->setData(insertIndex, urlList,
  679. RemuxEntryRole::NewPathsToProcessRole);
  680. }
  681. }
  682. void OBSRemux::dragEnterEvent(QDragEnterEvent *ev)
  683. {
  684. if (ev->mimeData()->hasUrls() && !worker->isWorking)
  685. ev->accept();
  686. }
  687. void OBSRemux::beginRemux()
  688. {
  689. if (worker->isWorking) {
  690. stopRemux();
  691. return;
  692. }
  693. bool proceedWithRemux = true;
  694. QFileInfoList overwriteFiles = queueModel->checkForOverwrites();
  695. if (!overwriteFiles.empty()) {
  696. QString message = QTStr("Remux.FileExists");
  697. message += "\n\n";
  698. for (QFileInfo fileInfo : overwriteFiles)
  699. message += fileInfo.canonicalFilePath() + "\n";
  700. if (OBSMessageBox::question(this,
  701. QTStr("Remux.FileExistsTitle"),
  702. message) != QMessageBox::Yes)
  703. proceedWithRemux = false;
  704. }
  705. if (!proceedWithRemux)
  706. return;
  707. // Set all jobs to "pending" first.
  708. queueModel->beginProcessing();
  709. ui->progressBar->setVisible(true);
  710. ui->buttonBox->button(QDialogButtonBox::Ok)
  711. ->setText(QTStr("Remux.Stop"));
  712. setAcceptDrops(false);
  713. remuxNextEntry();
  714. }
  715. void OBSRemux::AutoRemux(QString inFile, QString outFile)
  716. {
  717. if (inFile != "" && outFile != "" && autoRemux) {
  718. ui->progressBar->setVisible(true);
  719. emit remux(inFile, outFile);
  720. autoRemuxFile = outFile;
  721. }
  722. }
  723. void OBSRemux::remuxNextEntry()
  724. {
  725. worker->lastProgress = 0.f;
  726. QString inputPath, outputPath;
  727. if (queueModel->beginNextEntry(inputPath, outputPath)) {
  728. emit remux(inputPath, outputPath);
  729. } else {
  730. queueModel->autoRemux = autoRemux;
  731. queueModel->endProcessing();
  732. if (!autoRemux) {
  733. OBSMessageBox::information(
  734. this, QTStr("Remux.FinishedTitle"),
  735. queueModel->checkForErrors()
  736. ? QTStr("Remux.FinishedError")
  737. : QTStr("Remux.Finished"));
  738. }
  739. ui->progressBar->setVisible(autoRemux);
  740. ui->buttonBox->button(QDialogButtonBox::Ok)
  741. ->setText(QTStr("Remux.Remux"));
  742. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  743. ->setEnabled(true);
  744. ui->buttonBox->button(QDialogButtonBox::Reset)
  745. ->setEnabled(queueModel->canClearFinished());
  746. setAcceptDrops(true);
  747. }
  748. }
  749. void OBSRemux::closeEvent(QCloseEvent *event)
  750. {
  751. if (!stopRemux())
  752. event->ignore();
  753. else
  754. QDialog::closeEvent(event);
  755. }
  756. void OBSRemux::reject()
  757. {
  758. if (!stopRemux())
  759. return;
  760. QDialog::reject();
  761. }
  762. void OBSRemux::updateProgress(float percent)
  763. {
  764. ui->progressBar->setValue(percent * 10);
  765. }
  766. void OBSRemux::remuxFinished(bool success)
  767. {
  768. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
  769. queueModel->finishEntry(success);
  770. if (autoRemux && autoRemuxFile != "") {
  771. QTimer::singleShot(3000, this, SLOT(close()));
  772. OBSBasic *main = OBSBasic::Get();
  773. main->ShowStatusBarMessage(
  774. QTStr("Basic.StatusBar.AutoRemuxedTo")
  775. .arg(autoRemuxFile));
  776. }
  777. remuxNextEntry();
  778. }
  779. void OBSRemux::clearFinished()
  780. {
  781. queueModel->clearFinished();
  782. }
  783. void OBSRemux::clearAll()
  784. {
  785. queueModel->clearAll();
  786. }
  787. /**********************************************************
  788. Worker thread - Executes the libobs remux operation as a
  789. background process.
  790. **********************************************************/
  791. void RemuxWorker::UpdateProgress(float percent)
  792. {
  793. if (abs(lastProgress - percent) < 0.1f)
  794. return;
  795. emit updateProgress(percent);
  796. lastProgress = percent;
  797. }
  798. void RemuxWorker::remux(const QString &source, const QString &target)
  799. {
  800. isWorking = true;
  801. auto callback = [](void *data, float percent) {
  802. RemuxWorker *rw = static_cast<RemuxWorker *>(data);
  803. QMutexLocker lock(&rw->updateMutex);
  804. rw->UpdateProgress(percent);
  805. return rw->isWorking;
  806. };
  807. bool stopped = false;
  808. bool success = false;
  809. media_remux_job_t mr_job = nullptr;
  810. if (media_remux_job_create(&mr_job, QT_TO_UTF8(source),
  811. QT_TO_UTF8(target))) {
  812. success = media_remux_job_process(mr_job, callback, this);
  813. media_remux_job_destroy(mr_job);
  814. stopped = !isWorking;
  815. }
  816. isWorking = false;
  817. emit remuxFinished(!stopped && success);
  818. }