Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputEventTrace.cs 68KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine.InputSystem.Utilities;
  6. using Unity.Collections;
  7. using Unity.Collections.LowLevel.Unsafe;
  8. using UnityEngine.InputSystem.Layouts;
  9. using UnityEngine.Profiling;
  10. namespace UnityEngine.InputSystem.LowLevel
  11. {
  12. /// <summary>
  13. /// InputEventTrace lets you record input events for later processing. It also has features for writing traces
  14. /// to disk, for loading them from disk, and for playing back previously recorded traces.
  15. /// </summary>
  16. /// <remarks>
  17. /// InputEventTrace lets you record input events into a buffer for either a specific device, or for all events
  18. /// received by the input system. This is useful for testing purposes or for replaying recorded input.
  19. ///
  20. /// Note that event traces <em>must</em> be disposed of (by calling <see cref="Dispose"/>) after use or they
  21. /// will leak memory on the unmanaged (C++) memory heap.
  22. ///
  23. /// Event traces are serializable such that they can survive domain reloads in the editor.
  24. /// </remarks>
  25. [Serializable]
  26. public sealed unsafe class InputEventTrace : IDisposable, IEnumerable<InputEventPtr>
  27. {
  28. private const int kDefaultBufferSize = 1024 * 1024;
  29. /// <summary>
  30. /// If <see name="recordFrameMarkers"/> is enabled, an <see cref="InputEvent"/> with this <see cref="FourCC"/>
  31. /// code in its <see cref="InputEvent.type"/> is recorded whenever the input system starts a new update, i.e.
  32. /// whenever <see cref="InputSystem.onBeforeUpdate"/> is triggered. This is useful for replaying events in such
  33. /// a way that they are correctly spaced out over frames.
  34. /// </summary>
  35. public static FourCC FrameMarkerEvent => new FourCC('F', 'R', 'M', 'E');
  36. /// <summary>
  37. /// Set device to record events for. Set to <see cref="InputDevice.InvalidDeviceId"/> by default
  38. /// in which case events from all devices are recorded.
  39. /// </summary>
  40. public int deviceId
  41. {
  42. get => m_DeviceId;
  43. set => m_DeviceId = value;
  44. }
  45. /// <summary>
  46. /// Whether the trace is currently recording input.
  47. /// </summary>
  48. /// <value>True if the trace is currently recording events.</value>
  49. /// <seealso cref="Enable"/>
  50. /// <seealso cref="Disable"/>
  51. public bool enabled => m_Enabled;
  52. /// <summary>
  53. /// If true, input update boundaries will be recorded as events. By default, this is off.
  54. /// </summary>
  55. /// <value>Whether frame boundaries should be recorded in the trace.</value>
  56. /// <remarks>
  57. /// When recording with this off, all events are written one after the other for as long
  58. /// as the recording is active. This means that when a recording runs over multiple frames,
  59. /// it is no longer possible for the trace to tell which events happened in distinct frames.
  60. ///
  61. /// By turning this feature on, frame marker events (i.e. <see cref="InputEvent"/> instances
  62. /// with <see cref="InputEvent.type"/> set to <see cref="FrameMarkerEvent"/>) will be written
  63. /// to the trace every time an input update occurs. When playing such a trace back via <see
  64. /// cref="ReplayController.PlayAllFramesOneByOne"/>, events will get spaced out over frames corresponding
  65. /// to how they were spaced out when input was initially recorded.
  66. ///
  67. /// Note that having this feature enabled will fill up traces much quicker. Instead of being
  68. /// filled up only when there is input, TODO
  69. /// </remarks>
  70. /// <seealso cref="ReplayController.PlayAllFramesOneByOne"/>
  71. /// <seealso cref="FrameMarkerEvent"/>
  72. public bool recordFrameMarkers
  73. {
  74. get => m_RecordFrameMarkers;
  75. set
  76. {
  77. if (m_RecordFrameMarkers == value)
  78. return;
  79. m_RecordFrameMarkers = value;
  80. if (m_Enabled)
  81. {
  82. if (value)
  83. InputSystem.onBeforeUpdate += OnBeforeUpdate;
  84. else
  85. InputSystem.onBeforeUpdate -= OnBeforeUpdate;
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Total number of events currently in the trace.
  91. /// </summary>
  92. /// <value>Number of events recorded in the trace.</value>
  93. public long eventCount => m_EventCount;
  94. /// <summary>
  95. /// The amount of memory consumed by all events combined that are currently
  96. /// stored in the trace.
  97. /// </summary>
  98. /// <value>Total size of event data currently in trace.</value>
  99. public long totalEventSizeInBytes => m_EventSizeInBytes;
  100. /// <summary>
  101. /// Total size of memory buffer (in bytes) currently allocated.
  102. /// </summary>
  103. /// <value>Size of memory currently allocated.</value>
  104. /// <remarks>
  105. /// The buffer is allocated on the unmanaged heap.
  106. /// </remarks>
  107. public long allocatedSizeInBytes => m_EventBuffer != default ? m_EventBufferSize : 0;
  108. /// <summary>
  109. /// Largest size (in bytes) that the memory buffer is allowed to grow to. By default, this is
  110. /// the same as <see cref="allocatedSizeInBytes"/> meaning that the buffer is not allowed to grow but will
  111. /// rather wrap around when full.
  112. /// </summary>
  113. /// <value>Largest size the memory buffer is allowed to grow to.</value>
  114. public long maxSizeInBytes => m_MaxEventBufferSize;
  115. /// <summary>
  116. /// Information about all devices for which events have been recorded in the trace.
  117. /// </summary>
  118. /// <value>Record of devices recorded in the trace.</value>
  119. public ReadOnlyArray<DeviceInfo> deviceInfos => m_DeviceInfos;
  120. /// <summary>
  121. /// Optional delegate to decide whether an input should be stored in a trace. Null by default.
  122. /// </summary>
  123. /// <value>Delegate to accept or reject individual events.</value>
  124. /// <remarks>
  125. /// When this is set, the callback will be invoked on every event that would otherwise be stored
  126. /// directly in the trace. If the callback returns <c>true</c>, the trace will continue to record
  127. /// the event. If the callback returns <c>false</c>, the event will be ignored and not recorded.
  128. ///
  129. /// The callback should generally mutate the event. If you do so, note that this will impact
  130. /// event processing in general, not just recording of the event in the trace.
  131. /// </remarks>
  132. public Func<InputEventPtr, InputDevice, bool> onFilterEvent
  133. {
  134. get => m_OnFilterEvent;
  135. set => m_OnFilterEvent = value;
  136. }
  137. /// <summary>
  138. /// Event that is triggered every time an event has been recorded in the trace.
  139. /// </summary>
  140. public event Action<InputEventPtr> onEvent
  141. {
  142. add => m_EventListeners.AddCallback(value);
  143. remove => m_EventListeners.RemoveCallback(value);
  144. }
  145. public InputEventTrace(InputDevice device, long bufferSizeInBytes = kDefaultBufferSize, bool growBuffer = false,
  146. long maxBufferSizeInBytes = -1, long growIncrementSizeInBytes = -1)
  147. : this(bufferSizeInBytes, growBuffer, maxBufferSizeInBytes, growIncrementSizeInBytes)
  148. {
  149. if (device == null)
  150. throw new ArgumentNullException(nameof(device));
  151. m_DeviceId = device.deviceId;
  152. }
  153. /// <summary>
  154. /// Create a disabled event trace that does not perform any allocation yet. An event trace only starts consuming resources
  155. /// the first time it is enabled.
  156. /// </summary>
  157. /// <param name="bufferSizeInBytes">Size of buffer that will be allocated on first event captured by trace. Defaults to 1MB.</param>
  158. /// <param name="growBuffer">If true, the event buffer will be grown automatically when it reaches capacity, up to a maximum
  159. /// size of <paramref name="maxBufferSizeInBytes"/>. This is off by default.</param>
  160. /// <param name="maxBufferSizeInBytes">If <paramref name="growBuffer"/> is true, this is the maximum size that the buffer should
  161. /// be grown to. If the maximum size is reached, old events are being overwritten.</param>
  162. public InputEventTrace(long bufferSizeInBytes = kDefaultBufferSize, bool growBuffer = false, long maxBufferSizeInBytes = -1, long growIncrementSizeInBytes = -1)
  163. {
  164. m_EventBufferSize = (uint)bufferSizeInBytes;
  165. if (growBuffer)
  166. {
  167. if (maxBufferSizeInBytes < 0)
  168. m_MaxEventBufferSize = 256 * kDefaultBufferSize;
  169. else
  170. m_MaxEventBufferSize = maxBufferSizeInBytes;
  171. if (growIncrementSizeInBytes < 0)
  172. m_GrowIncrementSize = kDefaultBufferSize;
  173. else
  174. m_GrowIncrementSize = growIncrementSizeInBytes;
  175. }
  176. else
  177. {
  178. m_MaxEventBufferSize = m_EventBufferSize;
  179. }
  180. }
  181. /// <summary>
  182. /// Write the contents of the event trace to a file.
  183. /// </summary>
  184. /// <param name="filePath">Path of the file to write.</param>
  185. /// <exception cref="ArgumentNullException"><paramref name="filePath"/> is <c>null</c> or empty.</exception>
  186. /// <exception cref="FileNotFoundException"><paramref name="filePath"/> is invalid.</exception>
  187. /// <exception cref="DirectoryNotFoundException">A directory in <paramref name="filePath"/> is invalid.</exception>
  188. /// <exception cref="UnauthorizedAccessException"><paramref name="filePath"/> cannot be accessed.</exception>
  189. /// <seealso cref="ReadFrom(string)"/>
  190. public void WriteTo(string filePath)
  191. {
  192. if (string.IsNullOrEmpty(filePath))
  193. throw new ArgumentNullException(nameof(filePath));
  194. using (var stream = File.OpenWrite(filePath))
  195. WriteTo(stream);
  196. }
  197. /// <summary>
  198. /// Write the contents of the event trace to the given stream.
  199. /// </summary>
  200. /// <param name="stream">Stream to write the data to. Must support seeking (i.e. <c>Stream.canSeek</c> must be true).</param>
  201. /// <exception cref="ArgumentNullException"><paramref name="stream"/> is <c>null</c>.</exception>
  202. /// <exception cref="ArgumentException"><paramref name="stream"/> does not support seeking.</exception>
  203. /// <exception cref="IOException">An error occurred trying to write to <paramref name="stream"/>.</exception>
  204. public void WriteTo(Stream stream)
  205. {
  206. if (stream == null)
  207. throw new ArgumentNullException(nameof(stream));
  208. if (!stream.CanSeek)
  209. throw new ArgumentException("Stream does not support seeking", nameof(stream));
  210. var writer = new BinaryWriter(stream);
  211. var flags = default(FileFlags);
  212. if (InputSystem.settings.updateMode == InputSettings.UpdateMode.ProcessEventsInFixedUpdate)
  213. flags |= FileFlags.FixedUpdate;
  214. // Write header.
  215. writer.Write(kFileFormat);
  216. writer.Write(kFileVersion);
  217. writer.Write((int)flags);
  218. writer.Write((int)Application.platform);
  219. writer.Write((ulong)m_EventCount);
  220. writer.Write((ulong)m_EventSizeInBytes);
  221. // Write events.
  222. foreach (var eventPtr in this)
  223. {
  224. ////TODO: find way to directly write a byte* buffer to the stream instead of copying to a temp byte[]
  225. var sizeInBytes = eventPtr.sizeInBytes;
  226. var buffer = new byte[sizeInBytes];
  227. fixed(byte* bufferPtr = buffer)
  228. {
  229. UnsafeUtility.MemCpy(bufferPtr, eventPtr.data, sizeInBytes);
  230. writer.Write(buffer);
  231. }
  232. }
  233. // Write devices.
  234. writer.Flush();
  235. var positionOfDeviceList = stream.Position;
  236. var deviceCount = m_DeviceInfos.LengthSafe();
  237. writer.Write(deviceCount);
  238. for (var i = 0; i < deviceCount; ++i)
  239. {
  240. ref var device = ref m_DeviceInfos[i];
  241. writer.Write(device.deviceId);
  242. writer.Write(device.layout);
  243. writer.Write(device.stateFormat);
  244. writer.Write(device.stateSizeInBytes);
  245. writer.Write(device.m_FullLayoutJson ?? string.Empty);
  246. }
  247. // Write offset of device list.
  248. writer.Flush();
  249. var offsetOfDeviceList = stream.Position - positionOfDeviceList;
  250. writer.Write(offsetOfDeviceList);
  251. }
  252. /// <summary>
  253. /// Read the contents of an input event trace stored in the given file.
  254. /// </summary>
  255. /// <param name="filePath">Path to a file.</param>
  256. /// <exception cref="ArgumentNullException"><paramref name="filePath"/> is <c>null</c> or empty.</exception>
  257. /// <exception cref="FileNotFoundException"><paramref name="filePath"/> is invalid.</exception>
  258. /// <exception cref="DirectoryNotFoundException">A directory in <paramref name="filePath"/> is invalid.</exception>
  259. /// <exception cref="UnauthorizedAccessException"><paramref name="filePath"/> cannot be accessed.</exception>
  260. /// <remarks>
  261. /// This method replaces the contents of the trace with those read from the given file.
  262. /// </remarks>
  263. /// <seealso cref="WriteTo(string)"/>
  264. public void ReadFrom(string filePath)
  265. {
  266. if (string.IsNullOrEmpty(filePath))
  267. throw new ArgumentNullException(nameof(filePath));
  268. using (var stream = File.OpenRead(filePath))
  269. ReadFrom(stream);
  270. }
  271. /// <summary>
  272. /// Read the contents of an input event trace from the given stream.
  273. /// </summary>
  274. /// <param name="stream">A stream of binary data containing a recorded event trace as written out with <see cref="WriteTo(Stream)"/>.
  275. /// Must support reading.</param>
  276. /// <exception cref="ArgumentNullException"><paramref name="stream"/> is <c>null</c>.</exception>
  277. /// <exception cref="ArgumentException"><paramref name="stream"/> does not support reading.</exception>
  278. /// <exception cref="IOException">An error occurred trying to read from <paramref name="stream"/>.</exception>
  279. /// <remarks>
  280. /// This method replaces the contents of the event trace with those read from the stream. It does not append
  281. /// to the existing trace.
  282. /// </remarks>
  283. /// <seealso cref="WriteTo(Stream)"/>
  284. public void ReadFrom(Stream stream)
  285. {
  286. if (stream == null)
  287. throw new ArgumentNullException(nameof(stream));
  288. if (!stream.CanRead)
  289. throw new ArgumentException("Stream does not support reading", nameof(stream));
  290. var reader = new BinaryReader(stream);
  291. // Read header.
  292. if (reader.ReadInt32() != kFileFormat)
  293. throw new IOException($"Stream does not appear to be an InputEventTrace (no '{kFileFormat}' code)");
  294. if (reader.ReadInt32() > kFileVersion)
  295. throw new IOException($"Stream is an InputEventTrace but a newer version (expected version {kFileVersion} or below)");
  296. reader.ReadInt32(); // Flags; ignored for now.
  297. reader.ReadInt32(); // Platform; for now we're not doing anything with it.
  298. var eventCount = reader.ReadUInt64();
  299. var totalEventSizeInBytes = reader.ReadUInt64();
  300. var oldBuffer = m_EventBuffer;
  301. if (eventCount > 0 && totalEventSizeInBytes > 0)
  302. {
  303. // Allocate buffer, if need be.
  304. byte* buffer;
  305. if (m_EventBuffer != null && m_EventBufferSize >= (long)totalEventSizeInBytes)
  306. {
  307. // Existing buffer is large enough.
  308. buffer = m_EventBuffer;
  309. }
  310. else
  311. {
  312. buffer = (byte*)UnsafeUtility.Malloc((long)totalEventSizeInBytes, InputEvent.kAlignment, Allocator.Persistent);
  313. m_EventBufferSize = (long)totalEventSizeInBytes;
  314. }
  315. try
  316. {
  317. // Read events.
  318. var tailPtr = buffer;
  319. var endPtr = tailPtr + totalEventSizeInBytes;
  320. var totalEventSize = 0L;
  321. for (var i = 0ul; i < eventCount; ++i)
  322. {
  323. var eventType = reader.ReadInt32();
  324. var eventSizeInBytes = (uint)reader.ReadUInt16();
  325. var eventDeviceId = (uint)reader.ReadUInt16();
  326. if (eventSizeInBytes > endPtr - tailPtr)
  327. break;
  328. *(int*)tailPtr = eventType;
  329. tailPtr += 4;
  330. *(ushort*)tailPtr = (ushort)eventSizeInBytes;
  331. tailPtr += 2;
  332. *(ushort*)tailPtr = (ushort)eventDeviceId;
  333. tailPtr += 2;
  334. ////TODO: find way to directly read from stream into a byte* pointer
  335. var remainingSize = (int)eventSizeInBytes - sizeof(int) - sizeof(short) - sizeof(short);
  336. var tempBuffer = reader.ReadBytes(remainingSize);
  337. fixed(byte* tempBufferPtr = tempBuffer)
  338. UnsafeUtility.MemCpy(tailPtr, tempBufferPtr, remainingSize);
  339. tailPtr += remainingSize.AlignToMultipleOf(InputEvent.kAlignment);
  340. totalEventSize += eventSizeInBytes.AlignToMultipleOf(InputEvent.kAlignment);
  341. if (tailPtr >= endPtr)
  342. break;
  343. }
  344. // Read device infos.
  345. var deviceCount = reader.ReadInt32();
  346. var deviceInfos = new DeviceInfo[deviceCount];
  347. for (var i = 0; i < deviceCount; ++i)
  348. {
  349. deviceInfos[i] = new DeviceInfo
  350. {
  351. deviceId = reader.ReadInt32(),
  352. layout = reader.ReadString(),
  353. stateFormat = reader.ReadInt32(),
  354. stateSizeInBytes = reader.ReadInt32(),
  355. m_FullLayoutJson = reader.ReadString()
  356. };
  357. }
  358. // Install buffer.
  359. m_EventBuffer = buffer;
  360. m_EventBufferHead = m_EventBuffer;
  361. m_EventBufferTail = endPtr;
  362. m_EventCount = (long)eventCount;
  363. m_EventSizeInBytes = totalEventSize;
  364. m_DeviceInfos = deviceInfos;
  365. }
  366. catch
  367. {
  368. if (buffer != oldBuffer)
  369. UnsafeUtility.Free(buffer, Allocator.Persistent);
  370. throw;
  371. }
  372. }
  373. else
  374. {
  375. m_EventBuffer = default;
  376. m_EventBufferHead = default;
  377. m_EventBufferTail = default;
  378. }
  379. // Release old buffer, if we've switched to a new one.
  380. if (m_EventBuffer != oldBuffer && oldBuffer != null)
  381. UnsafeUtility.Free(oldBuffer, Allocator.Persistent);
  382. ++m_ChangeCounter;
  383. }
  384. /// <summary>
  385. /// Load an input event trace from the given file.
  386. /// </summary>
  387. /// <param name="filePath">Path to a file.</param>
  388. /// <exception cref="ArgumentNullException"><paramref name="filePath"/> is <c>null</c> or empty.</exception>
  389. /// <exception cref="FileNotFoundException"><paramref name="filePath"/> is invalid.</exception>
  390. /// <exception cref="DirectoryNotFoundException">A directory in <paramref name="filePath"/> is invalid.</exception>
  391. /// <exception cref="UnauthorizedAccessException"><paramref name="filePath"/> cannot be accessed.</exception>
  392. /// <seealso cref="WriteTo(string)"/>
  393. /// <seealso cref="ReadFrom(string)"/>
  394. public static InputEventTrace LoadFrom(string filePath)
  395. {
  396. if (string.IsNullOrEmpty(filePath))
  397. throw new ArgumentNullException(nameof(filePath));
  398. using (var stream = File.OpenRead(filePath))
  399. return LoadFrom(stream);
  400. }
  401. /// <summary>
  402. /// Load an event trace from a previously captured event stream.
  403. /// </summary>
  404. /// <param name="stream">A stream as written by <see cref="WriteTo(Stream)"/>. Must support reading.</param>
  405. /// <returns>The loaded event trace.</returns>
  406. /// <exception cref="ArgumentException"><paramref name="stream"/> is not readable.</exception>
  407. /// <exception cref="ArgumentNullException"><paramref name="stream"/> is <c>null</c>.</exception>
  408. /// <exception cref="IOException">The stream cannot be loaded (e.g. wrong format; details in the exception).</exception>
  409. /// <seealso cref="WriteTo(Stream)"/>
  410. public static InputEventTrace LoadFrom(Stream stream)
  411. {
  412. if (stream == null)
  413. throw new ArgumentNullException(nameof(stream));
  414. if (!stream.CanRead)
  415. throw new ArgumentException("Stream must be readable", nameof(stream));
  416. var trace = new InputEventTrace();
  417. trace.ReadFrom(stream);
  418. return trace;
  419. }
  420. /// <summary>
  421. /// Start a replay of the events in the trace.
  422. /// </summary>
  423. /// <returns>An object that controls playback.</returns>
  424. /// <remarks>
  425. /// Calling this method implicitly turns off recording, if currently enabled (i.e. it calls <see cref="Disable"/>),
  426. /// as replaying an event trace cannot be done while it is also concurrently modified.
  427. /// </remarks>
  428. public ReplayController Replay()
  429. {
  430. Disable();
  431. return new ReplayController(this);
  432. }
  433. /// <summary>
  434. /// Resize the current event memory buffer to the specified size.
  435. /// </summary>
  436. /// <param name="newBufferSize">Size to allocate for the buffer.</param>
  437. /// <param name="newMaxBufferSize">Optional parameter to specifying the mark up to which the buffer is allowed to grow. By default,
  438. /// this is negative which indicates the buffer should not grow. In this case, <see cref="maxSizeInBytes"/> will be set
  439. /// to <paramref name="newBufferSize"/>. If this parameter is a non-negative number, it must be greater than or equal to
  440. /// <paramref name="newBufferSize"/> and will become the new value for <see cref="maxSizeInBytes"/>.</param>
  441. /// <returns>True if the new buffer was successfully allocated.</returns>
  442. /// <exception cref="ArgumentException"><paramref name="newBufferSize"/> is negative.</exception>
  443. public bool Resize(long newBufferSize, long newMaxBufferSize = -1)
  444. {
  445. if (newBufferSize <= 0)
  446. throw new ArgumentException("Size must be positive", nameof(newBufferSize));
  447. if (m_EventBufferSize == newBufferSize)
  448. return true;
  449. if (newMaxBufferSize < newBufferSize)
  450. newMaxBufferSize = newBufferSize;
  451. // Allocate.
  452. var newEventBuffer = (byte*)UnsafeUtility.Malloc(newBufferSize, InputEvent.kAlignment, Allocator.Persistent);
  453. if (newEventBuffer == default)
  454. return false;
  455. // If we have existing contents, migrate them.
  456. if (m_EventCount > 0)
  457. {
  458. // If we're shrinking the buffer or have a buffer that has already wrapped around,
  459. // migrate events one by one.
  460. if (newBufferSize < m_EventBufferSize || m_HasWrapped)
  461. {
  462. var fromPtr = new InputEventPtr((InputEvent*)m_EventBufferHead);
  463. var toPtr = (InputEvent*)newEventBuffer;
  464. var newEventCount = 0;
  465. var newEventSizeInBytes = 0;
  466. var remainingEventBytes = m_EventSizeInBytes;
  467. for (var i = 0; i < m_EventCount; ++i)
  468. {
  469. var eventSizeInBytes = fromPtr.sizeInBytes;
  470. var alignedEventSizeInBytes = eventSizeInBytes.AlignToMultipleOf(InputEvent.kAlignment);
  471. // We only start copying once we know that the remaining events we have fit in the new buffer.
  472. // This way we get the newest events and not the oldest ones.
  473. if (remainingEventBytes <= newBufferSize)
  474. {
  475. UnsafeUtility.MemCpy(toPtr, fromPtr.ToPointer(), eventSizeInBytes);
  476. toPtr = InputEvent.GetNextInMemory(toPtr);
  477. newEventSizeInBytes += (int)alignedEventSizeInBytes;
  478. ++newEventCount;
  479. }
  480. remainingEventBytes -= alignedEventSizeInBytes;
  481. if (!GetNextEvent(ref fromPtr))
  482. break;
  483. }
  484. m_HasWrapped = false;
  485. m_EventCount = newEventCount;
  486. m_EventSizeInBytes = newEventSizeInBytes;
  487. }
  488. else
  489. {
  490. // Simple case of just having to copy everything between head and tail.
  491. UnsafeUtility.MemCpy(newEventBuffer,
  492. m_EventBufferHead,
  493. m_EventSizeInBytes);
  494. }
  495. }
  496. if (m_EventBuffer != null)
  497. UnsafeUtility.Free(m_EventBuffer, Allocator.Persistent);
  498. m_EventBufferSize = newBufferSize;
  499. m_EventBuffer = newEventBuffer;
  500. m_EventBufferHead = newEventBuffer;
  501. m_EventBufferTail = m_EventBuffer + m_EventSizeInBytes;
  502. m_MaxEventBufferSize = newMaxBufferSize;
  503. ++m_ChangeCounter;
  504. return true;
  505. }
  506. /// <summary>
  507. /// Reset the trace. Clears all recorded events.
  508. /// </summary>
  509. public void Clear()
  510. {
  511. m_EventBufferHead = m_EventBufferTail = default;
  512. m_EventCount = 0;
  513. m_EventSizeInBytes = 0;
  514. ++m_ChangeCounter;
  515. m_DeviceInfos = null;
  516. }
  517. /// <summary>
  518. /// Start recording events.
  519. /// </summary>
  520. /// <seealso cref="Disable"/>
  521. public void Enable()
  522. {
  523. if (m_Enabled)
  524. return;
  525. if (m_EventBuffer == default)
  526. Allocate();
  527. InputSystem.onEvent += OnInputEvent;
  528. if (m_RecordFrameMarkers)
  529. InputSystem.onBeforeUpdate += OnBeforeUpdate;
  530. m_Enabled = true;
  531. }
  532. /// <summary>
  533. /// Stop recording events.
  534. /// </summary>
  535. /// <seealso cref="Enable"/>
  536. public void Disable()
  537. {
  538. if (!m_Enabled)
  539. return;
  540. InputSystem.onEvent -= OnInputEvent;
  541. InputSystem.onBeforeUpdate -= OnBeforeUpdate;
  542. m_Enabled = false;
  543. }
  544. /// <summary>
  545. /// Based on the given event pointer, return a pointer to the next event in the trace.
  546. /// </summary>
  547. /// <param name="current">A pointer to an event in the trace or a <c>default(InputEventTrace)</c>. In the former case,
  548. /// the pointer will be updated to the next event, if there is one. In the latter case, the pointer will be updated
  549. /// to the first event in the trace, if there is one.</param>
  550. /// <returns>True if <c>current</c> has been set to the next event, false otherwise.</returns>
  551. /// <remarks>
  552. /// Event storage in memory may be circular if the event buffer is fixed in size or has reached maximum
  553. /// size and new events start overwriting old events. This method will automatically start with the first
  554. /// event when the given <paramref name="current"/> event is null. Any subsequent call with then loop over
  555. /// the remaining events until no more events are available.
  556. ///
  557. /// Note that it is VERY IMPORTANT that the buffer is not modified while iterating over events this way.
  558. /// If this is not ensured, invalid memory accesses may result.
  559. ///
  560. /// <example>
  561. /// <code>
  562. /// // Loop over all events in the InputEventTrace in the `trace` variable.
  563. /// var current = default(InputEventPtr);
  564. /// while (trace.GetNextEvent(ref current))
  565. /// {
  566. /// Debug.Log(current);
  567. /// }
  568. /// </code>
  569. /// </example>
  570. /// </remarks>
  571. public bool GetNextEvent(ref InputEventPtr current)
  572. {
  573. if (m_EventBuffer == default)
  574. return false;
  575. // If head is null, tail is too and it means there's nothing in the
  576. // buffer yet.
  577. if (m_EventBufferHead == default)
  578. return false;
  579. // If current is null, start iterating at head.
  580. if (!current.valid)
  581. {
  582. current = new InputEventPtr((InputEvent*)m_EventBufferHead);
  583. return true;
  584. }
  585. // Otherwise feel our way forward.
  586. var nextEvent = (byte*)current.Next().data;
  587. var endOfBuffer = m_EventBuffer + m_EventBufferSize;
  588. // If we've run into our tail, there's no more events.
  589. if (nextEvent == m_EventBufferTail)
  590. return false;
  591. // If we've reached blank space at the end of the buffer, wrap
  592. // around to the beginning. In this scenario there must be an event
  593. // at the beginning of the buffer; tail won't position itself at
  594. // m_EventBuffer.
  595. if (endOfBuffer - nextEvent < InputEvent.kBaseEventSize ||
  596. ((InputEvent*)nextEvent)->sizeInBytes == 0)
  597. {
  598. nextEvent = m_EventBuffer;
  599. if (nextEvent == current.ToPointer())
  600. return false; // There's only a single event in the buffer.
  601. }
  602. // We're good. There's still space between us and our tail.
  603. current = new InputEventPtr((InputEvent*)nextEvent);
  604. return true;
  605. }
  606. public IEnumerator<InputEventPtr> GetEnumerator()
  607. {
  608. return new Enumerator(this);
  609. }
  610. IEnumerator IEnumerable.GetEnumerator()
  611. {
  612. return GetEnumerator();
  613. }
  614. /// <summary>
  615. /// Stop recording, if necessary, and clear the trace such that it released unmanaged
  616. /// memory which might be allocated.
  617. /// </summary>
  618. /// <remarks>
  619. /// For any trace that has recorded events, calling this method is crucial in order to not
  620. /// leak memory on the unmanaged (C++) memory heap.
  621. /// </remarks>
  622. public void Dispose()
  623. {
  624. Disable();
  625. Release();
  626. }
  627. // We want to make sure that it's not possible to iterate with an enumerable over
  628. // a trace that is being changed so we bump this counter every time we modify the
  629. // buffer and check in the enumerator that the counts match.
  630. [NonSerialized] private int m_ChangeCounter;
  631. [NonSerialized] private bool m_Enabled;
  632. [NonSerialized] private Func<InputEventPtr, InputDevice, bool> m_OnFilterEvent;
  633. [SerializeField] private int m_DeviceId = InputDevice.InvalidDeviceId;
  634. [NonSerialized] private CallbackArray<Action<InputEventPtr>> m_EventListeners;
  635. // Buffer for storing event trace. Allocated in native so that we can survive a
  636. // domain reload without losing event traces.
  637. // NOTE: Ideally this would simply use InputEventBuffer but we can't serialize that one because
  638. // of the NativeArray it has inside. Also, due to the wrap-around nature, storage of
  639. // events in the buffer may not be linear.
  640. [SerializeField] private long m_EventBufferSize;
  641. [SerializeField] private long m_MaxEventBufferSize;
  642. [SerializeField] private long m_GrowIncrementSize;
  643. [SerializeField] private long m_EventCount;
  644. [SerializeField] private long m_EventSizeInBytes;
  645. // These are ulongs for the sake of Unity serialization which can't handle pointers or IntPtrs.
  646. [SerializeField] private ulong m_EventBufferStorage;
  647. [SerializeField] private ulong m_EventBufferHeadStorage;
  648. [SerializeField] private ulong m_EventBufferTailStorage;
  649. [SerializeField] private bool m_HasWrapped;
  650. [SerializeField] private bool m_RecordFrameMarkers;
  651. [SerializeField] private DeviceInfo[] m_DeviceInfos;
  652. private byte* m_EventBuffer
  653. {
  654. get => (byte*)m_EventBufferStorage;
  655. set => m_EventBufferStorage = (ulong)value;
  656. }
  657. private byte* m_EventBufferHead
  658. {
  659. get => (byte*)m_EventBufferHeadStorage;
  660. set => m_EventBufferHeadStorage = (ulong)value;
  661. }
  662. private byte* m_EventBufferTail
  663. {
  664. get => (byte*)m_EventBufferTailStorage;
  665. set => m_EventBufferTailStorage = (ulong)value;
  666. }
  667. private void Allocate()
  668. {
  669. m_EventBuffer = (byte*)UnsafeUtility.Malloc(m_EventBufferSize, InputEvent.kAlignment, Allocator.Persistent);
  670. }
  671. private void Release()
  672. {
  673. Clear();
  674. if (m_EventBuffer != default)
  675. {
  676. UnsafeUtility.Free(m_EventBuffer, Allocator.Persistent);
  677. m_EventBuffer = default;
  678. }
  679. }
  680. private void OnBeforeUpdate()
  681. {
  682. ////TODO: make this work correctly with the different update types
  683. if (m_RecordFrameMarkers)
  684. {
  685. // Record frame marker event.
  686. // NOTE: ATM these events don't get valid event IDs. Might be this is even useful but is more a side-effect
  687. // of there not being a method to obtain an ID except by actually queuing an event.
  688. var frameMarkerEvent = new InputEvent
  689. {
  690. type = FrameMarkerEvent,
  691. internalTime = InputRuntime.s_Instance.currentTime,
  692. sizeInBytes = (uint)UnsafeUtility.SizeOf<InputEvent>()
  693. };
  694. OnInputEvent(new InputEventPtr((InputEvent*)UnsafeUtility.AddressOf(ref frameMarkerEvent)), null);
  695. }
  696. }
  697. private void OnInputEvent(InputEventPtr inputEvent, InputDevice device)
  698. {
  699. // Ignore events that are already marked as handled.
  700. if (inputEvent.handled)
  701. return;
  702. // Ignore if the event isn't for our device (except if it's a frame marker).
  703. if (m_DeviceId != InputDevice.InvalidDeviceId && inputEvent.deviceId != m_DeviceId && inputEvent.type != FrameMarkerEvent)
  704. return;
  705. // Give callback a chance to filter event.
  706. if (m_OnFilterEvent != null && !m_OnFilterEvent(inputEvent, device))
  707. return;
  708. // This shouldn't happen but ignore the event if we're not tracing.
  709. if (m_EventBuffer == default)
  710. return;
  711. var bytesNeeded = inputEvent.sizeInBytes.AlignToMultipleOf(InputEvent.kAlignment);
  712. // Make sure we can fit the event at all.
  713. if (bytesNeeded > m_MaxEventBufferSize)
  714. return;
  715. Profiler.BeginSample("InputEventTrace");
  716. if (m_EventBufferTail == default)
  717. {
  718. // First event in buffer.
  719. m_EventBufferHead = m_EventBuffer;
  720. m_EventBufferTail = m_EventBuffer;
  721. }
  722. var newTail = m_EventBufferTail + bytesNeeded;
  723. var newTailOvertakesHead = newTail > m_EventBufferHead && m_EventBufferHead != m_EventBuffer;
  724. // If tail goes out of bounds, enlarge the buffer or wrap around to the beginning.
  725. var newTailGoesPastEndOfBuffer = newTail > m_EventBuffer + m_EventBufferSize;
  726. if (newTailGoesPastEndOfBuffer)
  727. {
  728. // If we haven't reached the max size yet, grow the buffer.
  729. if (m_EventBufferSize < m_MaxEventBufferSize && !m_HasWrapped)
  730. {
  731. var increment = Math.Max(m_GrowIncrementSize, bytesNeeded.AlignToMultipleOf(InputEvent.kAlignment));
  732. var newBufferSize = m_EventBufferSize + increment;
  733. if (newBufferSize > m_MaxEventBufferSize)
  734. newBufferSize = m_MaxEventBufferSize;
  735. if (newBufferSize < bytesNeeded)
  736. {
  737. Profiler.EndSample();
  738. return;
  739. }
  740. Resize(newBufferSize);
  741. newTail = m_EventBufferTail + bytesNeeded;
  742. }
  743. // See if we fit.
  744. var spaceLeft = m_EventBufferSize - (m_EventBufferTail - m_EventBuffer);
  745. if (spaceLeft < bytesNeeded)
  746. {
  747. // No, so wrap around.
  748. m_HasWrapped = true;
  749. // Make sure head isn't trying to advance into gap we may be leaving at the end of the
  750. // buffer by wiping the space if it could fit an event.
  751. if (spaceLeft >= InputEvent.kBaseEventSize)
  752. UnsafeUtility.MemClear(m_EventBufferTail, InputEvent.kBaseEventSize);
  753. m_EventBufferTail = m_EventBuffer;
  754. newTail = m_EventBuffer + bytesNeeded;
  755. // If the tail overtook both the head and the end of the buffer,
  756. // we need to make sure the head is wrapped around as well.
  757. if (newTailOvertakesHead)
  758. m_EventBufferHead = m_EventBuffer;
  759. // Recheck whether we're overtaking head.
  760. newTailOvertakesHead = newTail > m_EventBufferHead;
  761. }
  762. }
  763. // If the new tail runs into head, bump head as many times as we need to
  764. // make room for the event. Head may itself wrap around here.
  765. if (newTailOvertakesHead)
  766. {
  767. var newHead = m_EventBufferHead;
  768. var endOfBufferMinusOneEvent =
  769. m_EventBuffer + m_EventBufferSize - InputEvent.kBaseEventSize;
  770. while (newHead < newTail)
  771. {
  772. var numBytes = ((InputEvent*)newHead)->sizeInBytes;
  773. newHead += numBytes;
  774. --m_EventCount;
  775. m_EventSizeInBytes -= numBytes;
  776. if (newHead > endOfBufferMinusOneEvent || ((InputEvent*)newHead)->sizeInBytes == 0)
  777. {
  778. newHead = m_EventBuffer;
  779. break;
  780. }
  781. }
  782. m_EventBufferHead = newHead;
  783. }
  784. var buffer = m_EventBufferTail;
  785. m_EventBufferTail = newTail;
  786. // Copy data to buffer.
  787. UnsafeUtility.MemCpy(buffer, inputEvent.data, inputEvent.sizeInBytes);
  788. ++m_ChangeCounter;
  789. ++m_EventCount;
  790. m_EventSizeInBytes += bytesNeeded;
  791. // Make sure we have a record for the device.
  792. if (device != null)
  793. {
  794. var haveRecord = false;
  795. if (m_DeviceInfos != null)
  796. for (var i = 0; i < m_DeviceInfos.Length; ++i)
  797. if (m_DeviceInfos[i].deviceId == device.deviceId)
  798. {
  799. haveRecord = true;
  800. break;
  801. }
  802. if (!haveRecord)
  803. ArrayHelpers.Append(ref m_DeviceInfos, new DeviceInfo
  804. {
  805. m_DeviceId = device.deviceId,
  806. m_Layout = device.layout,
  807. m_StateFormat = device.stateBlock.format,
  808. m_StateSizeInBytes = (int)device.stateBlock.alignedSizeInBytes,
  809. // If it's a generated layout, store the full layout JSON in the device info. We do this so that
  810. // when saving traces for this kind of input, we can recreate the device.
  811. m_FullLayoutJson = InputControlLayout.s_Layouts.IsGeneratedLayout(device.m_Layout)
  812. ? InputSystem.LoadLayout(device.layout).ToJson()
  813. : null
  814. });
  815. }
  816. // Notify listeners.
  817. if (m_EventListeners.length > 0)
  818. DelegateHelpers.InvokeCallbacksSafe(ref m_EventListeners, new InputEventPtr((InputEvent*)buffer),
  819. "InputEventTrace.onEvent");
  820. Profiler.EndSample();
  821. }
  822. private class Enumerator : IEnumerator<InputEventPtr>
  823. {
  824. private InputEventTrace m_Trace;
  825. private int m_ChangeCounter;
  826. internal InputEventPtr m_Current;
  827. public Enumerator(InputEventTrace trace)
  828. {
  829. m_Trace = trace;
  830. m_ChangeCounter = trace.m_ChangeCounter;
  831. }
  832. public void Dispose()
  833. {
  834. m_Trace = null;
  835. m_Current = new InputEventPtr();
  836. }
  837. public bool MoveNext()
  838. {
  839. if (m_Trace == null)
  840. throw new ObjectDisposedException(ToString());
  841. if (m_Trace.m_ChangeCounter != m_ChangeCounter)
  842. throw new InvalidOperationException("Trace has been modified while enumerating!");
  843. return m_Trace.GetNextEvent(ref m_Current);
  844. }
  845. public void Reset()
  846. {
  847. m_Current = default;
  848. m_ChangeCounter = m_Trace.m_ChangeCounter;
  849. }
  850. public InputEventPtr Current => m_Current;
  851. object IEnumerator.Current => Current;
  852. }
  853. private static FourCC kFileFormat => new FourCC('I', 'E', 'V', 'T');
  854. private static int kFileVersion = 1;
  855. [Flags]
  856. private enum FileFlags
  857. {
  858. FixedUpdate = 1 << 0, // Events were recorded with system being in fixed-update mode.
  859. }
  860. /// <summary>
  861. /// Controls replaying of events recorded in an <see cref="InputEventTrace"/>.
  862. /// </summary>
  863. /// <remarks>
  864. /// Playback can be controlled either on a per-event or a per-frame basis. Note that playing back events
  865. /// frame by frame requires frame markers to be present in the trace (see <see cref="recordFrameMarkers"/>).
  866. ///
  867. /// By default, events will be queued as is except for their timestamps which will be set to the current
  868. /// time that each event is queued at.
  869. ///
  870. /// What this means is that events replay with the same device ID (see <see cref="InputEvent.deviceId"/>)
  871. /// they were captured on. If the trace is replayed in the same session that it was recorded in, this means
  872. /// that the events will replay on the same device (if it still exists).
  873. ///
  874. /// To map recorded events to a different device, you can either call <see cref="WithDeviceMappedFromTo(int,int)"/> to
  875. /// map an arbitrary device ID to a new one or call <see cref="WithAllDevicesMappedToNewInstances"/> to create
  876. /// new (temporary) devices for the duration of playback.
  877. ///
  878. /// <example>
  879. /// <code>
  880. /// var trace = new InputEventTrace(myDevice);
  881. /// trace.Enable();
  882. ///
  883. /// // ... run one or more frames ...
  884. ///
  885. /// trace.Replay().OneFrame();
  886. /// </code>
  887. /// </example>
  888. /// </remarks>
  889. /// <seealso cref="InputEventTrace.Replay"/>
  890. public class ReplayController : IDisposable
  891. {
  892. /// <summary>
  893. /// The event trace associated with the replay controller.
  894. /// </summary>
  895. /// <value>Trace from which events are replayed.</value>
  896. public InputEventTrace trace => m_EventTrace;
  897. /// <summary>
  898. /// Whether replay has finished.
  899. /// </summary>
  900. /// <value>True if replay has finished or is not in progress.</value>
  901. /// <seealso cref="PlayAllFramesOneByOne"/>
  902. /// <seealso cref="PlayAllEvents"/>
  903. public bool finished { get; private set; }
  904. /// <summary>
  905. /// Whether replay is paused.
  906. /// </summary>
  907. /// <value>True if replay is currently paused.</value>
  908. public bool paused { get; set; }
  909. /// <summary>
  910. /// Current position in the event stream.
  911. /// </summary>
  912. /// <value>Index of current event in trace.</value>
  913. public int position { get; private set; }
  914. /// <summary>
  915. /// List of devices created by the replay controller.
  916. /// </summary>
  917. /// <value>Devices created by the replay controller.</value>
  918. /// <remarks>
  919. /// By default, a replay controller will queue events as is, i.e. with <see cref="InputEvent.deviceId"/> of
  920. /// each event left as is. This means that the events will target existing devices (if any) that have the
  921. /// respective ID.
  922. ///
  923. /// Using <see cref="WithAllDevicesMappedToNewInstances"/>, a replay controller can be instructed to create
  924. /// new, temporary devices instead for each unique <see cref="InputEvent.deviceId"/> encountered in the stream.
  925. /// All devices created by the controller this way will be put on this list.
  926. /// </remarks>
  927. /// <seealso cref="WithAllDevicesMappedToNewInstances"/>
  928. public IEnumerable<InputDevice> createdDevices => m_CreatedDevices;
  929. private InputEventTrace m_EventTrace;
  930. private Enumerator m_Enumerator;
  931. private InlinedArray<KeyValuePair<int, int>> m_DeviceIDMappings;
  932. private bool m_CreateNewDevices;
  933. private InlinedArray<InputDevice> m_CreatedDevices;
  934. private Action m_OnFinished;
  935. private Action<InputEventPtr> m_OnEvent;
  936. private double m_StartTimeAsPerFirstEvent;
  937. private double m_StartTimeAsPerRuntime;
  938. private int m_AllEventsByTimeIndex = 0;
  939. private List<InputEventPtr> m_AllEventsByTime;
  940. internal ReplayController(InputEventTrace trace)
  941. {
  942. if (trace == null)
  943. throw new ArgumentNullException(nameof(trace));
  944. m_EventTrace = trace;
  945. }
  946. /// <summary>
  947. /// Removes devices created by the controller when using <see cref="WithAllDevicesMappedToNewInstances"/>.
  948. /// </summary>
  949. public void Dispose()
  950. {
  951. InputSystem.onBeforeUpdate -= OnBeginFrame;
  952. finished = true;
  953. foreach (var device in m_CreatedDevices)
  954. InputSystem.RemoveDevice(device);
  955. m_CreatedDevices = default;
  956. }
  957. /// <summary>
  958. /// Replay events recorded from <paramref name="recordedDevice"/> on device <paramref name="playbackDevice"/>.
  959. /// </summary>
  960. /// <param name="recordedDevice">Device events have been recorded from.</param>
  961. /// <param name="playbackDevice">Device events should be played back on.</param>
  962. /// <returns>The same ReplayController instance.</returns>
  963. /// <exception cref="ArgumentNullException"><paramref name="recordedDevice"/> is <c>null</c> -or-
  964. /// <paramref name="playbackDevice"/> is <c>null</c>.</exception>
  965. /// <remarks>
  966. /// This method causes all events with a device ID (see <see cref="InputDevice.deviceId"/> and <see cref="InputEvent.deviceId"/>)
  967. /// corresponding to the one of <paramref cref="recordedDevice"/> to be queued with the device ID of <paramref name="playbackDevice"/>.
  968. /// </remarks>
  969. public ReplayController WithDeviceMappedFromTo(InputDevice recordedDevice, InputDevice playbackDevice)
  970. {
  971. if (recordedDevice == null)
  972. throw new ArgumentNullException(nameof(recordedDevice));
  973. if (playbackDevice == null)
  974. throw new ArgumentNullException(nameof(playbackDevice));
  975. WithDeviceMappedFromTo(recordedDevice.deviceId, playbackDevice.deviceId);
  976. return this;
  977. }
  978. /// <summary>
  979. /// Replace <see cref="InputEvent.deviceId"/> values of events that are equal to <paramref name="recordedDeviceId"/>
  980. /// with device ID <paramref name="playbackDeviceId"/>.
  981. /// </summary>
  982. /// <param name="recordedDeviceId"><see cref="InputDevice.deviceId"/> to map from.</param>
  983. /// <param name="playbackDeviceId"><see cref="InputDevice.deviceId"/> to map to.</param>
  984. /// <returns>The same ReplayController instance.</returns>
  985. public ReplayController WithDeviceMappedFromTo(int recordedDeviceId, int playbackDeviceId)
  986. {
  987. // If there's an existing mapping entry for the device, update it.
  988. for (var i = 0; i < m_DeviceIDMappings.length; ++i)
  989. {
  990. if (m_DeviceIDMappings[i].Key != recordedDeviceId)
  991. continue;
  992. if (recordedDeviceId == playbackDeviceId) // Device mapped back to itself.
  993. m_DeviceIDMappings.RemoveAtWithCapacity(i);
  994. else
  995. m_DeviceIDMappings[i] = new KeyValuePair<int, int>(recordedDeviceId, playbackDeviceId);
  996. return this;
  997. }
  998. // Ignore if mapped to itself.
  999. if (recordedDeviceId == playbackDeviceId)
  1000. return this;
  1001. // Record mapping.
  1002. m_DeviceIDMappings.AppendWithCapacity(new KeyValuePair<int, int>(recordedDeviceId, playbackDeviceId));
  1003. return this;
  1004. }
  1005. /// <summary>
  1006. /// For all events, create new devices to replay the events on instead of replaying the events on existing devices.
  1007. /// </summary>
  1008. /// <returns>The same ReplayController instance.</returns>
  1009. /// <remarks>
  1010. /// Note that devices created by the <c>ReplayController</c> will stick around for as long as the replay
  1011. /// controller is not disposed of. This means that multiple successive replays using the same <c>ReplayController</c>
  1012. /// will replay the events on the same devices that were created on the first replay. It also means that in order
  1013. /// to do away with the created devices, it is necessary to call <see cref="Dispose"/>.
  1014. /// </remarks>
  1015. /// <seealso cref="Dispose"/>
  1016. /// <seealso cref="createdDevices"/>
  1017. public ReplayController WithAllDevicesMappedToNewInstances()
  1018. {
  1019. m_CreateNewDevices = true;
  1020. return this;
  1021. }
  1022. /// <summary>
  1023. /// Invoke the given callback when playback finishes.
  1024. /// </summary>
  1025. /// <param name="action">A callback to invoke when playback finishes.</param>
  1026. /// <returns>The same ReplayController instance.</returns>
  1027. public ReplayController OnFinished(Action action)
  1028. {
  1029. m_OnFinished = action;
  1030. return this;
  1031. }
  1032. /// <summary>
  1033. /// Invoke the given callback when an event is about to be queued.
  1034. /// </summary>
  1035. /// <param name="action">A callback to invoke when an event is getting queued.</param>
  1036. /// <returns>The same ReplayController instance.</returns>
  1037. public ReplayController OnEvent(Action<InputEventPtr> action)
  1038. {
  1039. m_OnEvent = action;
  1040. return this;
  1041. }
  1042. /// <summary>
  1043. /// Takes the next event from the trace and queues it.
  1044. /// </summary>
  1045. /// <returns>The same ReplayController instance.</returns>
  1046. /// <exception cref="InvalidOperationException">There are no more events in the <see cref="trace"/> -or- the only
  1047. /// events left are frame marker events (see <see cref="InputEventTrace.FrameMarkerEvent"/>).</exception>
  1048. /// <remarks>
  1049. /// This method takes the next event at the current read position and queues it using <see cref="InputSystem.QueueEvent"/>.
  1050. /// The read position is advanced past the taken event.
  1051. ///
  1052. /// Frame marker events (see <see cref="InputEventTrace.FrameMarkerEvent"/>) are skipped.
  1053. /// </remarks>
  1054. public ReplayController PlayOneEvent()
  1055. {
  1056. // Skip events until we hit something that isn't a frame marker.
  1057. if (!MoveNext(true, out var eventPtr))
  1058. throw new InvalidOperationException("No more events");
  1059. QueueEvent(eventPtr);
  1060. return this;
  1061. }
  1062. ////TODO: OneFrame
  1063. ////TODO: RewindOneEvent
  1064. ////TODO: RewindOneFrame
  1065. ////TODO: Stop
  1066. /// <summary>
  1067. /// Rewind playback all the way to the beginning of the event trace.
  1068. /// </summary>
  1069. /// <returns>The same ReplayController instance.</returns>
  1070. public ReplayController Rewind()
  1071. {
  1072. m_Enumerator = default;
  1073. m_AllEventsByTime = null;
  1074. m_AllEventsByTimeIndex = -1;
  1075. position = 0;
  1076. return this;
  1077. }
  1078. /// <summary>
  1079. /// Replay all frames one by one from the current playback position.
  1080. /// </summary>
  1081. /// <returns>The same ReplayController instance.</returns>
  1082. /// <remarks>
  1083. /// Events will be fed to the input system from within <see cref="InputSystem.onBeforeUpdate"/>. Each update
  1084. /// will receive events for one frame.
  1085. ///
  1086. /// Note that for this method to correctly space out events and distribute them to frames, frame markers
  1087. /// must be present in the trace (see <see cref="recordFrameMarkers"/>). If not present, all events will
  1088. /// be fed into first frame.
  1089. /// </remarks>
  1090. /// <seealso cref="recordFrameMarkers"/>
  1091. /// <seealso cref="InputSystem.onBeforeUpdate"/>
  1092. /// <seealso cref="PlayAllEvents"/>
  1093. /// <seealso cref="PlayAllEventsAccordingToTimestamps"/>
  1094. public ReplayController PlayAllFramesOneByOne()
  1095. {
  1096. finished = false;
  1097. InputSystem.onBeforeUpdate += OnBeginFrame;
  1098. return this;
  1099. }
  1100. /// <summary>
  1101. /// Go through all remaining event in the trace starting at the current read position and queue them using
  1102. /// <see cref="InputSystem.QueueEvent"/>.
  1103. /// </summary>
  1104. /// <returns>The same ReplayController instance.</returns>
  1105. /// <remarks>
  1106. /// Unlike methods such as <see cref="PlayAllFramesOneByOne"/>, this method immediately queues events and immediately
  1107. /// completes playback upon return from the method.
  1108. /// </remarks>
  1109. /// <seealso cref="PlayAllFramesOneByOne"/>
  1110. /// <seealso cref="PlayAllEventsAccordingToTimestamps"/>
  1111. public ReplayController PlayAllEvents()
  1112. {
  1113. finished = false;
  1114. try
  1115. {
  1116. while (MoveNext(true, out var eventPtr))
  1117. QueueEvent(eventPtr);
  1118. }
  1119. finally
  1120. {
  1121. Finished();
  1122. }
  1123. return this;
  1124. }
  1125. /// <summary>
  1126. /// Replay events in a way that tries to preserve the original timing sequence.
  1127. /// </summary>
  1128. /// <returns>The same ReplayController instance.</returns>
  1129. /// <remarks>
  1130. /// This method will take the current time as the starting time to which make all events
  1131. /// relative to. Based on this time, it will try to correlate the original event timing
  1132. /// with the timing of input updates as they happen. When successful, this will compensate
  1133. /// for differences in frame timings compared to when input was recorded and instead queue
  1134. /// input in frames that are closer to the original timing.
  1135. ///
  1136. /// Note that this method will perform one initial scan of the trace to determine a linear
  1137. /// ordering of the events by time (the input system does not require any such ordering on the
  1138. /// events in its queue and thus events in a trace, especially if there are multiple devices
  1139. /// involved, may be out of order).
  1140. /// </remarks>
  1141. /// <seealso cref="PlayAllFramesOneByOne"/>
  1142. /// <seealso cref="PlayAllEvents"/>
  1143. public ReplayController PlayAllEventsAccordingToTimestamps()
  1144. {
  1145. // Sort remaining events by time.
  1146. var eventsByTime = new List<InputEventPtr>();
  1147. while (MoveNext(true, out var eventPtr))
  1148. eventsByTime.Add(eventPtr);
  1149. eventsByTime.Sort((a, b) => a.time.CompareTo(b.time));
  1150. m_Enumerator.Dispose();
  1151. m_Enumerator = null;
  1152. m_AllEventsByTime = eventsByTime;
  1153. position = 0;
  1154. // Start playback.
  1155. finished = false;
  1156. m_StartTimeAsPerFirstEvent = -1;
  1157. m_AllEventsByTimeIndex = -1;
  1158. InputSystem.onBeforeUpdate += OnBeginFrame;
  1159. return this;
  1160. }
  1161. private void OnBeginFrame()
  1162. {
  1163. if (paused)
  1164. return;
  1165. if (!MoveNext(false, out var currentEventPtr))
  1166. {
  1167. if (m_AllEventsByTime == null || m_AllEventsByTimeIndex >= m_AllEventsByTime.Count)
  1168. Finished();
  1169. return;
  1170. }
  1171. // Check for empty frame (note: when playing back events by time, we won't see frame marker events
  1172. // returned from MoveNext).
  1173. if (currentEventPtr.type == FrameMarkerEvent)
  1174. {
  1175. if (!MoveNext(false, out var nextEvent))
  1176. {
  1177. // Last frame.
  1178. Finished();
  1179. return;
  1180. }
  1181. // Check for empty frame.
  1182. if (nextEvent.type == FrameMarkerEvent)
  1183. {
  1184. --position;
  1185. m_Enumerator.m_Current = currentEventPtr;
  1186. return;
  1187. }
  1188. currentEventPtr = nextEvent;
  1189. }
  1190. // Inject our events into the frame.
  1191. while (true)
  1192. {
  1193. QueueEvent(currentEventPtr);
  1194. // Stop if we reach the end of the stream.
  1195. if (!MoveNext(false, out var nextEvent))
  1196. {
  1197. if (m_AllEventsByTime == null || m_AllEventsByTimeIndex >= m_AllEventsByTime.Count)
  1198. Finished();
  1199. break;
  1200. }
  1201. // Stop if we've reached the next frame (won't happen if we're playing events by time).
  1202. if (nextEvent.type == FrameMarkerEvent)
  1203. {
  1204. // Back up one event.
  1205. m_Enumerator.m_Current = currentEventPtr;
  1206. --position;
  1207. break;
  1208. }
  1209. currentEventPtr = nextEvent;
  1210. }
  1211. }
  1212. private void Finished()
  1213. {
  1214. finished = true;
  1215. InputSystem.onBeforeUpdate -= OnBeginFrame;
  1216. m_OnFinished?.Invoke();
  1217. }
  1218. private void QueueEvent(InputEventPtr eventPtr)
  1219. {
  1220. // Shift time on event.
  1221. var originalTimestamp = eventPtr.internalTime;
  1222. if (m_AllEventsByTime != null)
  1223. eventPtr.internalTime = m_StartTimeAsPerRuntime + (eventPtr.internalTime - m_StartTimeAsPerFirstEvent);
  1224. else
  1225. eventPtr.internalTime = InputRuntime.s_Instance.currentTime;
  1226. // Remember original event ID. QueueEvent will automatically update the event ID
  1227. // and actually do so in place.
  1228. var originalEventId = eventPtr.id;
  1229. // Map device ID.
  1230. var originalDeviceId = eventPtr.deviceId;
  1231. eventPtr.deviceId = ApplyDeviceMapping(originalDeviceId);
  1232. // Notify.
  1233. m_OnEvent?.Invoke(eventPtr);
  1234. // Queue event.
  1235. try
  1236. {
  1237. InputSystem.QueueEvent(eventPtr);
  1238. }
  1239. finally
  1240. {
  1241. // Restore modification we made to the event buffer.
  1242. eventPtr.internalTime = originalTimestamp;
  1243. eventPtr.id = originalEventId;
  1244. eventPtr.deviceId = originalDeviceId;
  1245. }
  1246. }
  1247. private bool MoveNext(bool skipFrameEvents, out InputEventPtr eventPtr)
  1248. {
  1249. eventPtr = default;
  1250. if (m_AllEventsByTime != null)
  1251. {
  1252. if (m_AllEventsByTimeIndex + 1 >= m_AllEventsByTime.Count)
  1253. {
  1254. position = m_AllEventsByTime.Count;
  1255. m_AllEventsByTimeIndex = m_AllEventsByTime.Count;
  1256. return false;
  1257. }
  1258. if (m_AllEventsByTimeIndex < 0)
  1259. {
  1260. m_StartTimeAsPerFirstEvent = m_AllEventsByTime[0].internalTime;
  1261. m_StartTimeAsPerRuntime = InputRuntime.s_Instance.currentTime;
  1262. }
  1263. else if (m_AllEventsByTimeIndex < m_AllEventsByTime.Count - 1 &&
  1264. m_AllEventsByTime[m_AllEventsByTimeIndex + 1].internalTime > m_StartTimeAsPerFirstEvent + (InputRuntime.s_Instance.currentTime - m_StartTimeAsPerRuntime))
  1265. {
  1266. // We're queuing by original time and the next event isn't up yet,
  1267. // so early out.
  1268. return false;
  1269. }
  1270. ++m_AllEventsByTimeIndex;
  1271. ++position;
  1272. eventPtr = m_AllEventsByTime[m_AllEventsByTimeIndex];
  1273. }
  1274. else
  1275. {
  1276. if (m_Enumerator == null)
  1277. m_Enumerator = new Enumerator(m_EventTrace);
  1278. do
  1279. {
  1280. if (!m_Enumerator.MoveNext())
  1281. return false;
  1282. ++position;
  1283. eventPtr = m_Enumerator.Current;
  1284. }
  1285. while (skipFrameEvents && eventPtr.type == FrameMarkerEvent);
  1286. }
  1287. return true;
  1288. }
  1289. private int ApplyDeviceMapping(int originalDeviceId)
  1290. {
  1291. // Look up in mappings.
  1292. for (var i = 0; i < m_DeviceIDMappings.length; ++i)
  1293. {
  1294. var entry = m_DeviceIDMappings[i];
  1295. if (entry.Key == originalDeviceId)
  1296. return entry.Value;
  1297. }
  1298. // Create device, if needed.
  1299. if (m_CreateNewDevices)
  1300. {
  1301. try
  1302. {
  1303. // Find device info.
  1304. var deviceIndex = m_EventTrace.deviceInfos.IndexOf(x => x.deviceId == originalDeviceId);
  1305. if (deviceIndex != -1)
  1306. {
  1307. var deviceInfo = m_EventTrace.deviceInfos[deviceIndex];
  1308. // If we don't have the layout, try to add it from the persisted layout info.
  1309. var layoutName = new InternedString(deviceInfo.layout);
  1310. if (!InputControlLayout.s_Layouts.HasLayout(layoutName))
  1311. {
  1312. if (string.IsNullOrEmpty(deviceInfo.m_FullLayoutJson))
  1313. return originalDeviceId;
  1314. InputSystem.RegisterLayout(deviceInfo.m_FullLayoutJson);
  1315. }
  1316. // Create device.
  1317. var device = InputSystem.AddDevice(layoutName);
  1318. WithDeviceMappedFromTo(originalDeviceId, device.deviceId);
  1319. m_CreatedDevices.AppendWithCapacity(device);
  1320. return device.deviceId;
  1321. }
  1322. }
  1323. catch
  1324. {
  1325. // Swallow and just return originalDeviceId.
  1326. }
  1327. }
  1328. return originalDeviceId;
  1329. }
  1330. }
  1331. /// <summary>
  1332. /// Information about a device whose input has been captured in an <see cref="InputEventTrace"/>
  1333. /// </summary>
  1334. /// <seealso cref="InputEventTrace.deviceInfos"/>
  1335. [Serializable]
  1336. public struct DeviceInfo
  1337. {
  1338. /// <summary>
  1339. /// Id of the device as stored in the events for the device.
  1340. /// </summary>
  1341. /// <seealso cref="InputDevice.deviceId"/>
  1342. public int deviceId
  1343. {
  1344. get => m_DeviceId;
  1345. set => m_DeviceId = value;
  1346. }
  1347. /// <summary>
  1348. /// Name of the layout used by the device.
  1349. /// </summary>
  1350. /// <seealso cref="InputControl.layout"/>
  1351. public string layout
  1352. {
  1353. get => m_Layout;
  1354. set => m_Layout = value;
  1355. }
  1356. /// <summary>
  1357. /// Tag for the format in which state for the device is stored.
  1358. /// </summary>
  1359. /// <seealso cref="InputControl.stateBlock"/>
  1360. /// <seealso cref="InputStateBlock.format"/>
  1361. public FourCC stateFormat
  1362. {
  1363. get => m_StateFormat;
  1364. set => m_StateFormat = value;
  1365. }
  1366. /// <summary>
  1367. /// Size of a full state snapshot of the device.
  1368. /// </summary>
  1369. public int stateSizeInBytes
  1370. {
  1371. get => m_StateSizeInBytes;
  1372. set => m_StateSizeInBytes = value;
  1373. }
  1374. [SerializeField] internal int m_DeviceId;
  1375. [SerializeField] internal string m_Layout;
  1376. [SerializeField] internal FourCC m_StateFormat;
  1377. [SerializeField] internal int m_StateSizeInBytes;
  1378. [SerializeField] internal string m_FullLayoutJson;
  1379. }
  1380. }
  1381. }