-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAampCacheHandler.h
More file actions
executable file
·525 lines (471 loc) · 14.5 KB
/
AampCacheHandler.h
File metadata and controls
executable file
·525 lines (471 loc) · 14.5 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
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
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2018 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file AampCacheHandler.h
* @brief Cache handler for AAMP
*/
#ifndef __AAMP_CACHE_HANDLER_H__
#define __AAMP_CACHE_HANDLER_H__
#include <iostream>
#include <memory>
#include <unordered_map>
#include <exception>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <assert.h>
#include "AampMediaType.h"
#include "AampUtils.h"
#include "AampLogManager.h"
#include "AampDefine.h"
#define PLAYLIST_CACHE_SIZE_UNLIMITED -1
/**
* @brief AampCacheData to cache Initialization Fragments, HLS main manifest, and HLS VOD playlists
*
* For DASH playback, this module is not involved. Instead AampMPDDownloader handles caching the static VOD DASH manifest..
* Caches are cleared upon exiting from aamp player or by an async thread that automatically purges after ~10 seconds of no active playback.
*/
class AampCachedData
{
public:
std::string effectiveUrl;
std::shared_ptr<std::vector<uint8_t>> buffer;
AampMediaType mediaType;
long seqNo;
/**
* @brief Pointer to the "effectiveUrl" entry in the cache.
* For a Main URL entry, this points to the AampCachedData object
* keyed by the effectiveUrl. For an Alias entry, this is nullptr.
* use_count() on this shared_ptr provides an O(1) way to see
* how many URLs are currently redirected to the same effectiveUrl.
*/
std::shared_ptr<AampCachedData> eUrlCachedDataPtr{nullptr};
~AampCachedData() {};
/**
* @brief representation for a cache entry
*
* @param effectiveUrl if equal to url (primary key for cache), this is a self contained entry with no alternate effectiveUrl alias;
* if empty, this is a mirrored entry for the corresponding effectiveUrl, sharing storage with main entry;
* if populated and not same as url, this is main cache entry, with effectiveUrl also present in cache as an alias
*
* @param buffer data payload associated with cache entry (initialization fragment or playlist)
* @param mediaType type of cache entry
* @param seqNo bigger for more recent usage; used to drive LRU purging heuristic
*/
AampCachedData(const std::string &effectiveUrl, std::shared_ptr<std::vector<uint8_t>> buffer, AampMediaType mediaType)
: effectiveUrl(effectiveUrl), buffer(buffer), mediaType(mediaType), seqNo()
{
}
};
typedef enum
{
eCACHE_TYPE_INIT_FRAGMENT,
eCACHE_TYPE_PLAYLIST
} AampCacheType;
class AampCache
{
private:
AampCacheType cacheType;
size_t totalCachedBytes;
long seqNo;
/**
* @fn allocatePlaylistCacheSlot
* @param[in] mediaType type of playlist caller wants to add to cache
* @param[in] targetCacheSize threshold (bytes) for needed cache size reduction
*
* @return bool Success or Failure
*/
void reduceCacheSize(AampMediaType mediaType, size_t targetCacheSize)
{
// First pass - remove playlists only of specific type
auto iter = cache.begin();
AAMPLOG_WARN("removing %s playlists from cache", GetMediaTypeName(mediaType));
while (iter != cache.end())
{
AampCachedData *cachedData = iter->second.get();
if (cachedData->mediaType == eMEDIATYPE_MANIFEST || cachedData->mediaType != mediaType)
{ // leave main manifest and alternate playlist types
iter++;
}
else
{
iter = cache.erase(iter);
}
}
// Second Pass - if more reduction needed, remove other playlist types, too
if (totalCachedBytes > targetCacheSize)
{
AAMPLOG_WARN("removing ALL playlists from cache");
iter = cache.begin();
while (iter != cache.end())
{
AampCachedData *cachedData = iter->second.get();
if (cachedData->mediaType == eMEDIATYPE_MANIFEST)
{ // leave main manifest
iter++;
}
else
{
iter = cache.erase(iter);
}
}
}
}
bool makeRoomForPlaylist(AampMediaType mediaType, size_t bytesNeeded)
{
bool ok = true;
if (mediaType == eMEDIATYPE_MANIFEST)
{ // flush and old playlist files (associated with different manifest)
Clear();
}
else if (maxPlaylistCacheBytes != PLAYLIST_CACHE_SIZE_UNLIMITED)
{ // cache size constraint to be enforced
if (totalCachedBytes + bytesNeeded > maxPlaylistCacheBytes)
{
reduceCacheSize(mediaType, maxPlaylistCacheBytes - bytesNeeded);
ok = totalCachedBytes + bytesNeeded <= maxPlaylistCacheBytes;
}
}
return ok;
}
bool makeRoomForInitFragment(AampMediaType mediaType)
{
int count = 0;
auto lru = cache.end();
auto iter = cache.begin();
while (iter != cache.end())
{
AampCachedData *cachedData = iter->second.get();
if (cachedData->mediaType == mediaType && !cachedData->effectiveUrl.empty())
{
if (lru == cache.end() || cachedData->seqNo < lru->second->seqNo)
{
lru = iter;
}
count++;
}
iter++;
}
if (count >= maxCachedInitFragmentsPerTrack)
{
AAMPLOG_WARN("removing entry from %s init fragment cache", GetMediaTypeName(mediaType));
Remove(lru->first);
}
return true; // success
}
public:
int maxCachedInitFragmentsPerTrack;
int maxPlaylistCacheBytes;
std::unordered_map<std::string, std::shared_ptr<AampCachedData>> cache;
AampCache()
{
}
AampCache(AampCacheType cacheType) : cacheType(cacheType), cache(), totalCachedBytes(), maxPlaylistCacheBytes(MAX_PLAYLIST_CACHE_SIZE * 1024), maxCachedInitFragmentsPerTrack(MAX_INIT_FRAGMENT_CACHE_PER_TRACK), seqNo()
{
}
~AampCache()
{
}
public:
void Insert(const std::string &url, const std::vector<uint8_t> &buffer, const std::string &effectiveUrl, AampMediaType mediaType)
{
// High hit early exit scenario
if (cache.find(url) != cache.end())
{
AAMPLOG_ERR("%s %s already cached", GetMediaTypeName(mediaType), url.c_str());
return;
}
if (buffer.empty())
{
AAMPLOG_ERR("empty buffer");
return;
}
bool ok = false;
switch (cacheType)
{
case eCACHE_TYPE_INIT_FRAGMENT:
ok = makeRoomForInitFragment(mediaType);
break;
case eCACHE_TYPE_PLAYLIST:
ok = makeRoomForPlaylist(mediaType, buffer.size());
break;
default:
break;
}
if (ok)
{
try
{ // Do the allocations up front to avoid partial state updates on failure
size_t bytes = buffer.size();
auto cachedBuf = std::shared_ptr<std::vector<uint8_t>>(
new std::vector<uint8_t>(buffer),
[this, bytes](std::vector<uint8_t> *ptr)
{
this->totalCachedBytes -= bytes;
delete ptr;
});
// Update total cached bytes for the buffer as if a later allocation fails the dtor will then decrement it again leaving it correct.
totalCachedBytes += bytes;
auto cachedData = std::make_shared<AampCachedData>(effectiveUrl, cachedBuf, mediaType);
if (url != effectiveUrl)
{
std::shared_ptr<AampCachedData> aliasData = nullptr;
auto itEff = cache.find(effectiveUrl);
if (itEff == cache.end())
{
// only allocate when alias key not present
aliasData = std::make_shared<AampCachedData>("", cachedBuf,
mediaType);
cache.insert_or_assign(effectiveUrl, std::move(aliasData));
AAMPLOG_MIL("inserted %s eUrl %s",
GetMediaTypeName(mediaType), effectiveUrl.c_str());
}
else
{
// update existing alias to point to new buffer and metadata
aliasData = itEff->second;
aliasData->buffer = cachedBuf;
aliasData->mediaType = mediaType;
AAMPLOG_MIL("updated %s eUrl %s",
GetMediaTypeName(mediaType), effectiveUrl.c_str());
}
cachedData->eUrlCachedDataPtr = aliasData;
}
cachedData->seqNo = ++seqNo;
cache[url] = std::move(cachedData);
AAMPLOG_MIL("inserted %s %s", GetMediaTypeName(mediaType), url.c_str()); // used by l2tests
}
catch (const std::bad_alloc &e)
{
AAMPLOG_ERR("Memory allocation failed: %s", e.what());
}
}
}
void Remove(const std::string &url)
{
auto iter = cache.find(url);
assert(iter != cache.end());
AampCachedData *cachedData = iter->second.get();
assert(!cachedData->effectiveUrl.empty());
if (cachedData->eUrlCachedDataPtr)
{
if (cachedData->eUrlCachedDataPtr.use_count() == 2) // only this URL and effectiveUrl alias point to it
{
cache.erase(cachedData->effectiveUrl);
}
}
cache.erase(iter);
}
void Clear(void)
{
// smart ptr owned entries are destructed automatically
cache.clear();
totalCachedBytes = 0;
}
AampCachedData *Find(const std::string &url)
{
if (auto it = cache.find(url); it != cache.end())
{
it->second->seqNo = ++seqNo; // Update LRU priority
return it->second.get();
}
return nullptr;
}
};
/**
* @class AampCacheHandler
* @brief Handles Aamp cache operations
*/
class AampCacheHandler
{
private:
int mPlayerId;
std::mutex mCacheAccessMutex;
AampCache mPlaylistCache;
AampCache mInitFragmentCache;
bool mbCleanUpTaskInitialized;
bool mCacheActive;
bool mAsyncCacheCleanUpThread;
std::mutex mCondVarMutex;
std::condition_variable mCondVar;
std::thread mAsyncCleanUpTaskThreadId;
protected:
/**
* @brief Thread function for Async Cache clean
*/
void AsyncCacheCleanUpTask(void)
{
UsingPlayerId playerId(mPlayerId);
std::unique_lock<std::mutex> lock(mCondVarMutex);
while (mAsyncCacheCleanUpThread)
{
mCondVar.wait(lock);
if (!mCacheActive)
{
std::cv_status status = mCondVar.wait_for(lock, std::chrono::seconds(10));
if (status == std::cv_status::timeout)
{
AAMPLOG_MIL("[%p] Cacheflush timed out", this);
mPlaylistCache.Clear();
mInitFragmentCache.Clear();
}
}
}
}
/**
* @fn Init
*/
void InitializeIfNeeded(void)
{
if (!mbCleanUpTaskInitialized)
{
try
{
mAsyncCleanUpTaskThreadId = std::thread(&AampCacheHandler::AsyncCacheCleanUpTask, this);
{
std::lock_guard<std::mutex> guard(mCondVarMutex);
mAsyncCacheCleanUpThread = true;
}
AAMPLOG_INFO("Thread created AsyncCacheCleanUpTask[%zx]", GetPrintableThreadID(mAsyncCleanUpTaskThreadId));
}
catch (std::exception &e)
{
AAMPLOG_ERR("Failed to create AampCacheHandler thread : %s", e.what());
}
mbCleanUpTaskInitialized = true;
}
}
/**
* @brief Clear Cache Handler. Exit clean up thread.
*/
void ClearCacheHandler(void)
{
if (mbCleanUpTaskInitialized)
{
mCacheActive = true;
{
std::lock_guard<std::mutex> guard(mCondVarMutex);
mAsyncCacheCleanUpThread = false;
mCondVar.notify_one();
}
if (mAsyncCleanUpTaskThreadId.joinable())
{
mAsyncCleanUpTaskThreadId.join();
}
mPlaylistCache.Clear();
mInitFragmentCache.Clear();
mbCleanUpTaskInitialized = false;
}
}
public:
/**
* @brief constructor
*/
AampCacheHandler(int playerId);
/**
* @brief destructor
*/
~AampCacheHandler(void);
/**
* @brief Start playlist caching
*/
void StartPlaylistCache(void);
/**
* @brief Stop playlist caching
*/
void StopPlaylistCache(void);
/**
* @brief Add playlist to cache
* @param[in] url URL identifying the playlist to cache
* @param[in] buffer Reference to the payload buffer (std::vector<uint8_t>) to store in cache
* @param[in] effectiveUrl Final/effective URL for the resource (used as aliasing key)
* @param[in] isLive True if this is a live playlist
* @param[in] mediaType Type of the file inserted (see AampMediaType)
* @return void
*/
void InsertToPlaylistCache(const std::string &url, const std::vector<uint8_t> &buffer, const std::string &effectiveUrl, bool isLive, AampMediaType mediaType);
/**
* @brief Find playlist in cache
* @param[in] url URL to look up in cache
* @param[out] buffer Reference to a vector which will be replaced with the cached payload on a hit
* @param[out] effectiveUrl The effective URL associated with the cached entry (returned on hit)
* @param[in] mediaType Expected media type for this lookup (guards cache matching)
* @return true if entry found and buffer/effectiveUrl were set, false otherwise
*/
bool RetrieveFromPlaylistCache(const std::string &url, std::vector<uint8_t> &buffer, std::string &effectiveUrl, AampMediaType mediaType);
/**
* @brief Remove playlist from cache
* @param[in] url - URL
*/
void RemoveFromPlaylistCache(const std::string &url);
/**
* @brief set max playlist cache size (bytes)
*/
void SetMaxPlaylistCacheSize(int maxBytes);
/**
* @brief get max playlist cache size (bytes)
*
* @return int - maxCacheSize
*/
int GetMaxPlaylistCacheSize() { return mPlaylistCache.maxPlaylistCacheBytes; }
/**
* @brief check if playlist in cache
*/
bool IsPlaylistUrlCached(const std::string &playlistUrl);
/**
* @brief Add initialization fragment to cache
* @param[in] url URL identifying the initialization fragment
* @param[in] buffer Reference to the payload buffer (std::vector<uint8_t>) to store in cache
* @param[in] effectiveUrl Final/effective URL for the resource
* @param[in] mediaType Type of the file inserted (initial fragment media type)
* @return void
*/
void InsertToInitFragCache(const std::string &url, const std::vector<uint8_t> &buffer, const std::string &effectiveUrl, AampMediaType mediaType);
/**
* @brief Find initialization fragment in cache
* @param[in] url URL to look up in cache
* @param[out] buffer Reference to a vector which will be replaced with the cached payload on a hit
* @param[out] effectiveUrl The effective URL associated with the cached entry (returned on hit)
* @return true if entry found and buffer/effectiveUrl were set, false otherwise
*/
bool RetrieveFromInitFragmentCache(const std::string &url, std::vector<uint8_t> &buffer, std::string &effectiveUrl);
/**
* @brief set max initialization fragments allowed in cache (per track)
*
* @param[in] maxInitFragCacheSz - CacheSize
*
* @return None
*/
void SetMaxInitFragCacheSize(int maxFragmentsPerTrack);
/**
* @brief GetMaxPlaylistCacheSize - Get present CacheSize
*
* @return int - maxCacheSize
*/
int GetMaxInitFragCacheSize() { return mInitFragmentCache.maxCachedInitFragmentsPerTrack; }
/**
* @brief Copy constructor disabled
*/
AampCacheHandler(const AampCacheHandler &) = delete;
/**
* @brief assignment operator disabled
*/
AampCacheHandler &operator=(const AampCacheHandler &) = delete;
};
#endif