Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Delete circular buffer zero-length file #192

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -85,6 +86,7 @@ public override void Process(ImageContext context)
if (this.CurrentStream != null && this.CurrentStream.CanWrite)
{
this.CurrentStream.Write(this.Buffer.ToArray(), 0, this.Buffer.Size);
this.FileIsEmpty = false;
}

this.Processed += this.Buffer.Size;
Expand All @@ -94,6 +96,7 @@ public override void Process(ImageContext context)
if (this.CurrentStream != null && this.CurrentStream.CanWrite)
{
this.CurrentStream.Write(context.Data, 0, context.Data.Length);
this.FileIsEmpty = false;
}

this.Processed += context.Data.Length;
Expand Down Expand Up @@ -154,12 +157,6 @@ public void StopRecording()
_receivedIFrame = false;
}

/// <inheritdoc />
public override void Dispose()
{
this.CurrentStream?.Dispose();
}

/// <inheritdoc />
public override string TotalProcessed()
{
Expand Down
52 changes: 45 additions & 7 deletions src/MMALSharp.Processing/Handlers/FileStreamCaptureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>

using Microsoft.Extensions.Logging;
using MMALSharp.Common;
using MMALSharp.Common.Utility;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -36,17 +37,26 @@ public class FileStreamCaptureHandler : StreamCaptureHandler<FileStream>, IFileS
public string Extension { get; set; }

/// <summary>
/// The name of the current file associated with the FileStream.
/// The name of the file associated with the FileStream (without a path or extension).
/// </summary>
public string CurrentFilename { get; set; }

/// <summary>
/// When true, the Dispose method will delete the zero-length file identified by CurrentStream.Name.
/// Inheriting classes should set this to false any time they write to the file stream.
/// </summary>
protected bool FileIsEmpty { get; set; }

/// <summary>
/// Creates a new instance of the <see cref="FileStreamCaptureHandler"/> class without provisions for writing to a file. Supports
/// subclasses in which file output is optional.
/// </summary>
public FileStreamCaptureHandler()
{
MMALLog.Logger.LogDebug($"{nameof(FileStreamCaptureHandler)} empty ctor invoked, no file will be written");

// Prevent Dispose from attempting to delete a non-existent file.
this.FileIsEmpty = false;
}

/// <summary>
Expand All @@ -63,9 +73,9 @@ public FileStreamCaptureHandler(string directory, string extension)
MMALLog.Logger.LogDebug($"{nameof(FileStreamCaptureHandler)} created for directory {this.Directory} and extension {this.Extension}");

System.IO.Directory.CreateDirectory(this.Directory);

var now = DateTime.Now.ToString("dd-MMM-yy HH-mm-ss");

int i = 1;

var fileName = $"{this.Directory}/{now}.{this.Extension}";
Expand All @@ -80,6 +90,7 @@ public FileStreamCaptureHandler(string directory, string extension)

this.CurrentFilename = Path.GetFileNameWithoutExtension(fileInfo.Name);
this.CurrentStream = File.Create(fileName);
this.FileIsEmpty = true;
}

/// <summary>
Expand All @@ -94,12 +105,12 @@ public FileStreamCaptureHandler(string fullPath)
this.CurrentFilename = Path.GetFileNameWithoutExtension(fileInfo.Name);

var ext = fullPath.Split('.').LastOrDefault();

if (string.IsNullOrEmpty(ext))
{
throw new ArgumentNullException(nameof(ext), "Could not get file extension from path string.");
}

this.Extension = ext;

MMALLog.Logger.LogDebug($"{nameof(FileStreamCaptureHandler)} created for directory {this.Directory} and extension {this.Extension}");
Expand All @@ -109,17 +120,25 @@ public FileStreamCaptureHandler(string fullPath)
System.IO.Directory.CreateDirectory(this.Directory);

this.CurrentStream = File.Create(fullPath);
this.FileIsEmpty = true;
}

/// <inheritdoc />
public override void Process(ImageContext context)
{
base.Process(context);
this.FileIsEmpty = false;
}

/// <summary>
/// Gets the filename that a FileStream points to.
/// Gets the filename that a FileStream points to without a path or extension.
/// </summary>
/// <returns>The filename.</returns>
public string GetFilename() =>
(this.CurrentStream != null) ? Path.GetFileNameWithoutExtension(this.CurrentStream.Name) : string.Empty;

/// <summary>
/// Gets the filepath that a FileStream points to.
/// Gets the full file pathname that a FileStream points to.
/// </summary>
/// <returns>The filepath.</returns>
public string GetFilepath() =>
Expand Down Expand Up @@ -161,6 +180,7 @@ public virtual void NewFile()
}

this.CurrentStream = File.Create(newFilename);
this.FileIsEmpty = true;
}

/// <inheritdoc />
Expand All @@ -180,5 +200,23 @@ public override string TotalProcessed()
{
return $"{this.Processed}";
}

/// <inheritdoc />
public override void Dispose()
{
base.Dispose();

// Disposing the stream can leave a zero-length file on disk if nothing
// was recorded into it -- but after recording has taken place, only
// calling NewFile (or Split for videos) will create a new empty file.
if (this.FileIsEmpty)
{
try
{
File.Delete(this.CurrentStream.Name);
}
catch { }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ public override void Process(ImageContext context)
if (this.StoreVideoTimestamps && context.Pts.HasValue)
{
var str = $"{context.Pts / 1000}.{context.Pts % 1000:000}" + Environment.NewLine;

File.AppendAllText($"{this.Directory}/{this.CurrentFilename}.pts", str);
this.FileIsEmpty = false;
}
}

/// <summary>
/// Splits the current file by closing the current stream and opening a new one.
/// </summary>
public virtual void Split() => this.NewFile();

/// <summary>
/// Used to set the current working motion vector store.
/// </summary>
Expand Down