From 567e1a7a9c3f2c026df7da7d8c66c1dda126ce5e Mon Sep 17 00:00:00 2001 From: talksik Date: Sun, 23 Jul 2023 13:44:51 -0700 Subject: [PATCH] looks like seeing frames on new sample --- src/local_capture.cpp | 28 ++++++++++++++++++++++++---- src/local_capture.h | 4 +++- src/main.cpp | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/local_capture.cpp b/src/local_capture.cpp index 82d1280..95d1311 100644 --- a/src/local_capture.cpp +++ b/src/local_capture.cpp @@ -11,17 +11,21 @@ LocalCapture::LocalCapture() { GstElement *video = gst_element_factory_make("v4l2src", "video"); GstElement *videoconvert = gst_element_factory_make("videoconvert", "videoconvert"); - gtksink = gst_element_factory_make("autovideosink", "sink"); + sink = gst_element_factory_make("appsink", "sink"); - if (!video || !videoconvert || !gtksink) { + // set callback for appsink + g_object_set(G_OBJECT(sink), "emit-signals", TRUE, "sync", FALSE, nullptr); + g_signal_connect(sink, "new-sample", G_CALLBACK(on_new_sample), this); + + if (!video || !videoconvert || !sink) { std::cerr << "failed to create elements" << std::endl; return; } m_pipeline = gst_pipeline_new("local_capture"); - gst_bin_add_many(GST_BIN(m_pipeline), video, videoconvert, gtksink, nullptr); - if (!gst_element_link_many(video, videoconvert, gtksink, nullptr)) { + gst_bin_add_many(GST_BIN(m_pipeline), video, videoconvert, sink, nullptr); + if (!gst_element_link_many(video, videoconvert, sink, nullptr)) { g_printerr("Elements could not be linked.\n"); } @@ -58,6 +62,22 @@ LocalCapture::LocalCapture() { std::cout << "local capture ready" << std::endl; } +int LocalCapture::on_new_sample(GstElement *sink, gpointer data) { + GstSample *sample; + + /* Retrieve the buffer */ + g_signal_emit_by_name(sink, "pull-sample", &sample); + if (sample) { + /* The only thing we do in this example is print a * to indicate a received + * buffer */ + g_print("*"); + gst_sample_unref(sample); + return GST_FLOW_OK; + } + + return GST_FLOW_ERROR; +} + void LocalCapture::start() { if (m_isRunning) { return; diff --git a/src/local_capture.h b/src/local_capture.h index 4a00623..6ff63ff 100644 --- a/src/local_capture.h +++ b/src/local_capture.h @@ -11,13 +11,15 @@ public: gst_object_unref(m_bus); gst_element_set_state(m_pipeline, GST_STATE_NULL); gst_object_unref(m_pipeline); + gst_object_unref(sink); } bool isRunning() const { return m_isRunning; } - GstElement *gtksink; + GstElement *sink; private: bool m_isRunning; gboolean bus_watch(GMainLoop *loop); GstElement *m_pipeline; GstBus *m_bus; + static int on_new_sample(GstElement *sink, gpointer data); }; diff --git a/src/main.cpp b/src/main.cpp index e022084..55b1366 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ MyWindow::MyWindow() { m_localCapture = new LocalCapture(); + // todo: show frames here //GtkWidget *area; // g_object_get(m_localCapture->gtksink, "widget", &area, NULL); // Gtk::Widget *widget = Glib::wrap(area);