playbin2 using appsrc

James jkim at jetheaddev.com
Fri Dec 20 13:52:53 PST 2013


I was wondering if someone can help me figure out what I am missing in my
code below.
Basically I am trying to use appsrc to push MPEG2TS stream over to playbin2.
I found a gstreamer tutorial on this and followed the exact same logic.
In the tutorial, appsrc is configured to push audio/x-raw-int data to
manually configured pipeline(e.g. tee -> queue -> audioconverter, etc).
My code does run without any error but nothing happens; push_data function
is not triggered.
I have been suspicious about my not setting the caps of appsrc.
So far I have tried video/mpegts, video/mpeg,
"video/mpegts,systemstream=true,packet-size=188" with no success.
Here is my code:

#include <gst/gst.h>
#include <string.h>
#include <gst/app/gstappsrc.h>

#define STREAM_FILENAME "/home/jkim/media/test-mpeg2-ac3.ts"
#define CHUNK_SIZE 1024 * 10   /* Amount of bytes we are sending in each
buffer */
//#define APP_SRC_CAPS "video/mpegts,systemstream=true,packet-size=188"
#define APP_SRC_CAPS "video/mpegts"

GST_DEBUG_CATEGORY (playbin2);
#define GST_CAT_DEFAULT playbin2

/* Structure to contain all our information, so we can pass it to callbacks
*/
typedef struct _CustomData {
  GstElement *pipeline;
  GstElement *app_source;

  FILE* pFile;
  size_t totalBytesRead;

  guint sourceid;        /* To control the GSource */
 
  GMainLoop *main_loop;  /* GLib's Main Loop */
} CustomData;  

static gboolean push_data (CustomData *data) {
  GstBuffer *buffer;
  GstFlowReturn ret;

  g_print ("push data\n");

  /* Create a new empty buffer */
  buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);

  // Read stream from a file into GstBuffer
  if (fread(GST_BUFFER_DATA(buffer), CHUNK_SIZE, 1, data->pFile) != 1)
  {
    g_print("reached the end of stream");
    return FALSE;
  }
  data->totalBytesRead += CHUNK_SIZE;
  g_print("%d bytes read so far\n", (int)data->totalBytesRead);

  /* Push the buffer into the appsrc */
  g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
 
  /* Free the buffer now that we are done with it */
  gst_buffer_unref (buffer);
 
  if (ret != GST_FLOW_OK) {
    /* We got some error, stop sending data */
   g_printerr ("Error while pushing buffer: %d\n", ret);
   return FALSE;
  }

  return TRUE;
}

/* This signal callback triggers when appsrc needs data. Here, we add an
idle handler
 * to the mainloop to start pushing data into the appsrc */
static void start_feed (GstElement *source, guint size, CustomData *data) {
  if (data->sourceid == 0) {
    g_print ("Start feeding\n");
    data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
  }
}
 
/* This callback triggers when appsrc has enough data and we can stop
sending.
 * We remove the idle handler from the mainloop */
static void stop_feed (GstElement *source, CustomData *data) {
  if (data->sourceid != 0) {
    g_print ("Stop feeding\n");
    g_source_remove (data->sourceid);
    data->sourceid = 0;
  }
}

/* This function is called when an error message is posted on the bus */
static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
  GError *err;
  gchar *debug_info;
 
  /* Print error details on the screen */
  gst_message_parse_error (msg, &err, &debug_info);
  g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME
(msg->src), err->message);
  g_printerr ("Debugging information: %s\n", debug_info ? debug_info :
"none");
  g_clear_error (&err);
  g_free (debug_info);
 
  g_main_loop_quit (data->main_loop);
}

/* This function is called when playbin2 has created the appsrc element, so
we have
 * a chance to configure it. */
static void source_setup (GstElement *pipeline, GstElement *source,
CustomData *data) {
  GstCaps *appsrc_caps;
   
  g_print ("Source has been created. Configuring.\n");
  data->app_source = source;

  g_print("set appsrc caps to %s\n", APP_SRC_CAPS);
  appsrc_caps = gst_caps_from_string (APP_SRC_CAPS);
  g_object_set (data->app_source, "caps", appsrc_caps, NULL);
  gst_caps_unref (appsrc_caps);

  g_print("connect appsrc signals\n");
  g_signal_connect (data->app_source, "need-data", G_CALLBACK (start_feed),
&data);
  g_signal_connect (data->app_source, "enough-data", G_CALLBACK (stop_feed),
&data);  
}

int main(int argc, char *argv[]) {
  CustomData data;
  GstElement *pipeline;
  GstBus *bus;
  GstMessage *msg;
 
  /* Initialize cumstom data structure */
  memset (&data, 0, sizeof (data));

  g_print("init gstreamer\n");
  /* Initialize GStreamer */
  gst_init (&argc, &argv);
 
  GST_DEBUG_CATEGORY_INIT (playbin2, "pipeline", 0, "appsrc pipeline
example");

  // g_print("create appsrc\n");
  // data.app_source = gst_element_factory_make ("appsrc", "stream_source");

  g_print("open stream file\n");
  data.pFile = fopen(STREAM_FILENAME, "r");
  if (data.pFile == NULL)
  {
    g_printerr("file not found!\n");
    return 0;
  }

  /* Build the pipeline */
//  pipeline = gst_parse_launch ("playbin2
uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
  g_print("launch playbin2\n");
  data.pipeline = gst_parse_launch ("playbin2 uri=appsrc://", NULL);
  g_signal_connect (data.pipeline, "source-setup", G_CALLBACK
(source_setup), &data);

  /* Instruct the bus to emit signals for each received message, and connect
to the interesting signals */
  g_print("get pipeline bus\n");
  bus = gst_element_get_bus (data.pipeline);
  g_print("add signal watch\n");
  gst_bus_add_signal_watch (bus);
  g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb,
&data);
  gst_object_unref (bus);
 
  /* Start playing the pipeline */
  g_print("pipeline set to PLAYING\n");
  gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
 
  /* Create a GLib Main Loop and set it to run */
  g_print("create main loop\n");
  data.main_loop = g_main_loop_new (NULL, FALSE);

  g_print("run main loop\n");
  g_main_loop_run (data.main_loop);
 
  g_print("exiting\n");
  /* Free resources */
  gst_element_set_state (data.pipeline, GST_STATE_NULL);
  gst_object_unref (data.pipeline);

  return 0;
} 



--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/playbin2-using-appsrc-tp4664197.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.


More information about the gstreamer-devel mailing list