-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAampTsbMetaDataManager.cpp
More file actions
467 lines (403 loc) · 12.1 KB
/
AampTsbMetaDataManager.cpp
File metadata and controls
467 lines (403 loc) · 12.1 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2025 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 <inttypes.h>
#include <memory>
#include "AampTsbMetaDataManager.h"
#include "AampLogManager.h"
// AampTsbMetaDataManager implementation
AampTsbMetaDataManager::AampTsbMetaDataManager()
: mNextOrderAdded(1)
{
// Constructor implementation
}
AampTsbMetaDataManager::~AampTsbMetaDataManager()
{
Cleanup();
}
void AampTsbMetaDataManager::Initialize()
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
mMetaDataMap.clear();
mTypeTransienceMap.clear();
}
void AampTsbMetaDataManager::Cleanup()
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
mMetaDataMap.clear();
mTypeTransienceMap.clear();
}
/**
* @brief Register a metadata type to be used with the manager.
* @param type The metadata type to register.
* @param isTransient Whether metadata of this type is transient.
* @return True if registered successfully, false if the type already exists.
*/
bool AampTsbMetaDataManager::RegisterMetaDataType(AampTsbMetaData::Type type, bool isTransient)
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
bool ret = false;
// Check if type already exists
if (mMetaDataMap.find(type) != mMetaDataMap.end())
{
AAMPLOG_ERR("Metadata type %u already registered", (uint32_t)type);
}
else
{
// Add type with empty list
mMetaDataMap[type] = std::list<std::shared_ptr<AampTsbMetaData>>();
// Store the transience property for this type
mTypeTransienceMap[type] = isTransient;
AAMPLOG_INFO("Registered metadata type %u, isTransient=%d", (uint32_t)type, isTransient);
ret = true;
}
return ret;
}
/**
* @brief Check if a metadata type is registered and get its transience property.
* @param type The metadata type to check.
* @param[out] isTransient If type is registered, set to the transience property.
* @param[out] metadataList If type is registered, set to the list of metadata for this type.
* @return True if the metadata type is registered, false otherwise.
*/
bool AampTsbMetaDataManager::IsRegisteredType(AampTsbMetaData::Type type, bool& isTransient,
std::list<std::shared_ptr<AampTsbMetaData>>** metadataList) const
{
auto transIt = mTypeTransienceMap.find(type);
auto dataIt = mMetaDataMap.find(type);
bool registered = (transIt != mTypeTransienceMap.end() && dataIt != mMetaDataMap.end());
if (registered)
{
isTransient = transIt->second;
if (metadataList)
{
*metadataList = const_cast<std::list<std::shared_ptr<AampTsbMetaData>>*>(&dataIt->second);
}
}
AAMPLOG_INFO("Metadata type %u%s registered", (uint32_t)type, registered ? "" : "not ");
return registered;
}
/**
* @brief Calculate the total count of all metadata items across all types.
* @note This method assumes the caller holds mTsbMetaDataMutex.
* @return Total count of metadata items.
*/
size_t AampTsbMetaDataManager::CalculateTotalCount() const
{
size_t totalCount = 0;
for (const auto& pair : mMetaDataMap)
{
totalCount += pair.second.size();
}
return totalCount;
}
bool AampTsbMetaDataManager::AddMetaData(const std::shared_ptr<AampTsbMetaData> &metaData)
{
bool success = false;
if (!metaData)
{
AAMPLOG_WARN("Attempt to add null metadata");
}
else
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
AampTsbMetaData::Type type = metaData->GetType();
bool isTransient = false;
std::list<std::shared_ptr<AampTsbMetaData>>* metaDataList = nullptr;
if (!IsRegisteredType(type, isTransient, &metaDataList))
{
AAMPLOG_ERR("Cannot add metadata of type %u - type not registered", (uint32_t)type);
}
else
{
// Check if this exact metadata object already exists in the list
bool alreadyExists = false;
for (const auto& existingMetaData : *metaDataList)
{
if (existingMetaData == metaData)
{
AAMPLOG_WARN("Attempt to add duplicate metadata object of type %u", (uint32_t)type);
alreadyExists = true;
break;
}
}
if (!alreadyExists)
{
// Set incrementing order value to ensure deterministic ordering when positions are equal
metaData->SetOrderAdded(mNextOrderAdded++);
if (mNextOrderAdded == 0)
{
AAMPLOG_WARN("mNextOrderAdded overflow");
mNextOrderAdded = 1;
}
metaData->Dump("Add ");
// Custom comparison function for lower_bound considering both position and orderAdded
auto compareMetaData = [](const std::shared_ptr<AampTsbMetaData>& a, const std::shared_ptr<AampTsbMetaData>& b)
{
bool ret;
if (a->GetPosition() != b->GetPosition())
{
ret = a->GetPosition() < b->GetPosition();
}
else
{
ret = a->GetOrderAdded() < b->GetOrderAdded();
}
return ret;
};
// Find the correct insertion point using the custom comparison function
auto it = std::lower_bound(metaDataList->begin(), metaDataList->end(), metaData, compareMetaData);
metaDataList->insert(it, metaData);
AAMPLOG_TRACE("Added metadata of type %u at position %" PRIu64 "ms with order %u, total count: %zu",
(uint32_t)type, metaData->GetPosition().milliseconds(), metaData->GetOrderAdded(), CalculateTotalCount());
success = true;
}
}
}
return success;
}
bool AampTsbMetaDataManager::RemoveMetaData(const std::shared_ptr<AampTsbMetaData> &metaData)
{
bool removed = false;
if (!metaData)
{
AAMPLOG_WARN("Attempt to remove null metadata");
}
else
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
AampTsbMetaData::Type type = metaData->GetType();
bool isTransient = false;
std::list<std::shared_ptr<AampTsbMetaData>>* list = nullptr;
if (IsRegisteredType(type, isTransient, &list))
{
for (auto it = list->begin(); it != list->end(); )
{
if (*it == metaData)
{
(*it)->Dump("Erase ");
it = list->erase(it);
removed = true;
break;
}
else
{
++it;
}
}
if (removed)
{
AAMPLOG_TRACE("Removed metadata of type %u, remaining count: %zu",
(uint32_t)type, CalculateTotalCount());
}
}
}
return removed;
}
/**
* @brief Remove metadata at or before a specific position based on transience.
* @param positionMs Position in milliseconds.
* @return Number of items removed
*/
size_t AampTsbMetaDataManager::RemoveMetaData(const AampTime &position)
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
AAMPLOG_INFO("Removing metadata at or before position = %" PRIu64 "ms", position.milliseconds());
size_t totalRemoved = 0;
for (auto& mapEntry : mMetaDataMap)
{
AampTsbMetaData::Type type = mapEntry.first;
// Process only metadata of the matching type
std::list<std::shared_ptr<AampTsbMetaData>>* list = nullptr;
bool isTransient = false;
if (!IsRegisteredType(type, isTransient, &list))
{
continue;
}
if (list->empty())
{
continue;
}
// For non-transient metadata: keep latest up to position, remove rest
const size_t sizeBefore = list->size();
auto latestIt = list->end();
auto it = list->begin();
// Find the latest metadata at or before position
while (it != list->end())
{
if ((*it)->GetPosition() > position)
{
break;
}
latestIt = it;
++it;
}
if (latestIt == list->end())
{
AAMPLOG_INFO("No metadata found at or before position %" PRIu64 "ms", position.milliseconds());
continue;
}
// If transient we want to remove everything up to and including the metadata at "position"
// If non-transient we want to remove everything up to the metadata at "position", or if
// there is no metadata at "position" we want to remove everything up to the previous metadata
// before "position"
if (isTransient)
{
++latestIt;
}
const char* typeStr = isTransient ? "(transient) " : "(non-transient) ";
std::string prefix = std::string("Erase ") + typeStr + std::to_string(position.milliseconds()) + "ms ";
// If we found one, keep it and remove everything before it
it = list->begin();
// Remove up to latestIt
while (it != latestIt)
{
(*it)->Dump(prefix);
it = list->erase(it);
}
const size_t sizeAfter = list->size();
size_t removedFromType = sizeBefore - sizeAfter;
totalRemoved += removedFromType;
AAMPLOG_INFO("Type %u: removed %zu items, remaining %zu items",
(uint32_t)type, removedFromType, sizeAfter);
}
AAMPLOG_INFO("Metadata removal complete, removed %zu items, remaining: %zu",
totalRemoved, CalculateTotalCount());
return totalRemoved;
}
size_t AampTsbMetaDataManager::RemoveMetaDataIf(std::function<bool(const std::shared_ptr<AampTsbMetaData>&)> filter)
{
size_t totalRemoved = 0;
if (!filter)
{
AAMPLOG_WARN("Null filter provided to RemoveMetaDataIf");
}
else
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
// Iterate through all types in the map
for (auto& mapEntry : mMetaDataMap) {
auto& list = mapEntry.second;
size_t sizeBefore = list.size();
for (auto it = list.begin(); it != list.end(); )
{
if (filter(*it))
{
(*it)->Dump("Erase ");
it = list.erase(it);
}
else
{
++it;
}
}
size_t removedFromType = sizeBefore - list.size();
totalRemoved += removedFromType;
}
if (totalRemoved > 0)
{
AAMPLOG_TRACE("Removed %zu metadata items via filter, remaining count: %zu",
totalRemoved, CalculateTotalCount());
}
}
return totalRemoved;
}
size_t AampTsbMetaDataManager::GetSize() const
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
return CalculateTotalCount();
}
void AampTsbMetaDataManager::Dump()
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
size_t totalCount = CalculateTotalCount();
AAMPLOG_INFO("Dumping TSB Metadata - Start (%zu items)", totalCount);
for (const auto& pair : mMetaDataMap)
{
AAMPLOG_INFO("Metadata Type: %u, Count: %zu", (uint32_t)pair.first, pair.second.size());
for (const auto& metaData : pair.second)
{
metaData->Dump("Dump ");
}
}
AAMPLOG_INFO("Dumping TSB Metadata - End");
}
bool AampTsbMetaDataManager::ChangeMetaDataPosition(
const std::list<std::shared_ptr<AampTsbMetaData>>& metaDataList, const AampTime& newPosition)
{
bool allUpdated = true;
if (metaDataList.empty())
{
AAMPLOG_WARN("Empty metadata list provided to ChangeMetaDataPosition");
allUpdated = false;
}
else
{
std::lock_guard<std::mutex> lock(mTsbMetaDataMutex);
for (const auto& metaData : metaDataList)
{
if (!metaData)
{
AAMPLOG_WARN("Null metadata found in list");
allUpdated = false;
continue;
}
AampTsbMetaData::Type type = metaData->GetType();
bool isTransient = false;
std::list<std::shared_ptr<AampTsbMetaData>>* list = nullptr;
// Verify the metadata type is registered
if (!IsRegisteredType(type, isTransient, &list))
{
AAMPLOG_ERR("Metadata type %u not registered", (uint32_t)type);
allUpdated = false;
continue;
}
// Find the metadata in our list
auto it = std::find(list->begin(), list->end(), metaData);
if (it == list->end())
{
AAMPLOG_ERR("Metadata not found in manager");
allUpdated = false;
continue;
}
(*it)->Dump("Change Pos ");
// Remove and reinsert to maintain ordering
list->erase(it);
metaData->SetPosition(newPosition);
// Find new insertion point - considering both position and orderAdded
auto rpos = list->rbegin();
while (rpos != list->rend())
{
if ((*rpos)->GetPosition() < newPosition)
{
break;
}
if ((*rpos)->GetPosition() == newPosition &&
(*rpos)->GetOrderAdded() <= metaData->GetOrderAdded())
{
break;
}
++rpos;
}
list->insert(rpos.base(), metaData);
AAMPLOG_TRACE("Updated metadata of type %u to position %" PRIu64 "ms with order %u",
(uint32_t)type, newPosition.milliseconds(), metaData->GetOrderAdded());
}
}
return allUpdated;
}