mirror of
https://github.com/gen2brain/cam2ip.git
synced 2026-05-03 03:12:34 +00:00
Initial commit
This commit is contained in:
77
handlers/mjpeg.go
Normal file
77
handlers/mjpeg.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Package handlers.
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"time"
|
||||
|
||||
"github.com/gen2brain/cam2ip/camera"
|
||||
)
|
||||
|
||||
// MJPEG handler.
|
||||
type MJPEG struct {
|
||||
camera *camera.Camera
|
||||
delay int
|
||||
}
|
||||
|
||||
// NewMJPEG returns new MJPEG handler.
|
||||
func NewMJPEG(camera *camera.Camera, delay int) *MJPEG {
|
||||
return &MJPEG{camera, delay}
|
||||
}
|
||||
|
||||
// ServeHTTP handles requests on incoming connections.
|
||||
func (m *MJPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
mimeWriter := multipart.NewWriter(w)
|
||||
mimeWriter.SetBoundary("--boundary")
|
||||
|
||||
w.Header().Add("Connection", "close")
|
||||
w.Header().Add("Cache-Control", "no-store, no-cache")
|
||||
w.Header().Add("Content-Type", fmt.Sprintf("multipart/x-mixed-replace;boundary=%s", mimeWriter.Boundary()))
|
||||
|
||||
cn := w.(http.CloseNotifier).CloseNotify()
|
||||
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case <-cn:
|
||||
break loop
|
||||
|
||||
default:
|
||||
partHeader := make(textproto.MIMEHeader)
|
||||
partHeader.Add("Content-Type", "image/jpeg")
|
||||
|
||||
partWriter, err := mimeWriter.CreatePart(partHeader)
|
||||
if err != nil {
|
||||
log.Printf("mjpeg: createPart: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
img, err := m.camera.Read()
|
||||
if err != nil {
|
||||
log.Printf("mjpeg: read: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
enc := camera.NewEncoder(partWriter)
|
||||
|
||||
err = enc.Encode(img)
|
||||
if err != nil {
|
||||
log.Printf("mjpeg: encode: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
time.Sleep(time.Duration(m.delay) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
mimeWriter.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user