Gtk4paintablesink blackscreen in pure C

Hello

I am trying build simple app with GStreamer 1.26.1 in pure C. I need to display video source in gtk window. Trying rewrite Python example from gitlab. Python script works great but my C-code going to blackscreen when window is displayed.

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gst/gst.h>

typedef struct _StreamData {
    GstElement *pipeline;
    gpointer paintable_ptr;
} StreamData;


static void activate(GtkApplication *app, StreamData *data) {
    GtkWidget *window, *picture;
    GstStateChangeReturn ret;


    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "VID");
    gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);

    picture = gtk_picture_new_for_paintable(data->paintable_ptr);
    gtk_window_set_child(GTK_WINDOW(window), picture);

    gtk_application_add_window(app, GTK_WINDOW(window));
    gtk_window_present(GTK_WINDOW(window));

    gst_element_set_state(data->pipeline, GST_STATE_PLAYING);
}


int main(int argc, char *argv[]) {
    StreamData data;
    GtkApplication *app;
    GstElement *gtksink, *glsink, *src;
    GstBus *bus;
    gint status;

    gst_init(&argc, &argv);

    gtksink = gst_element_factory_make("gtk4paintablesink", NULL);
    g_assert(gtksink != NULL);
    g_object_get(gtksink, "paintable", &data.paintable_ptr, NULL);

    glsink = gst_element_factory_make("glsinkbin", NULL);
    g_assert(glsink != NULL);
    g_object_set(glsink, "sink", gtksink, NULL);

    src = gst_element_factory_make("gltestsrc", NULL);
    g_assert(src != NULL);

    data.pipeline = gst_pipeline_new("main-pipeline");
    g_assert(data.pipeline != NULL);

    gst_bin_add_many(GST_BIN(data.pipeline), src, glsink, NULL);
    g_assert(gst_element_link(src, glsink) == TRUE);

    app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect (app, "activate", G_CALLBACK (activate), &data);
    status = g_application_run(G_APPLICATION(app), argc, argv);

    g_object_unref(app);
    g_object_unref(data.pipeline);

    return status;
}

Can somebody help me with this issue

PS. Python script says that GL is supported

You’re forgetting to check the return value of gst_element_set_state(). That’s failing here.
The reason why it fails can be seen when enabling debug logs:

0:00:00.008202482 602755     0x38abfb00 WARN       gtk4paintablesink src/sink/imp.rs:969:gstgtk4::sink::imp::PaintableSink::initialize_gl_context_main:<gtk4paintablesink0> Failed to retrieve GDK display

That happens because you don’t initialize GTK before creating/using the GTK4 sink. Moving all the code into the activate callback (like in the Python example) or calling gtk_init() before creating the sink solves this.

Thank you very much!!! It’s works.