RGBCameraFrameProvider.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Record
  10. {
  11. using UnityEngine;
  12. /// <summary> A RGB camera frame provider. </summary>
  13. public class RGBCameraFrameProvider : AbstractFrameProvider
  14. {
  15. /// <summary> The RGB tex. </summary>
  16. private CameraModelView m_CameraTexture;
  17. private UniversalTextureFrame frameInfo;
  18. /// <summary> Default constructor. </summary>
  19. public RGBCameraFrameProvider()
  20. {
  21. var active_format = NativeCameraProxy.GetActiveCameraImageFormat(NRRgbCamera.ID);
  22. NRDebugger.Info("[CameraFrameProvider] Use format:{0}", active_format);
  23. switch (active_format)
  24. {
  25. case CameraImageFormat.YUV_420_888:
  26. m_CameraTexture = new NRRGBCamTextureYUV();
  27. ((NRRGBCamTextureYUV)m_CameraTexture).OnUpdate += UpdateYUVFrame;
  28. frameInfo.textures = new Texture[3];
  29. frameInfo.textureType = TextureType.YUV;
  30. break;
  31. case CameraImageFormat.RGB_888:
  32. m_CameraTexture = new NRRGBCamTexture();
  33. ((NRRGBCamTexture)m_CameraTexture).OnUpdate += UpdateRGBFrame;
  34. frameInfo.textures = new Texture[1];
  35. frameInfo.textureType = TextureType.RGB;
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. private void UpdateYUVFrame(NRRGBCamTextureYUV.YUVTextureFrame frame)
  42. {
  43. frameInfo.timeStamp = frame.timeStamp;
  44. frameInfo.gain = frame.gain;
  45. frameInfo.exposureTime = frame.exposureTime;
  46. frameInfo.textures[0] = frame.textureY;
  47. frameInfo.textures[1] = frame.textureU;
  48. frameInfo.textures[2] = frame.textureV;
  49. OnUpdate?.Invoke(frameInfo);
  50. m_IsFrameReady = true;
  51. }
  52. /// <summary> Updates the frame described by frame. </summary>
  53. /// <param name="frame"> The frame.</param>
  54. private void UpdateRGBFrame(CameraTextureFrame frame)
  55. {
  56. frameInfo.timeStamp = frame.timeStamp;
  57. frameInfo.gain = frame.gain;
  58. frameInfo.exposureTime = frame.exposureTime;
  59. frameInfo.textures[0] = frame.texture;
  60. OnUpdate?.Invoke(frameInfo);
  61. m_IsFrameReady = true;
  62. }
  63. /// <summary> Gets frame information. </summary>
  64. /// <returns> The frame information. </returns>
  65. public override Resolution GetFrameInfo()
  66. {
  67. Resolution resolution = new Resolution();
  68. resolution.width = m_CameraTexture.Width;
  69. resolution.height = m_CameraTexture.Height;
  70. return resolution;
  71. }
  72. /// <summary> Plays this object. </summary>
  73. public override void Play()
  74. {
  75. m_CameraTexture.Play();
  76. }
  77. /// <summary> Stops this object. </summary>
  78. public override void Stop()
  79. {
  80. m_CameraTexture.Pause();
  81. }
  82. /// <summary> Releases this object. </summary>
  83. public override void Release()
  84. {
  85. m_CameraTexture.Stop();
  86. m_CameraTexture = null;
  87. }
  88. }
  89. }