╔════════════════════════════════════════════════════════════════╗ ║ VIDEO STREAMING BANDWIDTH TEST - vid30s.com ║ ╚════════════════════════════════════════════════════════════════╝ Video Details: - ID: 1 - Slug: 0HpOeSU7 - Provider: vid30s - CDN Host: vidoycdn.b-cdn.net - HLS URL: https://vidoycdn.b-cdn.net/video.m3u8?token=xyz123&expires=1... ┌─ SKENARIO 1: PROXY MODE (Current - /stream/) ─────────────────┐ │ Flow: Browser → Server (Proxy) → CDN │ │ Route: GET /stream/0HpOeSU7 → StreamProxyController │ └───────────────────────────────────────────────────────────────┘ 📊 Single short video (100MB): └─ CDN outbound: 100MB ❌ (charged to CDN provider) └─ Server outbound: 100MB ❌ (charged to your server) └─ TOTAL: 200MB (Double bandwidth!) 📊 Medium length video (500MB): └─ CDN outbound: 500MB ❌ (charged to CDN provider) └─ Server outbound: 500MB ❌ (charged to your server) └─ TOTAL: 1000MB (Double bandwidth!) 📊 Full length movie (1000MB): └─ CDN outbound: 1000MB ❌ (charged to CDN provider) └─ Server outbound: 1000MB ❌ (charged to your server) └─ TOTAL: 2000MB (Double bandwidth!) ┌─ SKENARIO 2: REDIRECT MODE (Alternative) ──────────────────────┐ │ Flow: Browser → CDN (Direct redirect) │ │ Route: GET /e/0HpOeSU7 → return redirect() to CDN URL │ └───────────────────────────────────────────────────────────────┘ 📊 Single short video (100MB): └─ CDN outbound: 100MB ⚠️ (charged to CDN provider - NO WAY TO AVOID) └─ Server outbound: 0MB ✅ (your server bandwidth SAVED!) └─ TOTAL: 100MB (No double bandwidth) 📊 Medium length video (500MB): └─ CDN outbound: 500MB ⚠️ (charged to CDN provider - NO WAY TO AVOID) └─ Server outbound: 0MB ✅ (your server bandwidth SAVED!) └─ TOTAL: 500MB (No double bandwidth) 📊 Full length movie (1000MB): └─ CDN outbound: 1000MB ⚠️ (charged to CDN provider - NO WAY TO AVOID) └─ Server outbound: 0MB ✅ (your server bandwidth SAVED!) └─ TOTAL: 1000MB (No double bandwidth) ╔════════════════════════════════════════════════════════════════╗ ║ BANDWIDTH COMPARISON ║ ╚════════════════════════════════════════════════════════════════╝ Per Single Request: Request Proxy Mode Redirect Mode Saved ────────────────────────────────────────────────────────────── Single short video 200MB 100MB 100MB ✅ Medium length video 1000MB 500MB 500MB ✅ Full length movie 2000MB 1000MB 1000MB ✅ ╔════════════════════════════════════════════════════════════════╗ ║ CONCURRENT USERS IMPACT (100MB video) ║ ╚════════════════════════════════════════════════════════════════╝ Assuming 100MB video streaming simultaneously: Users Proxy Total Redirect Total Server Saved ────────────────────────────────────────────────────────────── 1 users 200MB 100MB 100MB ✅ 5 users 1000MB 500MB 500MB ✅ 10 users 2000MB 1000MB 1000MB ✅ 20 users 4000MB 2000MB 2000MB ✅ ╔════════════════════════════════════════════════════════════════╗ ║ ISSUES & CONSIDERATIONS ║ ╚════════════════════════════════════════════════════════════════╝ PROXY MODE (Current): ✅ CORS proxy - solves browser cross-origin issues ✅ Server kontrol - bisa add custom headers (Referer, auth) ✅ Bandwidth control - bisa rate limit per user ✅ Privacy - client IP terlihat dari server saja ❌ EXPENSIVE - double bandwidth usage! ❌ Server load - PHP process occupies memory ❌ Latency - extra hop through server REDIRECT MODE (Alternative): ✅ CHEAP - minimal server bandwidth ✅ Fast - direct CDN to client ✅ Scalable - no server bottleneck ❌ CORS issue - browser blocks cross-origin requests ❌ No Referer header - CDN may reject (403) ❌ No control - pure client-CDN communication ❌ Cannot add auth headers via browser ╔════════════════════════════════════════════════════════════════╗ ║ RECOMMENDATION ║ ╚════════════════════════════════════════════════════════════════╝ HYBRID APPROACH: ┌─ Use lightweight proxy for Referer-protected CDNs ────────────┐ │ │ │ 1. Check if CDN requires Referer header: │ │ HEAD request to CDN URL │ │ │ │ 2a. If 200 OK → Redirect client directly to CDN │ │ - No proxy, zero server bandwidth │ │ │ │ 2b. If 403 Forbidden → Use lightweight proxy │ │ - Just forward with Referer header │ │ - NOT full stream through PHP (use nginx/varnish) │ │ │ └───────────────────────────────────────────────────────────────┘ This way you get: ✅ 50% bandwidth savings (most videos) ✅ Still support Referer-protected CDNs ✅ Better performance ╔════════════════════════════════════════════════════════════════╗ ║ PROPOSED CODE CHANGES ║ ╚════════════════════════════════════════════════════════════════╝ In EmbedController.php - resolveHlsPlaybackUrl(): ─────────────────────────────────────────────────────────────── private function resolveHlsPlaybackUrl(Video $video, ?string $hlsUrl): ?string { if (!$hlsUrl) { return $hlsUrl; } // Try to access CDN directly first (no Referer needed) $host = parse_url($hlsUrl, PHP_URL_HOST); // Check if URL accessible without proxy if ($this->isCdnDirectlyAccessible($hlsUrl)) { // Safe to use direct CDN URL - no proxy needed! return $hlsUrl; // ← Client downloads directly from CDN } // If direct access fails (403), use proxy for Referer header $corsBlockedCdns = [ 'vidoycdn.b-cdn.net', 'b-cdn.net', 'cdnvvidey.de', ]; foreach ($corsBlockedCdns as $cdn) { if (str_contains($host, $cdn)) { // Use LIGHTWEIGHT proxy (just forward with header) return route('stream.video', $video); } } return $hlsUrl; } private function isCdnDirectlyAccessible(string $url): bool { // Cache result untuk 1 jam $cacheKey = 'cdn_access_' . md5($url); if (Cache::has($cacheKey)) { return Cache::get($cacheKey); } try { $response = Http::timeout(5)->head($url); $accessible = $response->successful(); // Cache result Cache::put($cacheKey, $accessible, now()->addHours(1)); return $accessible; } catch (\Throwable $e) { return false; // Assume needs proxy } } Result: 📊 Scenario 1: CDN accessible directly → Return plain → Browser fetches directly from CDN → 50% bandwidth savings! ✅ 📊 Scenario 2: CDN needs Referer (403) → Return route('stream.video') → Proxy adds Referer header → Still uses server bandwidth, but only when necessary ╔════════════════════════════════════════════════════════════════╗ ║ NEXT STEPS - TESTING ║ ╚════════════════════════════════════════════════════════════════╝ 1. Run this script to understand the scenarios: $ php scripts/test_bandwidth_scenario.php 2. Create test endpoint (optional): GET /test/cdn-accessibility → Test actual CDN access from your server 3. Modify EmbedController.php with hybrid approach: → Check CDN accessibility → Use direct URL if possible → Use proxy only when necessary 4. Monitor bandwidth impact: → Check server outbound traffic → Compare before/after Done! ✅