-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl.js
325 lines (282 loc) · 10.6 KB
/
dl.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
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
// MIT License
// Copyright (c) 2018 Alexandre Storelli
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
"use strict";
const { Readable } = require('stream');
const { log } = require("abr-log")("dl");
const axios = require('axios');
const cp = require("child_process");
const consts = {
SUPPORTED_EXTENSIONS: [
"mp3",
"aac",
"ogg"
],
CODEC_FFPROBE_TRANSLATION: { // translation from codecs from ffprobe to file extensions
"mp3": "mp3",
"aac": "aac",
"vorbis": "ogg",
},
CODEC_API_TRANSLATION: { // translation from codecs from radio-browser.info to file extensions
"MP3": "mp3",
"AAC": "aac",
"AAC+": "aac",
"OGG": "ogg",
"HLS": "aac"
},
API_PATH: "https://www.adblockradio.com/models/list.json",
API_PATH_ALT: "https://www.radio-browser.info/webservice/json/stations/bynameexact/",
}
// function that calls an API to get metadata about a radio (provider adblockradio.com)
const getRadioMetadata = function(country, name, callback) {
axios.get(consts.API_PATH).then(function(response) {
const result = response.data.find(radio => radio.country === country && radio.name === name);
if (result) {
if (isFinite(result.bitrate) && result.bitrate > 0) {
result.bitrate *= 1000 / 8; // result in kbps. convert to bytes per second
} else {
result.bitrate = 0;
log.warn(country + "_" + name + " getRadioMetadata: no ICY bitrate available");
// we will use ffprobe instead, or read the HLS manifest if relevant
}
return callback(null, result);
} else {
log.warn("getRadioMetadata: will try to find radio " + country + "_" + name + " on alt provider");
return getRadioMetadataAlt(country, name, callback);
}
}).catch(function(e) {
log.warn("getRadioMetadata: error " + e + " will try to find radio " + country + "_" + name + " on alt provider");
return getRadioMetadataAlt(country, name, callback);
});
}
// function that calls an API to get metadata about a radio (provider radio-browser.info)
const getRadioMetadataAlt = function(country, name, callback) {
axios.get(consts.API_PATH_ALT + encodeURIComponent(name)).then(function(response) {
const results = response.data;
const i = results.map(e => e.country).indexOf(country);
if (i >= 0) {
if (isFinite(results[i].bitrate) && results[i].bitrate > 0) {
results[i].bitrate = results[i].bitrate * 1000 / 8; // result in kbps. convert to bytes per second
} else {
results[i].bitrate = 0;
log.warn(country + "_" + name + " getRadioMetadata: no ICY bitrate available");
// we will use ffprobe instead, or read the HLS manifest if relevant
}
return callback(null, results[i]);
} else {
log.error("getRadioMetadata: radio not found: " + results);
return callback(null, null);
}
}).catch(function(e) {
log.error("getRatioMetadata: request error. err=" + e);
return callback(e, null);
});
}
// main Readable class that contains workarounds for most frequent errors
// it parses playlists if necessary (M3U, SCPLS, ASF, M3U8/HLS)
// it ensures the download is still active, and restarts if stalled.
// output events can be emitted in a way to have equal audio chunks
// output also contains the length of the audio buffer in the stream
class StreamDl extends Readable {
constructor(options) {
if (!options) options = {};
options.objectMode = true;
super(options);
this.country = options.country;
this.name = options.name;
this.canonical = this.country + "_" + this.name;
this.segDuration = options.segDuration;
this.buffer = 0;
this.startDl();
}
async refreshMetadata() {
const self = this;
return new Promise(function(resolve, reject) {
try {
getRadioMetadata(self.country, self.name, function(err, result) {
if (err || !result) {
log.warn(self.canonical + " problem fetching radio info: " + err);
self.emit("error", "problem fetching radio info: " + err);
return reject();
}
let translatedCodec = consts.CODEC_API_TRANSLATION[result.codec];
if (!consts.SUPPORTED_EXTENSIONS.includes(translatedCodec)) {
if (result.codec === "UNKNOWN" || result.codec === "") {
log.warn(self.canonical + ": API gives " + result.codec + " codec. Will use ffprobe to determine it.");
translatedCodec = "UNKNOWN";
} else {
log.error(self.canonical + ": API returned a codec, " + result.codec + ", that is not supported");
self.emit("error", "API returned an unsupported codec");
return reject();
}
} else if (!result.codec) {
log.warn(self.canonical + ": API returned an empty codec field");
self.emit("error", "API returned an empty codec")
return reject();
}
if (!self.url) self.url = result.url;
self.origUrl = result.url;
self.ext = translatedCodec;
self.hls = result.codec === "HLS" || result.hls === "1" || result.hls === 1;
self.bitrate = result.bitrate;
self.apiBitrate = result.bitrate;
self.apiresult = result;
resolve();
});
} catch (e) {
log.error(self.canonical + " error getting radio metadata. err=" + e);
reject();
}
});
}
startDl() {
const self = this;
(async function() {
await self.stopDl();
self.firstData = null;
self.lastData = new Date();
self.receivedBytes = 0;
self.receivedBytesInCurrentSegment = 0;
self.checkInterval = setInterval(function() {
if (+new Date() - self.lastData > 10000) {
log.info(self.canonical + " stream seems idle, we restart it");
self.startDl();
}
}, 5000);
try {
await self.refreshMetadata();
} catch (e) {
return;
}
self.worker = cp.fork(__dirname + "/worker.js", { //new Worker(__dirname + "/worker.js", {
//workerData: {
env: {
country: self.country,
name: self.name,
url: self.url,
hls: self.hls,
ext: self.ext,
bitrate: self.bitrate,
consts: JSON.stringify(consts),
}
});
self.worker.on("message", function(msg) {
if (msg.type === "headers") {
log.debug(self.canonical + " will emit headers");
self.emit("headers", msg.headers);
} else if (msg.type === "metadata") {
log.debug(self.canonical + " will emit metadata");
self.emit("metadata", {
country: self.country,
name: self.name,
url: self.url,
favicon: self.apiresult.favicon,
ext: self.ext,
bitrate: self.bitrate,
hls: self.apiresult.hls,
tags: self.apiresult.tags,
votes: self.apiresult.votes,
lastcheckok: self.apiresult.lastcheckok,
homepage: self.apiresult.homepage
});
} else if (msg.type === "data") {
msg.data = Buffer.from(msg.data);
//log.debug(self.canonical + " received " + msg.data.length + " bytes");
self.onData2(msg.data, msg.isFirstSegment);
} else if (msg.type === "bitrate") {
log.info(self.canonical + " bitrate updated to " + msg.bitrate);
self.bitrate = msg.bitrate;
} else if (msg.type === "ext") {
log.info(self.canonical + " ext updated to " + msg.ext);
self.ext = msg.ext;
} else if (msg.type === "url") {
log.info(self.canonical + " url updated to " + msg.url);
self.url = msg.url;
self.startDl(); // immediately restart the request
} else {
log.warn(self.canonical + " message not recognized. type=" + msg.type);
}
});
self.worker.once("error", function(err) {
log.error(self.canonical + " thread had error " + err);
// thread will restart by itself with "checkInterval" function
});
self.worker.exited = false;
self.worker.once("exit", function() {
log.debug(self.canonical + " thread exited");
self.worker = null;
});
})();
}
tBuffer() {
// the bitrate is not perfectly known. we freeze the buffer length after a few seconds so that
// the value does not drift over time
if (this.lastData - this.firstData < 20000) {
this.buffer = this.receivedBytes / this.bitrate - (this.lastData - this.firstData) / 1000;
}
return this.buffer;
}
onData2(data, isFirstSegment) {
if (this.firstData === null) {
this.firstData = new Date();
log.info(this.canonical + " first data received at " + this.firstData);
}
this.lastData = new Date();
let newSegment = isFirstSegment;
const limitBytes = this.segDuration * this.bitrate;
if (!limitBytes || this.receivedBytesInCurrentSegment + data.length < limitBytes) {
this.receivedBytesInCurrentSegment += data.length;
this.receivedBytes += data.length;
this.push({ newSegment: newSegment, tBuffer: this.tBuffer(), data: data });
} else {
// send the correct amount of bytes to fill the current segment
var fillAmount = (limitBytes - this.receivedBytesInCurrentSegment) % limitBytes;
if (fillAmount > 0) {
this.receivedBytes += fillAmount;
this.push({ newSegment: newSegment, tBuffer: this.tBuffer(), data: data.slice(0, fillAmount) });
data = data.slice(fillAmount);
}
this.receivedBytesInCurrentSegment = 0;
// send as many full segments as necessary
var nSegs = Math.floor(data.length / limitBytes);
for (let i=0; i<nSegs; i++) {
this.receivedBytes += limitBytes;
this.push({ newSegment: true, tBuffer: this.tBuffer(), data: data.slice(i*limitBytes, (i+1)*limitBytes) });
}
// send the remaining amount of bytes in a new segment
this.receivedBytesInCurrentSegment = data.length - nSegs * limitBytes;
this.receivedBytes += data.length - nSegs * limitBytes;
this.push({ newSegment: true, tBuffer: this.tBuffer(), data: data.slice(nSegs*limitBytes) });
}
}
async stopDl() {
if (this.checkInterval) {
clearInterval(this.checkInterval); // disable safety nets that restart the dl
}
if (this.worker) {
this.worker.kill();
delete this.worker;
}
}
_read() {
// pass
}
}
exports.StreamDl = StreamDl;
exports.getRadioMetadata = getRadioMetadata;