Guest User

Gstreamer code

a guest
Jun 13th, 2012
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <gst/gst.h>
  2.  
  3. GstElement *pipeline, *source, *muxer, *encoder, *conv, *sink;
  4. GstCaps *audio;
  5.  
  6. static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
  7. {
  8.   GMainLoop *loop = (GMainLoop *) data;
  9.  
  10.   switch (GST_MESSAGE_TYPE (msg))
  11.   {
  12.     case GST_MESSAGE_EOS:
  13.       g_print ("End-of-stream\n");
  14.       g_main_loop_quit (loop);
  15.       break;
  16.     case GST_MESSAGE_ERROR:
  17.     {
  18.       gchar *debug;
  19.       GError *err;
  20.  
  21.       gst_message_parse_error (msg, &err, &debug);
  22.       g_free (debug);
  23.  
  24.       g_print ("Error: %s\n", err->message);
  25.       g_error_free (err);
  26.  
  27.       g_main_loop_quit (loop);
  28.       break;
  29.     }
  30.     default:
  31.       break;
  32.   }
  33.  
  34.   return TRUE;
  35. }
  36.  
  37. int main (void)
  38. {
  39.   GMainLoop *loop;
  40.   GstBus *bus;
  41.  
  42.   /* initialize GStreamer */
  43.   gst_init (NULL, NULL);
  44.  
  45.   loop = g_main_loop_new (NULL, FALSE);
  46.  
  47.   /* create elements */
  48.   pipeline = gst_pipeline_new ("audio-player");
  49.   source = gst_element_factory_make ("pulsesrc", "pulse-source");
  50.   conv = gst_element_factory_make ("audioconvert", "converter");
  51.   encoder = gst_element_factory_make ("lamemp3enc", "mp3-muxer");  
  52.   sink = gst_element_factory_make ("filesink", "file-output");
  53.  
  54.   if (!pipeline || !source || !conv || !encoder || !sink)
  55.   {
  56.     g_print ("One element could not be created\n");
  57.     return -1;
  58.   }
  59.  
  60.   /* set device property on the pulsesource. Also add a message handler. */
  61.   g_object_set (G_OBJECT (source), "device", "alsa_input.pci-0000_00_1b.0.analog-stereo", NULL);
  62.   g_object_set (G_OBJECT (encoder), "target", 1, "bitrate", 128, "cbr", TRUE, NULL);
  63.   g_object_set (G_OBJECT (sink), "location", "audio.mp3", NULL);
  64.  
  65.   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  66.   gst_bus_add_watch (bus, bus_call, loop);
  67.   gst_object_unref (bus);
  68.  
  69.   /* put all elements in a bin */
  70.   gst_bin_add_many (GST_BIN (pipeline), source, conv, encoder, sink, NULL);
  71.  
  72.   /* set to playing and iterate. */
  73.   g_print ("Setting to PLAYING\n");
  74.   gst_element_set_state (pipeline, GST_STATE_PLAYING);
  75.   g_print ("Running\n");
  76.   g_main_loop_run (loop);
  77.  
  78.   /* clean up*/
  79.   g_print ("Returned, stopping playback\n");
  80.   gst_element_set_state (pipeline, GST_STATE_NULL);
  81.   g_print ("Deleting pipeline\n");
  82.   gst_object_unref (GST_OBJECT (pipeline));
  83.  
  84.   return 0;
  85. }
Add Comment
Please, Sign In to add comment