Advertisement
Guest User

Untitled

a guest
Jul 18th, 2011
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1. /*
  2.  * gst_rtsp_eos_example.c
  3.  *
  4.  *  Created on: Jul 18, 2011
  5.  *      Author: rjobbagy
  6.  */
  7.  
  8. #include <gst/gst.h>
  9. #include <time.h>
  10.  
  11. #define EOS_SEND_TIME 5
  12.  
  13. gboolean catch_eos = FALSE;
  14. clock_t start, end;
  15. double elapsed;
  16.  
  17. int iter = 1000;
  18. int catch = 0;
  19. int dontcatch = 0;
  20.  
  21. GstElement *bin,*pipeline, *source, *sink;
  22. GstElement *decodebin,*colorsp0;
  23. GstElement *queue;
  24. GstCaps* caps;
  25. GstStateChangeReturn ret;
  26. GstBus *bus;
  27.  
  28. static void on_rtspsrc_pad_added (GstElement *element, GstPad *pad, void *data)
  29. {
  30.     GstElement *decodebin = GST_ELEMENT (data);
  31.     GstPad *sinkpad;
  32.     sinkpad = gst_element_get_static_pad (decodebin, "sink");
  33.     GstPadLinkReturn ret = gst_pad_link (pad, sinkpad);
  34.     fprintf(stderr, "return: %d\n", ret);
  35.     gst_object_unref (sinkpad);
  36. }
  37.  
  38. static void on_decodebin2_pad_added (GstElement *element, GstPad *pad, gboolean last, void *data)
  39. {
  40.     GstElement *colorsp0 = GST_ELEMENT (data);
  41.     GstPad *sinkpad;
  42.     sinkpad = gst_element_get_static_pad (colorsp0, "sink");
  43.     GstPadLinkReturn ret = gst_pad_link (pad, sinkpad);
  44.     fprintf(stderr, "decodebin2 return: %d\n", ret);
  45.     gst_object_unref (sinkpad);
  46. }
  47.  
  48. int
  49. main (int   argc, char *argv[])
  50. {
  51.   /* init */
  52.   gst_init (&argc, &argv);
  53.   /* create */
  54.   pipeline = gst_pipeline_new ("my_pipeline");
  55.   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  56.   gst_object_unref (bus);
  57.   GstPad *pad,*binpad;
  58.   bin = gst_bin_new ("mybin");
  59.   fprintf(stderr,"ARGC: %d\n",argc);
  60.   if(argc == 2){
  61.       source    =   gst_element_factory_make    ("rtspsrc", "vidsource");
  62.       decodebin =   gst_element_factory_make    ("decodebin2", "decode");
  63.       colorsp0  =   gst_element_factory_make    ("ffmpegcolorspace", "colorspace0");
  64.   }else
  65.       source = gst_element_factory_make ("videotestsrc", "vidsource");
  66.  
  67.   sink = gst_element_factory_make ("xvimagesink", "sink");
  68.  
  69.   if (!pipeline || !bus || !bin || !source || !sink) {
  70.     g_printerr ("One element could not be created. Exiting.\n");
  71.     return -1;
  72.   }
  73.   if(argc == 2){
  74.     if ( !decodebin || !colorsp0 ) {
  75.       g_printerr ("One element could not be created. Exiting.\n");
  76.       return -1;
  77.     }
  78.   }
  79.  
  80.   if(argc == 2){
  81.       g_object_set (G_OBJECT (source), "location",argv[1], NULL);
  82.       g_object_set(G_OBJECT(source),"latency",1000,NULL);
  83.   }else{
  84.       g_object_set (G_OBJECT (source), "pattern", 18, NULL);
  85.       g_object_set (G_OBJECT (source), "is-live", 1, NULL);
  86.   }
  87.   /* First add the elements to the bin */
  88.   if(argc == 2){
  89.       gst_bin_add_many (GST_BIN (bin), source,decodebin, colorsp0,NULL);
  90.       gst_bin_add_many (GST_BIN (pipeline), bin,sink, NULL);
  91.  
  92.       g_signal_connect (source , "pad-added", G_CALLBACK (on_rtspsrc_pad_added),decodebin);
  93.       g_signal_connect (decodebin , "new-decoded-pad", G_CALLBACK (on_decodebin2_pad_added), colorsp0);
  94.       pad = gst_element_get_static_pad(colorsp0, "src");
  95.       binpad = gst_ghost_pad_new("src", pad);
  96.       if(!gst_element_add_pad(bin, binpad)) {
  97.         fprintf(stderr,"add_pad failed (ghost pad)\n");
  98.       }
  99.       gst_element_link_many (bin, sink, NULL);
  100.   }else if(argc < 2){
  101.       queue =   gst_element_factory_make    ("queue","queue0");
  102.       if(!queue){
  103.           fprintf(stderr,"Not created\n");
  104.       }
  105.       gst_bin_add_many (GST_BIN (bin), source,queue,NULL);
  106.  
  107.       /* add the bin to the pipeline */
  108.       gst_bin_add_many (GST_BIN (pipeline), bin,sink,NULL);
  109.       fprintf(stderr,"Created a bin \n");
  110.  
  111.       /* link the elements */
  112.       if(!gst_element_link_many (source,queue, sink,NULL)){
  113.               g_print ("Failed to link one or more elements!\n");
  114.               return -1;
  115.           }
  116.  
  117.   }
  118.  
  119.   g_print ("Now playing\n");
  120.   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  121.  
  122.   if (ret == GST_STATE_CHANGE_FAILURE) {
  123.     GstMessage *msg;
  124.  
  125.     g_print ("Failed to start up pipeline!\n");
  126.  
  127.     /* check if there is an error message with details on the bus */
  128.     msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
  129.     if (msg) {
  130.       GError *err = NULL;
  131.  
  132.       gst_message_parse_error (msg, &err, NULL);
  133.       g_print ("ERROR: %s\n", err->message);
  134.       g_error_free (err);
  135.       gst_message_unref (msg);
  136.     }
  137.     return -1;
  138.   }
  139.   g_print ("Running...\n");
  140.   start = clock();
  141.  
  142.   /* check if there is an error message with details on the bus */
  143.   gboolean sent_eos = FALSE;
  144.   while(TRUE){
  145.       end = clock();
  146.       elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
  147.       if(elapsed > EOS_SEND_TIME && !sent_eos){
  148.           fprintf(stderr,"POSTED EOS MSG: %f \n",elapsed);
  149.           gst_element_send_event (pipeline, gst_event_new_eos());
  150.         if(!sent_eos){
  151.             sent_eos = TRUE;
  152.         }
  153.       }
  154.       /* check if there is an error message with details on the bus */
  155.       if(elapsed > ((double)EOS_SEND_TIME + 0.5)){
  156.             GstMessage* msg;
  157.             msg = gst_bus_pop_filtered(bus,GST_MESSAGE_EOS);
  158.             if(msg){
  159.               g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (msg));
  160.  
  161.               switch (GST_MESSAGE_TYPE (msg)) {
  162.                 case GST_MESSAGE_EOS:
  163.                   fprintf (stderr,"End-of-stream\n");
  164.                   catch_eos = TRUE;
  165.                   break;
  166.                 case GST_MESSAGE_ERROR: {
  167.                   gchar *debug = NULL;
  168.                   GError *err = NULL;
  169.  
  170.                   gst_message_parse_error (msg, &err, &debug);
  171.  
  172.                   g_print ("Error: %s\n", err->message);
  173.                   g_error_free (err);
  174.  
  175.                   if (debug) {
  176.                     g_print ("Debug details: %s\n", debug);
  177.                     g_free (debug);
  178.                   }
  179.  
  180.                   break;
  181.                 }
  182.               }
  183.             }else if(elapsed > ((double)EOS_SEND_TIME + 5)){
  184.                 fprintf(stderr,"Don't find EOS MSG !!! : %f \n",elapsed);
  185.                 return -1;
  186.             }
  187.       }
  188.       if(catch_eos){
  189.           fprintf(stderr,"CATCH EOS \n");
  190.           gst_element_set_state (pipeline, GST_STATE_NULL);
  191.           gst_object_unref(pipeline);
  192.           return 0;
  193.       }else if(elapsed > (EOS_SEND_TIME + 5)){
  194.           fprintf(stderr,"DONT CATCH \n");
  195.           gst_element_set_state (pipeline, GST_STATE_NULL);
  196.           gst_object_unref(pipeline);
  197.           return -1;
  198.       }
  199.   }
  200.  
  201.   return 0;
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement