gstreamer_core/tsrc/examples/metadata/src/read-metadata.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
child 29 567bb019e3e3
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) 2003 Thomas Vander Stichele <thomas@apestaart.org>
       
     3  *               2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
       
     4  *               2005 Andy Wingo <wingo@pobox.com>
       
     5  *               2005 Jan Schmidt <thaytan@mad.scientist.com>
       
     6  *
       
     7  * gst-metadata.c: Use GStreamer to display metadata within files.
       
     8  *
       
     9  * This library is free software; you can redistribute it and/or
       
    10  * modify it under the terms of the GNU Library General Public
       
    11  * License as published by the Free Software Foundation; either
       
    12  * version 2 of the License, or (at your option) any later version.
       
    13  *
       
    14  * This library is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    17  * Library General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU Library General Public
       
    20  * License along with this library; if not, write to the
       
    21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    22  * Boston, MA 02111-1307, USA.
       
    23  */
       
    24 
       
    25 
       
    26 #ifdef HAVE_CONFIG_H
       
    27 #  include "config.h"
       
    28 #endif
       
    29 
       
    30 
       
    31 #include <gst/gst_global.h>
       
    32 #include <string.h>
       
    33 #include <stdlib.h>
       
    34 #include <locale.h>
       
    35 #include <gst/gst.h>
       
    36 
       
    37 static char *filename = NULL;
       
    38 static GstElement *pipeline = NULL;
       
    39 static GstElement *source = NULL;
       
    40 
       
    41 #define NEW_PIPE_PER_FILE
       
    42 
       
    43 
       
    44 #define LOG_FILE "c:\\logs\\read-metadata_logs.txt" 
       
    45 #include "std_log_result.h" 
       
    46 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    47 
       
    48 
       
    49 void create_xml(int result)
       
    50 {
       
    51     if(result)
       
    52         assert_failed = 1;
       
    53     
       
    54     testResultXml(xmlfile);
       
    55     close_log_file();
       
    56 }
       
    57 static gboolean
       
    58 message_loop (GstElement * element, GstTagList ** tags)
       
    59 {
       
    60   GstBus *bus;
       
    61   gboolean done = FALSE;
       
    62 
       
    63   bus = gst_element_get_bus (element);
       
    64   g_return_val_if_fail (bus != NULL, FALSE);
       
    65   g_return_val_if_fail (tags != NULL, FALSE);
       
    66 
       
    67   while (!done) {
       
    68     GstMessage *message;
       
    69 
       
    70     message = gst_bus_pop (bus);
       
    71     if (message == NULL)
       
    72       /* All messages read, we're done */
       
    73       break;
       
    74 
       
    75     switch (GST_MESSAGE_TYPE (message)) {
       
    76       case GST_MESSAGE_ERROR:
       
    77       case GST_MESSAGE_EOS:
       
    78         gst_message_unref (message);
       
    79         return TRUE;
       
    80       case GST_MESSAGE_TAG:
       
    81       {
       
    82         GstTagList *new_tags;
       
    83 
       
    84         gst_message_parse_tag (message, &new_tags);
       
    85         if (*tags)
       
    86           *tags = gst_tag_list_merge (*tags, new_tags, GST_TAG_MERGE_KEEP);
       
    87         else
       
    88           *tags = new_tags;
       
    89         break;
       
    90       }
       
    91       default:
       
    92         break;
       
    93     }
       
    94     gst_message_unref (message);
       
    95   }
       
    96   gst_object_unref (bus);
       
    97   return TRUE;
       
    98 }
       
    99 
       
   100 static void
       
   101 make_pipeline (void)
       
   102 {
       
   103   GstElement *decodebin;
       
   104 
       
   105   if (pipeline != NULL)
       
   106     gst_object_unref (pipeline);
       
   107 
       
   108   pipeline = gst_pipeline_new (NULL);
       
   109   source = gst_element_factory_make ("filesrc", "source");
       
   110   g_assert (GST_IS_ELEMENT (source));
       
   111   
       
   112   decodebin = gst_element_factory_make ("decodebin", "decodebin");
       
   113   g_assert (GST_IS_ELEMENT (decodebin));
       
   114   
       
   115   gst_bin_add_many (GST_BIN (pipeline), source, decodebin, NULL);
       
   116   
       
   117   gst_element_link (source, decodebin);
       
   118   
       
   119 		
       
   120  
       
   121 }
       
   122 
       
   123 static void
       
   124 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
       
   125 {
       
   126   gint i, count;
       
   127 
       
   128   count = gst_tag_list_get_tag_size (list, tag);
       
   129 
       
   130   for (i = 0; i < count; i++) {
       
   131     gchar *str;
       
   132 
       
   133     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
       
   134       if (!gst_tag_list_get_string_index (list, tag, i, &str))
       
   135         g_assert_not_reached ();
       
   136     } else {
       
   137       str =
       
   138           g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
       
   139     }
       
   140 
       
   141     if (i == 0) {
       
   142       g_print ("  %15s: %s\n", gst_tag_get_nick (tag), str);
       
   143     } else {
       
   144       g_print ("                 : %s\n", str);
       
   145     }
       
   146 
       
   147     g_free (str);
       
   148   }
       
   149 }
       
   150 
       
   151 int
       
   152 main (int argc, char *argv[])
       
   153 {
       
   154   guint i = 1;
       
   155 
       
   156 	xmlfile = "read-metadata_logs";
       
   157   std_log(LOG_FILENAME_LINE, "Test Started read-metadata");
       
   158   setlocale (LC_ALL, "");
       
   159 
       
   160   gst_init (&argc, &argv);
       
   161 
       
   162   if (argc < 2) {
       
   163     g_print ("Please give filenames to read metadata from\n\n");
       
   164     return 1;
       
   165   }
       
   166   
       
   167   make_pipeline ();
       
   168   
       
   169   while (i <= argc) {
       
   170     GstStateChangeReturn sret;
       
   171     GstState state;
       
   172     GstTagList *tags = NULL;
       
   173 
       
   174     filename = argv[i];
       
   175     
       
   176     g_object_set (source, "location", filename, NULL);
       
   177 
       
   178     GST_DEBUG ("Starting reading for %s", filename);
       
   179 
       
   180 
       
   181     /* Decodebin will only commit to PAUSED if it actually finds a type;
       
   182      * otherwise the state change fails */
       
   183    
       
   184     sret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
       
   185 
       
   186     if (GST_STATE_CHANGE_ASYNC == sret) {
       
   187       if (GST_STATE_CHANGE_SUCCESS !=
       
   188           gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL,
       
   189               5 * GST_SECOND)) {
       
   190         g_print ("State change failed for %s. Aborting\n", filename);
       
   191         break;
       
   192       }
       
   193     } else if (sret != GST_STATE_CHANGE_SUCCESS) {
       
   194       g_print ("%s - Could not read file\n", filename);
       
   195       goto next_file;
       
   196     }
       
   197 
       
   198     if (!message_loop (GST_ELEMENT (pipeline), &tags)) {
       
   199       g_print ("Failed in message reading for %s\n", argv[i]);
       
   200     }
       
   201 
       
   202     if (tags) {
       
   203       g_print ("Metadata for %s:\n", argv[i]);
       
   204       gst_tag_list_foreach (tags, print_tag, NULL);
       
   205       gst_tag_list_free (tags);
       
   206       tags = NULL;
       
   207     } else
       
   208       g_print ("No metadata found for %s\n", argv[i]);
       
   209 
       
   210     sret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
       
   211 #ifndef NEW_PIPE_PER_FILE
       
   212     if (GST_STATE_CHANGE_ASYNC == sret) {
       
   213       if (GST_STATE_CHANGE_FAILURE ==
       
   214           gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL,
       
   215               GST_CLOCK_TIME_NONE)) {
       
   216         g_print ("State change failed. Aborting");
       
   217         break;
       
   218       }
       
   219     }
       
   220 #endif
       
   221 
       
   222   next_file:
       
   223     i++;
       
   224 
       
   225 #ifdef NEW_PIPE_PER_FILE
       
   226     make_pipeline ();
       
   227 #endif
       
   228   }
       
   229 
       
   230   if (pipeline)
       
   231     gst_object_unref (pipeline);
       
   232   
       
   233   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   234   create_xml(0); 
       
   235   return 0;
       
   236 }