window-remux.cpp 25 KB

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