Kaynağa Gözat

Run raw camera preview capture outside the HTTP server task

SybexXx 1 gün önce
ebeveyn
işleme
92c5a178e9
1 değiştirilmiş dosya ile 35 ekleme ve 29 silme
  1. 35 29
      code/main/server_main.cpp

+ 35 - 29
code/main/server_main.cpp

@@ -564,53 +564,59 @@ esp_err_t img_tmp_handler(httpd_req_t *req)
 }
 
 
+static esp_err_t raw_jpg_capture_worker(httpd_req_t *req)
+{
+    bool previewCapture = false;
+    char query[64] = "";
+    char preview[8] = "";
+    if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK &&
+        httpd_query_key_value(query, "preview", preview, sizeof(preview)) == ESP_OK)
+    {
+        previewCapture = (strcmp(preview, "1") == 0);
+    }
+    return GetRawJPG(req, previewCapture);
+}
+
 esp_err_t img_tmp_virtual_handler(httpd_req_t *req)
 {
-    #ifdef DEBUG_DETAIL_ON      
-        LogFile.WriteHeapInfo("img_tmp_virtual_handler - Start");  
-    #endif
+#ifdef DEBUG_DETAIL_ON
+    LogFile.WriteHeapInfo("img_tmp_virtual_handler - Start");
+#endif
 
     char filepath[50];
-
     ESP_LOGD(TAG, "uri: %s", req->uri);
 
     char *base_path = (char*) req->user_ctx;
-    std::string filetosend(base_path);
-
     const char *filename = get_path_from_uri(filepath, base_path,
-                                             req->uri  + sizeof("/img_tmp/") - 1, sizeof(filepath));    
-    ESP_LOGD(TAG, "1 uri: %s, filename: %s, filepath: %s", req->uri, filename, filepath);
+                                             req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
+    if (filename == NULL)
+    {
+        httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long");
+        return ESP_FAIL;
+    }
 
-    filetosend = std::string(filename);
-    ESP_LOGD(TAG, "File to upload: %s", filetosend.c_str());
+    const std::string fileName(filename);
+    ESP_LOGD(TAG, "File to upload: %s", fileName.c_str());
 
-    // Only the test_take URL opts into temporary preview settings.
-    if (filetosend == "raw.jpg")
+    // A raw camera capture can take several seconds. Keep it off the single
+    // HTTP server task so status, log and static-file requests remain responsive.
+    if (fileName == "raw.jpg")
     {
-        bool previewCapture = false;
-        char query[64] = "";
-        char preview[8] = "";
-        if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK &&
-            httpd_query_key_value(query, "preview", preview, sizeof(preview)) == ESP_OK)
-        {
-            previewCapture = (strcmp(preview, "1") == 0);
-        }
-        return GetRawJPG(req, previewCapture);
-    } 
+        return dispatch_camera_http_request_async(req, raw_jpg_capture_worker, "raw capture");
+    }
 
-    // Serve alg.jpg, alg_roi.jpg or digit and analog ROIs
-    if (ESP_OK == GetJPG(filetosend, req))
+    // Serve alg.jpg, alg_roi.jpg or digit and analog ROIs.
+    if (ESP_OK == GetJPG(fileName, req))
         return ESP_OK;
 
-    #ifdef DEBUG_DETAIL_ON      
-        LogFile.WriteHeapInfo("img_tmp_virtual_handler - Done");   
-    #endif
+#ifdef DEBUG_DETAIL_ON
+    LogFile.WriteHeapInfo("img_tmp_virtual_handler - Done");
+#endif
 
-    // File was not served already --> serve with img_tmp_handler
+    // File was not served already --> serve img_tmp file.
     return img_tmp_handler(req);
 }
 
-
 esp_err_t sysinfo_handler(httpd_req_t *req)
 {
     // Take one snapshot of resource and card information for this response.