Skip to main content
Version: 20 Mar 2024

MLCameraBase

NameSpace: MagicLeap

MLCamera class exposes static functions to query camera related functions. Most functions are currently a direct pass through functions to the native C-API functions and incur no overhead. More...

Inherits from:
MLAPIBase

Inherited by:
UnityEngine.XR.MagicLeap.MLCamera

Detailed Description

public class MLCameraBase : MLAPIBase 

Details*

APIs for accessing Camera Device and to do Camera Capture.


Public Fields

OnDeviceAvailable

Camera status callback, device available.


public static MLCamera.OnDeviceAvailabilityStatusDelegate OnDeviceAvailable { get; set; }


OnDeviceUnavailable

Camera status callback, device unavailable.


public static MLCamera.OnDeviceAvailabilityStatusDelegate OnDeviceUnavailable { get; set; }


Public Methods

MLResult CaptureImage

Capture still image. Result will be availble via the MLCamera.OnRawImageAvailable event.

public MLResult CaptureImage(
uint numImages =1
)

Parameters

TypeNameDescription
uintnumImagesno of images to capture valid range is 1-10

MLResult CapturePreviewStart

Start preview provide raw frames through callback

public MLResult CapturePreviewStart()

MLResult CapturePreviewStop

Stop preview.

public MLResult CapturePreviewStop()

Returns:


MLResult CaptureVideoStart

Start video capture. capture either encoded video or YUV/RGBA frames. YUV/RGBA frames are provides through callback. For encoded video capture valid MediaRecorder object should be configured in the PrepareCapture() call. The captured video YUV/RGBA frames will be returned via the OnRawVideoFrameAvailable and OnRawVideoFrameAvailable_NativeCallbackThread events.

public MLResult CaptureVideoStart()

MLResult CaptureVideoStop

Stop video capture. Should only be called at least 500ms after a CaptureVideoStart().

public MLResult CaptureVideoStop()

void Cleanup

public void Cleanup()

void ClearPreviewTexture

public void ClearPreviewTexture()

MLResult Connect

public MLResult Connect(
MLCamera.ConnectContext connectContext
)

Parameters

TypeNameDescription
MLCamera.ConnectContextconnectContextCamera connection settings.

MLCameraBase Create

public static MLCameraBase Create()

MLResult Disconnect

Disconnect the current camera. Should call MLCamera.Connect() again on this instance or create a new MLCamera object using MLCamera.CreateAndConnect() function.

public MLResult Disconnect()

void FlipFrameVertically

public static void FlipFrameVertically(
ref CameraOutput frame
)

Parameters

TypeNameDescription
ref CameraOutputframeContains the information and data of each of the available buffers/planes in a capture Number of planes for JPEG and RGBA captures = 1 Number of planes for YUV captures = 3

MLResult GetCameraCharacteristics

Provides an MLCamera.Metadata object to configure the camera settings.

public MLResult GetCameraCharacteristics(
out MLCamera.Metadata cameraMetadata
)

Parameters

TypeNameDescription
out MLCamera.MetadatacameraMetadataMLCameraMetadata Summary placeholder.

MLResult GetDeviceAvailabilityStatus

Check availablity of a given camera device. MLCamera instance should only be created & connected when the device is available.

public static MLResult GetDeviceAvailabilityStatus(
MLCamera.Identifier camId,
out bool deviceAvailable
)

Parameters

TypeNameDescription
MLCamera.IdentifiercamIdCamera id to check availability for
out booldeviceAvailableTrue if device is available, false otherwise

Returns:


MLResult GetDeviceStatus

Poll camera device status. Use DeviceStatusFlag to view specific status bit. Call GetErrorCode(out ErrorType) to obtain the error code if DeviceStatusFlag.Error bit is set.

public MLResult GetDeviceStatus(
out MLCamera.DeviceStatusFlag status
)

Parameters

TypeNameDescription
out MLCamera.DeviceStatusFlagstatus

Returns:


MLResult GetErrorCode

Obtain device error code.

public MLResult GetErrorCode(
out MLCamera.ErrorType error
)

Parameters

TypeNameDescription
out MLCamera.ErrorTypeerrorThe ErrorType which triggered DeviceStatusFlag.Error

Returns:


StreamCapability [] GetImageStreamCapabilitiesForCamera

public static StreamCapability [] GetImageStreamCapabilitiesForCamera(
MLCamera camera,
params CaptureType [] captureTypes
)

Parameters

TypeNameDescription
MLCameracameraProvides a multithreadig implementation of the camera api. Handles pause behavior automatically.
params CaptureType []captureTypesCapture operation type

MLResult GetStreamCapabilities

Get the StreamCapabilitiesInfo for the connected camera, which contains the stream capabilities for each supported capture stream.

public MLResult GetStreamCapabilities(
out MLCamera.StreamCapabilitiesInfo [] streamCapabilities
)

Parameters

TypeNameDescription
out MLCamera.StreamCapabilitiesInfo []streamCapabilitiesEach item in the array refers to a possible capture stream. Every index within [StreamCapabilitiesInfo.StreamCapabilities] represents a StreamCapability of that particular stream.

Details*

One camera can support multiple capture streams and each stream has it's own set of StreamCapabilities (capture types and resolutions).


bool IsCaptureTypeSupported

public static bool IsCaptureTypeSupported(
MLCamera camera,
CaptureType captureType
)

Parameters

TypeNameDescription
MLCameracameraProvides a multithreadig implementation of the camera api. Handles pause behavior automatically.
CaptureTypecaptureTypeCapture operation type

delegate void OnCaptureAbortedDelegate

Delegate to notify the app when a capture request is aborted.

public delegate void OnCaptureAbortedDelegate()

delegate void OnCaptureCompletedDelegate

Delegate to notify the app when a capture request is completed.

public delegate void OnCaptureCompletedDelegate(
Metadata metadataHandle,
ResultExtras extra
)

Parameters

TypeNameDescription
MetadatametadataHandleHandle to metadata of captured frame.
ResultExtrasextraCarries capture result information of current captured frame.

delegate void OnCaptureFailedDelegate

Delegate to notify the app when a capture request fails.

public delegate void OnCaptureFailedDelegate(
ResultExtras extra
)

Parameters

TypeNameDescription
ResultExtrasextraCarries capture result information of current captured frame.

delegate void OnCapturedFrameAvailableDelegate

Delegate to notify the app when the frame data of a capture is available.

public delegate void OnCapturedFrameAvailableDelegate(
CameraOutput frameInfo,
ResultExtras resultExtras,
Metadata metadataHandle
)

Parameters

TypeNameDescription
CameraOutputframeInfoFrame data
ResultExtrasextraCarries capture result information of current captured frame.
MetadatametadataHandleMLCameraMetadata Summary placeholder.

delegate void OnDeviceAvailabilityStatusDelegate

public delegate void OnDeviceAvailabilityStatusDelegate(
Identifier camId
)

Parameters

TypeNameDescription
IdentifiercamIdLogical Camera identifiers available for access.

delegate void OnDeviceDisconnectedDelegate

Delegate to notify the app when the camera device disconnects.

public delegate void OnDeviceDisconnectedDelegate(
DisconnectReason reason
)

Parameters

TypeNameDescription
DisconnectReasonreasonDisconnect reason

delegate void OnDeviceErrorDelegate

Delegate to notify the app about camera device error

public delegate void OnDeviceErrorDelegate(
ErrorType error
)

Parameters

TypeNameDescription
ErrorTypeerrorError code

delegate void OnDeviceStatusDelegate

Delegate to notify the app about camera device status (idle or streaming)

public delegate void OnDeviceStatusDelegate()

delegate void OnPreviewBufferAvailableDelegate

Delegate to notify the app when the frame data of a preview is available.

public delegate void OnPreviewBufferAvailableDelegate(
Metadata metadataHandle,
ResultExtras extra
)

Parameters

TypeNameDescription
MetadatametadataHandleHandle to metadata of captured frame.
ResultExtrasextraCarries capture result information of current captured frame.

delegate void OnPreviewCaptureCompletedDelegate

Delegate to notify the app when the result of a preview capture is available.

public delegate void OnPreviewCaptureCompletedDelegate(
MLResult result
)

Parameters

TypeNameDescription
MLResultresultResult reporting whether the preview capture completed or not.

MLResult PreCaptureAEAWB

Trigger AEAWB Convergence. This API triggers AEAWB sequence and returns when AEAWB convergence has been achieved. The users can either trigger and converge AEAWB metadata settings or use this API.

public MLResult PreCaptureAEAWB()

MLResult PrepareCapture

Prepare for capture.

public MLResult PrepareCapture(
MLCamera.CaptureConfig captureConfig,
out MLCamera.Metadata cameraMetadata
)

Parameters

TypeNameDescription
MLCamera.CaptureConfigcaptureConfigCapture configuration.
out MLCamera.MetadatacameraMetadataCamera metadata object to configure the camera for captures.

bool TryGetBestFitStreamCapabilityFromCollection

public static bool TryGetBestFitStreamCapabilityFromCollection(
StreamCapability [] streamCapabilities,
int width,
int height,
CaptureType captureType,
out StreamCapability streamCapability
)

Parameters

TypeNameDescription
StreamCapability []streamCapabilities
intwidth
intheight
CaptureTypecaptureTypeCapture operation type
out StreamCapabilitystreamCapability

MLResult Uninitialize

public static MLResult Uninitialize()

MLResult UpdateCaptureSettings

Update capture setting This API updates the capture settings for an active preview/video stream. The API can be used to update capture settings like exposure compensation manual exposure settings like exposure time, iso setting. The capture settings can be changed in MLCamera.Metadata object returned by MLCamera.PrepareCapture() function and then this API can be called to apply the capture settings.

public MLResult UpdateCaptureSettings()

Protected Methods

CreatePreviewTexture

Create a preview texture.

protected void CreatePreviewTexture()

GLPluginEvent

Issues an event when a render event has occurred.

protected void GLPluginEvent()

HandleOnRawVideoFrameAvailableInternal

Handles the event of a new image getting captured.

protected void HandleOnRawVideoFrameAvailableInternal(
MLCamera.CameraOutput capturedFrame,
MLCamera.ResultExtras resultExtras,
MLCamera.Metadata metadataHandle
)

Parameters

TypeNameDescription
MLCamera.CameraOutputimageDataThe raw data of the image.
MLCamera.ResultExtrasresultExtras
MLCamera.MetadatametadataHandleMLCameraMetadata Summary placeholder.

InternalCheckCameraPermission

protected static MLResult.Code InternalCheckCameraPermission()

InternalConnect

protected MLResult.Code InternalConnect(
MLCamera.ConnectContext cameraConnectContext
)

Parameters

TypeNameDescription
MLCamera.ConnectContextcameraConnectContextCamera connection settings.

InternalDisconnect

Disconnect the camera.

protected MLResult.Code InternalDisconnect(
bool flagsOnly =false,
bool isAsync =false
)

Parameters

TypeNameDescription
boolflagsOnlySet to true if you only need to disconect the camera and not stop any ongoing captuers. Useful when disconnecting due to internal errors. False by default.
boolisAsync

Returns: MLResult.Result will be MLResult.Code.Ok if completed successfully. MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to internal error. MLResult.Result will be MLResult.Code.PermissionDenied if a required permission is missing.


InternalGetStreamCapabilities

protected MLResult InternalGetStreamCapabilities(
out MLCamera.StreamCapabilitiesInfo [] streamCapabilities
)

Parameters

TypeNameDescription
out MLCamera.StreamCapabilitiesInfo []streamCapabilities

InternalInitialize

Establish a connection to the camera.

protected static MLResult InternalInitialize()

Returns: MLResult.Result will be MLResult.Code.Ok if connected to camera device successfully. MLResult.Result will be MLResult.Code.MediaGenericUnexpectedNull if failed to connect to camera device due to null pointer. MLResult.Result will be MLResult.Code.Timeout if failed connecting the camera due to timeout. MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to internal error. MLResult.Result will be MLResult.Code.AllocFailed if failed to allocate memory. MLResult.Result will be MLResult.Code.PermissionDenied if a required permission is missing.


InternalPrepareCapture

protected MLResult.Code InternalPrepareCapture(
MLCamera.CaptureConfig captureConfig,
out MLCamera.Metadata cameraMetadata
)

Parameters

TypeNameDescription
MLCamera.CaptureConfigcaptureConfig
out MLCamera.MetadatacameraMetadataMLCameraMetadata Summary placeholder.

InternalUninitialize

protected static MLResult InternalUninitialize()

MLCameraBase

protected MLCameraBase()

OnApplicationPause

Callback sent to all MagicLeap APIs on application pause.

protected virtual override void OnApplicationPause(
bool pauseStatus
)

Parameters

TypeNameDescription
boolpauseStatusTrue if the application is paused, else False.

Reimplements: OnApplicationPause


Public Attributes

ConnectionContext

The connection context used to create this MLCamera object.


public MLCamera.ConnectContext ConnectionContext => cameraConnectContext;

TypeDescription
MLCamera.ConnectContextCamera connection settings.

ConnectionEstablished

Connection status of the camera.


public bool ConnectionEstablished => cameraConnectionEstablished;


CurrentFPS


public float CurrentFPS => currentFPS;


PreviewTexture

A reference to Camera Preview texture.


public RenderTexture PreviewTexture => previewTexture;


Protected Attributes

byteArrays


protected byte [][] byteArrays;


cameraCaptureConfig

Last used camera Capture Config


protected MLCamera.CaptureConfig cameraCaptureConfig;


cameraConnectContext

camera Connect Context


protected MLCamera.ConnectContext cameraConnectContext;

TypeDescription
MLCamera.ConnectContextCamera connection settings.

cameraConnectionEstablished

Connection status of the camera.


protected bool cameraConnectionEstablished = false;


cameraInited


protected static bool cameraInited = false;


currentFPS

Calculated FPS based on recevied frames interval.


protected float currentFPS;


gcHandle

GC Handle.


protected GCHandle gcHandle;


internalOnDeviceAvailable


protected static MLCamera.OnDeviceAvailabilityStatusDelegate internalOnDeviceAvailable;


internalOnDeviceUnavailable


protected static MLCamera.OnDeviceAvailabilityStatusDelegate internalOnDeviceUnavailable;


isCapturingPreview

Gets a value indicating whether the camera preview is enabled and textures are being rendered.


protected bool isCapturingPreview = false;


isCapturingVideo

Capture status of the camera.


protected bool isCapturingVideo = false;


previewRenderer

Preview Renderer;


protected MLCamera.Renderer previewRenderer;


previewTexture

A reference to Camera Preview texture.


protected RenderTexture previewTexture;


previewTexture2D

The preview texture for the camera captures.


protected Texture2D previewTexture2D = null;


previousCaptureTimestamp

Timestamp of previously captured frame. needed for FPS calculations.


protected long previousCaptureTimestamp;


rawVideoFrameQueueLockObject

A raw video queue lock object.


protected static object rawVideoFrameQueueLockObject = new object();


resumeConnect

The resume connect state of the camera.


protected bool resumeConnect = false;


resumePreviewCapture

The resume preview state of the camera.


protected bool resumePreviewCapture = false;


resumeVideoCapture

The resume raw video capture state of the camera.


protected bool resumeVideoCapture = false;


Public Events

OnCaptureAborted

Callback is invoked when an ongoing video or preview capture or both are aborteddue to an error.

public MLCamera.OnCaptureAbortedDelegate OnCaptureAborted()

OnCaptureCompleted

Callback is invoked when capturing single frame is completed and result is available.

public MLCamera.OnCaptureCompletedDelegate OnCaptureCompleted()

OnCaptureFailed

Callback is invoked when a capture has failed when the camera device failed to produce a capture result for the request.

public MLCamera.OnCaptureFailedDelegate OnCaptureFailed()

OnDeviceDisconnected

Callback is invoked when the camera is disconnected.

public MLCamera.OnDeviceDisconnectedDelegate OnDeviceDisconnected()

OnDeviceError

Camera status callback, device error.

public MLCamera.OnDeviceErrorDelegate OnDeviceError()

OnDeviceIdle

Callback is invoked when the camera stops streaming.

public MLCamera.OnDeviceStatusDelegate OnDeviceIdle()

OnDeviceStreaming

Callback is invoked when the camera is streaming.

public MLCamera.OnDeviceStatusDelegate OnDeviceStreaming()

OnPreviewBufferAvailable

Callback is invoked when a captured preview frame buffer is available, invoked on the main thread.

public MLCamera.OnPreviewBufferAvailableDelegate OnPreviewBufferAvailable()

OnPreviewCaptureCompleted

Callback is invoked when a preview video frame buffer is available with MLCamera.CaptureType.Preview. Not valid for MR/VR Capture since it does not have preview support.

public MLCamera.OnPreviewCaptureCompletedDelegate OnPreviewCaptureCompleted()

OnRawImageAvailable

Callback is invoked when a captured image buffer is available.

public MLCamera.OnCapturedFrameAvailableDelegate OnRawImageAvailable()

OnRawVideoFrameAvailable

Callback is invoked when a captured raw/compressed video frame buffer is available, invoked on the main thread.

public MLCamera.OnCapturedFrameAvailableDelegate OnRawVideoFrameAvailable()

OnRawVideoFrameAvailable_NativeCallbackThread

Camera capture callback, capture raw video frame, invoked on the same thread as the native callback, allowing the use of the unmanaged native pointer to the frame data memory.

public MLCamera.OnCapturedFrameAvailableDelegate OnRawVideoFrameAvailable_NativeCallbackThread()

Protected Events

OnRawVideoFrameAvailableInternal

Callback is invoked when a captured raw/compressed video frame buffer is available, invoked on the main thread.

protected MLCamera.OnCapturedFrameAvailableDelegate OnRawVideoFrameAvailableInternal()

Public Enums

CaptureFrameRate

Capture Frame Rate Call MLCameraPrepareCapture to configure frame rate use FrameRate_None when configuring only Image capture FrameRate_60fps only supported when resolution of captures <= 1080P.

EnumeratorValueDescription
NoneNone Still Capture
_15FPSSpecified 15FPS
_30FPSSpecified 30FPS
_60FPSSpecified 60FPS

CaptureType

Capture operation type

EnumeratorValueDescription
ImageTo capture an image
VideoTo capture a video
PreviewTo capture a video and and access the raw buffer of the frames.

ConnectFlag

Flags to describe various modules in camera pipeline.

EnumeratorValueDescription
CamOnlyCamera only frame capture
VirtualOnlyvirtual only capture
MRMixed Reality capture

DeviceStatusFlag

Client can implement polling mechanism to retrieve device status and use these masks to view device status.

EnumeratorValueDescription
Connected1 << 0Connected
Idle1 << 1Idle
Streaming1 << 2Opened.
Disconnected1 << 3Disconnected.
Error1 << 4Error. Call MLCameraGetErrorCode() to obtain the error code.

DisconnectReason

Camera Disconnect Reason.

EnumeratorValueDescription
DeviceLostDevice Lost.
PriorityLostPriority Lost.

ErrorType

Camera errors

EnumeratorValueDescription
None0No error
InvalidInvalid state
DisabledCamera disabled
DeviceFailedCamera device failed
ServiceFailedCamera service failed
CaptureFailedCapture failed

Identifier

Logical Camera identifiers available for access.

EnumeratorValueDescription
Main0x86 logical camera
CVCV logical camera

MRBlendType

Comment Needed!

EnumeratorValueDescription
Additive1Additive Blend Type. It simply adds pixel values of real world and virtual layer.

MRQuality

MR Video Quality enumeration

EnumeratorValueDescription
_648x7201Specifies 648 x 720 resolution. Aspect ratio: 9x10.
_972x10802Specifies 972 x 1080 resolution. Aspect ratio: 9x10.
_1944x21603Specifies 1944 x 2160 resolution. Aspect ratio: 9x10. CaptureFrameRate._60FPS is not supported for this quality mode.
_960x7204Specifies 960 x 720 resolution. Aspect ratio: 4x3.
_1440x10805Specifies 1440 x 1080 resolution. Aspect ratio: 4x3.
_2880x21606Specifies 2880 x 2160 resolution. Aspect ratio: 4x3. CaptureFrameRate._60FPS is not supported for this quality mode.

MetadataColorCorrectionAberrationMode

The metadata for the color correction aberration mode.

EnumeratorValueDescription
Off0The color correction aberration mode: Off.
FastThe color correction aberration mode: Fast.
HighQualityThe color correction aberration mode: High Quality.

MetadataColorCorrectionMode

The metadata for the color correction mode.

EnumeratorValueDescription
TransformMatrix0The color correction mode: Transform Matrix
FastThe color correction mode: Fast
HighQualityThe color correction mode: High Quality

MetadataControlAEAntibandingMode

The metadata for the control AE anti banding mode.

EnumeratorValueDescription
Off0The control AE anti banding mode: Off
FiftyHzThe control AE anti banding mode: 50hz
SixtyHzThe control AE anti banding mode: 60hz
AutoThe control AE anti banding mode: Auto

MetadataControlAELock

The metadata for the control AE lock.

EnumeratorValueDescription
Off0The control AE lock: Off
OnThe control AE lock: On

MetadataControlAEMode

The metadata for the control AE mode.

EnumeratorValueDescription
Off0The control AE mode: Off.
OnThe control AE mode: On.

MetadataControlAEState

The metadata for the control AE state.

EnumeratorValueDescription
Inactive0The control AE state: Inactive
SearchingThe control AE state: Searching
ConvergedThe control AE state: Converged
LockedThe control AE state: Locked
FlashRequiredThe control AE state: Flash Required
PreCaptureThe control AE state: Pre-capture

MetadataControlAWBLock

The metadata for the control AWB lock.

EnumeratorValueDescription
Off0The control AWB lock: Off
OnThe control AWB lock: On

MetadataControlAWBMode

The metadata for the control AWB mode.

EnumeratorValueDescription
Off0The control AWB mode: Off
AutoThe control AWB mode: Auto
IncandescentThe control AWB mode: Incandescent
FluorescentThe control AWB mode: Fluorescent
WarmFluorescentThe control AWB mode: Warm Fluorescent
DaylightThe control AWB mode: Daylight
CloudyDaylightThe control AWB mode: Cloudy Day Light
TwilightThe control AWB mode: Twilight
ShadeThe control AWB mode: Shade

MetadataControlAWBState

The metadata for the control AWB state.

EnumeratorValueDescription
Inactive0The control AWB state: Inactive
SearchingThe control AWB state: Searching
ConvergedThe control AWB state: Converged
LockedThe control AWB state: Locked

MetadataScalerAvailableFormats

The metadata for scaler available formats.

EnumeratorValueDescription
RAW160x20RAW16 Format
RAW_OPAQUE0x24RAW OPAQUE Format
YV120x32315659YV12 Format
YCrCb_420_SP0x11YCrCb 420 SP Format
IMPLEMENTATION_DEFINED0x22Implementation Defined
YCbCr_420_8880x23YCbCr 420 888 Format
BLOB0x21BLOB Format

MetadataScalerAvailableStreamConfigurations

The metadata for scaler available stream configurations.

EnumeratorValueDescription
OUTPUT0The scaler available stream configuration: Output
INPUTThe scaler available stream configuration: Input

OutputFormat

Captured output format

EnumeratorValueDescription
YUV_420_8881YUV planar format.
JPEGCompressed output stream.
RGBA_8888RGB32 format.