Interim work for screen recording pause support

This commit is contained in:
Jaex 2022-09-03 16:52:42 +03:00
parent 1d599b02a6
commit 930d8a9e1b
7 changed files with 169 additions and 51 deletions

View file

@ -131,6 +131,21 @@ public static string ChangeFileNameExtension(string fileName, string extension)
return fileName;
}
public static string AppendTextToFileName(string filePath, string text)
{
if (!string.IsNullOrEmpty(filePath))
{
int pos = filePath.LastIndexOf('.');
if (pos >= 0)
{
return filePath.Substring(0, pos) + text + filePath.Substring(pos);
}
}
return filePath + text;
}
public static string AppendExtension(string filePath, string extension)
{
return filePath.TrimEnd('.') + '.' + extension.TrimStart('.');

View file

@ -291,7 +291,7 @@ public void ConcatenateVideos(string[] inputFiles, string outputFile, bool autoD
{
foreach (string inputFile in inputFiles)
{
if (!inputFile.Equals(outputFile, StringComparison.OrdinalIgnoreCase) && File.Exists(inputFile))
if (File.Exists(inputFile))
{
File.Delete(inputFile);
}

View file

@ -339,6 +339,6 @@ public enum BorderStyle // Localized
public enum ScreenRecordState
{
Waiting, BeforeStart, AfterStart, AfterRecordingStart, Encoding
Waiting, BeforeStart, AfterStart, AfterRecordingStart, RecordingEnd, Encoding
}
}

View file

@ -26,6 +26,7 @@ private void InitializeComponent()
this.tsmiStart = new System.Windows.Forms.ToolStripMenuItem();
this.tsmiAbort = new System.Windows.Forms.ToolStripMenuItem();
this.niTray = new System.Windows.Forms.NotifyIcon(this.components);
this.btnPause = new ShareX.HelpersLib.NoFocusBorderButton();
this.pInfo.SuspendLayout();
this.cmsMain.SuspendLayout();
this.SuspendLayout();
@ -54,6 +55,7 @@ private void InitializeComponent()
// pInfo
//
resources.ApplyResources(this.pInfo, "pInfo");
this.pInfo.Controls.Add(this.btnPause);
this.pInfo.Controls.Add(this.btnAbort);
this.pInfo.Controls.Add(this.btnStart);
this.pInfo.Controls.Add(this.lblTimer);
@ -87,6 +89,12 @@ private void InitializeComponent()
resources.ApplyResources(this.niTray, "niTray");
this.niTray.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btnStart_MouseClick);
//
// btnPause
//
resources.ApplyResources(this.btnPause, "btnPause");
this.btnPause.Name = "btnPause";
this.btnPause.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btnPause_MouseClick);
//
// ScreenRecordForm
//
resources.ApplyResources(this, "$this");
@ -116,5 +124,6 @@ private void InitializeComponent()
private System.Windows.Forms.ToolStripMenuItem tsmiStart;
private System.Windows.Forms.ToolStripMenuItem tsmiAbort;
private System.Windows.Forms.NotifyIcon niTray;
private HelpersLib.NoFocusBorderButton btnPause;
}
}

View file

@ -44,6 +44,7 @@ public partial class ScreenRecordForm : Form
public Stopwatch Timer { get; private set; }
public ManualResetEvent RecordResetEvent { get; set; }
public bool IsStopRequested { get; private set; }
public bool IsPauseRequested { get; private set; }
public bool IsAbortRequested { get; private set; }
public bool ActivateWindow { get; set; } = true;
@ -171,6 +172,13 @@ public void StartRecordingTimer()
UpdateTimer();
}
public void StopRecordingTimer()
{
Timer.Stop();
timerRefresh.Stop();
UpdateTimer();
}
private void UpdateTimer()
{
if (!IsDisposed)
@ -216,6 +224,14 @@ private void btnStart_MouseClick(object sender, MouseEventArgs e)
}
}
private void btnPause_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
StartStopRecording(true);
}
}
private void btnAbort_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
@ -228,12 +244,18 @@ private void btnAbort_MouseClick(object sender, MouseEventArgs e)
}
}
public void StartStopRecording()
public void StartStopRecording(bool isPause = false)
{
if (IsWorking)
{
IsStopRequested = true;
if (isPause)
{
RecordResetEvent.Reset();
IsPauseRequested = true;
}
if (!IsRecording)
{
IsAbortRequested = true;
@ -274,6 +296,7 @@ public void ChangeState(ScreenRecordState state)
break;
case ScreenRecordState.AfterStart:
IsWorking = true;
IsPauseRequested = false;
string trayTextAfterStart = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_stop_recording_;
niTray.Text = trayTextAfterStart.Truncate(63);
niTray.Icon = Resources.control_record.ToIcon();
@ -284,6 +307,11 @@ public void ChangeState(ScreenRecordState state)
IsRecording = true;
StartRecordingTimer();
break;
case ScreenRecordState.RecordingEnd:
IsWorking = false;
IsRecording = false;
StopRecordingTimer();
break;
case ScreenRecordState.Encoding:
Hide();
string trayTextAfterStop = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Encoding___;

View file

@ -151,19 +151,19 @@
<value>pInfo</value>
</data>
<data name="&gt;&gt;btnStart.ZOrder" xml:space="preserve">
<value>1</value>
<value>2</value>
</data>
<data name="lblTimer.Font" type="System.Drawing.Font, System.Drawing">
<value>Arial, 12pt</value>
</data>
<data name="lblTimer.Location" type="System.Drawing.Point, System.Drawing">
<value>190, 0</value>
<value>285, 0</value>
</data>
<data name="lblTimer.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>0, 0, 0, 0</value>
</data>
<data name="lblTimer.Size" type="System.Drawing.Size, System.Drawing">
<value>90, 36</value>
<value>92, 36</value>
</data>
<data name="lblTimer.TabIndex" type="System.Int32, mscorlib">
<value>2</value>
@ -184,7 +184,7 @@
<value>pInfo</value>
</data>
<data name="&gt;&gt;lblTimer.ZOrder" xml:space="preserve">
<value>2</value>
<value>3</value>
</data>
<metadata name="timerRefresh.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
@ -196,7 +196,7 @@
<value>Arial, 12pt</value>
</data>
<data name="btnAbort.Location" type="System.Drawing.Point, System.Drawing">
<value>95, 0</value>
<value>190, 0</value>
</data>
<data name="btnAbort.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>0, 0, 0, 0</value>
@ -220,7 +220,7 @@
<value>pInfo</value>
</data>
<data name="&gt;&gt;btnAbort.ZOrder" xml:space="preserve">
<value>0</value>
<value>1</value>
</data>
<data name="pInfo.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
@ -228,11 +228,47 @@
<data name="pInfo.AutoSizeMode" type="System.Windows.Forms.AutoSizeMode, System.Windows.Forms">
<value>GrowAndShrink</value>
</data>
<data name="btnPause.FlatStyle" type="System.Windows.Forms.FlatStyle, System.Windows.Forms">
<value>Flat</value>
</data>
<data name="btnPause.Font" type="System.Drawing.Font, System.Drawing">
<value>Arial, 12pt</value>
</data>
<data name="btnPause.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="btnPause.Location" type="System.Drawing.Point, System.Drawing">
<value>95, 0</value>
</data>
<data name="btnPause.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>0, 0, 0, 0</value>
</data>
<data name="btnPause.Size" type="System.Drawing.Size, System.Drawing">
<value>96, 36</value>
</data>
<data name="btnPause.TabIndex" type="System.Int32, mscorlib">
<value>3</value>
</data>
<data name="btnPause.Text" xml:space="preserve">
<value>Pause</value>
</data>
<data name="&gt;&gt;btnPause.Name" xml:space="preserve">
<value>btnPause</value>
</data>
<data name="&gt;&gt;btnPause.Type" xml:space="preserve">
<value>ShareX.HelpersLib.NoFocusBorderButton, ShareX.HelpersLib, Version=14.1.2.0, Culture=neutral, PublicKeyToken=null</value>
</data>
<data name="&gt;&gt;btnPause.Parent" xml:space="preserve">
<value>pInfo</value>
</data>
<data name="&gt;&gt;btnPause.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<data name="pInfo.Location" type="System.Drawing.Point, System.Drawing">
<value>8, 8</value>
</data>
<data name="pInfo.Size" type="System.Drawing.Size, System.Drawing">
<value>280, 36</value>
<value>377, 36</value>
</data>
<data name="pInfo.TabIndex" type="System.Int32, mscorlib">
<value>0</value>

View file

@ -24,6 +24,7 @@
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using ShareX.MediaLib;
using ShareX.Properties;
using ShareX.ScreenCaptureLib;
using System;
@ -167,6 +168,8 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
IsRecording = true;
string path = "";
string concatPath = "";
string tempPath = "";
bool abortRequested = false;
float duration = taskSettings.CaptureSettings.ScreenRecordFixedDuration ? taskSettings.CaptureSettings.ScreenRecordDuration : 0;
@ -202,61 +205,88 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
{
abortRequested = true;
}
if (!abortRequested)
else
{
recordForm.ChangeState(ScreenRecordState.BeforeStart);
if (taskSettings.CaptureSettings.ScreenRecordAutoStart)
{
int delay = (int)(taskSettings.CaptureSettings.ScreenRecordStartDelay * 1000);
if (delay > 0)
{
recordForm.InvokeSafe(() => recordForm.StartCountdown(delay));
recordForm.RecordResetEvent.WaitOne(delay);
}
}
else
{
recordForm.RecordResetEvent.WaitOne();
}
if (recordForm.IsAbortRequested)
{
abortRequested = true;
}
concatPath = FileHelpers.AppendTextToFileName(path, "-concat");
FileHelpers.DeleteFile(concatPath);
tempPath = FileHelpers.AppendTextToFileName(path, "-temp");
FileHelpers.DeleteFile(tempPath);
}
do
{
if (!abortRequested)
{
ScreenRecordingOptions options = new ScreenRecordingOptions()
recordForm.ChangeState(ScreenRecordState.BeforeStart);
if (recordForm.IsPauseRequested || !taskSettings.CaptureSettings.ScreenRecordAutoStart)
{
IsRecording = true,
IsLossless = taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding,
FFmpeg = taskSettings.CaptureSettings.FFmpegOptions,
FPS = fps,
Duration = duration,
OutputPath = path,
CaptureArea = captureRectangle,
DrawCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor
};
recordForm.RecordResetEvent.WaitOne();
}
else
{
int delay = (int)(taskSettings.CaptureSettings.ScreenRecordStartDelay * 1000);
Screenshot screenshot = TaskHelpers.GetScreenshot(taskSettings);
screenshot.CaptureCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor;
if (delay > 0)
{
recordForm.InvokeSafe(() => recordForm.StartCountdown(delay));
screenRecorder = new ScreenRecorder(ScreenRecordOutput.FFmpeg, options, screenshot, captureRectangle);
screenRecorder.RecordingStarted += ScreenRecorder_RecordingStarted;
screenRecorder.EncodingProgressChanged += ScreenRecorder_EncodingProgressChanged;
recordForm.ChangeState(ScreenRecordState.AfterStart);
screenRecorder.StartRecording();
recordForm.RecordResetEvent.WaitOne(delay);
}
}
if (recordForm.IsAbortRequested)
{
abortRequested = true;
}
if (recordForm.IsPauseRequested && File.Exists(path))
{
FileHelpers.RenameFile(path, concatPath);
}
if (!abortRequested)
{
ScreenRecordingOptions options = new ScreenRecordingOptions()
{
IsRecording = true,
IsLossless = taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding,
FFmpeg = taskSettings.CaptureSettings.FFmpegOptions,
FPS = fps,
Duration = duration,
OutputPath = path,
CaptureArea = captureRectangle,
DrawCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor
};
Screenshot screenshot = TaskHelpers.GetScreenshot(taskSettings);
screenshot.CaptureCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor;
screenRecorder = new ScreenRecorder(ScreenRecordOutput.FFmpeg, options, screenshot, captureRectangle);
screenRecorder.RecordingStarted += ScreenRecorder_RecordingStarted;
screenRecorder.EncodingProgressChanged += ScreenRecorder_EncodingProgressChanged;
recordForm.ChangeState(ScreenRecordState.AfterStart);
screenRecorder.StartRecording();
recordForm.ChangeState(ScreenRecordState.RecordingEnd);
if (recordForm.IsAbortRequested)
{
abortRequested = true;
}
}
if (File.Exists(concatPath))
{
using (FFmpegCLIManager ffmpeg = new FFmpegCLIManager(taskSettings.CaptureSettings.FFmpegOptions.FFmpegPath))
{
ffmpeg.ShowError = true;
ffmpeg.ConcatenateVideos(new string[] { concatPath, path }, tempPath, true);
FileHelpers.RenameFile(tempPath, path);
}
}
}
}
while (recordForm.IsPauseRequested);
}
catch (Exception e)
{