Skip to content

Commit

Permalink
fix: reconnect to sourceStream ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
antebrl committed Dec 21, 2024
1 parent 9fba7a4 commit 84776cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 15 additions & 9 deletions backend/services/streaming/FFmpegService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,35 @@ const { spawn } = require('child_process');
require('dotenv').config();

let currentFFmpegProcess = null;
let currentChannelId = null;
const STORAGE_PATH = process.env.STORAGE_PATH;

function startFFmpeg(nextChannel) {
console.log('Starting FFmpeg process...');
console.log('Starting FFmpeg process with channel:', nextChannel.id);
if (currentFFmpegProcess) {
console.log('Gracefully terminate previous ffmpeg-Prozess...');
currentFFmpegProcess.kill('SIGTERM');
}

const channelUrl = nextChannel.url;
const channelId = nextChannel.id;
currentChannelId = nextChannel.id;
const headers = nextChannel.headers;


currentFFmpegProcess = spawn('ffmpeg', [
'-headers', headers.map(header => `${header.key}: ${header.value}`).join('\r\n'),
'-reconnect', '1',
'-reconnect_at_eof', '1',
'-reconnect_streamed', '1',
'-reconnect_delay_max', '2',
'-i', channelUrl,
'-c', 'copy',
'-f', 'hls',
'-hls_time', '6',
'-hls_list_size', '5',
'-hls_flags', 'delete_segments+program_date_time',
'-start_number', Math.floor(Date.now() / 1000),
`${STORAGE_PATH}${channelId}/${channelId}.m3u8`
`${STORAGE_PATH}${currentChannelId}/${currentChannelId}.m3u8`
]);

// currentFFmpegProcess.stdout.on('data', (data) => {
Expand All @@ -38,13 +43,14 @@ function startFFmpeg(nextChannel) {

currentFFmpegProcess.on('close', (code) => {
console.log(`ffmpeg-Process terminated with code: ${code}`);
currentFFmpegProcess = null;

//Restart if crashed
if (code && code !== 255) {
console.log(`Restarting FFmpeg process...`);
startFFmpeg(nextChannel);
}
// currentFFmpegProcess = null;
// //Restart if crashed
// if (code !== null && code !== 255) {
// console.log(`Restarting FFmpeg process with channel: ${nextChannel.id}`);
// //wait 1 second before restarting
// setTimeout(() => startFFmpeg(nextChannel), 2000);
// }
});
}

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ function VideoPlayer({ channel, syncEnabled }: VideoPlayerProps) {
hls.on(Hls.Events.ERROR, (_, data) => {
if (data.fatal) {

console.error('HLS error:', data);

if (toastStartId) {
removeToast(toastStartId);
}
Expand Down

0 comments on commit 84776cf

Please sign in to comment.