Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoFrameExtractor: Fix race condition in synchronous extraction mode #1390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/QtAV/VideoFrameExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Q_AV_EXPORT VideoFrameExtractor : public QObject
* Extract video frames in another thread. Default is true.
* In async mode, if current extraction is not finished, new
* setPosition() will be ignored.
* If you want to disable async extraction make sure to call setAsync(false) as soon as possible (before extracting any frames or settings the source).
* After the first frame is extracted setAsync() has no effect.
*/
void setAsync(bool value);
bool async() const;
Expand Down
40 changes: 23 additions & 17 deletions src/VideoFrameExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <QtCore/QStringList>
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include "QtAV/VideoCapture.h"
#include "QtAV/VideoDecoder.h"
#include "QtAV/AVDemuxer.h"
#include "QtAV/Packet.h"
Expand Down Expand Up @@ -369,15 +367,22 @@ class VideoFrameExtractorPrivate : public DPtrPrivate<VideoFrameExtractor>
}

void safeReleaseResource() {
class Cleaner : public QRunnable {
VideoFrameExtractorPrivate *p;
public:
Cleaner(VideoFrameExtractorPrivate* pri) : p(pri) {}
void run() {
p->releaseResourceInternal();
}
};
thread.addTask(new Cleaner(this));
if(!async && !thread.isRunning()) {
releaseResourceInternal();
return;
}

class Cleaner : public QRunnable {
VideoFrameExtractorPrivate *p;
public:
Cleaner(VideoFrameExtractorPrivate* pri) : p(pri) {}
void run() {
p->releaseResourceInternal();
}
};

thread.start();
thread.addTask(new Cleaner(this));
}

volatile bool abort_seek;
Expand All @@ -403,10 +408,7 @@ QVariantHash VideoFrameExtractorPrivate::dec_opt_normal;

VideoFrameExtractor::VideoFrameExtractor(QObject *parent) :
QObject(parent)
{
DPTR_D(VideoFrameExtractor);
d.thread.start();
}
{}

void VideoFrameExtractor::setSource(const QString url)
{
Expand All @@ -430,12 +432,15 @@ void VideoFrameExtractor::setAsync(bool value)
if (d.async == value)
return;
d.async = value;
if (!d.async && d.thread.isRunning()) {
qInfo() << "ExtractThread is already running. Setting async to false has no effect.";
}
Q_EMIT asyncChanged();
}

bool VideoFrameExtractor::async() const
{
return d_func().async;
return d_func().async || d_func().thread.isRunning();
}

void VideoFrameExtractor::setAutoExtract(bool value)
Expand Down Expand Up @@ -494,7 +499,7 @@ int VideoFrameExtractor::precision() const
void VideoFrameExtractor::extract()
{
DPTR_D(VideoFrameExtractor);
if (!d.async) {
if (!d.async && !d.thread.isRunning()) {
extractInternal(position());
return;
}
Expand All @@ -518,6 +523,7 @@ void VideoFrameExtractor::extract()
// (called by extractInternal()) and if true, method returns early.
// Note if seek/decode is aborted, aborted() signal will be emitted.
d.abort_seek = true;
d.thread.start();
d.thread.addTask(new ExtractTask(this, position()));
}

Expand Down