forked from amkovkov/GranuSightSoftware2
refactor: hardware services
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using LibCameraSharp;
|
||||
using Stream = LibCameraSharp.Stream;
|
||||
using System.Buffers;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using LibCameraSharp;
|
||||
|
||||
using GSS2.Core.Extentions;
|
||||
using GSS2.Core.Abstractions;
|
||||
|
||||
using Stream = LibCameraSharp.Stream;
|
||||
|
||||
namespace GSS2.Core.Hardware;
|
||||
|
||||
@@ -26,31 +32,6 @@ public class CameraService : IDisposable
|
||||
public ulong Length { get; init; }
|
||||
public ulong AlignedOffset { get; init; }
|
||||
}
|
||||
|
||||
public class CameraServiceConfiguration
|
||||
{
|
||||
public static CameraServiceConfiguration Default => new CameraServiceConfiguration
|
||||
{
|
||||
CameraId = "",
|
||||
ViewFinderFrameBufferCount = 5,
|
||||
ViewFinderWidth = 1920,
|
||||
ViewFinderHeight = 1080,
|
||||
ViewFinderFps = 30,
|
||||
ImageCaptureFrameBufferCount = 2,
|
||||
ImageCaptureWidth = 4056,
|
||||
ImageCaptureHeight = 3080,
|
||||
};
|
||||
|
||||
public string CameraId { get; set; } = "";
|
||||
public int ViewFinderFrameBufferCount { get; set; }
|
||||
public int ViewFinderWidth { get; set; }
|
||||
public int ViewFinderHeight { get; set; }
|
||||
public int ViewFinderFps { get; set; }
|
||||
public int ImageCaptureFrameBufferCount { get; set; }
|
||||
public int ImageCaptureWidth { get; set; }
|
||||
public int ImageCaptureHeight { get; set; }
|
||||
}
|
||||
|
||||
[Flags]
|
||||
private enum State
|
||||
{
|
||||
@@ -63,6 +44,30 @@ public class CameraService : IDisposable
|
||||
ImageCaptureFrameBuffersMapped = 1 << 6
|
||||
}
|
||||
|
||||
public record CameraServiceConfiguration : ServiceConfigurationBase
|
||||
{
|
||||
public static CameraServiceConfiguration Default => new CameraServiceConfiguration
|
||||
{
|
||||
CameraId = "",
|
||||
ViewFinderFrameBufferCount = 5,
|
||||
ViewFinderWidth = 4056,
|
||||
ViewFinderHeight = 3080,
|
||||
ViewFinderFps = 30,
|
||||
ImageCaptureFrameBufferCount = 2,
|
||||
ImageCaptureWidth = 4056,
|
||||
ImageCaptureHeight = 3080
|
||||
};
|
||||
|
||||
public string CameraId { get; set; } = "";
|
||||
public int ViewFinderFrameBufferCount { get; set; }
|
||||
public int ViewFinderWidth { get; set; }
|
||||
public int ViewFinderHeight { get; set; }
|
||||
public int ViewFinderFps { get; set; }
|
||||
public int ImageCaptureFrameBufferCount { get; set; }
|
||||
public int ImageCaptureWidth { get; set; }
|
||||
public int ImageCaptureHeight { get; set; }
|
||||
}
|
||||
|
||||
private bool _disposedValue;
|
||||
private readonly ILogger<CameraService> _logger;
|
||||
private readonly CameraServiceConfiguration _configuration;
|
||||
@@ -77,39 +82,40 @@ public class CameraService : IDisposable
|
||||
private Stream? _viewFinderStream;
|
||||
private Stream? _imageCaptureStream;
|
||||
private List<MappedBuffer>? _imageCaptureMappedBuffers;
|
||||
private System.Timers.Timer? _viewFinderTimer = null;
|
||||
private System.Timers.ElapsedEventHandler? _viewFinderTimerElapsed = null;
|
||||
private EventHandler<Camera.RequestCompletedEventArgs>? _viewFinderRequestComplitedHandler = null;
|
||||
private readonly CancellationTokenSource _disposeCts = new CancellationTokenSource();
|
||||
|
||||
public bool IsImageCapturing { get; private set; } = false;
|
||||
public List<FrameBuffer> ViewFinderBufferQueue { get; private set; } = new List<FrameBuffer>();
|
||||
|
||||
public CameraService(ILogger<CameraService> logger, CameraServiceConfiguration? configuration = null, bool autoInitialize = false)
|
||||
{
|
||||
_logger = logger;
|
||||
|
||||
_logger.LogInformation("CameraService initialization");
|
||||
_logger.LogInformation("Initialization");
|
||||
|
||||
if (configuration is null)
|
||||
{
|
||||
_logger.LogWarning("CameraService configuration not set. Using default configuration.");
|
||||
_configuration = CameraServiceConfiguration.Default;
|
||||
}
|
||||
else
|
||||
try
|
||||
{
|
||||
if (configuration is null)
|
||||
{
|
||||
configuration = CameraServiceConfiguration.Default;
|
||||
_logger.LogWarning("Configuration not set. Using default configuration.");
|
||||
}
|
||||
_configuration = configuration;
|
||||
_logger.LogInformation("Using following configuration: \n{}", _configuration.ToString("\n", 4));
|
||||
|
||||
if (autoInitialize)
|
||||
Initialize(CancellationToken.None);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogCritical(e, "An exception occurred while initialization");
|
||||
throw;
|
||||
}
|
||||
|
||||
_logger.LogDebug(@$"CameraService use following configuration:
|
||||
CameraId: {_configuration.CameraId}
|
||||
ViewFinderFrameBufferCount: {_configuration.ViewFinderFrameBufferCount}
|
||||
ViewFinderWidth: {_configuration.ViewFinderWidth}
|
||||
ViewFinderHeight: {_configuration.ViewFinderHeight}
|
||||
ViewFinderFps: {_configuration.ViewFinderFps}
|
||||
ImageCaptureFrameBufferCount: {_configuration.ImageCaptureFrameBufferCount}
|
||||
ImageCaptureWidth: {_configuration.ImageCaptureWidth}
|
||||
ImageCaptureHeight: {_configuration.ImageCaptureHeight}
|
||||
");
|
||||
|
||||
if (autoInitialize)
|
||||
Initialize(CancellationToken.None);
|
||||
_logger.LogInformation("Initialized");
|
||||
}
|
||||
|
||||
public async Task InitializeAsync(CancellationToken cancellationToken)
|
||||
@@ -664,11 +670,6 @@ public class CameraService : IDisposable
|
||||
return image;
|
||||
}
|
||||
|
||||
public List<FrameBuffer> ViewFinderBufferQueue { get; private set; } = new List<FrameBuffer>();
|
||||
private System.Timers.Timer? _viewFinderTimer = null;
|
||||
private System.Timers.ElapsedEventHandler? _viewFinderTimerElapsed = null;
|
||||
private EventHandler<Camera.RequestCompletedEventArgs>? _viewFinderRequestComplitedHandler = null;
|
||||
|
||||
public void StartViewFinder()
|
||||
{
|
||||
if (_camera is null)
|
||||
|
||||
Reference in New Issue
Block a user