escapi icon indicating copy to clipboard operation
escapi copied to clipboard

Rust API suggestions, finding device id from its name

Open Boscop opened this issue 8 years ago • 3 comments

In the Rust API it would be useful to be able to enumerate the capture devices so that one can get a device's name and id (and possibly max resolution) before initializing the capture on it with init(). Because the installed devices on a system change over time and don't always have the same ID, so I store the camera that I want to use by its name in my application's config file and then I want to iterate over the available cameras and find its ID by its name before calling init() with that id. This could be done by having an Iterator that yields DeviceInfo which has .id() -> usize, .name() -> String, .max_resolution() -> (u32, u32).

What do you think?

(Btw, I love ESCAPI, it makes dealing with a webcam so much easier.)

Boscop avatar Jul 07 '17 09:07 Boscop

I might need it at some time too. I only use the C++ library (I don't use the Rust wrapper at all ,not that I don't want to, but I can't) and consume it a C# "side" using PInvoke calls to do what I want to achieve. To get the cameras info and properties I used protocol buffers to exchange data in a big bytes array.

This is the proto file I written to exchange data between C++ and C#:

syntax = "proto3";

package camera;

enum CaptureEncoding {
    UNKNOWN = 0;
    RGBA32 = 1;
    RGB24 = 2;
    YUY2 = 3;
    NV12 = 4; 
    MJPG = 5;
}

message CaptureFormat {
    uint32 width = 1;
    uint32 height = 2;
    double framerate = 3;
    CaptureEncoding encoding = 4;
}

message Camera {
    string cameraName = 5;
    repeated CaptureFormat formats = 6;
}

message CameraList {
    repeated Camera cameras = 7;
}

With that, I got all the info I need on the C# side to choose the camera with the capture format I want.

Since there are multiples Rust libraries for protobuf it may be a viable road to solve this issue. Or we may use an alternative format like JSON for instance (because integrating Protobuf is not that easy). @jarikomppa What is your opinion on the matter?

codec-abc avatar Sep 18 '17 16:09 codec-abc

Btw, what I do at the moment to find the camera ID from its name (with my Rust bindings) is:

		let cam_id = (|| {
			for i in 0..camera_count {
				if Capture::get_device_name(i) == cam_name {
					return Some(i);
				}
			}
			return None;
		})().ok_or("webcam not found")?;

Boscop avatar Sep 19 '17 01:09 Boscop

Yeah it is the same idea. My solution is easier to use from a client point of view because I know almost all the properties of each camera upfront with a single API call but I have to admit that it is quite cumbersome to integrate into the code. I think it might worth taking a look at this pull request because it seems that we could have a unique identifier per camera which is more robust to identify a camera than a bare index in a list.

codec-abc avatar Sep 19 '17 08:09 codec-abc