forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebCamSource.cs
More file actions
299 lines (260 loc) · 8.31 KB
/
Copy pathWebCamSource.cs
File metadata and controls
299 lines (260 loc) · 8.31 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
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_ANDROID
using UnityEngine.Android;
#endif
namespace Mediapipe.Unity
{
public class WebCamSource : ImageSource
{
[Tooltip("For the default resolution, the one whose width is closest to this value will be chosen")]
[SerializeField] private int _preferableDefaultWidth = 1280;
private const string _TAG = nameof(WebCamSource);
[SerializeField]
private ResolutionStruct[] _defaultAvailableResolutions = new ResolutionStruct[] {
new ResolutionStruct(176, 144, 30),
new ResolutionStruct(320, 240, 30),
new ResolutionStruct(424, 240, 30),
new ResolutionStruct(640, 360, 30),
new ResolutionStruct(640, 480, 30),
new ResolutionStruct(848, 480, 30),
new ResolutionStruct(960, 540, 30),
new ResolutionStruct(1280, 720, 30),
new ResolutionStruct(1600, 896, 30),
new ResolutionStruct(1920, 1080, 30),
};
private static readonly object _PermissionLock = new object();
private static bool _IsPermitted = false;
private WebCamTexture _webCamTexture;
private WebCamTexture webCamTexture
{
get => _webCamTexture;
set
{
if (_webCamTexture != null)
{
_webCamTexture.Stop();
}
_webCamTexture = value;
}
}
public override int textureWidth => !isPrepared ? 0 : webCamTexture.width;
public override int textureHeight => !isPrepared ? 0 : webCamTexture.height;
public override bool isVerticallyFlipped => isPrepared && webCamTexture.videoVerticallyMirrored;
public override bool isFrontFacing => isPrepared && (webCamDevice is WebCamDevice valueOfWebCamDevice) && valueOfWebCamDevice.isFrontFacing;
public override RotationAngle rotation => !isPrepared ? RotationAngle.Rotation0 : (RotationAngle)webCamTexture.videoRotationAngle;
private WebCamDevice? _webCamDevice;
private WebCamDevice? webCamDevice
{
get => _webCamDevice;
set
{
if (_webCamDevice is WebCamDevice valueOfWebCamDevice)
{
if (value is WebCamDevice valueOfValue && valueOfValue.name == valueOfWebCamDevice.name)
{
// not changed
return;
}
}
else if (value == null)
{
// not changed
return;
}
_webCamDevice = value;
resolution = GetDefaultResolution();
}
}
public override string sourceName => (webCamDevice is WebCamDevice valueOfWebCamDevice) ? valueOfWebCamDevice.name : null;
private WebCamDevice[] _availableSources;
private WebCamDevice[] availableSources
{
get
{
if (_availableSources == null)
{
_availableSources = WebCamTexture.devices;
}
return _availableSources;
}
set => _availableSources = value;
}
public override string[] sourceCandidateNames => availableSources?.Select(device => device.name).ToArray();
#pragma warning disable IDE0025
public override ResolutionStruct[] availableResolutions
{
get
{
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
if (webCamDevice is WebCamDevice valueOfWebCamDevice) {
return valueOfWebCamDevice.availableResolutions.Select(resolution => new ResolutionStruct(resolution)).ToArray();
}
#endif
return webCamDevice == null ? null : _defaultAvailableResolutions;
}
}
#pragma warning restore IDE0025
public override bool isPrepared => webCamTexture != null;
public override bool isPlaying => webCamTexture != null && webCamTexture.isPlaying;
private bool _isInitialized;
private IEnumerator Start()
{
yield return GetPermission();
if (!_IsPermitted)
{
_isInitialized = true;
yield break;
}
availableSources = WebCamTexture.devices;
if (availableSources != null && availableSources.Length > 0)
{
webCamDevice = availableSources[0];
}
_isInitialized = true;
}
private IEnumerator GetPermission()
{
lock (_PermissionLock)
{
if (_IsPermitted)
{
yield break;
}
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Permission.RequestUserPermission(Permission.Camera);
yield return new WaitForSeconds(0.1f);
}
#elif UNITY_IOS
if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) {
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
}
#endif
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Logger.LogWarning(_TAG, "Not permitted to use Camera");
yield break;
}
#elif UNITY_IOS
if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) {
Logger.LogWarning(_TAG, "Not permitted to use WebCam");
yield break;
}
#endif
_IsPermitted = true;
yield return new WaitForEndOfFrame();
}
}
public override void SelectSource(int sourceId)
{
if (sourceId < 0 || sourceId >= availableSources.Length)
{
throw new ArgumentException($"Invalid source ID: {sourceId}");
}
webCamDevice = availableSources[sourceId];
}
public override IEnumerator Play()
{
yield return new WaitUntil(() => _isInitialized);
if (!_IsPermitted)
{
throw new InvalidOperationException("Not permitted to access cameras");
}
InitializeWebCamTexture();
webCamTexture.Play();
yield return WaitForWebCamTexture();
}
public override IEnumerator Resume()
{
if (!isPrepared)
{
throw new InvalidOperationException("WebCamTexture is not prepared yet");
}
if (!webCamTexture.isPlaying)
{
webCamTexture.Play();
}
yield return WaitForWebCamTexture();
}
public override void Pause()
{
if (isPlaying)
{
webCamTexture.Pause();
}
}
public override void Stop()
{
if (webCamTexture != null)
{
webCamTexture.Stop();
}
webCamTexture = null;
}
public override Texture GetCurrentTexture()
{
return webCamTexture;
}
private ResolutionStruct GetDefaultResolution()
{
var resolutions = availableResolutions;
return resolutions == null || resolutions.Length == 0 ? new ResolutionStruct() : resolutions.OrderBy(resolution => resolution, new ResolutionStructComparer(_preferableDefaultWidth)).First();
}
private void InitializeWebCamTexture()
{
Stop();
if (webCamDevice is WebCamDevice valueOfWebCamDevice)
{
webCamTexture = new WebCamTexture(valueOfWebCamDevice.name, resolution.width, resolution.height, (int)resolution.frameRate);
return;
}
throw new InvalidOperationException("Cannot initialize WebCamTexture because WebCamDevice is not selected");
}
private IEnumerator WaitForWebCamTexture()
{
const int timeoutFrame = 2000;
var count = 0;
Logger.LogVerbose("Waiting for WebCamTexture to start");
yield return new WaitUntil(() => count++ > timeoutFrame || webCamTexture.width > 16);
if (webCamTexture.width <= 16)
{
throw new TimeoutException("Failed to start WebCam");
}
}
private class ResolutionStructComparer : IComparer<ResolutionStruct>
{
private readonly int _preferableDefaultWidth;
public ResolutionStructComparer(int preferableDefaultWidth)
{
_preferableDefaultWidth = preferableDefaultWidth;
}
public int Compare(ResolutionStruct a, ResolutionStruct b)
{
var aDiff = Mathf.Abs(a.width - _preferableDefaultWidth);
var bDiff = Mathf.Abs(b.width - _preferableDefaultWidth);
if (aDiff != bDiff)
{
return aDiff - bDiff;
}
if (a.height != b.height)
{
// prefer smaller height
return a.height - b.height;
}
// prefer smaller frame rate
return (int)(a.frameRate - b.frameRate);
}
}
}
}