chore: clean up and optimisation
This commit is contained in:
+22
-11
@@ -3,12 +3,12 @@ use std::{error::Error, sync::Arc, time::Duration};
|
||||
use async_broadcast::broadcast;
|
||||
use chrono::Utc;
|
||||
use dashmap::DashMap;
|
||||
use entity::stream_session;
|
||||
use entity::{stream_session, users};
|
||||
use rml_rtmp::{
|
||||
handshake::{Handshake, HandshakeProcessResult, PeerType},
|
||||
sessions::{ServerSession, ServerSessionConfig, ServerSessionEvent, ServerSessionResult},
|
||||
};
|
||||
use sea_orm::{DatabaseConnection, IntoActiveModel, sqlx::types::chrono::Local};
|
||||
use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel, sqlx::types::chrono::Local};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
net::{TcpListener, TcpStream},
|
||||
@@ -35,10 +35,11 @@ pub struct Rtmp {
|
||||
async fn write_outbound(socket: &mut TcpStream, results: Vec<ServerSessionResult>) {
|
||||
for r in results {
|
||||
if let ServerSessionResult::OutboundResponse(p) = r
|
||||
&& let Err(e) = socket.write_all(&p.bytes).await {
|
||||
warn!("RTMP write error: {e}");
|
||||
return;
|
||||
}
|
||||
&& let Err(e) = socket.write_all(&p.bytes).await
|
||||
{
|
||||
warn!("RTMP write error: {e}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,8 +127,12 @@ impl Rtmp {
|
||||
}
|
||||
};
|
||||
info!(%peer_addr, "RTMP handshake complete");
|
||||
let (mut video_tx, video_rx) = broadcast::<Arc<VideoFrame>>(32);
|
||||
let (mut audio_tx, audio_rx) = broadcast::<Arc<OpusAudioFrame>>(32);
|
||||
// Ring sized by TIME, not frame count: 512 frames ≈ 5.7s @ 90fps,
|
||||
// so transient ingest/send stalls can't overflow it (overflow drops
|
||||
// the oldest frames and forces a freeze until the next keyframe).
|
||||
// ponytail: fixed 512; per-stream dynamic sizing if memory ever matters.
|
||||
let (mut video_tx, video_rx) = broadcast::<Arc<VideoFrame>>(512);
|
||||
let (mut audio_tx, audio_rx) = broadcast::<Arc<OpusAudioFrame>>(512);
|
||||
// video_rx.cycle
|
||||
|
||||
video_tx.set_overflow(true);
|
||||
@@ -295,12 +300,18 @@ impl Rtmp {
|
||||
}
|
||||
|
||||
info!(stream_key_id = key.id, label = %key.label, "stream started");
|
||||
let user = users::Entity::find_by_id(key.user_id)
|
||||
.one(&db)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
current_stream_key_id = Some(key.id);
|
||||
stream_sessions.insert(
|
||||
key.id,
|
||||
StreamSession {
|
||||
stream_key_id: key.id,
|
||||
stream_key_label: key.label,
|
||||
stream_key_user: user.username,
|
||||
frame_channel: video_tx.clone(),
|
||||
audio_channel: audio_tx.clone(),
|
||||
codec: None,
|
||||
@@ -364,9 +375,9 @@ impl Rtmp {
|
||||
if let Some(id) = current_stream_key_id
|
||||
&& let Some(mut session) =
|
||||
stream_sessions.get_mut(&id)
|
||||
{
|
||||
session.codec = Some(codec.clone());
|
||||
}
|
||||
{
|
||||
session.codec = Some(codec.clone());
|
||||
}
|
||||
codec_stamped = true;
|
||||
}
|
||||
match codec {
|
||||
|
||||
Reference in New Issue
Block a user