mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 19:42:20 +00:00
Support stream listening
This commit is contained in:
parent
087d6eee3b
commit
8498829e08
@ -72,21 +72,35 @@ func Serve(cfg *Options, backend auth.Backend, forwarding chan Packet) {
|
|||||||
// FIXME: See srtgo.SocketOptions and value, err := s.GetSockOptString to get parameters
|
// FIXME: See srtgo.SocketOptions and value, err := s.GetSockOptString to get parameters
|
||||||
// http://ffmpeg.org/ffmpeg-protocols.html#srt
|
// http://ffmpeg.org/ffmpeg-protocols.html#srt
|
||||||
|
|
||||||
|
// FIXME: Get the stream type
|
||||||
|
streamStarted := false
|
||||||
|
// FIXME Better structure
|
||||||
|
clientDataChannels := make([]chan Packet, cfg.MaxClients)
|
||||||
|
listeners := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Wait for new connection
|
// Wait for new connection
|
||||||
s, err := sck.Accept()
|
s, err := sck.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error occurred while accepting request:", err)
|
// log.Println("Error occurred while accepting request:", err)
|
||||||
break // FIXME: should not break here
|
continue // break // FIXME: should not break here
|
||||||
}
|
}
|
||||||
|
|
||||||
go acceptSocket(s)
|
if !streamStarted {
|
||||||
|
go acceptCallerSocket(s, clientDataChannels, &listeners)
|
||||||
|
streamStarted = true
|
||||||
|
} else {
|
||||||
|
dataChannel := make(chan Packet)
|
||||||
|
clientDataChannels[listeners] = dataChannel
|
||||||
|
listeners += 1
|
||||||
|
go acceptListeningSocket(s, dataChannel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sck.Close()
|
sck.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func acceptSocket(s *srtgo.SrtSocket) {
|
func acceptCallerSocket(s *srtgo.SrtSocket, clientDataChannels []chan Packet, listeners *int) {
|
||||||
streamName, err := authenticateSocket(s)
|
streamName, err := authenticateSocket(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Authentication failure:", err)
|
log.Println("Authentication failure:", err)
|
||||||
@ -122,6 +136,9 @@ func acceptSocket(s *srtgo.SrtSocket) {
|
|||||||
data := make([]byte, n)
|
data := make([]byte, n)
|
||||||
copy(data, buff[:n])
|
copy(data, buff[:n])
|
||||||
forwardingChannel <- Packet{StreamName: streamName, PacketType: "sendData", Data: data}
|
forwardingChannel <- Packet{StreamName: streamName, PacketType: "sendData", Data: data}
|
||||||
|
for i := 0; i < *listeners; i += 1 {
|
||||||
|
clientDataChannels[i] <- Packet{StreamName: streamName, PacketType: "sendData", Data: data}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Send to WebRTC
|
// TODO: Send to WebRTC
|
||||||
// See https://github.com/ebml-go/webm/blob/master/reader.go
|
// See https://github.com/ebml-go/webm/blob/master/reader.go
|
||||||
@ -131,6 +148,25 @@ func acceptSocket(s *srtgo.SrtSocket) {
|
|||||||
forwardingChannel <- Packet{StreamName: streamName, PacketType: "close", Data: nil}
|
forwardingChannel <- Packet{StreamName: streamName, PacketType: "close", Data: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func acceptListeningSocket(s *srtgo.SrtSocket, dataChannel chan Packet) {
|
||||||
|
streamName, err := s.GetSockOptString(C.SRTO_STREAMID)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
log.Printf("New listener for stream %s", streamName)
|
||||||
|
|
||||||
|
for {
|
||||||
|
packet := <-dataChannel
|
||||||
|
if packet.PacketType == "sendData" {
|
||||||
|
_, err := s.Write(packet.Data, 10000)
|
||||||
|
if err != nil {
|
||||||
|
s.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func authenticateSocket(s *srtgo.SrtSocket) (string, error) {
|
func authenticateSocket(s *srtgo.SrtSocket) (string, error) {
|
||||||
streamID, err := s.GetSockOptString(C.SRTO_STREAMID)
|
streamID, err := s.GetSockOptString(C.SRTO_STREAMID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user