using Iisu;
using System;
using System.Threading;
namespace BBIWARG.Input.InputProviding
{
///
/// signature for the event that the movie has restarted
///
/// event sender
/// event arguments
public delegate void MovieRestartedEventHandler(object sender, EventArgs e);
///
/// VideoInputProvider provides the raw depth and confidence data read from a video file.
///
internal class VideoInputProvider : InputProvider
{
///
/// parameter handle for the current frame of the movie
///
private IParameterHandle currentMovieFrame;
///
/// parameter handle for the number of frames in the movie
///
private IParameterHandle frameCount;
///
/// parameter handle for the play step, which is the value that gets added to the current frame in each iteration
///
private IParameterHandle playStep;
///
/// the id of the current frame
///
public override int CurrentFrameID { get { return currentMovieFrame.Value; } }
///
/// true iff the movie is paused
///
public bool IsPaused { get; private set; }
///
/// the path to the movie file
///
public String MoviePath { get; private set; }
///
/// event that the movie has restarted
///
public event MovieRestartedEventHandler MovieRestartedEvent;
///
/// Constructs a VideoInputProvider.
///
/// path to the movie file
public VideoInputProvider(String moviePath)
{
MoviePath = moviePath;
}
///
/// Jumps to the next movie frame.
///
public void goToNextFrame()
{
nextFrame();
}
///
/// Jumps to the previous movie frame.
///
public void goToPreviousFrame()
{
playStep.Value = -1;
nextFrame();
playStep.Value = 1;
}
///
/// Pauses the movie. No data is read anymore.
///
public void pause()
{
IsPaused = true;
}
///
/// Resumes playing the movie and reading data.
///
public void play()
{
IsPaused = false;
}
///
/// Returns an iisu device configuration, which uses the movie to read the data.
///
/// iisu device configuration
protected override IDeviceConfiguration createDeviceConfiguration()
{
IDeviceConfiguration conf = base.createDeviceConfiguration();
conf.MoviePath = MoviePath;
return conf;
}
///
/// Gets the next frame from the device and triggers an event if the movie has restarted.
///
protected override void nextFrame()
{
base.nextFrame();
if (CurrentFrameID == 0 && MovieRestartedEvent != null)
MovieRestartedEvent(this, new EventArgs());
}
///
/// Registers all parameter and data handles.
///
protected override void registerHandles()
{
base.registerHandles();
device.RegisterParameterHandle("SOURCE.MOVIE.PlayMode").Value = 1; // 0=once, 1=loop, 2=pingPong
currentMovieFrame = device.RegisterParameterHandle("SOURCE.MOVIE.CurrentFrame");
frameCount = device.RegisterParameterHandle("SOURCE.MOVIE.FrameCount");
playStep = device.RegisterParameterHandle("SOURCE.MOVIE.PlayStep");
}
///
/// Provides the main loop for reading data from the video file.
///
protected override void run()
{
while (IsActive)
{
if (!IsPaused)
nextFrame();
else
Thread.Sleep((int)(1000 / frameRate.Value));
}
}
}
}