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