-
Notifications
You must be signed in to change notification settings - Fork 118
/
Recorder.cs
440 lines (360 loc) · 12 KB
/
Recorder.cs
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
440
/*
* Copyright (c) 2015 Thomas Hourdel
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Moments.Encoder;
using ThreadPriority = System.Threading.ThreadPriority;
namespace Moments
{
using UnityObject = UnityEngine.Object;
public enum RecorderState
{
Recording,
Paused,
PreProcessing
}
[AddComponentMenu("Miscellaneous/Moments Recorder")]
[RequireComponent(typeof(Camera)), DisallowMultipleComponent]
public sealed class Recorder : MonoBehaviour
{
#region Exposed fields
// These fields aren't public, the user shouldn't modify them directly as they can't break
// everything if not used correctly. Use Setup() instead.
[SerializeField, Min(8)]
int m_Width = 320;
[SerializeField, Min(8)]
int m_Height = 200;
[SerializeField]
bool m_AutoAspect = true;
[SerializeField, Range(1, 30)]
int m_FramePerSecond = 15;
[SerializeField, Min(-1)]
int m_Repeat = 0;
[SerializeField, Range(1, 100)]
int m_Quality = 15;
[SerializeField, Min(0.1f)]
float m_BufferSize = 3f;
#endregion
#region Public fields
/// <summary>
/// Current state of the recorder.
/// </summary>
public RecorderState State { get; private set; }
/// <summary>
/// The folder to save the gif to. No trailing slash.
/// </summary>
public string SaveFolder { get; set; }
/// <summary>
/// Sets the worker threads priority. This will only affect newly created threads (on save).
/// </summary>
public ThreadPriority WorkerPriority = ThreadPriority.BelowNormal;
/// <summary>
/// Returns the estimated VRam used (in MB) for recording.
/// </summary>
public float EstimatedMemoryUse
{
get
{
float mem = m_FramePerSecond * m_BufferSize;
mem *= m_Width * m_Height * 4;
mem /= 1024 * 1024;
return mem;
}
}
#endregion
#region Delegates
/// <summary>
/// Called when the pre-processing step has finished.
/// </summary>
public Action OnPreProcessingDone;
/// <summary>
/// Called by each worker thread every time a frame is processed during the save process.
/// The first parameter holds the worker ID and the second one a value in range [0;1] for
/// the actual progress. This callback is probably not thread-safe, use at your own risks.
/// </summary>
public Action<int, float> OnFileSaveProgress;
/// <summary>
/// Called once a gif file has been saved. The first parameter will hold the worker ID and
/// the second one the absolute file path.
/// </summary>
public Action<int, string> OnFileSaved;
#endregion
#region Internal fields
int m_MaxFrameCount;
float m_Time;
float m_TimePerFrame;
Queue<RenderTexture> m_Frames;
RenderTexture m_RecycledRenderTexture;
ReflectionUtils<Recorder> m_ReflectionUtils;
#endregion
#region Public API
/// <summary>
/// Initializes the component. Use this if you need to change the recorder settings in a script.
/// This will flush the previously saved frames as settings can't be changed while recording.
/// </summary>
/// <param name="autoAspect">Automatically compute height from the current aspect ratio</param>
/// <param name="width">Width in pixels</param>
/// <param name="height">Height in pixels</param>
/// <param name="fps">Recording FPS</param>
/// <param name="bufferSize">Maximum amount of seconds to record to memory</param>
/// <param name="repeat">-1: no repeat, 0: infinite, >0: repeat count</param>
/// <param name="quality">Quality of color quantization (conversion of images to the maximum
/// 256 colors allowed by the GIF specification). Lower values (minimum = 1) produce better
/// colors, but slow processing significantly. Higher values will speed up the quantization
/// pass at the cost of lower image quality (maximum = 100).</param>
public void Setup(bool autoAspect, int width, int height, int fps, float bufferSize, int repeat, int quality)
{
if (State == RecorderState.PreProcessing)
{
Debug.LogWarning("Attempting to setup the component during the pre-processing step.");
return;
}
// Start fresh
FlushMemory();
// Set values and validate them
m_AutoAspect = autoAspect;
m_ReflectionUtils.ConstrainMin(x => x.m_Width, width);
if (!autoAspect)
m_ReflectionUtils.ConstrainMin(x => x.m_Height, height);
m_ReflectionUtils.ConstrainRange(x => x.m_FramePerSecond, fps);
m_ReflectionUtils.ConstrainMin(x => x.m_BufferSize, bufferSize);
m_ReflectionUtils.ConstrainMin(x => x.m_Repeat, repeat);
m_ReflectionUtils.ConstrainRange(x => x.m_Quality, quality);
// Ready to go
Init();
}
/// <summary>
/// Pauses recording.
/// </summary>
public void Pause()
{
if (State == RecorderState.PreProcessing)
{
Debug.LogWarning("Attempting to pause recording during the pre-processing step. The recorder is automatically paused when pre-processing.");
return;
}
State = RecorderState.Paused;
}
/// <summary>
/// Starts or resumes recording. You can't resume while it's pre-processing data to be saved.
/// </summary>
public void Record()
{
if (State == RecorderState.PreProcessing)
{
Debug.LogWarning("Attempting to resume recording during the pre-processing step.");
return;
}
State = RecorderState.Recording;
}
/// <summary>
/// Clears all saved frames from memory and starts fresh.
/// </summary>
public void FlushMemory()
{
if (State == RecorderState.PreProcessing)
{
Debug.LogWarning("Attempting to flush memory during the pre-processing step.");
return;
}
Init();
if (m_RecycledRenderTexture != null)
Flush(m_RecycledRenderTexture);
if (m_Frames == null)
return;
foreach (RenderTexture rt in m_Frames)
Flush(rt);
m_Frames.Clear();
}
/// <summary>
/// Saves the stored frames to a gif file. The filename will automatically be generated.
/// Recording will be paused and won't resume automatically. You can use the
/// <code>OnPreProcessingDone</code> callback to be notified when the pre-processing
/// step has finished.
/// </summary>
public void Save()
{
Save(GenerateFileName());
}
/// <summary>
/// Saves the stored frames to a gif file. If the filename is null or empty, an unique one
/// will be generated. You don't need to add the .gif extension to the name. Recording will
/// be paused and won't resume automatically. You can use the <code>OnPreProcessingDone</code>
/// callback to be notified when the pre-processing step has finished.
/// </summary>
/// <param name="filename">File name without extension</param>
public void Save(string filename)
{
if (State == RecorderState.PreProcessing)
{
Debug.LogWarning("Attempting to save during the pre-processing step.");
return;
}
if (m_Frames.Count == 0)
{
Debug.LogWarning("Nothing to save. Maybe you forgot to start the recorder ?");
return;
}
State = RecorderState.PreProcessing;
if (string.IsNullOrEmpty(filename))
filename = GenerateFileName();
StartCoroutine(PreProcess(filename));
}
#endregion
#region Unity events
void Awake()
{
m_ReflectionUtils = new ReflectionUtils<Recorder>(this);
m_Frames = new Queue<RenderTexture>();
Init();
}
void OnDestroy()
{
FlushMemory();
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (State != RecorderState.Recording)
{
Graphics.Blit(source, destination);
return;
}
m_Time += Time.unscaledDeltaTime;
if (m_Time >= m_TimePerFrame)
{
// Limit the amount of frames stored in memory
if (m_Frames.Count >= m_MaxFrameCount)
m_RecycledRenderTexture = m_Frames.Dequeue();
m_Time -= m_TimePerFrame;
// Frame data
RenderTexture rt = m_RecycledRenderTexture;
m_RecycledRenderTexture = null;
if (rt == null)
{
rt = new RenderTexture(m_Width, m_Height, 0, RenderTextureFormat.ARGB32);
rt.wrapMode = TextureWrapMode.Clamp;
rt.filterMode = FilterMode.Bilinear;
rt.anisoLevel = 0;
}
Graphics.Blit(source, rt);
m_Frames.Enqueue(rt);
}
Graphics.Blit(source, destination);
}
#endregion
#region Methods
// Used to reset internal values, called on Start(), Setup() and FlushMemory()
void Init()
{
State = RecorderState.Paused;
ComputeHeight();
m_MaxFrameCount = Mathf.RoundToInt(m_BufferSize * m_FramePerSecond);
m_TimePerFrame = 1f / m_FramePerSecond;
m_Time = 0f;
// Make sure the output folder is set or use the default one
if (string.IsNullOrEmpty(SaveFolder))
{
#if UNITY_EDITOR
SaveFolder = Application.dataPath; // Defaults to the asset folder in the editor for faster access to the gif file
#else
SaveFolder = Application.persistentDataPath;
#endif
}
}
// Automatically computes height from the current aspect ratio if auto aspect is set to true
public void ComputeHeight()
{
if (!m_AutoAspect)
return;
m_Height = Mathf.RoundToInt(m_Width / GetComponent<Camera>().aspect);
}
void Flush(UnityObject obj)
{
#if UNITY_EDITOR
if (Application.isPlaying)
Destroy(obj);
else
DestroyImmediate(obj);
#else
UnityObject.Destroy(obj);
#endif
}
// Gets a filename : GifCapture-yyyyMMddHHmmssffff
string GenerateFileName()
{
string timestamp = DateTime.Now.ToString("yyyyMMddHHmmssffff");
return "GifCapture-" + timestamp;
}
// Pre-processing coroutine to extract frame data and send everything to a separate worker thread
IEnumerator PreProcess(string filename)
{
string filepath = SaveFolder + "/" + filename + ".gif";
List<GifFrame> frames = new List<GifFrame>(m_Frames.Count);
// Get a temporary texture to read RenderTexture data
Texture2D temp = new Texture2D(m_Width, m_Height, TextureFormat.RGB24, false);
temp.hideFlags = HideFlags.HideAndDontSave;
temp.wrapMode = TextureWrapMode.Clamp;
temp.filterMode = FilterMode.Bilinear;
temp.anisoLevel = 0;
// Process the frame queue
while (m_Frames.Count > 0)
{
GifFrame frame = ToGifFrame(m_Frames.Dequeue(), temp);
frames.Add(frame);
yield return null;
}
// Dispose the temporary texture
Flush(temp);
// Switch the state to pause, let the user choose to keep recording or not
State = RecorderState.Paused;
// Callback
if (OnPreProcessingDone != null)
OnPreProcessingDone();
// Setup a worker thread and let it do its magic
GifEncoder encoder = new GifEncoder(m_Repeat, m_Quality);
encoder.SetDelay(Mathf.RoundToInt(m_TimePerFrame * 1000f));
Worker worker = new Worker(WorkerPriority)
{
m_Encoder = encoder,
m_Frames = frames,
m_FilePath = filepath,
m_OnFileSaved = OnFileSaved,
m_OnFileSaveProgress = OnFileSaveProgress
};
worker.Start();
}
// Converts a RenderTexture to a GifFrame
// Should be fast enough for low-res textures but will tank the framerate at higher res
GifFrame ToGifFrame(RenderTexture source, Texture2D target)
{
RenderTexture.active = source;
target.ReadPixels(new Rect(0, 0, source.width, source.height), 0, 0);
target.Apply();
RenderTexture.active = null;
return new GifFrame() { Width = target.width, Height = target.height, Data = target.GetPixels32() };
}
#endregion
}
}