window-remux.cpp 25 KB

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