I am writing a program in C language using gtk3 library. I want it to be able to receive a h264 video stream from a certain IP address (localhost) and UDP/RTP PORT (5000).
In order to do it, I am using gstreamer to both stream and receive the video.
I managed to stream the video using the following pipeline :
send.sh :
gst-launch-1.0 filesrc location=sample-mp4-file.mp4 ! decodebin ! x264enc ! 'video/x-h264, stream-format=(string)byte-stream' ! h264parse ! rtph264p
I managed to display the video in a new window using the following pipeline :
receive.sh :
gst-launch-1.0 udpsrc port=5000 caps="application/x-rtp,encoding-name=H264" ! rtph264depay ! decodebin ! videoconvert ! autovideosink
At this point, everything works fine. But now I want to receive the stream and display it inside my C/GTK program. I am using the following code (found on internet and adapted to make it compile) :
#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <gst/gst.h>
#include <glib.h>
#include <gdk/gdk.h>
#include <gst/video/videooverlay.h>
#include <gdk/gdkx.h>
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
static void decodebin_pad_added(GstElement *element, GstPad *pad, gpointer data)
{
gchar *name;
GstElement *other = data;
name = gst_pad_get_name (pad);
g_print ("A new pad %s was created for %s\n", name, gst_element_get_name(element));
g_free (name);
g_print ("element %s will be linked to %s\n",
gst_element_get_name(element),
gst_element_get_name(other));
gst_element_link(element, other);
}
int main (int argc, char **argv)
{
GdkWindow *video_window_xwindow;
GtkWidget *window, *video_window;
gulong embed_xid;
GstStateChangeReturn sret;
gst_init (&argc, &argv);
gtk_init (&argc, &argv);
GstElement *pipeline, *udpsrc, *appxrtp, *depay, *decodebin, *videoconvert, *sink;
pipeline = gst_pipeline_new ("xvoverlay");
udpsrc = gst_element_factory_make ("udpsrc", NULL); g_assert(udpsrc);
//Set CAPS
g_object_set (G_OBJECT (udpsrc), "port", 5000, NULL);
GstCaps * xrtpcaps = gst_caps_from_string("application/x-rtp,encoding-name=H264");
g_object_set (udpsrc, "caps", xrtpcaps, NULL);
depay = gst_element_factory_make ("rtph264depay", NULL); g_assert(depay);
decodebin = gst_element_factory_make ("decodebin", NULL); g_assert(decodebin);
videoconvert = gst_element_factory_make ("videoconvert", NULL); g_assert(videoconvert);
sink = gst_element_factory_make ("xvimagesink", NULL); g_assert(sink);
//ADD
gst_bin_add_many (GST_BIN (pipeline), udpsrc, depay, decodebin, videoconvert, sink, NULL);
//LINK
g_assert(gst_element_link (udpsrc, depay));
g_assert(gst_element_link (depay, decodebin));
//g_assert(gst_element_link (decodebin, videoconvert));
g_signal_connect(decodebin, "pad-added", (GCallback)decodebin_pad_added, videoconvert);
g_assert(gst_element_link (videoconvert, sink));
/* prepare the ui */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (destroy), (gpointer) pipeline);
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
gtk_window_set_title (GTK_WINDOW (window), "GstVideoOverlay Gtk+ demo");
video_window = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER (window), video_window);
gtk_container_set_border_width (GTK_CONTAINER (window), 2);
gtk_widget_show_all (window);
video_window_xwindow = gtk_widget_get_window (video_window);
embed_xid = GDK_WINDOW_XID (video_window_xwindow);
gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), embed_xid);
/* run the pipeline */
sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (sret == GST_STATE_CHANGE_FAILURE)
gst_element_set_state (pipeline, GST_STATE_NULL);
else
gtk_main ();
gst_object_unref (pipeline);
return 0;
}
Compiling with the following command :
gcc main.c -o main `pkg-config --cflags --libs gtk+-3.0 gstreamer-1.0 gstreamer-video-1.0`
-> This works exactly as excpected on my Ubuntu (16.04) VM
-> This is not working at all on my Xubuntu (18.04) VM and I don't understand why !
It compiles, it run without any errors but the window disapears as soon as it appears !
The only change I noticed while debugging is the following :
sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
-> returns 2 (GST_STATE_CHANGE_ASYNC) on Ubuntu
-> returns 0 (GST_STATE_CHANGE_FAILURE) on Xubuntu
Thank you in advance for your help, I am really stuck on this and could not find anything on internet ! If you need any other informations, just let me know, I'll be glade to complete my post.
Bye, opiDev.
Here is the solution I found :
Changin xvimagesink by ximagesink :
sink = gst_element_factory_make ("xvimagesink", NULL); g_assert(sink);
becomes
sink = gst_element_factory_make ("ximagesink", NULL); g_assert(sink);
Hope it will help some of you facing the same problem.