MCPcopy Create free account
hub / github.com/RustCV/RustCV / resolve_device_id

Method resolve_device_id

rustcv/src/videoio/mod.rs:305–334  ·  view source on GitHub ↗
(driver: &dyn Driver, index: u32)

Source from the content-addressed store, hash-verified

303 }
304
305 // 内部辅助:解决设备 ID 的平台差异
306 #[allow(unused_variables)]
307 fn resolve_device_id(driver: &dyn Driver, index: u32) -> Result<String> {
308 #[cfg(target_os = "linux")]
309 {
310 // Linux 策略:直接映射 ID,模仿 OpenCV
311 // index 0 -> "/dev/video0"
312 // index 1 -> "/dev/video1"
313 Ok(format!("/dev/video{}", index))
314 }
315
316 #[cfg(not(target_os = "linux"))]
317 {
318 // macOS/Windows 策略:列表排序后取索引
319 let mut devices = driver
320 .list_devices()
321 .map_err(|e| anyhow!("Failed to list devices: {}", e))?;
322
323 // 关键:必须排序,确保 new(0) 永远打开同一个设备
324 devices.sort_by(|a, b| a.id.cmp(&b.id));
325
326 let info = devices.get(index as usize).ok_or_else(|| {
327 anyhow!(
328 "Camera index {} out of range. Found {} devices.",
329 index,
330 devices.len()
331 )
332 })?;
333
334 Ok(info.id.clone())
335 }
336 }
337}

Callers

nothing calls this directly

Calls 1

list_devicesMethod · 0.45

Tested by

no test coverage detected