-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassetrecording.cpp
More file actions
190 lines (159 loc) · 4.62 KB
/
assetrecording.cpp
File metadata and controls
190 lines (159 loc) · 4.62 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "assetrecording.h"
AssetRecording::AssetRecording() :
start_time(0.0),
play_time_elapsed(0.0),
packet_index(0),
playing(false)
{
// qDebug() << "AssetRecording::AssetRecording()";
SetS("_type", "assetrecording");
SetI("sample_rate", 44100);
dt_time.start();
}
AssetRecording::~AssetRecording()
{
}
void AssetRecording::Load()
{
// qDebug() << "AssetRecording::Load()" << GetS("_src_url");
WebAsset::Load(QUrl(GetS("_src_url")));
}
void AssetRecording::Unload()
{
WebAsset::Unload();
}
bool AssetRecording::GetLoaded() const
{
return WebAsset::GetLoaded();
}
bool AssetRecording::GetProcessed() const
{
return WebAsset::GetProcessed();
}
void AssetRecording::Update()
{
// qDebug() << "AssetRecording::Update()" << GetLoaded() << GetProcessing() << room_id;
if (GetLoaded() && !GetProcessing() && !room_id.isEmpty()) {
SetProcessing(true);
QtConcurrent::run(this, &AssetRecording::LoadDataThread);
}
//increment duration timer while playing
if (playing) {
const float dt = float(dt_time.restart()) / 1000.0f;
play_time_elapsed += dt;
}
else {
//trigger for autoplay
if (GetLoaded() && GetProcessed() && GetB("auto_play")) {
Play(GetB("loop"));
}
}
}
void AssetRecording::Play(const bool loop)
{
// qDebug() << "AssetRecording::Play" << loop;
SetB("loop", loop);
packet_index = 0;
playing = true;
play_time_elapsed = 0.0;
}
void AssetRecording::Seek(const float pos)
{
if (!recording_data.isEmpty()) {
packet_index = 0;
play_time_elapsed = qMin(double(qMax(pos, 0.0f)), recording_data.last().pTime-start_time);
const double cur_time = start_time + play_time_elapsed;
for (int i=0; i<recording_data.size(); ++i) {
if (cur_time>recording_data[i].pTime) {
// qDebug() << "cur time less than recording_data i" << i << recording_data.size() << QString::number(cur_time, 'g', 17) << QString::number(recording_data[i].pTime, 'g', 17);
packet_index = i;
}
else {
break;
}
}
}
// qDebug() << "AssetRecording::Seek seek" << play_time_elapsed << "packet_index" << packet_index;
}
void AssetRecording::Pause()
{
playing = !playing;
}
void AssetRecording::Stop()
{
play_time_elapsed = 0.0f;
packet_index = 0;
playing = false;
}
QList <AssetRecordingPacket> AssetRecording::GetPackets()
{
const double cur_time = start_time + play_time_elapsed;
QList <AssetRecordingPacket> packet_list;
for (int i=packet_index; i<recording_data.size(); ++i) {
if (recording_data[i].pTime <= cur_time) {
packet_list.push_back(recording_data[i]);
packet_index = i+1;
}
else {
break;
}
}
//restart if loop is set to true
if (playing && packet_index >= recording_data.size()-1) {
if (GetB("loop")) {
packet_index = 0;
play_time_elapsed = 0.0;
}
}
// qDebug() << "AssetRecording::GetPackets()" << packet_list.size();
return packet_list;
}
bool AssetRecording::GetPlaying() const
{
return playing;
}
void AssetRecording::LoadDataThread()
{
const QByteArray & ba = GetData();
QTextStream ifs(ba);
recording_data.clear();
while (!ifs.atEnd()) {
const QString line = ifs.readLine();
if (line.isEmpty()) {
continue;
}
int time_index = line.indexOf(" ");
if (time_index < 0) {
continue;
}
AssetRecordingPacket p;
p.pTime = QString(line.left(time_index)).toDouble();
QString s = line.right(line.length()-time_index-1);
if (recording_data.isEmpty()) {
start_time = p.pTime;
//get roomid off of first packet
QJsonDocument doc = QJsonDocument::fromJson(s.toUtf8());
QJsonObject obj = doc.object();
QJsonObject dataObject = obj.value("data").toObject();
QVariantMap m = dataObject.toVariantMap();
// qDebug() << s.toUtf8() << obj << dataObject << m;
original_room_id = m["roomId"].toString();
}
// qDebug() << "replace" << original_room_id << room_id;
p.pPacket = s.replace(original_room_id, room_id);
// p.pPacket = s;
recording_data.push_back(p);
// qDebug() << "packet" << p.pTime << p.pPacket;
}
SetProcessed(true);
SetFinished(true);
ClearData();
}
void AssetRecording::SetRoomID(const QString s)
{
room_id = s;
}
QString AssetRecording::GetRoomID() const
{
return room_id;
}