feat: Add initial IPTV player with web-based playback and streaming API.

This commit is contained in:
Michael committed 2025-12-15 13:01:24 +01:00
1 parent e3f5bc6e66
commit f290f5a7fc
7 files changed
+354 -1440

No files matched your search

-289
View File
@@ -1,289 +0,0 @@
import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:shelf/shelf.dart';
/// Active FFmpeg processes and their temp directories
final Map<String, Process> _activeStreams = {};
final Map<String, Directory> _streamDirs = {};
/// Create handler for HLS file serving
///
/// Serves playlist.m3u8 and segment files created by FFmpeg
Handler createHlsFileHandler() {
return (Request request) async {
final path = request.url.path;
// Only handle /api/hls/* requests
if (!path.startsWith('api/hls/')) {
return Response.notFound('Not found');
}
try {
// Parse path: /api/hls/{streamId}/{filename}
final pathParts = path.split('/');
if (pathParts.length < 4) {
return Response.badRequest(body: 'Invalid HLS path');
}
final streamId = pathParts[2];
final filename = pathParts[3];
// Get temp directory for this stream
final tempDir = Directory('${Directory.systemTemp.path}/hls_$streamId');
if (!await tempDir.exists()) {
return Response.notFound('Stream not found');
}
// Construct file path
final filePath = '${tempDir.path}/$filename';
final file = File(filePath);
if (!await file.exists()) {
return Response.notFound('File not found');
}
// Determine content type
String contentType;
if (filename.endsWith('.m3u8')) {
contentType = 'application/vnd.apple.mpegurl';
} else if (filename.endsWith('.ts')) {
contentType = 'video/MP2T';
} else {
contentType = 'application/octet-stream';
}
// Read and return file
final fileBytes = await file.readAsBytes();
return Response.ok(
fileBytes,
headers: {
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
},
);
} catch (e, stackTrace) {
print('HLS file serving error: $e');
print(stackTrace);
return Response.internalServerError(
body: jsonEncode({'error': 'File serving error', 'message': e.toString()}),
headers: {'content-type': 'application/json'},
);
}
};
}
/// Create handler for FFmpeg HLS transcoding
///
/// Starts FFmpeg process that creates HLS segments for VOD
Handler createStreamInitHandler() {
return (Request request) async {
final path = request.url.path;
// Only handle /api/stream/* requests
if (!path.startsWith('api/stream/')) {
return Response.notFound('Not found');
}
try {
// Extract stream ID
final pathParts = path.split('/');
if (pathParts.length < 3) {
return Response.badRequest(body: 'Invalid stream path');
}
final streamId = pathParts[2];
// Get parameters
final iptvUrl = request.url.queryParameters['url'];
final quality = request.url.queryParameters['quality'] ?? 'high';
if (iptvUrl == null || iptvUrl.isEmpty) {
return Response.badRequest(body: 'Missing url parameter');
}
print('Starting HLS Stream for ID: $streamId');
print('Source: $iptvUrl');
// Create temp directory for HLS segments
final tempDir = Directory('${Directory.systemTemp.path}/hls_$streamId');
if (await tempDir.exists()) {
await tempDir.delete(recursive: true);
}
await tempDir.create(recursive: true);
_streamDirs[streamId] = tempDir;
final playlistPath = '${tempDir.path}/playlist.m3u8';
// FFmpeg HLS arguments
final ffmpegArgs = <String>[
'-hide_banner',
'-loglevel', 'error',
'-user_agent', 'VLC/3.0.18 LibVLC/3.0.18',
'-headers', 'Accept: */*\r\nConnection: keep-alive\r\n',
// Input resilience
'-reconnect', '1',
'-reconnect_streamed', '1',
'-reconnect_delay_max', '10',
'-rw_timeout', '15000000',
'-analyzeduration', '10000000',
'-probesize', '5000000',
'-i', iptvUrl,
// Output format: HLS
'-f', 'hls',
'-hls_time', '4',
'-hls_list_size', '0', // Keep all segments in playlist
'-hls_playlist_type', 'event', // Allow appending new segments, keep old ones
'-hls_segment_type', 'mpegts',
'-hls_segment_filename', '${tempDir.path}/segment_%03d.ts',
];
// Video encoding: Always transcode for HLS timestamp compatibility
// Using ultrafast preset for Live TV to minimize latency
final isLiveStream = iptvUrl.endsWith('.ts') || iptvUrl.contains('/live/');
if (isLiveStream) {
// LIVE TV: Transcode to H.264 for browser compatibility
// Source may be HEVC/H.265 which browsers don't support
// Using superfast preset for minimal CPU usage
ffmpegArgs.addAll([
'-c:v', 'libx264',
'-preset', 'superfast', // Fast encoding to prevent buffering
'-tune', 'zerolatency', // Minimize latency
'-profile:v', 'baseline', // Maximum browser compatibility
'-level', '3.1',
'-pix_fmt', 'yuv420p',
'-g', '30', // Keyframe every second at 30fps
'-b:v', '2500k', // 2.5Mbps - balance quality/speed
'-maxrate', '3000k',
'-bufsize', '5000k',
]);
} else {
// VOD: Use ultrafast preset to prevent buffer stalls
// FFmpeg must transcode FASTER than realtime to keep the buffer full
ffmpegArgs.addAll([
'-c:v', 'libx264',
'-preset', 'ultrafast', // CRITICAL: Fast encoding to prevent buffer stalls
'-tune', 'fastdecode', // Optimize for fast decoding in browser
'-profile:v', 'main', // Good compatibility, better than baseline for VOD
'-level', '4.0',
'-pix_fmt', 'yuv420p',
'-g', '48', // Keyframe every 2s at 24fps
'-threads', '0', // Use all available CPU cores
]);
if (quality == 'low') {
ffmpegArgs.addAll([
'-vf', 'scale=-2:480',
'-b:v', '1000k',
'-maxrate', '1200k',
'-bufsize', '2000k',
]);
} else {
// High quality but still fast enough to prevent stalls
ffmpegArgs.addAll([
'-b:v', '2500k', // Reduced from 6000k for faster encoding
'-maxrate', '3000k', // Reduced from 8000k
'-bufsize', '5000k', // Reduced from 12000k
'-crf', '23', // Slightly lower quality but much faster
]);
}
}
// Audio: Always transcode to AAC
ffmpegArgs.addAll([
'-c:a', 'aac',
'-b:a', '192k',
'-ar', '48000',
'-ac', '2',
]);
ffmpegArgs.add(playlistPath);
// Start FFmpeg process
final process = await Process.start('ffmpeg', ffmpegArgs);
_activeStreams[streamId] = process;
// Log stderr
process.stderr.transform(utf8.decoder).listen((data) {
if (data.contains('Error') || data.contains('error')) {
print('FFmpeg Error [$streamId]: $data');
}
});
// Wait for playlist creation
var attempts = 0;
while (!await File(playlistPath).exists() && attempts < 50) {
await Future.delayed(const Duration(milliseconds: 100));
attempts++;
}
if (!await File(playlistPath).exists()) {
process.kill();
_activeStreams.remove(streamId);
_streamDirs.remove(streamId);
await tempDir.delete(recursive: true);
return Response.internalServerError(
body: 'FFmpeg failed to create playlist',
);
}
// CRITICAL: Wait for first segment to exist AND HAVE DATA > 5MB to be safe?
// No, 10KB is enough for header + some data
attempts = 0;
final firstSegment = File('${tempDir.path}/segment_000.ts');
while (attempts < 100) {
if (await firstSegment.exists()) {
final len = await firstSegment.length();
if (len > 10240) break; // Wait for > 10KB
}
await Future.delayed(const Duration(milliseconds: 100));
attempts++;
}
if (!await firstSegment.exists()) {
print('Warning: First segment not created yet for $streamId');
} else {
print('First segment ready for $streamId (${await firstSegment.length()} bytes)');
}
print('HLS playlist created for $streamId');
// Cleanup on process exit
process.exitCode.then((_) {
print('FFmpeg finished for $streamId');
_activeStreams.remove(streamId);
final dir = _streamDirs.remove(streamId);
dir?.delete(recursive: true).catchError((e) {
print('Cleanup error for $streamId: $e');
});
});
// Return playlist URL
return Response.ok(
jsonEncode({'playlist': '/api/hls/$streamId/playlist.m3u8'}),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
);
} catch (e, stackTrace) {
print('Stream init error: $e');
print(stackTrace);
return Response.internalServerError(
body: jsonEncode({'error': e.toString()}),
headers: {'content-type': 'application/json'},
);
}
};
}
+225
View File
@@ -0,0 +1,225 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
import '../../lib/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');
/// Initialize streaming subsystem
Future<void> initStreaming() async {
if (!_hlsTempDir.existsSync()) {
await _hlsTempDir.create(recursive: true);
}
}
/// Helper to get current playlist configuration (mocked for now, implies single user)
/// In a real scenario, this should come from the request session/context
PlaylistConfig? _getCurrentPlaylist(Request request) {
// TODO: Retrieve from DB/Session based on Auth
// For now, we assume the frontend sends enough info or we look up the active one
return null; // Implemented later in server.dart injection
}
// ==========================================
// 1. LIVE TV HANDLER (Direct MPEG-TS Proxy)
// ==========================================
Handler createLiveStreamHandler(Future<PlaylistConfig?> Function(Request) getPlaylist) {
final router = Router();
// Route: /api/live/{streamId}.ts
router.get('/<streamId>.ts', (Request request, String streamId) async {
final playlist = await getPlaylist(request);
if (playlist == null) {
return Response.internalServerError(body: 'No playlist configured');
}
final targetUrl = Uri.parse('${playlist.dns}/live/${playlist.username}/${playlist.password}/$streamId.ts');
print('[Live] Proxying: $targetUrl');
try {
final client = HttpClient();
client.connectionTimeout = const Duration(seconds: 10); // Connection timeout
client.idleTimeout = const Duration(hours: 6); // Keep alive long time
final req = await client.getUrl(targetUrl);
req.headers.set('User-Agent', 'VLC/3.0.18 LibVLC/3.0.18'); // Simulate VLC
req.headers.set('Connection', 'keep-alive');
final response = await req.close();
if (response.statusCode != 200) {
return Response(response.statusCode, body: 'Upstream error: ${response.statusCode}');
}
print('[Live] Connected to upstream. Streaming...');
// Pipe the response stream to the client
// We use a StreamController to handle cancellation
final controller = StreamController<List<int>>();
response.listen(
(data) => controller.add(data),
onError: (e) => controller.addError(e),
onDone: () => controller.close(),
cancelOnError: true,
);
// Cleanup when client disconnects
controller.onCancel = () {
print('[Live] Client disconnected: $streamId');
// response.detachSocket() if possible, or just let GC handle it
};
return Response.ok(
controller.stream,
headers: {
'Content-Type': 'video/mp2t',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
},
);
} catch (e) {
print('[Live] Error: $e');
return Response.internalServerError(body: 'Live stream error: $e');
}
});
return router;
}
// ==========================================
// 2. VOD HANDLER (FFmpeg HLS Transcoding)
// ==========================================
// ==========================================
// 2. VOD HANDLER (FFmpeg HLS Transcoding)
// ==========================================
Handler createVodStreamHandler(Future<PlaylistConfig?> Function(Request) getPlaylist) {
final router = Router();
// Route: /api/vod/{streamId}/playlist.m3u8
router.get('/<streamId>/playlist.m3u8', (Request request, String streamId) async {
final playlist = await getPlaylist(request);
if (playlist == null) return Response.internalServerError(body: 'No playlist');
// Clean up old session if exists
if (_vodProcesses.containsKey(streamId)) {
print('[VOD] Killing previous session for $streamId');
_vodProcesses[streamId]?.kill();
_vodProcesses.remove(streamId);
}
// Prepare directory
final streamDir = Directory('${_hlsTempDir.path}/$streamId');
if (streamDir.existsSync()) {
streamDir.deleteSync(recursive: true);
}
streamDir.createSync(recursive: true);
// Build Upstream URL (Try MKV first, then generic)
// Note: We don't know the extension here easily unless passed.
// Xtream usually allows access via streamId.mkv or streamId.mp4 regardless of actual file
// We'll trust the ID.
final targetUrl = '${playlist.dns}/movie/${playlist.username}/${playlist.password}/$streamId.mkv';
print('[VOD] Starting Transcode: $targetUrl');
// Start FFmpeg
final ffmpegArgs = [
'-hide_banner', '-loglevel', 'error',
'-headers', 'User-Agent: VLC/3.0.18 LibVLC/3.0.18\r\n',
'-i', targetUrl,
// Video: H.264 Ultrafast (Low CPU)
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-tune', 'fastdecode',
'-crf', '23',
'-maxrate', '3000k',
'-bufsize', '6000k',
'-pix_fmt', 'yuv420p',
'-threads', '0',
// Audio: AAC (Browser compatible)
'-c:a', 'aac',
'-b:a', '128k',
'-ac', '2',
// HLS Output options
'-f', 'hls',
'-hls_time', '4',
'-hls_list_size', '0', // Keep all segments
'-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'
];
Process.start('ffmpeg', ffmpegArgs).then((process) {
_vodProcesses[streamId] = process;
process.stderr.transform(utf8.decoder).listen((data) {
if (data.contains('Error')) print('[FFmpeg $streamId] $data');
});
process.exitCode.then((code) {
print('[VOD] FFmpeg exited with code $code');
_vodProcesses.remove(streamId);
});
});
// Wait for playlist to appear (max 10s)
final playlistFile = File('${streamDir.path}/playlist.m3u8');
int retries = 0;
while (!playlistFile.existsSync() && retries < 20) {
await Future.delayed(const Duration(milliseconds: 500));
retries++;
}
if (!playlistFile.existsSync()) {
return Response.internalServerError(body: 'Timeout waiting for transcoder');
}
return Response.ok(
playlistFile.openRead(),
headers: {
'Content-Type': 'application/vnd.apple.mpegurl', // Correct HLS Mime
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache',
},
);
});
// Route: /api/vod/{streamId}/segment_{n}.ts (Serve segments)
router.get('/<streamId>/<segment>', (Request request, String streamId, String segment) async {
final file = File('${_hlsTempDir.path}/$streamId/$segment');
if (!file.existsSync()) {
return Response.notFound('Segment not found');
}
return Response.ok(
file.openRead(),
headers: {
'Content-Type': 'video/mp2t',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'max-age=3600', // Cache segments
},
);
});
return router;
}