Home/docs/guide/C ABI integration
C ABI integration
C ABI Embedding
C, C++, and other native hosts integrate through erika.h. The interface provides two families of entry points. Standard apps use ErikaPresenterHandle, where the kernel manages rendering and audio. Headless control and lightweight hosts can use ErikaHandle.
Choosing an entry point入口の選択#
| ErikaPresenterHandle (recommended) | ErikaHandle | |
|---|---|---|
| Architecture | Push model. The kernel manages hardware decoding, audio output, the rendering pipeline, subtitles, and danmaku. | Headless control model. Provides the state machine, transport controls, and an event receiver. |
| Host responsibilities | Provide a native surface (CAMetalLayer / HWND / wgpu) and call render_tick from the display clock. | Call playback controls and poll for events. There is no built-in rendering or audio device output. |
| Use cases | Native GUI players, game engine embedding, desktop and mobile apps. | Headless players, backend service control, or basic event bridging. |
| Platform support | macOS / iOS / tvOS / Windows / Android / OpenHarmony. | All platforms. |
Note: The C ABI does not currently export an interface for pulling raw pixel frames. To display video in a native host window, use ErikaPresenterHandle.
Common conventions共通規約#
- Return values. Status-returning functions use
ErikaStatus:ErikaStatus_Ok(0) indicates success andErikaStatus_NoEvent(5) indicates an empty event queue; other statuses indicate errors. For functions that return a pointer, a value, or nothing, check the header for their individual conventions. - Error messages. Some failures set a thread-local error, but not all of them provide a message. The slot may be empty or may retain an earlier value. On the same thread as the failed call, and before making another call, read any available message with
erika_last_error_message()and free a non-null result witherika_string_free(). For example,erika_add_external_subtitlereturns NullPointer directly whenout_track_idis NULL, without writing to the slot. - Ownership. Heap memory behind a
char*returned by liberika belongs to the caller. Free standalone strings witherika_string_free(). Free strings contained in a struct (such asErikaTrackInfo) with the matchingerika_track_info_free(). - Parameter borrowing.
const char*parameters are borrowed only for the duration of the call and must be NUL-terminated UTF-8 strings. - Counted arrays. List queries use the
(buf, capacity, &len)pattern:lenalways receives the actual total count in the stream, while at mostcapacityitems are written tobuf. Bound your iteration bymin(len, capacity)to avoid out-of-bounds access. - Thread safety. A handle has no internal lock. Do not call methods on the same handle concurrently from multiple threads.
Version 0.3.1 adds ErikaOpenOptions.start_position_micros, erika_presenter_set_open_start_position, the natural-end event ErikaEventKind_Ended, erika_presenter_set_subtitle_delay, danmaku-inclusive capture and native frame texture export. Zero-initialized existing structs still start at the beginning; an explicit stop does not emit Ended.
Presenter end-to-end examplePresenter の流れ#
Use ErikaPresenterHandle in a native app to drive the full playback path:
C / C++ native playback integration
#include <stdio.h> #include <stdint.h> #include "erika.h" /* Helper: read and print the thread-local error message */ static void print_last_error(const char *action, ErikaStatus status) { char *msg = erika_last_error_message(); fprintf(stderr, "[liberika Error] %s failed (status=%d): %s\n", action, status, msg ? msg : "unknown"); if (msg) { erika_string_free(msg); } } void play_video(void *metal_layer, int pixel_w, int pixel_h, double scale) { /* 1. Create a Presenter handle */ ErikaPresenterHandle *p = erika_presenter_create(); if (!p) { fprintf(stderr, "Failed to create liberika Presenter\n"); return; } /* 2. Bind a native render surface (Apple CAMetalLayer as an example) */ ErikaStatus st = erika_presenter_attach_metal_layer( p, (uint64_t)(uintptr_t)metal_layer, pixel_w, pixel_h, scale); if (st != ErikaStatus_Ok) { print_last_error("attach_metal_layer", st); erika_presenter_destroy(p); return; } /* 3. Open the media synchronously (probes streams, parses tracks, and enters the Ready state) */ st = erika_presenter_open(p, "https://example.com/video.mp4"); if (st != ErikaStatus_Ok) { print_last_error("open", st); erika_presenter_detach_surface(p); erika_presenter_destroy(p); return; } /* 4. Start asynchronous decoding and playback presentation */ st = erika_presenter_play(p); if (st != ErikaStatus_Ok) { print_last_error("play", st); } /* 5. Display clock callback (typically driven by CADisplayLink / CVDisplayLink) */ double host_time_seconds = 1000.0; ErikaPresenterStats stats; erika_presenter_render_tick(p, host_time_seconds, &stats); /* 6. Consume events (ErikaEvent is a flat struct) */ ErikaEvent ev; while (erika_presenter_poll_event(p, &ev) == ErikaStatus_Ok) { if (ev.kind == ErikaEventKind_StateChanged) { printf("State changed: %d\n", ev.state); } else if (ev.kind == ErikaEventKind_DurationChanged) { printf("Media duration: %lld us\n", (long long)ev.duration_micros); } else if (ev.kind == ErikaEventKind_PositionChanged) { printf("Position: %llu us\n", (unsigned long long)ev.position_micros); } else if (ev.kind == ErikaEventKind_Error) { print_last_error("event_error", ev.status); } } /* 7. Shut down safely: detach the render surface first, then destroy the handle */ erika_presenter_detach_surface(p); erika_presenter_destroy(p); }
Querying media tracks safelyトラック情報取得#
When you query the audio and subtitle track lists, check array bounds:
Query and release track information
ErikaTrackInfo tracks[16]; uintptr_t total_count = 0; /* Fills at most 16 entries; total_count receives the actual number of tracks in the stream */ ErikaStatus st = erika_presenter_tracks(p, tracks, 16, &total_count); if (st == ErikaStatus_Ok) { uintptr_t available = total_count < 16 ? total_count : 16; for (uintptr_t i = 0; i < available; i++) { printf("Track #%lld: %s (language: %s)\n", (long long)tracks[i].id, tracks[i].title ? tracks[i].title : "Untitled", tracks[i].language ? tracks[i].language : "Unknown"); /* Free the heap strings inside the struct */ erika_track_info_free(&tracks[i]); } }
Handle mode (headless control)Handle 概要#
For lightweight cases that need only the state machine and media control, and where liberika does not drive the display or audio, use ErikaHandle:
ErikaHandle control example
ErikaHandle *h = erika_create(); ErikaStatus st = erika_open(h, "https://example.com/video.mp4"); if (st == ErikaStatus_Ok) { erika_play(h); ErikaEvent ev; while (erika_poll_event(h, &ev) == ErikaStatus_Ok) { /* Handle ready and playback events */ } } erika_destroy(h);
HTTP cache#
http_read_ahead_bytes sets how many bytes to read ahead (default 2 MiB). http_back_buffer_bytes sets how much already-played data to keep (default 16 MiB). Omit either field or pass 0 to use its default. You can also set the read-ahead default with ERIKA_HTTP_READAHEAD_BYTES.
Backward seeks within the back buffer reuse data that is already downloaded. High-bitrate video needs a larger budget: keeping about 10 seconds of an 80 Mbit/s video takes about 100 MB. The cache is limited by bytes, so it does not correspond to a fixed number of playback seconds.
C
ErikaOpenOptions options = {0};
options.http_read_ahead_bytes = 8 * 1024 * 1024;
options.http_back_buffer_bytes = 128 * 1024 * 1024;
ErikaStatus status = erika_presenter_open_with_options(
player, "https://example.com/video.mp4", &options);
The header uses the same release version as the kernel. Since 0.1.8, ErikaPresenterConfig includes video_alpha_mode. In 0.2.1, ErikaOpenOptions added the back-buffer fields in previously reserved space; set the remaining reserved fields to zero.