mirror of
https://github.com/R0m1k3/xtremflow.git
synced 2026-10-22 22:48:04 +02:00
feat: implement HLS streaming handler with FFmpeg transcoding and add IPTV player screen UI
This commit is contained in:
1 parent
d82f9f6846
commit
f08fccc0d1
2 files changed
+25
-91
No files matched your search
@@ -562,117 +562,51 @@ Handler createRecordingStreamHandler(
|
|||||||
}) {
|
}) {
|
||||||
final router = Router();
|
final router = Router();
|
||||||
|
|
||||||
// Route: /api/recordings/stream/{streamId}/playlist.m3u8
|
router.get('/<streamId>.mkv', (Request request, String streamId) async {
|
||||||
router.get('/<streamId>/playlist.m3u8', (Request request, String streamId) async {
|
|
||||||
final recording = db.getRecordingById(streamId);
|
final recording = db.getRecordingById(streamId);
|
||||||
if (recording == null || recording.filePath == null) {
|
if (recording == null || recording.filePath == null) {
|
||||||
return Response.notFound('Recording or file not found');
|
return Response.notFound('Recording or file not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
final targetUrl = recording.filePath!;
|
final targetUrl = recording.filePath!;
|
||||||
if (!File(targetUrl).existsSync()) {
|
final file = File(targetUrl);
|
||||||
|
if (!file.existsSync()) {
|
||||||
return Response.notFound('Physical recording file not found');
|
return Response.notFound('Physical recording file not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
final streamDir = Directory('${_hlsTempDir.path}/rec_$streamId');
|
final fileStat = file.statSync();
|
||||||
|
final fileSize = fileStat.size;
|
||||||
|
final rangeHeader = request.headers['range'];
|
||||||
|
|
||||||
// Check if existing session is healthy
|
if (rangeHeader != null && rangeHeader.startsWith('bytes=')) {
|
||||||
if (_vodProcesses.containsKey('rec_$streamId')) {
|
final parts = rangeHeader.substring(6).split('-');
|
||||||
final existingProcess = _vodProcesses['rec_$streamId']!;
|
var start = int.tryParse(parts[0]) ?? 0;
|
||||||
final playlistFile = File('${streamDir.path}/playlist.m3u8');
|
var end = (parts.length > 1 && parts[1].isNotEmpty) ? int.tryParse(parts[1]) : null;
|
||||||
|
end ??= fileSize - 1;
|
||||||
|
|
||||||
if (playlistFile.existsSync() && playlistFile.readAsStringSync().contains('.ts')) {
|
if (start >= fileSize || end >= fileSize || start > end) {
|
||||||
return Response.ok(
|
return Response(416, headers: {'Content-Range': 'bytes */$fileSize'});
|
||||||
playlistFile.openRead(),
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/vnd.apple.mpegurl',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Cache-Control': 'no-cache',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try { existingProcess.kill(); } catch (_) {}
|
final contentLength = end - start + 1;
|
||||||
_vodProcesses.remove('rec_$streamId');
|
final stream = file.openRead(start, end + 1);
|
||||||
if (streamDir.existsSync()) streamDir.deleteSync(recursive: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_vodProcesses.containsKey('rec_$streamId')) {
|
return Response(206, body: stream, headers: {
|
||||||
if (streamDir.existsSync()) streamDir.deleteSync(recursive: true);
|
'Content-Type': 'video/x-matroska',
|
||||||
streamDir.createSync(recursive: true);
|
'Accept-Ranges': 'bytes',
|
||||||
|
'Content-Length': contentLength.toString(),
|
||||||
final useNvidiaGpu = isGpuEnabled?.call() ?? _isNvidiaGpuEnabled();
|
'Content-Range': 'bytes $start-$end/$fileSize',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
final ffmpegArgs = <String>[
|
|
||||||
'-hide_banner', '-loglevel', 'warning',
|
|
||||||
if (useNvidiaGpu) ...['-hwaccel', 'cuda'],
|
|
||||||
'-i', targetUrl,
|
|
||||||
];
|
|
||||||
|
|
||||||
if (useNvidiaGpu) {
|
|
||||||
ffmpegArgs.addAll([
|
|
||||||
'-c:v', 'h264_nvenc', '-preset', 'p4', '-tune', 'hq',
|
|
||||||
'-rc', 'cbr', '-b:v', '4000k', '-maxrate', '4500k', '-bufsize', '8000k',
|
|
||||||
'-g', '48', '-bf', '2', '-pix_fmt', 'yuv420p',
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
ffmpegArgs.addAll([
|
|
||||||
'-c:v', 'libx264', '-preset', 'medium', '-crf', '18',
|
|
||||||
'-maxrate', '12000k', '-bufsize', '24000k', '-pix_fmt', 'yuv420p',
|
|
||||||
'-g', '48', '-threads', '0',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
ffmpegArgs.addAll([
|
|
||||||
'-c:a', 'aac', '-b:a', '192k', '-ar', '48000',
|
|
||||||
'-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',
|
|
||||||
'-f', 'hls', '-hls_time', '4', '-hls_list_size', '0',
|
|
||||||
'-hls_playlist_type', 'event', '-hls_allow_cache', '1',
|
|
||||||
'-hls_flags', 'independent_segments', '-hls_segment_type', 'mpegts',
|
|
||||||
'-hls_segment_filename', 'segment_%03d.ts', '-start_number', '0',
|
|
||||||
'playlist.m3u8',
|
|
||||||
]);
|
|
||||||
|
|
||||||
final ffmpegPath = _getFFmpegPath();
|
|
||||||
Process.start(ffmpegPath, ffmpegArgs, workingDirectory: streamDir.path).then((process) {
|
|
||||||
_vodProcesses['rec_$streamId'] = process;
|
|
||||||
process.stderr.transform(utf8.decoder).listen((data) => print('[FFmpeg Rec $streamId] $data'));
|
|
||||||
process.exitCode.then((code) {
|
|
||||||
print('[Recording Stream] FFmpeg exited with code $code');
|
|
||||||
_vodProcesses.remove('rec_$streamId');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final playlistFile = File('${streamDir.path}/playlist.m3u8');
|
|
||||||
int retries = 0;
|
|
||||||
while (retries < 60) {
|
|
||||||
if (playlistFile.existsSync() && playlistFile.readAsStringSync().contains('.ts')) break;
|
|
||||||
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',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Cache-Control': 'no-cache',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/<streamId>/<segment>', (Request request, String streamId, String segment) async {
|
|
||||||
final file = File('${_hlsTempDir.path}/rec_$streamId/$segment');
|
|
||||||
if (!file.existsSync()) return Response.notFound('Segment not found');
|
|
||||||
return Response.ok(
|
return Response.ok(
|
||||||
file.openRead(),
|
file.openRead(),
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'video/mp2t',
|
'Content-Type': 'video/x-matroska',
|
||||||
|
'Accept-Ranges': 'bytes',
|
||||||
|
'Content-Length': fileSize.toString(),
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
'Cache-Control': 'max-age=3600',
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ class _PlayerScreenState extends ConsumerState<PlayerScreen> {
|
|||||||
widget.containerExtension,
|
widget.containerExtension,
|
||||||
);
|
);
|
||||||
} else if (widget.streamType == StreamType.recording) {
|
} else if (widget.streamType == StreamType.recording) {
|
||||||
streamUrl = '${service.backendBaseUrl}/api/recordings/stream/$currentStreamId/playlist.m3u8';
|
streamUrl = '${service.backendBaseUrl}/api/recordings/stream/$currentStreamId.mkv';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store URL for Lite Player
|
// Store URL for Lite Player
|
||||||
|
|||||||
Reference in new issue
Block a user