VideoCapture2RTPExample.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using NRKernal.Record;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace NRKernal.Experimental.NRExamples
  5. {
  6. /// <summary> A video capture 2 rtp example. </summary>
  7. [HelpURL("https://developer.nreal.ai/develop/unity/video-capture")]
  8. public class VideoCapture2RTPExample : MonoBehaviour
  9. {
  10. /// <summary> The previewer. </summary>
  11. public NRPreviewer Previewer;
  12. /// <summary> Gets the full pathname of the rtp file. </summary>
  13. /// <value> The full pathname of the rtp file. </value>
  14. public string RTPPath
  15. {
  16. get
  17. {
  18. return @"rtp://192.168.31.6:5555";
  19. }
  20. }
  21. /// <summary> The video capture. </summary>
  22. NRVideoCapture m_VideoCapture = null;
  23. /// <summary> Starts this object. </summary>
  24. void Start()
  25. {
  26. CreateVideoCaptureTest();
  27. }
  28. /// <summary> Updates this object. </summary>
  29. void Update()
  30. {
  31. if (m_VideoCapture == null)
  32. {
  33. return;
  34. }
  35. if (Input.GetKeyDown(KeyCode.R) || NRInput.GetButtonDown(ControllerButton.TRIGGER))
  36. {
  37. StartVideoCapture();
  38. Previewer.SetData(m_VideoCapture.PreviewTexture, true);
  39. }
  40. if (Input.GetKeyDown(KeyCode.T) || NRInput.GetButtonDown(ControllerButton.HOME))
  41. {
  42. StopVideoCapture();
  43. Previewer.SetData(m_VideoCapture.PreviewTexture, false);
  44. }
  45. }
  46. /// <summary> Tests create video capture. </summary>
  47. void CreateVideoCaptureTest()
  48. {
  49. NRVideoCapture.CreateAsync(false, delegate (NRVideoCapture videoCapture)
  50. {
  51. if (videoCapture != null)
  52. {
  53. m_VideoCapture = videoCapture;
  54. }
  55. else
  56. {
  57. NRDebugger.Error("Failed to create VideoCapture Instance!");
  58. }
  59. });
  60. }
  61. /// <summary> Starts video capture. </summary>
  62. void StartVideoCapture()
  63. {
  64. Resolution cameraResolution = NRVideoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
  65. NRDebugger.Info(cameraResolution);
  66. int cameraFramerate = NRVideoCapture.GetSupportedFrameRatesForResolution(cameraResolution).OrderByDescending((fps) => fps).First();
  67. NRDebugger.Info(cameraFramerate);
  68. if (m_VideoCapture != null)
  69. {
  70. NRDebugger.Info("Created VideoCapture Instance!");
  71. CameraParameters cameraParameters = new CameraParameters();
  72. cameraParameters.hologramOpacity = 1f;
  73. cameraParameters.frameRate = cameraFramerate;
  74. cameraParameters.cameraResolutionWidth = cameraResolution.width;
  75. cameraParameters.cameraResolutionHeight = cameraResolution.height;
  76. cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
  77. cameraParameters.blendMode = BlendMode.Blend;
  78. m_VideoCapture.StartVideoModeAsync(cameraParameters, OnStartedVideoCaptureMode, true);
  79. }
  80. }
  81. /// <summary> Stops video capture. </summary>
  82. void StopVideoCapture()
  83. {
  84. NRDebugger.Info("Stop Video Capture!");
  85. m_VideoCapture.StopRecordingAsync(OnStoppedRecordingVideo);
  86. }
  87. /// <summary> Executes the 'started video capture mode' action. </summary>
  88. /// <param name="result"> The result.</param>
  89. void OnStartedVideoCaptureMode(NRVideoCapture.VideoCaptureResult result)
  90. {
  91. NRDebugger.Info("Started Video Capture Mode!");
  92. m_VideoCapture.StartRecordingAsync(RTPPath, OnStartedRecordingVideo);
  93. }
  94. /// <summary> Executes the 'stopped video capture mode' action. </summary>
  95. /// <param name="result"> The result.</param>
  96. void OnStoppedVideoCaptureMode(NRVideoCapture.VideoCaptureResult result)
  97. {
  98. NRDebugger.Info("Stopped Video Capture Mode!");
  99. }
  100. /// <summary> Executes the 'started recording video' action. </summary>
  101. /// <param name="result"> The result.</param>
  102. void OnStartedRecordingVideo(NRVideoCapture.VideoCaptureResult result)
  103. {
  104. NRDebugger.Info("Started Recording Video!");
  105. }
  106. /// <summary> Executes the 'stopped recording video' action. </summary>
  107. /// <param name="result"> The result.</param>
  108. void OnStoppedRecordingVideo(NRVideoCapture.VideoCaptureResult result)
  109. {
  110. NRDebugger.Info("Stopped Recording Video!");
  111. m_VideoCapture.StopVideoModeAsync(OnStoppedVideoCaptureMode);
  112. }
  113. }
  114. }