iosswiftavfoundationquicktime

iOS Device not listed by AVCaptureDevice.devices() unless Quicktime is opened


I am trying to list the the devices connected to my machine using AVCaptureDevice.devices() in a Swift playground.

import Cocoa
import Foundation
import AVFoundation
import CoreMediaIO

var prop = CMIOObjectPropertyAddress(
    mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices),
    mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal),
    mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster))

var allow : UInt32 = 1
let dataSize : UInt32 = 4
let zero : UInt32 = 0
CMIOObjectSetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &prop, zero, nil, dataSize, &allow)


var session = AVCaptureSession()
session.sessionPreset = AVCaptureSession.Preset.low

let devices = AVCaptureDevice.devices()
for device in devices {
    let deviceID = device.uniqueID
    let deviceName = device.localizedName
    print("\(deviceID): \(deviceName)")
}

This gives me the following result even though my iPhone is connected to my computer

04-52-c7-c1-65-c4:input: Bose Tito
AppleHDAEngineInput:1B,0,1,0:1: Built-in Microphone
CC26311ECFEG1HNBA: FaceTime HD Camera

Now something I've noticed is that if I launch Quicktime Player, select New Movie Recording and selecting my device as a Camera source, then my device is listed

04-52-c7-c1-65-c4:input: Bose Tito
AppleHDAEngineInput:1B,0,1,0:1: Built-in Microphone
CC26311ECFEG1HNBA: FaceTime HD Camera
12345b7406eeb053e2d5cded2527315d6110a16e: tito

Is there anyway to prevent this?


Solution

  • You need to wait a bit for the device to appear.

    Register for AVCaptureDeviceWasConnectedNotification to be notified once it becomes available.