Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / debian / patches / 0004-tagreading-add-gst-tagread-test-app-to-examples.patch
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
5
6 Uses tagreadbin to extract tags from the given files/directories and
7 prints them out. See #564749.
8 ---
9  .gitignore                              |    1 +
10  configure.ac                            |    1 +
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
17
18 diff --git a/.gitignore b/.gitignore
19 index 005392e..b8398ca 100644
20 --- a/.gitignore
21 +++ b/.gitignore
22 @@ -27,6 +27,7 @@ depcomp
23  autoregen.sh
24  ABOUT-NLS
25  _stdint.h
26 +tests/examples/tagreading/gst-tagread
27  
28  gst-plugins-base-*.tar*
29  
30 diff --git a/configure.ac b/configure.ac
31 index 7a6ae5a..4de8751 100644
32 --- a/configure.ac
33 +++ b/configure.ac
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
39  tests/files/Makefile
40  tests/icles/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
47  GIO_SUBDIRS = gio
48  endif
49  
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
52  
53  DIST_SUBDIRS = app dynamic gio overlay seek snapshot playrec volume v4l encoding
54  
55 diff --git a/tests/examples/tagreading/Makefile.am b/tests/examples/tagreading/Makefile.am
56 new file mode 100644
57 index 0000000..2d34c54
58 --- /dev/null
59 +++ b/tests/examples/tagreading/Makefile.am
60 @@ -0,0 +1,6 @@
61 +noinst_PROGRAMS = gst-tagread
62 +
63 +gst_tagread_SOURCES = gst-tagread.c
64 +gst_tagread_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS)
65 +gst_tagread_LDFLAGS = $(GST_LIBS)
66 +
67 diff --git a/tests/examples/tagreading/gst-tagread.c b/tests/examples/tagreading/gst-tagread.c
68 new file mode 100644
69 index 0000000..4576b5f
70 --- /dev/null
71 +++ b/tests/examples/tagreading/gst-tagread.c
72 @@ -0,0 +1,285 @@
73 +/* GStreamer Command-Line Tag Reading Example
74 + * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
75 + *
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.
80 + *
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.
85 + *
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.
90 + */
91 +
92 +#ifdef HAVE_CONFIG_H
93 +#include "config.h"
94 +#endif
95 +
96 +#include <gst/gst.h>
97 +#include <glib/gi18n.h>
98 +
99 +static gboolean print_version = FALSE;
100 +
101 +static GstElement *tagreadbin = NULL;
102 +
103 +static void
104 +print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
105 +{
106 +  gint i, count;
107 +
108 +  count = gst_tag_list_get_tag_size (list, tag);
109 +
110 +  for (i = 0; i < count; i++) {
111 +    gchar *str;
112 +
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) {
117 +      GstBuffer *img;
118 +
119 +      img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
120 +      if (img) {
121 +        gchar *caps_str;
122 +
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);
127 +        g_free (caps_str);
128 +      } else {
129 +        str = g_strdup ("NULL buffer");
130 +      }
131 +    } else {
132 +      str =
133 +          g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
134 +    }
135 +
136 +    if (i == 0) {
137 +      g_print ("%25s: %s\n", gst_tag_get_nick (tag), str);
138 +    } else {
139 +      g_print ("%25s: %s\n", "", str);
140 +    }
141 +
142 +    g_free (str);
143 +  }
144 +}
145 +
146 +static GList *
147 +extract_tags (const gchar * uri)
148 +{
149 +  GstStateChangeReturn ret;
150 +  gboolean got_error = FALSE;
151 +  gboolean got_eos = FALSE;
152 +  GList *list = NULL;
153 +
154 +  g_print ("\n\nURI: %s\n", uri);
155 +
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!");
160 +  }
161 +
162 +  g_object_set (tagreadbin, "uri", uri, NULL);
163 +
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);
166 +  do {
167 +    GstMessage *msg;
168 +
169 +    msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (tagreadbin),
170 +        5 * GST_SECOND, GST_MESSAGE_TAG | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
171 +
172 +    /* no message for 5 seconds? - bad */
173 +    if (msg == NULL) {
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);
177 +      } else {
178 +        g_warning ("No tags or EOS for more than 5 seconds, bailing out! "
179 +            "(uri: %s)", uri);
180 +      }
181 +      break;
182 +    }
183 +
184 +    switch (GST_MESSAGE_TYPE (msg)) {
185 +      case GST_MESSAGE_TAG:{
186 +        GstTagList *tags = NULL;
187 +
188 +        gst_message_parse_tag (msg, &tags);
189 +        GST_LOG ("TAG: %" GST_PTR_FORMAT, tags);
190 +        list = g_list_append (list, tags);
191 +        break;
192 +      }
193 +      case GST_MESSAGE_EOS:
194 +        GST_LOG ("EOS");
195 +        got_eos = TRUE;
196 +        break;
197 +      case GST_MESSAGE_ERROR:{
198 +        GError *err = NULL;
199 +        gchar *dbg = NULL;
200 +
201 +        gst_message_parse_error (msg, &err, &dbg);
202 +        if (err) {
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);
206 +          g_free (dbg);
207 +          got_error = TRUE;
208 +        }
209 +        break;
210 +      }
211 +      default:
212 +        g_assert_not_reached ();
213 +        break;
214 +    }
215 +    gst_message_unref (msg);
216 +  }
217 +  while (!(got_error || got_eos));
218 +
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);
225 +
226 +  /* FIXME: should be able to re-use */
227 +  gst_object_unref (tagreadbin);
228 +  tagreadbin = NULL;
229 +
230 +  if (got_error) {
231 +    g_list_foreach (list, (GFunc) gst_tag_list_free, NULL);
232 +    g_list_free (list);
233 +    list = NULL;
234 +  }
235 +
236 +  return list;
237 +}
238 +
239 +static void
240 +process_file (const gchar * filename)
241 +{
242 +  GError *err = NULL;
243 +  GList *tags;
244 +  GDir *dir;
245 +  gchar *uri, *path;
246 +  guint i;
247 +
248 +  /* Recurse into directories */
249 +  if ((dir = g_dir_open (filename, 0, NULL))) {
250 +    const gchar *entry;
251 +
252 +    while ((entry = g_dir_read_name (dir))) {
253 +      gchar *path;
254 +
255 +      path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
256 +      process_file (path);
257 +      g_free (path);
258 +    }
259 +
260 +    g_dir_close (dir);
261 +    return;
262 +  }
263 +
264 +  if (!g_path_is_absolute (filename)) {
265 +    gchar *cur_dir;
266 +
267 +    cur_dir = g_get_current_dir ();
268 +    path = g_build_filename (cur_dir, filename, NULL);
269 +    g_free (cur_dir);
270 +  } else {
271 +    path = g_strdup (filename);
272 +  }
273 +
274 +  uri = g_filename_to_uri (path, NULL, &err);
275 +  g_free (path);
276 +  path = NULL;
277 +
278 +  if (err) {
279 +    g_warning ("Couldn't convert filename to URI: %s\n", err->message);
280 +    g_error_free (err);
281 +    return;
282 +  }
283 +
284 +  /* now get the tags */
285 +  tags = extract_tags (uri);
286 +
287 +  /* .. and print them */
288 +  i = 0;
289 +  while (tags != NULL) {
290 +    GstTagList *taglist = tags->data;
291 +
292 +    if (i == 0)
293 +      g_print ("\nCONTAINER TAGS:\n");
294 +    else
295 +      g_print ("\nSTREAM #%u TAGS:\n", i);
296 +
297 +    gst_tag_list_foreach (taglist, print_tag, NULL);
298 +    gst_tag_list_free (taglist);
299 +    tags = g_list_delete_link (tags, tags);
300 +    ++i;
301 +  }
302 +
303 +  g_free (uri);
304 +}
305 +
306 +int
307 +main (int argc, char **argv)
308 +{
309 +  gchar **filenames = NULL;
310 +  guint num, i;
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},
317 +    {NULL}
318 +  };
319 +
320 +#ifdef ENABLE_NLS
321 +  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
322 +  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
323 +  textdomain (GETTEXT_PACKAGE);
324 +#endif
325 +
326 +  if (!g_thread_supported ())
327 +    g_thread_init (NULL);
328 +
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));
334 +    exit (1);
335 +  }
336 +  g_option_context_free (ctx);
337 +
338 +  if (print_version) {
339 +    g_print ("gst-tagread version " VERSION "\n");
340 +    exit (0);
341 +  }
342 +
343 +  if (filenames == NULL || *filenames == NULL) {
344 +    g_print ("Please pass one or more filenames to gst-tagread\n\n");
345 +    return 1;
346 +  }
347 +
348 +  num = g_strv_length (filenames);
349 +
350 +  for (i = 0; i < num; ++i) {
351 +    process_file (filenames[i]);
352 +  }
353 +
354 +  g_strfreev (filenames);
355 +
356 +  return 0;
357 +}