Tanoda
NativeRecorder.cs
Go to the documentation of this file.
1/*
2* NatCorder
3* Copyright (c) 2020 Yusuf Olokoba.
4*/
5
6using System;
7using System.Runtime.InteropServices;
8using System.Threading.Tasks;
9using AOT;
10
12{
13 public sealed class NativeRecorder : IMediaRecorder
14 {
15 #region --IMediaRecorder--
16
17 public (int width, int height) frameSize
18 {
19 get
20 {
21 recorder.FrameSize(out var width, out var height);
22 return (width, height);
23 }
24 }
25
26 public NativeRecorder(Func<Bridge.RecordingHandler, IntPtr, IntPtr> recorderCreator)
27 {
28 recordingTask = new TaskCompletionSource<string>();
29 var handle = GCHandle.Alloc(recordingTask, GCHandleType.Normal);
30 recorder = recorderCreator(OnRecording, (IntPtr) handle);
31 }
32
33 public void CommitFrame<T>(T[] pixelBuffer, long timestamp) where T : struct
34 {
35 var handle = GCHandle.Alloc(pixelBuffer, GCHandleType.Pinned);
36 CommitFrame(handle.AddrOfPinnedObject(), timestamp);
37 handle.Free();
38 }
39
40 public void CommitFrame(IntPtr nativeBuffer, long timestamp)
41 {
42 recorder.CommitFrame(nativeBuffer, timestamp);
43 }
44
45 public void CommitSamples(float[] sampleBuffer, long timestamp)
46 {
47 recorder.CommitSamples(sampleBuffer, sampleBuffer.Length, timestamp);
48 }
49
50 public Task<string> FinishWriting()
51 {
52 recorder.FinishWriting();
53 return recordingTask.Task;
54 }
55
56 #endregion
57
58
59 #region --Operations--
60
61 private readonly IntPtr recorder;
62 private readonly TaskCompletionSource<string> recordingTask;
63
64 [MonoPInvokeCallback(typeof(Bridge.RecordingHandler))]
65 private static void OnRecording(IntPtr context, IntPtr path)
66 {
67 // Get task
68 var handle = (GCHandle) context;
69 var recordingTask = handle.Target as TaskCompletionSource<string>;
70 handle.Free();
71 // Invoke completion task
72 if (path != IntPtr.Zero)
73 recordingTask.SetResult(Marshal.PtrToStringAnsi(path));
74 else
75 recordingTask.SetException(new Exception(@"Recorder failed to finish writing"));
76 }
77
78 #endregion
79 }
80}
Task< string > FinishWriting()
Finish writing and return the path to the recorded media file.
void CommitFrame(IntPtr nativeBuffer, long timestamp)
Commit a video pixel buffer for encoding. The pixel buffer MUST have an RGBA8888 pixel layout.
NativeRecorder(Func< Bridge.RecordingHandler, IntPtr, IntPtr > recorderCreator)
void CommitFrame< T >(T[] pixelBuffer, long timestamp)
Commit a video pixel buffer for encoding. The pixel buffer MUST have an RGBA8888 pixel layout.
void CommitSamples(float[] sampleBuffer, long timestamp)
Commit an audio sample buffer for encoding.
A recorder capable of recording video frames, and optionally audio frames, to a media output....