Skip to content

Commit

Permalink
Merge pull request #32 from MerlinCooper/Worker_Threads_to_control_Re…
Browse files Browse the repository at this point in the history
…cordingSoftware_and_iRacingReplay

Worker threads to control recording software and i racing replay
  • Loading branch information
MerlinCooper authored Jun 6, 2020
2 parents f50c5b5 + cde6db0 commit e6ca230
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions ClassDiagram3.cd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram />
12 changes: 9 additions & 3 deletions Phases/CaptureOpeningScenes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ namespace iRacingReplayOverlay.Phases
{
public partial class IRacingReplay
{
static private VideoCapture raceVideo = new VideoCapture();

void _CaptureOpeningScenes(Action onComplete)
{
if (bRecordUsingPauseResume)
{

}
var data = iRacing.GetDataFeed().First();
var session = data.SessionData.SessionInfo.Sessions.Qualifying();
if (session == null || session.ResultsPositions == null)
Expand Down Expand Up @@ -57,13 +63,13 @@ void _CaptureOpeningScenes(Action onComplete)
var aCar = data.SessionData.DriverInfo.CompetingDrivers[1].CarNumberRaw;
iRacing.Replay.CameraOnDriver((short)aCar, (short)scenicCameras);

var videoCapture = new VideoCapture();
//var videoCapture = new VideoCapture();

videoCapture.Activate(workingFolder);
raceVideo.Activate(workingFolder);

Thread.Sleep(shortTestOnly ? 5000 : 20000);

var fileNames = videoCapture.Deactivate();
var fileNames = raceVideo.Deactivate(bRecordUsingPauseResume);
if( fileNames.Count == 0)
return;

Expand Down
34 changes: 22 additions & 12 deletions Phases/CaptureRace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public partial class IRacingReplay
string workingFolder;
string introVideo;


void _WithWorkingFolder(string workingFolder)
{
this.workingFolder = workingFolder;
Expand All @@ -63,30 +64,39 @@ internal void _CaptureRaceTest(Action<string> onComplete, IEnumerable<DataSample
//Retrieve list of raceEvents selected depending on the duration of the highlight video
var totalRaceEvents = RaceEventExtension.GetInterestingRaceEvents(overlayData.RaceEvents.ToList());

//set iRacing Replay to Race start and ensure that iRacing is playing out replay at normal speed
ApplyFirstLapCameraDirection(samples, replayControl);

/*//set iRacing Replay to Race start and ensure that iRacing is playing out replay at normal speed
iRacing.Replay.MoveToFrame(raceStartFrameNumber); //move in iRacing replay to start of Race
iRacing.Replay.SetSpeed((int)replaySpeeds.normal); //set replay speed to normal
iRacing.Replay.SetSpeed((int)replaySpeeds.normal); //set replay speed to normal*/

//Record the selected race events into a highlight video
var highligtVideoCapture = new VideoCapture(); //create instance to control by sending hot-keys to recording software (tested with OBS)
highligtVideoCapture.Activate(workingFolder); //Start video capturing FileName will be given by recording software.
//var highligtVideoCapture = new VideoCapture(); //create instance to control by sending hot-keys to recording software (tested with OBS)
raceVideo.Activate(workingFolder); //Active video-capturing and send start command to recording software.

//cycle through all raceEvents selected for the highlight video and record them (REMARK: Camera switching not implemented yet)
foreach (var raceEvent in totalRaceEvents)
{
TraceInfo.WriteLine("ADV_RECORDING: Type: {0} | Durations-Span: {1} | ".F(raceEvent.GetType(), raceEvent.ToString()));
//pause recording software before jumping to new position in iRacing Replay
highligtVideoCapture.Pause();
TraceInfo.WriteLine("ADV_RECORDING: Type: {0} | Durations-Span: {1} | ".F(raceEvent.GetType(), raceEvent.Duration));

//jump to selected RaceEvent in iRacing Replay
int framePositionInRace = raceStartFrameNumber + (int)Math.Round(raceEvent.StartTime * 60.0);
iRacing.Replay.MoveToFrame(raceStartFrameNumber + (int)Math.Round(raceEvent.StartTime * 60.0));


highligtVideoCapture.Resume(); //resume recording

Thread.Sleep((int)(1000*raceEvent.Duration)); //pause thread until scene is fully recorded.
iRacing.Replay.SetSpeed((int)replaySpeeds.normal); //start iRacing Replay at selected position

raceVideo.Resume(); //resume recording

TraceDebug.WriteLine("Recording Race-Event. Frame-Position: {0} | Duration: {1} ms".F(framePositionInRace, 1000 * raceEvent.Duration));

Thread.Sleep((int)(1000 * raceEvent.Duration)); //pause thread until scene is fully recorded.

raceVideo.Pause(); //pause recording software before jumping to new position in iRacing Replay
}

TraceDebug.WriteLine("Video Capture of Race-Events completed");
raceVideo.Stop();

} else { //Code to be removed after being able to implment working solution where analysis phase and replay-capture phase are distinct processes.
//use local variables for original code instead of global variables introduced to support full analysis in analysis-phase

Expand Down
47 changes: 38 additions & 9 deletions Phases/Direction/VideoCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,37 @@ namespace iRacingReplayOverlay.Phases.Direction
{
enum videoStatus { stopped, running, paused };

public class VideoCapture
public class VideoCapture
{
string workingFolder;
DateTime started;
Timer timer;
List<CapturedVideoFile> captureFileNames = new List<CapturedVideoFile>();
videoStatus curVideoStatus = videoStatus.stopped;

~VideoCapture()
{
if(curVideoStatus != videoStatus.stopped)
SendKeyStroke_StartStopp();
}

public void Activate(string workingFolder)
public void Activate(string workingFolder, bool bStartRecording = true)
{
this.workingFolder = workingFolder;
this.started = DateTime.Now;

timer = new Timer(500);
timer.Elapsed += CaptureNewFileNames; ;
timer.Elapsed += CaptureNewFileNames;
timer.AutoReset = false;
timer.Enabled = true;
curVideoStatus = videoStatus.running;

SendKeyStroke_StartStopp(); //Send hot-key to start recording

if (bStartRecording && (curVideoStatus == videoStatus.stopped))
{
SendKeyStroke_StartStopp(); //Send hot-key to start recording
curVideoStatus = videoStatus.running;
}else if( curVideoStatus == videoStatus.paused){
Resume();
}
}

private void CaptureNewFileNames(object sender, ElapsedEventArgs e)
Expand Down Expand Up @@ -80,18 +91,26 @@ private void CaptureNewFileNames(object sender, ElapsedEventArgs e)
}
}

public List<CapturedVideoFile> Deactivate()
public List<CapturedVideoFile> Deactivate(bool bRecordUsingPauseResume=false)
{
if (timer != null)
{
var t = timer;
timer = null;
t.Stop();
t.Dispose();
curVideoStatus = videoStatus.stopped;

}

SendKeyStroke_StartStopp();
if (bRecordUsingPauseResume && curVideoStatus != videoStatus.paused)
{
Pause();
curVideoStatus = videoStatus.paused;
} else
{
SendKeyStroke_StartStopp();
curVideoStatus = videoStatus.stopped;
}

System.Threading.Thread.Sleep(2000);

Expand Down Expand Up @@ -122,6 +141,16 @@ public void Resume()
}
}

public void Stop()
{
if(curVideoStatus == videoStatus.running || curVideoStatus == videoStatus.paused)
{
SendKeyStroke_StartStopp();
curVideoStatus = videoStatus.stopped;
}
}


private static void SendKeyStroke_StartStopp()
{
TraceInfo.WriteLine("Sending key event to start/stopp recording ALT+F9");
Expand Down
1 change: 1 addition & 0 deletions iRacingReplayDirector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
<ItemGroup>
<None Include="App.config" />
<None Include="ClassDiagram2.cd" />
<None Include="ClassDiagram3.cd" />
<None Include="DeploymentSite\.gitignore" />
<None Include="DeploymentSite\main.jsx" />
<None Include="DeploymentSite\package.json" />
Expand Down

0 comments on commit e6ca230

Please sign in to comment.