How s60sc/esp32-cam_mjpeg2sd works
Purpose and Firmware Entry
This is a monolithic Arduino firmware application for ESP32/ESP32-S3 camera boards. It captures JPEG frames, records them as AVI on SD storage, streams live or recorded media, and optionally integrates sensors, actuators, notifications, and remote services. The sketch entry is ESP32-CAM_MJPEG2SD.ino; compile-time board/feature selection and shared state are centered in appGlobals.h, globals.h, and camera_pins.h.
Startup, Configuration, and Runtime Coordination
Startup initializes hardware, storage, camera, networking, and enabled feature modules, then runs concurrent firmware tasks/services. Persistent settings are managed separately from compile-time feature flags: prefs.cpp owns configuration persistence, while setupAssist.cpp supports first-boot/AP setup. appSpecific.cpp provides application-level setup behavior. Configuration changes made through the UI are persisted and commonly require reboot to reinitialize hardware-dependent services.
Camera Capture and Recording Pipeline
The core media path is implemented by mjpeg2sd.cpp and avi.cpp. Camera JPEG frames are acquired, buffered in PSRAM, and assembled into sector-aligned AVI output on SD to reduce write overhead. Optional audio and telemetry are synchronized into associated media/artifact files. Recording may be manually requested, continuously scheduled, or event-triggered. Camera board mappings form the hardware boundary in camera_pins.h.
Motion and Event Control
motionDetect.cpp samples camera JPEGs, decodes reduced grayscale representations, and compares them with prior samples to decide when to start or stop recording. This control path can be augmented by external sensors/peripherals and can drive alerting or recording lifecycle decisions. The recording subsystem is therefore a shared resource: live streaming may coexist, but recording takes priority under constrained CPU, PSRAM, SD, and network bandwidth.
Media Delivery and Storage Access
Browser-facing HTTP/WebSocket control and MJPEG playback/live streaming are served by webServer.cpp and streamServer.cpp; static UI assets reside in data/MJPEG2SD.htm, data/Auxil.htm, and data/common.js. utilsFS.cpp mediates SD-backed file operations. Recorded AVI files and companion telemetry artifacts are browsed, downloaded, deleted, or transferred. Alternative delivery/access boundaries include rtsp.cpp for RTSP and webDav.cpp for SD-card WebDAV access.
Network, Remote Control, and Integrations
Wi-Fi is the normal transport, with optional Ethernet on supported ESP32-S3 hardware. Network services reuse the selected interface. External integrations are modular and feature-gated: mqtt.cpp publishes state/accepts commands, ftp.cpp transfers recordings, smtp.cpp and telegram.cpp send alerts, and externalHeartbeat.cpp posts device status to an external endpoint. HTTPS/TLS certificate handling is isolated in certificates.cpp; it materially increases memory and processing requirements.
Optional Device and Extension Subsystems
Hardware expansion is separated from core capture: peripherals.cpp handles sensors/actuators, periphsI2C.cpp handles shared or separate I2C devices, audio.cpp records/streams microphone audio and supports intercom, and telemetry.cpp emits recording-aligned sensor data. uart.cpp supports an auxiliary-board boundary for remote-control, servo, or photogrammetry commands; mcpwm.cpp supports motor control. photogram.cpp coordinates turntable/DSLR-oriented capture workflows.
Main entry points: mjpeg2sd.cpp MJPEG capture & recording · ESP32-CAM_MJPEG2SD.ino Arduino entry · utilsFS.cpp SD file access · webServer.cpp HTTP/WebSocket control
Components 20
Firmware core
- Arduino entry
ESP32-CAM_MJPEG2SD.ino - Build flags & state · Board selection, feature flags, and shared state.
appGlobals.h - Preferences
prefs.cpp - Setup assistance
setupAssist.cpp - Application setup
appSpecific.cpp
Media pipeline
- Camera hardware map
camera_pins.h - MJPEG capture & recording · Buffers JPEG frames in PSRAM and controls recording.
mjpeg2sd.cpp - AVI writer · Builds sector-aligned AVI output.
avi.cpp - SD file access
utilsFS.cpp - Motion detection · Analyzes reduced JPEG grayscale samples to control recording.
motionDetect.cpp
Network access
- HTTP/WebSocket control
webServer.cpp - Live & playback streaming
streamServer.cpp - Browser UI assets
data/MJPEG2SD.htm - RTSP delivery
rtsp.cpp - WebDAV SD access
webDav.cpp
Optional extensions
- Sensors & actuators
peripherals.cpp - Audio & intercom · Provides recording, streaming, and intercom audio.
audio.cpp - Recording telemetry
telemetry.cpp - Remote integrations · MQTT, FTP, alerting, heartbeat, and TLS-enabled remote access.
mqtt.cpp - Photogrammetry control
photogram.cpp
Connections 24
- Arduino entry → Build flags & state: uses
- Arduino entry → Preferences: loads settings
- Arduino entry → Setup assistance: first boot
- Arduino entry → Application setup: initializes
- Arduino entry → MJPEG capture & recording: starts
- Build flags & state → Camera hardware map: selects board map
- MJPEG capture & recording → Camera hardware map: acquires JPEGs
- MJPEG capture & recording → AVI writer: writes frames
- AVI writer → SD file access: stores AVI
- Motion detection → MJPEG capture & recording: starts/stops recording
- Sensors & actuators → Motion detection: augments events
- Audio & intercom → AVI writer: synchronized audio
- Recording telemetry → SD file access: companion artifacts
- HTTP/WebSocket control → Browser UI assets: serves
- Browser UI assets → HTTP/WebSocket control: control changes
- HTTP/WebSocket control → Preferences: persists settings
- Live & playback streaming → MJPEG capture & recording: live frames
- Live & playback streaming → SD file access: recorded playback
- HTTP/WebSocket control → SD file access: browse/download/delete
- RTSP delivery → MJPEG capture & recording: streams frames
- WebDAV SD access → SD file access: exposes SD card
- Remote integrations → MJPEG capture & recording: commands & state
- Remote integrations → SD file access: transfers recordings
- Photogrammetry control → MJPEG capture & recording: coordinates captures