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

Commit 0bc3731

Browse files
author
techyian
committed
Getting ready for 0.2 release. Close #7. Close #15. Close #19. Close #27.
1 parent 49e338d commit 0bc3731

8 files changed

Lines changed: 116 additions & 63 deletions

File tree

src/MMALSharp/Components/MMALCameraComponent.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ internal void Initialise()
136136
this.InitialiseStill();
137137
}
138138

139-
private void InitialisePreview()
139+
internal void InitialisePreview()
140140
{
141141
var vFormat = new MMAL_VIDEO_FORMAT_T(
142142
MMALUtil.VCOS_ALIGN_UP(MMALCameraConfig.VideoResolution.Width, 32),
@@ -157,7 +157,7 @@ private void InitialisePreview()
157157
this.PreviewPort.Commit();
158158
}
159159

160-
private void InitialiseVideo()
160+
internal void InitialiseVideo()
161161
{
162162
if (MMALCameraConfig.VideoResolution.Width == 0 || MMALCameraConfig.VideoResolution.Width > this.CameraInfo.MaxWidth)
163163
{
@@ -194,7 +194,7 @@ private void InitialiseVideo()
194194
this.VideoPort.BufferSizeMin);
195195
}
196196

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

223223
vFormat = new MMAL_VIDEO_FORMAT_T(
224224
MMALUtil.VCOS_ALIGN_UP(MMALCameraConfig.StillResolution.Width, 16),
225225
MMALUtil.VCOS_ALIGN_UP(MMALCameraConfig.StillResolution.Height, 16),
226-
new MMAL_RECT_T(0, 0, MMALCameraConfig.StillResolution.Width,
227-
MMALCameraConfig.StillResolution.Height),
226+
new MMAL_RECT_T(0, 0, MMALCameraConfig.StillResolution.Width, MMALCameraConfig.StillResolution.Height),
228227
new MMAL_RATIONAL_T(0, 1),
229228
this.StillPort.Ptr->Format->es->video.Par,
230229
this.StillPort.Ptr->Format->es->video.ColorSpace
@@ -242,14 +241,14 @@ private void InitialiseStill()
242241
this.StillPort.Ptr->Format->es->video.ColorSpace
243242
);
244243
}
245-
244+
246245
this.StillPort.Ptr->Format->es->video = vFormat;
247246
this.StillPort.Ptr->Format->encoding = MMALCameraConfig.StillEncoding.EncodingVal;
248247
this.StillPort.Ptr->Format->encodingVariant = MMALCameraConfig.StillSubFormat.EncodingVal;
249248

250249
if (MMALCameraConfig.Debug)
251250
Console.WriteLine("Commit still");
252-
251+
253252
this.StillPort.Commit();
254253

255254
this.StillPort.Ptr->BufferNum = Math.Max(this.StillPort.BufferNumRecommended,

src/MMALSharp/Components/MMALComponentBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ internal void CleanPortPools()
220220
if (MMALCameraConfig.Debug)
221221
Console.WriteLine("Destroying port pool");
222222

223+
if (port.Enabled)
224+
port.DisablePort();
225+
223226
port.DestroyPortPool();
224227
port.BufferPool = null;
225228
}
@@ -232,6 +235,9 @@ internal void CleanPortPools()
232235
if (MMALCameraConfig.Debug)
233236
Console.WriteLine("Destroying port pool");
234237

238+
if (port.Enabled)
239+
port.DisablePort();
240+
235241
port.DestroyPortPool();
236242
port.BufferPool = null;
237243
}

src/MMALSharp/MMALCamera.cs

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public async Task TakeVideo(MMALPortImpl connPort, DateTime? timeout = null, Spl
112112

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

117118
((MMALVideoPort)encoder.Outputs.ElementAt(0)).Timeout = timeout;
118119
((MMALVideoEncoder)encoder).Split = split;
@@ -138,19 +139,16 @@ public async Task TakeRawPicture(ICaptureHandler handler)
138139
{
139140
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.");
140141
}
141-
if (handler == null)
142-
{
143-
throw new PiCameraError("No handler specified");
144-
}
145142

146-
this.Camera.Handler = handler;
143+
this.Camera.Handler = handler ?? throw new PiCameraError("No handler specified");
147144

148145
this.CheckPreviewComponentStatus();
149146

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

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

214213
await BeginProcessing(encoder, encoder.Connection, this.Camera.StillPort, 0);
215214
}
@@ -228,8 +227,13 @@ public async Task TakePicture(MMALPortImpl connPort, bool rawBayer = false, bool
228227
/// <param name="useExif">Specify whether to include EXIF tags in the capture</param>
229228
/// <param name="exifTags">Custom EXIF tags to use in the capture</param>
230229
/// <returns>The awaitable Task</returns>
231-
public async Task TakePictureTimeout(MMALPortImpl connPort, DateTime timeout, bool rawBayer = false, bool useExif = true, params ExifTag[] exifTags)
232-
{
230+
public async Task TakePictureTimeout(MMALPortImpl connPort, DateTime timeout, bool rawBayer = false, bool useExif = true, bool burstMode = false, params ExifTag[] exifTags)
231+
{
232+
if(burstMode)
233+
{
234+
this.Camera.StillPort.SetParameter(MMALParametersCamera.MMAL_PARAMETER_CAMERA_BURST_CAPTURE, true);
235+
}
236+
233237
while (DateTime.Now.CompareTo(timeout) < 0)
234238
{
235239
await TakePicture(connPort, rawBayer, useExif, exifTags);
@@ -305,19 +309,16 @@ private async Task BeginProcessing(MMALComponentBase component, MMALConnectionIm
305309
component.CleanPortPools();
306310
}
307311

312+
313+
308314
/// <summary>
309315
/// Helper method to create a new preview component
310316
/// </summary>
311317
/// <param name="renderer">The renderer type</param>
312-
/// <returns>The static Camera instance</returns>
318+
/// <returns>The camera instance</returns>
313319
public MMALCamera CreatePreviewComponent(MMALRendererBase renderer)
314320
{
315-
if (this.Preview != null)
316-
{
317-
this.Preview?.Connection.Disable();
318-
this.Preview?.Connection.Destroy();
319-
this.Preview.Dispose();
320-
}
321+
this.DestroyPreviewComponent();
321322

322323
this.Preview = renderer;
323324
this.Preview.CreateConnection(this.Camera.PreviewPort);
@@ -327,7 +328,7 @@ public MMALCamera CreatePreviewComponent(MMALRendererBase renderer)
327328
/// <summary>
328329
/// Helper method to create a splitter component
329330
/// </summary>
330-
/// <returns>The static Camera instance</returns>
331+
/// <returns>The camera instance</returns>
331332
public MMALCamera CreateSplitterComponent()
332333
{
333334
this.Splitter = new MMALSplitterComponent();
@@ -339,7 +340,7 @@ public MMALCamera CreateSplitterComponent()
339340
/// </summary>
340341
/// <param name="encoder">The encoder component to attach to the output port</param>
341342
/// <param name="outputPort">The output port to attach to</param>
342-
/// <returns>The static Camera instance</returns>
343+
/// <returns>The camera instance</returns>
343344
public MMALCamera AddEncoder(MMALEncoderBase encoder, MMALPortImpl outputPort)
344345
{
345346
if (MMALCameraConfig.Debug)
@@ -359,7 +360,7 @@ public MMALCamera AddEncoder(MMALEncoderBase encoder, MMALPortImpl outputPort)
359360
/// Remove an encoder component from an output port
360361
/// </summary>
361362
/// <param name="outputPort">The output port we are removing an encoder component from</param>
362-
/// <returns>The static Camera instance</returns>
363+
/// <returns>The camera instance</returns>
363364
public MMALCamera RemoveEncoder(MMALPortImpl outputPort)
364365
{
365366
var enc = this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == outputPort).FirstOrDefault();
@@ -399,26 +400,76 @@ public void EnableCamera()
399400
}
400401

401402
/// <summary>
402-
/// Configures the camera component. This method applies configuration settings and initialises the components required
403-
/// for capturing images.
403+
/// Reconfigures the Camera's still port.
404404
/// </summary>
405-
/// <returns>The static Camera instance</returns>
406-
public MMALCamera ConfigureCamera()
405+
/// <returns>The camera instance</returns>
406+
public MMALCamera ConfigureStill()
407407
{
408-
if (MMALCameraConfig.Debug)
409-
{
410-
Console.WriteLine("Configuring camera parameters.");
411-
}
412-
413408
this.DisableCamera();
414409

415-
this.Camera.SetCameraParameters();
410+
this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.StillPort).ToList().ForEach(c => c.Connection.Disable());
411+
412+
this.Camera.InitialiseStill();
413+
414+
this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.StillPort).ToList().ForEach(c => c.Connection.Enable());
416415

417416
this.EnableCamera();
418-
417+
418+
return this;
419+
}
420+
421+
/// <summary>
422+
/// Reconfigures the Camera's video port.
423+
/// </summary>
424+
/// <returns>The camera instance</returns>
425+
public MMALCamera ConfigureVideo()
426+
{
427+
this.DisableCamera();
428+
429+
this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.VideoPort).ToList().ForEach(c => c.Connection.Disable());
430+
431+
this.Camera.InitialiseVideo();
432+
433+
this.Encoders.Where(c => c.Connection != null && c.Connection.OutputPort == this.Camera.VideoPort).ToList().ForEach(c => c.Connection.Enable());
434+
435+
this.EnableCamera();
436+
437+
return this;
438+
}
439+
440+
/// <summary>
441+
/// Reconfigures the Camera's preview port.
442+
/// </summary>
443+
/// <returns>The camera instance</returns>
444+
public MMALCamera ConfigurePreview()
445+
{
446+
this.DisableCamera();
447+
448+
this.Preview?.Connection?.Disable();
449+
this.Camera.InitialisePreview();
450+
this.Preview?.Connection?.Enable();
451+
452+
this.EnableCamera();
453+
419454
return this;
420455
}
421-
456+
457+
458+
private void DestroyPreviewComponent()
459+
{
460+
if (this.Preview != null)
461+
{
462+
this.Preview?.Connection.Disable();
463+
this.Preview?.Connection.Destroy();
464+
this.Preview.Dispose();
465+
}
466+
}
467+
468+
private void DestroyEncoders()
469+
{
470+
this.Encoders.ForEach(c => c.Dispose());
471+
}
472+
422473
/// <summary>
423474
/// Helper method to check the Renderer component status. If a Renderer has not been initialized, a warning will
424475
/// be shown to the user. If a Renderer has been created but a connection has not been initialized, this will be

src/MMALSharp/MMALCameraConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public static class MMALCameraConfig
184184
/// </summary>
185185
public static void Reload()
186186
{
187-
MMALCamera.Instance.ConfigureCamera();
187+
MMALCamera.Instance.Camera.SetCameraParameters();
188188
}
189189

190190
}

src/MMALSharp/MMALPortBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ internal void DestroyPortPool()
218218
{
219219
if (this.BufferPool != null)
220220
{
221+
if (this.Enabled)
222+
this.DisablePort();
223+
221224
MMALUtil.mmal_port_pool_destroy(this.Ptr, this.BufferPool.Ptr);
222225
}
223226
}

src/MMALSharp/Native/MMALFormat.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ public MMAL_SUBPICTURE_FORMAT_T(uint xOffset, uint yOffset)
112112
}
113113
}
114114

115-
[StructLayout(LayoutKind.Sequential)]
115+
//Union type.
116+
[StructLayout(LayoutKind.Explicit)]
116117
public struct MMAL_ES_SPECIFIC_FORMAT_T
117118
{
119+
[FieldOffset(0)]
118120
public MMAL_AUDIO_FORMAT_T audio;
121+
[FieldOffset(0)]
119122
public MMAL_VIDEO_FORMAT_T video;
123+
[FieldOffset(0)]
120124
public MMAL_SUBPICTURE_FORMAT_T subpicture;
121125

122126
public MMAL_ES_SPECIFIC_FORMAT_T(MMAL_AUDIO_FORMAT_T audio, MMAL_VIDEO_FORMAT_T video, MMAL_SUBPICTURE_FORMAT_T subpicture)

tests/MMALSharp.Tests/ImageEncoderTests.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,8 @@ public void TakePicture(string extension, MMALEncoding encodingType, MMALEncodin
348348
//Create our component pipeline.
349349
fixture.MMALCamera
350350
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
351-
.CreatePreviewComponent(new MMALNullSinkComponent())
352-
.ConfigureCamera();
353-
351+
.CreatePreviewComponent(new MMALNullSinkComponent());
352+
354353
await fixture.MMALCamera.TakePicture(fixture.MMALCamera.Camera.StillPort);
355354
}
356355

@@ -381,8 +380,7 @@ public void TakePictureRawBayer(string extension, MMALEncoding encodingType, MMA
381380
//Create our component pipeline.
382381
fixture.MMALCamera
383382
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
384-
.CreatePreviewComponent(new MMALNullSinkComponent())
385-
.ConfigureCamera();
383+
.CreatePreviewComponent(new MMALNullSinkComponent());
386384

387385
await fixture.MMALCamera.TakePicture(fixture.MMALCamera.Camera.StillPort, true);
388386
}
@@ -414,8 +412,7 @@ public void TakePictureTimelapse(string extension, MMALEncoding encodingType, MM
414412
//Create our component pipeline.
415413
fixture.MMALCamera
416414
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
417-
.CreatePreviewComponent(new MMALNullSinkComponent())
418-
.ConfigureCamera();
415+
.CreatePreviewComponent(new MMALNullSinkComponent());
419416

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

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

@@ -462,8 +458,7 @@ public void ChangeEncodingType()
462458
//Create our component pipeline.
463459
fixture.MMALCamera
464460
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
465-
.CreatePreviewComponent(new MMALNullSinkComponent())
466-
.ConfigureCamera();
461+
.CreatePreviewComponent(new MMALNullSinkComponent());
467462

468463
await fixture.MMALCamera.TakePicture(fixture.MMALCamera.Camera.StillPort);
469464
}
@@ -485,8 +480,7 @@ public void ChangeEncodingType()
485480
//Create our component pipeline.
486481
fixture.MMALCamera
487482
.AddEncoder(imgEncoder, fixture.MMALCamera.Camera.StillPort)
488-
.CreatePreviewComponent(new MMALNullSinkComponent())
489-
.ConfigureCamera();
483+
.CreatePreviewComponent(new MMALNullSinkComponent());
490484

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

0 commit comments

Comments
 (0)