void Update() { if (_receiver == IntPtr.Zero) { _receiver = PluginEntry.NDI_CreateReceiver(); if (_receiver == IntPtr.Zero) { return; } } var ready = PluginEntry.NDI_ReceiveFrame(_receiver); if (!ready) { return; } var width = PluginEntry.NDI_GetFrameWidth(_receiver); var height = PluginEntry.NDI_GetFrameHeight(_receiver); var data = PluginEntry.NDI_GetFrameData(_receiver); if (_texture != null) { Destroy(_texture); } _texture = new Texture2D(width, height, TextureFormat.BGRA32, false); _texture.LoadRawTextureData(data, width * height * 4); _texture.Apply(); GetComponent <Renderer>().material.mainTexture = _texture; PluginEntry.NDI_FreeFrame(_receiver); }
void OnGUI() { if (_sources == null) { _sources = new IntPtr[128]; } var count = PluginEntry.NDI_RetrieveSourceNames(_sources, _sources.Length); EditorGUILayout.Space(); EditorGUI.indentLevel++; if (count == 0) { EditorGUILayout.LabelField("No source found."); } else { EditorGUILayout.LabelField(count + " source(s) found."); } for (var i = 0; i < count; i++) { var name = Marshal.PtrToStringAnsi(_sources[i]); if (name != null) { EditorGUILayout.LabelField("- " + name); } } EditorGUI.indentLevel--; }
void Update() { while (_frameQueue.Count > 0) { var frame = _frameQueue.Peek(); if (frame.readback.hasError) { Debug.LogWarning("GPU readback error was detected."); _frameQueue.Dequeue(); } else if (frame.readback.done) { var array = frame.readback.GetData <Byte>(); unsafe { PluginEntry.NDI_SendFrame( _instance, (IntPtr)array.GetUnsafeReadOnlyPtr(), frame.width, frame.height ); } _frameQueue.Dequeue(); } else { break; } } }
void OnDisable() { if (_plugin != System.IntPtr.Zero) { PluginEntry.DestroyReceiver(_plugin); _plugin = System.IntPtr.Zero; } }
void OnDestroy() { Destroy(_material); if (_converted != null) { RenderTexture.ReleaseTemporary(_converted); } PluginEntry.NDI_DestroySender(_plugin); }
void OnDestroy() { if (_tempRT != null) { RenderTexture.ReleaseTemporary(_tempRT); } Destroy(_material); PluginEntry.NDI_DestroySender(_instance); }
// Scan available NDI sources and store their names into the given // collection object. public static void GetSourceNames(ICollection <string> store) { store.Clear(); var count = PluginEntry.RetrieveSourceNames(_pointers, _pointers.Length); for (var i = 0; i < count; i++) { store.Add(Marshal.PtrToStringAnsi(_pointers[i])); } }
void ProcessQueue() { while (_frameQueue.Count > 0) { var frame = _frameQueue.Peek(); // Edit mode: Wait for readback completion every frame. if (!Application.isPlaying) { frame.readback.WaitForCompletion(); } // Skip error frames. if (frame.readback.hasError) { Debug.LogWarning("GPU readback error was detected."); _frameQueue.Dequeue(); continue; } // Break when found a frame that hasn't been read back yet. if (!frame.readback.done) { break; } // Okay, we're going to send this frame. // Lazy initialization of the plugin sender instance. if (_plugin == IntPtr.Zero) { _plugin = PluginEntry.CreateSender(this.Name); } // Feed the frame data to the sender. It encodes/sends the // frame asynchronously. unsafe { PluginEntry.SendFrame( _plugin, (IntPtr)frame.readback.GetData <Byte>().GetUnsafeReadOnlyPtr(), frame.width, frame.height, frame.format ); } // Done. Remove the frame from the queue. _frameQueue.Dequeue(); } // Edit mode: We're not sure when the readback buffer will be // disposed, so let's synchronize with the sender to prevent it // from accessing disposed memory area. if (!Application.isPlaying && _plugin != IntPtr.Zero) { PluginEntry.SyncSender(_plugin); } }
// Scan available NDI sources and return their names via a newly // allocated string array. public static string[] GetSourceNames() { var count = PluginEntry.RetrieveSourceNames(_pointers, _pointers.Length); var names = new string [count]; for (var i = 0; i < count; i++) { names[i] = Marshal.PtrToStringAnsi(_pointers[i]); } return(names); }
void OnDestroy() { if (_receiver != IntPtr.Zero) { PluginEntry.NDI_DestroyReceiver(_receiver); } if (_texture != null) { Destroy(_texture); } }
void OnDestroy() { Destroy(_material); _commandBuffer.Dispose(); Destroy(_sourceTexture); if (_converted != null) { RenderTexture.ReleaseTemporary(_converted); } if (_plugin != IntPtr.Zero) { PluginEntry.NDI_DestroyReceiver(_plugin); } }
void OnDisable() { if (_commandBuffer != null) { _commandBuffer.Dispose(); _commandBuffer = null; } if (_plugin != IntPtr.Zero) { PluginEntry.NDI_DestroyReceiver(_plugin); _plugin = IntPtr.Zero; } }
void OnDisable() { if (_converted != null) { RenderTexture.ReleaseTemporary(_converted); _converted = null; } if (_plugin != IntPtr.Zero) { PluginEntry.DestroySender(_plugin); _plugin = IntPtr.Zero; } #if UNITY_EDITOR _delayUpdateAdded = false; #endif }
IEnumerator Start() { _material = new Material(_shader); _frameQueue = new Queue <Frame>(4); _plugin = PluginEntry.NDI_CreateSender(_senderName); _hasCamera = (GetComponent <Camera>() != null); // Synchronize with async send at the end of every frame. var wait = new WaitForEndOfFrame(); while (true) { yield return(wait); if (enabled) { PluginEntry.NDI_SyncSender(_plugin); } } }
IEnumerator Start() { _hasCamera = (GetComponent <Camera>() != null); // Only run the sync coroutine in the play mode. if (!Application.isPlaying) { yield break; } // Synchronize with the async sender at the end of every frame. for (var wait = new WaitForEndOfFrame();;) { yield return(wait); if (enabled && _plugin != IntPtr.Zero) { PluginEntry.SyncSender(_plugin); } } }
void Update() { // Process the readback queue. while (_frameQueue.Count > 0) { var frame = _frameQueue.Peek(); if (frame.readback.hasError) { Debug.LogWarning("GPU readback error was detected."); _frameQueue.Dequeue(); } else if (frame.readback.done) { var array = frame.readback.GetData <Byte>(); unsafe { PluginEntry.NDI_SendFrame( _plugin, (IntPtr)array.GetUnsafeReadOnlyPtr(), frame.width, frame.height, frame.alpha ? FourCC.UYVA : FourCC.UYVY ); } _frameQueue.Dequeue(); } else { break; } } // Request frame readback when in render texture mode. if (!_hasCamera && _sourceTexture != null) { QueueFrame(_sourceTexture); } }
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/ReceiverOneHalf")); _blitMaterial.hideFlags = HideFlags.DontSave; } if (_blitMaterialTwo == null) { _blitMaterialTwo = new Material(Shader.Find("Hidden/KlakNDI/ReceiverSecondHalf")); _blitMaterialTwo.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; var receiverTwo = _targetTextureTwo != null ? _targetTextureTwo : _receivedTexture; Graphics.Blit(_sourceTexture, receiver, _blitMaterial, alpha ? 1 : 0); Graphics.Blit(_sourceTexture, receiverTwo, _blitMaterialTwo, 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); } }
void Update() { if (!PluginEntry.IsAvailable) { return; } // Plugin lazy initialization if (_plugin == System.IntPtr.Zero) { _plugin = PluginEntry.CreateReceiver(_sourceName, _dataFormat); 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); var frameFormat = PluginEntry.GetFrameFourCC(_plugin); this.recievedResolution.x = width; this.recievedResolution.y = height; this.recievedFormat = frameFormat; if (width == 0 || height == 0) { return; // Not yet ready } // Source data dimensions var sw = width; var sh = height; var isRGBA = true; var alpha = true; switch (frameFormat) { case FourCC.UYVA: case FourCC.UYVY: { alpha = frameFormat == FourCC.UYVA; sw = width / 2; sh = height * (alpha ? 3 : 2) / 2; isRGBA = false; } break; } // 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.ARGB32, false, true); _sourceTexture.hideFlags = HideFlags.DontSave; _sourceTexture.filterMode = FilterMode.Point; _sourceTexture.anisoLevel = 0; } // 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, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); _receivedTexture.hideFlags = HideFlags.DontSave; _receivedTexture.useMipMap = false; _receivedTexture.filterMode = FilterMode.Point; _receivedTexture.anisoLevel = 0; _receivedTexture.antiAliasing = 1; _receivedTexture.autoGenerateMips = false; _receivedTexture.Create(); } // Texture format conversion using the blit shader var receiver = _targetTexture != null ? _targetTexture : _receivedTexture; if (isRGBA) { if (_invertY) { Graphics.Blit(_sourceTexture, receiver, _blitMaterial, 2);//pass 2 is invert y pass } else { Graphics.Blit(_sourceTexture, receiver); } } else { 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); } }
void Update() { if (!PluginEntry.IsAvailable) { return; } _dataTime = 1.0f / _FPS; _dataTime2 = 1.0f / _FPS2; // 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; } // Receiver texture lazy initialization if (_targetTextureOne == null && _receivedTexture == null) { _receivedTexture = new RenderTexture(width, height, 0); _receivedTexture.hideFlags = HideFlags.DontSave; } if (_backTexture == null) { _backTexture = new RenderTexture(width, height, 0); _backTexture.hideFlags = HideFlags.DontSave; } if (_frontTexture == null) { _frontTexture = new RenderTexture(width, height, 0); _frontTexture.hideFlags = HideFlags.DontSave; } if (_blendMaterial == null) { _blendMaterial = new Material(Shader.Find("Hidden/Marrow/BlendReceiver")); _blendMaterial.hideFlags = HideFlags.DontSave; _blendMaterial.SetTexture("_BackTex", _backTexture); } _timer += Time.deltaTime; _timer2 += Time.deltaTime; if (_timer > _dataTime) { _showBack = !_showBack; _timer = 0.0f; // Debug.Log("Blit!!"); if (_showBack) { Graphics.Blit(_sourceTexture, _frontTexture, _blendMaterial, 0); } else { Graphics.Blit(_sourceTexture, _backTexture, _blendMaterial, 0); } } if (!_showBack) { _blendFactor = _timer / _dataTime; } else { _blendFactor = 1 - (_timer / _dataTime); } _blendMaterial.SetFloat("_BlendFactor", _blendFactor); Graphics.Blit(_frontTexture, _targetTextureOne, _blendMaterial, 1); Graphics.Blit(_frontTexture, _targetTextureThree, _blendMaterial, 3); if (_timer2 > _dataTime2) { _timer2 = 0; Graphics.Blit(_sourceTexture, _targetTextureTwo, _blendMaterial, 2); _targetTextureTwo.IncrementUpdateCount(); } // Texture format conversion using the blit shader _targetTextureOne.IncrementUpdateCount(); _targetTextureThree.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, _targetTextureOne); _propertyBlock.SetTexture(_targetMaterialProperty, _targetTextureTwo); _propertyBlock.SetTexture(_targetMaterialProperty, _targetTextureThree); _targetRenderer.SetPropertyBlock(_propertyBlock); } }
void Update() { // Plugin lazy initialization if (_plugin == IntPtr.Zero) { _plugin = PluginEntry.NDI_TryOpenSourceNamedLike(_nameFilter); if (_plugin == IntPtr.Zero) { return; } } // Invoke the texture update callback in the plugin. _commandBuffer.IssuePluginCustomTextureUpdate( PluginEntry.NDI_GetTextureUpdateCallback(), _sourceTexture, PluginEntry.NDI_GetReceiverID(_plugin) ); Graphics.ExecuteCommandBuffer(_commandBuffer); _commandBuffer.Clear(); // Check the frame dimensions. var width = PluginEntry.NDI_GetFrameWidth(_plugin); var height = PluginEntry.NDI_GetFrameHeight(_plugin); if (width == 0 || height == 0) { return; // not yet ready } // Calculate the source data dimensions. var alpha = PluginEntry.NDI_GetFrameFourCC(_plugin) == FourCC.UYVA; var sw = width / 2; var sh = height * (alpha ? 3 : 2) / 2; // Renew the texture when the dimensions are changed. if (_sourceTexture.width != sw || _sourceTexture.height != sh) { Destroy(_sourceTexture); _sourceTexture = new Texture2D(sw, sh, TextureFormat.RGBA32, false, true); _sourceTexture.filterMode = FilterMode.Point; } // Update external objects. if (_converted != null) { RenderTexture.ReleaseTemporary(_converted); } if (_targetTexture != null) { Graphics.Blit(_sourceTexture, _targetTexture, _material, alpha ? 1 : 0); } else { _converted = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32); Graphics.Blit(_sourceTexture, _converted, _material, alpha ? 1 : 0); } if (_targetRenderer != null) { _targetRenderer.GetPropertyBlock(_propertyBlock); _propertyBlock.SetTexture(_targetMaterialProperty, receivedTexture); _targetRenderer.SetPropertyBlock(_propertyBlock); } }
void Start() { _material = new Material(_shader); _instance = PluginEntry.NDI_CreateSender(gameObject.name); _frameQueue = new Queue <Frame>(4); }