Exemplo n.º 1
0
 /// <inheritdoc/>
 public bool Stop()
 {
     lock (captureSync) {
         if (!Capturing)
         {
             return(true);
         }
         if (!VideoCaptureBackend.StopVideoCapture(videoCapture))
         {
             return(false);
         }
         rateAnalyzer.Stop();
         if (videoFrame != IntPtr.Zero)
         {
             VideoCaptureBackend.DestroyVideoCaptureFrame(videoCapture, videoFrame);
             videoFrame = IntPtr.Zero;
         }
         if (videoCapture != IntPtr.Zero)
         {
             VideoCaptureBackend.DestroyVideoCapture(captureContext, videoCapture);
             videoFrame = IntPtr.Zero;
         }
         if (backendSettings != IntPtr.Zero)
         {
             VideoCaptureBackend.FreeVideoCaptureSettings(backendSettings);
             backendSettings = IntPtr.Zero;
         }
     }
     return(true);
 }
Exemplo n.º 2
0
        public bool Start()
        {
            lock (captureSync) {
                if (Capturing)
                {
                    return(true);
                }
                if (captureContext == IntPtr.Zero)
                {
                    return(false);
                }
                // create and start the capture process
                bool started = false;
                do
                {
                    backendSettings = VideoCaptureBackend.AllocVideoCaptureSettings();

                    if (backendSettings == IntPtr.Zero)
                    {
                        break;
                    }
                    // TODO apply settings
                    VideoCaptureBackend.SetVideoCaptureProperty(backendSettings,
                                                                VideoCaptureBackend.TranslateSensorProperty(SensorProperty.Exposure), 0);

                    videoCapture = VideoCaptureBackend.CreateVideoCapture(captureContext, backendSettings);
                    if (videoCapture == IntPtr.Zero)
                    {
                        break;
                    }
                    videoFrame = VideoCaptureBackend.CreateVideoCaptureFrame(videoCapture);
                    if (videoFrame == IntPtr.Zero)
                    {
                        break;
                    }
                    if (!VideoCaptureBackend.StartVideoCapture(videoCapture, videoFrame))
                    {
                        break;
                    }
                    rateAnalyzer.Start(1000);
                    started = true;
                } while (false);
                // clean up in the correct order if starting was unsuccessful
                if (!started)
                {
                    if (videoFrame != IntPtr.Zero)
                    {
                        VideoCaptureBackend.DestroyVideoCaptureFrame(videoCapture, videoFrame);
                        videoFrame = IntPtr.Zero;
                    }
                    if (videoCapture != IntPtr.Zero)
                    {
                        VideoCaptureBackend.DestroyVideoCapture(captureContext, videoCapture);
                        videoFrame = IntPtr.Zero;
                    }
                    if (backendSettings != IntPtr.Zero)
                    {
                        VideoCaptureBackend.FreeVideoCaptureSettings(backendSettings);
                        backendSettings = IntPtr.Zero;
                    }
                    return(false);
                }
                // finally retrieve updated settings from the backend
                if (VideoCaptureBackend.GetVideoCaptureSettings(videoCapture, backendSettings))
                {
                    // TODO update local settings
                    int width    = VideoCaptureBackend.GetVideoCaptureProperty(backendSettings, VideoCaptureProperty.FrameWidth);
                    int height   = VideoCaptureBackend.GetVideoCaptureProperty(backendSettings, VideoCaptureProperty.FrameHeight);
                    int rate     = VideoCaptureBackend.GetVideoCaptureProperty(backendSettings, VideoCaptureProperty.FrameRate);
                    int bright   = VideoCaptureBackend.GetVideoCaptureProperty(backendSettings, VideoCaptureProperty.Brightness);
                    int gain     = VideoCaptureBackend.GetVideoCaptureProperty(backendSettings, VideoCaptureProperty.Gain);
                    int exposure = VideoCaptureBackend.GetVideoCaptureProperty(backendSettings, VideoCaptureProperty.Exposure);

                    if (width <= 0)
                    {
                        settings.SetAuto(SensorProperty.FrameWidth);
                    }
                    else
                    {
                        settings.SetInteger(SensorProperty.FrameWidth, width);
                    }
                    if (height <= 0)
                    {
                        settings.SetAuto(SensorProperty.FrameHeight);
                    }
                    else
                    {
                        settings.SetInteger(SensorProperty.FrameHeight, height);
                    }
                    if (rate <= 0)
                    {
                        settings.SetAuto(SensorProperty.FrameRate);
                    }
                    else
                    {
                        settings.SetInteger(SensorProperty.FrameRate, rate);
                    }
                    if (bright < 0)
                    {
                        settings.SetAuto(SensorProperty.Brightness);
                    }
                    else
                    {
                        settings.SetInteger(SensorProperty.Brightness, bright);
                    }
                    if (gain < 0)
                    {
                        settings.SetAuto(SensorProperty.Gain);
                    }
                    else
                    {
                        settings.SetInteger(SensorProperty.Gain, gain);
                    }
                    if (exposure < 0)
                    {
                        settings.SetAuto(SensorProperty.Exposure);
                    }
                    else
                    {
                        settings.SetInteger(SensorProperty.Exposure, exposure);
                    }
                }
            }
            return(true);
        }