nixamp 0.56.0 → 0.57.0
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/dist/channels.d.ts +20 -0
- package/dist/channels.js +24 -0
- package/dist/library.d.ts +6 -0
- package/dist/library.js +11 -0
- package/dist/playlist-input.d.ts +32 -7
- package/dist/playlist-input.js +20 -2
- package/dist/server.js +9 -1
- package/dist/station-routes.d.ts +1 -0
- package/dist/station-routes.js +23 -5
- package/dist/station-runner.d.ts +48 -0
- package/dist/station-runner.js +208 -4
- package/dist/stations.d.ts +14 -0
- package/dist/stations.js +11 -0
- package/package.json +1 -1
- package/src/channels.ts +30 -0
- package/src/library.ts +14 -0
- package/src/playlist-input.ts +46 -5
- package/src/server.ts +10 -1
- package/src/station-routes.ts +23 -6
- package/src/station-runner.ts +205 -4
- package/src/stations.ts +15 -0
- package/web/dist/assets/{ar-CwZJb_gN.js → ar-d05d_7Df.js} +1 -1
- package/web/dist/assets/{cs-BQeUHU5U.js → cs-CclrNwa7.js} +1 -1
- package/web/dist/assets/{da-B9gLGX-j.js → da-C30KTGl1.js} +1 -1
- package/web/dist/assets/{de-BtKJqMyc.js → de-D2A2RQwi.js} +1 -1
- package/web/dist/assets/{es-xCZPNIQ8.js → es-BXFx0WZ5.js} +1 -1
- package/web/dist/assets/{fi-Bs6hJxj9.js → fi-DPA3S8og.js} +1 -1
- package/web/dist/assets/{fr-Db3viYEV.js → fr-D_0wnr4h.js} +1 -1
- package/web/dist/assets/{hi-BEO0uXMK.js → hi-EfiCQnb9.js} +1 -1
- package/web/dist/assets/{hu-DddOFPOR.js → hu-CK5rnxKO.js} +1 -1
- package/web/dist/assets/{id-Ca4dDDtC.js → id-BgTV_4UK.js} +1 -1
- package/web/dist/assets/{it-B9cFkgz7.js → it-DGefapXn.js} +1 -1
- package/web/dist/assets/{main-CHubaPAu.js → main-C-tWhTWo.js} +2 -2
- package/web/dist/assets/{nl-kUxjpAwG.js → nl-Cyst29Bp.js} +1 -1
- package/web/dist/assets/{ru-BE9Sxy3t.js → ru-D8G6Wynm.js} +1 -1
- package/web/dist/assets/{station-DxBlD6Yl.js → station-D6sd4DqP.js} +1 -1
- package/web/dist/assets/{sv-Cp6BIRXI.js → sv-aYnDAFwz.js} +1 -1
- package/web/dist/assets/{uk-DavLH9cs.js → uk-BDtew7z3.js} +1 -1
- package/web/dist/assets/{vi-eAlsSX6P.js → vi-CRlDn2Xh.js} +1 -1
- package/web/dist/assets/{zh-D2Y8KIQG.js → zh-BCAIjeQq.js} +1 -1
- package/web/dist/index.html +1 -1
- package/web/dist/station.html +1 -1
- package/web/dist/sw.js +20 -20
package/dist/station-runner.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { EXCLUDE_AFTER, OFFLINE_RETRY_MS, RECOVERY_DELAYS_MS, StationError, channelOf, insideRoots, spreadByArtist, stationOfChannel, } from "./stations.js";
|
|
2
|
+
/** How long a listener waking a station is held for its first bytes. */
|
|
3
|
+
export const WAKE_WAIT_MS = 10_000;
|
|
2
4
|
/**
|
|
3
5
|
* Who a file is by when the tags do not say: "Artist - Album/track" names
|
|
4
6
|
* them in the folder, and "Artist/Album/track" one folder up.
|
|
@@ -26,6 +28,14 @@ export class StationRunner {
|
|
|
26
28
|
down = new Map();
|
|
27
29
|
/** The server is going down: its channels end because it is, not because a station failed. */
|
|
28
30
|
closing = false;
|
|
31
|
+
/** Wanted on, with nobody listening: no channel until somebody does. */
|
|
32
|
+
asleep = new Set();
|
|
33
|
+
/** For a station asleep: the rest of the pass it was playing, in order. */
|
|
34
|
+
parked = new Map();
|
|
35
|
+
/** Since when a station's channel has had nobody on it. */
|
|
36
|
+
quietSince = new Map();
|
|
37
|
+
waking = new Map();
|
|
38
|
+
idleTimer = null;
|
|
29
39
|
constructor(deps) {
|
|
30
40
|
this.deps = deps;
|
|
31
41
|
}
|
|
@@ -35,7 +45,21 @@ export class StationRunner {
|
|
|
35
45
|
* busy day every station said "something keeps stopping it".
|
|
36
46
|
*/
|
|
37
47
|
close() {
|
|
48
|
+
if (!this.closing) {
|
|
49
|
+
// Where each station on the air was, so after an update it wakes on
|
|
50
|
+
// the next track with its break clock, as it would from a sleep.
|
|
51
|
+
for (const station of this.deps.store.list()) {
|
|
52
|
+
if (station.desired !== "running" || this.asleep.has(station.id))
|
|
53
|
+
continue;
|
|
54
|
+
const point = this.deps.channels.resumePoint(channelOf(station.id));
|
|
55
|
+
if (point)
|
|
56
|
+
this.deps.store.setResume(station.id, { after: point.file || station.resume?.after || "", sinceBreak: point.sinceBreak });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
38
59
|
this.closing = true;
|
|
60
|
+
if (this.idleTimer)
|
|
61
|
+
clearInterval(this.idleTimer);
|
|
62
|
+
this.idleTimer = null;
|
|
39
63
|
for (const timer of this.timers.values())
|
|
40
64
|
clearTimeout(timer);
|
|
41
65
|
this.timers.clear();
|
|
@@ -60,6 +84,7 @@ export class StationRunner {
|
|
|
60
84
|
throw new StationError("nothing_playable", "none of this station's files can be found in the library", 409);
|
|
61
85
|
}
|
|
62
86
|
this.deps.store.setDesired(id, "running", this.now());
|
|
87
|
+
this.asleep.delete(id);
|
|
63
88
|
this.cancelRecovery(id);
|
|
64
89
|
this.attempts.delete(id);
|
|
65
90
|
await this.launch(id);
|
|
@@ -68,6 +93,9 @@ export class StationRunner {
|
|
|
68
93
|
/** Wanted off, and taken off. Stays off across restarts. */
|
|
69
94
|
stop(id) {
|
|
70
95
|
const station = this.deps.store.setDesired(id, "stopped", this.now());
|
|
96
|
+
this.asleep.delete(id);
|
|
97
|
+
this.parked.delete(id);
|
|
98
|
+
this.quietSince.delete(id);
|
|
71
99
|
this.down.delete(id);
|
|
72
100
|
this.cancelRecovery(id);
|
|
73
101
|
this.attempts.delete(id);
|
|
@@ -83,6 +111,11 @@ export class StationRunner {
|
|
|
83
111
|
skip(id) {
|
|
84
112
|
if (!this.deps.store.get(id))
|
|
85
113
|
throw new StationError("not_found", "no such station", 404);
|
|
114
|
+
// Asleep, the next track is the one after the one it will wake on.
|
|
115
|
+
if (this.asleep.has(id)) {
|
|
116
|
+
this.parked.get(id)?.shift();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
86
119
|
if (!this.deps.channels.skip(channelOf(id)))
|
|
87
120
|
throw new StationError("not_live", "the station is not playing", 409);
|
|
88
121
|
}
|
|
@@ -96,13 +129,162 @@ export class StationRunner {
|
|
|
96
129
|
}
|
|
97
130
|
/** Put back every station that was running when this server last stopped. */
|
|
98
131
|
restore() {
|
|
132
|
+
// Asleep until somebody listens, when stations sleep at all: putting
|
|
133
|
+
// every one on at once, to have each go back to sleep a minute later,
|
|
134
|
+
// is a storm of ffmpeg for nobody.
|
|
135
|
+
const sleepy = (this.deps.idleSeconds?.() ?? 0) > 0;
|
|
99
136
|
for (const station of this.deps.store.list()) {
|
|
100
137
|
if (station.desired !== "running")
|
|
101
138
|
continue;
|
|
139
|
+
if (sleepy) {
|
|
140
|
+
this.asleep.add(station.id);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
102
143
|
this.log(` Putting station "${station.name}" back on the air.`);
|
|
103
144
|
void this.launch(station.id);
|
|
104
145
|
}
|
|
105
146
|
}
|
|
147
|
+
/** Whether a station is asleep: wanted on, on the air to the guide, with no channel until somebody listens. */
|
|
148
|
+
isSleeping(id) {
|
|
149
|
+
return this.asleep.has(id);
|
|
150
|
+
}
|
|
151
|
+
/** The file a sleeping station would wake on, "" when it cannot say. */
|
|
152
|
+
nextUp(id) {
|
|
153
|
+
if (!this.asleep.has(id))
|
|
154
|
+
return "";
|
|
155
|
+
const parked = this.parked.get(id)?.[0];
|
|
156
|
+
if (parked)
|
|
157
|
+
return parked;
|
|
158
|
+
const station = this.deps.store.get(id);
|
|
159
|
+
if (!station || station.shuffle || !station.resume?.after)
|
|
160
|
+
return "";
|
|
161
|
+
const files = station.items;
|
|
162
|
+
const at = files.indexOf(station.resume.after);
|
|
163
|
+
return at >= 0 ? files[at + 1] ?? "" : "";
|
|
164
|
+
}
|
|
165
|
+
/** Look every so often for stations nobody is listening to. */
|
|
166
|
+
watchIdle(everyMs = 5_000) {
|
|
167
|
+
if (this.idleTimer)
|
|
168
|
+
return;
|
|
169
|
+
this.idleTimer = setInterval(() => this.sweep(), everyMs);
|
|
170
|
+
this.idleTimer.unref?.();
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Put to sleep every station whose channel has had nobody on it for the
|
|
174
|
+
* idle time. Anybody attached counts: a listener on the link or /radio, a
|
|
175
|
+
* class or room relaying it, an HLS packager, a peer pulling it. The
|
|
176
|
+
* now-playing events alone do not.
|
|
177
|
+
*/
|
|
178
|
+
sweep() {
|
|
179
|
+
const idle = this.deps.idleSeconds?.() ?? 0;
|
|
180
|
+
if (idle <= 0 || this.closing)
|
|
181
|
+
return;
|
|
182
|
+
const now = this.now();
|
|
183
|
+
for (const station of this.deps.store.list()) {
|
|
184
|
+
const id = station.id;
|
|
185
|
+
const channelId = channelOf(id);
|
|
186
|
+
if (station.desired !== "running" || this.asleep.has(id) || this.starting.has(id) || !this.deps.channels.has(channelId)) {
|
|
187
|
+
this.quietSince.delete(id);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (this.deps.channels.listenersOf(channelId) > 0) {
|
|
191
|
+
this.quietSince.delete(id);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const since = this.quietSince.get(id);
|
|
195
|
+
if (since === undefined)
|
|
196
|
+
this.quietSince.set(id, now);
|
|
197
|
+
else if (now - since >= idle * 1000)
|
|
198
|
+
this.sleep(id);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** Off the air for now, still wanted on: remembers where it was, and is not a failure. */
|
|
202
|
+
sleep(id) {
|
|
203
|
+
const station = this.deps.store.get(id);
|
|
204
|
+
const channelId = channelOf(id);
|
|
205
|
+
if (!station || station.desired !== "running" || this.asleep.has(id))
|
|
206
|
+
return;
|
|
207
|
+
const point = this.deps.channels.resumePoint(channelId);
|
|
208
|
+
this.deps.store.setResume(id, {
|
|
209
|
+
after: point?.file || station.resume?.after || "",
|
|
210
|
+
sinceBreak: point ? point.sinceBreak : station.resume?.sinceBreak ?? 0,
|
|
211
|
+
});
|
|
212
|
+
this.parked.set(id, point?.upcoming ?? []);
|
|
213
|
+
this.asleep.add(id);
|
|
214
|
+
this.quietSince.delete(id);
|
|
215
|
+
this.cancelRecovery(id);
|
|
216
|
+
this.attempts.delete(id);
|
|
217
|
+
this.errors.delete(id);
|
|
218
|
+
this.down.delete(id);
|
|
219
|
+
if (this.deps.channels.has(channelId)) {
|
|
220
|
+
this.stopping.add(channelId);
|
|
221
|
+
this.deps.channels.stop(channelId);
|
|
222
|
+
}
|
|
223
|
+
this.log(` Station "${station.name}" is asleep until somebody listens.`);
|
|
224
|
+
}
|
|
225
|
+
/** On again for a listener, if it was asleep. True when its channel is up (or coming up). */
|
|
226
|
+
wake(id) {
|
|
227
|
+
const pending = this.waking.get(id);
|
|
228
|
+
if (pending)
|
|
229
|
+
return pending;
|
|
230
|
+
if (!this.asleep.has(id))
|
|
231
|
+
return Promise.resolve(this.deps.channels.has(channelOf(id)) || this.starting.has(id));
|
|
232
|
+
this.asleep.delete(id);
|
|
233
|
+
this.quietSince.delete(id);
|
|
234
|
+
const station = this.deps.store.get(id);
|
|
235
|
+
if (station)
|
|
236
|
+
this.log(` Station "${station.name}" woke for a listener.`);
|
|
237
|
+
const woken = this.launch(id).finally(() => this.waking.delete(id));
|
|
238
|
+
this.waking.set(id, woken);
|
|
239
|
+
return woken;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Wake it if it is asleep, and wait (up to `timeoutMs`) for its first
|
|
243
|
+
* bytes, so a listener hears it start rather than a stream that ends.
|
|
244
|
+
* True when the channel is up.
|
|
245
|
+
*/
|
|
246
|
+
async awake(id, timeoutMs = WAKE_WAIT_MS, gone = () => false) {
|
|
247
|
+
const channelId = channelOf(id);
|
|
248
|
+
const deadline = Date.now() + timeoutMs;
|
|
249
|
+
const woke = this.asleep.has(id) || this.waking.has(id);
|
|
250
|
+
if (!woke)
|
|
251
|
+
return this.deps.channels.has(channelId);
|
|
252
|
+
let timer;
|
|
253
|
+
const up = await Promise.race([
|
|
254
|
+
this.wake(id),
|
|
255
|
+
new Promise((done) => { timer = setTimeout(() => done(false), timeoutMs); }),
|
|
256
|
+
]);
|
|
257
|
+
clearTimeout(timer);
|
|
258
|
+
if (!up && !this.deps.channels.has(channelId))
|
|
259
|
+
return false;
|
|
260
|
+
while (!this.deps.channels.flowing(channelId) && Date.now() < deadline && !gone()) {
|
|
261
|
+
if (!this.deps.channels.has(channelId) && !this.starting.has(id))
|
|
262
|
+
return false;
|
|
263
|
+
await new Promise((done) => setTimeout(done, 50));
|
|
264
|
+
}
|
|
265
|
+
return this.deps.channels.has(channelId);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Where a launch picks up: the rest of the pass it slept in, or (after the
|
|
269
|
+
* server restarted) the files after the one it was on; and the break
|
|
270
|
+
* clock. Taken once, by the next launch.
|
|
271
|
+
*/
|
|
272
|
+
takeResume(id, station, files) {
|
|
273
|
+
const parked = this.parked.get(id);
|
|
274
|
+
this.parked.delete(id);
|
|
275
|
+
let upcoming = parked && parked.length > 0 ? parked : null;
|
|
276
|
+
const after = station.resume?.after ?? "";
|
|
277
|
+
if (!parked && after && !station.shuffle) {
|
|
278
|
+
const at = files.indexOf(after);
|
|
279
|
+
if (at >= 0 && at + 1 < files.length)
|
|
280
|
+
upcoming = files.slice(at + 1);
|
|
281
|
+
}
|
|
282
|
+
const sinceBreak = station.resume?.sinceBreak ?? 0;
|
|
283
|
+
// Used once: a recovery later is a start like any other.
|
|
284
|
+
if (station.resume)
|
|
285
|
+
this.deps.store.setResume(id, null);
|
|
286
|
+
return { upcoming, sinceBreak };
|
|
287
|
+
}
|
|
106
288
|
async launch(id) {
|
|
107
289
|
const station = this.deps.store.get(id);
|
|
108
290
|
if (!station || station.desired !== "running")
|
|
@@ -118,6 +300,10 @@ export class StationRunner {
|
|
|
118
300
|
}
|
|
119
301
|
this.starting.add(id);
|
|
120
302
|
const store = this.deps.store;
|
|
303
|
+
const resume = this.takeResume(id, station, files);
|
|
304
|
+
// The first pass after a sleep is the rest of the one it slept in.
|
|
305
|
+
let carryOn = resume.upcoming;
|
|
306
|
+
let clock = resume.sinceBreak;
|
|
121
307
|
try {
|
|
122
308
|
const ok = await this.deps.pull(channelId, station.name, files, {
|
|
123
309
|
audioBitrate: station.bitrate,
|
|
@@ -125,9 +311,19 @@ export class StationRunner {
|
|
|
125
311
|
repeat: station.repeat,
|
|
126
312
|
passOver: true,
|
|
127
313
|
// Read at every pass, so turning shuffle on or off lands then.
|
|
128
|
-
order: (length, files) =>
|
|
129
|
-
|
|
130
|
-
|
|
314
|
+
order: (length, files) => {
|
|
315
|
+
if (carryOn) {
|
|
316
|
+
const wanted = carryOn;
|
|
317
|
+
carryOn = null;
|
|
318
|
+
const index = new Map(files.map((file, at) => [file, at]));
|
|
319
|
+
const picked = wanted.map((file) => index.get(file)).filter((at) => at !== undefined && !store.isExcluded(id, files[at]));
|
|
320
|
+
if (picked.length > 0)
|
|
321
|
+
return picked;
|
|
322
|
+
}
|
|
323
|
+
return store.get(id)?.shuffle
|
|
324
|
+
? spreadByArtist(files, (file) => this.deps.artistOf?.(file) || artistFromPath(file))
|
|
325
|
+
: [...Array(length).keys()];
|
|
326
|
+
},
|
|
131
327
|
// Read at every track, so an edit lands on the next one.
|
|
132
328
|
current: () => {
|
|
133
329
|
const now = store.get(id);
|
|
@@ -137,7 +333,12 @@ export class StationRunner {
|
|
|
137
333
|
// Read at every boundary, so setting breaks on a running station
|
|
138
334
|
// lands without a restart; 0 is none.
|
|
139
335
|
...(this.deps.breaks
|
|
140
|
-
? { breaks: {
|
|
336
|
+
? { breaks: {
|
|
337
|
+
everySeconds: () => store.get(id)?.breaksEvery ?? 0,
|
|
338
|
+
next: () => this.deps.breaks(station.video),
|
|
339
|
+
// Once: a redial later in the day starts its own clock, as it always has.
|
|
340
|
+
sinceBreak: () => { const was = clock; clock = 0; return was; },
|
|
341
|
+
} }
|
|
141
342
|
: {}),
|
|
142
343
|
onFailure: ({ file, reason }) => {
|
|
143
344
|
this.errors.set(id, reason);
|
|
@@ -218,6 +419,9 @@ export class StationRunner {
|
|
|
218
419
|
return "stopped";
|
|
219
420
|
if (station.desired === "stopped")
|
|
220
421
|
return "stopped";
|
|
422
|
+
// On the air to anyone asking: the first listener wakes it.
|
|
423
|
+
if (this.asleep.has(id))
|
|
424
|
+
return "live";
|
|
221
425
|
if (this.starting.has(id))
|
|
222
426
|
return "starting";
|
|
223
427
|
const info = this.deps.channels.info(channelOf(id));
|
package/dist/stations.d.ts
CHANGED
|
@@ -41,6 +41,15 @@ export interface Station {
|
|
|
41
41
|
idempotencyKey: string;
|
|
42
42
|
createdAt: number;
|
|
43
43
|
updatedAt: number;
|
|
44
|
+
/**
|
|
45
|
+
* Where it was when it last went to sleep with nobody listening: the file
|
|
46
|
+
* that was on, so it carries on after it, and the seconds of airtime since
|
|
47
|
+
* its last break, so breaks keep their spacing across sleeps.
|
|
48
|
+
*/
|
|
49
|
+
resume?: {
|
|
50
|
+
after: string;
|
|
51
|
+
sinceBreak: number;
|
|
52
|
+
};
|
|
44
53
|
}
|
|
45
54
|
/** Times an item may fail in a row before rotation leaves it out. */
|
|
46
55
|
export declare const EXCLUDE_AFTER = 3;
|
|
@@ -97,6 +106,11 @@ export declare class StationStore {
|
|
|
97
106
|
/** Name, words, files and rotation. Who may listen is changed by publish/unpublish. */
|
|
98
107
|
update(id: string, input: StationInput, items: string[] | null, now?: number): Station;
|
|
99
108
|
remove(id: string): boolean;
|
|
109
|
+
/** Where it was when it went to sleep (Station.resume); null forgets it. */
|
|
110
|
+
setResume(id: string, resume: {
|
|
111
|
+
after: string;
|
|
112
|
+
sinceBreak: number;
|
|
113
|
+
} | null): void;
|
|
100
114
|
setDesired(id: string, desired: Desired, now?: number): Station;
|
|
101
115
|
/**
|
|
102
116
|
* Who may hear it, and whether the directory lists it. Public needs a
|
package/dist/stations.js
CHANGED
|
@@ -262,6 +262,17 @@ export class StationStore {
|
|
|
262
262
|
this.save();
|
|
263
263
|
return had;
|
|
264
264
|
}
|
|
265
|
+
/** Where it was when it went to sleep (Station.resume); null forgets it. */
|
|
266
|
+
setResume(id, resume) {
|
|
267
|
+
const station = this.stations.get(id);
|
|
268
|
+
if (!station)
|
|
269
|
+
return;
|
|
270
|
+
if (resume)
|
|
271
|
+
station.resume = { after: resume.after, sinceBreak: Math.max(0, Math.round(resume.sinceBreak)) };
|
|
272
|
+
else
|
|
273
|
+
delete station.resume;
|
|
274
|
+
this.save();
|
|
275
|
+
}
|
|
265
276
|
setDesired(id, desired, now = Date.now()) {
|
|
266
277
|
const station = this.require(id);
|
|
267
278
|
station.desired = desired;
|
package/package.json
CHANGED
package/src/channels.ts
CHANGED
|
@@ -325,6 +325,19 @@ export class Channel {
|
|
|
325
325
|
return done;
|
|
326
326
|
}
|
|
327
327
|
|
|
328
|
+
/** How far listeners have got into this dial of a list, by the encoder's progress. */
|
|
329
|
+
private heard = 0;
|
|
330
|
+
|
|
331
|
+
/** Where a list would pick up after what listeners hear now (PlaylistInputStream.resumePoint). */
|
|
332
|
+
resumePoint(): { file: string; upcoming: string[]; sinceBreak: number } | null {
|
|
333
|
+
return this.playlistInput?.resumePoint(this.heard) ?? null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Whether listeners would get sound (and, for a picture, its opening) if they joined now. */
|
|
337
|
+
get flowing(): boolean {
|
|
338
|
+
return this.info.bytes > 0 && (!this.fragments || this.fragments.ready);
|
|
339
|
+
}
|
|
340
|
+
|
|
328
341
|
/** An entry's tags, as read when it was put on. */
|
|
329
342
|
tagsOf(file: string): { title: string; artist: string; album: string } | undefined {
|
|
330
343
|
return this.playlistInput?.tags.get(file);
|
|
@@ -503,6 +516,7 @@ export class Channel {
|
|
|
503
516
|
...resume.rotation,
|
|
504
517
|
}) : null;
|
|
505
518
|
this.playlistInput = playlistInput;
|
|
519
|
+
this.heard = 0;
|
|
506
520
|
const through = this.outroOn ? null : playlistInput
|
|
507
521
|
? { format: "mpegts", open: async (signal: AbortSignal) => playlistInput.open(signal) }
|
|
508
522
|
: this.options.through?.(this.info, from, input, audio) ?? null;
|
|
@@ -585,6 +599,7 @@ export class Channel {
|
|
|
585
599
|
if (match) {
|
|
586
600
|
const seconds = Number(match[1]) / 1e6;
|
|
587
601
|
if (playlistInput) {
|
|
602
|
+
this.heard = seconds;
|
|
588
603
|
const place = playlistInput.place(seconds);
|
|
589
604
|
// A break (-1) belongs to no list; a track's index belongs to its own.
|
|
590
605
|
if (place.at >= 0 && this.info.playlist !== place.list) this.info.playlist = place.list as string[];
|
|
@@ -1450,6 +1465,21 @@ export class Channels {
|
|
|
1450
1465
|
return this.open.get(id)?.tagsOf(file);
|
|
1451
1466
|
}
|
|
1452
1467
|
|
|
1468
|
+
/** Where a channel playing a list would pick up after what is on now; null when it is not one. */
|
|
1469
|
+
resumePoint(id: string): { file: string; upcoming: string[]; sinceBreak: number } | null {
|
|
1470
|
+
return this.open.get(id)?.resumePoint() ?? null;
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
/** Whether a channel is up and has started sending. */
|
|
1474
|
+
flowing(id: string): boolean {
|
|
1475
|
+
return this.open.get(id)?.flowing ?? false;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
/** Who is attached to one channel: listeners, relays, packagers. */
|
|
1479
|
+
listenersOf(id: string): number {
|
|
1480
|
+
return this.open.get(id)?.listeners.size ?? 0;
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1453
1483
|
/** The next entry, now, on a channel playing a list of files. */
|
|
1454
1484
|
skip(id: string): boolean {
|
|
1455
1485
|
return this.open.get(id)?.skip() ?? false;
|
package/src/library.ts
CHANGED
|
@@ -39,6 +39,19 @@ export interface Config {
|
|
|
39
39
|
limits?: Record<string, unknown>;
|
|
40
40
|
/** Proxies whose X-Forwarded-For is believed for listener shares (`nixamp station shares trust ADDRESS`). */
|
|
41
41
|
trustedProxies?: string[];
|
|
42
|
+
/** Seconds with nobody listening before a station sleeps; 0 never. 60 when unset. */
|
|
43
|
+
stationIdleSeconds?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A station with nobody listening sleeps after this many seconds, unless config or NIXAMP_STATION_IDLE says otherwise. */
|
|
47
|
+
export const DEFAULT_STATION_IDLE_SECONDS = 60;
|
|
48
|
+
|
|
49
|
+
/** How long a station waits with nobody listening before it sleeps: env, then config, then 60; 0 is never. */
|
|
50
|
+
export function stationIdleSeconds(env: Record<string, string | undefined>, config: Config): number {
|
|
51
|
+
const raw = env["NIXAMP_STATION_IDLE"];
|
|
52
|
+
if (raw !== undefined && raw.trim() !== "" && Number.isFinite(Number(raw)) && Number(raw) >= 0) return Number(raw);
|
|
53
|
+
const set = config.stationIdleSeconds;
|
|
54
|
+
return typeof set === "number" && Number.isFinite(set) && set >= 0 ? set : DEFAULT_STATION_IDLE_SECONDS;
|
|
42
55
|
}
|
|
43
56
|
|
|
44
57
|
export function configPath(): string {
|
|
@@ -55,6 +68,7 @@ export function readConfig(): Config {
|
|
|
55
68
|
...(typeof record["publish"] === "boolean" ? { publish: record["publish"] } : {}),
|
|
56
69
|
...(typeof record["autoStations"] === "object" && record["autoStations"] !== null ? { autoStations: record["autoStations"] as Record<string, unknown> } : {}),
|
|
57
70
|
...(typeof record["limits"] === "object" && record["limits"] !== null ? { limits: record["limits"] as Record<string, unknown> } : {}),
|
|
71
|
+
...(typeof record["stationIdleSeconds"] === "number" ? { stationIdleSeconds: record["stationIdleSeconds"] } : {}),
|
|
58
72
|
...(Array.isArray(record["trustedProxies"]) ? { trustedProxies: (record["trustedProxies"] as unknown[]).filter((one): one is string => typeof one === "string") } : {}),
|
|
59
73
|
...(Array.isArray(record["guideHosts"]) ? { guideHosts: (record["guideHosts"] as unknown[]).filter((one): one is string => typeof one === "string") } : {}),
|
|
60
74
|
...(typeof record["guideAds"] === "object" && record["guideAds"] !== null ? { guideAds: record["guideAds"] as Config["guideAds"] } : {}),
|
package/src/playlist-input.ts
CHANGED
|
@@ -39,7 +39,16 @@ export interface PlaylistInput {
|
|
|
39
39
|
* `next` answers a file to play (an advert, an ident) or null for no break
|
|
40
40
|
* this time. The break is outside the rotation and the station's history.
|
|
41
41
|
*/
|
|
42
|
-
breaks?: {
|
|
42
|
+
breaks?: {
|
|
43
|
+
everySeconds: number | (() => number);
|
|
44
|
+
next: () => Promise<string | null>;
|
|
45
|
+
/**
|
|
46
|
+
* Seconds of airtime since the last break when this stream opens, asked
|
|
47
|
+
* once per open: a station that slept with nobody listening carries its
|
|
48
|
+
* clock across, so breaks come every so often of time on the air.
|
|
49
|
+
*/
|
|
50
|
+
sinceBreak?: () => number;
|
|
51
|
+
};
|
|
43
52
|
/** Told when an entry is passed over because it would not play. */
|
|
44
53
|
onFailure?: (event: { at: number; file: string; reason: string }) => void;
|
|
45
54
|
}
|
|
@@ -51,12 +60,26 @@ export interface PlaylistInput {
|
|
|
51
60
|
*/
|
|
52
61
|
export const MAX_FAILURES_IN_A_ROW = 5;
|
|
53
62
|
|
|
63
|
+
/** An entry that started: where it sits in the stream and in its pass. */
|
|
64
|
+
interface Entry {
|
|
65
|
+
at: number;
|
|
66
|
+
file: string;
|
|
67
|
+
start: number;
|
|
68
|
+
position: number;
|
|
69
|
+
list: readonly string[];
|
|
70
|
+
/** The pass's order and how far into it the next entry is, for resumePoint. */
|
|
71
|
+
order: readonly number[];
|
|
72
|
+
step: number;
|
|
73
|
+
/** Where the break clock stood when it started. */
|
|
74
|
+
lastBreak: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
54
77
|
/** Decode each file independently; feed one persistent muxer a fixed stream
|
|
55
78
|
* profile and continuous timestamps. A concat demuxer cannot decode a list
|
|
56
79
|
* whose input codecs differ. Only one entry is encoded at a time, with pipe
|
|
57
80
|
* backpressure bounding read-ahead independently of the directory's size. */
|
|
58
81
|
export class PlaylistInputStream {
|
|
59
|
-
private entries:
|
|
82
|
+
private entries: Entry[] = [];
|
|
60
83
|
error: Error | null = null;
|
|
61
84
|
complete = false;
|
|
62
85
|
/**
|
|
@@ -68,7 +91,7 @@ export class PlaylistInputStream {
|
|
|
68
91
|
constructor(private readonly options: PlaylistInput) {}
|
|
69
92
|
|
|
70
93
|
/** Every entry that has started, in the order it started, with where it sits in the stream. */
|
|
71
|
-
get history(): ReadonlyArray<
|
|
94
|
+
get history(): ReadonlyArray<Entry> {
|
|
72
95
|
return this.entries;
|
|
73
96
|
}
|
|
74
97
|
|
|
@@ -84,6 +107,22 @@ export class PlaylistInputStream {
|
|
|
84
107
|
: { at: this.options.at, position: this.options.position, list: this.options.files };
|
|
85
108
|
}
|
|
86
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Where to pick up after this point of the stream, for a station that
|
|
112
|
+
* sleeps with nobody listening: the file that is on ("" in a break), the
|
|
113
|
+
* rest of this pass in the order it was going to play, and the seconds
|
|
114
|
+
* of airtime since the last break.
|
|
115
|
+
*/
|
|
116
|
+
resumePoint(seconds: number): { file: string; upcoming: string[]; sinceBreak: number } | null {
|
|
117
|
+
const entry = this.entries.findLast(one => seconds >= one.start) ?? this.entries[0];
|
|
118
|
+
if (!entry) return null;
|
|
119
|
+
return {
|
|
120
|
+
file: entry.at >= 0 ? entry.file : "",
|
|
121
|
+
upcoming: entry.order.slice(entry.step).map((index) => entry.list[index]).filter((one): one is string => typeof one === "string"),
|
|
122
|
+
sinceBreak: entry.at < 0 ? 0 : Math.max(0, seconds - entry.lastBreak),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
87
126
|
/** The decoder for the entry that is on, so skip() can end it early. */
|
|
88
127
|
private stopCurrent: (() => void) | null = null;
|
|
89
128
|
private skipping = false;
|
|
@@ -114,7 +153,9 @@ export class PlaylistInputStream {
|
|
|
114
153
|
let step = Math.min(Math.max(0, o.at), order.length);
|
|
115
154
|
let played = 0;
|
|
116
155
|
let last = "";
|
|
117
|
-
|
|
156
|
+
// Counted back from 0 by however long it had been, so a clock carried
|
|
157
|
+
// across a sleep comes due when it would have on the air.
|
|
158
|
+
let lastBreak = -Math.max(0, o.breaks?.sinceBreak?.() ?? 0);
|
|
118
159
|
// A film cut for a break, to be picked up where it stopped.
|
|
119
160
|
let resume: { at: number; file: string; position: number } | null = null;
|
|
120
161
|
for (;;) {
|
|
@@ -179,7 +220,7 @@ export class PlaylistInputStream {
|
|
|
179
220
|
const room = o.video && !isBreak && o.breaks && every > 0 ? every - (offset - lastBreak) : Infinity;
|
|
180
221
|
const cut = room >= 1 && full - room > 5;
|
|
181
222
|
const duration = cut ? room : full;
|
|
182
|
-
this.entries.push({ at, file, start: offset, position, list: files });
|
|
223
|
+
this.entries.push({ at, file, start: offset, position, list: files, order, step, lastBreak });
|
|
183
224
|
const hasVideo = Boolean(codecs.video);
|
|
184
225
|
const extra = o.video && !hasVideo
|
|
185
226
|
? ["-f", "lavfi", "-i", `color=c=black:s=${width}x${height}:r=30`]
|
package/src/server.ts
CHANGED
|
@@ -122,7 +122,7 @@ import { Porkbun, isIPv4, isIPv6, type DnsZone } from "./dns.ts";
|
|
|
122
122
|
import { NameError, Names } from "./names.ts";
|
|
123
123
|
import { AcmeIssuer, Certs } from "./certs.ts";
|
|
124
124
|
import { claimName, fetchCert, humanizeSource, labelFor, readCertFiles, writeCertFiles } from "./naming.ts";
|
|
125
|
-
import { forbiddenLibrary, readConfig, readLibrary, writeConfig } from "./library.ts";
|
|
125
|
+
import { forbiddenLibrary, readConfig, readLibrary, stationIdleSeconds, writeConfig } from "./library.ts";
|
|
126
126
|
import { createThrottle, presentedCredential, type Throttle } from "@profullstack/throttle";
|
|
127
127
|
import { Durable } from "./durable.ts";
|
|
128
128
|
import { notifyAll, resendEmail, webPush, type Notification } from "./notify.ts";
|
|
@@ -5940,6 +5940,12 @@ export function createHandler(engine: Engine, options: HandlerOptions) {
|
|
|
5940
5940
|
return;
|
|
5941
5941
|
}
|
|
5942
5942
|
|
|
5943
|
+
// A station asleep with nobody listening is woken by whoever asks for
|
|
5944
|
+
// its channel here too, and they are held for its first bytes.
|
|
5945
|
+
if ((action === "hls" || action === undefined) && request.method === "GET" && !channels.has(id) && stationOfChannel(id) && options.stations?.runner.isSleeping(stationOfChannel(id))) {
|
|
5946
|
+
await options.stations.runner.awake(stationOfChannel(id));
|
|
5947
|
+
}
|
|
5948
|
+
|
|
5943
5949
|
// The same channel as HLS: a playlist of short files, which is what
|
|
5944
5950
|
// Safari on an iPhone plays live -- it will not take the endless MP4
|
|
5945
5951
|
// below, and spun on it a few times before giving up. Packaged on
|
|
@@ -7455,6 +7461,8 @@ export async function serve(argv: string[], version = "0.1.0"): Promise<void> {
|
|
|
7455
7461
|
onRestart: (id) => stationUsage.restarted(id),
|
|
7456
7462
|
onFailure: (id) => stationUsage.failed(id),
|
|
7457
7463
|
artistOf: (file) => trackFor(engine, file)?.artist ?? "",
|
|
7464
|
+
// Nobody listening for this long and a station sleeps until somebody does.
|
|
7465
|
+
idleSeconds: () => stationIdleSeconds(process.env, readConfig()),
|
|
7458
7466
|
store: stationStore,
|
|
7459
7467
|
channels,
|
|
7460
7468
|
pull: async (channelId, name, files, known) =>
|
|
@@ -7729,6 +7737,7 @@ export async function serve(argv: string[], version = "0.1.0"): Promise<void> {
|
|
|
7729
7737
|
});
|
|
7730
7738
|
}
|
|
7731
7739
|
stationRunner.restore();
|
|
7740
|
+
stationRunner.watchIdle();
|
|
7732
7741
|
// Where each film has got to, written down every so often, so a restart
|
|
7733
7742
|
// picks it up from about there rather than from the start. Only when it
|
|
7734
7743
|
// has changed: a server carrying nothing but live television writes
|
package/src/station-routes.ts
CHANGED
|
@@ -114,14 +114,16 @@ function nowPlayingFile(ctx: StationContext, station: Station): string {
|
|
|
114
114
|
|
|
115
115
|
export function publicView(ctx: StationContext, station: Station): PublicStation {
|
|
116
116
|
const state = ctx.runner.observed(station.id);
|
|
117
|
-
|
|
117
|
+
// Asleep, what it will wake on: the first listener hears that next.
|
|
118
|
+
const asleep = ctx.runner.isSleeping?.(station.id) ?? false;
|
|
119
|
+
const file = asleep ? ctx.runner.nextUp(station.id) : state === "live" || state === "recovering" ? nowPlayingFile(ctx, station) : "";
|
|
118
120
|
// The tags the player read when it put this file on, else the library's.
|
|
119
121
|
const track = file ? (ctx.channels.tagsOf(channelOf(station.id), file) ?? ctx.trackFor(file)) : null;
|
|
120
122
|
// What the channel is carrying wins over the flag: a station made by hand
|
|
121
123
|
// from films streams video whether or not anyone said so.
|
|
122
124
|
const video = station.video || ctx.channels.kindOf(channelOf(station.id)) === "video";
|
|
123
125
|
// How far into the song (or film) on the air, by the encoder's clock, and how long it is.
|
|
124
|
-
const position = file ? ctx.channels.info(channelOf(station.id))?.position : undefined;
|
|
126
|
+
const position = file && !asleep ? ctx.channels.info(channelOf(station.id))?.position : undefined;
|
|
125
127
|
const duration = file && ctx.durationOf ? ctx.durationOf(file) : 0;
|
|
126
128
|
const progress = typeof position === "number" && duration > 0 ? { position: Math.round(Math.min(position, duration) * 10) / 10, duration: Math.round(duration * 10) / 10 } : null;
|
|
127
129
|
return {
|
|
@@ -161,11 +163,13 @@ export function guideEntries(ctx: StationContext): Array<PublicStation & { page:
|
|
|
161
163
|
return [...names].map(([slug, one]) => ({ ...publicView(ctx, one), page: links(ctx, one, base).page, m3u: links(ctx, one, base).m3u, group: guideGroup(one), radio: `${radioBase}/radio/${slug}` }));
|
|
162
164
|
}
|
|
163
165
|
|
|
164
|
-
export function ownerView(ctx: StationContext, station: Station): OwnerStation & { links: ReturnType<typeof links>; nowPlaying: string } {
|
|
166
|
+
export function ownerView(ctx: StationContext, station: Station): OwnerStation & { links: ReturnType<typeof links>; nowPlaying: string; sleeping: boolean } {
|
|
165
167
|
const { failures, idempotencyKey: _key, ...rest } = station;
|
|
166
168
|
return {
|
|
167
169
|
...rest,
|
|
168
170
|
state: ctx.runner.observed(station.id),
|
|
171
|
+
// On the air with nobody listening: no encoder until somebody does.
|
|
172
|
+
sleeping: ctx.runner.isSleeping?.(station.id) ?? false,
|
|
169
173
|
excluded: Object.entries(failures).filter(([, count]) => count >= 3).map(([file]) => file),
|
|
170
174
|
listeners: ctx.runner.listeners(station.id),
|
|
171
175
|
error: ctx.runner.error(station.id),
|
|
@@ -348,18 +352,31 @@ export async function handleStations(
|
|
|
348
352
|
return true;
|
|
349
353
|
}
|
|
350
354
|
const channelId = channelOf(station.id);
|
|
351
|
-
|
|
352
|
-
|
|
355
|
+
// Asleep, a HEAD is told what it would get without waking it.
|
|
356
|
+
if (method === "HEAD" && ctx.runner.isSleeping?.(station.id)) {
|
|
357
|
+
response.writeHead(200, { ...ctx.headers, "content-type": station.video ? "video/mp4" : "audio/mpeg", "cache-control": "no-store" });
|
|
358
|
+
response.end();
|
|
353
359
|
return true;
|
|
354
360
|
}
|
|
355
361
|
// Admitted only within the station's and the server's limits, and the
|
|
356
|
-
// month's allowance; refused with what to do about it.
|
|
362
|
+
// month's allowance; refused with what to do about it. Before waking it:
|
|
363
|
+
// a listener turned away is no reason to start the encoder.
|
|
357
364
|
const refused = method === "GET" && ctx.usage && ctx.limits ? ctx.usage.admit(station.id, ctx.limits) : null;
|
|
358
365
|
if (refused) {
|
|
359
366
|
response.writeHead(refused.status, { ...ctx.headers, "content-type": "application/json; charset=utf-8", "retry-after": String(refused.retryAfter) });
|
|
360
367
|
response.end(JSON.stringify({ error: refused.error, code: refused.code, retryAfter: refused.retryAfter }));
|
|
361
368
|
return true;
|
|
362
369
|
}
|
|
370
|
+
// The first listener wakes a sleeping station, and is held until it
|
|
371
|
+
// starts sending, so they hear it begin rather than a stream that ends.
|
|
372
|
+
let closed = false;
|
|
373
|
+
response.once("close", () => { closed = true; });
|
|
374
|
+
await ctx.runner.awake?.(station.id, undefined, () => closed);
|
|
375
|
+
if (closed) return true;
|
|
376
|
+
if (!ctx.channels.has(channelId)) {
|
|
377
|
+
ctx.json(response, 503, { error: "the station is not on the air", code: "not_live", state: ctx.runner.observed(station.id) });
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
363
380
|
// Signed in at nixamp.com: no breaks, starting a little behind live so
|
|
364
381
|
// they can be left out without a gap. Radio only (Channel.listenWithoutBreaks).
|
|
365
382
|
const pass = url.searchParams.get(PASS_PARAM) ?? "";
|