Skip to content

Commit f430d00

Browse files
committed
Add a global playlist
1 parent 7febf43 commit f430d00

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎docs/api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Each station exposes three endpoints under its ID prefix:
99
| `/<id>/stream` | `audio/mpeg` | Live MP3 audio stream with ICY metadata |
1010
| `/<id>/stream.pls` | `audio/x-scpls` | PLS playlist file pointing to the stream |
1111
| `/<id>/stream.m3u` | `audio/x-mpegurl` | M3U playlist file pointing to the stream |
12+
| `/streams.pls` | `audio/x-scpls` | PLS playlist file listing all stations |
1213

1314
The stream endpoint supports ICY metadata. Clients that send the `Icy-MetaData: 1` request header receive inline metadata blocks containing the current track title and artist.
1415

‎handler/playlist_pls.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handler
33
import (
44
"fmt"
55
"net/http"
6+
"sort"
67
)
78

89
// PlaylistPLS handles GET /{station}/stream.pls — PLS playlist file.
@@ -20,6 +21,35 @@ func (p *PlaylistPLS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2021
fmt.Fprintf(w, "[playlist]\nNumberOfEntries=1\n\nFile1=%s\nTitle1=%s\nLength1=-1\n\nVersion=2\n", url, p.Name)
2122
}
2223

24+
// PLSStation holds the info needed for one entry in the global PLS playlist.
25+
type PLSStation struct {
26+
Name string
27+
Prefix string
28+
}
29+
30+
// GlobalPlaylistPLS handles GET /streams.pls — PLS file listing all stations.
31+
type GlobalPlaylistPLS struct {
32+
Stations []PLSStation
33+
}
34+
35+
func (g *GlobalPlaylistPLS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
36+
w.Header().Set("Access-Control-Allow-Origin", "*")
37+
// Sort by prefix for deterministic output.
38+
sorted := make([]PLSStation, len(g.Stations))
39+
copy(sorted, g.Stations)
40+
sort.Slice(sorted, func(i, j int) bool { return sorted[i].Prefix < sorted[j].Prefix })
41+
42+
w.Header().Set("Content-Type", "audio/x-scpls")
43+
w.Header().Set("Content-Disposition", "inline; filename=\"streams.pls\"")
44+
45+
fmt.Fprintf(w, "[playlist]\nNumberOfEntries=%d\n\n", len(sorted))
46+
for i, st := range sorted {
47+
n := i + 1
48+
fmt.Fprintf(w, "File%d=%s\nTitle%d=%s\nLength%d=-1\n\n", n, streamURL(r, st.Prefix), n, st.Name, n)
49+
}
50+
fmt.Fprint(w, "Version=2\n")
51+
}
52+
2353
func streamURL(r *http.Request, prefix string) string {
2454
scheme := "http"
2555
if r.TLS != nil {

‎server/server.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ func New(cfg *config.Config, stations map[string]*Station, geoDB *geo.DB, statsD
9898
}
9999
}
100100

101+
// Global playlist with all stations
102+
plsStations := make([]handler.PLSStation, 0, len(stations))
103+
for id, st := range stations {
104+
plsStations = append(plsStations, handler.PLSStation{
105+
Name: st.Config.Name,
106+
Prefix: id,
107+
})
108+
}
109+
mux.Handle("/streams.pls", &handler.GlobalPlaylistPLS{Stations: plsStations})
110+
101111
mux.Handle("/status", &handler.GlobalStatus{
102112
Stations: stationInfos,
103113
StartTime: s.startTime,

0 commit comments

Comments
 (0)