Interact with telnet to select the stream id

This commit is contained in:
Yohann D'ANELLO 2020-10-13 00:52:08 +02:00
parent 95fcedf2fa
commit 727865f444
2 changed files with 59 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import (
"io"
"log"
"net"
"strings"
"time"
)
@ -12,7 +13,7 @@ var (
// TODO Config should not be exported
// Cfg contains the different options of the telnet package, see below
Cfg *Options
currentMessage *string
currentMessage map[string]string
)
// Options holds telnet package configuration
@ -32,14 +33,14 @@ func Serve(config *Options) {
return
}
currentMessage = make(map[string]string)
listener, err := net.Listen("tcp", Cfg.ListenAddress)
if err != nil {
log.Printf("Error while listening to the address %s: %s", Cfg.ListenAddress, err)
return
}
currentMessage = new(string)
go func() {
for {
s, err := listener.Accept()
@ -47,9 +48,51 @@ func Serve(config *Options) {
log.Printf("Error while accepting TCP socket: %s", s)
continue
}
go func(s net.Conn) {
streamID := ""
// Request for stream ID
for {
n, err := s.Write([]byte(*currentMessage))
_, _ = s.Write([]byte("[GHOSTREAM]\n"))
_, err = s.Write([]byte("Enter stream ID: "))
if err != nil {
log.Println("Error while requesting stream ID to telnet client")
_ = s.Close()
return
}
buff := make([]byte, 255)
n, err := s.Read(buff)
if err != nil {
log.Println("Error while requesting stream ID to telnet client")
_ = s.Close()
return
}
streamID = string(buff[:n])
streamID = strings.Replace(streamID, "\r", "", -1)
streamID = strings.Replace(streamID, "\n", "", -1)
if len(streamID) > 0 {
if strings.ToLower(streamID) == "exit" {
_, _ = s.Write([]byte("Goodbye!\n"))
_ = s.Close()
return
}
if _, ok := currentMessage[streamID]; !ok {
_, err = s.Write([]byte("Unknown stream ID.\n"))
if err != nil {
log.Println("Error while requesting stream ID to telnet client")
_ = s.Close()
return
}
continue
}
break
}
}
for {
n, err := s.Write([]byte(currentMessage[streamID]))
if err != nil {
log.Printf("Error while sending TCP data: %s", err)
_ = s.Close()
@ -70,11 +113,11 @@ func Serve(config *Options) {
func asciiChar(pixel byte) string {
asciiChars := []string{"@", "#", "$", "%", "?", "*", "+", ";", ":", ",", ".", " "}
return asciiChars[(255-pixel)/23]
return asciiChars[(255-pixel)/22]
}
// ServeAsciiArt send all packets received by ffmpeg as ASCII Art to telnet clients
func ServeAsciiArt(reader io.ReadCloser) {
func ServeAsciiArt(streamID string, reader io.ReadCloser) {
if !Cfg.Enabled {
_ = reader.Close()
return
@ -95,6 +138,6 @@ func ServeAsciiArt(reader io.ReadCloser) {
}
imageStr += "\n"
}
*currentMessage = header + imageStr
currentMessage[streamID] = header + imageStr
}
}

View File

@ -73,6 +73,15 @@ func ingestFrom(inputChannel chan srt.Packet) {
panic(err)
}
// Receive raw video output and convert it to ASCII art, then forward it TCP
if telnet.Cfg.Enabled {
output, err := ffmpeg.StdoutPipe()
if err != nil {
panic(err)
}
go telnet.ServeAsciiArt(srtPacket.StreamName, output)
}
if err := ffmpeg.Start(); err != nil {
panic(err)
}
@ -109,15 +118,6 @@ func ingestFrom(inputChannel chan srt.Packet) {
}
}()
// Receive raw video output and convert it to ASCII art, then forward it TCP
if telnet.Cfg.Enabled {
output, err := ffmpeg.StdoutPipe()
if err != nil {
panic(err)
}
go telnet.ServeAsciiArt(output)
}
// Receive audio
go func() {
for {