Skip to content

Commit

Permalink
test server for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
dmisol committed Sep 15, 2022
1 parent 385b196 commit 12054c0
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
118 changes: 118 additions & 0 deletions debugserver/srv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"image"
"image/color"
"log"
"net"
"os"
"time"

"github.com/dmisol/animportal/defs"
"gocv.io/x/gocv"
)

func main() {
// Listen for incoming connections.
l, err := net.Listen("tcp", ":50000")
if err != nil {
log.Println("istening:", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer l.Close()
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
log.Println("accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go handler(conn)
}
}

func handler(conn net.Conn) {
defer conn.Close()

buf := make([]byte, 2048)
// initial json
if _, err := conn.Read(buf); err != nil {
log.Println("reading:", err.Error())
return
}
var init *defs.Init

if err := json.Unmarshal(buf, init); err != nil {
log.Println("initial json", err)
return
}

var started chan bool
go func() {
// wait for the first "audio" file
audio := <-started
if !audio {
return
}

img := gocv.NewMatWithSize(init.W, init.H, gocv.MatTypeCV8UC3)
fps := 24
if init.FPS != 0 {
fps = init.FPS
}
dt := 10000000000 / fps
t := time.NewTicker(time.Nanosecond * time.Duration(dt))
defer t.Stop()

index := 0
for {
<-t.C
name := fmt.Sprintf("%s/%d.png", init.Dir, index)
index++

if err := firePng(conn, &img, name); err != nil {
log.Println("senging png", err)
return
}
}
}()

running := false
for {
i, err := conn.Read(buf)
if err != nil {
log.Println("read", err)
if !running {
started <- false
}
return
}
if err = os.Remove(string(buf[:i])); err != nil {
log.Println("removing", err)
if !running {
started <- false
}
return
}
if !running {
log.Println("first audio")
started <- true
running = true
}
}
}

func firePng(c net.Conn, img *gocv.Mat, name string) (err error) {
gocv.PutText(img, time.Now().String(), image.Point{5, 5}, 0, 5.0, color.RGBA{0, 0, 255, 0}, 2)
if res := gocv.IMWrite(name, *img); !res {
err = errors.New("failed to write " + name)
return
}
_, err = c.Write([]byte(name))
return
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/pion/webrtc/v3 v3.1.43
github.com/valyala/fasthttp v1.39.0
github.com/zaf/resample v0.0.0-20220109201959-aca35f45e6fa
gocv.io/x/gocv v0.31.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -156,6 +157,7 @@ github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDs
github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0=
github.com/onsi/gomega v1.17.0 h1:9Luw4uT5HTjHTN8+aNcSThgH1vdXnmdJ8xIfZ4wyTRE=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pion/datachannel v1.5.2 h1:piB93s8LGmbECrpO84DnkIVWasRMk3IimbcXkTQLE6E=
github.com/pion/datachannel v1.5.2/go.mod h1:FTGQWaHrdCwIJ1rw6xBIfZVkslikjShim5yr05XFuCQ=
github.com/pion/dtls/v2 v2.1.3/go.mod h1:o6+WvyLDAlXF7YiPB/RlskRoeK+/JtuaZa5emwQcWus=
Expand Down Expand Up @@ -258,6 +260,8 @@ go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
gocv.io/x/gocv v0.31.0 h1:BHDtK8v+YPvoSPQTTiZB2fM/7BLg6511JqkruY2z6LQ=
gocv.io/x/gocv v0.31.0/go.mod h1:oc6FvfYqfBp99p+yOEzs9tbYF9gOrAQSeL/dyIPefJU=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down

0 comments on commit 12054c0

Please sign in to comment.