diff --git a/stream/srt/handler.go b/stream/srt/handler.go index c0da7eb..84b784e 100644 --- a/stream/srt/handler.go +++ b/stream/srt/handler.go @@ -6,7 +6,7 @@ import ( "github.com/haivision/srtgo" ) -func handleStreamer(s *srtgo.SrtSocket, name string, clientDataChannels *[]chan Packet, forwardingChannel, webrtcChannel chan Packet) { +func handleStreamer(s *srtgo.SrtSocket, name string, clientDataChannels map[string][]chan Packet, forwardingChannel, webrtcChannel chan Packet) { log.Printf("New SRT streamer for stream %s", name) // Create a new buffer @@ -38,7 +38,7 @@ func handleStreamer(s *srtgo.SrtSocket, name string, clientDataChannels *[]chan copy(data, buff[:n]) forwardingChannel <- Packet{StreamName: name, PacketType: "sendData", Data: data} webrtcChannel <- Packet{StreamName: name, PacketType: "sendData", Data: data} - for _, dataChannel := range *clientDataChannels { + for _, dataChannel := range clientDataChannels[name] { dataChannel <- Packet{StreamName: name, PacketType: "sendData", Data: data} } } @@ -47,7 +47,7 @@ func handleStreamer(s *srtgo.SrtSocket, name string, clientDataChannels *[]chan webrtcChannel <- Packet{StreamName: name, PacketType: "close", Data: nil} } -func handleViewer(s *srtgo.SrtSocket, name string, dataChannel chan Packet, dataChannels *[]chan Packet) { +func handleViewer(s *srtgo.SrtSocket, name string, dataChannel chan Packet, dataChannels map[string][]chan Packet) { // FIXME Should not pass all dataChannels to one viewer log.Printf("New SRT viewer for stream %s", name) @@ -59,9 +59,9 @@ func handleViewer(s *srtgo.SrtSocket, name string, dataChannel chan Packet, data _, err := s.Write(packet.Data, 10000) if err != nil { s.Close() - for i, channel := range *dataChannels { + for i, channel := range dataChannels[name] { if channel == dataChannel { - *dataChannels = append((*dataChannels)[:i], (*dataChannels)[i+1:]...) + dataChannels[name] = append(dataChannels[name][:i], dataChannels[name][i+1:]...) } } return diff --git a/stream/srt/srt.go b/stream/srt/srt.go index 2850788..c05d15e 100644 --- a/stream/srt/srt.go +++ b/stream/srt/srt.go @@ -13,6 +13,10 @@ import ( "gitlab.crans.org/nounous/ghostream/auth" ) +var ( + clientDataChannels map[string][]chan Packet +) + // Options holds web package configuration type Options struct { ListenAddress string @@ -52,7 +56,7 @@ func Serve(cfg *Options, authBackend auth.Backend, forwardingChannel, webrtcChan log.Fatal("Unable to listen for SRT clients:", err) } - clientDataChannels := make([]chan Packet, 0, cfg.MaxClients) + clientDataChannels = make(map[string][]chan Packet) for { // Wait for new connection @@ -70,6 +74,10 @@ func Serve(cfg *Options, authBackend auth.Backend, forwardingChannel, webrtcChan } split := strings.Split(streamID, ":") + if clientDataChannels[streamID] == nil { + clientDataChannels[streamID] = make([]chan Packet, 0, cfg.MaxClients) + } + if len(split) > 1 { // password was provided so it is a streamer name, password := split[0], split[1] @@ -82,15 +90,15 @@ func Serve(cfg *Options, authBackend auth.Backend, forwardingChannel, webrtcChan } } - go handleStreamer(s, name, &clientDataChannels, forwardingChannel, webrtcChannel) + go handleStreamer(s, name, clientDataChannels, forwardingChannel, webrtcChannel) } else { // password was not provided so it is a viewer name := split[0] dataChannel := make(chan Packet, 4096) - clientDataChannels = append(clientDataChannels, dataChannel) + clientDataChannels[streamID] = append(clientDataChannels[streamID], dataChannel) - go handleViewer(s, name, dataChannel, &clientDataChannels) + go handleViewer(s, name, dataChannel, clientDataChannels) } } }