Home/docs/kernel/Architecture overview

Architecture overview

Architecture

liberika is implemented in Rust. Everything from demuxing and decoding to GPU presentation, audio, subtitles, and danmaku happens inside the kernel. The host provides a render surface and sends playback commands; it does not take part in the rendering pipeline.

Repository layout#

PathResponsibility
crates/erikaCore playback library: Player, Presenter, rendering backends, audio, subtitles, and danmaku.
crates/erika_capiC ABI export layer. include/erika.h is authoritative for all exports.
crates/erika_ffmpeg_sysFFmpeg bindgen bindings.
packages/erika_flutterFlutter plugin covering macOS / iOS / tvOS / Windows / Android / HarmonyOS.
packages/erika_ohosStandalone OpenHarmony ArkTS / OHPM native SDK. Provides ErikaPlayer and an XComponent surface bridge.
examples/Validation, smoke-test, and demo programs. macos_native_demo and windows_native_demo are end-to-end examples.
xtask/Native dependency build orchestration: deps plan / fetch / status / build.

Modules#

The modules under crates/erika/src/ are divided by responsibility. Platform-specific code is gated with #[cfg(target_os = …)] and concentrated in apple.rs, windows.rs, android.rs, ohos.rs, and their subdirectories.

ModuleResponsibility
corePlayer, PlayerConfig, event types, and the RendererBackend trait.
playbackPlayback state machine, audio master clock, frame scheduler, and VideoDecodePreference.
presenterPresenterRuntime. Ties together the player, renderer, audio, and overlay, and is driven by render_tick.
ffmpegDemux, decode, resample, and seek. Selection and configuration of the five DecoderBackend backends.
audioThe AudioOutputBackend trait, the f32 PCM ring buffer, and the audio clock.
rendererThe metal (Apple), d3d11 (Windows), and wgpu (Android, HarmonyOS) backends. pipeline makes the backend-independent color and tone mapping decisions.
subtitleSRT / WebVTT / ASS parsing and libass rendering, embedded and external subtitle tracks.
danmakuBilibili XML / JSON parsing, DFM+ collision-avoidance layout, and glyph atlas rendering.
overlayComposition timeline, and playback-window management for subtitles and danmaku.
sourceThe MediaSource trait, with two implementations: file and HTTP range.
trace / debug_hudDiagnostics: environment-variable switches and the HUD overlay.

Data flow#

The playback path runs from the media source to the render surface:

playback data flow

MediaSource (file / HTTP range)
  → demux (ffmpeg)
  → decode (DecoderBackend: hardware / software)
  → frame queue (video / audio / subtitle)
  → PresenterRuntime::render_tick(host_time)
  → RenderFrameContext { media_time, overlay, danmaku, output_size }
  → RendererBackend::render_current_frame
  → surface (Metal / D3D11 / wgpu)

State and events travel over a separate crossbeam channel. Player state changes, duration, tracks, buffering, errors, and similar information are published to the event channel, and the host consumes them through poll_event or, on the Rust side, an event subscription.

Time base. Audio output is the master clock. Video presentation is quantized to vsync and aligned to the audio clock; for details, see Clock and scheduling.

Threading model#

The kernel keeps 5 resident threads. On some platforms, audio output also runs in a system callback context (the CoreAudio callback and the WASAPI render thread).

ThreadResponsibility
erika-playbackCommand loop. Consumes PlaybackCommand, decodes video inline, and manages the state machine.
erika-demuxDemux consumer thread. Delivers packets to the playback thread through a bounded queue (capacity 512).
erika-danmakuGenerates the danmaku plan. Uses a Mutex + Condvar request model and returns results over an unbounded channel.
erika-upscaler-buildLoads ArtCNN weights and builds the backend (Metal path).
erika-wasapi-renderWindows WASAPI audio output thread.

The C ABI layer defines the boundary between host threads and the kernel: a handle has no internal synchronization and must not be called concurrently from multiple threads.

Three entry points#

liberika exposes three ways to use it, distinguished by host language and rendering ownership.

Entry pointHostRendering ownershipDescription
PlayerRustHost pulls frame dataFull Rust API. Event subscription plus video / audio / subtitle frame subscription.
ErikaHandleC / Swift / Dart FFIHost pulls frame dataPull model. The host manages the render loop itself.
ErikaPresenterHandleC / Swift / Dart FFIManaged by the kernelPush model. The host provides the surface and calls render_tick every frame.

All three entry points share one internal implementation. Every exported function in the C ABI layer is wrapped in catch_unwind; a panic maps to ErikaStatus_Panic and never crosses the FFI boundary.

Backend abstractions#

Platform differences are confined behind three traits, which keeps the Presenter and the render loop platform-independent.

  • RendererBackend (core.rs) — Surface attachment and resizing, render_current_frame, upload_player_frame, and screenshots. Implemented for Metal, D3D11, and wgpu.
  • AudioOutputBackend (audio.rs) — Configuration, playback control, push(PcmAudioFrame), and clock snapshots. Implemented for CoreAudio / AudioQueue / WASAPI / AAudio / OHAudio.
  • DecoderBackend (ffmpeg.rs) — Five decoding backends: software, VideoToolbox, D3D11VA, MediaCodec, and AVCodec.

Selection rules: RendererBackendPreference (platform-native or wgpu fallback) determines the rendering backend, and VideoDecodePreference picks the decoding backend using per-platform defaults.

Platform coverage#

PlatformMinimumDecodingRenderingAudioStatus
macOS14VideoToolboxMetalCoreAudioAvailable
iOS16VideoToolboxMetalAudioQueueAvailable
tvOS13VideoToolboxMetalAudioQueueAvailable
Windows10D3D11VADirect3D 11WASAPIAvailable
Android8MediaCodec / softwarewgpu · Vulkan+GLESAAudioAvailable
HarmonyOSAPI 18AVCodec / softwarewgpu · VulkanOHAudioAvailable
LinuxFFmpeg 8.xNVDEC / VA-APIwgpu (Vulkan / EGL)PulseAudioExperimental

The versions in the platform table are the feature baseline. The actual deployment targets at build time are macOS 11.0 / iOS 13.0; see Building and dependencies.

Browser backend · 0.3.0#

Web is a standalone JavaScript Player interface. FFmpeg WASM reads and demuxes inside a Worker and hands compressed packets to WebCodecs; libass and the Rust DFM engine handle subtitles and danmaku; WebGL2 composites the result; and Web Audio provides output and the playback clock. It does not use the native Presenter C ABI, and it does not include desktop HDR or ArtCNN. For deployment and supported environments, see Web / WASM integration.

liberika v0.3.8 Section Kernel License MPL-2.0