use videocall_cli::cli_args::{IndexKind, Info};
use videocall_nokhwa::{
native_api_backend,
pixel_format::RgbFormat,
query,
utils::{frame_formats, CameraIndex, RequestedFormat, RequestedFormatType},
Camera,
};
pub async fn get_info(info: Info) -> anyhow::Result<()> {
if info.list_cameras {
let backend = native_api_backend().unwrap();
let devices = query(backend).unwrap();
println!("There are {} available cameras.", devices.len());
for device in devices {
println!("{device}");
}
}
if info.list_audio_devices {
list_audio_devices()?;
}
if let Some(index) = info.list_formats {
let index = match index {
IndexKind::String(s) => CameraIndex::String(s.clone()),
IndexKind::Index(i) => CameraIndex::Index(i),
};
let mut camera = Camera::new(
index,
RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestFrameRate),
)?;
camera_compatible_formats(&mut camera);
}
Ok(())
}
fn list_audio_devices() -> anyhow::Result<()> {
use cpal::traits::{DeviceTrait, HostTrait};
let host = cpal::default_host();
let default_name = host.default_input_device().and_then(|d| d.name().ok());
let devices: Vec<_> = host.input_devices()?.collect();
println!("There are {} available audio input devices.", devices.len());
for device in &devices {
let Ok(name) = device.name() else { continue };
let marker = if Some(&name) == default_name.as_ref() {
" (default)"
} else {
""
};
println!("{name}{marker}");
match device.default_input_config() {
Ok(cfg) => println!(
" default: {} Hz, {} ch, {:?}",
cfg.sample_rate().0,
cfg.channels(),
cfg.sample_format()
),
Err(e) => println!(" default: <unavailable: {e}>"),
}
}
println!(
"\nPass one of these to `stream --audio-device <NAME>`, \
or use `--audio-device default`."
);
Ok(())
}
fn camera_compatible_formats(cam: &mut Camera) {
println!("{}", cam.info());
for ffmt in frame_formats() {
if let Ok(compatible) = cam.compatible_list_by_resolution(*ffmt) {
println!("{ffmt}:");
let mut formats = Vec::new();
for (resolution, fps) in compatible {
formats.push((resolution, fps));
}
formats.sort_by(|a, b| a.0.cmp(&b.0));
for fmt in formats {
let (resolution, res) = fmt;
println!(" - {resolution}: {res:?}")
}
}
}
}