InputEventTrace.cs 66 KB

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