/
NdiReceiver.cs
197 lines (151 loc) · 5.88 KB
/
NdiReceiver.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
// KlakNDI - NDI plugin for Unity
// https://github.com/keijiro/KlakNDI
using UnityEngine;
namespace Klak.Ndi
{
[ExecuteInEditMode]
[AddComponentMenu("Klak/NDI/NDI Receiver")]
public sealed class NdiReceiver : MonoBehaviour
{
#region Source settings
[SerializeField] string _sourceName;
public string sourceName {
get { return _sourceName; }
set {
if (_sourceName == value) return;
_sourceName = value;
RequestReconnect();
}
}
#endregion
#region Target settings
[SerializeField] RenderTexture _targetTexture;
public RenderTexture targetTexture {
get { return _targetTexture; }
set { _targetTexture = value; }
}
[SerializeField] Renderer _targetRenderer;
public Renderer targetRenderer {
get { return _targetRenderer; }
set { _targetRenderer = value; }
}
[SerializeField] string _targetMaterialProperty = null;
public string targetMaterialProperty {
get { return _targetMaterialProperty; }
set { _targetMaterialProperty = value; }
}
#endregion
#region Runtime properties
RenderTexture _receivedTexture;
public Texture receivedTexture {
get { return _targetTexture != null ? _targetTexture : _receivedTexture; }
}
#endregion
#region Private members
static System.IntPtr _callback;
System.IntPtr _plugin;
Texture2D _sourceTexture;
Material _blitMaterial;
MaterialPropertyBlock _propertyBlock;
#endregion
#region Internal members
internal void RequestReconnect()
{
OnDisable();
}
#endregion
#region MonoBehaviour implementation
void OnDisable()
{
if (_plugin != System.IntPtr.Zero)
{
PluginEntry.DestroyReceiver(_plugin);
_plugin = System.IntPtr.Zero;
}
}
void OnDestroy()
{
Util.Destroy(_blitMaterial);
Util.Destroy(_sourceTexture);
Util.Destroy(_receivedTexture);
}
void Update()
{
if (!PluginEntry.IsAvailable) return;
// Plugin lazy initialization
if (_plugin == System.IntPtr.Zero)
{
_plugin = PluginEntry.CreateReceiver(_sourceName);
if (_plugin == System.IntPtr.Zero) return; // No receiver support
}
// Texture update event invocation with lazy initialization
if (_callback == System.IntPtr.Zero)
_callback = PluginEntry.GetTextureUpdateCallback();
if (_sourceTexture == null)
{
_sourceTexture = new Texture2D(8, 8); // Placeholder
_sourceTexture.hideFlags = HideFlags.DontSave;
}
Util.IssueTextureUpdateEvent
(_callback, _sourceTexture, PluginEntry.GetReceiverID(_plugin));
// Texture information retrieval
var width = PluginEntry.GetFrameWidth(_plugin);
var height = PluginEntry.GetFrameHeight(_plugin);
if (width == 0 || height == 0) return; // Not yet ready
// Source data dimensions
var alpha = PluginEntry.GetFrameFourCC(_plugin) == FourCC.UYVA;
var sw = width / 2;
var sh = height * (alpha ? 3 : 2) / 2;
// Renew the textures when the dimensions are changed.
if (_sourceTexture.width != sw || _sourceTexture.height != sh)
{
Util.Destroy(_sourceTexture);
Util.Destroy(_receivedTexture);
_sourceTexture = new Texture2D(sw, sh, TextureFormat.RGBA32, false, true);
_sourceTexture.hideFlags = HideFlags.DontSave;
_sourceTexture.filterMode = FilterMode.Point;
}
// Blit shader lazy initialization
if (_blitMaterial == null)
{
_blitMaterial = new Material(Shader.Find("Hidden/KlakNDI/Receiver"));
_blitMaterial.hideFlags = HideFlags.DontSave;
}
// Receiver texture lazy initialization
if (_targetTexture == null && _receivedTexture == null)
{
_receivedTexture = new RenderTexture(width, height, 0);
_receivedTexture.hideFlags = HideFlags.DontSave;
}
// Texture format conversion using the blit shader
var receiver = _targetTexture != null ? _targetTexture : _receivedTexture;
Graphics.Blit(_sourceTexture, receiver, _blitMaterial, alpha ? 1 : 0);
receiver.IncrementUpdateCount();
// Renderer override
if (_targetRenderer != null)
{
// Material property block lazy initialization
if (_propertyBlock == null)
_propertyBlock = new MaterialPropertyBlock();
// Read-modify-write
_targetRenderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetTexture(_targetMaterialProperty, receiver);
_targetRenderer.SetPropertyBlock(_propertyBlock);
}
}
#if UNITY_EDITOR
// Invoke update on repaint in edit mode. This is needed to update the
// shared texture without getting the object marked dirty.
void OnRenderObject()
{
if (Application.isPlaying) return;
// Graphic.Blit used in Update will change the current active RT,
// so let us back it up and restore after Update.
var activeRT = RenderTexture.active;
Update();
RenderTexture.active = activeRT;
}
#endif
#endregion
}
}