/// <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); }
/// <inheritdoc/> public SensorDataPacket Process(long timestamp) { lock (captureSync) { if (!Capturing) { return(null); } long startTime = rateAnalyzer.GetTimeStamp(); // TODO implrement prediction service and divide split multiple calls into grab begin/end // get image and metadata from the backend if (!VideoCaptureBackend.BeginVideoCaptureFrameGrab(videoCapture)) { return(null); } if (!VideoCaptureBackend.EndVideoCaptureFrameGrab(videoCapture)) { return(null); } long endTime = rateAnalyzer.GetTimeStamp(); if (endTime - startTime > 0) { Console.WriteLine("waited additional " + (endTime - startTime) + "ms for the next frame"); } rateAnalyzer.Tick(); int width = VideoCaptureBackend.GetVideoCaptureFrameProperty(videoFrame, VideoFrameProperty.Width); int height = VideoCaptureBackend.GetVideoCaptureFrameProperty(videoFrame, VideoFrameProperty.Height); RasterImagePixelFormat format = (RasterImagePixelFormat)VideoCaptureBackend.GetVideoCaptureFrameProperty( videoFrame, VideoFrameProperty.PixelFormat); int pixelSize = VideoFrame.GetPixelSize(format); // construct a data packet as result if (width < 0 || width >= 16384 || height < 0 || height >= 16384 || pixelSize < 1 || pixelSize > 16) { return(null); } int imageSize = width * height * pixelSize; byte[] image = new byte[imageSize]; GCHandle imageBuffer = GCHandle.Alloc(image, GCHandleType.Pinned); if (!VideoCaptureBackend.CopyVideoCaptureFrame(videoFrame, imageBuffer.AddrOfPinnedObject(), imageSize)) { imageBuffer.Free(); return(null); } imageBuffer.Free(); var metadata = new VideoFrameMetadata() { FrameRate = rateAnalyzer.AverageFrameRate, FrameTime = rateAnalyzer.AverageFrameTime, FrameTimeDeviation = rateAnalyzer.StandardFrameTimeDeviation }; var frame = new VideoFrame(width, height, format, image, metadata); // TODO generate packet id return(new SensorDataPacket(this, SensorDataType.Video, frame, 0)); } }
static WebcamCapture() { // TODO catch exceptions to be safe captureContext = VideoCaptureBackend.CreateVideoCaptureContext(); }
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); }