This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AampCacheHandler.h
executable file
·318 lines (282 loc) · 9.3 KB
/
AampCacheHandler.h
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
/*
* 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 "priv_aamp.h"
#define PLAYLIST_CACHE_SIZE_UNLIMITED -1
/**
* @brief PlayListCachedData structure to store playlist data
*/
typedef struct playlistcacheddata{
std::string mEffectiveUrl;
GrowableBuffer* mCachedBuffer;
MediaType mFileType;
bool mDuplicateEntry;
playlistcacheddata() : mEffectiveUrl(""), mCachedBuffer(NULL), mFileType(eMEDIATYPE_DEFAULT),mDuplicateEntry(false)
{
}
playlistcacheddata(const playlistcacheddata& p) : mEffectiveUrl(p.mEffectiveUrl), mCachedBuffer(p.mCachedBuffer), mFileType(p.mFileType),mDuplicateEntry(p.mDuplicateEntry)
{
mCachedBuffer->ptr = p.mCachedBuffer->ptr;
mCachedBuffer->len = p.mCachedBuffer->len;
mCachedBuffer->avail = p.mCachedBuffer->avail;
}
playlistcacheddata& operator=(const playlistcacheddata &p)
{
mEffectiveUrl = p.mEffectiveUrl;
mCachedBuffer = p.mCachedBuffer;
mFileType = p.mFileType;
mDuplicateEntry = p.mDuplicateEntry;
return *this;
}
}PlayListCachedData;
/**
* @brief InitFragCacheStruct to store Init Fragment data
* Init fragment cache mechanism
* All types (VID/AUD/SUB/AUX) of Init Fragment maintained in a single Cache Table,
* and these fragment's url are stored in a Track Queue corrsponding to file type.
* This queue will be used to count fragments inserted, to remove entry in FIFO order
* upon exceeding limit with respect to file type.
* Eg:
* TrackQ[VID]={"http://sample_domain/vid_qual1.init"} umCacheTable={{"http://sample_domain/vid_qual1.init"}, VidUrlData1}
* {"http://sample_domain/vid_qual2.init"} {{"http://sample_domain/vid_qual1.init_redirect"}, VidUrlData1}
* {"http://sample_domain/vid_qual3.init"} {{"http://sample_domain/aud_qual1.init"}, AudUrlData1}
* TrackQ[AUD]={"http://sample_domain/aud_qual1.init"} {{"http://sample_domain/vid_qual2.init"}, VidUrlData2}
* {"http://sample_domain/aud_qual2.init"} {{"http://sample_domain/aud_qual1.init_redirect"}, AudUrlData1}
* {"http://sample_domain/aud_qual3.init"} {{"http://sample_domain/aud_qual3.init"}, AudUrlData3}
* {{"http://sample_domain/aud_qual2.init"}, AudUrlData2}
* {{"http://sample_domain/vid_qual2.init_redirect"}, VidUrlData2}
* {{"http://sample_domain/vid_qual3.init"}, VidUrlData3}
* {{"http://sample_domain/aud_qual2.init_redirect"}, AudUrlData2}
* Track queue will not maintain duplicate entry of cache table, so we can have maximum of different init fragments in cache table.
* As per above eg, TrackQ[VID] size is 3, but cache table has 5 including effective url entry. If we maintain effective url entry
* in cache queue, we will have only 3 init fragments in diff quality.
* If cache table reaches max no of cache per track, we remove both main entry and dup entry if present, in FIFO order.
*
* Fragment cache & track queue will be cleared upon exiting from aamp player or from async clear thread.
*/
typedef struct playlistcacheddata InitFragCacheStruct;
/**
* @brief initfragtrackstruct to store init fragment url per media track in FIFO Queue.
*/
typedef struct initfragtrackstruct
{
std::queue<std::string> Trackqueue;
initfragtrackstruct() : Trackqueue()
{
}
}InitFragTrackStruct;
/**
* @class AampCacheHandler
* @brief Handles Aamp Cahe operations
*/
class AampCacheHandler
{
private:
typedef std::unordered_map<std::string, PlayListCachedData *> PlaylistCache ;
typedef std::unordered_map<std::string, PlayListCachedData *>::iterator PlaylistCacheIter;
PlaylistCache mPlaylistCache;
int mCacheStoredSize;
bool mInitialized;
bool mCacheActive;
bool mAsyncCacheCleanUpThread;
bool mAsyncThreadStartedFlag;
int mMaxPlaylistCacheSize;
pthread_mutex_t mMutex;
pthread_mutex_t mCondVarMutex;
pthread_cond_t mCondVar ;
pthread_t mAsyncCleanUpTaskThreadId;
AampLogManager *mLogObj;
typedef std::unordered_map <std::string, InitFragCacheStruct*> InitFragCache ;
typedef std::unordered_map <std::string, InitFragCacheStruct*>::iterator InitFragCacheIter;
typedef std::unordered_map <MediaType, InitFragTrackStruct*, std::hash<int>> CacheTrackQueue;
typedef std::unordered_map <MediaType, InitFragTrackStruct*, std::hash<int>>::iterator CacheTrackQueueIter;
InitFragCache umInitFragCache;
CacheTrackQueue umCacheTrackQ;
pthread_mutex_t mInitFragMutex;
bool bInitFragCache;
int MaxInitCacheSlot; /**< Max no of init fragment per track */
private:
/**
* @fn Init
*/
void Init();
/**
* @fn ClearCacheHandler
*/
void ClearCacheHandler();
/**
* @fn AsyncCacheCleanUpTask
*
* @return void
*/
void AsyncCacheCleanUpTask();
/**
* @brief Thread entry function
*
* @return void
*/
static void * AampCacheThreadFunction(void * This) {((AampCacheHandler *)This)->AsyncCacheCleanUpTask(); return NULL;}
/**
* @fn ClearPlaylistCache
* @return void
*/
void ClearPlaylistCache();
/**
* @fn AllocatePlaylistCacheSlot
* @param[in] fileType - Indicate the type of playlist to store/remove
* @param[in] newLen - Size required to store new playlist
*
* @return bool Success or Failure
*/
bool AllocatePlaylistCacheSlot(MediaType fileType,size_t newLen);
/**
* @fn ClearInitFragCache
*
* @return void
*/
void ClearInitFragCache();
/**
* @fn RemoveInitFragCacheEntry
*
* @param fileType type of file format to be removed from cache table
*
* @return void
*/
void RemoveInitFragCacheEntry ( MediaType fileType );
public:
/**
* @fn AampCacheHandler
*
* @return void
*/
AampCacheHandler(AampLogManager *logObj);
/**
* @fn ~AampCacheHandler
*/
~AampCacheHandler();
/**
* @fn StartPlaylistCache
*
* @return void
*/
void StartPlaylistCache();
/**
* @fn StopPlaylistCache
*
* @return void
*/
void StopPlaylistCache();
/**
* @fn InsertToPlaylistCache
* @param[in] url - URL
* @param[in] buffer - Pointer to growable buffer
* @param[in] effectiveUrl - Final URL
* @param[in] trackLiveStatus - Live Status of the track inserted
* @param[in] fileType - Type of the file inserted
*
* @return void
*/
void InsertToPlaylistCache(const std::string url, const GrowableBuffer* buffer, std::string effectiveUrl,bool trackLiveStatus,MediaType fileType=eMEDIATYPE_DEFAULT);
/**
* @fn RetrieveFromPlaylistCache
* @param[in] url - URL
* @param[out] buffer - Pointer to growable buffer
* @param[out] effectiveUrl - Final URL
* @return true: found, false: not found
*/
bool RetrieveFromPlaylistCache(const std::string url, GrowableBuffer* buffer, std::string& effectiveUrl);
/**
* @brief Remove specific playlist cache
* @param[in] url - URL
*/
void RemoveFromPlaylistCache(const std::string url);
/**
* @fn SetMaxPlaylistCacheSize
*
* @param[in] maxPlaylistCacheSz - CacheSize
* @return None
*/
void SetMaxPlaylistCacheSize(int maxPlaylistCacheSz);
/**
* @brief GetMaxPlaylistCacheSiz @fn RetrieveFromPlaylistCache - Get present CacheSize
*
* @return int - maxCacheSize
*/
int GetMaxPlaylistCacheSize() { return mMaxPlaylistCacheSize; }
/**
* @fn IsUrlCached
*
* @return bool - true if file found, else false
*/
bool IsUrlCached(std::string);
/**
* @fn InsertToInitFragCache
*
* @param[in] url - URL
* @param[in] buffer - Pointer to growable buffer
* @param[in] effectiveUrl - Final URL
* @param[in] fileType - Type of the file inserted
*
* @return void
*/
void InsertToInitFragCache(const std::string url, const GrowableBuffer* buffer, std::string effectiveUrl,MediaType fileType);
/**
* @fn RetrieveFromInitFragCache
*
* @param[in] url - URL
* @param[out] buffer - Pointer to growable buffer
* @param[out] effectiveUrl - Final URL
*
* @return true: found, false: not found
*/
bool RetrieveFromInitFragCache(const std::string url, GrowableBuffer* buffer, std::string& effectiveUrl);
/**
* @fn SetMaxInitFragCacheSize
*
* @param[in] maxInitFragCacheSz - CacheSize
*
* @return None
*/
void SetMaxInitFragCacheSize( int maxInitFragCacheSz);
/**
* @brief GetMaxPlaylistCacheSize - Get present CacheSize
*
* @return int - maxCacheSize
*/
int GetMaxInitFragCacheSize() { return MaxInitCacheSlot; }
/**
* @brief Copy constructor disabled
*
*/
AampCacheHandler(const AampCacheHandler&) = delete;
/**
* @brief assignment operator disabled
*
*/
AampCacheHandler& operator=(const AampCacheHandler&) = delete;
};
#endif