-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAampMPDUtils.cpp
More file actions
482 lines (435 loc) · 11.5 KB
/
AampMPDUtils.cpp
File metadata and controls
482 lines (435 loc) · 11.5 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2023 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 "AampMPDUtils.h"
#include "AampBoxReader.h"
#include <inttypes.h>
/**
* @brief Get xml node form reader
*
* @retval xml node
*/
Node* MPDProcessNode(xmlTextReaderPtr *reader, std::string url, bool isAd)
{
static int UNIQ_PID = 0;
int type = xmlTextReaderNodeType(*reader);
if (type != WhiteSpace && type != Text && type != XML_CDATA_SECTION_NODE)
{
while (type == Comment || type == WhiteSpace)
{
if(!xmlTextReaderRead(*reader))
{
AAMPLOG_WARN("xmlTextReaderRead failed");
}
type = xmlTextReaderNodeType(*reader);
}
Node *node = new Node();
node->SetType(type);
node->SetMPDPath(Path::GetDirectoryPath(url));
const char *name = (const char *)xmlTextReaderConstName(*reader);
if (name == NULL)
{
SAFE_DELETE(node);
return NULL;
}
int isEmpty = xmlTextReaderIsEmptyElement(*reader);
node->SetName(name);
AddAttributesToNode(reader, node);
if(!strcmp("Period", name))
{
if(!node->HasAttribute("id"))
{
// Add a unique period id. AAMP needs these in multi-period
// static DASH assets to identify the current period.
std::string periodId = std::to_string(UNIQ_PID++) + "-";
node->AddAttribute("id", periodId);
}
else if(isAd)
{
// Make ad period ids unique. AAMP needs these for playing the same ad back to back.
std::string periodId = std::to_string(UNIQ_PID++) + "-" + node->GetAttributeValue("id");
node->AddAttribute("id", periodId);
}
else
{
// Non-ad period already has an id. Don't change dynamic period ids.
}
}
if (isEmpty)
return node;
Node *subnode = NULL;
int ret = xmlTextReaderRead(*reader);
int subnodeType = xmlTextReaderNodeType(*reader);
while (ret == 1)
{
if (!strcmp(name, (const char *)xmlTextReaderConstName(*reader)))
{
return node;
}
if(subnodeType != Comment && subnodeType != WhiteSpace)
{
subnode = MPDProcessNode(reader, url, isAd);
if (subnode != NULL)
node->AddSubNode(subnode);
}
ret = xmlTextReaderRead(*reader);
subnodeType = xmlTextReaderNodeType(*reader);
}
return node;
}
else if (type == Text || type == XML_CDATA_SECTION_NODE)
{
xmlChar* text = nullptr;
if (type == XML_CDATA_SECTION_NODE)
{
text = xmlTextReaderValue(*reader); // CDATA section
}
else
{
text = xmlTextReaderReadString(*reader); // Regular text node
}
if (text != NULL)
{
Node *node = new Node();
node->SetType(type);
node->SetText((const char*)text);
xmlFree(text);
return node;
}
}
return NULL;
}
/**
* @brief Add attributes to xml node
* @param reader xmlTextReaderPtr
* @param node xml Node
*/
void AddAttributesToNode(xmlTextReaderPtr *reader, Node *node)
{
if (xmlTextReaderHasAttributes(*reader))
{
while (xmlTextReaderMoveToNextAttribute(*reader))
{
std::string key = (const char *)xmlTextReaderConstName(*reader);
if(!key.empty())
{
std::string value = (const char *)xmlTextReaderConstValue(*reader);
node->AddAttribute(key, value);
}
else
{
AAMPLOG_WARN("key is null"); //CID:85916 - Null Returns
}
}
}
}
/**
* @brief Check if mime type is compatible with media type
* @param mimeType mime type
* @param mediaType media type
* @retval true if compatible
*/
bool IsCompatibleMimeType(const std::string& mimeType, AampMediaType mediaType)
{
bool isCompatible = false;
switch ( mediaType )
{
case eMEDIATYPE_VIDEO:
if (mimeType == "video/mp4")
isCompatible = true;
break;
case eMEDIATYPE_AUDIO:
if ((mimeType == "audio/webm") ||
(mimeType == "audio/mp4"))
isCompatible = true;
break;
case eMEDIATYPE_SUBTITLE:
if ((mimeType == "application/ttml+xml") ||
(mimeType == "text/vtt") ||
(mimeType == "application/mp4"))
isCompatible = true;
break;
default:
break;
}
return isCompatible;
}
/**
* @brief Computes the fragment duration
* @param duration of the fragment.
* @param timeScale value.
* @return - computed fragment duration in double.
*/
double ComputeFragmentDuration( uint32_t duration, uint32_t timeScale )
{
double newduration = 2.0;
if( duration && timeScale )
{
newduration = (double)duration / (double)timeScale;
}
else
{
AAMPLOG_ERR("Invalid %u %u",duration,timeScale);
}
return newduration;
}
/**
* @brief Parse segment index box
* @note The SegmentBase indexRange attribute points to Segment Index Box location with segments and random access points.
* @param start start of box
* @param size size of box
* @param segmentIndex segment index
* @param[out] referenced_size referenced size
* @param[out] referenced_duration referenced duration
* @retval true on success
*/
bool ParseSegmentIndexBox( const uint8_t *start, size_t size, int segmentIndex, unsigned int *referenced_size, float *referenced_duration, unsigned int *firstOffset)
{
if ((!start) || (size < 4))
{
AAMPLOG_WARN("Invalid parameters in ParseSegmentIndexBox: start=%p, size=%zu", start, size);
return false;
}
constexpr int SIDX_ENTRY_SIZE = 12;
constexpr uint32_t SIDX_BOX_TYPE =
('s' << 24) | ('i' << 16) | ('d' << 8) | 'x';
BoxReader reader{start};
auto len = reader.Read<uint32_t>();
if (len != size)
{
AAMPLOG_WARN("Wrong size in ParseSegmentIndexBox %u found, %zu expected", len, size);
if (firstOffset) *firstOffset = 0;
return false;
}
auto type = reader.Read<uint32_t>();
if (type != SIDX_BOX_TYPE)
{
AAMPLOG_WARN("Wrong type in ParseSegmentIndexBox %c%c%c%c found, sidx expected",
(type >> 24) & 0xff, (type >> 16) & 0xff,
(type >> 8) & 0xff, type & 0xff);
if (firstOffset) *firstOffset = 0;
return false;
}
// version is the top 8 bits; lower 24 bits are flags (ignored)
const auto version = static_cast<uint8_t>(reader.Read<uint32_t>() >> 24);
reader.Skip<uint32_t>(); // reference_ID
auto timescale = reader.Read<uint32_t>(); // timescale
uint64_t first_offset{0};
if (version == 0)
{
reader.Skip<uint32_t>(); // earliest_presentation_time
first_offset = reader.Read<uint32_t>(); // first_offset
}
else if (version == 1)
{
reader.Skip<uint64_t>(); // earliest_presentation_time
first_offset = reader.Read<uint64_t>(); // first_offset
}
else
{
AAMPLOG_WARN("Unsupported version in ParseSegmentIndexBox %u found, 0 or 1 expected", version);
if (firstOffset)
{
*firstOffset = 0;
}
return false;
}
reader.Skip<uint16_t>(); // reserved
auto reference_count = reader.Read<uint16_t>();
if (firstOffset)
{
*firstOffset = static_cast<unsigned int>(first_offset);
return true;
}
if (segmentIndex >= 0 && segmentIndex < static_cast<int>(reference_count))
{
reader.Skip(SIDX_ENTRY_SIZE * segmentIndex);
*referenced_size = reader.Read<uint32_t>() & 0x7fffffff;
// top bit is "reference_type"
*referenced_duration = reader.Read<uint32_t>() /
static_cast<float>(timescale);
return true;
}
return false;
}
/**
* @brief Replace matching token with given number
* @param str String in which operation to be performed
* @param from token
* @param toNumber number to replace token
* @retval position
*/
int replace(std::string &str, const std::string &from, uint64_t toNumber)
{
int rc = 0;
size_t tokenLength = from.length();
for (;;)
{
bool done = true;
size_t pos = 0;
for (;;)
{
pos = str.find('$', pos);
if (pos == std::string::npos)
{
break;
}
size_t next = str.find('$', pos + 1);
if (next != 0)
{
if (str.substr(pos + 1, tokenLength) == from)
{
size_t formatLen = next - pos - tokenLength - 1;
char buf[256];
if (formatLen > 0)
{
std::string format = str.substr(pos + tokenLength + 1, formatLen - 1);
char type = str[pos + tokenLength + formatLen];
switch (type)
{ // don't use the number-formatting string from dash manifest as-is; map to uint64_t equivalent
case 'd':
format += PRIu64;
break;
case 'x':
format += PRIx64;
break;
case 'X':
format += PRIX64;
break;
default:
AAMPLOG_WARN("unsupported template format: %s%c", format.c_str(), type);
format += type;
break;
}
snprintf(buf, sizeof(buf), format.c_str(), toNumber);
tokenLength += formatLen;
}
else
{
snprintf(buf, sizeof(buf), "%" PRIu64 "", toNumber);
}
str.replace(pos, tokenLength + 2, buf);
done = false;
rc++;
break;
}
pos = next + 1;
}
else
{
AAMPLOG_WARN("next is not found "); // CID:81252 - checked return
break;
}
}
if (done)
break;
}
return rc;
}
/**
* @brief Replace matching token with given string
* @param str String in which operation to be performed
* @param from token
* @param toString string to replace token
* @retval position
*/
int replace(std::string &str, const std::string &from, const std::string &toString)
{
int rc = 0;
size_t tokenLength = from.length();
for (;;)
{
bool done = true;
size_t pos = 0;
for (;;)
{
pos = str.find('$', pos);
if (pos == std::string::npos)
{
break;
}
size_t next = str.find('$', pos + 1);
if (next != 0)
{
if (str.substr(pos + 1, tokenLength) == from)
{
str.replace(pos, tokenLength + 2, toString);
done = false;
rc++;
break;
}
pos = next + 1;
}
else
{
AAMPLOG_ERR("Error at next"); // CID:81346 - checked return
break;
}
}
if (done)
break;
}
return rc;
}
/**
* @fn ConstructFragmentURL
* @param[out] fragmentUrl fragment url
* @param[in] fragmentDescriptor descriptor
* @param[in] media media information string
* @param[in] config Aamp configuration
*/
void ConstructFragmentURL(std::string &fragmentUrl, const FragmentDescriptor *fragmentDescriptor, std::string media, AampConfig *config)
{
std::string constructedUri = fragmentDescriptor->GetMatchingBaseUrl();
if (media.empty())
{
}
else if (aamp_IsAbsoluteURL(media))
{ // don't pre-pend baseurl if media starts with http:// or https://
constructedUri.clear();
}
else if (!constructedUri.empty())
{
if (config->IsConfigSet(eAAMPConfig_DASHIgnoreBaseURLIfSlash))
{
if (constructedUri == "/")
{
AAMPLOG_WARN("ignoring baseurl /");
constructedUri.clear();
}
}
// append '/' suffix to BaseURL if not already present
if (aamp_IsAbsoluteURL(constructedUri))
{
if (constructedUri.back() != '/')
{
constructedUri += '/';
}
}
}
else
{
AAMPLOG_TRACE("BaseURL not available");
}
constructedUri += media;
replace(constructedUri, "Bandwidth", fragmentDescriptor->Bandwidth);
replace(constructedUri, "RepresentationID", fragmentDescriptor->RepresentationID);
replace(constructedUri, "Number", fragmentDescriptor->Number);
replace(constructedUri, "Time", (uint64_t)fragmentDescriptor->Time );
aamp_ResolveURL(fragmentUrl, fragmentDescriptor->manifestUrl, constructedUri.c_str(), config->IsConfigSet(eAAMPConfig_PropagateURIParam));
}