hassan-razhei / entity-streamer
High-performance RFC 7233 partial content HTTP 206 video and audio streaming engine for Laravel.
Requires
- php: ^8.2|^8.3|^8.4
- illuminate/filesystem: ^11.0|^12.0|^13.0
- illuminate/http: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
- symfony/http-foundation: ^7.0|^8.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
High-performance RFC 7233 byte-range HTTP 206 video & audio streaming engine for Laravel.
๐ Overview
entity-streamer is a memory-safe binary streaming package designed to serve multi-gigabyte video and audio files in Laravel applications. It strictly complies with RFC 7233 (Range Requests), enabling seamless seeking, fast scrubbing, and bandwidth optimization in HTML5 players (<video>, <audio>), Mobile Apps, and desktop browsers.
๐ Key Features
- Strict RFC 7233 Engine: Full support for closed (
0-1024), open-ended (1048576-), and suffix ranges (-500), returning proper HTTP206 Partial Contentor416 Range Not Satisfiable. - Memory-Safe Kernel: Streams data in configurable chunks (default: 64 KB) without loading large media into server RAM.
- Storage Agnostic: Native integration with Laravel Storage (Local, S3, MinIO) with automatic fallback path lookup.
- Format Intelligence: Automatic MIME detection via binary magic bytes for all major video (MP4, WebM, MKV, QuickTime, AVI) and audio formats (MP3, WAV, AAC, FLAC, OGG, M4A).
- HTTP Cache Optimization: Automatic
ETagandLast-Modifiedgeneration with304 Not Modifiedvalidation to conserve bandwidth. - Security Shield: Multi-layer sanitization against path traversal (
..,%252e%252e, null bytes, system devices, and hidden files). - Signed URLs & Anti-Leech: HMAC-SHA256 time-limited streaming URLs with optional client IP binding to prevent hotlinking.
- Eloquent Integration:
HasStreamableMediatrait for one-line media streaming directly from models. - Developer Experience: Fluent builder,
Streamerfacade, andresponse()->streamMedia()macro.
๐ฅ Installation
Require the package via Composer:
composer require hassan-razhei/entity-streamer
Publish the configuration file (optional):
php artisan vendor:publish --tag=streamer-config
๐ Usage Examples
1. Using the Facade
use EntityStreamer\Facades\Streamer; Route::get('/watch/{video}', function (string $video, Request $request) { return Streamer::disk('s3') ->withChunkSize(64 * 1024) ->asInline("lesson.mp4") ->toResponse($request); });
2. Using the Response Macro
Route::get('/listen/{surah}', function (string $surah) { return response()->streamMedia("audio/{$surah}.mp3", disk: 'public'); });
3. Eloquent Model Integration
Implement CanStreamMedia and use HasStreamableMedia trait on your model:
namespace App\Models; use EntityStreamer\Contracts\CanStreamMedia; use EntityStreamer\Traits\HasStreamableMedia; use Illuminate\Database\Eloquent\Model; class Lesson extends Model implements CanStreamMedia { use HasStreamableMedia; protected $fillable = ['title', 'video_path']; }
Stream directly inside your controller:
public function stream(Lesson $lesson, Request $request) { return $lesson->streamResponse($request); }
Generate signed temporary streaming URLs with Anti-Leech protection:
// Signed for 1 hour, bound to the current user's IP $url = $lesson->getStreamUrl(expiration: 3600, bindToIp: true);
4. Forcing Download (Attachment Mode)
return Streamer::stream('exports/recorded_session.mp4') ->asAttachment('lecture_archive.mp4') ->toResponse($request);
๐งช Testing
Run the test suite:
php packages/entity-streamer/tests/run_tests.php
All 77 test cases cover:
- RFC 7233 partial, open, and suffix ranges.
- Memory bounding and zero-ballooning during 2GB+ streaming.
- Forward and backward bidirectional seeking accuracy.
- Directory traversal fuzzing and null-byte defenses.
- HMAC-SHA256 signature verification and Anti-Leech protection.
- HTML5 media player lifecycle simulations.
๐ License
This package is open-sourced software licensed under the MIT license.