Skip to content

Commit d6d4c88

Browse files
committed
Work on streaming via proxy and keeping connection alive
1 parent 1e0487b commit d6d4c88

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

app/Http/Controllers/ChannelStreamController.php

+23-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Channel;
66
use App\Settings\GeneralSettings;
77
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Storage;
89
use Mockery\Exception;
910
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1011
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -21,6 +22,11 @@ class ChannelStreamController extends Controller
2122
*/
2223
public function __invoke(Request $request, $id)
2324
{
25+
// Prevent timeouts
26+
ini_set('max_execution_time', 0);
27+
ini_set('output_buffering', 'off');
28+
ini_set('implicit_flush', 1);
29+
2430
// Find the channel by ID, else throw a 404
2531
$channel = Channel::findOrFail(base64_decode($id));
2632

@@ -38,40 +44,44 @@ public function __invoke(Request $request, $id)
3844

3945
// Stream the content directly from FFmpeg
4046
return new StreamedResponse(function () use ($streamUrls, $enabledDebug) {
47+
if (ob_get_level() > 0) {
48+
ob_end_clean();
49+
while (@ob_end_flush());
50+
}
51+
ini_set('zlib.output_compression', 0);
52+
4153
foreach ($streamUrls as $streamUrl) {
42-
// Try streaming from this URL
43-
$cmd = "ffmpeg -re -i \"$streamUrl\" -c copy -f mpegts pipe:1";
44-
if (!$enabledDebug) {
54+
$cmd = "ffmpeg -re -i \"$streamUrl\" -c copy -f mpegts pipe:1 -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -timeout 100000000 -http_persistent 1";
55+
if (config('dev.ffmpeg.debug')) {
56+
$cmd .= " 2> " . storage_path('logs/' . config('dev.ffmpeg.file'));
57+
} else if (!$enabledDebug) {
4558
$cmd .= " -hide_banner -nostats -loglevel quiet 2>/dev/null";
4659
}
4760
$process = popen($cmd, 'r');
4861

4962
if ($process) {
5063
while (!feof($process)) {
5164
if (connection_aborted()) {
52-
pclose($process); // Attempt to close FFmpeg connection immediately
65+
pclose($process);
5366
return;
5467
}
55-
$data = fread($process, 4096);
68+
$data = fread($process, 8192); // Increased from 4096 to 8192
5669
if ($data === false) {
57-
break; // Stop if no data
70+
break;
5871
}
5972
echo $data;
6073
flush();
6174
}
62-
6375
pclose($process);
64-
return; // Exit if stream works
76+
return;
6577
}
6678
}
67-
68-
// If all streams fail
6979
echo "Error: No available streams.";
7080
}, 200, [
7181
'Content-Type' => 'video/mp2t',
72-
'Cache-Control' => 'no-cache, must-revalidate',
73-
'Pragma' => 'no-cache',
74-
'Expires' => '0',
82+
'Connection' => 'keep-alive',
83+
'Cache-Control' => 'no-store, no-transform',
84+
'X-Accel-Buffering' => 'no', // Prevents Nginx from buffering
7585
]);
7686
}
7787

config/dev.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
'admin_emails' => [
1212
// Default admin email
1313
'admin@test.com'
14+
],
15+
'ffmpeg' => [
16+
'debug' => env('FFMPEG_DEBUG', false),
17+
'file' => env('FFMPEG_DEBUG_FILE', 'ffmpeg.log'),
1418
]
1519
];

0 commit comments

Comments
 (0)