1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-23 01:32:19 +00:00
ghostream/stream/srt/handler.go

103 lines
2.2 KiB
Go
Raw Normal View History

2020-10-09 20:36:02 +00:00
// Package srt serves a SRT server
2020-10-04 12:24:00 +00:00
package srt
import (
"log"
"github.com/haivision/srtgo"
"gitlab.crans.org/nounous/ghostream/messaging"
2020-10-04 12:24:00 +00:00
)
func handleStreamer(socket *srtgo.SrtSocket, streams *messaging.Streams, name string) {
// Create stream
stream, err := streams.Create(name)
if err != nil {
log.Printf("Error on stream creating: %s", err)
2020-10-17 10:26:24 +00:00
socket.Close()
return
}
// Create source quality
q, err := stream.CreateQuality("source")
if err != nil {
log.Printf("Error on quality creating: %s", err)
socket.Close()
return
}
log.Printf("New SRT streamer for stream '%s' quality 'source'", name)
2020-10-04 12:24:00 +00:00
// Read RTP packets forever and send them to the WebRTC Client
for {
2020-10-18 13:13:13 +00:00
// Create a new buffer
// UDP packet cannot be larger than MTU (1500)
buff := make([]byte, 1500)
2020-10-04 12:27:16 +00:00
// 5s timeout
2020-10-17 10:26:24 +00:00
n, err := socket.Read(buff, 5000)
2020-10-04 12:24:00 +00:00
if err != nil {
2020-10-14 19:36:12 +00:00
log.Println("Error occurred while reading SRT socket:", err)
2020-10-04 12:24:00 +00:00
break
}
if n == 0 {
// End of stream
log.Printf("Received no bytes, stopping stream.")
break
}
2020-10-17 10:26:24 +00:00
// Send raw data to other streams
2020-10-18 13:13:13 +00:00
buff = buff[:n]
q.Broadcast <- buff
2020-10-04 12:24:00 +00:00
}
2020-10-17 10:26:24 +00:00
// Close stream
streams.Delete(name)
2020-10-17 10:26:24 +00:00
socket.Close()
2020-10-04 12:24:00 +00:00
}
func handleViewer(socket *srtgo.SrtSocket, streams *messaging.Streams, name string) {
2020-10-17 10:26:24 +00:00
// Get requested stream
stream, err := streams.Get(name)
if err != nil {
log.Printf("Failed to get stream: %s", err)
socket.Close()
return
}
// Get requested quality
// FIXME: make qualities available
qualityName := "source"
q, err := stream.GetQuality(qualityName)
if err != nil {
log.Printf("Failed to get quality: %s", err)
socket.Close()
2020-10-17 10:26:24 +00:00
return
}
log.Printf("New SRT viewer for stream %s quality %s", name, qualityName)
2020-10-17 10:26:24 +00:00
// Register new output
2020-10-18 09:06:54 +00:00
c := make(chan []byte, 1024)
q.Register(c)
stream.IncrementClientCount()
2020-10-17 10:26:24 +00:00
// Receive data and send them
2020-10-17 11:43:16 +00:00
for data := range c {
2020-10-17 10:26:24 +00:00
if len(data) < 1 {
log.Print("Remove SRT viewer because of end of stream")
break
}
2020-10-17 11:43:16 +00:00
// Send data
_, err := socket.Write(data, 1000)
2020-10-17 10:26:24 +00:00
if err != nil {
log.Printf("Remove SRT viewer because of sending error, %s", err)
break
2020-10-04 12:24:00 +00:00
}
}
2020-10-17 10:26:24 +00:00
// Close output
q.Unregister(c)
stream.DecrementClientCount()
socket.Close()
2020-10-04 12:24:00 +00:00
}