fix(webrtc): use MediaAdded event to discover video mid instead of hardcoded iteration (closes #11)

This commit is contained in:
dailz
2026-06-06 21:16:55 +08:00
parent fc4733ffe8
commit 1beaea8088

View File

@@ -311,18 +311,14 @@ impl WebRtcInner {
} }
fn discover_video_params(&mut self) { fn discover_video_params(&mut self) {
for s in ["0", "1", "2", "3"] { let mid = match self.video_mid {
let mid: Mid = s.into(); Some(m) => m,
if let Some(media) = self.rtc.media(mid) { None => {
if media.kind() == MediaKind::Video { tracing::warn!("discover_video_params: no video_mid yet");
tracing::info!("Found video media: mid={mid}"); return;
self.video_mid = Some(mid);
break;
} }
} };
} self.video_pt = None;
if let Some(mid) = self.video_mid {
if let Some(writer) = self.rtc.writer(mid) { if let Some(writer) = self.rtc.writer(mid) {
for pp in writer.payload_params() { for pp in writer.payload_params() {
tracing::debug!("Codec: pt={:?} spec={:?}", pp.pt(), pp.spec()); tracing::debug!("Codec: pt={:?} spec={:?}", pp.pt(), pp.spec());
@@ -333,6 +329,8 @@ impl WebRtcInner {
} }
} }
} }
if self.video_pt.is_none() {
tracing::warn!("discover_video_params: no H.264 codec found for mid={mid}");
} }
} }
@@ -360,7 +358,18 @@ impl WebRtcInner {
return Ok(true); return Ok(true);
} }
Event::MediaAdded(ma) => { Event::MediaAdded(ma) => {
tracing::info!("Media added: mid={:?}", ma.mid); tracing::info!("Media added: mid={} kind={:?}", ma.mid, ma.kind);
if ma.kind == MediaKind::Video {
if let Some(media) = self.rtc.media(ma.mid) {
if media.direction().is_sending()
&& self.video_mid.is_none()
{
self.video_mid = Some(ma.mid);
tracing::info!("Captured video mid: {}", ma.mid);
self.discover_video_params();
}
}
}
} }
_ => { _ => {
tracing::debug!("WebRTC event: {:?}", e); tracing::debug!("WebRTC event: {:?}", e);