2012-10-16 16:27:04 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2012 Olivier Crete <olivier.crete@collabora.com>
|
|
|
|
*
|
|
|
|
* gstdevice.c: Device discovery
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2014-06-20 20:55:06 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstdevice
|
2017-01-16 14:26:16 +00:00
|
|
|
* @title: GstDevice
|
2014-06-20 20:55:06 +00:00
|
|
|
* @short_description: Object representing a device
|
2014-06-26 18:28:09 +00:00
|
|
|
* @see_also: #GstDeviceProvider
|
2014-06-20 20:55:06 +00:00
|
|
|
*
|
|
|
|
* #GstDevice are objects representing a device, they contain
|
|
|
|
* relevant metadata about the device, such as its class and the #GstCaps
|
|
|
|
* representing the media types it can produce or handle.
|
|
|
|
*
|
2014-06-26 18:28:09 +00:00
|
|
|
* #GstDevice are created by #GstDeviceProvider objects which can be
|
2014-07-05 16:29:29 +00:00
|
|
|
* aggregated by #GstDeviceMonitor objects.
|
2014-06-20 20:55:06 +00:00
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
|
2012-10-16 16:27:04 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-17 08:51:45 +00:00
|
|
|
#include "gst_private.h"
|
2012-10-16 16:27:04 +00:00
|
|
|
|
2014-03-17 08:51:45 +00:00
|
|
|
#include "gstdevice.h"
|
2012-10-16 16:27:04 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_DISPLAY_NAME = 1,
|
2014-03-16 19:56:59 +00:00
|
|
|
PROP_CAPS,
|
2015-07-15 16:21:13 +00:00
|
|
|
PROP_DEVICE_CLASS,
|
|
|
|
PROP_PROPERTIES
|
2012-10-16 16:27:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
REMOVED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GstDevicePrivate
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
2014-06-01 15:56:41 +00:00
|
|
|
gchar *device_class;
|
2012-10-16 16:27:04 +00:00
|
|
|
gchar *display_name;
|
2015-07-15 16:21:13 +00:00
|
|
|
GstStructure *properties;
|
2012-10-16 16:27:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL];
|
|
|
|
|
2018-06-23 15:01:09 +00:00
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstDevice, gst_device, GST_TYPE_OBJECT);
|
2012-10-16 16:27:04 +00:00
|
|
|
|
|
|
|
static void gst_device_get_property (GObject * object, guint property_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_device_set_property (GObject * object, guint property_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_device_finalize (GObject * object);
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_device_class_init (GstDeviceClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->get_property = gst_device_get_property;
|
|
|
|
object_class->set_property = gst_device_set_property;
|
|
|
|
object_class->finalize = gst_device_finalize;
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
|
|
|
|
g_param_spec_string ("display-name", "Display Name",
|
|
|
|
"The user-friendly name of the device", "",
|
|
|
|
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
g_object_class_install_property (object_class, PROP_CAPS,
|
|
|
|
g_param_spec_boxed ("caps", "Device Caps",
|
|
|
|
"The possible caps of a device", GST_TYPE_CAPS,
|
|
|
|
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2014-06-01 15:56:41 +00:00
|
|
|
g_object_class_install_property (object_class, PROP_DEVICE_CLASS,
|
|
|
|
g_param_spec_string ("device-class", "Device Class",
|
2014-03-16 19:56:59 +00:00
|
|
|
"The Class of the device", "",
|
|
|
|
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2015-07-15 16:21:13 +00:00
|
|
|
g_object_class_install_property (object_class, PROP_PROPERTIES,
|
|
|
|
g_param_spec_boxed ("properties", "Properties",
|
|
|
|
"The extra properties of the device", GST_TYPE_STRUCTURE,
|
|
|
|
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2012-10-16 16:27:04 +00:00
|
|
|
|
|
|
|
signals[REMOVED] = g_signal_new ("removed", G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_device_init (GstDevice * device)
|
|
|
|
{
|
2018-06-23 15:01:09 +00:00
|
|
|
device->priv = gst_device_get_instance_private (device);
|
2012-10-16 16:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_device_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstDevice *device = GST_DEVICE (object);
|
|
|
|
|
|
|
|
gst_caps_replace (&device->priv->caps, NULL);
|
|
|
|
|
2015-07-15 16:21:13 +00:00
|
|
|
if (device->priv->properties)
|
|
|
|
gst_structure_free (device->priv->properties);
|
2012-10-16 16:27:04 +00:00
|
|
|
g_free (device->priv->display_name);
|
2014-06-01 15:56:41 +00:00
|
|
|
g_free (device->priv->device_class);
|
2012-10-16 16:27:04 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_device_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_device_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstDevice *gstdevice;
|
|
|
|
|
|
|
|
gstdevice = GST_DEVICE_CAST (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_DISPLAY_NAME:
|
|
|
|
g_value_take_string (value, gst_device_get_display_name (gstdevice));
|
|
|
|
break;
|
|
|
|
case PROP_CAPS:
|
|
|
|
if (gstdevice->priv->caps)
|
|
|
|
g_value_take_boxed (value, gst_device_get_caps (gstdevice));
|
|
|
|
break;
|
2014-06-01 15:56:41 +00:00
|
|
|
case PROP_DEVICE_CLASS:
|
|
|
|
g_value_take_string (value, gst_device_get_device_class (gstdevice));
|
2014-03-16 19:56:59 +00:00
|
|
|
break;
|
2015-07-15 16:21:13 +00:00
|
|
|
case PROP_PROPERTIES:
|
|
|
|
if (gstdevice->priv->properties)
|
|
|
|
g_value_take_boxed (value, gst_device_get_properties (gstdevice));
|
|
|
|
break;
|
2012-10-16 16:27:04 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_device_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstDevice *gstdevice;
|
|
|
|
|
|
|
|
gstdevice = GST_DEVICE_CAST (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_DISPLAY_NAME:
|
|
|
|
gstdevice->priv->display_name = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
case PROP_CAPS:
|
|
|
|
gst_caps_replace (&gstdevice->priv->caps, g_value_get_boxed (value));
|
|
|
|
break;
|
2014-06-01 15:56:41 +00:00
|
|
|
case PROP_DEVICE_CLASS:
|
|
|
|
gstdevice->priv->device_class = g_value_dup_string (value);
|
2014-03-16 19:56:59 +00:00
|
|
|
break;
|
2015-07-15 16:21:13 +00:00
|
|
|
case PROP_PROPERTIES:
|
|
|
|
if (gstdevice->priv->properties)
|
|
|
|
gst_structure_free (gstdevice->priv->properties);
|
|
|
|
gstdevice->priv->properties = g_value_dup_boxed (value);
|
|
|
|
break;
|
2012-10-16 16:27:04 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_device_create_element:
|
|
|
|
* @device: a #GstDevice
|
2014-05-29 21:54:34 +00:00
|
|
|
* @name: (allow-none): name of new element, or %NULL to automatically
|
2012-10-16 16:27:04 +00:00
|
|
|
* create a unique name.
|
|
|
|
*
|
2016-06-15 14:12:23 +00:00
|
|
|
* Creates the element with all of the required parameters set to use
|
2014-06-20 20:55:06 +00:00
|
|
|
* this device.
|
|
|
|
*
|
2019-09-12 07:03:08 +00:00
|
|
|
* Returns: (transfer floating) (nullable): a new #GstElement configured to use
|
2017-10-22 12:35:30 +00:00
|
|
|
* this device
|
2012-10-16 16:27:04 +00:00
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
GstElement *
|
|
|
|
gst_device_create_element (GstDevice * device, const gchar * name)
|
|
|
|
{
|
|
|
|
GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
|
2019-09-12 07:08:39 +00:00
|
|
|
GstElement *element = NULL;
|
2012-10-16 16:27:04 +00:00
|
|
|
|
2014-06-26 20:31:51 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
|
|
|
|
|
2012-10-16 16:27:04 +00:00
|
|
|
if (klass->create_element)
|
2019-09-12 07:08:39 +00:00
|
|
|
element = klass->create_element (device, name);
|
|
|
|
|
2019-12-10 12:31:50 +00:00
|
|
|
if (element && !g_object_is_floating ((GObject *) element)) {
|
2019-12-04 19:12:02 +00:00
|
|
|
/* The reference we receive here should be floating, but we can't force
|
2019-12-10 12:31:50 +00:00
|
|
|
* it at our level. Simply raise a critical to make the issue obvious to bindings
|
2019-12-04 19:12:02 +00:00
|
|
|
* developers */
|
2019-12-10 12:31:50 +00:00
|
|
|
g_critical ("The created element should be floating, "
|
|
|
|
"this is probably caused by faulty bindings");
|
2019-12-04 19:12:02 +00:00
|
|
|
}
|
2019-09-12 07:08:39 +00:00
|
|
|
|
|
|
|
return element;
|
2012-10-16 16:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_device_get_caps:
|
|
|
|
* @device: a #GstDevice
|
|
|
|
*
|
|
|
|
* Getter for the #GstCaps that this device supports.
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Returns: (nullable): The #GstCaps supported by this device. Unref with
|
2012-10-16 16:27:04 +00:00
|
|
|
* gst_caps_unref() when done.
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
GstCaps *
|
|
|
|
gst_device_get_caps (GstDevice * device)
|
|
|
|
{
|
2014-06-26 20:31:51 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
|
|
|
|
|
2012-10-16 16:27:04 +00:00
|
|
|
if (device->priv->caps)
|
|
|
|
return gst_caps_ref (device->priv->caps);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_device_get_display_name:
|
|
|
|
* @device: a #GstDevice
|
|
|
|
*
|
|
|
|
* Gets the user-friendly name of the device.
|
|
|
|
*
|
|
|
|
* Returns: The device name. Free with g_free() after use.
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
gst_device_get_display_name (GstDevice * device)
|
|
|
|
{
|
2014-06-26 20:31:51 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
|
|
|
|
|
2014-03-16 19:56:59 +00:00
|
|
|
return
|
|
|
|
g_strdup (device->priv->display_name ? device->priv->display_name : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-01 15:56:41 +00:00
|
|
|
* gst_device_get_device_class:
|
2014-03-16 19:56:59 +00:00
|
|
|
* @device: a #GstDevice
|
|
|
|
*
|
|
|
|
* Gets the "class" of a device. This is a "/" separated list of
|
|
|
|
* classes that represent this device. They are a subset of the
|
2014-06-26 18:28:09 +00:00
|
|
|
* classes of the #GstDeviceProvider that produced this device.
|
2014-03-16 19:56:59 +00:00
|
|
|
*
|
|
|
|
* Returns: The device class. Free with g_free() after use.
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
gchar *
|
2014-06-01 15:56:41 +00:00
|
|
|
gst_device_get_device_class (GstDevice * device)
|
2014-03-16 19:56:59 +00:00
|
|
|
{
|
2014-06-26 20:31:51 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
|
|
|
|
|
2014-06-01 15:56:41 +00:00
|
|
|
if (device->priv->device_class != NULL)
|
|
|
|
return g_strdup (device->priv->device_class);
|
|
|
|
else
|
|
|
|
return g_strdup ("");
|
2012-10-16 16:27:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 16:21:13 +00:00
|
|
|
/**
|
|
|
|
* gst_device_get_properties:
|
|
|
|
* @device: a #GstDevice
|
|
|
|
*
|
|
|
|
* Gets the extra properties of a device.
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Returns: (nullable): The extra properties or %NULL when there are none.
|
2015-07-15 16:21:13 +00:00
|
|
|
* Free with gst_structure_free() after use.
|
|
|
|
*
|
|
|
|
* Since: 1.6
|
|
|
|
*/
|
|
|
|
GstStructure *
|
|
|
|
gst_device_get_properties (GstDevice * device)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
|
|
|
|
|
|
|
|
if (device->priv->properties != NULL)
|
|
|
|
return gst_structure_copy (device->priv->properties);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-16 16:27:04 +00:00
|
|
|
/**
|
|
|
|
* gst_device_reconfigure_element:
|
|
|
|
* @device: a #GstDevice
|
|
|
|
* @element: a #GstElement
|
|
|
|
*
|
|
|
|
* Tries to reconfigure an existing element to use the device. If this
|
|
|
|
* function fails, then one must destroy the element and create a new one
|
|
|
|
* using gst_device_create_element().
|
|
|
|
*
|
|
|
|
* Note: This should only be implemented for elements can change their
|
|
|
|
* device in the PLAYING state.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the element could be reconfigured to use this device,
|
|
|
|
* %FALSE otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_device_reconfigure_element (GstDevice * device, GstElement * element)
|
|
|
|
{
|
|
|
|
GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
|
|
|
|
|
2014-06-26 20:31:51 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
|
|
|
|
|
2012-10-16 16:27:04 +00:00
|
|
|
if (klass->reconfigure_element)
|
|
|
|
return klass->reconfigure_element (device, element);
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-03-16 19:56:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_device_has_classesv:
|
|
|
|
* @device: a #GstDevice
|
2016-01-21 15:45:30 +00:00
|
|
|
* @classes: (array zero-terminated=1): a %NULL terminated array of classes
|
|
|
|
* to match, only match if all classes are matched
|
2014-03-16 19:56:59 +00:00
|
|
|
*
|
|
|
|
* Check if @factory matches all of the given classes
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @device matches.
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_device_has_classesv (GstDevice * device, gchar ** classes)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
|
|
|
|
|
2014-06-26 20:31:51 +00:00
|
|
|
if (!classes)
|
|
|
|
return TRUE;
|
2014-03-16 19:56:59 +00:00
|
|
|
|
|
|
|
for (; classes[0]; classes++) {
|
2016-01-21 15:45:30 +00:00
|
|
|
const gchar *klass = classes[0];
|
2014-03-16 19:56:59 +00:00
|
|
|
const gchar *found;
|
|
|
|
guint len;
|
|
|
|
|
2016-01-21 15:45:30 +00:00
|
|
|
if (*klass == '\0')
|
2014-03-16 19:56:59 +00:00
|
|
|
continue;
|
|
|
|
|
2016-01-21 15:45:30 +00:00
|
|
|
found = strstr (device->priv->device_class, klass);
|
2014-03-16 19:56:59 +00:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return FALSE;
|
2014-06-01 15:56:41 +00:00
|
|
|
if (found != device->priv->device_class && *(found - 1) != '/')
|
2014-03-16 19:56:59 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2016-01-21 15:45:30 +00:00
|
|
|
len = strlen (klass);
|
2014-03-16 19:56:59 +00:00
|
|
|
if (found[len] != 0 && found[len] != '/')
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_device_has_classes:
|
|
|
|
* @device: a #GstDevice
|
2016-01-21 15:45:30 +00:00
|
|
|
* @classes: a "/"-separated list of device classes to match, only match if
|
2014-06-01 15:56:41 +00:00
|
|
|
* all classes are matched
|
2014-03-16 19:56:59 +00:00
|
|
|
*
|
|
|
|
* Check if @device matches all of the given classes
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @device matches.
|
|
|
|
*
|
|
|
|
* Since: 1.4
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_device_has_classes (GstDevice * device, const gchar * classes)
|
|
|
|
{
|
|
|
|
gchar **classesv;
|
|
|
|
gboolean res;
|
|
|
|
|
2014-06-26 20:31:51 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
|
|
|
|
|
|
|
|
if (!classes)
|
|
|
|
return TRUE;
|
|
|
|
|
2014-03-16 19:56:59 +00:00
|
|
|
classesv = g_strsplit (classes, "/", 0);
|
|
|
|
|
|
|
|
res = gst_device_has_classesv (device, classesv);
|
|
|
|
|
|
|
|
g_strfreev (classesv);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|