Beispiel #1
0
        public override void PromptDeviceSelector()
        {
            DelegatesPool dp = DelegatesPool.Instance();

            if (dp.DeactivateKeyboardHandler != null)
            {
                dp.DeactivateKeyboardHandler();
            }

            bool reconnected = false;

            // Ask the user which device he wants to use or which size/framerate.
            formDevicePicker fdp = new formDevicePicker(ListDevices(), m_CurrentVideoDevice, DisplayDevicePropertyPage);

            if (fdp.ShowDialog() == DialogResult.OK)
            {
                DeviceDescriptor dev = fdp.SelectedDevice;

                if (dev == null || dev.Empty)
                {
                    log.DebugFormat("Selected device is null or empty.");
                    if (m_CurrentVideoDevice != null)
                    {
                        // From something to empty.
                        Disconnect();
                    }
                }
                else if (dev.Network)
                {
                    if (m_CurrentVideoDevice == null || !m_CurrentVideoDevice.Network)
                    {
                        // From empty or non-network to network.
                        log.DebugFormat("Selected network camera - connect with default parameters");
                        reconnected = ConnectToDevice(dev);
                    }
                    else
                    {
                        // From network to network.
                        log.DebugFormat("Network camera - parameters changed - connect with new parameters");
                        // Parameters were set on the dialog. We don't care if the parameters were actually changed.
                        DeviceDescriptor netDevice = new DeviceDescriptor(ScreenManagerLang.Capture_NetworkCamera, fdp.SelectedUrl, fdp.SelectedFormat);
                        reconnected = ConnectToDevice(netDevice);
                    }
                }
                else
                {
                    if (m_CurrentVideoDevice == null || m_CurrentVideoDevice.Network || dev.Identification != m_CurrentVideoDevice.Identification)
                    {
                        // From network or different capture device to capture device.
                        log.DebugFormat("Selected capture device");
                        reconnected = ConnectToDevice(dev);
                    }
                    else
                    {
                        // From same capture device - caps changed.
                        DeviceCapability cap = fdp.SelectedCapability;
                        if (cap != null && !cap.Equals(m_CurrentVideoDevice.SelectedCapability))
                        {
                            log.DebugFormat("Capture device, capability changed.");

                            m_CurrentVideoDevice.SelectedCapability = cap;
                            //PreferencesManager.CapturePreferences.UpdateDeviceConfiguration(m_CurrentVideoDevice.Identification, cap);
                            //PreferencesManager.Save();

                            if (m_bIsGrabbing)
                            {
                                m_VideoSource.Stop();
                            }

                            ((VideoCaptureDevice)m_VideoSource).DesiredFrameSize = cap.FrameSize;
                            ((VideoCaptureDevice)m_VideoSource).DesiredFrameRate = cap.Framerate;

                            m_FrameSize      = cap.FrameSize;
                            m_FramesInterval = 1000 / (double)cap.Framerate;

                            log.Debug(String.Format("New capability: {0}", cap.ToString()));

                            m_bSizeChanged = true;

                            if (m_bIsGrabbing)
                            {
                                m_VideoSource.Start();
                            }
                        }
                    }
                }

                if (reconnected)
                {
                    m_Container.Connected();
                }
            }

            fdp.Dispose();

            if (dp.ActivateKeyboardHandler != null)
            {
                dp.ActivateKeyboardHandler();
            }
        }
        private void DisplayConfControls(DeviceDescriptor _currentDevice)
        {
            if (_currentDevice != null)
            {
                lblConfig.Visible           = !_currentDevice.Network;
                lblNoConf.Visible           = !_currentDevice.Network;
                btnDeviceProperties.Visible = !_currentDevice.Network;
                cmbCapabilities.Visible     = !_currentDevice.Network;

                lblUrl.Visible        = _currentDevice.Network;
                lblStreamType.Visible = _currentDevice.Network;
                cmbUrl.Visible        = _currentDevice.Network;
                cmbStreamType.Visible = _currentDevice.Network;

                if (_currentDevice.Network)
                {
                    btnCamcorder.Image = Resources.camera_network2;

                    // Recently used cameras.
                    cmbUrl.Text = _currentDevice.NetworkCameraUrl;

                    /*if(PreferencesManager.CapturePreferences.RecentNetworkCameras.Count > 0)
                     * {
                     *      foreach(string url in PreferencesManager.CapturePreferences.RecentNetworkCameras)
                     *      {
                     *              cmbUrl.Items.Add(url);
                     *      }
                     * }
                     * else
                     * {
                     *      cmbUrl.Items.Add(_currentDevice.NetworkCameraUrl);
                     * }*/

                    // Type of streams supported.
                    cmbStreamType.Items.Add("JPEG");
                    cmbStreamType.Items.Add("MJPEG");
                    if (_currentDevice.NetworkCameraFormat == NetworkCameraFormat.JPEG)
                    {
                        cmbStreamType.SelectedIndex = 0;
                    }
                    else if (_currentDevice.NetworkCameraFormat == NetworkCameraFormat.MJPEG)
                    {
                        cmbStreamType.SelectedIndex = 1;
                    }
                    else
                    {
                        _currentDevice.NetworkCameraFormat = NetworkCameraFormat.JPEG;
                        cmbStreamType.SelectedIndex        = 0;
                    }
                }
                else
                {
                    btnCamcorder.Image = Resources.camera_selected;

                    int selectedCap = 0;
                    for (int i = 0; i < _currentDevice.Capabilities.Count; i++)
                    {
                        DeviceCapability dc = _currentDevice.Capabilities[i];
                        cmbCapabilities.Items.Add(dc);
                        if (dc == _currentDevice.SelectedCapability)
                        {
                            selectedCap = i;
                        }
                    }

                    if (_currentDevice.Capabilities.Count > 0)
                    {
                        cmbCapabilities.SelectedIndex = selectedCap;
                        lblNoConf.Visible             = false;
                        cmbCapabilities.Visible       = true;
                    }
                    else
                    {
                        lblNoConf.Visible       = true;
                        cmbCapabilities.Visible = false;
                    }
                }
            }
            else
            {
                btnCamcorder.Image = Resources.camera_notfound;

                // No device currently selected.
                lblConfig.Visible           = false;
                lblNoConf.Visible           = false;
                btnDeviceProperties.Visible = false;
                cmbCapabilities.Visible     = false;

                lblUrl.Visible        = false;
                lblStreamType.Visible = false;
                cmbUrl.Visible        = false;
                cmbStreamType.Visible = false;
            }
        }
Beispiel #3
0
        private bool ConnectToDevice(DeviceDescriptor _device)
        {
            log.DebugFormat("Connecting to {0}", _device.Name);

            Disconnect();
            bool created = false;

            if (_device.Network)
            {
                // Network Camera. Connect to last used url.
                // The user will have to open the dialog again if parameters have changed or aren't good.

                // Parse URL for inline username:password.
                string login = "";
                string pass  = "";

                Uri networkCameraUrl = new Uri(_device.NetworkCameraUrl);
                if (!string.IsNullOrEmpty(networkCameraUrl.UserInfo))
                {
                    string [] split = networkCameraUrl.UserInfo.Split(new Char [] { ':' });
                    if (split.Length == 2)
                    {
                        login = split[0];
                        pass  = split[1];
                    }
                }

                if (_device.NetworkCameraFormat == NetworkCameraFormat.JPEG)
                {
                    JPEGStream source = new JPEGStream(_device.NetworkCameraUrl);
                    if (!string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(pass))
                    {
                        source.Login    = login;
                        source.Password = pass;
                    }

                    m_VideoSource = source;
                }
                else
                {
                    MJPEGStream source = new MJPEGStream(_device.NetworkCameraUrl);
                    if (!string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(pass))
                    {
                        source.Login    = login;
                        source.Password = pass;
                    }

                    m_VideoSource = source;
                }

                /*PreferencesManager.CapturePreferences.NetworkCameraFormat = _device.NetworkCameraFormat;
                 * PreferencesManager.CapturePreferences.NetworkCameraUrl = _device.NetworkCameraUrl;
                 * PreferencesManager.Save();*/

                created = true;
            }
            else
            {
                m_VideoSource = new VideoCaptureDevice(_device.Identification);
                VideoCaptureDevice captureDevice = m_VideoSource as VideoCaptureDevice;
                if (captureDevice != null)
                {
                    if ((captureDevice.VideoCapabilities != null) && (captureDevice.VideoCapabilities.Length > 0))
                    {
                        // Import the capabilities of the device.
                        foreach (VideoCapabilities vc in captureDevice.VideoCapabilities)
                        {
                            DeviceCapability dc = new DeviceCapability(vc.FrameSize, vc.FrameRate);
                            _device.Capabilities.Add(dc);

                            log.Debug(String.Format("Device Capability. {0}", dc.ToString()));
                        }

                        DeviceCapability selectedCapability = null;

                        // Check if we already know this device and have a preferred configuration.

                        /*foreach(DeviceConfiguration conf in PreferencesManager.CapturePreferences.DeviceConfigurations)
                         * {
                         *      if(conf.ID == _device.Identification)
                         *      {
                         *              // Try to find the previously selected capability.
                         *              selectedCapability = _device.GetCapabilityFromSpecs(conf.Capability);
                         *              if(selectedCapability != null)
                         *                      log.Debug(String.Format("Picking capability from preferences: {0}", selectedCapability.ToString()));
                         *      }
                         * }*/

                        /*if(selectedCapability == null)
                         * {
                         *      // Pick the one with max frame size.
                         *      selectedCapability = _device.GetBestSizeCapability();
                         *      log.Debug(String.Format("Picking a default capability (best size): {0}", selectedCapability.ToString()));
                         *      PreferencesManager.CapturePreferences.UpdateDeviceConfiguration(_device.Identification, selectedCapability);
                         *      PreferencesManager.Save();
                         * }*/

                        _device.SelectedCapability     = selectedCapability;
                        captureDevice.DesiredFrameSize = selectedCapability.FrameSize;
                        captureDevice.DesiredFrameRate = selectedCapability.Framerate;
                        m_FrameSize      = selectedCapability.FrameSize;
                        m_FramesInterval = 1000 / (double)selectedCapability.Framerate;
                    }
                    else
                    {
                        captureDevice.DesiredFrameRate = 0;
                    }

                    created = true;
                }
            }

            if (created)
            {
                m_CurrentVideoDevice            = _device;
                m_VideoSource.NewFrame         += new NewFrameEventHandler(VideoDevice_NewFrame);
                m_VideoSource.VideoSourceError += new VideoSourceErrorEventHandler(VideoDevice_VideoSourceError);
                m_bIsConnected = true;
            }
            else
            {
                log.Error("Couldn't create the capture device.");
            }

            return(created);
        }