-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
214 lines (182 loc) · 5.48 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"bufio"
"flag"
"fmt"
"io"
"sync"
"time"
golog "log"
log "github.com/s00500/env_logger"
"github.com/google/gousb"
)
//go:generate sh injectGitVars.sh
var magicStartBytes = []byte{0x52, 0x4d, 0x56, 0x54}
var useGstreamer *bool = flag.Bool("gstreamer", false, "use gstreamer")
var outputMode *string = flag.String("output", "", "different modes presets for the videodisplay")
func main() {
redirectStandardLogger()
flag.Parse()
log.Infof("Starting djifpvvideout version %s (commit %s)", gitTag, gitRevision)
if useGstreamer != nil && *useGstreamer {
log.Info("using gstreamer")
}
outModes := map[string]StreamSink{
"gstreamer": &GstSink{
Args: []string{"fdsrc", "fd=0", "!", "decodebin", "!", "videoconvert", "n-threads=8", "!", "autovideosink", "sync=false"}, // decodebin3 seems to be faster on macOS but does not work on RPI4
},
"gstreamer-sync": &GstSink{
Args: []string{"fdsrc", "fd=0", "!", "decodebin", "!", "videoconvert", "n-threads=8", "!", "autovideosink"}, // RPI Direct to framebuffer, sync=false messes things up here...
},
"gstreamer-rtmp": &GstSink{
Args: []string{"fdsrc", "fd=0", "!", "h264parse", "config-interval=1", "update-timecode=true", "!", "flvmux", "!", "rtmpsink", "location='rtmp://127.0.0.1/live/HkwUDjiqu'"}, // gstreamer rtmp direct test
},
"fifo": &FifoSink{},
"file": &FileSink{Path: "somerec.bin"},
"hello": &HelloVideoSink{},
"ffplay": &FFPlaySink{},
}
var sink StreamSink
if useGstreamer != nil && *useGstreamer {
sink = new(GstSink)
log.Info("Using gstreamer")
} else if outputMode != nil && *outputMode != "" {
ok := false
sink, ok = outModes[*outputMode]
if !ok {
log.Fatal("Unknown output mode ", *outputMode)
}
log.Info("Using ", *outputMode)
} else {
sink = new(FFPlaySink) // default
log.Info("Using ffplay")
}
openPorts := make([]string, 0)
openPortsMu := sync.RWMutex{}
ctx := gousb.NewContext()
defer ctx.Close()
vid, pid := gousb.ID(0x2ca3), gousb.ID(0x001f)
for {
devs, err := ctx.OpenDevices(func(d *gousb.DeviceDesc) bool {
// this function is called for every device present.
// Returning true means the device should be opened.
if d.Vendor != vid || d.Product != pid {
return false
}
openPortsMu.RLock()
defer openPortsMu.RUnlock()
return !containsString(openPorts, fmt.Sprintf("%d.%d", d.Bus, d.Address))
})
if log.Should(err) {
time.Sleep(time.Second)
continue
}
if len(devs) == 0 {
time.Sleep(time.Second * 3)
continue
}
log.Info("Found ", len(devs), " devices")
for _, devInArray := range devs {
dev := devInArray
openPortsMu.Lock()
openPorts = append(openPorts, fmt.Sprintf("%d.%d", dev.Desc.Bus, dev.Desc.Address))
openPortsMu.Unlock()
go func() {
log.Infof("connecting to device on %d.%d", dev.Desc.Bus, dev.Desc.Address)
if fifosink, ok := sink.(*FifoSink); ok {
fifosink.Path = fmt.Sprintf("stream%d-%d.fifo", dev.Desc.Bus, dev.Desc.Address)
openStream(dev, fifosink)
} else {
openStream(dev, sink)
}
dev.Close()
openPortsMu.Lock()
openPorts = deleteElement(openPorts, fmt.Sprintf("%d.%d", dev.Desc.Bus, dev.Desc.Address))
openPortsMu.Unlock()
log.Warnf("lost device on %d.%d", dev.Desc.Bus, dev.Desc.Address)
}()
}
time.Sleep(time.Second * 3)
}
}
func openStream(dev *gousb.Device, sink StreamSink) {
sinkIn, stopPlayer := sink.StartInstance()
// claim interface
intf, done, err := googleInterface(dev)
if err != nil {
log.Errorf("%s.GoogleInterface: %v", dev, err)
return
}
ep, err := intf.OutEndpoint(3)
if err != nil {
log.Errorf("%s.OutEndpoint: %v", intf, err)
return
}
inEP, err := intf.InEndpoint(4)
if err != nil {
log.Errorf("%s.InEndpoint: %v", intf, err)
return
}
// Write data to the USB device.
numBytes, err := ep.Write(magicStartBytes)
if numBytes != len(magicStartBytes) {
log.Errorf("%s.Write(%d): only %d bytes written, returned error is %v", ep, len(magicStartBytes), numBytes, err)
return
}
log.Debug("magic bytes successfully sent to the endpoint")
stream, err := inEP.NewStream(512, 3) // Took default form github
if err != nil {
log.Errorf("Could not open stream: %v", intf, err)
return
}
io.Copy(sinkIn, stream)
stopPlayer()
done()
}
func googleInterface(d *gousb.Device) (intf *gousb.Interface, done func(), err error) {
cfgNum, err := d.ActiveConfigNum()
if err != nil {
return nil, nil, fmt.Errorf("failed to get active config number of device %s: %v", d, err)
}
cfg, err := d.Config(cfgNum)
if err != nil {
return nil, nil, fmt.Errorf("failed to claim config %d of device %s: %v", cfgNum, d, err)
}
i, err := cfg.Interface(3, 0)
if err != nil {
cfg.Close()
return nil, nil, fmt.Errorf("failed to select interface #%d alternate setting %d of config %d of device %s: %v", 0, 0, cfgNum, d, err)
}
return i, func() {
intf.Close()
cfg.Close()
}, nil
}
func containsString(all []string, one string) bool {
for _, s := range all {
if s == one {
return true
}
}
return false
}
func deleteElement(all []string, one string) []string {
for index, elem := range all {
if elem == one {
return append(all[:index], all[index+1:]...)
}
}
return all
}
func redirectStandardLogger() {
// Redirect the default logger to catch the usb errors on a different log level
src, dst := io.Pipe()
golog.Default().SetOutput(dst)
scanner := bufio.NewScanner(src)
otherLogger := log.GetLoggerForPrefix("others")
go func() {
for scanner.Scan() {
otherLogger.Trace(scanner.Text())
}
}()
}