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

Update package srt with Quality structure

This commit is contained in:
Alexandre Iooss 2020-10-19 19:40:36 +02:00
parent c317d91b8d
commit 069b2155be
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
6 changed files with 61 additions and 31 deletions

View File

@ -25,13 +25,25 @@ func newStream() (s *Stream) {
return s return s
} }
// Close stream.
func (s *Stream) Close() {
for quality := range s.qualities {
s.DeleteQuality(quality)
}
}
// CreateQuality creates a new quality associated with this stream. // CreateQuality creates a new quality associated with this stream.
func (s *Stream) CreateQuality(name string) (quality *Quality) { func (s *Stream) CreateQuality(name string) (quality *Quality, err error) {
// If quality already exist, fail
if _, ok := s.qualities[name]; ok {
return nil, errors.New("quality already exists")
}
s.lockQualities.Lock() s.lockQualities.Lock()
quality = newQuality() quality = newQuality()
s.qualities[name] = quality s.qualities[name] = quality
s.lockQualities.Unlock() s.lockQualities.Unlock()
return quality return quality, nil
} }
// DeleteQuality removes a stream quality. // DeleteQuality removes a stream quality.

View File

@ -91,6 +91,7 @@ func (l *Streams) Delete(name string) {
// Make sure we did not already delete this stream // Make sure we did not already delete this stream
l.lockStreams.Lock() l.lockStreams.Lock()
if _, ok := l.streams[name]; ok { if _, ok := l.streams[name]; ok {
l.streams[name].Close()
delete(l.streams, name) delete(l.streams, name)
} }
l.lockStreams.Unlock() l.lockStreams.Unlock()

View File

@ -22,7 +22,10 @@ func TestWithOneStream(t *testing.T) {
} }
// Create a quality // Create a quality
quality := stream.CreateQuality("source") quality, err := stream.CreateQuality("source")
if err != nil {
t.Errorf("Failed to create quality")
}
// Register one output // Register one output
output := make(chan []byte, 64) output := make(chan []byte, 64)

View File

@ -5,21 +5,26 @@ import (
"log" "log"
"github.com/haivision/srtgo" "github.com/haivision/srtgo"
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/messaging"
) )
func handleStreamer(socket *srtgo.SrtSocket, streams map[string]*stream.Stream, name string) { func handleStreamer(socket *srtgo.SrtSocket, streams *messaging.Streams, name string) {
// Check stream does not exist // Create stream
if _, ok := streams[name]; ok { stream, err := streams.Create(name)
log.Print("Stream already exists, refusing new streamer") if err != nil {
log.Printf("Error on stream creating: %s", err)
socket.Close() socket.Close()
return return
} }
// Create stream // Create source quality
log.Printf("New SRT streamer for stream %s", name) q, err := stream.CreateQuality("source")
st := stream.New() if err != nil {
streams[name] = st log.Printf("Error on quality creating: %s", err)
socket.Close()
return
}
log.Printf("New SRT streamer for stream '%s' quality 'source'", name)
// Read RTP packets forever and send them to the WebRTC Client // Read RTP packets forever and send them to the WebRTC Client
for { for {
@ -42,29 +47,38 @@ func handleStreamer(socket *srtgo.SrtSocket, streams map[string]*stream.Stream,
// Send raw data to other streams // Send raw data to other streams
buff = buff[:n] buff = buff[:n]
st.Broadcast <- buff q.Broadcast <- buff
} }
// Close stream // Close stream
st.Close() streams.Delete(name)
socket.Close() socket.Close()
delete(streams, name)
} }
func handleViewer(s *srtgo.SrtSocket, streams map[string]*stream.Stream, name string) { func handleViewer(socket *srtgo.SrtSocket, streams *messaging.Streams, name string) {
log.Printf("New SRT viewer for stream %s", name)
// Get requested stream // Get requested stream
st, ok := streams[name] stream, err := streams.Get(name)
if !ok { if err != nil {
log.Println("Stream does not exist, refusing new viewer") log.Printf("Failed to get stream: %s", err)
socket.Close()
return 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()
return
}
log.Printf("New SRT viewer for stream %s quality %s", name, qualityName)
// Register new output // Register new output
c := make(chan []byte, 1024) c := make(chan []byte, 1024)
st.Register(c) q.Register(c)
st.IncrementClientCount() stream.IncrementClientCount()
// Receive data and send them // Receive data and send them
for data := range c { for data := range c {
@ -74,7 +88,7 @@ func handleViewer(s *srtgo.SrtSocket, streams map[string]*stream.Stream, name st
} }
// Send data // Send data
_, err := s.Write(data, 1000) _, err := socket.Write(data, 1000)
if err != nil { if err != nil {
log.Printf("Remove SRT viewer because of sending error, %s", err) log.Printf("Remove SRT viewer because of sending error, %s", err)
break break
@ -82,7 +96,7 @@ func handleViewer(s *srtgo.SrtSocket, streams map[string]*stream.Stream, name st
} }
// Close output // Close output
st.Unregister(c) q.Unregister(c)
st.DecrementClientCount() stream.DecrementClientCount()
s.Close() socket.Close()
} }

View File

@ -12,7 +12,7 @@ import (
"github.com/haivision/srtgo" "github.com/haivision/srtgo"
"gitlab.crans.org/nounous/ghostream/auth" "gitlab.crans.org/nounous/ghostream/auth"
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/messaging"
) )
// Options holds web package configuration // Options holds web package configuration
@ -39,7 +39,7 @@ func splitHostPort(hostport string) (string, uint16, error) {
} }
// Serve SRT server // Serve SRT server
func Serve(streams map[string]*stream.Stream, authBackend auth.Backend, cfg *Options) { func Serve(streams *messaging.Streams, authBackend auth.Backend, cfg *Options) {
if !cfg.Enabled { if !cfg.Enabled {
// SRT is not enabled, ignore // SRT is not enabled, ignore
return return

View File

@ -6,7 +6,7 @@ import (
"testing" "testing"
"time" "time"
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/messaging"
) )
// TestSplitHostPort Try to split a host like 127.0.0.1:1234 in host, port (127.0.0.1, 1234à // TestSplitHostPort Try to split a host like 127.0.0.1:1234 in host, port (127.0.0.1, 1234à
@ -58,7 +58,7 @@ func TestServeSRT(t *testing.T) {
} }
// Init streams messaging and SRT server // Init streams messaging and SRT server
streams := make(map[string]*stream.Stream) streams := messaging.New()
go Serve(streams, nil, &Options{Enabled: true, ListenAddress: ":9711", MaxClients: 2}) go Serve(streams, nil, &Options{Enabled: true, ListenAddress: ":9711", MaxClients: 2})
ffmpeg := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error", ffmpeg := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error",