-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCaptureDevice.cs
More file actions
119 lines (94 loc) · 3.6 KB
/
CaptureDevice.cs
File metadata and controls
119 lines (94 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
////////////////////////////////////////////////////////////////////////////
//
// FlashCap - Independent camera capture library.
// Copyright (c) Kouji Matsui (@kekyo@mi.kekyo.net)
//
// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0
//
////////////////////////////////////////////////////////////////////////////
using FlashCap.Internal;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace FlashCap;
public abstract class CaptureDevice :
#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1
IAsyncDisposable,
#endif
IDisposable
{
private readonly AsyncLock locker = new();
protected CaptureDevice(object identity, string name)
{
this.Identity = identity;
this.Name = name;
}
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public void Dispose() =>
_ = this.DisposeAsync().ConfigureAwait(true);
#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1
ValueTask IAsyncDisposable.DisposeAsync() =>
new(this.DisposeAsync());
#endif
public async Task DisposeAsync()
{
using var _ = await locker.LockAsync(default).
ConfigureAwait(true);
await this.OnDisposeAsync().
ConfigureAwait(true);
}
protected virtual Task OnDisposeAsync() =>
TaskCompat.CompletedTask;
protected abstract Task OnInitializeAsync(
VideoCharacteristics characteristics,
TranscodeFormats transcodeFormat,
FrameProcessor frameProcessor,
CancellationToken ct);
public object Identity { get; }
public string Name { get; }
public virtual bool HasPropertyPage => false;
public VideoCharacteristics Characteristics { get; protected set; } = null!;
public bool IsRunning { get; protected set; }
protected abstract Task OnStartAsync(CancellationToken ct);
protected abstract Task OnStopAsync(CancellationToken ct);
protected abstract void OnCapture(
IntPtr pData, int size, long timestampMicroseconds, long frameIndex, PixelBuffer buffer);
protected virtual Task<bool> OnShowPropertyPageAsync(
IntPtr parentWindow, CancellationToken ct) =>
TaskCompat.FromResult(false);
//////////////////////////////////////////////////////////////////////////
internal Task InternalInitializeAsync(
VideoCharacteristics characteristics,
TranscodeFormats transcodeFormat,
FrameProcessor frameProcessor,
CancellationToken ct) =>
this.OnInitializeAsync(characteristics, transcodeFormat, frameProcessor, ct);
internal async Task InternalStartAsync(CancellationToken ct)
{
using var _ = await locker.LockAsync(ct).
ConfigureAwait(false);
await this.OnStartAsync(ct);
}
internal async Task InternalStopAsync(CancellationToken ct)
{
using var _ = await locker.LockAsync(ct).
ConfigureAwait(false);
await this.OnStopAsync(ct);
}
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal void InternalOnCapture(
IntPtr pData, int size, long timestampMicroseconds, long frameIndex, PixelBuffer buffer) =>
this.OnCapture(pData, size, timestampMicroseconds, frameIndex, buffer);
internal async Task<bool> InternalShowPropertyPageAsync(
IntPtr parentWindow, CancellationToken ct)
{
using var _ = await locker.LockAsync(ct).
ConfigureAwait(false);
return await this.OnShowPropertyPageAsync(parentWindow, ct);
}
}