RemuxEntryPathItemDelegate.cpp 25 KB

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