A device that provides input (such as audio or video) for capture sessions and offers controls for hardware-specific capture features.
SDKs
- iOS 4.0+
- macOS 10.7+
- Mac Catalyst 13.0+
Framework
- AVFoundation
Declaration
class AVCaptureDevice : NSObject
Overview
An AVCapture
object represents a physical capture device and the properties associated with that device. You use a capture device to configure the properties of the underlying hardware. A capture device also provides input data (such as audio or video) to an AVCapture
object.
You use the methods of the AVCapture
class to enumerate the available devices, query their capabilities, and be informed about when devices come and go. Before you attempt to set properties of a capture device (its focus mode, exposure mode, and so on), you must first acquire a lock on the device using the lock
method. You should also query the device’s capabilities to ensure that the new modes you intend to set are valid for that device. You can then set the properties and release the lock using the unlock
method. You may hold the lock if you want all settable device properties to remain unchanged. However, holding the device lock unnecessarily may degrade capture quality in other applications sharing the device and is not recommended.
Most common configurations of capture settings are available through the AVCapture
object and its available presets. However, on iOS devices, some specialized options (such as high frame rate) require directly setting a capture format on an AVCapture
instance. The following code example illustrates how to select an iOS device’s highest possible frame rate:
func configureCameraForHighestFrameRate(device: AVCaptureDevice) {
var bestFormat: AVCaptureDevice.Format?
var bestFrameRateRange: AVFrameRateRange?
for format in device.formats {
for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate > bestFrameRateRange?.maxFrameRate ?? 0 {
bestFormat = format
bestFrameRateRange = range
}
}
}
if let bestFormat = bestFormat,
let bestFrameRateRange = bestFrameRateRange {
do {
try device.lockForConfiguration()
// Set the device's active format.
device.activeFormat = bestFormat
// Set the device's min/max frame duration.
let duration = bestFrameRateRange.minFrameDuration
device.activeVideoMinFrameDuration = duration
device.activeVideoMaxFrameDuration = duration
device.unlockForConfiguration()
} catch {
// Handle error.
}
}
}
Note
In iOS, directly configuring a capture device’s active
property changes the capture session’s preset to input
. Upon making this change, the capture session no longer automatically configures the capture format when you call the start
method or call the commit
method after changing the session topology.
In macOS, a capture session can still automatically configure the capture format after you make changes. To prevent automatic changes to the capture format in macOS, follow the advice listed under the lock
method.