File tree Expand file tree Collapse file tree 2 files changed +17
-6
lines changed
Expand file tree Collapse file tree 2 files changed +17
-6
lines changed Original file line number Diff line number Diff line change 11use crate :: config:: ImageConfig ;
22use crate :: { ThreadId , ThreadResult } ;
3- use flume:: { Receiver , Sender } ;
3+ use flume:: { Receiver , Sender , TrySendError } ;
44use image:: { DynamicImage , GenericImageView , ImageBuffer , Rgb } ;
5- use log:: { error, trace} ;
5+ use log:: { error, trace, warn } ;
66use nokhwa:: CallbackCamera ;
77use nokhwa:: pixel_format:: RgbFormat ;
88use 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) ;
Original file line number Diff line number Diff 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 ( ) ;
You can’t perform that action at this time.
0 commit comments