]> Zhao Yanbai Git Server - acecode.git/commitdiff
...
authorAceVest <zhaoyanbai@126.com>
Sun, 17 Jan 2021 05:54:45 +0000 (13:54 +0800)
committerAceVest <zhaoyanbai@126.com>
Sun, 17 Jan 2021 05:54:45 +0000 (13:54 +0800)
projects/esp32_video_stream/esp32camweb/canvas.html
projects/esp32_video_stream/esp32camweb/main.go

index eb4fce0ede654ab0de907de49ccd5861caf3f287..359eb7ef2d3f8be0255911f9148796eba23c9500 100644 (file)
 
 <body>
     <canvas id="Video" width="240" height="320"></canvas>
-    <pre id="FrameRate"></pre>
+    <pre id="Stat"></pre>
     <pre id="Msg"></pre>
 
     <script>
         var ws;
-        var wsURL = "wss://10.0.0.10/stream";
+        var wsURL = "ws://127.0.0.1/stream";
         var canvas = document.getElementById('Video');
         var ctx = canvas.getContext('2d'); 
 
         var frameCount = 0;
+        var totalBytes = 0;
         window.setInterval(function() {
             fps = frameCount;
+            bytes = totalBytes;
+
             frameCount = 0;
-            var FrameRate = document.getElementById('FrameRate');
-            FrameRate.innerHTML = "当前帧率: " + fps + " fps";
+            totalBytes = 0;
+            
+            var Stat = document.getElementById('Stat');
+            Stat.innerHTML = "当前帧率: " + fps + " fps, 带宽:" + (bytes*8.0/(1024.0*1024.0)).toFixed(1) + " Mbps";
         },1000)
 
         function Msg(...args) {
@@ -64,6 +69,7 @@
             ws.onmessage = function (event) {
                 imageData = event.data;
                 frameCount++;
+                totalBytes += imageData.size;
                 Msg("frame data len: ", imageData.size);
                 var image = new Image();
                 image.src = URL.createObjectURL(imageData);
index 18a7974a66ef2c1a83dd3ec41a6cda28020f88f9..0866a1aa05938c6b267844c860bbb237adfb7916 100644 (file)
@@ -32,7 +32,7 @@ var upgrader = websocket.Upgrader{
 var frameChan chan []byte
 
 func init() {
-       frameChan = make(chan []byte, 32)
+       frameChan = make(chan []byte, 1024)
 }
 
 const (
@@ -105,6 +105,8 @@ func streamHandler(w http.ResponseWriter, r *http.Request) {
        var err error
        var ws *websocket.Conn
 
+       log.Printf("stream\n")
+
        ws, err = upgrader.Upgrade(w, r, nil)
        if err != nil {
                log.Printf("err: %v", err)
@@ -117,7 +119,9 @@ func streamHandler(w http.ResponseWriter, r *http.Request) {
                var data []byte
                select {
                case data = <-frameChan:
-               case <-time.After(1 * time.Millisecond):
+        //case <-time.After(1 * time.Millisecond):
+        default:
+            time.Sleep(10 * time.Millisecond)
                        continue
                }