Skip to content

Commit

Permalink
worker thread to switch iRacing camera while recording using pause/re…
Browse files Browse the repository at this point in the history
…sume implemented
  • Loading branch information
MerlinCooper committed Jun 11, 2020
1 parent 25837d9 commit bf75962
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
25 changes: 13 additions & 12 deletions Phases/CaptureRace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.ComponentModel;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -66,13 +67,16 @@ internal void _CaptureRaceTest(Action<string> onComplete, IEnumerable<DataSample

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*/


//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)
raceVideo.Activate(workingFolder); //Active video-capturing and send start command to recording software.
raceVideo.Activate(workingFolder); //Active video-capturing and send start command to recording software.

OverlayData.CamDriver curCamDriver = overlayData.CamDrivers.First();

//start thread to control / switch cameras while recording
ReplayControl.cameraControl.ReplayCameraControlTask(overlayData);

//ReplayControl.cameraControl.CameraOnDriver(short.Parse(curCamDriver.CurrentDriver.CarNumber), (short)curCamDriver.camGroupNumber);

//cycle through all raceEvents selected for the highlight video and record them (REMARK: Camera switching not implemented yet)
foreach (var raceEvent in totalRaceEvents)
Expand All @@ -83,22 +87,19 @@ internal void _CaptureRaceTest(Action<string> onComplete, IEnumerable<DataSample
int framePositionInRace = raceStartFrameNumber + (int)Math.Round(raceEvent.StartTime * 60.0);
iRacing.Replay.MoveToFrame(raceStartFrameNumber + (int)Math.Round(raceEvent.StartTime * 60.0));

//TODO: SET CAMERA TO DRIVER


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));
//TODO: START THREAD TO SWITCH / CONTROL CAMERA WHILE RECORDING SCENE

Thread.Sleep((int)(1000 * raceEvent.Duration)); //pause thread until scene is fully recorded.
//TODO: TERMINATE THREAD CAMERA CONTROL
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
42 changes: 40 additions & 2 deletions Phases/Direction/CameraControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

using iRacingSDK;
using iRacingSDK.Support;
using iRacingReplayOverlay.Phases.Capturing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;


namespace iRacingReplayOverlay.Phases.Direction
{
Expand All @@ -29,6 +33,8 @@ public class CameraControl
readonly TrackCamera[] cameras;
readonly Random random;
readonly TrackCamera defaultCamera;
OverlayData overlayData;


public short LastLapCameraNumber
{
Expand Down Expand Up @@ -98,16 +104,48 @@ public TrackCamera FindACamera(IEnumerable<CameraAngle> cameraAngles, TrackCamer
return camera;
}

//Start worker thread to switch cameras as stored in camDriversList during race analyis when FastRecording is used.
public async Task ReplayCameraControlTask(OverlayData overlayData)
{
this.overlayData = overlayData;
await Task.Run(() => CameraSwitcherWorkerTask());
}

//worker thread to switch cameras.
private void CameraSwitcherWorkerTask()
{
for(var iCamDriverIndex = 0; iCamDriverIndex < overlayData.CamDrivers.Count; iCamDriverIndex++)
{
//get object of current CamDriver
var curCamDriver = overlayData.CamDrivers[iCamDriverIndex];
//Set camera to current camDriver Object
ReplayControl.cameraControl.CameraOnDriver(short.Parse(curCamDriver.CurrentDriver.CarNumber), (short)curCamDriver.camGroupNumber);
//remember start-time of current camers
double startTimeCurCamera = curCamDriver.StartTime;
try
{
double startTimeNextCamera = overlayData.CamDrivers[iCamDriverIndex+1].StartTime;
int camDuration = (int)(1000 * (startTimeNextCamera - startTimeCurCamera));
TraceDebug.WriteLine("Camera switched to driver {0} using camera-group {1}. Remaining Time: {2}".F(curCamDriver.CurrentDriver.UserName, curCamDriver.camGroupNumber, camDuration));
Thread.Sleep(camDuration);
}
catch
{
TraceDebug.Write("No more entries in list of CamDriver -> no more camera switches");
}
}
}

public void CameraOnDriver(short carNumber, short group, short camera = 0)
{
iRacing.Replay.CameraOnDriver(carNumber, group, camera);
TraceDebug.WriteLine("CameraOnDriver called for carNumber: {0} cameraGroup: {1} camera: {2}".F(carNumber, group, camera));
//TraceDebug.WriteLine("CameraOnDriver called for carNumber: {0} cameraGroup: {1} camera: {2}".F(carNumber, group, camera));
}

public void CameraOnPositon(short carPosition, short group, short camera = 0)
{
iRacing.Replay.CameraOnPositon(carPosition, group, camera);
TraceDebug.WriteLine("CameraOnPositon called for carNumber: {0} cameraGroup: {1} camera: {2}".F(carPosition, group, camera));
//TraceDebug.WriteLine("CameraOnPositon called for carPosition: {0} cameraGroup: {1} camera: {2}".F(carPosition, group, camera));
}
}
}
4 changes: 3 additions & 1 deletion Phases/Direction/ReplayControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ReplayControl
readonly IDirectionRule[] directionRules;
readonly IVetoRule ruleRandom;
IDirectionRule currentRule;
public static CameraControl cameraControl;

public ReplayControl(SessionData sessionData, Incidents incidents, RemovalEdits removalEdits, TrackCameras trackCameras)
{
Expand All @@ -43,7 +44,8 @@ public ReplayControl(SessionData sessionData, Incidents incidents, RemovalEdits

var camera = cameras.First(tc => tc.IsRaceStart);

var cameraControl = new CameraControl(cameras);
//var cameraControl = new CameraControl(cameras);
cameraControl = new CameraControl(cameras);
cameraControl.CameraOnPositon(1, camera.CameraNumber);

var battleMarker = removalEdits.For(InterestState.Battle);
Expand Down

0 comments on commit bf75962

Please sign in to comment.