{{- define "srs-cluster.config.apilist" -}}
{{- range $i, $e := int .Values.srs.replicas.origin | until }} helm-srs-og-{{ $i }}.srsog:{{ $.Values.srs.ports.cluster.api }}
{{- end -}}
{{- end -}}

{{- define "srs-cluster.config.rtmplist" -}}
{{- range $i, $e := int .Values.srs.replicas.origin | until }} helm-srs-og-{{ $i }}.srsog:{{ $.Values.srs.ports.cluster.rtmp }}
{{- end -}}
{{- end -}}

{{- define "srs-cluster.config.hooks" -}}
    http_hooks {
        enabled     on;
        on_publish      {{ .Values.srs.hooks.on_publish }};
        on_unpublish    {{ .Values.srs.hooks.on_unpublish }};
        on_play         {{ .Values.srs.hooks.on_play }};
    }
{{- end -}}

{{- define "srs-cluster.config.origin" -}}
listen              {{ .Values.srs.ports.cluster.rtmp }};
max_connections     4096;
srs_log_tank        console;
srs_log_file        /home/srs.log;
daemon              off;
http_api {
    enabled         on;
    listen          {{ .Values.srs.ports.cluster.api }};
    crossdomain     on;
    raw_api {
        enabled             on;
        allow_reload        on;
        allow_query         on;
        allow_update        on;
    }
}
stats {
    network         0;
}
{{- if lt .Values.srs.replicas.origin 2.0 }}
rtc_server {
    enabled         on;
    listen          {{ .Values.srs.ports.cluster.realPublicWebRTC }};
    candidate       $CANDIDATE;
}
{{- end }}
vhost __defaultVhost__ {
    cluster {
        origin_cluster  on;
        coworkers      {{ include "srs-cluster.config.apilist" . }};
    }
    http_hooks {
        enabled     off;
    }
{{- if lt .Values.srs.replicas.origin 2.0 }}
    rtc {
        enabled     on;
        bframe      discard;
    }
{{- end }}
    tcp_nodelay     on;
    min_latency     on;
    play {
        gop_cache       off;
        mw_latency      0;
        mw_msgs         0;
    }
    publish {
        mr off;
    }
    dvr {
        enabled      on;
        dvr_path     /home/srs/dvr/[app]/[2006][01]/[stream]/[timestamp].mp4;
        dvr_plan     session;
    }
    hls {
        enabled         on;
        hls_path        /home/srs/hls/default;
        hls_fragment    10;
        hls_window      60;
        hls_m3u8_file   [stream]/[app].m3u8;
        hls_ts_file     [stream]/[app]-[timestamp]-[duration].ts;
        hls_cleanup     off;
    }
}
{{- end -}}

{{- define "srs-cluster.config.edge" -}}
listen              {{ .Values.srs.ports.cluster.rtmp }};
max_connections     4096;
srs_log_tank        console;
srs_log_file        /home/srs.log;
daemon              off;
http_api {
    enabled         on;
    listen          {{ .Values.srs.ports.cluster.api }};
    crossdomain     on;
    raw_api {
        enabled             on;
        allow_reload        on;
        allow_query         on;
        allow_update        on;
    }
}
http_server {
    enabled         on;
    listen          {{ .Values.srs.ports.cluster.flv }};
    dir             /home/srs/;
}
stats {
    network         0;
}
vhost __defaultVhost__ {
    cluster {
        mode            remote;
        origin         {{ include "srs-cluster.config.rtmplist" . }};
    }
    {{ if .Values.srs.config.enableHooks }}
    {{- include "srs-cluster.config.hooks" . }}
    {{- end }}
    http_remux {
        enabled     on;
    }
    tcp_nodelay     on;
    min_latency     on;
    play {
        gop_cache       off;
        mw_latency      0;
        mw_msgs         0;
    }
    publish {
        mr off;
    }
}
{{- end -}}

{{- define "srs-cluster.config.rtc" -}}
listen              {{ .Values.srs.ports.rtc.rtmp }};
max_connections     4096;
srs_log_tank        console;
srs_log_file        /home/srs.log;
daemon              on;
http_api {
    enabled         on;
    listen          {{ .Values.srs.ports.rtc.api }};
    crossdomain     on;
}
stats {
    network         0;
}
http_server {
    enabled         on;
    listen          {{ .Values.srs.ports.rtc.flv }};
    dir             /home/srs/;
}
rtc_server {
    enabled         on;
    listen          {{ .Values.srs.ports.rtc.webrtc }};
    candidate       $CANDIDATE;
}
vhost __defaultVhost__ {
    http_hooks {
        enabled     off;
    }
    http_remux {
        enabled     on;
    }
    rtc {
        enabled     on;
        bframe      discard;
    }
    tcp_nodelay     on;
    min_latency     on;
    play {
        gop_cache       off;
        mw_latency      0;
        mw_msgs         0;
    }
    publish {
        mr off;
    }
    dvr {
        enabled      on;
        dvr_path     /home/srs/dvr/[app]/[2006][01]/[stream]/[timestamp].mp4;
        dvr_plan     session;
    }
    hls {
        enabled         on;
        hls_path        /home/srs/hls/default;
        hls_fragment    10;
        hls_window      60;
        hls_m3u8_file   [app]/[stream].m3u8;
        hls_ts_file     [app]/[2006][01]/[stream]/[timestamp]-[duration].ts;
        hls_cleanup     off;
    }
}
{{- end -}}

{{- define "srs-cluster.config.nginx" -}}
user root;
worker_processes 1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections 65535;
    multi_accept on;
}

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    server_tokens off;

    sendfile            on;
    #tcp_nopush          on;

    tcp_nodelay         on;
    keepalive_timeout   120;

    server {
        listen {{ .Values.srs.ports.nginx.hls }};
        server_name _;
        location / {
            root /home/srs/hls/default;
            autoindex on;
            autoindex_exact_size off;
        }
    }
}
{{- end -}}