worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {
    
    server {
        listen 1935;  # RTMP listen port

        application live {
            live on;
            # No RTMP playback
            deny play all;
            push rtmp://127.0.0.1:1935/hls-live;

            on_publish http://web_server:5000/publish_stream;
            on_publish_done http://web_server:5000/end_stream;

        }

        application hls-live {
            live on;

            hls on;
            hls_path /stream_data/hls;

            allow publish 127.0.0.1;
            deny publish all;

            hls_nested on;
            hls_fragment 5s;
            hls_playlist_length 60s;
        }
    }
}

http {
    access_log off;
    # Enable HLS
    server {
        listen 8080;
        root /var/www;

        location /api/ {
            rewrite ^/api/(.*)$ /$1 break;
            proxy_pass http://web_server:5000;  # flask-app is the name of the Flask container in docker-compose
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
        location /socket.io/ {
            proxy_pass http://web_server:5000/socket.io/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_cache_bypass $http_upgrade;
            proxy_buffers 8 32k;
            proxy_buffer_size 64k;
            proxy_read_timeout 86400;
        }

        location /hmr/ {
            proxy_pass http://frontend:5173;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        # The MPEG-TS video chunks are stored in /tmp/hls
        location ~ ^/stream/user/(.+\.ts)$ {
            alias /stream_data/hls/$1;

            # Let the MPEG-TS video chunks be cacheable
            expires max;
        }

        # The M3U8 playlists location
        location ~ ^/stream/user/(.+\.m3u8)$ {
            alias /stream_data/hls/$1;

            # The M3U8 playlists should not be cacheable
            expires -1d;
        }

        # The thumbnails location
        location ~ ^/stream/user/thumbnails/(.+\.jpg)$ {
            alias /stream_data/thumbnails/$1;

            # The thumbnails should not be cacheable
            expires -1d;
        }

        location ~ ^/\?token=.*$ {
            proxy_pass http://frontend:5173;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        location / {
            proxy_pass http://frontend:5173;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}
