-
Notifications
You must be signed in to change notification settings - Fork 261
/
CacheDisk.cpp
526 lines (433 loc) · 14.8 KB
/
CacheDisk.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
/**
* @file
* @brief Source file for CacheDisk class
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/
/* LICENSE
*
* Copyright (c) 2008-2019 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) 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 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) 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 OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/CacheDisk.h"
#include "../include/QtUtilities.h"
#include <Qt>
#include <QString>
#include <QTextStream>
using namespace std;
using namespace openshot;
// Default constructor, no max bytes
CacheDisk::CacheDisk(std::string cache_path, std::string format, float quality, float scale) : CacheBase(0) {
// Set cache type name
cache_type = "CacheDisk";
range_version = 0;
needs_range_processing = false;
frame_size_bytes = 0;
image_format = format;
image_quality = quality;
image_scale = scale;
max_bytes = 0;
// Init path directory
InitPath(cache_path);
}
// Constructor that sets the max bytes to cache
CacheDisk::CacheDisk(std::string cache_path, std::string format, float quality, float scale, int64_t max_bytes) : CacheBase(max_bytes) {
// Set cache type name
cache_type = "CacheDisk";
range_version = 0;
needs_range_processing = false;
frame_size_bytes = 0;
image_format = format;
image_quality = quality;
image_scale = scale;
// Init path directory
InitPath(cache_path);
}
// Initialize cache directory
void CacheDisk::InitPath(std::string cache_path) {
QString qpath;
if (!cache_path.empty()) {
// Init QDir with cache directory
qpath = QString(cache_path.c_str());
} else {
// Init QDir with user's temp directory
qpath = QDir::tempPath() + QString("/preview-cache/");
}
// Init QDir with cache directory
path = QDir(qpath);
// Check if cache directory exists
if (!path.exists())
// Create
path.mkpath(qpath);
}
// Calculate ranges of frames
void CacheDisk::CalculateRanges() {
// Only calculate when something has changed
if (needs_range_processing) {
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
// Sort ordered frame #s, and calculate JSON ranges
std::sort(ordered_frame_numbers.begin(), ordered_frame_numbers.end());
// Clear existing JSON variable
Json::Value ranges = Json::Value(Json::arrayValue);
// Increment range version
range_version++;
int64_t starting_frame = *ordered_frame_numbers.begin();
int64_t ending_frame = starting_frame;
// Loop through all known frames (in sequential order)
for (const auto frame_number : ordered_frame_numbers) {
if (frame_number - ending_frame > 1) {
// End of range detected
Json::Value range;
// Add JSON object with start/end attributes
// Use strings, since int64_ts are supported in JSON
range["start"] = std::to_string(starting_frame);
range["end"] = std::to_string(ending_frame);
ranges.append(range);
// Set new starting range
starting_frame = frame_number;
}
// Set current frame as end of range, and keep looping
ending_frame = frame_number;
}
// APPEND FINAL VALUE
Json::Value range;
// Add JSON object with start/end attributes
// Use strings, since int64_ts are supported in JSON
range["start"] = std::to_string(starting_frame);
range["end"] = std::to_string(ending_frame);
ranges.append(range);
// Cache range JSON as string
json_ranges = ranges.toStyledString();
// Reset needs_range_processing
needs_range_processing = false;
}
}
// Default destructor
CacheDisk::~CacheDisk()
{
frames.clear();
frame_numbers.clear();
ordered_frame_numbers.clear();
// remove critical section
delete cacheCriticalSection;
cacheCriticalSection = NULL;
}
// Add a Frame to the cache
void CacheDisk::Add(std::shared_ptr<Frame> frame)
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
int64_t frame_number = frame->number;
// Freshen frame if it already exists
if (frames.count(frame_number))
// Move frame to front of queue
MoveToFront(frame_number);
else
{
// Add frame to queue and map
frames[frame_number] = frame_number;
frame_numbers.push_front(frame_number);
ordered_frame_numbers.push_back(frame_number);
needs_range_processing = true;
// Save image to disk (if needed)
QString frame_path(path.path() + "/" + QString("%1.").arg(frame_number) + QString(image_format.c_str()).toLower());
frame->Save(frame_path.toStdString(), image_scale, image_format, image_quality);
if (frame_size_bytes == 0) {
// Get compressed size of frame image (to correctly apply max size against)
QFile image_file(frame_path);
frame_size_bytes = image_file.size();
}
// Save audio data (if needed)
if (frame->has_audio_data) {
QString audio_path(path.path() + "/" + QString("%1").arg(frame_number) + ".audio");
QFile audio_file(audio_path);
if (audio_file.open(QIODevice::WriteOnly)) {
QTextStream audio_stream(&audio_file);
audio_stream << frame->SampleRate() << Qt::endl;
audio_stream << frame->GetAudioChannelsCount() << Qt::endl;
audio_stream << frame->GetAudioSamplesCount() << Qt::endl;
audio_stream << frame->ChannelsLayout() << Qt::endl;
// Loop through all samples
for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++)
{
// Get audio for this channel
float *samples = frame->GetAudioSamples(channel);
for (int sample = 0; sample < frame->GetAudioSamplesCount(); sample++)
audio_stream << samples[sample] << Qt::endl;
}
}
}
// Clean up old frames
CleanUp();
}
}
// Get a frame from the cache (or NULL shared_ptr if no frame is found)
std::shared_ptr<Frame> CacheDisk::GetFrame(int64_t frame_number)
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
// Does frame exists in cache?
if (frames.count(frame_number)) {
// Does frame exist on disk
QString frame_path(path.path() + "/" + QString("%1.").arg(frame_number) + QString(image_format.c_str()).toLower());
if (path.exists(frame_path)) {
// Load image file
std::shared_ptr<QImage> image = std::shared_ptr<QImage>(new QImage());
image->load(frame_path);
// Set pixel formatimage->
image = std::shared_ptr<QImage>(new QImage(image->convertToFormat(QImage::Format_ARGB32_Premultiplied)));
// Create frame object
std::shared_ptr<Frame> frame(new Frame());
frame->number = frame_number;
frame->AddImage(image);
// Get audio data (if found)
QString audio_path(path.path() + "/" + QString("%1").arg(frame_number) + ".audio");
QFile audio_file(audio_path);
if (audio_file.exists()) {
// Open audio file
QTextStream in(&audio_file);
if (audio_file.open(QIODevice::ReadOnly)) {
int sample_rate = in.readLine().toInt();
int channels = in.readLine().toInt();
int sample_count = in.readLine().toInt();
int channel_layout = in.readLine().toInt();
// Set basic audio properties
frame->ResizeAudio(channels, sample_count, sample_rate, (ChannelLayout) channel_layout);
// Loop through audio samples and add to frame
int current_channel = 0;
int current_sample = 0;
float *channel_samples = new float[sample_count];
while (!in.atEnd()) {
// Add sample to channel array
channel_samples[current_sample] = in.readLine().toFloat();
current_sample++;
if (current_sample == sample_count) {
// Add audio to frame
frame->AddAudio(true, current_channel, 0, channel_samples, sample_count, 1.0);
// Increment channel, and reset sample position
current_channel++;
current_sample = 0;
}
}
}
}
// return the Frame object
return frame;
}
}
// no Frame found
return std::shared_ptr<Frame>();
}
// Get the smallest frame number (or NULL shared_ptr if no frame is found)
std::shared_ptr<Frame> CacheDisk::GetSmallestFrame()
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
std::shared_ptr<openshot::Frame> f;
// Loop through frame numbers
std::deque<int64_t>::iterator itr;
int64_t smallest_frame = -1;
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
{
if (*itr < smallest_frame || smallest_frame == -1)
smallest_frame = *itr;
}
// Return frame
f = GetFrame(smallest_frame);
return f;
}
// Gets the maximum bytes value
int64_t CacheDisk::GetBytes()
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
int64_t total_bytes = 0;
// Loop through frames, and calculate total bytes
std::deque<int64_t>::reverse_iterator itr;
for(itr = frame_numbers.rbegin(); itr != frame_numbers.rend(); ++itr)
total_bytes += frame_size_bytes;
return total_bytes;
}
// Remove a specific frame
void CacheDisk::Remove(int64_t frame_number)
{
Remove(frame_number, frame_number);
}
// Remove range of frames
void CacheDisk::Remove(int64_t start_frame_number, int64_t end_frame_number)
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
// Loop through frame numbers
std::deque<int64_t>::iterator itr;
for(itr = frame_numbers.begin(); itr != frame_numbers.end();)
{
//deque<int64_t>::iterator current = itr++;
if (*itr >= start_frame_number && *itr <= end_frame_number)
{
// erase frame number
itr = frame_numbers.erase(itr);
} else
itr++;
}
// Loop through ordered frame numbers
std::vector<int64_t>::iterator itr_ordered;
for(itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end();)
{
if (*itr_ordered >= start_frame_number && *itr_ordered <= end_frame_number)
{
// erase frame number
frames.erase(*itr_ordered);
// Remove the image file (if it exists)
QString frame_path(path.path() + "/" + QString("%1.").arg(*itr_ordered) + QString(image_format.c_str()).toLower());
QFile image_file(frame_path);
if (image_file.exists())
image_file.remove();
// Remove audio file (if it exists)
QString audio_path(path.path() + "/" + QString("%1").arg(*itr_ordered) + ".audio");
QFile audio_file(audio_path);
if (audio_file.exists())
audio_file.remove();
itr_ordered = ordered_frame_numbers.erase(itr_ordered);
} else
itr_ordered++;
}
// Needs range processing (since cache has changed)
needs_range_processing = true;
}
// Move frame to front of queue (so it lasts longer)
void CacheDisk::MoveToFront(int64_t frame_number)
{
// Does frame exists in cache?
if (frames.count(frame_number))
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
// Loop through frame numbers
std::deque<int64_t>::iterator itr;
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
{
if (*itr == frame_number)
{
// erase frame number
frame_numbers.erase(itr);
// add frame number to 'front' of queue
frame_numbers.push_front(frame_number);
break;
}
}
}
}
// Clear the cache of all frames
void CacheDisk::Clear()
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
// Clear all containers
frames.clear();
frame_numbers.clear();
ordered_frame_numbers.clear();
needs_range_processing = true;
frame_size_bytes = 0;
// Delete cache directory, and recreate it
QString current_path = path.path();
path.removeRecursively();
// Re-init folder
InitPath(current_path.toStdString());
}
// Count the frames in the queue
int64_t CacheDisk::Count()
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
// Return the number of frames in the cache
return frames.size();
}
// Clean up cached frames that exceed the number in our max_bytes variable
void CacheDisk::CleanUp()
{
// Do we auto clean up?
if (max_bytes > 0)
{
// Create a scoped lock, to protect the cache from multiple threads
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
while (GetBytes() > max_bytes && frame_numbers.size() > 20)
{
// Get the oldest frame number.
int64_t frame_to_remove = frame_numbers.back();
// Remove frame_number and frame
Remove(frame_to_remove);
}
}
}
// Generate JSON string of this object
std::string CacheDisk::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value CacheDisk::JsonValue() {
// Process range data (if anything has changed)
CalculateRanges();
// Create root json object
Json::Value root = CacheBase::JsonValue(); // get parent properties
root["type"] = cache_type;
root["path"] = path.path().toStdString();
Json::Value version;
std::stringstream range_version_str;
range_version_str << range_version;
root["version"] = range_version_str.str();
// Parse and append range data (if any)
// Parse and append range data (if any)
try {
const Json::Value ranges = openshot::stringToJson(json_ranges);
root["ranges"] = ranges;
} catch (...) { }
// return JsonValue
return root;
}
// Load JSON string into this object
void CacheDisk::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void CacheDisk::SetJsonValue(const Json::Value root) {
// Close timeline before we do anything (this also removes all open and closing clips)
Clear();
// Set parent data
CacheBase::SetJsonValue(root);
if (!root["type"].isNull())
cache_type = root["type"].asString();
if (!root["path"].isNull())
// Update duration of timeline
InitPath(root["path"].asString());
}