Add native source id hashing
This commit is contained in:
parent
cc468c0270
commit
22f643574e
7 changed files with 60 additions and 3 deletions
|
|
@ -19,6 +19,9 @@ tests, and offline analysis. It does not own dense live fusion or DSP.
|
|||
The reservoir ABI is intentionally small:
|
||||
|
||||
- `LocalcastReservoir` is opaque.
|
||||
- `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.
|
||||
- `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.
|
||||
|
|
|
|||
|
|
@ -94,6 +94,8 @@ typedef struct LocalcastRuntimeStatus {
|
|||
size_t render_packet_count;
|
||||
} LocalcastRuntimeStatus;
|
||||
|
||||
uint64_t localcast_hash_source_id(const uint8_t *bytes, size_t byte_len);
|
||||
|
||||
LocalcastReservoir *localcast_reservoir_create(uint64_t duration_ns);
|
||||
void localcast_reservoir_destroy(LocalcastReservoir *reservoir);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ use std::collections::VecDeque;
|
|||
pub const DEFAULT_RESERVOIR_NS: u64 = 5_000_000_000;
|
||||
pub const LOCALCAST_SAMPLE_FLAG_DIAGNOSTIC: u32 = 1 << 0;
|
||||
pub const LOCALCAST_AUDIO_SAMPLE_FORMAT_F32_INTERLEAVED: u32 = 1;
|
||||
const FNV1A_OFFSET_64: u64 = 14_695_981_039_346_656_037;
|
||||
const FNV1A_PRIME_64: u64 = 1_099_511_628_211;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum SampleKind {
|
||||
|
|
@ -357,6 +359,29 @@ fn is_live_sample(sample: LocalcastSampleHandle) -> bool {
|
|||
sample.flags == 0
|
||||
}
|
||||
|
||||
fn hash_source_id(bytes: &[u8]) -> u64 {
|
||||
if bytes.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
let mut hash = FNV1A_OFFSET_64;
|
||||
for byte in bytes {
|
||||
hash ^= u64::from(*byte);
|
||||
hash = hash.wrapping_mul(FNV1A_PRIME_64);
|
||||
}
|
||||
if hash == 0 { 1 } else { hash }
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn localcast_hash_source_id(bytes: *const u8, byte_len: usize) -> u64 {
|
||||
if byte_len == 0 {
|
||||
return 0;
|
||||
}
|
||||
if bytes.is_null() {
|
||||
return 0;
|
||||
}
|
||||
hash_source_id(unsafe { std::slice::from_raw_parts(bytes, byte_len) })
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn localcast_reservoir_create(duration_ns: u64) -> *mut LocalcastReservoir {
|
||||
Box::into_raw(Box::new(LocalcastReservoir {
|
||||
|
|
@ -923,6 +948,19 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_id_hash_is_stable_and_rejects_empty_sources() {
|
||||
let source = b"kiyo-primary";
|
||||
|
||||
assert_eq!(0, hash_source_id(b""));
|
||||
assert_eq!(0, localcast_hash_source_id(std::ptr::null(), source.len()));
|
||||
assert_eq!(
|
||||
hash_source_id(source),
|
||||
localcast_hash_source_id(source.as_ptr(), source.len())
|
||||
);
|
||||
assert_ne!(hash_source_id(source), hash_source_id(b"kiyo-secondary"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rolling_reservoir_expires_every_kind_from_one_edge() {
|
||||
let mut reservoir = RollingReservoir::new(DEFAULT_RESERVOIR_NS);
|
||||
|
|
|
|||
Loading…
Reference in a new issue