Tanoda
JPGRecorder.cs
Go to the documentation of this file.
1/*
2* NatCorder
3* Copyright (c) 2020 Yusuf Olokoba.
4*/
5
6using System;
7using System.Collections.Generic;
8using System.IO;
9using System.Runtime.InteropServices;
10using System.Threading.Tasks;
11using UnityEngine;
12using Object = UnityEngine.Object;
13
14namespace NatSuite.Recorders
15{
21 public sealed class JPGRecorder : IMediaRecorder
22 {
23 #region --Client API--
24
28 public (int width, int height) frameSize => (framebuffer.width, framebuffer.height);
29
35 public JPGRecorder(int imageWidth, int imageHeight)
36 {
37 // Save state
38 framebuffer = new Texture2D(imageWidth, imageHeight, TextureFormat.RGBA32, false, false);
39 writeTasks = new List<Task>();
40 // Create directory
41 recordingPath = Internal.Utility.GetPath(string.Empty);
42 Directory.CreateDirectory(recordingPath);
43 }
44
51 public void CommitFrame<T>(T[] pixelBuffer, long timestamp = default) where T : struct
52 {
53 var handle = GCHandle.Alloc(pixelBuffer, GCHandleType.Pinned);
54 CommitFrame(handle.AddrOfPinnedObject(), timestamp);
55 handle.Free();
56 }
57
64 public void CommitFrame(IntPtr nativeBuffer, long timestamp = default)
65 {
66 // Encode immediately
67 framebuffer.LoadRawTextureData(nativeBuffer, framebuffer.width * framebuffer.height * 4);
68 var frameData = framebuffer.EncodeToJPG();
69 // Write out on a worker thread
70 var frameIndex = ++frameCount;
71 writeTasks.Add(Task.Run(() =>
72 File.WriteAllBytes(Path.Combine(recordingPath, $"{frameIndex}.jpg"), frameData)));
73 }
74
78 public void CommitSamples(float[] sampleBuffer = default, long timestamp = default)
79 {
80 }
81
85 public Task<string> FinishWriting()
86 {
87 Object.Destroy(framebuffer);
88 return Task.WhenAll(writeTasks).ContinueWith(_ => recordingPath);
89 }
90
91 #endregion
92
93
94 #region --Operations--
95
96 private readonly Texture2D framebuffer;
97 private readonly string recordingPath;
98 private readonly List<Task> writeTasks;
99 private int frameCount;
100
101 #endregion
102 }
103}
JPG image sequence recorder. This recorder is currently supported on macOS and Windows....
Definition: JPGRecorder.cs:22
JPGRecorder(int imageWidth, int imageHeight)
Create a JPG recorder.
Definition: JPGRecorder.cs:35
Task< string > FinishWriting()
Finish writing and return the path to the recorded media file.
Definition: JPGRecorder.cs:85
void CommitSamples(float[] sampleBuffer=default, long timestamp=default)
This recorder does not support committing audio samples.
Definition: JPGRecorder.cs:78
void CommitFrame< T >(T[] pixelBuffer, long timestamp=default)
Commit a video pixel buffer for encoding. The pixel buffer MUST have an RGBA8888 pixel layout.
Definition: JPGRecorder.cs:51
void CommitFrame(IntPtr nativeBuffer, long timestamp=default)
Commit a video pixel buffer for encoding. The pixel buffer MUST have an RGBA8888 pixel layout.
Definition: JPGRecorder.cs:64
A recorder capable of recording video frames, and optionally audio frames, to a media output....
UnityEngine.Object Object