escapi icon indicating copy to clipboard operation
escapi copied to clipboard

Detecting when webcam was unplugged

Open Boscop opened this issue 8 years ago • 4 comments

How can I detect when the webcam was unplugged?

In my loop I could check if countCaptureDevices() changed from last frame, and if it did, I can check if the currently opened cam is still in the list of connected ones, but is there a better way?

Boscop avatar Sep 04 '17 20:09 Boscop

Not that I can think of.

On Mon, Sep 4, 2017 at 11:14 PM, Boscop [email protected] wrote:

How can I detect when the webcam was unplugged?

In my loop I could check if countCaptureDevices() changed from last frame, and if it did, I can check if the currently opened cam is still in the list of connected ones, but is there a better way?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jarikomppa/escapi/issues/10, or mute the thread https://github.com/notifications/unsubscribe-auth/AEQ_R2BMXxPgWuxXuMKHDHCoraCMcg7mks5sfFo-gaJpZM4PMOWh .

jarikomppa avatar Sep 06 '17 10:09 jarikomppa

Has someone looked at this pull request? From the commits it suggests that someone solved this problem, plus there are some other goodies (like having a unique identifier for each camera).

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

That seems useful! I'd appreciate if that PR got merged.

Boscop avatar Sep 19 '17 00:09 Boscop

Btw, what I do at the moment to check if the camera was disconnected since the last frame (with my Rust bindings) is:

	// TODO: count_devices() might be expensive, don't do every frame
	pub fn is_connected(&mut self) -> bool {
		let n = Capture::count_devices();
		if self.camera_count == n {
			true // might not be true if this cam got unplugged and another one plugged in since last frame
		} else { // check if this cam is still plugged in
			self.camera_count = n;
			for i in 0..n {
				if Capture::get_device_name(i) == self.cam_name {
					return true;
				}
			}
			false
		}
	}

It works but it's not elegant. Btw, do you know if count_devices() / countCaptureDevices() is an expensive function call?

Boscop avatar Sep 19 '17 01:09 Boscop