ObserverViewFrameProvider.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.Experimental.StreammingCast
  10. {
  11. using NRKernal.Record;
  12. using System.Collections;
  13. using UnityEngine;
  14. /// <summary> An observer view frame provider. </summary>
  15. public class ObserverViewFrameProvider : AbstractFrameProvider
  16. {
  17. /// <summary> Source camera. </summary>
  18. private Camera m_SourceCamera;
  19. /// <summary> Source frame. </summary>
  20. private UniversalTextureFrame m_SourceFrame;
  21. /// <summary> True if is play, false if not. </summary>
  22. private bool isPlay = false;
  23. /// <summary> The FPS. </summary>
  24. private int _FPS;
  25. /// <summary> Init provider with the camera target texture. </summary>
  26. /// <param name="camera"> camera target texture.</param>
  27. /// <param name="fps"> (Optional) The FPS.</param>
  28. public ObserverViewFrameProvider(Camera camera, int fps = 30)
  29. {
  30. this.m_SourceCamera = camera;
  31. this.m_SourceFrame.textures = new Texture[1];
  32. this.m_SourceFrame.textureType = TextureType.RGB;
  33. this._FPS = fps;
  34. NRKernalUpdater.Instance.StartCoroutine(UpdateFrame());
  35. }
  36. /// <summary> Updates the frame. </summary>
  37. /// <returns> An IEnumerator. </returns>
  38. public IEnumerator UpdateFrame()
  39. {
  40. while (true)
  41. {
  42. if (isPlay)
  43. {
  44. m_SourceFrame.textures[0] = m_SourceCamera.targetTexture;
  45. m_SourceFrame.timeStamp = NRTools.GetTimeStamp();
  46. OnUpdate?.Invoke(m_SourceFrame);
  47. }
  48. yield return new WaitForSeconds(1 / _FPS);
  49. }
  50. }
  51. /// <summary> Gets frame information. </summary>
  52. /// <returns> The frame information. </returns>
  53. public override Resolution GetFrameInfo()
  54. {
  55. Resolution resolution = new Resolution();
  56. resolution.width = m_SourceFrame.textures[0].width;
  57. resolution.height = m_SourceFrame.textures[0].height;
  58. return resolution;
  59. }
  60. /// <summary> Plays this object. </summary>
  61. public override void Play()
  62. {
  63. isPlay = true;
  64. }
  65. /// <summary> Stops this object. </summary>
  66. public override void Stop()
  67. {
  68. isPlay = false;
  69. }
  70. /// <summary> Releases this object. </summary>
  71. public override void Release()
  72. {
  73. isPlay = false;
  74. NRKernalUpdater.Instance.StopCoroutine(UpdateFrame());
  75. }
  76. }
  77. }