






sudo apt update
sudo apt install -y git build-essential pkg-config libssl-dev zlib1g-dev
apt update
apt install -y \
  git \
  unzip \
  automake \
  autoconf \
  libtool \
  build-essential \
  pkg-config \
  cmake \
  libssl-dev \
  zlib1g-dev \
  libpcre3 \
  libpcre3-dev \
  nasm \
  yasm

sudo mkdir -p /usr/local/src
cd /usr/local/src
sudo git clone https://gitee.com/ossrs/srs.git
cd srs
git checkout 7.0release
sudo git tag | grep "v7\|7.0"
sudo git fetch --all
sudo git branch -r | grep -E "7|develop"
sudo git checkout origin/develop
sudo git checkout origin/feature/7.0 2>/dev/null || echo "使用 develop 分支"
sudo git describe --tags --always
cd srs/trunk
sudo ./configure
sudo make -j"$(nproc)"
# 配置定义好路径, 4 “安装”到 /usr/local/srs（便于运维）
sudo mkdir -p /usr/local/srs/{bin,conf,log,run}
sudo install -m 755 ./objs/srs /usr/local/srs/bin/srs

# 设置在srs配置
sudo tee /usr/local/srs/conf/live.conf > /dev/null <<'CONF'
# ============ Global ============
listen              1935;
max_connections     10000;
daemon              off;

pid                 /usr/local/srs/run/srs.pid;
srs_log_tank        file;
srs_log_file        /usr/local/srs/log/srs.log;

# HTTP API: used by WebRTC WHIP/WHEP and other APIs.
http_api {
    enabled         on;
    listen          1985;
    crossdomain     on;
}

# HTTP server: serve HTTP-FLV and HLS files.
http_server {
    enabled         on;
    listen          8080;
    dir             /dev/shm/srs/html;
}

# WebRTC UDP port and candidate IP (public IP).
rtc_server {
    enabled         on;
    listen          8000;
    candidate       $CANDIDATE;
}

# ============ Default VHost ============
vhost __defaultVhost__ {
    enabled         on;

    # Low latency knobs (RTMP/HTTP-FLV)
    tcp_nodelay     on;
    min_latency     on;

    play {
        # For minimum latency, disable GOP cache.
        gop_cache       off;
        queue_length    10;
        mw_latency      100;
    }

    publish {
        # Disable merged-read for lower latency.
        mr              off;
    }

    # HTTP-FLV: remux RTMP to FLV over HTTP.
    http_remux {
        enabled     on;

        # Mount as /live/STREAM.flv (no vhost prefix)
        # This mount style is described in full.conf comments.
        mount       /[app]/[stream].flv;

        # When stream not ready, keep waiting instead of 404.
        hstrs       on;
    }

    # HLS: write segments to RAM (/dev/shm).
    # Low-latency HLS: smaller fragment/window but cannot be <~5s in practice.
    hls {
        enabled         on;
        hls_path        /dev/shm/srs/html;

        # Recommended low-latency baseline (about ~6-8s typical):
        hls_fragment    2;
        hls_window      10;

        hls_cleanup     on;
        hls_wait_keyframe on;
    }

    # WebRTC: convert RTMP -> RTC for ultra-low latency playback.
    rtc {
        enabled         on;
        rtmp_to_rtc     on;
        rtc_to_rtmp     off;

        # RTMP may contain B-frames; WebRTC generally doesn't like it.
        keep_bframe     off;
        bframe          discard;
    }
}
CONF

# 1 系统参数（文件句柄数，防止多人播放时崩）
echo '* soft nofile 100000' | sudo tee -a /etc/security/limits.conf
echo '* hard nofile 100000' | sudo tee -a /etc/security/limits.conf



# 建 service

sudo tee /etc/systemd/system/srs.service > /dev/null <<'SERVICE'
[Unit]
Description=SRS Media Server
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
Environment=CANDIDATE=39.108.169.242
ExecStart=/usr/local/srs/bin/srs -c /usr/local/srs/conf/live.conf
Restart=always
RestartSec=2
LimitNOFILE=100000

# 如果你未来让 SRS 直接监听 80 端口，可考虑加 CAP_NET_BIND_SERVICE；
# 现在我们用 Nginx 代理到 80/443，所以不需要。

[Install]
WantedBy=multi-user.target
SERVICE

# 启动并设置开机自启
sudo systemctl daemon-reload
sudo systemctl enable --now srs
sudo systemctl status srs --no-pager
tail -n 50 -f /usr/local/srs/log/srs.log




# 安装反向代理nginx
sudo apt install -y nginx
# 配置在反向代理
sudo tee /etc/nginx/sites-available/srs.conf > /dev/null <<'NGINX'
server {
    listen 80;
    server_name _;

    # FLV/HLS/静态页：转到 SRS HTTP 8080
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header Connection "";
        proxy_read_timeout 3600;
    }

    # WebRTC WHEP/WHIP：转到 SRS API 1985
    location /rtc/ {
        proxy_pass http://127.0.0.1:1985/rtc/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_read_timeout 60;
    }
}
NGINX

sudo ln -sf /etc/nginx/sites-available/srs.conf /etc/nginx/sites-enabled/srs.conf
sudo nginx -t && sudo systemctl restart nginx









