-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpd_parse.c
More file actions
255 lines (243 loc) · 11.8 KB
/
Copy pathmpd_parse.c
File metadata and controls
255 lines (243 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
#include "mpd_parse.h"
#include "utils.h"
int parse_mpd(char *filename, manifest *m)
{
xmlDoc *document;
// manifest m[] = {0};
xmlNode *root, *first_child, *node, *second_child, *node2, *third_child, *node3, *node4, *fourth_child;
xmlAttr *attribute;
char duration[25] = "\0";
float dur, segdur = 0, timescale = 0;
int num_of_rates = 0; //height;
char segnum[5];
char bw[25] = "", repid[25] = "";
char bandwidth[MAX_SUPPORTED_BITRATE_LEVELS][25];
char id[MAX_SUPPORTED_BITRATE_LEVELS][5] = {{0}};
char base_url[MAXURLLENGTH] = "";
char init_url[MAX_SUPPORTED_BITRATE_LEVELS][MAXURLLENGTH] = {""};
char media_url[MAX_SUPPORTED_BITRATE_LEVELS][MAXURLLENGTH] = {""};
int startNumber[MAX_SUPPORTED_BITRATE_LEVELS], sn = 1;
char init_url_template[MAXURLLENGTH] = "";
char media_url_template[MAXURLLENGTH] = "";
char *tmp, *newurl;
char keyword_id[12] = "$id$";
char keyword_bw[12] = "$Bandwidth$";
char keyword_num[12] = "$Number$";
document = xmlReadFile(filename, NULL, 0);
root = xmlDocGetRootElement(document);
//fprintf(stdout, "Root is <%s> (%i)\n", root->name, root->type);
first_child = root->children;
for (node = first_child; node; node = node->next)
{
//fprintf(stdout, "\t Child is <%s> (%i)\n", node->name, node->type);
if (xmlStrcmp(node->name, (const xmlChar *)"Period") == 0)
{
attribute = node->properties;
while (attribute)
{
if (xmlStrcmp(attribute->name, (const xmlChar *)"duration") == 0)
{
xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);
//fprintf(stdout, "\t\t%s : %s\n", (char *)attribute->name, (char *)value);
strcpy(duration, (char *)value);
//TODO: Why is get_duration() function called twice here?
fprintf(stdout, "Duration is %s and %f\n", duration, dur = get_duration(duration));
get_duration(duration);
xmlFree(value);
break;
}
attribute = attribute->next;
}
second_child = node->children;
for (node2 = second_child; node2; node2 = node2->next)
{
//fprintf(stdout, "\t\t Child is <%s> (%i)\n", node2->name, node2->type);
if (xmlStrcmp(node2->name, (const xmlChar *)"AdaptationSet") == 0)
{
third_child = node2->children;
for (node4 = third_child; node4; node4 = node4->next)
{
//fprintf(stdout, "\t\t Child is <%s> (%i)\n", node4->name, node4->type);
if (xmlStrcmp(node4->name, (const xmlChar *)"Representation") == 0)
{
fourth_child = node4->children;
attribute = node4->properties;
while (attribute)
{
if (xmlStrcmp(attribute->name, (const xmlChar *)"bandwidth") == 0)
{
xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);
strcpy(bw, (char *)value);
xmlFree(value);
break;
}
}
for (node3 = fourth_child; node3; node3 = node3->next)
{
if (xmlStrcmp(node3->name, (const xmlChar *)"SegmentTemplate") == 0)
{
//fprintf(stdout, "\t\t Child is <%s> (%i)\n", node3->name, node3->type);
attribute = node3->properties;
while (attribute)
{
//fprintf(stdout, ">>>>>>>>>>>>>>>>%s\n", (char *)attribute->name);
if (xmlStrcmp(attribute->name, (const xmlChar *)"duration") == 0)
{
xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);
segdur = atoi((char *)value);
xmlFree(value);
}
else if (xmlStrcmp(attribute->name, (const xmlChar *)"timescale") == 0)
{
xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);
timescale = atoi((char *)value);
xmlFree(value);
}
else if (xmlStrcmp(attribute->name, (const xmlChar *)"startNumber") == 0)
{
xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);
sn = atoi((char *)value);
xmlFree(value);
}
else if (xmlStrcmp(attribute->name, (const xmlChar *)"initialization") == 0 || xmlStrcmp(attribute->name, (const xmlChar *)"index") == 0)
{
xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);
strcpy(init_url_template, base_url);
strcat(init_url_template, (char *)value);
//fprintf(stdout, "Init_url being filled here!!! %s (%s)\n", init_url_template, (char *)value);
fflush(stdout);
xmlFree(value);
}
else if (xmlStrcmp(attribute->name, (const xmlChar *)"media") == 0)
{
xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);
strcpy(media_url_template, base_url);
strcat(media_url_template, (char *)value);
fprintf(stdout, "Media_url is %s \n", media_url_template);
strcpy(media_url[num_of_rates], media_url_template);
strcpy(init_url[num_of_rates], init_url_template);
strcpy(bandwidth[num_of_rates], bw);
strcpy(id[num_of_rates], repid);
startNumber[num_of_rates] = sn;
fprintf(stdout, "Init segment : %s\n", init_url[num_of_rates]);
fprintf(stdout, "Media segment : %s\n", media_url[num_of_rates]);
fprintf(stdout, "Timescale : %.0f, Seg duration : %.0f\n", timescale, segdur);
fprintf(stdout, "Number of Segments = %d\n", m->num_of_segments);
fprintf(stdout, "\t bw: %s\n\n", bandwidth[num_of_rates]);
num_of_rates++;
xmlFree(value);
}
attribute = attribute->next;
}
if (strlen(init_url_template) > 0)
m->init = 1;
else
m->init = 0;
m->num_of_segments = ceil(dur / (segdur / timescale)) + m->init;
}
}
}
}
if (num_of_rates >= MAX_SUPPORTED_BITRATE_LEVELS)
{
fprintf(stdout, "Number of rate levels exceeds the maximum allowed value\n");
return -1;
}
}
}
}
}
if (m->num_of_segments < 0)
{
fprintf(stdout, "Number of rate levels / segments is negative, check mpd. \n");
return -1;
}
m->num_of_levels = num_of_rates;
m->segment_dur_ms = (segdur / timescale);
int j, k;
for (j = 0; j < num_of_rates; j++)
{
level *next_level = &m->bitrate_level[j];
next_level->segments = (char **)malloc(m->num_of_segments * sizeof(char *));
for (k = 0; k < m->num_of_segments; k++)
next_level->segments[k] = (char *)malloc(MAXURLLENGTH * sizeof(char));
next_level->bitrate = atoi(bandwidth[j]);
fprintf(stdout, "bandwith: %ld\n", next_level->bitrate);
if (strlen(init_url[j]) != 0)
{
newurl = str_replace(init_url[j], keyword_bw, bandwidth[j]);
if (newurl == NULL)
{
newurl = str_replace(init_url[j], keyword_id, id[j]);
if (newurl == NULL)
strcpy(next_level->segments[0], init_url[j]);
else
{
strcpy(next_level->segments[0], newurl);
free(newurl);
}
}
else
{
fprintf(stdout, "Replaced bandwidth \n");
strcpy(next_level->segments[0], newurl);
free(newurl);
}
}
sn = startNumber[j];
for (k = m->init; k < m->num_of_segments; k++)
{
sprintf(segnum, "%d", sn);
sn++;
tmp = str_replace(media_url[j], keyword_bw, bandwidth[j]);
if (tmp == NULL)
{
tmp = str_replace(media_url[j], keyword_id, id[j]);
if (tmp == NULL)
{
newurl = str_replace(media_url[j], keyword_num, segnum);
if (newurl == NULL)
{
strcpy(next_level->segments[k], media_url[j]);
}
else
{
strcpy(next_level->segments[k], newurl);
free(newurl);
}
}
else
{
newurl = str_replace(tmp, keyword_num, segnum);
if (newurl == NULL)
{
strcpy(next_level->segments[k], tmp);
}
else
{
strcpy(next_level->segments[k], newurl);
free(newurl);
}
free(tmp);
}
}
else
{
newurl = str_replace(tmp, keyword_num, segnum);
if (newurl == NULL)
{
strcpy(next_level->segments[k], tmp);
}
else
{
strcpy(next_level->segments[k], newurl);
free(newurl);
}
free(tmp);
}
// fprintf(stdout, "%s\n", next_level->segments[k]);
}
}
fprintf(stdout, "...\n");
return 0;
}