ObserverViewer.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Linq;
  14. using NRKernal.Experimental.Persistence;
  15. using NRKernal.Experimental.NetWork;
  16. [RequireComponent(typeof(ImageTrackingAnchorTool))]
  17. public class ObserverViewer : MonoBehaviour
  18. {
  19. /// <summary> The pose alignment tool. </summary>
  20. public ImageTrackingAnchorTool PoseAlignmentTool;
  21. /// <summary> Type of the mask. </summary>
  22. public NRPointerRaycaster.MaskTypeEnum maskType = NRPointerRaycaster.MaskTypeEnum.Exclusive;
  23. /// <summary> The mask. </summary>
  24. public LayerMask mask;
  25. /// <summary> The observer capture. </summary>
  26. NRObserverViewCapture m_ObserverCapture = null;
  27. /// <summary> The configuration view. </summary>
  28. ObserverConfigView m_ConfigView;
  29. /// <summary> The current configuration. </summary>
  30. ObserverViewConfig m_CurrentConfig;
  31. public enum ViewState
  32. {
  33. UnInitialized,
  34. Initialized,
  35. Playing,
  36. Stopped,
  37. Error
  38. }
  39. private ViewState currentViewState = ViewState.UnInitialized;
  40. /// <summary> Starts this object. </summary>
  41. void Awake()
  42. {
  43. GameObject.DontDestroyOnLoad(gameObject);
  44. CreateVideoCapture();
  45. m_ConfigView = FindObjectOfType<ObserverConfigView>();
  46. m_ConfigView.OnConfigrationChanged += OnConfigrationChanged;
  47. PoseAlignmentTool.OnAnchorPoseUpdate += UpdateObserverViewPose;
  48. }
  49. /// <summary> Executes the 'configration changed' action. </summary>
  50. /// <param name="config"> The configuration.</param>
  51. private void OnConfigrationChanged(ObserverViewConfig config)
  52. {
  53. NRDebugger.Info("[ObserverViewer] OnConfigrationChanged : " + config.ToString());
  54. this.m_CurrentConfig.serverIP = config.serverIP;
  55. this.m_CurrentConfig.useDebugUI = config.useDebugUI;
  56. this.m_CurrentConfig.offset = config.offset;
  57. if (currentViewState == ViewState.Initialized)
  58. {
  59. LocalServerSearcher.Instance.Search((serverinfo) =>
  60. {
  61. if (serverinfo.isSuccess)
  62. {
  63. NRDebugger.Info("[ObserverViewer] Find observerview server success,ipaddress:" + serverinfo.endPoint.ToString());
  64. m_CurrentConfig.serverIP = serverinfo.endPoint.Address.ToString();
  65. StartVideoCapture();
  66. }
  67. else
  68. {
  69. NRDebugger.Warning("[ObserverViewer] Can not find observerview server.");
  70. }
  71. });
  72. }
  73. else if (currentViewState == ViewState.Playing)
  74. {
  75. m_ObserverCapture.GetContext()?.GetBehaviour()?.SwitchDebugPanel(config.useDebugUI);
  76. }
  77. }
  78. /// <summary> Tests create video capture. </summary>
  79. void CreateVideoCapture()
  80. {
  81. NRObserverViewCapture.CreateAsync(false, delegate (NRObserverViewCapture videoCapture)
  82. {
  83. if (videoCapture != null)
  84. {
  85. m_ObserverCapture = videoCapture;
  86. }
  87. else
  88. {
  89. NRDebugger.Error("[ObserverViewer] Failed to create VideoCapture Instance!");
  90. }
  91. currentViewState = ViewState.Initialized;
  92. });
  93. }
  94. /// <summary> Updates the observer view pose described by pose. </summary>
  95. /// <param name="pose"> The pose.</param>
  96. void UpdateObserverViewPose(Pose pose)
  97. {
  98. if (m_ObserverCapture != null)
  99. {
  100. var newposition = pose.position + m_CurrentConfig.offset;
  101. var newpose = new Pose(newposition, pose.rotation);
  102. m_ObserverCapture.GetContext().GetBehaviour().UpdatePose(newpose);
  103. }
  104. }
  105. /// <summary> Starts video capture. </summary>
  106. public void StartVideoCapture()
  107. {
  108. if (currentViewState == ViewState.UnInitialized || currentViewState == ViewState.Playing)
  109. {
  110. NRDebugger.Warning("[ObserverViewer] StartVideoCapture faild : Current view state is in illegal status.");
  111. return;
  112. }
  113. Resolution cameraResolution = NRObserverViewCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).Last();
  114. NRDebugger.Info("[ObserverViewer] Get CameraResolution:" + cameraResolution);
  115. int cameraFramerate = NRObserverViewCapture.GetSupportedFrameRatesForResolution(cameraResolution).OrderByDescending((fps) => fps).First();
  116. NRDebugger.Info("[ObserverViewer] Get CameraFramerate:" + cameraFramerate);
  117. if (m_ObserverCapture != null)
  118. {
  119. NRDebugger.Info("[ObserverViewer] Created VideoCapture Instance!");
  120. CameraParameters cameraParameters = new CameraParameters();
  121. cameraParameters.hologramOpacity = 0.0f;
  122. cameraParameters.frameRate = cameraFramerate;
  123. cameraParameters.cameraResolutionWidth = cameraResolution.width;
  124. cameraParameters.cameraResolutionHeight = cameraResolution.height;
  125. cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
  126. m_ObserverCapture.StartObserverViewModeAsync(cameraParameters,
  127. NRObserverViewCapture.AudioState.ApplicationAndMicAudio,
  128. OnStartedVideoCaptureMode, true);
  129. }
  130. }
  131. /// <summary> Executes the 'started video capture mode' action. </summary>
  132. /// <param name="result"> The result.</param>
  133. void OnStartedVideoCaptureMode(NRObserverViewCapture.ObserverViewCaptureResult result)
  134. {
  135. NRDebugger.Info("[ObserverViewer] Started Video Capture Mode!");
  136. if (result.success)
  137. {
  138. m_ObserverCapture.StartObserverViewAsync(this.m_CurrentConfig.serverIP, OnStartedRecordingVideo);
  139. }
  140. else
  141. {
  142. currentViewState = ViewState.Error;
  143. NRDebugger.Info("[ObserverViewer] Started Video Capture Mode faild!");
  144. }
  145. }
  146. /// <summary> Executes the 'started recording video' action. </summary>
  147. /// <param name="result"> The result.</param>
  148. void OnStartedRecordingVideo(NRObserverViewCapture.ObserverViewCaptureResult result)
  149. {
  150. NRDebugger.Info("[ObserverViewer] Started Recording Video!");
  151. if (result.success)
  152. {
  153. m_ObserverCapture.GetContext().GetBehaviour().SwitchDebugPanel(m_CurrentConfig.useDebugUI);
  154. // Set the camera culling musk
  155. m_ObserverCapture.GetContext().GetBehaviour().SetCullingMask(maskType == NRPointerRaycaster.MaskTypeEnum.Inclusive ? (int)mask : ~mask);
  156. currentViewState = ViewState.Playing;
  157. }
  158. else
  159. {
  160. currentViewState = ViewState.Error;
  161. NRDebugger.Info("[ObserverViewer] Started Video Capture Mode faild!");
  162. }
  163. }
  164. /// <summary> Stops video capture. </summary>
  165. public void StopVideoCapture()
  166. {
  167. if (currentViewState != ViewState.Playing)
  168. {
  169. NRDebugger.Warning("[ObserverViewer] StopVideoCapture faild : Current view state is in illegal status.");
  170. return;
  171. }
  172. NRDebugger.Info("[ObserverViewer] Stop Video Capture!");
  173. m_ObserverCapture.StopObserverViewAsync(OnStoppedRecordingVideo);
  174. }
  175. /// <summary> Executes the 'stopped recording video' action. </summary>
  176. /// <param name="result"> The result.</param>
  177. void OnStoppedRecordingVideo(NRObserverViewCapture.ObserverViewCaptureResult result)
  178. {
  179. NRDebugger.Info("[ObserverViewer] Stopped Recording Video!");
  180. if (result.success)
  181. {
  182. m_ObserverCapture.StopObserverViewModeAsync(OnStoppedVideoCaptureMode);
  183. }
  184. else
  185. {
  186. currentViewState = ViewState.Error;
  187. NRDebugger.Info("[ObserverViewer] Stopped Video Capture faild!");
  188. }
  189. }
  190. /// <summary> Executes the 'stopped video capture mode' action. </summary>
  191. /// <param name="result"> The result.</param>
  192. void OnStoppedVideoCaptureMode(NRObserverViewCapture.ObserverViewCaptureResult result)
  193. {
  194. NRDebugger.Info("[ObserverViewer] Stopped Video Capture Mode!");
  195. if (result.success)
  196. {
  197. currentViewState = ViewState.Stopped;
  198. }
  199. else
  200. {
  201. currentViewState = ViewState.Error;
  202. NRDebugger.Info("[ObserverViewer] Stopped Video Capture Mode faild!");
  203. }
  204. }
  205. }
  206. }