YT Live Streamer — Player Website
Installation Guide
==================

OPTION A — Same machine as the streamer (easiest)
──────────────────────────────────────────────────
Run everything on the same Ubuntu PC you use for streaming.
Your player will be accessible on your local network.
To make it public, forward ports on your router (see Section 3).

OPTION B — Separate Ubuntu server or VPS
─────────────────────────────────────────
Run the player on a VPS so it stays online even when your PC is off.
Viewers can always see the "stream is offline" page.


══════════════════════════════════════════════════
SECTION 1 — AUTOMATED SETUP (recommended)
══════════════════════════════════════════════════

Step 1: Copy the files to your server

  If running on your own machine, open a terminal in this folder.
  If using a VPS, upload the files first:

    scp stream_player.html chat_server.py setup_stream_server.sh \
        user@YOUR_SERVER_IP:~/

Step 2: Run the setup script

    chmod +x setup_stream_server.sh
    sudo bash setup_stream_server.sh

  The script installs and configures:
    - nginx with RTMP module
    - Python WebSocket chat server
    - systemd service (chat server starts on boot)
    - UFW firewall rules

  When done it prints your RTMP URL and player URL.

Step 3: Test it

  Open a browser and go to:
    http://YOUR_SERVER_IP/

  You should see the "Stream is offline" placeholder page.


══════════════════════════════════════════════════
SECTION 2 — MANUAL SETUP (if the script fails)
══════════════════════════════════════════════════

Step 1: Install packages

    sudo apt update
    sudo apt install -y nginx libnginx-mod-rtmp python3-pip
    pip3 install websockets

Step 2: Configure nginx

  Create /etc/nginx/nginx.conf with this content
  (or add to your existing config):

    rtmp {
        server {
            listen 1935;
            chunk_size 4096;
            application live {
                live on;
                record off;
                hls on;
                hls_path /var/www/stream/hls;
                hls_fragment 2s;
                hls_playlist_length 10s;
                hls_nested on;
            }
        }
    }

  Add to the http { } block:

    types {
        application/vnd.apple.mpegurl  m3u8;
        video/mp2t                     ts;
    }

    server {
        listen 80;
        location / {
            root /var/www/stream/html;
            index index.html;
        }
        location /hls {
            root /var/www/stream;
            add_header Cache-Control no-cache;
            add_header Access-Control-Allow-Origin *;
        }
    }

Step 3: Create folders and copy files

    sudo mkdir -p /var/www/stream/hls
    sudo mkdir -p /var/www/stream/html
    sudo chmod 777 /var/www/stream/hls
    sudo cp stream_player.html /var/www/stream/html/index.html
    sudo cp chat_server.py /opt/chat_server.py

Step 4: Start nginx

    sudo nginx -t
    sudo systemctl restart nginx

Step 5: Run the chat server

  For testing (foreground):
    python3 /opt/chat_server.py

  As a background service (permanent):
    Create /etc/systemd/system/stream-chat.service:

      [Unit]
      Description=Stream Chat WebSocket Server
      After=network.target

      [Service]
      Type=simple
      User=www-data
      ExecStart=/usr/bin/python3 /opt/chat_server.py
      Restart=always
      RestartSec=3

      [Install]
      WantedBy=multi-user.target

    Then:
      sudo systemctl daemon-reload
      sudo systemctl enable stream-chat
      sudo systemctl start stream-chat


══════════════════════════════════════════════════
SECTION 3 — PUBLIC ACCESS (port forwarding)
══════════════════════════════════════════════════

If running on your home machine and want public viewers:

Step 1: Find your machine's local IP

    hostname -I | awk '{print $1}'
    (e.g. 192.168.1.50)

Step 2: Log into your router admin page
  Usually: http://192.168.0.1 or http://192.168.1.1

Step 3: Add port forwarding rules

  Port 80   TCP   → YOUR_LOCAL_IP   (web player)
  Port 1935 TCP   → YOUR_LOCAL_IP   (RTMP ingest)
  Port 8765 TCP   → YOUR_LOCAL_IP   (chat WebSocket)

Step 4: Find your public IP

    curl ifconfig.me

  Your player will be at: http://YOUR_PUBLIC_IP/


══════════════════════════════════════════════════
SECTION 4 — TAILSCALE (no port forwarding needed)
══════════════════════════════════════════════════

Tailscale gives you a private VPN so you can access your player
from anywhere without touching your router. Free for personal use.

Install:
    curl -fsSL https://tailscale.com/install.sh | sh
    sudo tailscale up

Get your Tailscale IP:
    tailscale ip

Your player is at: http://TAILSCALE_IP/
Share your Tailscale IP with whoever you want to watch.


══════════════════════════════════════════════════
SECTION 5 — MULTI-RTMP PLUGIN SETUP
══════════════════════════════════════════════════

The multi_rtmp.py plugin sends your stream to YouTube AND your
server at the same time.

Step 1: Copy the plugin

    cp ../streamer_plugins/multi_rtmp.py ~/streamer_plugins/

Step 2: Restart the streamer

Step 3: Open the Multi-RTMP tab

Step 4: Enable the plugin (checkbox top right)

Step 5: Click "YouTube" quick add
  Enter your YouTube stream key

Step 6: Click "Self-hosted" quick add
  URL:  rtmp://YOUR_SERVER_IP/live/
  Key:  stream   (or anything you like)

Step 7: Click Save

Now when you press Start Stream it goes to both places.


══════════════════════════════════════════════════
SECTION 6 — ADDING HTTPS (optional but recommended)
══════════════════════════════════════════════════

HTTPS is needed if your main website uses HTTPS (mixed content rules).
You need a domain name pointing to your server for this.

Step 1: Point a domain or subdomain to your server IP
  Add an A record: stream.yourdomain.com → YOUR_SERVER_IP

Step 2: Get a free SSL certificate

    sudo apt install certbot python3-certbot-nginx -y
    sudo certbot --nginx -d stream.yourdomain.com

Step 3: Update stream_player.html CONFIG section

    hlsUrl:    "https://stream.yourdomain.com/hls/stream/index.m3u8",
    chatWsUrl: "wss://stream.yourdomain.com/chat-ws",

Step 4: Add WebSocket proxy to nginx server block

    location /chat-ws {
        proxy_pass http://localhost:8765;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }

Step 5: Reload nginx

    sudo systemctl reload nginx


══════════════════════════════════════════════════
SECTION 7 — TROUBLESHOOTING
══════════════════════════════════════════════════

Player page not loading
  → Check nginx is running: sudo systemctl status nginx
  → Check index.html is in /var/www/stream/html/
  → Check firewall: sudo ufw status

Stream not appearing in player
  → Check RTMP is reaching the server:
    sudo tail -f /var/log/nginx/error.log
  → Check HLS files are being created:
    ls /var/www/stream/hls/
  → Make sure you're pushing to the correct URL:
    rtmp://YOUR_SERVER_IP/live/stream

Chat not connecting
  → Check chat server is running:
    sudo systemctl status stream-chat
  → Check port 8765 is open:
    sudo ufw status
  → Check browser console for WebSocket errors

HLS files permission error in nginx log
  → sudo chmod 777 /var/www/stream/hls

Stream plays but chat shows "Disconnected"
  → The chat server may have crashed. Restart it:
    sudo systemctl restart stream-chat

Viewer count not showing
  → Normal — viewer count requires the chat WebSocket to be connected
