Skip to content

Commit 0fa7e59

Browse files
authored
Merge pull request #8 from faern/add-spectrum-analyze-backpressure-handling
Add backpressure handling between camera and spectrum analyzer
2 parents 89300b5 + e376394 commit 0fa7e59

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/camera.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::config::ImageConfig;
22
use crate::{ThreadId, ThreadResult};
3-
use flume::{Receiver, Sender};
3+
use flume::{Receiver, Sender, TrySendError};
44
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgb};
5-
use log::{error, trace};
5+
use log::{error, trace, warn};
66
use nokhwa::CallbackCamera;
77
use nokhwa::pixel_format::RgbFormat;
88
use nokhwa::utils::{
@@ -180,9 +180,15 @@ impl CameraThread {
180180
cfg.window.size.y as u32,
181181
)
182182
.to_image();
183-
if let Err(e) = window_tx.send(window) {
184-
error!("Could not send window: {e}");
185-
return;
183+
match window_tx.try_send(window) {
184+
Ok(_) => {}
185+
Err(TrySendError::Full(_)) => {
186+
warn!("Window buffer full. Dropping frame");
187+
}
188+
Err(TrySendError::Disconnected(_)) => {
189+
error!("Window receiver disconnected. Exiting");
190+
return;
191+
}
186192
};
187193
}
188194
*frame_tx.lock().expect("Mutex poisoned") = Some(frame);

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ fn main() -> eframe::Result {
99
init_logging();
1010

1111
let frame = Arc::new(Mutex::new(None));
12+
13+
// Image buffer holding the last read frame from the camera. Consumed by the GUI to display the camera feed.
1214
let (frame_tx, frame_rx) = (frame.clone(), frame.clone());
13-
let (window_tx, window_rx) = flume::unbounded();
15+
// Channel transporting image buffers from the camera thread to the spectrum calculator thread.
16+
// This is bounded to provide backpressure handling. If the spectrum calculator is slower than the
17+
// framerate of the camera, frames will be dropped instead of filling up the memory.
18+
let (window_tx, window_rx) = flume::bounded(5);
1419
let (spectrum_tx, spectrum_rx) = flume::bounded(1000);
1520
let (config_tx, config_rx) = flume::unbounded();
1621
let (result_tx, result_rx) = flume::unbounded();

0 commit comments

Comments
 (0)