34 pthread_mutex_lock(&mMutex);
44 PlaylistCacheIter it = mPlaylistCache.find(url);
45 if (it != mPlaylistCache.end())
47 AAMPLOG_INFO(
"playlist %s already present in cache", url.c_str());
58 if(mMaxPlaylistCacheSize == PLAYLIST_CACHE_SIZE_UNLIMITED || (mMaxPlaylistCacheSize != PLAYLIST_CACHE_SIZE_UNLIMITED && buffer->len < mMaxPlaylistCacheSize))
62 bool cacheStoreReady =
true;
63 if(mMaxPlaylistCacheSize != PLAYLIST_CACHE_SIZE_UNLIMITED && ((mCacheStoredSize + buffer->len) > mMaxPlaylistCacheSize))
65 AAMPLOG_WARN(
"Count[%lu]Avail[%d]Needed[%zu] Reached max cache size", mPlaylistCache.size(),mCacheStoredSize,buffer->len);
75 tmpData->mEffectiveUrl = effectiveUrl;
76 tmpData->mFileType = fileType;
77 mPlaylistCache[url] = tmpData;
78 mCacheStoredSize += buffer->len;
79 AAMPLOG_INFO(
"Inserted. url %s", url.c_str());
87 if(url != effectiveUrl)
91 newtmpData->mCachedBuffer = tmpData->mCachedBuffer;
92 newtmpData->mEffectiveUrl = effectiveUrl;
94 newtmpData->mDuplicateEntry =
true;
95 newtmpData->mFileType = fileType;
96 mPlaylistCache[effectiveUrl] = newtmpData;
97 AAMPLOG_INFO(
"Added an effective url entry %s", effectiveUrl.c_str());
104 pthread_mutex_unlock(&mMutex);
116 pthread_mutex_lock(&mMutex);
117 PlaylistCacheIter it = mPlaylistCache.find(url);
118 if (it != mPlaylistCache.end())
121 buf = tmpData->mCachedBuffer;
122 eUrl = tmpData->mEffectiveUrl;
134 pthread_mutex_unlock(&mMutex);
143 pthread_mutex_lock(&mMutex);
144 PlaylistCacheIter it = mPlaylistCache.find(url);
145 if (it != mPlaylistCache.end())
148 if(!tmpData->mDuplicateEntry)
151 SAFE_DELETE(tmpData->mCachedBuffer);
153 SAFE_DELETE(tmpData);
155 AAMPLOG_INFO(
"Removing Playlist URL %s from Cache", it->first.c_str());
156 mPlaylistCache.erase(it);
160 AAMPLOG_WARN(
"Playlist URL %s not found in cache", url.c_str());
162 pthread_mutex_unlock(&mMutex);
170 AAMPLOG_INFO(
"cache size %d", (
int)mPlaylistCache.size());
171 PlaylistCacheIter it = mPlaylistCache.begin();
172 for (;it != mPlaylistCache.end(); it++)
175 if(!tmpData->mDuplicateEntry)
178 SAFE_DELETE(tmpData->mCachedBuffer);
180 SAFE_DELETE(tmpData);
182 mCacheStoredSize = 0;
183 mPlaylistCache.clear();
193 if(mPlaylistCache.size())
204 PlaylistCacheIter Iter = mPlaylistCache.begin();
209 while(Iter != mPlaylistCache.end())
217 if(!tmpData->mDuplicateEntry)
219 freedSize += tmpData->mCachedBuffer->
len;
221 SAFE_DELETE(tmpData->mCachedBuffer);
223 SAFE_DELETE(tmpData);
224 Iter = mPlaylistCache.erase(Iter);
227 if(freedSize < newLen)
229 Iter = mPlaylistCache.begin();
230 while(Iter != mPlaylistCache.end())
238 if(!tmpData->mDuplicateEntry)
240 freedSize += tmpData->mCachedBuffer->
len;
242 SAFE_DELETE(tmpData->mCachedBuffer);
244 SAFE_DELETE(tmpData);
245 Iter = mPlaylistCache.erase(Iter);
248 mCacheStoredSize -= freedSize;
250 if(freedSize < newLen)
264 if(
true == mInitialized)
268 AAMPLOG_ERR(
"Failed to create AampCacheHandler thread errno = %d, %s", errno, strerror(errno));
272 pthread_mutex_lock(&mCondVarMutex);
273 mAsyncThreadStartedFlag =
true;
274 mAsyncCacheCleanUpThread =
true;
275 pthread_mutex_unlock(&mCondVarMutex);
286 if(
false == mInitialized)
290 pthread_mutex_lock(&mCondVarMutex);
291 mAsyncCacheCleanUpThread =
false;
292 pthread_cond_signal(&mCondVar);
293 pthread_mutex_unlock(&mCondVarMutex);
294 if(mAsyncThreadStartedFlag)
297 int rc = pthread_join(mAsyncCleanUpTaskThreadId, &ptr);
300 AAMPLOG_ERR(
"***pthread_join AsyncCacheCleanUpTask returned %d(%s)", rc, strerror(rc));
302 mAsyncThreadStartedFlag =
false;
308 mInitialized =
false;
315 mCacheStoredSize(0),mAsyncThreadStartedFlag(false),mAsyncCleanUpTaskThreadId(0),mCacheActive(false),
316 mAsyncCacheCleanUpThread(false),mMutex(),mCondVarMutex(),mCondVar(),mPlaylistCache()
319 ,umInitFragCache(),umCacheTrackQ(),bInitFragCache(false),mInitFragMutex()
322 pthread_mutex_init(&mMutex, NULL);
323 pthread_mutex_init(&mCondVarMutex, NULL);
324 pthread_cond_init(&mCondVar, NULL);
326 pthread_mutex_init(&mInitFragMutex, NULL);
335 if(
true == mInitialized)
337 pthread_mutex_destroy(&mMutex);
338 pthread_mutex_destroy(&mCondVarMutex);
339 pthread_cond_destroy(&mCondVar);
341 pthread_mutex_destroy(&mInitFragMutex);
350 pthread_mutex_lock(&mCondVarMutex);
351 pthread_cond_signal(&mCondVar);
352 pthread_mutex_unlock(&mCondVarMutex );
360 mCacheActive =
false;
361 pthread_mutex_lock(&mCondVarMutex);
362 pthread_cond_signal(&mCondVar);
363 pthread_mutex_unlock(&mCondVarMutex );
371 pthread_mutex_lock(&mCondVarMutex);
372 while (mAsyncCacheCleanUpThread)
374 pthread_cond_wait(&mCondVar, &mCondVarMutex);
380 if(ETIMEDOUT == pthread_cond_timedwait(&mCondVar, &mCondVarMutex, &ts))
382 AAMPLOG_INFO(
"[%p] Cacheflush timed out",
this);
390 pthread_mutex_unlock(&mCondVarMutex);
398 pthread_mutex_lock(&mMutex);
399 mMaxPlaylistCacheSize = maxPlaylistCacheSz;
400 AAMPLOG_WARN(
"Setting mMaxPlaylistCacheSize to :%d",maxPlaylistCacheSz);
401 pthread_mutex_unlock(&mMutex);
410 pthread_mutex_lock(&mMutex);
411 PlaylistCacheIter it = mPlaylistCache.find(url);
412 if (it != mPlaylistCache.end())
415 pthread_mutex_unlock(&mMutex);
424 std::string effectiveUrl,
MediaType fileType)
429 pthread_mutex_lock(&mInitFragMutex);
431 InitFragCacheIter Iter = umInitFragCache.find(url);
432 if ( Iter != umInitFragCache.end() )
434 AAMPLOG_INFO(
"playlist %s already present in cache", url.c_str());
443 NewInitData->mEffectiveUrl = effectiveUrl;
444 NewInitData->mFileType = fileType;
445 NewInitData->mDuplicateEntry = (effectiveUrl.length() && (effectiveUrl!=url));
451 CacheTrackQueueIter IterCq = umCacheTrackQ.find(fileType);
452 if(IterCq == umCacheTrackQ.end())
455 NewTrackQueueCache->Trackqueue.push(url);
457 umCacheTrackQ[fileType]=NewTrackQueueCache;
461 NewTrackQueueCache = IterCq->second;
462 NewTrackQueueCache->Trackqueue.push(url);
475 umInitFragCache[url] = NewInitData;
476 AAMPLOG_INFO(
"Inserted init url %s", url.c_str());
483 if ( NewInitData->mDuplicateEntry )
487 DupInitData->mCachedBuffer = NewInitData->mCachedBuffer;
488 DupInitData->mEffectiveUrl = effectiveUrl;
489 DupInitData->mFileType = fileType;
490 umInitFragCache[effectiveUrl] = DupInitData;
492 AAMPLOG_INFO(
"Inserted effective init url %s", url.c_str());
495 AAMPLOG_INFO(
"Size [CacheTable:%lu,TrackQ:%lu,CurrentTrack:%lu,MaxLimit:%d]\n", umInitFragCache.size(),umCacheTrackQ.size(),
496 NewTrackQueueCache->Trackqueue.size(),
500 pthread_mutex_unlock(&mInitFragMutex);
507 std::string& effectiveUrl)
512 pthread_mutex_lock(&mInitFragMutex);
513 InitFragCacheIter it = umInitFragCache.find(url);
514 if (it != umInitFragCache.end())
517 buf = findFragData->mCachedBuffer;
518 eUrl = findFragData->mEffectiveUrl;
522 AAMPLOG_INFO(
"url %s found", url.c_str());
527 AAMPLOG_INFO(
"url %s not found", url.c_str());
530 pthread_mutex_unlock(&mInitFragMutex);
540 CacheTrackQueueIter IterCq = umCacheTrackQ.find(fileType);
541 if(IterCq == umCacheTrackQ.end())
552 InitFragCacheIter Iter = umInitFragCache.find(QueperTrack->Trackqueue.front());
553 if (Iter != umInitFragCache.end())
561 if ( removeCacheData->mDuplicateEntry )
564 InitFragCacheIter IterDup = umInitFragCache.find(removeCacheData->mEffectiveUrl);
565 if(IterDup != umInitFragCache.end())
567 dupData = IterDup->second;
568 SAFE_DELETE(dupData);
569 umInitFragCache.erase(IterDup);
570 AAMPLOG_INFO(
"Removed dup url:%s",removeCacheData->mEffectiveUrl.c_str());
573 aamp_Free(removeCacheData->mCachedBuffer);
574 SAFE_DELETE(removeCacheData->mCachedBuffer);
575 SAFE_DELETE(removeCacheData);
576 umInitFragCache.erase(Iter);
577 AAMPLOG_INFO(
"Removed main url:%s",QueperTrack->Trackqueue.front().c_str());
580 QueperTrack->Trackqueue.pop();
589 AAMPLOG_INFO(
"Fragment cache size %d", (
int)umInitFragCache.size());
590 InitFragCacheIter it = umInitFragCache.begin();
591 for (;it != umInitFragCache.end(); it++)
594 if(!delData->mDuplicateEntry)
597 SAFE_DELETE(delData->mCachedBuffer);
599 SAFE_DELETE(delData);
601 umInitFragCache.clear();
603 AAMPLOG_INFO(
"Track queue size %d", (
int)umCacheTrackQ.size());
604 CacheTrackQueueIter IterCq = umCacheTrackQ.begin();
605 for (;IterCq != umCacheTrackQ.end(); IterCq++)
609 std::queue<std::string>().swap(delTrack->Trackqueue);
610 SAFE_DELETE(delTrack);
612 umCacheTrackQ.clear();
620 pthread_mutex_lock(&mInitFragMutex);
622 AAMPLOG_WARN(
"Setting mMaxPlaylistCacheSize to :%d",maxInitFragCacheSz);
623 pthread_mutex_unlock(&mInitFragMutex);