This commit is contained in:
Michael committed 2025-12-16 08:14:25 +01:00
commit 9326572fbc
59 files changed
+3683 -1083

No files matched your search

+180 -49
View File
@@ -5,13 +5,23 @@ import 'dart:math';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:http/http.dart' as http;
import '../../lib/core/models/playlist_config.dart';
import 'package:xtremflow/core/models/playlist_config.dart';
/// Active FFmpeg processes for VOD transcoding
final Map<String, Process> _vodProcesses = {};
/// Directory for temporary HLS segments
final Directory _hlsTempDir = Directory('/tmp/hls_vod');
final Directory _hlsTempDir = Directory('${Directory.systemTemp.path}/xtremflow_streams');
/// Helper to resolve FFmpeg path (SYSTEM PATH vs Portable)
String _getFFmpegPath() {
if (Platform.isWindows) {
if (File('ffmpeg.exe').existsSync()) return 'ffmpeg.exe';
if (File('bin/ffmpeg.exe').existsSync()) return 'bin/ffmpeg.exe';
if (File('ffmpeg/bin/ffmpeg.exe').existsSync()) return 'ffmpeg/bin/ffmpeg.exe';
}
return 'ffmpeg'; // Default to PATH
}
/// Initialize streaming subsystem
Future<void> initStreaming() async {
@@ -40,8 +50,11 @@ Handler createLiveStreamHandler(Future<PlaylistConfig?> Function(Request) getPla
print('[Live] Incoming request for streamId: $streamId');
// Remove .ts extension if present
// Remove extension if present (handle both .ts and .m3u8 just in case)
if (streamId.endsWith('.ts')) {
streamId = streamId.substring(0, streamId.length - 3);
} else if (streamId.endsWith('.m3u8')) {
streamId = streamId.substring(0, streamId.length - 5);
}
final playlist = await getPlaylist(request);
@@ -51,34 +64,72 @@ Handler createLiveStreamHandler(Future<PlaylistConfig?> Function(Request) getPla
return Response.internalServerError(body: 'No playlist configured');
}
final targetUrl = Uri.parse('${playlist.dns}/live/${playlist.username}/${playlist.password}/$streamId.ts');
print('[Live] Proxying: $targetUrl');
final targetUrl = '${playlist.dns}/live/${playlist.username}/${playlist.password}/$streamId.ts';
print('[Live] Streaming via FFmpeg: $targetUrl');
// Start FFmpeg to proxy and stabilize the stream
// We use "-c copy" to avoid transcoding (Low CPU) but gain FFmpeg's network robustness
final ffmpegArgs = [
'-hide_banner', '-loglevel', 'error',
'-headers', 'User-Agent: VLC/3.0.18 LibVLC/3.0.18\r\n',
// Robustness flags (Same as VOD)
'-reconnect', '1',
'-reconnect_at_eof', '1',
'-reconnect_streamed', '1',
'-reconnect_delay_max', '5',
'-rw_timeout', '15000000',
'-analyzeduration', '5000000', // Faster probe for live
'-probesize', '5000000',
'-i', targetUrl,
'-c', 'copy', // Direct stream copy (Very low CPU)
'-f', 'mpegts',
'pipe:1', // Output to stdout
];
final client = http.Client();
final streamRequest = http.Request('GET', targetUrl);
// Spoof User-Agent to avoid 403/405 from providers
streamRequest.headers['User-Agent'] = 'VLC/3.0.18 LibVLC/3.0.18';
streamRequest.headers['Connection'] = 'keep-alive';
try {
final streamResponse = await client.send(streamRequest);
print('[Live] Upstream response: ${streamResponse.statusCode} ${streamResponse.reasonPhrase}');
final ffmpegPath = _getFFmpegPath();
final process = await Process.start(ffmpegPath, ffmpegArgs);
// Cleanup handling: Kill FFmpeg when client disconnects
final controller = StreamController<List<int>>(
onCancel: () {
print('[Live] Client disconnected, killing FFmpeg for $streamId');
process.kill();
},
);
// Pipe stdout to controller
process.stdout.listen(
(data) => controller.add(data),
onDone: () {
print('[Live] FFmpeg stream ended for $streamId');
controller.close();
},
onError: (e) {
print('[Live] FFmpeg stream error: $e');
controller.addError(e);
},
);
// Log errors
process.stderr.transform(utf8.decoder).listen((data) {
print('[FFmpeg Live $streamId] $data');
});
// Filter headers to avoid encoding issues with shelf
final responseHeaders = Map<String, Object>.from(streamResponse.headers);
responseHeaders.remove('content-length');
responseHeaders.remove('content-encoding');
responseHeaders.remove('transfer-encoding');
return Response(
streamResponse.statusCode,
body: streamResponse.stream,
headers: responseHeaders,
return Response.ok(
controller.stream,
headers: {
'Content-Type': 'video/mp2t',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache',
},
);
} catch (e) {
print('[Live] Upstream connection error: $e');
return Response.internalServerError(body: 'Upstream error: $e');
print('[Live] Failed to start FFmpeg: $e');
return Response.internalServerError(body: 'Stream start error: $e');
}
});
@@ -93,6 +144,8 @@ Handler createLiveStreamHandler(Future<PlaylistConfig?> Function(Request) getPla
// 2. VOD HANDLER (FFmpeg HLS Transcoding)
// ==========================================
final _lastLogTime = <String, int>{};
Handler createVodStreamHandler(Future<PlaylistConfig?> Function(Request) getPlaylist) {
final router = Router();
@@ -141,25 +194,92 @@ Handler createVodStreamHandler(Future<PlaylistConfig?> Function(Request) getPlay
// Prepare directory
final streamDir = Directory('${_hlsTempDir.path}/$streamId');
if (streamDir.existsSync()) {
streamDir.deleteSync(recursive: true);
// Check if existing session is healthy
if (_vodProcesses.containsKey(streamId)) {
final existingProcess = _vodProcesses[streamId]!;
// Check if process is still running by checking if playlist exists and has content
final playlistFile = File('${streamDir.path}/playlist.m3u8');
if (playlistFile.existsSync()) {
final content = playlistFile.readAsStringSync();
// Check if playlist has segments (healthy transcoding)
if (content.contains('.ts')) {
// Only log once per second to avoid spam
final now = DateTime.now().millisecondsSinceEpoch;
if (!_lastLogTime.containsKey(streamId) || (now - _lastLogTime[streamId]! > 2000)) {
print('[VOD] Reusing existing session for $streamId');
_lastLogTime[streamId] = now;
}
// Serve existing playlist
return Response.ok(
playlistFile.openRead(),
headers: {
'Content-Type': 'application/vnd.apple.mpegurl',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache',
},
);
}
}
// Playlist missing or empty - process likely failed, clean up
print('[VOD] Cleaning up stale session for $streamId');
try {
existingProcess.kill();
} catch (e) {
// Process may already be dead
}
_vodProcesses.remove(streamId);
// Clean up directory
if (streamDir.existsSync()) {
try {
streamDir.deleteSync(recursive: true);
} catch (e) {
print('[VOD] Failed to clean directory: $e');
}
}
}
streamDir.createSync(recursive: true);
// Build Upstream URL with correct extension
final targetUrl = '${playlist.dns}/movie/${playlist.username}/${playlist.password}/$streamId.$extension';
print('[VOD] Starting Transcode: $targetUrl');
// Build Upstream URL based on content type
// Check for type parameter in request URL (?type=series or ?type=movie)
final contentType = request.url.queryParameters['type'] ?? 'movie';
final basePath = contentType == 'series' ? 'series' : 'movie';
// Only start FFmpeg if not already running
if (!_vodProcesses.containsKey(streamId)) {
// Prepare directory (Only for new session)
if (streamDir.existsSync()) {
streamDir.deleteSync(recursive: true);
}
streamDir.createSync(recursive: true);
final targetUrl = '${playlist.dns}/$basePath/${playlist.username}/${playlist.password}/$streamId.$extension';
print('[VOD] Starting Transcode ($contentType): $targetUrl');
// Start FFmpeg
final ffmpegArgs = [
'-hide_banner', '-loglevel', 'error',
'-headers', 'User-Agent: VLC/3.0.18 LibVLC/3.0.18\r\n',
// Robustness for network streams
'-reconnect', '1',
'-reconnect_at_eof', '1',
'-reconnect_streamed', '1',
'-reconnect_delay_max', '5',
'-rw_timeout', '15000000',
'-analyzeduration', '2000000', // Reduced from 10M to 2M for faster start
'-probesize', '2000000', // Reduced from 10M to 2M for faster start
'-i', targetUrl,
// Video: H.264 Ultrafast (Low CPU)
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-tune', 'zerolatency', // Optimize for low latency startup
'-tune', 'fastdecode',
'-crf', '23',
'-maxrate', '3000k',
@@ -167,40 +287,51 @@ Handler createVodStreamHandler(Future<PlaylistConfig?> Function(Request) getPlay
'-pix_fmt', 'yuv420p',
'-threads', '0',
// Audio: AAC (Browser compatible)
// Audio: AAC (Browser compatible, improved quality)
'-c:a', 'aac',
'-b:a', '128k',
'-ac', '2',
'-b:a', '192k',
'-ar', '48000',
// Advanced Audio Filtering:
// 1. "pan": Downmix 5.1/7.1 to Stereo with center mix (dialogue) boosted.
// 2. "dynaudnorm": Dynamic Audio Normalizer to boost quiet dialogue and limit loud explosions (Night Mode effect).
'-af', 'pan=stereo|FL=1.0*FL+0.707*FC+0.5*BL+0.5*SL+0.5*LFE|FR=1.0*FR+0.707*FC+0.5*BR+0.5*SR+0.5*LFE,dynaudnorm=f=150:g=15',
// HLS Output options
'-f', 'hls',
'-hls_time', '4',
'-hls_list_size', '0', // Keep all segments
'-hls_list_size', '0',
'-hls_playlist_type', 'event', // Event type allows immediate playback while transcoding
'-hls_allow_cache', '1',
'-hls_segment_type', 'mpegts',
'-hls_segment_filename', '${streamDir.path}/segment_%03d.ts',
'-start_number', '0',
// Write playlist to stdout or file? File is easier for static serving
'${streamDir.path}/playlist.m3u8'
'${streamDir.path}/playlist.m3u8',
];
Process.start('ffmpeg', ffmpegArgs).then((process) {
_vodProcesses[streamId] = process;
process.stderr.transform(utf8.decoder).listen((data) {
if (data.contains('Error')) print('[FFmpeg $streamId] $data');
});
final ffmpegPath = _getFFmpegPath();
Process.start(ffmpegPath, ffmpegArgs).then((process) {
_vodProcesses[streamId] = process;
process.stderr.transform(utf8.decoder).listen((data) {
// Log only major errors or startup info to keep logs clean(er)
// or keep verbose if debugging
print('[FFmpeg $streamId] $data');
});
process.exitCode.then((code) {
print('[VOD] FFmpeg exited with code $code');
_vodProcesses.remove(streamId);
process.exitCode.then((code) {
print('[VOD] FFmpeg exited with code $code');
_vodProcesses.remove(streamId);
});
});
});
}
// Wait for playlist to appear (max 10s)
// Wait for playlist to appear (max 30s)
final playlistFile = File('${streamDir.path}/playlist.m3u8');
int retries = 0;
while (!playlistFile.existsSync() && retries < 20) {
while (!playlistFile.existsSync() && retries < 60) {
await Future.delayed(const Duration(milliseconds: 500));
retries++;
}
+44 -20
View File
@@ -12,8 +12,8 @@ class AppDatabase {
/// Initialize database and create tables
Future<void> init() async {
final dbPath = '/app/data/xtremflow.db';
const dbPath = '/app/data/xtremflow.db';
// Ensure data directory exists
final dir = Directory('/app/data');
if (!await dir.exists()) {
@@ -21,7 +21,7 @@ class AppDatabase {
}
_db = sqlite3.open(dbPath);
await _createTables();
print('Database initialized: $dbPath');
}
@@ -79,9 +79,15 @@ class AppDatabase {
''');
// Indexes
_db.execute('CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions(token)');
_db.execute('CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions(expires_at)');
_db.execute('CREATE INDEX IF NOT EXISTS idx_playlists_user ON playlists(user_id)');
_db.execute(
'CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions(token)',
);
_db.execute(
'CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions(expires_at)',
);
_db.execute(
'CREATE INDEX IF NOT EXISTS idx_playlists_user ON playlists(user_id)',
);
}
/// Seed default admin user if no users exist
@@ -93,10 +99,13 @@ class AppDatabase {
final adminId = _uuid.v4();
final passwordHash = PasswordHasher.hash('admin');
_db.execute('''
_db.execute(
'''
INSERT INTO users (id, username, password_hash, is_admin)
VALUES (?, ?, ?, 1)
''', [adminId, 'admin', passwordHash],);
''',
[adminId, 'admin', passwordHash],
);
print('Default admin user created (username: admin, password: admin)');
}
@@ -148,10 +157,13 @@ class AppDatabase {
final userId = _uuid.v4();
final passwordHash = PasswordHasher.hash(password);
_db.execute('''
_db.execute(
'''
INSERT INTO users (id, username, password_hash, is_admin)
VALUES (?, ?, ?, ?)
''', [userId, username, passwordHash, isAdmin ? 1 : 0],);
''',
[userId, username, passwordHash, isAdmin ? 1 : 0],
);
return User(
id: userId,
@@ -197,10 +209,13 @@ class AppDatabase {
final token = _uuid.v4();
final expiresAt = DateTime.now().add(duration ?? const Duration(days: 7));
_db.execute('''
_db.execute(
'''
INSERT INTO sessions (id, user_id, token, expires_at)
VALUES (?, ?, ?, ?)
''', [sessionId, userId, token, expiresAt.toIso8601String()],);
''',
[sessionId, userId, token, expiresAt.toIso8601String()],
);
return models.Session(
id: sessionId,
@@ -221,7 +236,7 @@ class AppDatabase {
if (result.isEmpty) return null;
final session = models.Session.fromMap(result.first);
// Check if expired
if (session.isExpired) {
deleteSession(token);
@@ -279,10 +294,13 @@ class AppDatabase {
final playlistId = _uuid.v4();
final now = DateTime.now().toIso8601String();
_db.execute('''
_db.execute(
'''
INSERT INTO playlists (id, user_id, name, server_url, username, password, dns, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', [playlistId, userId, name, serverUrl, username, password, dns, now, now],);
''',
[playlistId, userId, name, serverUrl, username, password, dns, now, now],
);
return Playlist(
id: playlistId,
@@ -308,11 +326,14 @@ class AppDatabase {
}) {
final now = DateTime.now().toIso8601String();
_db.execute('''
_db.execute(
'''
UPDATE playlists
SET name = ?, server_url = ?, username = ?, password = ?, dns = ?, updated_at = ?
WHERE id = ?
''', [name, serverUrl, username, password, dns, now, playlistId],);
''',
[name, serverUrl, username, password, dns, now, playlistId],
);
return getPlaylistById(playlistId)!;
}
@@ -338,15 +359,18 @@ class AppDatabase {
/// Update user settings
void updateUserSettings(String userId, String settingsJson) {
final now = DateTime.now().toIso8601String();
// UPSERT (Insert or Replace)
_db.execute('''
_db.execute(
'''
INSERT INTO user_settings (user_id, settings_json, updated_at)
VALUES (?, ?, ?)
ON CONFLICT(user_id) DO UPDATE SET
settings_json = excluded.settings_json,
updated_at = excluded.updated_at
''', [userId, settingsJson, now]);
''',
[userId, settingsJson, now],
);
}
/// Close database connection
+17 -17
View File
@@ -10,7 +10,7 @@ import 'package:args/args.dart';
import 'database/database.dart';
import 'models/user.dart';
import 'models/playlist.dart';
import '../lib/core/models/playlist_config.dart';
import 'package:xtremflow/core/models/playlist_config.dart';
import 'api/auth_handler.dart';
import 'api/users_handler.dart';
import 'api/playlists_handler.dart';
@@ -49,17 +49,17 @@ void main(List<String> args) async {
// Auth endpoints
..mount('/api/auth', authHandler.router)
// Playlists endpoints
..mount('/api/playlists', Pipeline()
..mount('/api/playlists', const Pipeline()
.addMiddleware(authMiddleware(db))
.addHandler(playlistsHandler.router.call))
.addHandler(playlistsHandler.router.call),)
// Users endpoints
..mount('/api/users', Pipeline()
..mount('/api/users', const Pipeline()
.addMiddleware(authMiddleware(db))
.addHandler(usersHandler.router.call))
.addHandler(usersHandler.router.call),)
// Settings endpoints
..mount('/api/settings', Pipeline()
..mount('/api/settings', const Pipeline()
.addMiddleware(authMiddleware(db))
.addHandler(settingsHandler.router.call));
.addHandler(settingsHandler.router.call),);
// Initialize Cleanup Service
final cleanupService = CleanupService();
@@ -70,7 +70,7 @@ void main(List<String> args) async {
cleanupService.start();
// Admin Routes (protected)
apiRouter.mount('/api/admin', Pipeline()
apiRouter.mount('/api/admin', const Pipeline()
.addMiddleware(authMiddleware(db))
.addHandler((Request request) {
final router = Router();
@@ -90,7 +90,7 @@ void main(List<String> args) async {
});
return router(request);
}));
}),);
// Create static handler
final baseStaticHandler = createStaticHandler(
@@ -100,7 +100,7 @@ void main(List<String> args) async {
);
// Wrap static handler to enforce cache policies
Handler staticHandler = (Request request) async {
FutureOr<Response> staticHandler(Request request) async {
final response = await baseStaticHandler(request);
// Disable cache for entry points to ensure updates are seen immediately
@@ -113,17 +113,17 @@ void main(List<String> args) async {
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
'Pragma': 'no-cache',
'Expires': '0',
});
},);
}
// Allow aggressive caching for hashed assets
return response.change(headers: {
'Cache-Control': 'public, max-age=86400',
});
};
},);
}
// Helper to get playlist from request
Future<PlaylistConfig?> _getPlaylist(Request request) async {
Future<PlaylistConfig?> getPlaylist(Request request) async {
Playlist? playlist;
final user = request.context['user'] as User?;
@@ -155,8 +155,8 @@ void main(List<String> args) async {
// Create Streaming Router (Mount handlers on correct paths)
final streamingRouter = Router()
..mount('/api/live', createLiveStreamHandler(_getPlaylist))
..mount('/api/vod', createVodStreamHandler(_getPlaylist));
..mount('/api/live', createLiveStreamHandler(getPlaylist))
..mount('/api/vod', createVodStreamHandler(getPlaylist));
// Main handler with API proxy and NEW Streaming Handlers
final handler = Cascade()
@@ -167,7 +167,7 @@ void main(List<String> args) async {
.handler;
// Add middleware
final pipeline = Pipeline()
final pipeline = const Pipeline()
.addMiddleware(logRequests())
.addMiddleware(securityHeadersMiddleware())
.addMiddleware(honeypotMiddleware())