Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * gst_rtsp_eos_example.c
- *
- * Created on: Jul 18, 2011
- * Author: rjobbagy
- */
- #include <gst/gst.h>
- #include <time.h>
- #define EOS_SEND_TIME 5
- gboolean catch_eos = FALSE;
- clock_t start, end;
- double elapsed;
- int iter = 1000;
- int catch = 0;
- int dontcatch = 0;
- GstElement *bin,*pipeline, *source, *sink;
- GstElement *decodebin,*colorsp0;
- GstElement *queue;
- GstCaps* caps;
- GstStateChangeReturn ret;
- GstBus *bus;
- static void on_rtspsrc_pad_added (GstElement *element, GstPad *pad, void *data)
- {
- GstElement *decodebin = GST_ELEMENT (data);
- GstPad *sinkpad;
- sinkpad = gst_element_get_static_pad (decodebin, "sink");
- GstPadLinkReturn ret = gst_pad_link (pad, sinkpad);
- fprintf(stderr, "return: %d\n", ret);
- gst_object_unref (sinkpad);
- }
- static void on_decodebin2_pad_added (GstElement *element, GstPad *pad, gboolean last, void *data)
- {
- GstElement *colorsp0 = GST_ELEMENT (data);
- GstPad *sinkpad;
- sinkpad = gst_element_get_static_pad (colorsp0, "sink");
- GstPadLinkReturn ret = gst_pad_link (pad, sinkpad);
- fprintf(stderr, "decodebin2 return: %d\n", ret);
- gst_object_unref (sinkpad);
- }
- int
- main (int argc, char *argv[])
- {
- /* init */
- gst_init (&argc, &argv);
- /* create */
- pipeline = gst_pipeline_new ("my_pipeline");
- bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
- gst_object_unref (bus);
- GstPad *pad,*binpad;
- bin = gst_bin_new ("mybin");
- fprintf(stderr,"ARGC: %d\n",argc);
- if(argc == 2){
- source = gst_element_factory_make ("rtspsrc", "vidsource");
- decodebin = gst_element_factory_make ("decodebin2", "decode");
- colorsp0 = gst_element_factory_make ("ffmpegcolorspace", "colorspace0");
- }else
- source = gst_element_factory_make ("videotestsrc", "vidsource");
- sink = gst_element_factory_make ("xvimagesink", "sink");
- if (!pipeline || !bus || !bin || !source || !sink) {
- g_printerr ("One element could not be created. Exiting.\n");
- return -1;
- }
- if(argc == 2){
- if ( !decodebin || !colorsp0 ) {
- g_printerr ("One element could not be created. Exiting.\n");
- return -1;
- }
- }
- if(argc == 2){
- g_object_set (G_OBJECT (source), "location",argv[1], NULL);
- g_object_set(G_OBJECT(source),"latency",1000,NULL);
- }else{
- g_object_set (G_OBJECT (source), "pattern", 18, NULL);
- g_object_set (G_OBJECT (source), "is-live", 1, NULL);
- }
- /* First add the elements to the bin */
- if(argc == 2){
- gst_bin_add_many (GST_BIN (bin), source,decodebin, colorsp0,NULL);
- gst_bin_add_many (GST_BIN (pipeline), bin,sink, NULL);
- g_signal_connect (source , "pad-added", G_CALLBACK (on_rtspsrc_pad_added),decodebin);
- g_signal_connect (decodebin , "new-decoded-pad", G_CALLBACK (on_decodebin2_pad_added), colorsp0);
- pad = gst_element_get_static_pad(colorsp0, "src");
- binpad = gst_ghost_pad_new("src", pad);
- if(!gst_element_add_pad(bin, binpad)) {
- fprintf(stderr,"add_pad failed (ghost pad)\n");
- }
- gst_element_link_many (bin, sink, NULL);
- }else if(argc < 2){
- queue = gst_element_factory_make ("queue","queue0");
- if(!queue){
- fprintf(stderr,"Not created\n");
- }
- gst_bin_add_many (GST_BIN (bin), source,queue,NULL);
- /* add the bin to the pipeline */
- gst_bin_add_many (GST_BIN (pipeline), bin,sink,NULL);
- fprintf(stderr,"Created a bin \n");
- /* link the elements */
- if(!gst_element_link_many (source,queue, sink,NULL)){
- g_print ("Failed to link one or more elements!\n");
- return -1;
- }
- }
- g_print ("Now playing\n");
- ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
- if (ret == GST_STATE_CHANGE_FAILURE) {
- GstMessage *msg;
- g_print ("Failed to start up pipeline!\n");
- /* check if there is an error message with details on the bus */
- msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
- if (msg) {
- GError *err = NULL;
- gst_message_parse_error (msg, &err, NULL);
- g_print ("ERROR: %s\n", err->message);
- g_error_free (err);
- gst_message_unref (msg);
- }
- return -1;
- }
- g_print ("Running...\n");
- start = clock();
- /* check if there is an error message with details on the bus */
- gboolean sent_eos = FALSE;
- while(TRUE){
- end = clock();
- elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
- if(elapsed > EOS_SEND_TIME && !sent_eos){
- fprintf(stderr,"POSTED EOS MSG: %f \n",elapsed);
- gst_element_send_event (pipeline, gst_event_new_eos());
- if(!sent_eos){
- sent_eos = TRUE;
- }
- }
- /* check if there is an error message with details on the bus */
- if(elapsed > ((double)EOS_SEND_TIME + 0.5)){
- GstMessage* msg;
- msg = gst_bus_pop_filtered(bus,GST_MESSAGE_EOS);
- if(msg){
- g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (msg));
- switch (GST_MESSAGE_TYPE (msg)) {
- case GST_MESSAGE_EOS:
- fprintf (stderr,"End-of-stream\n");
- catch_eos = TRUE;
- break;
- case GST_MESSAGE_ERROR: {
- gchar *debug = NULL;
- GError *err = NULL;
- gst_message_parse_error (msg, &err, &debug);
- g_print ("Error: %s\n", err->message);
- g_error_free (err);
- if (debug) {
- g_print ("Debug details: %s\n", debug);
- g_free (debug);
- }
- break;
- }
- }
- }else if(elapsed > ((double)EOS_SEND_TIME + 5)){
- fprintf(stderr,"Don't find EOS MSG !!! : %f \n",elapsed);
- return -1;
- }
- }
- if(catch_eos){
- fprintf(stderr,"CATCH EOS \n");
- gst_element_set_state (pipeline, GST_STATE_NULL);
- gst_object_unref(pipeline);
- return 0;
- }else if(elapsed > (EOS_SEND_TIME + 5)){
- fprintf(stderr,"DONT CATCH \n");
- gst_element_set_state (pipeline, GST_STATE_NULL);
- gst_object_unref(pipeline);
- return -1;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement