mirror of
https://github.com/arkhipenko/esp32-mjpeg-multiclient-espcam-drivers.git
synced 2025-02-28 09:21:24 +01:00
Code cleanup
This commit is contained in:
parent
87f7216b97
commit
b9451ebb6d
9 changed files with 127 additions and 120 deletions
|
@ -3,3 +3,7 @@
|
|||
// ESP32 has two cores: APPlication core and PROcess core (the one that runs ESP32 SDK stack)
|
||||
#define APP_CPU 1
|
||||
#define PRO_CPU 0
|
||||
|
||||
#define KILOBYTE 1024
|
||||
|
||||
#define SERIAL_RATE 115200
|
||||
|
|
|
@ -2,6 +2,18 @@
|
|||
#include <Arduino.h>
|
||||
#include <ArduinoLog.h>
|
||||
#include <WebServer.h>
|
||||
#include "logging.h"
|
||||
|
||||
#include "esp_camera.h"
|
||||
#include "ov2640.h"
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WifiManager.h>
|
||||
#include <vector>
|
||||
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_sleep.h>
|
||||
#include <driver/rtc_io.h>
|
||||
|
||||
extern SemaphoreHandle_t frameSync;
|
||||
extern WebServer server;
|
||||
|
@ -10,4 +22,4 @@ extern TaskHandle_t tCam; // handles getting picture frames from the camera
|
|||
extern TaskHandle_t tStream;
|
||||
extern uint8_t noActiveClients; // number of active clients
|
||||
|
||||
void handleNotFound();
|
||||
extern const char* STREAMING_URL;
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
#include "definitions.h"
|
||||
#include "references.h"
|
||||
|
||||
#include "esp_camera.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t frame;
|
||||
WiFiClient *client;
|
||||
|
@ -23,6 +21,7 @@ typedef struct {
|
|||
|
||||
void camCB(void* pvParameters);
|
||||
void handleJPGSstream(void);
|
||||
void handleNotFound(void);
|
||||
void streamCB(void * pvParameters);
|
||||
void mjpegCB(void * pvParameters);
|
||||
|
||||
|
|
|
@ -22,9 +22,9 @@
|
|||
build_flags =
|
||||
-D ARDUINO_ARCH_ESP32
|
||||
; pick one of the three
|
||||
-D CAMERA_MULTICLIENT_QUEUE
|
||||
; -D CAMERA_MULTICLIENT_TASK
|
||||
; -D CAMERA_ALL_FRAMES
|
||||
-D CAMERA_MULTICLIENT_QUEUE ; current frames served by a single task
|
||||
; -D CAMERA_MULTICLIENT_TASK ; current frames served by dedicated tasks
|
||||
; -D CAMERA_ALL_FRAMES ; all frames served by dedicated tasks
|
||||
; Includes for the ESP-camera components
|
||||
-I components/esp32-camera/sensors
|
||||
-I components/esp32-camera/sensors/private_include
|
||||
|
@ -74,15 +74,16 @@
|
|||
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-D CAMERA_MODEL_AI_THINKER
|
||||
-D FRAME_SIZE=FRAMESIZE_VGA
|
||||
-D FPS=10
|
||||
-D WSINTERVAL=100
|
||||
-D MAX_CLIENTS=10
|
||||
-D JPEG_QUALITY=16 ; 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=0
|
||||
-D DISABLE_LOGGING
|
||||
-D WM_NODEBUG
|
||||
-D CAMERA_MODEL_AI_THINKER ; camera model
|
||||
-D FRAME_SIZE=FRAMESIZE_VGA ; frame size
|
||||
-D XCLK_FREQ=20000000 ; frame acquisition rate clock
|
||||
-D FPS=10 ; desired FPS, not to exceed (may be lower)
|
||||
-D WSINTERVAL=100 ; webserver processing rate
|
||||
-D MAX_CLIENTS=10 ; max number of streaming clients
|
||||
-D JPEG_QUALITY=16 ; JPEG picture quality - 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=0 ; LOG level for ArduinoLog
|
||||
-D DISABLE_LOGGING ; Disable logging completely
|
||||
-D WM_NODEBUG ; Disable logging completely for WiFi Manager as well
|
||||
|
||||
|
||||
[env:ai-thinker-cam-debug]
|
||||
|
@ -106,14 +107,15 @@
|
|||
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-D CAMERA_MODEL_AI_THINKER
|
||||
-D FRAME_SIZE=FRAMESIZE_VGA
|
||||
-D FPS=10
|
||||
-D WSINTERVAL=100
|
||||
-D MAX_CLIENTS=10
|
||||
-D JPEG_QUALITY=16 ; 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=6
|
||||
-D BENCHMARK
|
||||
-D CAMERA_MODEL_AI_THINKER ; camera model
|
||||
-D FRAME_SIZE=FRAMESIZE_VGA ; frame size
|
||||
-D XCLK_FREQ=20000000 ; frame acquisition rate clock
|
||||
-D FPS=10 ; desired FPS, not to exceed (may be lower)
|
||||
-D WSINTERVAL=100 ; webserver processing rate
|
||||
-D MAX_CLIENTS=10 ; max number of streaming clients
|
||||
-D JPEG_QUALITY=16 ; JPEG picture quality - 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=6 ; LOG level for ArduinoLog
|
||||
-D BENCHMARK ; Print streaming benchmarking information
|
||||
|
||||
|
||||
[env:esp-eye]
|
||||
|
@ -137,16 +139,17 @@
|
|||
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-D CAMERA_MODEL_ESP_EYE
|
||||
; -D FLIP_VERTICALLY
|
||||
-D FRAME_SIZE=FRAMESIZE_VGA
|
||||
-D FPS=10
|
||||
-D WSINTERVAL=100
|
||||
-D MAX_CLIENTS=10
|
||||
-D JPEG_QUALITY=24 ; 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=0
|
||||
-D DISABLE_LOGGING
|
||||
-D WM_NODEBUG
|
||||
-D CAMERA_MODEL_ESP_EYE ; camera model
|
||||
; -D FLIP_VERTICALLY ; flip picture vertically after capture
|
||||
-D FRAME_SIZE=FRAMESIZE_VGA ; frame size
|
||||
-D XCLK_FREQ=20000000 ; frame acquisition rate clock
|
||||
-D FPS=10 ; desired FPS, not to exceed (may be lower)
|
||||
-D WSINTERVAL=100 ; webserver processing rate
|
||||
-D MAX_CLIENTS=10 ; max number of streaming clients
|
||||
-D JPEG_QUALITY=24 ; JPEG picture quality - 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=0 ; LOG level for ArduinoLog
|
||||
-D DISABLE_LOGGING ; Disable logging completely
|
||||
-D WM_NODEBUG ; Disable logging completely for WiFi Manager as well
|
||||
|
||||
|
||||
[env:esp-eye-debug]
|
||||
|
@ -170,16 +173,17 @@
|
|||
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-D CAMERA_MODEL_ESP_EYE
|
||||
-D FLIP_VERTICALLY
|
||||
-D FRAME_SIZE=FRAMESIZE_SVGA
|
||||
-D FPS=10
|
||||
-D WSINTERVAL=100
|
||||
-D MAX_CLIENTS=10
|
||||
-D JPEG_QUALITY=32 ; 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=6
|
||||
-D WM_DEBUG_LEVEL=WM_DEBUG_VERBOSE
|
||||
-D BENCHMARK
|
||||
-D CAMERA_MODEL_ESP_EYE ; camera model
|
||||
-D FLIP_VERTICALLY ; flip picture vertically after capture
|
||||
-D FRAME_SIZE=FRAMESIZE_SVGA ; frame size
|
||||
-D XCLK_FREQ=20000000 ; frame acquisition rate clock
|
||||
-D FPS=10 ; desired FPS, not to exceed (may be lower)
|
||||
-D WSINTERVAL=100 ; webserver processing rate
|
||||
-D MAX_CLIENTS=10 ; max number of streaming clients
|
||||
-D JPEG_QUALITY=32 ; JPEG picture quality - 0-63 lower means higher qualityr means higher quality
|
||||
-D LOG_LEVEL=6 ; LOG level for ArduinoLog
|
||||
-D WM_DEBUG_LEVEL=WM_DEBUG_VERBOSE ; LOG level for WiFi Manager
|
||||
-D BENCHMARK ; Print streaming benchmarking information
|
||||
|
||||
|
||||
; EXAMPLE of the additional board configuration
|
||||
|
@ -207,12 +211,14 @@
|
|||
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-D CAMERA_MODEL_WROVER_KIT
|
||||
; -D FLIP_VERTICALLY
|
||||
-D FRAME_SIZE=FRAMESIZE_HVGA
|
||||
-D FPS=5
|
||||
-D WSINTERVAL=100
|
||||
-D MAX_CLIENTS=10
|
||||
-D JPEG_QUALITY=24 ; 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=0
|
||||
-D DISABLE_LOGGING
|
||||
-D CAMERA_MODEL_WROVER_KIT ; camera model
|
||||
; -D FLIP_VERTICALLY ; flip picture vertically after capture
|
||||
-D FRAME_SIZE=FRAMESIZE_HVGA ; frame size
|
||||
-D XCLK_FREQ=20000000 ; frame acquisition rate clock
|
||||
-D FPS=10 ; desired FPS, not to exceed (may be lower)
|
||||
-D WSINTERVAL=100 ; webserver processing rate
|
||||
-D MAX_CLIENTS=10 ; max number of streaming clients
|
||||
-D JPEG_QUALITY=24 ; JPEG picture quality - 0-63 lower means higher quality
|
||||
-D LOG_LEVEL=0 ; LOG level for ArduinoLog
|
||||
-D DISABLE_LOGGING ; Disable logging completely
|
||||
-D WM_NODEBUG ; Disable logging completely for WiFi Manager as well
|
||||
|
|
|
@ -13,18 +13,7 @@
|
|||
|
||||
#include "definitions.h"
|
||||
#include "references.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include "esp_camera.h"
|
||||
#include "ov2640.h"
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WifiManager.h>
|
||||
#include <vector>
|
||||
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_sleep.h>
|
||||
#include <driver/rtc_io.h>
|
||||
|
||||
#include "credentials.h"
|
||||
#include "streaming.h"
|
||||
|
@ -51,31 +40,12 @@ uint8_t noActiveClients; // number of active clients
|
|||
// frameSync semaphore is used to prevent streaming buffer as it is replaced with the next frame
|
||||
SemaphoreHandle_t frameSync = NULL;
|
||||
|
||||
void handleNotFound();
|
||||
|
||||
|
||||
// ==== Handle invalid URL requests ============================================
|
||||
void handleNotFound()
|
||||
{
|
||||
String message = "Server is running!\n\n";
|
||||
message += "URI: ";
|
||||
message += server.uri();
|
||||
message += "\nMethod: ";
|
||||
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += "\nArguments: ";
|
||||
message += server.args();
|
||||
message += "\n";
|
||||
server.send(200, "text / plain", message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ==== SETUP method ==================================================================
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
|
||||
// Setup Serial connection:
|
||||
Serial.begin(115200);
|
||||
Serial.begin(SERIAL_RATE);
|
||||
delay(500); // wait for a bit to let Serial connect
|
||||
|
||||
setupLogging();
|
||||
|
@ -83,7 +53,7 @@ void setup()
|
|||
Log.trace("\n\nMulti-client MJPEG Server\n");
|
||||
Log.trace("setup: total heap : %d\n", ESP.getHeapSize());
|
||||
Log.trace("setup: free heap : %d\n", ESP.getFreeHeap());
|
||||
Log.trace("setup: free psram : %d\n", ESP.getPsramSize());
|
||||
Log.trace("setup: total psram : %d\n", ESP.getPsramSize());
|
||||
Log.trace("setup: free psram : %d\n", ESP.getFreePsram());
|
||||
|
||||
static camera_config_t camera_config = {
|
||||
|
@ -104,8 +74,7 @@ void setup()
|
|||
.pin_href = HREF_GPIO_NUM,
|
||||
.pin_pclk = PCLK_GPIO_NUM,
|
||||
|
||||
// .xclk_freq_hz = 16000000,
|
||||
.xclk_freq_hz = 20000000,
|
||||
.xclk_freq_hz = XCLK_FREQ,
|
||||
.ledc_timer = LEDC_TIMER_0,
|
||||
.ledc_channel = LEDC_CHANNEL_0,
|
||||
.pixel_format = PIXFORMAT_JPEG,
|
||||
|
@ -160,15 +129,15 @@ void setup()
|
|||
ip = WiFi.localIP();
|
||||
Log.verbose(F("setup: WiFi connected\n"));
|
||||
// Log.verbose("Stream Link: http://%S/mjpeg/1\n\n", ip.toString());
|
||||
Serial.printf("Stream Link: http://%s/mjpeg/1\n\n", ip.toString().c_str());
|
||||
Serial.printf("Stream Link: http://%s%s\n\n", ip.toString().c_str(), STREAMING_URL);
|
||||
|
||||
// Start main streaming RTOS task
|
||||
xTaskCreatePinnedToCore(
|
||||
mjpegCB,
|
||||
"mjpeg",
|
||||
3 * 1024,
|
||||
3 * KILOBYTE,
|
||||
NULL,
|
||||
2,
|
||||
tskIDLE_PRIORITY + 2,
|
||||
&tMjpeg,
|
||||
PRO_CPU);
|
||||
|
||||
|
|
|
@ -13,6 +13,8 @@ volatile uint32_t frameNumber;
|
|||
frameChunck_t* fstFrame = NULL; // first frame
|
||||
frameChunck_t* curFrame = NULL; // current frame being captured by the camera
|
||||
|
||||
const char* STREAMING_URL = "/mjpeg/1";
|
||||
|
||||
void mjpegCB(void* pvParameters) {
|
||||
TickType_t xLastWakeTime;
|
||||
const TickType_t xFrequency = pdMS_TO_TICKS(WSINTERVAL);
|
||||
|
@ -26,14 +28,14 @@ void mjpegCB(void* pvParameters) {
|
|||
xTaskCreatePinnedToCore(
|
||||
camCB, // callback
|
||||
"cam", // name
|
||||
4096, // stacj size
|
||||
4 * KILOBYTE, // stack size
|
||||
NULL, // parameters
|
||||
2, // priority
|
||||
tskIDLE_PRIORITY + 2, // priority
|
||||
&tCam, // RTOS task handle
|
||||
APP_CPU); // core
|
||||
|
||||
// Registering webserver handling routines
|
||||
server.on("/mjpeg/1", HTTP_GET, handleJPGSstream);
|
||||
server.on(STREAMING_URL, HTTP_GET, handleJPGSstream);
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
// Starting webserver
|
||||
|
@ -54,6 +56,13 @@ void mjpegCB(void* pvParameters) {
|
|||
|
||||
|
||||
// ==== Memory allocator that takes advantage of PSRAM if present =======================
|
||||
char* allocatePSRAM(size_t aSize) {
|
||||
if ( psramFound() && ESP.getFreePsram() > aSize ) {
|
||||
return (char*) ps_malloc(aSize);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* allocateMemory(char* aPtr, size_t aSize, bool fail, bool psramOnly) {
|
||||
|
||||
// Since current buffer is too smal, free it
|
||||
|
@ -65,25 +74,19 @@ char* allocateMemory(char* aPtr, size_t aSize, bool fail, bool psramOnly) {
|
|||
char* ptr = NULL;
|
||||
|
||||
if ( psramOnly ) {
|
||||
if ( psramFound() && ESP.getFreePsram() > aSize ) {
|
||||
ptr = (char*) ps_malloc(aSize);
|
||||
}
|
||||
ptr = allocatePSRAM(aSize);
|
||||
}
|
||||
else {
|
||||
// If memory requested is more than 2/3 of the currently free heap, try PSRAM immediately
|
||||
if ( aSize > ESP.getFreeHeap() * 2 / 3 ) {
|
||||
if ( psramFound() && ESP.getFreePsram() > aSize ) {
|
||||
ptr = (char*) ps_malloc(aSize);
|
||||
}
|
||||
ptr = allocatePSRAM(aSize);
|
||||
}
|
||||
else {
|
||||
// Enough free heap - let's try allocating fast RAM as a buffer
|
||||
ptr = (char*) malloc(aSize);
|
||||
|
||||
// If allocation on the heap failed, let's give PSRAM one more chance:
|
||||
if ( ptr == NULL && psramFound() && ESP.getFreePsram() > aSize) {
|
||||
ptr = (char*) ps_malloc(aSize);
|
||||
}
|
||||
if ( ptr == NULL ) ptr = allocatePSRAM(aSize);
|
||||
}
|
||||
}
|
||||
// Finally, if the memory pointer is NULL, we were not able to allocate any memory, and that is a terminal condition.
|
||||
|
@ -95,3 +98,17 @@ char* allocateMemory(char* aPtr, size_t aSize, bool fail, bool psramOnly) {
|
|||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
// ==== Handle invalid URL requests ============================================
|
||||
void handleNotFound() {
|
||||
String message = "Server is running!\n\n";
|
||||
message += "URI: ";
|
||||
message += server.uri();
|
||||
message += "\nMethod: ";
|
||||
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += "\nArguments: ";
|
||||
message += server.args();
|
||||
message += "\n";
|
||||
server.send(200, "text / plain", message);
|
||||
}
|
||||
|
||||
|
|
|
@ -140,9 +140,9 @@ void handleJPGSstream(void)
|
|||
int rc = xTaskCreatePinnedToCore(
|
||||
streamCB,
|
||||
"streamCB",
|
||||
3 * 1024,
|
||||
3 * KILOBYTE,
|
||||
(void*) info,
|
||||
2,
|
||||
tskIDLE_PRIORITY + 2,
|
||||
&info->task,
|
||||
APP_CPU);
|
||||
if ( rc != pdPASS ) {
|
||||
|
|
|
@ -29,10 +29,10 @@ void camCB(void* pvParameters) {
|
|||
// Creating task to push the stream to all connected clients
|
||||
xTaskCreatePinnedToCore(
|
||||
streamCB,
|
||||
"strmCB",
|
||||
4096,
|
||||
"streamCB",
|
||||
4 * KILOBYTE,
|
||||
NULL, //(void*) handler,
|
||||
2,
|
||||
tskIDLE_PRIORITY + 2,
|
||||
&tStream,
|
||||
APP_CPU);
|
||||
// PRO_CPU);
|
||||
|
|
|
@ -133,10 +133,10 @@ void handleJPGSstream(void)
|
|||
// Creating task to push the stream to all connected clients
|
||||
int rc = xTaskCreatePinnedToCore(
|
||||
streamCB,
|
||||
"strmCB",
|
||||
3 * 1024,
|
||||
"streamCB",
|
||||
3 * KILOBYTE,
|
||||
(void*) info,
|
||||
2,
|
||||
tskIDLE_PRIORITY + 2,
|
||||
&info->task,
|
||||
APP_CPU);
|
||||
if ( rc != pdPASS ) {
|
||||
|
|
Loading…
Add table
Reference in a new issue