-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
376 lines (322 loc) · 10.5 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"time"
//_ "net/http/pprof"
"os"
"path/filepath"
"strconv"
"sentry-picam/broker"
h "sentry-picam/helper"
"sentry-picam/raspivid"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
// ProductName string
const ProductName = "sentry-picam"
// ProductVersion #
const ProductVersion = "0.7.4"
var clients = make(map[*websocket.Conn]bool)
var clientsMotion = make(map[*websocket.Conn]bool)
var camera raspivid.Camera
var motion raspivid.Motion
var recorder raspivid.Recorder
//go:embed www
var staticAssets embed.FS
func streamVideoToWS(ws *websocket.Conn, caster *broker.Broker, quit chan bool) {
stream := caster.Subscribe()
var x interface{}
//f, _ := os.Create("temp.h264")
for {
select {
case <-quit:
log.Println("Ending a WS video stream")
caster.Unsubscribe(stream)
return
default:
x = <-stream
ws.WriteMessage(websocket.BinaryMessage, x.([]byte))
//f.Write(x.([]byte))
// log.Println("sending--------------\n" + hex.Dump(x.([]byte)))
// log.Println("sent-----------------")
//log.Println("sent bytes: " + strconv.Itoa(len(x.([]byte))))
}
}
}
func streamMotionToWS(ws *websocket.Conn, caster *broker.Broker, quit chan bool) {
stream := caster.Subscribe()
var x interface{}
//f, _ := os.Create("motion.vec")
for {
select {
case <-quit:
log.Println("Ending a WS motion stream")
caster.Unsubscribe(stream)
return
default:
x = <-stream
ws.WriteMessage(websocket.BinaryMessage, x.([]byte))
//f.Write(x.([]byte))
// log.Println("sending--------------\n" + hex.Dump(x.([]byte)))
// log.Println("sent-----------------")
//log.Println("sent bytes: " + strconv.Itoa(len(x.([]byte))))
}
}
}
func initClientVideo(ws *websocket.Conn) {
type initVideo struct {
Action string `json:"action"`
Width int `json:"width"`
Height int `json:"height"`
MBwidth int `json:"mbWidth"`
}
settings := initVideo{
"init",
*camera.Width,
*camera.Height,
motion.BlockWidth,
}
message, err := json.Marshal(settings)
h.CheckError(err)
//log.Println("Initializing client with: " + string(message))
ws.WriteMessage(websocket.TextMessage, message)
}
func initClientMotion(ws *websocket.Conn) {
type initMotion struct {
Mask []int8 `json:"mask"`
}
out := make([]int8, len(motion.MotionMask))
for i, v := range motion.MotionMask {
out[i] = int8(v)
}
settings := initMotion{
out,
}
message, err := json.Marshal(settings)
h.CheckError(err)
//log.Println("Initializing client motion with: " + string(message))
ws.WriteMessage(websocket.TextMessage, message)
}
func wsHandler(caster *broker.Broker) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
// upgrade this connection to a WebSocket connection
ws, err := upgrader.Upgrade(w, r, nil)
h.CheckError(err)
defer ws.Close()
clients[ws] = true
log.Println("Client Connected")
initClientVideo(ws)
quit := make(chan bool)
requestStreamStatus := false
for {
// read in a message
messageType, p, err := ws.ReadMessage()
// log.Println("Message Type: " + strconv.Itoa(messageType))
if err != nil {
delete(clients, ws)
//log.Println(err)
quit <- true
break
}
if messageType == websocket.TextMessage {
log.Println("Message Received: " + string(p))
switch string(p) {
case "start":
if !requestStreamStatus {
requestStreamStatus = true
go streamVideoToWS(ws, caster, quit)
} else {
log.Println("Already requested stream")
}
case "stop":
quit <- true
requestStreamStatus = false
case "mode:night":
camera.CameraNightMode <- true
case "mode:day":
camera.CameraNightMode <- false
case "startrecord":
recorder.RequestedRecord = true
case "stoprecord":
recorder.RequestedRecord = false
}
}
}
})
}
func wsHandlerMotion(caster *broker.Broker) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
// upgrade this connection to a WebSocket connection
ws, err := upgrader.Upgrade(w, r, nil)
h.CheckError(err)
defer ws.Close()
clientsMotion[ws] = true
log.Println("Client Connected for motion")
initClientMotion(ws)
quit := make(chan bool)
requestStreamStatus := false
for {
// read in a message
messageType, p, err := ws.ReadMessage()
// log.Println("Message Type: " + strconv.Itoa(messageType))
if err != nil {
delete(clientsMotion, ws)
//log.Println(err)
quit <- true
break
}
if messageType == websocket.TextMessage {
//log.Println("Motion Message Received: " + string(p))
switch string(p) {
case "start":
if !requestStreamStatus {
requestStreamStatus = true
go streamMotionToWS(ws, caster, quit)
} else {
log.Println("Already requested motion stream")
}
case "stop":
quit <- true
requestStreamStatus = false
}
} else {
//log.Println("Applying motion detection mask")
motion.ApplyMask(p)
}
}
})
}
func httpStreamHandler(caster *broker.Broker) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Starting HTTP stream")
w.Header().Add("Content-Type", "video/H264")
w.Header().Add("Transfer-Encoding", "chunked")
w.WriteHeader(200)
quit := r.Context().Done()
seenHeader := false
stream := caster.Subscribe()
var x interface{}
loop:
for {
select {
case <-quit:
break loop
default:
x = <-stream
if !seenHeader && x.([]byte)[4] == 39 { // SPS header
seenHeader = true
}
if seenHeader {
w.Write(x.([]byte))
}
}
}
log.Println("Ending HTTP stream")
})
}
func main() {
version := flag.Bool("version", false, "Show version")
port := flag.Int("port", 8080, "Port to listen on.\nX+1 and X+2 ports are also used with raspivid")
camera.Width = flag.Int("width", 1280, "Video width")
camera.Height = flag.Int("height", 960, "Video height. 1080 needs to be 1088 for motion detection.")
camera.Fps = flag.Int("fps", 12, "Video framerate. Minimum 1 fps")
camera.SensorMode = flag.Int("sensor", 0, "Sensor mode")
camera.Bitrate = flag.Int("bitrate", 2000000, "Video bitrate")
minFreeSpace := flag.Uint64("minFreeSpace", 1073741824, "Keep at least minFreeSpace available by deleting old recordings")
camera.ExposureValue = flag.Int("ev", 3, "(raspivid) Exposure Value")
camera.MeteringMode = flag.String("mm", "backlit", "(raspivid) Metering Mode")
camera.DynamicRangeCompression = flag.String("drc", "high", "(raspivid) Dynamic Range Compression")
camera.ImageEffect = flag.String("ifx", "denoise", "(raspivid) Image Effect")
camera.ExposureMode = flag.String("ex", "backlight", "(raspivid) Exposure Mode")
camera.Rotation = flag.Int("rot", 0, "Rotate 0, 90, 180, or 270 degrees")
camera.DisableMotion = flag.Bool("disablemotion", false, "Disable motion detection. Lowers CPU usage.")
record := flag.Bool("record", false, "Record detected motion events.")
camera.Protocol = "tcp"
camera.ListenPort = ":" + strconv.Itoa(*port+1)
camera.ListenPortMotion = ":" + strconv.Itoa(*port+2)
//mNumInspectFrames := flag.Int("mframes", 3, "Number of motion frames to examine. Minimum 2.\nLower # increases sensitivity.")
mThreshold := flag.Int("mthreshold", 9, "Motion sensitivity.\nLower # increases sensitivity.")
mBlockWidth := flag.Int("mblockwidth", 0, "Width of motion detection block.\nVideo width and height be divisible by mblockwidth * 16\nLower # increases detection resolution")
usePrevMotionMask := flag.Bool("upmm", false, "Use previous motion mask")
triggerScript := flag.String("run", "", "Run script when motion is detected")
flag.Parse()
if *version {
fmt.Println(ProductName + " version " + ProductVersion)
return
}
log.Println(ProductName + " version " + ProductVersion)
//motion.NumInspectFrames = *mNumInspectFrames
motion.SenseThreshold = int8(*mThreshold)
motion.BlockWidth = *mBlockWidth
listenPort := ":" + strconv.Itoa(*port)
if *camera.Bitrate < 1 || *camera.Fps < 1 {
log.Fatal("FPS and bitrate must be greater than 1")
}
exDir, _ := os.Executable()
exDir = filepath.Dir(exDir)
recordingFolder := exDir + "/www/recordings/"
// setup motion detector
motion.Protocol = "tcp"
motion.ListenPort = camera.ListenPortMotion
motion.Width = *camera.Width
motion.Height = *camera.Height
motion.RecordingFolder = recordingFolder
motion.Init(*usePrevMotionMask)
// start broadcaster and camera
castVideo := broker.New()
castMotion := broker.New()
go castVideo.Start()
go castMotion.Start()
go motion.Start(castMotion, &recorder)
go camera.Start(castVideo)
recorder.MinFreeSpace = *minFreeSpace
go recorder.Init(castVideo, recordingFolder, *camera.Fps, *triggerScript)
go update(recordingFolder)
if *record {
time.AfterFunc(2*time.Second, func() { // let raspivid settle in
log.Println("Recording enabled from console")
recorder.RequestedRecord = true
})
}
// setup web services
r := mux.NewRouter()
//fs := http.FileServer(http.Dir(exDir + "/www"))
//r.Handle("/", fs)
r.Handle("/ws/video", wsHandler(castVideo))
r.Handle("/ws/motion", wsHandlerMotion(castMotion))
r.Handle("/video.h264", httpStreamHandler(castVideo))
recordingList := RecordingList{}
recordingList.Folder = recordingFolder
status := Status{}
status.Recorder = &recorder
api := r.PathPrefix("/api").Subrouter()
api.HandleFunc("/videos", recordingList.handleRecordingList).Methods("GET")
api.HandleFunc("/videos/cleanup", recordingList.handleDestroyRecording).Methods("DELETE")
api.HandleFunc("/videos/{videoID}", recordingList.handleDeleteRecording).Methods("DELETE")
//api.HandleFunc("/videos/{videoID}/thumbnail", recordingList.handleThumbnailUpdate).Methods("POST")
api.HandleFunc("/status", status.handleStatus).Methods("GET")
// static files
//r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(http.Dir(exDir+"/www/js"))))
os.MkdirAll(recordingFolder, 0700)
r.PathPrefix("/recordings/").Handler(http.StripPrefix("/recordings/", http.FileServer(http.Dir(exDir+"/www/recordings"))))
webRoot, _ := fs.Sub(staticAssets, "www")
r.PathPrefix("/").Handler(http.FileServer(http.FS(webRoot)))
log.Println("HTTP Listening on " + listenPort)
http.ListenAndServe(listenPort, r)
}