gstreamer_core/tsrc/examples/helloworld/src/helloworld.c
changeset 2 5505e8908944
child 8 4a7fac7dd34a
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     1 
       
     2 #include <gst/gst_global.h>
       
     3 #include <stdlib.h>
       
     4 #include <gst/gst.h>
       
     5 
       
     6 
       
     7 #define LOG_FILE "c:\\logs\\hello_world_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     if(result)
       
    15         assert_failed = 1;
       
    16     
       
    17     testResultXml(xmlfile);
       
    18     close_log_file();
       
    19 }
       
    20 
       
    21 static void
       
    22 event_loop (GstElement * pipe)
       
    23 {
       
    24   GstBus *bus;
       
    25   GstMessage *message = NULL;
       
    26 
       
    27   bus = gst_element_get_bus (GST_ELEMENT (pipe));
       
    28 
       
    29   while (TRUE) {
       
    30     message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
       
    31 
       
    32     g_assert (message != NULL);
       
    33 
       
    34     switch (message->type) {
       
    35       case GST_MESSAGE_EOS:
       
    36         gst_message_unref (message);
       
    37         return;
       
    38       case GST_MESSAGE_WARNING:
       
    39       case GST_MESSAGE_ERROR:{
       
    40         GError *gerror;
       
    41         gchar *debug;
       
    42 
       
    43         gst_message_parse_error (message, &gerror, &debug);
       
    44         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
       
    45         gst_message_unref (message);
       
    46         g_error_free (gerror);
       
    47         g_free (debug);
       
    48         return;
       
    49       }
       
    50       default:
       
    51         gst_message_unref (message);
       
    52         break;
       
    53     }
       
    54   }
       
    55 }
       
    56 
       
    57 int
       
    58 main (int argc, char *argv[])
       
    59 {
       
    60   GstElement *bin, *filesrc, *decoder, *audiosink;
       
    61   GstElement *conv, *resample;
       
    62 	xmlfile = "helloworld_logs";
       
    63   std_log(LOG_FILENAME_LINE, "Test Started hellowworld");
       
    64   
       
    65   gst_init (&argc, &argv);
       
    66  
       
    67   if (argc != 2) {
       
    68     g_print ("usage: %s <mp3 file>\n", argv[0]);
       
    69     std_log(LOG_FILENAME_LINE, "Test Failed argument need to be passed");
       
    70     create_xml(1); 
       
    71     exit (-1);
       
    72   }
       
    73 
       
    74   /* create a new bin to hold the elements */
       
    75   bin = gst_pipeline_new ("pipeline");
       
    76   g_assert (bin);
       
    77 
       
    78   /* create a disk reader */
       
    79   filesrc = gst_element_factory_make ("filesrc", "disk_source");
       
    80   g_assert (filesrc);
       
    81   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
       
    82 
       
    83   /* now it's time to get the decoder */
       
    84   decoder = gst_element_factory_make ("mad", "decode");
       
    85   if (!decoder) {
       
    86     std_log(LOG_FILENAME_LINE, "could not find plugin mad");
       
    87     g_print ("could not find plugin \"mad\"");
       
    88     return -1;
       
    89   }
       
    90 
       
    91   /* also, we need to add some converters to make sure the audio stream
       
    92    * from the decoder is converted into a format the audio sink can
       
    93    * understand (if necessary) */
       
    94   conv = gst_element_factory_make ("audioconvert", "audioconvert");
       
    95   if (!conv) {
       
    96     std_log(LOG_FILENAME_LINE, "could not create \"audioconvert\" element!");
       
    97     g_print ("could not create \"audioconvert\" element!");
       
    98     return -1;
       
    99   }
       
   100   resample = gst_element_factory_make ("audioresample", "audioresample");
       
   101   if (!conv) {
       
   102     std_log(LOG_FILENAME_LINE, "could not create \"audioresample\" element!");
       
   103     g_print ("could not create \"audioresample\" element!");
       
   104     return -1;
       
   105   }
       
   106 
       
   107   /* and an audio sink */
       
   108   audiosink = gst_element_factory_make ("devsoundsink", "play_audio");
       
   109   g_assert (audiosink);
       
   110 
       
   111   /* add objects to the main pipeline */
       
   112   gst_bin_add_many (GST_BIN (bin), filesrc, decoder, conv,
       
   113       resample, audiosink, NULL);
       
   114 
       
   115   /* link the elements */
       
   116   gst_element_link_many (filesrc, decoder, conv, resample, audiosink, NULL);
       
   117 
       
   118   /* start playing */
       
   119   std_log(LOG_FILENAME_LINE, "START PLAYING");
       
   120   gst_element_set_state (bin, GST_STATE_PLAYING);
       
   121   std_log(LOG_FILENAME_LINE, "STOP PLAYING ");
       
   122   /* Run event loop listening for bus messages until EOS or ERROR */
       
   123   event_loop (bin);
       
   124 
       
   125   /* stop the bin */
       
   126   std_log(LOG_FILENAME_LINE, "START BIN");
       
   127   gst_element_set_state (bin, GST_STATE_NULL);
       
   128   std_log(LOG_FILENAME_LINE, "START BIN");
       
   129 
       
   130 	std_log(LOG_FILENAME_LINE, "Test Successful");
       
   131   create_xml(0); 
       
   132   exit (0);
       
   133 }