Commit e4e85fc8 authored by Olivier Crête's avatar Olivier Crête 👻 Committed by Tim-Philipp Müller

devicemonitor: Add GstDeviceMonitor and related

Also add GstDevice and GstDeviceMonitorFactory
And add code to the registry to save them

https://bugzilla.gnome.org/show_bug.cgi?id=678402
parent 10d53423
......@@ -71,6 +71,9 @@ libgstreamer_@GST_API_VERSION@_la_SOURCES = \
gstcontrolsource.c \
gstdatetime.c \
gstdebugutils.c \
gstdevice.c \
gstdevicemonitor.c \
gstdevicemonitorfactory.c \
gstelement.c \
gstelementfactory.c \
gsterror.c \
......@@ -172,6 +175,9 @@ gst_headers = \
gstdebugutils.h \
gstelement.h \
gstelementmetadata.h \
gstdevice.h \
gstdevicemonitor.h \
gstdevicemonitorfactory.h \
gstelementfactory.h \
gsterror.h \
gstevent.h \
......
......@@ -42,6 +42,8 @@
#include <gst/gstcontrolsource.h>
#include <gst/gstdatetime.h>
#include <gst/gstdebugutils.h>
#include <gst/gstdevice.h>
#include <gst/gstdevicemonitor.h>
#include <gst/gstelement.h>
#include <gst/gstelementmetadata.h>
#include <gst/gsterror.h>
......
......@@ -54,6 +54,9 @@ extern const char g_log_domain_gstreamer[];
/* for GstElement */
#include "gstelement.h"
/* for GstDeviceMonitor */
#include "gstdevicemonitor.h"
/* for GstToc */
#include "gsttoc.h"
......@@ -393,5 +396,24 @@ struct _GstElementFactoryClass {
gpointer _gst_reserved[GST_PADDING];
};
struct _GstDeviceMonitorFactory {
GstPluginFeature feature;
/* <private> */
GType type; /* unique GType the device factory or 0 if not loaded */
volatile GstDeviceMonitor *monitor;
gpointer metadata;
gpointer _gst_reserved[GST_PADDING];
};
struct _GstDeviceMonitorFactoryClass {
GstPluginFeatureClass parent;
/* <private> */
gpointer _gst_reserved[GST_PADDING];
};
G_END_DECLS
#endif /* __GST_PRIVATE_H__ */
/* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gst/gst_private.h"
#include <gst/gstdevice.h>
#include <gst/gst.h>
enum
{
PROP_DISPLAY_NAME = 1,
PROP_CAPS
};
enum
{
REMOVED,
LAST_SIGNAL
};
struct _GstDevicePrivate
{
GstCaps *caps;
gchar *display_name;
};
static guint signals[LAST_SIGNAL];
G_DEFINE_ABSTRACT_TYPE (GstDevice, gst_device, GST_TYPE_OBJECT);
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);
g_type_class_add_private (klass, sizeof (GstDevicePrivate));
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));
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)
{
device->priv = G_TYPE_INSTANCE_GET_PRIVATE (device, GST_TYPE_DEVICE,
GstDevicePrivate);
}
static void
gst_device_finalize (GObject * object)
{
GstDevice *device = GST_DEVICE (object);
gst_caps_replace (&device->priv->caps, NULL);
g_free (device->priv->display_name);
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;
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;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* gst_device_create_element:
* @device: a #GstDevice
* @name: (allow-none): name of new element, or NULL to automatically
* create a unique name.
*
* Returns: (transfer full): a new #GstElement configured to use this device
*
* Since: 1.4
*/
GstElement *
gst_device_create_element (GstDevice * device, const gchar * name)
{
GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
if (klass->create_element)
return klass->create_element (device, name);
else
return NULL;
}
/**
* gst_device_get_caps:
* @device: a #GstDevice
*
* Getter for the #GstCaps that this device supports.
*
* Returns: The #GstCaps supported by this device. Unref with
* gst_caps_unref() when done.
*
* Since: 1.4
*/
GstCaps *
gst_device_get_caps (GstDevice * device)
{
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)
{
return g_strdup (device->priv->display_name);
}
/**
* 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);
if (klass->reconfigure_element)
return klass->reconfigure_element (device, element);
else
return FALSE;
}
/* 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.
*/
#ifndef __GST_DEVICE_H__
#define __GST_DEVICE_H__
typedef struct _GstDevice GstDevice;
typedef struct _GstDeviceClass GstDeviceClass;
#include <gst/gstelement.h>
#include <gst/gstcaps.h>
G_BEGIN_DECLS
typedef struct _GstDevicePrivate GstDevicePrivate;
#define GST_TYPE_DEVICE (gst_device_get_type())
#define GST_IS_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_DEVICE))
#define GST_IS_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_DEVICE))
#define GST_DEVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_DEVICE, GstDeviceClass))
#define GST_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_DEVICE, GstDevice))
#define GST_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_DEVICE, GstDeviceClass))
#define GST_DEVICE_CAST(obj) ((GstDevice *)(obj))
struct _GstDevice {
GstObject parent;
/*< private >*/
GstDevicePrivate *priv;
gpointer _gst_reserved[GST_PADDING];
};
struct _GstDeviceClass {
GstObjectClass parent_class;
GstElement * (*create_element) (GstDevice * device, const gchar * name);
gboolean (*reconfigure_element) (GstDevice * device, GstElement * element);
/*< private >*/
gpointer _gst_reserved[GST_PADDING];
};
GType gst_device_get_type (void);
GstElement * gst_device_create_element (GstDevice * device, const gchar * name);
GstCaps * gst_device_get_caps (GstDevice * device);
gchar * gst_device_get_display_name (GstDevice * device);
gboolean gst_device_reconfigure_element (GstDevice * device,
GstElement * element);
G_END_DECLS
#endif /* __GST_DEVICE_H__ */
/* GStreamer
* Copyright (C) 2012 Olivier Crete <olivier.crete@collabora.com>
*
* gstdevicemonitor.c: Device probing and monitoring
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gstdevicemonitor.h>
#include "gst/gst_private.h"
#include <gst/gst.h>
#include <gst/gstelementmetadata.h>
#include <gst/gstquark.h>
struct _GstDeviceMonitorPrivate
{
GstBus *bus;
GMutex start_lock;
gboolean started_count;
};
/* this is used in gstelementfactory.c:gst_element_register() */
GQuark __gst_devicemonitorclass_factory = 0;
static void gst_device_monitor_class_init (GstDeviceMonitorClass * klass);
static void gst_device_monitor_init (GstDeviceMonitor * element);
static void gst_device_monitor_base_class_init (gpointer g_class);
static void gst_device_monitor_base_class_finalize (gpointer g_class);
static void gst_device_monitor_dispose (GObject * object);
static void gst_device_monitor_finalize (GObject * object);
static gpointer gst_device_monitor_parent_class = NULL;
GType
gst_device_monitor_get_type (void)
{
static volatile gsize gst_device_monitor_type = 0;
if (g_once_init_enter (&gst_device_monitor_type)) {
GType _type;
static const GTypeInfo element_info = {
sizeof (GstDeviceMonitorClass),
gst_device_monitor_base_class_init,
gst_device_monitor_base_class_finalize,
(GClassInitFunc) gst_device_monitor_class_init,
NULL,
NULL,
sizeof (GstDeviceMonitor),
0,
(GInstanceInitFunc) gst_device_monitor_init,
NULL
};
_type = g_type_register_static (GST_TYPE_OBJECT, "GstDeviceMonitor",
&element_info, G_TYPE_FLAG_ABSTRACT);
__gst_devicemonitorclass_factory =
g_quark_from_static_string ("GST_DEVICEMONITORCLASS_FACTORY");
g_once_init_leave (&gst_device_monitor_type, _type);
}
return gst_device_monitor_type;
}
static void
gst_device_monitor_base_class_init (gpointer g_class)
{
GstDeviceMonitorClass *klass = GST_DEVICE_MONITOR_CLASS (g_class);
/* Copy the element details here so elements can inherit the
* details from their base class and classes only need to set
* the details in class_init instead of base_init */
klass->metadata =
klass->metadata ? gst_structure_copy (klass->metadata) :
gst_structure_new_empty ("metadata");
klass->factory = g_type_get_qdata (G_TYPE_FROM_CLASS (klass),
__gst_devicemonitorclass_factory);
}
static void
gst_device_monitor_base_class_finalize (gpointer g_class)
{
GstDeviceMonitorClass *klass = GST_DEVICE_MONITOR_CLASS (g_class);
gst_structure_free (klass->metadata);
}
static void
gst_device_monitor_class_init (GstDeviceMonitorClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
gst_device_monitor_parent_class = g_type_class_peek_parent (klass);
g_type_class_add_private (klass, sizeof (GstDeviceMonitorPrivate));
gobject_class->dispose = gst_device_monitor_dispose;
gobject_class->finalize = gst_device_monitor_finalize;
}
static void
gst_device_monitor_init (GstDeviceMonitor * monitor)
{
monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
GST_TYPE_DEVICE_MONITOR, GstDeviceMonitorPrivate);
g_mutex_init (&monitor->priv->start_lock);
monitor->priv->bus = gst_bus_new ();
gst_bus_set_flushing (monitor->priv->bus, TRUE);
}
static void
gst_device_monitor_dispose (GObject * object)
{
GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
gst_object_replace ((GstObject **) & monitor->priv->bus, NULL);
GST_OBJECT_LOCK (monitor);
g_list_free_full (monitor->devices, (GDestroyNotify) gst_object_unparent);
monitor->devices = NULL;
GST_OBJECT_UNLOCK (monitor);
G_OBJECT_CLASS (gst_device_monitor_parent_class)->dispose (object);
}
static void
gst_device_monitor_finalize (GObject * object)
{
GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
g_mutex_clear (&monitor->priv->start_lock);
G_OBJECT_CLASS (gst_device_monitor_parent_class)->finalize (object);
}
/**
* gst_device_monitor_class_add_metadata:
* @klass: class to set metadata for
* @key: the key to set
* @value: the value to set
*
* Set @key with @value as metadata in @klass.
*/
void
gst_device_monitor_class_add_metadata (GstDeviceMonitorClass * klass,
const gchar * key, const gchar * value)
{
g_return_if_fail (GST_IS_DEVICE_MONITOR_CLASS (klass));
g_return_if_fail (key != NULL);
g_return_if_fail (value != NULL);
gst_structure_set ((GstStructure *) klass->metadata,
key, G_TYPE_STRING, value, NULL);
}
/**
* gst_device_monitor_class_add_static_metadata:
* @klass: class to set metadata for
* @key: the key to set
* @value: the value to set
*
* Set @key with @value as metadata in @klass.
*
* Same as gst_device_monitor_class_add_metadata(), but @value must be a static string
* or an inlined string, as it will not be copied. (GStreamer plugins will
* be made resident once loaded, so this function can be used even from
* dynamically loaded plugins.)
*
* Since: 1.4
*/
void
gst_device_monitor_class_add_static_metadata (GstDeviceMonitorClass * klass,
const gchar * key, const gchar * value)
{
GValue val = G_VALUE_INIT;
g_return_if_fail (GST_IS_DEVICE_MONITOR_CLASS (klass));
g_return_if_fail (key != NULL);
g_return_if_fail (value != NULL);
g_value_init (&val, G_TYPE_STRING);
g_value_set_static_string (&val, value);
gst_structure_take_value ((GstStructure *) klass->metadata, key, &val);
}
/**
* gst_device_monitor_class_set_metadata:
* @klass: class to set metadata for
* @longname: The long English name of the device monitor. E.g. "File Sink"
* @classification: String describing the type of device monitor, as an unordered list
* separated with slashes ('/'). See draft-klass.txt of the design docs
* for more details and common types. E.g: "Sink/File"
* @description: Sentence describing the purpose of the device monitor.
* E.g: "Write stream to a file"
* @author: Name and contact details of the author(s). Use \n to separate
* multiple author metadata. E.g: "Joe Bloggs &lt;joe.blogs at foo.com&gt;"
*
* Sets the detailed information for a #GstDeviceMonitorClass.
* <note>This function is for use in _class_init functions only.</note>
*
* Since: 1.4
*/
void
gst_device_monitor_class_set_metadata (GstDeviceMonitorClass * klass,
const gchar * longname, const gchar * classification,
const gchar * description, const gchar * author)
{
g_return_if_fail (GST_IS_DEVICE_MONITOR_CLASS (klass));
g_return_if_fail (longname != NULL && *longname != '\0');
g_return_if_fail (classification != NULL && *classification != '\0');
g_return_if_fail (description != NULL && *description != '\0');
g_return_if_fail (author != NULL && *author != '\0');
gst_structure_id_set ((GstStructure *) klass->metadata,
GST_QUARK (ELEMENT_METADATA_LONGNAME), G_TYPE_STRING, longname,
GST_QUARK (ELEMENT_METADATA_KLASS), G_TYPE_STRING, classification,
GST_QUARK (ELEMENT_METADATA_DESCRIPTION), G_TYPE_STRING, description,
GST_QUARK (ELEMENT_METADATA_AUTHOR), G_TYPE_STRING, author, NULL);
}
/**
* gst_device_monitor_class_set_static_metadata:
* @klass: class to set metadata for
* @longname: The long English name of the element. E.g. "File Sink"
* @classification: String describing the type of element, as an unordered list
* separated with slashes ('/'). See draft-klass.txt of the design docs
* for more details and common types. E.g: "Sink/File"
* @description: Sentence describing the purpose of the element.
* E.g: "Write stream to a file"
* @author: Name and contact details of the author(s). Use \n to separate
* multiple author metadata. E.g: "Joe Bloggs &lt;joe.blogs at foo.com&gt;"
*
* Sets the detailed information for a #GstDeviceMonitorClass.
* <note>This function is for use in _class_init functions only.</note>
*
* Same as gst_device_monitor_class_set_metadata(), but @longname, @classification,
* @description, and @author must be static strings or inlined strings, as