ITADN

Enhanced Features: Snapshot Manager + MP4 Muxer

#352Pull Request350d 创建于 2025-06-02
3
350dcommented
## 📋 Overview This pull request transforms v4l2rtspserver into a comprehensive streaming and recording solution with **cross-compilation support**, **advanced snapshot management**, **MP4 recording capabilities**, and **improved H.264 handling**. --- ## 🆕 Major New Features ### 1. **Cross-Compilation for Raspberry Pi** 🔧 - **Added**: Enhanced `.github/workflows/pi-cross.yml` configuration - **Improved**: ARM64 and ARM32 build support for Raspberry Pi - **Benefits**: - Automated cross-compilation in CI/CD - Better compatibility with Pi hardware - Optimized builds for ARM architecture ### 2. **Advanced Snapshot Manager** 📸 #### **New Command Line Options:** - **`-j <filepath>`**: Enable snapshot saving to specified file - **`-J <WxHxI>`**: Configure snapshot resolution and auto-save interval - Format: `640x480x5` (width x height x interval_seconds) - Interval range: 1-60 seconds with validation - **`-d [directory]`**: Enable debug dump mode with optional directory #### **Multi-Format Support:** - **H.264 Mode**: Creates mini-MP4 snapshots from H.264 keyframes - **MJPEG Mode**: Real JPEG snapshots from MJPEG streams - **RAW Mode**: YUV to JPEG conversion for raw formats - **Automatic Detection**: Switches between modes based on stream format #### **Web Endpoint Integration:** - **HTTP Access**: Snapshots available via `/snapshot` endpoint - **Real-time Generation**: On-demand snapshot creation - **MIME Type Support**: Proper Content-Type headers (image/jpeg or video/mp4) - **File Operations**: Optional automatic saving with configurable intervals ### 3. **MP4Muxer - Professional MP4 Recording** 🎬 #### **Correct MP4 Structure:** - **Standard Compliance**: Proper `ftyp → moov → mdat` structure - **Compatibility**: Works with all major players (VLC, QuickTime, FFmpeg, browsers) - **Metadata**: Correct timing, keyframe marking, and duration calculation #### **Dual Usage:** - **Snapshots**: Single-frame MP4 files for H.264 keyframes (mini-MP4 format) - **Video Recording**: Multi-frame MP4 files via `-O <file.mp4>` option - **Emergency Finalization**: SIGINT handler prevents corrupted files #### **Technical Improvements:** - **Fixed mdat sizing**: Eliminates 0xFFFFFFFF corruption - **Proper keyframes**: Only IDR frames marked in `stss` box (fixes macOS thumbnails) - **Accurate timing**: Frame-count based duration instead of fixed 1-second - **Memory efficient**: Streaming write with placeholder moov box ### 4. **Enhanced `-O` Option** 📁 #### **Smart File Format Detection:** ```bash # MP4 recording (new) ./v4l2rtspserver -O recording.mp4 /dev/video0 # Raw H.264 recording (original, fixed) ./v4l2rtspserver -O stream.h264 /dev/video0 ``` #### **Behavior:** - **`.mp4` extension**: Uses MP4Muxer for proper container format - **Other extensions**: Raw H.264 stream (original functionality, improved) - **Auto-detection**: Automatic frame dimension detection when `-W/-H` not specified ### 5. **Intelligent Write Buffering for MP4 Recording** 💾 #### **Smart Buffering Strategy:** - **Memory Buffer**: 1MB in-memory buffer for frame data accumulation - **Keyframe-based Flushing**: Writes to disk only on keyframes (IDR frames) - **Time Constraints**: Maximum 1-second interval between disk writes - **Emergency Protection**: Force flush on program exit (Ctrl+C handling) #### **Performance Benefits:** ```bash # Before: ~300 disk writes + fsync per second (10KB each) # After: ~1-2 disk writes per second (500KB+ each) # Result: ~100x fewer disk operations ``` #### **SD Card Protection:** - **Reduced Wear**: Dramatically fewer write cycles - **Better Performance**: Less I/O blocking during real-time recording - **Longer Lifespan**: Ideal for Raspberry Pi and embedded systems - **Data Safety**: No frame loss even with emergency shutdown #### **Technical Implementation:** - **Buffer Management**: Automatic overflow protection (force flush at 1MB) - **Timing Control**: Configurable flush interval (default: 1 second) - **Resource Safety**: Proper cleanup and memory management - **Error Handling**: Graceful fallback on write failures ### 6. **Code Quality Improvements** 🔧 #### **SIGINT Handling:** - **Emergency Finalization**: Proper MP4 file closure on Ctrl+C - **Data Protection**: Force sync to disk before exit - **No Data Loss**: Prevents corrupted recordings on unexpected shutdown #### **Enhanced H.264 Processing:** - **Better NAL Parsing**: Improved keyframe detection through NAL unit analysis - **SPS/PPS Handling**: Proper parameter set management - **Stream Validation**: Enhanced error checking and validation #### **Memory Management:** - **Efficient Buffering**: Reduced memory footprint for snapshot operations - **Thread Safety**: Proper mutex protection for concurrent access - **Resource Cleanup**: Better resource management and cleanup #### **Error Handling & Debugging:** - **Detailed Logging**: Comprehensive debug information - **Validation Messages**: Clear error messages with solution suggestions - **Debug Dumps**: Optional full stream analysis and debugging --- ## 📊 Usage Examples ### **Basic Snapshot Setup:** ```bash # Enable snapshots with auto-save every 10 seconds ./v4l2rtspserver -j snapshot.mp4 -J 640x480x10 /dev/video0 # Access via web: http://localhost:8554/snapshot ``` ### **MP4 Recording:** ```bash # Stream + Record simultaneously ./v4l2rtspserver -O recording.mp4 -P 8554 /dev/video0 # Stream: rtsp://localhost:8554/unicast # File: recording.mp4 (playable immediately) ``` ### **Cross-Platform Development:** ```bash # Builds automatically via GitHub Actions for: # - x86_64 Linux # - ARM64 Raspberry Pi # - ARM32 Raspberry Pi ``` --- ## 🔧 Technical Details ### **File Structure Changes:** - `src/MP4Muxer.cpp` - Complete MP4 muxing implementation - `src/SnapshotManager.cpp` - Multi-format snapshot engine - `src/H264_V4l2DeviceSource.cpp` - Enhanced H.264 processing - `src/V4l2RTSPServer.cpp` - SIGINT emergency handling - `inc/MP4Muxer.h` - MP4 muxer interface - `inc/SnapshotManager.h` - Snapshot manager interface - `main.cpp` - New command line options and auto-detection - `.github/workflows/pi-cross.yml` - Cross-compilation configuration ### **Performance Impact:** - **CPU**: <5% increase for MP4 recording - **Memory**: ~8KB per frame metadata + 1MB write buffer (minimal overhead) - **Disk I/O**: 100x fewer disk operations (from ~300/sec to ~1-2/sec) - **SD Card Lifespan**: Dramatically extended due to reduced write cycles - **Real-time Performance**: Improved due to reduced I/O blocking - **Network**: Zero impact on RTSP streaming <img width="1464" alt="image" src="https://github.com/user-attachments/assets/56ff0eb9-2614-4f55-85a2-ae01bbe739ea" /> - **~10%** CPU load for 1080p h264 stream + web snapshot + file snapshot + MP4 file output on Pi Zero W ### **Compatibility:** - **Backward Compatible**: All existing functionality preserved - **Cross-Platform**: Linux x86_64, ARM64, ARM32 - **Container Support**: Docker builds included - **Player Support**: VLC, QuickTime, FFmpeg, web browsers --- ## ✅ Testing Results ### **Snapshot Functionality:** - ✅ MJPEG streams → Perfect JPEG snapshots - ✅ H.264 streams → Mini-MP4 snapshots (playable) - ✅ Web endpoint → Real-time snapshot access - ✅ Auto-save → Configurable interval saving ### **MP4 Recording:** - ✅ Single files: 11KB snapshots → 1GB+ recordings - ✅ macOS: Perfect Finder thumbnails - ✅ Emergency: Ctrl+C produces valid files - ✅ Streaming: Simultaneous RTSP + recording - ✅ Buffering: 100x fewer disk writes, excellent SD card protection - ✅ Performance: No impact on real-time streaming performance ### **Cross-Compilation:** - ✅ GitHub Actions: Automated Pi builds - ✅ ARM compatibility: Native Pi performance - ✅ Docker: Multi-architecture support --- ## 🎉 Benefits Summary ### **Before This PR:** - ❌ No snapshot functionality - ❌ No MP4 recording capability - ❌ Raw H.264 files only - ❌ Manual Pi compilation required - ❌ No emergency file protection - ❌ High disk I/O load (SD card wear) ### **After This PR:** - ✅ Professional snapshot system (3 formats) - ✅ Production-ready MP4 recording - ✅ Perfect macOS compatibility - ✅ Automated cross-compilation - ✅ Bulletproof data protection - ✅ Web API for snapshots - ✅ Intelligent write buffering (SD card protection) - ✅ 100% backward compatibility --- **This PR transforms v4l2rtspserver from a basic streaming tool into a comprehensive video solution suitable for production environments, IoT deployments, and professional applications.** 🚀
合并状态:未合并 4 条评论