Skip to content

The media helper (holo-media.exe)

Melange 0.12 plays video for client mods (wum.media) in a separate 64-bit process, holo-media.exe, so a crash in a decoder or the torrent engine can never end a match, and the 32-bit game spends only about 25 MB of its address space on it. This page describes the media pack the helper comes in, how Melange starts and checks it, and the protocol between the two. The protocol is defined by four headers that both builds compile: src/media/media_proto.h (names, caps, timings), src/media/pipe_codec.h (the messages), src/media/shm_logic.h (the frame section) and src/media/url_logic.h (which links are accepted). A change to any of them needs both sides and a new protocol number.

Melange 0.18.0 speaks protocol 2. url_logic.h adds the audio kinds (SoundCloud, Bandcamp, archive.org, Jamendo and radio:<uuid>, accepted only when the caller asks for them, one row each in kAudioKinds), and ValidStreamUrl, the rule a radio station’s resolved stream URL must pass on both sides: http or https, at most 1024 bytes, no user info, and a DNS name or a public IPv4 literal as host (no loopback, private, link-local, CGNAT, multicast or localhost). Protocol 2 adds audio-only players, the audio kinds’ tracks and lists, radio streams, the icy-title property and the YouTube search (see Protocol 2).

The core still uses a protocol 1 helper (kProtoV1, media pack v1), and it reads the helper’s protocol from its hello. It never sends a protocol 1 helper anything of protocol 2: wum.media answers "pack-update" instead (before the helper runs, media pack v1 counts as protocol 1), and a player that meets a protocol 1 helper anyway (a pack whose helper turned out older) gets an error event "pack-update" in place of its open.

The pack is downloaded once per machine, on the user’s click in a mod’s own panel, into Documents\Melange\media\<ver>\:

File What Licence
holo-media.exe The helper, built and Authenticode-signed by Melange’s CI (helper/holo-media), libtorrent statically linked MIT (libtorrent BSD, Boost BSL, OpenSSL Apache 2.0)
libmpv-2.dll mpv and ffmpeg, the unmodified LGPL x86_64 build of a pinned zhongfly release LGPL 2.1+
deno.exe The JavaScript runtime yt-dlp needs for YouTube MIT
LICENSES\*, SOURCES.md Every licence, and where each component’s source is
pack.json The pack’s own manifest

yt-dlp is not in the pack: it lives in its own folder Documents\Melange\media\ytdlp\<ver>\yt-dlp.exe and is updated from a rolling manifest (media-ytdlp.json) at most once a day, or at once after yt-dlp reports a YouTube change. Both come from https://github.com/JaminB/melange/releases/download/media-... only, over https. Every file’s size and SHA-256 are compiled into melange.asi (src/media/pack_manifest.h), so a Melange build accepts exactly one pack; the yt-dlp manifest carries a serial number that may never go down. [Media] AllowDownload=0 blocks both.

media-v2 carries the protocol 2 helper beside the very same libmpv-2.dll and deno.exe as media-v1 (media/media.lock.json pins both). Nobody should download 84 MB again for a 10 MB change, so the release has two zips: the whole pack melange-media-2.zip and media-v2-helper.zip (the helper, pack.json, the notices; about 4 MB: the 9.9 MB libtorrent helper zips to 3.7 MB). Both are published by the same workflow run with --latest=false, and media-pack.ps1 -ReuseFrom 1 fails when libmpv or deno differ from the media-v1 table frozen in pack_manifest.h.

  • While only media-v1 is installed, it keeps running (helper protocol 1, so a video party works). wum.media.pack() says state = "ready", version = "1", proto = 1, updateAvailable = true, and downloadMB is the small zip. A protocol 1 helper never receives a protocol 2 form: audio-only and the new sites answer "pack-update".
  • wum.media.install() (still on the user’s click) starts the upgrade. The state is "updating" while it runs, so a video that is playing is not stopped. Core re-hashes libmpv-2.dll and deno.exe of the old folder against the new manifest and copies them (through a 1 MiB buffer, hashing again; the log says [media] reused libmpv-2.dll from the media pack 1 (verified)), downloads and extracts only the helper zip, checks pack.json against the whole manifest, renames the folder into place and deletes the old one (when its helper is still running it stays and the next start removes it).
  • A tampered or missing libmpv or deno (any hash, size or location that differs) falls back to the whole zip, with a log line. A failed upgrade returns to "ready" with updateError set, so media-v1 stays usable.
  • [Media] PackUrl replaces the URL of whichever zip the install uses (the helper zip when libmpv and deno can be reused, else the full zip); the size and hash stay the manifest’s.
  • The new pack runs from the next helper launch; a running protocol 1 helper keeps its own files until it stops.

Publishing is the owner’s: push the tag media-v2, then commit the published hashes. py -3 scripts/pin-media-v2.py does the second step: it downloads melange-media-2.zip, media-v2-helper.zip and pack_v2_values.txt from the release with gh, runs scripts/gen-media-manifest.py --helper-zip ... --reuse-from 1 --reuse libmpv-2.dll,deno.exe --v2-out (both zips checked against each other and against the rules melange.asi applies), checks that the reused libmpv and deno equal the media-v1 table and that CI’s pack_v2_values.txt holds the same values, and writes them into namespace v2 of src/media/pack_manifest.h, where kPublished flips to true. --check only checks, and --zip / --helper-zip take local copies. Until then a build compiles in media-v1 and installs nothing newer.

Developer upgrade pack: scripts\make-dev-pack.ps1 -ReuseFrom 1 -Helper ... -Libmpv <media\libmpv-2.dll> -Deno <media\deno.exe> also writes the helper zip and media_dev_upgrade.h; rebuild, and a machine that has media-v1 upgrades by the copy.

Installing streams the zip to disk with a running hash (a dropped download resumes), then extracts only the exact names of the manifest, each checked against its size and hash, into a temporary folder that is renamed into place. No step holds more than a few MB of memory, since the game is a 2 GB process.

Before every start Melange (on its media worker thread, never the game thread) opens holo-media.exe, libmpv-2.dll, deno.exe and yt-dlp.exe so they cannot be changed or deleted, hashes them again, and checks that holo-media.exe is signed by the same publisher as melange.asi (skipped, and logged, on an unsigned developer build). [Media] AllowCustomLibmpv=1 with CustomLibmpvPath loads a library of the user’s choice instead of the pack’s libmpv-2.dll, unchecked (the LGPL’s relinking right); Melange logs that it did.

The helper is started suspended, with no inherited handles, in a job object that kills it (and the yt-dlp and deno processes it starts) when the game exits, then resumed. It runs at Low integrity ([Media] HelperLowIntegrity=1): it can write only its own folder %USERPROFILE%\AppData\LocalLow\Melange\media-cache\<pid>-<random>\ ([Media] CacheDir), and its environment is rebuilt from scratch (no PYTHON*, YTDLP* or MPV_HOME from the user’s). Command line:

holo-media.exe --pipe \\.\pipe\melange-holo-<pid>-<rand64> --parent <game pid> --cache <folder>
--libmpv <path> --ytdlp <path> --deno <path> [--sweep]

--sweep (the first start in a game session only) deletes the cache folders of helpers that are no longer running. The helper exits when the game’s process ends or on quit, never because pings stop.

mpv runs with no configuration, scripts or user files (config=no, an empty config-dir, load-scripts=no and every built-in script off, no input bindings or IPC, access-references=no, ordered-chapters=no, no automatic subtitle or audio files), vo=libmpv with the software renderer, hwdec from [Media] Hwdec, and ytdl only for the items yt-dlp extracts. yt-dlp options are built inside the helper from a fixed list (ignore-config, no-plugin-dirs, no-exec, no-cache-dir, the item’s own site’s extractors, deno as the JavaScript runtime, and the subtitle language when it is a plain language code); nothing a mod or another player sends ever becomes a yt-dlp or mpv option. URLs are rebuilt from the parsed video or playlist id (or the audio kinds’ parsed parts) on both sides. Each item sets the same list of per-item options before it loads (mpv_opts.h ItemOptions, pinned per kind by holo_media_logic_test), so nothing of one item leaks into the next.

Melange creates the pipe (first instance, local clients only, current-user access) and accepts only the helper’s own process id. Each line is one JSON object of at most 64 KB, named by "t"; a longer line is dropped whole, and every message is range-checked and capped on receipt (each side treats the other’s lines as untrusted). Melange reads the pipe on its own thread and writes it from another; the game thread never waits on the pipe.

Melange to the helper:

t Fields
hello proto, hwdec, audioDelayMs, maxHeight; protocol 2: httpRadio ([Media] AllowHttpRadio) first line
open id (player), gen (load), shm (the player’s frame section; "" with audio), url, bt {ih, index} or stream (exactly one), start, paused, volume, mute, speed, height, alang, slang; protocol 2: audio, kind loads an item (again, for a quality change)
set id, prop (pause, volume, mute, speed, aid, sid), value
seek id, pos (seconds), mode (exact, keyframes, relative)
size id, w, h the drawn size, at most 1280x720
close id
resolve rid, url, or (protocol 2) search {q, n} a playlist or video through yt-dlp; protocol 2 also a set, an album, an archive.org item, or a YouTube search
bt.add ih, magnet, uploadKBps, upnp, port
bt.select ih, index
bt.remove ih closes the streams, removes the torrent and its files
ping every 2 s, from the writer thread
quit

The helper to Melange:

t Fields
hello proto, mpv, ffmpeg, libtorrent, hwdec first line
prop id, gen, name, value, qpc time-pos and audio-pts (at most 10 a second, with the helper’s QueryPerformanceCounter time in ms, which Melange uses to extrapolate), pause, paused-for-cache (also true from file-loaded until the load first plays, while mpv seeks to start: time-pos already reads start then, and the clock must not run), duration, track-list, demuxer-cache-time, eof-reached, video-params, media-title, speed; protocol 2: icy-title
event id, gen, name (file-loaded, end-file), reason (eof, error, stop)
error id, gen, rid, ih, code, message load, shm, proto, torrent, disk, internal
resolved rid, title, items [{url, id, title, duration}] (protocol 2: id only for YouTube, plus artist and live), or error (ytdlp, network, private, timeout) and message
bt.files ih, name, files [{index, path, size, video}] once the metadata arrived
bt.status ih, down, up, peers, progress, have, metadata once a second
bt.removed ih
pong

Melange keeps only the latest value of each property, and at most 256 other messages, so a stalled game thread never blocks the helper. A level load can stall the game for many seconds, so the watchdog runs on the pipe threads: the helper is restarted only after 6 seconds without a pong while its process is alive, at most once a minute, and the player is then reopened where it was.

What Melange 0.18.0 and media pack v2 add. A protocol 1 reader ignores the new fields of hello and resolved; the other forms reach only a protocol 2 helper.

Audio-only players. open with audio: true names no section (shm is ""), and the helper maps none, renders nothing and sets vid=no; a video open still needs its section. Every link but a YouTube video needs audio. The helper logs player <id> gen <gen>: audio only (<kind>), no frame section mapped and, once it plays, section none; the core logs player <id> (<mod>): audio only (<kind>), no frame section. The yt-dlp format is ba[acodec^=mp4a]/ba[acodec=aac]/ba[acodec=mp3]/b[acodec=mp3][height<=?360]/b[acodec^=mp4a][height<=?360]/ ba[acodec!=none][acodec!=alac][acodec!=vorbis]/ba/b[height<=?360]: AAC or MP3 first, because mpv’s ytdl_hook opens one yt-dlp format as a delayed track that carries the codec but no extradata, which ALAC and Vorbis cannot start without (a free-download Bandcamp track lists its lossless downloads as the best audio), and <=? because Jamendo’s formats have no height.

Per-kind options (mpv_opts.h ItemOptions; access-references is yes exactly where ytdl is):

kind ytdl yt-dlp use-extractors lavf protocol_whitelist network-timeout
youtube yes youtube,youtube:tab 60
soundcloud yes soundcloud,soundcloud:set 60
bandcamp yes Bandcamp,Bandcamp:album 60
jamendo yes Jamendo,JamendoAlbum 60
archive (one file) no: https://archive.org/download/<id>/<file>, rebuilt from the canonical link (archive.org for resolve) http,https,tls,tcp,crypto,hls,httpproxy 60
radio no http,https,tls,tcp,crypto,hls,httpproxy 15
torrent no 60

The whitelist is set in both demuxer-lavf-o and stream-lavf-o, so a radio stream or its HLS playlist can never reach file, data, concat or the like; load-unsafe-playlists=no holds for every item.

Radio. open carries stream (with kind: "radio" and audio) in place of url. The core resolves the station UUID to its stream through the station resolver (src/media/station.h; Radio Browser through netfeed), checks it with ValidStreamUrl and [Media] AllowHttpRadio, and the helper checks it again against hello.httpRadio. The helper reports the stream’s metadata/by-key/icy-title as the icy-title property: control characters (C0 and C1) removed, bytes that are not UTF-8 read as Latin-1, trimmed, at most 512 bytes.

Resolve. A set, album or archive.org item lists at most 50 tracks (archive.org’s entries carry every format, about 400 KB of JSON each), each a canonical track link of the same kind (archive.org’s download paths become archive.org/details/<id>/<file>; files in subfolders and other hosts are dropped), with artist and live where yt-dlp says. Of an archive.org track’s formats the most streamable one is chosen: the VBR MP3 derivative, then Ogg or Opus, then M4A, then the FLAC original (archive.org serves about 0.5 MB/s, and a FLAC original started mid-track took 12 to 30 s to play). search {q, n} runs yt-dlp with use-extractors=youtube:search and the argument ytsearch<n>:<q> after --, so the query can never become an option; q has its control characters stripped, is trimmed and cut to 100 bytes (on both sides), and n is 1..30.

Each player has a section Local\melange-holo-<pid>-<random>-<id> that Melange creates (current-user access, Low label): a 128-byte header and three slots of 1280x720 BGR0 pixels from offset 4096, about 11 MB. The header holds magic (“HMF1”), version, the size limits, latest (slot plus a sequence number), reading (the slot Melange is copying) and each slot’s {w, h, stride, fmt, frameNo, pts}. The helper renders into the slot that is neither latest’s nor reading’s and then publishes it; Melange announces the slot it reads and re-checks latest, so the helper never writes a slot being copied. Melange trusts nothing in the header: it uses its own limits, and copies a frame only if 1 <= w <= 1280, 1 <= h <= 720 and stride = 4 * w; anything else is skipped and counted.

Melange uploads a new frame only for a player that was drawn in the last 250 ms, through a pixel buffer object into a texture allocated once at 1280x720 (about 0.3 ms at 960x540 on the game thread).

libtorrent adds a magnet with every file at priority 0; the chosen file then downloads sequentially, with the start and the last 2 MB first (an MP4’s index) and about 30 seconds read ahead. mpv reads it through a stream callback, with no local web server. The magnets are canonical (infohash, name and at most 8 udp/http/https trackers; web seeds, direct peers and other parameters are dropped), url seeds from the metadata are removed, and trackers in loopback, link-local or private address ranges are ignored. DHT, local peer discovery and trackers are on; UPnP and NAT-PMP are off ([Media] TorrentUPnP). Data lives under the helper’s own cache folder and is deleted when the torrent is closed, when the party ends, and at the next start after a crash. Every file any open torrent has selected, now or earlier (a deselected file keeps its data until the torrent closes), counts against [Media] TorrentCacheMB, together with the file being opened.

media_selftest (the core, x86) and holo_media_shared_test (the helper project, x64) run the same cases against the shared headers: the accepted and refused links, magnet canonicalisation, the frame section’s checks and a two-thread triple-buffer run, and every pipe message’s round trip, hostile values and a mutation fuzz (protocol 2’s forms in their own part). holo_media_logic_test pins the per-kind option lists, the resolve and search command lines and the yt-dlp answers of every kind. holo-media.exe --selftest plays av://lavfi:testsrc into a private section in CI, then a 440 Hz sine audio only on the same player (no section mapped, audio-pts advancing). holo_media_lowil_test --audio (local, online) resolves a search, a set, an album, an archive.org item and a Jamendo album and plays each audio kind and a radio stream at Low integrity. --item <link> (resolve, then play the first track) and --track <link> (play it as it is), with --start <s>, --volume <n>, --seconds <n>, --paused and --timeline, play chosen items instead and print the time-pos, audio-pts and paused-for-cache timeline.