GStreamer GTK4 TooolKit sample C++

Basic implementation of GStreamer on GTK4 Window via gtk4paintablesink

#include <bits/stdc++.h>
using namespace  std;

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

GstElement   *playbin=nullptr;           /* Our one and only pipeline */
GstElement   *gtksink=nullptr;
GdkPaintable *paintable_ptr=nullptr;
GtkWidget    *picture=nullptr;

void Gstreamer()
{
    playbin = gst_element_factory_make ("playbin", "playbin");
    gtksink = gst_element_factory_make("gtk4paintablesink", NULL);

    g_object_get(gtksink, "paintable", &paintable_ptr, NULL);
    picture = gtk_picture_new_for_paintable((GdkPaintable*)paintable_ptr);
    g_object_set (playbin, "video-sink", gtksink, NULL);
    g_object_set (playbin, "uri", "https://gstreamer.freedesktop.org/data/media/sintel_trailer-480p.webm", NULL);
    gst_element_set_state (playbin, GST_STATE_PLAYING);
}

static void activate (GtkApplication *app,gpointer        user_data)
{
    Gstreamer();

    auto window = gtk_application_window_new (app);
    gtk_window_set_default_size (GTK_WINDOW (window), 800, 800);

    auto label0 = gtk_label_new ("label0");
    auto label1 = gtk_label_new ("label1");

    auto placeholder0 = gtk_label_new ("placeholder0");
    auto placeholder1 = gtk_label_new ("placeholder1");

    auto notebook = gtk_notebook_new ();
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), picture, label0);
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook), placeholder1, label1);

    gtk_window_set_child (GTK_WINDOW (window), notebook);
    gtk_widget_show(window);
}
int main(int argc,char *argv[])
{
    gst_init(&argc, &argv);

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

    cout<<"picture:"<<picture->parent_instance.ref_count<<endl;
    g_object_unref (app);

    g_object_unref(gtksink);
    g_object_unref(paintable_ptr);
    g_object_unref(playbin);
    return 0;
}

the below header and libraries are needed to compile this

INCLUDEPATH +=/usr/include/gtk-4.0
INCLUDEPATH +=/usr/include/gio-unix-2.0
INCLUDEPATH +=/usr/include/cairo
INCLUDEPATH +=/usr/include/pango-1.0
INCLUDEPATH +=/usr/include/harfbuzz
INCLUDEPATH +=/usr/include/pango-1.0
INCLUDEPATH +=/usr/include/fribidi
INCLUDEPATH +=/usr/include/harfbuzz
INCLUDEPATH +=/usr/include/gdk-pixbuf-2.0
INCLUDEPATH +=/usr/include/x86_64-linux-gnu
INCLUDEPATH +=/usr/include/cairo
INCLUDEPATH +=/usr/include/pixman-1
INCLUDEPATH +=/usr/include/uuid
INCLUDEPATH +=/usr/include/freetype2
INCLUDEPATH +=/usr/include/libpng16
INCLUDEPATH +=/usr/include/graphene-1.0
INCLUDEPATH +=/usr/lib/x86_64-linux-gnu/graphene-1.0/include
INCLUDEPATH +=/usr/include/libmount
INCLUDEPATH +=/usr/include/blkid
INCLUDEPATH +=/usr/include/glib-2.0
INCLUDEPATH +=/usr/lib/x86_64-linux-gnu/glib-2.0/include

LIBS +=-mfpmath=sse -msse -msse2 -pthread -lgtk-4 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

some stuff I picked up from :Gtk4paintablesink blackscreen in pure C

and https://gstreamer.freedesktop.org/documentation/tutorials/basic/toolkit-integration.html

You might want to give peel a try to avoid having to write plain C code in C++, and especially to have to deal with memory management yourself.

You can find GTK4 examples inside the peel repository, and GStreamer examples here (some depend on unmerged patches but nothing that should be relevant here).

I also had a presentation about this at the GStreamer conference this year.

1 Like