public override string GetSummaryAsText(CameraSummary summary)
        {
            string result = "";
            string alias  = summary.Alias;

            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info != null && info.MediaTypeIndex >= 0)
            {
                VideoCaptureDevice          device     = new VideoCaptureDevice(summary.Identifier);
                Dictionary <int, MediaType> mediaTypes = MediaTypeImporter.Import(device);
                if (mediaTypes.ContainsKey(info.MediaTypeIndex))
                {
                    Size   size        = mediaTypes[info.MediaTypeIndex].FrameSize;
                    float  fps         = (float)info.SelectedFramerate;
                    string compression = mediaTypes[info.MediaTypeIndex].Compression;
                    result = string.Format("{0} - {1}×{2} @ {3:0.##} fps ({4}).", alias, size.Width, size.Height, fps, compression);
                }
                else
                {
                    result = string.Format("{0}", alias);
                }
            }
            else
            {
                result = string.Format("{0}", alias);
            }

            return(result);
        }
        public override bool Configure(CameraSummary summary)
        {
            bool needsReconnection = false;
            FormConfiguration form = new FormConfiguration(summary);

            FormsHelper.Locate(form);
            if (form.ShowDialog() == DialogResult.OK)
            {
                if (form.AliasChanged)
                {
                    summary.UpdateAlias(form.Alias, form.PickedIcon);
                }

                if (form.SpecificChanged)
                {
                    SpecificInfo info = new SpecificInfo();
                    info.MediaTypeIndex    = form.SelectedMediaTypeIndex;
                    info.SelectedFramerate = form.SelectedFramerate;
                    info.CameraProperties  = form.CameraProperties;

                    summary.UpdateSpecific(info);

                    summary.UpdateDisplayRectangle(Rectangle.Empty);
                    needsReconnection = form.NeedsReconnection;
                }

                CameraTypeManager.UpdatedCameraSummary(summary);
            }

            form.Dispose();
            return(needsReconnection);
        }
Beispiel #3
0
        private void SetPostConnectionOptions()
        {
            // Some options only work after the graph is actually connected.
            // For example logitech exposure. Probably due to a bug in Logitech firmware.
            SpecificInfo info = summary.Specific as SpecificInfo;

            // Only do this for Logitech devices.
            if (!summary.Identifier.Contains("usb#vid_046d") || info == null)
            {
                return;
            }

            if (info.CameraProperties.ContainsKey("exposure_logitech"))
            {
                if (info.CameraProperties["exposure_logitech"].Automatic)
                {
                    return;
                }

                int exposure = int.Parse(info.CameraProperties["exposure_logitech"].CurrentValue, CultureInfo.InvariantCulture);
                device.Logitech_SetExposure(exposure, true);
            }
            else if (info.CameraProperties.ContainsKey("exposure"))
            {
                if (info.CameraProperties["exposure"].Automatic)
                {
                    return;
                }

                int exposure = int.Parse(info.CameraProperties["exposure"].CurrentValue, CultureInfo.InvariantCulture);
                device.SetCameraProperty(CameraControlProperty.Exposure, exposure, CameraControlFlags.Manual);
            }
        }
        private string SpecificInfoSerialize(CameraSummary summary)
        {
            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info == null)
            {
                return(null);
            }

            XmlDocument doc     = new XmlDocument();
            XmlElement  xmlRoot = doc.CreateElement("DirectShow");

            if (info.MediaTypeIndex < 0)
            {
                doc.AppendChild(xmlRoot);
                return(doc.OuterXml);
            }

            XmlElement xmlIndex = doc.CreateElement("MediaTypeIndex");

            xmlIndex.InnerText = string.Format("{0}", info.MediaTypeIndex);
            xmlRoot.AppendChild(xmlIndex);

            XmlElement xmlFramerate = doc.CreateElement("SelectedFramerate");
            string     fps          = info.SelectedFramerate.ToString("0.000", CultureInfo.InvariantCulture);

            xmlFramerate.InnerText = fps;
            xmlRoot.AppendChild(xmlFramerate);

            XmlElement xmlCameraProperties = doc.CreateElement("CameraProperties");

            foreach (KeyValuePair <string, CameraProperty> pair in info.CameraProperties)
            {
                XmlElement   xmlCameraProperty2 = doc.CreateElement("CameraProperty2");
                XmlAttribute attr = doc.CreateAttribute("key");
                attr.Value = pair.Key;
                xmlCameraProperty2.Attributes.Append(attr);

                XmlElement xmlCameraProperty2Value = doc.CreateElement("Value");
                xmlCameraProperty2Value.InnerText = pair.Value.CurrentValue;
                xmlCameraProperty2.AppendChild(xmlCameraProperty2Value);

                XmlElement xmlCameraProperty2Auto = doc.CreateElement("Auto");
                xmlCameraProperty2Auto.InnerText = pair.Value.Automatic.ToString().ToLower();
                xmlCameraProperty2.AppendChild(xmlCameraProperty2Auto);

                xmlCameraProperties.AppendChild(xmlCameraProperty2);
            }

            xmlRoot.AppendChild(xmlCameraProperties);

            doc.AppendChild(xmlRoot);

            return(doc.OuterXml);
        }
        private void InitializeMediaTypes(CameraSummary summary)
        {
            device = new VideoCaptureDevice(summary.Identifier);

            if (device.VideoCapabilities == null || device.VideoCapabilities.Length == 0)
            {
                canStreamConfig = false;
                return;
            }

            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info != null)
            {
                selectedMediaTypeIndex = info.MediaTypeIndex;
                selectedFramerate      = info.SelectedFramerate;
            }
            else
            {
                // First configuration of the device.
                // We should be using the default parameters.
                VideoCapabilities[] caps = device.VideoCapabilities;
                if (caps.Length == 0)
                {
                    log.ErrorFormat("Cannot get any media type for the device.");
                    return;
                }

                selectedMediaTypeIndex = caps[0].Index;
                selectedFramerate      = caps[selectedMediaTypeIndex].AverageFrameRate;
            }

            mediaTypes = MediaTypeImporter.Import(device);
            if (mediaTypes == null || mediaTypes.Count == 0)
            {
                canStreamConfig = false;
                return;
            }

            // Ensure indexing by selected media type is valid.
            if (!mediaTypes.ContainsKey(selectedMediaTypeIndex))
            {
                log.ErrorFormat("Mediatype index not found, using first media type.");
                selectedMediaTypeIndex = mediaTypes[0].MediaTypeIndex;
            }

            organizer.Organize(mediaTypes, MediaTypeImporter.GetSupportedFramerates(device));

            canStreamConfig = true;
        }
Beispiel #6
0
        private void InitializeMediaTypes(CameraSummary summary)
        {
            device = new VideoCaptureDevice(summary.Identifier);

            if (device.VideoCapabilities == null || device.VideoCapabilities.Length == 0)
            {
                canStreamConfig = false;
                return;
            }

            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info != null)
            {
                selectedMediaTypeIndex = info.MediaTypeIndex;
                selectedFramerate      = info.SelectedFramerate;
            }

            mediaTypes = MediaTypeImporter.Import(device);
            if (mediaTypes == null || mediaTypes.Count == 0)
            {
                canStreamConfig = false;
                return;
            }

            // Ensure indexing by selected media type is valid.
            if (!mediaTypes.ContainsKey(selectedMediaTypeIndex))
            {
                selectedMediaTypeIndex = mediaTypes[0].MediaTypeIndex;
                log.ErrorFormat("Mediatype index not found, using first media type.");
            }

            organizer.Organize(mediaTypes, MediaTypeImporter.GetSupportedFramerates(device));

            canStreamConfig = true;
        }
Beispiel #7
0
        /// <summary>
        /// Configure the device according to what is saved in the preferences for it.
        /// </summary>
        private void ConfigureDevice()
        {
            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info == null || info.MediaTypeIndex < 0)
            {
                log.DebugFormat("No configuration saved in preferences for this device.");
                return;
            }

            // Initialize device configuration (Extract and cache media types on the output pin).
            // Double check we have an existing index and set the format.
            AForge.Video.DirectShow.VideoCapabilities[] capabilities = device.VideoCapabilities;
            AForge.Video.DirectShow.VideoCapabilities   match        = capabilities.FirstOrDefault(c => c.Index == info.MediaTypeIndex);
            if (match == null)
            {
                log.ErrorFormat("Could not match the saved media type.");
                return;
            }

            device.SetMediaTypeAndFramerate(info.MediaTypeIndex, info.SelectedFramerate);

            log.DebugFormat("Device set to saved configuration: Index:{0}. ({1}×{2} @ {3:0.###} fps ({4})).",
                            info.MediaTypeIndex, match.FrameSize.Width, match.FrameSize.Height, info.SelectedFramerate, match.Compression);

            // Reload camera properties in case the firmware "forgot" them.
            // This means changes done in other softwares will be overwritten.
            try
            {
                CameraPropertyManager.Write(device, info.CameraProperties);
            }
            catch
            {
                log.ErrorFormat("An error occured while reloading camera properties.");
            }
        }
        public override List <CameraSummary> DiscoverCameras(IEnumerable <CameraBlurb> blurbs)
        {
            // DirectShow has active discovery. We just ask for the list of cameras connected to the PC.
            List <CameraSummary> summaries = new List <CameraSummary>();
            List <CameraSummary> found     = new List <CameraSummary>();

            FilterInfoCollection cameras = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            foreach (FilterInfo camera in cameras)
            {
                if (bypass.Contains(camera.Name))
                {
                    continue;
                }

                // For now consider that the moniker string is like a serial number.
                // Cameras that don't have a serial number will appear to be new when changing USB port.
                string identifier = camera.MonikerString;
                bool   cached     = cache.ContainsKey(identifier);

                string             alias            = camera.Name;
                Bitmap             icon             = null;
                SpecificInfo       specific         = null;
                Rectangle          displayRectangle = Rectangle.Empty;
                CaptureAspectRatio aspectRatio      = CaptureAspectRatio.Auto;

                if (blurbs != null)
                {
                    foreach (CameraBlurb blurb in blurbs)
                    {
                        if (blurb.CameraType != this.CameraType || blurb.Identifier != identifier)
                        {
                            continue;
                        }

                        alias            = blurb.Alias;
                        icon             = blurb.Icon ?? SelectDefaultIcon(identifier);
                        displayRectangle = blurb.DisplayRectangle;
                        if (!string.IsNullOrEmpty(blurb.AspectRatio))
                        {
                            aspectRatio = (CaptureAspectRatio)Enum.Parse(typeof(CaptureAspectRatio), blurb.AspectRatio);
                        }

                        specific = SpecificInfoDeserialize(blurb.Specific);
                        VendorHelper.IdentifyModel(identifier);
                        break;
                    }
                }

                if (icon == null)
                {
                    icon = SelectDefaultIcon(identifier);
                }

                CameraSummary summary = new CameraSummary(alias, camera.Name, identifier, icon, displayRectangle, aspectRatio, specific, this);
                summaries.Add(summary);

                if (cached)
                {
                    found.Add(cache[identifier]);
                }

                if (!cached)
                {
                    cache.Add(identifier, summary);
                    found.Add(summary);
                    log.DebugFormat("DirectShow device enumeration: {0} (moniker:{1}).", summary.Alias, identifier);
                }
            }

            // TODO: do we need to do all this. Just replace the cache with the current list.

            List <CameraSummary> lost = new List <CameraSummary>();

            foreach (CameraSummary summary in cache.Values)
            {
                if (!found.Contains(summary))
                {
                    lost.Add(summary);
                }
            }

            foreach (CameraSummary summary in lost)
            {
                cache.Remove(summary.Identifier);
            }

            return(summaries);
        }
        private SpecificInfo SpecificInfoDeserialize(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                return(null);
            }

            SpecificInfo info = null;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(new StringReader(xml));

                info = new SpecificInfo();

                float selectedFramerate = -1;
                int   index             = -1;

                XmlNode xmlSelectedFrameRate = doc.SelectSingleNode("/DirectShow/SelectedFramerate");
                if (xmlSelectedFrameRate != null)
                {
                    selectedFramerate = float.Parse(xmlSelectedFrameRate.InnerText, CultureInfo.InvariantCulture);
                }

                XmlNode xmlIndex = doc.SelectSingleNode("/DirectShow/MediaTypeIndex");
                if (xmlIndex != null)
                {
                    index = int.Parse(xmlIndex.InnerText, CultureInfo.InvariantCulture);
                }

                Dictionary <string, CameraProperty> cameraProperties = new Dictionary <string, CameraProperty>();

                XmlNodeList props = doc.SelectNodes("/DirectShow/CameraProperties/CameraProperty2");
                foreach (XmlNode node in props)
                {
                    XmlAttribute keyAttribute = node.Attributes["key"];
                    if (keyAttribute == null)
                    {
                        continue;
                    }

                    string         key      = keyAttribute.Value;
                    CameraProperty property = new CameraProperty();

                    string  xpath            = string.Format("/DirectShow/CameraProperties/CameraProperty2[@key='{0}']", key);
                    XmlNode xmlPropertyValue = doc.SelectSingleNode(xpath + "/Value");
                    if (xmlPropertyValue != null)
                    {
                        property.CurrentValue = xmlPropertyValue.InnerText;
                    }
                    else
                    {
                        property.Supported = false;
                    }

                    XmlNode xmlPropertyAuto = doc.SelectSingleNode(xpath + "/Auto");
                    if (xmlPropertyAuto != null)
                    {
                        property.Automatic = XmlHelper.ParseBoolean(xmlPropertyAuto.InnerText);
                    }
                    else
                    {
                        property.Supported = false;
                    }

                    cameraProperties.Add(key, property);
                }

                info.MediaTypeIndex    = index;
                info.SelectedFramerate = selectedFramerate;
                info.CameraProperties  = cameraProperties;
            }
            catch (Exception e)
            {
                log.ErrorFormat(e.Message);
            }

            return(info);
        }