Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if the camera is in use by another process?

In OS X, how can I tell if the camera or microphone is in use by another application or process? The following doesn't seem to work unless the other application has locked the device.

NSArray *devices = [AVCaptureDevice devices];

for (AVCaptureDevice *device in devices) {
    NSLog(@"In use by other application %hhd", [device isInUseByAnotherApplication]);
}
like image 360
TerryB Avatar asked May 26 '16 20:05

TerryB


People also ask

Why does my camera say it's being used by another application?

One of the reasons why you're getting this error message when trying to use the camera is closing your Windows 10 operating system without closing the application you used the camera on. In this case, the camera is still running in the background registries of Windows 10.

How do you fix camera can't be accessed because it's being used by another application?

Press Windows key + R to open up a Run dialog box. Then, type “ms-settings:privacy-webcam” and press Enter to open up the Camera tab under Privacy Settings. Inside the Settings app, move over to the right-hand pane and make sure that the toggle associated with Allow apps to access your camera is turned On.


1 Answers

You can use CoreAudio to check if microphone is in use or not.

AudioObjectPropertyAddress propertyAddress = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };

UInt32 dataSize = 0;
OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if(kAudioHardwareNoError != status) {
    fprintf(stderr, "AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i\n", status);
    //return NULL;
    return;
}

UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID));

AudioDeviceID *audioDevices = (AudioDeviceID*)(malloc(dataSize));
if(NULL == audioDevices) {
    fputs("Unable to allocate memory", stderr);
    return;
}

status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if(kAudioHardwareNoError != status) {
    fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %i\n", status);
    free(audioDevices), audioDevices = NULL;
    return ;
}

CFMutableArrayRef inputDeviceArray = CFArrayCreateMutable(kCFAllocatorDefault, deviceCount, &kCFTypeArrayCallBacks);
if(NULL == inputDeviceArray) {
    fputs("CFArrayCreateMutable failed", stderr);
    free(audioDevices), audioDevices = NULL;
    return ;
}`

Now Iterate through all the devices and fetch property data kAudioDevicePropertyDeviceIsRunningSomewhere

CFBooleanRef deviceIsRunning = NULL; dataSize = sizeof(deviceIsRunning); propertyAddress.mSelector = kAudioDevicePropertyDeviceIsRunningSomewhere; status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceIsRunning);

Check deviceIsRunning variable.

I don't have idea about video device. But i will update my answer if i find some solution.

Hope this help.

like image 97
Rohan Avatar answered Sep 27 '22 21:09

Rohan