unsafe void TryCreateNdiRecv() { if (_ndiFind == IntPtr.Zero) { return; } // NDI source enumeration var count = (System.UInt32) 0; var sources = NDIlib.find_get_current_sources(_ndiFind, ref count); if (count == 0) { return; } // First source entry var source = UnsafeUtility.ReadArrayElement <NDIlib.source_t>((void *)sources, 0); var name = Marshal.PtrToStringAnsi(source.p_ndi_name); Debug.Log($"Sender found: {name}"); // Recv instantiation var opt = new NDIlib.recv_create_v3_t { bandwidth = NDIlib.recv_bandwidth_e.recv_bandwidth_highest, color_format = NDIlib.recv_color_format_e.recv_color_format_fastest, source_to_connect_to = source }; _ndiRecv = NDIlib.recv_create_v3(ref opt); }
// connect to an NDI source in our Dictionary by name public void Connect(Source source) { // just in case we're already connected // _delegateUpdateDynImage = UpdateDynImage; Disconnect(); // Sanity if (source == null || (String.IsNullOrEmpty(source.Name))) { return; } // a source_t to describe the source to connect to. NDIlib.source_t source_t = new NDIlib.source_t() { p_ndi_name = UTF.StringToUtf8(source.Name) }; // make a description of the receiver we want NDIlib.recv_create_v3_t recvDescription = new NDIlib.recv_create_v3_t() { // the source we selected source_to_connect_to = source_t, // we want BGRA frames for this example color_format = NDIlib.recv_color_format_e.recv_color_format_BGRX_BGRA, // we want full quality - for small previews or limited bandwidth, choose lowest bandwidth = NDIlib.recv_bandwidth_e.recv_bandwidth_highest, // let NDIlib deinterlace for us if needed allow_video_fields = false }; // create a new instance connected to this source _recvInstancePtr = NDIlib.recv_create_v3(ref recvDescription); // free the memory we allocated with StringToUtf8 Marshal.FreeHGlobal(source_t.p_ndi_name); // did it work? System.Diagnostics.Debug.Assert(_recvInstancePtr != IntPtr.Zero, "Failed to create NDI receive instance."); if (_recvInstancePtr != IntPtr.Zero) { // We are now going to mark this source as being on program output for tally purposes (but not on preview) SetTallyIndicators(true, false); _source = source; // start up a thread to receive on _receiveThread = new Thread(ReceiveThreadProc) { IsBackground = true, Name = "NdiReceiveThread" }; _receiveThread.Start(); } }
// connect to an NDI source in our Dictionary by name public void Connect(NDIlib.recv_color_format_e colorFormat) { // just in case we're already connected Disconnect(); // a source_t to describe the source to connect to. NDIlib.source_t source_t = new NDIlib.source_t() { p_ndi_name = UTF.StringToUtf8(_sourceName) }; // make a description of the receiver we want NDIlib.recv_create_v3_t recvDescription = new NDIlib.recv_create_v3_t() { // the source we selected source_to_connect_to = source_t, // we want BGRA frames for this example color_format = colorFormat, // we want full quality - for small previews or limited bandwidth, choose lowest bandwidth = _videoEnabled ? NDIlib.recv_bandwidth_e.recv_bandwidth_highest : NDIlib.recv_bandwidth_e.recv_bandwidth_audio_only, // let NDIlib deinterlace for us if needed allow_video_fields = false, // The name of the NDI receiver to create. This is a NULL terminated UTF8 string and should be // the name of receive channel that you have. This is in many ways symettric with the name of // senders, so this might be "Channel 1" on your system. p_ndi_recv_name = UTF.StringToUtf8(_receiverName) }; // create a new instance connected to this source _recvInstancePtr = NDIlib.recv_create_v3(ref recvDescription); // free the memory we allocated with StringToUtf8 Marshal.FreeHGlobal(source_t.p_ndi_name); Marshal.FreeHGlobal(recvDescription.p_ndi_recv_name); // did it work? System.Diagnostics.Debug.Assert(_recvInstancePtr != IntPtr.Zero, "Failed to create NDI receive instance."); if (_recvInstancePtr != IntPtr.Zero) { // We are now going to mark this source as being on program output for tally purposes (but not on preview) SetTallyIndicators(true, false); // start up a thread to receive on _receiveThread = new Thread(ReceiveThreadProc) { IsBackground = true, Name = "NdiExampleReceiveThread" }; _receiveThread.Start(); } }
unsafe void Update() { if (_findInstance != IntPtr.Zero) { UInt32 count = 0; var sources = NDIlib.find_get_current_sources(_findInstance, ref count); if (count == 0) { return; } var source = UnsafeUtility.ReadArrayElement <NDIlib.source_t>((void *)sources, 0); Debug.Log("Sender found: " + Marshal.PtrToStringAnsi(source.p_ndi_name)); var recvOptions = new NDIlib.recv_create_v3_t { source_to_connect_to = source, color_format = NDIlib.recv_color_format_e.recv_color_format_fastest, bandwidth = NDIlib.recv_bandwidth_e.recv_bandwidth_highest }; _recvInstance = NDIlib.recv_create_v3(ref recvOptions); NDIlib.find_destroy(_findInstance); _findInstance = IntPtr.Zero; } if (_recvInstance != IntPtr.Zero) { var frame = new NDIlib.video_frame_v2_t(); var type = NDIlib.recv_capture_v2(_recvInstance, ref frame, IntPtr.Zero, IntPtr.Zero, 0); if (type != NDIlib.frame_type_e.frame_type_video) { return; } UpdateTexture(frame.xres, frame.yres, frame.p_data); NDIlib.recv_free_video_v2(_recvInstance, ref frame); } }
// connect to an NDI source in our Dictionary by name void Connect(Source source) { if (FInReceiverName.SliceCount == 0 || String.IsNullOrEmpty(FInReceiverName[0])) { FLogger.Log(LogType.Error, "receiverName can not be null or empty."); } // just in case we're already connected Disconnect(); // Sanity if (source == null || String.IsNullOrEmpty(source.Name)) { return; } // a source_t to describe the source to connect to. NDIlib.source_t source_t = new NDIlib.source_t() { p_ndi_name = UTF.StringToUtf8(source.Name) }; // make a description of the receiver we want NDIlib.recv_create_v3_t recvDescription = new NDIlib.recv_create_v3_t() { // the source we selected source_to_connect_to = source_t, // we want BGRA frames for this example //color_format = NDIlib.recv_color_format_e.recv_color_format_BGRX_BGRA, color_format = (NDIlib.recv_color_format_e)Enum.ToObject(typeof(NDIlib.recv_color_format_e), (int)FInColorFormat[0]), // we want full quality - for small previews or limited bandwidth, choose lowest //bandwidth = NDIlib.recv_bandwidth_e.recv_bandwidth_highest, bandwidth = (NDIlib.recv_bandwidth_e)Enum.ToObject(typeof(NDIlib.recv_bandwidth_e), (int)FInBandwidth[0]), // let NDIlib deinterlace for us if needed allow_video_fields = false, // The name of the NDI receiver to create. This is a NULL terminated UTF8 string and should be // the name of receive channel that you have. This is in many ways symettric with the name of // senders, so this might be "Channel 1" on your system. p_ndi_recv_name = UTF.StringToUtf8(FInReceiverName[0]) }; // create a new instance connected to this source _recvInstancePtr = NDIlib.recv_create_v3(ref recvDescription); // free the memory we allocated with StringToUtf8 Marshal.FreeHGlobal(source_t.p_ndi_name); Marshal.FreeHGlobal(recvDescription.p_ndi_recv_name); // did it work? //System.Diagnostics.Debug.Assert(_recvInstancePtr != IntPtr.Zero, "Failed to create NDI receive instance."); if (_recvInstancePtr == IntPtr.Zero) { FLogger.Log(LogType.Warning, "Failed to create NDI receive instance."); } if (_recvInstancePtr != IntPtr.Zero) { // We are now going to mark this source as being on program output for tally purposes (but not on preview) SetTallyIndicators(true, false); // start up a thread to receive on _receiveThread = new Thread(ReceiveThreadProc) { IsBackground = true, Name = "NDIReceiveThread" }; _receiveThread.Start(); } }
// connect to an NDI source in our Dictionary by name private void Connect(Source source) { if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) { return; } if (String.IsNullOrEmpty(ReceiverName)) { throw new ArgumentException("sourceName can not be null or empty.", ReceiverName); } using (var volOut = new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Shared, 50)) _volume = volOut.Volume; // just in case we're already connected Disconnect(); // before we are connected, we need to set up our image // it's bad practice to do this in the constructor if (Child == null) { Child = VideoSurface; } // Sanity if (source == null || String.IsNullOrEmpty(source.Name)) { return; } // a source_t to describe the source to connect to. NDIlib.source_t source_t = new NDIlib.source_t() { p_ndi_name = UTF.StringToUtf8(source.Name) }; // make a description of the receiver we want NDIlib.recv_create_v3_t recvDescription = new NDIlib.recv_create_v3_t() { // the source we selected source_to_connect_to = source_t, // we want BGRA frames for this example color_format = NDIlib.recv_color_format_e.recv_color_format_BGRX_BGRA, // we want full quality - for small previews or limited bandwidth, choose lowest bandwidth = _lowBandwidth ? NDIlib.recv_bandwidth_e.recv_bandwidth_lowest : NDIlib.recv_bandwidth_e.recv_bandwidth_highest, // let NDIlib deinterlace for us if needed allow_video_fields = false, // The name of the NDI receiver to create. This is a NULL terminated UTF8 string and should be // the name of receive channel that you have. This is in many ways symettric with the name of // senders, so this might be "Channel 1" on your system. p_ndi_recv_name = UTF.StringToUtf8(ReceiverName) }; // create a new instance connected to this source _recvInstancePtr = NDIlib.recv_create_v3(ref recvDescription); // free the memory we allocated with StringToUtf8 Marshal.FreeHGlobal(source_t.p_ndi_name); Marshal.FreeHGlobal(recvDescription.p_ndi_recv_name); // did it work? System.Diagnostics.Debug.Assert(_recvInstancePtr != IntPtr.Zero, "Failed to create NDI receive instance."); if (_recvInstancePtr != IntPtr.Zero) { // We are now going to mark this source as being on program output for tally purposes (but not on preview) SetTallyIndicators(true, false); // start up a thread to receive on _receiveThread = new Thread(ReceiveThreadProc) { IsBackground = true, Name = "NdiExampleReceiveThread" }; _receiveThread.Start(); } }