-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiodevice.cpp
66 lines (62 loc) · 1.5 KB
/
iodevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (C) 2018-2019 Wang Bin - wbsecg1 at gmail.com
* https://github.com/wang-bin/qtmultimedia-plugins-mdk
* MIT License
*/
#include "iodevice.h"
#ifdef MDK_ABI
#include <QFile>
#include <QtDebug>
void QMediaIO::registerOnce()
{
MediaIO::registerOnce("QIODevice", []{ return new QMediaIO();});
}
QMediaIO::QMediaIO(QIODevice* io)
: MediaIO()
, owns_io_(!!io)
, io_(io)
{
}
bool QMediaIO::onUrlChanged()
{
if (io_)
io_->close();
if (owns_io_) {
delete io_;
io_ = nullptr;
}
if (url().empty())
return true;
if (!io_) {
auto s = QString::fromStdString(url());
static const std::string kScheme = "qio:";
if (url().substr(0, kScheme.size()) != kScheme) {
io_ = new QFile(s);
} else {
io_ = (QIODevice*)s.mid(4).toULongLong();
}
}
if (io_->isOpen() && io_->isReadable())
return true;
if (!io_->open(accessMode() == MediaIO::AccessMode::Write ? QIODevice::WriteOnly : QIODevice::ReadOnly)) {
qWarning() << "Failed to open qiodevice: " << io_->errorString();
if (owns_io_) {
delete io_;
io_ = nullptr;
}
return false;
}
return true;
}
bool QMediaIO::seek(int64_t offset, int from)
{
if (!io_)
return false;
int p = offset;
if (from == SEEK_CUR)
p += io_->pos();
else if (from == SEEK_END)
p += size(); // offset < 0
return io_->seek(p);
}
#endif // MDK_ABI