forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachingreader.cpp
533 lines (453 loc) · 18.6 KB
/
cachingreader.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include <QtDebug>
#include <QFileInfo>
#include "controlobject.h"
#include "controlobjectthread.h"
#include "cachingreader.h"
#include "trackinfoobject.h"
#include "soundsourceproxy.h"
#include "sampleutil.h"
#include "util/counter.h"
#include "util/math.h"
#include "util/assert.h"
// currently CachingReaderWorker::kChunkLength is 65536 (0x10000);
// For 80 chunks we need 5242880 (0x500000) bytes (5 MiB) of Memory
//static
const int CachingReader::maximumChunksInMemory = 80;
CachingReader::CachingReader(QString group,
ConfigObject<ConfigValue>* config)
: m_pConfig(config),
m_chunkReadRequestFIFO(1024),
m_readerStatusFIFO(1024),
m_readerStatus(INVALID),
m_mruChunk(NULL),
m_lruChunk(NULL),
m_pRawMemoryBuffer(NULL),
m_iTrackNumSamplesCallbackSafe(0) {
int rawMemoryBufferLength = CachingReaderWorker::kSamplesPerChunk * maximumChunksInMemory;
m_pRawMemoryBuffer = new CSAMPLE[rawMemoryBufferLength];
m_allocatedChunks.reserve(maximumChunksInMemory);
CSAMPLE* bufferStart = m_pRawMemoryBuffer;
// Divide up the allocated raw memory buffer into total_chunks
// chunks. Initialize each chunk to hold nothing and add it to the free
// list.
for (int i = 0; i < maximumChunksInMemory; ++i) {
Chunk* c = new Chunk;
c->chunk_number = -1;
c->length = 0;
c->data = bufferStart;
c->next_lru = NULL;
c->prev_lru = NULL;
c->state = Chunk::FREE;
m_chunks.push_back(c);
m_freeChunks.push_back(c);
bufferStart += CachingReaderWorker::kSamplesPerChunk;
}
m_pWorker = new CachingReaderWorker(group,
&m_chunkReadRequestFIFO,
&m_readerStatusFIFO);
// Forward signals from worker
connect(m_pWorker, SIGNAL(trackLoading()),
this, SIGNAL(trackLoading()),
Qt::DirectConnection);
connect(m_pWorker, SIGNAL(trackLoaded(TrackPointer, int, int)),
this, SIGNAL(trackLoaded(TrackPointer, int, int)),
Qt::DirectConnection);
connect(m_pWorker, SIGNAL(trackLoadFailed(TrackPointer, QString)),
this, SIGNAL(trackLoadFailed(TrackPointer, QString)),
Qt::DirectConnection);
m_pWorker->start(QThread::HighPriority);
}
CachingReader::~CachingReader() {
m_pWorker->quitWait();
delete m_pWorker;
m_freeChunks.clear();
m_allocatedChunks.clear();
m_lruChunk = m_mruChunk = NULL;
qDeleteAll(m_chunks);
delete [] m_pRawMemoryBuffer;
m_pRawMemoryBuffer = NULL;
}
// static
Chunk* CachingReader::removeFromLRUList(Chunk* chunk, Chunk* head) {
if (chunk == NULL) {
qDebug() << "ERROR: NULL chunk argument to removeFromLRUList";
return NULL;
}
// Remove chunk from the doubly-linked list.
Chunk* next = chunk->next_lru;
Chunk* prev = chunk->prev_lru;
if (next) {
next->prev_lru = prev;
}
if (prev) {
prev->next_lru = next;
}
chunk->next_lru = NULL;
chunk->prev_lru = NULL;
if (chunk == head)
return next;
return head;
}
// static
Chunk* CachingReader::insertIntoLRUList(Chunk* chunk, Chunk* head) {
if (chunk == NULL) {
qDebug() << "ERROR: NULL chunk argument to insertIntoLRUList";
return NULL;
}
// Chunk is the new head of the list, so connect the head as the next from
// chunk.
chunk->next_lru = head;
chunk->prev_lru = NULL;
// If there are any elements in the list, point their prev pointer back at
// chunk since it is the new head
if (head) {
head->prev_lru = chunk;
}
// Chunk is the new head
return chunk;
}
void CachingReader::freeChunk(Chunk* pChunk) {
int removed = m_allocatedChunks.remove(pChunk->chunk_number);
// We'll tolerate not being in allocatedChunks because sometime you free a
// chunk right after you allocated it.
if (removed > 1) {
qDebug() << "ERROR: freeChunk free'd a chunk that was multiply-allocated.";
}
// If this is the LRU chunk then set its previous LRU chunk to the LRU
if (m_lruChunk == pChunk) {
m_lruChunk = pChunk->prev_lru;
}
m_mruChunk = removeFromLRUList(pChunk, m_mruChunk);
pChunk->state = Chunk::FREE;
pChunk->chunk_number = -1;
pChunk->length = 0;
m_freeChunks.push_back(pChunk);
}
void CachingReader::freeAllChunks() {
m_allocatedChunks.clear();
m_mruChunk = NULL;
for (QVector<Chunk*>::const_iterator it = m_chunks.constBegin();
it != m_chunks.constEnd(); ++it) {
Chunk* pChunk = *it;
// We will receive CHUNK_READ_INVALID for all pending chunk reads which
// should free the chunks individually.
if (pChunk->state == Chunk::READ_IN_PROGRESS) {
continue;
}
if (pChunk->state != Chunk::FREE) {
pChunk->state = Chunk::FREE;
pChunk->chunk_number = -1;
pChunk->length = 0;
pChunk->next_lru = NULL;
pChunk->prev_lru = NULL;
m_freeChunks.append(pChunk);
}
}
}
Chunk* CachingReader::allocateChunk(int chunk) {
if (m_freeChunks.isEmpty()) {
return NULL;
}
Chunk* pChunk = m_freeChunks.takeFirst();
pChunk->state = Chunk::ALLOCATED;
pChunk->chunk_number = chunk;
//qDebug() << "Allocating chunk" << pChunk << pChunk->chunk_number;
m_allocatedChunks.insert(pChunk->chunk_number, pChunk);
// Insert the chunk into the least-recently-used linked list as the "most
// recently used" item.
m_mruChunk = insertIntoLRUList(pChunk, m_mruChunk);
// If this chunk has no next least-recently-used pointer then it is the
// least recently used chunk despite having just been allocated. This only
// happens if this is the first allocated chunk.
if (pChunk->next_lru == NULL) {
m_lruChunk = pChunk;
}
return pChunk;
}
Chunk* CachingReader::allocateChunkExpireLRU(int chunk) {
Chunk* pChunk = allocateChunk(chunk);
if (pChunk == NULL) {
if (m_lruChunk == NULL) {
qDebug() << "ERROR: No LRU chunk to free in allocateChunkExpireLRU.";
return NULL;
}
//qDebug() << "Expiring LRU" << m_lruChunk << m_lruChunk->chunk_number;
freeChunk(m_lruChunk);
pChunk = allocateChunk(chunk);
}
//qDebug() << "allocateChunkExpireLRU" << chunk << pChunk;
return pChunk;
}
Chunk* CachingReader::lookupChunk(int chunk_number) {
// Defaults to NULL if it's not in the hash.
Chunk* chunk = m_allocatedChunks.value(chunk_number, NULL);
// Make sure the allocated number matches the indexed chunk number.
DEBUG_ASSERT(chunk == NULL || chunk_number == chunk->chunk_number);
return chunk;
}
void CachingReader::freshenChunk(Chunk* pChunk) {
// If this is the LRU chunk then set its previous LRU to be the new LRU.
if (pChunk == m_lruChunk && pChunk->prev_lru != NULL) {
m_lruChunk = pChunk->prev_lru;
}
// Remove the chunk from the LRU list and insert it at the head so that it
// is now the most recently used chunk.
m_mruChunk = removeFromLRUList(pChunk, m_mruChunk);
m_mruChunk = insertIntoLRUList(pChunk, m_mruChunk);
}
Chunk* CachingReader::lookupChunkAndFreshen(int chunk_number) {
Chunk* pChunk = lookupChunk(chunk_number);
if (pChunk != NULL) {
freshenChunk(pChunk);
}
return pChunk;
}
void CachingReader::newTrack(TrackPointer pTrack) {
m_pWorker->newTrack(pTrack);
m_pWorker->workReady();
}
void CachingReader::process() {
ReaderStatusUpdate status;
while (m_readerStatusFIFO.read(&status, 1) == 1) {
// qDebug() << "Got ReaderStatusUpdate:" << status.status
// << (status.chunk ? status.chunk->chunk_number : -1);
if (status.status == TRACK_NOT_LOADED) {
m_readerStatus = status.status;
} else if (status.status == TRACK_LOADED) {
freeAllChunks();
m_readerStatus = status.status;
m_iTrackNumSamplesCallbackSafe = status.trackNumSamples;
} else if (status.status == CHUNK_READ_SUCCESS) {
Chunk* pChunk = status.chunk;
// This should not be possible unless there is a bug in
// CachingReaderWorker. If it is NULL then the only thing we can do
// is to skip this ReaderStatusUpdate.
DEBUG_ASSERT_AND_HANDLE(pChunk != NULL) {
continue;
}
// After a read success the state ought to be READ_IN_PROGRESS.
DEBUG_ASSERT(pChunk->state == Chunk::READ_IN_PROGRESS);
// Switch state to READ.
pChunk->state = Chunk::READ;
} else if (status.status == CHUNK_READ_EOF) {
Chunk* pChunk = status.chunk;
if (pChunk == NULL) {
qDebug() << "ERROR: status.chunk is NULL in CHUNK_READ_EOF ReaderStatusUpdate. Ignoring update.";
continue;
}
DEBUG_ASSERT(pChunk->state == Chunk::READ_IN_PROGRESS);
freeChunk(pChunk);
} else if (status.status == CHUNK_READ_INVALID) {
qDebug() << "WARNING: READER THREAD RECEIVED INVALID CHUNK READ";
Chunk* pChunk = status.chunk;
if (pChunk == NULL) {
qDebug() << "ERROR: status.chunk is NULL in CHUNK_READ_INVALID ReaderStatusUpdate. Ignoring update.";
continue;
}
DEBUG_ASSERT(pChunk->state == Chunk::READ_IN_PROGRESS);
freeChunk(pChunk);
}
}
}
int CachingReader::read(int sample, int num_samples, CSAMPLE* buffer) {
// Check for bad inputs
DEBUG_ASSERT_AND_HANDLE(sample % 2 == 0) {
// This problem is easy to fix, but this type of call should be
// complained about loudly.
--sample;
}
DEBUG_ASSERT_AND_HANDLE(num_samples % 2 == 0) {
--num_samples;
}
if (num_samples < 0 || !buffer) {
QString temp = QString("Sample = %1").arg(sample);
qDebug() << "CachingReader::read() invalid arguments sample:" << sample
<< "num_samples:" << num_samples << "buffer:" << buffer;
return 0;
}
// If asked to read 0 samples, don't do anything. (this is a perfectly
// reasonable request that happens sometimes. If no track is loaded, don't
// do anything.
if (num_samples == 0 || m_readerStatus != TRACK_LOADED) {
return 0;
}
// Process messages from the reader thread.
process();
// TODO: is it possible to move this code out of caching reader
// and into enginebuffer? It doesn't quite make sense here, although
// it makes preroll completely transparent to the rest of the code
//if we're in preroll...
int zerosWritten = 0;
if (sample < 0) {
if (sample + num_samples <= 0) {
//everything is zeros, easy
SampleUtil::clear(buffer, num_samples);
return num_samples;
} else {
//some of the buffer is zeros, some is from the file
SampleUtil::clear(buffer, 0 - sample);
buffer += (0 - sample);
num_samples = sample + num_samples;
zerosWritten = (0 - sample);
sample = 0;
//continue processing the rest of the chunks normally
}
}
int start_sample = math_min(m_iTrackNumSamplesCallbackSafe,
sample);
int start_chunk = chunkForSample(start_sample);
int end_sample = math_min(m_iTrackNumSamplesCallbackSafe,
sample + num_samples - 1);
int end_chunk = chunkForSample(end_sample);
int samples_remaining = num_samples;
int current_sample = sample;
// Sanity checks
if (start_chunk > end_chunk) {
qDebug() << "CachingReader::read() bad chunk range to read ["
<< start_chunk << end_chunk << "]";
return 0;
}
for (int chunk_num = start_chunk; chunk_num <= end_chunk; chunk_num++) {
Chunk* current = lookupChunkAndFreshen(chunk_num);
// If the chunk is not in cache, then we must return an error.
if (current == NULL || current->state != Chunk::READ) {
// qDebug() << "Couldn't get chunk " << chunk_num
// << " in read() of [" << sample << "," << sample + num_samples
// << "] chunks " << start_chunk << "-" << end_chunk;
// Something is wrong. Break out of the loop, that should fill the
// samples requested with zeroes.
Counter("CachingReader::read cache miss")++;
break;
}
int chunk_start_sample = CachingReaderWorker::sampleForChunk(chunk_num);
int chunk_offset = current_sample - chunk_start_sample;
int chunk_remaining_samples = current->length - chunk_offset;
// More sanity checks
if (current_sample < chunk_start_sample || current_sample % 2 != 0) {
qDebug() << "CachingReader::read() bad chunk parameters"
<< "chunk_start_sample" << chunk_start_sample
<< "current_sample" << current_sample;
break;
}
// If we're past the start_chunk then current_sample should be
// chunk_start_sample.
if (start_chunk != chunk_num && chunk_start_sample != current_sample) {
qDebug() << "CachingReader::read() bad chunk parameters"
<< "chunk_num" << chunk_num
<< "start_chunk" << start_chunk
<< "chunk_start_sample" << chunk_start_sample
<< "current_sample" << current_sample;
break;
}
if (samples_remaining < 0) {
qDebug() << "CachingReader::read() bad samples remaining"
<< samples_remaining;
break;
}
// It is completely possible that chunk_remaining_samples is less than
// zero. If the caller is trying to read from beyond the end of the
// file, then this can happen. We should tolerate it.
if (chunk_remaining_samples < 0) {
chunk_remaining_samples = 0;
}
int samples_to_read = math_clamp(samples_remaining, 0,
chunk_remaining_samples);
// If we did not decide to read any samples from this chunk then that
// means we have exhausted all the samples in the song.
if (samples_to_read == 0) {
break;
}
// samples_to_read should be non-negative and even
if (samples_to_read < 0 || samples_to_read % 2 != 0) {
qDebug() << "CachingReader::read() samples_to_read invalid"
<< samples_to_read;
break;
}
CSAMPLE *data = current->data + chunk_offset;
SampleUtil::copy(buffer, data, samples_to_read);
buffer += samples_to_read;
current_sample += samples_to_read;
samples_remaining -= samples_to_read;
}
// If we didn't supply all the samples requested, that probably means we're
// at the end of the file, or something is wrong. Provide zeroes and pretend
// all is well. The caller can't be bothered to check how long the file is.
SampleUtil::clear(buffer, samples_remaining);
samples_remaining = 0;
// if (samples_remaining != 0) {
// qDebug() << "CachingReader::read() did read all requested samples.";
// }
return zerosWritten + num_samples - samples_remaining;
}
void CachingReader::hintAndMaybeWake(const HintVector& hintList) {
// If no file is loaded, skip.
if (m_readerStatus != TRACK_LOADED) {
return;
}
// To prevent every bit of code having to guess how many samples
// forward it makes sense to keep in memory, the hinter can provide
// either 0 for a forward hint or -1 for a backward hint. We should
// be calculating an appropriate number of samples to go backward as
// some function of the latency, but for now just leave this as a
// constant. 2048 is a pretty good number of samples because 25ms
// latency corresponds to 1102.5 mono samples and we need double
// that for stereo samples.
const int default_samples = 2048;
// For every chunk that the hints indicated, check if it is in the cache. If
// any are not, then wake.
bool shouldWake = false;
for (HintVector::const_iterator it = hintList.constBegin();
it != hintList.constEnd(); ++it) {
// Copy, don't use reference.
Hint hint = *it;
if (hint.length == 0) {
hint.length = default_samples;
} else if (hint.length == -1) {
hint.sample -= default_samples;
hint.length = default_samples;
if (hint.sample < 0) {
hint.length += hint.sample;
hint.sample = 0;
}
}
if (hint.length < 0) {
qDebug() << "ERROR: Negative hint length. Ignoring.";
continue;
}
int start_sample = math_clamp(hint.sample, 0,
m_iTrackNumSamplesCallbackSafe);
int start_chunk = chunkForSample(start_sample);
int end_sample = math_clamp(hint.sample + hint.length - 1, 0,
m_iTrackNumSamplesCallbackSafe);
int end_chunk = chunkForSample(end_sample);
for (int current = start_chunk; current <= end_chunk; ++current) {
Chunk* pChunk = lookupChunk(current);
if (pChunk == NULL) {
shouldWake = true;
Chunk* pChunk = allocateChunkExpireLRU(current);
if (pChunk == NULL) {
qDebug() << "ERROR: Couldn't allocate spare Chunk to make ChunkReadRequest.";
continue;
}
pChunk->state = Chunk::READ_IN_PROGRESS;
ChunkReadRequest request;
request.chunk = pChunk;
// qDebug() << "Requesting read of chunk" << current << "into" << pChunk;
// qDebug() << "Requesting read into " << request.chunk->data;
if (m_chunkReadRequestFIFO.write(&request, 1) != 1) {
qDebug() << "ERROR: Could not submit read request for "
<< current;
}
//qDebug() << "Checking chunk " << current << " shouldWake:" << shouldWake << " chunksToRead" << m_chunksToRead.size();
} else if (pChunk->state == Chunk::READ) {
// This will cause the chunk to be 'freshened' in the cache. The
// chunk will be moved to the end of the LRU list.
freshenChunk(pChunk);
}
}
}
// If there are chunks to be read, wake up.
if (shouldWake) {
m_pWorker->workReady();
}
}