I tried to integrate a video stream from a webcam and display it on the GTK4 application window. But the video window is displayed separately. How can I fix this error?
static void setup_video_pipeline(GtkWidget *video_area) {
GstElement *video_source, *video_convert, *video_sink;
video_source = gst_element_factory_make("v4l2src", "video_source");
video_convert = gst_element_factory_make("videoconvert", "video_convert");
#ifdef GDK_WINDOWING_X11
video_sink = gst_element_factory_make("xvimagesink", "video_sink");
#endif
#ifdef GDK_WINDOWING_WAYLAND
video_sink = gst_element_factory_make("waylandsink", "video_sink");
#endif
if (!video_source || !video_convert || !video_sink) {
g_printerr("Failed to create one or more GStreamer elements:\n");
if (video_source) gst_object_unref(video_source);
if (video_convert) gst_object_unref(video_convert);
if (video_sink) gst_object_unref(video_sink);
return;
}
pipeline = gst_pipeline_new("video_pipeline");
gst_bin_add_many(GST_BIN(pipeline), video_source, video_convert, video_sink, NULL);
if (!gst_element_link_many(video_source, video_convert, video_sink, NULL)) {
g_printerr("Elements could not be linked.\n");
gst_object_unref(pipeline);
pipeline = NULL;
return;
}
#ifdef GDK_WINDOWING_X11
if (GDK_IS_X11_DISPLAY(gdk_display_get_default())) {
video_window_handle = GDK_SURFACE_XID(gtk_native_get_surface(gtk_widget_get_native(video_area)));
}
#endif
#ifdef GDK_WINDOWING_WAYLAND
if (GDK_IS_WAYLAND_DISPLAY(gdk_display_get_default())) {
video_window_handle = (guintptr) gdk_wayland_surface_get_wl_surface(gtk_native_get_surface(gtk_widget_get_native(video_area)));
}
#endif
if (video_window_handle) {
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(video_sink), video_window_handle);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
} else {
g_printerr("Failed to set video window handle.\n");
}
gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
static void realize_cb(GtkWidget *widget, gpointer data) {
setup_video_pipeline(widget);
}