public override void StopSource()
 {
     quitNum++;
     isActive = false;
     if (webCam != null)
     {
         webCam.Stop();
         webCam = null;
     }
 }
        public override bool StartSource(string preferedCameraMoniker)
        {
            quitNum++;

            if (WebCam.CameraCount > 0)
            {
                SetupProfile();

                MultipleCameraSelector mcs = new MultipleCameraSelector(cameraTitles, WebCam.AvailableWebCamMonikers);
                mcs.ShowDialog();

                string[] cameraSelections = mcs.SelectedCameras;

                if (cameraSelections == null)
                {
                    return(false);
                }

                frames     = new Bitmap[cameraSelections.Length];
                videoSizes = new Size[cameraSelections.Length];

                for (int i = 0; i < cameraSelections.Length; i++)
                {
                    frames[i]     = null;
                    videoSizes[i] = Size.Empty;
                }

                cameraWatch = new Thread(new ThreadStart(CameraWatcher));
                cameraWatch.Start();

                webCams = new WebCam[cameraSelections.Length];
                for (int i = 0; i < webCams.Length; i++)
                {
                    webCams[i] = new WebCam(cameraSelections[i], parentForm);
                    webCams[i].CaptureDeviceVideoInputSizeDetermined += new VideoInputSizeDetermined(CMSMultipleWebcamSource_CaptureDeviceVideoInputSizeDetermined);
                    webCams[i].NewFrame += new WebCamEventHandler(CMSMultipleWebcamSource_NewFrame);
                    webCams[i].Start();
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public override bool StartSource(string preferedCameraMoniker)
        {
            lock (mutex)
            {
                quitNum++;
                isActive    = true;
                cameraWatch = new Thread(new ThreadStart(CameraWatcher));
                cameraWatch.Start();

                if (webCam != null)
                {
                    webCam.Stop();
                    webCam = null;
                }
                if (WebCam.CameraCount > 0)
                {
                    if (preferedCameraMoniker != null && preferedCameraMoniker.Length > 0 &&
                        WebCam.GetFilter(preferedCameraMoniker) == null)
                    {
                        preferedCameraMoniker = null;
                    }

                    if (WebCam.CameraCount == 1)
                    {
                        preferedCameraMoniker = WebCam.AvailableWebCamMonikers[0].Moniker;
                    }
                    else if (preferedCameraMoniker == null || preferedCameraMoniker.Length == 0)
                    {
                        CameraSelector cs = new CameraSelector(WebCam.AvailableWebCamMonikers);
                        cs.ShowDialog();
                        preferedCameraMoniker = cs.SelectedCamera;
                    }

                    webCam = new WebCam(preferedCameraMoniker, parentForm);
                    webCam.CaptureDeviceVideoInputSizeDetermined += videoInputSizeDeterminedFunc;
                    webCam.NewFrame += new WebCamEventHandler(webCam_NewFrame);
                    webCam.Start();
                    cameraCount    = WebCam.CameraCount;
                    currentMonikor = preferedCameraMoniker;

                    return(true);
                }
                return(false);
            }
        }
        private void CameraWatcher()
        {
            bool firstTime  = true;
            int  lastCount  = 0;
            int  curQuitNum = quitNum;

            try
            {
                while (curQuitNum == quitNum)
                {
                    if (isActive)
                    {
                        bool bCameraLost  = false;
                        bool bCameraFound = false;
                        int  count;

                        lock (mutex)
                        {
                            if (firstTime)
                            {
                                firstTime = false;
                                lastCount = WebCam.CameraCount;
                            }

                            count = WebCam.CameraCount;

                            if (count < lastCount)
                            {
                                if (count > 0)
                                {
                                    bool sourceStillPluggedIn = false;
                                    foreach (WebCamDescription monikor in WebCam.AvailableWebCamMonikers)
                                    {
                                        if (monikor.Moniker.Equals(currentMonikor))
                                        {
                                            sourceStillPluggedIn = true;
                                            break;
                                        }
                                    }

                                    if (sourceStillPluggedIn)
                                    {
                                        Thread.Sleep(500);
                                        continue;
                                    }
                                }

                                if (webCam != null)
                                {
                                    webCam.Stop();
                                    webCam = null;
                                }

                                bCameraLost = true;
                            }
                            else if (lastCount == 0 && count > 0)
                            {
                                bCameraFound = true;
                            }
                        }

                        if (bCameraLost)
                        {
                            CameraLostFunc(count > 0);
                        }
                        if (bCameraFound)
                        {
                            CameraFoundFunc();
                        }

                        lastCount = count;
                    }
                    else
                    {
                        firstTime = true;
                    }
                    Thread.Sleep(500);
                }
            }
            catch (Exception e)
            {
            }
        }