Move the media wire envelope where both ends can reach it
A media record travels wrapped in cultnet.document_put_raw.v0 with a schema id, a record key and provenance. Both ends have to agree on that exactly, and until now only Muninn had it: the producer was Rust in one repository and the consumer was 6,515 lines of C++ in another, each with its own reading of the same bytes. Ratatoskr could not decode a real stream without it. Two things change in the move rather than after it. The producer namespace is now the caller's. The Muninn implementation hardcoded "muninn-media:" into every message id and "muninn.media" into every tag, so a second producer would have had to label its traffic as Muninn's to be understood. `MediaWireProvenance` carries it instead, and a test asserts the envelope names no producer the caller did not supply. The four hand-written wire payload structs are gone. Each restated a record's entire field list as a positional tuple, in order, kept in step by discipline alone — and existed for one reason: `#[serde(with = "serde_bytes")]` on the payload, which cannot be applied to a DatabaseEntry struct because the derive does not read serde attributes. Now that the records carry `#[cultcache(key = N, bytes)]` the derive emits bin itself, so the records serialize directly. That last part is a wire claim, so it is tested rather than asserted: `the_derive_reproduces_the_hand_written_wire_payload_byte_for_byte` reconstructs the legacy struct and compares output byte for byte. It matches, which makes the deletion a simplification instead of a silent break. Record key shapes move here too. A consumer parses them to tell a chunk from its siblings and a retransmission from a new frame, so they are contract and belong beside the records rather than in whichever producer wrote them first. Muninn's copies are not removed yet; that is the other half of this move. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
2ba404cc0b
commit
a3e6877945
3 changed files with 404 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ mod contracts;
|
|||
mod idunn_deployment_brake;
|
||||
mod idunn_lifecycle_brake;
|
||||
mod media_stream_contracts;
|
||||
mod media_stream_wire;
|
||||
mod provider_health_contracts;
|
||||
mod runtime_authority_contracts;
|
||||
mod service_identity;
|
||||
|
|
@ -21,6 +22,7 @@ pub use contracts::*;
|
|||
pub use idunn_deployment_brake::*;
|
||||
pub use idunn_lifecycle_brake::*;
|
||||
pub use media_stream_contracts::*;
|
||||
pub use media_stream_wire::*;
|
||||
pub use provider_health_contracts::*;
|
||||
pub use runtime_authority_contracts::*;
|
||||
pub use service_identity::*;
|
||||
|
|
|
|||
185
packages/cultnet-rs/src/media_stream_wire.rs
Normal file
185
packages/cultnet-rs/src/media_stream_wire.rs
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
//! How a CultMesh Media Stream record travels: the envelope both ends must
|
||||
//! agree on.
|
||||
//!
|
||||
//! The records in `media_stream_contracts` describe media. This describes how
|
||||
//! one reaches a peer — wrapped in `cultnet.document_put_raw.v0` with a schema
|
||||
//! id, a record key, and provenance. It lives here because a producer and a
|
||||
//! consumer that never link each other still have to agree on it exactly, and
|
||||
//! the last time they did not, one end was Rust and the other was 6,515 lines
|
||||
//! of C++ that had drifted.
|
||||
//!
|
||||
//! # No producer's name belongs in a shared envelope
|
||||
//!
|
||||
//! The Muninn implementation this replaces hardcoded `"muninn-media:"` into
|
||||
//! every message id and `"muninn.media"` into every tag, so a second producer
|
||||
//! would have had to label its traffic as Muninn's. The producer namespace is a
|
||||
//! parameter here: see [`MediaWireProvenance`].
|
||||
//!
|
||||
//! # Record keys are addresses, not decoration
|
||||
//!
|
||||
//! A key identifies one piece of media within a session, so a consumer can tell
|
||||
//! a retransmission from a new frame and a chunk from its siblings. The shapes
|
||||
//! are fixed by this module rather than by each producer, because a consumer
|
||||
//! parses them.
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
use crate::contracts::{
|
||||
CultNetMessage, CultNetRawDocumentRecord, CultNetRawPayloadEncoding, CultNetWireContract,
|
||||
decode_cultnet_message_from_slice, encode_cultnet_message_to_vec,
|
||||
};
|
||||
use crate::media_stream_contracts::{
|
||||
GAMECULT_MEDIA_AUDIO_PACKET_SCHEMA, GAMECULT_MEDIA_RECEIVER_FEEDBACK_SCHEMA,
|
||||
GAMECULT_MEDIA_VIDEO_ACCESS_UNIT_SCHEMA, GAMECULT_MEDIA_VIDEO_PARITY_SHARD_SCHEMA,
|
||||
GameCultMediaAudioPacketRecord, GameCultMediaReceiverFeedbackRecord,
|
||||
GameCultMediaVideoAccessUnitRecord, GameCultMediaVideoParityShardRecord,
|
||||
};
|
||||
|
||||
/// The CultNet channel media rides.
|
||||
pub const GAMECULT_MEDIA_CHANNEL: &str = "media";
|
||||
|
||||
/// One media record on its way somewhere.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum GameCultMediaWireRecord {
|
||||
Video(GameCultMediaVideoAccessUnitRecord),
|
||||
VideoParity(GameCultMediaVideoParityShardRecord),
|
||||
Audio(GameCultMediaAudioPacketRecord),
|
||||
Feedback(GameCultMediaReceiverFeedbackRecord),
|
||||
}
|
||||
|
||||
/// Who is sending, and when. `producer` namespaces message ids and tags so a
|
||||
/// stream can be attributed without the envelope knowing any producer by name.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct MediaWireProvenance<'a> {
|
||||
pub stored_at: &'a str,
|
||||
pub runtime_id: &'a str,
|
||||
pub role: &'a str,
|
||||
pub producer: &'a str,
|
||||
}
|
||||
|
||||
impl MediaWireProvenance<'_> {
|
||||
fn validate(&self) -> Result<()> {
|
||||
for (name, value) in [
|
||||
("stored_at", self.stored_at),
|
||||
("runtime_id", self.runtime_id),
|
||||
("role", self.role),
|
||||
("producer", self.producer),
|
||||
] {
|
||||
if value.is_empty() {
|
||||
return Err(anyhow!("media wire provenance {name} must be non-empty"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl GameCultMediaWireRecord {
|
||||
pub fn schema_id(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Video(_) => GAMECULT_MEDIA_VIDEO_ACCESS_UNIT_SCHEMA,
|
||||
Self::VideoParity(_) => GAMECULT_MEDIA_VIDEO_PARITY_SHARD_SCHEMA,
|
||||
Self::Audio(_) => GAMECULT_MEDIA_AUDIO_PACKET_SCHEMA,
|
||||
Self::Feedback(_) => GAMECULT_MEDIA_RECEIVER_FEEDBACK_SCHEMA,
|
||||
}
|
||||
}
|
||||
|
||||
/// Addresses this record within its session.
|
||||
pub fn record_key(&self) -> String {
|
||||
match self {
|
||||
Self::Video(record) => format!(
|
||||
"{}:{}:video:{}:{}",
|
||||
record.stream_id, record.session_id, record.frame_id, record.chunk_index
|
||||
),
|
||||
Self::VideoParity(record) => format!(
|
||||
"{}:{}:video-parity:{}:{}",
|
||||
record.stream_id, record.session_id, record.frame_id, record.parity_index
|
||||
),
|
||||
Self::Audio(record) => format!(
|
||||
"{}:{}:audio:{}",
|
||||
record.stream_id, record.session_id, record.packet_id
|
||||
),
|
||||
Self::Feedback(record) => format!(
|
||||
"{}:{}:feedback:{}",
|
||||
record.stream_id, record.session_id, record.receiver_id
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// The record's own MessagePack form.
|
||||
///
|
||||
/// Serializing the record directly is only correct because the payload
|
||||
/// fields carry `#[cultcache(key = N, bytes)]`. Muninn previously mirrored
|
||||
/// each record into a parallel tuple struct — every field restated, in
|
||||
/// order, by hand — solely so `#[serde(with = "serde_bytes")]` could be
|
||||
/// applied to the payload, because the `DatabaseEntry` derive does not read
|
||||
/// serde attributes. Those shadow structs are gone; the derive emits `bin`
|
||||
/// itself now. Do not reintroduce one.
|
||||
fn payload_bytes(&self) -> Result<Vec<u8>> {
|
||||
match self {
|
||||
Self::Video(record) => rmp_serde::to_vec(record),
|
||||
Self::VideoParity(record) => rmp_serde::to_vec(record),
|
||||
Self::Audio(record) => rmp_serde::to_vec(record),
|
||||
Self::Feedback(record) => rmp_serde::to_vec(record),
|
||||
}
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps a media record for the wire.
|
||||
pub fn encode_media_wire_record(
|
||||
record: &GameCultMediaWireRecord,
|
||||
provenance: MediaWireProvenance<'_>,
|
||||
) -> Result<Vec<u8>> {
|
||||
provenance.validate()?;
|
||||
|
||||
let schema_id = record.schema_id();
|
||||
let record_key = record.record_key();
|
||||
let message = CultNetMessage::DocumentPutRaw {
|
||||
message_id: format!(
|
||||
"{}-media:{}:{}",
|
||||
provenance.producer,
|
||||
schema_id,
|
||||
record_key.replace(':', "-")
|
||||
),
|
||||
document: CultNetRawDocumentRecord {
|
||||
schema_id: schema_id.to_string(),
|
||||
record_key,
|
||||
stored_at: provenance.stored_at.to_string(),
|
||||
payload_encoding: CultNetRawPayloadEncoding::Messagepack,
|
||||
payload: record.payload_bytes()?,
|
||||
source_runtime_id: Some(provenance.runtime_id.to_string()),
|
||||
source_agent_id: None,
|
||||
source_role: Some(provenance.role.to_string()),
|
||||
tags: Some(vec![format!("{}.media", provenance.producer)]),
|
||||
},
|
||||
};
|
||||
|
||||
encode_cultnet_message_to_vec(&message, CultNetWireContract::CultNetSchemaV0).map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Unwraps a media record from the wire.
|
||||
pub fn decode_media_wire_record(payload: &[u8]) -> Result<GameCultMediaWireRecord> {
|
||||
let message = decode_cultnet_message_from_slice(payload, CultNetWireContract::CultNetSchemaV0)?;
|
||||
let CultNetMessage::DocumentPutRaw { document, .. } = message else {
|
||||
return Err(anyhow!("expected cultnet.document_put_raw.v0"));
|
||||
};
|
||||
if document.payload_encoding != CultNetRawPayloadEncoding::Messagepack {
|
||||
return Err(anyhow!("media document payload must be MessagePack"));
|
||||
}
|
||||
|
||||
match document.schema_id.as_str() {
|
||||
GAMECULT_MEDIA_VIDEO_ACCESS_UNIT_SCHEMA => Ok(GameCultMediaWireRecord::Video(
|
||||
rmp_serde::from_slice(&document.payload)?,
|
||||
)),
|
||||
GAMECULT_MEDIA_VIDEO_PARITY_SHARD_SCHEMA => Ok(GameCultMediaWireRecord::VideoParity(
|
||||
rmp_serde::from_slice(&document.payload)?,
|
||||
)),
|
||||
GAMECULT_MEDIA_AUDIO_PACKET_SCHEMA => Ok(GameCultMediaWireRecord::Audio(
|
||||
rmp_serde::from_slice(&document.payload)?,
|
||||
)),
|
||||
GAMECULT_MEDIA_RECEIVER_FEEDBACK_SCHEMA => Ok(GameCultMediaWireRecord::Feedback(
|
||||
rmp_serde::from_slice(&document.payload)?,
|
||||
)),
|
||||
other => Err(anyhow!("unknown media schema {other}")),
|
||||
}
|
||||
}
|
||||
217
packages/cultnet-rs/tests/media_stream_wire.rs
Normal file
217
packages/cultnet-rs/tests/media_stream_wire.rs
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
//! The envelope, and one proof that removing Muninn's shadow structs changed
|
||||
//! nothing on the wire.
|
||||
|
||||
use cultnet_rs::{
|
||||
GAMECULT_MEDIA_CHANNEL, GameCultMediaAudioPacketRecord, GameCultMediaReceiverFeedbackRecord,
|
||||
GameCultMediaVideoAccessUnitRecord, GameCultMediaVideoParityShardRecord,
|
||||
GameCultMediaWireRecord, MediaWireProvenance, decode_media_wire_record,
|
||||
encode_media_wire_record,
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
fn provenance() -> MediaWireProvenance<'static> {
|
||||
MediaWireProvenance {
|
||||
stored_at: "unix:1000",
|
||||
runtime_id: "raven",
|
||||
role: "muninn.media",
|
||||
producer: "muninn",
|
||||
}
|
||||
}
|
||||
|
||||
fn access_unit() -> GameCultMediaVideoAccessUnitRecord {
|
||||
GameCultMediaVideoAccessUnitRecord {
|
||||
stream_id: "raven-primary-av".to_string(),
|
||||
session_id: "session-1".to_string(),
|
||||
frame_id: 4242,
|
||||
codec: "h264".to_string(),
|
||||
pts_ticks: 90_000,
|
||||
duration_ticks: 3_000,
|
||||
timebase_num: 1,
|
||||
timebase_den: 90_000,
|
||||
keyframe: true,
|
||||
dependency_frame_id: None,
|
||||
deadline_ticks: 108_000,
|
||||
chunk_index: 2,
|
||||
chunk_count: 5,
|
||||
payload: (0u8..=255).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Muninn's `VideoAccessUnitWirePayload`, reproduced exactly as it was.
|
||||
///
|
||||
/// It existed only so `#[serde(with = "serde_bytes")]` could reach the payload,
|
||||
/// because the `DatabaseEntry` derive ignores serde attributes. Every other
|
||||
/// field was restated by hand, in order, and had to stay in step with the record
|
||||
/// by discipline alone.
|
||||
#[derive(Serialize)]
|
||||
struct LegacyVideoAccessUnitWirePayload<'a>(
|
||||
&'a str,
|
||||
&'a str,
|
||||
u64,
|
||||
&'a str,
|
||||
i64,
|
||||
u32,
|
||||
u32,
|
||||
u32,
|
||||
bool,
|
||||
Option<u64>,
|
||||
i64,
|
||||
u16,
|
||||
u16,
|
||||
#[serde(with = "serde_bytes")] &'a [u8],
|
||||
);
|
||||
|
||||
/// If these differ, deleting the shadow structs was a wire break rather than a
|
||||
/// simplification, and every existing consumer would need to move in step.
|
||||
#[test]
|
||||
fn the_derive_reproduces_the_hand_written_wire_payload_byte_for_byte() {
|
||||
let record = access_unit();
|
||||
let legacy = LegacyVideoAccessUnitWirePayload(
|
||||
&record.stream_id,
|
||||
&record.session_id,
|
||||
record.frame_id,
|
||||
&record.codec,
|
||||
record.pts_ticks,
|
||||
record.duration_ticks,
|
||||
record.timebase_num,
|
||||
record.timebase_den,
|
||||
record.keyframe,
|
||||
record.dependency_frame_id,
|
||||
record.deadline_ticks,
|
||||
record.chunk_index,
|
||||
record.chunk_count,
|
||||
&record.payload,
|
||||
);
|
||||
|
||||
let from_derive = rmp_serde::to_vec(&record).expect("record encodes");
|
||||
let from_shadow = rmp_serde::to_vec(&legacy).expect("shadow struct encodes");
|
||||
|
||||
assert_eq!(
|
||||
from_derive, from_shadow,
|
||||
"the `bytes` attribute must reproduce what serde_bytes produced by hand"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_video_record_round_trips_through_the_envelope() {
|
||||
let record = GameCultMediaWireRecord::Video(access_unit());
|
||||
let wire = encode_media_wire_record(&record, provenance()).expect("encodes");
|
||||
assert_eq!(decode_media_wire_record(&wire).expect("decodes"), record);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_variant_round_trips() {
|
||||
let variants = vec![
|
||||
GameCultMediaWireRecord::Video(access_unit()),
|
||||
GameCultMediaWireRecord::VideoParity(GameCultMediaVideoParityShardRecord {
|
||||
stream_id: "s".to_string(),
|
||||
session_id: "x".to_string(),
|
||||
frame_id: 1,
|
||||
codec: "h264".to_string(),
|
||||
pts_ticks: 0,
|
||||
duration_ticks: 1,
|
||||
timebase_num: 1,
|
||||
timebase_den: 90_000,
|
||||
keyframe: false,
|
||||
dependency_frame_id: Some(0),
|
||||
deadline_ticks: 1,
|
||||
chunk_count: 4,
|
||||
parity_index: 1,
|
||||
parity_count: 2,
|
||||
chunk_payload_bytes: 848,
|
||||
last_chunk_payload_bytes: 400,
|
||||
payload: vec![1, 2, 3],
|
||||
}),
|
||||
GameCultMediaWireRecord::Audio(GameCultMediaAudioPacketRecord {
|
||||
stream_id: "s".to_string(),
|
||||
session_id: "x".to_string(),
|
||||
packet_id: 9,
|
||||
codec: "opus".to_string(),
|
||||
pts_ticks: 0,
|
||||
duration_ticks: 960,
|
||||
timebase_num: 1,
|
||||
timebase_den: 48_000,
|
||||
deadline_ticks: 1,
|
||||
payload: vec![4, 5, 6],
|
||||
}),
|
||||
GameCultMediaWireRecord::Feedback(GameCultMediaReceiverFeedbackRecord {
|
||||
stream_id: "s".to_string(),
|
||||
session_id: "x".to_string(),
|
||||
receiver_id: "starfire-obs".to_string(),
|
||||
highest_decodable_frame_id: Some(41),
|
||||
missing_frame_ids: vec![42],
|
||||
late_frame_ids: vec![43],
|
||||
requested_keyframe: true,
|
||||
jitter_us: 500,
|
||||
decode_queue_us: 2_000,
|
||||
observed_at: "unix:1000".to_string(),
|
||||
missing_video_chunk_keys: vec!["42:1".to_string()],
|
||||
}),
|
||||
];
|
||||
|
||||
for record in variants {
|
||||
let wire = encode_media_wire_record(&record, provenance()).expect("encodes");
|
||||
assert_eq!(
|
||||
decode_media_wire_record(&wire).expect("decodes"),
|
||||
record,
|
||||
"variant {} did not survive the envelope",
|
||||
record.schema_id()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Record keys address one piece of media within a session. A consumer parses
|
||||
/// them, so their shape is contract.
|
||||
#[test]
|
||||
fn record_keys_distinguish_chunks_parity_and_senders() {
|
||||
let mut second_chunk = access_unit();
|
||||
second_chunk.chunk_index = 3;
|
||||
|
||||
let first = GameCultMediaWireRecord::Video(access_unit()).record_key();
|
||||
let second = GameCultMediaWireRecord::Video(second_chunk).record_key();
|
||||
assert_ne!(first, second, "chunks of one frame must not share a key");
|
||||
assert!(first.contains(":video:"), "got {first}");
|
||||
}
|
||||
|
||||
/// A shared envelope must not label another producer's traffic as Muninn's.
|
||||
#[test]
|
||||
fn the_producer_namespace_is_the_callers_not_the_envelopes() {
|
||||
let record = GameCultMediaWireRecord::Video(access_unit());
|
||||
let theirs = MediaWireProvenance {
|
||||
stored_at: "unix:1000",
|
||||
runtime_id: "nightwing",
|
||||
role: "brokkr.media",
|
||||
producer: "brokkr",
|
||||
};
|
||||
|
||||
let wire = encode_media_wire_record(&record, theirs).expect("encodes");
|
||||
let text = String::from_utf8_lossy(&wire);
|
||||
assert!(text.contains("brokkr-media:"), "message id names the producer");
|
||||
assert!(text.contains("brokkr.media"), "tag names the producer");
|
||||
assert!(
|
||||
!text.contains("muninn"),
|
||||
"the envelope must not name a producer the caller did not supply"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provenance_must_be_complete() {
|
||||
let record = GameCultMediaWireRecord::Video(access_unit());
|
||||
for blank in ["stored_at", "runtime_id", "role", "producer"] {
|
||||
let mut incomplete = provenance();
|
||||
match blank {
|
||||
"stored_at" => incomplete.stored_at = "",
|
||||
"runtime_id" => incomplete.runtime_id = "",
|
||||
"role" => incomplete.role = "",
|
||||
_ => incomplete.producer = "",
|
||||
}
|
||||
let error = encode_media_wire_record(&record, incomplete)
|
||||
.expect_err("empty provenance must be refused");
|
||||
assert!(error.to_string().contains(blank), "got {error}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_media_channel_is_named_once() {
|
||||
assert_eq!(GAMECULT_MEDIA_CHANNEL, "media");
|
||||
}
|
||||
Loading…
Reference in a new issue