如果我有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
modelID
和 uniqueID
的组合可以帮助您找到匹配的 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
类似,那么 productID
、vendorID
和 locationID
(哪个 USB 端口)的组合应该足以唯一标识底层设备的硬件,尽管这种方式很脆弱且未记录。
对于视频设备,AVCaptureDevice uniqueID 的形式似乎为 0xLLLLLLLLVVVVPPPP,其中:
这些值应该对应于询问 io_service_t 时返回的值
CFNumberRef vendorIdObj = IORegistryEntrySearchCFProperty(ioService, kIOUSBPlane, CFSTR(kUSBVendorID), kCFAllocatorDefault, 0);
CFNumberRef productIdObj = IORegistryEntrySearchCFProperty(ioService, kIOUSBPlane, CFSTR(kUSBProductID), kCFAllocatorDefault, 0);
CFNumberRef locationIdObj = IORegistryEntrySearchCFProperty(ioService, kIOUSBPlane, CFSTR(kUSBDevicePropertyLocationID), kCFAllocatorDefault, 0);
通过比较两组值,你应该是