Search code examples
cgtkgstreamergtk3

Unable to embed Gstreamer video in a GTK+ window


I'm trying to write a very simple GTK+ program, which would allow me to play a video using Gstreamer. Following is the code:

#include <gtk/gtk.h>
#include <gst/gst.h>
#include <gst/video/videooverlay.h>
#include <gdk/gdkwin32.h>

static GstElement *playbin;
static guintptr window_handle = 0;

static void report_error(GstBus *playbin_bus, GstMessage *error_message, gpointer user_data) {
    GError *error;
    gchar *debug_info;
    gst_message_parse_error(error_message, &error, &debug_info);
    g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(error_message->src), error->message);
    g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");
    g_clear_error(&error);
    g_free(debug_info);
}

static GstBusSyncReply prepare_window_handle(GstBus *bus, GstMessage *message, GstPipeline * pipeline) {
    if (!gst_is_video_overlay_prepare_window_handle_message(message)) {
        return GST_BUS_PASS;
    }
    g_assert(window_handle != 0);
    gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(playbin), window_handle);
    gst_message_unref(message);
    return GST_BUS_DROP;
}

static void realize_video_drawing_area(GtkWidget *video_drawing_area, gpointer user_data) {
    GdkWindow *video_window = gtk_widget_get_window(video_drawing_area);
    if (!gdk_window_ensure_native(video_window)) {
        g_error("Couldn't create native window needed for GstVideoOverlay!");
    }
    window_handle = (guintptr) GDK_WINDOW_HWND(video_window);
}

static void play(GtkButton *play_button, gpointer user_data) {
    gst_element_set_state(playbin, GST_STATE_PLAYING);
}

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

    GtkWidget *window = gtk_application_window_new(app);

    GtkWidget *root_pane = gtk_overlay_new();
    gtk_container_add(GTK_CONTAINER(window), root_pane);

    GtkWidget *video_drawing_area = gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(root_pane), video_drawing_area);
    gtk_widget_set_size_request(video_drawing_area, 800, 600);
    gtk_widget_set_double_buffered(video_drawing_area, FALSE);
    g_signal_connect(video_drawing_area, "realize", G_CALLBACK(realize_video_drawing_area), NULL);

    GtkWidget *play_button = gtk_button_new_with_label("Play");
    gtk_overlay_add_overlay(GTK_OVERLAY(root_pane), play_button);
    gtk_widget_set_halign(play_button, GTK_ALIGN_END);
    gtk_widget_set_valign(play_button, GTK_ALIGN_END);
    gtk_widget_set_margin_end(play_button, 20);
    gtk_widget_set_margin_bottom(play_button, 20);
    g_signal_connect(G_OBJECT(play_button), "clicked", G_CALLBACK(play), NULL);

    gtk_widget_show_all(window);

}

int main(int argc, char **argv) {

    gst_init(&argc, &argv);
    playbin = gst_element_factory_make("playbin", NULL);
    g_object_set(playbin, "uri", "http://techslides.com/demos/sample-videos/small.ogv", NULL);
    GstBus *playbin_bus = gst_element_get_bus(playbin);
    gst_bus_enable_sync_message_emission(playbin_bus);
    gst_bus_set_sync_handler(playbin_bus, (GstBusSyncHandler) prepare_window_handle, NULL, NULL);
    gst_bus_add_signal_watch(playbin_bus);
    g_signal_connect(playbin_bus, "message::error", G_CALLBACK(report_error), NULL);
    gst_object_unref(playbin_bus);

    GtkApplication *app = gtk_application_new("com.techn.videotest", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    return g_application_run(G_APPLICATION(app), argc, argv);

}

Whenever I press "Play", audio playback starts, but I can see no video. If I comment out the line

gst_bus_set_sync_handler(playbin_bus, (GstBusSyncHandler) prepare_window_handle, NULL, NULL);

the video starts in a separate D3D windows and plays with no problem. I see no relevant error messages in the console, unless the following counts:

(video_test.exe:5172): GStreamer-WARNING **: Failed to load plugin 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstchromaprint.dll': 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstchromaprint.dll': The specified module could not be found.

(video_test.exe:5172): GStreamer-WARNING **: Failed to load plugin 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstfragmented.dll': 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstfragmented.dll': The specified module could not be found.

(video_test.exe:5172): GStreamer-WARNING **: Failed to load plugin 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstx264.dll': 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstx264.dll': The specified module could not be found.

(video_test.exe:5172): GStreamer-WARNING **: Failed to load plugin 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstx265.dll': 'C:\msys64\mingw32\lib\gstreamer-1.0\libgstx265.dll': The specified procedure could not be found.

(video_test.exe:5172): GLib-GObject-WARNING **: attempt to override closure->va_marshal (63c488b0) with new marshal (66925ea8)

I am compiling the program with the following command line:

gcc video_test.c -o video_test `pkg-config --cflags --libs gtk+-3.0 gstreamer-1.0` -lgstvideo-1.0 -Wno-deprecated-declarations

I'm using MSYS2 environment on Windows 10 machine and MinGW 32 bit compiler. I have version 1.0 of Gstreamer and version 3.0 of GTK+ libraries.


Solution

  • I had the same problem last month integrating gstreamer with gtk+3 on windows 10 with msys2.

    Videooverlay won't work! Use gtksink instead.

    Try:

    #include <gtk/gtk.h>
    #include <gst/gst.h>
    
    static GstElement *playbin, *sink;
    
    static void play(GtkButton *play_button, gpointer user_data) {
        gst_element_set_state(playbin, GST_STATE_PLAYING);
    }
    
    static void activate(GtkApplication *app, gpointer user_data) {
    
        GtkWidget *window = gtk_application_window_new(app);
    
        GtkWidget *root_pane = gtk_overlay_new();
        gtk_container_add(GTK_CONTAINER(window), root_pane);
    
        GtkWidget *video_drawing_area = gtk_drawing_area_new();
    
        g_object_get (sink, "widget", &video_drawing_area, NULL);
        gtk_widget_set_size_request(video_drawing_area, 800, 600);
        gtk_container_add(GTK_CONTAINER(root_pane), video_drawing_area);
    
        GtkWidget *play_button = gtk_button_new_with_label("Play");
        gtk_overlay_add_overlay(GTK_OVERLAY(root_pane), play_button);
        gtk_widget_set_halign(play_button, GTK_ALIGN_END);
        gtk_widget_set_valign(play_button, GTK_ALIGN_END);
        gtk_widget_set_margin_end(play_button, 20);
        gtk_widget_set_margin_bottom(play_button, 20);
        g_signal_connect(G_OBJECT(play_button), "clicked", G_CALLBACK(play), NULL);
    
        gtk_widget_show_all(window);
    
    }
    
    int main(int argc, char **argv) {
    
        gst_init(&argc, &argv);
        playbin = gst_element_factory_make("playbin", NULL);
        g_object_set(playbin, "uri", "http://techslides.com/demos/sample-videos/small.ogv", NULL);
    
        sink = gst_element_factory_make ("gtksink", NULL);
        g_object_set(playbin, "video-sink", sink, NULL);
    
    
        GtkApplication *app = gtk_application_new("com.techn.videotest", G_APPLICATION_FLAGS_NONE);
        g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
        return g_application_run(G_APPLICATION(app), argc, argv);
    
    }
    

    And compile it with:

    gcc videotest.c -o videotest `pkg-config --cflags --libs gtk+-3.0 gstreamer-video-1.0`
    

    Or try:

    #include <gtk/gtk.h>
    #include <gst/gst.h>
    
    GstElement *pipeline, *src, *sink;
    
    int main (int argc, char **argv)
    {
        gst_init (&argc, &argv);
        gtk_init (&argc, &argv);
    
        pipeline = gst_pipeline_new ("xvoverlay");
        src = gst_element_factory_make ("videotestsrc", NULL);
        sink = gst_element_factory_make ("gtksink", NULL);
    
        gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
        gst_element_link (src, sink);
    
        GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        GtkWidget *area;
        g_object_get (sink, "widget", &area, NULL);
        gtk_container_add (GTK_CONTAINER (window), area);
    
        gtk_widget_realize(area);
    
        gtk_widget_show_all (window);
        gst_element_set_state (pipeline, GST_STATE_PLAYING);
    
        gtk_main ();
        return 0;
    }
    

    This SO answer has led me to the solution : https://stackoverflow.com/a/32695253