Skip to content

Commit cffb60b

Browse files
author
Datanoise
committed
fix: implement real-time AutoDJ updates via SSE and resolve timeline reset issues
1 parent b3e88ae commit cffb60b

3 files changed

Lines changed: 166 additions & 43 deletions

File tree

relay/streamer.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,52 @@ func (s *Streamer) RemoveFromPlaylist(idx int) {
201201
}
202202
}
203203

204+
type StreamerStats struct {
205+
Name string
206+
Mount string
207+
State StreamerState
208+
CurrentSong string
209+
StartTime time.Time
210+
Duration time.Duration
211+
PlaylistPos int
212+
PlaylistLen int
213+
Shuffle bool
214+
MPDPort string
215+
MPDPassword string
216+
MusicDir string
217+
Loop bool
218+
InjectMetadata bool
219+
}
220+
221+
func (s *Streamer) GetStats() StreamerStats {
222+
s.mu.RLock()
223+
defer s.mu.RUnlock()
224+
225+
mpdPort := ""
226+
mpdPassword := ""
227+
if s.MPDServer != nil {
228+
mpdPort = s.MPDServer.Port
229+
mpdPassword = s.MPDPassword
230+
}
231+
232+
return StreamerStats{
233+
Name: s.Name,
234+
Mount: s.OutputMount,
235+
State: s.State,
236+
CurrentSong: s.CurrentFile,
237+
StartTime: s.CurrentFileTime,
238+
Duration: s.CurrentFileDuration,
239+
PlaylistPos: s.CurrentPos,
240+
PlaylistLen: len(s.Playlist),
241+
Shuffle: s.Shuffle,
242+
MPDPort: mpdPort,
243+
MPDPassword: mpdPassword,
244+
MusicDir: s.MusicDir,
245+
Loop: s.Loop,
246+
InjectMetadata: s.InjectMetadata,
247+
}
248+
}
249+
204250
func (sm *StreamerManager) StartStreamer(name, mount, musicDir string, loop bool, format string, bitrate int, injectMetadata bool, initialPlaylist []string, mpdEnabled bool, mpdPort, mpdPassword string) (*Streamer, error) {
205251
sm.mu.Lock()
206252
defer sm.mu.Unlock()

server/server.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,18 @@ type relayEventInfo struct {
18601860
Enabled bool `json:"enabled"`
18611861
}
18621862

1863+
type streamerEventInfo struct {
1864+
Name string `json:"name"`
1865+
Mount string `json:"mount"`
1866+
State int `json:"state"`
1867+
CurrentSong string `json:"song"`
1868+
StartTime int64 `json:"start_time"`
1869+
Duration float64 `json:"duration"`
1870+
PlaylistPos int `json:"playlist_pos"`
1871+
PlaylistLen int `json:"playlist_len"`
1872+
Shuffle bool `json:"shuffle"`
1873+
}
1874+
18631875
func (s *Server) collectStatsPayload(user *config.User) ([]byte, error) {
18641876
bi, bo := s.Relay.GetMetrics()
18651877
allStreams := s.Relay.Snapshot()
@@ -1900,6 +1912,25 @@ func (s *Server) collectStatsPayload(user *config.User) ([]byte, error) {
19001912
}
19011913
}
19021914

1915+
activeStreamers := s.StreamerM.GetStreamers()
1916+
streamers := make([]streamerEventInfo, 0, len(activeStreamers))
1917+
for _, st := range activeStreamers {
1918+
if s.hasAccess(user, st.OutputMount) {
1919+
stats := st.GetStats()
1920+
streamers = append(streamers, streamerEventInfo{
1921+
Name: stats.Name,
1922+
Mount: stats.Mount,
1923+
State: int(stats.State),
1924+
CurrentSong: stats.CurrentSong,
1925+
StartTime: stats.StartTime.Unix(),
1926+
Duration: stats.Duration.Seconds(),
1927+
PlaylistPos: stats.PlaylistPos,
1928+
PlaylistLen: stats.PlaylistLen,
1929+
Shuffle: stats.Shuffle,
1930+
})
1931+
}
1932+
}
1933+
19031934
var m runtime.MemStats
19041935
runtime.ReadMemStats(&m)
19051936
totalDropped := int64(0)
@@ -1916,6 +1947,7 @@ func (s *Server) collectStatsPayload(user *config.User) ([]byte, error) {
19161947
"total_streamers": ts,
19171948
"streams": info,
19181949
"relays": relays,
1950+
"streamers": streamers,
19191951
"visible_mounts": s.Config.VisibleMounts,
19201952
"sys_ram": m.Sys,
19211953
"heap_alloc": m.HeapAlloc,

0 commit comments

Comments
 (0)