/* * splb.h — scorpiox-scorpioplayer-ffmpeg-ios-android-playback-engine-linux-backend-c-egl-pipewire * * The Linux backend for the `sppe_` playback core: GPU video sink (EGL + GLES, * YUV -> RGB in the fragment shader) and a PipeWire audio sink that owns the * master clock. * * Layering (ADR-0001, `…-ffmpeg-ios-android-playback-engine/docs/ADR-0001-one-core-per-platform.md`): * * sppe_ core (separate repo) this backend * demux / decode upload decoded planes to GL textures * master clock + A/V sync PipeWire sink position == the clock * drop / repeat decisions YUV -> RGB on the GPU * subtitle decode + render RGBA overlay draw * seek / track selection (nothing — those are the core's) * * Nothing here decides WHEN a frame is due, which stream plays, or how far a * seek lands. Those arrive as vtable calls with the arguments already fixed. * * ZERO hard link dependencies. EGL, GLESv2 and libpipewire are resolved with * dlopen() at first use, so: * - the shell's own EGLDisplay/EGLContext can be handed in (GTK4 GtkGLArea, * Qt, a raw wl_egl_window, X11 — anything), * - the same .so starts on a box with no GPU, no PipeWire daemon, or a * different soname, and reports honestly what it could not open, * - cross-building linux-aarch64 needs no aarch64 system libraries. * * Backend ABI: 2 since v1.1.0 — only the AUDIO entries of the v2 tail are * filled: `audio_configure_ex` (HDMI bitstream AC-3 / E-AC-3 / DTS / TrueHD as * IEC 61937 over PipeWire's iec958 format, PCM fallback on every refusal) and * `audio_discontinuity`. Config keys: * "spdif": "" (default: PCM only) | "auto" (HDMI ELD) | "ac3,eac3,..." * "iec_rate": "transport" (default) | "codec" — what PipeWire's iec958 * `rate` carries (192000 vs 48000 for E-AC-3) * "iec_wait_ms": how long to wait for the graph to accept (default 1500) * "abi": 1 to install on a v1.0.0 core (the tail is nulled) * Still no dmabuf export, no display-refresh pacing, no zero-copy claim (ADR-0001 §9: * the shipped FFmpeg has zero generic hwaccels, so hw_probe returns 0 for * every codec and hardware decode arrives as NV12 software frames from * FFmpeg's stateful V4L2 M2M decoders). * * Threading: one instance is used from one thread at a time (the core's rule). * PipeWire runs its own thread inside libpipewire; the only data shared with it * is the audio ring, guarded by a mutex. * * SPDX-License-Identifier: MIT */ #ifndef SPLB_H #define SPLB_H #include #include #ifdef __cplusplus extern "C" { #endif #if defined(_WIN32) # if defined(SPLB_BUILD) # define SPLB_API __declspec(dllexport) # else # define SPLB_API __declspec(dllimport) # endif #else # define SPLB_API __attribute__((visibility("default"))) #endif #define SPLB_VERSION "1.1.0" /* Error codes are the core's, so a shell never has to translate two enums. */ #define SPLB_OK 0 #define SPLB_AGAIN 1 #define SPLB_EOF 2 #define SPLB_ERR_TRANSPORT -1 #define SPLB_ERR_FORMAT -2 #define SPLB_ERR_CODEC -3 #define SPLB_ERR_ARG -4 #define SPLB_ERR_OOM -5 #define SPLB_ERR_NOT_FOUND -6 #define SPLB_ERR_SEEK -7 #define SPLB_ERR_STATE -8 #define SPLB_ERR_UNSUPPORTED -9 #define SPLB_ERR_CLOSED -10 #define SPLB_ERR_IO -11 #define SPLB_ERR_HW -12 /* Opaque instance. Created by splb_create, freed by splb_destroy. */ typedef struct splb splb_t; /* ============================================================ lifecycle ==== */ SPLB_API const char *splb_version(void); /* * `config_json` is optional (NULL == "{}"). Recognised keys: * "video_w", "video_h" initial FBO size (default 1280x720) * "audio_rate", "audio_ch" default 48000 / 2 * "audio_node" PipeWire target node name (default: default sink) * "audio_latency_ms" requested target latency (default 60) * "gl" "auto" (default) | "off" — "off" never dlopens EGL * "audio" "auto" (default) | "virtual" — "virtual" never dlopens PipeWire * "log" "quiet" | "info" (default) * Malformed JSON is SPLB_ERR_ARG (same policy as sppe_set_config: a typo must * not silently become defaults). */ SPLB_API splb_t *splb_create(const char *config_json); SPLB_API void splb_destroy(splb_t *b); /* ================================================================== gl ===== */ /* * Bring up (or adopt) the GL renderer. * * `json` keys — every one optional: * "display": an EGLDisplay the shell already owns * "context": an EGLContext to render with (shared ok) * "surface": an EGLSurface to blit the final quad to * "own_display" / "own_context" / "own_surface" (default: own whatever was * not handed in; an adopted object is never destroyed) * "width", "height" FBO size when no surface is used * "es": 1|2|3 GLES major version requested (default 3, * falling back to 2) * "vsync": 0|1 eglSwapInterval for an adopted surface * * With no display it creates a surfaceless EGL display (EGL_PLATFORM_SURFACELESS_MESA) * and renders into an FBO of width x height — the headless mode the tests use. * * Returns SPLB_OK, or SPLB_ERR_HW when no EGL/GLES could be loaded at all * (the backend then still counts frames; splb_gl_info_json says why). */ SPLB_API int splb_gl_init(splb_t *b, const char *json); SPLB_API void splb_gl_shutdown(splb_t *b); /* renderer/vendor/version/extensions, FBO size, whether it is offscreen, and * the honest reason when GL is absent. */ SPLB_API const char *splb_gl_info_json(splb_t *b); /* Resize the offscreen target (ignored when the shell owns the surface). */ SPLB_API int splb_gl_resize(splb_t *b, int w, int h); /* * Read the last rendered picture back as tightly packed RGBA (bottom-up, GL * order). `cap` must be >= w*h*4. Returns the byte count, or <0. This is the * only way a headless test can see pixels, and it is cheap enough to call per * frame on llvmpipe at 640x360. */ SPLB_API int splb_gl_read_rgba(splb_t *b, uint8_t *dst, int cap, int *w, int *h); /* FNV-1a of the last splb_gl_read_rgba() result (0 = nothing read yet). */ SPLB_API unsigned splb_gl_last_crc(splb_t *b); /* =============================================================== audio ===== */ /* * Open the PipeWire playback sink. `json` keys: * "rate", "channels", "node", "latency_ms", "clock" ("sink" (default) | * "virtual" | "off"). * * "sink" — PipeWire; the stream position IS the master clock. * "virtual" — no PipeWire call at all: writes are accounted and the clock * advances with the wall clock. This is what a box with no sound * server gets, and it keeps the video moving (never freezes on a * missing device). * "off" — audio sink disabled entirely; the core then falls back to its own * monotonic clock. */ SPLB_API int splb_audio_init(splb_t *b, const char *json); SPLB_API void splb_audio_shutdown(splb_t *b); /* mode, rate, channels, quantum, node id, latency frames, clock ticks, and the * reason when PipeWire was not reachable. */ SPLB_API const char *splb_audio_info_json(splb_t *b); /* The bitstream codecs the connected HDMI sink advertises in its ELD * (/proc/asound/cardN/eld#N): "ac3,eac3,dts,truehd" or "" when no HDMI sink / * no valid ELD. What "spdif":"auto" uses. Engine-owned string. */ SPLB_API const char *splb_audio_hdmi_codecs(splb_t *b); /* Freeze / resume the sink (and therefore the master clock). A shell calls this * next to sppe_pause()/sppe_play(): the core cannot pause a PipeWire stream, and * an active stream keeps advancing pw_time while paused (every frame "late" on * resume). PipeWire: pw_stream_set_active; virtual: frozen by construction. */ SPLB_API int splb_audio_set_paused(splb_t *b, int paused); /* The clock the core sees (100 ns ticks), or -1 before the first buffer. */ SPLB_API int64_t splb_audio_clock_ticks(splb_t *b); /* ============================================================= overlay ===== */ /* The last subtitle overlay: geometry, push/clear counters, FNV-1a of the RGBA. */ SPLB_API const char *splb_overlay_info_json(splb_t *b); /* Copy the newest overlay pixels (tightly packed RGBA); returns the byte count. */ SPLB_API int splb_overlay_copy_rgba(splb_t *b, uint8_t *dst, int cap, int *w, int *h, int *x, int *y); /* ============================================================ the vtable === */ /* * Fill `out` with the `sppe_backend` bound to this instance, exactly as the * core's `sppe.h` declares it. `out` must be a per-instance struct (the core * stores the pointer, not a copy) — pass the one inside your shell, or use * splb_backend_ptr(). Returns the abi written (2, or 1 with cfg "abi":1), or * <0 on a bad arg. */ SPLB_API int splb_backend(splb_t *b, void *out_sppe_backend); /* The instance's own vtable storage; install this pointer directly: * sppe_install_backend(e, splb_backend_ptr(b)); */ SPLB_API const void *splb_backend_ptr(splb_t *b); /* ===================================================== deferred present === */ /* * Config "present":"deferred" (v1.1.0). For a shell whose PLAYBACK thread owns * the core while the GL context lives on the UI thread (GTK4 GtkGLArea: its * context is current only inside the "render" signal). video_present then * COPIES the frame into one pending slot (newest wins) and draws nothing; the * shell's render handler calls splb_gl_present_pending() after * splb_gl_init({"display","context","framebuffer":0}). * * Returns 1 = a new frame drawn, 2 = the same frame redrawn (a GtkGLArea must * draw on every render signal or it shows black), 0 = nothing queued yet, <0 = * GL error. Thread-safe against video_present. */ SPLB_API int splb_gl_present_pending(splb_t *b); /* Bumped every time video_present queues a frame: poll it to decide whether to * queue a render (gtk_gl_area_queue_render) from a timer / idle callback. */ SPLB_API uint64_t splb_pending_serial(splb_t *b); /* {"deferred","queued","drawn","redrawn","replaced",...}: `replaced` = frames the * core presented that the screen never showed (the UI thread was too slow). */ SPLB_API const char *splb_pending_info_json(splb_t *b); /* ============================================================== counters === */ /* * Everything measured, as JSON: * frames_presented, frames_hw, frames_sw, unsupported_fmt, gl_failures, * last_w, last_h, last_pix_fmt, last_pts_ticks, upload_bytes, * cpu_ms_total/max/mean (CPU time per frame inside video_present), * audio_writes, audio_frames, audio_flushes, audio_underruns, * clock_ticks, clock_source, overlay_pushes, overlay_clears, * hw_probes, first_pts_ticks, last_pts_seen_ticks * * cpu_ms is measured with CLOCK_THREAD_CPUTIME_ID around the texture upload + * draw; it is the number that says whether the Pi5 can afford this sink. */ SPLB_API const char *splb_stats_json(splb_t *b); SPLB_API void splb_stats_reset(splb_t *b); /* 0 for every codec in this build — see splb_hw_info_json() for why. */ SPLB_API int splb_hw_probe_all(splb_t *b); SPLB_API const char *splb_hw_info_json(splb_t *b); /* --------------------------------------------------------- per-frame log ==== */ /* * The last 256 frames the sink was HANDED, each with the FNV-1a of the source * planes and (when attached) of the GL readback. This is the seam a headless * differential test uses: run the same file through the core twice — once with * the core's fake backend, once with this one — and compare the pts lists and * the picture hashes without either side knowing about the other. * * splb_flog_set(0) disables the source hash (it costs a pass over the planes). */ SPLB_API void splb_flog_set(splb_t *b, int on); SPLB_API int splb_frame_count(splb_t *b); SPLB_API const char *splb_frame_json(splb_t *b, int ordinal); /* Attach the CRC of the last splb_gl_read_rgba() to the newest record. */ SPLB_API void splb_flog_attach_gl(splb_t *b); /* ================================================================ errors === */ /* IntPtr-only on the C# side: a const char* return marshaled to `string` * aborts the Linux .NET runtime (see the .console repo). */ SPLB_API const char *splb_last_error(splb_t *b); SPLB_API int splb_last_error_code(splb_t *b); #ifdef __cplusplus } #endif #endif /* SPLB_H */