From: acevest Date: Sun, 17 Jan 2021 05:10:00 +0000 (+0800) Subject: add esp32camweb X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=8838ca2dc16084ef63234ffdd7d295258c3c07fa;p=acecode.git add esp32camweb --- diff --git a/projects/esp32_video_stream/esp32cam/esp32cam.ino b/projects/esp32_video_stream/esp32cam/esp32cam.ino index 0086a24..08ccb51 100644 --- a/projects/esp32_video_stream/esp32cam/esp32cam.ino +++ b/projects/esp32_video_stream/esp32cam/esp32cam.ino @@ -10,7 +10,7 @@ typedef struct { const char *pwd; } WiFiItem_t; -const char* websockets_server_host = "10.0.0.6"; +const char* websockets_server_host = "10.0.0.10"; const uint16_t websockets_server_port = 80; WiFiItem_t WiFiTable[] = { diff --git a/projects/esp32_video_stream/esp32camweb/canvas.html b/projects/esp32_video_stream/esp32camweb/canvas.html new file mode 100644 index 0000000..eb4fce0 --- /dev/null +++ b/projects/esp32_video_stream/esp32camweb/canvas.html @@ -0,0 +1,86 @@ + + + + + + CANVAS VIDEO + + + + + +

+    

+
+    
+
+
+
diff --git a/projects/esp32_video_stream/esp32camweb/go.mod b/projects/esp32_video_stream/esp32camweb/go.mod
new file mode 100644
index 0000000..64471b9
--- /dev/null
+++ b/projects/esp32_video_stream/esp32camweb/go.mod
@@ -0,0 +1,5 @@
+module esp32camweb
+
+go 1.14
+
+require github.com/gorilla/websocket v1.4.2
diff --git a/projects/esp32_video_stream/esp32camweb/go.sum b/projects/esp32_video_stream/esp32camweb/go.sum
new file mode 100644
index 0000000..85efffd
--- /dev/null
+++ b/projects/esp32_video_stream/esp32camweb/go.sum
@@ -0,0 +1,2 @@
+github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
+github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
diff --git a/projects/esp32_video_stream/esp32camweb/main.go b/projects/esp32_video_stream/esp32camweb/main.go
new file mode 100644
index 0000000..18a7974
--- /dev/null
+++ b/projects/esp32_video_stream/esp32camweb/main.go
@@ -0,0 +1,151 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: main.go
+ *      Author: Zhao Yanbai
+ *              2021-01-16 23:01:01 Saturday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+package main
+
+import (
+	"fmt"
+	"html/template"
+	"log"
+	"net/http"
+	"time"
+
+	"github.com/gorilla/websocket"
+)
+
+var upgrader = websocket.Upgrader{
+	ReadBufferSize:  102400,
+	WriteBufferSize: 102400,
+
+	// 解决跨域问题
+	CheckOrigin: func(r *http.Request) bool {
+		return true
+	},
+}
+
+var frameChan chan []byte
+
+func init() {
+	frameChan = make(chan []byte, 32)
+}
+
+const (
+	partBOUNDARY      = "123456789000000000000987654321"
+	streamContentType = "multipart/x-mixed-replace;boundary=" + partBOUNDARY
+	streamBoundary    = "\r\n--" + partBOUNDARY + "\r\n"
+	streamPart        = "Content-Type: image/jpeg\r\nContent-Length: %d\r\n\r\n"
+)
+
+func watchHandler(w http.ResponseWriter, r *http.Request) {
+	var err error
+	w.Header().Set("Content-Type", streamContentType)
+	w.Header().Set("Access-Control-Allow-Origin", "*")
+
+	flusher, _ := w.(http.Flusher)
+
+	for i := 0; ; i++ {
+		var data []byte
+		select {
+		case data = <-frameChan:
+			fmt.Fprintf(w, "%v", streamBoundary)
+			fmt.Fprintf(w, streamPart, i)
+			_, err = w.Write(data)
+			flusher.Flush()
+			if err != nil {
+				break
+			}
+		default:
+			continue
+		}
+
+	}
+}
+
+func videoHandler(w http.ResponseWriter, r *http.Request) {
+	var err error
+	var ws *websocket.Conn
+
+	log.Printf("video\n")
+	ws, err = upgrader.Upgrade(w, r, nil)
+	if err != nil {
+		log.Printf("err: %v", err)
+		return
+	}
+
+	defer ws.Close()
+
+	for {
+		t, p, e := ws.ReadMessage()
+		log.Printf("type %v data len %v err %v", t, len(p), e)
+		if e != nil {
+			break
+		}
+
+		if len(p) < 100 {
+			continue
+		}
+
+		select {
+		case frameChan <- p:
+			log.Printf("put frame")
+		default:
+			log.Printf("drop frame")
+		}
+
+	}
+}
+
+func streamHandler(w http.ResponseWriter, r *http.Request) {
+	var err error
+	var ws *websocket.Conn
+
+	ws, err = upgrader.Upgrade(w, r, nil)
+	if err != nil {
+		log.Printf("err: %v", err)
+		return
+	}
+
+	defer ws.Close()
+
+	for {
+		var data []byte
+		select {
+		case data = <-frameChan:
+		case <-time.After(1 * time.Millisecond):
+			continue
+		}
+
+		err = ws.WriteMessage(websocket.BinaryMessage, data)
+		if err != nil {
+			break
+		}
+	}
+}
+
+func canvasHandler(w http.ResponseWriter, r *http.Request) {
+	tmpl := template.Must(template.ParseFiles("./canvas.html"))
+	var data = map[string]interface{}{}
+	tmpl.Execute(w, data)
+}
+
+func main() {
+	defer fmt.Println("Program Exited...")
+
+	m := http.NewServeMux()
+	m.HandleFunc("/watch", watchHandler)
+	m.HandleFunc("/video", videoHandler)
+	m.HandleFunc("/canvas", canvasHandler)
+	m.HandleFunc("/stream", streamHandler)
+	server := http.Server{
+		Addr:    ":80",
+		Handler: m,
+	}
+
+	server.ListenAndServe()
+}