-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawVideoCapturePreviewExample.cs
More file actions
439 lines (380 loc) · 15 KB
/
Copy pathRawVideoCapturePreviewExample.cs
File metadata and controls
439 lines (380 loc) · 15 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
//
// Copyright (c) 2019-present, Magic Leap, Inc. All Rights Reserved.
// Use of this file is governed by the Developer Agreement, located
// here: https://auth.magicleap.com/terms/developer
//
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using UnityEngine.UI;
using System;
using System.Text;
namespace MagicLeap
{
/// <summary>
/// This class handles video recording and loading based on controller
/// input.
/// </summary>
public class RawVideoCapturePreviewExample : MonoBehaviour
{
[SerializeField, Tooltip("The text used to display status information for the example.")]
private Text _statusText = null;
[Space, SerializeField, Tooltip("MLControllerConnectionHandlerBehavior reference.")]
private MLControllerConnectionHandlerBehavior _controllerConnectionHandler = null;
[SerializeField, Tooltip("Refrence to the Privilege requester Prefab")]
private MLPrivilegeRequesterBehavior _privilegeRequester = null;
[SerializeField, Tooltip("Refrence to the Raw Video Capture Visualizer gameobject")]
private RawVideoCaptureVisualizer _rawVideoCaptureVisualizer = null;
private string _intrinsicValuesText = null;
// Is the camera currently recording
private bool _isCapturing = false;
private bool _isCameraConnected = false;
private float _captureStartTime = 0.0f;
private bool _hasStarted = false;
#pragma warning disable 414
private bool _appPaused = false;
#pragma warning restore 414
#if PLATFORM_LUMIN
private event Action<MLCamera.ResultExtras, MLCamera.YUVFrameInfo, MLCamera.FrameMetadata> OnRawVideoDataReceived = null;
#endif
#pragma warning disable 414
private event Action OnRawVideoCaptureStarted = null;
private event Action OnRawVideoCaptureEnded = null;
#pragma warning restore 414
// Using Awake so that Privileges is set before MLPrivilegeRequesterBehavior Start
void Awake()
{
if (_controllerConnectionHandler == null)
{
Debug.LogError("Error: VideoCamptureExample._controllerConnectionHandler is not set, disabling script.");
enabled = false;
return;
}
if (_privilegeRequester == null)
{
Debug.LogError("Error: RawVideoCapturePreviewExample._privilegeRequester is not set, disabling script.");
enabled = false;
return;
}
if (_statusText == null)
{
Debug.LogError("Error: RawVideoCapturePreviewExample._statusText is not set, disabling script.");
enabled = false;
return;
}
#if PLATFORM_LUMIN
// Before enabling the Camera, the scene must wait until the privileges have been granted.
_privilegeRequester.OnPrivilegesDone += HandlePrivilegesDone;
#endif
OnRawVideoCaptureStarted += _rawVideoCaptureVisualizer.OnCaptureStarted;
OnRawVideoCaptureEnded += _rawVideoCaptureVisualizer.OnCaptureEnded;
#if PLATFORM_LUMIN
MLCamera.OnCameraConnected += OnCameraConnected;
MLCamera.OnCameraDisconnected += OnCameraDisconnected;
MLCamera.OnCameraCaptureStarted += OnCameraCaptureStarted;
MLCamera.OnCameraCaptureCompleted +=OnCameraCaptureCompleted;
OnRawVideoDataReceived += _rawVideoCaptureVisualizer.OnRawCaptureDataReceived;
#endif
}
#if PLATFORM_LUMIN
private void OnCameraConnected(MLResult result)
{
if (result.IsOk)
{
MLCamera.OnRawVideoFrameAvailableYUV += OnRawCaptureDataReceived;
_isCameraConnected = true;
}
else
{
Debug.LogErrorFormat("Error: RawVideoCapturePreviewExample failed to connect camera. Error Code: {0}", MLCamera.GetErrorCode().ToString());
}
}
private void OnCameraDisconnected(MLResult result)
{
_isCameraConnected = false;
MLCamera.OnRawVideoFrameAvailableYUV -= OnRawCaptureDataReceived;
}
private void OnCameraCaptureStarted(MLResult result, string pathName)
{
if (result.IsOk)
{
_isCapturing = true;
_captureStartTime = Time.time;
OnRawVideoCaptureStarted.Invoke();
SetupCameraIntrinsics();
}
else
{
Debug.LogError($"Error: RawVideoCapturePreviewExample failed to start raw video capture. Reason: {MLCamera.GetErrorCode()}");
}
}
private void OnCameraCaptureCompleted(MLResult result)
{
if (result.IsOk)
{
OnRawVideoCaptureEnded.Invoke();
_isCapturing = false;
_captureStartTime = 0;
}
else
{
Debug.LogErrorFormat("Error: RawVideoCapturePreviewExample failed to end video capture. Error Code: {0}", MLCamera.GetErrorCode().ToString());
}
}
#endif
void Update()
{
UpdateStatusText();
}
/// <summary>
/// Stop the camera, unregister callbacks, and stop input and privileges APIs.
/// </summary>
void OnDisable()
{
#if PLATFORM_LUMIN
MLInput.OnControllerButtonDown -= OnButtonDown;
#endif
if (_isCameraConnected)
{
DisableMLCamera();
}
}
/// <summary>
/// Cannot make the assumption that a privilege is still granted after
/// returning from pause. Return the application to the state where it
/// requests privileges needed and clear out the list of already granted
/// privileges. Also, disable the camera and unregister callbacks.
/// </summary>
void OnApplicationPause(bool pause)
{
if (pause)
{
_appPaused = true;
#if PLATFORM_LUMIN
if (_isCameraConnected)
{
MLResult result = MLCamera.ApplicationPause(_appPaused);
if (!result.IsOk)
{
Debug.LogErrorFormat("Error: RawVideoCapturePreviewExample failed to pause MLCamera, disabling script. Reason: {0}", result);
enabled = false;
return;
}
if (_isCapturing)
{
OnRawVideoCaptureEnded.Invoke();
}
_isCapturing = false;
_captureStartTime = 0;
_isCameraConnected = false;
}
MLInput.OnControllerButtonDown -= OnButtonDown;
#endif
}
}
void OnDestroy()
{
if (_privilegeRequester != null)
{
#if PLATFORM_LUMIN
_privilegeRequester.OnPrivilegesDone -= HandlePrivilegesDone;
#endif
}
OnRawVideoCaptureStarted -= _rawVideoCaptureVisualizer.OnCaptureStarted;
OnRawVideoCaptureEnded -= _rawVideoCaptureVisualizer.OnCaptureEnded;
#if PLATFORM_LUMIN
OnRawVideoDataReceived -= _rawVideoCaptureVisualizer.OnRawCaptureDataReceived;
MLCamera.OnCameraCaptureStarted -= OnCameraCaptureStarted;
MLCamera.OnCameraCaptureCompleted -= OnCameraCaptureCompleted;
MLCamera.OnCameraConnected -= OnCameraConnected;
MLCamera.OnCameraDisconnected -= OnCameraDisconnected;
MLCamera.OnRawVideoFrameAvailableYUV -= OnRawCaptureDataReceived;
#endif
}
/// <summary>
/// Start capturing video.
/// </summary>
public void StartCapture()
{
#if PLATFORM_LUMIN
if (!_isCapturing && _isCameraConnected)
{
MLCamera.StartRawVideoCaptureAsync();
}
else
{
Debug.LogErrorFormat("Error: RawVideoCapturePreviewExample failed to start raw video capture.");
}
#endif
}
/// <summary>
/// Stop capturing video.
/// </summary>
public void EndCapture()
{
if (_isCapturing)
{
#if PLATFORM_LUMIN
MLCamera.StopVideoCaptureAsync();
#endif
}
else
{
Debug.LogError("Error: RawVideoCapturePreviewExample failed to end video capture because the camera is not recording.");
}
}
/// <summary>
/// Update Status Tabin Ui.
/// </summary>
private void UpdateStatusText()
{
_statusText.text = string.Format("<color=#dbfb76><b>Controller Data </b></color>\nStatus: {0}\n", ControllerStatus.Text);
_statusText.text += "\n<color=#dbfb76><b>VideoData</b></color>:\n";
_statusText.text += "Mode: Raw Video Capture\n";
_statusText.text += _intrinsicValuesText;
}
/// <summary>
/// Connects the MLCamera component and instantiates a new instance
/// if it was never created.
/// </summary>
private void EnableMLCamera()
{
#if PLATFORM_LUMIN
MLCamera.ConnectAsync();
#endif
}
/// <summary>
/// Disconnects the MLCamera if it was ever created or connected.
/// Also stops any video recording if active.
/// </summary>
private void DisableMLCamera()
{
#if PLATFORM_LUMIN
MLCamera.DisconnectAsync();
#endif
}
/// <summary>
/// Enable the camera and callbacks. Called once privileges have been granted.
/// </summary>
private void EnableCapture()
{
if (!_hasStarted)
{
EnableMLCamera();
#if PLATFORM_LUMIN
MLInput.OnControllerButtonDown += OnButtonDown;
#endif
_hasStarted = true;
}
}
#if PLATFORM_LUMIN
/// <summary>
/// Responds to privilege requester result.
/// </summary>
/// <param name="result"/>
private void HandlePrivilegesDone(MLResult result)
{
if (!result.IsOk)
{
Debug.LogErrorFormat("Error: RawVideoCapturePreviewExample failed to get all requested privileges, disabling script. Reason: {0}", result);
enabled = false;
return;
}
Debug.Log("Succeeded in requesting all privileges");
// Called here because it needs privileges to be granted first on resume by MLPrivilegeRequesterBehavior.
if (_appPaused)
{
_appPaused = false;
result = MLCamera.ApplicationPause(_appPaused);
if (!result.IsOk)
{
Debug.LogErrorFormat("Error: RawVideoCapturePreviewExample failed to resume MLCamera, disabling script. Reason: {0}", result);
enabled = false;
return;
}
_isCameraConnected = true;
MLInput.OnControllerButtonDown += OnButtonDown;
}
else
{
EnableCapture();
}
}
/// <summary>
/// Handles the event for raw capture data recieved, and forwards it to any listeners.
/// </summary>
/// <param name="extras">Contains timestamp to use with GetFramePose, also forwarded to listeners.</param>
/// <param name="frameData">Forwarded to listeners.</param>
/// <param name="frameMetadata">Forwarded to listeners.</param>
private void OnRawCaptureDataReceived(MLCamera.ResultExtras extras, MLCamera.YUVFrameInfo frameData, MLCamera.FrameMetadata frameMetadata)
{
OnRawVideoDataReceived?.Invoke(extras, frameData, frameMetadata);
}
#endif
/// <summary>
/// Handles the event for button down. Starts or stops recording.
/// </summary>
/// <param name="controllerId">The id of the controller.</param>
/// <param name="button">The button that is being pressed.</param>
private void OnButtonDown(byte controllerId, MLInput.Controller.Button button)
{
if (_controllerConnectionHandler.IsControllerValid(controllerId) && MLInput.Controller.Button.Bumper == button)
{
if (!_isCapturing)
{
StartCapture();
}
else
{
EndCapture();
}
}
}
/// <summary>
/// Setup the text field for camera intrinsic values.
/// Precondition: MLCamera must be successfully started.
/// </summary>
void SetupCameraIntrinsics()
{
#if PLATFORM_LUMIN
MLCVCamera.IntrinsicCalibrationParameters parameters;
MLResult result = MLCVCamera.GetIntrinsicCalibrationParameters(out parameters);
if (result.IsOk)
{
_intrinsicValuesText = CalibrationParametersToString(parameters);
}
else
{
Debug.LogErrorFormat("Error: RawVideoCapturePreviewExample failed to GetIntrinsicCalibrationParameters. Reason: {0}", result);
}
#endif
}
#if PLATFORM_LUMIN
/// <summary>
/// Convert camera calibration parameters to a string.
/// </summary>
/// <param name="parameters">The camera calibration values to pull from.</param>
static string CalibrationParametersToString(MLCVCamera.IntrinsicCalibrationParameters parameters)
{
StringBuilder b = new StringBuilder();
b.AppendFormat("\n <color=#dbfb76><b>Camera Intrinsic Values:</b></color>")
.AppendFormat("\n Width: {0}", parameters.Width)
.AppendFormat("\n Height: {0}", parameters.Height)
.AppendFormat("\n Focal Length: {0}", parameters.FocalLength)
.AppendFormat("\n Principal Point: {0}", parameters.PrincipalPoint)
.AppendFormat("\n FOV: {0}", parameters.FOV)
.AppendFormat("\n Distortion Coeff:");
for (int i = 0; i < parameters.Distortion.Length; ++i)
{
b.AppendFormat("\n [{0}]: {1}", i, parameters.Distortion[i]);
}
return b.ToString();
}
#endif
}
}