tools/: Add back --version command line option (#340460).
[platform/upstream/gstreamer.git] / tools / gst-typefind.c
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  *
6  * gst-typefind.c: Use GStreamer to find the type of a file
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <locale.h>
32 #include <gst/gst.h>
33
34 #include "tools.h"
35
36 static void
37 have_type_handler (GstElement * typefind, guint probability,
38     const GstCaps * caps, GstCaps ** p_caps)
39 {
40   if (p_caps) {
41     *p_caps = gst_caps_copy (caps);
42   }
43 }
44
45 static void
46 typefind_file (const gchar * filename)
47 {
48   GstStateChangeReturn sret;
49   GstElement *pipeline;
50   GstElement *source;
51   GstElement *typefind;
52   GstElement *fakesink;
53   GstState state;
54   GstCaps *caps = NULL;
55   GDir *dir;
56
57   if ((dir = g_dir_open (filename, 0, NULL))) {
58     const gchar *entry;
59
60     while ((entry = g_dir_read_name (dir))) {
61       gchar *path;
62
63       path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
64       typefind_file (path);
65       g_free (path);
66     }
67
68     g_dir_close (dir);
69     return;
70   }
71
72   pipeline = gst_pipeline_new ("pipeline");
73
74   source = gst_element_factory_make ("filesrc", "source");
75   g_assert (GST_IS_ELEMENT (source));
76   typefind = gst_element_factory_make ("typefind", "typefind");
77   g_assert (GST_IS_ELEMENT (typefind));
78   fakesink = gst_element_factory_make ("fakesink", "fakesink");
79   g_assert (GST_IS_ELEMENT (typefind));
80
81   gst_bin_add_many (GST_BIN (pipeline), source, typefind, fakesink, NULL);
82   gst_element_link_many (source, typefind, fakesink, NULL);
83
84   g_signal_connect (G_OBJECT (typefind), "have-type",
85       G_CALLBACK (have_type_handler), &caps);
86
87   g_object_set (source, "location", filename, NULL);
88
89   GST_DEBUG ("Starting typefinding for %s", filename);
90
91   /* typefind will only commit to PAUSED if it actually finds a type;
92    * otherwise the state change fails */
93   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
94
95   /* wait until state change either completes or fails */
96   sret = gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL, -1);
97
98   switch (sret) {
99     case GST_STATE_CHANGE_FAILURE:{
100       GstMessage *msg;
101       GstBus *bus;
102       GError *err = NULL;
103
104       bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
105       msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
106       gst_object_unref (bus);
107
108       if (msg) {
109         gst_message_parse_error (msg, &err, NULL);
110         g_printerr ("%s - FAILED: %s\n", filename, err->message);
111         g_error_free (err);
112         gst_message_unref (msg);
113       } else {
114         g_printerr ("%s - FAILED: unknown error\n", filename);
115       }
116       break;
117     }
118     case GST_STATE_CHANGE_SUCCESS:{
119       if (caps) {
120         gchar *caps_str;
121
122         caps_str = gst_caps_to_string (caps);
123         g_print ("%s - %s\n", filename, caps_str);
124         g_free (caps_str);
125         gst_caps_unref (caps);
126       } else {
127         g_print ("%s - %s\n", filename, "No type found");
128       }
129       break;
130     }
131     default:
132       g_assert_not_reached ();
133   }
134
135   gst_element_set_state (pipeline, GST_STATE_NULL);
136   gst_object_unref (pipeline);
137 }
138
139 int
140 main (int argc, char *argv[])
141 {
142   gchar **filenames = NULL;
143   guint num, i;
144   GError *err = NULL;
145   GOptionContext *ctx;
146   GOptionEntry options[] = {
147     GST_TOOLS_GOPTION_VERSION,
148     {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
149     {NULL}
150   };
151
152 #ifdef ENABLE_NLS
153   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
154   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
155   textdomain (GETTEXT_PACKAGE);
156 #endif
157
158   ctx = g_option_context_new ("gst-typefind");
159   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
160   g_option_context_add_group (ctx, gst_init_get_option_group ());
161   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
162     g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
163     exit (1);
164   }
165   g_option_context_free (ctx);
166
167   gst_tools_print_version ("gst-typefind");
168
169   if (filenames == NULL || *filenames == NULL) {
170     g_print ("Please give a filename to typefind\n\n");
171     return 1;
172   }
173
174   num = g_strv_length (filenames);
175
176   for (i = 0; i < num; ++i) {
177     typefind_file (filenames[i]);
178   }
179
180   g_strfreev (filenames);
181
182   return 0;
183 }