Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Jan 15, 2024
1 parent e3a5377 commit 87f4402
Showing 1 changed file with 67 additions and 42 deletions.
109 changes: 67 additions & 42 deletions FlashCap.Core/Devices/V4L2Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public sealed class V4L2Device : CaptureDevice

private long frameIndex;

private readonly IntPtr[] pBuffers = new IntPtr[BufferCount];
private readonly int[] bufferLength = new int[BufferCount];
private IntPtr[] pBuffers = new IntPtr[BufferCount];
private int[] bufferLength = new int[BufferCount];

private int fd;
private IntPtr pBih;
Expand Down Expand Up @@ -107,30 +107,55 @@ protected override unsafe Task OnInitializeAsync(
close(this.abortwfd);
this.abortwfd = -1;
}

if (this.abortrfd != -1)
{
close(this.abortrfd);
this.abortrfd = -1;
}

if (this.pBuffers is { } pBuffers &&
this.bufferLength is { } bufferLength)
{
for (var index = 0; index < pBuffers.Length; index++)
{
if (pBuffers[index] != default &&
bufferLength[index] != default)
{
munmap(pBuffers[index], (ulong)bufferLength[index]);
pBuffers[index] = default;
bufferLength[index] = default;
}
}

this.pBuffers = null!;
this.bufferLength = null!;
}

if (this.fd != -1)
{
close(this.fd);
this.fd = -1;
}

if (this.pBih != IntPtr.Zero)
{
NativeMethods.FreeMemory(this.pBih);
this.pBih = IntPtr.Zero;
}

this.IsRunning = false;
}

protected override async Task OnDisposeAsync()
{
if (this.abortwfd != -1)
if (this.IsRunning)
{
await this.frameProcessor.DisposeAsync().
ConfigureAwait(false);

if (this.IsRunning)
{
this.IsRunning = false;
ioctl(
this.fd, Interop.VIDIOC_STREAMOFF,
(int)v4l2_buf_type.VIDEO_CAPTURE);
}

write(this.abortwfd, new byte[] { 0x01 }, 1);

var task = Interlocked.Exchange(ref this.task, null!);
await task.ConfigureAwait(false);

close(this.abortwfd);
this.abortwfd = -1;
await this.InternalStopAsync(default).
ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -238,28 +263,6 @@ static bool IsIgnore(int code) =>
{
Trace.WriteLine(ex);
}
finally
{
for (var index = 0; index < pBuffers.Length; index++)
{
if (this.pBuffers[index] != default &&
this.bufferLength[index] != default)
{
munmap(this.pBuffers[index], (ulong)this.bufferLength[index]);
this.pBuffers[index] = default;
this.bufferLength[index] = default;
}
}

close(this.abortrfd);
close(this.fd);

NativeMethods.FreeMemory(this.pBih);

this.abortrfd = -1;
this.fd = -1;
this.pBih = IntPtr.Zero;
}
}

protected override Task OnStartAsync(CancellationToken ct)
Expand Down Expand Up @@ -407,14 +410,36 @@ protected override async Task OnStopAsync(CancellationToken ct)
(int)v4l2_buf_type.VIDEO_CAPTURE) < 0)
{
var code = Marshal.GetLastWin32Error();
this.IsRunning = true;
throw new ArgumentException(
$"FlashCap: Couldn't stop capture: Code={code}, DevicePath={this.devicePath}");
}

write(this.abortwfd, new byte[] { 0x01 }, 1);

this.IsRunning = false;
var task = Interlocked.Exchange(ref this.task, null!);
await task.ConfigureAwait(false);

close(this.abortwfd);
this.abortwfd = -1;

for (var index = 0; index < this.pBuffers.Length; index++)
{
if (this.pBuffers[index] != default &&
this.bufferLength[index] != default)
{
munmap(this.pBuffers[index], (ulong)this.bufferLength[index]);
this.pBuffers[index] = default;
this.bufferLength[index] = default;
}
}

close(this.abortrfd);
close(this.fd);

this.abortrfd = -1;
this.fd = -1;

this.IsRunning = false;
}
}

Expand Down

0 comments on commit 87f4402

Please sign in to comment.