-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
129 lines (104 loc) · 3.07 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
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
package main
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"sync"
"gocv.io/x/gocv"
)
const (
StillOnce = "once"
StillOnceRPI = "once_rpi"
StillStream = "stream"
StillStreamRPI = "stream_rpi"
StillFalse = "false"
StillDefault = ""
)
// ServeStillUrl returns a proxied frame from a still url
func ServeStillUrl(stillURL string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
response, err := http.DefaultClient.Get(stillURL)
if err != nil {
http.Error(w, fmt.Sprintf("could not fetch frame: %v", err.Error()), http.StatusBadGateway)
return
}
defer response.Body.Close()
for key, values := range response.Header {
w.Header().Add(key, strings.Join(values, " "))
}
io.Copy(w, response.Body)
}
}
// ServeStillStream scrapes a frame from RTSP stream
func ServeStillStream(rtspURL string, method string) http.HandlerFunc {
var err error
var capture *gocv.VideoCapture
var m sync.RWMutex
// The OpenCV Buffer
img := gocv.NewMat()
needFrame := make(chan struct{})
frame := make(chan []byte)
go func() {
needFrameLoop:
for {
<-needFrame
if capture == nil {
if method == StillOnce {
capture, err = gocv.OpenVideoCapture(fmt.Sprintf("%s", rtspURL)) // gstreamer doesn't like to be opened and closed
} else if method == StillStreamRPI {
capture, err = gocv.OpenVideoCapture(fmt.Sprintf("rtspsrc location=%s ! rtph264depay ! h264parse ! omxh264dec ! appsink max-buffers=1 drop=true", rtspURL))
} else if method == StillStream || method == StillDefault {
capture, err = gocv.OpenVideoCapture(fmt.Sprintf("rtspsrc location=%s ! decodebin ! videoconvert ! appsink max-buffers=1 drop=true", rtspURL))
// capture, err = gocv.OpenVideoCapture(fmt.Sprintf("rtspsrc location=%s latency=10 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink max-buffers=1 drop=true", rtspURL))
// capture, err = gocv.OpenVideoCapture(fmt.Sprintf("rtspsrc location=%s ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink max-buffers=1 drop=true", rtspURL))
} else {
log.Printf("unsupported still method: %s", method)
frame <- nil
continue
}
if err != nil {
log.Printf("could not open rtsp: %s error: %v", rtspURL, err)
frame <- nil
continue
}
}
m.RLock()
for {
// Read an image
if ok := capture.Read(&img); !ok {
log.Printf("could not read frame: %v", err)
frame <- nil
continue needFrameLoop
}
if img.Empty() {
continue
}
break
}
// Re-Encode to jpg
data, err := gocv.IMEncode(".jpg", img)
if err != nil {
log.Printf("could not create frame: %v", err)
frame <- nil
continue
}
frame <- data
if method == StillOnce || method == StillOnceRPI {
capture.Close()
capture = nil
}
}
}()
return func(w http.ResponseWriter, r *http.Request) {
needFrame <- struct{}{}
data := <-frame
if data == nil {
http.Error(w, fmt.Sprintf("could not get frame"), http.StatusBadGateway)
return
}
w.Header().Add("Content-Type", "image/jpg")
w.Write(data)
}
}