Mac Developer Library

Developer

AV Foundation Framework Reference AVCaptureSession Class Reference

Options
Deployment Target:

On This Page
Language:

AVCaptureSession

You use an AVCaptureSession object to coordinate the flow of data from AV input devices to outputs.

To perform a real-time or offline capture, you instantiate an AVCaptureSession object and add appropriate inputs (such as AVCaptureDeviceInput), and outputs (such as AVCaptureMovieFileOutput). The following code fragment illustrates how to configure a capture device to record audio:

  1. AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
  2. AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  3. NSError *error = nil;
  4. AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
  5. if (audioInput) {
  6. [captureSession addInput:audioInput];
  7. }
  8. else {
  9. // Handle the failure.
  10. }

You invoke startRunning to start the flow of data from the inputs to the outputs, and invoke stopRunning to stop the flow.

You use the sessionPreset property to customize the quality level, bitrate, or other settings for the output. Most common capture configurations are available through session presets; however, some specialized options (such as high frame rate) require directly setting a capture format on an AVCaptureDevice instance.

  • Tells the receiver to start running.

    Declaration

    Swift

    func startRunning()

    Objective-C

    - (void)startRunning

    Discussion

    This method is used to start the flow of data from the inputs to the outputs connected to the AVCaptureSession instance that is the receiver. This method is synchronous and blocks until the receiver has either completely started running or failed to start running. If an error occurs during this process and the receiver fails to start running, you receive an AVCaptureSessionRuntimeErrorNotification.

    Availability

    Available in OS X v10.7 and later.

    See Also

    – stopRunning

  • Tells the receiver to stop running.

    Declaration

    Swift

    func stopRunning()

    Objective-C

    - (void)stopRunning

    Discussion

    This method is used to stop the flow of data from the inputs to the outputs connected to the AVCaptureSession instance that is the receiver. This method is synchronous and blocks until the receiver has completely stopped running.

    Availability

    Available in OS X v10.7 and later.

  • Indicates whether the receiver is running. (read-only)

    Declaration

    Swift

    var running: Bool { get }

    Objective-C

    @property(nonatomic, readonly, getter=isRunning) BOOL running

    Discussion

    You can observe the value of this property using key-value observing.

    Availability

    Available in OS X v10.7 and later.

  • Indicates the start of a set of configuration changes to be made atomically.

    Declaration

    Swift

    func beginConfiguration()

    Objective-C

    - (void)beginConfiguration

    Discussion

    You use beginConfiguration and commitConfiguration to batch multiple configuration operations on a running session into an atomic update.

    After calling beginConfiguration, you can for example add or remove outputs, alter the sessionPreset, or configure individual capture input or output properties. No changes are actually made until you invoke commitConfiguration, at which time they are applied together.

    Availability

    Available in OS X v10.7 and later.

  • Commits a set of configuration changes.

    Declaration

    Swift

    func commitConfiguration()

    Objective-C

    - (void)commitConfiguration

    Discussion

    For discussion, see beginConfiguration.

    Availability

    Available in OS X v10.7 and later.

  • A constant value indicating the quality level or bitrate of the output.

    Declaration

    Swift

    var sessionPreset: String!

    Objective-C

    @property(nonatomic, copy) NSString *sessionPreset

    Discussion

    You use this property to customize the quality level or bitrate of the output. For possible values of sessionPreset, see Video Input Presets. The default value is AVCaptureSessionPresetHigh.

    You can set this value while the session is running.

    You can only set a preset if canSetSessionPreset: returns YEStrue for that preset.

    Availability

    Available in OS X v10.7 and later.

  • Returns a Boolean value that indicates whether the receiver can use the given preset.

    Declaration

    Swift

    func canSetSessionPreset(_ preset: String!) -> Bool

    Objective-C

    - (BOOL)canSetSessionPreset:(NSString *)preset

    Parameters

    preset

    A preset you would like to set for the receiver. For possible values, see Video Input Presets.

    Return Value

    YEStrue if the receiver can use preset, otherwise NOfalse.

    Availability

    Available in OS X v10.7 and later.

  • A clock object used for output synchronization. (read-only)

    Declaration

    Swift

    var masterClock: CMClock! { get }

    Objective-C

    @property(nonatomic, readonly) CMClockRef masterClock

    Discussion

    The returned CMClock object is read-only and provides a timebase for sample buffers in capture output. This can be used in conjunction with the clock from an AVCaptureInputPort object to synchronize timestamps from multiple devices.

    You can observe the value of this property using key-value observing.

    Availability

    Available in OS X v10.9 and later.

  • Constants to specify the device orientation during video capture.

    Declaration

    Swift

    enum AVCaptureVideoOrientation : Int { case Portrait case PortraitUpsideDown case LandscapeRight case LandscapeLeft }

    Objective-C

    enum { AVCaptureVideoOrientationPortrait = 1, AVCaptureVideoOrientationPortraitUpsideDown, AVCaptureVideoOrientationLandscapeRight, AVCaptureVideoOrientationLandscapeLeft, }; typedef NSInteger AVCaptureVideoOrientation;

    Constants

    • portrait

      AVCaptureVideoOrientationPortrait

      Indicates that video should be oriented vertically, with the device home button on the bottom.

      Available in OS X v10.7 and later.

    • portraitUpsideDown

      AVCaptureVideoOrientationPortraitUpsideDown

      Indicates that video should be oriented vertically, with the device home button on the top.

      Available in OS X v10.7 and later.

    • landscapeRight

      AVCaptureVideoOrientationLandscapeRight

      Indicates that video should be oriented horizontally, with the device home button on the left.

      Available in OS X v10.7 and later.

    • landscapeLeft

      AVCaptureVideoOrientationLandscapeLeft

      Indicates that video should be oriented horizontally, with the device home button on the right.

      Available in OS X v10.7 and later.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in OS X v10.7 and later.

  • Key to retrieve information from a notification from a capture session.

    Declaration

    Swift

    let AVCaptureSessionErrorKey: String

    Objective-C

    NSString *const AVCaptureSessionErrorKey;

    Constants

  • Constants to define capture setting presets using the sessionPreset property.

    Declaration

    Swift

    let AVCaptureSessionPresetPhoto: String let AVCaptureSessionPresetHigh: String let AVCaptureSessionPresetMedium: String let AVCaptureSessionPresetLow: String let AVCaptureSessionPreset320x240: String let AVCaptureSessionPreset352x288: String let AVCaptureSessionPreset640x480: String let AVCaptureSessionPreset960x540: String let AVCaptureSessionPreset1280x720: String let AVCaptureSessionPresetiFrame960x540: String let AVCaptureSessionPresetiFrame1280x720: String

    Objective-C

    NSString *const AVCaptureSessionPresetPhoto; NSString *const AVCaptureSessionPresetHigh; NSString *const AVCaptureSessionPresetMedium; NSString *const AVCaptureSessionPresetLow; NSString *const AVCaptureSessionPreset320x240; NSString *const AVCaptureSessionPreset352x288; NSString *const AVCaptureSessionPreset640x480; NSString *const AVCaptureSessionPreset960x540; NSString *const AVCaptureSessionPreset1280x720; NSString *const AVCaptureSessionPresetiFrame960x540; NSString *const AVCaptureSessionPresetiFrame1280x720;

    Constants

    • AVCaptureSessionPresetPhoto

      AVCaptureSessionPresetPhoto

      Specifies capture settings suitable for high resolution photo quality output.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPresetHigh

      AVCaptureSessionPresetHigh

      Specifies capture settings suitable for high quality video and audio output.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPresetMedium

      AVCaptureSessionPresetMedium

      Specifies capture settings suitable for output video and audio bitrates suitable for sharing over WiFi.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPresetLow

      AVCaptureSessionPresetLow

      Specifies capture settings suitable for output video and audio bitrates suitable for sharing over 3G.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPreset320x240

      AVCaptureSessionPreset320x240

      Specifies capture settings suitable for 320x240 pixel video output.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPreset352x288

      AVCaptureSessionPreset352x288

      Specifies capture settings suitable for CIF quality (352x288 pixel) video output.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPreset640x480

      AVCaptureSessionPreset640x480

      Specifies capture settings suitable for VGA quality (640x480 pixel) video output.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPreset960x540

      AVCaptureSessionPreset960x540

      Specifies capture settings suitable for quarter HD quality (960x540 pixel) video output.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPreset1280x720

      AVCaptureSessionPreset1280x720

      Specifies capture settings suitable for 720p quality (1280x720 pixel) video output.

      Available in OS X v10.7 and later.

    • AVCaptureSessionPresetiFrame960x540

      AVCaptureSessionPresetiFrame960x540

      Specifies capture settings to achieve 960x540 quality iFrame H.264 video at about 30 Mbits/sec with AAC audio.

      QuickTime movies captured in iFrame format are optimal for editing applications.

      Available in OS X v10.9 and later.

    • AVCaptureSessionPresetiFrame1280x720

      AVCaptureSessionPresetiFrame1280x720

      Specifies capture settings to achieve 1280x720 quality iFrame H.264 video at about 40 Mbits/sec with AAC audio.

      QuickTime movies captured in iFrame format are optimal for editing applications.

      Available in OS X v10.9 and later.