Create producers from source ids

This commit is contained in:
Meta Vens 2026-05-19 12:40:39 +01:00
commit cd9b3fd059
7 changed files with 57 additions and 0 deletions

View file

@ -22,6 +22,9 @@ The reservoir ABI is intentionally small:
- `localcast_hash_source_id` is the stable FNV-1a source-id hash used before
creating producers. Empty or null source ids hash to zero and are rejected by
producer creation.
- `localcast_producer_create_for_source` is the preferred producer constructor
for adapters that have source-id bytes; it hashes through the same native
authority before creating the producer.
- `LocalcastSampleHandle` is sample metadata plus a `payload_handle` and
provenance flags. Live samples must have `flags == 0`; diagnostic or unknown
flagged samples are rejected by the live reservoir.

View file

@ -174,6 +174,12 @@ LocalcastProducer *localcast_producer_create(
uint64_t sensor_id_hash,
uint64_t initial_sequence
);
LocalcastProducer *localcast_producer_create_for_source(
uint32_t sample_kind,
const uint8_t *source_id_bytes,
size_t source_id_byte_len,
uint64_t initial_sequence
);
void localcast_producer_destroy(LocalcastProducer *producer);
uint64_t localcast_producer_next_sequence(const LocalcastProducer *producer);

View file

@ -750,6 +750,17 @@ pub extern "C" fn localcast_producer_create(
)))
}
#[unsafe(no_mangle)]
pub extern "C" fn localcast_producer_create_for_source(
sample_kind: u32,
source_id_bytes: *const u8,
source_id_byte_len: usize,
initial_sequence: u64,
) -> *mut LocalcastProducer {
let hash = localcast_hash_source_id(source_id_bytes, source_id_byte_len);
localcast_producer_create(sample_kind, hash, initial_sequence)
}
#[unsafe(no_mangle)]
pub extern "C" fn localcast_producer_destroy(ptr: *mut LocalcastProducer) {
if ptr.is_null() {
@ -1378,6 +1389,8 @@ mod tests {
fn producer_rejects_unknown_kind_null_runtime_and_empty_source() {
assert!(localcast_producer_create(99, 1, 0).is_null());
assert!(localcast_producer_create(0, 0, 0).is_null());
assert!(localcast_producer_create_for_source(0, std::ptr::null(), 4, 0).is_null());
assert!(localcast_producer_create_for_source(0, b"".as_ptr(), 0, 0).is_null());
assert_eq!(0, localcast_producer_next_sequence(std::ptr::null()));
let producer = localcast_producer_create(0, 1, 0);
@ -1405,6 +1418,34 @@ mod tests {
localcast_runtime_destroy(runtime);
}
#[test]
fn producer_can_be_created_from_source_id_bytes() {
let source_id = b"kiyo-primary";
let producer = localcast_producer_create_for_source(
SampleKind::CameraFrame.id(),
source_id.as_ptr(),
source_id.len(),
5,
);
let runtime = localcast_runtime_create(DEFAULT_RESERVOIR_NS);
let mut sample = handle(0, 0, 0, 0, 0);
assert!(!producer.is_null());
assert!(localcast_producer_push(
producer,
runtime,
1_000,
1_010,
90,
&mut sample,
));
assert_eq!(hash_source_id(source_id), sample.sensor_id_hash);
assert_eq!(5, sample.sequence);
localcast_producer_destroy(producer);
localcast_runtime_destroy(runtime);
}
#[test]
fn audio_block_descriptor_is_typed_payload_boundary_not_reservoir_storage() {
let runtime = localcast_runtime_create(DEFAULT_RESERVOIR_NS);