AVCaptureSession Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/AVFoundation.framework
Availability
Available in OS X v10.7 and later.
Companion guide
Declared in
AVCaptureSession.h
Related sample code

Overview

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:

AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
if (audioInput) {
    [captureSession addInput:audioInput];
}
else {
    // Handle the failure.
}

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.

Tasks

Managing Inputs and Outputs

Managing Running State

Configuration Change

Managing Session Presets

Managing Connections

Synchronizing Multiple Inputs and Outputs

Properties

inputs

The capture session’s inputs. (read-only)

@property(nonatomic, readonly) NSArray *inputs
Discussion

The array contains instances of subclasses of AVCaptureInput.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

masterClock

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

@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.
Declared In
AVCaptureSession.h

outputs

The capture session’s outputs. (read-only)

@property(nonatomic, readonly) NSArray *outputs
Discussion

The array contains instances of subclasses of AVCaptureOutput.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

running

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

@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.
Declared In
AVCaptureSession.h

sessionPreset

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

@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 YES for that preset.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

Instance Methods

addConnection:

Adds a given capture connection to the session.

- (void)addConnection:(AVCaptureConnection *)connection
Parameters
connection

The capture connection to add to the session.

Discussion

You can only add an AVCaptureConnection instance to a session using this method if canAddConnection: returns YES.

When using addInput: or addOutput:, connections are formed automatically between all compatible inputs and outputs. Manually adding connections is only necessary when adding an input or output with no connections.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

addInput:

Adds a given input to the session.

- (void)addInput:(AVCaptureInput *)input
Parameters
input

An input to add to the session.

Discussion

You can only add an input to a session using this method if canAddInput: returns YES.

You can invoke this method while the session is running.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

addInputWithNoConnections:

Adds an capture input to the session without forming any connections.

- (void)addInputWithNoConnections:(AVCaptureInput *)input
Parameters
input

The capture input to add to the session.

Discussion

You can invoke this method while the session is running.

Typically you should use addInput: to add an input to a session. You use this method if you need fine-grained control over which inputs are connected to which outputs.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

addOutput:

Adds a given output to the session.

- (void)addOutput:(AVCaptureOutput *)output
Parameters
output

An output to add to the session.

Discussion

You can only add an output to a session using this method if canAddOutput: returns YES.

You can invoke this method while the session is running.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

addOutputWithNoConnections:

Adds an capture output to the session without forming any connections.

- (void)addOutputWithNoConnections:(AVCaptureOutput *)output
Parameters
output

The capture output to add to the session.

Discussion

You can invoke this method while the session is running.

Typically you should use addOutput: to add an output to a session. You use this method if you need fine-grained control over which inputs are connected to which outputs.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

beginConfiguration

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

- (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.
Declared In
AVCaptureSession.h

canAddConnection:

Returns a Boolean value that indicates whether a given connection can be added to the receiver.

- (BOOL)canAddConnection:(AVCaptureConnection *)connection
Parameters
connection

An AVCaptureConnection instance.

Return Value

YES if connection can be added to the receiver, otherwise NO.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

canAddInput:

Returns a Boolean value that indicates whether a given input can be added to the session.

- (BOOL)canAddInput:(AVCaptureInput *)input
Parameters
input

An input that you want to add to the session.

Return Value

YES if input can be added to the session, otherwise NO.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

canAddOutput:

Returns a Boolean value that indicates whether a given output can be added to the session.

- (BOOL)canAddOutput:(AVCaptureOutput *)output
Parameters
output

An output that you want to add to the session.

Return Value

YES if output can be added to the session, otherwise NO.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

canSetSessionPreset:

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

- (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

YES if the receiver can use preset, otherwise NO.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

commitConfiguration

Commits a set of configuration changes.

- (void)commitConfiguration
Discussion

For discussion, see beginConfiguration.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

removeConnection:

Removes a capture connection from the session.

- (void)removeConnection:(AVCaptureConnection *)connection
Parameters
connection

The capture connection to remove from the session.

Discussion

You can invoke this method while the session is running.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

removeInput:

Removes a given input.

- (void)removeInput:(AVCaptureInput *)input
Parameters
input

An input to remove from the receiver.

Discussion

You can invoke this method while the session is running.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

removeOutput:

Removes a given output.

- (void)removeOutput:(AVCaptureOutput *)output
Parameters
output

An output to remove from the receiver.

Discussion

You can invoke this method while the session is running.

Availability
  • Available in OS X v10.7 and later.
Declared In
AVCaptureSession.h

startRunning

Tells the receiver to start running.

- (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.
Declared In
AVCaptureSession.h

stopRunning

Tells the receiver to stop running.

- (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.
Declared In
AVCaptureSession.h

Constants

AVCaptureVideoOrientation

Constants to specify the device orientation during video capture.

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

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureVideoOrientationPortraitUpsideDown

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureVideoOrientationLandscapeLeft

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureVideoOrientationLandscapeRight

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

Notification User Info Key

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

NSString *const AVCaptureSessionErrorKey;
Constants
AVCaptureSessionErrorKey

Key to retrieve the error object from the user info dictionary of an AVCaptureSessionRuntimeErrorNotification.

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

Video Input Presets

Constants to define capture setting presets using the sessionPreset property.

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

Specifies capture settings suitable for high resolution photo quality output.

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPresetHigh

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPresetMedium

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPresetLow

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPreset320x240

Specifies capture settings suitable for 320x240 pixel video output.

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPreset352x288

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPreset640x480

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPreset960x540

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

AVCaptureSessionPreset1280x720

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

Available in OS X v10.7 and later.

Declared in AVCaptureSession.h.

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.

Declared in AVCaptureSession.h.

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.

Declared in AVCaptureSession.h.

Notifications

AVCaptureSessionRuntimeErrorNotification

Posted if an error occurred during a capture session.

You retrieve the underlying error from the notification’s user info dictionary using the key AVCaptureSessionErrorKey.

Availability
Declared In
AVCaptureSession.h

AVCaptureSessionDidStartRunningNotification

Posted when a capture session starts.
Availability
Declared In
AVCaptureSession.h

AVCaptureSessionDidStopRunningNotification

Posted when a capture session stops.
Availability
Declared In
AVCaptureSession.h