Building Leyla: a media server that fits in one container
Leyla is a media server for the films and series you already own. You run it at home, and it plays your library on the TV, the phone and the laptop. It is in open beta at leyla.media.
This post is about how it is built: the problem, the architecture, a few decisions we would make again, and a few we had to undo.
The problem
Running your own media server usually means running several things. One program plays the files. Another counts what you watched. Another takes requests from the people you share with. Backups are a script you wrote once and hope still works. Each has its own login, its own database and its own way of breaking.
We wanted one thing to install and one thing to look after. It should play a file as it is whenever the screen can take it, and convert only when it has to. It should run on a Raspberry Pi as well as on a gaming PC. And when it does something unexpected, it should say why.
How it is put together
The server is one Rust process, built on Tokio and Axum. Inside it are modules for the catalog, the library scanner, playback, requests, stats and backups. They talk through their public interfaces, not over the network. There are no services to wire together.
PostgreSQL is the only database. It ships inside the same container and Leyla starts and supervises it, so nobody types a connection string. If you already run PostgreSQL, you can point Leyla at it instead.
ffmpeg does the media work, always as a separate process and never linked into the server. A transcode that crashes or hangs is a child process we can kill, not a server that goes down. We build our own ffmpeg, based on jellyfin-ffmpeg, with a few patches for how we cut HLS segments.
The clients are a React web app, one Flutter app for Android, iPhone, iPad and Android TV, a Roku channel and a Kodi add-on. All of them speak the same HTTP and JSON API.
A few small services run on Cloudflare: the licence service, a gateway that looks up titles and artwork with our provider keys, and the download storage for releases and model packs. None of them sees your library. A server sends a random id for its licence and a title name when it looks something up, and nothing about who is watching.
Choices and why
Playback decisions are data. Before anything plays, a planner takes the file’s facts and the device’s abilities and decides: play it as it is, repackage it, or convert it. Every decision carries a reason code, like AUDIO_CODEC_UNSUPPORTED. When a film converts on one TV and not on another, the answer is in the plan, not in a log nobody reads.
Play the file as it is first. Converting costs CPU, heat and picture quality. Leyla converts only what the screen cannot take. Often that is just the audio track, and the picture passes through untouched.
Built for small machines. Every night the test suite runs on a simulated machine with two cores, 2 GB of memory and the speed of a hard disk. A change that only works on a fast server does not pass.
The licence works offline. Paid features check a signed key on the server itself, with 30 days of grace. If our licence service disappears, your server keeps running.
How you run it
Leyla is a Docker image for amd64 and arm64, a .deb and an .rpm for the common Linux distributions, and a Windows installer. On a NAS it runs from the same compose file as on any Linux machine:
mkdir -p ~/leyla && cd ~/leyla
curl -fsSLo compose.yaml https://leyla.media/compose.yaml
docker compose up -d
Then you open port 10001 in a browser and answer the setup. Your films stay where they are. Leyla reads them and writes its own data in its own folder. Backups are sealed with a recovery code on your server before they go anywhere, to a folder of yours or to our storage, and we cannot open them.
Every release runs through the same checks before it ships. Each package starts on its real operating system, gets set up, scans three short clips, plays one directly, converts one, checks that sound and picture line up, and shuts down cleanly.
Two decisions worth explaining
We count work instead of timing it
For months our performance tests measured time: how long a scan took, how fast the first frame appeared. The numbers moved by 30% depending on what else the build machine was doing, so a real regression looked like noise.
So we started counting. A separate test drives the server through a fixed list of actions (start, first scan, an unchanged rescan, browsing, playing, seeking, restarting) and counts what each one costs: SQL statements, rows read and written, commits, bytes read from disk, ffmpeg starts. Each count has a budget, and budgets only go down.
The first run found waste no timing test had caught. A start ran 80,760 SQL statements. Watcher scans copied 1.29 million rows. Neither was slow on a fast machine. Both were slow on a Pi.
We test sync with a flash and a beep
“Audio and video stay in sync” is easy to promise and hard to check by eye. Our test files have a white frame and a short beep at the same moments, at irregular gaps. For every way Leyla can deliver a video, the test decodes what a player would receive, finds every flash and every beep, and fails if any beep moves away from its flash. It checks the timing the way Chrome, Firefox and Safari each read it, because they do not agree.
What failed and what we changed
The catalog outgrew the database. We stored titles, credits, streams and file records as ordinary rows. On our own library of 44,472 items, the database reached 2.1 GB, and about a third of that was indexes. We moved the catalog’s read-mostly facts into compressed column segments inside PostgreSQL, with a versioned manifest per library. The same library’s catalog data now comes to about 197 MB.
The move had its own bug. The in-memory file index re-read every file of a library after any write to it: 52,000 rows for our TV library, after every enrichment. It now reads only the files the writes since its last look touched.
Our own tests polluted our numbers. This week we added a count of active installs to our internal dashboard and saw 404 trials. Almost all of them were ours: every test server in CI started a real trial against the production licence service. The test harnesses now run without a licence service, and we deleted the test rows.
One bad copy locked a file for an hour. When a copied stream fails a validation check, Leyla stops copying that file for an hour and serves it another way. The planner did not know, and kept planning a copy for Roku, which cannot play the fallback. So a single failure meant every start on that TV was refused for the hour. The planner now converts such a file for those devices instead. The fix is in the next release.
A cache that did the same work twice. Two parts of a rescan sometimes asked for the same library’s files at the same moment, and both caught up on the same changes. A lock per library fixed it, and the unchanged rescan now reads about 1,500 rows instead of up to 2,649.
None of these were dramatic, and all of them came from measuring before guessing. Most bugs we find now come from reading what the server says about itself.
Leyla is free until December 31. If you run your own films at home, we would like to hear what breaks: leyla.media.