apache2 update

This commit is contained in:
ShreeBhattJi 2025-11-20 23:52:00 +00:00
parent 06bbf2fe39
commit 31a3989918
29 changed files with 773 additions and 235 deletions

1
encoder-main.sh Normal file
View File

@ -0,0 +1 @@
ffmpeg -thread_queue_size 512 -f v4l2 -input_format mjpeg -framerate 30 -video_size 1920x1080 -i /dev/video0 -f alsa -i hw:0,0 -init_hw_device qsv=hw:/dev/dri/renderD128 -filter_hw_device hw -fflags +genpts -use_wallclock_as_timestamps 1 -vf "format=nv12,hwupload=extra_hw_frames=64,format=qsv" -c:v h264_qsv -b:v 3.3M -maxrate 3.3M -bufsize 10M -g 25 -af "aresample=async=1:first_pts=0" -c:a aac -ar 48000 -b:a 96k -vsync 1 -copytb 1 -f mpegts udp://239.255.255.254:39000?localaddr=127.0.0.1

1
encoder-rtmp.sh Executable file
View File

@ -0,0 +1 @@
ffmpeg -fflags nobuffer -i "udp://239.255.255.254:39000?localaddr=127.0.0.1&fifo_size=5000000&overrun_nonfatal=1" -c:v copy -c:a aac -f flv rtmp://127.0.0.1:1935/shree/bhattji

0
html/domain.php → encoder/domain.php Normal file → Executable file
View File

View File

@ -25,6 +25,10 @@
<div>CPU: <span id="lastCpu"></span>% · RAM: <span id="lastRam"></span>% · In: <span id="lastIn"></span>KB/s · <div>CPU: <span id="lastCpu"></span>% · RAM: <span id="lastRam"></span>% · In: <span id="lastIn"></span>KB/s ·
Out: <span id="lastOut"></span>KB/s</div> Out: <span id="lastOut"></span>KB/s</div>
</div> </div>
<br>
<br>
<br>
<br>
</div> </div>
<script> <script>
const POLL_MS = 1000; const POLL_MS = 1000;

22
encoder/input.json Executable file
View File

@ -0,0 +1,22 @@
{
"input": "hdmi",
"hdmi": {
"resolution": "1920x1080",
"audio_source": "hw:0,0",
"framerate": "30"
},
"url": "https://cdn.urmic.org/unavailable.mp4",
"rtmp": {
"mount": "channel_name",
"password": "live",
"port": "1935"
},
"srt": {
"stream_id_1": "har",
"stream_id_2": "har",
"stream_id_3": "mahadev",
"port": "1937"
},
"udp": "udp://@224.1.1.1:8000",
"custom": ""
}

View File

@ -10,14 +10,14 @@ $defaults = [
], ],
'url' => 'https://cdn.urmic.org/unavailable.mp4', 'url' => 'https://cdn.urmic.org/unavailable.mp4',
'rtmp' => [ 'rtmp' => [
'mount' => 'shreebhattji', 'mount' => 'channel_name',
'password' => 'live', 'password' => 'live',
'port' => '1935' 'port' => '1935'
], ],
'srt' => [ 'srt' => [
'stream_id_1' => 'har', 'stream_id_1' => 'har',
'stream_id_2' => 'har', 'stream_id_2' => 'har',
'stream_id_3' => 'Mahadev', 'stream_id_3' => 'mahadev',
'port' => '1937' 'port' => '1937'
], ],
'udp'=>'udp://@224.1.1.1:8000', 'udp'=>'udp://@224.1.1.1:8000',
@ -72,7 +72,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$saveSuccess = "Saved."; $saveSuccess = "Saved.";
} }
update_service(); update_service("input");
} }
?> ?>

1
encoder/metrics.json Executable file

File diff suppressed because one or more lines are too long

140
encoder/output.json Executable file
View File

@ -0,0 +1,140 @@
{
"video": {
"resolution": "1920x1080",
"format": "h264_qsv",
"framerate": "25",
"data_rate": "3.3M",
"gop": "25"
},
"audio": {
"format": "aac",
"sample_rate": "48000",
"bit_rate": "96k",
"db_gain": "mp2"
},
"service_display": "disable",
"output_display": "1920x1080@60.00",
"output_display_audio": "0,3",
"service_rtmp_multiple": "enable",
"service_rtmp_hls": "enable",
"service_rtmp_dash": "enable",
"service_srt_multiple": "disable",
"service_udp": "disable",
"service_custom": "disable",
"rtmp_multiple": {
"1": {
"url": "",
"name": "",
"enabled": false
},
"2": {
"url": "",
"name": "",
"enabled": false
},
"3": {
"url": "",
"name": "",
"enabled": false
},
"4": {
"url": "",
"name": "",
"enabled": false
},
"5": {
"url": "",
"name": "",
"enabled": false
},
"6": {
"url": "",
"name": "",
"enabled": false
},
"7": {
"url": "",
"name": "",
"enabled": false
},
"8": {
"url": "",
"name": "",
"enabled": false
},
"9": {
"url": "",
"name": "",
"enabled": false
},
"10": {
"url": "",
"name": "",
"enabled": false
},
"11": {
"url": "",
"name": "",
"enabled": false
}
},
"srt_multiple": {
"1": {
"url": "",
"name": "",
"enabled": false
},
"2": {
"url": "",
"name": "",
"enabled": false
},
"3": {
"url": "",
"name": "",
"enabled": false
},
"4": {
"url": "",
"name": "",
"enabled": false
},
"5": {
"url": "",
"name": "",
"enabled": false
},
"6": {
"url": "",
"name": "",
"enabled": false
},
"7": {
"url": "",
"name": "",
"enabled": false
},
"8": {
"url": "",
"name": "",
"enabled": false
},
"9": {
"url": "",
"name": "",
"enabled": false
},
"10": {
"url": "",
"name": "",
"enabled": false
},
"11": {
"url": "",
"name": "",
"enabled": false
}
},
"udp": "",
"custom_output": ""
}

View File

@ -71,6 +71,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$new['output_display'] = $get('output_display', $defaults['output_display']); $new['output_display'] = $get('output_display', $defaults['output_display']);
$new['output_display_audio'] = $get('output_display_audio', $defaults['output_display_audio']); $new['output_display_audio'] = $get('output_display_audio', $defaults['output_display_audio']);
$new['service_display'] = $get('service_display', $defaults['service_display']); $new['service_display'] = $get('service_display', $defaults['service_display']);
$new['service_rtmp_multiple'] = $get('service_rtmp_multiple', $defaults['service_rtmp_multiple']);
$new['service_rtmp_hls'] = $get('service_rtmp_hls', $defaults['service_rtmp_hls']);
$new['service_rtmp_dash'] = $get('service_rtmp_dash', $defaults['service_rtmp_dash']);
$new['service_srt_multiple'] = $get('service_srt_multiple', $defaults['service_srt_multiple']);
$new['service_display'] = $get('service_display', $defaults['service_display']);
$new['service_custom'] = $get('service_custom', $defaults['service_custom']);
$new['udp'] = $get('udp', ''); $new['udp'] = $get('udp', '');
$new['custom_output'] = $get('custom_output', ''); $new['custom_output'] = $get('custom_output', '');
@ -100,7 +106,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$success = "Saved."; $success = "Saved.";
} }
} }
update_service(); if (isset($_POST['display'])) {
update_service("display");
}
if (isset($_POST['rtmp'])) {
update_service("rtmp");
}
if (isset($_POST['srt'])) {
update_service("srt");
}
if (isset($_POST['udo'])) {
update_service("udo");
}
if (isset($_POST['custom'])) {
update_service("custom");
}
} }
?> ?>
<form method="POST"> <form method="POST">
@ -198,7 +224,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
<div class="dropdown-container"> <div class="dropdown-container">
<span class="dropdown-label">Service Status :</span> <span class="dropdown-label">Service Status :</span>
<div class="dropdown"> <div class="dropdown">
<select name="service_display" id="display"> <select name="service_display" id="service_display">
<option value="enable" <?php if ($data['service_display'] == 'enable') echo 'selected'; ?>>Enable</option> <option value="enable" <?php if ($data['service_display'] == 'enable') echo 'selected'; ?>>Enable</option>
<option value="disable" <?php if ($data['service_display'] == 'disable') echo 'selected'; ?>>Disable</option> <option value="disable" <?php if ($data['service_display'] == 'disable') echo 'selected'; ?>>Disable</option>
</select> </select>
@ -284,6 +310,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
</select> </select>
</div> </div>
</div> </div>
<div style="text-align:center; width:100%; margin-top:12px;">
<button type="submit" name="display" style="background:#c00;color:#fff;padding:10px 20px;border:none;font-weight:bold;border-radius:6px;">Save Display</button>
</div>
</div> </div>
<div class="card wide"> <div class="card wide">
@ -334,6 +364,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
</div> </div>
</div> </div>
<?php endfor; ?> <?php endfor; ?>
<div style="text-align:center; width:100%; margin-top:12px;">
<button type="submit" name="rtmp" style="background:#c00;color:#fff;padding:10px 20px;border:none;font-weight:bold;border-radius:6px;">Save RTMP</button>
</div>
</div> </div>
<div class="card wide"> <div class="card wide">
@ -366,6 +400,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
</div> </div>
</div> </div>
<?php endfor; ?> <?php endfor; ?>
<div style="text-align:center; width:100%; margin-top:12px;">
<button type="submit" name="srt" style="background:#c00;color:#fff;padding:10px 20px;border:none;font-weight:bold;border-radius:6px;">Save SRT</button>
</div>
</div> </div>
<div class="card wide"> <div class="card wide">
@ -383,6 +420,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
<input type="text" id="udp" name="udp" placeholder="udp" value="<?php echo htmlspecialchars($data['udp']); ?>"> <input type="text" id="udp" name="udp" placeholder="udp" value="<?php echo htmlspecialchars($data['udp']); ?>">
<label for="udp">UDP Primary URL</label> <label for="udp">UDP Primary URL</label>
</div> </div>
<div style="text-align:center; width:100%; margin-top:12px;">
<button type="submit" name="udp" style="background:#c00;color:#fff;padding:10px 20px;border:none;font-weight:bold;border-radius:6px;">Save UDP</button>
</div>
</div> </div>
<div class="card wide"> <div class="card wide">
@ -400,15 +440,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
<input type="text" id="custom_output" name="custom_output" placeholder="custom" value="<?php echo htmlspecialchars($data['custom_output']); ?>"> <input type="text" id="custom_output" name="custom_output" placeholder="custom" value="<?php echo htmlspecialchars($data['custom_output']); ?>">
<label for="custom_output">Custom Output</label> <label for="custom_output">Custom Output</label>
</div> </div>
</div>
</div>
<div style="text-align:center; width:100%; margin-top:12px;"> <div style="text-align:center; width:100%; margin-top:12px;">
<button type="submit" style="background:#c00;color:#fff;padding:10px 20px;border:none;font-weight:bold;border-radius:6px;">Save</button> <button type="submit" name="custom" style="background:#c00;color:#fff;padding:10px 20px;border:none;font-weight:bold;border-radius:6px;">Save Custom</button>
</div>
</div>
</div> </div>
<br><br><br>
<br><br><br>
</form> </form>
<?php <?php

View File

0
html/request_cert.php → encoder/request_cert.php Normal file → Executable file
View File

357
encoder/static.php Executable file
View File

@ -0,0 +1,357 @@
<?php
$nginx_top = "
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 2048;
multi_accept on;
}
";
$nginx_bottom = "
http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
";
$sls = "
srt {
worker_threads 64;
worker_connections 500;
log_file /tmp/logs/error.log ;
log_level info;
server {
listen 1937;
latency 1000; #ms
domain_player srt.urmic.org;
domain_publisher pass1pass1pass1;
backlog 100;
idle_streams_timeout 10;#s -1: unlimited
app {
app_player onshreeganeshaynamah ;
app_publisher pass2pass2pass2 ;
record_hls off;#on, off
record_hls_segment_duration 10; #unit s
#relay {
# type push;
# mode all; #all; hash
# reconnect_interval 10;
# idle_streams_timeout 10;#s -1: unlimited
# upstreams 192.168.31.106:8080?streamid=uplive.sls.com/live ;
#}
}
}
}
";
function update_service($which_service)
{
$candidate = '/var/www/html/nginx.conf';
$fallback = '/var/www/html/default_nginx.conf';
$target = '/etc/nginx/nginx.conf';
$cp_cmd = function (string $src, string $dst): string {
return 'sudo /bin/cp ' . escapeshellarg($src) . ' ' . escapeshellarg($dst);
};
$test_cmd = 'sudo /usr/sbin/nginx -t -q';
$restart_cmd = 'sudo /bin/systemctl reload nginx';
$input = "";
$input_link = "";
$input_source = "";
$input_rtmp_port = "";
$input_port_srt = "";
$input_rtmp_mount = "";
$input_rtmp_pass = "";
$output = "";
global $nginx_top;
global $nginx_bottom;
$rtmp_multiple[] = [];
$srt_multiple[] = [];
$defaults = [
'input' => 'url',
'hdmi' => [
'resolution' => '1920x1080',
'audio_source' => 'hw:1,0',
'framerate' => '30'
],
'url' => 'https://cdn.urmic.org/unavailable.mp4',
'rtmp' => [
'mount' => 'channel_name',
'password' => 'live',
'port' => '1935'
],
'srt' => [
'stream_id_1' => 'har',
'stream_id_2' => 'har',
'stream_id_3' => 'mahadev',
'port' => '1937'
],
'udp' => 'udp://@224.1.1.1:8000',
'custom' => ''
];
$jsonFile = __DIR__ . '/input.json';
if (file_exists($jsonFile)) {
$raw = file_get_contents($jsonFile);
$data = json_decode($raw, true);
if (!is_array($data)) $data = $defaults;
}
$input_source = $data['input'];
$input_rtmp_port = $data['rtmp']['port'];
$input_rtmp_mount = $data['rtmp']['mount'];
$input_rtmp_pass = $data['rtmp']['password'];
$input_port_srt = $data['srt']['port'];
if ($input_rtmp_port === "80" || $input_rtmp_port === "443" || $input_port_srt === "80" || $input_port_srt === "443") {
echo '<script>alert("80 or 443 port is not allowed .");</script>';
die();
}
switch ($input_source) {
case "hdmi":
$input = "ffmpeg -thread_queue_size 512 -f v4l2 -input_format mjpeg -framerate " . $data['hdmi']['framerate'] . " -video_size " . $data['hdmi']['resolution'] . " -i /dev/video0 " .
"-f alsa -i " . $data['hdmi']['audio_source'] . ' -init_hw_device qsv=hw:/dev/dri/renderD128 -filter_hw_device hw -fflags +genpts -use_wallclock_as_timestamps 1 -vf "format=nv12,hwupload=extra_hw_frames=64,format=qsv" -c:v h264_qsv';
break;
case "url":
$input .= "ffmpeg -hwaccel auto -stream_loop -1 -re -i " . $data['url'];
break;
case "rtmp":
$input .= "ffmpeg -hwaccel auto -stream_loop -1 -re -i rtmp://127.0.0.1:" . $$input_rtmp_port . "/" . $$input_rtmp_mount . "/" . $input_rtmp_pass;
break;
case "srt":
$input .= "-stream_loop -1 -re -i srt://127.0.0.1:" . $data['srt']['port'] . "/" . $data['srt']['stream_id_1'] . "/" . $data['srt']['stream_id_2'] . "/" . $data['srt']['stream_id_3'];
$input_port_srt = $data['srt']['port'];
break;
}
$input .= " ";
$jsonFile = __DIR__ . '/output.json';
$defaults = [
'video' => [
'resolution' => '1920x1080',
'format' => 'h264_qsv',
'framerate' => '25',
'data_rate' => '3.3M',
'gop' => '12'
],
'audio' => [
'format' => 'aac',
'sample_rate' => '48000',
'bit_rate' => '96k',
'db_gain' => '0dB'
],
'service_display' => 'disable',
'output_display' => '1920x1080@60.00',
'output_display_audio' => '0,3',
'service_rtmp_multiple' => 'disable',
'service_rtmp_hls' => 'disable',
'service_rtmp_dash' => 'disable',
'service_srt_multiple' => 'disable',
'service_udp' => 'disable',
'service_custom' => 'disable',
'rtmp_multiple' => [],
'srt_multiple' => [],
'udp' => '',
'custom_output' => ''
];
for ($i = 1; $i <= 11; $i++) {
$defaults['rtmp_multiple'][$i] = ['url' => '', 'name' => '', 'enabled' => false];
$defaults['srt_multiple'][$i] = ['url' => '', 'name' => '', 'enabled' => false];
}
if (file_exists($jsonFile)) {
$raw = file_get_contents($jsonFile);
$data = json_decode($raw, true);
if (!is_array($data)) $data = $defaults;
$data = array_replace_recursive($defaults, $data);
} else {
$data = $defaults;
}
$service_display = $data['service_display'];
$service_rtmp_multiple = $data['service_rtmp_multiple'];
$service_rtmp_hls = $data['service_rtmp_hls'];
$service_rtmp_dash = $data['service_rtmp_dash'];
$service_srt_multiple = $data['service_srt_multiple'];
$service_udp = $data['service_udp'];
$service_custom = $data['service_custom'];
$rtmp_multiple = $data['rtmp_multiple'];
$srt_multiple = $data['srt_multiple'];
$input .= ' -b:v ' . $data['video']['data_rate'] . ' -maxrate ' . $data['video']['data_rate'] . ' -bufsize 10M -g ' . $data['video']['gop'] . ' -af "aresample=async=1:first_pts=0" ' .
' -c:a ' . $data['audio']['format'] . ' -ar ' . $data['audio']['sample_rate'] . ' -b:a ' . $data['audio']['bit_rate'] . ' -vsync 1 -copytb 1 -f mpegts udp://239.255.255.254:39000?localaddr=127.0.0.1';
$service = $input;
$file = "/var/www/encoder-main.sh";
if (file_put_contents($file, $service) !== false) {
echo "File saved.";
} else {
echo "Error writing file.";
}
switch ($which_service) {
case 'input':
exec('sudo systemctl restart encoder-main');
break;
case 'display';
break;
case 'rtmp';
//if hls service enable add this to nginx
if ($service_rtmp_hls === "enable") {
$hls = "
hls on;
hls_path /var/www/html/hls/shree;
hls_fragment 3;
hls_playlist_length 60;
";
} else {
$hls = "
";
}
//if dash service enable add this to nginx
if ($service_rtmp_dash === "enable") {
$dash = "
dash on;
dash_path /var/www/html/dash/shree;
";
} else {
$dash = "
";
}
if (empty($input_rtmp_port))
$input_rtmp_port = "1935";
$rtmp_push = "";
for ($i = 1; $i <= 11; $i++) {
if ($rtmp_multiple[$i]['enabled'] == 1) {
$rtmp_push .= "
push " . $rtmp_multiple[$i]['url'] . ";";
}
}
$nginx = $nginx_top;
if ($input_source === "rtmp") {
$nginx .= "
rtmp {
server {
listen " . $input_rtmp_port . ";
chunk_size 4096;
application " . $input_rtmp_mount . " {
live on;
record off;
meta off;
wait_video on;
}
application shree {
live on;
record off;
meta off;
wait_video on;
allow publish 127.0.0.1;
deny publish all;
" . $rtmp_push . "
}
}
}
";
} else {
$nginx .= "
rtmp {
server {
listen 1935;
chunk_size 4096;
application shree {
live on;
record off;
meta off;
wait_video on;
" . $rtmp_push . "
" . $hls . "
" . $dash . "
}
}
}";
}
$nginx .= $nginx_bottom;
$file = "/var/www/nginx.conf";
file_put_contents($file, $nginx);
if ($service_rtmp_multiple === "enable") {
$rtmp = 'ffmpeg -fflags nobuffer -i "udp://239.255.255.254:39000?localaddr=127.0.0.1&fifo_size=5000000&overrun_nonfatal=1" -c:v copy -c:a aac -f flv rtmp://127.0.0.1:1935/shree/bhattji';
$file = "/var/www/encoder-rtmp.sh";
file_put_contents($file, $rtmp);
exec('sudo cp /var/www/nginx.conf /etc/nginx/');
exec("$nginx -t 2>&1", $output, $status);
if ($status === 0) {
exec("sudo systemctl restart nginx 2>&1", $o, $s);
while (trim(shell_exec("systemctl is-active nginx")) !== "active") {
sleep(1);
}
exec("sudo systemctl enable encoder-rtmp");
} else {
exec('sudo cp /var/www/nginx.conf /etc/nginx/');
exec("sudo systemctl restart nginx");
}
} {
}
break;
case "srt";
break;
case "udp";
break;
case "custom";
break;
default:
error_log("Error no input found");
break;
}
}
function update_firewall() {}
function update_network() {}
function update_firmware() {}

0
html/status.php → encoder/status.php Normal file → Executable file
View File

View File

@ -1,117 +0,0 @@
<?php
// firewall-form-handler.php
$jsonFile = __DIR__ . '/firewall.json';
// defaults
$defaults = [
'firewall' => 'disable',
'ips' => ['', '', '', '', '']
];
// load existing data
if (file_exists($jsonFile)) {
$raw = file_get_contents($jsonFile);
$data = json_decode($raw, true);
if (!is_array($data)) $data = $defaults;
} else {
$data = $defaults;
}
$errors = [];
$saveSuccess = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// read posted values
$posted_firewall = isset($_POST['firewall']) && in_array($_POST['firewall'], ['enable', 'disable']) ? $_POST['firewall'] : 'disable';
$posted_ips = [];
for ($i = 1; $i <= 5; $i++) {
$key = "ip{$i}";
$val = isset($_POST[$key]) ? trim((string)$_POST[$key]) : '';
$posted_ips[] = $val;
}
// validate IPs (allow empty)
foreach ($posted_ips as $index => $ip) {
if ($ip !== '' && !filter_var($ip, FILTER_VALIDATE_IP)) {
$errors[] = "IP" . ($index + 1) . " is not a valid IP address: " . htmlspecialchars($ip);
}
}
if (empty($errors)) {
$new = [
'firewall' => $posted_firewall,
'ips' => $posted_ips
];
$json = json_encode($new, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
if (file_put_contents($jsonFile, $json, LOCK_EX) === false) {
$errors[] = "Failed to write {$jsonFile}. Check permissions.";
} else {
$data = $new;
$saveSuccess = "Saved.";
}
}
}
?>
<?php include 'header.php'; ?>
<form method="POST" novalidate>
<div class="containerindex">
<div class="grid">
<div class="card wide">
<h3>Firewall</h3>
<br>
<div class="input-container">
<input type="radio" id="firewall_enable" name="firewall" value="enable" <?php if ($data['firewall'] === 'enable') echo 'checked'; ?>>
<label for="firewall_enable">Enable</label><br>
<input type="radio" id="firewall_disable" name="firewall" value="disable" <?php if ($data['firewall'] === 'disable') echo 'checked'; ?>>
<label for="firewall_disable">Disable</label><br>
</div>
<br>
<br>
<br>
<div class="input-group">
<input type="text" id="ip1" name="ip1" placeholder="IP1" value="<?php echo htmlspecialchars($data['ips'][0] ?? ''); ?>">
<label for="ip1">IP1</label>
</div>
<div class="input-group">
<input type="text" id="ip2" name="ip2" placeholder="IP2" value="<?php echo htmlspecialchars($data['ips'][1] ?? ''); ?>">
<label for="ip2">IP2</label>
</div>
<div class="input-group">
<input type="text" id="ip3" name="ip3" placeholder="IP3" value="<?php echo htmlspecialchars($data['ips'][2] ?? ''); ?>">
<label for="ip3">IP3</label>
</div>
<div class="input-group">
<input type="text" id="ip4" name="ip4" placeholder="IP4" value="<?php echo htmlspecialchars($data['ips'][3] ?? ''); ?>">
<label for="ip4">IP4</label>
</div>
<div class="input-group">
<input type="text" id="ip5" name="ip5" placeholder="IP5" value="<?php echo htmlspecialchars($data['ips'][4] ?? ''); ?>">
<label for="ip5">IP5</label>
</div>
<div style="text-align:center; width:100%; margin-top:12px;">
<button type="submit" style="background:#c00;color:#fff;padding:10px 20px;border:none;font-weight:bold;border-radius:6px;">Save</button>
</div>
</div>
</div>
</div>
<br>
<br>
</form>
<?php
// status output
if (!empty($errors)) {
echo '<div style="color:#b00;text-align:center;margin-top:12px;">';
foreach ($errors as $e) echo '<div>' . $e . '</div>';
echo '</div>';
}
if ($saveSuccess) {
echo '<div style="color:green;text-align:center;margin-top:12px;">' . htmlspecialchars($saveSuccess) . '</div>';
}
?>
<?php include 'footer.php'; ?>

View File

@ -1 +0,0 @@
ffmpeg -stream_loop -1 -re -i https://cdn.urmic.org/unavailable.mp4 -vf "scale=1920x1080" -c:v mpeg2video -b:v 3.3M -minrate 3.3M -maxrate 3.3M -bufsize 3.3M -g 12 -c:a aac -ar 48000 -b:a 96k -f flv rtmp://127.0.0.1:1935/shree/bhattji

1
html/metrics.json Normal file

File diff suppressed because one or more lines are too long

175
html/static.php Executable file → Normal file
View File

@ -72,11 +72,9 @@ srt {
} }
"; ";
function update_service() function update_service($which_service)
{ {
shell_exec("sudo /bin/systemctl stop main-encoder");
$candidate = '/var/www/html/nginx.conf'; $candidate = '/var/www/html/nginx.conf';
$fallback = '/var/www/html/default_nginx.conf'; $fallback = '/var/www/html/default_nginx.conf';
$target = '/etc/nginx/nginx.conf'; $target = '/etc/nginx/nginx.conf';
@ -110,16 +108,18 @@ function update_service()
], ],
'url' => 'https://cdn.urmic.org/unavailable.mp4', 'url' => 'https://cdn.urmic.org/unavailable.mp4',
'rtmp' => [ 'rtmp' => [
'mount' => '', 'mount' => 'channel_name',
'password' => 'live', 'password' => 'live',
'port' => '1935' 'port' => '1935'
], ],
'srt' => [ 'srt' => [
'stream_id_1' => 'har', 'stream_id_1' => 'har',
'stream_id_2' => 'har', 'stream_id_2' => 'har',
'stream_id_3' => 'Mahadev', 'stream_id_3' => 'mahadev',
'port' => '1937' 'port' => '1937'
] ],
'udp' => 'udp://@224.1.1.1:8000',
'custom' => ''
]; ];
$jsonFile = __DIR__ . '/input.json'; $jsonFile = __DIR__ . '/input.json';
@ -146,10 +146,10 @@ function update_service()
"-f alsa -i " . $data['hdmi']['audio_source'] . ' -init_hw_device qsv=hw:/dev/dri/renderD128 -filter_hw_device hw -fflags +genpts -use_wallclock_as_timestamps 1 -vf "format=nv12,hwupload=extra_hw_frames=64,format=qsv" -c:v h264_qsv'; "-f alsa -i " . $data['hdmi']['audio_source'] . ' -init_hw_device qsv=hw:/dev/dri/renderD128 -filter_hw_device hw -fflags +genpts -use_wallclock_as_timestamps 1 -vf "format=nv12,hwupload=extra_hw_frames=64,format=qsv" -c:v h264_qsv';
break; break;
case "url": case "url":
$input .= "ffmpeg -stream_loop -1 -re -i " . $data['url']; $input .= "ffmpeg -hwaccel auto -stream_loop -1 -re -i " . $data['url'];
break; break;
case "rtmp": case "rtmp":
$input .= "-stream_loop -1 -re -i rtmp://127.0.0.1:" . $data['rtmp']['port'] . "/" . $data['rtmp']['mount'] . "/" . $data['rtmp']['password']; $input .= "ffmpeg -hwaccel auto -stream_loop -1 -re -i rtmp://127.0.0.1:" . $$input_rtmp_port . "/" . $$input_rtmp_mount . "/" . $input_rtmp_pass;
break; break;
case "srt": case "srt":
$input .= "-stream_loop -1 -re -i srt://127.0.0.1:" . $data['srt']['port'] . "/" . $data['srt']['stream_id_1'] . "/" . $data['srt']['stream_id_2'] . "/" . $data['srt']['stream_id_3']; $input .= "-stream_loop -1 -re -i srt://127.0.0.1:" . $data['srt']['port'] . "/" . $data['srt']['stream_id_1'] . "/" . $data['srt']['stream_id_2'] . "/" . $data['srt']['stream_id_3'];
@ -160,28 +160,32 @@ function update_service()
$jsonFile = __DIR__ . '/output.json'; $jsonFile = __DIR__ . '/output.json';
// default structure
$defaults = [ $defaults = [
'output' => 'display',
'video' => [ 'video' => [
'resolution' => '1920x1080', 'resolution' => '1920x1080',
'format' => 'h264_h264', 'format' => 'h264_qsv',
'data_rate' => '4M', 'framerate' => '25',
'data_rate' => '3.3M',
'gop' => '12' 'gop' => '12'
], ],
'audio' => [ 'audio' => [
'format' => 'aac', 'format' => 'aac',
'sample_rate' => '48000', 'sample_rate' => '48000',
'bit_rate' => '96k' 'bit_rate' => '96k',
'db_gain' => '0dB'
], ],
'service_display' => 'disable',
'output_display' => '1920x1080@60.00', 'output_display' => '1920x1080@60.00',
'output_display_audio' => '0,3', 'output_display_audio' => '0,3',
'rtmp_single' => '', 'service_rtmp_multiple' => 'disable',
'srt_single' => '', 'service_rtmp_hls' => 'disable',
'service_rtmp_dash' => 'disable',
'service_srt_multiple' => 'disable',
'service_udp' => 'disable',
'service_custom' => 'disable',
'rtmp_multiple' => [], 'rtmp_multiple' => [],
'srt_multiple' => [], 'srt_multiple' => [],
'udp_primary' => '', 'udp' => '',
'udp_vlan' => '',
'custom_output' => '' 'custom_output' => ''
]; ];
@ -199,55 +203,56 @@ function update_service()
$data = $defaults; $data = $defaults;
} }
$service_display = $data['service_display'];
$service_rtmp_multiple = $data['service_rtmp_multiple'];
$service_rtmp_hls = $data['service_rtmp_hls'];
$service_rtmp_dash = $data['service_rtmp_dash'];
$service_srt_multiple = $data['service_srt_multiple'];
$service_udp = $data['service_udp'];
$service_custom = $data['service_custom'];
$rtmp_multiple = $data['rtmp_multiple']; $rtmp_multiple = $data['rtmp_multiple'];
$srt_multiple = $data['srt_multiple']; $srt_multiple = $data['srt_multiple'];
$input .= ' -b:v ' . $data['video']['data_rate'] . ' -maxrate ' . $data['video']['data_rate'] . ' -bufsize 10M -g ' . $data['video']['gop'] .' -af "aresample=async=1:first_pts=0" '. $input .= ' -b:v ' . $data['video']['data_rate'] . ' -maxrate ' . $data['video']['data_rate'] . ' -bufsize 10M -g ' . $data['video']['gop'] . ' -af "aresample=async=1:first_pts=0" ' .
' -c:a ' . $data['audio']['format'] . ' -ar ' . $data['audio']['sample_rate'] . ' -b:a ' . $data['audio']['bit_rate'] . ' -vsync 1 -copytb 1 -f mpegts udp://239.255.255.254:39000?localaddr=127.0.0.1'; ' -c:a ' . $data['audio']['format'] . ' -ar ' . $data['audio']['sample_rate'] . ' -b:a ' . $data['audio']['bit_rate'] . ' -vsync 1 -copytb 1 -f mpegts udp://239.255.255.254:39000?localaddr=127.0.0.1';
$service = $input; $service = $input;
$file = "/var/www/main-encoder.sh"; $file = "/var/www/encoder-main.sh";
if (file_put_contents($file, $service) !== false) { if (file_put_contents($file, $service) !== false) {
echo "File saved."; echo "File saved.";
} else { } else {
echo "Error writing file."; echo "Error writing file.";
} }
switch ($which_service) {
case 'input':
exec('sudo systemctl restart encoder-main');
break;
case 'display';
break;
case 'rtmp';
//if hls service enable add this to nginx
if ($service_rtmp_hls === "enable") {
$hls = "
hls on;
hls_path /var/www/html/hls/shree;
hls_fragment 3;
hls_playlist_length 60;
";
} else {
$hls = "
";
}
switch ($data['output']) { //if dash service enable add this to nginx
case "display": if ($service_rtmp_dash === "enable") {
$output_display_audio = $data['output_display_audio']; $dash = "
$input = "mpv --fs --loop --hwdec=auto --audio-device=alsa/plughw:" . $output_display_audio . " udp://239.255.255.254:39000"; dash on;
break; dash_path /var/www/html/dash/shree;
case "rtmp_single": ";
$input .= '-vf "scale=' . $data['video']['resolution'] . '" -c:v ' . $data['video']['format'] . ' -b:v ' . $data['video']['data_rate'] } else {
. ' -minrate ' . $data['video']['data_rate'] . ' -maxrate ' . $data['video']['data_rate'] . ' -bufsize ' . $data['video']['data_rate'] . ' -g ' . $data['video']['gop'] . $dash = "
' -c:a ' . $data['audio']['format'] . ' -ar ' . $data['audio']['sample_rate'] . ' -b:a ' . $data['audio']['bit_rate'] . ' -f flv ' . $data['rtmp_single']; ";
break;
case "srt_single":
$input .= '-vf "scale=' . $data['video']['resolution'] . '" -c:v ' . $data['video']['format'] . ' -b:v ' . $data['video']['data_rate'] . ' -minrate ' . $data['video']['data_rate'] . ' -maxrate ' . $data['video']['data_rate'] . ' -bufsize ' . $data['video']['data_rate'] . ' -g ' . $data['video']['gop'] .
' -c:a ' . $data['audio']['format'] . ' -ar ' . $data['audio']['sample_rate'] . ' -b:a ' . $data['audio']['bit_rate'] . ' -f mpegts ' . $data['srt_single'];
break;
case "rtmp_multiple":
$input .= '-vf "scale=' . $data['video']['resolution'] . '" -c:v ' . $data['video']['format'] . ' -b:v ' . $data['video']['data_rate']
. ' -minrate ' . $data['video']['data_rate'] . ' -maxrate ' . $data['video']['data_rate'] . ' -bufsize ' . $data['video']['data_rate'] . ' -g ' . $data['video']['gop'] .
' -c:a ' . $data['audio']['format'] . ' -ar ' . $data['audio']['sample_rate'] . ' -b:a ' . $data['audio']['bit_rate'] . ' -f flv rtmp://127.0.0.1:'
. $input_rtmp_port . '/shree/bhattji';
break;
case "srt_multiple":
if (empty($input_port))
$input_port = "1937";
$input .= '-vf "scale=' . $data['video']['resolution'] . '" -c:v ' . $data['video']['format'] . ' -b:v ' . $data['video']['data_rate']
. ' -minrate ' . $data['video']['data_rate'] . ' -maxrate ' . $data['video']['data_rate'] . ' -bufsize ' . $data['video']['data_rate'] . ' -g ' . $data['video']['gop'] .
' -c:a ' . $data['audio']['format'] . ' -ar ' . $data['audio']['sample_rate'] . ' -b:a ' . $data['audio']['bit_rate'] . ' -f mpegts srt://127.0.0.1:'
. $input_port . '/shree/bhatt/ji';
break;
case "udp_primary":
break;
case "udp_vlan":
break;
case "custom_output":
break;
} }
if (empty($input_rtmp_port)) if (empty($input_rtmp_port))
@ -282,9 +287,9 @@ rtmp {
record off; record off;
meta off; meta off;
wait_video on; wait_video on;
" . allow publish 127.0.0.1;
$rtmp_push deny publish all;
. " " . $rtmp_push . "
} }
} }
} }
@ -301,39 +306,47 @@ rtmp {
record off; record off;
meta off; meta off;
wait_video on; wait_video on;
" . " . $rtmp_push . "
$rtmp_push " . $hls . "
. " " . $dash . "
} }
} }
} }";
";
} }
$nginx .= $nginx_bottom; $nginx .= $nginx_bottom;
$file = "/var/www/html/nginx.conf"; $file = "/var/www/nginx.conf";
if (file_put_contents($file, $nginx) !== false) { file_put_contents($file, $nginx);
echo "File saved.";
if ($service_rtmp_multiple === "enable") {
$rtmp = 'ffmpeg -fflags nobuffer -i "udp://239.255.255.254:39000?localaddr=127.0.0.1&fifo_size=5000000&overrun_nonfatal=1" -c:v copy -c:a aac -f flv rtmp://127.0.0.1:1935/shree/bhattji';
$file = "/var/www/encoder-rtmp.sh";
file_put_contents($file, $rtmp);
exec('sudo cp /var/www/nginx.conf /etc/nginx/');
exec("$nginx -t 2>&1", $output, $status);
if ($status === 0) {
// Config OK → restart nginx
exec("sudo systemctl restart nginx");
exec("sudo systemctl reset-failed encoder-rtmp.service");
exec("sudo systemctl restart encoder-rtmp");
} else { } else {
echo "Error writing file."; exec('sudo cp /var/www/nginx.conf /etc/nginx/');
exec("sudo systemctl restart nginx");
exec("sudo systemctl stop encoder-rtmp");
}
} {
} }
exec($cp_cmd($candidate, $target), $out, $rc); break;
// if nginx config test OK, restart and exit case "srt";
exec($test_cmd, $out, $rc); break;
if ($rc === 0) { case "udp";
exec($restart_cmd, $out, $rc2); break;
return; case "custom";
break;
default:
error_log("Error no input found");
break;
} }
// fallback copy
exec($cp_cmd($fallback, $target), $out, $rc);
exec($test_cmd, $out, $rc);
if ($rc === 0) {
exec($restart_cmd, $out, $rc2);
}
sleep(10);
shell_exec("sudo /bin/systemctl restart main-encoder");
} }

View File

@ -1,6 +1,6 @@
sudo mkdir /etc/srt; sudo mkdir /etc/srt;
sudo apt update sudo apt update
sudo apt install -y vainfo intel-media-va-driver-non-free i965-va-driver-shaders ffmpeg nginx v4l-utils python3-pip php8.3-fpm mpv libnginx-mod-rtmp alsa-utils vlan git zlib1g-dev sudo apt install -y vainfo intel-media-va-driver-non-free i965-va-driver libmfx1 certbot intel-gpu-tools python3-certbot-nginx ffmpeg nginx v4l-utils python3-pip php8.3-fpm mpv libnginx-mod-rtmp alsa-utils vlan git zlib1g-dev
sudo pip3 install psutil --break-system-packages sudo pip3 install psutil --break-system-packages
dpkg -i srt-1.5.5-Linux.deb dpkg -i srt-1.5.5-Linux.deb
@ -9,14 +9,33 @@ cat >/etc/sudoers.d/www-data<<EOL
www-data ALL=(ALL) NOPASSWD: ALL www-data ALL=(ALL) NOPASSWD: ALL
EOL EOL
cat > /etc/systemd/system/main-encoder.service<<EOL cat > /etc/systemd/system/encoder-main.service<<EOL
[Unit] [Unit]
Description=Main Encoder by ShreeBhattJi Description=Main Encoder by ShreeBhattJi
After=network.target Requires=nginx.service
After=nginx.service
[Service] [Service]
ExecStart=/bin/bash /var/www/html/main-encoder.sh ExecStart=/bin/bash /var/www/encoder-main.sh
WorkingDirectory=/var/www/html WorkingDirectory=/var/www/
Restart=always
User=root
Environment=PATH=/usr/bin:/usr/local/bin
[Install]
WantedBy=multi-user.target
EOL
cat > /etc/systemd/system/encoder-rtmp.service<<EOL
[Unit]
Description= Rtmp Encoder by ShreeBhattJi
Requires=nginx.service
After=nginx.service
[Service]
ExecStart=/bin/bash /var/www/encoder-rtmp.sh
WorkingDirectory=/var/www/
Restart=always Restart=always
User=root User=root
Environment=PATH=/usr/bin:/usr/local/bin Environment=PATH=/usr/bin:/usr/local/bin
@ -93,8 +112,8 @@ def sample_once():
if elapsed <= 0: if elapsed <= 0:
elapsed = SAMPLE_INTERVAL elapsed = SAMPLE_INTERVAL
in_rate = (net.bytes_recv - _prev_net.bytes_recv) / elapsed in_rate = int(((net.bytes_recv - _prev_net.bytes_recv) / elapsed) * 8)
out_rate = (net.bytes_sent - _prev_net.bytes_sent) / elapsed out_rate = int(((net.bytes_sent - _prev_net.bytes_sent) / elapsed) * 8)
read_rate = (disk.read_bytes - _prev_disk.read_bytes) / elapsed read_rate = (disk.read_bytes - _prev_disk.read_bytes) / elapsed
write_rate = (disk.write_bytes - _prev_disk.write_bytes) / elapsed write_rate = (disk.write_bytes - _prev_disk.write_bytes) / elapsed
@ -205,10 +224,13 @@ sudo chmod +x /usr/local/bin/nginx_system_monitor_sampler.py
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl enable --now system-monitor.service sudo systemctl enable --now system-monitor.service
sudo systemctl status system-monitor.service --no-pager sudo systemctl status system-monitor.service --no-pager
sudo systemctl enable --now main-encoder.service sudo systemctl enable --now encoder-main.service
sudo systemctl status main-encoder.service --no-pager sudo systemctl status encoder-main.service --no-pager
sudo systemctl enable --now srt.service sudo systemctl enable --now srt.service
sudo systemctl status srt.service --no-pager sudo systemctl status srt.service --no-pager
sudo mkdir -p /var/www/html/hls/shree;
sudo mkdir -p /var/www/html/dash/shree;
sudo systemctl enable --now nginx.service sudo systemctl enable --now nginx.service
sudo systemctl status nginx.service --no-pager sudo systemctl status nginx.service --no-pager
sudo chmod -R 777 /var/www/html/* sudo chmod -R 777 /var/www/html/*

56
nginx.conf Normal file
View File

@ -0,0 +1,56 @@
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 2048;
multi_accept on;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application shree {
live on;
record off;
meta off;
wait_video on;
hls on;
hls_path /var/www/html/hls/shree;
hls_fragment 3;
hls_playlist_length 60;
dash on;
dash_path /var/www/html/dash/shree;
}
}
}
http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}