-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathNdiSender.cs
240 lines (185 loc) · 7.43 KB
/
NdiSender.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
using UnityEngine;
using UnityEngine.Rendering;
namespace Klak.Ndi {
[ExecuteInEditMode]
public sealed partial class NdiSender : MonoBehaviour
{
#region Sender objects
Interop.Send _send;
ReadbackPool _pool;
FormatConverter _converter;
System.Action<AsyncGPUReadbackRequest> _onReadback;
void PrepareSenderObjects()
{
// Game view capture method: Borrow the shared sender instance.
if (_send == null && captureMethod == CaptureMethod.GameView)
_send = SharedInstance.GameViewSend;
// Private object initialization
if (_send == null) _send = Interop.Send.Create(ndiName);
if (_pool == null) _pool = new ReadbackPool();
if (_converter == null) _converter = new FormatConverter(_resources);
if (_onReadback == null) _onReadback = OnReadback;
}
void ReleaseSenderObjects()
{
// Total synchronization: This may cause a frame hiccup, but it's
// needed to dispose the readback buffers safely.
AsyncGPUReadback.WaitAllRequests();
// Game view capture method: Leave the sender instance without
// disposing (we're not the owner) but synchronize it. It's needed to
// dispose the readback buffers safely too.
if (SharedInstance.IsGameViewSend(_send))
{
_send.SendVideoAsync(); // Sync by null-send
_send = null;
}
// Private objet disposal
_send?.Dispose();
_send = null;
_pool?.Dispose();
_pool = null;
_converter?.Dispose();
_converter = null;
// We don't dispose _onReadback because it's reusable.
}
#endregion
#region Capture coroutine for the Texture/GameView capture methods
System.Collections.IEnumerator CaptureCoroutine()
{
for (var eof = new WaitForEndOfFrame(); true;)
{
#if !UNITY_ANDROID || UNITY_EDITOR
// Wait for the end of the frame.
yield return eof;
#else
// Temporary workaround for glitches on Android:
// Process the input at the beginning of the frame instead of EoF.
// I don't know why these glitches occur, but this change solves
// them anyway. I should investigate them further if they reappear.
yield return null;
#endif
PrepareSenderObjects();
// Texture capture method
if (captureMethod == CaptureMethod.Texture && sourceTexture != null)
{
var (w, h) = (sourceTexture.width, sourceTexture.height);
// Pixel format conversion
var buffer = _converter.Encode(sourceTexture, keepAlpha, true);
// Readback entry allocation and request
_pool.NewEntry(w, h, keepAlpha, metadata)
.RequestReadback(buffer, _onReadback);
}
// Game View capture method
if (captureMethod == CaptureMethod.GameView)
{
// Game View screen capture with a temporary RT
var (w, h) = (Screen.width, Screen.height);
var tempRT = RenderTexture.GetTemporary(w, h, 0);
ScreenCapture.CaptureScreenshotIntoRenderTexture(tempRT);
// Pixel format conversion
var buffer = _converter.Encode(tempRT, keepAlpha, false);
RenderTexture.ReleaseTemporary(tempRT);
// Readback entry allocation and request
_pool.NewEntry(w, h, keepAlpha, metadata)
.RequestReadback(buffer, _onReadback);
}
}
}
#endregion
#region SRP camera capture callback for the Camera capture method
void OnCameraCapture(RenderTargetIdentifier source, CommandBuffer cb)
{
// A SRP may call this callback after object destruction. We can
// exclude those cases by null-checking _attachedCamera.
if (_attachedCamera == null) return;
PrepareSenderObjects();
// Pixel format conversion
var (w, h) = (sourceCamera.pixelWidth, sourceCamera.pixelHeight);
var buffer = _converter.Encode(cb, source, w, h, keepAlpha, true);
// Readback entry allocation and request
_pool.NewEntry(w, h, keepAlpha, metadata)
.RequestReadback(buffer, _onReadback);
}
#endregion
#region GPU readback completion callback
unsafe void OnReadback(AsyncGPUReadbackRequest req)
{
// Readback entry retrieval
var entry = _pool.FindEntry(req.GetData<byte>());
if (entry == null) return;
// Invalid state detection
if (req.hasError || _send == null || _send.IsInvalid || _send.IsClosed)
{
// Do nothing but release the readback entry.
_pool.Free(entry);
return;
}
// Frame data
var frame = new Interop.VideoFrame
{ Width = entry.Width,
Height = entry.Height,
LineStride = entry.Stride,
FourCC = entry.FourCC,
FrameFormat = Interop.FrameFormat.Progressive,
Data = entry.ImagePointer,
Metadata = entry.MetadataPointer };
// Async-send initiation
// This causes a synchronization for the last frame -- i.e., It locks
// the thread if the last frame is still under processing.
_send.SendVideoAsync(frame);
// We don't need the last frame anymore. Free it.
_pool.FreeMarkedEntry();
// Mark this frame to get freed in the next frame.
_pool.Mark(entry);
}
#endregion
#region Component state controller
Camera _attachedCamera;
// Component state reset without NDI object disposal
internal void ResetState(bool willBeActive)
{
// Camera capture coroutine termination
// We use this to kill only a single coroutine. It may sound like
// overkill, but I think there is no side effect in doing so.
StopAllCoroutines();
#if KLAK_NDI_HAS_SRP
// A SRP may call this callback after camera destruction. We can
// exclude those cases by null-checking _attachedCamera.
if (_attachedCamera != null)
CameraCaptureBridge.RemoveCaptureAction(_attachedCamera, OnCameraCapture);
#endif
_attachedCamera = null;
// The following part of code is to activate the subcomponents. We can
// break here if willBeActive is false.
if (!willBeActive) return;
if (captureMethod == CaptureMethod.Camera)
{
#if KLAK_NDI_HAS_SRP
// Camera capture callback setup
if (sourceCamera != null)
CameraCaptureBridge.AddCaptureAction(sourceCamera, OnCameraCapture);
#endif
_attachedCamera = sourceCamera;
}
else
{
// Capture coroutine initiation
StartCoroutine(CaptureCoroutine());
}
}
// Component state reset with NDI object disposal
internal void Restart(bool willBeActivate)
{
ResetState(willBeActivate);
ReleaseSenderObjects();
}
internal void ResetState() => ResetState(isActiveAndEnabled);
internal void Restart() => Restart(isActiveAndEnabled);
#endregion
#region MonoBehaviour implementation
void OnEnable() => ResetState();
void OnDisable() => Restart(false);
void OnDestroy() => Restart(false);
#endregion
}
} // namespace Klak.Ndi