Created
November 24, 2018 18:51
-
-
Save techyian/3959f4c1918239611826738ad3afd7fa to your computer and use it in GitHub Desktop.
MMALSharp Callback handler describing FPS calculation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// A callback handler specifically for rapid image capture from the camera's video port. | |
/// </summary> | |
public class FastImageOutputCallbackHandler : DefaultOutputCallbackHandler | |
{ | |
private static DateTime _start; | |
private static int _fps; | |
/// <summary> | |
/// Creates a new instance of <see cref="FastImageOutputCallbackHandler"/>. | |
/// </summary> | |
/// <param name="port">The working <see cref="IOutputPort"/>.</param> | |
public FastImageOutputCallbackHandler(IOutputPort port) | |
: base(port) | |
{ | |
_start = DateTime.Now; | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="FastImageOutputCallbackHandler"/>. | |
/// </summary> | |
/// <param name="port">The working <see cref="IOutputPort"/>.</param> | |
/// <param name="encoding">The <see cref="MMALEncoding"/> type to restrict on.</param> | |
public FastImageOutputCallbackHandler(IOutputPort port, MMALEncoding encoding) | |
: base(port, encoding) | |
{ | |
} | |
/// <inheritdoc /> | |
public override void Callback(MMALBufferImpl buffer) | |
{ | |
if (this.WorkingPort.ComponentReference.GetType() != typeof(MMALImageEncoder)) | |
{ | |
throw new ArgumentException($"Working port component is not of type {nameof(MMALImageEncoder)}"); | |
} | |
base.Callback(buffer); | |
var eos = buffer.AssertProperty(MMALBufferProperties.MMAL_BUFFER_HEADER_FLAG_FRAME_END) || | |
buffer.AssertProperty(MMALBufferProperties.MMAL_BUFFER_HEADER_FLAG_EOS); | |
if (eos) | |
{ | |
if (DateTime.Now - _start > TimeSpan.FromSeconds(1)) | |
{ | |
MMALLog.Logger.Info($"FPS: {_fps}"); | |
_fps = 0; | |
_start = DateTime.Now; | |
} | |
_fps++; | |
// In rapid capture mode, provide the ability to do post-processing once we have a complete frame. | |
this.WorkingPort.Handler?.PostProcess(); | |
} | |
if (eos && this.WorkingPort.Handler?.GetType() == typeof(ImageStreamCaptureHandler)) | |
{ | |
((ImageStreamCaptureHandler)this.WorkingPort.Handler).NewFile(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment