-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
63 lines (56 loc) · 1.07 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package goarstream
import (
"bufio"
"fmt"
"log"
"net"
"os"
"time"
)
const (
host = "192.168.1.1:"
port = "5555"
connType = "tcp"
)
func main() {
video, _, _, _ := ffmpeg()
conn, err := net.Dial(connType, host+port)
if err != nil {
fmt.Println(err)
}
reader := bufio.NewReader(conn)
vidc := make(chan frame)
quit := make(chan bool)
startWorker(video, vidc, quit)
for {
var f frame
err := f.parse(reader)
if err != nil {
fmt.Println("Error parsing:", err.Error())
} else {
vidc <- f
}
}
}
func startWorker(video *bufio.Writer, videoFrames chan frame, quit chan bool) {
go func() {
for {
select {
case frame := <-videoFrames:
start := time.Now()
if i, err := video.Write(frame.Payload); err != nil {
fmt.Println("Error writing to ffmpeg: ", err.Error(), i)
} else {
fmt.Printf("Wrote %d bytes to ffmpeg in %v \n", i, time.Since(start))
}
if err := video.Flush(); err != nil {
log.Fatal(err)
}
//fmt.Printf("%+v\n", frame)
case <-quit:
fmt.Println("quit")
os.Exit(0)
}
}
}()
}