Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing devices #7

Merged
merged 8 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions examples/window-rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::num::NonZeroU32;

use winit::{
dpi::PhysicalSize,
event::{Event, WindowEvent},
event::{DeviceEvent, ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
Expand All @@ -15,17 +15,15 @@ fn main() {
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();

let camera = Camera::new_default_device();
let mut camera = Camera::new_default_device();
camera.start();

event_loop.run(move |event, _x, control_flow| {
*control_flow = ControlFlow::Poll;

match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
let Some(frame) = camera.wait_for_frame() else {
return
};
let Some(frame) = camera.wait_for_frame() else { return };
let (w, h) = frame.size_u32();

surface.resize(NonZeroU32::new(w).unwrap(), NonZeroU32::new(h).unwrap()).unwrap();
Expand All @@ -47,6 +45,12 @@ fn main() {
Event::RedrawEventsCleared => {
window.request_redraw();
}
Event::DeviceEvent {
event: DeviceEvent::Button { button: _, state: ElementState::Released },
device_id: _,
} => {
camera.change_device();
}
_ => {}
}
});
Expand Down
5 changes: 5 additions & 0 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl Camera {
pub fn wait_for_frame(&self) -> Option<Frame> {
self.inner.wait_for_frame().map(|inner| Frame { inner })
}

pub fn change_device(&mut self) {
self.inner.change_device();
}
}

impl Frame {
Expand Down Expand Up @@ -66,4 +70,5 @@ pub(crate) trait InnerCamera: std::fmt::Debug {
fn start(&self);
fn stop(&self);
fn wait_for_frame(&self) -> Option<Self::Frame>;
fn change_device(&mut self);
}
120 changes: 82 additions & 38 deletions src/linux_v4l2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use ffimage::color::Bgra;

use v4l::context::Node;
use v4l::io::traits::CaptureStream;

use v4l::video::Capture;
use v4l::*;

use std::marker::PhantomData;

use std::sync::RwLock;

use crate::InnerCamera;
Expand All @@ -16,52 +18,78 @@ pub struct Camera {
stream: RwLock<Option<v4l::io::mmap::Stream<'static>>>,
}

impl InnerCamera for Camera {
type Frame = Frame;

fn new_default_device() -> Self {
let device_node = v4l::context::enum_devices().into_iter().next().unwrap();
let device_name =
device_node.name().unwrap_or_else(|| device_node.path().to_string_lossy().to_string());
fn name_or_path(device_node: &v4l::context::Node) -> String {
device_node.name().unwrap_or_else(|| device_node.path().to_string_lossy().to_string())
}

println!(
"Node {{ index: {}, name: {:?}, path: {:?} }}",
device_node.index(),
device_node.name(),
device_node.path()
);
fn get_next_best_format(device: &Device) -> Format {
let _rgb = FourCC::new(b"RGB3");
let mut fmt = device.format().expect("device.format()");
let size = device
.enum_framesizes(fmt.fourcc)
.unwrap()
.into_iter()
.next()
.unwrap()
.size
.to_discrete()
.into_iter()
.last()
.unwrap();
fmt.width = size.width;
fmt.height = size.height;
fmt
}

let device = v4l::Device::new(0).unwrap();
#[allow(unused)]
fn display_node(node: &Node) {
println!(
"Node {{ index: {}, name: {:?}, path: {:?} }}",
node.index(),
node.name(),
node.path()
);
}

for fmt in device.enum_formats().unwrap() {
println!("{:?}", fmt);
#[allow(unused)]
fn display_device_formats(device: &Device) {
println!("Device formats:");
for fmt in device.enum_formats().unwrap() {
println!(" {:?}", fmt);

for size in device.enum_framesizes(fmt.fourcc).unwrap() {
println!("{:?}", size);
}
for size in device.enum_framesizes(fmt.fourcc).unwrap() {
println!(" {:?}", size);
}
}
}

fn enum_devices() -> Vec<Node> {
v4l::context::enum_devices()
.into_iter()
.filter_map(|node| Device::with_path(node.path()).ok().map(|device| (node, device)))
.filter(|(_, device)| device.format().is_ok())
.map(|(node, _)| node)
.collect()
}

let _rgb = FourCC::new(b"RGB3");
let mut fmt = device.format().unwrap();
let size = device
.enum_framesizes(fmt.fourcc)
.unwrap()
.into_iter()
.next()
.unwrap()
.size
.to_discrete()
.into_iter()
.last()
.unwrap();
fmt.width = size.width;
fmt.height = size.height;

if let Err(error) = device.set_format(&fmt) {
eprintln!("Device.set_format: {}", error);
impl Camera {
fn from_node(node: &v4l::context::Node) -> Self {
let device = v4l::Device::with_path(node.path()).unwrap();
device.set_format(&get_next_best_format(&device)).unwrap();
Self {
device: RwLock::new(device),
device_name: name_or_path(node),
stream: RwLock::new(None),
}
}
}

impl InnerCamera for Camera {
type Frame = Frame;

Self { device: RwLock::new(device), device_name, stream: RwLock::new(None) }
fn new_default_device() -> Self {
let node = enum_devices().into_iter().next().unwrap();
Self::from_node(&node)
}

fn start(&self) {
Expand Down Expand Up @@ -94,6 +122,22 @@ impl InnerCamera for Camera {
None
}
}

fn change_device(&mut self) {
let devices = enum_devices();
if let Some(pos) = devices.iter().position(|n| name_or_path(n) == self.device_name) {
let new_pos = (pos + 1) % devices.len();
if new_pos != pos {
*self = Self::from_node(&devices[new_pos]);
self.start();
}
} else if !devices.is_empty() {
*self = Self::from_node(&devices[0]);
self.start();
} else {
self.stop();
}
}
}

impl std::fmt::Debug for Camera {
Expand Down
4 changes: 4 additions & 0 deletions src/mac_avf/av_capture_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl AVCaptureSession {
pub fn add_output(&self, output: &AVCaptureVideoDataOutput) {
unsafe { msg_send!(self, addOutput: output) }
}

pub fn remove_input(&self, input: &AVCaptureDeviceInput) {
unsafe { msg_send!(self, removeInput: input) }
}
}

#[test]
Expand Down
48 changes: 44 additions & 4 deletions src/mac_avf/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::sync::Arc;

#[derive(Debug)]
pub struct Camera {
_device: Id<AVCaptureDevice>,
_input: Id<AVCaptureDeviceInput>,
_output: Id<AVCaptureVideoDataOutput>,
device: Id<AVCaptureDevice>,
input: Id<AVCaptureDeviceInput>,
#[allow(unused)]
output: Id<AVCaptureVideoDataOutput>,
session: Id<AVCaptureSession>,
slot: Arc<Slot>,
}
Expand All @@ -33,7 +34,7 @@ impl Camera {
session.add_input(&input);
session.add_output(&output);

Camera { _device: device, _input: input, _output: output, session, slot }
Camera { device, input, output, session, slot }
}

pub fn start(&self) {
Expand All @@ -47,6 +48,24 @@ impl Camera {
pub fn wait_for_frame(&self) -> Option<Frame> {
self.slot.wait_for_sample().map(|sample| Frame { sample })
}

pub fn change_device(&mut self) {
let devices = AVCaptureDevice::all_video_devices();
let Some(index) = devices.iter().position(|d| d.unique_id() == self.device.unique_id())
else {
return;
};
let new_index = (index + 1) % devices.len();
if new_index == index {
return;
}
let new_device = devices[new_index].retain();
let new_input = AVCaptureDeviceInput::from_device(&new_device).unwrap();
self.session.remove_input(&self.input);
self.device = new_device;
self.input = new_input;
self.session.add_input(&self.input);
}
}

impl Frame {
Expand All @@ -69,3 +88,24 @@ impl<'a> FrameData<'a> {
self.pixels.u32
}
}

#[cfg(test)]
const TEST_FRAMES: usize = 3;

#[test]
fn change_device() {
let mut camera = Camera::new_default_device();
camera.start();

std::iter::from_fn(|| camera.wait_for_frame())
.map(|s| println!("{s:?}"))
.take(TEST_FRAMES)
.count();

camera.change_device();

std::iter::from_fn(|| camera.wait_for_frame())
.map(|s| println!("{s:?}"))
.take(TEST_FRAMES)
.count();
}
36 changes: 36 additions & 0 deletions src/mac_avf/test_scenarios.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use objc2::rc::Id;

use super::*;

const TEST_FRAMES: usize = 3;
Expand Down Expand Up @@ -121,3 +123,37 @@ fn running_capture_session_for_all_cameras_in_yuv2() {
session.stop_running();
}
}

#[test]
fn running_capture_session_from_changing_cameras() {
println!();
let session = AVCaptureSession::new();
let output = AVCaptureVideoDataOutput::new();
let delegate = SampleBufferDelegate::new();
let slot = delegate.slot();
output.set_sample_buffer_delegate(delegate);
session.add_output(&output);

let mut input: Option<Id<AVCaptureDeviceInput>> = None;

session.start_running();

for device in AVCaptureDevice::all_video_devices() {
println!("{}", device.localized_name());

if let Some(input) = input {
session.remove_input(&input);
}

let new_input = AVCaptureDeviceInput::from_device(&device).unwrap();
session.add_input(&new_input);
input = Some(new_input);

std::iter::from_fn(|| slot.wait_for_sample())
.map(|s| println!("{s:?}"))
.take(TEST_FRAMES)
.count();
}

session.stop_running();
}
Loading
Loading