More ways to generate an IO::Disconnect error

This commit is contained in:
QuantumEntangledAndy committed 2023-10-02 12:15:57 +07:00
1 parent f13887a369
commit 3d0f8b794e
5 files changed
+69 -14

No files matched your search

+3
View File
@@ -746,6 +746,9 @@ pub struct LinkType {
/// The Snap contains the binary jpeg image details /// The Snap contains the binary jpeg image details
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct Snap { pub struct Snap {
/// The snap xml version. Observed values "1.1"
#[yaserde(attribute)]
pub version: String,
#[yaserde(rename = "channelId")] #[yaserde(rename = "channelId")]
/// The channel id to get the snapshot from /// The channel id to get the snapshot from
pub channel_id: u8, pub channel_id: u8,
+1
View File
@@ -25,6 +25,7 @@ impl BcCamera {
}), }),
payload: Some(BcPayloads::BcXml(BcXml { payload: Some(BcPayloads::BcXml(BcXml {
snap: Some(Snap { snap: Some(Snap {
version: "1.1".to_string(),
channel_id: self.channel_id, channel_id: self.channel_id,
logic_channel: Some(self.channel_id), logic_channel: Some(self.channel_id),
time: 0, time: 0,
+27 -4
View File
@@ -128,17 +128,37 @@ impl NeoInstance {
camera = None; camera = None;
None None
}, },
Ok(neolink_core::Error::Io(e)) => {
log::debug!("Std IO Error");
// Check if the inner error is the Other type and then the discomnect
let is_dropped = e.get_ref().is_some_and(|e| {
log::debug!("Std IO Error: Inner: {:?}", e);
matches!(e.downcast_ref::<neolink_core::Error>(),
Some(neolink_core::Error::DroppedConnection) | Some(neolink_core::Error::TimeoutDisconnected)
)
});
if is_dropped {
// Retry is a None
camera = None;
None
} else {
Some(Err(e.into()))
}
}
Ok(e) => Some(Err(e.into())), Ok(e) => Some(Err(e.into())),
Err(e) => { Err(e) => {
// Check if it is an io error // Check if it is an io error
log::debug!("Other Error: {:?}", e);
match e.downcast::<std::io::Error>() { match e.downcast::<std::io::Error>() {
Ok(e) => { Ok(e) => {
// Check if the inner error is the Other type and then the discomnect log::debug!("Std IO Error");
let is_dropped = e.get_ref().is_some_and(|e| // Check if the inner error is the Other type and then the discomnect
let is_dropped = e.get_ref().is_some_and(|e| {
log::debug!("Std IO Error: Inner: {:?}", e);
matches!(e.downcast_ref::<neolink_core::Error>(), matches!(e.downcast_ref::<neolink_core::Error>(),
Some(neolink_core::Error::DroppedConnection) | Some(neolink_core::Error::TimeoutDisconnected) Some(neolink_core::Error::DroppedConnection) | Some(neolink_core::Error::TimeoutDisconnected)
) )
); });
if is_dropped { if is_dropped {
// Retry is a None // Retry is a None
camera = None; camera = None;
@@ -147,7 +167,10 @@ impl NeoInstance {
Some(Err(e.into())) Some(Err(e.into()))
} }
}, },
Err(e) =>Some(Err(e)) Err(e) => {
log::debug!("Other Error: {:?}", e);
Some(Err(e))
}
} }
}, },
} }
+5 -1
View File
@@ -93,7 +93,11 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
let mut buffer = File::create(file_path).await?; let mut buffer = File::create(file_path).await?;
let jpeg_data = camera let jpeg_data = camera
.run_task(|camera| Box::pin(async move { Ok(camera.get_snapshot().await?) })) .run_task(|camera| Box::pin(async move { Ok(camera.get_snapshot().await?) }))
.await?; .await;
if jpeg_data.is_err() {
log::debug!("jpeg_data: {:?}", jpeg_data);
}
let jpeg_data = jpeg_data?;
buffer.write_all(jpeg_data.as_slice()).await?; buffer.write_all(jpeg_data.as_slice()).await?;
} }
+33 -9
View File
@@ -67,7 +67,7 @@ use tokio::{
watch::channel as watch, watch::channel as watch,
}, },
task::JoinSet, task::JoinSet,
time::{interval, sleep, Duration}, time::{interval, sleep, Duration, Instant},
}; };
use tokio_stream::wrappers::IntervalStream; use tokio_stream::wrappers::IntervalStream;
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt}; use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
@@ -1099,6 +1099,22 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
let mut rt = Duration::ZERO; let mut rt = Duration::ZERO;
let appsrc = app.clone(); let appsrc = app.clone();
let (tx, mut rx) = mpsc(150);
let thread_appsrc = appsrc.clone();
tokio::task::spawn(async move {
while let Some((time, buf)) = rx.recv().await {
let now = Instant::now();
if now < time {
sleep(time - now).await;
}
thread_appsrc
.push_buffer(buf)
.map(|_| ())
.map_err(|_| anyhow!("Could not push buffer to appsrc"))?;
}
AnyResult::Ok(())
});
while let Some(Ok(data)) = data_rx.next().await { while let Some(Ok(data)) = data_rx.next().await {
check_live(app)?; // Stop if appsrc is dropped check_live(app)?; // Stop if appsrc is dropped
// Cache the last runtime // Cache the last runtime
@@ -1127,8 +1143,14 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
// Sync ft to rt if > 1500ms difference // Sync ft to rt if > 1500ms difference
const MAX_DELTA_T: Duration = Duration::from_millis(1500); const MAX_DELTA_T: Duration = Duration::from_millis(1500);
let mut push_at = if ft > rt {
Instant::now() + (ft - rt)
} else {
Instant::now()
};
let delta_t = if rt > ft { rt - ft } else { ft - rt }; let delta_t = if rt > ft { rt - ft } else { ft - rt };
if delta_t > MAX_DELTA_T { if delta_t > MAX_DELTA_T {
push_at = Instant::now();
ft = rt; ft = rt;
} }
@@ -1146,10 +1168,8 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
gst_buf gst_buf
}; };
appsrc let send = (push_at, buf);
.push_buffer(buf) tx.send(send).await?;
.map(|_| ())
.map_err(|_| anyhow!("Could not push buffer to appsrc"))?;
} }
} }
} }
@@ -1226,7 +1246,7 @@ fn build_unknown(bin: &Element) -> Result<()> {
} }
fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> { fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = stream_config.bitrate * 15 / 8; let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}"); log::debug!("buffer_size: {buffer_size}");
let bin = bin let bin = bin
.clone() .clone()
@@ -1260,7 +1280,7 @@ fn build_h264(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
} }
fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> { fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = stream_config.bitrate * 15 / 8; let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}"); log::debug!("buffer_size: {buffer_size}");
let bin = bin let bin = bin
.clone() .clone()
@@ -1293,7 +1313,7 @@ fn build_h265(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
} }
fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> { fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = stream_config.bitrate * 15 / 8; let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}"); log::debug!("buffer_size: {buffer_size}");
let bin = bin let bin = bin
.clone() .clone()
@@ -1334,7 +1354,7 @@ fn build_aac(bin: &Element, stream_config: &StreamConfig) -> Result<AppSrc> {
} }
fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> Result<AppSrc> { fn build_adpcm(bin: &Element, block_size: u32, stream_config: &StreamConfig) -> Result<AppSrc> {
let buffer_size = stream_config.bitrate * 15 / 8; let buffer_size = buffer_size(stream_config.bitrate);
log::debug!("buffer_size: {buffer_size}"); log::debug!("buffer_size: {buffer_size}");
let bin = bin let bin = bin
.clone() .clone()
@@ -1468,3 +1488,7 @@ fn make_queue(name: &str, buffer_size: u32) -> AnyResult<Element> {
.map_err(|_| anyhow!("Cannot convert bin"))?; .map_err(|_| anyhow!("Cannot convert bin"))?;
Ok(bin) Ok(bin)
} }
fn buffer_size(bitrate: u32) -> u32 {
std::cmp::max(bitrate * 15u32 / 8u32, 4u32 * 1024u32 * 1024u32)
}