1
0

window-remux.cpp 26 KB

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