Introducing a new way to review your PRs.

Read the announcement

huynhsontung / screenbox

LibVLC-based media player for the Universal Windows Platform

View on GitHub

19 documents · Updated Nov 23, 2026

docs / library-indexing-and-files

Library indexing and file access

The library system turns local media folders into browsable music and video collections. It watches the selected storage libraries, reads file metadata, keeps a local cache, and exposes stable media, album, artist, and genre objects to the browsing pages. The file service supports the related actions that need direct access to files, such as picking files, moving between nearby media, and opening a file's location.

Library state

Library context

The Screenbox.Core/…/LibraryContext.cs context is the shared live state for library access and results. It holds the Windows music and video storage libraries, loading flags, and the current MusicLibrary and VideosLibrary snapshots. Pages observe replacement of these snapshots rather than managing the scan themselves.

MusicLibrary groups songs with their albums, artists, genres, and fallback groups. VideosLibrary provides the current video list. An empty snapshot is available before the first scan, so consumers can use the same shape during startup and refresh.

References: Screenbox.Core/…/LibraryContext.cs• L7-30 Screenbox.Core/…/MusicLibrary.cs• L6-41 Screenbox.Core/…/VideosLibrary.cs• L6-19

Library settings

The Screenbox.Core/…/LibrarySettingsContext.cs settings define how the library is found and shown. The main choices are whether queries use the Windows indexer, whether removable storage is searched on Xbox, and how songs and albums are sorted.

These settings affect more than presentation. A change to indexer use or removable-storage search causes the coordinator to rebuild its watchers so later results match the new scope.

References: Screenbox.Core/…/LibrarySettingsContext.cs• L6-39 Screenbox.Core/…/LibraryCoordinator.cs• L70-102

Library indexing

Library coordinator

The Screenbox.Core/…/LibraryCoordinator.cs coordinator connects storage events to library snapshots. It creates watchers for the music and video libraries, starts scans through the library service, reports progress into the shared context, and cancels an older scan when a newer one starts.

The coordinator debounces file-change events before a refresh. This groups several storage changes into one scan and prevents a burst of file events from producing a burst of UI updates. On Xbox, a separate device watcher can trigger both library scans when removable storage changes.

The refresh flow moves through these stages:

  1. Resolve the system music or video library and check access.
  2. Create a query with the chosen indexer mode and supported media types.
  3. Fetch from the cache when it is valid; otherwise read storage files in batches.
  4. Publish partial results while a full scan continues.
  5. Replace the library snapshot and save the current result for the next scan.

This diagram shows how storage changes become new browsing state.

flowchart LR
    A[Library settings or storage change] --> B[Library coordinator]
    B --> C[Storage query watcher]
    C --> D[Library service]
    D --> E[Cache or file scan]
    E --> F[Metadata and view models]
    F --> G[Library context snapshot]
    G --> H[Music and video pages]

References: Screenbox.Core/…/LibraryCoordinator.cs• L19-42 Screenbox.Core/…/LibraryCoordinator.cs• L85-168 Screenbox.Core/…/LibraryCoordinator.cs• L181-310

Library queries

The Screenbox.Core/…/LibraryService.cs queries define the boundary of indexed media. Music and video queries use separate supported-format sets and sort rules. They can use the Windows indexer when enabled or scan without it when the setting requires direct enumeration.

The service also checks the user's known-folder access before attaching a query. Removable storage is an optional second source on Xbox. It is searched only when the setting is enabled and the required access is available.

References: Screenbox.Core/…/LibraryService.cs• L50-75 Screenbox.Core/…/LibraryCoordinator.cs• L205-257 Screenbox.Core/…/LibraryService.cs• L728-747 Screenbox.Core/…/FilesHelpers.cs• L9-23

Library cache

The library cache is a saved description of the last known media set. It stores the library folder paths and the metadata needed to rebuild media objects without reading every file again. The service compares the saved folder scope with the current scope and applies storage change records when possible.

When the cache is not safe to use, the service performs a new scan. It then replaces the saved records for that media type. A lost change history, an unknown change state, or a folder-scope change therefore causes a full rebuild instead of risking a stale collection.

References: Screenbox.Core/…/LibraryService.cs• L121-214 Screenbox.Core/…/LibraryService.cs• L228-305 Screenbox.Core/…/LibraryService.cs• L626-726 Screenbox.Core/…/DatabaseService.LibraryCache.cs• L13-47

File access

File service

The Screenbox.Core/…/FilesService.cs service provides file operations that sit beside library indexing. It finds supported media in a folder, resolves the next or previous supported file, opens a file's location, and presents file and folder pickers. It also adds file-backed media to the system recent list and resolves a frame-capture folder.

The service keeps these operations separate from the library scan. A picked file or a file opened from another flow can be used without adding it to the indexed library. File support is based on the shared audio, video, playlist, and subtitle format rules.

References: Screenbox.Core/…/FilesService.cs• L19-81 Screenbox.Core/…/FilesService.cs• L83-161 Screenbox.Core/…/FilesService.cs• L163-213 Screenbox.Core/…/FilesHelpers.cs• L11-35

File media source

The Screenbox.Core/…/FileMediaSource.cs source gives a media item a stable file path and media type. It can hold a resolved Windows StorageFile or resolve one later from the path. This allows cached library records to create usable media objects before the current storage handle is available.

When a file is renamed or a change event provides a new storage object, the source updates its cached file handle and display name while keeping the media item's location as the identity used by the library.

References: Screenbox.Core/…/IFileMediaSource.cs• L6-35 Screenbox.Core/…/FileMediaSource.cs• L10-61 Screenbox.Core/…/MediaViewModelFactory.cs• L75-121

Metadata extraction

Storage metadata

The Screenbox.Core/…/StorageMetadataService.cs service reads metadata from Windows file properties for file-backed media. It supplies common file data plus music or video properties, and it returns a usable media-info object when a file is unavailable or a known transient storage error occurs.

The library service loads these details for each file as it scans. A cancellation request stops work for the current scan, while a missing property does not prevent the rest of the library from being shown.

References: Screenbox.Core/…/ISourceMetadataService.cs• L8-19 Screenbox.Core/…/StorageMetadataService.cs• L15-81 Screenbox.Core/…/LibraryService.cs• L512-581

Playback metadata

The Screenbox.Core/…/VlcMetadataService.cs service reads metadata from the playback media when file properties are not the source of truth. It parses the playback item with a time limit and returns the media information only when parsing succeeds. This path supports playback-time detail loading and is distinct from the storage scan path.

Both metadata paths produce MediaInfo. The shared model carries the media type, file facts, duration, title, and type-specific music or video properties so the view-model layer can update one media item consistently.

References: Screenbox.Core/…/IPlaybackMetadataService.cs• L8-19 Screenbox.Core/…/VlcMetadataService.cs• L12-49 Screenbox.Core/…/MediaInfo.cs• L7-79

Media models

Media view model

The Screenbox.Core/…/MediaViewModel.cs MediaViewModel is the item that browsing and playback use. It joins a media source with its name, location, type, dates, metadata, availability, thumbnail, and music relationships. It can load details and artwork when needed, so the library can show a useful item before every optional property is ready.

The media factory reuses an existing item when a change or cache record refers to the same location. This keeps library updates connected to the objects already used by pages and playback instead of creating unrelated duplicates.

References: Screenbox.Core/…/MediaViewModel.cs• L21-79 Screenbox.Core/…/MediaViewModel.cs• L102-117 Screenbox.Core/…/MediaViewModel.cs• L148-234 Screenbox.Core/…/MediaViewModelFactory.cs• L11-27 Screenbox.Core/…/MediaViewModelFactory.cs• L75-143

Music relationships

Music items are organized into album and artist view models while the scan runs. Album and artist groups keep related songs together and provide the group-level name, year, artwork, and playback actions used by music browsing.

The grouping uses metadata from each song. Missing album or artist values go into explicit unknown groups, so incomplete tags remain visible instead of being dropped. Genres are collected into a distinct sorted list for filtering and navigation.

References: Screenbox.Core/…/AlbumViewModelFactory.cs• L9-50 Screenbox.Core/…/ArtistViewModelFactory.cs• L9-56 Screenbox.Core/…/LibraryService.cs• L192-214 Screenbox.Core/…/AlbumViewModel.cs• L19-46 Screenbox.Core/…/ArtistViewModel.cs• L17-26

Browsing view models

The music and video page view models listen for replacement library snapshots and adapt them for the UI. Music pages use the grouped library state and can add folders to the system library. The all-videos page mirrors the current video list and can build a playback queue from it.

During a scan, pages can show partial results and loading state. This makes the library usable before the full folder set has finished loading while keeping the coordinator as the owner of scan timing.

References: Screenbox.Core/…/MusicPageViewModel.cs• L15-59 Screenbox.Core/…/AllVideosPageViewModel.cs• L16-73 Screenbox.Core/…/ILibraryCoordinator.cs• L12-31

The broader library model is described in media-library. Source selection and path resolution are covered in media-source-resolution. Queue and recent-item behavior is covered in playlists-and-recent-media, while discovery and search are covered in search-and-content-discovery.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the query's format filters and the metadata fields stored in the cache aligned for music and video.
  • If a library setting changes query scope, make sure watcher recreation still follows the setting change.
  • If a media identity or path rule changes, check cache restoration, change-event handling, and factory reuse together.
  • If music metadata names change, check album and artist grouping as well as genre extraction.
  • If snapshot replacement or progress reporting changes, check page listeners for both loading and partial-result behavior.

Important failure modes

TriggerConsequenceGuard
Known-folder access is deniedThe affected library has no active query and cannot refresh from that folder.Request access before creating the query and keep the empty snapshot usable.
The saved folder scope differs from the current libraryCached records may describe the wrong set of folders.Compare folder paths and force a full scan when they differ.
Storage change tracking is lost or cannot be resolvedIncremental updates may miss a create, delete, or rename.Abandon the cache path and rebuild from the library query.
A file is unavailable during metadata extractionThe item may have limited title or duration data.Keep a default MediaInfo and continue scanning other files.
Removable storage changes arrive in a burstRepeated scans can waste work and cause unstable intermediate UI state.Debounce device and query change notifications before fetching.

Key files

FileWhy to read it
Screenbox.Core/…/LibraryContext.csShared library state and loading signals.
Screenbox.Core/…/LibraryCoordinator.csWatcher, refresh, cancellation, and progress flow.
Screenbox.Core/…/LibraryService.csQuery scope, cache choice, scan, and snapshot assembly.
Screenbox.Core/…/FilesService.csDirect file access, pickers, neighboring files, and recent-file actions.
Screenbox.Core/…/StorageMetadataService.csStorage-backed metadata extraction and safe fallback behavior.
Screenbox.Core/…/MusicLibrary.csMusic collection shape for browsing.
Screenbox.Core/…/MediaViewModel.csShared media item used by library pages and playback.
Screenbox.Core/…/MediaViewModelFactory.csMedia identity and reuse across cache and change updates.
Screenbox.Core/…/AlbumViewModelFactory.csAlbum grouping and unknown-album handling.
Screenbox.Core/…/ArtistViewModelFactory.csArtist grouping and unknown-artist handling.