forked from mafintosh/hls-buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (132 loc) · 3.68 KB
/
index.js
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
var stream = require('stream-wrapper');
var ForeverAgent = require('forever-agent');
var request = require('request');
var once = require('once');
var crypto = require('crypto');
var thunky = require('thunky');
var resolveUrl = require('url').resolve;
var noop = function() {};
var md5 = function(str) {
return crypto.createHash('md5').update(str).digest('hex');
};
module.exports = function(url, opts) {
if (!opts) opts = {};
var queued = [];
var that = {};
var max = opts.max || 10; // default max ~10 in queue
var onplaylist;
var agent = undefined;
if (opts.agent !== false) agent = /^https/.test(url) ? new ForeverAgent.SSL() : new ForeverAgent();
var readPlaylist = function(playlistUrl, callback) {
var ts = [];
request(playlistUrl, {agent:agent}, function(err, response) {
if (response.statusCode !== 200) return callback();
var body = response.body.toString().trim();
body = body.split('\n').map(function(line) {
if (line[0] === '#') return line;
var url = resolveUrl(playlistUrl, line);
var id = '/'+md5(line)+'.ts';
ts.push({url:url, id:id});
return id;
}).join('\n')+'\n';
callback(null, ts, body);
});
};
var preload = function() {
if (that.destroyed) return;
var fetching = queued.some(function(q) {
return q.source && !q.finished;
});
if (fetching) return;
fetching = queued.some(function(q, i) {
if (q.finished || q.source) return;
if (i > max) return true;
var req = request(q.url, {agent:agent});
var onclose = once(function() {
q.finished = true;
q.streams.forEach(function(s) {
s.push(null);
});
preload();
});
req.on('data', function(buf) {
q.buffers.push(buf);
q.streams.forEach(function(s) {
s.push(buf);
});
});
req.on('end', onclose);
req.on('close', onclose);
q.source = req;
return true;
});
if (fetching) return;
if (queued.length >= max) return;
that.playlist(); // refetch the playlist so we can preload some more stuff...
};
that.destroyed = false;
that.playlist = function(callback) {
if (!callback) callback = noop;
if (onplaylist) return onplaylist(callback);
onplaylist = thunky(function(callback) {
var retries = 0;
var retry = function() {
if (that.destroyed) return callback(new Error('destroyed'));
readPlaylist(url, function(err, ts, playlist) {
if (!ts) return callback(new Error('could not fetch playlist'));
if (err && retries < 5) {
retries++;
return setTimeout(retry, 5000);
}
if (err) return callback(err); // retry instead
ts.forEach(function(item) {
var exists = queued.some(function(q) {
return q.url === item.url;
});
item.buffers = [];
item.streams = [];
if (!exists) queued.push(item);
});
preload();
callback(null, playlist);
process.nextTick(function() { // maybe wait a bit longer to bust the old playlist?
onplaylist = null;
});
});
};
retry();
});
onplaylist(callback);
};
that.segment = function(hex) {
if (that.destroyed) return null;
// gc
while (queued.length) {
var q = queued[0];
if (q.id === hex) break;
queued.shift();
if (q.source && !q.finished) q.source.destroy();
}
if (!queued[0]) return null;
var rs = stream.readable();
var q = queued[0];
preload();
q.streams.push(rs);
q.buffers.forEach(function(b) {
rs.push(b);
});
if (q.finished) rs.push(null);
rs.on('close', function() {
q.streams.splice(q.streams.indexOf(rs), 1);
});
return rs;
};
that.destroy = function() {
that.destroyed = true;
queued.forEach(function(q) {
if (q.source && !q.finished) q.source.destroy();
});
queued = [];
};
return that;
};