public async Task ImageFxComponentFromCameraVideoPortWithSplitterAndEncoder(MMAL_PARAM_IMAGEFX_T effect, bool throwsException) { TestHelper.BeginTest($"Video - ImageFxComponentFromCameraVideoPortWithSplitterAndEncoder - {effect}"); TestHelper.SetConfigurationDefaults(); TestHelper.CleanDirectory("/home/pi/videos/tests"); using (var vidCaptureHandler = new VideoStreamCaptureHandler("/home/pi/videos/tests", "h264")) using (var preview = new MMALNullSinkComponent()) using (var imageFx = new MMALImageFxComponent()) using (var splitter = new MMALSplitterComponent()) using (var vidEncoder = new MMALVideoEncoder()) { Fixture.MMALCamera.ConfigureCameraSettings(); var vidEncoderConfig = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420); var splitterConfig = new MMALPortConfig(MMALEncoding.I420, MMALEncoding.I420); var fxConfig = new MMALPortConfig(MMALEncoding.I420, MMALEncoding.I420); imageFx.ConfigureOutputPort <VideoPort>(0, fxConfig, null); splitter.ConfigureInputPort(new MMALPortConfig(MMALEncoding.I420, MMALEncoding.I420), imageFx.Outputs[0], null); splitter.ConfigureOutputPort <VideoPort>(0, splitterConfig, null); vidEncoder.ConfigureInputPort(new MMALPortConfig(MMALEncoding.I420, MMALEncoding.I420), splitter.Outputs[0], null); vidEncoder.ConfigureOutputPort(0, vidEncoderConfig, vidCaptureHandler); if (throwsException) { Assert.Throws <MMALInvalidException>(() => { imageFx.ImageEffect = effect; }); } else { imageFx.ImageEffect = effect; } // Create our component pipeline. Fixture.MMALCamera.Camera.VideoPort .ConnectTo(imageFx); Fixture.MMALCamera.Camera.PreviewPort .ConnectTo(preview); imageFx.Outputs[0].ConnectTo(splitter); splitter.Outputs[0].ConnectTo(vidEncoder); // Camera warm up time await Task.Delay(2000); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.VideoPort, cts.Token); Fixture.CheckAndAssertFilepath(vidCaptureHandler.GetFilepath()); } }
// motion without raw recording, decouple from onDetect event static async Task motion(int totalSeconds, int recordSeconds, int sensitivity) { DeleteFiles(ramdiskPath, "*.h264"); DeleteFiles(ramdiskPath, "*.raw"); var cam = GetConfiguredCamera(); // No longer cut-and-paste from the MMALSharp wiki: // The built-in MotionConfig "recordingTime" argument only applies to calling StartRecording // on the motion buffer, which is RAW (and huge). That also means the onStopDetect action // for cam.WithMotionDetection is not especially useful. So this variation doesn't record the // RAW stream and instead uses a token timeout to terminate the recording. // When using H.264 encoding we require key frames to be generated for the Circular buffer capture handler. MMALCameraConfig.InlineHeaders = true; Console.WriteLine("Preparing pipeline..."); using (var splitter = new MMALSplitterComponent()) { // Two capture handlers are being used here, one for motion detection and the other to record a H.264 stream. using var vidCaptureHandler = new CircularBufferCaptureHandler(4000000, "/media/ramdisk", "h264"); using var motionCircularBufferCaptureHandler = new CircularBufferCaptureHandler(4000000, "/media/ramdisk", "raw"); using var resizer = new MMALIspComponent(); using var vidEncoder = new MMALVideoEncoder(); using var renderer = new MMALVideoRenderer(); cam.ConfigureCameraSettings(); var splitterPortConfig = new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420, 0, 0, null); var vidEncoderPortConfig = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 0, MMALVideoEncoder.MaxBitrateLevel4, null); // The ISP resizer is being used for better performance. Frame difference motion detection will only work if using raw video data. Do not encode to H.264/MJPEG. // Resizing to a smaller image may improve performance, but ensure that the width/height are multiples of 32 and 16 respectively to avoid cropping. var resizerPortConfig = new MMALPortConfig(MMALEncoding.RGB24, MMALEncoding.RGB24, 640, 480, 0, 0, 0, false, null); splitter.ConfigureInputPort(new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420), cam.Camera.VideoPort, null); splitter.ConfigureOutputPort(0, splitterPortConfig, null); splitter.ConfigureOutputPort(1, splitterPortConfig, null); resizer.ConfigureOutputPort <VideoPort>(0, resizerPortConfig, motionCircularBufferCaptureHandler); vidEncoder.ConfigureInputPort(new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420), splitter.Outputs[1], null); vidEncoder.ConfigureOutputPort(vidEncoderPortConfig, vidCaptureHandler); cam.Camera.VideoPort.ConnectTo(splitter); cam.Camera.PreviewPort.ConnectTo(renderer); splitter.Outputs[0].ConnectTo(resizer); splitter.Outputs[1].ConnectTo(vidEncoder); Console.WriteLine("Camera warmup..."); await Task.Delay(2000); Console.WriteLine($"Detecting motion for {totalSeconds} seconds with sensitivity threshold {sensitivity}..."); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(totalSeconds)); // The recording duration doesn't matter; see notes at top of this method. var motionConfig = new MotionConfig(TimeSpan.FromSeconds(10), sensitivity); // Stephen Cleary says CTS disposal is unnecessary as long as you cancel! https://stackoverflow.com/a/19005066/152997 var startRecordingCTS = LocalPrepareToRecord(); await cam.WithMotionDetection( motionCircularBufferCaptureHandler, motionConfig, // This callback will be invoked when motion has been detected. () => { // This has no effect if the token is already cancelled. startRecordingCTS.Cancel(); }) .ProcessAsync(cam.Camera.VideoPort, cts.Token); CancellationTokenSource LocalPrepareToRecord() { var cts = new CancellationTokenSource(); cts.Token.Register(LocalStartRecording); return(cts); } async void LocalStartRecording() { Console.WriteLine($"Motion detected, recording {recordSeconds} seconds..."); motionCircularBufferCaptureHandler.DisableMotionDetection(); vidCaptureHandler.StartRecording(); vidEncoder.RequestIFrame(); // Prepare to record // Stephen Cleary says CTS disposal is unnecessary as long as you cancel! https://stackoverflow.com/a/19005066/152997 var recordingCTS = new CancellationTokenSource(); // When the token expires, stop recording and re-enable capture recordingCTS.Token.Register(LocalEndRecording); // Start the clock recordingCTS.CancelAfter(recordSeconds * 1000); // Record until the duration passes or the overall motion detection token expires await Task.WhenAny(new Task[] { cts.Token.AsTask(), recordingCTS.Token.AsTask() }); if (!recordingCTS.IsCancellationRequested) { recordingCTS.Cancel(); } } void LocalEndRecording() { Console.WriteLine("...recording stopped."); startRecordingCTS = LocalPrepareToRecord(); motionCircularBufferCaptureHandler.EnableMotionDetection(); vidCaptureHandler.StopRecording(); vidCaptureHandler.Split(); } } // can't use the convenient fall-through using or MMALCamera.Cleanup // throws: Argument is invalid. Unable to destroy component cam.Cleanup(); Console.WriteLine("Exiting."); }
// motion as in the wiki (records raw file) static async Task motion_record_raw(int totalSeconds, int recordSeconds, int sensitivity) { DeleteFiles(ramdiskPath, "*.h264"); DeleteFiles(ramdiskPath, "*.raw"); var cam = GetConfiguredCamera(); // When using H.264 encoding we require key frames to be generated for the Circular buffer capture handler. MMALCameraConfig.InlineHeaders = true; Console.WriteLine("Preparing pipeline..."); using (var splitter = new MMALSplitterComponent()) { // Two capture handlers are being used here, one for motion detection and the other to record a H.264 stream. using var vidCaptureHandler = new CircularBufferCaptureHandler(4000000, "/media/ramdisk", "h264"); using var motionCircularBufferCaptureHandler = new CircularBufferCaptureHandler(4000000, "/media/ramdisk", "raw"); using var resizer = new MMALIspComponent(); using var vidEncoder = new MMALVideoEncoder(); using var renderer = new MMALVideoRenderer(); cam.ConfigureCameraSettings(); // The ISP resizer is being used for better performance. Frame difference motion detection will only work if using raw video data. Do not encode to H.264/MJPEG. // Resizing to a smaller image may improve performance, but ensure that the width/height are multiples of 32 and 16 respectively to avoid cropping. var resizerPortConfig = new MMALPortConfig(MMALEncoding.RGB24, MMALEncoding.RGB24, 640, 480, 0, 0, 0, false, null); var vidEncoderPortConfig = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 0, MMALVideoEncoder.MaxBitrateLevel4, null); var splitterPortConfig = new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420, 0, 0, null); splitter.ConfigureInputPort(new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420), cam.Camera.VideoPort, null); splitter.ConfigureOutputPort(0, splitterPortConfig, null); splitter.ConfigureOutputPort(1, splitterPortConfig, null); resizer.ConfigureOutputPort <VideoPort>(0, resizerPortConfig, motionCircularBufferCaptureHandler); vidEncoder.ConfigureInputPort(new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420), splitter.Outputs[1], null); vidEncoder.ConfigureOutputPort(vidEncoderPortConfig, vidCaptureHandler); cam.Camera.VideoPort.ConnectTo(splitter); cam.Camera.PreviewPort.ConnectTo(renderer); splitter.Outputs[0].ConnectTo(resizer); splitter.Outputs[1].ConnectTo(vidEncoder); Console.WriteLine("Camera warmup..."); await Task.Delay(2000); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(totalSeconds)); var motionConfig = new MotionConfig(TimeSpan.FromSeconds(recordSeconds), sensitivity); Console.WriteLine($"Detecting motion for {totalSeconds} seconds with sensitivity threshold {sensitivity}..."); await cam.WithMotionDetection( motionCircularBufferCaptureHandler, motionConfig, // This callback will be invoked when motion has been detected. () => { Console.WriteLine($"Motion detected, recording {recordSeconds} seconds..."); // Stop motion detection while we are recording. motionCircularBufferCaptureHandler.DisableMotionDetection(); // Start recording our H.264 video. vidCaptureHandler.StartRecording(); motionCircularBufferCaptureHandler.StartRecording(); // Request a key frame to be immediately generated by the h.264 encoder. vidEncoder.RequestIFrame(); }, // Invoked when motion handler recording-time expires () => { // We want to re-enable the motion detection. motionCircularBufferCaptureHandler.EnableMotionDetection(); // Stop recording on our capture handlers. motionCircularBufferCaptureHandler.StopRecording(); vidCaptureHandler.StopRecording(); // Optionally create new file for our next recording run (don't do the RAW file, we don't want it). vidCaptureHandler.Split(); Console.WriteLine("...recording stopped."); }) .ProcessAsync(cam.Camera.VideoPort, cts.Token); } // can't use the convenient fall-through using or MMALCamera.Cleanup // throws: Argument is invalid. Unable to destroy component cam.Cleanup(); Console.WriteLine("Exiting."); }
public void ChangeColorSpace() { TestHelper.BeginTest("ChangeColorSpace"); TestHelper.SetConfigurationDefaults(); TestHelper.CleanDirectory("/home/pi/videos/tests"); MMALCameraConfig.VideoColorSpace = MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601; using (var handler = new VideoStreamCaptureHandler("/home/pi/videos/tests", "h264")) using (var handler2 = new VideoStreamCaptureHandler("/home/pi/video/tests", "h264")) using (var handler3 = new VideoStreamCaptureHandler("/home/pi/video/tests", "h264")) using (var handler4 = new VideoStreamCaptureHandler("/home/pi/video/tests", "h264")) using (var splitter = new MMALSplitterComponent(null)) using (var vidEncoder = new MMALVideoEncoder(handler)) using (var vidEncoder2 = new MMALVideoEncoder(handler2)) using (var vidEncoder3 = new MMALVideoEncoder(handler3)) using (var vidEncoder4 = new MMALVideoEncoder(handler4)) using (var renderer = new MMALVideoRenderer()) { Fixture.MMALCamera.ConfigureCameraSettings(); var splitterPortConfig = new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420, 10, 0, 13000000, null); var portConfig1 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 10, 10, 13000000, DateTime.Now.AddSeconds(20)); var portConfig2 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 10, 20, 13000000, DateTime.Now.AddSeconds(15)); var portConfig3 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 25, 30, 13000000, DateTime.Now.AddSeconds(10)); var portConfig4 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 25, 40, 13000000, DateTime.Now.AddSeconds(10)); // Create our component pipeline. splitter.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, Fixture.MMALCamera.Camera.VideoPort); splitter.ConfigureOutputPort(0, splitterPortConfig); splitter.ConfigureOutputPort(1, splitterPortConfig); splitter.ConfigureOutputPort(2, splitterPortConfig); splitter.ConfigureOutputPort(3, splitterPortConfig); vidEncoder.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[0]); vidEncoder.ConfigureOutputPort(0, portConfig1); vidEncoder2.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[1]); vidEncoder2.ConfigureOutputPort(0, portConfig2); vidEncoder3.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[2]); vidEncoder3.ConfigureOutputPort(0, portConfig3); vidEncoder4.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[3]); vidEncoder4.ConfigureOutputPort(0, portConfig4); Fixture.MMALCamera.Camera.VideoPort.ConnectTo(splitter); splitter.Outputs[0].ConnectTo(vidEncoder); splitter.Outputs[1].ConnectTo(vidEncoder2); splitter.Outputs[2].ConnectTo(vidEncoder3); splitter.Outputs[3].ConnectTo(vidEncoder4); Fixture.MMALCamera.Camera.PreviewPort.ConnectTo(renderer); // Assert that all output ports have the same video color space. Assert.True(Fixture.MMALCamera.Camera.VideoPort.VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(splitter.Outputs[0].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(splitter.Outputs[1].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(splitter.Outputs[2].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(splitter.Outputs[3].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(vidEncoder.Outputs[0].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(vidEncoder2.Outputs[0].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(vidEncoder3.Outputs[0].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); Assert.True(vidEncoder4.Outputs[0].VideoColorSpace.EncodingVal == MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT601.EncodingVal); } }
public async Task VideoSplitterComponent() { TestHelper.BeginTest("VideoSplitterComponent"); TestHelper.SetConfigurationDefaults(); TestHelper.CleanDirectory("/home/pi/videos/tests"); using (var handler = new VideoStreamCaptureHandler("/home/pi/videos/tests", "h264")) using (var handler2 = new VideoStreamCaptureHandler("/home/pi/videos/tests", "h264")) using (var handler3 = new VideoStreamCaptureHandler("/home/pi/videos/tests", "h264")) using (var handler4 = new VideoStreamCaptureHandler("/home/pi/videos/tests", "h264")) using (var splitter = new MMALSplitterComponent(null)) using (var vidEncoder = new MMALVideoEncoder(handler)) using (var vidEncoder2 = new MMALVideoEncoder(handler2)) using (var vidEncoder3 = new MMALVideoEncoder(handler3)) using (var vidEncoder4 = new MMALVideoEncoder(handler4)) using (var renderer = new MMALNullSinkComponent()) { Fixture.MMALCamera.ConfigureCameraSettings(); var splitterPortConfig = new MMALPortConfig(MMALEncoding.OPAQUE, MMALEncoding.I420, 10, 0, 13000000, null); var portConfig1 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 10, 10, 13000000, DateTime.Now.AddSeconds(20)); var portConfig2 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 10, 20, 13000000, DateTime.Now.AddSeconds(15)); var portConfig3 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 25, 30, 13000000, DateTime.Now.AddSeconds(10)); var portConfig4 = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 25, 40, 13000000, DateTime.Now.AddSeconds(10)); // Create our component pipeline. splitter.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, Fixture.MMALCamera.Camera.VideoPort); splitter.ConfigureOutputPort(0, splitterPortConfig); splitter.ConfigureOutputPort(1, splitterPortConfig); splitter.ConfigureOutputPort(2, splitterPortConfig); splitter.ConfigureOutputPort(3, splitterPortConfig); vidEncoder.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[0]); vidEncoder.ConfigureOutputPort(0, portConfig1); vidEncoder2.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[1]); vidEncoder2.ConfigureOutputPort(0, portConfig2); vidEncoder3.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[2]); vidEncoder3.ConfigureOutputPort(0, portConfig3); vidEncoder4.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[3]); vidEncoder4.ConfigureOutputPort(0, portConfig4); Fixture.MMALCamera.Camera.VideoPort.ConnectTo(splitter); splitter.Outputs[0].ConnectTo(vidEncoder); splitter.Outputs[1].ConnectTo(vidEncoder2); splitter.Outputs[2].ConnectTo(vidEncoder3); splitter.Outputs[3].ConnectTo(vidEncoder4); Fixture.MMALCamera.Camera.PreviewPort.ConnectTo(renderer); // Camera warm up time await Task.Delay(2000); await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.VideoPort); Fixture.CheckAndAssertFilepath(handler.GetFilepath()); Fixture.CheckAndAssertFilepath(handler2.GetFilepath()); Fixture.CheckAndAssertFilepath(handler3.GetFilepath()); Fixture.CheckAndAssertFilepath(handler4.GetFilepath()); } }
public void VideoSplitterComponent() { TestHelper.BeginTest("VideoSplitterComponent"); TestHelper.SetConfigurationDefaults(); AsyncContext.Run(async() => { TestHelper.CleanDirectory("/home/pi/videos/tests"); using (var handler = new VideoStreamCaptureHandler("/home/pi/videos/tests", "avi")) using (var handler2 = new VideoStreamCaptureHandler("/home/pi/video/tests", "avi")) using (var handler3 = new VideoStreamCaptureHandler("/home/pi/video/tests", "avi")) using (var handler4 = new VideoStreamCaptureHandler("/home/pi/video/tests", "avi")) using (var splitter = new MMALSplitterComponent(null)) using (var vidEncoder = new MMALVideoEncoder(handler, DateTime.Now.AddSeconds(10))) using (var vidEncoder2 = new MMALVideoEncoder(handler2, DateTime.Now.AddSeconds(15))) using (var vidEncoder3 = new MMALVideoEncoder(handler3, DateTime.Now.AddSeconds(10))) using (var vidEncoder4 = new MMALVideoEncoder(handler4, DateTime.Now.AddSeconds(10))) using (var renderer = new MMALVideoRenderer()) { _fixture.MMALCamera.ConfigureCameraSettings(); // Create our component pipeline. splitter.ConfigureInputPort(MMALEncoding.I420, MMALEncoding.I420, _fixture.MMALCamera.Camera.VideoPort); splitter.ConfigureOutputPort(0, MMALEncoding.OPAQUE, MMALEncoding.I420, 0); splitter.ConfigureOutputPort(1, MMALEncoding.OPAQUE, MMALEncoding.I420, 0); splitter.ConfigureOutputPort(2, MMALEncoding.OPAQUE, MMALEncoding.I420, 0); splitter.ConfigureOutputPort(3, MMALEncoding.OPAQUE, MMALEncoding.I420, 0); vidEncoder.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[0]); vidEncoder.ConfigureOutputPort(0, MMALEncoding.H264, MMALEncoding.I420, 10, 25000000); vidEncoder2.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[1]); vidEncoder2.ConfigureOutputPort(0, MMALEncoding.H264, MMALEncoding.I420, 20, 25000000); vidEncoder3.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[2]); vidEncoder3.ConfigureOutputPort(0, MMALEncoding.H264, MMALEncoding.I420, 30, 25000000); vidEncoder4.ConfigureInputPort(MMALEncoding.OPAQUE, MMALEncoding.I420, splitter.Outputs[3]); vidEncoder4.ConfigureOutputPort(0, MMALEncoding.H264, MMALEncoding.I420, 40, 25000000); _fixture.MMALCamera.Camera.VideoPort.ConnectTo(splitter); splitter.Outputs[0].ConnectTo(vidEncoder); splitter.Outputs[1].ConnectTo(vidEncoder2); splitter.Outputs[2].ConnectTo(vidEncoder3); splitter.Outputs[3].ConnectTo(vidEncoder4); _fixture.MMALCamera.Camera.PreviewPort.ConnectTo(renderer); // Camera warm up time await Task.Delay(2000); await _fixture.MMALCamera.ProcessAsync(_fixture.MMALCamera.Camera.VideoPort); _fixture.CheckAndAssertFilepath(handler.GetFilepath()); _fixture.CheckAndAssertFilepath(handler2.GetFilepath()); _fixture.CheckAndAssertFilepath(handler3.GetFilepath()); _fixture.CheckAndAssertFilepath(handler4.GetFilepath()); } }); }