-
Notifications
You must be signed in to change notification settings - Fork 8
/
video.go
98 lines (76 loc) · 2.18 KB
/
video.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
87
88
89
90
91
92
93
94
95
96
97
98
package systems
import (
"image"
"unsafe"
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
"github.com/brunoga/robomaster/sdk2/examples/robotcontrol/entities"
"github.com/brunoga/robomaster/sdk2/module/camera"
"github.com/brunoga/robomaster/sdk2/unitybridge/support/token"
)
type Video struct {
videoEntity *entities.Video
frameCh chan *image.NRGBA
dataHandlerToken token.Token
Camera *camera.Camera
}
func (v *Video) New(w *ecs.World) {
rect := image.Rect(0, 0, 1280, 720)
v.videoEntity = &entities.Video{BasicEntity: ecs.NewBasic()}
v.videoEntity.SpaceComponent = common.SpaceComponent{
Position: engo.Point{X: 0, Y: 0},
Width: 1280,
Height: 720,
}
img := image.NewNRGBA(rect)
obj := common.NewImageObject(img)
v.videoEntity.RenderComponent = common.RenderComponent{
Drawable: common.NewTextureSingle(obj),
}
for _, system := range w.Systems() {
switch sys := system.(type) {
case *common.RenderSystem:
sys.Add(&v.videoEntity.BasicEntity,
&v.videoEntity.RenderComponent,
&v.videoEntity.SpaceComponent)
}
}
v.frameCh = make(chan *image.NRGBA, 30)
v.Camera.SetVideoQuality(camera.VideoQualityBest)
index, err := v.Camera.AddVideoCallback(v.DataHandler)
if err != nil {
panic(err)
}
v.dataHandlerToken = index
}
func (v *Video) Add() {}
func (v *Video) Remove(basic ecs.BasicEntity) {}
func (v *Video) Update(dt float32) {
select {
case img := <-v.frameCh:
obj := common.NewImageObject(img)
tex := common.NewTextureSingle(obj)
v.videoEntity.Drawable.Close()
v.videoEntity.Drawable = tex
default:
//do nothing
}
}
func (v *Video) DataHandler(frame *camera.RGB) {
// Create an image out of the data byte slice.
img := common.ImageToNRGBA(frame, 1280, 720)
v.frameCh <- img
}
func NRGBA(rgbData []byte) []byte {
numPixels := len(rgbData) / 3
nrgbaData := make([]byte, numPixels*4)
intNRGBAData := *(*[]uint32)(unsafe.Pointer(&nrgbaData))
intNRGBAData = intNRGBAData[:len(nrgbaData)/4]
for i, j := 0, 0; i < len(rgbData); i, j = i+3, j+1 {
intRGB := (*(*uint32)(unsafe.Pointer(&rgbData[i]))) |
(0b11111111 << 24)
intNRGBAData[j] = intRGB
}
return nrgbaData
}