fix: rtmp can timeout now
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use std::{error::Error, sync::Arc};
|
use std::{error::Error, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use async_broadcast::broadcast;
|
use async_broadcast::broadcast;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
@@ -12,7 +12,7 @@ use sea_orm::{DatabaseConnection, IntoActiveModel, sqlx::types::chrono::Local};
|
|||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
time::Instant,
|
time::{Instant, timeout},
|
||||||
};
|
};
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
@@ -116,6 +116,7 @@ impl Rtmp {
|
|||||||
video_tx.set_overflow(true);
|
video_tx.set_overflow(true);
|
||||||
audio_tx.set_overflow(true);
|
audio_tx.set_overflow(true);
|
||||||
let mut parser: Option<Box<dyn CodecParser>> = None;
|
let mut parser: Option<Box<dyn CodecParser>> = None;
|
||||||
|
let mut stream_id: Option<i32> = None;
|
||||||
let mut aac_parser = AACParser::new();
|
let mut aac_parser = AACParser::new();
|
||||||
let mut audio_proc = AudioProcesser::new();
|
let mut audio_proc = AudioProcesser::new();
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
@@ -143,22 +144,27 @@ impl Rtmp {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut buf = [0u8; 4096];
|
let mut buf = [0u8; 4096];
|
||||||
let n = match socket.read(&mut buf).await {
|
const IDLE_TIMEOUT: Duration = Duration::from_secs(15);
|
||||||
Ok(0) => {
|
let n = match timeout(IDLE_TIMEOUT, socket.read(&mut buf)).await {
|
||||||
|
Ok(Ok(0)) => {
|
||||||
debug!("RTMP connection closed by peer");
|
debug!("RTMP connection closed by peer");
|
||||||
if let Some(id) = current_stream_key_id {
|
if let Some(id) = current_stream_key_id {
|
||||||
cleanup(id).await;
|
cleanup(id).await;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Ok(n) => n,
|
Ok(Ok(n)) => n,
|
||||||
Err(e) => {
|
Ok(Err(e)) => {
|
||||||
warn!("RTMP read error: {e}");
|
warn!("RTMP read error: {e}");
|
||||||
if let Some(id) = current_stream_key_id {
|
if let Some(id) = current_stream_key_id {
|
||||||
cleanup(id).await;
|
cleanup(id).await;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Err(_) => {
|
||||||
|
warn!("RTMP timed out (15 sec no packets) id: {:?}", stream_id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let events = match session.handle_input(&buf[..n]) {
|
let events = match session.handle_input(&buf[..n]) {
|
||||||
Ok(e) => e,
|
Ok(e) => e,
|
||||||
@@ -247,6 +253,7 @@ impl Rtmp {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
stream_id = Some(key.id);
|
||||||
write_outbound(&mut socket, reply).await;
|
write_outbound(&mut socket, reply).await;
|
||||||
}
|
}
|
||||||
ServerSessionEvent::PublishStreamFinished {
|
ServerSessionEvent::PublishStreamFinished {
|
||||||
|
|||||||
Reference in New Issue
Block a user