ZEDSVOManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. // ##DEPRECATED
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. /// <summary>
  8. /// [Obsolete] Lets you play back a recorded SVO file, which works in place of the input from a real ZED,
  9. /// or record an SVO file from a live ZED.
  10. /// <para>To use, attach to the same GameObject as ZED Manager (ZED_Rig_Mono or ZED_Rig_Stereo).</para
  11. /// </summary><remarks>
  12. /// [Obsolete] When playing an SVO, the SDK behaves the same as if a real ZED were attached, so all regular features
  13. /// are available. The one exception is pass-through AR, as the ZED's timestamps and transform will both be
  14. /// significantly out of sync with the VR headset.
  15. /// </remarks>
  16. [System.Obsolete("SVO Management has been moved to ZEDManager directly. Not used anymore", true)]
  17. public class ZEDSVOManager : MonoBehaviour
  18. {
  19. /// <summary>
  20. /// Set to true to record an SVO. Recording begins when the ZED initialized and ends when the
  21. /// application finishes.
  22. /// </summary>
  23. [SerializeField]
  24. [Tooltip("Set to true to record an SVO. Recording begins when the ZED initialized and ends when " +
  25. "the application finishes.")]
  26. public bool record = false;
  27. /// <summary>
  28. /// Set to true to read an SVO, using it as input instead of a real ZED. An SVO cannot be read and recorded
  29. /// at the same time.
  30. /// </summary>
  31. [SerializeField]
  32. [Tooltip("Set to true to read an SVO, using it as input instead of a real ZED. " +
  33. "An SVO cannot be read and recorded at the same time.")]
  34. public bool read = false;
  35. /// <summary>
  36. /// Path to the SVO to be read, or where a new SVO will be recorded.
  37. /// <para>Note: If building the application, put the file in the root directory or specify a non-relative path
  38. /// to preserve this reference.</para>
  39. /// </summary>
  40. [SerializeField]
  41. [HideInInspector]
  42. public string videoFile = "Assets/ZEDRecording.svo";
  43. /// <summary>
  44. /// If reading an SVO, set this to true if the SVO should repeat once it finishes.
  45. /// <para>Some features won't handle this gracefully, such as tracking.</para>
  46. /// </summary>
  47. [SerializeField]
  48. [Tooltip("If reading an SVO, set this to true if the SVO should repeat once it finishes. " +
  49. "Some features won't handle this gracefully, such as tracking.")]
  50. public bool loop = false;
  51. /// <summary>
  52. /// If reading an SVO, set true to use frame timestamps to set playback speed. Dropped
  53. /// frames will cause a 'pause' in playback instead of a 'skip.'
  54. /// </summary>
  55. [Tooltip("If reading an SVO, set true to use frame timestamps to set playback speed. " +
  56. "Dropped frames will cause a 'pause' in playback instead of a 'skip.'")]
  57. [SerializeField]
  58. [HideInInspector]
  59. public bool realtimePlayback = false;
  60. /// <summary>
  61. /// Current frame being read from the SVO. Doesn't apply when recording.
  62. /// </summary>
  63. [HideInInspector]
  64. [SerializeField]
  65. private int currentFrame = 0;
  66. /// <summary>
  67. /// Current frame being read from the SVO. Doesn't apply when recording.
  68. /// </summary>
  69. public int CurrentFrame
  70. {
  71. get
  72. {
  73. return currentFrame;
  74. }
  75. set
  76. {
  77. currentFrame = value;
  78. }
  79. }
  80. /// <summary>
  81. /// Total number of frames in a loaded SVO.
  82. /// </summary>
  83. [HideInInspector]
  84. [SerializeField]
  85. private int numberFrameMax = 0;
  86. /// <summary>
  87. /// Total number of frames in a loaded SVO.
  88. /// </summary>
  89. public int NumberFrameMax
  90. {
  91. set
  92. {
  93. numberFrameMax = value;
  94. }
  95. get
  96. {
  97. return numberFrameMax;
  98. }
  99. }
  100. /// <summary>
  101. /// Set true to pause the SVO reading or recording. Will not pause the Unity scene itself.
  102. /// </summary>
  103. [HideInInspector]
  104. [SerializeField]
  105. public bool pause = false;
  106. /// <summary>
  107. /// Compression mode used when recording an SVO.
  108. /// Uncompressed SVOs are extremely large (multiple gigabytes per minute).
  109. /// </summary>
  110. [Tooltip("Compression mode used when recording an SVO. " +
  111. "Uncompressed SVOs are extremely large (multiple gigabytes per minute).")]
  112. [SerializeField]
  113. public sl.SVO_COMPRESSION_MODE compressionMode = sl.SVO_COMPRESSION_MODE.H264_BASED;
  114. /// <summary>
  115. /// Flag set to true when we need to force ZEDManager to grab a new frame, even though
  116. /// SVO playback is paused. Used when the SVO is paused but the playback slider has moved.
  117. /// </summary>
  118. public bool NeedNewFrameGrab { get; set; }
  119. /// <summary>
  120. /// ZED Camera
  121. /// </summary>
  122. public sl.ZEDCamera zedCam;
  123. /// <summary>
  124. /// Changes the value of record if recording fails, and gets the length of a read SVO file.
  125. /// </summary>
  126. /// <param name="zedCamera">Reference to the Scene's ZEDCamera instance.</param>
  127. public void InitSVO(sl.ZEDCamera zedCamera)
  128. {
  129. zedCam = zedCamera;
  130. if (record)
  131. {
  132. sl.ERROR_CODE svoerror = zedCamera.EnableRecording(videoFile, compressionMode,0,0);
  133. if (svoerror != sl.ERROR_CODE.SUCCESS)
  134. {
  135. record = false;
  136. }
  137. else if(svoerror == sl.ERROR_CODE.SVO_RECORDING_ERROR)
  138. {
  139. Debug.LogError("SVO recording failed. Check that there is enough space on the drive and that the "
  140. + "path provided is valid.");
  141. }
  142. }
  143. if (read)
  144. {
  145. NumberFrameMax = zedCamera.GetSVONumberOfFrames();
  146. }
  147. }
  148. }
  149. #if UNITY_EDITOR
  150. /// <summary>
  151. /// Custom editor for ZEDSVOManager to change how it's drawn in the Inspector.
  152. /// Adds a playback slider and pause button, and makes Record and Read mutually-exclusive.
  153. /// </summary>
  154. [System.Obsolete("SVO Management has been moved to ZEDManager directly. Not used anymore", true)]
  155. [CustomEditor(typeof(ZEDSVOManager)), CanEditMultipleObjects]
  156. public class SVOManagerInspector : Editor
  157. {
  158. //Caches for record and read values, to make them mutually exclusive.
  159. private bool current_recordValue = false;
  160. private bool current_readValue = false;
  161. //Serializable versions of ZEDSVOManager's values so changes can be saved/serialized.
  162. private SerializedProperty pause;
  163. private SerializedProperty record;
  164. private SerializedProperty read;
  165. private SerializedProperty loop;
  166. private SerializedProperty videoFile;
  167. private SerializedProperty currentFrame;
  168. private SerializedProperty numberFrameMax;
  169. Rect drop_area; //Bounds for dragging and dropping SVO files.
  170. private GUILayoutOption[] optionsButtonBrowse = { GUILayout.MaxWidth(30) }; //Adds padding for the SVO browse button.
  171. string pauseText = "Pause";
  172. string pauseTooltip = " SVO playback or recording."; //Appended to the pause Text to make tooltip text.
  173. string[] filters = { "Svo files", "svo" }; //Filters used for browsing for an SVO.
  174. private ZEDSVOManager obj;
  175. /// <summary>
  176. /// Called by Unity each time the editor is viewed.
  177. /// </summary>
  178. public override void OnInspectorGUI()
  179. {
  180. serializedObject.Update();
  181. obj = (ZEDSVOManager)target;
  182. EditorGUI.BeginChangeCheck();
  183. DrawDefaultInspector();
  184. using (new EditorGUI.DisabledScope(Application.isPlaying))
  185. {
  186. string tooltip = "If reading an SVO, set true to use frame timestamps to set playback speed." +
  187. "Dropped frames will cause a 'pause' in playback instead of a 'skip.'";
  188. obj.realtimePlayback = EditorGUILayout.Toggle(new GUIContent("Realtime Playback", tooltip), obj.realtimePlayback);
  189. }
  190. EditorGUILayout.BeginHorizontal();
  191. GUIContent pathlabel = new GUIContent("SVO Path", "Path to the SVO to be read, or where a new SVO will be recorded. " +
  192. "Note: If building the application, put the file in the root directory or specify a non-relative path " +
  193. "to preserve this reference.");
  194. videoFile.stringValue = EditorGUILayout.TextField(pathlabel, videoFile.stringValue);
  195. GUIContent loadlabel = new GUIContent("...", "Browse for existing SVO file.");
  196. if (GUILayout.Button(loadlabel, optionsButtonBrowse))
  197. {
  198. obj.videoFile = EditorUtility.OpenFilePanelWithFilters("Load SVO", "", filters);
  199. serializedObject.ApplyModifiedProperties();
  200. }
  201. EditorGUILayout.EndHorizontal();
  202. if (drop_area.width != EditorGUIUtility.currentViewWidth || drop_area.height != Screen.height)
  203. {
  204. drop_area = new Rect(0, 0, EditorGUIUtility.currentViewWidth, Screen.height);
  205. }
  206. if (EditorGUI.EndChangeCheck())
  207. {
  208. CheckChange();
  209. }
  210. EditorGUI.BeginChangeCheck();
  211. GUI.enabled = (obj.NumberFrameMax > 0);
  212. GUIContent sliderlabel = new GUIContent("Frame ", "SVO playback position");
  213. currentFrame.intValue = EditorGUILayout.IntSlider(sliderlabel, currentFrame.intValue, 0, numberFrameMax.intValue);
  214. if (EditorGUI.EndChangeCheck())
  215. {
  216. if (obj.zedCam != null)
  217. {
  218. //If the slider of frame from the SVO has moved, manually grab the frame and update the textures.
  219. obj.zedCam.SetSVOPosition(currentFrame.intValue);
  220. if (pause.boolValue)
  221. {
  222. obj.NeedNewFrameGrab = true;
  223. }
  224. }
  225. }
  226. GUI.enabled = false;
  227. if (obj.zedCam != null)
  228. GUI.enabled = obj.zedCam.IsCameraReady;
  229. pauseText = pause.boolValue ? "Resume" : "Pause";
  230. GUIContent pauselabel = new GUIContent(pauseText, pauseText + pauseTooltip);
  231. if (GUILayout.Button(pauselabel))
  232. {
  233. pause.boolValue = !pause.boolValue;
  234. }
  235. GUI.enabled = true;
  236. DropAreaGUI();
  237. serializedObject.ApplyModifiedProperties(); //Applies changes to serialized properties to the values they represent.
  238. }
  239. /// <summary>
  240. /// Binds the serialized properties to their respective values in ZEDSVOManager.
  241. /// </summary>
  242. private void OnEnable()
  243. {
  244. pause = serializedObject.FindProperty("pause");
  245. record = serializedObject.FindProperty("record");
  246. read = serializedObject.FindProperty("read");
  247. loop = serializedObject.FindProperty("loop");
  248. videoFile = serializedObject.FindProperty("videoFile");
  249. currentFrame = serializedObject.FindProperty("currentFrame");
  250. numberFrameMax = serializedObject.FindProperty("numberFrameMax");
  251. }
  252. /// <summary>
  253. /// Allows looping when reading an SVO file (only) and prevents
  254. /// reading and recording from both being true at the same time.
  255. /// </summary>
  256. private void CheckChange()
  257. {
  258. if (loop.boolValue && record.boolValue)
  259. {
  260. loop.boolValue = false;
  261. }
  262. if (read.boolValue && (current_readValue != read.boolValue))
  263. {
  264. record.boolValue = false;
  265. current_recordValue = false;
  266. current_readValue = read.boolValue;
  267. }
  268. if (!read.boolValue && (current_readValue != read.boolValue))
  269. {
  270. loop.boolValue = false;
  271. }
  272. if (record.boolValue && (current_recordValue != record.boolValue))
  273. {
  274. read.boolValue = false;
  275. current_readValue = false;
  276. loop.boolValue = false;
  277. current_recordValue = record.boolValue;
  278. }
  279. }
  280. /// <summary>
  281. /// Helper to get the name of a drag and dropped file.
  282. /// </summary>
  283. public void DropAreaGUI()
  284. {
  285. Event evt = Event.current;
  286. ZEDSVOManager obj = (ZEDSVOManager)target;
  287. switch (evt.type)
  288. {
  289. case EventType.DragUpdated:
  290. case EventType.DragPerform:
  291. if (!drop_area.Contains(evt.mousePosition))
  292. return;
  293. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  294. if (evt.type == EventType.DragPerform)
  295. {
  296. DragAndDrop.AcceptDrag();
  297. foreach (string dragged_object in DragAndDrop.paths)
  298. {
  299. videoFile.stringValue = dragged_object;
  300. }
  301. }
  302. break;
  303. }
  304. }
  305. }
  306. #endif