Files
hyprland-headless-stream-pl…/checks/unit.nix
T
Hermenix 7190960120 capture: encode JPEG in-process too, so the live stream keeps working
PNG-only was a real defect, not a nice-to-have: `hhstream_live.server` serves
`multipart/x-mixed-replace` with `image/jpeg` parts and its own capture default is
`grim -c -t jpeg -q 80 -`, so switching this backend on would have broken video
streaming while looking faster in a benchmark.

JPEG now goes through libjpeg-turbo by ctypes, using its `TJPF_BGRX` input format
-- which is exactly the compositor's B, G, R, X memory order, so the frame goes
into the encoder with no copy and no colour conversion. Measured on this VM
against the real compositor, the deployment's own settings (Virtual-1, 1280x720,
jpeg q80, 200 frames each, two runs):

                        p50 ms   fps    our CPU   compositor CPU
    in-process           76.2   13.1     2.1 ms      51.5 ms
    grim -c -t jpeg     137.0    7.3     0.5 ms      80.2 ms
                                                          (second run: 84/144,
                                                           55.1/79.8 -> 1.71x)

So ~1.7-1.8x the frame rate and ~1.5x less of the compositor's CPU, with frames
of the same order (188 KB against grim's 222 KB). PNG stays available through the
stdlib for a deployment that would rather not carry the library.

Both segfault-class traps from the spike are handled and documented: every
argtype is declared (an undeclared pitch is a crash, not a wrong number), and
`*jpegBuf` is reset to NULL after `tjFree` (turbojpeg reads a non-NULL pointer as
a caller buffer, so the next frame would write into freed memory). The decoder is
here because a lossy codec can only be tested by round-tripping pixels -- the new
suite encodes solid blocks whose channels differ enough that a swap is
unmistakable, and includes a padded stride.

Packaging: libjpeg-turbo is reached through ctypes, so nothing links it and the
path must reach the loader -- the console scripts get a wrapper with it on
LD_LIBRARY_PATH, and the unit check exports it too. A missing library is reported
at startup rather than once per frame: a server that is up but can never produce
a frame is worse than one that refuses to start.

Verified end to end: server with `--capture wayland:Virtual-1 --format jpeg`,
client Hello `image/jpeg`, three frames read, each magic `ffd8`.
2026-10-11 22:58:28 +00:00

108 lines
3.9 KiB
Nix

# checks.<system>.unit -- the Python unit suite from tests/, run against the
# package built by nix/package.nix, inside the build sandbox.
#
# The whole tree, not one directory: it used to take tests/unit alone, which
# meant the broker's and the pointer driver's suites (and the sessions one) were
# never collected by any check -- a gate that could not see 432 of 551 tests.
#
# pywayland is in the test environment because the pointer driver imports it;
# without it those tests fail as an import error rather than as the thing they
# test.
#
# nix/package.nix sets doCheck = false deliberately, so this is the only place
# the suite runs in a build.
{
lib,
libjpeg_turbo,
runCommand,
python3,
hhstream,
}:
let
# `xkbcommon` beside `pywayland`, and it is not optional: `tests/pointer/
# test_keymap.py` compiles a real `us` keymap and a real `de` one, because
# only xkbcommon can say that `é` is unmappable on `us` and that `@` on `de`
# needs a level-three mask rather than Shift. A suite that could skip those
# would let the unmappable-character path rot, which is the path that decides
# whether a bad string types a prefix.
pytestEnv = python3.withPackages (ps: [ ps.pytest ps.pywayland ps.xkbcommon ]);
# Only the suite itself, so an editor's droppings or a local .pytest_cache
# cannot change the derivation's hash.
testSrc = lib.cleanSourceWith {
name = "hhstream-tests";
src = ../tests;
filter =
path: type:
let
base = baseNameOf (toString path);
in
!(type == "directory" && (base == "__pycache__" || base == ".pytest_cache"))
&& !lib.hasSuffix ".pyc" base;
};
in
runCommand "hhstream-unit"
{
nativeBuildInputs = [ pytestEnv ];
passthru.testSrc = testSrc;
meta.description = "hhstream unit tests (pytest)";
} ''
cp -r ${testSrc} suite
chmod -R u+w suite
# `tests/sessions/integration/test_lifecycle.py` locates the repository by
# looking upward for a `pyproject.toml` (it needs the root for `src/` on
# PYTHONPATH and as a cwd for the subprocesses it spawns). In a build there is
# no repository above the copied suite, so it raises at import time and takes
# the whole run down as a collection error. Giving it the marker it documents
# is the fix; the test itself is hermetic -- no compositor, no network.
cp ${../pyproject.toml} suite/pyproject.toml
# The package is an application, so its library is not on the default path:
# point PYTHONPATH at what nix/package.nix installed. Importing it here rather
# than letting pytest do it makes a packaging problem distinguishable from a
# test failure.
export PYTHONPATH=${hhstream}/${python3.sitePackages}
# `tests/unit/test_jpeg.py` round-trips a real frame through libjpeg-turbo: a
# lossy codec can only be checked by comparing pixels, so the suite needs the
# library -- and it needs the *loader* to find it, because the encoder reaches
# it through `ctypes` and nothing links it. Same reason nix/package.nix wraps
# the console scripts with this path.
export LD_LIBRARY_PATH=${libjpeg_turbo}/lib
export PYTHONDONTWRITEBYTECODE=1
export HOME=$PWD
python3 -c 'import hhstream; print("testing hhstream", hhstream.__version__, "from", hhstream.__file__)'
python3 -c 'import hhstreamd.ipc, hhstream_pointer; print("broker ipc client + pointer driver import")'
set +e
pytest suite \
--color=no -rf -q \
--junit-xml=report.xml \
-p no:cacheprovider \
--strict-markers \
--strict-config
pytest_rc=$?
set -e
echo
set +e
python3 ${./lib/check_report.py} report.xml
report_rc=$?
set -e
if [ "$report_rc" -eq 1 ]; then
echo "unit check: the suite is empty or its report is unusable -- failing." >&2
exit 1
fi
if [ "$report_rc" -ne 0 ] || [ "$pytest_rc" -ne 0 ]; then
echo "unit check: the suite ran but did not pass (pytest exit $pytest_rc)." >&2
exit 1
fi
mkdir -p $out
cp report.xml $out/junit.xml
''