mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2025-06-27 20:22:16 +02:00
Add transcoder package with text transcoder
This commit is contained in:
@ -1,103 +0,0 @@
|
||||
package telnet
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os/exec"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
)
|
||||
|
||||
// Convert rawvideo to ANSI text
|
||||
func streamToTextStream(stream *stream.Stream, text *[]byte, cfg *Options) {
|
||||
// Start ffmpeg
|
||||
video := make(chan []byte)
|
||||
stream.Register(video)
|
||||
_, rawvideo, err := startFFmpeg(video, cfg)
|
||||
if err != nil {
|
||||
log.Printf("Error while starting ffmpeg: %s", err)
|
||||
}
|
||||
|
||||
pixelBuff := make([]byte, cfg.Width*cfg.Height)
|
||||
textBuff := bytes.Buffer{}
|
||||
for {
|
||||
n, err := (*rawvideo).Read(pixelBuff)
|
||||
if err != nil {
|
||||
log.Printf("An error occurred while reading input: %s", err)
|
||||
break
|
||||
}
|
||||
if n == 0 {
|
||||
// Stream is finished
|
||||
break
|
||||
}
|
||||
|
||||
// Header
|
||||
textBuff.Reset()
|
||||
textBuff.Grow((40*cfg.Width+6)*cfg.Height + 47)
|
||||
for i := 0; i < 42; i++ {
|
||||
textBuff.WriteByte('\n')
|
||||
}
|
||||
|
||||
// Convert image to ASCII
|
||||
for i, pixel := range pixelBuff {
|
||||
if i%cfg.Width == 0 {
|
||||
// New line
|
||||
textBuff.WriteString("\033[49m\n")
|
||||
}
|
||||
|
||||
// Print two times the character to make a square
|
||||
text := fmt.Sprintf("\033[48;2;%d;%d;%dm ", pixel, pixel, pixel)
|
||||
textBuff.WriteString(text)
|
||||
textBuff.WriteString(text)
|
||||
}
|
||||
textBuff.WriteString("\033[49m")
|
||||
|
||||
*text = textBuff.Bytes()
|
||||
}
|
||||
}
|
||||
|
||||
// Start a ffmpeg instance to convert stream into rawvideo
|
||||
func startFFmpeg(in <-chan []byte, cfg *Options) (*exec.Cmd, *io.ReadCloser, error) {
|
||||
bitrate := fmt.Sprintf("%dk", cfg.Width*cfg.Height/cfg.Delay)
|
||||
ffmpegArgs := []string{"-hide_banner", "-loglevel", "error", "-i", "pipe:0",
|
||||
"-an", "-vf", fmt.Sprintf("scale=%dx%d", cfg.Width, cfg.Height),
|
||||
"-b:v", bitrate, "-minrate", bitrate, "-maxrate", bitrate, "-bufsize", bitrate,
|
||||
"-q", "42", "-pix_fmt", "gray", "-f", "rawvideo", "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("[TELNET 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, nil
|
||||
}
|
@ -9,7 +9,7 @@ import (
|
||||
"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
|
||||
if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil {
|
||||
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()
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(string(buff[:n]))
|
||||
name := strings.TrimSpace(string(buff[:n])) + "@text"
|
||||
if len(name) < 1 {
|
||||
// Too short, exit
|
||||
s.Close()
|
||||
@ -45,7 +45,9 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, textStreams map
|
||||
}
|
||||
|
||||
// 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()
|
||||
|
||||
// Hide terminal cursor
|
||||
@ -55,28 +57,23 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, textStreams map
|
||||
return
|
||||
}
|
||||
|
||||
// Send stream
|
||||
for {
|
||||
text, ok := textStreams[name]
|
||||
if !ok {
|
||||
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)
|
||||
}
|
||||
// Receive data and send them
|
||||
for data := range c {
|
||||
if len(data) < 1 {
|
||||
log.Print("Remove Telnet viewer because of end of stream")
|
||||
break
|
||||
}
|
||||
|
||||
// Send text to client
|
||||
n, err := s.Write(*text)
|
||||
if err != nil || n == 0 {
|
||||
log.Printf("Error while sending TCP data: %s", err)
|
||||
// Send data
|
||||
_, err := s.Write(data)
|
||||
if err != nil {
|
||||
log.Printf("Remove Telnet viewer because of sending error, %s", err)
|
||||
break
|
||||
}
|
||||
|
||||
time.Sleep(time.Duration(cfg.Delay) * time.Millisecond)
|
||||
}
|
||||
|
||||
// Close connection
|
||||
s.Close()
|
||||
// Close output
|
||||
st.Unregister(c)
|
||||
st.DecrementClientCount()
|
||||
s.Close()
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ package telnet
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"gitlab.crans.org/nounous/ghostream/stream"
|
||||
)
|
||||
@ -13,9 +12,6 @@ import (
|
||||
type Options struct {
|
||||
Enabled bool
|
||||
ListenAddress string
|
||||
Width int
|
||||
Height int
|
||||
Delay int
|
||||
}
|
||||
|
||||
// Serve Telnet server
|
||||
@ -25,10 +21,6 @@ func Serve(streams map[string]*stream.Stream, cfg *Options) {
|
||||
return
|
||||
}
|
||||
|
||||
// Start conversion routine
|
||||
textStreams := make(map[string]*[]byte)
|
||||
go autoStartConversion(streams, textStreams, cfg)
|
||||
|
||||
// Start TCP server
|
||||
listener, err := net.Listen("tcp", cfg.ListenAddress)
|
||||
if err != nil {
|
||||
@ -44,27 +36,6 @@ func Serve(streams map[string]*stream.Stream, cfg *Options) {
|
||||
continue
|
||||
}
|
||||
|
||||
go handleViewer(s, streams, textStreams, 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)
|
||||
go handleViewer(s, streams, cfg)
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,6 @@ func TestTelnetOutput(t *testing.T) {
|
||||
cfg := Options{
|
||||
Enabled: true,
|
||||
ListenAddress: "127.0.0.1:8023",
|
||||
Width: 80,
|
||||
Height: 45,
|
||||
Delay: 50,
|
||||
}
|
||||
go Serve(streams, &cfg)
|
||||
|
||||
|
Reference in New Issue
Block a user