|
3 | 3 | // Licensed under the MIT License. Please see LICENSE.txt for License info. |
4 | 4 | // </copyright> |
5 | 5 |
|
| 6 | +using System; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using MMALSharp.Components; |
| 10 | +using MMALSharp.FFmpeg; |
| 11 | +using MMALSharp.Handlers; |
| 12 | +using MMALSharp.Native; |
| 13 | +using Nito.AsyncEx; |
| 14 | +using Xunit; |
| 15 | + |
6 | 16 | namespace MMALSharp.Tests |
7 | 17 | { |
| 18 | + [Collection("MMALCollection")] |
8 | 19 | public class FFmpegTests |
9 | 20 | { |
| 21 | + private readonly MMALFixture _fixture; |
| 22 | + |
| 23 | + public FFmpegTests(MMALFixture fixture) |
| 24 | + { |
| 25 | + _fixture = fixture; |
| 26 | + TestData.Fixture = fixture; |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void RawVideoConvert() |
| 31 | + { |
| 32 | + TestHelper.BeginTest("RawVideoConvert"); |
| 33 | + TestHelper.SetConfigurationDefaults(); |
| 34 | + |
| 35 | + AsyncContext.Run(async () => |
| 36 | + { |
| 37 | + TestHelper.CleanDirectory("/home/pi/videos/tests"); |
| 38 | + |
| 39 | + using (var ffCaptureHandler = FFmpegCaptureHandler.RawVideoToAvi("/home/pi/videos/tests", "testing1234")) |
| 40 | + using (var vidEncoder = new MMALVideoEncoder(ffCaptureHandler)) |
| 41 | + using (var renderer = new MMALVideoRenderer()) |
| 42 | + { |
| 43 | + _fixture.MMALCamera.ConfigureCameraSettings(); |
| 44 | + |
| 45 | + vidEncoder.ConfigureOutputPort(0, MMALEncoding.H264, MMALEncoding.I420, 0, 25000000); |
| 46 | + |
| 47 | + _fixture.MMALCamera.Camera.VideoPort.ConnectTo(vidEncoder); |
| 48 | + _fixture.MMALCamera.Camera.PreviewPort.ConnectTo(renderer); |
| 49 | + |
| 50 | + // Camera warm up time |
| 51 | + await Task.Delay(2000); |
| 52 | + |
| 53 | + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); |
| 54 | + |
| 55 | + // Take video for 3 minutes. |
| 56 | + await _fixture.MMALCamera.ProcessAsync(_fixture.MMALCamera.Camera.VideoPort, cts.Token); |
| 57 | + |
| 58 | + _fixture.CheckAndAssertFilepath("/home/pi/videos/tests/testing1234.avi"); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void ImagesToVideo() |
| 65 | + { |
| 66 | + TestHelper.BeginTest("ImagesToVideo"); |
| 67 | + TestHelper.SetConfigurationDefaults(); |
| 68 | + |
| 69 | + AsyncContext.Run(async () => |
| 70 | + { |
| 71 | + TestHelper.CleanDirectory("/home/pi/videos/tests"); |
| 72 | + TestHelper.CleanDirectory("/home/pi/images/tests"); |
| 73 | + |
| 74 | + // This example will take an image every 5 seconds for 1 minute. |
| 75 | + using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg")) |
| 76 | + { |
| 77 | + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)); |
| 78 | + |
| 79 | + var tl = new Timelapse { Mode = TimelapseMode.Second, CancellationToken = cts.Token, Value = 5 }; |
| 80 | + await _fixture.MMALCamera.TakePictureTimelapse(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420, tl); |
| 81 | + |
| 82 | + // Process all images captured into a video at 2fps. |
| 83 | + imgCaptureHandler.ImagesToVideo("/home/pi/videos/tests", 2); |
| 84 | + |
| 85 | + _fixture.CheckAndAssertFilepath("/home/pi/videos/tests/out.avi"); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
10 | 89 | } |
11 | 90 | } |
0 commit comments