This commit is contained in:
root 2025-10-02 09:53:43 +00:00
parent 355544a560
commit dba2222f46
9 changed files with 238 additions and 22 deletions

View File

@ -1,5 +1,5 @@
{ {
"input": "rtmp", "input": "hdmi",
"hdmi": { "hdmi": {
"resolution": "1920x1080", "resolution": "1920x1080",
"audio_source": "hw:1,0" "audio_source": "hw:1,0"

View File

@ -1 +1 @@
ffmpeg -stream_loop -1 -re -i rtmp://127.0.0.1:1935/mahadev/live -vf "scale=1920x1080" -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 ffmpeg -f v4l2 -input_format mjpeg -framerate 30 -video_size 1920x1080 -i /dev/video0 -f alsa -i hw:1,0 -vf "scale=1920x1080" -c:v h264 -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

File diff suppressed because one or more lines are too long

View File

@ -15,28 +15,11 @@ rtmp {
listen 1935; listen 1935;
chunk_size 4096; chunk_size 4096;
application mahadev {
live on;
drop_idle_publisher 10;
idle_streams off;
record off;
meta off;
wait_video on;
allow publish all;
deny play all;
allow play 127.0.0.1;
}
application shree { application shree {
live on; live on;
drop_idle_publisher 10;
idle_streams off;
record off; record off;
meta off; meta off;
wait_video on; wait_video on;
deny publish all;
allow publish 127.0.0.1;
allow play all;
push rtmp://192.168.2.45/live/live1; push rtmp://192.168.2.45/live/live1;
push rtmp://192.168.2.45/live/live2; push rtmp://192.168.2.45/live/live2;

View File

@ -36,7 +36,6 @@ http {
} }
"; ";
#sls.conf
$sls = " $sls = "
srt { srt {
@ -75,6 +74,7 @@ srt {
function update_service() function update_service()
{ {
system('sudo 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';
@ -85,7 +85,7 @@ function update_service()
}; };
$test_cmd = 'sudo /usr/sbin/nginx -t -q'; $test_cmd = 'sudo /usr/sbin/nginx -t -q';
$restart_cmd = 'sudo /bin/systemctl restart nginx'; $restart_cmd = 'sudo /bin/systemctl reload nginx';
$input = "ffmpeg "; $input = "ffmpeg ";
@ -334,6 +334,8 @@ rtmp {
if ($rc === 0) { if ($rc === 0) {
exec($restart_cmd, $out, $rc2); exec($restart_cmd, $out, $rc2);
} }
system('sudo systemctl restart main-encoder');
} }

189
install.sh Executable file
View File

@ -0,0 +1,189 @@
sudo mkdir /etc/srt;
sudo apt update
sudo apt install -y vainfo intel-media-va-driver-non-free i965-va-driver-shaders ffmpeg nginx v4l-utils python3-pip php-fpm mpv libnginx-mod-rtmp alsa-utils vlan git zlib1g-dev
sudo pip3 install psutil --break-system-packages
dpkg -i srt-1.5.5-Linux.deb
cat >/etc/sudoers.d/www-data<<EOL
www-data ALL=(ALL) NOPASSWD: ALL
EOL
cat > /etc/systemd/system/main-encoder.service<<EOL
[Unit]
Description=Main Encoder by ShreeBhattJi
After=network.target
[Service]
ExecStart=/bin/bash /var/www/html/main-encoder.sh
WorkingDirectory=/var/www/html
Restart=always
User=root
Environment=PATH=/usr/bin:/usr/local/bin
[Install]
WantedBy=multi-user.target
EOL
# graph monitor setup
cat > /etc/systemd/system/system-monitor.service<<EOL
[Unit]
Description=Lightweight System Monitor Sampler by ShreeBhattJi
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/local/bin/nginx_system_monitor_sampler.py
Restart=always
RestartSec=2
User=root
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOL
cat > /usr/local/bin/nginx_system_monitor_sampler.py<<EOL
#!/usr/bin/env python3
"""
Lightweight sampler for nginx static frontend.
"""
import time, json, os
from collections import deque
from datetime import datetime
import psutil
OUT_FILE = "/var/www/html/metrics.json"
TMP_FILE = OUT_FILE + ".tmp"
SAMPLE_INTERVAL = 10.0 # seconds between samples
HISTORY_SECONDS = 15 * 60 # 15 minutes
MAX_SAMPLES = int(HISTORY_SECONDS / SAMPLE_INTERVAL)
# circular buffers
timestamps = deque(maxlen=MAX_SAMPLES)
cpu_hist = deque(maxlen=MAX_SAMPLES)
ram_hist = deque(maxlen=MAX_SAMPLES)
net_in_hist = deque(maxlen=MAX_SAMPLES)
net_out_hist = deque(maxlen=MAX_SAMPLES)
disk_read_hist = deque(maxlen=MAX_SAMPLES)
disk_write_hist = deque(maxlen=MAX_SAMPLES)
disk_percent_hist = deque(maxlen=MAX_SAMPLES)
_prev_net = psutil.net_io_counters()
_prev_disk = psutil.disk_io_counters()
_prev_time = time.time()
def sample_once():
global _prev_net, _prev_disk, _prev_time
now = time.time()
iso = datetime.fromtimestamp(now).isoformat(timespec='seconds')
cpu = psutil.cpu_percent(interval=None)
ram = psutil.virtual_memory().percent
net = psutil.net_io_counters()
disk = psutil.disk_io_counters()
try:
disk_percent = psutil.disk_usage("/").percent
except Exception:
disk_percent = 0.0
elapsed = now - _prev_time if _prev_time else SAMPLE_INTERVAL
if elapsed <= 0:
elapsed = SAMPLE_INTERVAL
in_rate = (net.bytes_recv - _prev_net.bytes_recv) / elapsed
out_rate = (net.bytes_sent - _prev_net.bytes_sent) / elapsed
read_rate = (disk.read_bytes - _prev_disk.read_bytes) / elapsed
write_rate = (disk.write_bytes - _prev_disk.write_bytes) / elapsed
timestamps.append(iso)
cpu_hist.append(round(cpu, 2))
ram_hist.append(round(ram, 2))
net_in_hist.append(int(in_rate))
net_out_hist.append(int(out_rate))
disk_read_hist.append(int(read_rate))
disk_write_hist.append(int(write_rate))
disk_percent_hist.append(round(disk_percent, 2))
_prev_net = net
_prev_disk = disk
_prev_time = now
def write_json_atomic():
payload = {
"timestamps": list(timestamps),
"cpu_percent": list(cpu_hist),
"ram_percent": list(ram_hist),
"net_in_Bps": list(net_in_hist),
"net_out_Bps": list(net_out_hist),
"disk_read_Bps": list(disk_read_hist),
"disk_write_Bps": list(disk_write_hist),
"disk_percent": list(disk_percent_hist),
"sample_interval": SAMPLE_INTERVAL,
"generated_at": datetime.utcnow().isoformat(timespec='seconds') + "Z"
}
with open(TMP_FILE, "w") as f:
json.dump(payload, f)
os.replace(TMP_FILE, OUT_FILE)
def main():
global _prev_net, _prev_disk, _prev_time
_prev_net = psutil.net_io_counters()
_prev_disk = psutil.disk_io_counters()
_prev_time = time.time()
time.sleep(0.2) # warm-up
while True:
try:
sample_once()
write_json_atomic()
except Exception as e:
# systemd journal will capture prints
print("Sampler error:", e)
time.sleep(SAMPLE_INTERVAL)
if __name__ == "__main__":
main()
EOL
# srt server setup
sudo chmod +x /etc/srt/srt.sh
sudo cp srt /etc/srt/
cat > /etc/systemd/system/srt.service<<EOL
[Unit]
Description=Srt by ShreeBhattJi
Documentation=https://dbhatt.org
[Service]
Type=simple
User=root
Group=root
TimeoutStartSec=0
Restart=always
RestartSec=30s
Requires=srt
ExecStart=/bin/bash /etc/srt/srt.sh
SyslogIdentifier=srt
#ExecStop=
[Install]
WantedBy=multi-user.target
EOL
cat > /etc/srt/srt.sh<<EOL
/etc/srt/srt -c /var/www/html/sls.conf
EOL
sudo chmod +x /usr/local/bin/nginx_system_monitor_sampler.py
sudo systemctl daemon-reload
sudo systemctl enable --now system-monitor.service
sudo systemctl status system-monitor.service --no-pager
sudo systemctl enable --now main-encoder.service
sudo systemctl status main-encoder.service --no-pager
sudo systemctl enable --now srt.service
sudo systemctl status srt.service --no-pager
sudo systemctl enable --now nginx.service
sudo systemctl status nginx.service --no-pager

BIN
sls Executable file

Binary file not shown.

42
sls.conf Normal file
View File

@ -0,0 +1,42 @@
srt {
worker_threads 8;
worker_connections 100;
log_file /var/log/srt/error.log ;
log_level info;
record_hls_path_prefix /tmp/mov/sls;
server {
listen 1937;
latency 2000;
domain_player domain_playerdomain_playerdomain_player;
domain_publisher domain_publisherdomain_publisherdomain_publisher;
backlog 100;
idle_streams_timeout 15;
app {
app_player app_playerapp_playerapp_player ;
app_publisher app_publisherapp_publisherapp_publisher ;
record_hls off;
record_hls_segment_duration 10;
#relay {
# type pull;
# mode loop;#loop; hash;
# reconnect_interval 10;
# idle_streams_timeout -1;#s -1: unlimited
# upstreams 127.0.0.1:9090?streamid=live.sls.com/live 192.168.1.100:8080/?streamid=live.test.com/live;
#}
#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 ;
#}
}
}
}

BIN
srt-1.5.5-Linux.deb Normal file

Binary file not shown.