# SRS Configuration for EasyStream Live Streaming
# Works alongside Caddy for HTTP/HTTPS while handling RTMP

listen              1935;
max_connections     1000;
srs_log_tank        file;
srs_log_level       info;
srs_log_file        ./objs/srs.log;

# HTTP API for stream management
http_api {
    enabled         on;
    listen          1985;
    crossdomain     on;
}

# HTTP server for HLS delivery
http_server {
    enabled         on;
    listen          8080;
    dir             ./objs/nginx/html;
    crossdomain     on;
}

# Statistics for monitoring
stats {
    network         0;
    disk            sda sdb xvda xvdb;
}

# RTMP server configuration
vhost __defaultVhost__ {
    # Enable HLS
    hls {
        enabled         on;
        hls_fragment    10;
        hls_window      60;
        hls_path        ./objs/nginx/html/live;
        hls_m3u8_file   [app]/[stream].m3u8;
        hls_ts_file     [app]/[stream]-[seq].ts;
    }
    
    # Enable HTTP-FLV for low latency
    http_remux {
        enabled     on;
        mount       [vhost]/[app]/[stream].flv;
    }
    
    # Recording configuration
    dvr {
        enabled         off;  # Enable per stream as needed
        dvr_path        ./objs/nginx/html/recordings/[app]/[stream]/[2006]/[01]/[02]/[stream].[timestamp].flv;
        dvr_plan        session;
        dvr_duration    30;
        dvr_wait_keyframe   on;
    }
    
    # Transcoding for multiple qualities
    transcode {
        enabled     off;  # Enable per stream as needed
        ffmpeg      ./objs/ffmpeg/bin/ffmpeg;
        
        engine hd {
            enabled         on;
            vfilter {
                v               quiet;
                vf              scale=1280:720;
            }
            vcodec          libx264;
            vbitrate        2500;
            vfps            25;
            vwidth          1280;
            vheight         720;
            vthreads        4;
            vprofile        main;
            vpreset         medium;
            vparams {
                g               50;
                keyint_min      25;
                sc_threshold    0;
            }
            acodec          aac;
            abitrate        128;
            asample_rate    44100;
            achannels       2;
            aparams {
            }
            output          rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_hd;
        }
        
        engine sd {
            enabled         on;
            vfilter {
                v               quiet;
                vf              scale=854:480;
            }
            vcodec          libx264;
            vbitrate        1000;
            vfps            25;
            vwidth          854;
            vheight         480;
            vthreads        2;
            vprofile        main;
            vpreset         medium;
            vparams {
                g               50;
                keyint_min      25;
                sc_threshold    0;
            }
            acodec          aac;
            abitrate        96;
            asample_rate    44100;
            achannels       2;
            aparams {
            }
            output          rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_sd;
        }
    }
    
    # Security and authentication
    refer {
        enabled         off;
        all             off;
        publish         off;
        play            off;
    }
    
    # Bandwidth testing
    bandcheck {
        enabled         off;
        key             35c9b402c12a7246868752e2878f7e0e;
        interval        30;
        limit_kbps      4000;
    }
    
    # Security settings
    security {
        enabled         off;
        seo {
            enabled         on;
            summary         "EasyStream Live Streaming";
        }
    }
}

# Webhook callbacks to EasyStream
http_hooks {
    enabled         on;
    on_connect      http://php:80/api/srs/webhook;
    on_close        http://php:80/api/srs/webhook;
    on_publish      http://php:80/api/srs/webhook;
    on_unpublish    http://php:80/api/srs/webhook;
    on_play         http://php:80/api/srs/webhook;
    on_stop         http://php:80/api/srs/webhook;
    on_dvr          http://php:80/api/srs/webhook;
    on_hls          http://php:80/api/srs/webhook;
}

# Heartbeat for monitoring
heartbeat {
    enabled         on;
    interval        9.9;
    url             http://php:80/api/srs/heartbeat;
    device_id       easystream-srs;
    summaries       off;
}