RDK Documentation (Open Sourced RDK Components)
AampCacheHandler.h
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file AampCacheHandler.h
22  * @brief Cache handler for AAMP
23  */
24 
25 #ifndef __AAMP_CACHE_HANDLER_H__
26 #define __AAMP_CACHE_HANDLER_H__
27 
28 #include <iostream>
29 #include <memory>
30 #include <unordered_map>
31 #include "priv_aamp.h"
32 
33 #define PLAYLIST_CACHE_SIZE_UNLIMITED -1
34 
35 /**
36  * @brief PlayListCachedData structure to store playlist data
37  */
38 typedef struct playlistcacheddata{
39  std::string mEffectiveUrl;
40  GrowableBuffer* mCachedBuffer;
41  MediaType mFileType;
42  bool mDuplicateEntry;
43 
44  playlistcacheddata() : mEffectiveUrl(""), mCachedBuffer(NULL), mFileType(eMEDIATYPE_DEFAULT),mDuplicateEntry(false)
45  {
46  }
47 
48  playlistcacheddata(const playlistcacheddata& p) : mEffectiveUrl(p.mEffectiveUrl), mCachedBuffer(p.mCachedBuffer), mFileType(p.mFileType),mDuplicateEntry(p.mDuplicateEntry)
49  {
50  mCachedBuffer->ptr = p.mCachedBuffer->ptr;
51  mCachedBuffer->len = p.mCachedBuffer->len;
52  mCachedBuffer->avail = p.mCachedBuffer->avail;
53  }
54 
55  playlistcacheddata& operator=(const playlistcacheddata &p)
56  {
57  mEffectiveUrl = p.mEffectiveUrl;
58  mCachedBuffer = p.mCachedBuffer;
59  mFileType = p.mFileType;
60  mDuplicateEntry = p.mDuplicateEntry;
61  return *this;
62  }
63 
65 
66 /**
67  * @brief InitFragCacheStruct to store Init Fragment data
68  * Init fragment cache mechanism
69  * All types (VID/AUD/SUB/AUX) of Init Fragment maintained in a single Cache Table,
70  * and these fragment's url are stored in a Track Queue corrsponding to file type.
71  * This queue will be used to count fragments inserted, to remove entry in FIFO order
72  * upon exceeding limit with respect to file type.
73  * Eg:
74  * TrackQ[VID]={"http://sample_domain/vid_qual1.init"} umCacheTable={{"http://sample_domain/vid_qual1.init"}, VidUrlData1}
75  * {"http://sample_domain/vid_qual2.init"} {{"http://sample_domain/vid_qual1.init_redirect"}, VidUrlData1}
76  * {"http://sample_domain/vid_qual3.init"} {{"http://sample_domain/aud_qual1.init"}, AudUrlData1}
77  * TrackQ[AUD]={"http://sample_domain/aud_qual1.init"} {{"http://sample_domain/vid_qual2.init"}, VidUrlData2}
78  * {"http://sample_domain/aud_qual2.init"} {{"http://sample_domain/aud_qual1.init_redirect"}, AudUrlData1}
79  * {"http://sample_domain/aud_qual3.init"} {{"http://sample_domain/aud_qual3.init"}, AudUrlData3}
80  * {{"http://sample_domain/aud_qual2.init"}, AudUrlData2}
81  * {{"http://sample_domain/vid_qual2.init_redirect"}, VidUrlData2}
82  * {{"http://sample_domain/vid_qual3.init"}, VidUrlData3}
83  * {{"http://sample_domain/aud_qual2.init_redirect"}, AudUrlData2}
84  * Track queue will not maintain duplicate entry of cache table, so we can have maximum of different init fragments in cache table.
85  * As per above eg, TrackQ[VID] size is 3, but cache table has 5 including effective url entry. If we maintain effective url entry
86  * in cache queue, we will have only 3 init fragments in diff quality.
87  * If cache table reaches max no of cache per track, we remove both main entry and dup entry if present, in FIFO order.
88  *
89  * Fragment cache & track queue will be cleared upon exiting from aamp player or from async clear thread.
90  */
92 
93 /**
94  * @brief initfragtrackstruct to store init fragment url per media track in FIFO Queue.
95  */
96 typedef struct initfragtrackstruct
97 {
98  std::queue<std::string> Trackqueue;
99 
100  initfragtrackstruct() : Trackqueue()
101  {
102  }
104 
105 /**
106  * @class AampCacheHandler
107  * @brief Handles Aamp Cahe operations
108  */
109 
111 {
112 private:
113  typedef std::unordered_map<std::string, PlayListCachedData *> PlaylistCache ;
114  typedef std::unordered_map<std::string, PlayListCachedData *>::iterator PlaylistCacheIter;
115  PlaylistCache mPlaylistCache;
116  int mCacheStoredSize;
117  bool mInitialized;
118  bool mCacheActive;
119  bool mAsyncCacheCleanUpThread;
120  bool mAsyncThreadStartedFlag;
121  int mMaxPlaylistCacheSize;
122  pthread_mutex_t mMutex;
123  pthread_mutex_t mCondVarMutex;
124  pthread_cond_t mCondVar ;
125  pthread_t mAsyncCleanUpTaskThreadId;
126  AampLogManager *mLogObj;
127 
128  typedef std::unordered_map <std::string, InitFragCacheStruct*> InitFragCache ;
129  typedef std::unordered_map <std::string, InitFragCacheStruct*>::iterator InitFragCacheIter;
130  typedef std::unordered_map <MediaType, InitFragTrackStruct*, std::hash<int>> CacheTrackQueue;
131  typedef std::unordered_map <MediaType, InitFragTrackStruct*, std::hash<int>>::iterator CacheTrackQueueIter;
132  InitFragCache umInitFragCache;
133  CacheTrackQueue umCacheTrackQ;
134  pthread_mutex_t mInitFragMutex;
135  bool bInitFragCache;
136  int MaxInitCacheSlot; /**< Max no of init fragment per track */
137 
138 private:
139 
140  /**
141  * @fn Init
142  */
143  void Init();
144 
145  /**
146  * @fn ClearCacheHandler
147  */
148  void ClearCacheHandler();
149 
150  /**
151  * @fn AsyncCacheCleanUpTask
152  *
153  * @return void
154  */
155  void AsyncCacheCleanUpTask();
156  /**
157  * @brief Thread entry function
158  *
159  * @return void
160  */
161  static void * AampCacheThreadFunction(void * This) {((AampCacheHandler *)This)->AsyncCacheCleanUpTask(); return NULL;}
162  /**
163  * @fn ClearPlaylistCache
164  * @return void
165  */
166  void ClearPlaylistCache();
167  /**
168  * @fn AllocatePlaylistCacheSlot
169  * @param[in] fileType - Indicate the type of playlist to store/remove
170  * @param[in] newLen - Size required to store new playlist
171  *
172  * @return bool Success or Failure
173  */
174  bool AllocatePlaylistCacheSlot(MediaType fileType,size_t newLen);
175 
176  /**
177  * @fn ClearInitFragCache
178  *
179  * @return void
180  */
181  void ClearInitFragCache();
182 
183  /**
184  * @fn RemoveInitFragCacheEntry
185  *
186  * @param fileType type of file format to be removed from cache table
187  *
188  * @return void
189  */
190  void RemoveInitFragCacheEntry ( MediaType fileType );
191 
192 public:
193 
194  /**
195  * @fn AampCacheHandler
196  *
197  * @return void
198  */
200 
201  /**
202  * @fn ~AampCacheHandler
203  */
205 
206  /**
207  * @fn StartPlaylistCache
208  *
209  * @return void
210  */
211  void StartPlaylistCache();
212  /**
213  * @fn StopPlaylistCache
214  *
215  * @return void
216  */
217  void StopPlaylistCache();
218 
219  /**
220  * @fn InsertToPlaylistCache
221  * @param[in] url - URL
222  * @param[in] buffer - Pointer to growable buffer
223  * @param[in] effectiveUrl - Final URL
224  * @param[in] trackLiveStatus - Live Status of the track inserted
225  * @param[in] fileType - Type of the file inserted
226  *
227  * @return void
228  */
229  void InsertToPlaylistCache(const std::string url, const GrowableBuffer* buffer, std::string effectiveUrl,bool trackLiveStatus,MediaType fileType=eMEDIATYPE_DEFAULT);
230 
231  /**
232  * @fn RetrieveFromPlaylistCache
233  * @param[in] url - URL
234  * @param[out] buffer - Pointer to growable buffer
235  * @param[out] effectiveUrl - Final URL
236  * @return true: found, false: not found
237  */
238  bool RetrieveFromPlaylistCache(const std::string url, GrowableBuffer* buffer, std::string& effectiveUrl);
239 
240  /**
241  * @brief Remove specific playlist cache
242  * @param[in] url - URL
243  */
244  void RemoveFromPlaylistCache(const std::string url);
245 
246  /**
247  * @fn SetMaxPlaylistCacheSize
248  *
249  * @param[in] maxPlaylistCacheSz - CacheSize
250  * @return None
251  */
252  void SetMaxPlaylistCacheSize(int maxPlaylistCacheSz);
253  /**
254  * @brief GetMaxPlaylistCacheSiz @fn RetrieveFromPlaylistCache - Get present CacheSize
255  *
256  * @return int - maxCacheSize
257  */
258  int GetMaxPlaylistCacheSize() { return mMaxPlaylistCacheSize; }
259  /**
260  * @fn IsUrlCached
261  *
262  * @return bool - true if file found, else false
263  */
264  bool IsUrlCached(std::string);
265 
266  /**
267  * @fn InsertToInitFragCache
268  *
269  * @param[in] url - URL
270  * @param[in] buffer - Pointer to growable buffer
271  * @param[in] effectiveUrl - Final URL
272  * @param[in] fileType - Type of the file inserted
273  *
274  * @return void
275  */
276  void InsertToInitFragCache(const std::string url, const GrowableBuffer* buffer, std::string effectiveUrl,MediaType fileType);
277 
278  /**
279  * @fn RetrieveFromInitFragCache
280  *
281  * @param[in] url - URL
282  * @param[out] buffer - Pointer to growable buffer
283  * @param[out] effectiveUrl - Final URL
284  *
285  * @return true: found, false: not found
286  */
287  bool RetrieveFromInitFragCache(const std::string url, GrowableBuffer* buffer, std::string& effectiveUrl);
288 
289  /**
290  * @fn SetMaxInitFragCacheSize
291  *
292  * @param[in] maxInitFragCacheSz - CacheSize
293  *
294  * @return None
295  */
296  void SetMaxInitFragCacheSize( int maxInitFragCacheSz);
297 
298  /**
299  * @brief GetMaxPlaylistCacheSize - Get present CacheSize
300  *
301  * @return int - maxCacheSize
302  */
304 
305  /**
306  * @brief Copy constructor disabled
307  *
308  */
309  AampCacheHandler(const AampCacheHandler&) = delete;
310  /**
311  * @brief assignment operator disabled
312  *
313  */
314  AampCacheHandler& operator=(const AampCacheHandler&) = delete;
315 };
316 
317 
318 #endif
AampCacheHandler::AampCacheHandler
AampCacheHandler(AampLogManager *logObj)
Default Constructor.
Definition: AampCacheHandler.cpp:314
AampCacheHandler::Init
void Init()
Initialization Function.
Definition: AampCacheHandler.cpp:261
GrowableBuffer::avail
size_t avail
Definition: AampMemoryUtils.h:43
AampCacheHandler::InsertToPlaylistCache
void InsertToPlaylistCache(const std::string url, const GrowableBuffer *buffer, std::string effectiveUrl, bool trackLiveStatus, MediaType fileType=eMEDIATYPE_DEFAULT)
Retrieve playlist from cache
Definition: AampCacheHandler.cpp:31
GrowableBuffer::len
size_t len
Definition: AampMemoryUtils.h:42
AampCacheHandler::StartPlaylistCache
void StartPlaylistCache()
Start playlist caching.
Definition: AampCacheHandler.cpp:347
AampCacheHandler::AampCacheThreadFunction
static void * AampCacheThreadFunction(void *This)
Thread entry function.
Definition: AampCacheHandler.h:161
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
AampCacheHandler::~AampCacheHandler
~AampCacheHandler()
Destructor Function.
Definition: AampCacheHandler.cpp:333
eMEDIATYPE_DEFAULT
@ eMEDIATYPE_DEFAULT
Definition: AampMediaType.h:58
AampCacheHandler::RetrieveFromInitFragCache
bool RetrieveFromInitFragCache(const std::string url, GrowableBuffer *buffer, std::string &effectiveUrl)
Retrieve init fragment from cache.
Definition: AampCacheHandler.cpp:506
initfragtrackstruct
initfragtrackstruct to store init fragment url per media track in FIFO Queue.
Definition: AampCacheHandler.h:96
AampCacheHandler::RetrieveFromPlaylistCache
bool RetrieveFromPlaylistCache(const std::string url, GrowableBuffer *buffer, std::string &effectiveUrl)
Retrieve playlist from cache.
Definition: AampCacheHandler.cpp:111
MediaType
MediaType
Media types.
Definition: AampMediaType.h:37
AampCacheHandler::IsUrlCached
bool IsUrlCached(std::string)
IsUrlCached - Check if URL is already cached.
Definition: AampCacheHandler.cpp:407
AampCacheHandler::MaxInitCacheSlot
int MaxInitCacheSlot
Definition: AampCacheHandler.h:136
AampCacheHandler::RemoveFromPlaylistCache
void RemoveFromPlaylistCache(const std::string url)
Remove specific playlist cache.
Definition: AampCacheHandler.cpp:141
GrowableBuffer::ptr
char * ptr
Definition: AampMemoryUtils.h:41
AampCacheHandler::InsertToInitFragCache
void InsertToInitFragCache(const std::string url, const GrowableBuffer *buffer, std::string effectiveUrl, MediaType fileType)
Insert init fragment into cache table.
Definition: AampCacheHandler.cpp:423
AampCacheHandler::StopPlaylistCache
void StopPlaylistCache()
Stop playlist caching.
Definition: AampCacheHandler.cpp:358
playlistcacheddata
PlayListCachedData structure to store playlist data.
Definition: AampCacheHandler.h:38
InitFragTrackStruct
struct initfragtrackstruct InitFragTrackStruct
initfragtrackstruct to store init fragment url per media track in FIFO Queue.
priv_aamp.h
Private functions and types used internally by AAMP.
GrowableBuffer
Structure of GrowableBuffer.
Definition: AampMemoryUtils.h:39
AampCacheHandler
Handles Aamp Cahe operations.
Definition: AampCacheHandler.h:110
AampCacheHandler::operator=
AampCacheHandler & operator=(const AampCacheHandler &)=delete
assignment operator disabled
AampCacheHandler::ClearCacheHandler
void ClearCacheHandler()
Clear Cache Handler. Exit clean up thread.
Definition: AampCacheHandler.cpp:283
AampCacheHandler::SetMaxInitFragCacheSize
void SetMaxInitFragCacheSize(int maxInitFragCacheSz)
SetMaxInitFragCacheSize - Set Max Cache Size.
Definition: AampCacheHandler.cpp:618
AampCacheHandler::SetMaxPlaylistCacheSize
void SetMaxPlaylistCacheSize(int maxPlaylistCacheSz)
SetMaxPlaylistCacheSize - Set Max Cache Size.
Definition: AampCacheHandler.cpp:396
AampCacheHandler::AllocatePlaylistCacheSlot
bool AllocatePlaylistCacheSlot(MediaType fileType, size_t newLen)
AllocatePlaylistCacheSlot Allocate Slot for adding new playlist.
Definition: AampCacheHandler.cpp:189
AampCacheHandler::ClearPlaylistCache
void ClearPlaylistCache()
Clear playlist cache.
Definition: AampCacheHandler.cpp:168
PlayListCachedData
struct playlistcacheddata PlayListCachedData
PlayListCachedData structure to store playlist data.
AampCacheHandler::RemoveInitFragCacheEntry
void RemoveInitFragCacheEntry(MediaType fileType)
Removes very first inserted entry ( and duplicate entry, if present) of given filetype from fragment ...
Definition: AampCacheHandler.cpp:538
AampCacheHandler::AsyncCacheCleanUpTask
void AsyncCacheCleanUpTask()
Thread function for Async Cache clean.
Definition: AampCacheHandler.cpp:369
AampCacheHandler::GetMaxInitFragCacheSize
int GetMaxInitFragCacheSize()
GetMaxPlaylistCacheSize - Get present CacheSize.
Definition: AampCacheHandler.h:303
AampCacheHandler::ClearInitFragCache
void ClearInitFragCache()
Clear init fragment cache & track queue table.
Definition: AampCacheHandler.cpp:587