2017-06-04 01:04:42 +02:00
|
|
|
#include "recordingcontroller.hpp"
|
|
|
|
#include <QImage>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <iostream>
|
|
|
|
#include <mainwindow.hpp>
|
|
|
|
#include <screenareaselector/screenareaselector.hpp>
|
|
|
|
#include <screenshotutil.hpp>
|
|
|
|
#include <settings.hpp>
|
|
|
|
#include <stdio.h>
|
2017-06-04 22:58:29 +02:00
|
|
|
#include <uploaders/uploadersingleton.hpp>
|
2017-06-04 01:04:42 +02:00
|
|
|
#include <worker/worker.hpp>
|
|
|
|
|
|
|
|
RecordingController::RecordingController() : timer(this) {
|
|
|
|
connect(&timer, &QTimer::timeout, this, &RecordingController::timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecordingController::isRunning() {
|
2017-06-06 12:51:16 +02:00
|
|
|
return preview;
|
2017-06-04 01:04:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RecordingController::start(RecordingContext *context) {
|
|
|
|
if (isRunning()) return false;
|
|
|
|
if (_context) delete _context;
|
|
|
|
_context = context;
|
|
|
|
ScreenAreaSelector *sel = new ScreenAreaSelector;
|
|
|
|
connect(sel, &ScreenAreaSelector::selectedArea, this, &RecordingController::startWithArea);
|
2017-06-18 19:04:25 +02:00
|
|
|
connect(this, &RecordingController::ended, sel, &ScreenAreaSelector::deleteLater);
|
2017-06-04 01:04:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecordingController::end() {
|
2017-06-18 19:04:25 +02:00
|
|
|
emit ended();
|
2017-06-04 01:04:42 +02:00
|
|
|
if (!isRunning()) return false;
|
|
|
|
area = QRect();
|
2017-06-06 12:51:16 +02:00
|
|
|
if (preview) {
|
|
|
|
preview->close();
|
|
|
|
preview->deleteLater();
|
|
|
|
}
|
|
|
|
|
2017-06-04 01:04:42 +02:00
|
|
|
preview = 0;
|
2017-06-06 01:26:26 +02:00
|
|
|
WorkerContext *c = new WorkerContext;
|
2017-06-06 17:05:34 +02:00
|
|
|
c->consumer = [&](QImage) {
|
|
|
|
_QueueContext contx;
|
2017-07-01 22:34:11 +02:00
|
|
|
contx.file = _context->finalizer();
|
2017-06-06 17:05:34 +02:00
|
|
|
contx.format = _context->anotherFormat;
|
2017-06-13 18:23:33 +02:00
|
|
|
contx.postUploadTask = _context->postUploadTask;
|
2017-06-06 17:05:34 +02:00
|
|
|
queue(contx);
|
|
|
|
};
|
2017-06-06 01:26:26 +02:00
|
|
|
c->targetFormat = QImage::Format_Alpha8;
|
|
|
|
c->pixmap = QPixmap(0, 0);
|
2017-06-06 12:51:16 +02:00
|
|
|
Worker::queue(c);
|
2017-06-06 01:26:26 +02:00
|
|
|
|
2017-06-04 01:04:42 +02:00
|
|
|
frame = 0;
|
|
|
|
time = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:05:34 +02:00
|
|
|
void RecordingController::queue(_QueueContext arr) {
|
2017-06-06 01:26:26 +02:00
|
|
|
QMutexLocker l(&lock);
|
|
|
|
uploadQueue.enqueue(arr);
|
|
|
|
}
|
|
|
|
|
2017-06-04 01:04:42 +02:00
|
|
|
void RecordingController::timeout() {
|
|
|
|
if (isRunning()) {
|
2017-06-13 00:38:32 +02:00
|
|
|
if (!_context->validator(area.size())) {
|
2017-06-06 12:51:16 +02:00
|
|
|
if (preview) {
|
|
|
|
preview->close();
|
|
|
|
preview->deleteLater();
|
|
|
|
}
|
2017-06-06 01:26:26 +02:00
|
|
|
frame = 0;
|
|
|
|
time = 0;
|
|
|
|
preview = 0;
|
|
|
|
area = QRect();
|
2017-06-17 17:32:47 +02:00
|
|
|
timer.stop();
|
2017-06-06 12:51:16 +02:00
|
|
|
return;
|
2017-06-06 01:26:26 +02:00
|
|
|
}
|
2017-06-04 01:04:42 +02:00
|
|
|
time++;
|
|
|
|
int localTime = time * timer.interval() - 3000;
|
|
|
|
if (localTime > 0) {
|
2017-07-02 20:51:15 +02:00
|
|
|
QPixmap pp = screenshotutil::fullscreenArea(settings::settings().value("captureCursor", true).toBool(),
|
|
|
|
area.x(), area.y(), area.width(), area.height());
|
2017-06-04 01:04:42 +02:00
|
|
|
WorkerContext *context = new WorkerContext;
|
|
|
|
context->consumer = _context->consumer;
|
|
|
|
context->targetFormat = _context->format;
|
2017-07-02 20:51:15 +02:00
|
|
|
context->pixmap = pp;
|
2017-06-04 01:04:42 +02:00
|
|
|
frame++;
|
2017-07-02 20:51:15 +02:00
|
|
|
preview->setPixmap(pp);
|
2017-06-04 01:04:42 +02:00
|
|
|
Worker::queue(context);
|
|
|
|
}
|
|
|
|
long second = localTime / 1000 % 60;
|
|
|
|
long minute = localTime / 60000;
|
2017-06-06 12:51:16 +02:00
|
|
|
if (isRunning())
|
|
|
|
preview->setTime(QString("%1:%2").arg(QString::number(minute)).arg(QString::number(second)), frame);
|
2017-06-06 01:26:26 +02:00
|
|
|
} else {
|
|
|
|
QMutexLocker l(&lock);
|
2017-06-06 17:05:34 +02:00
|
|
|
if (!uploadQueue.isEmpty()) {
|
|
|
|
auto a = uploadQueue.dequeue();
|
2017-07-01 22:34:11 +02:00
|
|
|
if (!a.file.isEmpty()) {
|
|
|
|
QFile f(a.file);
|
|
|
|
UploaderSingleton::inst().upload(f, a.format);
|
|
|
|
}
|
2017-06-13 18:23:33 +02:00
|
|
|
if (a.postUploadTask) a.postUploadTask();
|
2017-06-06 17:05:34 +02:00
|
|
|
}
|
2017-06-04 01:04:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordingController::startWithArea(QRect newArea) {
|
|
|
|
area = newArea;
|
|
|
|
preview = new RecordingPreview(newArea);
|
|
|
|
timer.start(1000 / settings::settings().value("recording/framerate", 30).toInt());
|
|
|
|
}
|