VideoInputProvider.cs 4.5 KB

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