@@ -29,7 +29,8 @@ private let kIOUSBInterfaceInterfaceID: CFUUID = CFUUIDGetConstantUUIDWithBytes
2929typealias DeviceInterfacePointer = UnsafeMutablePointer < UnsafeMutablePointer < IOUSBDeviceInterface > >
3030
3131extension AVCaptureDevice {
32- func usbDevice( ) throws -> USBDevice {
32+
33+ private func getIOService( ) throws -> io_service_t {
3334 var camera : io_service_t = 0
3435 let cameraInformation = try self . modelID. extractCameraInformation ( )
3536 let dictionary : NSMutableDictionary = IOServiceMatching ( " IOUSBDevice " ) as NSMutableDictionary
@@ -45,14 +46,23 @@ extension AVCaptureDevice {
4546 if IOServiceGetMatchingServices ( kIOMasterPortDefault, dictionary, & iter) == kIOReturnSuccess {
4647 var cameraCandidate : io_service_t
4748 cameraCandidate = IOIteratorNext ( iter)
48- while ( cameraCandidate != 0 ) {
49- var propsRef : Unmanaged < CFMutableDictionary > ? = nil
49+ while cameraCandidate != 0 {
50+ var propsRef : Unmanaged < CFMutableDictionary > ?
5051
51- if IORegistryEntryCreateCFProperties ( cameraCandidate, & propsRef, kCFAllocatorDefault, 0 ) == kIOReturnSuccess {
52+ if IORegistryEntryCreateCFProperties (
53+ cameraCandidate,
54+ & propsRef,
55+ kCFAllocatorDefault,
56+ 0 ) == kIOReturnSuccess {
5257 if let properties = propsRef? . takeRetainedValue ( ) {
5358
5459 // these are common keys that might have the device name stored in the propery dictionary
55- let keysToTry : [ String ] = [ " kUSBProductString " , " kUSBVendorString " , " USB Product Name " , " USB Vendor Name " ]
60+ let keysToTry : [ String ] = [
61+ " kUSBProductString " ,
62+ " kUSBVendorString " ,
63+ " USB Product Name " ,
64+ " USB Vendor Name "
65+ ]
5666
5767 var found : Bool = false
5868 for key in keysToTry {
@@ -80,6 +90,13 @@ extension AVCaptureDevice {
8090 if camera == 0 {
8191 camera = IOServiceGetMatchingService ( kIOMasterPortDefault, dictionary)
8292 }
93+
94+ return camera
95+ }
96+
97+ func usbDevice( ) throws -> USBDevice {
98+
99+ let camera = try self . getIOService ( )
83100 defer {
84101 let code : kern_return_t = IOObjectRelease ( camera)
85102 assert ( code == kIOReturnSuccess )
0 commit comments