-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdsrc.go
117 lines (101 loc) · 1.99 KB
/
cmdsrc.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
// Read MJPEG data from running a command
// commandMJPEGSource manages the state for running `libcamera-vid` or similar and getting MJPEG stream from it
package main
import (
"context"
"encoding/json"
"io"
"log"
"os/exec"
"time"
)
type commandMJPEGSource struct {
argv []string
cmd *exec.Cmd
ctx context.Context
retryDelay time.Duration
out *io.PipeWriter
reader *io.PipeReader
buf []byte
}
type CmdJSON struct {
Argv []string `json:"cmd"`
RetryDelay string `json:"retry"` // for time.ParseDuration
}
func JsonCmd(ctx context.Context, fin io.Reader) (*commandMJPEGSource, error) {
dec := json.NewDecoder(fin)
var cj CmdJSON
err := dec.Decode(&cj)
if err != nil {
return nil, err
}
out := new(commandMJPEGSource)
out.argv = cj.Argv
out.ctx = ctx
if len(cj.RetryDelay) > 0 {
out.retryDelay, err = time.ParseDuration(cj.RetryDelay)
if err != nil {
return nil, err
}
}
return out, nil
}
func (s *commandMJPEGSource) Init() {
s.reader, s.out = io.Pipe()
if s.retryDelay < 1 {
s.retryDelay = time.Second
}
if s.ctx == nil {
s.ctx = context.Background()
}
}
func (s *commandMJPEGSource) Run() {
for {
s.runOnce()
select {
case <-s.ctx.Done():
return
default:
}
time.Sleep(s.retryDelay)
}
}
func (s *commandMJPEGSource) runOnce() {
if s.buf == nil {
s.buf = make([]byte, 128*1024)
}
cmd := exec.CommandContext(s.ctx, s.argv[0], s.argv[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Printf("cmd setup: %v", err)
return
}
err = cmd.Start()
if err != nil {
log.Printf("cmd start: %v", err)
return
}
log.Printf("started command")
for {
select {
case <-s.ctx.Done():
return
default:
}
actual, err := stdout.Read(s.buf)
if actual > 0 {
_, e2 := s.out.Write(s.buf[:actual])
if e2 != nil {
log.Printf("cmd data internal write: %v", e2)
return
}
}
if err != nil {
log.Printf("cmd read: %v", err)
return
}
}
}
func (s *commandMJPEGSource) Read(b []byte) (int, error) {
return s.reader.Read(b)
}