// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
// Copyright (c) (2024) Magic Leap, Inc. All Rights Reserved.
// Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2
// Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein.
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
namespace UnityEngine.XR.MagicLeap
{
///
/// APIs for accessing Camera Device and to do Camera Capture.
///
public partial class MLCameraBase
{
///
/// Connection settings for MR capture.
///
public struct MRConnectInfo
{
///
/// video quality
///
public MRQuality MRQuality;
///
/// blending type for mixed reality capture
///
public MRBlendType MRBlendType;
///
/// capture frame rate
///
public CaptureFrameRate FrameRate;
///
/// Create and return an initialized version of this struct.
///
/// A new instance of this struct.
public static MRConnectInfo Create()
{
return new MRConnectInfo()
{
MRQuality = MRQuality._1440x1080,
MRBlendType = MRBlendType.Additive,
FrameRate = CaptureFrameRate._30FPS
};
}
public override string ToString()
{
return $"Quality : {MRQuality}, BlendType : {MRBlendType}, FrameRate : {FrameRate}";
}
}
///
/// Camera connection settings.
///
public struct ConnectContext
{
///
/// logical camera idenitifier
///
public Identifier CamId;
///
/// The Context in which the camera should operate in.
///
public ConnectFlag Flags;
///
/// Video Stabilization enable/disable,
/// true - enable; false - disabled
///
public bool EnableVideoStabilization;
///
/// MR capture connection settings.
/// Ignored for camera only capture.
///
public MRConnectInfo MixedRealityConnectInfo;
///
/// Create and return an initialized version of this struct.
///
/// A new instance of this struct.
public static ConnectContext Create()
{
return new ConnectContext()
{
CamId = Identifier.Main,
Flags = ConnectFlag.CamOnly,
EnableVideoStabilization = false,
MixedRealityConnectInfo = MRConnectInfo.Create()
};
}
public override string ToString()
{
return $"CamId : {CamId}, Flags : {Flags}, Stabilization : {EnableVideoStabilization}, MRConnectInfo : {MixedRealityConnectInfo}";
}
}
public struct StreamCapability
{
///
/// Capture Type
///
public CaptureType CaptureType;
///
/// Resolution height
///
public int Width;
///
/// Resolution height
///
public int Height;
public override string ToString()
{
return $"CaptureType : {CaptureType}, Width : {Width}, Height : {Height}";
}
}
public struct StreamCapabilitiesInfo
{
public StreamCapability[] StreamCapabilities;
}
public struct CaptureStreamConfig
{
///
/// Capture Type
///
public CaptureType CaptureType;
///
/// Capture Resolution
///
public int Width;
///
/// Resolution height
///
public int Height;
///
/// output Format
///
public OutputFormat OutputFormat;
///
/// Media recorder surface, only valid for capture type video * set to ML_INVALID_HANDLE for yuv/rgba video capture
///
public MLNativeSurface Surface;
public static CaptureStreamConfig Create(StreamCapability streamCapability, OutputFormat outputFormat, MLNativeSurface recorderSurface = null)
{
return new CaptureStreamConfig()
{
Width = streamCapability.Width,
Height = streamCapability.Height,
CaptureType = streamCapability.CaptureType,
OutputFormat = outputFormat,
Surface = recorderSurface
};
}
public override string ToString()
{
return $"CaptureType : {CaptureType}, Width : {Width}, Height : {Height}, OutputFormat : {OutputFormat}, MediaRecorder : {(Surface == null ? "NULL" : Surface.Handle.ToString())}";
}
}
public struct CaptureConfig
{
///
/// capture frame rate
///
public CaptureFrameRate CaptureFrameRate;
///
/// Stream configurations
///
public CaptureStreamConfig[] StreamConfigs;
}
///
/// Camera intrinsic parameters.
///
public readonly struct IntrinsicCalibrationParameters
{
public readonly uint Width;
public readonly uint Height;
public readonly Vector2 FocalLength;
public readonly Vector2 PrincipalPoint;
public readonly float FOV;
///
/// Distortion vector.
/// The distortion co-efficients are in the following order:
/// [k1, k2, p1, p2, k3]
///
public readonly double[] Distortion;
public IntrinsicCalibrationParameters(uint width, uint height, in Vector2 focalLength, in Vector2 principalPoint, float fov, double[] distortion)
{
Width = width;
Height = height;
FocalLength = focalLength;
PrincipalPoint = principalPoint;
FOV = fov;
Distortion = distortion;
}
public override string ToString()
{
return $"Width : {Width}, Height : {Height}, FocalLength : {FocalLength}, PrincipalPoint : {PrincipalPoint}, FOV : {FOV}, Distortion : {string.Join(", ", Distortion)}";
}
}
public readonly struct ResultExtras
{
///
/// Frame number associated with this result.
///
public readonly long FrameNumber;
///
/// Frame timestamp in microseconds.
///
public readonly MLTime VCamTimestamp;
///
/// Intrinsic calibration parameters.
///
public readonly IntrinsicCalibrationParameters? Intrinsics;
internal ResultExtras(long frameNumber, long vcamTimestampUs, in IntrinsicCalibrationParameters? intrinsics)
{
FrameNumber = frameNumber;
VCamTimestamp = vcamTimestampUs;
Intrinsics = intrinsics;
}
}
///
/// Contains both the data and information necessary to read the data for a specific buffer in a capture
///
public struct PlaneInfo
{
///
/// Indicate if this structure contains valid data
///
public bool IsValid;
///
/// Width of the output image in pixels
///
public uint Width;
///
/// Height of the output image in pixels
///
public uint Height;
///
/// Stride of the output image in pixels
///
public uint Stride;
///
/// Number of bytes used to represent a pixel
///
public uint BytesPerPixel;
///
/// Distance between 2 consecutive pixels in bytes.
///
public uint PixelStride;
///
/// Image Data (in managed memory)
///
public byte[] Data;
///
/// Pointer to the unmanaged memory where the actual image data is found.
/// Only valid when received via the OnRawVideoFrameAvailableYUV_NativeCallbackThread event.
///
public IntPtr DataPtr;
///
/// Number of bytes in the image output data
///
public uint Size;
///
/// Create and return an initialized version of this struct.
///
/// Returns a new YUVBuffer structure.
public static PlaneInfo Create()
{
return new PlaneInfo
{
IsValid = false,
Width = 0u,
Height = 0u,
Stride = 0u,
BytesPerPixel = 0u,
PixelStride = 0u,
Data = new byte[0],
Size = 0u
};
}
public override string ToString()
{
return $"IsValid: {IsValid}, Width: {Width}, Height: {Height}, Stride: {Stride}, BytesPerPixel: {BytesPerPixel} " +
$"PixelStride: {PixelStride}, ByteBufferLength: {Data?.Length}, DataPtr: {DataPtr}, Size: {Size}";
}
}
///
/// Contains 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
///
public struct CameraOutput
{
public PlaneInfo[] Planes;
public OutputFormat Format;
public override string ToString()
{
string result = $"Format: {Format}\n";
for (int i = 0; i < Planes.Length; ++i)
{
result += $"Plane[{i}]: {Planes[i]}\n";
}
return result;
}
}
}
}