implements QIODevice as input

This commit is contained in:
wang-bin
2014-12-05 19:48:40 +08:00
parent fecc8ce77b
commit 496c8d7dcd
7 changed files with 242 additions and 18 deletions
+7 -1
View File
@@ -23,6 +23,7 @@
#define QTAV_AVINPUT_H
#include <QtAV/QtAV_Global.h>
#include <QtCore/QStringList>
namespace QtAV {
@@ -34,7 +35,11 @@ class Q_AV_EXPORT AVInput
public:
AVInput();
virtual ~AVInput();
virtual bool isSeekable() = 0;
virtual void setUrl(const QString& url);
QString url() const;
/// supported protocols. default is empty
virtual const QStringList& protocols() const;
virtual bool isSeekable() const = 0;
virtual qint64 read(char *data, qint64 maxSize) = 0;
/*!
* \brief seek
@@ -60,6 +65,7 @@ public:
void release(); //TODO: how to remove it?
protected:
AVInput(AVInputPrivate& d);
virtual void onUrlChanged();
DPTR_DECLARE(AVInput)
};
+2
View File
@@ -24,6 +24,7 @@
#include "QtAV/QtAV_Global.h"
#include "QtAV/private/AVCompat.h"
#include <QtCore/QString>
namespace QtAV {
@@ -36,6 +37,7 @@ public:
{}
// TODO: how to manage ctx?
AVIOContext *ctx;
QString url;
};
} //namespace QtAV
+24
View File
@@ -21,6 +21,7 @@
#include "QtAV/AVInput.h"
#include "QtAV/private/AVInput_p.h"
#include <QtCore/QStringList>
namespace QtAV {
@@ -64,6 +65,29 @@ AVInput::~AVInput()
{
}
void AVInput::setUrl(const QString &url)
{
DPTR_D(AVInput);
if (d.url == url)
return;
d.url = url;
onUrlChanged();
}
QString AVInput::url() const
{
return d_func().url;
}
void AVInput::onUrlChanged()
{}
const QStringList& AVInput::protocols() const
{
static QStringList no_protocols;
return no_protocols;
}
#define IODATA_BUFFER_SIZE 32768 //
void* AVInput::avioContext()
+146 -17
View File
@@ -1,33 +1,162 @@
#include "QtAV/AVInput.h"
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2014 Wang Bin <wbsecg1@gmail.com>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QIODeviceInput.h"
#include "QtAV/private/AVInput_p.h"
#include <QtCore/QIODevice>
#include <QtCore/QFile>
#ifndef TEST_QTAV_QIODEVICEINPUT
#include "utils/Logger.h"
#else
#include <QtDebug>
#endif
namespace QtAV {
class QIODeviceInput : public AVInput
class QIODeviceInputPrivate : public AVInputPrivate
{
public:
void setIODevice(QIODevice *dev);
QIODevice* device() const;
virtual bool isSeekable() Q_DECL_OVERRIDE;
virtual qint64 read(char *data, qint64 maxSize) Q_DECL_OVERRIDE;
virtual bool seek(qint64 offset, int from) Q_DECL_OVERRIDE;
virtual qint64 position() const Q_DECL_OVERRIDE;
/*!
* \brief size
* \return <=0 if not support
*/
virtual qint64 size() const Q_DECL_OVERRIDE;
QIODeviceInputPrivate()
: AVInputPrivate()
, dev(0)
{}
QIODevice *dev;
};
QIODeviceInput::QIODeviceInput() : AVInput(*new QIODeviceInputPrivate()) {}
QIODeviceInput::QIODeviceInput(QIODeviceInputPrivate &d) : AVInput(d) {}
void QIODeviceInput::setIODevice(QIODevice *dev)
{
d_func().dev = dev;
}
QIODevice* QIODeviceInput::device() const
{
return d_func().dev;
}
bool QIODeviceInput::isSeekable() const
{
DPTR_D(const QIODeviceInput);
return d.dev && !d.dev->isSequential();
}
qint64 QIODeviceInput::read(char *data, qint64 maxSize)
{
DPTR_D(QIODeviceInput);
if (!d.dev)
return 0;
return d.dev->read(data, maxSize);
}
bool QIODeviceInput::seek(qint64 offset, int from)
{
DPTR_D(QIODeviceInput);
if (!d.dev)
return false;
if (from == 2) {
offset = d.dev->size() - offset;
} else if (from == 1) {
offset = d.dev->pos() + offset;
}
return d.dev->seek(offset);
}
qint64 QIODeviceInput::position() const
{
DPTR_D(const QIODeviceInput);
if (!d.dev)
return 0;
return d.dev->pos();
}
qint64 QIODeviceInput::size() const
{
DPTR_D(const QIODeviceInput);
if (!d.dev)
return 0;
return d.dev->size(); // sequential device returns bytesAvailable()
}
// qrc support
class QFileInputPrivate;
class QFileInput : public QIODeviceInput
{
DPTR_DECLARE_PRIVATE(QFileInput)
public:
void setFileName(const QString& name);
QString fileName() const;
QFileInput();
const QStringList& protocols() const Q_DECL_OVERRIDE
{
static QStringList p = QStringList() << "" << "qrc";
return p;
}
protected:
void onUrlChanged() Q_DECL_OVERRIDE;
private:
using QIODeviceInput::setIODevice;
};
class QFileInputPrivate : public QIODeviceInputPrivate
{
public:
QFileInputPrivate() : QIODeviceInputPrivate() {}
~QFileInputPrivate() {
if (file.isOpen())
file.close();
}
QFile file;
};
QFileInput::QFileInput()
: QIODeviceInput(*new QFileInputPrivate())
{
setIODevice(&d_func().file);
}
void QFileInput::onUrlChanged()
{
DPTR_D(QFileInput);
if (d.file.isOpen())
d.file.close();
QString path(url());
if (path.startsWith("qrc:"))
path = path.mid(3);
d.file.setFileName(path);
if (path.isEmpty())
return;
if (!d.file.open(QIODevice::ReadOnly))
qWarning() << "Failed to open [" << d.file.fileName() << "]: " << d.file.errorString();
}
} //namespace QtAV
#ifdef TEST_QTAV_QIODEVICEINPUT
int main(int, char**)
{
QtAV::QFileInput fi;
qDebug() << "protocols: " << fi.protocols();
fi.setUrl("qrc:/QtAV.svg");
QByteArray data(1024, 0);
fi.read(data.data(), data.size());
qDebug("QFileInput url: %s, seekable: %d, size: %lld", fi.url().toUtf8().constData(), fi.isSeekable(), fi.size());
qDebug() << data;
return 0;
}
#endif
+57
View File
@@ -0,0 +1,57 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2014 Wang Bin <wbsecg1@gmail.com>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifndef QTAV_QIODEVICEINPUT
#define QTAV_QIODEVICEINPUT
#include <QtAV/AVInput.h>
#include <QtCore/QIODevice>
namespace QtAV {
class QIODeviceInputPrivate;
class QIODeviceInput : public AVInput
{
DPTR_DECLARE_PRIVATE(QIODeviceInput)
public:
QIODeviceInput();
// MUST open/close outside
void setIODevice(QIODevice *dev); // set private in QFileInput etc
QIODevice* device() const;
virtual bool isSeekable() const Q_DECL_OVERRIDE;
virtual qint64 read(char *data, qint64 maxSize) Q_DECL_OVERRIDE;
virtual bool seek(qint64 offset, int from) Q_DECL_OVERRIDE;
virtual qint64 position() const Q_DECL_OVERRIDE;
/*!
* \brief size
* \return <=0 if not support
*/
virtual qint64 size() const Q_DECL_OVERRIDE;
protected:
QIODeviceInput(QIODeviceInputPrivate &d);
};
} //namespace QtAV
#include "QtAV/AVInput.h"
#endif // QTAV_QIODEVICEINPUT
+5
View File
@@ -0,0 +1,5 @@
QT += av av-private
CONFIG -= app_bundle
DEFINES += TEST_QTAV_QIODEVICEINPUT
INCLUDEPATH += $$[QT_INSTALL_HEADERS]
SOURCES = QIODeviceInput.cpp
+1
View File
@@ -427,6 +427,7 @@ HEADERS *= \
AudioThread.h \
VideoThread.h \
filter/FilterManager.h \
input/QIODeviceInput.h \
subtitle/CharsetDetector.h \
subtitle/PlainText.h \
utils/BlockingQueue.h \