All files / app-play/src/services recording.ts

100% Statements 18/18
50% Branches 1/2
100% Functions 11/11
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                            4x                 4x                 4x 4x 4x 4x 4x 4x         1x         3x                     10x     6x 7x   4x   2x 3x 3x       4x        
// The Options dialog's Capture folder at work: what a video recording of a play and a screenshot of the stage
// (`recording-controller.ts`) need of the app, and the state they keep between plays.
 
import type { RecordingFormat, RecordingQuality } from '@be-music/player-browser/recording';
import type { GameplayRecorder, RecordingDeps } from '../recording-controller.ts';
import { parseRecordingFormatKey, parseRecordingQuality } from '../recording-format-choice.ts';
 
export interface RecordingServiceOptions {
  /** The scene host a screenshot is taken from. */
  readonly sceneHost: RecordingDeps['sceneHost'];
  /** The Options dialog's record button (`OptionsDialog.recordController`), once the menu is built. */
  readonly recordController: () => RecordingDeps['recordController'];
  /** The recorder of the play (`PlayScene.recording`), on the stage or loading behind a decide screen. */
  readonly activeGameplay: () => GameplayRecorder | undefined;
  /** The state the Options dialog's "Video format" and "Video quality" are kept in. */
  readonly settings: { readonly recordingFormat: string; readonly recordingQuality: string };
  /** Locks the Video format and Video quality controls while a recording runs. */
  readonly setRecordingSettingsLocked?: (locked: boolean) => void;
  readonly setStatus: (text: string) => void;
}
 
/** A video recording armed on the select screen, the name the next one is saved under, and the format it is made in. */
export class RecordingService {
  /**
   * `true` after the user clicks Record on the song-select screen but before they actually pick a song. Consumed (and
   * cleared) by `startArmedRecording` as the play starts, kicking off `startRecording()` so the very first frame of the
   * chart is captured.
   *
   * A second click on Record before picking a song flips this back to `false` ("disarm").
   */
  private autoRecordArmed = false;
  /**
   * Filename base for the next saved recording. Derived from the currently-playing song's title (sanitized for
   * filesystem safety) or `gameplay-<timestamp>` when no song info is available. Updated on every `playChart` so
   * back-to-back recordings don't overwrite each other in the user's downloads folder.
   *
   * Read by `toggleRecording` (in `recording-controller.ts`) when assembling the download filename; mutated in
   * `playChart` / `preloadGameplay` as each chart starts.
   */
  private recordingFilenameBase = 'gameplay';
  private readonly sceneHost: RecordingServiceOptions['sceneHost'];
  private readonly recordController: RecordingServiceOptions['recordController'];
  private readonly activeGameplay: RecordingServiceOptions['activeGameplay'];
  private readonly settings: RecordingServiceOptions['settings'];
  private readonly setStatus: RecordingServiceOptions['setStatus'];
  private readonly setRecordingSettingsLocked: RecordingServiceOptions['setRecordingSettingsLocked'];
 
  public constructor(options: RecordingServiceOptions) {
    this.sceneHost = options.sceneHost;
    this.recordController = options.recordController;
    this.activeGameplay = options.activeGameplay;
    this.settings = options.settings;
    this.setStatus = options.setStatus;
    this.setRecordingSettingsLocked = options.setRecordingSettingsLocked;
  }
 
  /** Names the next recording after the chart about to play. */
  public setFilenameBase(base: string): void {
    this.recordingFilenameBase = base;
  }
 
  /** The container and codec the Options dialog's "Video format" asks recordings to be written in. */
  private get recordingFormat(): RecordingFormat {
    return parseRecordingFormatKey(this.settings.recordingFormat);
  }
 
  /**
   * Bundle the player's recording-related fields into the `RecordingDeps` shape that
   * `recording-controller.ts` consumes. Built as a getter-based bundle (rather than a structural copy of `this`) so
   * `RecordingDeps` can stay an interface of getter / setter functions — TypeScript otherwise refuses to satisfy a
   * structural `RecordingHost` from a class whose backing fields are `private`. Cheap to allocate per call; called at
   * most a handful of times per session (record toggle / screenshot / chart-end recording finalize).
   */
  public deps(): RecordingDeps {
    return {
      sceneHost: this.sceneHost,
      recordController: this.recordController(),
      getActiveGameplay: () => this.activeGameplay(),
      getAutoRecordArmed: () => this.autoRecordArmed,
      setAutoRecordArmed: (value) => {
        this.autoRecordArmed = value;
      },
      getRecordingFilenameBase: () => this.recordingFilenameBase,
      getRecordingFormat: () => this.recordingFormat,
      getRecordingQuality: (): RecordingQuality => parseRecordingQuality(this.settings.recordingQuality),
      ...(this.setRecordingSettingsLocked !== undefined
        ? { setRecordingSettingsLocked: this.setRecordingSettingsLocked }
        : {}),
      setStatus: (text) => this.setStatus(text),
    };
  }
}