AVCaptureDevice 的 USB 接口

USB interface of an AVCaptureDevice

如果我有一个 AVCaptureDevice,我如何获得相关的 USB 接口,以便我可以通过 IOKit 访问底层硬件?

我试过通过 vid & pid 找到它,但是如果我插入两个具有相同 vid、pid 的设备,这将不起作用。这是我用来从 AVCaptureDevice 中提取 vid 和 pid 的代码:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithDeviceType:AVMediaTypeVideo];
NSString *modelID = [device modelID];

NSRange vidRange = [modelID rangeOfString:@"VendorID_"];
int vid = [[modelID substringWithRange:NSMakeRange(vidRange.location + 9, 5)] intValue];

NSRange pidRange = [modelID rangeOfString:@"ProductID_"];
int vid = [[modelID substringWithRange:NSMakeRange(pidRange.location + 10, 5)] intValue];

然后我搜索匹配的 IOService:

CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
CFNumberRef numberRef;

numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vid);
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID), numberRef);
CFRelease(numberRef);

numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pid);
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID), numberRef);
CFRelease(numberRef);

io_service_t camera = IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict);

也许 AVCaptureDevice modelIDuniqueID 的组合可以帮助您找到匹配的 IOService。这两个 ID 的格式看起来都没有记录,并且取决于传输和媒体类型,但您应该能够弄清楚。这是USB音频的初步映射(因为我没有任何USB摄像头),基于两个设备:

modelID: USB Headphone Set:0C76:1607
uniqueID: AppleUSBAudioEngine:Unknown Manufacturer:USB Headphone Set:14200000:2,1,
    USB Headphone Set:

    Product ID: 0x1607
    Vendor ID:  0x0c76  (Solid State System Co., Ltd.)
Version:    1.00
Speed:  Up to 12 Mb/sec
    Location ID:    0x14200000 / 9
    Current Available (mA): 1000
    Current Required (mA):  100
    Extra Operating Current (mA):   0


uniqueID: AppleUSBAudioEngine:Burr-Brown from TI              :USB Audio CODEC :14200000:2,
modelID: USB Audio CODEC :08BB:2902
    USB Audio CODEC :

    Product ID: 0x2902
    Vendor ID:  0x08bb  (Texas Instruments Japan)
Version:    1.00
Speed:  Up to 12 Mb/sec
Manufacturer:   Burr-Brown from TI
    Location ID:    0x14200000 / 10
    Current Available (mA): 1000
    Current Required (mA):  100
    Extra Operating Current (mA):   0

(USB 音频)格式类似于

modelID: name:vendorID:productID
uniqueID: AppleUSBAudioEngine:Manufacturer or Unknown Manufacturer:location ID:???

如果USB视频的uniqueID相似,那么productIDvendorIDlocationID(哪个USB端口)的组合应该足以唯一标识底层设备的硬件,尽管是以一种脆弱且未记录的方式。