-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHLSProcessing.php
executable file
·438 lines (375 loc) · 17.1 KB
/
HLSProcessing.php
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
<?php
/* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function processHLS(){
global $session_dir, $mpd_url, $mpd_features, // Client block input
$current_period, $current_adaptation_set, $current_representation, // HLS process data
$progress_xml, $progress_report, $reprsentation_error_log_template, // Reporting
$hls_manifest_type, // HLS data
$cmaf_conformance, $cmaf_function_name, $cmaf_when_to_call, // CMAF data
$ctawave_conformance, $ctawave_function_name, $ctawave_when_to_call; // CTA WAVE data
$StreamInfURLArray = array();
$IframeURLArray = array();
$XMediaURLArray = array();
$CodecArray = array();
## Open related files
$progress_xml = simplexml_load_string('<root><Progress><percent>0</percent><dataProcessed>0</dataProcessed><dataDownloaded>0</dataDownloaded><CurrentAdapt>1</CurrentAdapt><CurrentRep>1</CurrentRep></Progress><completed>false</completed><allDownloadComplete>false</allDownloadComplete></root>');
$progress_xml->asXml($session_dir . '/' . $progress_report);
## Read each line of manifest file into an array
$m3u8 = playlistToArray($mpd_url);
if($m3u8){
## If the manifest is a master playlist
## extract the urls to stream_inf, iframe and x_media playlists from master playlist and save them in separate arrays
## If the manifest is a media playlist
## extract the urls to either stream_inf or iframe and save it into arrays
if($hls_manifest_type=="MasterPlaylist"){
list($StreamInfURLArray, $IframeURLArray,$XMediaURLArray, $CodecArray) = playlistURLs($m3u8);
}
else{
if(strpos(file_get_contents($mpd_url),"#EXT-X-I-FRAMES-ONLY"))
$IframeURLArray[0] =$mpd_url;
else
$StreamInfURLArray[0]=$mpd_url;
}
## Download segments from stream_inf playlists, x_media playlists, and Iframe playlists
if($cmaf_conformance)
$return_arr = $cmaf_function_name($cmaf_when_to_call[0]);
if($ctawave_conformance)
$return_arr = $ctawave_function_name($ctawave_when_to_call[0]);
$file_location = validate_segment_hls(array($StreamInfURLArray, $IframeURLArray,$XMediaURLArray), $CodecArray);
## Group the playlists together
groupPlaylists($file_location);
## Crete $mpd_features structure since it is used in conformance server checks
formMpdFeatures();
## Perform enforced profile segment validation
$current_period = 0;
$current_adaptation_set = 0;
$current_representation = 0;
$adapts = $mpd_features['Period'][$current_period]['AdaptationSet'];
while($current_adaptation_set < sizeof($adapts)){
$reps = $adapts[$current_adaptation_set]['Representation'];
while($current_representation < sizeof($reps)){
$error_log = str_replace(array('$AS$', '$R$'), array($current_adaptation_set, $current_representation), $reprsentation_error_log_template);
if($cmaf_conformance)
$return_seg_val[] = $cmaf_function_name($cmaf_when_to_call[1]);
err_file_op(1);
print_console($session_dir . '/Period0/' . $error_log . '.txt', "Period 1 AdaptationSet " . ($current_adaptation_set+1) . " Representation " . ($current_representation+1) . " Results");
$current_representation++;
}
if($cmaf_conformance)
$return_arr = $cmaf_function_name($cmaf_when_to_call[2]);
$current_representation = 0;
$current_adaptation_set++;
}
## Perform Cross Validation
if($cmaf_conformance)
$return_arr = $cmaf_function_name($cmaf_when_to_call[3]);
if($ctawave_conformance)
$return_arr = $ctawave_function_name($ctawave_when_to_call[1]);
$current_adaptation_set = 0;
err_file_op(2);
}
$progress_xml->allDownloadComplete = "true";
$progress_xml->completed = "true";
$progress_xml->completed->addAttribute('time', time());
$progress_xml->asXml(trim($session_dir . '/' . $progress_report));
writeEndTime((int)$progress_xml->completed->attributes());
exit;
}
#put each line of a playlist into an array
function playlistToArray($url) {
global $hls_manifest_type;
$m3u8 = file_get_contents($url);
if(!$m3u8)
return false;
$array = preg_split('/$\n?^/m', trim($m3u8), NULL, PREG_SPLIT_NO_EMPTY);
if($array[0]!= '#EXTM3U')
return false;
else{
if(strpos($m3u8, 'EXT-X-STREAM-INF') !== FALSE || strpos($m3u8, 'EXT-X-I-FRAME-STREAM-INF') !== FALSE){
$hls_manifest_type = "MasterPlaylist";
return $array;
}
else {
$hls_manifest_type = "MediaPlaylist";
return $array;
}
}
}
//extract the urls to stream_inf, iframe and x_media playlists from master playlist and save them in separate arrays
function playlistURLs($array){
global $mpd_url;
$base_url = dirname($mpd_url);
//get the unique urls of media playlists
foreach ($array as $line) {
$line = trim($line);
if ($line[0] != '#') { // true just in case of checking the stream inf playlist
//each line that is not started with '#' is a url
$streamURL = getAbsURL($line, $base_url);
if(!in_array($streamURL, $StreamInfURLArray)){ //check the url not to be a duplicate
$StreamInfURLArray[] = $streamURL;
}
}
else{
//geting the iframe playlist urls
if(strpos($line,"EXT-X-I-FRAME-STREAM-INF") !== FALSE) {
$IFrameURL = getURLTag($line, $base_url);
if($IFrameURL && !in_array($IFrameURL, $IframeURLArray)){ //check the url not to be a duplicate
$IframeURLArray[] = $IFrameURL;
}
}
//getting the x_media playlist urls
if(strpos($line,"EXT-X-MEDIA") !== FALSE) {
$XMediaURL = getURLTag($line, $base_url);
if($XMediaURL && !in_array($XMediaURL, $XMediaURLArray)){ //check the url not to be a duplicate
$XMediaURLArray[] = $XMediaURL;
}
}
//getting the CODECS
if(strpos($line,"CODECS") !== FALSE) {
$sub = substr($line, strpos($line,"CODECS=")+8);
$codecStr = substr($sub,0,strpos($sub,"\""));
$pos = strpos($codecStr,",");
while($pos !== FALSE) {
$codec = substr($codecStr,0,$pos);
if($codec && !in_array($codec, $CodecArray)){ //check the codec not to be a duplicate
$CodecArray[] = $codec;
}
$codecStr = substr($codecStr,$pos+1);
$pos = strpos($codecStr,",");
}
$codec = $codecStr;
if($codec && !in_array($codec, $CodecArray)){ //check the codec not to be a duplicate
$CodecArray[] = $codec;
}
}
}
}
return [$StreamInfURLArray, $IframeURLArray, $XMediaURLArray, $CodecArray];
}
//returns the absolute url
function getAbsURL($line, $base_url){
if (isAbsoluteURL($line)) { //if absolute url
return $line;
}
else{//if relative url
return $base_url . '/' . $line;
}
}
//return the url that is mentioned in playlist using a tag
function getURLTag($line, $base_url){
if (strpos($line, 'URI') !== false) {
$sub = substr($line, strpos($line,"URI=")+5);
$x = substr($sub,0,strpos($sub,"\"")); //get value of URI tag
return getAbsURL($x, $base_url); //return the absolute url
}
else
return false;
}
/*get the urls to the segments of a playlist
* input is the url to the playlist
* output is an array of segments' urls
*/
function segmentURLs($url){
$segmentURLs = array();
$base_url = dirname($url);
$array = playlistToArray($url);
foreach ($array as $line) {
$line = trim($line);
if ($line[0] != '#') {
$segment = getAbsURL($line, $base_url);
if(!in_array($segment, $segmentURLs)){
$segmentURLs[] = $segment;
}
}
}
return $segmentURLs;
}
/*
* segmentDonload downloads the segments of a playlist and returns the size of downloaded the content
* inputs are the url to the playlist and the type of the playlist
*/
function segmentDownload($urlarray, $type, $is_dolby){
global $session_dir, $hls_iframe_file, $hls_mdat_file, $hls_current_index, $hls_byte_range_begin, $hls_byte_range_size, $progress_xml, $progress_report;
$segment_urls = array();
$sizearray=array();
if(!$urlarray)
return [$segment_urls, $sizearray];
## Define a directory for downloading segments
$dir = $session_dir.'/'.$type;
mkdir($dir, 0777, true);
## Iterate over the playlists, download the segments for each one, and save the segments of each playlist into a different folder
foreach($urlarray as $url){
$progress_xml->Progress->CurrentRep = $hls_current_index+1;
$progress_xml->asXml(trim($session_dir . '/' . $progress_report));
// create a new folder for this playlist
$tmpdir = $dir.'/'.$hls_current_index.'/';
mkdir($tmpdir, 0777, true);
// extract the url of the segments of a playlist
$segmentURLs = segmentURLs($url);
if($type == $hls_iframe_file){
$tmparray = playlistToArray($url);
$array = segURLs($tmparray, $segmentURLs);
if(!$array)
continue;
}
// download data of the playlist into the folder and return size of the downloaded content
$segment_urls[] = $segmentURLs;
$is_subtitle_rep = false; // this is not used in HLS always false
$sizearray[] = download_data($tmpdir, ($type == $hls_iframe_file) ? $array : $segmentURLs, $is_subtitle_rep, $is_dolby);
rename_file($session_dir . '/' . $hls_mdat_file . '.txt', $session_dir . '/' . $type . '_' . $hls_current_index . '_' . $hls_mdat_file . '.txt');
if($type == $hls_iframe_file){
$hls_byte_range_begin = array();
$hls_byte_range_size = array();
}
$hls_current_index++;
}
$hls_current_index = 0;
return [$segment_urls, $sizearray];
}
function segURLs($tmparray,$segmentURL){
global $hls_byte_range_begin, $hls_byte_range_size;
$array = array();
$i = 0;
foreach ($tmparray as $line) {
if(strpos($line,"EXT-X-MAP") !== FALSE)
{
if (strpos($line,"@")!== FALSE ){
$hls_byte_range_begin[]= (int)(substr($line,strpos($line,"@")+1,strlen($line)-strpos($line,"@")));
$st = strpos($line,"BYTERANGE=")+11;
$en = strpos($line,"@");
$hls_byte_range_size[] = (int)(substr($line,$st, $en-$st));
$array[]=$segmentURL[0];
}
else{
if($hls_byte_range_size[$i-1]){
$st = strpos($line,"BYTERANGE=")+11;
$hls_byte_range_begin[] = $hls_byte_range_size[$i-1];
$hls_byte_range_size[] = (int)(substr($line,$st));
}
else{
return NULL;
}
}
}
if(strpos($line,"EXT-X-BYTERANGE") !== FALSE){
if (strpos($line,"@")!== FALSE ){
$hls_byte_range_begin[]= (int)(substr($line,strpos($line,"@")+1));
$st = strpos($line,":")+1;
$en = strpos($line,"@");
$hls_byte_range_size[] = (int)(substr($line,$st, $en-$st));
$array[]=$segmentURL[0];
}
else{
if($hls_byte_range_size[$i-1]){
$st = strpos($line,":")+1;
$hls_byte_range_begin[] = $hls_byte_range_size[$i-1];
$hls_byte_range_size[] = (int)(substr($line,$st));
}
else{
return NULL;
}
}
}
$i++;
}
return $array;
}
function groupPlaylists($file_location){
global $session_dir, $hls_media_types, $adaptation_set_template, $reprsentation_template, $progress_xml, $progress_report, $string_info;
$ResultXML = $progress_xml->addChild('Results');
$PeriodXML = $ResultXML->addChild('Period');
$i = 0;
$file_location_ind = 0;
$period_dir = $session_dir . '/Period0';
create_folder_in_session($period_dir);
foreach($hls_media_types as $hls_media_type){
foreach($hls_media_type as $sw){
$AdaptationXML = $PeriodXML->addChild('Adaptation');
$new_sw_path = str_replace('$AS$', $i, $adaptation_set_template);
create_folder_in_session($period_dir . '/' . $new_sw_path);
$j = 0;
foreach($sw as $track){
$RepXML = $AdaptationXML->addChild('Representation');
$RepXML->addAttribute('id', $j + 1);
if($file_location[$file_location_ind] == 'notexist'){
$RepXML->textContent = "notexist";
$file_location_ind++;
}
else{
$RepXML->textContent = $file_location[0][2];
$file_location_ind ++;
}
$name = explode('_', $track);
$pathdir = $name[0]; $pathdir_ind = $name[1];
$track_path = $session_dir . '/' . $pathdir . '/' . $pathdir_ind;
$new_track_path = str_replace(array('$AS$', '$R$'), array($i, $j), $reprsentation_template);
rename_file($track_path . '.xml', $period_dir . '/' . $new_sw_path . '/' . $new_track_path . '.xml');
rename_file($session_dir . '/' . $track .'log.txt' , $period_dir . '/' . $new_track_path . 'log.txt');
rename_file($track_path, $period_dir . '/' . $new_track_path);
tabulateResults($period_dir . '/' . $new_track_path . 'log.txt', 'Segment');
$j++;
}
$i++;
}
}
$progress_xml->asXml(trim($session_dir . '/' . $progress_report));
}
function formMpdFeatures(){
global $hls_media_types, $mpd_features;
$mpd_features = array();
$mpd_features['Period'][0] = array();
$adapt_cnt = 0;
foreach($hls_media_types as $hls_media_type){
foreach($hls_media_type as $codec => $adaptation_set){
$mpd_features['Period'][0]['AdaptationSet'][$adapt_cnt] = array();
$mpd_features['Period'][0]['AdaptationSet'][$adapt_cnt]['codec'] = $codec;
$rep_count = 0;
foreach($adaptation_set as $representation){
$mpd_features['Period'][0]['AdaptationSet'][$adapt_cnt]['Representation'][$rep_count] = array();
$rep_count++;
}
$adapt_cnt++;
}
}
}
function determineMediaType($path, $tag){
global $hls_media_types, $hls_iframe_file;
if(file_exists($path)){
$xml = get_DOM($path, 'atomlist');
if($xml){
$hdlr = $xml->getElementsByTagName('hdlr')->item(0);
$hdlr_type = $hdlr->getAttribute('handler_type');
$sdType = $xml->getElementsByTagName($hdlr_type.'_sampledescription')->item(0)->getAttribute('sdType');
switch($hdlr_type){
case 'vide':
if(strpos($tag, $hls_iframe_file) === FALSE)
$hls_media_types['video'][$sdType][] = $tag;
else
$hls_media_types['iframe'][$sdType][] = $tag;
break;
case 'soun':
$hls_media_types['audio'][$sdType][] = $tag;
break;
case 'subt':
$hls_media_types['subtitle'][$sdType][] = $tag;
break;
default:
$hls_media_types['unknown'][$sdType][] = $tag;
break;
}
}
else{
$hls_media_types['unknown'][$sdType][] = $tag;
}
}
}