jetson-utils icon indicating copy to clipboard operation
jetson-utils copied to clipboard

Get keyboard from glDisplay

Open minhvogremsy opened this issue 1 year ago • 0 comments

I modified code in glDisplay but I can't how to get input from keyboard or mouse

#include "glDisplay.h"
#include "glTexture.h"
#include "glBuffer.h"
#include "glCamera.h"

#include "cudaFont.h"
#include "cudaNormalize.h"
#include "cudaInteropKernels.h"

#include "timespec.h"

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <gst/gst.h>
#include <gst/app/app.h>
#include <opencv2/core/types_c.h>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/videoio/videoio_c.h>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/video.hpp>
#include <deque>
#include "cudaRGB.h"
#include <X11/keysymdef.h>

using namespace cv;

#define TEXTURE_WIDTH 768
#define TEXTURE_HEIGHT 64
#define TEXTURE_OFFSET 30

#define GRID_N          128
#define GRID_POINTS     (GRID_N * GRID_N)
#define GRID_WORLD_SIZE 10.0f


bool signal_recieved = false;

GstElement *pipeline;
GMainLoop *loop;
pthread_mutex_t _lock = PTHREAD_MUTEX_INITIALIZER;
std::deque<cv::Mat> frameQueue;
cv::Mat prevFrame;
static int width;
static int height;

static GstFlowReturn new_preroll(GstAppSink* /*appsink*/, gpointer /*data*/)
{
    return GST_FLOW_OK;
}
static GstFlowReturn new_sample(GstAppSink* appsink, void* user_data)
{
    static int framecount = 0;

    // Get caps and frame
    GstSample *sample = gst_app_sink_pull_sample(appsink);
    GstCaps *caps = gst_sample_get_caps(sample);
    GstBuffer *buffer = gst_sample_get_buffer(sample);
    GstStructure *structure = gst_caps_get_structure(caps, 0);
    width = g_value_get_int(gst_structure_get_value(structure, "width"));
    height = g_value_get_int(gst_structure_get_value(structure, "height"));

    framecount++;

    // printf("%s %d %d, count %d \n", __func__, width, height, framecount);

    if(!sample) return GST_FLOW_OK;

    GstMapInfo map;
    gst_buffer_map( buffer, &map, GST_MAP_READ);

    Mat _frame(Size(width, height), CV_8UC3, (char*)map.data, Mat::AUTO_STEP);

    if (!_frame.empty())
    {
        pthread_mutex_lock(&_lock);
        frameQueue.push_back(_frame.clone());
        pthread_mutex_unlock(&_lock);
    }
    
    gst_buffer_unmap(buffer, &map);
    gst_sample_unref(sample);

    return GST_FLOW_OK;
}

void* fn_read_image(void *args)
{
    g_print ("Running sample...\n");
    GError *error = nullptr;
    loop = g_main_loop_new (NULL, FALSE);

/* Initialize GStreamer */
    gst_init(NULL, NULL); //

    gchar *descr = g_strdup(
        "v4l2src device=/dev/video0 ! video/x-raw,height=480,width=640,framerate=30/1 "
        "! tee name=t_raw "
        "t_raw. ! queue "
        "! videoconvert ! video/x-raw, format=(string)BGR, height=480,width=640, framerate=30/1"
        "! appsink name=my_appsink async=false sync=false drop=true max-buffers=1"
        );

    pipeline = gst_parse_launch(descr, &error);

    if(error){
        g_print ("pipeline error...\n");
    }

    GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "my_appsink");
    GstAppSinkCallbacks callbacks = { nullptr, new_preroll, new_sample };
    gst_app_sink_set_callbacks(GST_APP_SINK(sink), &callbacks, nullptr, nullptr);

    /* Start playing */
    gst_element_set_state (pipeline, GST_STATE_PLAYING);

    g_print ("Running...\n");
    return NULL;
}


void sig_handler(int signo)
{
	if( signo == SIGINT )
	{
		printf("received SIGINT\n");
		signal_recieved = true;
	}
}


int main( int argc, char** argv )
{
    pthread_t thread_gs = 0;
	pthread_create(&thread_gs, NULL, &fn_read_image, NULL);

	/*
	 * register signal handler (Ctrl+C)
	 */
	if( signal(SIGINT, sig_handler) == SIG_ERR )
		printf("can't catch SIGINT\n");


	/*
	 * create openGL window
	 */
	glDisplay* display = glDisplay::Create("NVIDIA OpenGL/CUDA Interoperability Test");
	
	if( !display )
	{
		printf("gl-display-test:  failed to create openGL display\n");
		return 0;
	}
	

	/*
	 * create font
	 */
	cudaFont* font = cudaFont::Create();
	
	if( !font )
	{
		printf("gl-display-test:  failed to create cudaFont object\n");
		return 0;
	}


	/*
	 * rendering loop
	 */
	while( !signal_recieved && display->IsOpen() )
	{
        pthread_mutex_lock(&_lock);
        if(!frameQueue.empty())
        {
            // lay frame cuoi cuoi, xoa mang
            prevFrame = frameQueue.back().clone();
            frameQueue.clear();
        }
        pthread_mutex_unlock(&_lock);
         if(!prevFrame.empty()){
            cvtColor(prevFrame, prevFrame, COLOR_BGR2RGB);

            uchar3* imgBufferRGB = NULL;
            float4* imgBufferRGBAf = NULL;
            cudaMalloc((void**)&imgBufferRGB, width * sizeof(uchar3) * height);
            cudaMalloc((void**)&imgBufferRGBAf, width * sizeof(float4) * height);
            
            cudaMemcpy2D((void*)imgBufferRGB, width*sizeof(uchar3), (void*)prevFrame.data, prevFrame.step, width*sizeof(uchar3), height, cudaMemcpyHostToDevice);
            cudaRGB8ToRGBA32(imgBufferRGB, imgBufferRGBAf, width, height); // defined in cudaRGB.h
            
            if( display != NULL )
            {
                display->Render(imgBufferRGBAf, width, height);
                if( display->GetKey(XK_a)){
                    printf("the 'a' key is pressed \n");
                }
            }
            prevFrame.release();
            cudaFree(imgBufferRGB);
            cudaFree(imgBufferRGBAf);
        }
	}
	

	/*
	 * close the window
	 */
	if( display != NULL )
	{
		delete display;
		display = NULL;
	}
	
	printf("gl-display-test:  OpenGL display has been un-initialized.\n");
	printf("gl-display-test:  this concludes the test of the device.\n");

	return 0;
}

Kindly help me to solve this issue ASAP. Thank You!

minhvogremsy avatar Jul 21 '22 16:07 minhvogremsy