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

Add transcoder package with text transcoder

This commit is contained in:
Alexandre Iooss 2020-10-17 19:40:37 +02:00
parent 1469bd8759
commit 23b6eeaa05
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
8 changed files with 112 additions and 67 deletions

View File

@ -13,6 +13,8 @@ import (
"gitlab.crans.org/nounous/ghostream/stream/srt" "gitlab.crans.org/nounous/ghostream/stream/srt"
"gitlab.crans.org/nounous/ghostream/stream/telnet" "gitlab.crans.org/nounous/ghostream/stream/telnet"
"gitlab.crans.org/nounous/ghostream/stream/webrtc" "gitlab.crans.org/nounous/ghostream/stream/webrtc"
"gitlab.crans.org/nounous/ghostream/transcoder"
"gitlab.crans.org/nounous/ghostream/transcoder/text"
"gitlab.crans.org/nounous/ghostream/web" "gitlab.crans.org/nounous/ghostream/web"
) )
@ -54,9 +56,14 @@ func New() *Config {
Telnet: telnet.Options{ Telnet: telnet.Options{
Enabled: false, Enabled: false,
ListenAddress: ":8023", ListenAddress: ":8023",
},
Transcoder: transcoder.Options{
Text: text.Options{
Enabled: false,
Width: 80, Width: 80,
Height: 45, Height: 45,
Delay: 50, Framerate: 20,
},
}, },
Web: web.Options{ Web: web.Options{
Enabled: true, Enabled: true,

View File

@ -15,6 +15,7 @@ import (
"gitlab.crans.org/nounous/ghostream/stream/srt" "gitlab.crans.org/nounous/ghostream/stream/srt"
"gitlab.crans.org/nounous/ghostream/stream/telnet" "gitlab.crans.org/nounous/ghostream/stream/telnet"
"gitlab.crans.org/nounous/ghostream/stream/webrtc" "gitlab.crans.org/nounous/ghostream/stream/webrtc"
"gitlab.crans.org/nounous/ghostream/transcoder"
"gitlab.crans.org/nounous/ghostream/web" "gitlab.crans.org/nounous/ghostream/web"
) )
@ -48,7 +49,8 @@ func main() {
streams := make(map[string]*stream.Stream) streams := make(map[string]*stream.Stream)
// Start routines // Start routines
go forwarding.Serve(streams, cfg.Forwarding) go transcoder.Init(streams, &cfg.Transcoder)
go forwarding.Serve(streams, &cfg.Forwarding)
go monitoring.Serve(&cfg.Monitoring) go monitoring.Serve(&cfg.Monitoring)
go srt.Serve(streams, authBackend, &cfg.Srt) go srt.Serve(streams, authBackend, &cfg.Srt)
go telnet.Serve(streams, &cfg.Telnet) go telnet.Serve(streams, &cfg.Telnet)

View File

@ -9,7 +9,7 @@ import (
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/stream"
) )
func handleViewer(s net.Conn, streams map[string]*stream.Stream, textStreams map[string]*[]byte, cfg *Options) { func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
// Prompt user about stream name // Prompt user about stream name
if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil { if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil {
log.Printf("Error while writing to TCP socket: %s", err) log.Printf("Error while writing to TCP socket: %s", err)
@ -23,7 +23,7 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, textStreams map
s.Close() s.Close()
return return
} }
name := strings.TrimSpace(string(buff[:n])) name := strings.TrimSpace(string(buff[:n])) + "@text"
if len(name) < 1 { if len(name) < 1 {
// Too short, exit // Too short, exit
s.Close() s.Close()
@ -45,7 +45,9 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, textStreams map
} }
// Register new client // Register new client
log.Printf("New Telnet viewer for stream %s", name) log.Printf("New Telnet viewer for stream '%s'", name)
c := make(chan []byte, 128)
st.Register(c)
st.IncrementClientCount() st.IncrementClientCount()
// Hide terminal cursor // Hide terminal cursor
@ -55,28 +57,23 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, textStreams map
return return
} }
// Send stream // Receive data and send them
for { for data := range c {
text, ok := textStreams[name] if len(data) < 1 {
if !ok { log.Print("Remove Telnet viewer because of end of stream")
log.Println("Stream is not converted to text, kicking Telnet viewer")
if _, err := s.Write([]byte("This stream cannot be opened.\n")); err != nil {
log.Printf("Error while writing to TCP socket: %s", err)
}
break break
} }
// Send text to client // Send data
n, err := s.Write(*text) _, err := s.Write(data)
if err != nil || n == 0 { if err != nil {
log.Printf("Error while sending TCP data: %s", err) log.Printf("Remove Telnet viewer because of sending error, %s", err)
break break
} }
time.Sleep(time.Duration(cfg.Delay) * time.Millisecond)
} }
// Close connection // Close output
s.Close() st.Unregister(c)
st.DecrementClientCount() st.DecrementClientCount()
s.Close()
} }

View File

@ -4,7 +4,6 @@ package telnet
import ( import (
"log" "log"
"net" "net"
"time"
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/stream"
) )
@ -13,9 +12,6 @@ import (
type Options struct { type Options struct {
Enabled bool Enabled bool
ListenAddress string ListenAddress string
Width int
Height int
Delay int
} }
// Serve Telnet server // Serve Telnet server
@ -25,10 +21,6 @@ func Serve(streams map[string]*stream.Stream, cfg *Options) {
return return
} }
// Start conversion routine
textStreams := make(map[string]*[]byte)
go autoStartConversion(streams, textStreams, cfg)
// Start TCP server // Start TCP server
listener, err := net.Listen("tcp", cfg.ListenAddress) listener, err := net.Listen("tcp", cfg.ListenAddress)
if err != nil { if err != nil {
@ -44,27 +36,6 @@ func Serve(streams map[string]*stream.Stream, cfg *Options) {
continue continue
} }
go handleViewer(s, streams, textStreams, cfg) go handleViewer(s, streams, cfg)
}
}
// Convertion routine listen to existing stream and start text conversions
func autoStartConversion(streams map[string]*stream.Stream, textStreams map[string]*[]byte, cfg *Options) {
for {
for name, stream := range streams {
textStream, ok := textStreams[name]
if ok {
// Everything is fine
continue
}
// Start conversion
log.Printf("Starting text conversion of %s", name)
// FIXME that is not how to use a pointer
textStream = &[]byte{}
textStreams[name] = textStream
go streamToTextStream(stream, textStream, cfg)
}
time.Sleep(time.Second)
} }
} }

View File

@ -18,9 +18,6 @@ func TestTelnetOutput(t *testing.T) {
cfg := Options{ cfg := Options{
Enabled: true, Enabled: true,
ListenAddress: "127.0.0.1:8023", ListenAddress: "127.0.0.1:8023",
Width: 80,
Height: 45,
Delay: 50,
} }
go Serve(streams, &cfg) go Serve(streams, &cfg)

View File

@ -1,4 +1,5 @@
package telnet // Package text transcode a video to text
package text
import ( import (
"bufio" "bufio"
@ -7,20 +8,70 @@ import (
"io" "io"
"log" "log"
"os/exec" "os/exec"
"strings"
"time"
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/stream"
) )
// Convert rawvideo to ANSI text // Options holds text package configuration
func streamToTextStream(stream *stream.Stream, text *[]byte, cfg *Options) { type Options struct {
// Start ffmpeg Enabled bool
video := make(chan []byte) Width int
stream.Register(video) Height int
_, rawvideo, err := startFFmpeg(video, cfg) Framerate int
if err != nil {
log.Printf("Error while starting ffmpeg: %s", err)
} }
// Init text transcoder
func Init(streams map[string]*stream.Stream, 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
}
// Check that the transcoded stream does not already exist
name := sourceName + "@text"
_, ok := streams[name]
if ok {
// Stream is already transcoded
continue
}
// Start conversion
log.Printf("Starting text 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)
}
}
// Convert video to ANSI text
func transcode(input, output *stream.Stream, cfg *Options) {
// Start ffmpeg to transcode video to rawvideo
videoInput := make(chan []byte)
input.Register(videoInput)
ffmpeg, rawvideo, err := startFFmpeg(videoInput, cfg)
if err != nil {
log.Printf("Error while starting ffmpeg: %s", err)
return
}
// Transcode rawvideo to ANSI text
pixelBuff := make([]byte, cfg.Width*cfg.Height) pixelBuff := make([]byte, cfg.Width*cfg.Height)
textBuff := bytes.Buffer{} textBuff := bytes.Buffer{}
for { for {
@ -55,13 +106,16 @@ func streamToTextStream(stream *stream.Stream, text *[]byte, cfg *Options) {
} }
textBuff.WriteString("\033[49m") textBuff.WriteString("\033[49m")
*text = textBuff.Bytes() output.Broadcast <- textBuff.Bytes()
} }
// Stop transcode
ffmpeg.Process.Kill()
} }
// Start a ffmpeg instance to convert stream into rawvideo // Start a ffmpeg instance to convert stream into rawvideo
func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, error) { func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, error) {
bitrate := fmt.Sprintf("%dk", cfg.Width*cfg.Height/cfg.Delay) bitrate := fmt.Sprintf("%dk", cfg.Width*cfg.Height*cfg.Framerate)
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0", ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
"-an", "-vf", fmt.Sprintf("scale=%dx%d", cfg.Width, cfg.Height), "-an", "-vf", fmt.Sprintf("scale=%dx%d", cfg.Width, cfg.Height),
"-b:v", bitrate, "-minrate", bitrate, "-maxrate", bitrate, "-bufsize", bitrate, "-b:v", bitrate, "-minrate", bitrate, "-maxrate", bitrate, "-bufsize", bitrate,

View File

@ -0,0 +1 @@
package text

16
transcoder/transcoder.go Normal file
View File

@ -0,0 +1,16 @@
package transcoder
import (
"gitlab.crans.org/nounous/ghostream/stream"
"gitlab.crans.org/nounous/ghostream/transcoder/text"
)
// Options holds text package configuration
type Options struct {
Text text.Options
}
// Init all transcoders
func Init(streams map[string]*stream.Stream, cfg *Options) {
go text.Init(streams, &cfg.Text)
}