-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
87 lines (75 loc) · 1.52 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"net"
"time"
"github.com/google/draco/go/dracogen"
"github.com/google/draco/go/dracoutils"
"github.com/google/draco/go/gltf"
"gocv.io/x/gocv"
)
func main() {
// Bind to a local port.
ln, err := net.Listen("tcp", ":8000")
if err != nil {
fmt.Println(err)
return
}
defer ln.Close()
// Accept incoming connections.
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
// Open the video capture device.
webcam, err := gocv.VideoCaptureDevice(0)
if err != nil {
fmt.Println(err)
return
}
defer webcam.Close()
// Compress the 3D model using Draco.
compressor, err := dracogen.NewCompressor()
if err != nil {
fmt.Println(err)
return
}
defer compressor.Close()
// Continuously transmit the 3D model as part of a live stream.
ticker := time.NewTicker(time.Second)
for range ticker.C {
// Read a frame from the video capture device.
mat := gocv.NewMat()
if ok := webcam.Read(&mat); !ok {
fmt.Println("error reading webcam")
return
}
if mat.Empty() {
continue
}
// Convert the frame to a GLTF model.
doc, err := dracoutils.MatToGLTF(mat)
if err != nil {
fmt.Println(err)
return
}
// Compress the GLTF model using Draco.
encoded, err := compressor.Compress(doc)
if err != nil {
fmt.Println(err)
return
}
// Send the encoded data to the client.
_, err = conn.Write(encoded)
if err != nil {
fmt.Println(err)
return
}
}
}