mono/packages/media/cpp/README.md
2026-04-12 22:57:40 +02:00

4.3 KiB
Raw Blame History

media-img (C++)

CMake-based media-img binary: CLI resize, HTTP REST (serve), and line-delimited JSON IPC (ipc) over TCP (all platforms) or Unix domain sockets (non-Windows). Loads optional .env from the working directory via laserpants/dotenv-cpp (same idea as kbot).

Image stack (not libvips)

Processing uses stb (nothings/stb): stb_image, stb_image_resize2 (stbir_*), and stb_image_write. There is no libvips / glib dependency in this package.

Concurrency and defaults

Area Behavior
HTTP serve Uses cpp-httplibs default thread pool: CPPHTTPLIB_THREAD_POOL_COUNT is max(8, hardware_concurrency() - 1) when hardware concurrency is known (see httplib.h in the fetched dependency). There is no extra app-level job queue or MEDIA_* concurrency cap yet—each accepted request runs resize on the pool.
IPC ipc One JSON line per accepted connection; the handler replies with one line and returns (sequential per socket). For parallel work, open multiple connections or run multiple processes.
CLI resize Single invocation, single file pair.

CLI defaults (see src/main.cpp):

  • serve: --host 127.0.0.1, --port 8080
  • ipc: --host 127.0.0.1, --port 9333, or --unix <path> (Unix only)

REST

  • GET /health{"ok":true,"service":"media-img"}
  • POST /v1/resize — JSON body: input, output (paths), optional max_width, max_height, format (png / jpg / jpeg)

IPC — one line per request, newline-terminated JSON:

  • Success: {"ok":true}
  • Failure: {"ok":false,"error":"..."}

Prerequisites

Requirement Notes
CMake ≥ 3.20
C++ compiler C++17 (MSVC, GCC, Clang)
Git For FetchContent dependencies
Node.js Optional; for integration tests (npm run test:media) — Node 18+ recommended (fetch, AbortSignal.timeout)

Quick start (build)

From packages/media/cpp:

npm install   # optional if you only use cmake
npm run build:release

Artifacts: dist/media-img (or dist/media-img.exe on Windows).

cmake --preset release
cmake --build --preset release

Presets

Preset Role
dev Debug build
release Release build

CLI overview

media-img --help
media-img -v,--version
Subcommand Description
resize <input> <output> Resize file on disk (--max-width, --max-height, --format)
serve HTTP server (--host, -p/--port)
ipc JSON-line IPC: TCP (--host, --port) or --unix (Unix only)
kbot ... Forwards extra arguments to KBOT_EXE (optional AI / kbot workflows; build kbot separately)

Example:

set KBOT_EXE=C:\path\to\kbot.exe
media-img kbot ai --prompt "hello"

Integration tests (REST + IPC)

Tests mirror the style of kbot orchestrators (e.g. spawn binary, wait for listen, talk over TCP): see orchestrator/test-media.mjs. They cover:

  • REST: GET /health, POST /v1/resize (PNG + JPEG output), error path for missing input
  • IPC TCP: line JSON request/response, error path
  • IPC Unix: same protocol on a Unix socket (skipped on Windows — use TCP there)
npm run build:release
npm run generate:assets   # if tests/assets PNGs are missing
npm run test:media
Script Purpose
npm run test:media REST + IPC (TCP + Unix where supported)
npm run test:media:rest --rest-only
npm run test:media:ipc --ipc-only

Env: MEDIA_IMG_TEST_UNIX — Unix socket path for the UDS test (default /tmp/media-img-test.sock).

Test fixtures

Under tests/assets/:

  • build-fixtures.mjs — Generates RGB PNGs (no extra npm deps). Run: node tests/assets/build-fixtures.mjs (also exposed as npm run generate:assets).
  • Additional subfolders (in/, out/, *_webp/, watermark samples, etc.) are available for broader manual or future tests.
  • ROLLOUT.md — Phased rollout notes.
  • polymech.md — Broader Polymech context (if present).

License

See LICENSE in this directory when present.