VideoInputProvider.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Iisu;
  2. using System;
  3. using System.Threading;
  4. namespace BBIWARG.Input.InputProviding
  5. {
  6. /// <summary>
  7. /// signature for the event that the movie has restarted
  8. /// </summary>
  9. /// <param name="sender">event sender</param>
  10. /// <param name="e">event arguments</param>
  11. public delegate void MovieRestartedEventHandler(object sender, EventArgs e);
  12. /// <summary>
  13. /// VideoInputProvider provides the raw depth and confidence data read from a video file.
  14. /// </summary>
  15. internal class VideoInputProvider : InputProviderIisu
  16. {
  17. /// <summary>
  18. /// parameter handle for the current frame of the movie
  19. /// </summary>
  20. private IParameterHandle<int> currentMovieFrame;
  21. /// <summary>
  22. /// parameter handle for the number of frames in the movie
  23. /// </summary>
  24. private IParameterHandle<int> frameCount;
  25. /// <summary>
  26. /// parameter handle for the play step, which is the value that gets added to the current frame in each iteration
  27. /// </summary>
  28. private IParameterHandle<int> playStep;
  29. /// <summary>
  30. /// the id of the current frame
  31. /// </summary>
  32. public override int CurrentFrameID { get { return currentMovieFrame.Value; } }
  33. /// <summary>
  34. /// true iff the movie is paused
  35. /// </summary>
  36. public bool IsPaused { get; private set; }
  37. /// <summary>
  38. /// the path to the movie file
  39. /// </summary>
  40. public String MoviePath { get; private set; }
  41. /// <summary>
  42. /// event that the movie has restarted
  43. /// </summary>
  44. public event MovieRestartedEventHandler MovieRestartedEvent;
  45. /// <summary>
  46. /// Constructs a VideoInputProvider.
  47. /// </summary>
  48. /// <param name="moviePath">path to the movie file</param>
  49. public VideoInputProvider(String moviePath)
  50. {
  51. MoviePath = moviePath;
  52. }
  53. /// <summary>
  54. /// Jumps to the next movie frame.
  55. /// </summary>
  56. public void goToNextFrame()
  57. {
  58. nextFrame();
  59. }
  60. /// <summary>
  61. /// Jumps to the previous movie frame.
  62. /// </summary>
  63. public void goToPreviousFrame()
  64. {
  65. playStep.Value = -1;
  66. nextFrame();
  67. playStep.Value = 1;
  68. }
  69. /// <summary>
  70. /// Pauses the movie. No data is read anymore.
  71. /// </summary>
  72. public void pause()
  73. {
  74. IsPaused = true;
  75. }
  76. /// <summary>
  77. /// Resumes playing the movie and reading data.
  78. /// </summary>
  79. public void play()
  80. {
  81. IsPaused = false;
  82. }
  83. /// <summary>
  84. /// Returns an iisu device configuration, which uses the movie to read the data.
  85. /// </summary>
  86. /// <returns>iisu device configuration</returns>
  87. protected override IDeviceConfiguration createDeviceConfiguration()
  88. {
  89. IDeviceConfiguration conf = base.createDeviceConfiguration();
  90. conf.MoviePath = MoviePath;
  91. return conf;
  92. }
  93. /// <summary>
  94. /// Gets the next frame from the device and triggers an event if the movie has restarted.
  95. /// </summary>
  96. protected override void nextFrame()
  97. {
  98. base.nextFrame();
  99. if (CurrentFrameID == 0 && MovieRestartedEvent != null)
  100. MovieRestartedEvent(this, new EventArgs());
  101. }
  102. /// <summary>
  103. /// Registers all parameter and data handles.
  104. /// </summary>
  105. protected override void registerHandles()
  106. {
  107. base.registerHandles();
  108. device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 1; // 0=once, 1=loop, 2=pingPong
  109. currentMovieFrame = device.RegisterParameterHandle<int>("SOURCE.MOVIE.CurrentFrame");
  110. frameCount = device.RegisterParameterHandle<int>("SOURCE.MOVIE.FrameCount");
  111. playStep = device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayStep");
  112. }
  113. /// <summary>
  114. /// Provides the main loop for reading data from the video file.
  115. /// </summary>
  116. protected override void run()
  117. {
  118. while (IsActive)
  119. {
  120. if (!IsPaused)
  121. nextFrame();
  122. else
  123. Thread.Sleep((int)(1000 / frameRate.Value));
  124. }
  125. }
  126. }
  127. }