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

Commit

Permalink
Closes #139 - Single ctor in MMALPortConfig and make use of named & o…
Browse files Browse the repository at this point in the history
…ptional params.
  • Loading branch information
techyian committed May 7, 2020
1 parent 1716312 commit b7820c7
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 205 deletions.
7 changes: 3 additions & 4 deletions src/MMALSharp.Demo/ImageOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using MMALSharp.Common;
using MMALSharp.Components;
using MMALSharp.Handlers;
using MMALSharp.Native;
using MMALSharp.Ports;

namespace MMALSharp.Demo
Expand Down Expand Up @@ -82,7 +81,7 @@ private async Task TakePictureManual(string extension, MMALEncoding encoding, MM
this.Cam.ConfigureCameraSettings();
await Task.Delay(2000);

var encoderConfig = new MMALPortConfig(encoding, pixelFormat, 90);
var encoderConfig = new MMALPortConfig(encoding, pixelFormat, quality: 90);

// Create our component pipeline.
imgEncoder.ConfigureOutputPort(encoderConfig, imgCaptureHandler);
Expand Down Expand Up @@ -113,8 +112,8 @@ private async Task ResizePicture(string extension, MMALEncoding encoding, MMALEn

await Task.Delay(2000);

var resizerConfig = new MMALPortConfig(pixelFormat, pixelFormat, width, height, 0, 0, 0, false, null);
var encoderConfig = new MMALPortConfig(encoding, pixelFormat, 90);
var resizerConfig = new MMALPortConfig(pixelFormat, pixelFormat, width: width, height: height);
var encoderConfig = new MMALPortConfig(encoding, pixelFormat, quality: 90);

// Create our component pipeline.
resizer.ConfigureInputPort(new MMALPortConfig(MMALCameraConfig.Encoding, MMALCameraConfig.EncodingSubFormat), this.Cam.Camera.StillPort, null);
Expand Down
2 changes: 1 addition & 1 deletion src/MMALSharp.Demo/VideoOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private async Task TakeVideoManual(string extension, MMALEncoding encoding, MMAL
{
this.Cam.ConfigureCameraSettings();

var portConfig = new MMALPortConfig(encoding, pixelFormat, 0, bitrate, null);
var portConfig = new MMALPortConfig(encoding, pixelFormat, bitrate: bitrate);

vidEncoder.ConfigureOutputPort(portConfig, vidCaptureHandler);

Expand Down
19 changes: 15 additions & 4 deletions src/MMALSharp/Components/EncoderComponents/MMALVideoEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public override IDownstreamComponent ConfigureOutputPort(int outputPort, IMMALPo
this.Quality = config.Quality;

var bufferSize = 0;
var framerate = 0;

if (config.EncodingType == MMALEncoding.H264)
{
Expand All @@ -83,9 +82,21 @@ public override IDownstreamComponent ConfigureOutputPort(int outputPort, IMMALPo
var bitrate = this.GetValidBitrate(outputPort, config);

// Force framerate to be 0 in case it was provided by user.
config = new MMALPortConfig(config.EncodingType, config.PixelFormat, config.Width, config.Height, framerate,
config.Quality, bitrate, config.ZeroCopy, config.Timeout, config.Split, config.BufferNum, bufferSize, config.Crop,
config.StoreMotionVectors);
config = new MMALPortConfig(
config.EncodingType,
config.PixelFormat,
config.Quality,
bitrate,
config.Timeout,
config.Split,
config.StoreMotionVectors,
config.Width,
config.Height,
config.Framerate,
config.ZeroCopy,
config.BufferNum,
bufferSize,
config.Crop);

base.ConfigureOutputPort(outputPort, config, handler);

Expand Down
54 changes: 34 additions & 20 deletions src/MMALSharp/Components/MMALCameraComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,12 @@ private void SetSensorDefaults()
/// </summary>
private void InitialisePreview()
{
var portConfig = new MMALPortConfig(MMALCameraConfig.Encoding, MMALCameraConfig.EncodingSubFormat,
MMALCameraConfig.Resolution.Width, MMALCameraConfig.Resolution.Height,
MMALCameraConfig.Framerate.Num, 0, 0, false, null, 0, 0);
var portConfig = new MMALPortConfig(
MMALCameraConfig.Encoding,
MMALCameraConfig.EncodingSubFormat,
width: MMALCameraConfig.Resolution.Width,
height: MMALCameraConfig.Resolution.Height,
framerate: MMALCameraConfig.Framerate.Num);

MMALLog.Logger.LogDebug("Commit preview");

Expand Down Expand Up @@ -261,12 +264,15 @@ private void InitialiseVideo(IOutputCaptureHandler handler)
currentHeight = this.CameraInfo.MaxHeight;
}

var portConfig = new MMALPortConfig(MMALCameraConfig.Encoding, MMALCameraConfig.EncodingSubFormat,
currentWidth, currentHeight,
MMALCameraConfig.Framerate.Num, 0, 0, false, null,
Math.Max(this.VideoPort.BufferNumRecommended, 3),
Math.Max(this.VideoPort.BufferSizeRecommended, this.VideoPort.BufferSizeMin),
new Rectangle(0, 0, currentWidth, currentHeight));
var portConfig = new MMALPortConfig(
MMALCameraConfig.Encoding,
MMALCameraConfig.EncodingSubFormat,
width: currentWidth,
height: currentHeight,
framerate: MMALCameraConfig.Framerate.Num,
bufferNum: Math.Max(this.VideoPort.BufferNumRecommended, 3),
bufferSize: Math.Max(this.VideoPort.BufferSizeRecommended, this.VideoPort.BufferSizeMin),
crop: new Rectangle(0, 0, currentWidth, currentHeight));

MMALLog.Logger.LogDebug("Commit video");

Expand Down Expand Up @@ -328,22 +334,30 @@ private void InitialiseStill(IOutputCaptureHandler handler)
MMALLog.Logger.LogWarning("Using old firmware. Setting encoding to BGR24");
encoding = MMALEncoding.BGR24;
}

portConfig = new MMALPortConfig(encoding, null, resolution.Width, resolution.Height,
MMALCameraConfig.Framerate.Num, 0, 0, false, null,
Math.Max(this.StillPort.BufferNumRecommended, 3),
Math.Max(this.StillPort.BufferSizeRecommended, this.StillPort.BufferSizeMin),
new Rectangle(0, 0, currentWidth, currentHeight));

portConfig = new MMALPortConfig(
encoding,
null,
width: currentWidth,
height: currentHeight,
framerate: MMALCameraConfig.Framerate.Num,
bufferNum: Math.Max(this.StillPort.BufferNumRecommended, 3),
bufferSize: Math.Max(this.StillPort.BufferSizeRecommended, this.StillPort.BufferSizeMin),
crop: new Rectangle(0, 0, currentWidth, currentHeight));
}
else
{
var resolution = MMALCameraConfig.Resolution.Pad();

portConfig = new MMALPortConfig(MMALCameraConfig.Encoding, MMALCameraConfig.EncodingSubFormat, resolution.Width, resolution.Height,
MMALCameraConfig.Framerate.Num, 0, 0, false, null,
Math.Max(this.StillPort.BufferNumRecommended, 3),
Math.Max(this.StillPort.BufferSizeRecommended, this.StillPort.BufferSizeMin),
new Rectangle(0, 0, currentWidth, currentHeight));
portConfig = new MMALPortConfig(
MMALCameraConfig.Encoding,
MMALCameraConfig.EncodingSubFormat,
width: resolution.Width,
height: resolution.Height,
framerate: MMALCameraConfig.Framerate.Num,
bufferNum: Math.Max(this.StillPort.BufferNumRecommended, 3),
bufferSize: Math.Max(this.StillPort.BufferSizeRecommended, this.StillPort.BufferSizeMin),
crop: new Rectangle(0, 0, currentWidth, currentHeight));
}

MMALLog.Logger.LogDebug("Commit still");
Expand Down
8 changes: 6 additions & 2 deletions src/MMALSharp/Components/MMALRendererComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,12 @@ public MMALOverlayRenderer(MMALVideoRenderer parent, PreviewOverlayConfiguration
{
throw new PiCameraError($"Incompatible encoding type for use with Preview Render overlay {this.Inputs[0].NativeEncodingType.ParseEncoding().EncodingName}.");
}

var portConfig = new MMALPortConfig(config.Encoding, null, width, height, 0, 0, 0, false, null, 0, 0);

var portConfig = new MMALPortConfig(
config.Encoding,
null,
width: width,
height: height);

this.ConfigureInputPort(portConfig, null);

Expand Down
34 changes: 16 additions & 18 deletions src/MMALSharp/Components/MMALSplitterComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,26 @@ public unsafe MMALSplitterComponent()
public override IDownstreamComponent ConfigureInputPort(IMMALPortConfig config, IPort copyPort, IInputCaptureHandler handler)
{
var bufferNum = Math.Max(Math.Max(this.Inputs[0].BufferNumRecommended, 3), config.BufferNum);

config = new MMALPortConfig(config.EncodingType, config.PixelFormat, config.Width, config.Height, config.Framerate,
config.Quality, config.Bitrate, config.ZeroCopy, config.Timeout, bufferNum, config.BufferSize, config.Crop,
config.StoreMotionVectors);

config = new MMALPortConfig(
config.EncodingType,
config.PixelFormat,
config.Quality,
config.Bitrate,
config.Timeout,
config.Split,
config.StoreMotionVectors,
config.Width,
config.Height,
config.Framerate,
config.ZeroCopy,
bufferNum,
config.BufferSize,
config.Crop);

base.ConfigureInputPort(config, copyPort, handler);

return this;
}

/// <inheritdoc />
public override IDownstreamComponent ConfigureInputPort(IMMALPortConfig config, IInputCaptureHandler handler)
{
var bufferNum = Math.Max(Math.Max(this.Inputs[0].BufferNumRecommended, 3), config.BufferNum);

config = new MMALPortConfig(config.EncodingType, config.PixelFormat, config.Width, config.Height, config.Framerate,
config.Quality, config.Bitrate, config.ZeroCopy, config.Timeout, bufferNum, config.BufferSize, config.Crop,
config.StoreMotionVectors);

base.ConfigureInputPort(config, handler);

return this;
}
}
}
4 changes: 2 additions & 2 deletions src/MMALSharp/MMALCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task TakeRawVideo(IVideoCaptureHandler handler, CancellationToken c
{
this.ConfigureCameraSettings();

var splitterOutputConfig = new MMALPortConfig(MMALCameraConfig.Encoding, MMALCameraConfig.EncodingSubFormat, 0);
var splitterOutputConfig = new MMALPortConfig(MMALCameraConfig.Encoding, MMALCameraConfig.EncodingSubFormat);

// Force port type to SplitterVideoPort to prevent resolution from being set against splitter component.
splitter.ConfigureOutputPort<SplitterVideoPort>(0, splitterOutputConfig, handler);
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task TakeVideo(IVideoCaptureHandler handler, CancellationToken canc
{
this.ConfigureCameraSettings();

var portConfig = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 10, MMALVideoEncoder.MaxBitrateLevel4, null, split);
var portConfig = new MMALPortConfig(MMALEncoding.H264, MMALEncoding.I420, 10, MMALVideoEncoder.MaxBitrateLevel4, split: split);

vidEncoder.ConfigureOutputPort(portConfig, handler);

Expand Down
112 changes: 27 additions & 85 deletions src/MMALSharp/Ports/MMALPortConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Drawing;
using MMALSharp.Common;
using MMALSharp.Config;
using MMALSharp.Native;

namespace MMALSharp.Ports
{
Expand All @@ -25,24 +24,24 @@ public class MMALPortConfig : IMMALPortConfig
/// The pixel format this output port will send data in.
/// </summary>
public MMALEncoding PixelFormat { get; }

/// <summary>
/// User provided width of output frame.
/// The input/output width value.
/// </summary>
public int Width { get; }

/// <summary>
/// User provided height of output frame.
/// The input/output height value.
/// </summary>
public int Height { get; }

/// <summary>
/// The framerate of the outputted data.
/// </summary>
public int Framerate { get; }

/// <summary>
/// The quality of our outputted data.
/// The quality value. Can be used with JPEG encoding (value between 1-100). Can be used with H.264 encoding which affects the quantization parameter (typical values between 10-40, see wiki for info). Set both bitrate param and quality param to 0 for variable bitrate.
/// </summary>
public int Quality { get; }

Expand Down Expand Up @@ -87,94 +86,37 @@ public class MMALPortConfig : IMMALPortConfig
public bool StoreMotionVectors { get; }

/// <summary>
/// Create a new instance of <see cref="MMALPortConfig"/> with parameters useful for image capture.
/// </summary>
/// <param name="encodingType">The encoding type.</param>
/// <param name="pixelFormat">The pixel format.</param>
/// <param name="quality">The output quality. Only affects JPEG quality for image stills.</param>
public MMALPortConfig(MMALEncoding encodingType, MMALEncoding pixelFormat, int quality = 0)
{
this.EncodingType = encodingType;
this.PixelFormat = pixelFormat;
this.Quality = quality;
}

/// <summary>
/// Create a new instance of <see cref="MMALPortConfig"/> with parameters useful for video capture.
/// Create a new instance of <see cref="MMALPortConfig"/>.
/// </summary>
/// <param name="encodingType">The encoding type.</param>
/// <param name="pixelFormat">The pixel format.</param>
/// <param name="quality">The output quality. Affects the quantization parameter for H.264 encoding. Set bitrate 0 and set this for variable bitrate.</param>
/// <param name="bitrate">The output bitrate.</param>
/// <param name="quality">The quality value. Can be used with JPEG encoding (value between 1-100). Can be used with H.264 encoding which affects the quantization parameter (typical values between 10-40, see wiki for info). Set both bitrate param and quality param to 0 for variable bitrate.</param>
/// <param name="bitrate">The working bitrate, applies to Video Encoder only.</param>
/// <param name="timeout">Video record timeout. This is useful if you have multiple video recording streams which you want to stop at different times.</param>
/// <param name="split">Video split configuration object.</param>
/// <param name="storeMotionVectors">Indicates whether to store motion vectors. Applies to H.264 video encoding.</param>
public MMALPortConfig(MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, int bitrate, DateTime? timeout, Split split = null, bool storeMotionVectors = false)
{
this.EncodingType = encodingType;
this.PixelFormat = pixelFormat;
this.Quality = quality;
this.Bitrate = bitrate;
this.Timeout = timeout;
this.Split = split;
this.StoreMotionVectors = storeMotionVectors;
}

/// <summary>
/// Create a new instance of <see cref="MMALPortConfig"/> with parameters useful for standalone image/video processing.
/// </summary>
/// <param name="encodingType">The encoding type.</param>
/// <param name="pixelFormat">The pixel format.</param>
/// <param name="width">The output width.</param>
/// <param name="height">The output height.</param>
/// <param name="framerate">The output framerate.</param>
/// <param name="quality">The output quality.</param>
/// <param name="bitrate">The output bitrate.</param>
/// <param name="zeroCopy">Specify zero copy.</param>
/// <param name="timeout">Video record timeout.</param>
/// <param name="bufferNum">Requested number of buffer headers.</param>
/// <param name="bufferSize">Requested size of buffer headers.</param>
/// <param name="crop">The Region of Interest requested.</param>
/// <param name="storeMotionVectors">Indicates whether to store motion vectors. Applies to H.264 video encoding.</param>
public MMALPortConfig(MMALEncoding encodingType, MMALEncoding pixelFormat, int width, int height, int framerate,
int quality, int bitrate, bool zeroCopy, DateTime? timeout, int bufferNum = 0, int bufferSize = 0, Rectangle? crop = null,
bool storeMotionVectors = false)
{
this.EncodingType = encodingType;
this.PixelFormat = pixelFormat;
this.Width = width;
this.Height = height;
this.Framerate = framerate;
this.Quality = quality;
this.Bitrate = bitrate;
this.ZeroCopy = zeroCopy;
this.Timeout = timeout;
this.BufferNum = bufferNum;
this.BufferSize = bufferSize;
this.Crop = crop;
this.StoreMotionVectors = storeMotionVectors;
}

/// <summary>
/// Create a new instance of <see cref="MMALPortConfig"/>.
/// </summary>
/// <param name="encodingType">The encoding type.</param>
/// <param name="pixelFormat">The pixel format.</param>
/// <param name="width">The output width.</param>
/// <param name="height">The output height.</param>
/// <param name="framerate">The output framerate.</param>
/// <param name="quality">The output quality.</param>
/// <param name="bitrate">The output bitrate.</param>
/// <param name="zeroCopy">Specify zero copy.</param>
/// <param name="timeout">Video record timeout.</param>
/// <param name="split">Video split configuration object.</param>
/// <param name="width">The input/output width value.</param>
/// <param name="height">The input/output height value.</param>
/// <param name="framerate">Framerate value. Only useful when not using the camera component to specify input framerate.</param>
/// <param name="zeroCopy">Instruct MMAL to not copy buffers to ARM memory (useful for large buffers and handling raw data).</param>
/// <param name="bufferNum">Requested number of buffer headers.</param>
/// <param name="bufferSize">Requested size of buffer headers.</param>
/// <param name="crop">The Region of Interest requested.</param>
/// <param name="storeMotionVectors">Indicates whether to store motion vectors. Applies to H.264 video encoding.</param>
public MMALPortConfig(MMALEncoding encodingType, MMALEncoding pixelFormat, int width, int height, int framerate,
int quality, int bitrate, bool zeroCopy, DateTime? timeout, Split split, int bufferNum, int bufferSize, Rectangle? crop,
bool storeMotionVectors)
public MMALPortConfig(
MMALEncoding encodingType,
MMALEncoding pixelFormat,
int quality = 0,
int bitrate = 0,
DateTime? timeout = null,
Split split = null,
bool storeMotionVectors = false,
int width = 0,
int height = 0,
int framerate = 0,
bool zeroCopy = false,
int bufferNum = 0,
int bufferSize = 0,
Rectangle? crop = null)
{
this.EncodingType = encodingType;
this.PixelFormat = pixelFormat;
Expand Down
Loading

0 comments on commit b7820c7

Please sign in to comment.