-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
85 lines (68 loc) · 1.87 KB
/
handler.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
package main
import (
"errors"
"fmt"
"mime/multipart"
"net/http"
"net/textproto"
"strconv"
"syscall"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
const (
boundary string = "frame"
)
func stream(w http.ResponseWriter, r *http.Request) {
log.Infof("received stream request from %s", r.RemoteAddr)
w.Header().Add(
"Content-Type",
fmt.Sprintf("multipart/x-mixed-replace;boundary=%s", boundary),
)
multipartWriter := multipart.NewWriter(w)
multipartWriter.SetBoundary(boundary)
for {
// wait for a frame to be available
frame := <-frames
partWriter, err := multipartWriter.CreatePart(textproto.MIMEHeader{
"Content-Type": []string{"image/jpeg"},
"Content-Length": []string{strconv.Itoa(len(frame))},
})
// Handle any errors that might have occur creating the multipart
// writer. If an error occurs that is not a 'broken pipe' error, then
// log it, otherwise just break out of the loop.
//
// TODO: handle errors with appropriate status code?
if err != nil {
if !errors.Is(err, syscall.EPIPE) {
log.Errorf("error creating multipart: %s", err.Error())
}
break
}
// Write the frame to the stream. If an error occurs that is not a
// 'broken pipe' error then log, otherwise just break out of the loop.
//
// TODO: handle errors with appropriate status code?
if _, err := partWriter.Write(frame); err != nil {
if !errors.Is(err, syscall.EPIPE) {
log.Errorf("error writing to stream: %s", err.Error())
}
break
}
}
log.Infof("closing stream to %s", r.RemoteAddr)
}
func listCameras(w http.ResponseWriter, r *http.Request) {
}
func getCamera(w http.ResponseWriter, r *http.Request) {
}
func switchCamera(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
lock.Lock()
defer lock.Unlock()
if id > len(cameras)-1 {
return
}
selected = cameras[id]
}