-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAampTsbDataManager.h
More file actions
407 lines (359 loc) · 11.8 KB
/
AampTsbDataManager.h
File metadata and controls
407 lines (359 loc) · 11.8 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
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2024 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 AampTsbDataManager.h
* @brief TSB Handler for AAMP
*/
#ifndef __AAMP_TSB_DATA_MANAGER_H__
#define __AAMP_TSB_DATA_MANAGER_H__
#include <iostream>
#include <cmath>
#include <memory>
#include <map>
#include <exception>
#include <mutex>
#include <utility>
#include "StreamAbstractionAAMP.h"
#include "abr.h"
#include "AampTime.h"
#define TSB_DATA_DEBUG_ENABLED 0 /** Enable debug log on development/debug */
struct TSBWriteData
{
std::string url;
std::shared_ptr<CachedFragment> cachedFragment;
double pts;
std::string periodId;
};
/**
* @class Parent class
* @brief Abstracted parent class
*/
class TsbSegment
{
protected:
/* data */
std::string url; /**< URL of the fragment init or media*/
AampMediaType mediaType; /**< Type of the fragment*/
AampTime absolutePositionS; /**< absolute position of the current fragment, in seconds since 1970 */
std::string periodId; /**< period Id of the fragment*/
TsbSegment(std::string link, AampMediaType media, AampTime absolutePositionS, std::string prId) : url(std::move(link)), mediaType(media), absolutePositionS(absolutePositionS), periodId(std::move(prId)){}
public:
/**
* @fn GetUrl
* @return string url
*/
std::string GetUrl() { return url; }
/**
* @fn GetMediaType
* @return AampMediaType media type
*/
AampMediaType GetMediaType() { return mediaType; }
/**
* @fn GetPeriodId
* @return API to get periodId
*/
std::string& GetPeriodId() { return periodId; }
/**
* @fn GetAbsolutePosition
*
* @return absolute position of the current fragment, in seconds since 1970
*/
AampTime GetAbsolutePosition() const { return absolutePositionS; }
};
/**
* @class TsbInitData
* @brief Prototype to Store the fragment and initFragment information aka meta data
*/
class TsbInitData : public TsbSegment
{
private:
/* data */
StreamInfo fragStreamInfo; /**< Fragment stream info such as bandwidth, resolution and framerate*/
int profileIndex;
unsigned long users; /**< No of fragments using this init data*/
public:
/**
* @fn incrementUser
* @brief Increment count of fragments used
*/
void incrementUser() { users++; };
/**
* @fn decrementUser
* @brief Decrement count of fragments used
*/
void decrementUser() { users--; };
/**
* @fn constructor
* @param[in] url - Segment URL as string
* @param[in] media - Segment type as AampMediaType
* @param[in] absolutePositionS - absolute position of the current fragment, in seconds since 1970
* @param[in] streamInfo - fragment stream info
* @param[in] prId - Period Id of the fragment
* @param[in] profileIdx - ABR profile index
* @return void
*/
TsbInitData(std::string url, AampMediaType media, AampTime absolutePositionS, const StreamInfo &streamInfo, std::string prId, int profileIdx)
: TsbSegment(std::move(url), media, absolutePositionS, std::move(prId)), fragStreamInfo(streamInfo), users(0), profileIndex(profileIdx)
{
}
/**
* @brief destructor of init data
*/
~TsbInitData()
{
}
/**
* @fn GetBandWidth
* @brief Get the bandwidth information
* @return Bandwidth information in bits per second
*/
BitsPerSecond GetBandWidth() { return fragStreamInfo.bandwidthBitsPerSecond; }
/**
* @fn GetCacheFragStreamInfo
* @brief Get the cached fragment stream info
* @return Fragment stream info
*/
const StreamInfo& GetCacheFragStreamInfo() { return fragStreamInfo; }
/**
* @fn GetProfileIndex
* @brief Get ABR profile index
* @return ABR profile index
*/
int GetProfileIndex() { return profileIndex; }
/**
* @brief Get the fragments used count
* @fn GetUsers
* @return fragments used count as unsigned long
*/
unsigned long GetUsers() { return users; }
};
/**
* @class TsbFragmentData
* @brief Linked list with data related to the fragment and initFragment stored in AAMP TSB
*/
class TsbFragmentData : public TsbSegment
{
private:
AampTime duration; /**< duration of the current fragment*/
AampTime mPTS; /**< PTS of the current fragment in seconds before applying PTS offset, i.e. ISO BMFF baseMediaDecodeTime / timescale */
bool isDiscontinuous; /**< the current fragment is discontinuous*/
std::shared_ptr<TsbInitData> initFragData; /**< init Fragment of the current fragment*/
uint32_t timeScale; /**< timescale of the current fragment */
AampTime PTSOffset; /**< PTS offset of the current fragment */
/* data */
public:
std::shared_ptr<TsbFragmentData> next; /**< Link list next node for easy access*/
std::weak_ptr<TsbFragmentData> prev; /**< Non-owning link to previous node for easy access, and to avoid circular references*/
/**
* @fn constructor
* @param[in] url - Segment URL as string
* @param[in] media - Segment type as AampMediaType
* @param[in] absolutePositionS - absolute position of the current fragment, in seconds since 1970
* @param[in] duration - duration of the current fragment
* @param[in] pts - PTS of the current fragment in seconds before applying PTS offset, i.e. ISO BMFF baseMediaDecodeTime / timescale
* @param[in] disc - discontinuity flag
* @param[in] prId - Period Id of the fragment
* @param[in] initData - Pointer to initData
* @param[in] timeScale - timescale of the current fragment
* @param[in] PTSOffset - PTS offset of the current fragment
*/
TsbFragmentData(std::string url, AampMediaType media, AampTime absolutePositionS, AampTime duration, AampTime pts, bool disc,
std::string prId, std::shared_ptr<TsbInitData> initData, uint32_t timeScale, AampTime PTSOffset)
: TsbSegment(std::move(url), media, absolutePositionS, std::move(prId)), duration(duration), mPTS(pts), isDiscontinuous(disc), initFragData(std::move(initData)),
timeScale(timeScale), PTSOffset(PTSOffset)
{
}
/**
* @fn Destructor
*/
~TsbFragmentData()
{
}
/**
* @fn GetInitFragData
* @return return initFragment shared pointer associated with it
*/
std::shared_ptr<TsbInitData> GetInitFragData() const { return initFragData; }
/**
* @fn GetPTS
*
* @return Query the PST of fragment
*/
AampTime GetPTS() const { return mPTS; }
/**
* @fn GetDuration
*
* @return duration of the fragment as double
*/
AampTime GetDuration() const { return duration; }
/**
* @fn IsDiscontinuous
*
* @return whether is it is discontinuous fragment
*/
bool IsDiscontinuous() const { return isDiscontinuous; }
/**
* @fn GetTimeScale
*
* @return timescale of the fragment
*/
uint32_t GetTimeScale() const { return timeScale; }
/**
* @fn GetPTSOffset
*
* @return PTS offset of the fragment
*/
AampTime GetPTSOffset() const { return PTSOffset; }
};
typedef std::shared_ptr<TsbFragmentData> TsbFragmentDataPtr;
typedef std::shared_ptr<TsbInitData> TsbInitDataPtr;
/**
* @class AampTsbDataManager
* @brief Handle the TSB meta Data informations;
*/
class AampTsbDataManager
{
private:
/**
* @brief Internal API to compare data in the sorted Map
*/
struct PositionComparator
{
bool operator()(const double &a, const double &b) const
{
return a < b;
}
};
std::mutex mTsbDataMutex;
std::map<double, std::shared_ptr<TsbFragmentData>, PositionComparator> mTsbFragmentData;
std::list<std::shared_ptr<TsbInitData>> mTsbInitData;
std::shared_ptr<TsbInitData> mCurrentInitData;
std::shared_ptr<TsbFragmentData> mCurrHead;
double mRelativePos;
public:
/**
* @brief Construct a new Aamp Tsb Data Manager object
*/
AampTsbDataManager() : mTsbDataMutex(), mTsbFragmentData(), mTsbInitData(), mCurrentInitData(nullptr), mCurrHead(nullptr), mRelativePos(0.0)
{
AAMPLOG_INFO("Constructor");
}
/**
* @brief Destroy the Aamp Tsb Data Manager object
*/
~AampTsbDataManager()
{
AAMPLOG_INFO("Destructor");
}
/**
* @fn GetNearestFragment
* @brief Get the nearest fragment for the position
* @param[in] position - Absolute position of the fragment, in seconds since 1970
* @return pointer to the nearest fragment data
*/
std::shared_ptr<TsbFragmentData> GetNearestFragment(double position);
/**
* @fn GetFragment
* @brief Get fragment for the position
* @param[in] position - Exact absolute position of the fragment, in seconds since 1970
* @param[out] eos - Flag to identify the End of stream
* @return pointer to Fragment data and TsbFragmentData
*/
std::shared_ptr<TsbFragmentData> GetFragment(double position, bool &eos);
/**
* @fn GetFirstFragmentPosition
* @return Absolute position of the first fragment, in seconds since 1970
*/
double GetFirstFragmentPosition();
/**
* @fn GetLastFragmentPosition
* @return Absolute position of the last fragment, in seconds since 1970
*/
double GetLastFragmentPosition();
/**
* @fn GetFirstFragment
* @return return the first fragment in the list
*/
TsbFragmentDataPtr GetFirstFragment();
/**
* @fn GetLastFragment
* @return return the last fragment in the list
*/
TsbFragmentDataPtr GetLastFragment();
/**
* @fn RemoveFragment
* @param[in,out] deleteInit - True if init segment was removed as well
* @return Shared pointer to removed fragment
*/
std::shared_ptr<TsbFragmentData> RemoveFragment(bool &deleteInit);
/**
* @fn RemoveFragments
* @brief Remove all fragments until the given position
* @param[in] position - Absolute position, in seconds since 1970, to remove segment until
* @return shared pointer List of TSB fragments removed
*/
std::list<std::shared_ptr<TsbFragmentData>> RemoveFragments(double position);
/**
* @fn AddInitFragment
* @param[in] url - Segment URL as string
* @param[in] media - Segment type as AampMediaType
* @param[in] streamInfo - fragment stream info
* @param[in] periodId - Period Id of this fragment
* @param[in] absPosition - Abs position of this fragment
* @param[in] profileIdx - ABR profile index
* @return true if no exception
*/
bool AddInitFragment(std::string &url, AampMediaType media, const StreamInfo &streamInfo, std::string &periodId, double absPosition, int profileIdx = 0);
/**
* @fn AddFragment
* @brief AddFragment - add Fragment to TSB data
* @param[in] writeData - Segment data
* @param[in] media - Segment type as AampMediaType
* @param[in] discont - discontinuity flag
* @return true if no exception
*/
bool AddFragment(TSBWriteData &writeData, AampMediaType media, bool discont);
/**
* @fn IsFragmentPresent
* @brief Check for any fragment availability at the given position
* @param[in] position - Absolute position of the fragment, in seconds since 1970
* @return true if present
*/
bool IsFragmentPresent(double position);
/**
* @fn Flush
* @return none
*/
void Flush();
/**
* @fn GetNextDiscFragment
* @brief API to get next discontinuous fragment in the list. If not found, will return nullptr.
* @param[in] position - Absolute position, in seconds since 1970, for querying the discontinuous fragment
* @param[in] backwordSearch - Search direction from the position to discontinuous fragment, default forward
* @return TsbFragmentData shared object to fragment data
*/
std::shared_ptr<TsbFragmentData> GetNextDiscFragment(double position, bool backwordSearch = false);
/**
* @fn DumpData
* @return true if no exception
*/
bool DumpData();
};
/**EOF*/
#endif