ObserverViewFrameCaptureContext.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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;
  13. using System.Collections.Generic;
  14. using UnityEngine;
  15. /// <summary> An observer view frame capture context. </summary>
  16. public class ObserverViewFrameCaptureContext
  17. {
  18. /// <summary> The blender. </summary>
  19. private ObserverViewBlender m_Blender;
  20. /// <summary> The frame provider. </summary>
  21. private AbstractFrameProvider m_FrameProvider;
  22. /// <summary> The capture behaviour. </summary>
  23. private NRObserverViewBehaviour m_CaptureBehaviour;
  24. /// <summary> The net worker. </summary>
  25. private ObserverViewNetWorker m_NetWorker;
  26. /// <summary> The encoder. </summary>
  27. private IEncoder m_Encoder;
  28. /// <summary> Options for controlling the camera. </summary>
  29. private CameraParameters m_CameraParameters;
  30. /// <summary> True if is initialize, false if not. </summary>
  31. private bool m_IsInitialized = false;
  32. private List<IFrameConsumer> m_FrameConsumerList;
  33. /// <summary> Gets the preview texture. </summary>
  34. /// <value> The preview texture. </value>
  35. public Texture PreviewTexture
  36. {
  37. get
  38. {
  39. return m_Blender?.BlendTexture;
  40. }
  41. }
  42. /// <summary> Gets the behaviour. </summary>
  43. /// <returns> The behaviour. </returns>
  44. public NRObserverViewBehaviour GetBehaviour()
  45. {
  46. return m_CaptureBehaviour;
  47. }
  48. /// <summary> Gets frame provider. </summary>
  49. /// <returns> The frame provider. </returns>
  50. public AbstractFrameProvider GetFrameProvider()
  51. {
  52. return m_FrameProvider;
  53. }
  54. /// <summary> Request camera parameter. </summary>
  55. /// <returns> The CameraParameters. </returns>
  56. public CameraParameters RequestCameraParam()
  57. {
  58. return m_CameraParameters;
  59. }
  60. /// <summary> Gets the encoder. </summary>
  61. /// <returns> The encoder. </returns>
  62. public IEncoder GetEncoder()
  63. {
  64. return m_Encoder;
  65. }
  66. /// <summary> Default constructor. </summary>
  67. public ObserverViewFrameCaptureContext()
  68. {
  69. }
  70. /// <summary> Starts capture mode. </summary>
  71. /// <param name="param"> The parameter.</param>
  72. public void StartCaptureMode(CameraParameters param)
  73. {
  74. this.m_CameraParameters = param;
  75. if (m_CaptureBehaviour == null)
  76. {
  77. this.m_CaptureBehaviour = this.GetObserverViewCaptureBehaviour();
  78. }
  79. this.m_Encoder = new VideoEncoder();
  80. this.m_Encoder.Config(param);
  81. this.m_Blender = new ObserverViewBlender();
  82. this.m_Blender.Config(m_CaptureBehaviour.CaptureCamera, m_Encoder, param);
  83. this.m_NetWorker = new ObserverViewNetWorker(this);
  84. this.m_NetWorker.Listen();
  85. this.m_FrameProvider = new ObserverViewFrameProvider(m_CaptureBehaviour.CaptureCamera, this.m_CameraParameters.frameRate);
  86. this.m_FrameProvider.OnUpdate += UpdateFrame;
  87. this.m_FrameConsumerList = new List<IFrameConsumer>();
  88. this.Sequence(m_Blender);
  89. this.m_IsInitialized = true;
  90. }
  91. /// <summary> Auto adaption for BlendMode based on supported feature on current device. </summary>
  92. /// <param name="blendMode"> source blendMode.</param>
  93. /// <returns> Fallback blendMode. </returns>
  94. public BlendMode AutoAdaptBlendMode(BlendMode blendMode)
  95. {
  96. if (!NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_RGB_CAMERA))
  97. return BlendMode.VirtualOnly;
  98. return blendMode;
  99. }
  100. private ObserverViewFrameCaptureContext Sequence(IFrameConsumer consummer)
  101. {
  102. this.m_FrameConsumerList.Add(consummer);
  103. return this;
  104. }
  105. private void UpdateFrame(UniversalTextureFrame frame)
  106. {
  107. for (int i = 0; i < m_FrameConsumerList.Count; i++)
  108. {
  109. m_FrameConsumerList[i].OnFrame(frame);
  110. }
  111. }
  112. /// <summary> Gets observer view capture behaviour. </summary>
  113. /// <returns> The observer view capture behaviour. </returns>
  114. private NRObserverViewBehaviour GetObserverViewCaptureBehaviour()
  115. {
  116. NRObserverViewBehaviour capture = GameObject.FindObjectOfType<NRObserverViewBehaviour>();
  117. Transform headParent = null;
  118. if (NRSessionManager.Instance.NRSessionBehaviour != null)
  119. {
  120. headParent = NRSessionManager.Instance.NRSessionBehaviour.transform.parent;
  121. }
  122. if (capture == null)
  123. {
  124. capture = GameObject.Instantiate(Resources.Load<NRObserverViewBehaviour>("Record/Prefabs/NRObserverViewBehaviour"), headParent);
  125. }
  126. GameObject.DontDestroyOnLoad(capture.gameObject);
  127. return capture;
  128. }
  129. /// <summary> Stops capture mode. </summary>
  130. public void StopCaptureMode()
  131. {
  132. this.Release();
  133. }
  134. /// <summary> Starts a capture. </summary>
  135. /// <param name="ip"> The IP.</param>
  136. /// <param name="callback"> The callback.</param>
  137. public void StartCapture(string ip, Action<bool> callback)
  138. {
  139. if (!m_IsInitialized)
  140. {
  141. callback?.Invoke(false);
  142. return;
  143. }
  144. NRDebugger.Info("[CaptureContext] Start... {0}", ip);
  145. ((VideoEncoder)m_Encoder).EncodeConfig.SetOutPutPath(string.Format("rtp://{0}:5555", ip));
  146. m_NetWorker.CheckServerAvailable(ip, (result) =>
  147. {
  148. NRDebugger.Info("[ObserverView] CheckServerAvailable : " + result);
  149. if (result)
  150. {
  151. m_Encoder?.Start();
  152. m_FrameProvider?.Play();
  153. }
  154. else
  155. {
  156. this.Release();
  157. }
  158. callback?.Invoke(result);
  159. });
  160. }
  161. /// <summary> Stops a capture. </summary>
  162. public void StopCapture()
  163. {
  164. if (!m_IsInitialized)
  165. {
  166. return;
  167. }
  168. NRDebugger.Info("[CaptureContext] Stop...");
  169. // Need stop encoder firstly.
  170. m_Encoder?.Stop();
  171. m_FrameProvider?.Stop();
  172. }
  173. /// <summary> Releases this object. </summary>
  174. public void Release()
  175. {
  176. if (!m_IsInitialized)
  177. {
  178. return;
  179. }
  180. NRDebugger.Info("[CaptureContext] Release...");
  181. if (m_FrameProvider != null)
  182. {
  183. this.m_FrameProvider.OnUpdate -= UpdateFrame;
  184. m_FrameProvider?.Release();
  185. }
  186. m_Blender?.Dispose();
  187. m_Encoder?.Release();
  188. m_NetWorker?.Close();
  189. m_Blender = null;
  190. m_Encoder = null;
  191. m_NetWorker = null;
  192. if (m_CaptureBehaviour != null)
  193. {
  194. GameObject.DestroyImmediate(m_CaptureBehaviour.gameObject);
  195. m_CaptureBehaviour = null;
  196. }
  197. m_IsInitialized = false;
  198. }
  199. }
  200. }