|
Packit |
971217 |
/* GStreamer command line device monitor testing utility
|
|
Packit |
971217 |
* Copyright (C) 2014 Tim-Philipp Müller <tim@centricular.com>
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
971217 |
* modify it under the terms of the GNU Library General Public
|
|
Packit |
971217 |
* License as published by the Free Software Foundation; either
|
|
Packit |
971217 |
* version 2 of the License, or (at your option) any later version.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
971217 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
971217 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
971217 |
* Library General Public License for more details.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* You should have received a copy of the GNU Library General Public
|
|
Packit |
971217 |
* License along with this library; if not, write to the
|
|
Packit |
971217 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
Packit |
971217 |
* Boston, MA 02110-1301, USA.
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
971217 |
#include "config.h"
|
|
Packit |
971217 |
#endif
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#include <locale.h>
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#include <gst/gst.h>
|
|
Packit |
971217 |
#include <gst/gst-i18n-app.h>
|
|
Packit |
971217 |
#include <gst/math-compat.h>
|
|
Packit |
971217 |
#include <stdlib.h>
|
|
Packit |
971217 |
#include <stdio.h>
|
|
Packit |
971217 |
#include <string.h>
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG_CATEGORY (devmon_debug);
|
|
Packit |
971217 |
#define GST_CAT_DEFAULT devmon_debug
|
|
Packit |
971217 |
|
|
Packit |
971217 |
typedef struct
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GMainLoop *loop;
|
|
Packit |
971217 |
GstDeviceMonitor *monitor;
|
|
Packit |
971217 |
guint bus_watch_id;
|
|
Packit |
971217 |
} DevMonApp;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gboolean bus_msg_handler (GstBus * bus, GstMessage * msg, gpointer data);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gchar *
|
|
Packit |
971217 |
get_launch_line (GstDevice * device)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
static const char *const ignored_propnames[] =
|
|
Packit |
971217 |
{ "name", "parent", "direction", "template", "caps", NULL };
|
|
Packit |
971217 |
GString *launch_line;
|
|
Packit |
971217 |
GstElement *element;
|
|
Packit |
971217 |
GstElement *pureelement;
|
|
Packit |
971217 |
GParamSpec **properties, *property;
|
|
Packit |
971217 |
GValue value = G_VALUE_INIT;
|
|
Packit |
971217 |
GValue pvalue = G_VALUE_INIT;
|
|
Packit |
971217 |
guint i, number_of_properties;
|
|
Packit |
971217 |
GstElementFactory *factory;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
element = gst_device_create_element (device, NULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (!element)
|
|
Packit |
971217 |
return NULL;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
factory = gst_element_get_factory (element);
|
|
Packit |
971217 |
if (!factory) {
|
|
Packit |
971217 |
gst_object_unref (element);
|
|
Packit |
971217 |
return NULL;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (!gst_plugin_feature_get_name (factory)) {
|
|
Packit |
971217 |
gst_object_unref (element);
|
|
Packit |
971217 |
return NULL;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
launch_line = g_string_new (gst_plugin_feature_get_name (factory));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
pureelement = gst_element_factory_create (factory, NULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* get paramspecs and show non-default properties */
|
|
Packit |
971217 |
properties =
|
|
Packit |
971217 |
g_object_class_list_properties (G_OBJECT_GET_CLASS (element),
|
|
Packit |
971217 |
&number_of_properties);
|
|
Packit |
971217 |
if (properties) {
|
|
Packit |
971217 |
for (i = 0; i < number_of_properties; i++) {
|
|
Packit |
971217 |
gint j;
|
|
Packit |
971217 |
gboolean ignore = FALSE;
|
|
Packit |
971217 |
property = properties[i];
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* skip some properties */
|
|
Packit |
971217 |
if ((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE)
|
|
Packit |
971217 |
continue;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
for (j = 0; ignored_propnames[j]; j++)
|
|
Packit |
971217 |
if (!g_strcmp0 (ignored_propnames[j], property->name))
|
|
Packit |
971217 |
ignore = TRUE;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (ignore)
|
|
Packit |
971217 |
continue;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* Can't use _param_value_defaults () because sub-classes modify the
|
|
Packit |
971217 |
* values already.
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_value_init (&value, property->value_type);
|
|
Packit |
971217 |
g_value_init (&pvalue, property->value_type);
|
|
Packit |
971217 |
g_object_get_property (G_OBJECT (element), property->name, &value);
|
|
Packit |
971217 |
g_object_get_property (G_OBJECT (pureelement), property->name, &pvalue);
|
|
Packit |
971217 |
if (gst_value_compare (&value, &pvalue) != GST_VALUE_EQUAL) {
|
|
Packit |
971217 |
gchar *valuestr = gst_value_serialize (&value);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (!valuestr) {
|
|
Packit |
971217 |
GST_WARNING ("Could not serialize property %s:%s",
|
|
Packit |
971217 |
GST_OBJECT_NAME (element), property->name);
|
|
Packit |
971217 |
g_free (valuestr);
|
|
Packit |
971217 |
goto next;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_string_append_printf (launch_line, " %s=%s",
|
|
Packit |
971217 |
property->name, valuestr);
|
|
Packit |
971217 |
g_free (valuestr);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
next:
|
|
Packit |
971217 |
g_value_unset (&value);
|
|
Packit |
971217 |
g_value_unset (&pvalue);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
g_free (properties);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_object_unref (element);
|
|
Packit |
971217 |
gst_object_unref (pureelement);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return g_string_free (launch_line, FALSE);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gboolean
|
|
Packit |
971217 |
print_structure_field (GQuark field_id, const GValue * value,
|
|
Packit |
971217 |
gpointer user_data)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
gchar *val;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (G_VALUE_HOLDS_UINT (value)) {
|
|
Packit |
971217 |
val = g_strdup_printf ("%u (0x%08x)", g_value_get_uint (value),
|
|
Packit |
971217 |
g_value_get_uint (value));
|
|
Packit |
971217 |
} else {
|
|
Packit |
971217 |
val = gst_value_serialize (value);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (val != NULL)
|
|
Packit |
971217 |
g_print ("\n\t\t%s = %s", g_quark_to_string (field_id), val);
|
|
Packit |
971217 |
else
|
|
Packit |
971217 |
g_print ("\n\t\t%s - could not serialise field of type %s",
|
|
Packit |
971217 |
g_quark_to_string (field_id), G_VALUE_TYPE_NAME (value));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_free (val);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return TRUE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
device_added (GstDevice * device)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
gchar *device_class, *str, *name;
|
|
Packit |
971217 |
GstCaps *caps;
|
|
Packit |
971217 |
GstStructure *props;
|
|
Packit |
971217 |
guint i, size = 0;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
caps = gst_device_get_caps (device);
|
|
Packit |
971217 |
if (caps != NULL)
|
|
Packit |
971217 |
size = gst_caps_get_size (caps);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
name = gst_device_get_display_name (device);
|
|
Packit |
971217 |
device_class = gst_device_get_device_class (device);
|
|
Packit |
971217 |
props = gst_device_get_properties (device);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_print ("\nDevice found:\n\n");
|
|
Packit |
971217 |
g_print ("\tname : %s\n", name);
|
|
Packit |
971217 |
g_print ("\tclass : %s\n", device_class);
|
|
Packit |
971217 |
for (i = 0; i < size; ++i) {
|
|
Packit |
971217 |
GstStructure *s = gst_caps_get_structure (caps, i);
|
|
Packit |
971217 |
str = gst_structure_to_string (s);
|
|
Packit |
971217 |
g_print ("\t%s %s\n", (i == 0) ? "caps :" : " ", str);
|
|
Packit |
971217 |
g_free (str);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
if (props) {
|
|
Packit |
971217 |
g_print ("\tproperties:");
|
|
Packit |
971217 |
gst_structure_foreach (props, print_structure_field, NULL);
|
|
Packit |
971217 |
gst_structure_free (props);
|
|
Packit |
971217 |
g_print ("\n");
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
str = get_launch_line (device);
|
|
Packit |
971217 |
if (gst_device_has_classes (device, "Source"))
|
|
Packit |
971217 |
g_print ("\tgst-launch-1.0 %s ! ...\n", str);
|
|
Packit |
971217 |
if (gst_device_has_classes (device, "Sink"))
|
|
Packit |
971217 |
g_print ("\tgst-launch-1.0 ... ! %s\n", str);
|
|
Packit |
971217 |
g_free (str);
|
|
Packit |
971217 |
g_print ("\n");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_free (name);
|
|
Packit |
971217 |
g_free (device_class);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (caps != NULL)
|
|
Packit |
971217 |
gst_caps_unref (caps);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static void
|
|
Packit |
971217 |
device_removed (GstDevice * device)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
gchar *name;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
name = gst_device_get_display_name (device);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_print ("Device removed:\n");
|
|
Packit |
971217 |
g_print ("\tname : %s\n", name);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_free (name);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gboolean
|
|
Packit |
971217 |
bus_msg_handler (GstBus * bus, GstMessage * msg, gpointer user_data)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GstDevice *device;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
switch (GST_MESSAGE_TYPE (msg)) {
|
|
Packit |
971217 |
case GST_MESSAGE_DEVICE_ADDED:
|
|
Packit |
971217 |
gst_message_parse_device_added (msg, &device);
|
|
Packit |
971217 |
device_added (device);
|
|
Packit |
971217 |
gst_object_unref (device);
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
case GST_MESSAGE_DEVICE_REMOVED:
|
|
Packit |
971217 |
gst_message_parse_device_removed (msg, &device);
|
|
Packit |
971217 |
device_removed (device);
|
|
Packit |
971217 |
gst_object_unref (device);
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
default:
|
|
Packit |
971217 |
g_print ("%s message\n", GST_MESSAGE_TYPE_NAME (msg));
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return TRUE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
int
|
|
Packit |
971217 |
main (int argc, char **argv)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
gboolean print_version = FALSE;
|
|
Packit |
971217 |
GError *err = NULL;
|
|
Packit |
971217 |
gchar **arg, **args = NULL;
|
|
Packit |
971217 |
gboolean follow = FALSE;
|
|
Packit |
971217 |
GOptionContext *ctx;
|
|
Packit |
971217 |
GOptionEntry options[] = {
|
|
Packit |
971217 |
{"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
|
|
Packit |
971217 |
N_("Print version information and exit"), NULL},
|
|
Packit |
971217 |
{"follow", 'f', 0, G_OPTION_ARG_NONE, &follow,
|
|
Packit |
971217 |
N_("Don't exit after showing the initial device list, but wait "
|
|
Packit |
971217 |
"for devices to added/removed."), NULL},
|
|
Packit |
971217 |
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL},
|
|
Packit |
971217 |
{NULL}
|
|
Packit |
971217 |
};
|
|
Packit |
971217 |
GTimer *timer;
|
|
Packit |
971217 |
DevMonApp app;
|
|
Packit |
971217 |
GstBus *bus;
|
|
Packit |
971217 |
GList *devices;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
setlocale (LC_ALL, "");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#ifdef ENABLE_NLS
|
|
Packit |
971217 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
|
|
Packit |
971217 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
|
Packit |
971217 |
textdomain (GETTEXT_PACKAGE);
|
|
Packit |
971217 |
#endif
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_set_prgname ("gst-device-monitor-" GST_API_VERSION);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
ctx = g_option_context_new ("[DEVICE_CLASSES[:FILTER_CAPS]] "
|
|
Packit |
971217 |
"[DEVICE_CLASSES[:FILTER_CAPS]] …");
|
|
Packit |
971217 |
g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
|
|
Packit |
971217 |
g_option_context_add_group (ctx, gst_init_get_option_group ());
|
|
Packit |
971217 |
if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
|
|
Packit |
971217 |
g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
|
|
Packit |
971217 |
g_option_context_free (ctx);
|
|
Packit |
971217 |
g_clear_error (&err;;
|
|
Packit |
971217 |
return 1;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
g_option_context_free (ctx);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_DEBUG_CATEGORY_INIT (devmon_debug, "device-monitor", 0,
|
|
Packit |
971217 |
"gst-device-monitor");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (print_version) {
|
|
Packit |
971217 |
gchar *version_str;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
version_str = gst_version_string ();
|
|
Packit |
971217 |
g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
|
|
Packit |
971217 |
g_print ("%s\n", version_str);
|
|
Packit |
971217 |
g_print ("%s\n", GST_PACKAGE_ORIGIN);
|
|
Packit |
971217 |
g_free (version_str);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return 0;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
app.loop = g_main_loop_new (NULL, FALSE);
|
|
Packit |
971217 |
app.monitor = gst_device_monitor_new ();
|
|
Packit |
971217 |
|
|
Packit |
971217 |
bus = gst_device_monitor_get_bus (app.monitor);
|
|
Packit |
971217 |
app.bus_watch_id = gst_bus_add_watch (bus, bus_msg_handler, &app);
|
|
Packit |
971217 |
gst_object_unref (bus);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* process optional remaining arguments in the form
|
|
Packit |
971217 |
* DEVICE_CLASSES or DEVICE_CLASSES:FILTER_CAPS */
|
|
Packit |
971217 |
for (arg = args; arg != NULL && *arg != NULL; ++arg) {
|
|
Packit |
971217 |
gchar **filters = g_strsplit (*arg, ":", -1);
|
|
Packit |
971217 |
if (filters != NULL && filters[0] != NULL) {
|
|
Packit |
971217 |
GstCaps *caps = NULL;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (filters[1] != NULL) {
|
|
Packit |
971217 |
caps = gst_caps_from_string (filters[1]);
|
|
Packit |
971217 |
if (caps == NULL)
|
|
Packit |
971217 |
g_warning ("Couldn't parse device filter caps '%s'", filters[1]);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
gst_device_monitor_add_filter (app.monitor, filters[0], caps);
|
|
Packit |
971217 |
if (caps)
|
|
Packit |
971217 |
gst_caps_unref (caps);
|
|
Packit |
971217 |
g_strfreev (filters);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
g_strfreev (args);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_print ("Probing devices...\n\n");
|
|
Packit |
971217 |
|
|
Packit |
971217 |
timer = g_timer_new ();
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (!gst_device_monitor_start (app.monitor)) {
|
|
Packit |
971217 |
g_printerr ("Failed to start device monitor!\n");
|
|
Packit |
971217 |
return -1;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_INFO ("Took %.2f seconds", g_timer_elapsed (timer, NULL));
|
|
Packit |
971217 |
|
|
Packit |
971217 |
devices = gst_device_monitor_get_devices (app.monitor);
|
|
Packit |
971217 |
if (devices != NULL) {
|
|
Packit |
971217 |
while (devices != NULL) {
|
|
Packit |
971217 |
GstDevice *device = devices->data;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
device_added (device);
|
|
Packit |
971217 |
gst_object_unref (device);
|
|
Packit |
971217 |
devices = g_list_delete_link (devices, devices);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
} else {
|
|
Packit |
971217 |
g_print ("No devices found!\n");
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (follow) {
|
|
Packit |
971217 |
g_print ("Monitoring devices, waiting for devices to be removed or "
|
|
Packit |
971217 |
"new devices to be added...\n");
|
|
Packit |
971217 |
g_main_loop_run (app.loop);
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gst_device_monitor_stop (app.monitor);
|
|
Packit |
971217 |
gst_object_unref (app.monitor);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_source_remove (app.bus_watch_id);
|
|
Packit |
971217 |
g_main_loop_unref (app.loop);
|
|
Packit |
971217 |
g_timer_destroy (timer);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return 0;
|
|
Packit |
971217 |
}
|