1 From aa8dd50e9c9c37476a803cd5dc530786dd2a4104 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= <tim.muller@collabora.co.uk>
3 Date: Sat, 23 May 2009 00:45:33 +0100
4 Subject: [PATCH] tagreading: add gst-tagread test app to examples
6 Uses tagreadbin to extract tags from the given files/directories and
7 prints them out. See #564749.
11 tests/examples/Makefile.am | 2 +-
12 tests/examples/tagreading/Makefile.am | 6 +
13 tests/examples/tagreading/gst-tagread.c | 285 +++++++++++++++++++++++++++++++
14 5 files changed, 294 insertions(+), 1 deletions(-)
15 create mode 100644 tests/examples/tagreading/Makefile.am
16 create mode 100644 tests/examples/tagreading/gst-tagread.c
18 diff --git a/.gitignore b/.gitignore
19 index 005392e..b8398ca 100644
22 @@ -27,6 +27,7 @@ depcomp
26 +tests/examples/tagreading/gst-tagread
28 gst-plugins-base-*.tar*
30 diff --git a/configure.ac b/configure.ac
31 index 7a6ae5a..4de8751 100644
34 @@ -1015,6 +1015,7 @@ tests/examples/snapshot/Makefile
35 tests/examples/playrec/Makefile
36 tests/examples/volume/Makefile
37 tests/examples/v4l/Makefile
38 +tests/examples/tagreading/Makefile
41 tests/icles/playback/Makefile
42 diff --git a/tests/examples/Makefile.am b/tests/examples/Makefile.am
43 index 48c38a5..87d6e15 100644
44 --- a/tests/examples/Makefile.am
45 +++ b/tests/examples/Makefile.am
46 @@ -8,7 +8,7 @@ if USE_GIO
50 -SUBDIRS = app dynamic $(FT2_SUBDIRS) $(GIO_SUBDIRS) overlay playrec volume v4l encoding
51 +SUBDIRS = app dynamic $(FT2_SUBDIRS) $(GIO_SUBDIRS) overlay playrec volume v4l encoding tagreading
53 DIST_SUBDIRS = app dynamic gio overlay seek snapshot playrec volume v4l encoding
55 diff --git a/tests/examples/tagreading/Makefile.am b/tests/examples/tagreading/Makefile.am
57 index 0000000..2d34c54
59 +++ b/tests/examples/tagreading/Makefile.am
61 +noinst_PROGRAMS = gst-tagread
63 +gst_tagread_SOURCES = gst-tagread.c
64 +gst_tagread_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS)
65 +gst_tagread_LDFLAGS = $(GST_LIBS)
67 diff --git a/tests/examples/tagreading/gst-tagread.c b/tests/examples/tagreading/gst-tagread.c
69 index 0000000..4576b5f
71 +++ b/tests/examples/tagreading/gst-tagread.c
73 +/* GStreamer Command-Line Tag Reading Example
74 + * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
76 + * This library is free software; you can redistribute it and/or
77 + * modify it under the terms of the GNU Library General Public
78 + * License as published by the Free Software Foundation; either
79 + * version 2 of the License, or (at your option) any later version.
81 + * This library is distributed in the hope that it will be useful,
82 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
83 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84 + * Library General Public License for more details.
86 + * You should have received a copy of the GNU Library General Public
87 + * License along with this library; if not, write to the
88 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
89 + * Boston, MA 02111-1307, USA.
97 +#include <glib/gi18n.h>
99 +static gboolean print_version = FALSE;
101 +static GstElement *tagreadbin = NULL;
104 +print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
108 + count = gst_tag_list_get_tag_size (list, tag);
110 + for (i = 0; i < count; i++) {
113 + if (gst_tag_get_type (tag) == G_TYPE_STRING) {
114 + if (!gst_tag_list_get_string_index (list, tag, i, &str))
115 + g_assert_not_reached ();
116 + } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
119 + img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
123 + caps_str = GST_BUFFER_CAPS (img) ?
124 + gst_caps_to_string (GST_BUFFER_CAPS (img)) : g_strdup ("unknown");
125 + str = g_strdup_printf ("buffer of %u bytes, type: %s",
126 + GST_BUFFER_SIZE (img), caps_str);
129 + str = g_strdup ("NULL buffer");
133 + g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
137 + g_print ("%25s: %s\n", gst_tag_get_nick (tag), str);
139 + g_print ("%25s: %s\n", "", str);
147 +extract_tags (const gchar * uri)
149 + GstStateChangeReturn ret;
150 + gboolean got_error = FALSE;
151 + gboolean got_eos = FALSE;
152 + GList *list = NULL;
154 + g_print ("\n\nURI: %s\n", uri);
156 + if (tagreadbin == NULL) {
157 + tagreadbin = gst_element_factory_make ("tagreadbin", NULL);
158 + if (tagreadbin == NULL)
159 + g_error ("Could not create 'tagreadbin' element, check your setup!");
162 + g_object_set (tagreadbin, "uri", uri, NULL);
164 + /* this might fail, but if it does there'll be an error message on the bus */
165 + ret = gst_element_set_state (tagreadbin, GST_STATE_PLAYING);
169 + msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (tagreadbin),
170 + 5 * GST_SECOND, GST_MESSAGE_TAG | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
172 + /* no message for 5 seconds? - bad */
174 + if (ret == GST_STATE_CHANGE_FAILURE) {
175 + g_warning ("tagreadbin state change failed, but did not get an error "
176 + "message! (uri: %s)", uri);
178 + g_warning ("No tags or EOS for more than 5 seconds, bailing out! "
184 + switch (GST_MESSAGE_TYPE (msg)) {
185 + case GST_MESSAGE_TAG:{
186 + GstTagList *tags = NULL;
188 + gst_message_parse_tag (msg, &tags);
189 + GST_LOG ("TAG: %" GST_PTR_FORMAT, tags);
190 + list = g_list_append (list, tags);
193 + case GST_MESSAGE_EOS:
197 + case GST_MESSAGE_ERROR:{
198 + GError *err = NULL;
201 + gst_message_parse_error (msg, &err, &dbg);
203 + g_warning ("Failed to read tags from %s:\nError: %s\nDebug: %s\n",
204 + uri, err->message, (dbg) ? dbg : "(No debug details)");
205 + g_error_free (err);
212 + g_assert_not_reached ();
215 + gst_message_unref (msg);
217 + while (!(got_error || got_eos));
219 + /* In case we got here after a timeout, some applications may prefer to not
220 + * set the state to NULL here and instead throw away (and leak) the
221 + * tagreadbin instance and continue with a new one, in case the reason for
222 + * the timeout was that the streaming thread deadlocked due to a buggy
223 + * element, in which case _set_state() is likely to deadlock too). */
224 + gst_element_set_state (tagreadbin, GST_STATE_NULL);
226 + /* FIXME: should be able to re-use */
227 + gst_object_unref (tagreadbin);
231 + g_list_foreach (list, (GFunc) gst_tag_list_free, NULL);
232 + g_list_free (list);
240 +process_file (const gchar * filename)
242 + GError *err = NULL;
248 + /* Recurse into directories */
249 + if ((dir = g_dir_open (filename, 0, NULL))) {
250 + const gchar *entry;
252 + while ((entry = g_dir_read_name (dir))) {
255 + path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
256 + process_file (path);
264 + if (!g_path_is_absolute (filename)) {
267 + cur_dir = g_get_current_dir ();
268 + path = g_build_filename (cur_dir, filename, NULL);
271 + path = g_strdup (filename);
274 + uri = g_filename_to_uri (path, NULL, &err);
279 + g_warning ("Couldn't convert filename to URI: %s\n", err->message);
280 + g_error_free (err);
284 + /* now get the tags */
285 + tags = extract_tags (uri);
287 + /* .. and print them */
289 + while (tags != NULL) {
290 + GstTagList *taglist = tags->data;
293 + g_print ("\nCONTAINER TAGS:\n");
295 + g_print ("\nSTREAM #%u TAGS:\n", i);
297 + gst_tag_list_foreach (taglist, print_tag, NULL);
298 + gst_tag_list_free (taglist);
299 + tags = g_list_delete_link (tags, tags);
307 +main (int argc, char **argv)
309 + gchar **filenames = NULL;
311 + GError *err = NULL;
312 + GOptionContext *ctx;
313 + GOptionEntry options[] = {
314 + {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
315 + N_("Print version information and exit"), NULL},
316 + {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
321 + bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
322 + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
323 + textdomain (GETTEXT_PACKAGE);
326 + if (!g_thread_supported ())
327 + g_thread_init (NULL);
329 + ctx = g_option_context_new ("FILES");
330 + g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
331 + g_option_context_add_group (ctx, gst_init_get_option_group ());
332 + if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
333 + g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
336 + g_option_context_free (ctx);
338 + if (print_version) {
339 + g_print ("gst-tagread version " VERSION "\n");
343 + if (filenames == NULL || *filenames == NULL) {
344 + g_print ("Please pass one or more filenames to gst-tagread\n\n");
348 + num = g_strv_length (filenames);
350 + for (i = 0; i < num; ++i) {
351 + process_file (filenames[i]);
354 + g_strfreev (filenames);