events {
  worker_connections 1024;
}

rtmp {
  server {
    listen 1935;

    application live {
      live on;
      record off;

      push rtmp://localhost:1935/channel-live;
      on_publish http://localhost:3000/api/rtmp/publish;

      deny play all;
    }

    application channel-live {
      live on;
      record off;

      allow publish 127.0.0.1;
      deny publish all;
      
      deny play all;

      hls on;
      hls_type live;
      hls_path /dev/shm/hls;
      hls_fragment 2s;
      hls_playlist_length 10s;
      hls_cleanup on;

      hls_fragment_naming timestamp;
      hls_fragment_slicing aligned;

      hls_variant _low BANDWIDTH=300000 RESOLUTION=480x270;
      hls_variant _mid BANDWIDTH=600000 RESOLUTION=640x360;
      hls_variant _hi BANDWIDTH=1000000 RESOLUTION=854x480;
    }
  }
}

http {
  include mime.types;
  default_type application/octet-stream;

  # performance optimizations
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;

  map $http_authorization $is_authorized {
    default 0;
    ${API_AUTH} 1;
  }

  server {
    listen 8888;

    location /stat {
      if ($is_authorized = 0) {
        return 401 "Unauthorized";
      }
      rtmp_stat all;
      rtmp_stat_format json;
    }
  }
}