Advertisement
Guest User

Untitled

a guest
Sep 28th, 2010
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <string> /*sorry, I feel comfortable using std::strings.*/
  2. #include <gst/gst.h>
  3. #include <glib.h>
  4.  
  5. /*Global vars*/
  6. GstElement *pipeline=NULL, *textoverlay=NULL;
  7. GMainLoop *loop=NULL;
  8.  
  9. const char *avipath = "hardcoded-path-to-avi-file";
  10. const char *srtpath = "hardcoded-path-to-srt-file";
  11.  
  12.  
  13. /*Various functions*/
  14.  
  15. static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
  16. {
  17.  
  18.   switch (GST_MESSAGE_TYPE (msg)) {
  19.  
  20.     case GST_MESSAGE_EOS:
  21.       g_print ("End of stream\n");
  22.       g_main_loop_quit (loop);
  23.       break;
  24.  
  25.     case GST_MESSAGE_ERROR: {
  26.       gchar  *debug;
  27.       GError *error;
  28.  
  29.       gst_message_parse_error (msg, &error, &debug);
  30.       g_free (debug);
  31.  
  32.       g_printerr ("Error: %s\n", error->message);
  33.       g_error_free (error);
  34.  
  35.       g_main_loop_quit (loop);
  36.       break;
  37.     }
  38.     default:
  39.       break;
  40.   }
  41.  
  42.   return TRUE;
  43. }
  44.  
  45. static void on_pad_added (GstElement *element, GstPad *pad, gpointer data)
  46. {
  47.     std::string mime_type;
  48.     GstElement *queue, *decoder, *converter, *sink, *parser;
  49.     GstPad *sinkpad;   
  50.    
  51.     /*get mime-type*/
  52.     GstCaps* caps = gst_pad_get_caps (pad);
  53.     GstStructure* structure = gst_caps_get_structure(caps, 0);
  54.     mime_type = gst_structure_get_name(structure);
  55.     gst_caps_unref(caps);
  56.    
  57.     /*plug a queue in the pad and decided what to do according to the mimetype*/
  58.     queue = gst_element_factory_make("queue", NULL);
  59.     sinkpad = gst_element_get_static_pad(queue, "sink");
  60.     gst_pad_link(pad, sinkpad);
  61.     gst_object_unref(sinkpad);
  62.    
  63.     /*now check the mimetype and decide what to do*/
  64.     if (mime_type.find("video") != mime_type.npos)
  65.     {/*the video stream. I assume an XVID/DIVX stream*/
  66.    
  67.         decoder = gst_element_factory_make("ffdec_mpeg4", NULL);
  68.         converter = gst_element_factory_make("ffmpegcolorspace", NULL);
  69.         textoverlay = gst_element_factory_make("textoverlay", NULL);
  70.         sink = gst_element_factory_make("xvimagesink", NULL);
  71.        
  72.         gst_bin_add_many(GST_BIN(pipeline), queue, decoder, converter, textoverlay, sink, NULL);
  73.         gst_element_link_many(queue, decoder, converter, textoverlay, sink, NULL);
  74.        
  75.         gst_element_set_state(queue, GST_STATE_PAUSED);
  76.         gst_element_set_state(decoder, GST_STATE_PAUSED);
  77.         gst_element_set_state(converter, GST_STATE_PAUSED);
  78.         gst_element_set_state(textoverlay, GST_STATE_PAUSED);
  79.         gst_element_set_state(sink, GST_STATE_PAUSED);
  80.     }
  81.     else
  82.     {/*the audio stream. I assume an mp3 stream.*/
  83.    
  84.         parser = gst_element_factory_make("mp3parse", NULL);
  85.         decoder = gst_element_factory_make("mad", NULL);
  86.         converter = gst_element_factory_make("audioconvert", NULL);    
  87.         sink = gst_element_factory_make("alsasink", NULL);     
  88.        
  89.         gst_bin_add_many(GST_BIN(pipeline), queue, parser, decoder, converter, sink, NULL);    
  90.         gst_element_link_many(queue, parser, decoder, converter, sink, NULL);      
  91.        
  92.         gst_element_set_state(queue, GST_STATE_PAUSED);    
  93.         gst_element_set_state(parser, GST_STATE_PAUSED);
  94.         gst_element_set_state(decoder, GST_STATE_PAUSED);
  95.         gst_element_set_state(converter, GST_STATE_PAUSED);    
  96.         gst_element_set_state(sink, GST_STATE_PAUSED);
  97.     }
  98.  
  99. }
  100.  
  101. static void no_more_pads(GstElement *element, gpointer data)
  102. {  
  103.     gst_element_set_state(pipeline, GST_STATE_PLAYING);
  104. }
  105.  
  106.  
  107. int main(int argc, char *argv[])
  108. {
  109.     GstElement *source=NULL, *demuxer=NULL;
  110.     GstBus *bus;
  111.     gst_init(&argc, &argv);
  112.    
  113.     loop = g_main_loop_new(NULL, FALSE);
  114.    
  115.     pipeline = gst_pipeline_new("pipeline");
  116.    
  117.     source = gst_element_factory_make("filesrc", NULL);
  118.     demuxer = gst_element_factory_make("avidemux", NULL);
  119.    
  120.     g_object_set(G_OBJECT(source), "location", avipath, NULL);
  121.    
  122.     bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
  123.     gst_bus_add_watch(bus, bus_call, NULL);
  124.     gst_object_unref(bus);
  125.    
  126.     gst_bin_add_many(GST_BIN(pipeline), source, demuxer, NULL);
  127.     gst_element_link(source, demuxer);
  128.    
  129.     g_signal_connect (demuxer, "pad-added", G_CALLBACK(on_pad_added), NULL);
  130.     g_signal_connect (demuxer, "no-more-pads", G_CALLBACK(no_more_pads), NULL);
  131.    
  132.     gst_element_set_state(pipeline, GST_STATE_PAUSED);
  133.     g_main_loop_run(loop); 
  134.    
  135.     gst_element_set_state(pipeline, GST_STATE_NULL);
  136.     gst_object_unref(GST_OBJECT(pipeline));
  137.    
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement