-
Notifications
You must be signed in to change notification settings - Fork 32
MMALCamera.ProcessAsync sometimes hangs indefinitely #198
Description
Hello, I'm running the following code on my Raspberry Pi:
public static async Task<byte[]> CaptureRawData()
{
MMALCamera cam = MMALCamera.Instance;
MMALCameraConfig.StillEncoding = MMALEncoding.BGR24;
MMALCameraConfig.StillSubFormat = MMALEncoding.BGR24;
using (var imgCaptureHandler = new MemoryStreamCaptureHandler())
using (var renderer = new MMALNullSinkComponent())
{
cam.ConfigureCameraSettings(imgCaptureHandler);
cam.Camera.PreviewPort.ConnectTo(renderer);
// Camera warm up time
await Task.Delay(2000);
Serilog.Log.Logger.Debug("cam.ProcessAsync(cam.Camera.StillPort)");
await cam.ProcessAsync(cam.Camera.StillPort));
Serilog.Log.Logger.Debug("Got response, returning data");
return imgCaptureHandler.CurrentStream.ToArray();
}
}
This captures an image to memory and returns it as a byte array.
Mostly it works great. I'm running this to capture an image every 10 seconds or so.
Randomly, after a few hours the program freezes on the line "await cam.ProcessAsync" which I can tell because of the serilog logging.
Having left it for several hours, it never recovers, it just hangs forever on that line and I have to Ctrl+C to kill the program.
If I run it again it can capture images again without issue, only to hang again after a few hours in the same place.
I read that this may be caused due to power issues, but I'm using an official RPi power supply without any other peripherals.
Regardless, even if it is caused by intermittent power brown out, it doesn't really matter to me if it fails to capture one image every few hours, I just want to be able to detect the failure and try again. As it stands, I can't retry since the main thread completely locks up.
It's an absolute requirement that I capture the image to memory, I can't use the other methods in MMALSharp to capture the image to disk, since I'm doing some post filtering on the image in memory after capturing it, and then deciding that I only want to save certain images to disk if they pass certain tests.
I've attempted to detect the hang with a timeout and retry, but this also hangs with some strange behaviour. This is already quite a long post so I'll post as a reply my attempt to work around the issue.