window-remux.cpp 26 KB

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