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

Introduce QAVIODevice::setBufferSize() #420

Merged
merged 1 commit into from
Dec 4, 2023
Merged
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
11 changes: 6 additions & 5 deletions examples/qml_video/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QtAVPlayer/qavplayer.h>
#include <QtAVPlayer/qavvideoframe.h>
#include <QtAVPlayer/qavaudiooutput.h>
#include <QtAVPlayer/qaviodevice.h>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QAbstractVideoSurface>
#include <private/qdeclarativevideooutput_p.h>
Expand Down Expand Up @@ -168,13 +169,13 @@ int main(int argc, char *argv[])
}
});

std::unique_ptr<QFile> qrc;
QSharedPointer<QAVIODevice> qrc;
if (file.startsWith(":/")) {
qrc = std::make_unique<QFile>(file);
if (!qrc->open(QIODevice::ReadOnly))
qrc.reset();
QSharedPointer<QIODevice> io(new QFile(file));
if (io->open(QIODevice::ReadOnly))
qrc.reset(new QAVIODevice(io));
}
p.setSource(file, qrc.get());
p.setSource(file, qrc);
p.setFilter(filter);
//p.setSynced(false);

Expand Down
1 change: 1 addition & 0 deletions examples/qml_video/qml.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file alias="valbok">../../tests/auto/integration/testdata/20190821_075842.jpg</file>
</qresource>
</RCC>
2 changes: 1 addition & 1 deletion src/QtAVPlayer/QtAVPlayer.pri
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ PRIVATE_HEADERS += \
$$PWD/qavaudioinputfilter_p.h \
$$PWD/qavvideooutputfilter_p.h \
$$PWD/qavaudiooutputfilter_p.h \
$$PWD/qaviodevice_p.h \
$$PWD/qavfilters_p.h

PUBLIC_HEADERS += \
$$PWD/qaviodevice.h \
$$PWD/qavaudioformat.h \
$$PWD/qavstreamframe.h \
$$PWD/qavframe.h \
Expand Down
2 changes: 1 addition & 1 deletion src/QtAVPlayer/qavdemuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "qavaudiocodec_p.h"
#include "qavsubtitlecodec_p.h"
#include "qavhwdevice_p.h"
#include "qaviodevice_p.h"
#include "qaviodevice.h"
#include <QtAVPlayer/qtavplayerglobal.h>

#if defined(QT_AVPLAYER_VA_X11) && QT_CONFIG(opengl)
Expand Down
42 changes: 28 additions & 14 deletions src/QtAVPlayer/qaviodevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Free Qt Media Player based on FFmpeg. *
*********************************************************/

#include "qaviodevice_p.h"
#include "qaviodevice.h"
#include <QMutex>
#include <QWaitCondition>

Expand All @@ -30,13 +30,13 @@ class QAVIODevicePrivate
{
Q_DECLARE_PUBLIC(QAVIODevice)
public:
explicit QAVIODevicePrivate(QAVIODevice *q, QIODevice &device)
explicit QAVIODevicePrivate(QAVIODevice *q, const QSharedPointer<QIODevice> &device)
: q_ptr(q)
, device(device)
, buffer(static_cast<unsigned char*>(av_malloc(buffer_size)))
, ctx(avio_alloc_context(buffer, static_cast<int>(buffer_size), 0, this, &QAVIODevicePrivate::read, nullptr, !device.isSequential() ? &QAVIODevicePrivate::seek : nullptr))
, ctx(avio_alloc_context(buffer, static_cast<int>(buffer_size), 0, this, &QAVIODevicePrivate::read, nullptr, !device->isSequential() ? &QAVIODevicePrivate::seek : nullptr))
{
if (!device.isSequential())
if (!device->isSequential())
ctx->seekable = AVIO_SEEKABLE_NORMAL;
}

Expand All @@ -52,7 +52,7 @@ class QAVIODevicePrivate
if (readRequest.data == nullptr || readRequest.wroteBytes)
return;

readRequest.wroteBytes = !device.atEnd() ? device.read((char *)readRequest.data, readRequest.maxSize) : AVERROR_EOF;
readRequest.wroteBytes = !device->atEnd() ? device->read((char *)readRequest.data, readRequest.maxSize) : AVERROR_EOF;
// Unblock the decoder thread when there is available bytes
if (readRequest.wroteBytes) {
waitCond.wakeAll();
Expand Down Expand Up @@ -96,14 +96,14 @@ class QAVIODevicePrivate
QMetaObject::invokeMethod(d->q_ptr, [&] {
QMutexLocker locker(&d->mutex);
if (whence == AVSEEK_SIZE) {
pos = d->device.size() > 0 ? d->device.size() : 0;
pos = d->device->size() > 0 ? d->device->size() : 0;
} else {
if (whence == SEEK_END)
offset = d->device.size() - offset;
offset = d->device->size() - offset;
else if (whence == SEEK_CUR)
offset = d->device.pos() + offset;
offset = d->device->pos() + offset;

pos = d->device.seek(offset) ? d->device.pos() : -1;
pos = d->device->seek(offset) ? d->device->pos() : -1;
}
d->waitCond.wakeAll();
wake = true;
Expand All @@ -116,23 +116,23 @@ class QAVIODevicePrivate
return pos;
}

const size_t buffer_size = 64 * 1024;
size_t buffer_size = 64 * 1024;
QAVIODevice *q_ptr = nullptr;
QIODevice &device;
QSharedPointer<QIODevice> device;
unsigned char *buffer = nullptr;
AVIOContext *ctx = nullptr;
QMutex mutex;
mutable QMutex mutex;
QWaitCondition waitCond;
bool aborted = false;
bool wakeRead = false;
ReadRequest readRequest;
};

QAVIODevice::QAVIODevice(QIODevice &device, QObject *parent)
QAVIODevice::QAVIODevice(const QSharedPointer<QIODevice> &device, QObject *parent)
: QObject(parent)
, d_ptr(new QAVIODevicePrivate(this, device))
{
connect(&device, &QIODevice::readyRead, this, [this] {
connect(device.get(), &QIODevice::readyRead, this, [this] {
Q_D(QAVIODevice);
d->readData();
});
Expand All @@ -156,4 +156,18 @@ void QAVIODevice::abort(bool aborted)
d->waitCond.wakeAll();
}

void QAVIODevice::setBufferSize(size_t size)
{
Q_D(QAVIODevice);
QMutexLocker locker(&d->mutex);
d->buffer_size = size;
}

size_t QAVIODevice::bufferSize() const
{
Q_D(const QAVIODevice);
QMutexLocker locker(&d->mutex);
return d->buffer_size;
}

QT_END_NAMESPACE
17 changes: 5 additions & 12 deletions src/QtAVPlayer/qaviodevice_p.h → src/QtAVPlayer/qaviodevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@
#ifndef QAVFIODEVICE_P_H
#define QAVFIODEVICE_P_H

//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtAVPlayer/qtavplayerglobal.h>
#include <QIODevice>
#include <QSharedPointer>
#include <memory>

QT_BEGIN_NAMESPACE
Expand All @@ -30,12 +20,15 @@ class QAVIODevicePrivate;
class QAVIODevice : public QObject
{
public:
QAVIODevice(QIODevice &device, QObject *parent = nullptr);
QAVIODevice(const QSharedPointer<QIODevice> &device, QObject *parent = nullptr);
~QAVIODevice();

AVIOContext *ctx() const;
void abort(bool aborted);

void setBufferSize(size_t size);
size_t bufferSize() const;

protected:
std::unique_ptr<QAVIODevicePrivate> d_ptr;

Expand Down
11 changes: 5 additions & 6 deletions src/QtAVPlayer/qavplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "qavplayer.h"
#include "qavdemuxer_p.h"
#include "qaviodevice_p.h"
#include "qaviodevice.h"
#include "qavvideocodec_p.h"
#include "qavaudiocodec_p.h"
#include "qavvideoframe.h"
Expand Down Expand Up @@ -110,7 +110,7 @@ class QAVPlayerPrivate

QAVPlayer *q_ptr = nullptr;
QString url;
QScopedPointer<QAVIODevice> dev;
QSharedPointer<QAVIODevice> dev;
QAVPlayer::MediaStatus mediaStatus = QAVPlayer::NoMedia;
QList<PendingMediaStatus> pendingMediaStatuses;
QAVPlayer::State state = QAVPlayer::StoppedState;
Expand Down Expand Up @@ -502,7 +502,7 @@ void QAVPlayerPrivate::doLoad()
{
demuxer.abort(false);
demuxer.unload();
int ret = demuxer.load(url, dev.data());
int ret = demuxer.load(url, dev.get());
if (ret < 0) {
setError(QAVPlayer::ResourceError, err_str(ret));
return;
Expand Down Expand Up @@ -867,7 +867,7 @@ QAVPlayer::~QAVPlayer()
d->terminate();
}

void QAVPlayer::setSource(const QString &url, QIODevice *dev)
void QAVPlayer::setSource(const QString &url, const QSharedPointer<QAVIODevice> &dev)
{
Q_D(QAVPlayer);
if (d->url == url)
Expand All @@ -877,8 +877,7 @@ void QAVPlayer::setSource(const QString &url, QIODevice *dev)

d->terminate();
d->url = url;
if (dev)
d->dev.reset(new QAVIODevice(*dev));
d->dev = dev;
emit sourceChanged(url);
d->wait(true);
d->quit = false;
Expand Down
4 changes: 2 additions & 2 deletions src/QtAVPlayer/qavplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

QT_BEGIN_NAMESPACE

class QIODevice;
class QAVIODevice;
class QAVPlayerPrivate;
class QAVPlayer : public QObject
{
Expand Down Expand Up @@ -53,7 +53,7 @@ class QAVPlayer : public QObject
QAVPlayer(QObject *parent = nullptr);
~QAVPlayer();

void setSource(const QString &url, QIODevice *dev = nullptr);
void setSource(const QString &url, const QSharedPointer<QAVIODevice> &dev = {});
QString source() const;

QList<QAVStream> availableVideoStreams() const;
Expand Down
10 changes: 5 additions & 5 deletions tests/auto/integration/qavdemuxer/tst_qavdemuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "qavdemuxer_p.h"
#include "qavaudioframe.h"
#include "qavvideoframe.h"
#include "qaviodevice_p.h"
#include "qaviodevice.h"
#include "qavvideocodec_p.h"
#include "qavaudiocodec_p.h"

Expand Down Expand Up @@ -226,8 +226,8 @@ void tst_QAVDemuxer::fileIO()
{
QAVDemuxer d;

QFile file(testData("colors.mp4"));
if (!file.open(QIODevice::ReadOnly)) {
QSharedPointer<QIODevice> file(new QFile(testData("colors.mp4")));
if (!file->open(QIODevice::ReadOnly)) {
QFAIL("Could not open");
return;
}
Expand Down Expand Up @@ -285,8 +285,8 @@ void tst_QAVDemuxer::qrcIO()
{
QAVDemuxer d;

QFile file(":/test.wav");
if (!file.open(QIODevice::ReadOnly)) {
QSharedPointer<QIODevice> file(new QFile(":/test.wav"));
if (!file->open(QIODevice::ReadOnly)) {
QFAIL("Could not open");
return;
}
Expand Down
32 changes: 17 additions & 15 deletions tests/auto/integration/qavplayer/tst_qavplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "qavplayer.h"
#include "qavaudiooutput.h"
#include "qaviodevice_p.h"
#include "qaviodevice.h"

#include <QDebug>
#include <QtTest/QtTest>
Expand Down Expand Up @@ -1263,13 +1263,13 @@ void tst_QAVPlayer::files_io()
QAVPlayer p;

QFileInfo fileInfo(path);
QFile file(fileInfo.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly)) {
QSharedPointer<QIODevice> file(new QFile(fileInfo.absoluteFilePath()));
if (!file->open(QIODevice::ReadOnly)) {
QFAIL("Could not open");
return;
}

p.setSource(path, &file);
QSharedPointer<QAVIODevice> dev(new QAVIODevice(file));
p.setSource(path, dev);

int vf = 0;
QAVVideoFrame videoFrame;
Expand Down Expand Up @@ -2457,21 +2457,22 @@ void tst_QAVPlayer::filesIO()
return;
}

Buffer buffer;
buffer.m_size = file.size();
buffer.open(QIODevice::ReadWrite);
QSharedPointer<Buffer> buffer(new Buffer);
buffer->m_size = file.size();
buffer->open(QIODevice::ReadWrite);

QAVPlayer p;
QAVVideoFrame frame;
int framesCount = 0;
QObject::connect(&p, &QAVPlayer::videoFrame, &p, [&](const QAVVideoFrame &f) { frame = f; ++framesCount; });

p.setSource(fileInfo.fileName(), &buffer);
QSharedPointer<QAVIODevice> dev(new QAVIODevice(buffer));
p.setSource(fileInfo.fileName(), dev);
p.play();

while(!file.atEnd()) {
auto bytes = file.read(64 * 1024);
buffer.write(bytes);
buffer->write(bytes);
QTest::qWait(50);
}

Expand Down Expand Up @@ -2506,21 +2507,22 @@ void tst_QAVPlayer::filesIOSequential()
QFile file(fileInfo.absoluteFilePath());
file.open(QFile::ReadOnly);

BufferSequential buffer;
buffer.m_size = file.size();
buffer.open(QIODevice::ReadWrite);
QSharedPointer<BufferSequential> buffer(new BufferSequential);
buffer->m_size = file.size();
buffer->open(QIODevice::ReadWrite);

QAVPlayer p;
QAVVideoFrame frame;
int framesCount = 0;
QObject::connect(&p, &QAVPlayer::videoFrame, &p, [&](const QAVVideoFrame &f) { frame = f; ++framesCount; });

p.setSource(fileInfo.fileName(), &buffer);
QSharedPointer<QAVIODevice> dev(new QAVIODevice(buffer));
p.setSource(fileInfo.fileName(), dev);
p.play();

while(!file.atEnd()) {
auto bytes = file.read(64 * 1024);
buffer.write(bytes);
buffer->write(bytes);
QTest::qWait(50);
}

Expand Down