2017-06-04 22:58:29 +02:00
|
|
|
#include "recordingformats.hpp"
|
|
|
|
|
2017-06-06 01:26:26 +02:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QDateTime>
|
2017-06-06 12:51:16 +02:00
|
|
|
#include <QDebug>
|
2017-06-06 01:26:26 +02:00
|
|
|
#include <QDir>
|
2017-06-04 22:58:29 +02:00
|
|
|
#include <QFile>
|
2017-06-06 01:26:26 +02:00
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QTimer>
|
2017-06-06 17:05:34 +02:00
|
|
|
#include <formats.hpp>
|
2017-06-13 14:15:37 +02:00
|
|
|
#include <notifications.hpp>
|
2017-06-06 01:26:26 +02:00
|
|
|
#include <platformbackend.hpp>
|
|
|
|
#include <settings.hpp>
|
|
|
|
#include <time.h>
|
2017-06-06 12:51:16 +02:00
|
|
|
#include <unistd.h>
|
2017-06-04 22:58:29 +02:00
|
|
|
|
2017-06-17 17:32:47 +02:00
|
|
|
#include <recording/encoders/encodersettings.hpp>
|
|
|
|
|
2017-06-06 17:05:34 +02:00
|
|
|
RecordingFormats::RecordingFormats(formats::Recording f) {
|
2017-06-13 01:15:57 +02:00
|
|
|
QString tmp = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
|
|
|
|
if (tmp.isEmpty()) {
|
2017-06-13 00:38:32 +02:00
|
|
|
validator = [](QSize) { return false; };
|
2017-06-06 01:26:26 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-13 01:15:57 +02:00
|
|
|
tmpDir = QDir(tmp);
|
2017-06-06 01:26:26 +02:00
|
|
|
QString name
|
|
|
|
= QString("KShareTemp-") + QString::number(PlatformBackend::inst().pid()) + "-" + QTime::currentTime().toString();
|
|
|
|
tmpDir.mkdir(name);
|
|
|
|
tmpDir.cd(name);
|
2017-06-13 01:15:57 +02:00
|
|
|
iFormat = QImage::Format_RGB888;
|
|
|
|
path = tmpDir.absoluteFilePath("res." + formats::recordingFormatName(f).toLower());
|
|
|
|
finalizer = [&] {
|
|
|
|
delete enc;
|
2017-06-13 14:15:37 +02:00
|
|
|
if (interrupt) {
|
|
|
|
tmpDir.removeRecursively();
|
2017-06-13 17:02:35 +02:00
|
|
|
return QByteArray();
|
2017-06-13 14:15:37 +02:00
|
|
|
}
|
2017-06-13 01:15:57 +02:00
|
|
|
QFile res(path);
|
|
|
|
if (!res.open(QFile::ReadOnly)) {
|
2017-06-22 17:41:29 +02:00
|
|
|
qCritical().noquote() << "Could not open resulting file: " << res.errorString();
|
2017-06-13 01:15:57 +02:00
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
QByteArray data = res.readAll();
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
validator = [&](QSize s) {
|
|
|
|
if (!enc) {
|
2017-06-13 14:15:37 +02:00
|
|
|
try {
|
2017-06-17 17:32:47 +02:00
|
|
|
auto es = EncoderSettings::inst().getSettings();
|
|
|
|
enc = new Encoder(path, s, es);
|
|
|
|
delete es;
|
2017-06-13 14:15:37 +02:00
|
|
|
if (!enc->isRunning()) {
|
|
|
|
delete enc;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (std::runtime_error e) {
|
2017-06-22 17:41:29 +02:00
|
|
|
// notifications::notify("KShare Video Encoder Error", e.what(),
|
|
|
|
// QSystemTrayIcon::Critical);
|
2017-06-13 14:15:37 +02:00
|
|
|
qCritical() << "Encoder error: " << e.what();
|
|
|
|
interrupt = true;
|
2017-06-13 01:15:57 +02:00
|
|
|
delete enc;
|
|
|
|
return false;
|
2017-06-06 12:51:16 +02:00
|
|
|
}
|
2017-06-13 01:15:57 +02:00
|
|
|
}
|
2017-06-17 17:32:47 +02:00
|
|
|
return !interrupt;
|
2017-06-13 01:15:57 +02:00
|
|
|
};
|
2017-06-13 14:15:37 +02:00
|
|
|
consumer = [&](QImage img) {
|
2017-06-13 18:23:33 +02:00
|
|
|
if (!interrupt) try {
|
2017-06-13 14:15:37 +02:00
|
|
|
enc->addFrame(img);
|
|
|
|
} catch (std::runtime_error e) {
|
2017-06-22 17:41:29 +02:00
|
|
|
// notifications::notify("KShare Video Encoder Error", e.what(),
|
|
|
|
// QSystemTrayIcon::Critical);
|
2017-06-13 14:15:37 +02:00
|
|
|
qCritical() << "Encoder error: " << e.what();
|
|
|
|
interrupt = true;
|
|
|
|
}
|
|
|
|
};
|
2017-06-13 18:23:33 +02:00
|
|
|
postUploadTask = [&] {
|
|
|
|
tmpDir.removeRecursively();
|
|
|
|
QScopedPointer<RecordingFormats> th(this);
|
|
|
|
};
|
2017-06-13 01:15:57 +02:00
|
|
|
anotherFormat = formats::recordingFormatName(f);
|
2017-06-04 22:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(QImage)> RecordingFormats::getConsumer() {
|
|
|
|
return consumer;
|
|
|
|
}
|
|
|
|
|
2017-06-04 23:04:35 +02:00
|
|
|
std::function<QByteArray()> RecordingFormats::getFinalizer() {
|
2017-06-04 22:58:29 +02:00
|
|
|
return finalizer;
|
|
|
|
}
|
|
|
|
|
2017-06-13 18:23:33 +02:00
|
|
|
std::function<void()> RecordingFormats::getPostUploadTask() {
|
|
|
|
return postUploadTask;
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:38:32 +02:00
|
|
|
std::function<bool(QSize)> RecordingFormats::getValidator() {
|
2017-06-06 01:26:26 +02:00
|
|
|
return validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage::Format RecordingFormats::getFormat() {
|
|
|
|
return iFormat;
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:05:34 +02:00
|
|
|
QString RecordingFormats::getAnotherFormat() {
|
|
|
|
return anotherFormat;
|
2017-06-04 22:58:29 +02:00
|
|
|
}
|