@vestra-player/sdk - v1.11.3
    Preparing search index...

    Interface MediaSources

    plugin.media.sources: remembered local files. The player gives a plugin a token for the file playing now; the token survives restarts and stands for that file in MediaCapture.export, MediaCapture.exportCheck and MediaSources.open.

    Uses beyond exports: a bookmarks or history plugin keeps tokens to reopen what the user watched at a position (MediaSources.open, with Locate… when a file moved), a resume plugin follows files across renames, a compare plugin switches between two remembered files, a chapters plugin keeps its marks per file without storing paths.

    Tokens hide paths as an API convenience, not as a security boundary: while the file plays, player.state.source shows its path, and a plugin is an ordinary Node.js process, not sandboxed from the file system, that can find files by other means. What a token gives is a way to name the user's files without the plugin keeping their paths, and to find them again after a move.

    Only the player makes tokens, and only for a local file that is playing (the user opened it: the file dialog, a drop, the playlist, the command line); a stream, an address or a plugin's own picture is refused. A token belongs to the plugin that got it and means nothing to another. It gives the file's name, duration and stream facts, never its path or content. The player keeps the plugin's tokens in the profile until the plugin forgets them or is uninstalled, at most 500 per plugin. When a file has moved, MediaSources.locate lets the user find it again in the player's own file dialog.

    The plugin keeps the token in its storage and checks the file before using it. remember answers for the file playing now; get looks at the file on disk again, so a moved file reads missing.

    const store = plugin.storage.json("parts", { defaults: { tokens: [] as string[] } });
    const added = await plugin.media.sources.remember();
    store.save({ tokens: [...store.value.tokens, added.token] });
    // … after a restart:
    const again = await plugin.media.sources.get(added.token);
    if (again.status !== "ok") console.log(`${again.name} is ${again.status}`);

    1.7.8

    interface MediaSources {
        remember(options?: Conditional): Promise<SourceReference>;
        get(token: string): Promise<SourceReference>;
        locate(token: string): Promise<SourceReference | null>;
        forget(token: string): Promise<void>;
        list(): Promise<SourceReference[]>;
        open(token: string, options?: { start?: number }): Promise<void>;
    }
    Index
    • A token for the file playing now (the same token again for the same file as it is). Fails unsupported for a stream, an address or a plugin's picture, stale when the source changed since ifCurrent.

      Parameters

      Returns Promise<SourceReference>

      unsupported (rejects) The source is not a local file, or the player cannot read its streams.

      stale (rejects) The source changed since ifCurrent.

      limit (rejects) The plugin holds 500 references; forget one.

      busy (rejects) This plugin has 4 source requests or checks running; try again when one has answered.

      unavailable (rejects) No FFmpeg to read the file with.

      The range the user marked becomes a part of the plugin's list. The token stands for the file; the plugin keeps it with the times, and the name is what the list shows. There is no thumbnail API: while the file plays, the plugin captures the part's first frame (MediaCapture.capture) and exports it into its own data directory; plugin.ui.image shows it scaled down.

      const file = await plugin.media.sources.remember();
      // A new name each time: an export never overwrites a file.
      const thumbnail = `${plugin.dataDirectory}/thumb-${file.token}-${Date.now()}.jpg`;
      const snapshot = await plugin.media.capture({ at: start });
      try {
      await (await snapshot.export({ format: "jpeg", destination: thumbnail, quality: 80 })).done;
      } finally {
      await snapshot.dispose();
      }
      list.push({ token: file.token, name: file.name, start, end, thumbnail });
      // In the row: plugin.ui.image(thumbnail, { maxSide: 64 })

      1.7.8

    • The reference with its file looked at now: status says whether it is still there as it was.

      Parameters

      • token: string

      Returns Promise<SourceReference>

      missing (rejects) Not one of this plugin's tokens.

      busy (rejects) This plugin has 4 source requests or checks running; try again when one has answered.

      At start the plugin checks each remembered file once. A part whose file was moved or deleted reads missing, and its row offers Locate.

      for (const token of tokens) {
      const file = await plugin.media.sources.get(token);
      if (file.status === "missing") console.log(`${file.name}: File missing`);
      }

      1.7.8

    • Ask the user where the file is now: the player shows its own file dialog (named after the file, opened in its old folder) and, when the user picks a file, the token stands for that file from then on. Resolves null when the user cancels.

      Parameters

      • token: string

      Returns Promise<SourceReference | null>

      missing (rejects) Not one of this plugin's tokens.

      busy (rejects) Another dialog of the player is open, or this plugin showed a Locate dialog less than 2 seconds ago.

      unavailable (rejects) No window to show the dialog in.

      unsupported (rejects) The player cannot read the chosen file's streams.

      The row's Locate… button asks the user for the file. The token keeps its value, so every part that uses it works again.

      const found = await plugin.media.sources.locate(token);
      if (found) console.log(`${found.name} found again`);

      1.7.8

    • The player forgets the token (an unknown one is ignored).

      Parameters

      • token: string

      Returns Promise<void>

      When the last part from a file leaves the list, the plugin lets the player forget the file, which frees one of its 500 references.

      await plugin.media.sources.forget(token);
      

      disabled (rejects) The plugin is stopping.

      1.7.8

    • Every reference this plugin holds, each with its file looked at now.

      Returns Promise<SourceReference[]>

      busy (rejects) This plugin has 4 source requests or checks running; try again when one has answered.

      After the user cleared the list, the plugin forgets every file none of its parts names. list gives all of the plugin's tokens, including ones its own storage lost.

      for (const file of await plugin.media.sources.list())
      if (!used.has(file.token)) await plugin.media.sources.forget(file.token);

      1.7.8

    • Open the remembered file in the player and play it from start seconds (default its beginning): the user's "preview this part". It is an ordinary open, as Player.open of that file would be (the current source is replaced; the playlist treats it as any open). The plugin names the file by its token instead of keeping its path; that is a convenience, not a boundary: once the file plays, player.state.source shows its path, as for any open. Resolves once the file is loaded. A file that is gone or changed is refused before anything opens.

      Parameters

      • token: string
      • Optionaloptions: { start?: number }
        • Optionalstart?: number

          Seconds of the file to start at; default 0.

      Returns Promise<void>

      source-missing (rejects) No file is at the token's path any more; offer MediaSources.locate.

      source-changed (rejects) The file changed since it was remembered; offer MediaSources.locate.

      missing (rejects) Not one of this plugin's tokens.

      invalid (rejects) start negative, or at or after the file's end.

      replaced (rejects) Another source replaced it before it loaded.

      backend (rejects) The player could not load the file.

      Double-clicking a row of the plugin's list plays that part's file from the part's start. A file that has moved since is refused, and the row then offers Locate….

      try {
      await plugin.media.sources.open(part.token, { start: part.start });
      } catch (error) {
      // Moved or rewritten: the row shows "File missing · Locate…".
      if ((error as { code?: string }).code === "source-missing") console.log("missing");
      }

      1.7.8