/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ namespace NRKernal { using System; using System.Runtime.InteropServices; /// Session Native API. internal partial class NativeCamera : ICameraDataProvider { /// Handle of the native camera. private UInt64 m_NativeCameraHandle; private bool _IsErrorState = false; /// Creates a new bool. /// True if it succeeds, false if it fails. public bool Create() { _IsErrorState = false; var result = NativeApi.NRRGBCameraCreate(ref m_NativeCameraHandle); NativeErrorListener.Check(result, this, "Create", true); return result == NativeResult.Success; } /// Gets raw data. /// Handle of the image. /// The eye. /// [in,out] The pointer. /// [in,out] The size. /// True if it succeeds, false if it fails. public bool GetRawData(UInt64 imageHandle, int eye, ref IntPtr ptr, ref int size) { if (_IsErrorState) { return false; } uint data_size = 0; var result = NativeApi.NRRGBCameraImageGetRawData(m_NativeCameraHandle, imageHandle, ref ptr, ref data_size); size = (int)data_size; NativeErrorListener.Check(result, this, "GetRawData"); return result == NativeResult.Success; } /// Gets a resolution. /// Handle of the image. /// The eye. /// The resolution. public NativeResolution GetResolution(UInt64 imageHandle, int eye) { NativeResolution resolution = new NativeResolution(0, 0); var result = NativeApi.NRRGBCameraImageGetResolution(m_NativeCameraHandle, imageHandle, ref resolution); NativeErrorListener.Check(result, this, "GetResolution"); return resolution; } /// Gets hmd time nanos. /// Handle of the image. /// The eye. /// The hmd time nanos. public UInt64 GetHMDTimeNanos(UInt64 imageHandle, int eye) { UInt64 time = 0; NativeApi.NRRGBCameraImageGetHMDTimeNanos(m_NativeCameraHandle, imageHandle, ref time); return time; } /// Get exposure time. /// Handle of the image. /// The eye. /// Exposure time of the image. public UInt32 GetExposureTime(UInt64 imageHandle, int eye) { UInt32 exposureTime = 0; return exposureTime; } /// Get Gain. /// Handle of the image. /// The eye. /// Gain of the image. public UInt32 GetGain(UInt64 imageHandle, int eye) { UInt32 gain = 0; return gain; } /// Callback, called when the set capture. /// The callback. /// (Optional) The userdata. /// True if it succeeds, false if it fails. public bool SetCaptureCallback(CameraImageCallback callback, UInt64 userdata = 0) { if (_IsErrorState) { return false; } var result = NativeApi.NRRGBCameraSetCaptureCallback(m_NativeCameraHandle, callback, userdata); NativeErrorListener.Check(result, this, "SetCaptureCallback"); return result == NativeResult.Success; } /// Sets image format. /// Describes the format to use. /// True if it succeeds, false if it fails. public bool SetImageFormat(CameraImageFormat format) { if (_IsErrorState) { return false; } var result = NativeApi.NRRGBCameraSetImageFormat(m_NativeCameraHandle, format); NativeErrorListener.Check(result, this, "SetImageFormat"); return result == NativeResult.Success; } /// Starts a capture. /// True if it succeeds, false if it fails. public bool StartCapture() { if (_IsErrorState) { NativeErrorListener.Check(NativeResult.RGBCameraDeviceNotFind, this, "StartCapture", true); return false; } var result = NativeApi.NRRGBCameraStartCapture(m_NativeCameraHandle); _IsErrorState = (result != NativeResult.Success); NativeErrorListener.Check(result, this, "StartCapture", true); return result == NativeResult.Success; } /// Stops a capture. /// True if it succeeds, false if it fails. public bool StopCapture() { if (_IsErrorState) { return false; } var result = NativeApi.NRRGBCameraStopCapture(m_NativeCameraHandle); NativeErrorListener.Check(result, this, "StopCapture", true); return result == NativeResult.Success; } /// Destroys the image described by imageHandle. /// Handle of the image. /// True if it succeeds, false if it fails. public bool DestroyImage(UInt64 imageHandle) { if (_IsErrorState) { return false; } var result = NativeApi.NRRGBCameraImageDestroy(m_NativeCameraHandle, imageHandle); NativeErrorListener.Check(result, this, "DestroyImage"); return result == NativeResult.Success; } /// Releases this object. /// True if it succeeds, false if it fails. public bool Release() { _IsErrorState = false; var result = NativeApi.NRRGBCameraDestroy(m_NativeCameraHandle); NativeErrorListener.Check(result, this, "Release"); return result == NativeResult.Success; } /// A native api. private struct NativeApi { /// Nrrgb camera image get raw data. /// Handle of the RGB camera. /// Handle of the RGB camera image. /// [in,out] Information describing the out image raw. /// [in,out] Size of the out image raw data. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraImageGetRawData(UInt64 rgb_camera_handle, UInt64 rgb_camera_image_handle, ref IntPtr out_image_raw_data, ref UInt32 out_image_raw_data_size); /// Nrrgb camera image get resolution. /// Handle of the RGB camera. /// Handle of the RGB camera image. /// [in,out] The out image resolution. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraImageGetResolution(UInt64 rgb_camera_handle, UInt64 rgb_camera_image_handle, ref NativeResolution out_image_resolution); /// Nrrgb camera image get hmd time nanos. /// Handle of the RGB camera. /// Handle of the RGB camera image. /// [in,out] The out image hmd time nanos. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraImageGetHMDTimeNanos( UInt64 rgb_camera_handle, UInt64 rgb_camera_image_handle, ref UInt64 out_image_hmd_time_nanos); /// Nrrgb camera create. /// [in,out] Handle of the out RGB camera. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraCreate(ref UInt64 out_rgb_camera_handle); /// Nrrgb camera destroy. /// Handle of the RGB camera. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraDestroy(UInt64 rgb_camera_handle); /// Callback, called when the nrrgb camera set capture. /// Handle of the RGB camera. /// The image callback. /// The userdata. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary, CallingConvention = CallingConvention.Cdecl)] public static extern NativeResult NRRGBCameraSetCaptureCallback( UInt64 rgb_camera_handle, CameraImageCallback image_callback, UInt64 userdata); /// Nrrgb camera set image format. /// Handle of the RGB camera. /// Describes the format to use. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraSetImageFormat( UInt64 rgb_camera_handle, CameraImageFormat format); /// Nrrgb camera start capture. /// Handle of the RGB camera. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraStartCapture(UInt64 rgb_camera_handle); /// Nrrgb camera stop capture. /// Handle of the RGB camera. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraStopCapture(UInt64 rgb_camera_handle); /// Nrrgb camera image destroy. /// Handle of the RGB camera. /// Handle of the RGB camera image. /// A NativeResult. [DllImport(NativeConstants.NRNativeLibrary)] public static extern NativeResult NRRGBCameraImageDestroy(UInt64 rgb_camera_handle, UInt64 rgb_camera_image_handle); }; } }