NRRgbCamera.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using AOT;
  12. using System;
  13. using UnityEngine;
  14. /// <summary> Manage the life cycle of the entire rgb camera. </summary>
  15. internal class NRRgbCamera : NativeCameraProxy
  16. {
  17. /// <summary> The identifier. </summary>
  18. public new static string ID = "NRRgbCamera";
  19. /// <summary> Gets the resolution. </summary>
  20. /// <value> The resolution. </value>
  21. public override NativeResolution Resolution
  22. {
  23. get
  24. {
  25. #if !UNITY_EDITOR
  26. NativeResolution resolution = NRFrame.GetDeviceResolution(NativeDevice.RGB_CAMERA);
  27. #else
  28. NativeResolution resolution = new NativeResolution(1280, 720);
  29. #endif
  30. return resolution;
  31. }
  32. }
  33. #if !UNITY_EDITOR
  34. public NRRgbCamera() : base(new NativeCamera()) { }
  35. #else
  36. /// <summary> Default constructor. </summary>
  37. public NRRgbCamera() : base(new EditorCameraDataProvider()) { }
  38. #endif
  39. /// <summary> Initializes this object. </summary>
  40. public override void Initialize()
  41. {
  42. base.Initialize();
  43. RegistCaptureCallback(RGBCameraCapture);
  44. }
  45. /// <summary>
  46. /// RGB camera capture callback for native layer. Note: Here must be static to avoid function
  47. /// pointer lost in native layer. </summary>
  48. /// <param name="camera_handle"> .</param>
  49. /// <param name="camera_image_handle"> .</param>
  50. /// <param name="userdata"> .</param>
  51. [MonoPInvokeCallback(typeof(CameraImageCallback))]
  52. public static void RGBCameraCapture(UInt64 camera_handle, UInt64 camera_image_handle, UInt64 userdata)
  53. {
  54. var controller = CameraProxyFactory.GetInstance(NRRgbCamera.ID);
  55. if (controller == null)
  56. {
  57. NRDebugger.Error("[NRRgbCamera] get controller instance faild.");
  58. return;
  59. }
  60. controller.UpdateFrame(camera_handle, camera_image_handle, userdata);
  61. }
  62. /// <summary> Updates the frame. </summary>
  63. /// <param name="camera_handle"> Handle of the camera.</param>
  64. /// <param name="camera_image_handle"> Handle of the camera image.</param>
  65. /// <param name="userdata"> The userdata.</param>
  66. public override void UpdateFrame(UInt64 camera_handle, UInt64 camera_image_handle, UInt64 userdata)
  67. {
  68. int RawDataSize = 0;
  69. this.CameraDataProvider.GetRawData(camera_image_handle, (int)NativeDevice.RGB_CAMERA, ref this.m_TexturePtr, ref RawDataSize);
  70. var timestamp = this.CameraDataProvider.GetHMDTimeNanos(camera_image_handle, (int)NativeDevice.RGB_CAMERA);
  71. this.QueueFrame(this.m_TexturePtr, RawDataSize, timestamp);
  72. this.CameraDataProvider.DestroyImage(camera_image_handle);
  73. }
  74. }
  75. }