2016-06-24 10:37:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-06-24 10:37:29 +00:00
|
|
|
*/
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/clip/media_clip_reader.h"
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_document.h"
|
2017-08-15 15:59:40 +00:00
|
|
|
#include "storage/file_download.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/clip/media_clip_ffmpeg.h"
|
|
|
|
#include "media/clip/media_clip_check_streaming.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "mainwidget.h"
|
|
|
|
#include "mainwindow.h"
|
2017-08-15 15:59:40 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
#include <QtCore/QAbstractEventDispatcher>
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavutil/opt.h>
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
QVector<QThread*> threads;
|
|
|
|
QVector<Manager*> managers;
|
|
|
|
|
2017-03-09 09:47:45 +00:00
|
|
|
QImage PrepareFrameImage(const FrameRequest &request, const QImage &original, bool hasAlpha, QImage &cache) {
|
|
|
|
auto needResize = (original.width() != request.framew) || (original.height() != request.frameh);
|
|
|
|
auto needOuterFill = (request.outerw != request.framew) || (request.outerh != request.frameh);
|
|
|
|
auto needRounding = (request.radius != ImageRoundRadius::None);
|
|
|
|
if (!needResize && !needOuterFill && !hasAlpha && !needRounding) {
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto factor = request.factor;
|
|
|
|
auto needNewCache = (cache.width() != request.outerw || cache.height() != request.outerh);
|
|
|
|
if (needNewCache) {
|
|
|
|
cache = QImage(request.outerw, request.outerh, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
cache.setDevicePixelRatio(factor);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Painter p(&cache);
|
|
|
|
if (needNewCache) {
|
|
|
|
if (request.framew < request.outerw) {
|
|
|
|
p.fillRect(0, 0, (request.outerw - request.framew) / (2 * factor), cache.height() / factor, st::imageBg);
|
|
|
|
p.fillRect((request.outerw - request.framew) / (2 * factor) + (request.framew / factor), 0, (cache.width() / factor) - ((request.outerw - request.framew) / (2 * factor) + (request.framew / factor)), cache.height() / factor, st::imageBg);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
2017-03-09 09:47:45 +00:00
|
|
|
if (request.frameh < request.outerh) {
|
|
|
|
p.fillRect(qMax(0, (request.outerw - request.framew) / (2 * factor)), 0, qMin(cache.width(), request.framew) / factor, (request.outerh - request.frameh) / (2 * factor), st::imageBg);
|
|
|
|
p.fillRect(qMax(0, (request.outerw - request.framew) / (2 * factor)), (request.outerh - request.frameh) / (2 * factor) + (request.frameh / factor), qMin(cache.width(), request.framew) / factor, (cache.height() / factor) - ((request.outerh - request.frameh) / (2 * factor) + (request.frameh / factor)), st::imageBg);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 09:47:45 +00:00
|
|
|
if (hasAlpha) {
|
|
|
|
p.fillRect(qMax(0, (request.outerw - request.framew) / (2 * factor)), qMax(0, (request.outerh - request.frameh) / (2 * factor)), qMin(cache.width(), request.framew) / factor, qMin(cache.height(), request.frameh) / factor, st::imageBgTransparent);
|
|
|
|
}
|
|
|
|
auto position = QPoint((request.outerw - request.framew) / (2 * factor), (request.outerh - request.frameh) / (2 * factor));
|
|
|
|
if (needResize) {
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
|
|
|
auto dst = QRect(position, QSize(request.framew / factor, request.frameh / factor));
|
|
|
|
auto src = QRect(0, 0, original.width(), original.height());
|
|
|
|
p.drawImage(dst, original, src, Qt::ColorOnly);
|
|
|
|
} else {
|
|
|
|
p.drawImage(position, original);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 09:47:45 +00:00
|
|
|
if (needRounding) {
|
|
|
|
Images::prepareRound(cache, request.radius, request.corners);
|
|
|
|
}
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PrepareFrame(const FrameRequest &request, const QImage &original, bool hasAlpha, QImage &cache) {
|
|
|
|
return QPixmap::fromImage(PrepareFrameImage(request, original, hasAlpha, cache), Qt::ColorOnly);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
Reader::Reader(const QString &filepath, Callback &&callback, Mode mode, crl::time seekMs)
|
2017-02-21 13:45:56 +00:00
|
|
|
: _callback(std::move(callback))
|
2016-07-12 11:38:16 +00:00
|
|
|
, _mode(mode)
|
2016-07-13 17:34:57 +00:00
|
|
|
, _seekPositionMs(seekMs) {
|
2017-05-18 20:18:59 +00:00
|
|
|
init(FileLocation(filepath), QByteArray());
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
Reader::Reader(not_null<DocumentData*> document, FullMsgId msgId, Callback &&callback, Mode mode, crl::time seekMs)
|
2017-05-18 20:18:59 +00:00
|
|
|
: _callback(std::move(callback))
|
|
|
|
, _mode(mode)
|
2019-02-28 21:03:25 +00:00
|
|
|
, _audioMsgId(
|
|
|
|
document,
|
|
|
|
msgId,
|
|
|
|
(mode == Mode::Video) ? AudioMsgId::CreateExternalPlayId() : 0)
|
2017-05-18 20:18:59 +00:00
|
|
|
, _seekPositionMs(seekMs) {
|
|
|
|
init(document->location(), document->data());
|
|
|
|
}
|
|
|
|
|
2019-04-08 18:13:18 +00:00
|
|
|
Reader::Reader(const QByteArray &data, Callback &&callback, Mode mode, crl::time seekMs)
|
|
|
|
: _callback(std::move(callback))
|
|
|
|
, _mode(mode)
|
|
|
|
, _seekPositionMs(seekMs) {
|
|
|
|
init(FileLocation(QString()), data);
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:18:59 +00:00
|
|
|
void Reader::init(const FileLocation &location, const QByteArray &data) {
|
2016-06-24 10:37:29 +00:00
|
|
|
if (threads.size() < ClipThreadsCount) {
|
|
|
|
_threadIndex = threads.size();
|
|
|
|
threads.push_back(new QThread());
|
|
|
|
managers.push_back(new Manager(threads.back()));
|
|
|
|
threads.back()->start();
|
|
|
|
} else {
|
|
|
|
_threadIndex = int32(rand_value<uint32>() % threads.size());
|
|
|
|
int32 loadLevel = 0x7FFFFFFF;
|
|
|
|
for (int32 i = 0, l = threads.size(); i < l; ++i) {
|
|
|
|
int32 level = managers.at(i)->loadLevel();
|
|
|
|
if (level < loadLevel) {
|
|
|
|
_threadIndex = i;
|
|
|
|
loadLevel = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
managers.at(_threadIndex)->append(this, location, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader::Frame *Reader::frameToShow(int32 *index) const { // 0 means not ready
|
2016-07-26 12:09:40 +00:00
|
|
|
int step = _step.loadAcquire(), i;
|
2016-06-24 10:37:29 +00:00
|
|
|
if (step == WaitingForDimensionsStep) {
|
|
|
|
if (index) *index = 0;
|
2016-07-12 12:28:07 +00:00
|
|
|
return nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
} else if (step == WaitingForRequestStep) {
|
|
|
|
i = 0;
|
|
|
|
} else if (step == WaitingForFirstFrameStep) {
|
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
i = (step / 2) % 3;
|
|
|
|
}
|
|
|
|
if (index) *index = i;
|
|
|
|
return _frames + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader::Frame *Reader::frameToWrite(int32 *index) const { // 0 means not ready
|
|
|
|
int32 step = _step.loadAcquire(), i;
|
|
|
|
if (step == WaitingForDimensionsStep) {
|
|
|
|
i = 0;
|
|
|
|
} else if (step == WaitingForRequestStep) {
|
|
|
|
if (index) *index = 0;
|
2016-07-12 12:28:07 +00:00
|
|
|
return nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
} else if (step == WaitingForFirstFrameStep) {
|
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
i = ((step + 2) / 2) % 3;
|
|
|
|
}
|
|
|
|
if (index) *index = i;
|
|
|
|
return _frames + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader::Frame *Reader::frameToWriteNext(bool checkNotWriting, int32 *index) const {
|
|
|
|
int32 step = _step.loadAcquire(), i;
|
|
|
|
if (step == WaitingForDimensionsStep || step == WaitingForRequestStep || (checkNotWriting && (step % 2))) {
|
|
|
|
if (index) *index = 0;
|
2016-07-12 12:28:07 +00:00
|
|
|
return nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
i = ((step + 4) / 2) % 3;
|
|
|
|
if (index) *index = i;
|
|
|
|
return _frames + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::moveToNextShow() const {
|
|
|
|
int32 step = _step.loadAcquire();
|
|
|
|
if (step == WaitingForDimensionsStep) {
|
|
|
|
} else if (step == WaitingForRequestStep) {
|
|
|
|
_step.storeRelease(WaitingForFirstFrameStep);
|
|
|
|
} else if (step == WaitingForFirstFrameStep) {
|
|
|
|
} else if (!(step % 2)) {
|
|
|
|
_step.storeRelease(step + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::moveToNextWrite() const {
|
|
|
|
int32 step = _step.loadAcquire();
|
|
|
|
if (step == WaitingForDimensionsStep) {
|
|
|
|
_step.storeRelease(WaitingForRequestStep);
|
|
|
|
} else if (step == WaitingForRequestStep) {
|
|
|
|
} else if (step == WaitingForFirstFrameStep) {
|
|
|
|
_step.storeRelease(0);
|
2016-07-13 11:24:31 +00:00
|
|
|
|
|
|
|
// Force paint the first frame so moveToNextShow() is called.
|
|
|
|
_frames[0].displayed.storeRelease(0);
|
2016-06-24 10:37:29 +00:00
|
|
|
} else if (step % 2) {
|
|
|
|
_step.storeRelease((step + 1) % 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:29:28 +00:00
|
|
|
void Reader::callback(Reader *reader, qint32 threadIndex, qint32 notification) {
|
|
|
|
// Check if reader is not deleted already
|
2016-09-26 12:09:59 +00:00
|
|
|
if (managers.size() > threadIndex && managers.at(threadIndex)->carries(reader) && reader->_callback) {
|
2019-04-02 10:29:28 +00:00
|
|
|
reader->_callback(Notification(notification));
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:50:25 +00:00
|
|
|
void Reader::start(int32 framew, int32 frameh, int32 outerw, int32 outerh, ImageRoundRadius radius, RectParts corners) {
|
2016-06-24 10:37:29 +00:00
|
|
|
if (managers.size() <= _threadIndex) error();
|
|
|
|
if (_state == State::Error) return;
|
|
|
|
|
|
|
|
if (_step.loadAcquire() == WaitingForRequestStep) {
|
|
|
|
int factor = cIntRetinaFactor();
|
|
|
|
FrameRequest request;
|
|
|
|
request.factor = factor;
|
|
|
|
request.framew = framew * factor;
|
|
|
|
request.frameh = frameh * factor;
|
|
|
|
request.outerw = outerw * factor;
|
|
|
|
request.outerh = outerh * factor;
|
2016-07-19 10:54:43 +00:00
|
|
|
request.radius = radius;
|
2016-11-21 20:26:54 +00:00
|
|
|
request.corners = corners;
|
2016-06-24 10:37:29 +00:00
|
|
|
_frames[0].request = _frames[1].request = _frames[2].request = request;
|
|
|
|
moveToNextShow();
|
|
|
|
managers.at(_threadIndex)->start(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
QPixmap Reader::current(int32 framew, int32 frameh, int32 outerw, int32 outerh, ImageRoundRadius radius, RectParts corners, crl::time ms) {
|
2017-05-26 12:20:11 +00:00
|
|
|
Expects(outerw > 0);
|
|
|
|
Expects(outerh > 0);
|
|
|
|
|
2016-07-26 12:09:40 +00:00
|
|
|
auto frame = frameToShow();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(frame != nullptr);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-11-12 15:02:19 +00:00
|
|
|
auto shouldBePaused = !ms;
|
|
|
|
if (!shouldBePaused) {
|
2016-06-24 10:37:29 +00:00
|
|
|
frame->displayed.storeRelease(1);
|
2016-07-13 11:24:31 +00:00
|
|
|
if (_autoPausedGif.loadAcquire()) {
|
|
|
|
_autoPausedGif.storeRelease(0);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (managers.size() <= _threadIndex) error();
|
|
|
|
if (_state != State::Error) {
|
|
|
|
managers.at(_threadIndex)->update(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-12 15:02:19 +00:00
|
|
|
frame->displayed.storeRelease(-1);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:02:19 +00:00
|
|
|
auto factor = cIntRetinaFactor();
|
2016-11-21 20:26:54 +00:00
|
|
|
if (frame->pix.width() == outerw * factor
|
|
|
|
&& frame->pix.height() == outerh * factor
|
|
|
|
&& frame->request.radius == radius
|
|
|
|
&& frame->request.corners == corners) {
|
2016-06-24 10:37:29 +00:00
|
|
|
moveToNextShow();
|
|
|
|
return frame->pix;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->request.framew = framew * factor;
|
|
|
|
frame->request.frameh = frameh * factor;
|
|
|
|
frame->request.outerw = outerw * factor;
|
|
|
|
frame->request.outerh = outerh * factor;
|
|
|
|
|
|
|
|
QImage cacheForResize;
|
|
|
|
frame->original.setDevicePixelRatio(factor);
|
|
|
|
frame->pix = QPixmap();
|
2017-03-09 09:47:45 +00:00
|
|
|
frame->pix = PrepareFrame(frame->request, frame->original, true, cacheForResize);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-11-21 20:26:54 +00:00
|
|
|
auto other = frameToWriteNext(true);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (other) other->request = frame->request;
|
|
|
|
|
|
|
|
moveToNextShow();
|
|
|
|
|
|
|
|
if (managers.size() <= _threadIndex) error();
|
|
|
|
if (_state != State::Error) {
|
|
|
|
managers.at(_threadIndex)->update(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame->pix;
|
|
|
|
}
|
|
|
|
|
2017-05-22 15:25:49 +00:00
|
|
|
QPixmap Reader::current() {
|
|
|
|
Expects(_mode == Mode::Video);
|
|
|
|
|
|
|
|
auto frame = frameToShow();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(frame != nullptr);
|
2017-05-22 15:25:49 +00:00
|
|
|
|
|
|
|
frame->displayed.storeRelease(1);
|
|
|
|
moveToNextShow();
|
|
|
|
return frame->pix;
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
bool Reader::ready() const {
|
|
|
|
if (_width && _height) return true;
|
|
|
|
|
2016-11-21 20:26:54 +00:00
|
|
|
auto frame = frameToShow();
|
2016-06-24 10:37:29 +00:00
|
|
|
if (frame) {
|
|
|
|
_width = frame->original.width();
|
|
|
|
_height = frame->original.height();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-12 12:28:07 +00:00
|
|
|
bool Reader::hasAudio() const {
|
|
|
|
return ready() ? _hasAudio : false;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time Reader::getPositionMs() const {
|
2016-07-12 12:28:07 +00:00
|
|
|
if (auto frame = frameToShow()) {
|
|
|
|
return frame->positionMs;
|
|
|
|
}
|
2016-07-13 17:34:57 +00:00
|
|
|
return _seekPositionMs;
|
2016-07-12 12:28:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time Reader::getDurationMs() const {
|
2016-07-12 12:28:07 +00:00
|
|
|
return ready() ? _durationMs : 0;
|
|
|
|
}
|
|
|
|
|
2016-07-13 11:24:31 +00:00
|
|
|
void Reader::pauseResumeVideo() {
|
|
|
|
if (managers.size() <= _threadIndex) error();
|
|
|
|
if (_state == State::Error) return;
|
|
|
|
|
|
|
|
_videoPauseRequest.storeRelease(1 - _videoPauseRequest.loadAcquire());
|
|
|
|
managers.at(_threadIndex)->start(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::videoPaused() const {
|
|
|
|
return _videoPauseRequest.loadAcquire() != 0;
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
int32 Reader::width() const {
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 Reader::height() const {
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
|
|
|
State Reader::state() const {
|
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::stop() {
|
|
|
|
if (managers.size() <= _threadIndex) error();
|
|
|
|
if (_state != State::Error) {
|
|
|
|
managers.at(_threadIndex)->stop(this);
|
|
|
|
_width = _height = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::error() {
|
|
|
|
_state = State::Error;
|
2016-07-13 17:34:57 +00:00
|
|
|
_private = nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:04:34 +00:00
|
|
|
void Reader::finished() {
|
|
|
|
_state = State::Finished;
|
2016-07-13 17:34:57 +00:00
|
|
|
_private = nullptr;
|
2016-07-12 18:04:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
Reader::~Reader() {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReaderPrivate {
|
|
|
|
public:
|
|
|
|
ReaderPrivate(Reader *reader, const FileLocation &location, const QByteArray &data) : _interface(reader)
|
2016-07-05 17:43:30 +00:00
|
|
|
, _mode(reader->mode())
|
2017-05-18 20:18:59 +00:00
|
|
|
, _audioMsgId(reader->audioMsgId())
|
2016-07-13 17:34:57 +00:00
|
|
|
, _seekPositionMs(reader->seekPositionMs())
|
2016-07-12 18:04:34 +00:00
|
|
|
, _data(data) {
|
|
|
|
if (_data.isEmpty()) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_location = std::make_unique<FileLocation>(location);
|
2016-07-12 18:04:34 +00:00
|
|
|
if (!_location->accessEnable()) {
|
|
|
|
error();
|
|
|
|
return;
|
|
|
|
}
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
_accessed = true;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
ProcessResult start(crl::time ms) {
|
2016-07-14 11:20:46 +00:00
|
|
|
if (!_implementation && !init()) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return error();
|
|
|
|
}
|
2019-02-16 10:29:55 +00:00
|
|
|
if (frame()->original.isNull()) {
|
2016-07-14 11:20:46 +00:00
|
|
|
auto readResult = _implementation->readFramesTill(-1, ms);
|
2016-07-30 10:03:44 +00:00
|
|
|
if (readResult == internal::ReaderImplementation::ReadResult::EndOfFile && _seekPositionMs > 0) {
|
2016-07-14 11:20:46 +00:00
|
|
|
// If seek was done to the end: try to read the first frame,
|
|
|
|
// get the frame size and return a black frame with that size.
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
auto firstFramePositionMs = crl::time(0);
|
2017-05-18 20:18:59 +00:00
|
|
|
auto reader = std::make_unique<internal::FFMpegReaderImplementation>(_location.get(), &_data, AudioMsgId());
|
2016-07-14 11:20:46 +00:00
|
|
|
if (reader->start(internal::ReaderImplementation::Mode::Normal, firstFramePositionMs)) {
|
|
|
|
auto firstFrameReadResult = reader->readFramesTill(-1, ms);
|
|
|
|
if (firstFrameReadResult == internal::ReaderImplementation::ReadResult::Success) {
|
|
|
|
if (reader->renderFrame(frame()->original, frame()->alpha, QSize())) {
|
|
|
|
frame()->original.fill(QColor(0, 0, 0));
|
|
|
|
|
|
|
|
frame()->positionMs = _seekPositionMs;
|
|
|
|
|
|
|
|
_width = frame()->original.width();
|
|
|
|
_height = frame()->original.height();
|
|
|
|
_durationMs = _implementation->durationMs();
|
|
|
|
_hasAudio = _implementation->hasAudio();
|
|
|
|
return ProcessResult::Started;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error();
|
|
|
|
} else if (readResult != internal::ReaderImplementation::ReadResult::Success) { // Read the first frame.
|
2016-06-24 10:37:29 +00:00
|
|
|
return error();
|
|
|
|
}
|
|
|
|
if (!_implementation->renderFrame(frame()->original, frame()->alpha, QSize())) {
|
|
|
|
return error();
|
|
|
|
}
|
2016-07-13 17:34:57 +00:00
|
|
|
frame()->positionMs = _implementation->frameRealTime();
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
_width = frame()->original.width();
|
|
|
|
_height = frame()->original.height();
|
2016-07-12 12:28:07 +00:00
|
|
|
_durationMs = _implementation->durationMs();
|
2016-07-12 18:04:34 +00:00
|
|
|
_hasAudio = _implementation->hasAudio();
|
2016-06-24 10:37:29 +00:00
|
|
|
return ProcessResult::Started;
|
|
|
|
}
|
|
|
|
return ProcessResult::Wait;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
ProcessResult process(crl::time ms) { // -1 - do nothing, 0 - update, 1 - reinit
|
2016-07-12 18:04:34 +00:00
|
|
|
if (_state == State::Error) {
|
|
|
|
return ProcessResult::Error;
|
|
|
|
} else if (_state == State::Finished) {
|
|
|
|
return ProcessResult::Finished;
|
|
|
|
}
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
if (!_request.valid()) {
|
|
|
|
return start(ms);
|
|
|
|
}
|
2016-07-13 11:24:31 +00:00
|
|
|
if (!_started) {
|
|
|
|
_started = true;
|
2017-05-21 13:16:39 +00:00
|
|
|
if (!_videoPausedAtMs && _hasAudio) {
|
|
|
|
Player::mixer()->resume(_audioMsgId, true);
|
2016-07-13 11:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-07-13 11:24:31 +00:00
|
|
|
if (!_autoPausedGif && !_videoPausedAtMs && ms >= _nextFrameWhen) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return ProcessResult::Repaint;
|
|
|
|
}
|
|
|
|
return ProcessResult::Wait;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
ProcessResult finishProcess(crl::time ms) {
|
2016-07-14 11:20:46 +00:00
|
|
|
auto frameMs = _seekPositionMs + ms - _animationStarted;
|
|
|
|
auto readResult = _implementation->readFramesTill(frameMs, ms);
|
2016-07-30 10:03:44 +00:00
|
|
|
if (readResult == internal::ReaderImplementation::ReadResult::EndOfFile) {
|
2017-05-21 13:16:39 +00:00
|
|
|
stop(Player::State::StoppedAtEnd);
|
2016-07-12 18:04:34 +00:00
|
|
|
_state = State::Finished;
|
|
|
|
return ProcessResult::Finished;
|
|
|
|
} else if (readResult == internal::ReaderImplementation::ReadResult::Error) {
|
2016-06-24 10:37:29 +00:00
|
|
|
return error();
|
|
|
|
}
|
2016-07-12 12:28:07 +00:00
|
|
|
_nextFramePositionMs = _implementation->frameRealTime();
|
2016-07-10 19:44:55 +00:00
|
|
|
_nextFrameWhen = _animationStarted + _implementation->framePresentationTime();
|
2016-12-01 19:20:33 +00:00
|
|
|
if (_nextFrameWhen > _seekPositionMs) {
|
2016-07-14 11:20:46 +00:00
|
|
|
_nextFrameWhen -= _seekPositionMs;
|
2016-07-13 17:34:57 +00:00
|
|
|
} else {
|
|
|
|
_nextFrameWhen = 1;
|
|
|
|
}
|
2016-07-10 19:44:55 +00:00
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
if (!renderFrame()) {
|
|
|
|
return error();
|
|
|
|
}
|
|
|
|
return ProcessResult::CopyFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool renderFrame() {
|
2019-02-16 10:29:55 +00:00
|
|
|
Expects(_request.valid());
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
if (!_implementation->renderFrame(frame()->original, frame()->alpha, QSize(_request.framew, _request.frameh))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
frame()->original.setDevicePixelRatio(_request.factor);
|
|
|
|
frame()->pix = QPixmap();
|
2017-03-09 09:47:45 +00:00
|
|
|
frame()->pix = PrepareFrame(_request, frame()->original, frame()->alpha, frame()->cache);
|
2016-06-24 10:37:29 +00:00
|
|
|
frame()->when = _nextFrameWhen;
|
2016-07-12 12:28:07 +00:00
|
|
|
frame()->positionMs = _nextFramePositionMs;
|
2016-06-24 10:37:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-14 11:20:46 +00:00
|
|
|
bool init() {
|
2017-08-15 15:59:40 +00:00
|
|
|
if (_data.isEmpty() && QFileInfo(_location->name()).size() <= Storage::kMaxAnimationInMemory) {
|
2016-06-24 10:37:29 +00:00
|
|
|
QFile f(_location->name());
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
_data = f.readAll();
|
|
|
|
if (f.error() != QFile::NoError) {
|
|
|
|
_data = QByteArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:18:59 +00:00
|
|
|
_implementation = std::make_unique<internal::FFMpegReaderImplementation>(_location.get(), &_data, _audioMsgId);
|
2016-07-05 17:43:30 +00:00
|
|
|
|
|
|
|
auto implementationMode = [this]() {
|
|
|
|
using ImplementationMode = internal::ReaderImplementation::Mode;
|
|
|
|
if (_mode == Reader::Mode::Gif) {
|
|
|
|
return ImplementationMode::Silent;
|
|
|
|
}
|
|
|
|
return ImplementationMode::Normal;
|
|
|
|
};
|
2016-07-14 11:20:46 +00:00
|
|
|
return _implementation->start(implementationMode(), _seekPositionMs);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void startedAt(crl::time ms) {
|
2016-07-10 19:44:55 +00:00
|
|
|
_animationStarted = _nextFrameWhen = ms;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void pauseVideo(crl::time ms) {
|
2016-07-13 11:24:31 +00:00
|
|
|
if (_videoPausedAtMs) return; // Paused already.
|
|
|
|
|
|
|
|
_videoPausedAtMs = ms;
|
2017-05-21 13:16:39 +00:00
|
|
|
if (_hasAudio) {
|
|
|
|
Player::mixer()->pause(_audioMsgId, true);
|
|
|
|
}
|
2016-07-13 11:24:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void resumeVideo(crl::time ms) {
|
2016-07-13 11:24:31 +00:00
|
|
|
if (!_videoPausedAtMs) return; // Not paused.
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
auto delta = ms - _videoPausedAtMs;
|
2016-07-13 11:24:31 +00:00
|
|
|
_animationStarted += delta;
|
|
|
|
_nextFrameWhen += delta;
|
|
|
|
|
|
|
|
_videoPausedAtMs = 0;
|
2017-05-21 13:16:39 +00:00
|
|
|
if (_hasAudio) {
|
|
|
|
Player::mixer()->resume(_audioMsgId, true);
|
|
|
|
}
|
2016-07-13 11:24:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
ProcessResult error() {
|
2017-05-21 13:16:39 +00:00
|
|
|
stop(Player::State::StoppedAtError);
|
2016-06-24 10:37:29 +00:00
|
|
|
_state = State::Error;
|
|
|
|
return ProcessResult::Error;
|
|
|
|
}
|
|
|
|
|
2017-05-21 13:16:39 +00:00
|
|
|
void stop(Player::State audioState) {
|
2016-07-05 17:43:30 +00:00
|
|
|
_implementation = nullptr;
|
2017-05-21 13:16:39 +00:00
|
|
|
if (_hasAudio) {
|
|
|
|
Player::mixer()->stop(_audioMsgId, audioState);
|
|
|
|
}
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
if (_location) {
|
|
|
|
if (_accessed) {
|
|
|
|
_location->accessDisable();
|
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
_location = nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
_accessed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ReaderPrivate() {
|
2017-05-21 13:16:39 +00:00
|
|
|
stop(Player::State::Stopped);
|
2016-06-24 10:37:29 +00:00
|
|
|
_data.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Reader *_interface;
|
|
|
|
State _state = State::Reading;
|
2016-07-05 17:43:30 +00:00
|
|
|
Reader::Mode _mode;
|
2017-05-18 20:18:59 +00:00
|
|
|
AudioMsgId _audioMsgId;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _seekPositionMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
QByteArray _data;
|
2017-02-21 13:45:56 +00:00
|
|
|
std::unique_ptr<FileLocation> _location;
|
2016-06-24 10:37:29 +00:00
|
|
|
bool _accessed = false;
|
|
|
|
|
|
|
|
QBuffer _buffer;
|
2017-02-21 13:45:56 +00:00
|
|
|
std::unique_ptr<internal::ReaderImplementation> _implementation;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
FrameRequest _request;
|
|
|
|
struct Frame {
|
|
|
|
QPixmap pix;
|
|
|
|
QImage original, cache;
|
|
|
|
bool alpha = true;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time when = 0;
|
2016-07-12 12:28:07 +00:00
|
|
|
|
|
|
|
// Counted from the end, so that positionMs <= durationMs despite keep up delays.
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time positionMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
};
|
|
|
|
Frame _frames[3];
|
|
|
|
int _frame = 0;
|
2019-02-16 10:29:55 +00:00
|
|
|
not_null<Frame*> frame() {
|
2016-06-24 10:37:29 +00:00
|
|
|
return _frames + _frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
|
|
|
|
2016-07-12 18:04:34 +00:00
|
|
|
bool _hasAudio = false;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _durationMs = 0;
|
|
|
|
crl::time _animationStarted = 0;
|
|
|
|
crl::time _nextFrameWhen = 0;
|
|
|
|
crl::time _nextFramePositionMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2016-07-13 11:24:31 +00:00
|
|
|
bool _autoPausedGif = false;
|
|
|
|
bool _started = false;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _videoPausedAtMs = 0;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
friend class Manager;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-04-02 10:29:28 +00:00
|
|
|
Manager::Manager(QThread *thread) {
|
2016-06-24 10:37:29 +00:00
|
|
|
moveToThread(thread);
|
|
|
|
connect(thread, SIGNAL(started()), this, SLOT(process()));
|
|
|
|
connect(thread, SIGNAL(finished()), this, SLOT(finish()));
|
|
|
|
connect(this, SIGNAL(processDelayed()), this, SLOT(process()), Qt::QueuedConnection);
|
|
|
|
|
|
|
|
_timer.setSingleShot(true);
|
|
|
|
_timer.moveToThread(thread);
|
|
|
|
connect(&_timer, SIGNAL(timeout()), this, SLOT(process()));
|
|
|
|
|
2019-04-02 10:29:28 +00:00
|
|
|
connect(
|
|
|
|
this,
|
|
|
|
&Manager::callback,
|
|
|
|
QCoreApplication::instance(),
|
|
|
|
&Reader::callback);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::append(Reader *reader, const FileLocation &location, const QByteArray &data) {
|
|
|
|
reader->_private = new ReaderPrivate(reader, location, data);
|
|
|
|
_loadLevel.fetchAndAddRelaxed(AverageGifSize);
|
|
|
|
update(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::start(Reader *reader) {
|
|
|
|
update(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::update(Reader *reader) {
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
|
|
|
auto i = _readerPointers.find(reader);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (i == _readerPointers.cend()) {
|
2016-09-25 18:05:47 +00:00
|
|
|
_readerPointers.insert(reader, QAtomicInt(1));
|
2016-06-24 10:37:29 +00:00
|
|
|
} else {
|
2016-09-25 18:05:47 +00:00
|
|
|
i->storeRelease(1);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
emit processDelayed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::stop(Reader *reader) {
|
|
|
|
if (!carries(reader)) return;
|
|
|
|
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
2016-06-24 10:37:29 +00:00
|
|
|
_readerPointers.remove(reader);
|
|
|
|
emit processDelayed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::carries(Reader *reader) const {
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
2016-06-24 10:37:29 +00:00
|
|
|
return _readerPointers.contains(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::ReaderPointers::iterator Manager::unsafeFindReaderPointer(ReaderPrivate *reader) {
|
|
|
|
ReaderPointers::iterator it = _readerPointers.find(reader->_interface);
|
|
|
|
|
|
|
|
// could be a new reader which was realloced in the same address
|
|
|
|
return (it == _readerPointers.cend() || it.key()->_private == reader) ? it : _readerPointers.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::ReaderPointers::const_iterator Manager::constUnsafeFindReaderPointer(ReaderPrivate *reader) const {
|
|
|
|
ReaderPointers::const_iterator it = _readerPointers.constFind(reader->_interface);
|
|
|
|
|
|
|
|
// could be a new reader which was realloced in the same address
|
|
|
|
return (it == _readerPointers.cend() || it.key()->_private == reader) ? it : _readerPointers.cend();
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
bool Manager::handleProcessResult(ReaderPrivate *reader, ProcessResult result, crl::time ms) {
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
|
|
|
auto it = unsafeFindReaderPointer(reader);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (result == ProcessResult::Error) {
|
|
|
|
if (it != _readerPointers.cend()) {
|
2016-09-25 18:05:47 +00:00
|
|
|
it.key()->error();
|
|
|
|
emit callback(it.key(), it.key()->threadIndex(), NotificationReinit);
|
|
|
|
_readerPointers.erase(it);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
return false;
|
2016-07-12 18:04:34 +00:00
|
|
|
} else if (result == ProcessResult::Finished) {
|
|
|
|
if (it != _readerPointers.cend()) {
|
|
|
|
it.key()->finished();
|
|
|
|
emit callback(it.key(), it.key()->threadIndex(), NotificationReinit);
|
|
|
|
}
|
|
|
|
return false;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
if (it == _readerPointers.cend()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ProcessResult::Started) {
|
|
|
|
_loadLevel.fetchAndAddRelaxed(reader->_width * reader->_height - AverageGifSize);
|
2016-07-12 12:28:07 +00:00
|
|
|
it.key()->_durationMs = reader->_durationMs;
|
2016-07-12 18:04:34 +00:00
|
|
|
it.key()->_hasAudio = reader->_hasAudio;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
2016-07-10 19:44:55 +00:00
|
|
|
// See if we need to pause GIF because it is not displayed right now.
|
2016-07-13 11:24:31 +00:00
|
|
|
if (!reader->_autoPausedGif && reader->_mode == Reader::Mode::Gif && result == ProcessResult::Repaint) {
|
2016-06-24 10:37:29 +00:00
|
|
|
int32 ishowing, iprevious;
|
2016-09-25 18:05:47 +00:00
|
|
|
auto showing = it.key()->frameToShow(&ishowing), previous = it.key()->frameToWriteNext(false, &iprevious);
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(previous != nullptr && showing != nullptr && ishowing >= 0 && iprevious >= 0);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (reader->_frames[ishowing].when > 0 && showing->displayed.loadAcquire() <= 0) { // current frame was not shown
|
|
|
|
if (reader->_frames[ishowing].when + WaitBeforeGifPause < ms || (reader->_frames[iprevious].when && previous->displayed.loadAcquire() <= 0)) {
|
2016-07-13 11:24:31 +00:00
|
|
|
reader->_autoPausedGif = true;
|
|
|
|
it.key()->_autoPausedGif.storeRelease(1);
|
2016-06-24 10:37:29 +00:00
|
|
|
result = ProcessResult::Paused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == ProcessResult::Started || result == ProcessResult::CopyFrame) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(reader->_frame >= 0);
|
2016-09-25 18:05:47 +00:00
|
|
|
auto frame = it.key()->_frames + reader->_frame;
|
2016-06-24 10:37:29 +00:00
|
|
|
frame->clear();
|
|
|
|
frame->pix = reader->frame()->pix;
|
|
|
|
frame->original = reader->frame()->original;
|
|
|
|
frame->displayed.storeRelease(0);
|
2016-07-12 12:28:07 +00:00
|
|
|
frame->positionMs = reader->frame()->positionMs;
|
2016-06-24 10:37:29 +00:00
|
|
|
if (result == ProcessResult::Started) {
|
2016-07-10 19:44:55 +00:00
|
|
|
reader->startedAt(ms);
|
2016-06-24 10:37:29 +00:00
|
|
|
it.key()->moveToNextWrite();
|
|
|
|
emit callback(it.key(), it.key()->threadIndex(), NotificationReinit);
|
|
|
|
}
|
|
|
|
} else if (result == ProcessResult::Paused) {
|
|
|
|
it.key()->moveToNextWrite();
|
|
|
|
emit callback(it.key(), it.key()->threadIndex(), NotificationReinit);
|
|
|
|
} else if (result == ProcessResult::Repaint) {
|
|
|
|
it.key()->moveToNextWrite();
|
|
|
|
emit callback(it.key(), it.key()->threadIndex(), NotificationRepaint);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
Manager::ResultHandleState Manager::handleResult(ReaderPrivate *reader, ProcessResult result, crl::time ms) {
|
2016-06-24 10:37:29 +00:00
|
|
|
if (!handleProcessResult(reader, result, ms)) {
|
|
|
|
_loadLevel.fetchAndAddRelaxed(-1 * (reader->_width > 0 ? reader->_width * reader->_height : AverageGifSize));
|
|
|
|
delete reader;
|
|
|
|
return ResultHandleRemove;
|
|
|
|
}
|
|
|
|
|
|
|
|
_processingInThread->eventDispatcher()->processEvents(QEventLoop::AllEvents);
|
|
|
|
if (_processingInThread->isInterruptionRequested()) {
|
|
|
|
return ResultHandleStop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ProcessResult::Repaint) {
|
|
|
|
{
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
|
|
|
auto it = constUnsafeFindReaderPointer(reader);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (it != _readerPointers.cend()) {
|
|
|
|
int32 index = 0;
|
|
|
|
Reader *r = it.key();
|
|
|
|
Reader::Frame *frame = it.key()->frameToWrite(&index);
|
|
|
|
if (frame) {
|
|
|
|
frame->clear();
|
|
|
|
} else {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!reader->_request.valid());
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
reader->_frame = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handleResult(reader, reader->finishProcess(ms), ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResultHandleContinue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::process() {
|
|
|
|
if (_processingInThread) {
|
|
|
|
_needReProcess = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_timer.stop();
|
|
|
|
_processingInThread = thread();
|
|
|
|
|
2016-07-13 17:34:57 +00:00
|
|
|
bool checkAllReaders = false;
|
2019-03-01 14:41:10 +00:00
|
|
|
auto ms = crl::now(), minms = ms + 86400 * crl::time(1000);
|
2016-06-24 10:37:29 +00:00
|
|
|
{
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
2016-07-05 17:43:30 +00:00
|
|
|
for (auto it = _readerPointers.begin(), e = _readerPointers.end(); it != e; ++it) {
|
2016-09-25 18:05:47 +00:00
|
|
|
if (it->loadAcquire() && it.key()->_private != nullptr) {
|
2016-07-05 17:43:30 +00:00
|
|
|
auto i = _readers.find(it.key()->_private);
|
2016-06-24 10:37:29 +00:00
|
|
|
if (i == _readers.cend()) {
|
|
|
|
_readers.insert(it.key()->_private, 0);
|
|
|
|
} else {
|
|
|
|
i.value() = ms;
|
2016-07-13 11:24:31 +00:00
|
|
|
if (i.key()->_autoPausedGif && !it.key()->_autoPausedGif.loadAcquire()) {
|
|
|
|
i.key()->_autoPausedGif = false;
|
|
|
|
}
|
|
|
|
if (it.key()->_videoPauseRequest.loadAcquire()) {
|
|
|
|
i.key()->pauseVideo(ms);
|
|
|
|
} else {
|
|
|
|
i.key()->resumeVideo(ms);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-25 18:05:47 +00:00
|
|
|
auto frame = it.key()->frameToWrite();
|
2016-06-24 10:37:29 +00:00
|
|
|
if (frame) it.key()->_private->_request = frame->request;
|
2016-09-25 18:05:47 +00:00
|
|
|
it->storeRelease(0);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 17:34:57 +00:00
|
|
|
checkAllReaders = (_readers.size() > _readerPointers.size());
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 17:43:30 +00:00
|
|
|
for (auto i = _readers.begin(), e = _readers.end(); i != e;) {
|
2016-06-24 10:37:29 +00:00
|
|
|
ReaderPrivate *reader = i.key();
|
|
|
|
if (i.value() <= ms) {
|
|
|
|
ResultHandleState state = handleResult(reader, reader->process(ms), ms);
|
|
|
|
if (state == ResultHandleRemove) {
|
|
|
|
i = _readers.erase(i);
|
|
|
|
continue;
|
|
|
|
} else if (state == ResultHandleStop) {
|
2019-04-02 10:29:28 +00:00
|
|
|
_processingInThread = nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-19 06:57:53 +00:00
|
|
|
ms = crl::now();
|
2016-07-13 11:24:31 +00:00
|
|
|
if (reader->_videoPausedAtMs) {
|
|
|
|
i.value() = ms + 86400 * 1000ULL;
|
|
|
|
} else if (reader->_nextFrameWhen && reader->_started) {
|
|
|
|
i.value() = reader->_nextFrameWhen;
|
|
|
|
} else {
|
|
|
|
i.value() = (ms + 86400 * 1000ULL);
|
|
|
|
}
|
2016-07-13 17:34:57 +00:00
|
|
|
} else if (checkAllReaders) {
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
|
|
|
auto it = constUnsafeFindReaderPointer(reader);
|
2016-07-13 17:34:57 +00:00
|
|
|
if (it == _readerPointers.cend()) {
|
|
|
|
_loadLevel.fetchAndAddRelaxed(-1 * (reader->_width > 0 ? reader->_width * reader->_height : AverageGifSize));
|
|
|
|
delete reader;
|
|
|
|
i = _readers.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
2016-07-13 11:24:31 +00:00
|
|
|
if (!reader->_autoPausedGif && i.value() < minms) {
|
2016-06-24 10:37:29 +00:00
|
|
|
minms = i.value();
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
ms = crl::now();
|
2016-06-24 10:37:29 +00:00
|
|
|
if (_needReProcess || minms <= ms) {
|
|
|
|
_needReProcess = false;
|
|
|
|
_timer.start(1);
|
|
|
|
} else {
|
|
|
|
_timer.start(minms - ms);
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:29:28 +00:00
|
|
|
_processingInThread = nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::finish() {
|
|
|
|
_timer.stop();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::clear() {
|
|
|
|
{
|
2016-09-25 18:05:47 +00:00
|
|
|
QMutexLocker lock(&_readerPointersMutex);
|
|
|
|
for (auto it = _readerPointers.begin(), e = _readerPointers.end(); it != e; ++it) {
|
2016-07-13 17:34:57 +00:00
|
|
|
it.key()->_private = nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
_readerPointers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Readers::iterator i = _readers.begin(), e = _readers.end(); i != e; ++i) {
|
|
|
|
delete i.key();
|
|
|
|
}
|
|
|
|
_readers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::~Manager() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
FileMediaInformation::Video PrepareForSending(const QString &fname, const QByteArray &data) {
|
|
|
|
auto result = FileMediaInformation::Video();
|
2017-03-09 09:47:45 +00:00
|
|
|
auto localLocation = FileLocation(fname);
|
|
|
|
auto localData = QByteArray(data);
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2019-03-01 14:41:10 +00:00
|
|
|
auto seekPositionMs = crl::time(0);
|
2017-05-18 20:18:59 +00:00
|
|
|
auto reader = std::make_unique<internal::FFMpegReaderImplementation>(&localLocation, &localData, AudioMsgId());
|
2017-03-09 09:47:45 +00:00
|
|
|
if (reader->start(internal::ReaderImplementation::Mode::Inspecting, seekPositionMs)) {
|
|
|
|
auto durationMs = reader->durationMs();
|
2017-03-27 12:50:40 +00:00
|
|
|
if (durationMs > 0) {
|
|
|
|
result.isGifv = reader->isGifv();
|
2018-10-25 08:12:56 +00:00
|
|
|
// Use first video frame as a thumbnail.
|
|
|
|
// All other apps and server do that way.
|
|
|
|
//if (!result.isGifv) {
|
|
|
|
// auto middleMs = durationMs / 2;
|
|
|
|
// if (!reader->inspectAt(middleMs)) {
|
|
|
|
// return result;
|
|
|
|
// }
|
|
|
|
//}
|
2017-03-27 12:50:40 +00:00
|
|
|
auto hasAlpha = false;
|
2019-02-19 06:57:53 +00:00
|
|
|
auto readResult = reader->readFramesTill(-1, crl::now());
|
2017-03-27 12:50:40 +00:00
|
|
|
auto readFrame = (readResult == internal::ReaderImplementation::ReadResult::Success);
|
|
|
|
if (readFrame && reader->renderFrame(result.thumbnail, hasAlpha, QSize())) {
|
|
|
|
if (hasAlpha) {
|
|
|
|
auto cacheForResize = QImage();
|
|
|
|
auto request = FrameRequest();
|
|
|
|
request.framew = request.outerw = result.thumbnail.width();
|
|
|
|
request.frameh = request.outerh = result.thumbnail.height();
|
|
|
|
request.factor = 1;
|
|
|
|
result.thumbnail = PrepareFrameImage(request, result.thumbnail, hasAlpha, cacheForResize);
|
|
|
|
}
|
|
|
|
result.duration = static_cast<int>(durationMs / 1000);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
2018-11-07 14:07:20 +00:00
|
|
|
|
|
|
|
result.supportsStreaming = CheckStreamingSupport(
|
|
|
|
localLocation,
|
|
|
|
localData);
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 09:47:45 +00:00
|
|
|
return result;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Finish() {
|
|
|
|
if (!threads.isEmpty()) {
|
|
|
|
for (int32 i = 0, l = threads.size(); i < l; ++i) {
|
|
|
|
threads.at(i)->quit();
|
|
|
|
DEBUG_LOG(("Waiting for clipThread to finish: %1").arg(i));
|
|
|
|
threads.at(i)->wait();
|
|
|
|
delete managers.at(i);
|
|
|
|
delete threads.at(i);
|
|
|
|
}
|
|
|
|
threads.clear();
|
|
|
|
managers.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 06:06:02 +00:00
|
|
|
Reader *const ReaderPointer::BadPointer = SharedMemoryLocation<Reader, 0>();
|
|
|
|
|
|
|
|
ReaderPointer::~ReaderPointer() {
|
|
|
|
if (valid()) {
|
|
|
|
delete _pointer;
|
|
|
|
}
|
|
|
|
_pointer = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-24 10:37:29 +00:00
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|