macOS console application with AVCaptureDevice

I am working on a console app (Actually a library, but designed to be used from console), where I want to be able to enumerate the AVCaptureDevices at runtime. Attempting to do this however, I am noticing that the AVCaptureDevice devices property is never updating. I have some minimal code to reproduce this. Run it with a USB camera plugged in, and as you unplug and replug the camera in the output never changes, and it says there are devices that don't actually exist. Note I am not running this in a UI application, so there is no standard loop. Its just meant to be running from a standard "c" main.


#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        while (true) {
            NSArray* captureDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
           
            for (id device in captureDevices) {
                NSString* uniqueIdentifier = [(AVCaptureDevice*)device uniqueID];
               
                NSLog(uniqueIdentifier);
               
               
            }
            usleep(1000000);
        }
    }
    return 0;
}


Thanks for any help. I would be ok firing up a message loop in a background thread if I needed to, but I can't guarentee that the user application has a message loop (sorry if terminology is different, I come from windows).

you need to run the aplication's main runloop. Replace usleep with [[NSRunLoop mainRunLoop] runUntilDate:]

when is apple going to bring back the bluetooth debugger?

So that fixes the issue if I am running from the main loop. However, the end goal is for this to be a library, where I don't actually have control over if the main loop is properly handling it's RunLoop. Is there any way in my library to for instance spawn a secondary thread to handle the notification events so I can ensure that in all use cases a function that grabs all currently connected devices work, no matter what the main application thread is doing?

Sorry to bring up a very old thread, but I never found a solution to this issue. I need to be able to run AVFoundation code without requiring a main RunLoop to be running. Is there any way I can move the events required to make this work to another run loop? I can fire up a background run loop easily to make that work.

macOS console application with AVCaptureDevice
 
 
Q