segmentation fault in g_main_loop_run

Mayank Agarwal mayank77fromindia at gmail.com
Mon Nov 24 03:07:32 PST 2014


Hi,

I am writing the code for mpeg dash player using gstreamer.Right now i
am able to get
media type of mpd file as application/xml but its giving segmentation fault in a
g_main_loop_run.Also i have following doubts:

1.how should i be able to download the ts or mp4 streams
2.Is the procedure same as buffering the stream in Progressive
streaming tutorials in gstreamer tutorials
3.I don't want to use stanadlone hlsdemux plugin as i want to control
the stream and bitrate.
4.Any code i can refer to,also please guide regarding the below
code,whether i am going right
or i should change path,the aim is to build mpeg dash player using gstreamer.

Regards
Mayank


#include <stdio.h>
#include <gst/gst.h>


#define GRAPH_LENGTH 80

typedef struct _CustomData {
  gboolean is_live;
  GstElement *pipeline;
  GMainLoop *loop;
  gint buffering_level;
} CustomData;


static gboolean
my_bus_callback (GstBus     *bus,
GstMessage *message,
gpointer    data)
{

  GMainLoop *loop = data;
  g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR: {
      GError *err;
      gchar *debug;

      gst_message_parse_error (message, &err, &debug);
      g_print ("Error: %s\n", err->message);
      g_error_free (err);
      g_free (debug);

      g_main_loop_quit (loop);
      break;
    }
    case GST_MESSAGE_EOS:
      /* end-of-stream */

      g_print ("End of stream reached\n");
      g_main_loop_quit (loop);

      break;
    default:
      /* unhandled message */
      break;
  }

  /* we want to be notified again the next time there is a message
   * on the bus, so returning TRUE (FALSE means we want to stop watching
   * for messages on the bus and our callback should not be called again)
   */
  return TRUE;
}


static gboolean
idle_exit_loop (gpointer data)
{
  g_main_loop_quit ((GMainLoop *) data);

  /* once */
  return FALSE;
}

static void
cb_typefound (GstElement *typefind,
     guint       probability,
     GstCaps    *caps,
     gpointer    data)
{
  GMainLoop *loop = data;
  gchar *type;

  type = gst_caps_to_string (caps);
  g_print ("Media type %s found, probability %d%%\n", type, probability);
  g_free (type);


  g_idle_add (idle_exit_loop, loop);
}




static gboolean refresh_ui (CustomData *data) {
  GstQuery *query;
  gboolean result;

  printf("cmes in..refresh_ui....0...\n");
  query = gst_query_new_buffering (GST_FORMAT_PERCENT);
  result = gst_element_query (data->pipeline, query);
  printf("cmes in..refresh_ui....1...\n");
  if (result) {
    gint n_ranges, range, i;
    gchar graph[GRAPH_LENGTH + 1];
    GstFormat format = GST_FORMAT_TIME;
    gint64 position = 0, duration = 0;

    memset (graph, ' ', GRAPH_LENGTH);
    graph[GRAPH_LENGTH] = '\0';

    n_ranges = gst_query_get_n_buffering_ranges (query);
    for (range = 0; range < n_ranges; range++) {
      gint64 start, stop;
      gst_query_parse_nth_buffering_range (query, range, &start, &stop);
      start = start * GRAPH_LENGTH / 100;
      stop = stop * GRAPH_LENGTH / 100;
      for (i = (gint)start; i < stop; i++)
        graph [i] = '-';
    }

    printf("cmes in..refresh_ui....2...\n");

    if (gst_element_query_position (data->pipeline, &format, &position) &&
        GST_CLOCK_TIME_IS_VALID (position) &&
        gst_element_query_duration (data->pipeline, &format, &duration) &&
        GST_CLOCK_TIME_IS_VALID (duration)) {
      i = (gint)(GRAPH_LENGTH * (double)position / (double)(duration + 1));
      graph [i] = data->buffering_level < 100 ? 'X' : '>';
    }
    g_print ("[%s]", graph);
    printf("cmes in..refresh_ui....3...\n");
    if (data->buffering_level < 100) {
      g_print (" Buffering: %3d%%", data->buffering_level);
    } else {
      g_print ("                ");
    }
    g_print ("\r");
  }

  printf("cmes in..refresh_ui....4...\n");
  return TRUE;

}









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


  GMainLoop *loop;
  GstElement *pipeline, *source, *typefind, *fakesink;
  GstBus *bus;
  CustomData data;

  gst_init(&argc,&argv);


  memset (&data, 0, sizeof (data));
  data.buffering_level = 100;

  printf("cmes here..0...\n");
/* create a new pipeline to hold the elements */
  pipeline = gst_pipeline_new ("pipe");

  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  gst_bus_add_watch (bus, my_bus_callback, NULL);
  gst_object_unref (bus);

  printf("cmes here...1...\n");

  /* create file source and typefind element */
  source = gst_element_factory_make("souphttpsrc","source");
  g_object_set (source, "location", argv[1], NULL);
  typefind = gst_element_factory_make ("typefind", "typefinder");
  g_signal_connect (typefind, "have-type", G_CALLBACK (cb_typefound), loop);
  fakesink = gst_element_factory_make ("fakesink", "sink");

 if (!pipeline || !source || !typefind || !fakesink) {
    g_printerr ("Not all elements could be created.\n");
    return -1;
  }

  if(!source)
  g_printerr ("source element could not be created.\n");

  if(!typefind)
  g_printerr ("typefind element could not be created.\n");

  if(!fakesink)
  g_printerr ("fakesink element could not be created.\n");


  printf("cmes here...2...\n");
  /* setup */
  gst_bin_add_many (GST_BIN (pipeline), source, typefind, fakesink, NULL);

   printf("cmes here...2.5...\n");
  if(gst_element_link_many (source, typefind, fakesink, NULL) != TRUE)
  {
    g_printerr ("Elements could not be linked.\n");
    gst_object_unref (pipeline);
    return -1;
  }

  printf("cmes here...2.6...\n");
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
  printf("cmes here...2.61...\n");
  loop = g_main_loop_new (NULL, FALSE);
  data.loop = loop;
  data.pipeline = pipeline;
  data.is_live = TRUE;

  printf("cmes here...2.62...\n");
  g_timeout_add_seconds (1, (GSourceFunc)refresh_ui, &data);
  g_main_loop_run (loop);
  printf("cmes here...2.63...\n");


  /* unset */

  printf("cmes here...2.7...\n");
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
  gst_object_unref (GST_OBJECT (pipeline));

  printf("cmes here...3...\n");
   return 0;


}


More information about the gstreamer-devel mailing list