gstreamer_core/tsrc/examples/gstseek/src/gstseek.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
     1 
       
     2 #include <gst/gst_global.h>
       
     3 #include <stdlib.h>
       
     4 #include <gst/gst.h>
       
     5 #include <gst/gstelement.h>
       
     6 
       
     7 #define LOG_FILE "c:\\logs\\launch_logs.txt" 
       
     8 #include "std_log_result.h" 
       
     9 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    10 
       
    11 
       
    12 void create_xml(int result)
       
    13 {
       
    14 
       
    15     if(result)
       
    16     {
       
    17         assert_failed = 1;
       
    18     } 
       
    19 
       
    20     testResultXml(xmlfile);
       
    21     close_log_file();
       
    22 
       
    23     if(result)
       
    24     {
       
    25         exit (-1);
       
    26     }    
       
    27 
       
    28 }
       
    29 
       
    30 GstElement *pipeline, *source, *wavparse,*sink,*conv,*resample,*decoder,*audioconvert,*audioresample ;
       
    31 GstBus *bus;
       
    32 GMainLoop *loop;
       
    33 
       
    34 static gboolean
       
    35 bus_call (GstBus     *bus,
       
    36           GstMessage *msg,
       
    37           gpointer    data)
       
    38 {
       
    39   switch (GST_MESSAGE_TYPE (msg)) {
       
    40     case GST_MESSAGE_EOS:
       
    41         gst_element_set_state (pipeline, GST_STATE_NULL);
       
    42         g_main_loop_quit(loop);
       
    43         gst_object_unref (GST_OBJECT (pipeline));
       
    44         std_log(LOG_FILENAME_LINE, "Test Successful");
       
    45         create_xml(0); 
       
    46       break;
       
    47     case GST_MESSAGE_ERROR: {
       
    48       gchar *debug;
       
    49       GError *err;
       
    50       gst_message_parse_error (msg, &err, &debug);
       
    51       g_free (debug);
       
    52       g_print ("Error: %s\n", err->message);
       
    53       g_error_free (err);
       
    54       std_log(LOG_FILENAME_LINE, "Test Failed");
       
    55       create_xml(1); 
       
    56       break;
       
    57     }
       
    58     default:
       
    59       break;
       
    60   }
       
    61 
       
    62   return TRUE;
       
    63 }
       
    64 
       
    65 static gboolean  cb_autoplug( GstBin * *bin,
       
    66                         GstPad        *pad,
       
    67                         GstCaps       *caps,
       
    68                         gpointer       user_data)
       
    69 {
       
    70     GstCaps* sinkcaps = NULL;    
       
    71     gboolean supported = FALSE;
       
    72     
       
    73     GstPad* sinkpad = gst_element_get_pad( sink, "sink");
       
    74     sinkcaps = gst_pad_get_caps( sinkpad );
       
    75     supported = gst_caps_is_subset( caps, sinkcaps );
       
    76     
       
    77     if( supported )
       
    78     {
       
    79         //link_with_devsoundsink = TRUE;
       
    80         return FALSE;
       
    81     }
       
    82     return TRUE;
       
    83 }
       
    84 
       
    85 static void
       
    86 cb_newpad (GstElement *decodebin,
       
    87        GstPad     *pad,
       
    88        gboolean    last,
       
    89        gpointer    data)
       
    90 {
       
    91     GstCaps *caps;
       
    92     GstStructure *str;
       
    93     GstPadLinkReturn linkret= GST_PAD_LINK_OK ;
       
    94    
       
    95 
       
    96     GstPad* sinkpad = NULL;
       
    97 
       
    98     
       
    99     /* check media type */
       
   100     caps = gst_pad_get_caps (pad);
       
   101     str = gst_caps_get_structure (caps, 0);
       
   102     if (!g_strrstr (gst_structure_get_name (str), "audio")) {
       
   103     gst_caps_unref (caps);
       
   104     return;
       
   105     }
       
   106     gst_caps_unref (caps);
       
   107     
       
   108 //    if( link_with_devsoundsink )
       
   109 //        {
       
   110         sinkpad = gst_element_get_pad( sink, "sink");
       
   111         //gst_bin_add_many (GST_BIN (pipeline),source, decoder,sink, NULL);
       
   112             
       
   113 //        }
       
   114 //    else
       
   115 //        {
       
   116 //        audioconvert = gst_element_factory_make ("audioconvert", "audioconvert");
       
   117 //        audioresample = gst_element_factory_make ("audioresample", "audioresample");        
       
   118 //        sinkpad = gst_element_get_pad( audioconvert, "sink");
       
   119 //        
       
   120 //        gst_bin_add_many (GST_BIN (pipeline),source, decoder,audioconvert,audioresample,sink, NULL);
       
   121 //        
       
   122 //        if (!gst_element_link (audioconvert,audioresample))
       
   123 //           g_error ("link(audioconvert,audioresample, sink) failed!\n");   
       
   124 //        if (!gst_element_link (audioresample, sink))
       
   125 //           g_error ("link(audioconvert,audioresample, sink) failed!\n");         
       
   126 //        }
       
   127     
       
   128     linkret = gst_pad_link (pad, sinkpad);
       
   129 }
       
   130 
       
   131 
       
   132 static gboolean
       
   133 cb_get_position (GstElement* temp /*pipeline*/)
       
   134 {
       
   135     GstFormat fmt = GST_FORMAT_TIME;
       
   136     gint64 pos = 0, len, time_nanoseconds, diff;
       
   137     GstState state;
       
   138     
       
   139     gst_element_get_state(GST_ELEMENT(pipeline),&state,NULL,-1);
       
   140     if (gst_element_query_position (pipeline, &fmt, &pos)
       
   141     && gst_element_query_duration (pipeline, &fmt, &len)) {
       
   142     g_print ("Time: %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",
       
   143     GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
       
   144     }
       
   145     time_nanoseconds = pos + 1000000000;
       
   146     
       
   147     if (!gst_element_seek (pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
       
   148     GST_SEEK_TYPE_SET, time_nanoseconds,
       
   149     GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
       
   150     g_print ("Seek failed!\n");
       
   151     }
       
   152     
       
   153     gst_element_get_state(GST_ELEMENT(pipeline),&state,NULL,-1);
       
   154     
       
   155     if (gst_element_query_position (pipeline, &fmt, &pos)
       
   156     && gst_element_query_duration (pipeline, &fmt, &len)) {
       
   157     g_print ("Time: %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "\r",
       
   158     GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
       
   159     }
       
   160     
       
   161     diff = pos - time_nanoseconds;
       
   162     
       
   163     if( diff > 1000000 )
       
   164         {
       
   165         create_xml( 1 );
       
   166         }
       
   167 
       
   168     /* call me again */
       
   169     return TRUE;
       
   170 }
       
   171 
       
   172 int main (int argc, char *argv[])
       
   173 {
       
   174     
       
   175     xmlfile = "launch_logs";
       
   176     std_log(LOG_FILENAME_LINE, "Test Started launch");
       
   177  
       
   178     if (argc != 2) {
       
   179       g_print ("usage: %s <Test file>\n", argv[0]);
       
   180       std_log(LOG_FILENAME_LINE, "Test Failed");
       
   181       create_xml(1); 
       
   182       exit (-1);
       
   183     }
       
   184     //link_with_devsoundsink = FALSE;
       
   185     gst_init (&argc, &argv);
       
   186     loop = g_main_loop_new (NULL, FALSE);
       
   187     
       
   188     /* create elements */
       
   189     pipeline = gst_pipeline_new ("audio-player");
       
   190     source = gst_element_factory_make ("filesrc", "file-source");
       
   191     decoder = gst_element_factory_make ("decodebin2", "decodebin2-decoder");
       
   192     sink = gst_element_factory_make ("devsoundsink", "sink");    
       
   193 //    audioconvert = gst_element_factory_make ("audioconvert", "audioconvert");
       
   194 //    audioresample = gst_element_factory_make ("audioresample", "audioresample");    
       
   195 //    sink = gst_element_factory_make ("devsoundsink", "sink");
       
   196     
       
   197     if (!pipeline || !source || !decoder || !sink) {
       
   198         g_print ("One element could not be created\n");
       
   199         return -1;
       
   200         }
       
   201     /* set filename property on the file source. Also add a message  handler. */
       
   202     g_object_set (G_OBJECT (source), "location", argv[1], NULL);
       
   203             /* put all elements in a bin */
       
   204     gst_bin_add_many (GST_BIN (pipeline),source, decoder,sink, NULL);
       
   205             /* link together - note that we cannot link the parser and  decoder yet, because the parser uses dynamic pads. For that, we set a pad-added signal handler. */
       
   206     gst_element_link (source, decoder);
       
   207    
       
   208 
       
   209     
       
   210     
       
   211     gst_bus_add_watch (gst_pipeline_get_bus (GST_PIPELINE (pipeline)), bus_call, loop);
       
   212     
       
   213     g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK (cb_newpad), NULL);
       
   214     g_signal_connect (decoder, "autoplug-continue", G_CALLBACK (cb_autoplug), NULL);
       
   215     
       
   216     g_timeout_add (1000/*in mili sec*/, (GSourceFunc) cb_get_position, NULL);
       
   217     
       
   218     //g_signal_connect (decoder, "pad-added", G_CALLBACK (new_pad_cb),pipeline);
       
   219             /* Now set to playing and iterate. */
       
   220     g_print ("Setting to PLAYING\n");
       
   221     gst_element_set_state (pipeline, GST_STATE_PLAYING);
       
   222     g_print ("Running\n");
       
   223     g_main_loop_run (loop);
       
   224             /* clean up nicely */
       
   225     g_print ("Returned, stopping playback\n");
       
   226     gst_element_set_state (pipeline, GST_STATE_NULL);
       
   227     g_print ("Deleting pipeline\n");
       
   228     gst_object_unref (GST_OBJECT (pipeline));
       
   229     
       
   230     g_print ("completed palying audio\n");
       
   231     //std_log(LOG_FILENAME_LINE, "Test Successful");
       
   232     //create_xml(0); 
       
   233     return 0;
       
   234 }