Skip to content

Commit 0625f02

Browse files
committed
Add a global m3u
1 parent f430d00 commit 0625f02

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎docs/api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Each station exposes three endpoints under its ID prefix:
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 |
1212
| `/streams.pls` | `audio/x-scpls` | PLS playlist file listing all stations |
13+
| `/streams.m3u` | `audio/x-mpegurl` | M3U playlist file listing all stations |
1314

1415
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.
1516

‎handler/playlist_m3u.go‎

Lines changed: 27 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
// PlaylistM3U handles GET /{station}/stream.m3u — M3U playlist file.
@@ -19,3 +20,29 @@ func (p *PlaylistM3U) ServeHTTP(w http.ResponseWriter, r *http.Request) {
1920

2021
fmt.Fprintf(w, "#EXTM3U\n#EXTINF:-1,%s\n%s\n", p.Name, url)
2122
}
23+
24+
// M3UStation holds the info needed for one entry in the global M3U playlist.
25+
type M3UStation struct {
26+
Name string
27+
Prefix string
28+
}
29+
30+
// GlobalPlaylistM3U handles GET /streams.m3u — M3U file listing all stations.
31+
type GlobalPlaylistM3U struct {
32+
Stations []M3UStation
33+
}
34+
35+
func (g *GlobalPlaylistM3U) ServeHTTP(w http.ResponseWriter, r *http.Request) {
36+
sorted := make([]M3UStation, len(g.Stations))
37+
copy(sorted, g.Stations)
38+
sort.Slice(sorted, func(i, j int) bool { return sorted[i].Prefix < sorted[j].Prefix })
39+
40+
w.Header().Set("Access-Control-Allow-Origin", "*")
41+
w.Header().Set("Content-Type", "audio/x-mpegurl")
42+
w.Header().Set("Content-Disposition", "inline; filename=\"streams.m3u\"")
43+
44+
fmt.Fprint(w, "#EXTM3U\n")
45+
for _, st := range sorted {
46+
fmt.Fprintf(w, "#EXTINF:-1,%s\n%s\n", st.Name, streamURL(r, st.Prefix))
47+
}
48+
}

‎server/server.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ func New(cfg *config.Config, stations map[string]*Station, geoDB *geo.DB, statsD
108108
}
109109
mux.Handle("/streams.pls", &handler.GlobalPlaylistPLS{Stations: plsStations})
110110

111+
m3uStations := make([]handler.M3UStation, 0, len(stations))
112+
for id, st := range stations {
113+
m3uStations = append(m3uStations, handler.M3UStation{
114+
Name: st.Config.Name,
115+
Prefix: id,
116+
})
117+
}
118+
mux.Handle("/streams.m3u", &handler.GlobalPlaylistM3U{Stations: m3uStations})
119+
111120
mux.Handle("/status", &handler.GlobalStatus{
112121
Stations: stationInfos,
113122
StartTime: s.startTime,

0 commit comments

Comments
 (0)