mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2025-07-06 14:53:58 +02:00
Compare commits
15 Commits
webrtc-mes
...
91c4e9d14d
Author | SHA1 | Date | |
---|---|---|---|
91c4e9d14d | |||
5ea8a0913b | |||
ac2f87e936 | |||
cd63c93dce | |||
4727b2bf64 | |||
e1f83a32df | |||
e848d92a1a | |||
d263f743f7 | |||
d03d4fed40 | |||
34200afaed | |||
340d0447a8 | |||
069b2155be | |||
c317d91b8d | |||
bb589a71ce | |||
f825d3d513 |
@ -38,13 +38,17 @@ auth:
|
||||
## Stream forwarding ##
|
||||
# Forward an incoming stream to other servers
|
||||
# The URL can be anything FFMpeg can accept as an stream output
|
||||
# If a file is specified, the name may contains %Y, %m, %d, %H, %M or %S
|
||||
# that will be replaced by the current date information.
|
||||
forwarding:
|
||||
# By default nothing is forwarded.
|
||||
#
|
||||
# This example forwards a stream named "demo" to Twitch and YouTube,
|
||||
# and save the record in a timestamped-file,
|
||||
#demo:
|
||||
# - rtmp://live-cdg.twitch.tv/app/STREAM_KEY
|
||||
# - rtmp://a.rtmp.youtube.com/live2/STREAM_KEY
|
||||
# - /home/ghostream/lives/%name/live-%Y-%m-%d-%H-%M-%S.flv
|
||||
|
||||
## Prometheus monitoring ##
|
||||
# Expose a monitoring endpoint for Prometheus
|
||||
|
1
go.mod
1
go.mod
@ -3,7 +3,6 @@ module gitlab.crans.org/nounous/ghostream
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035
|
||||
github.com/go-ldap/ldap/v3 v3.2.3
|
||||
github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a
|
||||
github.com/markbates/pkger v0.17.1
|
||||
|
2
go.sum
2
go.sum
@ -7,8 +7,6 @@ dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBr
|
||||
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
|
||||
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
|
||||
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
|
||||
github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035 h1:QZb1aMKxiYdGGieyIDmXuw9I9YcGWGViTrpQ6vcZX7Q=
|
||||
github.com/3d0c/gmf v0.0.0-20200614092945-e58d8d5a6035/go.mod h1:0QMRcUq2JsDECeAq7bj4h79k7XbhtTsrPUQf6G7qfPs=
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28=
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
|
@ -2,7 +2,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"gitlab.crans.org/nounous/ghostream/transcoder/audio"
|
||||
"net"
|
||||
|
||||
"github.com/sherifabdlnaby/configuro"
|
||||
@ -60,10 +59,6 @@ func New() *Config {
|
||||
ListenAddress: ":8023",
|
||||
},
|
||||
Transcoder: transcoder.Options{
|
||||
Audio: audio.Options{
|
||||
Enabled: true,
|
||||
Bitrate: 160,
|
||||
},
|
||||
Text: text.Options{
|
||||
Enabled: false,
|
||||
Width: 80,
|
||||
|
15
main.go
15
main.go
@ -10,7 +10,7 @@ import (
|
||||
"gitlab.crans.org/nounous/ghostream/auth"
|
||||
"gitlab.crans.org/nounous/ghostream/internal/config"
|
||||
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
"gitlab.crans.org/nounous/ghostream/stream/forwarding"
|
||||
"gitlab.crans.org/nounous/ghostream/stream/srt"
|
||||
"gitlab.crans.org/nounous/ghostream/stream/telnet"
|
||||
@ -38,15 +38,8 @@ func main() {
|
||||
defer authBackend.Close()
|
||||
}
|
||||
|
||||
// WebRTC session description channels
|
||||
remoteSdpChan := make(chan struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
})
|
||||
localSdpChan := make(chan webrtc.SessionDescription)
|
||||
|
||||
// Init streams messaging
|
||||
streams := make(map[string]*stream.Stream)
|
||||
streams := messaging.New()
|
||||
|
||||
// Start routines
|
||||
go transcoder.Init(streams, &cfg.Transcoder)
|
||||
@ -54,8 +47,8 @@ func main() {
|
||||
go monitoring.Serve(&cfg.Monitoring)
|
||||
go srt.Serve(streams, authBackend, &cfg.Srt)
|
||||
go telnet.Serve(streams, &cfg.Telnet)
|
||||
go web.Serve(streams, remoteSdpChan, localSdpChan, &cfg.Web)
|
||||
go webrtc.Serve(streams, remoteSdpChan, localSdpChan, &cfg.WebRTC)
|
||||
go web.Serve(streams, &cfg.Web)
|
||||
go webrtc.Serve(streams, &cfg.WebRTC)
|
||||
|
||||
// Wait for routines
|
||||
select {}
|
||||
|
89
messaging/quality.go
Normal file
89
messaging/quality.go
Normal file
@ -0,0 +1,89 @@
|
||||
// Package messaging defines a structure to communication between inputs and outputs
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
// Quality holds a specific stream quality.
|
||||
// It makes packages able to subscribe to an incoming stream.
|
||||
type Quality struct {
|
||||
// Incoming data come from this channel
|
||||
Broadcast chan<- []byte
|
||||
|
||||
// Incoming data will be outputted to all those outputs.
|
||||
// Use a map to be able to delete an item.
|
||||
outputs map[chan []byte]struct{}
|
||||
|
||||
// Mutex to lock outputs map
|
||||
lockOutputs sync.Mutex
|
||||
|
||||
// WebRTC session descriptor exchange.
|
||||
// When new client connects, a SDP arrives on WebRtcRemoteSdp,
|
||||
// then webrtc package answers on WebRtcLocalSdp.
|
||||
WebRtcLocalSdp chan webrtc.SessionDescription
|
||||
WebRtcRemoteSdp chan webrtc.SessionDescription
|
||||
}
|
||||
|
||||
func newQuality() (q *Quality) {
|
||||
q = &Quality{}
|
||||
broadcast := make(chan []byte, 1024)
|
||||
q.Broadcast = broadcast
|
||||
q.outputs = make(map[chan []byte]struct{})
|
||||
q.WebRtcLocalSdp = make(chan webrtc.SessionDescription, 1)
|
||||
q.WebRtcRemoteSdp = make(chan webrtc.SessionDescription, 1)
|
||||
go q.run(broadcast)
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *Quality) run(broadcast <-chan []byte) {
|
||||
for msg := range broadcast {
|
||||
q.lockOutputs.Lock()
|
||||
for output := range q.outputs {
|
||||
select {
|
||||
case output <- msg:
|
||||
default:
|
||||
// If full, do a ring buffer
|
||||
// Check that output is not of size zero
|
||||
if len(output) > 1 {
|
||||
<-output
|
||||
}
|
||||
}
|
||||
}
|
||||
q.lockOutputs.Unlock()
|
||||
}
|
||||
|
||||
// Incoming chan has been closed, close all outputs
|
||||
q.lockOutputs.Lock()
|
||||
for ch := range q.outputs {
|
||||
delete(q.outputs, ch)
|
||||
close(ch)
|
||||
}
|
||||
q.lockOutputs.Unlock()
|
||||
}
|
||||
|
||||
// Close the incoming chan, this will also delete all outputs.
|
||||
func (q *Quality) Close() {
|
||||
close(q.Broadcast)
|
||||
}
|
||||
|
||||
// Register a new output on a stream.
|
||||
func (q *Quality) Register(output chan []byte) {
|
||||
q.lockOutputs.Lock()
|
||||
q.outputs[output] = struct{}{}
|
||||
q.lockOutputs.Unlock()
|
||||
}
|
||||
|
||||
// Unregister removes an output.
|
||||
func (q *Quality) Unregister(output chan []byte) {
|
||||
// Make sure we did not already close this output
|
||||
q.lockOutputs.Lock()
|
||||
_, ok := q.outputs[output]
|
||||
if ok {
|
||||
delete(q.outputs, output)
|
||||
close(output)
|
||||
}
|
||||
defer q.lockOutputs.Unlock()
|
||||
}
|
84
messaging/stream.go
Normal file
84
messaging/stream.go
Normal file
@ -0,0 +1,84 @@
|
||||
// Package messaging defines a structure to communication between inputs and outputs
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Stream makes packages able to subscribe to an incoming stream
|
||||
type Stream struct {
|
||||
// Different qualities of this stream
|
||||
qualities map[string]*Quality
|
||||
|
||||
// Mutex to lock outputs map
|
||||
lockQualities sync.Mutex
|
||||
|
||||
// Count clients for statistics
|
||||
nbClients int
|
||||
}
|
||||
|
||||
func newStream() (s *Stream) {
|
||||
s = &Stream{}
|
||||
s.qualities = make(map[string]*Quality)
|
||||
s.nbClients = 0
|
||||
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.
|
||||
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()
|
||||
quality = newQuality()
|
||||
s.qualities[name] = quality
|
||||
s.lockQualities.Unlock()
|
||||
return quality, nil
|
||||
}
|
||||
|
||||
// DeleteQuality removes a stream quality.
|
||||
func (s *Stream) DeleteQuality(name string) {
|
||||
// Make sure we did not already close this output
|
||||
s.lockQualities.Lock()
|
||||
if _, ok := s.qualities[name]; ok {
|
||||
s.qualities[name].Close()
|
||||
delete(s.qualities, name)
|
||||
}
|
||||
s.lockQualities.Unlock()
|
||||
}
|
||||
|
||||
// GetQuality gets a specific stream quality.
|
||||
func (s *Stream) GetQuality(name string) (quality *Quality, err error) {
|
||||
s.lockQualities.Lock()
|
||||
quality, ok := s.qualities[name]
|
||||
s.lockQualities.Unlock()
|
||||
if !ok {
|
||||
return nil, errors.New("quality does not exist")
|
||||
}
|
||||
return quality, nil
|
||||
}
|
||||
|
||||
// ClientCount returns the number of clients.
|
||||
func (s *Stream) ClientCount() int {
|
||||
return s.nbClients
|
||||
}
|
||||
|
||||
// IncrementClientCount increments the number of clients.
|
||||
func (s *Stream) IncrementClientCount() {
|
||||
s.nbClients++
|
||||
}
|
||||
|
||||
// DecrementClientCount decrements the number of clients.
|
||||
func (s *Stream) DecrementClientCount() {
|
||||
s.nbClients--
|
||||
}
|
98
messaging/streams.go
Normal file
98
messaging/streams.go
Normal file
@ -0,0 +1,98 @@
|
||||
// Package messaging defines a structure to communication between inputs and outputs
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Streams hold all application streams.
|
||||
type Streams struct {
|
||||
// Associate each stream name to the stream
|
||||
streams map[string]*Stream
|
||||
|
||||
// Mutex to lock streams
|
||||
lockStreams sync.Mutex
|
||||
|
||||
// Subscribers get notified when a new stream is created
|
||||
// Use a map to be able to delete a subscriber
|
||||
eventSubscribers map[chan string]struct{}
|
||||
|
||||
// Mutex to lock eventSubscribers
|
||||
lockSubscribers sync.Mutex
|
||||
}
|
||||
|
||||
// New creates a new stream list.
|
||||
func New() (l *Streams) {
|
||||
l = &Streams{}
|
||||
l.streams = make(map[string]*Stream)
|
||||
l.eventSubscribers = make(map[chan string]struct{})
|
||||
return l
|
||||
}
|
||||
|
||||
// Subscribe to get notified on new stream.
|
||||
func (l *Streams) Subscribe(output chan string) {
|
||||
l.lockSubscribers.Lock()
|
||||
l.eventSubscribers[output] = struct{}{}
|
||||
l.lockSubscribers.Unlock()
|
||||
}
|
||||
|
||||
// Unsubscribe to no longer get notified on new stream.
|
||||
func (l *Streams) Unsubscribe(output chan string) {
|
||||
// Make sure we did not already delete this subscriber
|
||||
l.lockSubscribers.Lock()
|
||||
if _, ok := l.eventSubscribers[output]; ok {
|
||||
delete(l.eventSubscribers, output)
|
||||
}
|
||||
l.lockSubscribers.Unlock()
|
||||
}
|
||||
|
||||
// Create a new stream.
|
||||
func (l *Streams) Create(name string) (s *Stream, err error) {
|
||||
// If stream already exist, fail
|
||||
if _, ok := l.streams[name]; ok {
|
||||
return nil, errors.New("stream already exists")
|
||||
}
|
||||
|
||||
// Create stream
|
||||
s = newStream()
|
||||
l.lockStreams.Lock()
|
||||
l.streams[name] = s
|
||||
l.lockStreams.Unlock()
|
||||
|
||||
// Notify
|
||||
l.lockSubscribers.Lock()
|
||||
for sub := range l.eventSubscribers {
|
||||
select {
|
||||
case sub <- name:
|
||||
default:
|
||||
log.Printf("Failed to announce stream '%s' to subscriber", name)
|
||||
}
|
||||
}
|
||||
l.lockSubscribers.Unlock()
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Get a stream.
|
||||
func (l *Streams) Get(name string) (s *Stream, err error) {
|
||||
// If stream does exist, return it
|
||||
l.lockStreams.Lock()
|
||||
s, ok := l.streams[name]
|
||||
l.lockStreams.Unlock()
|
||||
if !ok {
|
||||
return nil, errors.New("stream does not exist")
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Delete a stream.
|
||||
func (l *Streams) Delete(name string) {
|
||||
// Make sure we did not already delete this stream
|
||||
l.lockStreams.Lock()
|
||||
if _, ok := l.streams[name]; ok {
|
||||
l.streams[name].Close()
|
||||
delete(l.streams, name)
|
||||
}
|
||||
l.lockStreams.Unlock()
|
||||
}
|
55
messaging/streams_test.go
Normal file
55
messaging/streams_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
package messaging
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestWithOneStream(t *testing.T) {
|
||||
streams := New()
|
||||
|
||||
// Subscribe to new streams
|
||||
event := make(chan string, 8)
|
||||
streams.Subscribe(event)
|
||||
|
||||
// Create a stream
|
||||
stream, err := streams.Create("demo")
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create stream")
|
||||
}
|
||||
|
||||
// Check that we receive the creation event
|
||||
e := <-event
|
||||
if e != "demo" {
|
||||
t.Errorf("Message has wrong content: %s != demo", e)
|
||||
}
|
||||
|
||||
// Create a quality
|
||||
quality, err := stream.CreateQuality("source")
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create quality")
|
||||
}
|
||||
|
||||
// Register one output
|
||||
output := make(chan []byte, 64)
|
||||
quality.Register(output)
|
||||
stream.IncrementClientCount()
|
||||
|
||||
// Try to pass one message
|
||||
quality.Broadcast <- []byte("hello world")
|
||||
msg := <-output
|
||||
if string(msg) != "hello world" {
|
||||
t.Errorf("Message has wrong content: %s != hello world", msg)
|
||||
}
|
||||
|
||||
// Check client count
|
||||
if count := stream.ClientCount(); count != 1 {
|
||||
t.Errorf("Client counter returned %d, expected 1", count)
|
||||
}
|
||||
|
||||
// Unregister
|
||||
quality.Unregister(output)
|
||||
stream.DecrementClientCount()
|
||||
|
||||
// Check client count
|
||||
if count := stream.ClientCount(); count != 0 {
|
||||
t.Errorf("Client counter returned %d, expected 0", count)
|
||||
}
|
||||
}
|
@ -3,11 +3,13 @@ package forwarding
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// Options to configure the stream forwarding.
|
||||
@ -15,43 +17,65 @@ import (
|
||||
type Options map[string][]string
|
||||
|
||||
// Serve handles incoming packets from SRT and forward them to other external services
|
||||
func Serve(streams map[string]*stream.Stream, cfg Options) {
|
||||
func Serve(streams *messaging.Streams, cfg Options) {
|
||||
if len(cfg) < 1 {
|
||||
// No forwarding, ignore
|
||||
return
|
||||
}
|
||||
|
||||
// Subscribe to new stream event
|
||||
event := make(chan string, 8)
|
||||
streams.Subscribe(event)
|
||||
log.Printf("Stream forwarding initialized")
|
||||
for {
|
||||
for name, st := range streams {
|
||||
fwdCfg, ok := cfg[name]
|
||||
|
||||
// For each new stream
|
||||
for name := range event {
|
||||
streamCfg, ok := cfg[name]
|
||||
if !ok {
|
||||
// Not configured
|
||||
continue
|
||||
}
|
||||
|
||||
// Start forwarding
|
||||
log.Printf("Starting forwarding for '%s'", name)
|
||||
go forward(st, fwdCfg)
|
||||
// Get stream
|
||||
stream, err := streams.Get(name)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get stream '%s'", name)
|
||||
}
|
||||
|
||||
// Regulary pull stream list,
|
||||
// it may be better to tweak the messaging system
|
||||
// to get an event on a new stream.
|
||||
time.Sleep(time.Second)
|
||||
// Get specific quality
|
||||
// FIXME: make it possible to forward other qualities
|
||||
qualityName := "source"
|
||||
quality, err := stream.GetQuality(qualityName)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get quality '%s'", qualityName)
|
||||
}
|
||||
|
||||
// Start forwarding
|
||||
log.Printf("Starting forwarding for '%s' quality '%s'", name, qualityName)
|
||||
go forward(name, quality, streamCfg)
|
||||
}
|
||||
}
|
||||
|
||||
// Start a FFMPEG instance and redirect stream output to forwarded streams
|
||||
func forward(st *stream.Stream, fwdCfg []string) {
|
||||
func forward(streamName string, q *messaging.Quality, fwdCfg []string) {
|
||||
output := make(chan []byte, 1024)
|
||||
st.Register(output)
|
||||
q.Register(output)
|
||||
|
||||
// Launch FFMPEG instance
|
||||
params := []string{"-hide_banner", "-loglevel", "error", "-re", "-i", "pipe:0"}
|
||||
params := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0"}
|
||||
for _, url := range fwdCfg {
|
||||
// If the url should be date-formatted, replace special characters with the current time information
|
||||
now := time.Now()
|
||||
formattedURL := strings.ReplaceAll(url, "%Y", fmt.Sprintf("%04d", now.Year()))
|
||||
formattedURL = strings.ReplaceAll(formattedURL, "%m", fmt.Sprintf("%02d", now.Month()))
|
||||
formattedURL = strings.ReplaceAll(formattedURL, "%d", fmt.Sprintf("%02d", now.Day()))
|
||||
formattedURL = strings.ReplaceAll(formattedURL, "%H", fmt.Sprintf("%02d", now.Hour()))
|
||||
formattedURL = strings.ReplaceAll(formattedURL, "%M", fmt.Sprintf("%02d", now.Minute()))
|
||||
formattedURL = strings.ReplaceAll(formattedURL, "%S", fmt.Sprintf("%02d", now.Second()))
|
||||
formattedURL = strings.ReplaceAll(formattedURL, "%name", streamName)
|
||||
|
||||
params = append(params, "-f", "flv", "-preset", "ultrafast", "-tune", "zerolatency",
|
||||
"-c", "copy", url)
|
||||
"-c", "copy", formattedURL)
|
||||
}
|
||||
ffmpeg := exec.Command("ffmpeg", params...)
|
||||
|
||||
@ -77,14 +101,14 @@ func forward(st *stream.Stream, fwdCfg []string) {
|
||||
_ = input.Close()
|
||||
_ = errOutput.Close()
|
||||
_ = ffmpeg.Process.Kill()
|
||||
st.Unregister(output)
|
||||
q.Unregister(output)
|
||||
}()
|
||||
|
||||
// Log standard error output
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(errOutput)
|
||||
for scanner.Scan() {
|
||||
log.Printf("[FORWARDING FFMPEG] %s", scanner.Text())
|
||||
log.Printf("[FORWARDING FFMPEG %s] %s", streamName, scanner.Text())
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
"gitlab.crans.org/nounous/ghostream/stream/srt"
|
||||
)
|
||||
|
||||
@ -35,7 +35,7 @@ func TestForwardStream(t *testing.T) {
|
||||
cfg["demo"] = []string{"rtmp://127.0.0.1:1936/live/app"}
|
||||
|
||||
// Register forwarding stream list
|
||||
streams := make(map[string]*stream.Stream)
|
||||
streams := messaging.New()
|
||||
go Serve(streams, cfg)
|
||||
|
||||
// Serve SRT Server without authentification backend
|
||||
|
@ -1,99 +0,0 @@
|
||||
// Package stream defines a structure to communication between inputs and outputs
|
||||
package stream
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Stream makes packages able to subscribe to an incoming stream
|
||||
type Stream struct {
|
||||
// Incoming data come from this channel
|
||||
Broadcast chan<- []byte
|
||||
|
||||
// Use a map to be able to delete an item
|
||||
outputs map[chan []byte]struct{}
|
||||
|
||||
// Count clients for statistics
|
||||
nbClients int
|
||||
|
||||
// Mutex to lock outputs map
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
// New creates a new stream.
|
||||
func New() *Stream {
|
||||
s := &Stream{}
|
||||
broadcast := make(chan []byte, 1024)
|
||||
s.Broadcast = broadcast
|
||||
s.outputs = make(map[chan []byte]struct{})
|
||||
s.nbClients = 0
|
||||
go s.run(broadcast)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Stream) run(broadcast <-chan []byte) {
|
||||
for msg := range broadcast {
|
||||
s.lock.Lock()
|
||||
for output := range s.outputs {
|
||||
select {
|
||||
case output <- msg:
|
||||
default:
|
||||
// If full, do a ring buffer
|
||||
// Check that output is not of size zero
|
||||
if len(output) > 1 {
|
||||
<-output
|
||||
}
|
||||
}
|
||||
}
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
// Incoming chan has been closed, close all outputs
|
||||
s.lock.Lock()
|
||||
for ch := range s.outputs {
|
||||
delete(s.outputs, ch)
|
||||
close(ch)
|
||||
}
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
// Close the incoming chan, this will also delete all outputs
|
||||
func (s *Stream) Close() {
|
||||
close(s.Broadcast)
|
||||
}
|
||||
|
||||
// Register a new output on a stream.
|
||||
func (s *Stream) Register(output chan []byte) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.outputs[output] = struct{}{}
|
||||
}
|
||||
|
||||
// Unregister removes an output.
|
||||
// If hidden in true, then do not count this client.
|
||||
func (s *Stream) Unregister(output chan []byte) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
// Make sure we did not already close this output
|
||||
_, ok := s.outputs[output]
|
||||
if ok {
|
||||
delete(s.outputs, output)
|
||||
close(output)
|
||||
}
|
||||
}
|
||||
|
||||
// ClientCount returns the number of clients
|
||||
func (s *Stream) ClientCount() int {
|
||||
return s.nbClients
|
||||
}
|
||||
|
||||
// IncrementClientCount increments the number of clients
|
||||
func (s *Stream) IncrementClientCount() {
|
||||
s.nbClients++
|
||||
}
|
||||
|
||||
// DecrementClientCount decrements the number of clients
|
||||
func (s *Stream) DecrementClientCount() {
|
||||
s.nbClients--
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWithoutOutputs(t *testing.T) {
|
||||
stream := New()
|
||||
defer stream.Close()
|
||||
stream.Broadcast <- []byte("hello world")
|
||||
}
|
||||
|
||||
func TestWithOneOutput(t *testing.T) {
|
||||
stream := New()
|
||||
defer stream.Close()
|
||||
|
||||
// Register one output
|
||||
output := make(chan []byte, 64)
|
||||
stream.Register(output)
|
||||
stream.IncrementClientCount()
|
||||
|
||||
// Try to pass one message
|
||||
stream.Broadcast <- []byte("hello world")
|
||||
msg := <-output
|
||||
if string(msg) != "hello world" {
|
||||
t.Errorf("Message has wrong content: %s != hello world", msg)
|
||||
}
|
||||
|
||||
// Check client count
|
||||
if count := stream.ClientCount(); count != 1 {
|
||||
t.Errorf("Client counter returned %d, expected 1", count)
|
||||
}
|
||||
|
||||
// Unregister
|
||||
stream.Unregister(output)
|
||||
stream.DecrementClientCount()
|
||||
|
||||
// Check client count
|
||||
if count := stream.ClientCount(); count != 0 {
|
||||
t.Errorf("Client counter returned %d, expected 0", count)
|
||||
}
|
||||
}
|
@ -5,21 +5,26 @@ import (
|
||||
"log"
|
||||
|
||||
"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) {
|
||||
// Check stream does not exist
|
||||
if _, ok := streams[name]; ok {
|
||||
log.Print("Stream already exists, refusing new streamer")
|
||||
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)
|
||||
socket.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// Create stream
|
||||
log.Printf("New SRT streamer for stream %s", name)
|
||||
st := stream.New()
|
||||
streams[name] = st
|
||||
// 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)
|
||||
|
||||
// Read RTP packets forever and send them to the WebRTC Client
|
||||
for {
|
||||
@ -42,29 +47,38 @@ func handleStreamer(socket *srtgo.SrtSocket, streams map[string]*stream.Stream,
|
||||
|
||||
// Send raw data to other streams
|
||||
buff = buff[:n]
|
||||
st.Broadcast <- buff
|
||||
q.Broadcast <- buff
|
||||
}
|
||||
|
||||
// Close stream
|
||||
st.Close()
|
||||
streams.Delete(name)
|
||||
socket.Close()
|
||||
delete(streams, name)
|
||||
}
|
||||
|
||||
func handleViewer(s *srtgo.SrtSocket, streams map[string]*stream.Stream, name string) {
|
||||
log.Printf("New SRT viewer for stream %s", name)
|
||||
|
||||
func handleViewer(socket *srtgo.SrtSocket, streams *messaging.Streams, name string) {
|
||||
// Get requested stream
|
||||
st, ok := streams[name]
|
||||
if !ok {
|
||||
log.Println("Stream does not exist, refusing new viewer")
|
||||
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()
|
||||
return
|
||||
}
|
||||
log.Printf("New SRT viewer for stream %s quality %s", name, qualityName)
|
||||
|
||||
// Register new output
|
||||
c := make(chan []byte, 1024)
|
||||
st.Register(c)
|
||||
st.IncrementClientCount()
|
||||
q.Register(c)
|
||||
stream.IncrementClientCount()
|
||||
|
||||
// Receive data and send them
|
||||
for data := range c {
|
||||
@ -74,7 +88,7 @@ func handleViewer(s *srtgo.SrtSocket, streams map[string]*stream.Stream, name st
|
||||
}
|
||||
|
||||
// Send data
|
||||
_, err := s.Write(data, 1000)
|
||||
_, err := socket.Write(data, 1000)
|
||||
if err != nil {
|
||||
log.Printf("Remove SRT viewer because of sending error, %s", err)
|
||||
break
|
||||
@ -82,7 +96,7 @@ func handleViewer(s *srtgo.SrtSocket, streams map[string]*stream.Stream, name st
|
||||
}
|
||||
|
||||
// Close output
|
||||
st.Unregister(c)
|
||||
st.DecrementClientCount()
|
||||
s.Close()
|
||||
q.Unregister(c)
|
||||
stream.DecrementClientCount()
|
||||
socket.Close()
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
|
||||
"github.com/haivision/srtgo"
|
||||
"gitlab.crans.org/nounous/ghostream/auth"
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// Options holds web package configuration
|
||||
@ -39,7 +39,7 @@ func splitHostPort(hostport string) (string, uint16, error) {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// SRT is not enabled, ignore
|
||||
return
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
"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à
|
||||
@ -58,7 +58,7 @@ func TestServeSRT(t *testing.T) {
|
||||
}
|
||||
|
||||
// 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})
|
||||
|
||||
ffmpeg := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error",
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// Options holds telnet package configuration
|
||||
@ -17,7 +17,7 @@ type Options struct {
|
||||
}
|
||||
|
||||
// Serve Telnet server
|
||||
func Serve(streams map[string]*stream.Stream, cfg *Options) {
|
||||
func Serve(streams *messaging.Streams, cfg *Options) {
|
||||
if !cfg.Enabled {
|
||||
// Telnet is not enabled, ignore
|
||||
return
|
||||
@ -32,17 +32,17 @@ func Serve(streams map[string]*stream.Stream, cfg *Options) {
|
||||
|
||||
// Handle each new client
|
||||
for {
|
||||
s, err := listener.Accept()
|
||||
socket, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Printf("Error while accepting TCP socket: %s", s)
|
||||
log.Printf("Error while accepting TCP socket: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
go handleViewer(s, streams, cfg)
|
||||
go handleViewer(socket, streams, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
|
||||
func handleViewer(s net.Conn, streams *messaging.Streams, cfg *Options) {
|
||||
// Prompt user about stream name
|
||||
if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil {
|
||||
log.Printf("Error while writing to TCP socket: %s", err)
|
||||
@ -56,7 +56,7 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
|
||||
s.Close()
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(string(buff[:n])) + "@text"
|
||||
name := strings.TrimSpace(string(buff[:n]))
|
||||
if len(name) < 1 {
|
||||
// Too short, exit
|
||||
s.Close()
|
||||
@ -67,9 +67,9 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// Get requested stream
|
||||
st, ok := streams[name]
|
||||
if !ok {
|
||||
log.Println("Stream does not exist, kicking new Telnet viewer")
|
||||
stream, err := streams.Get(name)
|
||||
if err != nil {
|
||||
log.Printf("Kicking new Telnet viewer: %s", err)
|
||||
if _, err := s.Write([]byte("This stream is inactive.\n")); err != nil {
|
||||
log.Printf("Error while writing to TCP socket: %s", err)
|
||||
}
|
||||
@ -77,11 +77,23 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get requested quality
|
||||
qualityName := "text"
|
||||
q, err := stream.GetQuality(qualityName)
|
||||
if err != nil {
|
||||
log.Printf("Kicking new Telnet viewer: %s", err)
|
||||
if _, err := s.Write([]byte("This stream is not converted to text.\n")); err != nil {
|
||||
log.Printf("Error while writing to TCP socket: %s", err)
|
||||
}
|
||||
s.Close()
|
||||
return
|
||||
}
|
||||
log.Printf("New Telnet viewer for stream %s quality %s", name, qualityName)
|
||||
|
||||
// Register new client
|
||||
log.Printf("New Telnet viewer for stream '%s'", name)
|
||||
c := make(chan []byte, 128)
|
||||
st.Register(c)
|
||||
st.IncrementClientCount()
|
||||
q.Register(c)
|
||||
stream.IncrementClientCount()
|
||||
|
||||
// Hide terminal cursor
|
||||
if _, err = s.Write([]byte("\033[?25l")); err != nil {
|
||||
@ -106,7 +118,7 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
|
||||
}
|
||||
|
||||
// Close output
|
||||
st.Unregister(c)
|
||||
st.DecrementClientCount()
|
||||
q.Unregister(c)
|
||||
stream.DecrementClientCount()
|
||||
s.Close()
|
||||
}
|
||||
|
@ -3,13 +3,13 @@ package telnet
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// TestTelnetOutput creates a TCP client that connects to the server and get one image.
|
||||
func TestTelnetOutput(t *testing.T) {
|
||||
// Try to start Telnet server while it is disabled
|
||||
streams := make(map[string]*stream.Stream)
|
||||
streams := messaging.New()
|
||||
go Serve(streams, &Options{Enabled: false})
|
||||
|
||||
// FIXME test connect
|
||||
|
@ -2,153 +2,22 @@
|
||||
package webrtc
|
||||
|
||||
import (
|
||||
"github.com/3d0c/gmf"
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"bufio"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
"os/exec"
|
||||
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
var (
|
||||
activeStream map[string]struct{}
|
||||
)
|
||||
|
||||
func autoIngest(streams map[string]*stream.Stream) {
|
||||
// Regulary check existing streams
|
||||
activeStream = make(map[string]struct{})
|
||||
for {
|
||||
for name, st := range streams {
|
||||
if strings.Contains(name, "@") {
|
||||
// Not a source stream, pass
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := activeStream[name]; ok {
|
||||
// Stream is already ingested
|
||||
continue
|
||||
}
|
||||
|
||||
// Start ingestion
|
||||
log.Printf("Starting webrtc for '%s'", name)
|
||||
// FIXME Ensure that the audio stream exist, but that's poop code
|
||||
time.Sleep(time.Second)
|
||||
go ingest(name, st, streams[name+"@audio"])
|
||||
}
|
||||
|
||||
// Regulary pull stream list,
|
||||
// it may be better to tweak the messaging system
|
||||
// to get an event on a new stream.
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func ingest(name string, input *stream.Stream, audio *stream.Stream) {
|
||||
func ingest(name string, q *messaging.Quality) {
|
||||
// Register to get stream
|
||||
videoInput := make(chan []byte, 1024)
|
||||
input.Register(videoInput)
|
||||
audioInput := make(chan []byte, 1024)
|
||||
audio.Register(audioInput)
|
||||
activeStream[name] = struct{}{}
|
||||
q.Register(videoInput)
|
||||
|
||||
inputCtx := gmf.NewCtx()
|
||||
avioInputCtx, _ := gmf.NewAVIOContext(inputCtx, &gmf.AVIOHandlers{ReadPacket: func() ([]byte, int) {
|
||||
data := <-audioInput
|
||||
return data, len(data)
|
||||
}})
|
||||
log.Println("Open input")
|
||||
inputCtx.SetPb(avioInputCtx).OpenInput("")
|
||||
log.Println("Opened")
|
||||
defer inputCtx.CloseInput()
|
||||
defer avioInputCtx.Release()
|
||||
|
||||
if audioTracks[name] == nil {
|
||||
audioTracks[name] = make([]*webrtc.Track, 0)
|
||||
}
|
||||
|
||||
udpListener, _ := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1234})
|
||||
|
||||
outputCtx, _ := gmf.NewOutputCtxWithFormatName("rtp://127.0.0.1:1234", "rtp")
|
||||
avioOutputCtx, _ := gmf.NewAVIOContext(outputCtx, &gmf.AVIOHandlers{WritePacket: func(data []byte) int {
|
||||
n := len(data)
|
||||
log.Printf("Read %d bytes", n)
|
||||
|
||||
return n
|
||||
}})
|
||||
// FIXME DON'T RAN AN UDP LISTENER, PLIZ GET DIRECTLY UDP PACKETS, WHY IS IT SO COMPLICATED????
|
||||
// outputCtx.SetPb(avioOutputCtx)
|
||||
defer outputCtx.CloseOutput()
|
||||
defer avioOutputCtx.Release()
|
||||
|
||||
log.Printf("%d streams", inputCtx.StreamsCnt())
|
||||
|
||||
for i := 0; i < inputCtx.StreamsCnt(); i++ {
|
||||
srcStream, err := inputCtx.GetStream(i)
|
||||
if err != nil {
|
||||
log.Println("GetStream error")
|
||||
}
|
||||
|
||||
outputCtx.AddStreamWithCodeCtx(srcStream.CodecCtx())
|
||||
}
|
||||
outputCtx.Dump()
|
||||
|
||||
if err := outputCtx.WriteHeader(); err != nil {
|
||||
log.Printf("Unable to write RTP header: %s", err)
|
||||
}
|
||||
|
||||
// Receive audio data
|
||||
go func() {
|
||||
buff := make([]byte, 1500)
|
||||
for {
|
||||
n, _ := udpListener.Read(buff)
|
||||
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
packet := &rtp.Packet{}
|
||||
if err := packet.Unmarshal(buff[:n]); err != nil {
|
||||
log.Printf("Failed to unmarshal RTP srtPacket: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if audioTracks[name] == nil {
|
||||
audioTracks[name] = make([]*webrtc.Track, 0)
|
||||
}
|
||||
|
||||
// Write RTP srtPacket to all audio tracks
|
||||
// Adapt payload and SSRC to match destination
|
||||
for _, audioTrack := range audioTracks[name] {
|
||||
packet.Header.PayloadType = audioTrack.PayloadType()
|
||||
packet.Header.SSRC = audioTrack.SSRC()
|
||||
if writeErr := audioTrack.WriteRTP(packet); writeErr != nil {
|
||||
log.Printf("Failed to write to audio track: %s", writeErr)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
first := false
|
||||
for packet := range inputCtx.GetNewPackets() {
|
||||
if first { //if read from rtsp ,the first packets is wrong.
|
||||
if err := outputCtx.WritePacketNoBuffer(packet); err != nil {
|
||||
log.Printf("Error while writing packet: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
first = true
|
||||
|
||||
packet.Free()
|
||||
}
|
||||
|
||||
select {}
|
||||
|
||||
// TODO Register to all substreams and make RTP packets. Don't transcode in this package.
|
||||
|
||||
/* // Open a UDP Listener for RTP Packets on port 5004
|
||||
// Open a UDP Listener for RTP Packets on port 5004
|
||||
videoListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5004})
|
||||
if err != nil {
|
||||
log.Printf("Faited to open UDP listener %s", err)
|
||||
@ -242,11 +111,11 @@ func ingest(name string, input *stream.Stream, audio *stream.Stream) {
|
||||
}
|
||||
if err = audioListener.Close(); err != nil {
|
||||
log.Printf("Faited to close UDP listener: %s", err)
|
||||
}*/
|
||||
delete(activeStream, name)
|
||||
}
|
||||
q.Unregister(videoInput)
|
||||
}
|
||||
|
||||
/* func startFFmpeg(in <-chan []byte) (ffmpeg *exec.Cmd, err error) {
|
||||
func startFFmpeg(in <-chan []byte) (ffmpeg *exec.Cmd, err error) {
|
||||
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
|
||||
"-an", "-vcodec", "libvpx", "-crf", "10", "-cpu-used", "5", "-b:v", "6000k", "-maxrate", "8000k", "-bufsize", "12000k", // TODO Change bitrate when changing quality
|
||||
"-qmin", "10", "-qmax", "42", "-threads", "4", "-deadline", "1", "-error-resilient", "1",
|
||||
@ -287,4 +156,4 @@ func ingest(name string, input *stream.Stream, audio *stream.Stream) {
|
||||
// Start process
|
||||
err = ffmpeg.Start()
|
||||
return ffmpeg, err
|
||||
} */
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// Options holds web package configuration
|
||||
@ -45,13 +45,10 @@ func GetNumberConnectedSessions(streamID string) int {
|
||||
|
||||
// newPeerHandler is called when server receive a new session description
|
||||
// this initiates a WebRTC connection and return server description
|
||||
func newPeerHandler(localSdpChan chan webrtc.SessionDescription, remoteSdp struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
}, cfg *Options) {
|
||||
func newPeerHandler(name string, localSdpChan chan webrtc.SessionDescription, remoteSdp webrtc.SessionDescription, cfg *Options) {
|
||||
// Create media engine using client SDP
|
||||
mediaEngine := webrtc.MediaEngine{}
|
||||
if err := mediaEngine.PopulateFromSDP(remoteSdp.RemoteDescription); err != nil {
|
||||
if err := mediaEngine.PopulateFromSDP(remoteSdp); err != nil {
|
||||
log.Println("Failed to create new media engine", err)
|
||||
localSdpChan <- webrtc.SessionDescription{}
|
||||
return
|
||||
@ -106,13 +103,13 @@ func newPeerHandler(localSdpChan chan webrtc.SessionDescription, remoteSdp struc
|
||||
}
|
||||
|
||||
// Set the remote SessionDescription
|
||||
if err = peerConnection.SetRemoteDescription(remoteSdp.RemoteDescription); err != nil {
|
||||
if err = peerConnection.SetRemoteDescription(remoteSdp); err != nil {
|
||||
log.Println("Failed to set remote description", err)
|
||||
localSdpChan <- webrtc.SessionDescription{}
|
||||
return
|
||||
}
|
||||
|
||||
streamID := remoteSdp.StreamID
|
||||
streamID := name
|
||||
split := strings.SplitN(streamID, "@", 2)
|
||||
streamID = split[0]
|
||||
quality := "source"
|
||||
@ -182,10 +179,7 @@ func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecNa
|
||||
}
|
||||
|
||||
// Serve WebRTC media streaming server
|
||||
func Serve(streams map[string]*stream.Stream, remoteSdpChan chan struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
}, localSdpChan chan webrtc.SessionDescription, cfg *Options) {
|
||||
func Serve(streams *messaging.Streams, cfg *Options) {
|
||||
if !cfg.Enabled {
|
||||
// WebRTC is not enabled, ignore
|
||||
return
|
||||
@ -193,17 +187,42 @@ func Serve(streams map[string]*stream.Stream, remoteSdpChan chan struct {
|
||||
|
||||
log.Printf("WebRTC server using UDP from port %d to %d", cfg.MinPortUDP, cfg.MaxPortUDP)
|
||||
|
||||
// Allocate memory
|
||||
// WebRTC ingested tracks
|
||||
videoTracks = make(map[string][]*webrtc.Track)
|
||||
audioTracks = make(map[string][]*webrtc.Track)
|
||||
|
||||
// Ingest data
|
||||
go autoIngest(streams)
|
||||
// Subscribe to new stream event
|
||||
event := make(chan string, 8)
|
||||
streams.Subscribe(event)
|
||||
|
||||
// For each new stream
|
||||
for name := range event {
|
||||
// Get stream
|
||||
stream, err := streams.Get(name)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get stream '%s'", name)
|
||||
}
|
||||
|
||||
// Get specific quality
|
||||
// FIXME: make it possible to forward other qualities
|
||||
qualityName := "source"
|
||||
quality, err := stream.GetQuality(qualityName)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get quality '%s'", qualityName)
|
||||
}
|
||||
|
||||
// Start forwarding
|
||||
log.Printf("Starting webrtc for '%s' quality '%s'", name, qualityName)
|
||||
go ingest(name, quality)
|
||||
go listenSdp(name, quality.WebRtcLocalSdp, quality.WebRtcRemoteSdp, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func listenSdp(name string, localSdp, remoteSdp chan webrtc.SessionDescription, cfg *Options) {
|
||||
// Handle new connections
|
||||
for {
|
||||
// Wait for incoming session description
|
||||
// then send the local description to browser
|
||||
newPeerHandler(localSdpChan, <-remoteSdpChan, cfg)
|
||||
newPeerHandler(name, localSdp, <-remoteSdp, cfg)
|
||||
}
|
||||
}
|
||||
|
@ -5,24 +5,19 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
func TestServe(t *testing.T) {
|
||||
// Init streams messaging and WebRTC server
|
||||
streams := make(map[string]*stream.Stream)
|
||||
remoteSdpChan := make(chan struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
})
|
||||
localSdpChan := make(chan webrtc.SessionDescription)
|
||||
streams := messaging.New()
|
||||
cfg := Options{
|
||||
Enabled: true,
|
||||
MinPortUDP: 10000,
|
||||
MaxPortUDP: 10005,
|
||||
STUNServers: []string{"stun:stun.l.google.com:19302"},
|
||||
}
|
||||
go Serve(streams, remoteSdpChan, localSdpChan, &cfg)
|
||||
go Serve(streams, &cfg)
|
||||
|
||||
// New client connection
|
||||
mediaEngine := webrtc.MediaEngine{}
|
||||
@ -58,12 +53,6 @@ func TestServe(t *testing.T) {
|
||||
peerConnection.SetLocalDescription(offer)
|
||||
<-gatherComplete
|
||||
|
||||
// Send offer to server
|
||||
remoteSdpChan <- struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
}{"demo", *peerConnection.LocalDescription()}
|
||||
_ = <-localSdpChan
|
||||
|
||||
// FIXME: Send offer to server
|
||||
// FIXME: verify connection did work
|
||||
}
|
||||
|
@ -1,153 +0,0 @@
|
||||
// Package audio transcode a stream to filter the audio
|
||||
package audio
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/3d0c/gmf"
|
||||
"io"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
)
|
||||
|
||||
// Options holds audio package configuration
|
||||
type Options struct {
|
||||
Enabled bool
|
||||
Bitrate int
|
||||
}
|
||||
|
||||
// Init text transcoder
|
||||
func Init(streams map[string]*stream.Stream, cfg *Options) {
|
||||
if !cfg.Enabled {
|
||||
// Audio transcode is not enabled, ignore
|
||||
return
|
||||
}
|
||||
|
||||
// Regulary check existing streams
|
||||
for {
|
||||
for sourceName, sourceStream := range streams {
|
||||
if strings.Contains(sourceName, "@") {
|
||||
// Not a source stream, pass
|
||||
continue
|
||||
}
|
||||
|
||||
// Check that the transcoded stream does not already exist
|
||||
name := sourceName + "@audio"
|
||||
_, ok := streams[name]
|
||||
if ok {
|
||||
// Stream is already transcoded
|
||||
continue
|
||||
}
|
||||
|
||||
// Start conversion
|
||||
log.Printf("Starting audio transcode '%s'", name)
|
||||
st := stream.New()
|
||||
streams[name] = st
|
||||
|
||||
go transcode(sourceStream, st, cfg)
|
||||
}
|
||||
|
||||
// Regulary pull stream list,
|
||||
// it may be better to tweak the messaging system
|
||||
// to get an event on a new stream.
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// Extract audio from stream
|
||||
func transcode(input, output *stream.Stream, cfg *Options) {
|
||||
// Start ffmpeg to transcode video to audio
|
||||
videoInput := make(chan []byte, 1024)
|
||||
input.Register(videoInput)
|
||||
ffmpeg, audio, err := startFFmpeg(videoInput, cfg)
|
||||
if err != nil {
|
||||
log.Printf("Error while starting ffmpeg: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
dataBuff := make([]byte, gmf.IO_BUFFER_SIZE) // UDP MTU
|
||||
for {
|
||||
n, err := (*audio).Read(dataBuff)
|
||||
if err != nil {
|
||||
log.Printf("An error occurred while reading input: %s", err)
|
||||
break
|
||||
}
|
||||
if n == 0 {
|
||||
// Stream is finished
|
||||
break
|
||||
}
|
||||
|
||||
output.Broadcast <- dataBuff[:n]
|
||||
}
|
||||
|
||||
// Stop transcode
|
||||
_ = ffmpeg.Process.Kill()
|
||||
_ = (*audio).Close()
|
||||
}
|
||||
|
||||
// Start a ffmpeg instance to convert stream into audio
|
||||
func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, error) {
|
||||
// TODO in a future release: remove FFMPEG dependency and transcode directly using the libopus API
|
||||
|
||||
// FIXME It seems impossible to get a RTP Packet from standard output.
|
||||
// We need to find a clean solution, without waiting on UDP listeners.
|
||||
// FIXME We should also not build RTP packets here.
|
||||
|
||||
/* port := 0
|
||||
var udpListener *net.UDPConn
|
||||
var err error
|
||||
for {
|
||||
port = rand.Intn(65535)
|
||||
udpListener, err = net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port})
|
||||
if err != nil {
|
||||
if strings.Contains(fmt.Sprintf("%s", err), "address already in use") {
|
||||
continue
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
break
|
||||
}*/
|
||||
|
||||
bitrate := fmt.Sprintf("%dk", cfg.Bitrate)
|
||||
// Use copy audio codec, assume for now that libopus is used by the streamer
|
||||
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
|
||||
"-vn", "-c:a", "copy", "-b:a", bitrate, "-f", "mpegts", "pipe:1"}
|
||||
ffmpeg := exec.Command("ffmpeg", ffmpegArgs...)
|
||||
|
||||
// Handle errors output
|
||||
errOutput, err := ffmpeg.StderrPipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(errOutput)
|
||||
for scanner.Scan() {
|
||||
log.Printf("[AUDIO FFMPEG %s] %s", "demo", scanner.Text())
|
||||
}
|
||||
}()
|
||||
|
||||
// Handle text output
|
||||
output, err := ffmpeg.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Handle stream input
|
||||
input, err := ffmpeg.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
go func() {
|
||||
for data := range in {
|
||||
_, _ = input.Write(data)
|
||||
}
|
||||
}()
|
||||
|
||||
// Start process
|
||||
err = ffmpeg.Start()
|
||||
return ffmpeg, &output, err
|
||||
}
|
@ -1 +0,0 @@
|
||||
package audio
|
@ -8,10 +8,8 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// Options holds text package configuration
|
||||
@ -23,45 +21,46 @@ type Options struct {
|
||||
}
|
||||
|
||||
// Init text transcoder
|
||||
func Init(streams map[string]*stream.Stream, cfg *Options) {
|
||||
func Init(streams *messaging.Streams, cfg *Options) {
|
||||
if !cfg.Enabled {
|
||||
// Text transcode is not enabled, ignore
|
||||
return
|
||||
}
|
||||
|
||||
// Regulary check existing streams
|
||||
for {
|
||||
for sourceName, sourceStream := range streams {
|
||||
if strings.Contains(sourceName, "@") {
|
||||
// Not a source stream, pass
|
||||
continue
|
||||
// Subscribe to new stream event
|
||||
event := make(chan string, 8)
|
||||
streams.Subscribe(event)
|
||||
|
||||
// For each new stream
|
||||
for name := range event {
|
||||
// Get stream
|
||||
stream, err := streams.Get(name)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get stream '%s'", name)
|
||||
}
|
||||
|
||||
// Check that the transcoded stream does not already exist
|
||||
name := sourceName + "@text"
|
||||
_, ok := streams[name]
|
||||
if ok {
|
||||
// Stream is already transcoded
|
||||
continue
|
||||
// Get specific quality
|
||||
// FIXME: make it possible to forward other qualities
|
||||
qualityName := "source"
|
||||
quality, err := stream.GetQuality(qualityName)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get quality '%s'", qualityName)
|
||||
}
|
||||
|
||||
// Start conversion
|
||||
log.Printf("Starting text transcode '%s'", name)
|
||||
st := stream.New()
|
||||
streams[name] = st
|
||||
|
||||
go transcode(sourceStream, st, cfg)
|
||||
// Create new text quality
|
||||
outputQuality, err := stream.CreateQuality("text")
|
||||
if err != nil {
|
||||
log.Printf("Failed to create quality 'text': %s", err)
|
||||
}
|
||||
|
||||
// Regulary pull stream list,
|
||||
// it may be better to tweak the messaging system
|
||||
// to get an event on a new stream.
|
||||
time.Sleep(time.Second)
|
||||
// Start forwarding
|
||||
log.Printf("Starting text transcoder for '%s' quality '%s'", name, qualityName)
|
||||
go transcode(quality, outputQuality, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert video to ANSI text
|
||||
func transcode(input, output *stream.Stream, cfg *Options) {
|
||||
func transcode(input, output *messaging.Quality, cfg *Options) {
|
||||
// Start ffmpeg to transcode video to rawvideo
|
||||
videoInput := make(chan []byte, 1024)
|
||||
input.Register(videoInput)
|
||||
|
@ -2,19 +2,16 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/transcoder/audio"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
"gitlab.crans.org/nounous/ghostream/transcoder/text"
|
||||
)
|
||||
|
||||
// Options holds text package configuration
|
||||
type Options struct {
|
||||
Text text.Options
|
||||
Audio audio.Options
|
||||
}
|
||||
|
||||
// Init all transcoders
|
||||
func Init(streams map[string]*stream.Stream, cfg *Options) {
|
||||
func Init(streams *messaging.Streams, cfg *Options) {
|
||||
go text.Init(streams, &cfg.Text)
|
||||
go audio.Init(streams, &cfg.Audio)
|
||||
}
|
||||
|
@ -51,12 +51,27 @@ func viewerPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get requested stream
|
||||
stream, err := streams.Get(path)
|
||||
if err != nil {
|
||||
http.Error(w, "Stream not found", http.StatusNotFound)
|
||||
log.Printf("Stream not found: %s", path)
|
||||
return
|
||||
}
|
||||
|
||||
// Get requested quality
|
||||
// FIXME: extract quality from request
|
||||
qualityName := "source"
|
||||
q, err := stream.GetQuality(qualityName)
|
||||
if err != nil {
|
||||
http.Error(w, "Quality not found", http.StatusNotFound)
|
||||
log.Printf("Quality not found: %s", qualityName)
|
||||
return
|
||||
}
|
||||
|
||||
// Exchange session descriptions with WebRTC stream server
|
||||
remoteSdpChan <- struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
}{StreamID: path, RemoteDescription: remoteDescription}
|
||||
localDescription := <-localSdpChan
|
||||
q.WebRtcRemoteSdp <- remoteDescription
|
||||
localDescription := <-q.WebRtcLocalSdp
|
||||
|
||||
// Send server description as JSON
|
||||
jsonDesc, err := json.Marshal(localDescription)
|
||||
@ -153,19 +168,16 @@ func statisticsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.SplitN(strings.Replace(r.URL.Path[7:], "/", "", -1), "@", 2)[0]
|
||||
userCount := 0
|
||||
|
||||
// Get all substreams
|
||||
for _, outputType := range []string{"", "@720p", "@480p", "@360p", "@240p", "@text"} {
|
||||
// Get requested stream
|
||||
stream, ok := streams[name+outputType]
|
||||
if ok {
|
||||
// Get number of output channels
|
||||
userCount += stream.ClientCount()
|
||||
}
|
||||
stream, err := streams.Get(name)
|
||||
if err == nil {
|
||||
userCount = stream.ClientCount()
|
||||
userCount += webrtc.GetNumberConnectedSessions(name)
|
||||
}
|
||||
|
||||
// Display connected users statistics
|
||||
enc := json.NewEncoder(w)
|
||||
err := enc.Encode(struct{ ConnectedViewers int }{userCount})
|
||||
err = enc.Encode(struct{ ConnectedViewers int }{userCount})
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to generate JSON.", http.StatusInternalServerError)
|
||||
log.Printf("Failed to generate JSON: %s", err)
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
func TestViewerPageGET(t *testing.T) {
|
||||
@ -12,6 +14,9 @@ func TestViewerPageGET(t *testing.T) {
|
||||
t.Errorf("Failed to load templates: %v", err)
|
||||
}
|
||||
|
||||
// Init streams messaging
|
||||
streams = messaging.New()
|
||||
|
||||
cfg = &Options{}
|
||||
|
||||
// Test GET request
|
||||
|
@ -2,7 +2,7 @@
|
||||
video {
|
||||
display: block;
|
||||
flex-grow: 1;
|
||||
width: 100%;
|
||||
max-width: 100%;
|
||||
|
||||
/* Black borders around video */
|
||||
background-color: #000;
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<title>{{if .Path}}{{.Path}} - {{end}}{{.Cfg.Name}}</title>
|
||||
<link rel="stylesheet" href="static/css/style.css">
|
||||
<link rel="stylesheet" href="static/css/player.css">
|
||||
|
19
web/web.go
19
web/web.go
@ -10,8 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/markbates/pkger"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// Options holds web package configuration
|
||||
@ -33,18 +32,11 @@ type Options struct {
|
||||
var (
|
||||
cfg *Options
|
||||
|
||||
// WebRTC session description channels
|
||||
remoteSdpChan chan struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
}
|
||||
localSdpChan chan webrtc.SessionDescription
|
||||
|
||||
// Preload templates
|
||||
templates *template.Template
|
||||
|
||||
// Streams to get statistics
|
||||
streams map[string]*stream.Stream
|
||||
streams *messaging.Streams
|
||||
)
|
||||
|
||||
// Load templates with pkger
|
||||
@ -78,13 +70,8 @@ func loadTemplates() error {
|
||||
}
|
||||
|
||||
// Serve HTTP server
|
||||
func Serve(s map[string]*stream.Stream, rSdpChan chan struct {
|
||||
StreamID string
|
||||
RemoteDescription webrtc.SessionDescription
|
||||
}, lSdpChan chan webrtc.SessionDescription, c *Options) {
|
||||
func Serve(s *messaging.Streams, c *Options) {
|
||||
streams = s
|
||||
remoteSdpChan = rSdpChan
|
||||
localSdpChan = lSdpChan
|
||||
cfg = c
|
||||
|
||||
if !cfg.Enabled {
|
||||
|
@ -5,16 +5,16 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
"gitlab.crans.org/nounous/ghostream/messaging"
|
||||
)
|
||||
|
||||
// TestHTTPServe tries to serve a real HTTP server and load some pages
|
||||
func TestHTTPServe(t *testing.T) {
|
||||
// Init streams messaging
|
||||
streams := make(map[string]*stream.Stream)
|
||||
streams := messaging.New()
|
||||
|
||||
// Create a disabled web server
|
||||
go Serve(streams, nil, nil, &Options{Enabled: false, ListenAddress: "127.0.0.1:8081"})
|
||||
go Serve(streams, &Options{Enabled: false, ListenAddress: "127.0.0.1:8081"})
|
||||
|
||||
// Sleep 500ms to ensure that the web server is running, to avoid fails because the request came too early
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
@ -26,7 +26,7 @@ func TestHTTPServe(t *testing.T) {
|
||||
}
|
||||
|
||||
// Now let's really start the web server
|
||||
go Serve(streams, nil, nil, &Options{Enabled: true, ListenAddress: "127.0.0.1:8081"})
|
||||
go Serve(streams, &Options{Enabled: true, ListenAddress: "127.0.0.1:8081"})
|
||||
|
||||
// Sleep 500ms to ensure that the web server is running, to avoid fails because the request came too early
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
Reference in New Issue
Block a user