1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 20:52:20 +00:00

Client receives the stream corresponding to the requested stream id

This commit is contained in:
Yohann D'ANELLO 2020-10-05 22:34:55 +02:00
parent 0a1329b929
commit 635081f269
2 changed files with 17 additions and 9 deletions

View File

@ -6,7 +6,7 @@ import (
"github.com/haivision/srtgo" "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) log.Printf("New SRT streamer for stream %s", name)
// Create a new buffer // Create a new buffer
@ -38,7 +38,7 @@ func handleStreamer(s *srtgo.SrtSocket, name string, clientDataChannels *[]chan
copy(data, buff[:n]) copy(data, buff[:n])
forwardingChannel <- Packet{StreamName: name, PacketType: "sendData", Data: data} forwardingChannel <- Packet{StreamName: name, PacketType: "sendData", Data: data}
webrtcChannel <- 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} 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} 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 // FIXME Should not pass all dataChannels to one viewer
log.Printf("New SRT viewer for stream %s", name) 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) _, err := s.Write(packet.Data, 10000)
if err != nil { if err != nil {
s.Close() s.Close()
for i, channel := range *dataChannels { for i, channel := range dataChannels[name] {
if channel == dataChannel { if channel == dataChannel {
*dataChannels = append((*dataChannels)[:i], (*dataChannels)[i+1:]...) dataChannels[name] = append(dataChannels[name][:i], dataChannels[name][i+1:]...)
} }
} }
return return

View File

@ -13,6 +13,10 @@ import (
"gitlab.crans.org/nounous/ghostream/auth" "gitlab.crans.org/nounous/ghostream/auth"
) )
var (
clientDataChannels map[string][]chan Packet
)
// Options holds web package configuration // Options holds web package configuration
type Options struct { type Options struct {
ListenAddress string ListenAddress string
@ -52,7 +56,7 @@ func Serve(cfg *Options, authBackend auth.Backend, forwardingChannel, webrtcChan
log.Fatal("Unable to listen for SRT clients:", err) log.Fatal("Unable to listen for SRT clients:", err)
} }
clientDataChannels := make([]chan Packet, 0, cfg.MaxClients) clientDataChannels = make(map[string][]chan Packet)
for { for {
// Wait for new connection // Wait for new connection
@ -70,6 +74,10 @@ func Serve(cfg *Options, authBackend auth.Backend, forwardingChannel, webrtcChan
} }
split := strings.Split(streamID, ":") split := strings.Split(streamID, ":")
if clientDataChannels[streamID] == nil {
clientDataChannels[streamID] = make([]chan Packet, 0, cfg.MaxClients)
}
if len(split) > 1 { if len(split) > 1 {
// password was provided so it is a streamer // password was provided so it is a streamer
name, password := split[0], split[1] 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 { } else {
// password was not provided so it is a viewer // password was not provided so it is a viewer
name := split[0] name := split[0]
dataChannel := make(chan Packet, 4096) 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)
} }
} }
} }