window-remux.cpp 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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, &QLineEdit::editingFinished, this,
  97. &RemuxEntryPathItemDelegate::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),
  580. &QPushButton::clicked, this, &OBSRemux::beginRemux);
  581. connect(ui->buttonBox->button(QDialogButtonBox::Reset),
  582. &QPushButton::clicked, this, &OBSRemux::clearFinished);
  583. connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults),
  584. &QPushButton::clicked, this, &OBSRemux::clearAll);
  585. connect(ui->buttonBox->button(QDialogButtonBox::Close),
  586. &QPushButton::clicked, this, &OBSRemux::close);
  587. worker->moveToThread(&remuxer);
  588. remuxer.start();
  589. connect(worker.data(), &RemuxWorker::updateProgress, this,
  590. &OBSRemux::updateProgress);
  591. connect(&remuxer, &QThread::finished, worker.data(),
  592. &QObject::deleteLater);
  593. connect(worker.data(), &RemuxWorker::remuxFinished, this,
  594. &OBSRemux::remuxFinished);
  595. connect(this, &OBSRemux::remux, worker.data(), &RemuxWorker::remux);
  596. connect(queueModel.data(), &RemuxQueueModel::rowsInserted, this,
  597. &OBSRemux::rowCountChanged);
  598. connect(queueModel.data(), &RemuxQueueModel::rowsRemoved, this,
  599. &OBSRemux::rowCountChanged);
  600. QModelIndex index = queueModel->createIndex(0, 1);
  601. QMetaObject::invokeMethod(ui->tableView, "setCurrentIndex",
  602. Qt::QueuedConnection,
  603. Q_ARG(const QModelIndex &, index));
  604. }
  605. bool OBSRemux::stopRemux()
  606. {
  607. if (!worker->isWorking)
  608. return true;
  609. // By locking the worker thread's mutex, we ensure that its
  610. // update poll will be blocked as long as we're in here with
  611. // the popup open.
  612. QMutexLocker lock(&worker->updateMutex);
  613. bool exit = false;
  614. if (QMessageBox::critical(nullptr, QTStr("Remux.ExitUnfinishedTitle"),
  615. QTStr("Remux.ExitUnfinished"),
  616. QMessageBox::Yes | QMessageBox::No,
  617. QMessageBox::No) == QMessageBox::Yes) {
  618. exit = true;
  619. }
  620. if (exit) {
  621. // Inform the worker it should no longer be
  622. // working. It will interrupt accordingly in
  623. // its next update callback.
  624. worker->isWorking = false;
  625. }
  626. return exit;
  627. }
  628. OBSRemux::~OBSRemux()
  629. {
  630. stopRemux();
  631. remuxer.quit();
  632. remuxer.wait();
  633. }
  634. void OBSRemux::rowCountChanged(const QModelIndex &, int, int)
  635. {
  636. // See if there are still any rows ready to remux. Change
  637. // the state of the "go" button accordingly.
  638. // There must be more than one row, since there will always be
  639. // at least one row for the empty insertion point.
  640. if (queueModel->rowCount() > 1) {
  641. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
  642. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  643. ->setEnabled(true);
  644. ui->buttonBox->button(QDialogButtonBox::Reset)
  645. ->setEnabled(queueModel->canClearFinished());
  646. } else {
  647. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
  648. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  649. ->setEnabled(false);
  650. ui->buttonBox->button(QDialogButtonBox::Reset)
  651. ->setEnabled(false);
  652. }
  653. }
  654. void OBSRemux::dropEvent(QDropEvent *ev)
  655. {
  656. QStringList urlList;
  657. for (QUrl url : ev->mimeData()->urls()) {
  658. QFileInfo fileInfo(url.toLocalFile());
  659. if (fileInfo.isDir()) {
  660. QStringList directoryFilter;
  661. directoryFilter << "*.flv"
  662. << "*.mp4"
  663. << "*.mov"
  664. << "*.mkv"
  665. << "*.ts"
  666. << "*.m3u8";
  667. QDirIterator dirIter(fileInfo.absoluteFilePath(),
  668. directoryFilter, QDir::Files,
  669. QDirIterator::Subdirectories);
  670. while (dirIter.hasNext()) {
  671. urlList.append(dirIter.next());
  672. }
  673. } else {
  674. urlList.append(fileInfo.canonicalFilePath());
  675. }
  676. }
  677. if (urlList.empty()) {
  678. QMessageBox::information(nullptr,
  679. QTStr("Remux.NoFilesAddedTitle"),
  680. QTStr("Remux.NoFilesAdded"),
  681. QMessageBox::Ok);
  682. } else if (!autoRemux) {
  683. QModelIndex insertIndex =
  684. queueModel->index(queueModel->rowCount() - 1,
  685. RemuxEntryColumn::InputPath);
  686. queueModel->setData(insertIndex, urlList,
  687. RemuxEntryRole::NewPathsToProcessRole);
  688. }
  689. }
  690. void OBSRemux::dragEnterEvent(QDragEnterEvent *ev)
  691. {
  692. if (ev->mimeData()->hasUrls() && !worker->isWorking)
  693. ev->accept();
  694. }
  695. void OBSRemux::beginRemux()
  696. {
  697. if (worker->isWorking) {
  698. stopRemux();
  699. return;
  700. }
  701. bool proceedWithRemux = true;
  702. QFileInfoList overwriteFiles = queueModel->checkForOverwrites();
  703. if (!overwriteFiles.empty()) {
  704. QString message = QTStr("Remux.FileExists");
  705. message += "\n\n";
  706. for (QFileInfo fileInfo : overwriteFiles)
  707. message += fileInfo.canonicalFilePath() + "\n";
  708. if (OBSMessageBox::question(this,
  709. QTStr("Remux.FileExistsTitle"),
  710. message) != QMessageBox::Yes)
  711. proceedWithRemux = false;
  712. }
  713. if (!proceedWithRemux)
  714. return;
  715. // Set all jobs to "pending" first.
  716. queueModel->beginProcessing();
  717. ui->progressBar->setVisible(true);
  718. ui->buttonBox->button(QDialogButtonBox::Ok)
  719. ->setText(QTStr("Remux.Stop"));
  720. setAcceptDrops(false);
  721. remuxNextEntry();
  722. }
  723. void OBSRemux::AutoRemux(QString inFile, QString outFile)
  724. {
  725. if (inFile != "" && outFile != "" && autoRemux) {
  726. ui->progressBar->setVisible(true);
  727. emit remux(inFile, outFile);
  728. autoRemuxFile = outFile;
  729. }
  730. }
  731. void OBSRemux::remuxNextEntry()
  732. {
  733. worker->lastProgress = 0.f;
  734. QString inputPath, outputPath;
  735. if (queueModel->beginNextEntry(inputPath, outputPath)) {
  736. emit remux(inputPath, outputPath);
  737. } else {
  738. queueModel->autoRemux = autoRemux;
  739. queueModel->endProcessing();
  740. if (!autoRemux) {
  741. OBSMessageBox::information(
  742. this, QTStr("Remux.FinishedTitle"),
  743. queueModel->checkForErrors()
  744. ? QTStr("Remux.FinishedError")
  745. : QTStr("Remux.Finished"));
  746. }
  747. ui->progressBar->setVisible(autoRemux);
  748. ui->buttonBox->button(QDialogButtonBox::Ok)
  749. ->setText(QTStr("Remux.Remux"));
  750. ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)
  751. ->setEnabled(true);
  752. ui->buttonBox->button(QDialogButtonBox::Reset)
  753. ->setEnabled(queueModel->canClearFinished());
  754. setAcceptDrops(true);
  755. }
  756. }
  757. void OBSRemux::closeEvent(QCloseEvent *event)
  758. {
  759. if (!stopRemux())
  760. event->ignore();
  761. else
  762. QDialog::closeEvent(event);
  763. }
  764. void OBSRemux::reject()
  765. {
  766. if (!stopRemux())
  767. return;
  768. QDialog::reject();
  769. }
  770. void OBSRemux::updateProgress(float percent)
  771. {
  772. ui->progressBar->setValue(percent * 10);
  773. }
  774. void OBSRemux::remuxFinished(bool success)
  775. {
  776. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
  777. queueModel->finishEntry(success);
  778. if (autoRemux && autoRemuxFile != "") {
  779. QTimer::singleShot(3000, this, &OBSRemux::close);
  780. OBSBasic *main = OBSBasic::Get();
  781. main->ShowStatusBarMessage(
  782. QTStr("Basic.StatusBar.AutoRemuxedTo")
  783. .arg(autoRemuxFile));
  784. }
  785. remuxNextEntry();
  786. }
  787. void OBSRemux::clearFinished()
  788. {
  789. queueModel->clearFinished();
  790. }
  791. void OBSRemux::clearAll()
  792. {
  793. queueModel->clearAll();
  794. }
  795. /**********************************************************
  796. Worker thread - Executes the libobs remux operation as a
  797. background process.
  798. **********************************************************/
  799. void RemuxWorker::UpdateProgress(float percent)
  800. {
  801. if (abs(lastProgress - percent) < 0.1f)
  802. return;
  803. emit updateProgress(percent);
  804. lastProgress = percent;
  805. }
  806. void RemuxWorker::remux(const QString &source, const QString &target)
  807. {
  808. isWorking = true;
  809. auto callback = [](void *data, float percent) {
  810. RemuxWorker *rw = static_cast<RemuxWorker *>(data);
  811. QMutexLocker lock(&rw->updateMutex);
  812. rw->UpdateProgress(percent);
  813. return rw->isWorking;
  814. };
  815. bool stopped = false;
  816. bool success = false;
  817. media_remux_job_t mr_job = nullptr;
  818. if (media_remux_job_create(&mr_job, QT_TO_UTF8(source),
  819. QT_TO_UTF8(target))) {
  820. success = media_remux_job_process(mr_job, callback, this);
  821. media_remux_job_destroy(mr_job);
  822. stopped = !isWorking;
  823. }
  824. isWorking = false;
  825. emit remuxFinished(!stopped && success);
  826. }