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 / database-and-playback-history

Database and playback history

Screenbox uses one local SQLite database to make library loading fast and to keep user-created playlists and playback positions across app runs. The database is initialized at startup, while the library cache can be rebuilt from storage and playback history is kept as a small saved snapshot.

Database lifecycle

The database lifecycle turns local app data into a ready store before other services read it. Screenbox.Core/…/DatabaseService.cs places screenbox.db in the app's local data folder and shares one initialization path for all database operations.

Schema initialization

Schema initialization creates the library, playlist, and playback-history records when the database is new. It also checks table columns on later starts and imports old JSON playlists when the new playlist tables are empty.

The main data areas are:

Data areaRoleRecovery expectation
Library folders and media recordsRebuildable view of known music and video filesSafe to recreate from the device library
Playback progressLast saved position for recent mediaCan start empty if it cannot be loaded
Playlists and playlist itemsUser-created playlist names and ordered media pathsIntended to persist across library rescans

Database recovery

SQLite or file errors during schema setup cause the database file to be removed and created again with all known tables. This lets the app start with a usable store, but it can also remove playlists because the recovery boundary is the whole database file.

The app logs startup initialization errors and continues its startup path. Later reads treat a missing or unreadable library cache as a reason to scan storage again.

References: Screenbox.Core/…/DatabaseService.cs• L9-69 Screenbox.Core/…/DatabaseService.Schema.cs• L15-76 Screenbox/App.xaml.cs• L317-328

Library cache

The library cache is a saved snapshot of library folders and media metadata. Screenbox.Core/…/LibraryService.cs uses it to avoid a full file and metadata scan when the saved folders and storage change history still match the current library.

Cache refresh

Music and video use separate cache snapshots. Each refresh replaces the folders and media records for one media type in one database transaction, so the cache represents the latest scan rather than a long-lived record of every file ever seen.

The normal read path is:

  1. Load the saved folders and media records for the requested media type.
  2. Compare the saved folders with the current library.
  3. Apply tracked file changes when the cache still matches.
  4. Run a full scan and write a new snapshot when the cache is missing, stale, or cannot resolve changes.

This flow keeps cached metadata useful without treating it as the source of truth. The files and the current storage library remain authoritative.

References: Screenbox.Core/…/LibraryService.cs• L110-214 Screenbox.Core/…/LibraryService.cs• L217-305 Screenbox.Core/…/DatabaseService.LibraryCache.cs• L12-175

Playlist persistence

Playlist persistence keeps named, ordered selections of media separate from the rebuildable library cache. Screenbox.Core/…/PlaylistService.cs saves a playlist as one record plus its ordered media paths and exposes the result to playlist screens through the shared playlist context.

Playlist records

Saving a playlist replaces its current item list as one operation. Loading joins each saved path with matching library metadata when available, so a playlist can still identify an item after a cache refresh. If no cached metadata exists, the path supplies a basic fallback identity.

Playlist changes have these effects:

OperationResult
Save or add itemsUpdates the playlist timestamp and rewrites its ordered items
List playlistsReturns playlists by most recent update with their items
Delete a playlistRemoves the playlist and its child items
Import or exportReads or writes playlist entries through a file format without replacing the saved database playlist until a save occurs

References: Screenbox.Core/…/PlaylistService.cs• L14-80 Screenbox.Core/…/DatabaseService.Playlists.cs• L11-136 Screenbox.Core/…/DatabaseService.Playlists.cs• L138-191 Screenbox.Core/…/PlaylistsContext.cs• L7-16

Playback progress

Playback progress provides resume-from-position behavior for media locations. Screenbox.Core/…/PlaybackProgressTracker.cs keeps recent positions in memory, limits the list to 64 entries, and writes the full current snapshot to the database when the app suspends.

Progress flow

The tracker uses the media location as the identity for a saved position. A new playback update moves that location to the front of the recent list; asking for an unknown location returns zero.

This diagram shows how playback state moves between the player, the in-memory tracker, and the database.

flowchart LR
    A[Playback position update] --> B[In-memory recent positions]
    B --> C[Resume lookup]
    B --> D[App suspension]
    D --> E[Replace saved progress snapshot]
    E --> F[(SQLite playback history)]
    F --> G[Load on next app run]
    G --> B

The tracker loads saved positions when requested and saves them in one replacement transaction. A load or save error is non-fatal: the app can continue with the positions that remain in memory, or with an empty list after a failed load.

References: Screenbox.Core/…/PlaybackProgressTracker.cs• L13-39 Screenbox.Core/…/PlaybackProgressTracker.cs• L41-137 Screenbox.Core/…/DatabaseService.PlaybackProgress.cs• L9-54 Screenbox/App.xaml.cs• L293-315

Change checklist

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

  • Keep the table shape, cache DTOs, and media-type filters aligned when adding or removing cached metadata.
  • Preserve the playlist item order contract between saved sort_order values and playlist display order.
  • Keep playlist loading usable when a media path is absent from the library cache.
  • Treat database-file recreation and schema-drift handling as a playlist data-loss boundary.
  • Keep suspension handling connected to the progress snapshot save so recent positions are not left only in memory.
  • Keep the media-location identity consistent between playback updates, progress reads, and saved rows.

Important failure modes

TriggerConsequenceGuard
Database schema setup hits a SQLite or file errorThe whole database is recreated and saved playlists can be lostLog the failure and treat the library cache as rebuildable; protect playlist data before changing recovery behavior
Saved library folders or tracked changes no longer match storageThe app discards the cache path and performs a full scanUse the current storage library as the source of truth
A playlist item has no matching media-cache recordThe playlist still loads, but its metadata is limited to path-based fallback dataDo not make playlist loading depend on a cache row
Playback progress cannot load from SQLiteResume positions start empty for that runKeep playback usable without saved history
The app suspends before progress is savedRecent position changes can be lostComplete the suspension deferral and await the progress save

Key files

FileWhy to read it
Screenbox.Core/…/DatabaseService.Schema.csShows database creation, schema checks, legacy playlist import, and recovery boundaries
Screenbox.Core/…/DatabaseService.LibraryCache.csShows the cache snapshot contract for music and video
Screenbox.Core/…/LibraryService.csShows when the app trusts, updates, or rebuilds the library cache
Screenbox.Core/…/DatabaseService.Playlists.csShows playlist records, ordered items, and metadata fallback behavior
Screenbox.Core/…/PlaylistService.csShows the product-level playlist save, update, delete, import, and export flow
Screenbox.Core/…/PlaybackProgressTracker.csShows recent-position limits and the in-memory to disk lifecycle
Screenbox.Core/…/DatabaseService.PlaybackProgress.csShows the saved playback-progress snapshot contract
Screenbox/App.xaml.csShows startup database initialization and suspension-time save coordination