Exemple #1
0
        /// <summary>
        /// Method accepts address in form ndi://ip_address:port and ndi://ip_address:ndi_name
        /// </summary>
        /// <param name="sourceUrl"></param>
        public void SetSource(string sourceUrl)
        {
            if (string.IsNullOrWhiteSpace(sourceUrl))
            {
                return;
            }
            Uri sourceUri = null;

            if ((Uri.TryCreate(sourceUrl, UriKind.Absolute, out sourceUri) && sourceUri.Scheme == "ndi") ||
                string.Equals(sourceUrl.Substring(0, sourceUrl.IndexOf(':')), "ndi", StringComparison.InvariantCultureIgnoreCase))
            {
                Application.Current.Dispatcher.BeginInvoke((Action) delegate
                {
                    if (_ndiSources == null)
                    {
                        return;
                    }
                    string source = null;
                    if (sourceUri != null)
                    {
                        source = _ndiSources.FirstOrDefault(s => Ndi.Utf8ToString(s.Value.p_ip_address) == sourceUri.Host).Key;
                    }
                    else
                    {
                        string address = sourceUrl.Substring(sourceUrl.IndexOf("//") + 2);
                        if (!string.IsNullOrWhiteSpace(address))
                        {
                            string host = address.Substring(0, address.IndexOf(':'));
                            string name = address.Substring(address.IndexOf(':') + 1);
                            var ndi     = _ndiSources.FirstOrDefault(s =>
                            {
                                string ndiFullAddress = Ndi.Utf8ToString(s.Value.p_ip_address);
                                string ndiFullName    = Ndi.Utf8ToString(s.Value.p_ndi_name);
                                int openingBraceIndex = ndiFullName.IndexOf('(', 1);
                                int closingBraceIndex = ndiFullName.IndexOf(')', openingBraceIndex);
                                if (openingBraceIndex > 0 &&
                                    closingBraceIndex > openingBraceIndex &&
                                    ndiFullAddress.Substring(0, ndiFullAddress.IndexOf(':')).Equals(host, StringComparison.InvariantCultureIgnoreCase) &&
                                    ndiFullName.Substring(openingBraceIndex + 1, closingBraceIndex - openingBraceIndex - 1).Equals(name, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return(true);
                                }
                                return(false);
                            }).Key;
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(source))
                    {
                        VideoSource = source;
                    }
                });
            }
        }
Exemple #2
0
        private static void RefreshSources()
        {
            var numSources = 0;
            var ndiSources = Ndi.NDIlib_find_get_current_sources(NdiFindInstance, ref numSources);

            if (numSources <= 0)
            {
                return;
            }
            var sourceSizeInBytes = Marshal.SizeOf(typeof(NDIlib_source_t));
            var sources           = new Dictionary <string, NDIlib_source_t>();

            for (var i = 0; i < numSources; i++)
            {
                var p       = IntPtr.Add(ndiSources, (i * sourceSizeInBytes));
                var src     = (NDIlib_source_t)Marshal.PtrToStructure(p, typeof(NDIlib_source_t));
                var ndiName = Ndi.Utf8ToString(src.p_ndi_name);
                sources.Add(ndiName, src);
                Debug.WriteLine($"Added source name:{Ndi.Utf8ToString(src.p_ndi_name)} address :{Ndi.Utf8ToString(src.p_ip_address)}");
            }
            _ndiSources = sources;
            SourceRefreshed?.Invoke(null, EventArgs.Empty);
        }
Exemple #3
0
 private void RefreshSources(object obj)
 {
     if (_ndiFindInstance != IntPtr.Zero)
     {
         int numSources  = 0;
         var ndi_sources = Ndi.NDIlib_find_get_current_sources(_ndiFindInstance, ref numSources);
         if (numSources > 0)
         {
             int SourceSizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf(typeof(NDIlib_source_t));
             Dictionary <string, NDIlib_source_t> sources = new Dictionary <string, NDIlib_source_t>();
             for (int i = 0; i < numSources; i++)
             {
                 IntPtr          p       = IntPtr.Add(ndi_sources, (i * SourceSizeInBytes));
                 NDIlib_source_t src     = (NDIlib_source_t)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(NDIlib_source_t));
                 var             ndiName = Ndi.Utf8ToString(src.p_ndi_name);
                 sources.Add(ndiName, src);
                 Debug.WriteLine($"Added source name:{Ndi.Utf8ToString(src.p_ndi_name)} address :{Ndi.Utf8ToString(src.p_ip_address)}");
             }
             // removing non-existing sources
             var notExistingSources = _videoSources.Where(s => !(sources.ContainsKey(s) || s == Common.Properties.Resources._none_)).ToArray();
             foreach (var source in notExistingSources)
             {
                 _videoSources.Remove(source);
             }
             //adding new sources
             foreach (var source in sources)
             {
                 if (!_videoSources.Contains(source.Key))
                 {
                     _videoSources.Add(source.Key);
                 }
             }
             _ndiSources = sources;
         }
     }
 }