vomit prevention from stream finish with clients still connected

This commit is contained in:
2026-07-06 13:01:49 +01:00
parent 528dff7e8a
commit 7d61d7b425
3 changed files with 20 additions and 13 deletions
Generated
+1 -1
View File
@@ -2866,7 +2866,7 @@ dependencies = [
[[package]] [[package]]
name = "server" name = "server"
version = "0.1.1" version = "0.2.0"
dependencies = [ dependencies = [
"argon2", "argon2",
"async-broadcast", "async-broadcast",
+3 -7
View File
@@ -46,8 +46,8 @@ pub struct AppState {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum StreamCodec { pub enum StreamCodec {
H264, H264,
H265, H265, // Very shity suport
AV1, // Unsupported AV1,
} }
pub struct StreamSession { pub struct StreamSession {
@@ -61,11 +61,7 @@ pub struct StreamSession {
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
let env_filter = EnvFilter::builder() let env_filter = EnvFilter::builder().from_env().unwrap();
.with_default_directive(LevelFilter::DEBUG.into())
.from_env()
// .parse("")
.unwrap();
tracing_subscriber::fmt().with_env_filter(env_filter).init(); tracing_subscriber::fmt().with_env_filter(env_filter).init();
+16 -5
View File
@@ -11,6 +11,7 @@ use std::{
use tokio::{ use tokio::{
net::UdpSocket, net::UdpSocket,
sync::mpsc::{Receiver, Sender}, sync::mpsc::{Receiver, Sender},
time::sleep,
}; };
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
@@ -137,7 +138,8 @@ impl Webrtc {
}); });
if !video_has_pts { if !video_has_pts {
warn!( warn!(
request_id, stream_id, request_id,
stream_id,
"no video codec negotiated — browser likely doesn't support {:?}; rejecting offer", "no video codec negotiated — browser likely doesn't support {:?}; rejecting offer",
stream_codec stream_codec
); );
@@ -222,7 +224,10 @@ impl Webrtc {
video_pt = Some(params.pt()); video_pt = Some(params.pt());
video_mid = Some(ma.mid); video_mid = Some(ma.mid);
} else { } else {
warn!(stream_id, "video writer has no payload params — codec not negotiated"); warn!(
stream_id,
"video writer has no payload params — codec not negotiated"
);
} }
} else { } else {
warn!(stream_id, mid = ?ma.mid, "no writer for video mid"); warn!(stream_id, mid = ?ma.mid, "no writer for video mid");
@@ -301,13 +306,16 @@ impl Webrtc {
for _ in 0..8 { for _ in 0..8 {
match stream.try_recv() { match stream.try_recv() {
Ok(frame) => { Ok(frame) => {
debug!(stream_id, is_keyframe = frame.is_keyframe, ts = frame.timestamp_ms, bytes = frame.data.len(), "WebRTC received video frame");
if !saw_keyframe { if !saw_keyframe {
if !frame.is_keyframe { if !frame.is_keyframe {
continue; continue;
} }
saw_keyframe = true; saw_keyframe = true;
info!(stream_id, ts = frame.timestamp_ms, "first keyframe — starting RTP send"); info!(
stream_id,
ts = frame.timestamp_ms,
"first keyframe — starting RTP send"
);
} }
let now = Instant::now(); let now = Instant::now();
let rtp_time = let rtp_time =
@@ -316,7 +324,9 @@ impl Webrtc {
(Some(pt), Some(writer)) => { (Some(pt), Some(writer)) => {
match writer.write(pt, now, rtp_time, frame.data.to_vec()) { match writer.write(pt, now, rtp_time, frame.data.to_vec()) {
Ok(_) => wrote_any = true, Ok(_) => wrote_any = true,
Err(e) => error!(stream_id, "video RTP write error: {:?}", e), Err(e) => {
error!(stream_id, "video RTP write error: {:?}", e)
}
} }
} }
_ => warn!( _ => warn!(
@@ -330,6 +340,7 @@ impl Webrtc {
Err(async_broadcast::TryRecvError::Empty) => break, Err(async_broadcast::TryRecvError::Empty) => break,
Err(async_broadcast::TryRecvError::Closed) => { Err(async_broadcast::TryRecvError::Closed) => {
warn!("video channel closed, stream ended"); warn!("video channel closed, stream ended");
sleep(Duration::from_secs(1)).await;
break; break;
} }
Err(async_broadcast::TryRecvError::Overflowed(_)) => { Err(async_broadcast::TryRecvError::Overflowed(_)) => {