Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Getting ready for 0.2 release. Close #7. Close #15. Close #19. Close #27
Browse files Browse the repository at this point in the history
.
  • Loading branch information
techyian committed May 16, 2017
1 parent 49e338d commit 0bc3731
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 63 deletions.
15 changes: 7 additions & 8 deletions src/MMALSharp/Components/MMALCameraComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ internal void Initialise()
this.InitialiseStill();
}

private void InitialisePreview()
internal void InitialisePreview()
{
var vFormat = new MMAL_VIDEO_FORMAT_T(
MMALUtil.VCOS_ALIGN_UP(MMALCameraConfig.VideoResolution.Width, 32),
Expand All @@ -157,7 +157,7 @@ private void InitialisePreview()
this.PreviewPort.Commit();
}

private void InitialiseVideo()
internal void InitialiseVideo()
{
if (MMALCameraConfig.VideoResolution.Width == 0 || MMALCameraConfig.VideoResolution.Width > this.CameraInfo.MaxWidth)
{
Expand Down Expand Up @@ -194,7 +194,7 @@ private void InitialiseVideo()
this.VideoPort.BufferSizeMin);
}

private void InitialiseStill()
internal void InitialiseStill()
{
//If user hasn't specified Width/Height, or one which is too high, use highest resolution supported by sensor.
if (MMALCameraConfig.StillResolution == null)
Expand All @@ -218,13 +218,12 @@ private void InitialiseStill()
MMALCameraConfig.StillEncoding == MMALEncoding.MMAL_ENCODING_RGB24 ||
MMALCameraConfig.StillEncoding == MMALEncoding.MMAL_ENCODING_RGB16)
{
Helpers.PrintWarning("Encoding set to RGB. Increasing width padding to multiple of 16.");
Helpers.PrintWarning("Encoding set to RGB. Setting width padding to multiple of 16.");

vFormat = new MMAL_VIDEO_FORMAT_T(
MMALUtil.VCOS_ALIGN_UP(MMALCameraConfig.StillResolution.Width, 16),
MMALUtil.VCOS_ALIGN_UP(MMALCameraConfig.StillResolution.Height, 16),
new MMAL_RECT_T(0, 0, MMALCameraConfig.StillResolution.Width,
MMALCameraConfig.StillResolution.Height),
new MMAL_RECT_T(0, 0, MMALCameraConfig.StillResolution.Width, MMALCameraConfig.StillResolution.Height),
new MMAL_RATIONAL_T(0, 1),
this.StillPort.Ptr->Format->es->video.Par,
this.StillPort.Ptr->Format->es->video.ColorSpace
Expand All @@ -242,14 +241,14 @@ private void InitialiseStill()
this.StillPort.Ptr->Format->es->video.ColorSpace
);
}

this.StillPort.Ptr->Format->es->video = vFormat;
this.StillPort.Ptr->Format->encoding = MMALCameraConfig.StillEncoding.EncodingVal;
this.StillPort.Ptr->Format->encodingVariant = MMALCameraConfig.StillSubFormat.EncodingVal;

if (MMALCameraConfig.Debug)
Console.WriteLine("Commit still");

this.StillPort.Commit();

this.StillPort.Ptr->BufferNum = Math.Max(this.StillPort.BufferNumRecommended,
Expand Down
6 changes: 6 additions & 0 deletions src/MMALSharp/Components/MMALComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ internal void CleanPortPools()
if (MMALCameraConfig.Debug)
Console.WriteLine("Destroying port pool");

if (port.Enabled)
port.DisablePort();

port.DestroyPortPool();
port.BufferPool = null;
}
Expand All @@ -232,6 +235,9 @@ internal void CleanPortPools()
if (MMALCameraConfig.Debug)
Console.WriteLine("Destroying port pool");

if (port.Enabled)
port.DisablePort();

port.DestroyPortPool();
port.BufferPool = null;
}
Expand Down
115 changes: 83 additions & 32 deletions src/MMALSharp/MMALCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public async Task TakeVideo(MMALPortImpl connPort, DateTime? timeout = null, Spl

try
{
Console.WriteLine($"Preparing to take video. Resolution: {MMALCameraConfig.VideoResolution.Width} x {MMALCameraConfig.VideoResolution.Height}. Encoder: {encoder.EncodingType.EncodingName}. Pixel Format: {encoder.PixelFormat.EncodingName}.");
Console.WriteLine($"Preparing to take video. Resolution: {MMALCameraConfig.VideoResolution.Width} x {MMALCameraConfig.VideoResolution.Height}. " +
$"Encoder: {encoder.EncodingType.EncodingName}. Pixel Format: {encoder.PixelFormat.EncodingName}.");

((MMALVideoPort)encoder.Outputs.ElementAt(0)).Timeout = timeout;
((MMALVideoEncoder)encoder).Split = split;
Expand All @@ -138,19 +139,16 @@ public async Task TakeRawPicture(ICaptureHandler handler)
{
throw new PiCameraError("A connection was found to the Camera still port. No encoder should be connected to the Camera's still port for raw capture.");
}
if (handler == null)
{
throw new PiCameraError("No handler specified");
}

this.Camera.Handler = handler;
this.Camera.Handler = handler ?? throw new PiCameraError("No handler specified");

this.CheckPreviewComponentStatus();

//Enable the image encoder output port.
try
{
Console.WriteLine($"Preparing to take picture - Resolution: {MMALCameraConfig.StillResolution.Width} x {MMALCameraConfig.StillResolution.Height}");
Console.WriteLine($"Preparing to take raw picture - Resolution: {MMALCameraConfig.StillResolution.Width} x {MMALCameraConfig.StillResolution.Height}. " +
$"Encoder: {MMALCameraConfig.StillEncoding.EncodingName}. Pixel Format: {MMALCameraConfig.StillSubFormat.EncodingName}.");

await BeginProcessing(this.Camera, null, this.Camera.StillPort, MMALCameraComponent.MMALCameraStillPort);
}
Expand Down Expand Up @@ -209,7 +207,8 @@ public async Task TakePicture(MMALPortImpl connPort, bool rawBayer = false, bool
//Enable the image encoder output port.
try
{
Console.WriteLine($"Preparing to take picture. Resolution: {MMALCameraConfig.StillResolution.Width} x {MMALCameraConfig.StillResolution.Height}. Encoder: {encoder.EncodingType.EncodingName}. Pixel Format: {encoder.PixelFormat.EncodingName}.");
Console.WriteLine($"Preparing to take picture. Resolution: {MMALCameraConfig.StillResolution.Width} x {MMALCameraConfig.StillResolution.Height}. " +
$"Encoder: {encoder.EncodingType.EncodingName}. Pixel Format: {encoder.PixelFormat.EncodingName}.");

await BeginProcessing(encoder, encoder.Connection, this.Camera.StillPort, 0);
}
Expand All @@ -228,8 +227,13 @@ public async Task TakePicture(MMALPortImpl connPort, bool rawBayer = false, bool
/// <param name="useExif">Specify whether to include EXIF tags in the capture</param>
/// <param name="exifTags">Custom EXIF tags to use in the capture</param>
/// <returns>The awaitable Task</returns>
public async Task TakePictureTimeout(MMALPortImpl connPort, DateTime timeout, bool rawBayer = false, bool useExif = true, params ExifTag[] exifTags)
{
public async Task TakePictureTimeout(MMALPortImpl connPort, DateTime timeout, bool rawBayer = false, bool useExif = true, bool burstMode = false, params ExifTag[] exifTags)
{
if(burstMode)
{
this.Camera.StillPort.SetParameter(MMALParametersCamera.MMAL_PARAMETER_CAMERA_BURST_CAPTURE, true);
}

while (DateTime.Now.CompareTo(timeout) < 0)
{
await TakePicture(connPort, rawBayer, useExif, exifTags);
Expand Down Expand Up @@ -305,19 +309,16 @@ private async Task BeginProcessing(MMALComponentBase component, MMALConnectionIm
component.CleanPortPools();
}



/// <summary>
/// Helper method to create a new preview component
/// </summary>
/// <param name="renderer">The renderer type</param>
/// <returns>The static Camera instance</returns>
/// <returns>The camera instance</returns>
public MMALCamera CreatePreviewComponent(MMALRendererBase renderer)
{
if (this.Preview != null)
{
this.Preview?.Connection.Disable();
this.Preview?.Connection.Destroy();
this.Preview.Dispose();
}
this.DestroyPreviewComponent();

this.Preview = renderer;
this.Preview.CreateConnection(this.Camera.PreviewPort);
Expand All @@ -327,7 +328,7 @@ public MMALCamera CreatePreviewComponent(MMALRendererBase renderer)
/// <summary>
/// Helper method to create a splitter component
/// </summary>
/// <returns>The static Camera instance</returns>
/// <returns>The camera instance</returns>
public MMALCamera CreateSplitterComponent()
{
this.Splitter = new MMALSplitterComponent();
Expand All @@ -339,7 +340,7 @@ public MMALCamera CreateSplitterComponent()
/// </summary>
/// <param name="encoder">The encoder component to attach to the output port</param>
/// <param name="outputPort">The output port to attach to</param>
/// <returns>The static Camera instance</returns>
/// <returns>The camera instance</returns>
public MMALCamera AddEncoder(MMALEncoderBase encoder, MMALPortImpl outputPort)
{
if (MMALCameraConfig.Debug)
Expand All @@ -359,7 +360,7 @@ public MMALCamera AddEncoder(MMALEncoderBase encoder, MMALPortImpl outputPort)
/// Remove an encoder component from an output port
/// </summary>
/// <param name="outputPort">The output port we are removing an encoder component from</param>
/// <returns>The static Camera instance</returns>
/// <returns>The camera instance</returns>
public MMALCamera RemoveEncoder(MMALPortImpl outputPort)
{
var enc = this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == outputPort).FirstOrDefault();
Expand Down Expand Up @@ -399,26 +400,76 @@ public void EnableCamera()
}

/// <summary>
/// Configures the camera component. This method applies configuration settings and initialises the components required
/// for capturing images.
/// Reconfigures the Camera's still port.
/// </summary>
/// <returns>The static Camera instance</returns>
public MMALCamera ConfigureCamera()
/// <returns>The camera instance</returns>
public MMALCamera ConfigureStill()
{
if (MMALCameraConfig.Debug)
{
Console.WriteLine("Configuring camera parameters.");
}

this.DisableCamera();

this.Camera.SetCameraParameters();
this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.StillPort).ToList().ForEach(c => c.Connection.Disable());

this.Camera.InitialiseStill();

this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.StillPort).ToList().ForEach(c => c.Connection.Enable());

this.EnableCamera();


return this;
}

/// <summary>
/// Reconfigures the Camera's video port.
/// </summary>
/// <returns>The camera instance</returns>
public MMALCamera ConfigureVideo()
{
this.DisableCamera();

this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.VideoPort).ToList().ForEach(c => c.Connection.Disable());

this.Camera.InitialiseVideo();

this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.VideoPort).ToList().ForEach(c => c.Connection.Enable());

this.EnableCamera();

return this;
}

/// <summary>
/// Reconfigures the Camera's preview port.
/// </summary>
/// <returns>The camera instance</returns>
public MMALCamera ConfigurePreview()
{
this.DisableCamera();

this.Preview?.Connection?.Disable();
this.Camera.InitialisePreview();
this.Preview?.Connection?.Enable();

this.EnableCamera();

return this;
}



private void DestroyPreviewComponent()
{
if (this.Preview != null)
{
this.Preview?.Connection.Disable();
this.Preview?.Connection.Destroy();
this.Preview.Dispose();
}
}

private void DestroyEncoders()
{
this.Encoders.ForEach(c => c.Dispose());
}

/// <summary>
/// Helper method to check the Renderer component status. If a Renderer has not been initialized, a warning will
/// be shown to the user. If a Renderer has been created but a connection has not been initialized, this will be
Expand Down
2 changes: 1 addition & 1 deletion src/MMALSharp/MMALCameraConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static class MMALCameraConfig
/// </summary>
public static void Reload()
{
MMALCamera.Instance.ConfigureCamera();
MMALCamera.Instance.Camera.SetCameraParameters();
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/MMALSharp/MMALPortBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ internal void DestroyPortPool()
{
if (this.BufferPool != null)
{
if (this.Enabled)
this.DisablePort();

MMALUtil.mmal_port_pool_destroy(this.Ptr, this.BufferPool.Ptr);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/MMALSharp/Native/MMALFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ public MMAL_SUBPICTURE_FORMAT_T(uint xOffset, uint yOffset)
}
}

[StructLayout(LayoutKind.Sequential)]
//Union type.
[StructLayout(LayoutKind.Explicit)]
public struct MMAL_ES_SPECIFIC_FORMAT_T
{
[FieldOffset(0)]
public MMAL_AUDIO_FORMAT_T audio;
[FieldOffset(0)]
public MMAL_VIDEO_FORMAT_T video;
[FieldOffset(0)]
public MMAL_SUBPICTURE_FORMAT_T subpicture;

public MMAL_ES_SPECIFIC_FORMAT_T(MMAL_AUDIO_FORMAT_T audio, MMAL_VIDEO_FORMAT_T video, MMAL_SUBPICTURE_FORMAT_T subpicture)
Expand Down
20 changes: 7 additions & 13 deletions tests/MMALSharp.Tests/ImageEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,8 @@ public void TakePicture(string extension, MMALEncoding encodingType, MMALEncodin
//Create our component pipeline.
fixture.MMALCamera
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
.CreatePreviewComponent(new MMALNullSinkComponent())
.ConfigureCamera();

.CreatePreviewComponent(new MMALNullSinkComponent());

await fixture.MMALCamera.TakePicture(fixture.MMALCamera.Camera.StillPort);
}

Expand Down Expand Up @@ -381,8 +380,7 @@ public void TakePictureRawBayer(string extension, MMALEncoding encodingType, MMA
//Create our component pipeline.
fixture.MMALCamera
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
.CreatePreviewComponent(new MMALNullSinkComponent())
.ConfigureCamera();
.CreatePreviewComponent(new MMALNullSinkComponent());

await fixture.MMALCamera.TakePicture(fixture.MMALCamera.Camera.StillPort, true);
}
Expand Down Expand Up @@ -414,8 +412,7 @@ public void TakePictureTimelapse(string extension, MMALEncoding encodingType, MM
//Create our component pipeline.
fixture.MMALCamera
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
.CreatePreviewComponent(new MMALNullSinkComponent())
.ConfigureCamera();
.CreatePreviewComponent(new MMALNullSinkComponent());

await fixture.MMALCamera.TakePictureTimelapse(fixture.MMALCamera.Camera.StillPort,
new Timelapse { Mode = TimelapseMode.Second, Value = 5, Timeout = DateTime.Now.AddSeconds(20) });
Expand All @@ -438,8 +435,7 @@ public void TakePictureTimeout(string extension, MMALEncoding encodingType, MMAL
//Create our component pipeline.
fixture.MMALCamera
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
.CreatePreviewComponent(new MMALNullSinkComponent())
.ConfigureCamera();
.CreatePreviewComponent(new MMALNullSinkComponent());

await fixture.MMALCamera.TakePictureTimeout(fixture.MMALCamera.Camera.StillPort, DateTime.Now.AddSeconds(20));

Expand All @@ -462,8 +458,7 @@ public void ChangeEncodingType()
//Create our component pipeline.
fixture.MMALCamera
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
.CreatePreviewComponent(new MMALNullSinkComponent())
.ConfigureCamera();
.CreatePreviewComponent(new MMALNullSinkComponent());

await fixture.MMALCamera.TakePicture(fixture.MMALCamera.Camera.StillPort);
}
Expand All @@ -485,8 +480,7 @@ public void ChangeEncodingType()
//Create our component pipeline.
fixture.MMALCamera
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
.CreatePreviewComponent(new MMALNullSinkComponent())
.ConfigureCamera();
.CreatePreviewComponent(new MMALNullSinkComponent());

await fixture.MMALCamera.TakePicture(fixture.MMALCamera.Camera.StillPort);
}
Expand Down
Loading

0 comments on commit 0bc3731

Please sign in to comment.