-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathCameraHelper.cs
More file actions
313 lines (271 loc) · 11.5 KB
/
CameraHelper.cs
File metadata and controls
313 lines (271 loc) · 11.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Windows.Devices.Enumeration;
using Windows.Media.Capture;
using Windows.Media.Capture.Frames;
using Windows.Media.MediaProperties;
namespace CommunityToolkit.WinUI.Helpers;
/// <summary>
/// Helper class for capturing frames from available camera sources.
/// Make sure you have the capability webcam enabled for your app to access the device's camera.
/// </summary>
#pragma warning disable CA1063 // Implement IDisposable Correctly
public partial class CameraHelper : IDisposable
{
private static IReadOnlyList<MediaFrameSourceGroup>? _frameSourceGroups;
#pragma warning disable CA2213 // Disposable fields should be disposed
private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1);
#pragma warning restore CA2213 // Disposable fields should be disposed
private MediaCapture? _mediaCapture;
private MediaFrameReader? _frameReader;
private MediaFrameSourceGroup? _group;
private bool groupChanged = false;
private bool _initialized;
/// <summary>
/// Gets a list of <see cref="MediaFrameSourceGroup"/> available for video preview or video record.
/// </summary>
/// <returns>A <see cref="MediaFrameSourceGroup"/> list.</returns>
public static async Task<IReadOnlyList<MediaFrameSourceGroup>> GetFrameSourceGroupsAsync()
{
if (_frameSourceGroups == null)
{
var videoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var groups = await MediaFrameSourceGroup.FindAllAsync();
// Filter out color video preview and video record type sources and remove duplicates video devices.
_frameSourceGroups = groups.Where(g => g.SourceInfos.Any(s => s.SourceKind == MediaFrameSourceKind.Color &&
(s.MediaStreamType == MediaStreamType.VideoPreview || s.MediaStreamType == MediaStreamType.VideoRecord))
&& g.SourceInfos.All(sourceInfo => videoDevices.Any(vd => vd.Id == sourceInfo.DeviceInformation.Id))).ToList();
}
return _frameSourceGroups;
}
/// <summary>
/// Gets a list of <see cref="MediaFrameFormat"/> available on the source.
/// </summary>
public IReadOnlyList<MediaFrameFormat>? FrameFormatsAvailable { get; private set; }
/// <summary>
/// Gets or sets the source group for camera video preview.
/// </summary>
public MediaFrameSourceGroup FrameSourceGroup
{
get => _group!;
set
{
groupChanged = _group != value;
_group = value;
}
}
/// <summary>
/// Gets the currently selected <see cref="MediaFrameSource"/> for video preview.
/// </summary>
public MediaFrameSource? PreviewFrameSource { get; private set; }
/// <summary>
/// Occurs when a new frame arrives.
/// </summary>
public event EventHandler<FrameEventArgs>? FrameArrived;
/// <summary>
/// Initializes Camera Media Capture settings and initializes Frame Reader to capture frames in real time.
/// If no MediaFrameSourceGroup is provided, it selects the first available camera source to use for media capture.
/// You could select a specific MediaFrameSourceGroup from the available sources using the CameraHelper FrameSourceGroups property.
/// </summary>
/// <returns>Result of the async operation.<see cref="CameraHelperResult"/></returns>
public async Task<CameraHelperResult> InitializeAndStartCaptureAsync()
{
CameraHelperResult result;
try
{
await _semaphoreSlim.WaitAsync();
// if FrameSourceGroup hasn't changed from last initialization, just return back.
if (_initialized && _group != null && !groupChanged)
{
return CameraHelperResult.Success;
}
groupChanged = false;
await StopReaderAsync();
if (_mediaCapture != null)
{
_mediaCapture.Dispose();
_mediaCapture = null;
}
if (_frameSourceGroups == null)
{
_frameSourceGroups = await GetFrameSourceGroupsAsync();
}
if (_group == null)
{
_group = _frameSourceGroups.FirstOrDefault();
}
else
{
// Verify selected group is part of existing FrameSourceGroups
_group = _frameSourceGroups.FirstOrDefault(g => g.Id == _group.Id);
}
// If there is no camera source available, we can't proceed
if (_group == null)
{
return CameraHelperResult.NoFrameSourceGroupAvailable;
}
result = await InitializeMediaCaptureAsync();
if (PreviewFrameSource != null && _mediaCapture != null)
{
_frameReader = await _mediaCapture.CreateFrameReaderAsync(PreviewFrameSource);
_frameReader.AcquisitionMode = MediaFrameReaderAcquisitionMode.Realtime;
_frameReader.FrameArrived += Reader_FrameArrived;
if (_frameReader == null)
{
result = CameraHelperResult.CreateFrameReaderFailed;
}
else
{
MediaFrameReaderStartStatus statusResult = await _frameReader.StartAsync();
if (statusResult != MediaFrameReaderStartStatus.Success)
{
result = CameraHelperResult.StartFrameReaderFailed;
}
}
}
_initialized = result == CameraHelperResult.Success;
return result;
}
catch (Exception)
{
await CleanUpAsync();
return CameraHelperResult.InitializationFailed_UnknownError;
}
finally
{
_semaphoreSlim.Release();
}
}
/// <summary>
/// Clean up the Camera Helper resources
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task CleanUpAsync()
{
await _semaphoreSlim.WaitAsync();
try
{
_initialized = false;
await StopReaderAsync();
if (_mediaCapture != null)
{
_mediaCapture.Dispose();
_mediaCapture = null;
}
}
finally
{
_semaphoreSlim.Release();
}
}
private async Task<CameraHelperResult> InitializeMediaCaptureAsync()
{
if (_mediaCapture == null)
{
_mediaCapture = new MediaCapture();
}
var settings = new MediaCaptureInitializationSettings()
{
SourceGroup = _group,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
StreamingCaptureMode = StreamingCaptureMode.Video
};
try
{
await _mediaCapture.InitializeAsync(settings);
// Find the first video preview or record stream available
PreviewFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoPreview
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
if (PreviewFrameSource == null)
{
PreviewFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoRecord
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
}
if (PreviewFrameSource == null)
{
return CameraHelperResult.NoFrameSourceAvailable;
}
// Get only formats of a certain frame-rate and compatible subtype for previewing, order them by resolution
FrameFormatsAvailable = PreviewFrameSource.SupportedFormats.Where(format =>
format.FrameRate.Numerator / format.FrameRate.Denominator >= 15 // fps
&& (string.Compare(format.Subtype, MediaEncodingSubtypes.Nv12, true) == 0
|| string.Compare(format.Subtype, MediaEncodingSubtypes.Bgra8, true) == 0
|| string.Compare(format.Subtype, MediaEncodingSubtypes.Yuy2, true) == 0
|| string.Compare(format.Subtype, MediaEncodingSubtypes.Rgb32, true) == 0))?.OrderBy(format => format.VideoFormat.Width * format.VideoFormat.Height).ToList();
if (FrameFormatsAvailable == null || !FrameFormatsAvailable.Any())
{
return CameraHelperResult.NoCompatibleFrameFormatAvailable;
}
// Set the format with the highest resolution available by default
var defaultFormat = FrameFormatsAvailable.Last();
await PreviewFrameSource.SetFormatAsync(defaultFormat);
}
catch (UnauthorizedAccessException)
{
await StopReaderAsync();
if (_mediaCapture != null)
{
_mediaCapture.Dispose();
_mediaCapture = null;
}
return CameraHelperResult.CameraAccessDenied;
}
catch (Exception)
{
await StopReaderAsync();
if (_mediaCapture != null)
{
_mediaCapture.Dispose();
_mediaCapture = null;
}
return CameraHelperResult.InitializationFailed_UnknownError;
}
return CameraHelperResult.Success;
}
/// <summary>
/// Stops reading from the frame reader and disposes of the reader.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
private async Task StopReaderAsync()
{
if (_frameReader != null)
{
_frameReader.FrameArrived -= Reader_FrameArrived;
await _frameReader.StopAsync();
_frameReader.Dispose();
_frameReader = null;
}
}
/// <summary>
/// Handles the frame arrived event by converting the frame to a displayable
/// format and rendering it to the screen.
/// </summary>
private void Reader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
// TryAcquireLatestFrame will return the latest frame that has not yet been acquired.
// This can return null if there is no such frame, or if the reader is not in the
// "Started" state. The latter can occur if a FrameArrived event was in flight
// when the reader was stopped.
var frame = sender.TryAcquireLatestFrame();
if (frame != null)
{
var vmf = frame.VideoMediaFrame;
EventHandler<FrameEventArgs> handler = FrameArrived!;
var frameArgs = new FrameEventArgs() { VideoFrame = vmf.GetVideoFrame() };
handler?.Invoke(sender, frameArgs);
frame.Dispose();
}
}
private bool disposedValue = false;
/// <inheritdoc/>
public async void Dispose()
{
if (!disposedValue)
{
disposedValue = true;
await CleanUpAsync();
}
}
}
#pragma warning restore CA1063 // Implement IDisposable Correctly