Skip to content

MagicInUse/MagicPi-NVR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŽ₯ MagicPi NVR - Network Video Recorder

A high-performance surveillance system featuring real-time binary JPEG streaming from ESP32-CAM devices with live frontend video display, centralized recording, and complete web dashboard control.

✨ Key Features

πŸ”΄ Live Video Streaming - Real-time JPEG frames via WebSocket binary streaming
πŸ“Ή H.264 Recording - FFmpeg transcoding with automatic file management
πŸŽ›οΈ Web Dashboard - React frontend with device control and live video display
πŸ” Secure HTTPS - SSL encryption for all communications
⚑ Real-time Status - Device heartbeats, connection monitoring, and status updates
🎯 ESP32-CAM Support - Optimized for ESP32-CAM binary streaming protocol

πŸš€ Quick Start

Prerequisites

  • Node.js 18+
  • npm
  • OpenSSL (for SSL certificates)

Installation & Deployment

# 1. Install all dependencies
npm run install:all

# 2. Generate SSL certificates (development)
npm run setup:certs

# 3. Build the complete application
npm run build

# 4. Start the server
npm start

The application will be available at: https://localhost:3443

πŸ“ Project Structure

MagicPi-NVR/
β”œβ”€β”€ client/                 # React TypeScript frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/     # React components
β”‚   β”‚   β”œβ”€β”€ services/       # API services
β”‚   β”‚   └── types/          # TypeScript interfaces
β”‚   └── dist/              # Built frontend (after npm run build)
β”œβ”€β”€ pi-cam-server/         # Node.js TypeScript backend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/           # REST API controllers
β”‚   β”‚   β”œβ”€β”€ services/      # Business logic services  
β”‚   β”‚   β”œβ”€β”€ middleware/    # Express middleware
β”‚   β”‚   └── types/         # TypeScript interfaces
β”‚   β”œβ”€β”€ dist/              # Built backend + deployed frontend
β”‚   β”‚   β”œβ”€β”€ client/        # Frontend files (copied during build)
β”‚   β”‚   └── *.js           # Compiled backend files
β”‚   └── security/          # SSL certificates
└── package.json           # Root workspace configuration

πŸ› οΈ Development

Frontend Development

npm run dev:frontend      # Start Vite dev server (http://localhost:5173)

Backend Development

npm run dev:backend       # Start nodemon with auto-reload

Full Development Setup

# Terminal 1: Frontend dev server
npm run dev:frontend

# Terminal 2: Backend dev server  
npm run dev:backend

πŸ—οΈ Build Process

The build process creates a single deployable package:

  1. Frontend Build: React app compiled to static files
  2. Backend Build: TypeScript compiled to JavaScript
  3. Integration: Frontend copied to backend's dist/client/
  4. Result: Single pi-cam-server/dist/ folder ready for deployment

Build Commands

# Build everything for production
npm run build

# Or step by step:
cd client && npm run build              # Build frontend
cd pi-cam-server && npm run build      # Build backend  
cd pi-cam-server && npm run copy:frontend  # Copy frontend to backend

πŸ”§ Configuration

SSL Certificates

For development:

npm run setup:certs  # Generates self-signed certificates

For production: Replace files in pi-cam-server/security/:

  • key.pem - Private key
  • cert.pem - Certificate

Server Configuration

Edit pi-cam-server/src/config.ts:

  • Server ports and host
  • Recording settings
  • Video processing options
  • Security settings

πŸ“¦ Deployment

Single Server Deployment

  1. Build the application:

    npm run build
  2. Copy the pi-cam-server/dist/ folder to your server

  3. Install production dependencies:

    cd pi-cam-server
    npm install --production
  4. Add proper SSL certificates to security/

  5. Start the server:

    npm start

Docker Deployment (Future)

Docker support will be added in future versions for easier deployment.

🎯 Features

πŸš€ Binary Video Streaming Architecture (Latest Implementation)

  • Real-time JPEG Streaming: ESP32-CAM devices send raw binary JPEG frames via WebSocket
  • Automatic Format Detection: Server detects JPEG magic numbers (0xFF, 0xD8) vs JSON commands
  • Live Video Recording: Binary frames processed through FFmpeg pipeline to MP4 files
  • High Performance: Eliminates JSON parsing overhead for video data transmission
  • Stream Processing: PassThrough streams with MJPEG input format for efficient processing

Frontend (React + TypeScript)

  • πŸ“± Responsive surveillance dashboard
  • πŸŽ₯ Real-time video streaming interface
  • πŸ“ Recording browser with hierarchical navigation
  • βš™οΈ Device configuration interface
  • πŸ”„ WebSocket real-time updates
  • πŸ“Š Device status monitoring

Backend (Node.js + TypeScript)

  • πŸ›‘οΈ Device registration and authentication
  • 🎬 FFmpeg Binary Stream Processing: Direct MJPEG input from binary WebSocket data
  • πŸ” HTTPS and WebSocket secure connections
  • πŸ“‘ mDNS service discovery
  • 🧹 Automatic cleanup services
  • πŸ”„ Dual WebSocket support (frontend + devices)
  • πŸ“¦ Binary Data Handling: Efficient processing of raw JPEG frames
  • πŸŽ₯ H.264 MP4 Recording: Real-time encoding with configurable quality settings

ESP32-CAM Client Features

  • πŸ“Ή Binary JPEG Transmission: Sends raw camera frames via wsClient.sendBinary()
  • πŸ”„ Automatic server discovery and registration
  • πŸ’€ Power-efficient operation modes
  • πŸ”§ Remote configuration capabilities
  • πŸ“± Hardware motion sensor support

οΏ½ Technical Implementation

Binary Streaming Architecture

The system implements a high-performance binary streaming architecture for real-time video transmission:

graph LR
    A[ESP32-CAM] -->|Binary JPEG| B[WebSocket]
    B -->|Magic Number Detection| C[Server Handler]
    C -->|JPEG Data| D[PassThrough Stream]
    D -->|MJPEG Input| E[FFmpeg Process]
    E -->|H.264 Encode| F[MP4 File]
Loading

Key Components

1. ESP32-CAM Binary Transmission

// Sends raw JPEG buffer data
wsClient.sendBinary((const char*)frameBuffer->buf, frameBuffer->len);

2. Server-side JPEG Detection & Live Streaming

// Detect JPEG magic numbers (0xFF, 0xD8)
if (data[0] === 0xFF && data[1] === 0xD8) {
    // Process as binary JPEG frame for recording
    await this.videoProcessor.writeFrame(deviceId, data);
    
    // Forward frame to subscribed frontend clients for live viewing
    this.forwardVideoFrameToFrontend(deviceId, data);
}

3. Frontend Live Video Subscription

// Subscribe to live video stream
const subscribeMessage = {
    type: 'subscribe',
    deviceId: deviceId
};
this.ws.send(JSON.stringify(subscribeMessage));

// Handle incoming video frames
ws.onmessage = (event) => {
    if (event.data instanceof ArrayBuffer) {
        // Display live JPEG frame in video player
        this.handleVideoFrame(new Uint8Array(event.data));
    }
};

4. FFmpeg Stream Processing

// Direct MJPEG input from WebSocket stream
ffmpeg()
    .inputFormat('mjpeg')
    .input('pipe:0')
    .videoCodec('libx264')
    .outputOptions(['-preset ultrafast', '-tune zerolatency'])
    .fps(10)
    .save(outputPath)

Performance Benefits

  • Zero JSON Overhead: Raw binary transmission eliminates parsing delays
  • Real-time Live Streaming: JPEG frames forwarded directly to frontend via WebSocket
  • Direct Stream Processing: JPEG frames pipe directly to FFmpeg without intermediate storage
  • Dual Output: Simultaneous live viewing and H.264 recording from single stream
  • Memory Efficient: PassThrough streams prevent buffer accumulation

Live Streaming Pipeline

  1. Frame Capture: ESP32-CAM captures JPEG at configured quality
  2. Binary Transmission: Raw frame data sent via WebSocket to server
  3. Magic Number Detection: Server identifies JPEG vs command data
  4. Dual Processing:
    • Live Stream: Frame forwarded to subscribed frontend clients
    • Recording: Frame written to FFmpeg PassThrough stream
  5. Frontend Display: Browser receives binary JPEG and displays in real-time
  6. H.264 Recording: Simultaneous conversion to MP4 with configurable settings
  7. File Management: Automatic directory creation and file rotation

�🚨 Troubleshooting

Build Errors

  • Ensure all dependencies installed: npm run install:all
  • Clear build cache: npm run clean && npm run build

SSL Certificate Issues

  • Regenerate certificates: npm run setup:certs
  • Check pi-cam-server/security/ folder exists

Frontend Not Loading

  • Verify build completed: Check pi-cam-server/dist/client/ exists
  • Check server logs for frontend path detection

Port Conflicts

  • Default HTTPS port: 3443
  • Modify in pi-cam-server/src/config.ts if needed

οΏ½ Next Steps - Frontend Integration

Phase 1: Real-time Binary Stream Display (In Progress)

The backend binary streaming architecture is complete and operational. The next development phase focuses on frontend integration:

🎯 Immediate Goals

  1. Live Stream Viewer Component

    • Create React component to display real-time JPEG frames
    • WebSocket connection to receive binary frame data
    • Canvas-based rendering for smooth playback
  2. Recording Status Integration

    • Real-time recording indicators on device dashboard
    • Live file size and duration tracking
    • Recording start/stop controls
  3. Binary Stream Monitoring

    • Frame rate statistics and quality metrics
    • Network throughput visualization
    • Device performance monitoring

πŸ”§ Technical Implementation Plan

// Frontend WebSocket handler for binary frames
const handleBinaryFrame = (data: ArrayBuffer) => {
  const blob = new Blob([data], { type: 'image/jpeg' });
  const imageUrl = URL.createObjectURL(blob);
  updateCanvasWithFrame(imageUrl);
};

πŸ“Š Development Status

  • βœ… Backend Binary Streaming: Complete
  • βœ… ESP32-CAM Binary Transmission: Complete
  • βœ… FFmpeg Pipeline: Complete
  • βœ… MP4 Recording: Complete
  • 🚧 Frontend Live Display: Next Phase
  • 🚧 Stream Controls: Next Phase
  • 🚧 Recording Management UI: Next Phase

Phase 2: Enhanced UI Features (Planned)

  • Advanced video player with scrubbing
  • Multi-camera grid view
  • Motion detection visualization
  • Mobile-responsive controls

License

MIT License - see LICENSE file for details.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

Ready for deployment! πŸš€ - Wireless Camera Security System

A complete, secure wireless camera system consisting of a Raspberry Pi 5 host server and multiple ESP32-CAM clients. This system provides automated video recording, motion detection, and secure wireless communication for comprehensive surveillance coverage.

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    WiFi/TLS    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   ESP32-CAM     │◄──────────────►│ Raspberry Pi 5  β”‚
β”‚   (Client)      β”‚   WebSocket    β”‚ (Host Server)   β”‚
β”‚                 β”‚                β”‚                 β”‚
β”‚ β€’ Motion Detectionβ”‚              β”‚ β€’ Video Recordingβ”‚
β”‚ β€’ Camera Streamingβ”‚              β”‚ β€’ Device Managementβ”‚
β”‚ β€’ Deep Sleep    β”‚                β”‚ β€’ Web API       β”‚
β”‚ β€’ TLS Security  β”‚                β”‚ β€’ mDNS Service  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                                   β”‚
        β”‚            Multiple Clients       β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Features

Raspberry Pi 5 Host Server

  • Secure Communication: HTTPS/WSS with TLS encryption
  • Device Management: Automatic discovery and registration
  • Video Recording: H.264 encoding with FFmpeg
  • RESTful API: Complete device control and monitoring
  • Automated Cleanup: Configurable retention policies
  • mDNS Discovery: Zero-configuration networking
  • Multi-client Support: Handle up to 50 ESP32-CAM devices

ESP32-CAM Clients

  • Motion-Triggered Recording: PIR sensor integration
  • Always-On Mode: Continuous monitoring without motion sensor
  • Continuous Mode: Non-stop streaming for critical areas
  • Auto-Detection: Automatically detects hardware and configures mode
  • Power Efficient: Deep sleep with wake-on-motion or timer
  • Auto-Discovery: Finds server via mDNS
  • Secure Registration: API key authentication
  • Real-time Streaming: JPEG over WebSocket
  • Remote Configuration: Server-controlled camera settings
  • OTA Ready: Over-the-air update capable

πŸ“ Project Structure

MagicPi-NVR/
β”œβ”€β”€ pi-cam-server/              # Raspberry Pi 5 Host Server
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ services/           # Core business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ DeviceManager.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ VideoProcessor.ts
β”‚   β”‚   β”‚   └── CleanupService.ts
β”‚   β”‚   β”œβ”€β”€ middleware/         # Authentication & security
β”‚   β”‚   β”‚   └── auth.ts
β”‚   β”‚   β”œβ”€β”€ config.ts          # Configuration settings
β”‚   β”‚   └── server.ts          # Main application
β”‚   β”œβ”€β”€ security/              # SSL certificates
β”‚   β”œβ”€β”€ recordings/            # Video storage
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   └── README.md
β”‚
β”œβ”€β”€ esp32-cam-client/          # ESP32-CAM Client Code
β”‚   β”œβ”€β”€ esp32_cam_client.ino   # Arduino sketch
β”‚   └── README.md
β”‚
β”œβ”€β”€ LICENSE
└── README.md                  # This file

πŸ› οΈ Hardware Requirements

Raspberry Pi 5 (Host Server)

  • Raspberry Pi 5 (4GB+ RAM recommended)
  • MicroSD card (32GB+ Class 10)
  • Ethernet connection or WiFi
  • Power supply (USB-C, 5V/5A)

ESP32-CAM AI-Thinker (Clients)

  • ESP32-CAM AI-Thinker modules
  • PIR motion sensors (HC-SR501)
  • Stable 3.3V power supplies
  • MicroSD cards (optional)

πŸ”§ Quick Start

1. Setup Raspberry Pi Server

# Clone the repository
git clone https://github.com/MagicInUse/MagicPi-NVR.git
cd MagicPi-NVR/pi-cam-server

# Install dependencies
npm install

# Generate SSL certificates
openssl genrsa -out security/key.pem 2048
openssl req -new -x509 -key security/key.pem -out security/cert.pem -days 365 \
  -subj "/C=US/ST=State/L=City/O=Pi Camera Server/CN=$(hostname -I | awk '{print $1}')"

# Install FFmpeg
sudo apt update && sudo apt install ffmpeg

# Start the server
npm run build && npm start

2. Setup ESP32-CAM Clients

  1. Install Arduino IDE with ESP32 support
  2. Install required libraries:
    • ArduinoWebsockets
    • ArduinoJson
  3. Configure the code:
    • Update WiFi credentials
    • Paste server certificate
  4. Upload to ESP32-CAM
  5. Connect motion sensor to GPIO 13

3. Verify System Operation

  1. Check server logs:

    # Server should show:
    # "Device registered: XX:XX:XX:XX:XX:XX"
    # "WebSocket connection established"
  2. Trigger motion detection on ESP32-CAM

  3. Check recordings in /recordings/ directory

πŸ“‘ API Reference

Device Registration

POST /register
Content-Type: application/json

{
  "deviceId": "AA:BB:CC:DD:EE:FF"
}

Get Device Status

GET /device/status
X-API-Key: your-api-key

Update Configuration

PUT /device/config
X-API-Key: your-api-key

{
  "config": {
    "resolution": "VGA",
    "framerate": 15
  }
}

List Recordings

GET /recordings?date=2023-12-01
X-API-Key: your-api-key

πŸ”’ Security Features

  • TLS 1.2+ Encryption: All communications encrypted
  • API Key Authentication: Unique keys per device
  • Certificate Validation: ESP32 clients validate server
  • Secure Headers: HSTS, CSP, and other security headers
  • Rate Limiting: Protection against abuse

πŸ“Ή Recording System

Directory Structure

/recordings/
β”œβ”€β”€ AA-BB-CC-DD-EE-FF/          # Device MAC address
β”‚   β”œβ”€β”€ 2023-12-01/             # Date (YYYY-MM-DD)
β”‚   β”‚   β”œβ”€β”€ 08.mp4              # Hour-based files
β”‚   β”‚   β”œβ”€β”€ 09.mp4
β”‚   β”‚   └── ...
β”‚   └── 2023-12-02/
└── ...

Video Specifications

  • Format: MP4 (H.264)
  • Input: MJPEG from ESP32-CAM
  • Quality: Configurable (CRF 23 default)
  • Resolution: Up to UXGA (1600Γ—1200)
  • Frame Rate: Configurable (10 FPS default)

⚑ Power Management

ESP32-CAM Power Consumption

  • Active (Streaming): ~200-300mA
  • Deep Sleep: ~10-50Β΅A
  • Battery Life: 7-10 hours active, 4-20 months sleep

Optimization Strategies

  • Motion-triggered activation
  • Configurable streaming duration
  • Deep sleep between triggers
  • Optimized camera settings

πŸ”§ Configuration

Server Configuration (pi-cam-server/src/config.ts)

export const config = {
  server: {
    httpsPort: 3443,
    recordingRetentionDays: 7,
    cleanupSchedule: '0 2 * * *'  // Daily at 2 AM
  },
  video: {
    inputFormat: 'mjpeg',
    outputCodec: 'libx264',
    defaultResolution: 'SVGA'
  }
};

Client Configuration (Arduino)

const unsigned long STREAMING_DURATION = 30000; // 30 seconds
const unsigned long FRAME_INTERVAL = 100;       // 10 FPS
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

πŸ“Š Monitoring

Server Statistics

GET /stats
X-API-Key: your-api-key

Returns:

  • Device counts by status
  • Recording statistics
  • System resources
  • Cleanup information

Health Check

GET /health

Log Monitoring

  • Server logs: Console output
  • Client logs: Serial monitor (115200 baud)
  • Recording errors: Automatic logging

πŸ› οΈ Troubleshooting

Common Issues

  1. ESP32-CAM won't connect

    • Check WiFi credentials
    • Verify power supply stability
    • Ensure 2.4GHz network
  2. Server discovery fails

    • Check mDNS service
    • Verify network connectivity
    • Check firewall settings
  3. Recording issues

    • Verify FFmpeg installation
    • Check disk space
    • Review camera settings
  4. Certificate errors

    • Regenerate certificates
    • Check date/time sync
    • Verify certificate format

Debug Mode

Enable verbose logging:

# Server
NODE_ENV=development npm run dev

# Client
Serial.setDebugOutput(true);  // In Arduino code

πŸ”„ Updates and Maintenance

Server Updates

cd pi-cam-server
git pull origin main
npm install
npm run build
sudo systemctl restart pi-camera-server

Client Updates

  • Use Arduino IDE for firmware updates
  • Consider implementing OTA updates
  • Update libraries regularly

Maintenance Tasks

  • Monitor disk usage
  • Check certificate expiration
  • Review cleanup logs
  • Update dependencies

πŸ“ Development

Contributing

  1. Fork the repository
  2. Create feature branch
  3. Make changes with tests
  4. Submit pull request

Adding Features

  • Server: Add services in src/services/
  • Client: Modify Arduino sketch
  • Update documentation

Testing

# Server tests
npm test

# Client tests
# Use Arduino IDE serial monitor

πŸ“„ License

MIT License - see LICENSE file for details.

🀝 Support

Documentation

Issues

  • Check existing issues on GitHub
  • Provide detailed error logs
  • Include hardware specifications
  • Test with minimal configuration

Community

  • GitHub Discussions for questions
  • Wiki for additional documentation
  • Examples in /examples directory

🌟 Acknowledgments

  • ESP32 Camera library by Espressif
  • FFmpeg for video processing
  • Arduino WebSocket library
  • Node.js and TypeScript communities

Made with ❀️ by MagicInUse A repository for ESP32 Cameras and a Raspberry Pi home network video recording client/server interaction.

About

A repository for ESP32 Cameras and a Raspberry Pi home network video recording client/server interaction.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors