FrameCaptureContext.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 System;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. /// <summary> A frame capture context. </summary>
  15. public class FrameCaptureContext
  16. {
  17. /// <summary> The blender. </summary>
  18. private BlenderBase m_Blender;
  19. /// <summary> The encoder. </summary>
  20. private IEncoder m_Encoder;
  21. /// <summary> Options for controlling the camera. </summary>
  22. private CameraParameters m_CameraParameters;
  23. /// <summary> The frame provider. </summary>
  24. private AbstractFrameProvider m_FrameProvider;
  25. /// <summary> The capture behaviour. </summary>
  26. private CaptureBehaviourBase m_CaptureBehaviour;
  27. /// <summary> True if is initialize, false if not. </summary>
  28. private bool m_IsInitialized = false;
  29. private List<IFrameConsumer> m_FrameConsumerList;
  30. /// <summary> Gets the preview texture. </summary>
  31. /// <value> The preview texture. </value>
  32. public Texture PreviewTexture
  33. {
  34. get
  35. {
  36. return m_Blender?.BlendTexture;
  37. }
  38. }
  39. /// <summary> Gets the behaviour. </summary>
  40. /// <returns> The behaviour. </returns>
  41. public CaptureBehaviourBase GetBehaviour()
  42. {
  43. return m_CaptureBehaviour;
  44. }
  45. /// <summary> Gets frame provider. </summary>
  46. /// <returns> The frame provider. </returns>
  47. public AbstractFrameProvider GetFrameProvider()
  48. {
  49. return m_FrameProvider;
  50. }
  51. /// <summary> Gets the blender. </summary>
  52. /// <returns> The blender. </returns>
  53. public BlenderBase GetBlender()
  54. {
  55. return m_Blender;
  56. }
  57. /// <summary> Request camera parameter. </summary>
  58. /// <returns> The CameraParameters. </returns>
  59. public CameraParameters RequestCameraParam()
  60. {
  61. return m_CameraParameters;
  62. }
  63. /// <summary> Gets the encoder. </summary>
  64. /// <returns> The encoder. </returns>
  65. public IEncoder GetEncoder()
  66. {
  67. return m_Encoder;
  68. }
  69. /// <summary> Constructor. </summary>
  70. public FrameCaptureContext() { }
  71. /// <summary> Starts capture mode. </summary>
  72. /// <param name="param"> The parameter.</param>
  73. public void StartCaptureMode(CameraParameters param)
  74. {
  75. if (m_IsInitialized)
  76. {
  77. this.Release();
  78. NRDebugger.Warning("[CaptureContext] Capture context has been started already, release it and restart a new one.");
  79. }
  80. NRDebugger.Info("[CaptureContext] Create...");
  81. if (m_CaptureBehaviour == null)
  82. {
  83. this.m_CaptureBehaviour = this.GetCaptureBehaviourByMode(param.camMode);
  84. }
  85. this.m_CameraParameters = param;
  86. this.m_Encoder = GetEncoderByMode(param.camMode);
  87. this.m_Encoder.Config(param);
  88. this.m_Blender = new ExtraFrameBlender();
  89. this.m_Blender.Init(m_CaptureBehaviour.CaptureCamera, m_Encoder, param);
  90. this.m_CaptureBehaviour.Init(this);
  91. this.m_FrameProvider = CreateFrameProviderByMode(param.blendMode, param.frameRate);
  92. this.m_FrameProvider.OnUpdate += UpdateFrame;
  93. this.m_FrameConsumerList = new List<IFrameConsumer>();
  94. this.Sequence(m_CaptureBehaviour)
  95. .Sequence(m_Blender);
  96. this.m_IsInitialized = true;
  97. }
  98. /// <summary> Auto adaption for BlendMode based on supported feature on current device. </summary>
  99. /// <param name="blendMode"> source blendMode.</param>
  100. /// <returns> Fallback blendMode. </returns>
  101. public BlendMode AutoAdaptBlendMode(BlendMode blendMode)
  102. {
  103. if (!NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_RGB_CAMERA))
  104. return BlendMode.VirtualOnly;
  105. return blendMode;
  106. }
  107. private FrameCaptureContext Sequence(IFrameConsumer consummer)
  108. {
  109. this.m_FrameConsumerList.Add(consummer);
  110. return this;
  111. }
  112. private void UpdateFrame(UniversalTextureFrame frame)
  113. {
  114. for (int i = 0; i < m_FrameConsumerList.Count; i++)
  115. {
  116. m_FrameConsumerList[i].OnFrame(frame);
  117. }
  118. }
  119. /// <summary> Gets capture behaviour by mode. </summary>
  120. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  121. /// <param name="mode"> The mode.</param>
  122. /// <returns> The capture behaviour by mode. </returns>
  123. private CaptureBehaviourBase GetCaptureBehaviourByMode(CamMode mode)
  124. {
  125. if (mode == CamMode.PhotoMode)
  126. {
  127. NRCaptureBehaviour capture = GameObject.FindObjectOfType<NRCaptureBehaviour>();
  128. var headParent = NRSessionManager.Instance.NRSessionBehaviour.transform.parent;
  129. if (capture == null)
  130. {
  131. capture = GameObject.Instantiate(Resources.Load<NRCaptureBehaviour>("Record/Prefabs/NRCaptureBehaviour"), headParent);
  132. }
  133. GameObject.DontDestroyOnLoad(capture.gameObject);
  134. return capture;
  135. }
  136. else if (mode == CamMode.VideoMode)
  137. {
  138. NRRecordBehaviour capture = GameObject.FindObjectOfType<NRRecordBehaviour>();
  139. var headParent = NRSessionManager.Instance.NRSessionBehaviour.transform.parent;
  140. if (capture == null)
  141. {
  142. capture = GameObject.Instantiate(Resources.Load<NRRecordBehaviour>("Record/Prefabs/NRRecorderBehaviour"), headParent);
  143. }
  144. GameObject.DontDestroyOnLoad(capture.gameObject);
  145. return capture;
  146. }
  147. else
  148. {
  149. throw new Exception("CamMode need to be set correctly for capture behaviour!");
  150. }
  151. }
  152. private AbstractFrameProvider CreateFrameProviderByMode(BlendMode mode, int fps)
  153. {
  154. AbstractFrameProvider provider;
  155. switch (mode)
  156. {
  157. case BlendMode.Blend:
  158. case BlendMode.RGBOnly:
  159. #if UNITY_EDITOR
  160. provider = new EditorFrameProvider();
  161. #else
  162. provider = new RGBCameraFrameProvider();
  163. #endif
  164. break;
  165. case BlendMode.VirtualOnly:
  166. default:
  167. provider = new NullDataFrameProvider(fps);
  168. break;
  169. }
  170. return provider;
  171. }
  172. /// <summary> Gets encoder by mode. </summary>
  173. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  174. /// <param name="mode"> The mode.</param>
  175. /// <returns> The encoder by mode. </returns>
  176. private IEncoder GetEncoderByMode(CamMode mode)
  177. {
  178. if (mode == CamMode.PhotoMode)
  179. {
  180. return new ImageEncoder();
  181. }
  182. else if (mode == CamMode.VideoMode)
  183. {
  184. return new VideoEncoder();
  185. }
  186. else
  187. {
  188. throw new Exception("CamMode need to be set correctly for encoder!");
  189. }
  190. }
  191. /// <summary> Stops capture mode. </summary>
  192. public void StopCaptureMode()
  193. {
  194. this.Release();
  195. }
  196. /// <summary> Starts a capture. </summary>
  197. public void StartCapture()
  198. {
  199. if (!m_IsInitialized)
  200. {
  201. return;
  202. }
  203. NRDebugger.Info("[CaptureContext] Start...");
  204. m_Encoder?.Start();
  205. m_FrameProvider?.Play();
  206. }
  207. /// <summary> Stops a capture. </summary>
  208. public void StopCapture()
  209. {
  210. if (!m_IsInitialized)
  211. {
  212. return;
  213. }
  214. NRDebugger.Info("[CaptureContext] Stop...");
  215. // Need stop encoder firstly.
  216. m_Encoder?.Stop();
  217. m_FrameProvider?.Stop();
  218. }
  219. /// <summary> Releases this object. </summary>
  220. public void Release()
  221. {
  222. if (!m_IsInitialized)
  223. {
  224. return;
  225. }
  226. NRDebugger.Info("[CaptureContext] Release begin...");
  227. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  228. stopwatch.Start();
  229. if (m_FrameProvider != null)
  230. {
  231. m_FrameProvider.OnUpdate -= UpdateFrame;
  232. m_FrameProvider?.Release();
  233. m_FrameProvider = null;
  234. }
  235. m_Blender?.Dispose();
  236. m_Encoder?.Release();
  237. if (m_CaptureBehaviour != null)
  238. {
  239. GameObject.DestroyImmediate(m_CaptureBehaviour.gameObject);
  240. m_CaptureBehaviour = null;
  241. }
  242. NRDebugger.Info("[CaptureContext] Release end, cost:{0} ms", stopwatch.ElapsedMilliseconds);
  243. m_IsInitialized = false;
  244. }
  245. }
  246. }