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構成#
| Path | Responsibility |
|---|---|
| crates/erika | Core playback library: Player, Presenter, rendering backends, audio, subtitles, and danmaku. |
| crates/erika_capi | C ABI export layer. include/erika.h is authoritative for all exports. |
| crates/erika_ffmpeg_sys | FFmpeg bindgen bindings. |
| packages/erika_flutter | Flutter plugin covering macOS / iOS / tvOS / Windows / Android / HarmonyOS. |
| packages/erika_ohos | Standalone 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.
| Module | Responsibility |
|---|---|
| core | Player, PlayerConfig, event types, and the RendererBackend trait. |
| playback | Playback state machine, audio master clock, frame scheduler, and VideoDecodePreference. |
| presenter | PresenterRuntime. Ties together the player, renderer, audio, and overlay, and is driven by render_tick. |
| ffmpeg | Demux, decode, resample, and seek. Selection and configuration of the five DecoderBackend backends. |
| audio | The AudioOutputBackend trait, the f32 PCM ring buffer, and the audio clock. |
| renderer | The metal (Apple), d3d11 (Windows), and wgpu (Android, HarmonyOS) backends. pipeline makes the backend-independent color and tone mapping decisions. |
| subtitle | SRT / WebVTT / ASS parsing and libass rendering, embedded and external subtitle tracks. |
| danmaku | Bilibili XML / JSON parsing, DFM+ collision-avoidance layout, and glyph atlas rendering. |
| overlay | Composition timeline, and playback-window management for subtitles and danmaku. |
| source | The MediaSource trait, with two implementations: file and HTTP range. |
| trace / debug_hud | Diagnostics: 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).
| Thread | Responsibility |
|---|---|
| erika-playback | Command loop. Consumes PlaybackCommand, decodes video inline, and manages the state machine. |
| erika-demux | Demux consumer thread. Delivers packets to the playback thread through a bounded queue (capacity 512). |
| erika-danmaku | Generates the danmaku plan. Uses a Mutex + Condvar request model and returns results over an unbounded channel. |
| erika-upscaler-build | Loads ArtCNN weights and builds the backend (Metal path). |
| erika-wasapi-render | Windows 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 point | Host | Rendering ownership | Description |
|---|---|---|---|
| Player | Rust | Host pulls frame data | Full Rust API. Event subscription plus video / audio / subtitle frame subscription. |
| ErikaHandle | C / Swift / Dart FFI | Host pulls frame data | Pull model. The host manages the render loop itself. |
| ErikaPresenterHandle | C / Swift / Dart FFI | Managed by the kernel | Push 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対応環境#
| Platform | Minimum | Decoding | Rendering | Audio | Status |
|---|---|---|---|---|---|
| macOS | 14 | VideoToolbox | Metal | CoreAudio | Available |
| iOS | 16 | VideoToolbox | Metal | AudioQueue | Available |
| tvOS | 13 | VideoToolbox | Metal | AudioQueue | Available |
| Windows | 10 | D3D11VA | Direct3D 11 | WASAPI | Available |
| Android | 8 | MediaCodec / software | wgpu · Vulkan+GLES | AAudio | Available |
| HarmonyOS | API 18 | AVCodec / software | wgpu · Vulkan | OHAudio | Available |
| Linux | FFmpeg 8.x | NVDEC / VA-API | wgpu (Vulkan / EGL) | PulseAudio | Experimental |
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.