NRObserverViewCapture.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 UnityEngine;
  12. using NRKernal.Record;
  13. using System.Collections.Generic;
  14. using System;
  15. /// <summary> A nr observer view capture. </summary>
  16. public class NRObserverViewCapture : IDisposable
  17. {
  18. /// <summary> Default constructor. </summary>
  19. public NRObserverViewCapture()
  20. {
  21. IsRecording = false;
  22. }
  23. /// <summary> Finalizer. </summary>
  24. ~NRObserverViewCapture()
  25. {
  26. }
  27. /// <summary> A list of all the supported device resolutions for observer view videos. </summary>
  28. /// <value> The supported resolutions. </value>
  29. public static IEnumerable<Resolution> SupportedResolutions
  30. {
  31. get
  32. {
  33. NativeResolution rgbResolution = new NativeResolution(1280, 720);
  34. if (NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_RGB_CAMERA))
  35. rgbResolution = NRFrame.GetDeviceResolution(NativeDevice.RGB_CAMERA);
  36. Resolution stand_resolution = new Resolution()
  37. {
  38. width = rgbResolution.width,
  39. height = rgbResolution.height,
  40. refreshRate = NativeConstants.RECORD_FPS_DEFAULT,
  41. };
  42. yield return stand_resolution;
  43. Resolution low_resolution = new Resolution()
  44. {
  45. width = stand_resolution.width / 2,
  46. height = stand_resolution.height / 2,
  47. refreshRate = NativeConstants.RECORD_FPS_DEFAULT,
  48. };
  49. yield return low_resolution;
  50. Resolution high_resolution = new Resolution()
  51. {
  52. width = stand_resolution.width * 3 / 2,
  53. height = stand_resolution.height * 3 / 2,
  54. refreshRate = NativeConstants.RECORD_FPS_DEFAULT,
  55. };
  56. yield return high_resolution;
  57. }
  58. }
  59. /// <summary>
  60. /// Indicates whether or not the VideoCapture instance is currently recording video. </summary>
  61. /// <value> True if this object is recording, false if not. </value>
  62. public bool IsRecording { get; private set; }
  63. /// <summary> Context for the capture. </summary>
  64. private ObserverViewFrameCaptureContext m_CaptureContext;
  65. /// <summary> Gets the context. </summary>
  66. /// <returns> The context. </returns>
  67. public ObserverViewFrameCaptureContext GetContext()
  68. {
  69. return m_CaptureContext;
  70. }
  71. /// <summary> Gets the preview texture. </summary>
  72. /// <value> The preview texture. </value>
  73. public Texture PreviewTexture
  74. {
  75. get
  76. {
  77. return m_CaptureContext?.PreviewTexture;
  78. }
  79. }
  80. /// <summary> Creates an asynchronous. </summary>
  81. /// <param name="showHolograms"> True to show, false to hide the holograms.</param>
  82. /// <param name="onCreatedCallback"> The on created callback.</param>
  83. public static void CreateAsync(bool showHolograms, OnObserverViewResourceCreatedCallback onCreatedCallback)
  84. {
  85. NRObserverViewCapture capture = new NRObserverViewCapture();
  86. capture.m_CaptureContext = new ObserverViewFrameCaptureContext();
  87. onCreatedCallback?.Invoke(capture);
  88. }
  89. /// <summary>
  90. /// Returns the supported frame rates at which a video can be recorded given a resolution. </summary>
  91. /// <param name="resolution"> A recording resolution.</param>
  92. /// <returns> The frame rates at which the video can be recorded. </returns>
  93. public static IEnumerable<int> GetSupportedFrameRatesForResolution(Resolution resolution)
  94. {
  95. yield return NativeConstants.RECORD_FPS_DEFAULT;
  96. }
  97. /// <summary> Dispose must be called to shutdown the PhotoCapture instance. </summary>
  98. public void Dispose()
  99. {
  100. if (m_CaptureContext != null)
  101. {
  102. m_CaptureContext.Release();
  103. m_CaptureContext = null;
  104. }
  105. }
  106. /// <summary> Starts observer view mode asynchronous. </summary>
  107. /// <param name="setupParams"> Options for controlling the setup.</param>
  108. /// <param name="audioState"> State of the audio.</param>
  109. /// <param name="onVideoModeStartedCallback"> The on video mode started callback.</param>
  110. public void StartObserverViewModeAsync(CameraParameters setupParams, AudioState audioState, OnObserverViewModeStartedCallback onVideoModeStartedCallback, bool autoAdaptBlendMode = false)
  111. {
  112. setupParams.camMode = CamMode.VideoMode;
  113. if (autoAdaptBlendMode)
  114. {
  115. var blendMode = m_CaptureContext.AutoAdaptBlendMode(setupParams.blendMode);
  116. if (blendMode != setupParams.blendMode)
  117. {
  118. NRDebugger.Warning("[VideoCapture] AutoAdaptBlendMode : {0} => {1}", setupParams.blendMode, blendMode);
  119. setupParams.blendMode = blendMode;
  120. }
  121. }
  122. m_CaptureContext.StartCaptureMode(setupParams);
  123. var result = new ObserverViewCaptureResult();
  124. result.resultType = CaptureResultType.Success;
  125. onVideoModeStartedCallback?.Invoke(result);
  126. }
  127. /// <summary> Starts observer view asynchronous. </summary>
  128. /// <param name="ip"> The IP.</param>
  129. /// <param name="onStartedRecordingVideoCallback"> The on started recording video callback.</param>
  130. public void StartObserverViewAsync(string ip, OnStartedObserverViewCallback onStartedRecordingVideoCallback)
  131. {
  132. var captureResult = new ObserverViewCaptureResult();
  133. if (IsRecording)
  134. {
  135. captureResult.resultType = CaptureResultType.UnknownError;
  136. onStartedRecordingVideoCallback?.Invoke(captureResult);
  137. }
  138. else
  139. {
  140. m_CaptureContext.StartCapture(ip, (result) =>
  141. {
  142. if (result)
  143. {
  144. IsRecording = true;
  145. captureResult.resultType = CaptureResultType.Success;
  146. onStartedRecordingVideoCallback?.Invoke(captureResult);
  147. }
  148. else
  149. {
  150. IsRecording = false;
  151. captureResult.resultType = CaptureResultType.ServiceIsNotAvailable;
  152. onStartedRecordingVideoCallback?.Invoke(captureResult);
  153. }
  154. });
  155. }
  156. }
  157. /// <summary> Stops observer view asynchronous. </summary>
  158. /// <param name="onStoppedRecordingVideoCallback"> The on stopped recording video callback.</param>
  159. public void StopObserverViewAsync(OnStoppedObserverViewCallback onStoppedRecordingVideoCallback)
  160. {
  161. var result = new ObserverViewCaptureResult();
  162. if (!IsRecording)
  163. {
  164. result.resultType = CaptureResultType.UnknownError;
  165. onStoppedRecordingVideoCallback?.Invoke(result);
  166. }
  167. else
  168. {
  169. try
  170. {
  171. m_CaptureContext.StopCapture();
  172. }
  173. catch (Exception e)
  174. {
  175. NRDebugger.Info("Stop recording error :" + e.ToString());
  176. throw;
  177. }
  178. IsRecording = false;
  179. result.resultType = CaptureResultType.Success;
  180. onStoppedRecordingVideoCallback?.Invoke(result);
  181. }
  182. }
  183. /// <summary> Stops observer view mode asynchronous. </summary>
  184. /// <param name="onVideoModeStoppedCallback"> The on video mode stopped callback.</param>
  185. public void StopObserverViewModeAsync(OnObserverViewModeStoppedCallback onVideoModeStoppedCallback)
  186. {
  187. m_CaptureContext.StopCaptureMode();
  188. var result = new ObserverViewCaptureResult();
  189. result.resultType = CaptureResultType.Success;
  190. onVideoModeStoppedCallback?.Invoke(result);
  191. }
  192. /// <summary> Contains the result of the capture request. </summary>
  193. public enum CaptureResultType
  194. {
  195. /// <summary>
  196. /// Specifies that the desired operation was successful.
  197. /// </summary>
  198. Success,
  199. /// <summary>
  200. /// Service is not available.
  201. /// </summary>
  202. ServiceIsNotAvailable,
  203. /// <summary>
  204. /// Specifies that an unknown error occurred.
  205. /// </summary>
  206. UnknownError
  207. }
  208. /// <summary>
  209. /// Specifies what audio sources should be recorded while recording the video. </summary>
  210. public enum AudioState
  211. {
  212. /// <summary>
  213. /// Only include the mic audio in the video recording.
  214. /// </summary>
  215. MicAudio = 0,
  216. /// <summary>
  217. /// Only include the application audio in the video recording.
  218. /// </summary>
  219. ApplicationAudio = 1,
  220. /// <summary>
  221. /// Include both the application audio as well as the mic audio in the video recording.
  222. /// </summary>
  223. ApplicationAndMicAudio = 2,
  224. /// <summary>
  225. /// Do not include any audio in the video recording.
  226. /// </summary>
  227. None = 3
  228. }
  229. /// <summary>
  230. /// A data container that contains the result information of a video recording operation. </summary>
  231. public struct ObserverViewCaptureResult
  232. {
  233. /// <summary>
  234. /// A generic result that indicates whether or not the VideoCapture operation succeeded. </summary>
  235. public CaptureResultType resultType;
  236. /// <summary> The specific HResult value. </summary>
  237. public long hResult;
  238. /// <summary> Indicates whether or not the operation was successful. </summary>
  239. /// <value> True if success, false if not. </value>
  240. public bool success
  241. {
  242. get
  243. {
  244. return resultType == CaptureResultType.Success;
  245. }
  246. }
  247. }
  248. /// <summary> Called when the web camera begins recording the video. </summary>
  249. /// <param name="result"> Indicates whether or not video recording started successfully.</param>
  250. public delegate void OnStartedObserverViewCallback(ObserverViewCaptureResult result);
  251. /// <summary> Called when a VideoCapture resource has been created. </summary>
  252. /// <param name="captureObject"> The VideoCapture instance.</param>
  253. public delegate void OnObserverViewResourceCreatedCallback(NRObserverViewCapture captureObject);
  254. /// <summary> Called when video mode has been started. </summary>
  255. /// <param name="result"> Indicates whether or not video mode was successfully activated.</param>
  256. public delegate void OnObserverViewModeStartedCallback(ObserverViewCaptureResult result);
  257. /// <summary> Called when video mode has been stopped. </summary>
  258. /// <param name="result"> Indicates whether or not video mode was successfully deactivated.</param>
  259. public delegate void OnObserverViewModeStoppedCallback(ObserverViewCaptureResult result);
  260. /// <summary> Called when the video recording has been saved to the file system. </summary>
  261. /// <param name="result"> Indicates whether or not video recording was saved successfully to the
  262. /// file system.</param>
  263. public delegate void OnStoppedObserverViewCallback(ObserverViewCaptureResult result);
  264. }
  265. }