-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.js
More file actions
144 lines (112 loc) · 4.8 KB
/
parser.js
File metadata and controls
144 lines (112 loc) · 4.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
function fetchStreamDatetime(options) {
var opts = $.extend({
'manifestUrl': null,
'callback': null,
'fragmentLength': 2,
'pollingDelay': 10000
}, options);
var state = {
'index': 0,
'datetime': 0
};
window.console && console.debug("Manifest url: " + opts.manifestUrl);
$.ajax({
url: opts.manifestUrl,
success: processManifest,
error: ajaxError
});
function processManifest(data, textStatus, jqXHR) {
window.console && console.log("Loaded manifest file");
indexOfSlash = opts.manifestUrl.lastIndexOf('/') + 1;
audioFile = data.match(/^[a-z]+-audio=\d*\.m3u8/m);
var url = opts.manifestUrl.substring(0, indexOfSlash) + audioFile;
window.console && console.debug("Audio file url: " + url);
audioUrl = url;
fetchInitialAudio(url);
}
function fetchInitialAudio(url) {
$.ajax({
url: url,
success: processAudio
});
window.setTimeout(fetchAudioFragment, opts.pollingDelay, [url]);
}
function fetchAudioFragment(url) {
rangeHeader = 'bytes=' + state.index + '-' + (state.index + 50000);
window.console && console.debug('Range Header Value: ' + rangeHeader);
$.ajax({
url: url,
beforeSend: function(xhr) {
xhr.setRequestHeader('Range', rangeHeader);
},
success: processAudioFragment,
error: ajaxError
});
window.setTimeout(fetchAudioFragment, opts.pollingDelay, [url]);
}
function interimEvents() {
var currentDatetime = state.datetime;
var loops = opts.pollingDelay / 1000;
for (var i = 0; i < loops; i++) {
window.setTimeout(notify, 1000 * i, [currentDatetime + (1000 * i)]);
}
}
function processAudio(data, textStatus, jqXHR) {
var start = performance.now();
window.console && console.log("Loaded audio file");
// Parse the datestamp
var datestamps = data.match(/#EXT-X-PROGRAM-DATE-TIME:\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{6})?Z/gm);
var datestamp = datestamps[datestamps.length - 1];
window.console && console.debug(datestamps.length + ' datestamps found');
window.console && console.debug('The last datestamp is "' + datestamp + '"');
state.datetime = datetimeParser(datestamp);
// Get rid of the uneeded part of the stream
var datestampIndex = data.indexOf(datestamp);
window.console && console.debug('The datestamp index is ' + datestampIndex);
var cleanedData = data.substring(datestampIndex, data.length);
// Handle stream fragments
processAudioFragment(cleanedData, data.length);
// Cleanup
var executionTime = performance.now() - start;
window.console && console.debug('All Audio data parsed in: ' + executionTime + ' milliseconds');
}
function processAudioFragment(data, originalLength) {
var start = performance.now();
window.console && console.debug('The starting datetime is: ' + state.datetime);
var fragments = data.match(/=\d*-\d*.ts/gm);
var additionalSeconds = fragments.length * opts.fragmentLength;
state.datetime += (additionalSeconds * 1000);
window.console && console.debug('Adding ' + additionalSeconds + ' seconds');
window.console && console.info('Final Date is: ' + state.datetime);
if (typeof(originalLength) == "number")
state.index = originalLength;
else
state.index += data.length;
window.console && console.debug('Current index: ' + state.index);
notify(state.datetime);
interimEvents();
var end = performance.now() - start;
window.console && console.debug('Audio fragment data parsed in ' + end + ' milliseconds');
}
function datetimeParser(datestamp) {
datestamp = datestamp.replace('#EXT-X-PROGRAM-DATE-TIME:', '');
var year = datestamp.substring(0, 4);
var month = datestamp.substring(5, 7) - 1;
var day = datestamp.substring(8, 10);
var hours = datestamp.substring(11, 13);
var minutes = datestamp.substring(14, 16);
var seconds = datestamp.substring(17, 19);
var milliseconds = datestamp.substring(20, 23);
datestamp = Date.UTC(year, month, day, hours, minutes, seconds, milliseconds);
return datestamp;
}
function notify(datetime) {
datetime = new Date(parseInt(datetime));
$(document).trigger("streamDatetime", [datetime]);
if(typeof(opts.callback) == "function")
opts.callback(datetime);
}
function ajaxError(jqXHR, textStatus, errorThrown) {
window.console && console.error('Failed to load ' + this.url);
}
}