-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediamp4.cpp
103 lines (97 loc) · 2.36 KB
/
mediamp4.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "mediamp4.h"
#include <QDebug>
MediaMP4::MediaMP4()
{
// TODO: Handle plaintext here
QFile f(":/ico/UI/ico/128-128.png");
if (f.open(QFile::ReadOnly))
{
key = f.readAll();
f.close();
}
file = NULL;
}
MediaMP4::~MediaMP4()
{
if (file)
{
qDebug() << __FILE__ << "(" << __LINE__ << "):";
file->close();
delete file;
qDebug() << __FILE__ << "(" << __LINE__ << "):";
}
key.clear();
}
// Success returns NoError
int MediaMP4::open(const QUrl& url, uint64_t* fsize)
{
if (file)
{
file->close();
delete file;
}
// file = new QFile(url.toString().replace("file:///", ""));
// qDebug() << __FUNCTION__ << url.path().right(url.path().size() - 1);
file = new QFile(url.path().right(url.path().size() - 1));
if (file == NULL)
{
// qDebug() << __FUNCTION__ << "line:" << __LINE__;
return QFile::FatalError;
}
if (file->error() != QFile::NoError)
{
qDebug() << __FUNCTION__ << "line:" << __LINE__;
int ret = file->error();
delete file;
qDebug() << __FILE__ << "(" << __LINE__ << "):";
file = NULL;
return ret;
}
if (file->open(QFile::ReadOnly))
{
*fsize = file->size();
pos = uint16_t(*fsize & 0xFFFF);
// qDebug() << __FUNCTION__ << "pos:" << pos;
return QFile::NoError;
}
return file->error();
}
ssize_t MediaMP4::read(uint8_t* buffer, size_t length)
{
if (file == NULL)
{
qDebug() << __FUNCTION__ << "line:" << __LINE__;
return QFile::FatalError;
}
auto position = file->pos();
qint64 len = file->read((char*)buffer, length);
// qDebug() << __FUNCTION__ << "len:" << len;
// qDebug() << __FUNCTION__ << "length:" << length;
// qDebug() << __FUNCTION__ << "position:" << position;
if (len > 0)
{
for (qint64 i = 0; i < len; i++)
{
buffer[i] = buffer[i] ^ key.at((pos + position + i) % key.size());
}/**/
return len;
}
return QFile::ReadError;
}
int MediaMP4::seek(uint64_t offset)
{
// qDebug() << __FUNCTION__ << "offset:" << offset;
if (file == NULL)
{
return QFile::FatalError;
}
file->seek(offset);
return file->error();
}
void MediaMP4::close()
{
if (file)
{
file->close();
}
}