iobroker.assistant 1.4.8 → 1.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/build/lib/piAgent.js +2 -0
- package/build/lib/piAgent.js.map +1 -1
- package/build/lib/timers.js +7 -0
- package/build/lib/timers.js.map +1 -1
- package/build/lib/voice/esphome.js +52 -24
- package/build/lib/voice/esphome.js.map +1 -1
- package/build/lib/voice/esphomeEntities.js +10 -0
- package/build/lib/voice/esphomeEntities.js.map +1 -1
- package/build/lib/voice/esphomeProto.js.map +1 -1
- package/build/lib/voice/mediaServer.js +69 -0
- package/build/lib/voice/mediaServer.js.map +1 -1
- package/build/lib/voice/mediaStream.js +264 -0
- package/build/lib/voice/mediaStream.js.map +1 -0
- package/build/main.js +247 -16
- package/build/main.js.map +1 -1
- package/io-package.json +27 -27
- package/package.json +1 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.STREAM_EXTENSIONS = exports.STREAM_CONTENT_TYPES = void 0;
|
|
4
|
+
exports.kindFromContentType = kindFromContentType;
|
|
5
|
+
exports.kindFromUrl = kindFromUrl;
|
|
6
|
+
exports.parsePlaylist = parsePlaylist;
|
|
7
|
+
exports.probeStream = probeStream;
|
|
8
|
+
exports.deviceCodecs = deviceCodecs;
|
|
9
|
+
exports.transcodeTarget = transcodeTarget;
|
|
10
|
+
exports.transcodeArgs = transcodeArgs;
|
|
11
|
+
exports.planStream = planStream;
|
|
12
|
+
const DEVICE_CODECS = ['flac', 'mp3', 'opus', 'wav'];
|
|
13
|
+
/** What every ESPHome speaker media player decoded before the codecs followed the pipelines. */
|
|
14
|
+
const CLASSIC_CODECS = ['mp3', 'flac', 'wav'];
|
|
15
|
+
/** Bytes of a playlist we read; a real one is a few hundred. */
|
|
16
|
+
const MAX_PLAYLIST_BYTES = 64 * 1024;
|
|
17
|
+
/** Playlists pointing at playlists: how deep we follow. */
|
|
18
|
+
const MAX_DEPTH = 3;
|
|
19
|
+
/**
|
|
20
|
+
* The kind of stream a Content-Type announces; 'unknown' when it says nothing useful (missing,
|
|
21
|
+
* `application/octet-stream`, `text/html` of a server that does not care).
|
|
22
|
+
*/
|
|
23
|
+
function kindFromContentType(contentType) {
|
|
24
|
+
const [type, ...params] = contentType.toLowerCase().split(';');
|
|
25
|
+
const t = type.trim();
|
|
26
|
+
if (/^audio\/(mpeg|mp3|mpeg3|x-mpeg|x-mp3)$/.test(t)) {
|
|
27
|
+
return 'mp3';
|
|
28
|
+
}
|
|
29
|
+
if (/^audio\/(x-)?flac$/.test(t)) {
|
|
30
|
+
return 'flac';
|
|
31
|
+
}
|
|
32
|
+
if (/^audio\/((x-)?wav|wave|vnd\.wave)$/.test(t)) {
|
|
33
|
+
return 'wav';
|
|
34
|
+
}
|
|
35
|
+
// ESPHome takes Ogg only with Opus in it; plain audio/ogg is almost always Vorbis
|
|
36
|
+
if (t === 'audio/opus' || (t === 'audio/ogg' && params.join(';').includes('opus'))) {
|
|
37
|
+
return 'opus';
|
|
38
|
+
}
|
|
39
|
+
// m3u and HLS share their types; the body tells them apart
|
|
40
|
+
if (/^(application|audio)\/(vnd\.apple\.mpegurl|x-mpegurl|mpegurl)$/.test(t)) {
|
|
41
|
+
return 'playlist';
|
|
42
|
+
}
|
|
43
|
+
if (t === 'audio/x-scpls' || t === 'application/pls+xml') {
|
|
44
|
+
return 'playlist';
|
|
45
|
+
}
|
|
46
|
+
if (/^(audio|video)\//.test(t)) {
|
|
47
|
+
return 'other'; // AAC, Vorbis, MP4 …
|
|
48
|
+
}
|
|
49
|
+
return 'unknown';
|
|
50
|
+
}
|
|
51
|
+
/** The kind of stream the file extension of a URL suggests. */
|
|
52
|
+
function kindFromUrl(url) {
|
|
53
|
+
let path;
|
|
54
|
+
try {
|
|
55
|
+
path = new URL(url).pathname.toLowerCase();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return 'unknown';
|
|
59
|
+
}
|
|
60
|
+
const ext = /\.([a-z0-9]+)$/.exec(path)?.[1] || '';
|
|
61
|
+
const byExt = {
|
|
62
|
+
mp3: 'mp3',
|
|
63
|
+
flac: 'flac',
|
|
64
|
+
wav: 'wav',
|
|
65
|
+
opus: 'opus',
|
|
66
|
+
m3u8: 'hls',
|
|
67
|
+
m3u: 'playlist',
|
|
68
|
+
pls: 'playlist',
|
|
69
|
+
aac: 'other',
|
|
70
|
+
m4a: 'other',
|
|
71
|
+
mp4: 'other',
|
|
72
|
+
ogg: 'other',
|
|
73
|
+
oga: 'other',
|
|
74
|
+
wma: 'other',
|
|
75
|
+
};
|
|
76
|
+
return byExt[ext] || 'unknown';
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Read a playlist: HLS (`#EXT-X-…`) is a stream in itself and stays as it is; a `.pls` or plain `.m3u` gives its
|
|
80
|
+
* first http(s) entry, resolved against the playlist's own URL. `url` is '' when there is none.
|
|
81
|
+
*/
|
|
82
|
+
function parsePlaylist(text, base) {
|
|
83
|
+
if (/^#EXT-X-/m.test(text)) {
|
|
84
|
+
return { hls: true, url: base };
|
|
85
|
+
}
|
|
86
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
87
|
+
const line = raw.trim();
|
|
88
|
+
const entry = /^File\d+\s*=\s*(.+)$/i.exec(line)?.[1] ??
|
|
89
|
+
(line && !line.startsWith('#') && !line.startsWith('[') ? line : '');
|
|
90
|
+
if (!entry || /^[a-z]+\s*=/i.test(entry)) {
|
|
91
|
+
continue; // "NumberOfEntries=1", "Title1=…"
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const url = new URL(entry.trim(), base);
|
|
95
|
+
if (url.protocol === 'http:' || url.protocol === 'https:') {
|
|
96
|
+
return { hls: false, url: url.href };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
/* not a URL */
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { hls: false, url: '' };
|
|
104
|
+
}
|
|
105
|
+
/** Read at most `max` bytes of a response body, then let go of it. */
|
|
106
|
+
async function readSome(res, max) {
|
|
107
|
+
const reader = res.body?.getReader();
|
|
108
|
+
if (!reader) {
|
|
109
|
+
return '';
|
|
110
|
+
}
|
|
111
|
+
const chunks = [];
|
|
112
|
+
let size = 0;
|
|
113
|
+
try {
|
|
114
|
+
while (size < max) {
|
|
115
|
+
const { done, value } = await reader.read();
|
|
116
|
+
if (done || !value) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
chunks.push(value);
|
|
120
|
+
size += value.length;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
reader.cancel().catch(() => { });
|
|
125
|
+
}
|
|
126
|
+
return Buffer.concat(chunks).subarray(0, max).toString('utf8');
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Find out what `url` is: one GET per step for the headers — the body is read only for a playlist, a live
|
|
130
|
+
* stream is dropped right after its headers. Playlists are followed to their first stream.
|
|
131
|
+
*/
|
|
132
|
+
async function probeStream(url, opts = {}) {
|
|
133
|
+
const doFetch = opts.fetch ?? fetch;
|
|
134
|
+
const timeoutMs = opts.timeoutMs ?? 5000;
|
|
135
|
+
let current = url;
|
|
136
|
+
for (let depth = 0; depth < MAX_DEPTH; depth++) {
|
|
137
|
+
const controller = new AbortController();
|
|
138
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
139
|
+
try {
|
|
140
|
+
const res = await doFetch(current, {
|
|
141
|
+
signal: controller.signal,
|
|
142
|
+
redirect: 'follow',
|
|
143
|
+
headers: { 'User-Agent': 'ioBroker.assistant', 'Icy-MetaData': '0' },
|
|
144
|
+
});
|
|
145
|
+
const contentType = res.headers.get('content-type') || '';
|
|
146
|
+
const final = res.url || current;
|
|
147
|
+
if (!res.ok) {
|
|
148
|
+
controller.abort();
|
|
149
|
+
return { url: current, kind: kindFromUrl(final), contentType, error: `HTTP ${res.status}` };
|
|
150
|
+
}
|
|
151
|
+
let kind = kindFromContentType(contentType);
|
|
152
|
+
if (kind === 'unknown' || (kind === 'other' && kindFromUrl(final) === 'hls')) {
|
|
153
|
+
kind = kindFromUrl(final);
|
|
154
|
+
}
|
|
155
|
+
if (kind !== 'playlist' && kind !== 'hls') {
|
|
156
|
+
controller.abort(); // a live stream would never end
|
|
157
|
+
return { url: current, kind, contentType };
|
|
158
|
+
}
|
|
159
|
+
const list = parsePlaylist(await readSome(res, MAX_PLAYLIST_BYTES), final);
|
|
160
|
+
if (list.hls) {
|
|
161
|
+
return { url: current, kind: 'hls', contentType };
|
|
162
|
+
}
|
|
163
|
+
if (!list.url) {
|
|
164
|
+
return { url: current, kind: 'playlist', contentType, error: 'empty playlist' };
|
|
165
|
+
}
|
|
166
|
+
current = list.url;
|
|
167
|
+
}
|
|
168
|
+
catch (e) {
|
|
169
|
+
const err = e;
|
|
170
|
+
const error = controller.signal.aborted ? 'timeout' : err.cause?.message || err.message;
|
|
171
|
+
return { url: current, kind: kindFromUrl(current), contentType: '', error };
|
|
172
|
+
}
|
|
173
|
+
finally {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { url: current, kind: 'playlist', contentType: '', error: 'playlists nested too deep' };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The codecs the device decodes, from its media players' `supported_formats`: the listed ones; all classic ones
|
|
181
|
+
* when it lists nothing (older firmware, built with every decoder) or a pipeline takes `NONE` (no preference — the
|
|
182
|
+
* firmware then has to be built with every decoder).
|
|
183
|
+
*/
|
|
184
|
+
function deviceCodecs(formats) {
|
|
185
|
+
const listed = formats.map(f => f.format.trim().toLowerCase());
|
|
186
|
+
if (!listed.length || listed.includes('none')) {
|
|
187
|
+
return new Set(CLASSIC_CODECS);
|
|
188
|
+
}
|
|
189
|
+
return new Set(DEVICE_CODECS.filter(c => listed.includes(c)));
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The format a transcoded stream gets: FLAC if the device decodes it (lossless, cheap to write), else MP3, Opus,
|
|
193
|
+
* WAV. Rate and channels from the device's entry for that codec, music (default purpose) before announcements;
|
|
194
|
+
* without a preference 48 kHz mono like the spoken replies.
|
|
195
|
+
*/
|
|
196
|
+
function transcodeTarget(formats) {
|
|
197
|
+
const codecs = deviceCodecs(formats);
|
|
198
|
+
const codec = DEVICE_CODECS.find(c => codecs.has(c)) || 'flac';
|
|
199
|
+
const entry = [
|
|
200
|
+
...formats.filter(f => f.purpose === 'default'),
|
|
201
|
+
...formats.filter(f => f.purpose !== 'default'),
|
|
202
|
+
].find(f => f.format.trim().toLowerCase() === codec);
|
|
203
|
+
let sampleRate = entry?.sampleRate || 48000;
|
|
204
|
+
// libopus runs at 48/24/16/12/8 kHz only
|
|
205
|
+
if (codec === 'opus' && ![48000, 24000, 16000, 12000, 8000].includes(sampleRate)) {
|
|
206
|
+
sampleRate = 48000;
|
|
207
|
+
}
|
|
208
|
+
return { codec, sampleRate, channels: entry?.channels === 2 ? 2 : 1 };
|
|
209
|
+
}
|
|
210
|
+
/** Content-Type of a transcoded stream; ESPHome goes by it (or the extension). */
|
|
211
|
+
exports.STREAM_CONTENT_TYPES = {
|
|
212
|
+
flac: 'audio/flac',
|
|
213
|
+
mp3: 'audio/mpeg',
|
|
214
|
+
opus: 'audio/ogg; codecs=opus',
|
|
215
|
+
wav: 'audio/wav',
|
|
216
|
+
};
|
|
217
|
+
/** File extension of a transcoded stream's URL. */
|
|
218
|
+
exports.STREAM_EXTENSIONS = { flac: 'flac', mp3: 'mp3', opus: 'opus', wav: 'wav' };
|
|
219
|
+
/** ffmpeg arguments that read `url` and write `target` to stdout, for as long as the source sends. */
|
|
220
|
+
function transcodeArgs(url, target, hls) {
|
|
221
|
+
// A dropped radio connection comes back by itself instead of ending the music; HLS reloads its playlist anyway.
|
|
222
|
+
const reconnect = hls ? [] : ['-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5'];
|
|
223
|
+
const codec = {
|
|
224
|
+
flac: ['-c:a', 'flac', '-f', 'flac'],
|
|
225
|
+
mp3: ['-c:a', 'libmp3lame', '-b:a', '192k', '-f', 'mp3'],
|
|
226
|
+
opus: ['-c:a', 'libopus', '-b:a', '128k', '-f', 'ogg'],
|
|
227
|
+
wav: ['-c:a', 'pcm_s16le', '-f', 'wav'],
|
|
228
|
+
};
|
|
229
|
+
// prettier-ignore
|
|
230
|
+
return [
|
|
231
|
+
'-hide_banner', '-loglevel', 'error', '-nostdin',
|
|
232
|
+
...reconnect,
|
|
233
|
+
'-i', url,
|
|
234
|
+
'-map', '0:a:0', '-vn', '-map_metadata', '-1',
|
|
235
|
+
'-ar', String(target.sampleRate), '-ac', String(target.channels), '-sample_fmt', 's16',
|
|
236
|
+
...codec[target.codec],
|
|
237
|
+
'-',
|
|
238
|
+
];
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Direct or through ffmpeg?
|
|
242
|
+
*
|
|
243
|
+
* @param probe what the URL is
|
|
244
|
+
* @param codecs what the device decodes ({@link deviceCodecs})
|
|
245
|
+
* @param always the player's `transcode` switch: through ffmpeg whatever the stream is
|
|
246
|
+
* @param ffmpeg whether ffmpeg is there; without it everything is played directly — maybe the device manages
|
|
247
|
+
*/
|
|
248
|
+
function planStream(probe, codecs, always, ffmpeg) {
|
|
249
|
+
const detail = [probe.contentType.split(';')[0].trim(), probe.error].filter(Boolean).join(', ');
|
|
250
|
+
const what = `${probe.kind === 'unknown' ? 'unknown format' : probe.kind}${detail ? ` (${detail})` : ''}`;
|
|
251
|
+
const decodable = DEVICE_CODECS.includes(probe.kind) && codecs.has(probe.kind);
|
|
252
|
+
let mode = always || !decodable ? 'transcode' : 'direct';
|
|
253
|
+
let reason = always
|
|
254
|
+
? `${what}, transcoding is switched on`
|
|
255
|
+
: decodable
|
|
256
|
+
? `${what}, the device decodes it`
|
|
257
|
+
: `${what}, the device decodes ${[...codecs].join('/') || 'nothing it lists'}`;
|
|
258
|
+
if (mode === 'transcode' && !ffmpeg) {
|
|
259
|
+
mode = 'direct';
|
|
260
|
+
reason += ' — but ffmpeg is missing, so the device gets it as it is';
|
|
261
|
+
}
|
|
262
|
+
return { mode, url: probe.url, kind: probe.kind, reason };
|
|
263
|
+
}
|
|
264
|
+
//# sourceMappingURL=mediaStream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediaStream.js","sourceRoot":"","sources":["../../../src/lib/voice/mediaStream.ts"],"names":[],"mappings":";;;AAsCA,kDA2BC;AAGD,kCAwBC;AAMD,sCAsBC;AA6CD,kCA4CC;AAOD,oCAMC;AAcD,0CAaC;AAcD,sCAmBC;AAoBD,gCAeC;AAtSD,MAAM,aAAa,GAAkB,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;AAEpE,gGAAgG;AAChG,MAAM,cAAc,GAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;AAE7D,gEAAgE;AAChE,MAAM,kBAAkB,GAAG,EAAE,GAAG,IAAI,CAAC;AAErC,2DAA2D;AAC3D,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB;;;GAGG;AACH,SAAgB,mBAAmB,CAAC,WAAmB;IACnD,MAAM,CAAC,IAAI,EAAE,GAAG,MAAM,CAAC,GAAG,WAAW,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC/D,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;IACtB,IAAI,wCAAwC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QACnD,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/B,OAAO,MAAM,CAAC;IAClB,CAAC;IACD,IAAI,oCAAoC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC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;IACzE,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC;AAC9D,CAAC","sourcesContent":["/**\n * Music from a URL on an ESPHome satellite's media player: internet radio, a file on a web server.\n *\n * The device plays a URL itself (`MediaPlayerCommandRequest.media_url` with `announcement = false`: the media\n * pipeline, so replies and announcements only duck it, and play/pause/stop/volume apply to it). It decodes only\n * the codecs its firmware was built with, though: since ESPHome 2026.10 the media player gets the decoders of the\n * formats its pipelines announce (`supported_formats`), all of MP3/FLAC/WAV only with format `NONE` or on older\n * firmware that announces nothing. Many stations send AAC or HLS (`.m3u8`), and radio directories hand out\n * playlists (`.pls`, `.m3u`) that no device plays.\n *\n * So before a URL goes to the device, {@link planStream} looks at what it really is: one request for the headers\n * (and the body of a playlist), the first stream of a playlist instead of the playlist, then the codec — from the\n * Content-Type, else the extension. A codec the device decodes is played directly; anything else runs through\n * ffmpeg and is served by our media server in a codec the device takes, FLAC first (what Home Assistant does).\n */\nimport type { MediaFormat } from './replyFormat';\n\n/** What a URL turned out to be. */\nexport type StreamKind = 'mp3' | 'flac' | 'wav' | 'opus' | 'hls' | 'playlist' | 'other' | 'unknown';\n\n/** Codecs an ESPHome media player can decode (`audio_decoder`). */\nexport type DeviceCodec = 'mp3' | 'flac' | 'wav' | 'opus';\n\nconst DEVICE_CODECS: DeviceCodec[] = ['flac', 'mp3', 'opus', 'wav'];\n\n/** What every ESPHome speaker media player decoded before the codecs followed the pipelines. */\nconst CLASSIC_CODECS: DeviceCodec[] = ['mp3', 'flac', 'wav'];\n\n/** Bytes of a playlist we read; a real one is a few hundred. */\nconst MAX_PLAYLIST_BYTES = 64 * 1024;\n\n/** Playlists pointing at playlists: how deep we follow. */\nconst MAX_DEPTH = 3;\n\n/**\n * The kind of stream a Content-Type announces; 'unknown' when it says nothing useful (missing,\n * `application/octet-stream`, `text/html` of a server that does not care).\n */\nexport function kindFromContentType(contentType: string): StreamKind {\n const [type, ...params] = contentType.toLowerCase().split(';');\n const t = type.trim();\n if (/^audio\\/(mpeg|mp3|mpeg3|x-mpeg|x-mp3)$/.test(t)) {\n return 'mp3';\n }\n if (/^audio\\/(x-)?flac$/.test(t)) {\n return 'flac';\n }\n if (/^audio\\/((x-)?wav|wave|vnd\\.wave)$/.test(t)) {\n return 'wav';\n }\n // ESPHome takes Ogg only with Opus in it; plain audio/ogg is almost always Vorbis\n if (t === 'audio/opus' || (t === 'audio/ogg' && params.join(';').includes('opus'))) {\n return 'opus';\n }\n // m3u and HLS share their types; the body tells them apart\n if (/^(application|audio)\\/(vnd\\.apple\\.mpegurl|x-mpegurl|mpegurl)$/.test(t)) {\n return 'playlist';\n }\n if (t === 'audio/x-scpls' || t === 'application/pls+xml') {\n return 'playlist';\n }\n if (/^(audio|video)\\//.test(t)) {\n return 'other'; // AAC, Vorbis, MP4 …\n }\n return 'unknown';\n}\n\n/** The kind of stream the file extension of a URL suggests. */\nexport function kindFromUrl(url: string): StreamKind {\n let path: string;\n try {\n path = new URL(url).pathname.toLowerCase();\n } catch {\n return 'unknown';\n }\n const ext = /\\.([a-z0-9]+)$/.exec(path)?.[1] || '';\n const byExt: Record<string, StreamKind> = {\n mp3: 'mp3',\n flac: 'flac',\n wav: 'wav',\n opus: 'opus',\n m3u8: 'hls',\n m3u: 'playlist',\n pls: 'playlist',\n aac: 'other',\n m4a: 'other',\n mp4: 'other',\n ogg: 'other',\n oga: 'other',\n wma: 'other',\n };\n return byExt[ext] || 'unknown';\n}\n\n/**\n * Read a playlist: HLS (`#EXT-X-…`) is a stream in itself and stays as it is; a `.pls` or plain `.m3u` gives its\n * first http(s) entry, resolved against the playlist's own URL. `url` is '' when there is none.\n */\nexport function parsePlaylist(text: string, base: string): { hls: boolean; url: string } {\n if (/^#EXT-X-/m.test(text)) {\n return { hls: true, url: base };\n }\n for (const raw of text.split(/\\r?\\n/)) {\n const line = raw.trim();\n const entry =\n /^File\\d+\\s*=\\s*(.+)$/i.exec(line)?.[1] ??\n (line && !line.startsWith('#') && !line.startsWith('[') ? line : '');\n if (!entry || /^[a-z]+\\s*=/i.test(entry)) {\n continue; // \"NumberOfEntries=1\", \"Title1=…\"\n }\n try {\n const url = new URL(entry.trim(), base);\n if (url.protocol === 'http:' || url.protocol === 'https:') {\n return { hls: false, url: url.href };\n }\n } catch {\n /* not a URL */\n }\n }\n return { hls: false, url: '' };\n}\n\n/** What a URL turned out to be after following its playlists. */\nexport interface StreamProbe {\n /** The stream itself (a playlist's entry) — what the device or ffmpeg should open. */\n url: string;\n kind: StreamKind;\n contentType: string;\n /** Why the probe could not look (network error, a server that does not speak HTTP/1.1 like old SHOUTcast). */\n error?: string;\n}\n\nexport interface ProbeOptions {\n /** Per request. */\n timeoutMs?: number;\n fetch?: typeof fetch;\n}\n\n/** Read at most `max` bytes of a response body, then let go of it. */\nasync function readSome(res: Response, max: number): Promise<string> {\n const reader = res.body?.getReader();\n if (!reader) {\n return '';\n }\n const chunks: Uint8Array[] = [];\n let size = 0;\n try {\n while (size < max) {\n const { done, value } = await reader.read();\n if (done || !value) {\n break;\n }\n chunks.push(value);\n size += value.length;\n }\n } finally {\n reader.cancel().catch(() => {});\n }\n return Buffer.concat(chunks).subarray(0, max).toString('utf8');\n}\n\n/**\n * Find out what `url` is: one GET per step for the headers — the body is read only for a playlist, a live\n * stream is dropped right after its headers. Playlists are followed to their first stream.\n */\nexport async function probeStream(url: string, opts: ProbeOptions = {}): Promise<StreamProbe> {\n const doFetch = opts.fetch ?? fetch;\n const timeoutMs = opts.timeoutMs ?? 5000;\n let current = url;\n for (let depth = 0; depth < MAX_DEPTH; depth++) {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n try {\n const res = await doFetch(current, {\n signal: controller.signal,\n redirect: 'follow',\n headers: { 'User-Agent': 'ioBroker.assistant', 'Icy-MetaData': '0' },\n });\n const contentType = res.headers.get('content-type') || '';\n const final = res.url || current;\n if (!res.ok) {\n controller.abort();\n return { url: current, kind: kindFromUrl(final), contentType, error: `HTTP ${res.status}` };\n }\n let kind = kindFromContentType(contentType);\n if (kind === 'unknown' || (kind === 'other' && kindFromUrl(final) === 'hls')) {\n kind = kindFromUrl(final);\n }\n if (kind !== 'playlist' && kind !== 'hls') {\n controller.abort(); // a live stream would never end\n return { url: current, kind, contentType };\n }\n const list = parsePlaylist(await readSome(res, MAX_PLAYLIST_BYTES), final);\n if (list.hls) {\n return { url: current, kind: 'hls', contentType };\n }\n if (!list.url) {\n return { url: current, kind: 'playlist', contentType, error: 'empty playlist' };\n }\n current = list.url;\n } catch (e) {\n const err = e as Error & { cause?: { message?: string } };\n const error = controller.signal.aborted ? 'timeout' : err.cause?.message || err.message;\n return { url: current, kind: kindFromUrl(current), contentType: '', error };\n } finally {\n clearTimeout(timer);\n }\n }\n return { url: current, kind: 'playlist', contentType: '', error: 'playlists nested too deep' };\n}\n\n/**\n * The codecs the device decodes, from its media players' `supported_formats`: the listed ones; all classic ones\n * when it lists nothing (older firmware, built with every decoder) or a pipeline takes `NONE` (no preference — the\n * firmware then has to be built with every decoder).\n */\nexport function deviceCodecs(formats: MediaFormat[]): Set<DeviceCodec> {\n const listed = formats.map(f => f.format.trim().toLowerCase());\n if (!listed.length || listed.includes('none')) {\n return new Set(CLASSIC_CODECS);\n }\n return new Set(DEVICE_CODECS.filter(c => listed.includes(c)));\n}\n\n/** What ffmpeg writes for the device. */\nexport interface TranscodeTarget {\n codec: DeviceCodec;\n sampleRate: number;\n channels: 1 | 2;\n}\n\n/**\n * The format a transcoded stream gets: FLAC if the device decodes it (lossless, cheap to write), else MP3, Opus,\n * WAV. Rate and channels from the device's entry for that codec, music (default purpose) before announcements;\n * without a preference 48 kHz mono like the spoken replies.\n */\nexport function transcodeTarget(formats: MediaFormat[]): TranscodeTarget {\n const codecs = deviceCodecs(formats);\n const codec = DEVICE_CODECS.find(c => codecs.has(c)) || 'flac';\n const entry = [\n ...formats.filter(f => f.purpose === 'default'),\n ...formats.filter(f => f.purpose !== 'default'),\n ].find(f => f.format.trim().toLowerCase() === codec);\n let sampleRate = entry?.sampleRate || 48000;\n // libopus runs at 48/24/16/12/8 kHz only\n if (codec === 'opus' && ![48000, 24000, 16000, 12000, 8000].includes(sampleRate)) {\n sampleRate = 48000;\n }\n return { codec, sampleRate, channels: entry?.channels === 2 ? 2 : 1 };\n}\n\n/** Content-Type of a transcoded stream; ESPHome goes by it (or the extension). */\nexport const STREAM_CONTENT_TYPES: Record<DeviceCodec, string> = {\n flac: 'audio/flac',\n mp3: 'audio/mpeg',\n opus: 'audio/ogg; codecs=opus',\n wav: 'audio/wav',\n};\n\n/** File extension of a transcoded stream's URL. */\nexport const STREAM_EXTENSIONS: Record<DeviceCodec, string> = { flac: 'flac', mp3: 'mp3', opus: 'opus', wav: 'wav' };\n\n/** ffmpeg arguments that read `url` and write `target` to stdout, for as long as the source sends. */\nexport function transcodeArgs(url: string, target: TranscodeTarget, hls: boolean): string[] {\n // A dropped radio connection comes back by itself instead of ending the music; HLS reloads its playlist anyway.\n const reconnect = hls ? [] : ['-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5'];\n const codec: Record<DeviceCodec, string[]> = {\n flac: ['-c:a', 'flac', '-f', 'flac'],\n mp3: ['-c:a', 'libmp3lame', '-b:a', '192k', '-f', 'mp3'],\n opus: ['-c:a', 'libopus', '-b:a', '128k', '-f', 'ogg'],\n wav: ['-c:a', 'pcm_s16le', '-f', 'wav'],\n };\n // prettier-ignore\n return [\n '-hide_banner', '-loglevel', 'error', '-nostdin',\n ...reconnect,\n '-i', url,\n '-map', '0:a:0', '-vn', '-map_metadata', '-1',\n '-ar', String(target.sampleRate), '-ac', String(target.channels), '-sample_fmt', 's16',\n ...codec[target.codec],\n '-',\n ];\n}\n\n/** How a URL is going to be played. */\nexport interface StreamPlan {\n mode: 'direct' | 'transcode';\n /** The stream itself (a playlist resolved): what the device or ffmpeg opens. */\n url: string;\n kind: StreamKind;\n /** For the log: why this way. */\n reason: string;\n}\n\n/**\n * Direct or through ffmpeg?\n *\n * @param probe what the URL is\n * @param codecs what the device decodes ({@link deviceCodecs})\n * @param always the player's `transcode` switch: through ffmpeg whatever the stream is\n * @param ffmpeg whether ffmpeg is there; without it everything is played directly — maybe the device manages\n */\nexport function planStream(probe: StreamProbe, codecs: Set<DeviceCodec>, always: boolean, ffmpeg: boolean): StreamPlan {\n const detail = [probe.contentType.split(';')[0].trim(), probe.error].filter(Boolean).join(', ');\n const what = `${probe.kind === 'unknown' ? 'unknown format' : probe.kind}${detail ? ` (${detail})` : ''}`;\n const decodable = (DEVICE_CODECS as StreamKind[]).includes(probe.kind) && codecs.has(probe.kind as DeviceCodec);\n let mode: StreamPlan['mode'] = always || !decodable ? 'transcode' : 'direct';\n let reason = always\n ? `${what}, transcoding is switched on`\n : decodable\n ? `${what}, the device decodes it`\n : `${what}, the device decodes ${[...codecs].join('/') || 'nothing it lists'}`;\n if (mode === 'transcode' && !ffmpeg) {\n mode = 'direct';\n reason += ' — but ffmpeg is missing, so the device gets it as it is';\n }\n return { mode, url: probe.url, kind: probe.kind, reason };\n}\n"]}
|