kind: ConfigMap
apiVersion: v1
metadata:
  namespace: jitsi
  name: jvb-base
data:
  # custom footer that references Schul Cloud imprint and data privacy protection
  jvb.conf: |
    {{ $JVB_TCP_PORT := .Env.JVB_TCP_PORT | default "4443" }}
    {{ $JVB_TCP_MAPPED_PORT := .Env.JVB_TCP_MAPPED_PORT | default $JVB_TCP_PORT }}
    {{ $PUBLIC_URL_DOMAIN := .Env.PUBLIC_URL | default "https://localhost:8443" | trimPrefix "https://" | trimSuffix "/" -}}
    {{ $WS_DOMAIN := .Env.JVB_WS_DOMAIN | default $PUBLIC_URL_DOMAIN -}}
    {{ $ENABLE_JVB_WEBSOCKET := .Env.ENABLE_JVB_WEBSOCKET | default "1" | toBool }}
    {{ $ENABLE_OCTO := .Env.TESTING_OCTO_PROBABILITY | default "0" | toBool }}

    videobridge {
        ice {
            udp {
                port = {{ .Env.JVB_PORT }}
            }
            {{ if not .Env.JVB_TCP_HARVESTER_DISABLED }}
            tcp {
                enabled = {{ not .Env.JVB_TCP_HARVESTER_DISABLED }}
                port = {{ .Env.JVB_TCP_PORT }}
                {{ if not (eq $JVB_TCP_PORT $JVB_TCP_MAPPED_PORT) }}
                mapped-port = {{ $JVB_TCP_MAPPED_PORT }}
                {{ end }}
            }
            {{ end }}
        }
        {{ if .Env.JVB_CC_ENABLE | default "false" | toBool -}}
        cc {
            {{ if .Env.JVB_BWE_CHANGE_THRESHOLD }}
            # def 0.15
            bwe-change-threshold={{ .Env.JVB_BWE_CHANGE_THRESHOLD }}
            {{ end }}
            {{ if .Env.JVB_THUMBNAIL_MAX_HEIGHT }}
            # def 180
            thumbnail-max-height-px={{ .Env.JVB_THUMBNAIL_MAX_HEIGHT }}
            {{ end }}
            {{ if .Env.JVB_ONSTAGE_IDEAL_HEIGHT }}
            # def 1080
            onstage-ideal-height-px={{ .Env.JVB_ONSTAGE_IDEAL_HEIGHT }}
            {{ end }}
            {{ if .Env.JVB_ONSTAGE_PREF_HEIGHT }}
            # def 360
            onstage-preferred-height-px={{ .Env.JVB_ONSTAGE_PREF_HEIGHT }}
            {{ end }}
            {{ if .Env.JVB_ONSTAGE_PREF_FRAMERATE }}
            # 30
            onstage-preferred-framerate={{ .Env.JVB_ONSTAGE_PREF_FRAMERATE }}
            {{ end }}
            {{ if .Env.JVB_ALLOW_OVERSEND_ONSTAGE }}
            # def true
            allow-oversend-onstage={{ .Env.JVB_ALLOW_OVERSEND_ONSTAGE }}
            {{ end }}
            {{ if .Env.JVB_TRUST_BWE }}
            # def true
            trust-bwe={{ .Env.JVB_TRUST_BWE }}
            {{ end }}
            {{ if .Env.JVB_CC_PADDING_PERIOD }}
            # How often we check to send probing data. def 15ms
            padding-period={{ .Env.JVB_CC_PADDING_PERIOD }}
            {{ end }}
            {{ if .Env.JVB_FORWARD_STEAM_RECALCULATION }}
            # How often we'll force recalculations of forwarded
            # streams. def 15 seconds 
            max-time-between-calculations = {{ .Env.JVB_FORWARD_STEAM_RECALCULATION }}
            {{ end }}
            {{ if .Env.JVB_LAST_N }}
            # A JVB-wide last-n value, observed by all endpoints.  Endpoints
            # will take the minimum of their setting and this one (-1 implies
            # no last-n limit)
            jvb-last-n = {{ .Env.JVB_LAST_N }}
            {{ end }}
        }
        {{ end }}

        apis {
            xmpp-client {
                configs {
                    shard {
                        HOSTNAME = "{{ .Env.XMPP_SERVER }}"
                        DOMAIN = "{{ .Env.XMPP_AUTH_DOMAIN }}"
                        USERNAME = "{{ .Env.JVB_AUTH_USER }}"
                        PASSWORD = "{{ .Env.JVB_AUTH_PASSWORD }}"
                        MUC_JIDS = "{{ .Env.JVB_BREWERY_MUC }}@{{ .Env.XMPP_INTERNAL_MUC_DOMAIN }}"
                        MUC_NICKNAME = "{{ .Env.OCTO_REGION }}-{{ .Env.HOSTNAME }}"
                        DISABLE_CERTIFICATE_VERIFICATION = true
                    }
                }
            }

            # The COLIBRI REST API
            {{ if .Env.JVB_COLIBRI_API_ENABLE | default "false" | toBool -}}
            rest {
                enabled = {{ .Env.JVB_COLIBRI_API_ENABLE }}
            }
            {{ end }}
            {{ if .Env.JVB_API_ENABLE | default "false" | toBool -}}
            jvb-api {
                enabled = {{ .Env.JVB_API_ENABLE }}
            }
            {{ end }}
        }

        {{ if .Env.JVB_REST_API_ENABLE }}
        # Configuration of the different REST APIs.
        # Note that the COLIBRI REST API is configured under videobridge.apis.rest instead.
        rest {
            {{ if .Env.JVB_REST_DEBUG_ENABLE }}
            debug {
            enabled = {{ .Env.JVB_REST_DEBUG_ENABLE }}
            }
            {{ end }}
            {{ if .Env.JVB_REST_HEALTH_ENABLE }}
            health {
            enabled = {{ .Env.JVB_REST_HEALTH_ENABLE }}
            }
            {{ end }}

            {{ if and .Env.JVB_REST_SHUTDOWN_ENABLE .Env.JVB_COLIBRI_API_ENABLE }}
            shutdown {
            # Note that the shutdown API requires the COLIBRI API to also be enabled.
            enabled = {{ .Env.JVB_REST_SHUTDOWN_ENABLE }}
            }
            {{ end }}
        }
        {{ end }}

        {{ if .Env.JVB_LOAD_MGMT_ENABLE | default "false" | toBool -}}
        load-management {
            {{ if .Env.JVB_LOAD_MGMT_REDUCER }}
            # Whether or not the reducer will be enabled to take actions to mitigate load
            reducer-enabled = {{ .Env.JVB_LOAD_MGMT_REDUCER_ENABLE }}
            {{ end }}

            {{ if or .Env.JVB_PKT_RATE_LOAD_THRESHOLD .Env.JVB_PKT_RATE_RCVRY_THRESHOLD }}
            load-measurements {
              packet-rate {
                {{ if .Env.JVB_PKT_RATE_LOAD_THRESHOLD }}
                # The packet rate at which we'll consider the bridge overloaded. default 50K
                load-threshold = {{ .Env.JVB_PKT_RATE_LOAD_THRESHOLD }}
                {{ end }}
                {{ if .Env.JVB_PKT_RATE_RCVRY_THRESHOLD }}
                # The packet rate at which we'll consider the bridge 'underloaded' enough
                # to start recovery. default 40K
                recovery-threshold = {{ .Env.JVB_PKT_RATE_RCVRY_THRESHOLD }}
                {{ end }}
              }
            }
            {{ end }}

            {{ if .Env.JVB_LOAD_REDUCER_ENABLE | default "false" | toBool -}}
            load-reducers {
                last-n {
                    {{ if .Env.JVB_LAST_N_REDUCTION_SCALE }}
                    # The factor by which we'll reduce the current last-n when trying to reduce load. def 0.75
                    reduction-scale = {{ .Env.JVB_LAST_N_REDUCTION_SCALE }}
                    {{ end }}

                    {{ if .Env.JVB_LAST_N_RECOVER_SCALE }}
                    # The factor by which we'll increase the current last-n when trying to recover. def 1.25
                    recover-scale = {{ .Env.JVB_LAST_N_RECOVER_SCALE }}
                    {{ end }}

                    {{ if .Env.JVB_LAST_N_IMPACT_TIME }}
                    # The minimum time in between runs of the last-n reducer to reduce or recover from
                    # load. def 1 min
                    impact-time = {{ .Env.JVB_LAST_N_IMPACT_TIME }}
                    {{ end }}

                    {{ if .Env.JVB_MIN_LAST_N_VAL }}
                    # The lowest value we'll set for last-n. def 1
                    minimum-last-n-value = {{ .Env.JVB_MIN_LAST_N_VAL }}
                    {{ end }}

                    {{ if .Env.JVB_MAX_ENFORCED_LAST_N_VAL }}
                    # The highest last-n value we'll enforce.  Once the enforced last-n exceeds this value
                    # we'll remove the limit entirely. def 40
                    maximum-enforced-last-n-value = {{ .Env.JVB_MAX_ENFORCED_LAST_N_VAL }}
                    {{ end }}
                }
            }
            {{ end }}
        }
        {{ end }}
        stats {
            enabled = true
        }
        {{ if $ENABLE_JVB_WEBSOCKET }}
        websockets {
            enabled = true
            domain = "{{ $WS_DOMAIN }}"
            tls = true
            server-id = "{{ .Env.JVB_PUBLIC_IP }}:{{ .Env.JETTY_PORT }}"
        }
        {{ end }}

        sctp {
            # Whether SCTP data channels are enabled.
            enabled=true
        }
        

        http-servers {
            private { 
              host = 0.0.0.0
            }
            public {
                host = 0.0.0.0
                port = "{{ .Env.JETTY_PORT }}"
            }
        }
        {{ if $ENABLE_OCTO }}
        octo {
            enabled = true
            bind-address = "{{ .Env.JVB_NAT_HARVESTER_LOCAL_ADDRESS | default "0.0.0.0" }}"
            public-address = "{{ .Env.JVB_PUBLIC_IP }}"
            bind-port = "{{ .Env.OCTO_BIND_PORT | default "4096" }}"
            region = "{{ .Env.OCTO_REGION | default "Mumbai" }}"
        }
        {{ end }}
    }

    ice4j {
        harvest {
            mapping {
                stun {
                    {{ if .Env.JVB_STUN_SERVERS }}
                    addresses = [ "{{ join "\",\"" (splitList "," .Env.JVB_STUN_SERVERS) }}" ]
                    {{ end }}
                }
            }
        }
    }
    
  sip-communicator.properties: |
    org.ice4j.ice.harvest.NAT_HARVESTER_LOCAL_ADDRESS={{ .Env.LOCAL_ADDRESS }}
    org.ice4j.ice.harvest.NAT_HARVESTER_PUBLIC_ADDRESS={{ .Env.DOCKER_HOST_ADDRESS }}
    
  10-config: |
    #!/usr/bin/with-contenv bash

    if [[ -z $JVB_AUTH_PASSWORD ]]; then
        echo 'FATAL ERROR: JVB auth password must be set'
        exit 1
    fi

    OLD_JVB_AUTH_PASSWORD=passw0rd
    if [[ "$JVB_AUTH_PASSWORD" == "$OLD_JVB_AUTH_PASSWORD" ]]; then
        echo 'FATAL ERROR: JVB auth password must be changed, check the README'
        exit 1
    fi

        echo "SETTING JVB_PORT..."
        cp /defaults/sip-communicator.properties /config/default-sip-communicator.properties
        echo "HOST NAME:" "$HOSTNAME"
        IFS='-' read -ra ADDR <<< "$HOSTNAME"
        let jvb_port=$((31000+${ADDR[1]}))
        let octo_port=$((30960+${ADDR[1]}))
        let jetty_port=$((30900+${ADDR[1]}))
        export OCTO_BIND_PORT="$octo_port"
        export JVB_PORT="$jvb_port"
        export JVB_PUBLIC_IP=`(curl ifconfig.me)`
        export JETTY_PORT="$jetty_port"

      if [[ ! -f /config/sip-communicator.properties ]]; then  
        
        sed -i  "s/NAT_HARVESTER_LOCAL_ADDRESS={{ .Env.LOCAL_ADDRESS }}/NAT_HARVESTER_LOCAL_ADDRESS=$JVB_PUBLIC_IP/" /config/default-sip-communicator.properties
        tpl /config/default-sip-communicator.properties > /config/sip-communicator.properties
      fi

    if [[ ! -f /config/logging.properties ]]; then
        cp /defaults/logging.properties /config
    fi
  
    if [[ ! -f /config/jvb.conf ]]; then
        tpl /defaults/jvb.conf > /config/jvb.conf
    fi
  config: |
    # Jitsi Videobridge settings
    # sets the XMPP domain (default: none) << update this >> use your xmpp_server
    JVB_HOSTNAME=  

    # sets the hostname of the XMPP server (default: domain if set, localhost otherwise)
    JVB_HOST=

    # sets the port of the XMPP server (default: 5275)
    JVB_PORT=5347

    # sets the shared secret used to authenticate to the XMPP server
    JVB_SECRET=bSp2WL0R

    # extra options to pass to the JVB daemon
    JVB_OPTS="--apis=,"


    # adds java system props that are passed to jvb (default are for home and logging config file)
    JAVA_SYS_PROPS="-Dconfig.file=/etc/jitsi/videobridge/jvb.conf -Dnet.java.sip.communicator.SC_HOME_DIR_LOCATION=/etc/jitsi -Dnet.java.sip.communicator.SC_HOME_DIR_NAME=videobridge -Dnet.java.sip.communicator.SC_LOG_DIR_LOCATION=/var/log/jitsi -Djava.util.logging.config.file=/etc/jitsi/videobridge/logging.properties"
