<my-admin-save-bar i18n-title title="Live configuration" (save)="save()" [form]="form" [formErrors]="formErrors" [inconsistentOptions]="checkTranscodingConsistentOptions()"></my-admin-save-bar>

<form [formGroup]="form" id="admin-config-form">

  <div class="pt-two-cols">
    <div class="title-col">
      <h2 i18n>LIVE</h2>

      <div i18n class="inner-form-description">
        Enable users of your platform to stream a live.
      </div>
    </div>

    <div class="content-col">

      <ng-container formGroupName="live">

        <div class="form-group">
          <my-peertube-checkbox inputName="liveEnabled" formControlName="enabled">
            <ng-template ptTemplate="label">
              <ng-container i18n>Allow live streaming</ng-container>
            </ng-template>

            <ng-container ngProjectAs="description">
              <div i18n>⚠️ Enabling live streaming requires trust in your users and extra moderation work</div>
              <div i18n>If enabled, your server needs to accept incoming TCP traffic on port {{ getLiveRTMPPort() }}</div>
            </ng-container>

            <ng-container ngProjectAs="extra">

              <div class="form-group" [ngClass]="getDisabledLiveClass()">
                <my-peertube-checkbox
                  inputName="liveAllowReplay" formControlName="allowReplay"
                  i18n-labelText labelText="Allow your users to automatically publish a replay of their live"
                  >
                </my-peertube-checkbox>
              </div>

              <div class="form-group" formGroupName="latencySetting" [ngClass]="getDisabledLiveClass()">
                <my-peertube-checkbox
                  inputName="liveLatencySettingEnabled" formControlName="enabled"
                  i18n-labelText labelText="Allow your users to change live latency"
                  >
                  <ng-container ngProjectAs="description" i18n>
                    Small latency disables P2P and high latency can increase P2P ratio
                  </ng-container>
                </my-peertube-checkbox>
              </div>

              <div class="form-group" [ngClass]="getDisabledLiveClass()">
                <label i18n for="liveMaxInstanceLives">Max simultaneous lives created on your platform</label>

                <span i18n class="ms-2 small muted">(-1 for "unlimited")</span>

                <div class="number-with-unit">
                  <input type="number" id="liveMaxInstanceLives" formControlName="maxInstanceLives" />
                  <span i18n>{form.value.live.maxInstanceLives, plural, =1 {live} other {lives}}</span>
                </div>

                @if (formErrors.live.maxInstanceLives) {
                  <div class="form-error" role="alert">{{ formErrors.live.maxInstanceLives }}</div>
                }
              </div>

              <div class="form-group" [ngClass]="getDisabledLiveClass()">
                <label i18n for="liveMaxUserLives">Max simultaneous lives created per user</label>
                <span i18n class="ms-2 small muted">(-1 for "unlimited")</span>

                <div class="number-with-unit">
                  <input type="number" id="liveMaxUserLives" formControlName="maxUserLives" />
                  <span i18n>{form.value.live.maxUserLives, plural, =1 {live} other {lives}}</span>
                </div>

                @if (formErrors.live.maxUserLives) {
                  <div class="form-error" role="alert">{{ formErrors.live.maxUserLives }}</div>
                }
              </div>

              <div class="form-group" [ngClass]="getDisabledLiveClass()">
                <label i18n for="liveMaxDuration">Max live duration</label>

                <my-select-options inputId="liveMaxDuration" [items]="liveMaxDurationOptions" formControlName="maxDuration"></my-select-options>

                @if (formErrors.live.maxDuration) {
                  <div class="form-error" role="alert">{{ formErrors.live.maxDuration }}</div>
                }
              </div>

            </ng-container>
          </my-peertube-checkbox>
        </div>
      </ng-container>
    </div>
  </div>

  <div class="pt-two-cols"> <!-- transcoding live streams grid -->
    <div class="title-col">
      <h2 i18n>TRANSCODING</h2>
      <div i18n class="inner-form-description">
        Same as VOD transcoding, transcoding live streams so that they are in a streamable form that any device can play. Requires a beefy CPU, and then some.
      </div>
    </div>

    <div class="content-col">

      <ng-container formGroupName="live">
        <ng-container formGroupName="transcoding">

          <div class="form-group" [ngClass]="getDisabledLiveClass()">
            <my-peertube-checkbox
              inputName="liveTranscodingEnabled" formControlName="enabled"
              >
              <ng-template ptTemplate="label">
                <ng-container i18n>Transcoding enabled for live streams</ng-container>
              </ng-template>
            </my-peertube-checkbox>
          </div>

          <div class="callout callout-light pt-2 mt-2 pb-0">
            <h3 class="callout-title" i18n>Output formats</h3>

            <div [ngClass]="getDisabledLiveTranscodingClass()">

              <div class="form-group" formGroupName="fps">
                <label i18n for="liveTranscodingFPSMax">Max live FPS</label>

                <span i18n class="ms-2 small muted">Cap transcoded live FPS. Max resolution stream still keeps the original FPS.</span>

                <div class="number-with-unit">
                  <input type="number" name="liveTranscodingFPSMax" formControlName="max" />
                  <span>FPS</span>
                </div>

                @if (formErrors.live.transcoding.fps.max) {
                  <div class="form-error" role="alert">{{ formErrors.live.transcoding.fps.max }}</div>
                }
              </div>

              <div class="ms-2 mt-3">
                <h4 i18n>Live resolutions to generate</h4>

                <div class="mt-3">

                  <ng-container formGroupName="resolutions">
                    @for (resolution of liveResolutions; track resolution) {
                      <div class="form-group mb-2">
                        <my-peertube-checkbox
                          [inputName]="getResolutionKey(resolution.id)" [formControlName]="resolution.id"
                          labelText="{{resolution.label}}"
                          >
                          @if (resolution.description) {
                            <ng-template ptTemplate="help">
                              <div [innerHTML]="resolution.description"></div>
                            </ng-template>
                          }
                        </my-peertube-checkbox>
                      </div>
                    }
                  </ng-container>

                  <div class="form-group">
                    <my-peertube-checkbox
                      inputName="liveTranscodingAlwaysTranscodeOriginalResolution" formControlName="alwaysTranscodeOriginalResolution"
                      i18n-labelText labelText="Also transcode original resolution"
                      >
                      <ng-container i18n ngProjectAs="description">
                        Even if it's above your maximum enabled resolution
                      </ng-container>
                    </my-peertube-checkbox>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div class="form-group mt-4" formGroupName="remoteRunners" [ngClass]="getDisabledLiveTranscodingClass()">
            <my-peertube-checkbox
              inputName="liveTranscodingRemoteRunnersEnabled" formControlName="enabled"
              i18n-labelText labelText="Enable remote runners for lives"
              >
              <ng-container ngProjectAs="description">
                <div i18n>Use <a routerLink="/admin/settings/system/runners/runners-list">remote runners</a> to process live transcoding.</div>
                <div i18n>Remote runners have to register on your instance first.</div>
              </ng-container>
            </my-peertube-checkbox>
          </div>

          <div class="form-group" [ngClass]="getDisabledLiveLocalTranscodingClass()">
            <label i18n id="liveTranscodingThreadsLabel" for="liveTranscodingThreads">Live transcoding threads</label>

            <span class="small muted ms-1">
              @if (getTotalTranscodingThreads().atMost) {
                <ng-container i18n>
                will claim at most {{ getTotalTranscodingThreads().value }} {{ getTotalTranscodingThreads().unit }} with VOD transcoding
              </ng-container>
              }

              @if (!getTotalTranscodingThreads().atMost) {
                <ng-container i18n>
                will claim at least {{ getTotalTranscodingThreads().value }} {{ getTotalTranscodingThreads().unit }} with VOD transcoding
              </ng-container>
              }
            </span>

            <my-select-custom-value
              labelId="liveTranscodingThreadsLabel"
              inputId="liveTranscodingThreads"
              [items]="transcodingThreadOptions"
              formControlName="threads"
              [clearable]="false"
            ></my-select-custom-value>
            @if (formErrors.live.transcoding.threads) {
              <div class="form-error" role="alert">{{ formErrors.live.transcoding.threads }}</div>
            }
          </div>

          <div class="form-group mt-4" [ngClass]="getDisabledLiveLocalTranscodingClass()">
            <label i18n for="liveTranscodingProfile">Live transcoding profile</label>
            <span class="small muted ms-1" i18n>new live transcoding profiles can be added by PeerTube plugins</span>

            <my-select-options inputId="liveTranscodingProfile" formControlName="profile" [items]="transcodingProfiles"></my-select-options>

            @if (formErrors.live.transcoding.profile) {
              <div class="form-error" role="alert">{{ formErrors.live.transcoding.profile }}</div>
            }
          </div>

        </ng-container>
      </ng-container>

    </div>
  </div>
</form>
