Guest User

mp3 gstreamer test

a guest
Feb 8th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <gst/gst.h>
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include <gst/app/gstappsink.h>
  7.  
  8. GMainLoop *loop;
  9. GstElement *pipeline;
  10. GstElement *uridecodebin;
  11. GstElement *appsink;
  12. GstPad *sinkpad;
  13. GThread *loop_thread;
  14.  
  15. static gboolean _gst_on_bus_message (GstBus * bus, GstMessage * message, void * data)
  16. {
  17.     switch (GST_MESSAGE_TYPE (message))
  18.     {
  19.     case GST_MESSAGE_STATE_CHANGED:
  20.     {
  21.         gchar *name;
  22.         GstState old_state, new_state;
  23.         gst_message_parse_state_changed (message, &old_state, &new_state, NULL);
  24.         name = gst_object_get_name (message->src);
  25.         printf("state change: %d -> %d (object %s)\r\n", old_state, new_state, name);
  26.         g_free (name);
  27.  
  28.         break;
  29.     }
  30.     case GST_MESSAGE_EOS:
  31.     {
  32.         printf("Finished playback\n");
  33.         break;
  34.      }  
  35.     case GST_MESSAGE_ERROR:
  36.     {
  37.         GError *err = NULL;
  38.         gchar *dbg_info = NULL;
  39.        
  40.         gst_message_parse_error (message, &err, &dbg_info);
  41.         printf("BUSMSG ERROR from element %s: %s\r\n", GST_OBJECT_NAME (message->src), err->message);
  42.         printf("Debugging info: %s\r\n", (dbg_info) ? dbg_info : "none");
  43.         g_error_free (err);
  44.         g_free (dbg_info);
  45.  
  46.         break;
  47.     }
  48.     case GST_MESSAGE_BUFFERING:
  49.         printf ("GST_MESSAGE_BUFFERING\r\n");
  50.         break;
  51.     default:
  52.     printf ("other bus msg %d %s\r\n", GST_MESSAGE_TYPE (message), gst_message_type_get_name(GST_MESSAGE_TYPE (message)));
  53.         break;
  54.     }
  55.  
  56.     return TRUE;
  57. }
  58.  
  59. static GstFlowReturn _gst_on_new_sample (GstElement * elt, void * data)
  60. {
  61.   GstSample *sample;
  62.   printf ("on_new_sample\r\n");
  63.   sample = gst_app_sink_pull_sample (GST_APP_SINK (elt));
  64.   gst_sample_unref (sample);
  65.   return GST_FLOW_OK;
  66. }
  67.  
  68. static gboolean _gst_on_pad_added (GstElement *gstdecodebin, GstPad *pad, gpointer user_data)
  69. {
  70.     GstCaps *caps;
  71.     gchar *caps_str;
  72.     gst_object_ref (pad);
  73.  
  74.     caps = gst_pad_query_caps (pad, NULL);
  75.     caps_str = gst_caps_to_string (caps);
  76.     printf ("pad added, caps: %s\r\n", caps_str);
  77.     g_free (caps_str);
  78.     gst_caps_unref (caps);
  79.  
  80.     appsink = gst_element_factory_make ("appsink", NULL);
  81.     gst_bin_add (GST_BIN(pipeline), appsink);
  82.  
  83.     g_signal_connect (appsink, "new-sample", G_CALLBACK (_gst_on_new_sample), NULL);
  84.     g_object_set (G_OBJECT (appsink), "max-buffers", 1, "sync", FALSE, "emit-signals", TRUE, NULL);
  85.  
  86.     sinkpad = gst_element_get_compatible_pad (appsink, pad, NULL);
  87.     if (gst_pad_link (pad,sinkpad) == GST_PAD_LINK_OK)
  88.     {
  89.         printf ("link src->sinkpad ok\r\n");
  90.     }
  91.     else
  92.     {
  93.         printf ("link src->sinkpad FAIL\r\n");
  94.     }
  95.  
  96.     return TRUE;
  97. }
  98.  
  99. static gpointer _gst_loop_thread_func (gpointer data)
  100. {
  101.     g_main_loop_run (loop);
  102.     return NULL;
  103. }
  104.  
  105. static void _gst_setup (char *uri)
  106. {
  107.     GstBus *bus;
  108.  
  109.     loop = g_main_loop_new (NULL, FALSE);
  110.     loop_thread = g_thread_new ("myloop", _gst_loop_thread_func, NULL);
  111.     pipeline = gst_pipeline_new ("mypipeline");
  112.     uridecodebin = gst_element_factory_make ("uridecodebin", "myuridecodebin");
  113.  
  114.     gst_bin_add (GST_BIN(pipeline), uridecodebin);
  115.  
  116.     // If I create the appsink here, it works
  117.     //appsink = gst_element_factory_make ("appsink", NULL);
  118.     //gst_bin_add (GST_BIN(pipeline), appsink);
  119.  
  120.     bus = gst_element_get_bus (pipeline);
  121.     gst_bus_add_watch (bus, (GstBusFunc) _gst_on_bus_message, NULL);
  122.     gst_object_unref (bus);
  123.  
  124.     g_signal_connect (uridecodebin, "pad-added", G_CALLBACK (_gst_on_pad_added), NULL);
  125.     g_object_set (G_OBJECT(uridecodebin), "uri", uri, NULL);
  126.     g_object_set (G_OBJECT (uridecodebin), "caps", gst_caps_from_string ("audio/mpeg, mpegversion = (int) { 1, 2 };"), NULL);
  127. }
  128.  
  129. int main (int argc, char *argv[])
  130. {
  131.   gst_init (&argc, &argv);
  132.   _gst_setup (argv[1]);
  133.   sleep (2);
  134.   printf ("\r\ngoing to PAUSED now\r\n");
  135.   gst_element_set_state (pipeline, GST_STATE_PAUSED);
  136.   sleep (2);
  137.   printf ("\r\ngoing to PLAYING now\r\n");
  138.   gst_element_set_state (pipeline, GST_STATE_PLAYING);
  139.   sleep(10000);
  140.  
  141.   return 0;
  142. }
Add Comment
Please, Sign In to add comment