Skip to content

Commit d9d1c89

Browse files
committed
impl: add codec metadata in rtmp frame
1 parent 3b1b44a commit d9d1c89

File tree

2 files changed

+6
-27
lines changed

2 files changed

+6
-27
lines changed

moqt-ingest-gateway/src/audio.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ pub struct AacState {
88
audio_specific_config: Option<Vec<u8>>,
99
sample_rate: Option<u32>,
1010
channels: Option<u8>,
11-
config_sent: bool,
1211
}
1312

1413
pub struct AudioFrame {
1514
pub data: Vec<u8>,
1615
pub sample_rate: u32,
1716
pub channels: u8,
18-
pub include_config: bool,
1917
pub audio_specific_config: Vec<u8>,
2018
}
2119

@@ -29,21 +27,17 @@ pub fn pack_audio_chunk_payload(
2927
duration_us: Option<u64>,
3028
sent_at_ms: u64,
3129
) -> Vec<u8> {
32-
let description_b64 = if frame.include_config {
33-
Some(general_purpose::STANDARD.encode(&frame.audio_specific_config))
34-
} else {
35-
None
36-
};
30+
let description_b64 = general_purpose::STANDARD.encode(&frame.audio_specific_config);
3731

3832
let meta = serde_json::json!({
3933
"type": "key",
4034
"timestamp": timestamp_us as i64,
4135
"duration": duration_us.map(|d| d as i64),
4236
"sentAt": sent_at_ms as i64,
43-
"codec": description_b64.as_ref().map(|_| "mp4a.40.2"),
37+
"codec": "mp4a.40.2",
4438
"descriptionBase64": description_b64,
45-
"sampleRate": frame.include_config.then_some(frame.sample_rate as i64),
46-
"channels": frame.include_config.then_some(frame.channels as i64),
39+
"sampleRate": frame.sample_rate as i64,
40+
"channels": frame.channels as i64,
4741
});
4842
let meta_bytes = meta.to_string().into_bytes();
4943
let meta_len = meta_bytes.len() as u32;
@@ -71,7 +65,6 @@ impl AacState {
7165
match packet_type {
7266
0 => {
7367
self.parse_audio_specific_config(payload)?;
74-
self.config_sent = false;
7568
Ok(None)
7669
}
7770
1 => {
@@ -83,13 +76,10 @@ impl AacState {
8376
(Some(sr), Some(ch), Some(asc)) => (sr, ch, asc.clone()),
8477
_ => return Ok(None),
8578
};
86-
let include_config = !self.config_sent;
87-
self.config_sent = true;
8879
Ok(Some(AudioFrame {
8980
data: payload.to_vec(),
9081
sample_rate,
9182
channels,
92-
include_config,
9383
audio_specific_config: asc,
9484
}))
9585
}

moqt-ingest-gateway/src/rtmp/session.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub struct RtmpState {
2222
pub moqt: Option<MoqtManager>,
2323
pub video_states: HashMap<(String, String), AvcState>,
2424
pub audio_states: HashMap<(String, String), AacState>,
25-
pub video_codec_sent: HashMap<(String, String), bool>,
2625
}
2726

2827
impl RtmpState {
@@ -33,7 +32,6 @@ impl RtmpState {
3332
moqt: Some(moqt),
3433
video_states: HashMap::new(),
3534
audio_states: HashMap::new(),
36-
video_codec_sent: HashMap::new(),
3735
}
3836
}
3937
}
@@ -89,8 +87,6 @@ pub async fn handle_event(
8987
println!("[rtmp {label}] publish finished app={app_name} stream={stream_key}");
9088
let (namespace_path, _cleaned_stream_key) =
9189
split_namespace_and_key(&app_name, &stream_key);
92-
let key = (namespace_path.clone(), "video".to_string());
93-
state.video_codec_sent.remove(&key);
9490
let audio_key = (namespace_path, "audio".to_string());
9591
state.audio_states.remove(&audio_key);
9692
}
@@ -199,13 +195,7 @@ pub async fn handle_event(
199195
.duration_since(std::time::UNIX_EPOCH)
200196
.unwrap_or_default()
201197
.as_millis() as u64;
202-
let codec_key = (namespace_path.clone(), track_name.clone());
203-
let already_sent = state
204-
.video_codec_sent
205-
.get(&codec_key)
206-
.copied()
207-
.unwrap_or(false);
208-
let include_codec = frame.is_key && !already_sent;
198+
let include_codec = frame.is_key;
209199
let payload = pack_video_chunk_payload(
210200
frame.is_key,
211201
timestamp_us,
@@ -218,13 +208,12 @@ pub async fn handle_event(
218208
},
219209
None,
220210
);
221-
if frame.is_key && include_codec {
211+
if frame.is_key {
222212
if let Some(codec) = frame.codec.as_deref() {
223213
println!(
224214
"[rtmp {label}] detected video codec from SPS: {codec} ns={:?} track={}",
225215
namespace, track_name
226216
);
227-
state.video_codec_sent.insert(codec_key.clone(), true);
228217
} else {
229218
println!(
230219
"[rtmp {label}] video codec from SPS unavailable ns={:?} track={}",

0 commit comments

Comments
 (0)