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

How to detect if camera is being used by other applications? #15

Open
geocine opened this issue Oct 20, 2022 · 8 comments
Open

How to detect if camera is being used by other applications? #15

geocine opened this issue Oct 20, 2022 · 8 comments
Assignees
Labels
help wanted Extra attention is needed suspended

Comments

@geocine
Copy link

geocine commented Oct 20, 2022

The only method I could think of is this pseudocode. Is there a more reliable/better way to do this?

Load
  start camera
  cameraInUseTimer.Interval = 1000;
  cameraInUseTimer.Elapsed += CameraInUseTimer_Elapsed;
  cameraInUseTimer.Start();

CameraInUseTimer_Elapsed
  // Camera In Use

OnPixelBufferArrivedAsync
   cameraInUseTimer.Stop()

I also need to know why the Camera is unable to start

@kekyo
Copy link
Owner

kekyo commented Oct 28, 2022

@geocine Sorry for the delay .

In the current version, there is no direct way to check if it is open or not. (If an error is detected, it is possible that an exception will be raised.)
Are you saying that if the camera device is already in use, it will open successfully but not call back?

Maybe there is a mistake in your error checking, or perhaps you need to check in a different way...
Is your environment Windows or Linux?

@geocine
Copy link
Author

geocine commented Oct 31, 2022

Are you saying that if the camera device is already in use, it will open successfully but not call back?

Correct, I'm on Windows. I'm using the demo in this repo to reproduce issue.

@kekyo kekyo self-assigned this Nov 13, 2022
@kekyo
Copy link
Owner

kekyo commented Nov 14, 2022

(This report contains with #16 )

I was able to reproduce your problem and have investigated. Unfortunately, it seems that the DirectShow filter graph cannot fully determine that you have entered this state.

The above memoized claims that it can be detected by having the filter graph play and examining its status. However, when I run playback with the capture device bound, DirectShow becomes unresponsive at that point.
Later, when I checked the status, I found that there is the following division of cases:

  • After execution of FlashCap, another application uses the same camera: Filter graph status is continue "playing".
  • Before execution of FlashCap, another application uses the same camera: Filter graph status is "stopped".

In the latter case, it is possible to recover (if another application stops) by monitoring it with an interval timer, etc.
And forcing the filter graph to stop and then play. However, in the end, I don't think this hack is appealing because it doesn't solve the former problem.

Furthermore, I did a little research to see how other applications (e.g. Windows Camera, maybe UWP based) get around this. It seems that those applications use at least one method that is not DirectShow.
I have seen error codes defined in the Media Foundation API (MF_E_HW_MFT_FAILED_START_STREAMING), which is the oldest, but newer than DirectShow. Perhaps the UWP and other high-level APIs have this under the hood.

Either way, DirectShow is positioned by MS itself as an API of the past, so don't be surprised if corner case support is flawed, although it may be preferable for FlashCap to move to the Media Foundation API. It may take some time to address this, as we have no experience with the Media Foundation API.

@kekyo kekyo added suspended help wanted Extra attention is needed labels Nov 14, 2022
@geocine
Copy link
Author

geocine commented Nov 15, 2022

Thank your for investigating

@kekyo
Copy link
Owner

kekyo commented Nov 16, 2022

I too find this issue very disappointing, and I find the API design that does not attempt to address it offensive, considering that even though DirectShow is an old technology, it is still used in a wide variety of applications ☹

@yangjieshao
Copy link

yangjieshao commented Feb 1, 2023

事实上 在实际使用时在启动时就判断设备是否被占用意义并不大
只需要由前端程序判断 在启动相机后连续3~5帧的时间内没有帧数据返回就可以认为相机异常了
异常包括被占用或者播放到一半相机被移除

3~5帧的时间非常短暂,对于用户来说可能也就是眨个眼的时间,前端页面甚至连连接中的动画都不用做

UsbCamera进行改造后的例子

private bool _hadLinked = false;
/// <summary>
 /// 是否已连接成功(获取成功一次图像)
/// </summary>
/// <returns></returns>
public bool HadLinked()
{
	return _hadLinked;
}
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
	lock (_bufferLocker)
	{
		// 重连完成
		IsRestarting = false;
	}
	if (!_hadLinked)
	{
		_hadLinked = true;
	}
	return 0;
}

前端调用

bool canGrab = false;
int frameNum = 0;

{
	if (_UsbCamera != null)
	{
                _UsbCamera.Start();
		while (!canGrab
		&& frameNum<5)
		{
			Thread.Sleep(_usbCameraTimePerFrame);
			canGrab = _UsbCamera.HadLinked();
			frameNum++;
		}
	}
	Debug.WriteLine("canGrab:" + canGrab);
}

@kekyo
Copy link
Owner

kekyo commented Feb 2, 2023

Thanks for the useful suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed suspended
Projects
None yet
Development

No branches or pull requests

3 participants