Segmentation fault when query the EGL Stream for new frame

Hello everyone.

I am trying to query the EGL stream for new frames in the cudaHistogram sample but I am getting segmentation fault inside the eglQueryStreamKHR function. To reproduce use the following patch

--- main.cpp	2019-07-18 09:24:37.635346869 +0000
+++ ../../../samples/cudaHistogram/main.cpp	2019-07-18 09:26:44.681268938 +0000
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <Argus/Argus.h>
+#include "EGLGlobal.h"
 
 #include "Error.h"
 #include "Options.h"
@@ -123,6 +124,18 @@
         ORIGINATE_ERROR("Failed to allocate histogram");
     for (unsigned int frame = 0; frame < options.frameCount; ++frame)
     {
+        EGLint streamState = 0;
+        while (streamState != EGL_STREAM_STATE_NEW_FRAME_AVAILABLE_KHR && streamState != EGL_STREAM_STATE_NEW_FRAME_AVAILABLE_KHR)
+        {
+            if (!eglQueryStreamKHR(iStream->getEGLDisplay(), iStream->getEGLStream(), EGL_STREAM_STATE_KHR, &streamState))
+                printf("eglQueryStreamKHR EGL_STREAM_STATE_KHR faile\n");
+        }
+
+        if (streamState == EGL_STREAM_STATE_DISCONNECTED_KHR)
+        {
+            printf("EGL Stream state: DISCONNECTED\n");
+            break;
+        }
         /*
          * For simplicity this example submits a capture then waits for an output.
          * This pattern will not provide the best possible performance as the camera

Copy the above patch to a file cudaHistogram.patch and patch the main.cpp file of the cudaHistogram sample with the following command

patch main.cpp cudaHistogram.patch

Hi,

Could you share the error message you meet with us first?
Thanks.

Hi,
Please also refer to tegra_multimedia_api\argus\samples\bayerAverageMap. It calls

eglStreamConsumerAcquireKHR(g_display.get(), m_eglStream);

Argus is the producer and you should use consumer APIs.

Hello everyone and thanks for the response.

First of all, there is no error message from the application, only from the OS (“Segmentation fault”) which indicates illegal access of memory inside the eglQueryStreamKHR function after debugging with gdb.

Second, the cudaHistogram sample uses CUDA as EGL consumer and acquires frames with the cudaEGLStreamConsumerAcquireFrame function. My intend here, since the argus lib creates a mailbox stream by default, is to check the stream for new frames before acquire, to avoid duplicate frames. Is there any other way to do that except with the eglQueryStreamKHR ?

Hi,
Please refer to yuvJpeg for getting metadata. You can get timestamp of each frame. Every frame shall have a unique timestamp.

Hello DaneLLL.

In order to get the metadata you have to acquire the image from the EGL stream first. This has a performance cost, which can be significant if the consumer acquires in high frequency. This is why functions like eglQueryStreamKHR exists. The question a have asked is why I am getting segmentation fault when I run this the eglQueryStreamKHR function. Have you tried to reproduce my issue ?

Hi,
We will check this issue. Thanks.

Hi,
Please apply the following patch and try again.

diff --git a/public/samples/cudaHistogram/main.cpp b/public/samples/cudaHistogram/main.cpp
index d751d6b..297da5d 100644
--- a/public/samples/cudaHistogram/main.cpp
+++ b/public/samples/cudaHistogram/main.cpp
@@ -41,6 +41,7 @@
 
 #include "CUDAHelper.h"
 #include "histogram.h"
+#include "EGLGlobal.h"
 
 using namespace Argus;
 
@@ -49,9 +50,10 @@ namespace ArgusSamples
 
 // Global variables
 CUcontext g_cudaContext = 0;
-
+EGLDisplayHolder g_display;
 static bool execute(const ArgusSamples::CommonOptions& options)
 {
+    PROPAGATE_ERROR(g_display.initialize());
     // Create the CameraProvider object
     UniqueObj<CameraProvider> cameraProvider(CameraProvider::create());
     ICameraProvider *iCameraProvider = interface_cast<ICameraProvider>(cameraProvider);
@@ -84,6 +86,7 @@ static bool execute(const ArgusSamples::CommonOptions& options)
         ORIGINATE_ERROR("Failed to create OutputStreamSettings");
     iEGLStreamSettings->setPixelFormat(PIXEL_FMT_YCbCr_420_888);
     iEGLStreamSettings->setResolution(iSensorMode->getResolution());
+    iEGLStreamSettings->setEGLDisplay(g_display.get());
     UniqueObj<OutputStream> outputStream(iCaptureSession->createOutputStream(streamSettings.get()));
     IEGLOutputStream *iEGLOutputStream = interface_cast<IEGLOutputStream>(outputStream);
     if (!iEGLOutputStream)
@@ -120,6 +123,16 @@ static bool execute(const ArgusSamples::CommonOptions& options)
         ORIGINATE_ERROR("Failed to allocate histogram");
     for (unsigned int frame = 0; frame < options.frameCount(); ++frame)
     {
+        EGLint streamState = 0;
+        if (!eglQueryStreamKHR(iEGLOutputStream->getEGLDisplay(),
+                    iEGLOutputStream->getEGLStream(), EGL_STREAM_STATE_KHR, &streamState))
+            printf("eglQueryStreamKHR EGL_STREAM_STATE_KHR failed\n");
+
+        if (streamState == EGL_STREAM_STATE_DISCONNECTED_KHR)
+        {
+            printf("EGL Stream state: DISCONNECTED\n");
+            break;
+        }
         /*
          * For simplicity this example submits a capture then waits for an output.
          * This pattern will not provide the best possible performance as the camera
@@ -208,6 +221,9 @@ static bool execute(const ArgusSamples::CommonOptions& options)
 
     printf("Cleaning up\n");
 
+    // Cleanup the EGL display
+    PROPAGATE_ERROR(g_display.cleanup());
+
     // Disconnect the Argus producer from the stream.
     /// @todo: This is a WAR for a bug in cuEGLStreamConsumerDisconnect (see bug 200239336).
     iEGLOutputStream->disconnect();
--

Hello DaneLLL.

I tried the patch. It seems to work, but at the end of the program I get this error

Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused

(argus_cudahistogram:6172): Gtk-WARNING **: cannot open display:

I think it occurs because the XServer in my system is disabled
You can check this behavior by running

sudo service lightdm stop

and then run the cudaHistogram application.

Hi,
We are checking if we can support the samples running without X11.

Hello

Do you have any update ?

Hi,
We have confirmed the application depends on X11. X11 is must-have for running this app.