1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
//! Main binary for the Geek szitman supercamera
use clap::Parser;
use geek_szitman_supercamera::{Error, SuperCamera, video};
use tracing::{error, info, warn};
use tracing_subscriber::{fmt, EnvFilter};
#[derive(Parser)]
#[command(
name = "geek-szitman-supercamera",
about = "Rust implementation of Geek szitman supercamera endoscope viewer",
version,
author
)]
struct Cli {
/// Enable debug logging
#[arg(short, long)]
debug: bool,
/// Enable verbose logging
#[arg(short, long)]
verbose: bool,
/// Video backend to use
#[arg(short, long, value_enum, default_value = "pipewire")]
backend: BackendChoice,
/// Output directory for saved frames
#[arg(short, long, default_value = "pics")]
output_dir: String,
/// Frame rate hint
#[arg(short, long, default_value = "30")]
fps: u32,
/// Exit automatically after N seconds (0 = run until Ctrl+C)
#[arg(long, default_value = "0")]
timeout_seconds: u64,
}
#[derive(Clone, clap::ValueEnum)]
enum BackendChoice {
PipeWire,
V4L2,
Stdout,
}
impl std::fmt::Display for BackendChoice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BackendChoice::PipeWire => write!(f, "pipewire"),
BackendChoice::V4L2 => write!(f, "v4l2"),
BackendChoice::Stdout => write!(f, "stdout"),
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse command line arguments
let cli = Cli::parse();
// Initialize logging
init_logging(cli.debug, cli.verbose)?;
info!("Starting Geek szitman supercamera viewer");
info!("Backend: {}", cli.backend);
info!("Output directory: {}", cli.output_dir);
info!("Frame rate hint: {} fps", cli.fps);
// Create output directory
std::fs::create_dir_all(&cli.output_dir)?;
// Set up signal handling
let signal_handler = setup_signal_handling()?;
// Create camera instance with selected backend
let camera = match cli.backend {
BackendChoice::PipeWire => SuperCamera::with_backend(crate::video::VideoBackendType::PipeWire),
BackendChoice::V4L2 => SuperCamera::with_backend(crate::video::VideoBackendType::V4L2),
BackendChoice::Stdout => SuperCamera::with_backend(crate::video::VideoBackendType::Stdout),
};
let camera = match camera {
Ok(camera) => camera,
Err(e) => {
error!("Failed to create camera: {}", e);
return Err(e.into());
}
};
// Start camera stream
if let Err(e) = camera.start_stream() {
error!("Failed to start camera stream: {}", e);
return Err(e.into());
}
info!("Camera stream started successfully");
info!("Press Ctrl+C to stop");
// Wait for shutdown signal or optional timeout
if cli.timeout_seconds > 0 {
let timed_out = !signal_handler
.wait_for_shutdown_with_timeout(std::time::Duration::from_secs(cli.timeout_seconds));
if timed_out {
info!("Timeout reached ({}s), initiating shutdown...", cli.timeout_seconds);
}
} else {
signal_handler.wait_for_shutdown();
}
info!("Shutting down...");
// Stop camera stream
if let Err(e) = camera.stop_stream() {
warn!("Error stopping camera stream: {}", e);
}
info!("Shutdown complete");
Ok(())
}
/// Initialize logging system
fn init_logging(debug: bool, verbose: bool) -> Result<(), Box<dyn std::error::Error>> {
let filter = if verbose {
"geek_szitman_supercamera=trace"
} else if debug {
"geek_szitman_supercamera=debug"
} else {
"geek_szitman_supercamera=info"
};
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(filter));
fmt::Subscriber::builder()
.with_env_filter(env_filter)
.with_target(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_file(false)
.with_line_number(false)
.init();
Ok(())
}
/// Set up signal handling for graceful shutdown
fn setup_signal_handling(
) -> Result<geek_szitman_supercamera::utils::SignalHandler, Box<dyn std::error::Error>> {
geek_szitman_supercamera::utils::SignalHandler::new()
}
/// Handle errors gracefully
fn handle_error(error: Error) {
match error {
Error::Usb(e) => {
error!("USB error: {}", e);
match e {
geek_szitman_supercamera::error::UsbError::DeviceNotFound => {
eprintln!("Device not found. Please check that the camera is connected.");
}
geek_szitman_supercamera::error::UsbError::PermissionDenied => {
eprintln!("Permission denied. Try running with sudo or add udev rules.");
}
geek_szitman_supercamera::error::UsbError::DeviceDisconnected => {
eprintln!("Device disconnected.");
}
_ => {
eprintln!("USB error: {e}");
}
}
}
Error::Video(e) => {
error!("Video backend error: {}", e);
match e {
geek_szitman_supercamera::error::VideoError::PipeWire(msg) => {
eprintln!("PipeWire error: {msg}. Make sure PipeWire is running.");
}
geek_szitman_supercamera::error::VideoError::V4L2(msg) => {
eprintln!("V4L2 error: {msg}. Make sure v4l2loopback is loaded.");
}
geek_szitman_supercamera::error::VideoError::Stdout(msg) => {
eprintln!("Stdout error: {msg}. Check if stdout is writable.");
}
_ => {
eprintln!("Video error: {e}");
}
}
}
Error::Protocol(e) => {
error!("Protocol error: {}", e);
eprintln!("Protocol error: {e}. Check camera firmware version.");
}
Error::Jpeg(e) => {
error!("JPEG error: {}", e);
eprintln!("JPEG processing error: {e}");
}
Error::System(e) => {
error!("System error: {}", e);
eprintln!("System error: {e}");
}
Error::Generic(msg) => {
error!("Generic error: {}", msg);
eprintln!("Error: {msg}");
}
}
}
|