1 /* GStreamer command line device monitor testing utility
2 * Copyright (C) 2014 Tim-Philipp Müller <tim@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
27 #include <gst/gst-i18n-app.h>
28 #include <gst/math-compat.h>
33 GST_DEBUG_CATEGORY (devmon_debug);
34 #define GST_CAT_DEFAULT devmon_debug
39 GstDeviceMonitor *monitor;
43 static gboolean bus_msg_handler (GstBus * bus, GstMessage * msg, gpointer data);
46 get_launch_line (GstDevice * device)
48 static const char *const ignored_propnames[] =
49 { "name", "parent", "direction", "template", "caps", NULL };
52 GstElement *pureelement;
53 GParamSpec **properties, *property;
54 GValue value = G_VALUE_INIT;
55 GValue pvalue = G_VALUE_INIT;
56 guint i, number_of_properties;
57 GstElementFactory *factory;
59 element = gst_device_create_element (device, NULL);
64 factory = gst_element_get_factory (element);
66 gst_object_unref (element);
70 if (!gst_plugin_feature_get_name (factory)) {
71 gst_object_unref (element);
75 launch_line = g_string_new (gst_plugin_feature_get_name (factory));
77 pureelement = gst_element_factory_create (factory, NULL);
79 /* get paramspecs and show non-default properties */
81 g_object_class_list_properties (G_OBJECT_GET_CLASS (element),
82 &number_of_properties);
84 for (i = 0; i < number_of_properties; i++) {
86 gboolean ignore = FALSE;
87 property = properties[i];
89 /* skip some properties */
90 if ((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE)
93 for (j = 0; ignored_propnames[j]; j++)
94 if (!g_strcmp0 (ignored_propnames[j], property->name))
100 /* Can't use _param_value_defaults () because sub-classes modify the
104 g_value_init (&value, property->value_type);
105 g_value_init (&pvalue, property->value_type);
106 g_object_get_property (G_OBJECT (element), property->name, &value);
107 g_object_get_property (G_OBJECT (pureelement), property->name, &pvalue);
108 if (gst_value_compare (&value, &pvalue) != GST_VALUE_EQUAL) {
109 gchar *valuestr = gst_value_serialize (&value);
112 GST_WARNING ("Could not serialize property %s:%s",
113 GST_OBJECT_NAME (element), property->name);
118 g_string_append_printf (launch_line, " %s=%s",
119 property->name, valuestr);
125 g_value_unset (&value);
126 g_value_unset (&pvalue);
131 gst_object_unref (element);
132 gst_object_unref (pureelement);
134 return g_string_free (launch_line, FALSE);
139 print_structure_field (GQuark field_id, const GValue * value,
144 if (G_VALUE_HOLDS_UINT (value)) {
145 val = g_strdup_printf ("%u (0x%08x)", g_value_get_uint (value),
146 g_value_get_uint (value));
148 val = gst_value_serialize (value);
152 g_print ("\n\t\t%s = %s", g_quark_to_string (field_id), val);
154 g_print ("\n\t\t%s - could not serialise field of type %s",
155 g_quark_to_string (field_id), G_VALUE_TYPE_NAME (value));
163 device_added (GstDevice * device)
165 gchar *device_class, *str, *name;
170 caps = gst_device_get_caps (device);
172 size = gst_caps_get_size (caps);
174 name = gst_device_get_display_name (device);
175 device_class = gst_device_get_device_class (device);
176 props = gst_device_get_properties (device);
178 g_print ("\nDevice found:\n\n");
179 g_print ("\tname : %s\n", name);
180 g_print ("\tclass : %s\n", device_class);
181 for (i = 0; i < size; ++i) {
182 GstStructure *s = gst_caps_get_structure (caps, i);
183 str = gst_structure_to_string (s);
184 g_print ("\t%s %s\n", (i == 0) ? "caps :" : " ", str);
188 g_print ("\tproperties:");
189 gst_structure_foreach (props, print_structure_field, NULL);
190 gst_structure_free (props);
193 str = get_launch_line (device);
194 if (gst_device_has_classes (device, "Source"))
195 g_print ("\tgst-launch-1.0 %s ! ...\n", str);
196 if (gst_device_has_classes (device, "Sink"))
197 g_print ("\tgst-launch-1.0 ... ! %s\n", str);
202 g_free (device_class);
205 gst_caps_unref (caps);
209 device_removed (GstDevice * device)
213 name = gst_device_get_display_name (device);
215 g_print ("Device removed:\n");
216 g_print ("\tname : %s\n", name);
222 bus_msg_handler (GstBus * bus, GstMessage * msg, gpointer user_data)
226 switch (GST_MESSAGE_TYPE (msg)) {
227 case GST_MESSAGE_DEVICE_ADDED:
228 gst_message_parse_device_added (msg, &device);
229 device_added (device);
230 gst_object_unref (device);
232 case GST_MESSAGE_DEVICE_REMOVED:
233 gst_message_parse_device_removed (msg, &device);
234 device_removed (device);
235 gst_object_unref (device);
238 g_print ("%s message\n", GST_MESSAGE_TYPE_NAME (msg));
246 main (int argc, char **argv)
248 gboolean print_version = FALSE;
250 gchar **arg, **args = NULL;
251 gboolean follow = FALSE;
253 GOptionEntry options[] = {
254 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
255 N_("Print version information and exit"), NULL},
256 {"follow", 'f', 0, G_OPTION_ARG_NONE, &follow,
257 N_("Don't exit after showing the initial device list, but wait "
258 "for devices to added/removed."), NULL},
259 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL},
267 setlocale (LC_ALL, "");
270 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
271 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
272 textdomain (GETTEXT_PACKAGE);
275 g_set_prgname ("gst-device-monitor-" GST_API_VERSION);
277 ctx = g_option_context_new ("[DEVICE_CLASSES[:FILTER_CAPS]] "
278 "[DEVICE_CLASSES[:FILTER_CAPS]] …");
279 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
280 g_option_context_add_group (ctx, gst_init_get_option_group ());
281 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
282 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
283 g_option_context_free (ctx);
284 g_clear_error (&err);
287 g_option_context_free (ctx);
289 GST_DEBUG_CATEGORY_INIT (devmon_debug, "device-monitor", 0,
290 "gst-device-monitor");
295 version_str = gst_version_string ();
296 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
297 g_print ("%s\n", version_str);
298 g_print ("%s\n", GST_PACKAGE_ORIGIN);
299 g_free (version_str);
304 app.loop = g_main_loop_new (NULL, FALSE);
305 app.monitor = gst_device_monitor_new ();
307 bus = gst_device_monitor_get_bus (app.monitor);
308 app.bus_watch_id = gst_bus_add_watch (bus, bus_msg_handler, &app);
309 gst_object_unref (bus);
311 /* process optional remaining arguments in the form
312 * DEVICE_CLASSES or DEVICE_CLASSES:FILTER_CAPS */
313 for (arg = args; arg != NULL && *arg != NULL; ++arg) {
314 gchar **filters = g_strsplit (*arg, ":", -1);
315 if (filters != NULL && filters[0] != NULL) {
316 GstCaps *caps = NULL;
318 if (filters[1] != NULL) {
319 caps = gst_caps_from_string (filters[1]);
321 g_warning ("Couldn't parse device filter caps '%s'", filters[1]);
323 gst_device_monitor_add_filter (app.monitor, filters[0], caps);
325 gst_caps_unref (caps);
326 g_strfreev (filters);
331 g_print ("Probing devices...\n\n");
333 timer = g_timer_new ();
335 if (!gst_device_monitor_start (app.monitor)) {
336 g_printerr ("Failed to start device monitor!\n");
340 GST_INFO ("Took %.2f seconds", g_timer_elapsed (timer, NULL));
342 devices = gst_device_monitor_get_devices (app.monitor);
343 if (devices != NULL) {
344 while (devices != NULL) {
345 GstDevice *device = devices->data;
347 device_added (device);
348 gst_object_unref (device);
349 devices = g_list_delete_link (devices, devices);
352 g_print ("No devices found!\n");
356 g_print ("Monitoring devices, waiting for devices to be removed or "
357 "new devices to be added...\n");
358 g_main_loop_run (app.loop);
361 gst_device_monitor_stop (app.monitor);
362 gst_object_unref (app.monitor);
364 g_source_remove (app.bus_watch_id);
365 g_main_loop_unref (app.loop);
366 g_timer_destroy (timer);