{{- define "srs.config.apilist" -}}
{{- range $i, $e := int .Values.apps.srs.replicas.origin | until }} {{ $.Values.deploy.name }}-og-{{ $i }}.{{ $.Values.apps.srs.service.headless.og }}:{{ $.Values.apps.srs.ports.api }}
{{- end -}}
{{- end -}}

{{- define "srs.config.rtmplist" -}}
{{- range $i, $e := int .Values.apps.srs.replicas.origin | until }} {{ $.Values.deploy.name }}-og-{{ $i }}.{{ $.Values.apps.srs.service.headless.og }}:{{ $.Values.apps.srs.ports.rtmp }}
{{- end -}}
{{- end -}}

{{- define "srs.config.hooks" -}}
    {{ if .Values.enabled.hook }}
    http_hooks {
        enabled       on;
        on_publish    http://{{ $.Values.deploy.name }}-op-svc:8080/hooks/on_publish;
    }
    {{- end }}
{{- end -}}

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

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

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