Share via


AVCaptureSession Class

Definition

Coordinates a recording session.

[Foundation.Register("AVCaptureSession", true)]
public class AVCaptureSession : Foundation.NSObject
[<Foundation.Register("AVCaptureSession", true)>]
type AVCaptureSession = class
    inherit NSObject
Inheritance
AVCaptureSession
Derived
Attributes

Remarks

The AVCaptureSession object coordinates the recording of video or audio input and passing the recorded information to one or more output objects. As the iOS line has advanced, different devices have gained multiple capture devices (in particular, gained multiple cameras). Application developers can use GetDefaultDevice(AVMediaTypes) or DevicesWithMediaType(String), passing in the constants defined in AVMediaTypes.

Configuring capture consists of setting the Inputs and Outputs properties of the AVCaptureSession. Notice that multiple AVCaptureInputs and AVCaptureOutputs are possible. For instance, to capture both audio and video, one would use two capture inputs:

var session = new AVCaptureSession();

var camera = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);
var mic = AVCaptureDevice.GetDefaultDevice(AVMediaType.Audio);
if(camera == null || mic == null){
throw new Exception("Can't find devices");
}

var cameraInput = AVCaptureDeviceInput.FromDevice (camera);
//info.plist _must_ contain NSMicrophoneUsageDescription key
var micInput = AVCaptureDeviceInput.FromDevice (mic);

if(session.CanAddInput(cameraInput)){
session.AddInput(cameraInput);
}
if(session.CanAddInput(micInput)){
session.AddInput(micInput);
}

Note that permission to access the microphone (and in some regions, the camera) must be given by the user, requiring the developer to add the NSMicrophoneUsageDescription to the application's info.plist file.

Video can be captured directly to file with AVCaptureMovieFileOutput. However, this class has no display-able data and cannot be used simultaneously with AVCaptureVideoDataOutput. Instead, application developers can use it in combination with a AVCaptureVideoPreviewLayer, as shown in the following example:

var layer = new AVCaptureVideoPreviewLayer (session);
layer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;

var cameraView = new UIView ();
cameraView.Layer.AddSublayer (layer);

var filePath = Path.Combine (Path.GetTempPath (), "temporary.mov");
var fileUrl = NSUrl.FromFilename (filePath);

var movieFileOutput = new AVCaptureMovieFileOutput ();
var recordingDelegate = new MyRecordingDelegate ();
session.AddOutput (movieFileOutput);

movieFileOutput.StartRecordingToOutputFile (fileUrl, recordingDelegate);

Application developers should note that the function StopRecording() is asynchronous; developers should wait until the FinishedRecording(AVCaptureFileOutput, NSUrl, NSObject[], NSError) delegate method before manipulating the file.

public class MyRecordingDelegate : AVCaptureFileOutputRecordingDelegate
{
public override void FinishedRecording (AVCaptureFileOutput captureOutput, NSUrl outputFileUrl, NSObject [] connections, NSError error)
{
if (UIVideo.IsCompatibleWithSavedPhotosAlbum (outputFileUrl.Path))
{
var library = new ALAssetsLibrary ();
library.WriteVideoToSavedPhotosAlbum (outputFileUrl, (path, e2) =>
{
if (e2 != null)
{
new UIAlertView ("Error", e2.ToString (), null, "OK", null).Show ();
}
else
{
new UIAlertView ("Saved", "Saved to Photos", null, "OK", null).Show ();
File.Delete (outputFileUrl.Path);
}
});
}
else
{
new UIAlertView ("Incompatible", "Incompatible", null, "OK", null).Show ();
}

}
}          

Application developers can configure one or more output ports for the captured data, and these can be still frames, video frames with timing information, audio samples, quicktime movie files, or can be rendered directly to a CoreAnimation layer.

Once the input and output components of the session are set, the actual processing is begun by calling the StartRunning() method.


void SetupCapture ()
	/ configure the capture session for low resolution, change this if your code
	// can cope with more data or volume
	session = new AVCaptureSession () {
	        SessionPreset = AVCaptureSession.PresetMedium
	};

	// create a device input and attach it to the session
	var captureDevice = AVCaptureDevice.GetDefaultDevice (AVMediaTypes.Video);
	var input = AVCaptureDeviceInput.FromDevice (captureDevice);
	if (input == null){
	        Console.WriteLine ("No video input device");
	        return false;
	}
	session.AddInput (input);

	// create a VideoDataOutput and add it to the sesion
	var output = new AVCaptureVideoDataOutput () {
	        VideoSettings = new AVVideoSettings (CVPixelFormatType.CV32BGRA),

	        // If you want to cap the frame rate at a given speed, in this sample: 15 frames per second
	        MinFrameDuration = new CMTime (1, 15)
	};

	// configure the output
	queue = new MonoTouch.CoreFoundation.DispatchQueue ("myQueue");
	outputRecorder = new OutputRecorder ();
	output.SetSampleBufferDelegateAndQueue (outputRecorder, queue);
	session.AddOutput (output);

	session.StartRunning ();
}

public class OutputRecorder : AVCaptureVideoDataOutputSampleBufferDelegate {
public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
{
try {
var image = ImageFromSampleBuffer (sampleBuffer);

// Do something with the image, we just stuff it in our main view.
AppDelegate.ImageView.BeginInvokeOnMainThread (delegate {
AppDelegate.ImageView.Image = image;
});

//
// Although this looks innocent "Oh, he is just optimizing this case away"
// this is incredibly important to call on this callback, because the AVFoundation
// has a fixed number of buffers and if it runs out of free buffers, it will stop
// delivering frames.
//
sampleBuffer.Dispose ();
} catch (Exception e){
Console.WriteLine (e);
}
}

UIImage ImageFromSampleBuffer (CMSampleBuffer sampleBuffer)
{
// Get the CoreVideo image
using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer){
// Lock the base address
pixelBuffer.Lock (0);
// Get the number of bytes per row for the pixel buffer
var baseAddress = pixelBuffer.BaseAddress;
int bytesPerRow = pixelBuffer.BytesPerRow;
int width = pixelBuffer.Width;
int height = pixelBuffer.Height;
var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
// Create a CGImage on the RGB colorspace from the configured parameter above
using (var cs = CGColorSpace.CreateDeviceRGB ())
using (var context = new CGBitmapContext (baseAddress,width, height, 8, bytesPerRow, cs, (CGImageAlphaInfo) flags))
using (var cgImage = context.ToImage ()){
pixelBuffer.Unlock (0);
return UIImage.FromImage (cgImage);
}
}
}
}

Constructors

Name Description
AVCaptureSession()

Creates a new AVCaptureSession with default values.

AVCaptureSession(NativeHandle)

A constructor used when creating managed representations of unmanaged objects. Called by the runtime.

AVCaptureSession(NSObjectFlag)

Constructor to call on derived classes to skip initialization and merely allocate the object.

Properties

Name Description
AccessibilityAttributedUserInputLabels (Inherited from NSObject)
AccessibilityRespondsToUserInteraction (Inherited from NSObject)
AccessibilityTextualContext (Inherited from NSObject)
AccessibilityUserInputLabels (Inherited from NSObject)
AutomaticallyConfiguresApplicationAudioSession
AutomaticallyConfiguresCaptureDeviceForWideColor
AutomaticallyRunsDeferredStart
Class (Inherited from NSObject)
ClassHandle

The Objective-C class handle for this class.

ConfiguresApplicationAudioSessionForBluetoothHighQualityRecording
ConfiguresApplicationAudioSessionToMixWithOthers
Connections
Controls
ControlsDelegate
ControlsDelegateCallbackQueue
DebugDescription (Inherited from NSObject)
DeferredStartDelegate
DeferredStartDelegateCallbackQueue
Description (Inherited from NSObject)
DidStartRunningNotification

Notification constant for DidStartRunning

DidStopRunningNotification

Notification constant for DidStopRunning

ErrorKey

Represents the value associated with the constant AVCaptureSessionErrorKey

ExposedBindings (Inherited from NSObject)
Handle

Handle (pointer) to the unmanaged object representation.

(Inherited from NSObject)
HardwareCost
Inputs
Interrupted

Whether the session has been interrupted.

InterruptionEndedNotification

Notification constant for InterruptionEnded

InterruptionReasonKey

Gets a key that accesses the reason that a capture session was interrupted.

InterruptionSystemPressureStateKey
IsDirectBinding (Inherited from NSObject)
IsProxy (Inherited from NSObject)
ManualDeferredStartSupported
MasterClock
MaxControlsCount
MultitaskingCameraAccessEnabled
MultitaskingCameraAccessSupported
Outputs
Preset1280x720

Represents the value associated with the constant AVCaptureSessionPreset1280x720

Preset1920x1080

Represents the value associated with the constant AVCaptureSessionPreset1920x1080

Preset320x240
Preset352x288

Represents the value associated with the constant AVCaptureSessionPreset352x288

Preset3840x2160

Represents the value associated with the constant AVCaptureSessionPreset3840x2160.

Preset640x480

Represents the value associated with the constant AVCaptureSessionPreset640x480

Preset960x540
PresetHigh

Represents the value associated with the constant AVCaptureSessionPresetHigh

PresetiFrame1280x720

Represents the value associated with the constant AVCaptureSessionPresetiFrame1280x720

PresetiFrame960x540

Represents the value associated with the constant AVCaptureSessionPresetiFrame960x540

PresetInputPriority

Represents the value associated with the constant AVCaptureSessionPresetInputPriority

PresetLow

Represents the value associated with the constant AVCaptureSessionPresetLow

PresetMedium

Represents the value associated with the constant AVCaptureSessionPresetMedium

PresetPhoto

Represents the value associated with the constant AVCaptureSessionPresetPhoto

RetainCount (Inherited from NSObject)
Running

Whether the capture session is currently running.

RuntimeErrorNotification

Notification constant for RuntimeError

Self (Inherited from NSObject)
SessionPreset
Superclass (Inherited from NSObject)
SuperHandle

Handle used to represent the methods in the base class for this NSObject.

(Inherited from NSObject)
SupportsControls
SynchronizationClock
UsesApplicationAudioSession
WasInterruptedNotification

Notification constant for WasInterrupted

WeakControlsDelegate
Zone (Inherited from NSObject)

Methods

Name Description
AddConnection(AVCaptureConnection)
AddControl(AVCaptureControl)
AddInput(AVCaptureInput)
AddInputWithNoConnections(AVCaptureInput)
AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr) (Inherited from NSObject)
AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr) (Inherited from NSObject)
AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AddOutput(AVCaptureOutput)
AddOutputWithNoConnections(AVCaptureOutput)
AwakeFromNib() (Inherited from NSObject)
BeginConfiguration()
BeginInvokeOnMainThread(Action) (Inherited from NSObject)
BeginInvokeOnMainThread(Selector, NSObject)

Invokes asynchrously the specified code on the main UI thread.

(Inherited from NSObject)
Bind(NSString, NSObject, String, NSBindingOptions) (Inherited from NSObject)
Bind(NSString, NSObject, String, NSDictionary) (Inherited from NSObject)
CanAddConnection(AVCaptureConnection)
CanAddControl(AVCaptureControl)
CanAddInput(AVCaptureInput)
CanAddOutput(AVCaptureOutput)
CanSetSessionPreset(NSString)
CommitConfiguration()
CommitEditing() (Inherited from NSObject)
CommitEditing(NSObject, Selector, IntPtr) (Inherited from NSObject)
ConformsToProtocol(NativeHandle) (Inherited from NSObject)
Copy() (Inherited from NSObject)
DangerousAutorelease()

Calls the 'autorelease' selector on this object.

(Inherited from NSObject)
DangerousRelease()

Calls the 'release' selector on this object.

(Inherited from NSObject)
DangerousRetain()

Calls the 'retain' selector on this object.

(Inherited from NSObject)
DidChange(NSKeyValueChange, NSIndexSet, NSString) (Inherited from NSObject)
DidChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
DidChangeValue(String) (Inherited from NSObject)
Dispose()

Releases the resources used by the NSObject object.

(Inherited from NSObject)
Dispose(Boolean)

Releases the resources used by the NSObject object.

(Inherited from NSObject)
DoesNotRecognizeSelector(Selector) (Inherited from NSObject)
Equals(NSObject) (Inherited from NSObject)
Equals(Object) (Inherited from NSObject)
GetBindingInfo(NSString) (Inherited from NSObject)
GetBindingOptionDescriptions(NSString) (Inherited from NSObject)
GetBindingValueClass(NSString) (Inherited from NSObject)
GetDictionaryOfValuesFromKeys(NSString[]) (Inherited from NSObject)
GetHashCode()

Generates a hash code for the current instance.

(Inherited from NSObject)
GetMethodForSelector(Selector) (Inherited from NSObject)
GetNativeHash() (Inherited from NSObject)
Init() (Inherited from NSObject)
InitializeHandle(NativeHandle, String) (Inherited from NSObject)
InitializeHandle(NativeHandle) (Inherited from NSObject)
Invoke(Action, Double) (Inherited from NSObject)
Invoke(Action, TimeSpan) (Inherited from NSObject)
InvokeOnMainThread(Action) (Inherited from NSObject)
InvokeOnMainThread(Selector, NSObject)

Invokes synchrously the specified code on the main UI thread.

(Inherited from NSObject)
IsEqual(NSObject) (Inherited from NSObject)
IsKindOfClass(Class) (Inherited from NSObject)
IsMemberOfClass(Class) (Inherited from NSObject)
MarkDirty()

Promotes a regular peer object (IsDirectBinding is true) into a toggleref object.

(Inherited from NSObject)
MutableCopy() (Inherited from NSObject)
ObjectDidEndEditing(NSObject) (Inherited from NSObject)
ObserveValue(NSString, NSObject, NSDictionary, IntPtr) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double) (Inherited from NSObject)
PerformSelector(Selector, NSObject, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean) (Inherited from NSObject)
PerformSelector(Selector) (Inherited from NSObject)
PrepareForInterfaceBuilder() (Inherited from NSObject)
RemoveConnection(AVCaptureConnection)
RemoveControl(AVCaptureControl)
RemoveInput(AVCaptureInput)
RemoveObserver(NSObject, NSString, IntPtr) (Inherited from NSObject)
RemoveObserver(NSObject, NSString) (Inherited from NSObject)
RemoveObserver(NSObject, String, IntPtr) (Inherited from NSObject)
RemoveObserver(NSObject, String) (Inherited from NSObject)
RemoveOutput(AVCaptureOutput)
RespondsToSelector(Selector) (Inherited from NSObject)
RunDeferredStartWhenNeeded()
SetControlsDelegate(IAVCaptureSessionControlsDelegate, DispatchQueue)
SetDeferredStartDelegate(IAVCaptureSessionDeferredStartDelegate, DispatchQueue)
SetNilValueForKey(NSString) (Inherited from NSObject)
SetValueForKey(NSObject, NSString) (Inherited from NSObject)
SetValueForKeyPath(NativeHandle, NSString) (Inherited from NSObject)
SetValueForKeyPath(NSObject, NSString) (Inherited from NSObject)
SetValueForUndefinedKey(NSObject, NSString) (Inherited from NSObject)
SetValuesForKeysWithDictionary(NSDictionary) (Inherited from NSObject)
StartRunning()
StopRunning()
ToString()

Returns a string representation of the value of the current instance.

(Inherited from NSObject)
Unbind(NSString) (Inherited from NSObject)
ValueForKey(NSString) (Inherited from NSObject)
ValueForKeyPath(NSString) (Inherited from NSObject)
ValueForUndefinedKey(NSString) (Inherited from NSObject)
WillChange(NSKeyValueChange, NSIndexSet, NSString) (Inherited from NSObject)
WillChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
WillChangeValue(String) (Inherited from NSObject)

Extension Methods

Name Description
ObjectDidBeginEditing(NSObject, INSEditor)
ObjectDidEndEditing(NSObject, INSEditor)
GetValidModes(NSObject, NSFontPanel)
ValidateToolbarItem(NSObject, NSToolbarItem)
BrowserAccessibilityDeleteTextAtCursor(NSObject, IntPtr)
BrowserAccessibilityInsertTextAtCursor(NSObject, String)
GetAccessibilityLineEndPositionFromCurrentSelection(NSObject)
GetAccessibilityLineRangeForPosition(NSObject, IntPtr)
GetAccessibilityLineStartPositionFromCurrentSelection(NSObject)
GetBrowserAccessibilityAttributedValue(NSObject, NSRange)
GetBrowserAccessibilityContainerType(NSObject)
GetBrowserAccessibilityCurrentStatus(NSObject)
GetBrowserAccessibilityHasDomFocus(NSObject)
GetBrowserAccessibilityIsRequired(NSObject)
GetBrowserAccessibilityPressedState(NSObject)
GetBrowserAccessibilityRoleDescription(NSObject)
GetBrowserAccessibilitySelectedTextRange(NSObject)
GetBrowserAccessibilitySortDirection(NSObject)
GetBrowserAccessibilityValue(NSObject, NSRange)
SetBrowserAccessibilityContainerType(NSObject, BEAccessibilityContainerType)
SetBrowserAccessibilityCurrentStatus(NSObject, String)
SetBrowserAccessibilityHasDomFocus(NSObject, Boolean)
SetBrowserAccessibilityIsRequired(NSObject, Boolean)
SetBrowserAccessibilityPressedState(NSObject, BEAccessibilityPressedState)
SetBrowserAccessibilityRoleDescription(NSObject, String)
SetBrowserAccessibilitySelectedTextRange(NSObject, NSRange)
SetBrowserAccessibilitySortDirection(NSObject, String)
ProvideImageData(NSObject, IntPtr, UIntPtr, UIntPtr, UIntPtr, UIntPtr, UIntPtr, NSObject)
ProvideImageToMTLTexture(NSObject, IMTLTexture, IMTLCommandBuffer, UIntPtr, UIntPtr, UIntPtr, UIntPtr, NSObject)
SetSharedObservers(NSObject, NSKeyValueSharedObserversSnapshot)
GetDebugDescription(INSObjectProtocol)
GetHandle(INativeObject)
GetNonNullHandle(INativeObject, String)
AcceptsPreviewPanelControl(NSObject, QLPreviewPanel)
BeginPreviewPanelControl(NSObject, QLPreviewPanel)
EndPreviewPanelControl(NSObject, QLPreviewPanel)
GetAccessibilityCustomRotors(NSObject)

Gets the array of UIAccessibilityCustomRotor objects appropriate for this object.

SetAccessibilityCustomRotors(NSObject, UIAccessibilityCustomRotor[])

Sets the array of UIAccessibilityCustomRotor objects appropriate for this object.

AccessibilityHitTest(NSObject, CGPoint, UIEvent)
GetAccessibilityNextTextNavigationElement(NSObject)
GetAccessibilityNextTextNavigationElementBlock(NSObject)
GetAccessibilityPreviousTextNavigationElement(NSObject)
GetAccessibilityPreviousTextNavigationElementBlock(NSObject)
SetAccessibilityNextTextNavigationElement(NSObject, NSObject)
SetAccessibilityNextTextNavigationElementBlock(NSObject, AXObjectReturnBlock)
SetAccessibilityPreviousTextNavigationElement(NSObject, NSObject)
SetAccessibilityPreviousTextNavigationElementBlock(NSObject, AXObjectReturnBlock)
GetAccessibilityTextInputResponder(NSObject)
GetAccessibilityTextInputResponderHandler(NSObject)
SetAccessibilityTextInputResponder(NSObject, IUITextInput)
SetAccessibilityTextInputResponderHandler(NSObject, UITextInputReturnHandler)

Applies to

See also