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

Commit 5a96edf

Browse files
committed
Breaking change: MMALPortConfig
Added new MMALPortConfig class to simplify port configuration - too many overloads were complicating port classes. Also moved width/height to ports.
1 parent 9583e56 commit 5a96edf

31 files changed

Lines changed: 663 additions & 635 deletions

src/MMALSharp/Components/EncoderComponents/MMALImageDecoder.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ namespace MMALSharp.Components
1414
/// </summary>
1515
public class MMALImageDecoder : MMALEncoderBase
1616
{
17-
private int _width;
18-
private int _height;
19-
2017
/// <summary>
2118
/// Creates a new instance of <see cref="MMALImageDecoder"/>.
2219
/// </summary>
@@ -25,44 +22,5 @@ public MMALImageDecoder(ICaptureHandler handler)
2522
: base(MMALParameters.MMAL_COMPONENT_DEFAULT_IMAGE_DECODER, handler)
2623
{
2724
}
28-
29-
/// <inheritdoc />
30-
public override int Width
31-
{
32-
get
33-
{
34-
if (this._width == 0)
35-
{
36-
return MMALCameraConfig.StillResolution.Width;
37-
}
38-
39-
return this._width;
40-
}
41-
set => this._width = value;
42-
}
43-
44-
/// <inheritdoc />
45-
public override int Height
46-
{
47-
get
48-
{
49-
if (this._height == 0)
50-
{
51-
return MMALCameraConfig.StillResolution.Height;
52-
}
53-
54-
return this._height;
55-
}
56-
set => this._height = value;
57-
}
58-
59-
/// <summary>
60-
/// Prints a summary of the ports and the resolution associated with this component to the console.
61-
/// </summary>
62-
public override void PrintComponent()
63-
{
64-
base.PrintComponent();
65-
MMALLog.Logger.Info($" Width: {this.Width}. Height: {this.Height}");
66-
}
6725
}
6826
}

src/MMALSharp/Components/EncoderComponents/MMALImageEncoder.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using MMALSharp.Config;
1313
using MMALSharp.Handlers;
1414
using MMALSharp.Native;
15+
using MMALSharp.Ports;
1516
using MMALSharp.Ports.Outputs;
1617
using static MMALSharp.MMALNativeExceptionHelper;
1718

@@ -27,27 +28,6 @@ public unsafe class MMALImageEncoder : MMALEncoderBase
2728
/// </summary>
2829
public const int MaxExifPayloadLength = 128;
2930

30-
private int _width;
31-
private int _height;
32-
33-
/// <summary>
34-
/// Gets or sets the width of the resulting image. If not specified, the camera's still resolution is used.
35-
/// </summary>
36-
public override int Width
37-
{
38-
get => _width == 0 ? MMALCameraConfig.StillResolution.Width : _width;
39-
set => _width = value;
40-
}
41-
42-
/// <summary>
43-
/// Gets or sets the height of the resulting image. If not specified, the camera's still resolution is used.
44-
/// </summary>
45-
public override int Height
46-
{
47-
get => _height == 0 ? MMALCameraConfig.StillResolution.Height : _height;
48-
set => _height = value;
49-
}
50-
5131
/// <summary>
5232
/// When enabled, raw bayer metadata will be included in JPEG still captures.
5333
/// </summary>
@@ -93,10 +73,10 @@ public MMALImageEncoder(ICaptureHandler handler, bool rawBayer = false, bool use
9373
this.JpegThumbnailConfig = thumbnailConfig;
9474
}
9575

96-
/// <inheritdoc />>
97-
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, bool zeroCopy = false)
76+
/// <inheritdoc />
77+
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALPortConfig config)
9878
{
99-
base.ConfigureOutputPort(outputPort, encodingType, pixelFormat, quality, zeroCopy);
79+
base.ConfigureOutputPort(outputPort, config);
10080

10181
if (this.RawBayer)
10282
{
@@ -128,15 +108,6 @@ public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMAL
128108
return this;
129109
}
130110

131-
/// <summary>
132-
/// Prints a summary of the ports and the resolution associated with this component to the console.
133-
/// </summary>
134-
public override void PrintComponent()
135-
{
136-
base.PrintComponent();
137-
MMALLog.Logger.Info($" Width: {this.Width}. Height: {this.Height}");
138-
}
139-
140111
internal override void InitialiseOutputPort(int outputPort)
141112
{
142113
if (this.ContinuousCapture)

src/MMALSharp/Components/EncoderComponents/MMALImageFileDecoder.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// </copyright>
55

66
using System;
7+
using System.Drawing;
78
using System.Threading.Tasks;
89
using MMALSharp.Native;
910
using System.Text;
@@ -36,23 +37,23 @@ public MMALImageFileDecoder(ICaptureHandler handler)
3637
/// </summary>
3738
public static MMALQueueImpl WorkingQueue { get; set; }
3839

39-
/// <inheritdoc />>
40-
public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding encodingType, MMALEncoding pixelFormat, int width, int height, bool zeroCopy = false)
40+
/// <inheritdoc />
41+
public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALPortConfig config)
4142
{
4243
this.InitialiseInputPort(0);
4344

44-
if (encodingType != null)
45+
if (config.EncodingType != null)
4546
{
46-
this.Inputs[0].Ptr->Format->Encoding = encodingType.EncodingVal;
47+
this.Inputs[0].Ptr->Format->Encoding = config.EncodingType.EncodingVal;
4748
}
4849

4950
this.Inputs[0].Ptr->Format->Type = MMALFormat.MMAL_ES_TYPE_T.MMAL_ES_TYPE_VIDEO;
50-
this.Inputs[0].Ptr->Format->Es->Video.Height = 0;
51-
this.Inputs[0].Ptr->Format->Es->Video.Width = 0;
51+
this.Inputs[0].Resolution = new Resolution(config.Width, config.Height);
52+
this.Inputs[0].Crop = new Rectangle(0, 0, config.Width, config.Height);
5253
this.Inputs[0].Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(0, 1);
5354
this.Inputs[0].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
5455

55-
this.Inputs[0].EncodingType = encodingType;
56+
this.Inputs[0].EncodingType = config.EncodingType;
5657

5758
this.Inputs[0].Commit();
5859

@@ -62,8 +63,8 @@ public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding e
6263
return this;
6364
}
6465

65-
/// <inheritdoc />>
66-
public override unsafe MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, int bitrate = 0, bool zeroCopy = false)
66+
/// <inheritdoc />
67+
public override unsafe MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALPortConfig config)
6768
{
6869
this.InitialiseOutputPort(outputPort);
6970

@@ -74,20 +75,23 @@ public override unsafe MMALDownstreamComponent ConfigureOutputPort(int outputPor
7475

7576
this.ProcessingPorts.Add(outputPort, this.Outputs[outputPort]);
7677

77-
if (encodingType != null)
78+
if (config.EncodingType != null)
7879
{
79-
this.Outputs[outputPort].Ptr->Format->Encoding = encodingType.EncodingVal;
80+
this.Outputs[outputPort].Ptr->Format->Encoding = config.EncodingType.EncodingVal;
8081
}
8182

82-
if (zeroCopy)
83+
if (config.ZeroCopy)
8384
{
8485
this.Outputs[outputPort].ZeroCopy = true;
8586
this.Outputs[outputPort].SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);
8687
}
8788

89+
this.Outputs[outputPort].Resolution = new Resolution(config.Width, config.Height).Pad();
90+
this.Outputs[outputPort].Crop = new Rectangle(0, 0, config.Width, config.Height);
91+
8892
this.Outputs[outputPort].Commit();
8993

90-
this.Outputs[outputPort].EncodingType = encodingType;
94+
this.Outputs[outputPort].EncodingType = config.EncodingType;
9195

9296
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumRecommended, this.Outputs[outputPort].Ptr->BufferNumMin);
9397
this.Outputs[outputPort].Ptr->BufferSize = Math.Max(this.Outputs[outputPort].Ptr->BufferSizeRecommended, this.Outputs[outputPort].Ptr->BufferSizeMin);
@@ -205,8 +209,8 @@ private unsafe void ConfigureOutputPortWithoutInit(int outputPort, MMALEncoding
205209

206210
this.Outputs[outputPort].EncodingType = encodingType;
207211

208-
this.Outputs[outputPort].Ptr->BufferNum = 2;
209-
this.Outputs[outputPort].Ptr->BufferSize = this.Outputs[outputPort].Ptr->BufferSizeRecommended;
212+
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumMin, this.Outputs[outputPort].Ptr->BufferNumRecommended);
213+
this.Outputs[outputPort].Ptr->BufferSize = Math.Max(this.Outputs[outputPort].Ptr->BufferSizeMin, this.Outputs[outputPort].Ptr->BufferSizeRecommended);
210214

211215
MMALLog.Logger.Info($"New buffer number {this.Outputs[outputPort].Ptr->BufferNum}");
212216
MMALLog.Logger.Info($"New buffer size {this.Outputs[outputPort].Ptr->BufferSize}");
@@ -293,18 +297,15 @@ private void GetAndSendOutputBuffer(int outputPort = 0)
293297

294298
private void ProcessFormatChangedEvent(MMALBufferImpl buffer, int outputPort = 0)
295299
{
296-
MMALLog.Logger.Debug("Received MMAL_EVENT_FORMAT_CHANGED event");
300+
MMALLog.Logger.Info("Received MMAL_EVENT_FORMAT_CHANGED event");
297301

298302
var ev = MMALEventFormat.GetEventFormat(buffer);
299303

300-
MMALLog.Logger.Debug("-- Event format changed from -- ");
304+
MMALLog.Logger.Info("-- Event format changed from -- ");
301305
this.LogFormat(new MMALEventFormat(this.Outputs[outputPort].Format), this.Outputs[outputPort]);
302306

303-
MMALLog.Logger.Debug("-- To -- ");
307+
MMALLog.Logger.Info("-- To -- ");
304308
this.LogFormat(ev, null);
305-
306-
// Port format changed
307-
this.Outputs[outputPort].ManagedOutputCallback.Callback(buffer);
308309

309310
lock (OutputPort.OutputLock)
310311
{

src/MMALSharp/Components/EncoderComponents/MMALImageFileEncoder.cs

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
// Licensed under the MIT License. Please see LICENSE.txt for License info.
44
// </copyright>
55

6+
using System;
7+
using System.Drawing;
68
using System.Threading.Tasks;
79
using MMALSharp.Native;
810
using System.Text;
11+
using MMALSharp.Callbacks.Providers;
912
using MMALSharp.Common.Utility;
1013
using MMALSharp.Handlers;
1114
using MMALSharp.Ports;
@@ -34,14 +37,14 @@ public MMALImageFileEncoder(ICaptureHandler handler)
3437
/// </summary>
3538
public static MMALQueueImpl WorkingQueue { get; set; }
3639

37-
/// <inheritdoc />>
38-
public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding encodingType, MMALEncoding pixelFormat, int width, int height, bool zeroCopy = false)
40+
/// <inheritdoc />
41+
public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALPortConfig config)
3942
{
4043
this.InitialiseInputPort(0);
4144

42-
if (encodingType != null)
45+
if (config.EncodingType != null)
4346
{
44-
this.Inputs[0].Ptr->Format->Encoding = encodingType.EncodingVal;
47+
this.Inputs[0].Ptr->Format->Encoding = config.EncodingType.EncodingVal;
4548
}
4649

4750
/*if (pixelFormat != null)
@@ -50,13 +53,13 @@ public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding e
5053
}*/
5154

5255
this.Inputs[0].Ptr->Format->Type = MMALFormat.MMAL_ES_TYPE_T.MMAL_ES_TYPE_VIDEO;
53-
this.Inputs[0].Ptr->Format->Es->Video.Height = height;
54-
this.Inputs[0].Ptr->Format->Es->Video.Width = width;
56+
this.Inputs[0].Ptr->Format->Es->Video.Height = config.Height;
57+
this.Inputs[0].Ptr->Format->Es->Video.Width = config.Width;
5558
this.Inputs[0].Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(0, 1);
5659
this.Inputs[0].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
57-
this.Inputs[0].Ptr->Format->Es->Video.Crop = new MMAL_RECT_T(0, 0, width, height);
60+
this.Inputs[0].Ptr->Format->Es->Video.Crop = new MMAL_RECT_T(0, 0, config.Width, config.Height);
5861

59-
this.Inputs[0].EncodingType = encodingType;
62+
this.Inputs[0].EncodingType = config.EncodingType;
6063

6164
this.Inputs[0].Commit();
6265

@@ -68,15 +71,60 @@ public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding e
6871
this.Inputs[0].Ptr->BufferNum = this.Inputs[0].Ptr->BufferNumMin;
6972
this.Inputs[0].Ptr->BufferSize = this.Inputs[0].Ptr->BufferSizeMin;
7073

71-
if (zeroCopy)
74+
if (config.ZeroCopy)
7275
{
7376
this.Inputs[0].ZeroCopy = true;
7477
this.Inputs[0].SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);
7578
}
7679

7780
return this;
7881
}
79-
82+
83+
/// <inheritdoc />
84+
public override unsafe MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALPortConfig config)
85+
{
86+
this.Outputs[outputPort].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
87+
88+
this.InitialiseOutputPort(outputPort);
89+
90+
if (this.ProcessingPorts.ContainsKey(outputPort))
91+
{
92+
this.ProcessingPorts.Remove(outputPort);
93+
}
94+
95+
this.ProcessingPorts.Add(outputPort, this.Outputs[outputPort]);
96+
97+
if (config.EncodingType != null)
98+
{
99+
this.Outputs[outputPort].Ptr->Format->Encoding = config.EncodingType.EncodingVal;
100+
}
101+
102+
if (config.PixelFormat != null)
103+
{
104+
this.Outputs[outputPort].Ptr->Format->EncodingVariant = config.PixelFormat.EncodingVal;
105+
}
106+
107+
if (config.ZeroCopy)
108+
{
109+
this.Outputs[outputPort].ZeroCopy = true;
110+
this.Outputs[outputPort].SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);
111+
}
112+
113+
this.Outputs[outputPort].Resolution = new Resolution(config.Width, config.Height).Pad();
114+
this.Outputs[outputPort].Crop = new Rectangle(0, 0, config.Width, config.Height);
115+
116+
this.Outputs[outputPort].Commit();
117+
118+
this.Outputs[outputPort].EncodingType = config.EncodingType;
119+
120+
this.Outputs[outputPort].ManagedOutputCallback = OutputCallbackProvider.FindCallback(this.Outputs[outputPort]);
121+
122+
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumMin, this.Outputs[outputPort].Ptr->BufferNumRecommended);
123+
this.Outputs[outputPort].Ptr->BufferSize = Math.Max(this.Outputs[outputPort].Ptr->BufferSizeMin, this.Outputs[outputPort].Ptr->BufferSizeRecommended);
124+
125+
return this;
126+
}
127+
80128
/// <summary>
81129
/// Encodes/decodes user provided image data.
82130
/// </summary>
@@ -203,8 +251,8 @@ private unsafe void ConfigureOutputPortWithoutInit(int outputPort, MMALEncoding
203251

204252
this.Outputs[outputPort].EncodingType = encodingType;
205253

206-
this.Outputs[outputPort].Ptr->BufferNum = 2;
207-
this.Outputs[outputPort].Ptr->BufferSize = this.Outputs[outputPort].Ptr->BufferSizeRecommended;
254+
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumMin, this.Outputs[outputPort].Ptr->BufferNumRecommended);
255+
this.Outputs[outputPort].Ptr->BufferSize = Math.Max(this.Outputs[outputPort].Ptr->BufferSizeMin, this.Outputs[outputPort].Ptr->BufferSizeRecommended);
208256

209257
MMALLog.Logger.Info($"New buffer number {this.Outputs[outputPort].Ptr->BufferNum}");
210258
MMALLog.Logger.Info($"New buffer size {this.Outputs[outputPort].Ptr->BufferSize}");
@@ -291,18 +339,15 @@ private void GetAndSendOutputBuffer()
291339

292340
private void ProcessFormatChangedEvent(MMALBufferImpl buffer)
293341
{
294-
MMALLog.Logger.Debug("Received MMAL_EVENT_FORMAT_CHANGED event");
342+
MMALLog.Logger.Info("Received MMAL_EVENT_FORMAT_CHANGED event");
295343

296344
var ev = MMALEventFormat.GetEventFormat(buffer);
297345

298-
MMALLog.Logger.Debug("-- Event format changed from -- ");
346+
MMALLog.Logger.Info("-- Event format changed from -- ");
299347
this.LogFormat(new MMALEventFormat(this.Outputs[0].Format), this.Outputs[0]);
300348

301-
MMALLog.Logger.Debug("-- To -- ");
349+
MMALLog.Logger.Info("-- To -- ");
302350
this.LogFormat(ev, null);
303-
304-
// Port format changed
305-
this.Outputs[0].ManagedOutputCallback.Callback(buffer);
306351

307352
lock (OutputPort.OutputLock)
308353
{

0 commit comments

Comments
 (0)