mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 20:52:20 +00:00
Interact with telnet to select the stream id
This commit is contained in:
parent
95fcedf2fa
commit
727865f444
@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ var (
|
|||||||
// TODO Config should not be exported
|
// TODO Config should not be exported
|
||||||
// Cfg contains the different options of the telnet package, see below
|
// Cfg contains the different options of the telnet package, see below
|
||||||
Cfg *Options
|
Cfg *Options
|
||||||
currentMessage *string
|
currentMessage map[string]string
|
||||||
)
|
)
|
||||||
|
|
||||||
// Options holds telnet package configuration
|
// Options holds telnet package configuration
|
||||||
@ -32,14 +33,14 @@ func Serve(config *Options) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentMessage = make(map[string]string)
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", Cfg.ListenAddress)
|
listener, err := net.Listen("tcp", Cfg.ListenAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while listening to the address %s: %s", Cfg.ListenAddress, err)
|
log.Printf("Error while listening to the address %s: %s", Cfg.ListenAddress, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
currentMessage = new(string)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
s, err := listener.Accept()
|
s, err := listener.Accept()
|
||||||
@ -47,9 +48,51 @@ func Serve(config *Options) {
|
|||||||
log.Printf("Error while accepting TCP socket: %s", s)
|
log.Printf("Error while accepting TCP socket: %s", s)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(s net.Conn) {
|
go func(s net.Conn) {
|
||||||
|
streamID := ""
|
||||||
|
// Request for stream ID
|
||||||
for {
|
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 {
|
if err != nil {
|
||||||
log.Printf("Error while sending TCP data: %s", err)
|
log.Printf("Error while sending TCP data: %s", err)
|
||||||
_ = s.Close()
|
_ = s.Close()
|
||||||
@ -70,11 +113,11 @@ func Serve(config *Options) {
|
|||||||
|
|
||||||
func asciiChar(pixel byte) string {
|
func asciiChar(pixel byte) string {
|
||||||
asciiChars := []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
|
// 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 {
|
if !Cfg.Enabled {
|
||||||
_ = reader.Close()
|
_ = reader.Close()
|
||||||
return
|
return
|
||||||
@ -95,6 +138,6 @@ func ServeAsciiArt(reader io.ReadCloser) {
|
|||||||
}
|
}
|
||||||
imageStr += "\n"
|
imageStr += "\n"
|
||||||
}
|
}
|
||||||
*currentMessage = header + imageStr
|
currentMessage[streamID] = header + imageStr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,15 @@ func ingestFrom(inputChannel chan srt.Packet) {
|
|||||||
panic(err)
|
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 {
|
if err := ffmpeg.Start(); err != nil {
|
||||||
panic(err)
|
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
|
// Receive audio
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
Loading…
Reference in New Issue
Block a user