This commit is contained in:
2026-07-02 19:20:41 +01:00
parent c1da3abe7d
commit a577296608
13 changed files with 476 additions and 66 deletions
+18 -9
View File
@@ -12,7 +12,7 @@ use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream},
};
use tracing::warn;
use tracing::{debug, info, warn};
use crate::{
StreamSession,
@@ -72,8 +72,10 @@ impl Rtmp {
} = self;
tokio::spawn(async move {
loop {
let (socket, _) = listener.accept().await.unwrap();
let (socket, peer_addr) = listener.accept().await.unwrap();
info!(%peer_addr, "RTMP connection accepted");
let (mut session, mut socket) = Rtmp::handshake(socket).await.unwrap();
info!(%peer_addr, "RTMP handshake complete");
let (mut video_tx, mut video_rx) = broadcast::<Arc<VideoFrame>>(32);
let (mut audio_tx, mut audio_rx) = broadcast::<Arc<OpusAudioFrame>>(32);
// video_rx.cycle
@@ -87,10 +89,13 @@ impl Rtmp {
let stream_sessions = stream_sessions.clone();
tokio::spawn(async move {
// let video_rx = video_rx;
loop {
let mut buf = [0u8; 4096];
let n = socket.read(&mut buf).await.unwrap();
if n == 0 {
debug!("RTMP connection closed by peer");
return;
}
let events = session.handle_input(&buf[..n]).unwrap();
// Blankly using it, so it doesnt drop
video_rx.is_closed();
@@ -105,6 +110,7 @@ impl Rtmp {
ServerSessionEvent::ConnectionRequested {
request_id, ..
} => {
debug!("RTMP ConnectionRequested, accepting");
let reply = session.accept_request(request_id).unwrap();
write_outbound(&mut socket, reply).await;
}
@@ -113,6 +119,7 @@ impl Rtmp {
stream_key,
..
} => {
info!(stream_key = %stream_key, "publish stream requested");
let key = entity::stream_key::Entity::find_by_key(
&db,
&stream_key,
@@ -122,6 +129,7 @@ impl Rtmp {
let key = if let Ok(Some(key)) = key {
key
} else {
warn!(stream_key = %stream_key, "stream key not found, rejecting");
let reply = session
.reject_request(
request_id,
@@ -140,6 +148,7 @@ impl Rtmp {
.await
.unwrap();
if already_live.is_some() {
warn!(stream_key_id = key.id, label = %key.label, "stream key already live, rejecting duplicate publish");
let reply = session
.reject_request(
request_id,
@@ -151,6 +160,7 @@ impl Rtmp {
break;
}
info!(stream_key_id = key.id, label = %key.label, "stream started");
stream_sessions.insert(
key.id,
StreamSession {
@@ -175,6 +185,7 @@ impl Rtmp {
stream_key,
..
} => {
info!(stream_key = %stream_key, "publish stream finished");
let key = entity::stream_key::Entity::find_by_key(
&db,
&stream_key,
@@ -214,12 +225,10 @@ impl Rtmp {
..
} => {
// Consume the non-Send error before any await point.
let opus_frames: Vec<_> = aac_parser
.parse(&data, timestamp.value)
.ok()
.flatten()
.map(|f| audio_proc.encode(f))
.unwrap_or_default();
let opus_frames: Vec<_> = match aac_parser.parse(&data, timestamp.value) {
Err(e) => { warn!("AAC parse error: {}", e); vec![] }
Ok(frame) => frame.map(|f| audio_proc.encode(f)).unwrap_or_default(),
};
for frame in opus_frames {
audio_tx.broadcast(Arc::new(frame)).await.ok();
}