forked from rdkcmf/rdk-aamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AampCacheHandler.cpp
357 lines (338 loc) · 11.3 KB
/
AampCacheHandler.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
/*
* 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.
*/
#include "AampCacheHandler.h"
/**
* @brief Insert playlist to playlist cache
* @param url URL corresponding to playlist
* @param buffer Contains the playlist
* @param effectiveUrl Effective URL of playlist
*/
void AampCacheHandler::InsertToPlaylistCache(const std::string url, const GrowableBuffer* buffer, std::string effectiveUrl,bool trackLiveStatus,MediaType fileType)
{
PlayListCachedData *tmpData,*newtmpData;
pthread_mutex_lock(&mMutex);
// First check point , Caching is allowed only if its VOD and for Main Manifest(HLS) for both VOD/Live
// For Main manifest , fileType will bypass storing for live content
if(trackLiveStatus==false || fileType==eMEDIATYPE_MANIFEST)
{
PlaylistCacheIter it = mPlaylistCache.find(url);
if (it != mPlaylistCache.end())
{
logprintf("%s:%d : playlist %s already present in cache", __FUNCTION__, __LINE__, url.c_str());
}
// insert only if buffer size is less than Max size
else
{
if(fileType==eMEDIATYPE_MANIFEST && mPlaylistCache.size())
{
// If new Manifest is inserted which is not present in the cache , flush out other playlist files related with old manifest,
ClearPlaylistCache();
}
// Dont check for CacheSize if Max is configured as unlimited
if(mMaxPlaylistCacheSize == PLAYLIST_CACHE_SIZE_UNLIMITED || (mMaxPlaylistCacheSize != PLAYLIST_CACHE_SIZE_UNLIMITED && buffer->len < mMaxPlaylistCacheSize))
{
// Before inserting into cache, need to check if max cache size will exceed or not on adding new data
// if more , need to pop out some from same type of playlist
bool cacheStoreReady = true;
if(mMaxPlaylistCacheSize != PLAYLIST_CACHE_SIZE_UNLIMITED && ((mCacheStoredSize + buffer->len) > mMaxPlaylistCacheSize))
{
AAMPLOG_WARN("[%s][%d] Count[%d]Avail[%d]Needed[%d] Reached max cache size ",__FUNCTION__,__LINE__,mPlaylistCache.size(),mCacheStoredSize,buffer->len);
cacheStoreReady = AllocatePlaylistCacheSlot(fileType,buffer->len);
}
if(cacheStoreReady)
{
tmpData = new PlayListCachedData();
tmpData->mCachedBuffer = new GrowableBuffer();
memset (tmpData->mCachedBuffer, 0, sizeof(GrowableBuffer));
aamp_AppendBytes(tmpData->mCachedBuffer, buffer->ptr, buffer->len );
tmpData->mEffectiveUrl = effectiveUrl;
tmpData->mFileType = fileType;
mPlaylistCache[url] = tmpData;
mCacheStoredSize += buffer->len;
AAMPLOG_INFO("[%s][%d] Inserted. url %s", __FUNCTION__, __LINE__, url.c_str());
// There are cases where Main url and effective url will be different ( for Main manifest)
// Need to store both the entries with same content data
// When retune happens within aamp due to failure , effective url wll be asked to read from cached manifest
// When retune happens from JS , regular Main url will be asked to read from cached manifest.
// So need to have two entries in cache table but both pointing to same CachedBuffer (no space is consumed for storage)
{
// if n only there is diff in url , need to store both
if(url != effectiveUrl)
{
newtmpData = new PlayListCachedData();
// Not to allocate for Cachebuffer again , use the same buffer as above
newtmpData->mCachedBuffer = tmpData->mCachedBuffer;
newtmpData->mEffectiveUrl = effectiveUrl;
// This is a duplicate entry
newtmpData->mDuplicateEntry = true;
newtmpData->mFileType = fileType;
mPlaylistCache[effectiveUrl] = newtmpData;
AAMPLOG_INFO("[%s][%d]Added an effective url entry %s", __FUNCTION__, __LINE__, effectiveUrl.c_str());
}
}
}
}
}
}
pthread_mutex_unlock(&mMutex);
}
/**
* @brief Retrieve playlist from playlist cache
* @param url URL corresponding to playlist
* @param[out] buffer Output buffer containing playlist
* @param[out] effectiveUrl effective URL of retrieved playlist
* @retval true if playlist is successfully retrieved.
*/
bool AampCacheHandler::RetrieveFromPlaylistCache(const std::string url, GrowableBuffer* buffer, std::string& effectiveUrl)
{
GrowableBuffer* buf = NULL;
bool ret;
std::string eUrl;
pthread_mutex_lock(&mMutex);
PlaylistCacheIter it = mPlaylistCache.find(url);
if (it != mPlaylistCache.end())
{
PlayListCachedData *tmpData = it->second;
buf = tmpData->mCachedBuffer;
eUrl = tmpData->mEffectiveUrl;
buffer->len = 0;
aamp_AppendBytes(buffer, buf->ptr, buf->len );
effectiveUrl = eUrl;
traceprintf("%s:%d : url %s found", __FUNCTION__, __LINE__, url.c_str());
ret = true;
}
else
{
traceprintf("%s:%d : url %s not found", __FUNCTION__, __LINE__, url.c_str());
ret = false;
}
pthread_mutex_unlock(&mMutex);
return ret;
}
/**
* @brief Clear playlist cache
*/
void AampCacheHandler::ClearPlaylistCache()
{
AAMPLOG_INFO("%s:%d : cache size %d", __FUNCTION__, __LINE__, (int)mPlaylistCache.size());
PlaylistCacheIter it = mPlaylistCache.begin();
for (;it != mPlaylistCache.end(); it++)
{
PlayListCachedData *tmpData = it->second;
if(!tmpData->mDuplicateEntry)
{
aamp_Free(&tmpData->mCachedBuffer->ptr);
delete tmpData->mCachedBuffer;
tmpData->mCachedBuffer = NULL;
}
delete tmpData;
}
mCacheStoredSize = 0;
mPlaylistCache.clear();
}
/**
* @brief AllocatePlaylistCacheSlot Allocate Slot for adding new playlist
*/
bool AampCacheHandler::AllocatePlaylistCacheSlot(MediaType fileType,size_t newLen)
{
bool retVal = true;
size_t freedSize=0;
if(mPlaylistCache.size())
{
if(fileType == eMEDIATYPE_MANIFEST)
{
// This case cannot happen, but for safety need to handle.
// If for any reason Main Manifest is pushed after cache is full , better clear all the playlist cached .
// As per new Main Manifest ,new playlist files need to be downloaded and cached
ClearPlaylistCache();
}
else // for non main manifest
{
PlaylistCacheIter Iter = mPlaylistCache.begin();
// Two pass to remove the item from cache to create space for caching
// First pass : Search for same file type to clean, If Video need to be inserted , free another Video type
// if audio type to be inserted , remove older audio type . Same for iframe .
// Second pass : Even after removing same file type entry ,still not enough space to add new item then remove from other file type ( rare scenario)
while(Iter != mPlaylistCache.end())
{
PlayListCachedData *tmpData = Iter->second;
if(tmpData->mFileType == eMEDIATYPE_MANIFEST || tmpData->mFileType != fileType)
{ // Not to remove main manifest file and filetype which are different
Iter++;
continue;
}
if(!tmpData->mDuplicateEntry)
{
freedSize += tmpData->mCachedBuffer->len;
aamp_Free(&tmpData->mCachedBuffer->ptr);
delete tmpData->mCachedBuffer;
tmpData->mCachedBuffer = NULL;
}
delete tmpData;
Iter = mPlaylistCache.erase(Iter);
}
//Second Pass - if still more cleanup required for space, remove from other playlist types
if(freedSize < newLen)
{
Iter = mPlaylistCache.begin();
while(Iter != mPlaylistCache.end())
{
PlayListCachedData *tmpData = Iter->second;
if(tmpData->mFileType == eMEDIATYPE_MANIFEST)
{ // Not to remove main manifest file
Iter++;
continue;
}
if(!tmpData->mDuplicateEntry)
{
freedSize += tmpData->mCachedBuffer->len;
aamp_Free(&tmpData->mCachedBuffer->ptr);
delete tmpData->mCachedBuffer;
tmpData->mCachedBuffer = NULL;
}
delete tmpData;
Iter = mPlaylistCache.erase(Iter);
}
}
mCacheStoredSize -= freedSize;
// After all freeing still size is not enough to insert , better not allow to insert such huge file
if(freedSize < newLen)
retVal = false;
}
}
return retVal;
}
AampCacheHandler::AampCacheHandler():
mCacheStoredSize(0),mAsyncThreadStartedFlag(false),mAsyncCleanUpTaskThreadId(0),mCacheActive(false),
mAsyncCacheCleanUpThread(false),mMutex(),mCondVarMutex(),mCondVar(),mPlaylistCache()
,mMaxPlaylistCacheSize(MAX_PLAYLIST_CACHE_SIZE)
{
pthread_mutex_init(&mMutex, NULL);
pthread_mutex_init(&mCondVarMutex, NULL);
pthread_cond_init(&mCondVar, NULL);
if(0 != pthread_create(&mAsyncCleanUpTaskThreadId, NULL, &AampCacheThreadFunction, this))
{
AAMPLOG_ERR("Failed to create AampCacheHandler thread errno = %d, %s", errno, strerror(errno));
}
else
{
mAsyncThreadStartedFlag = true;
mAsyncCacheCleanUpThread = true;
}
}
/**
* @brief Destructor Function
*/
AampCacheHandler::~AampCacheHandler()
{
mCacheActive = true;
pthread_mutex_lock(&mCondVarMutex);
mAsyncCacheCleanUpThread = false;
pthread_cond_signal(&mCondVar);
pthread_mutex_unlock(&mCondVarMutex);
if(mAsyncThreadStartedFlag)
{
void *ptr = NULL;
int rc = pthread_join(mAsyncCleanUpTaskThreadId, &ptr);
if (rc != 0)
{
AAMPLOG_ERR("***pthread_join AsyncCacheCleanUpTask returned %d(%s)", rc, strerror(rc));
}
}
ClearPlaylistCache();
pthread_mutex_destroy(&mMutex);
pthread_mutex_destroy(&mCondVarMutex);
pthread_cond_destroy(&mCondVar);
}
/**
* @brief Start playlist caching
*
* @return void
*/
void AampCacheHandler::StartPlaylistCache()
{
mCacheActive = true;
pthread_mutex_lock(&mCondVarMutex);
pthread_cond_signal(&mCondVar);
pthread_mutex_unlock(&mCondVarMutex );
}
/**
* @brief Stop playlist caching
*
* @return void
*/
void AampCacheHandler::StopPlaylistCache()
{
mCacheActive = false;
pthread_mutex_lock(&mCondVarMutex);
pthread_cond_signal(&mCondVar);
pthread_mutex_unlock(&mCondVarMutex );
}
/**
* @brief Thread function for Async Cache clean
*
* @return void
*/
void AampCacheHandler::AsyncCacheCleanUpTask()
{
pthread_mutex_lock(&mCondVarMutex);
while (mAsyncCacheCleanUpThread)
{
pthread_cond_wait(&mCondVar, &mCondVarMutex);
if(!mCacheActive)
{
struct timespec ts;
ts = aamp_GetTimespec(10000);
if(ETIMEDOUT == pthread_cond_timedwait(&mCondVar, &mCondVarMutex, &ts))
{
AAMPLOG_INFO("%s:%d[%p] Cacheflush timed out", __FUNCTION__, __LINE__, this);
ClearPlaylistCache();
}
}
}
pthread_mutex_unlock(&mCondVarMutex);
}
/**
* @brief SetMaxPlaylistCacheSize - Set Max Cache Size
*
* @param[in] cacheSz- CacheSize
* @return None
*/
void AampCacheHandler::SetMaxPlaylistCacheSize(int maxPlaylistCacheSz)
{
pthread_mutex_lock(&mMutex);
mMaxPlaylistCacheSize = maxPlaylistCacheSz;
AAMPLOG_WARN("%s Setting mMaxPlaylistCacheSize to :%d",__FUNCTION__,maxPlaylistCacheSz);
pthread_mutex_unlock(&mMutex);
}
/**
* @brief IsUrlCached - Check if URL is already cached
*
* @return bool - true if file found, else false
*/
bool AampCacheHandler::IsUrlCached(std::string url)
{
bool retval = false;
pthread_mutex_lock(&mMutex);
PlaylistCacheIter it = mPlaylistCache.find(url);
if (it != mPlaylistCache.end())
retval = true;
pthread_mutex_unlock(&mMutex);
return retval;
}