Skip to content

Instantly share code, notes, and snippets.

@beeender
Last active February 27, 2024 12:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save beeender/d539734794606a38d4e3 to your computer and use it in GitHub Desktop.
Save beeender/d539734794606a38d4e3 to your computer and use it in GitHub Desktop.
Gstreamer appsrc streaming though udpsink
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>
static GMainLoop *loop;
static void
cb_need_data (GstAppSrc *appsrc,
guint unused_size,
gpointer user_data)
{
g_print("In %s\n", __func__);
static gboolean white = FALSE;
static GstClockTime timestamp = 0;
GstBuffer *buffer;
guint size;
GstFlowReturn ret;
size = 385 * 288 * 2;
buffer = gst_buffer_new_allocate (NULL, size, NULL);
/* this makes the image black/white */
gst_buffer_memset (buffer, 0, white ? 0xff : 0x0, size);
white = !white;
GST_BUFFER_PTS (buffer) = timestamp;
GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (1, GST_SECOND, 2);
timestamp += GST_BUFFER_DURATION (buffer);
//g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
ret = gst_app_src_push_buffer(appsrc, buffer);
if (ret != GST_FLOW_OK) {
/* something wrong, stop pushing */
g_main_loop_quit (loop);
}
}
static void cb_enough_data(GstAppSrc *src, gpointer user_data)
{
g_print("In %s\n", __func__);
}
static gboolean cb_seek_data(GstAppSrc *src, guint64 offset, gpointer user_data)
{
g_print("In %s\n", __func__);
return TRUE;
}
gint
main (gint argc,
gchar *argv[])
{
GstElement *pipeline, *appsrc, *conv, *videosink, *payloader, *udpsink, *videoenc;
/* init GStreamer */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* setup pipeline */
pipeline = gst_pipeline_new ("pipeline");
appsrc = gst_element_factory_make ("appsrc", "source");
conv = gst_element_factory_make ("videoconvert", "conv");
videoenc = gst_element_factory_make("avenc_mpeg4", "ffenc_mpeg4");
videosink = gst_element_factory_make ("xvimagesink", "videosink");
payloader = gst_element_factory_make("rtpmp4vpay", "rtpmp4vpay");
g_object_set(G_OBJECT(payloader),
"config-interval", 60,
NULL);
udpsink = gst_element_factory_make("udpsink", "udpsink");
g_object_set(G_OBJECT(udpsink),
"host", "127.0.0.1",
"port", 5000,
NULL);
GstElement *freeze = gst_element_factory_make("imagefreeze", "imagefreeze0");
GstElement *colorspace = gst_element_factory_make("ffmpegcolorspace", "colorspace");
/* setup */
g_object_set (G_OBJECT (appsrc), "caps",
gst_caps_new_simple ("video/x-raw",
"format", G_TYPE_STRING, "RGB16",
"width", G_TYPE_INT, 384,
"height", G_TYPE_INT, 288,
"pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
"framerate", GST_TYPE_FRACTION, 0, 1,
NULL), NULL);
#if 0
// THIS WORKS!
gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videosink, NULL);
gst_element_link_many (appsrc, conv, videosink, NULL);
#elif 0
// WOKRS!
gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, freeze, videosink, NULL);
gst_element_link_many (appsrc, conv, freeze, videosink, NULL);
#else
// WORKS!
gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videoenc, payloader, udpsink, NULL);
gst_element_link_many (appsrc, conv, videoenc, payloader, udpsink, NULL);
#endif
/* setup appsrc */
g_object_set (G_OBJECT (appsrc),
"stream-type", 0,
"is-live", TRUE,
"format", GST_FORMAT_TIME, NULL);
GstAppSrcCallbacks cbs;
cbs.need_data = cb_need_data;
cbs.enough_data = cb_enough_data;
cbs.seek_data = cb_seek_data;
//g_signal_connect (appsrc, "need-data", G_CALLBACK (cb_need_data), NULL);
gst_app_src_set_callbacks(GST_APP_SRC_CAST(appsrc), &cbs, NULL, NULL);
/* play */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
/* clean up */
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (pipeline));
g_main_loop_unref (loop);
return 0;
}
@tcanabrava
Copy link

for me the above code (without modification) doesn't works, the cb_need_data is called only once and there's no udp streaming through the network. I'm trying to make this work for quite a while already, but I'm still a bit lost. =[

@AlamMohd
Copy link

AlamMohd commented Jan 11, 2021

I need to do a live streaming of webcam to VLC through UDP. I have got a good reference from the above code but i failed to produce the expected output. Video frames are pushed to pipeline using appsrc.

live streaming was displayed successfully with below pipeline:

appsrc ! videoconvert ! autovideosink

As i need to live streaming on VLC through UDP, i have updated the pipeline as shown below:

appsrc ! videoconvert ! avenc_mpeg4 ! rtpmp4vpay ! udpsink

With the above pipeline i was able to stream video to VLC through UDP, but the video displayed at VLC was having very low quality (Just a blurry video displayed, where no objects can be identified and blocks of green and red are displayed at multiple places). I tried multiple update options and failed to produce the output. Any suggestion will be really helpful. Thanks in advance.

Please find the code used for pipeline creation.

void start_stream(void)
{

  /* Pointer to hold bus address */
  GstBus *bus = nullptr;

  gst_init (nullptr, nullptr);
  loop = g_main_loop_new (NULL, FALSE);
  
  pipeline = gst_pipeline_new ("pipeline");
  appsrc = gst_element_factory_make ("appsrc", "source");
  conv = gst_element_factory_make ("videoconvert", "conv");
  
  #if(0)
  /* TEST WITH autovideosink */
  videosink = gst_element_factory_make ("autovideosink", "videosink");
  #else
  /* TEST WITH udpsink */
  videoenc = gst_element_factory_make ("avenc_mpeg4", "videoenc");
  payloader = gst_element_factory_make ("rtpmp4vpay", "payloader");
  videosink = gst_element_factory_make ("udpsink", "videosink");
  g_object_set(G_OBJECT(StreamPipeline.videosink), "host", "127.0.0.1", "port", 5000, NULL);
  g_object_set(G_OBJECT(StreamPipeline.payloader), "config-interval", 60, NULL);
  #endif

  /* setup */
  g_object_set (G_OBJECT (appsrc), "caps",
    gst_caps_new_simple ("video/x-raw",
    "format", G_TYPE_STRING, "RGB",
    "width", G_TYPE_INT, 1280,
    "height", G_TYPE_INT, 720,
    "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
    "framerate", GST_TYPE_FRACTION, 30, 1,
    NULL), NULL);

  #if(0)
  /* TEST WITH autovideosink */
  /* WORKING AS EXPECTED, ABLE TO SEE LIVE STREAMING ON DISPLAY */
  gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videosink, NULL);
  gst_element_link_many (appsrc, conv, videosink, NULL);
  #else
  /* TEST WITH udpsink */
  /* NOT WORKING AS EXPECTED, DISPLAYING A BLURRY VIDEO */
  gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videoenc, payloader, videosink, NULL);
  gst_element_link_many (appsrc, conv, videoenc, payloader, videosink, NULL);
  #endif
  
  g_object_set (G_OBJECT (appsrc),
    "stream-type", 0,
    "is-live", TRUE,
    "format", GST_FORMAT_TIME, NULL);
    
   bus = gst_element_get_bus(pipeline);
   gst_bus_add_watch (bus, BusWatch, loop);
   gst_object_unref(bus);
 
   gst_element_set_state(StreamPipeline.pipeline, GST_STATE_PLAYING);
   
   g_main_loop_run(StreamPipeline.loop);
    
   gst_element_set_state(StreamPipeline.pipeline, GST_STATE_NULL);
   gst_object_unref(StreamPipeline.pipeline);
}

@cameronelliott
Copy link

Any change you would add a BSD or public domain license?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment