2012-11-26 11:34:05 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2013-07-11 14:57:14 +00:00
|
|
|
/**
|
|
|
|
* SECTION:rtsp-mount-points
|
|
|
|
* @short_description: Map a path to media
|
|
|
|
* @see_also: #GstRTSPMediaFactory, #GstRTSPClient
|
|
|
|
*
|
|
|
|
* A #GstRTSPMountPoints object maintains a relation between paths
|
|
|
|
* and #GstRTSPMediaFactory objects. This object is usually given to
|
|
|
|
* #GstRTSPClient and used to find the media attached to a path.
|
|
|
|
*
|
|
|
|
* With gst_rtsp_mount_points_add_factory () and
|
|
|
|
* gst_rtsp_mount_points_remove_factory(), factories can be added and
|
|
|
|
* removed.
|
|
|
|
*
|
|
|
|
* With gst_rtsp_mount_points_match() you can find the #GstRTSPMediaFactory
|
|
|
|
* object that completely matches the given path.
|
|
|
|
*
|
|
|
|
* Last reviewed on 2013-07-11 (1.0.0)
|
|
|
|
*/
|
2018-09-24 08:36:21 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-11-26 11:34:05 +00:00
|
|
|
#include "rtsp-mount-points.h"
|
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
gint len;
|
|
|
|
GstRTSPMediaFactory *factory;
|
|
|
|
} DataItem;
|
|
|
|
|
|
|
|
static DataItem *
|
|
|
|
data_item_new (gchar * path, gint len, GstRTSPMediaFactory * factory)
|
|
|
|
{
|
|
|
|
DataItem *item;
|
|
|
|
|
|
|
|
item = g_slice_alloc (sizeof (DataItem));
|
|
|
|
item->path = path;
|
|
|
|
item->len = len;
|
|
|
|
item->factory = factory;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
data_item_free (gpointer data)
|
|
|
|
{
|
|
|
|
DataItem *item = data;
|
|
|
|
|
|
|
|
g_free (item->path);
|
|
|
|
g_object_unref (item->factory);
|
|
|
|
g_slice_free1 (sizeof (DataItem), item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-02-09 09:45:28 +00:00
|
|
|
data_item_dump (gconstpointer a, gconstpointer prefix)
|
2013-07-03 08:25:46 +00:00
|
|
|
{
|
|
|
|
const DataItem *item = a;
|
|
|
|
|
2013-07-11 18:45:11 +00:00
|
|
|
GST_DEBUG ("%s%s %p", (gchar *) prefix, item->path, item->factory);
|
2013-07-03 08:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
data_item_compare (gconstpointer a, gconstpointer b, gpointer user_data)
|
|
|
|
{
|
|
|
|
const DataItem *item1 = a, *item2 = b;
|
|
|
|
gint res;
|
|
|
|
|
|
|
|
res = g_strcmp0 (item1->path, item2->path);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
struct _GstRTSPMountPointsPrivate
|
|
|
|
{
|
|
|
|
GMutex lock;
|
2013-07-03 08:25:46 +00:00
|
|
|
GSequence *mounts; /* protected by lock */
|
|
|
|
gboolean dirty;
|
2012-11-29 10:11:05 +00:00
|
|
|
};
|
|
|
|
|
2018-06-24 10:44:26 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GstRTSPMountPoints, gst_rtsp_mount_points,
|
|
|
|
G_TYPE_OBJECT);
|
2012-11-26 11:34:05 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
|
|
|
|
#define GST_CAT_DEFAULT rtsp_media_debug
|
|
|
|
|
2013-09-16 14:47:40 +00:00
|
|
|
static gchar *default_make_path (GstRTSPMountPoints * mounts,
|
|
|
|
const GstRTSPUrl * url);
|
2012-11-26 11:34:05 +00:00
|
|
|
static void gst_rtsp_mount_points_finalize (GObject * obj);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_mount_points_class_init (GstRTSPMountPointsClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_rtsp_mount_points_finalize;
|
|
|
|
|
2013-09-16 14:47:40 +00:00
|
|
|
klass->make_path = default_make_path;
|
|
|
|
|
2012-11-26 11:34:05 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmountpoints", 0,
|
|
|
|
"GstRTSPMountPoints");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_mount_points_init (GstRTSPMountPoints * mounts)
|
|
|
|
{
|
2018-06-24 10:44:26 +00:00
|
|
|
GstRTSPMountPointsPrivate *priv;
|
2012-11-29 10:11:05 +00:00
|
|
|
|
2012-11-26 11:34:05 +00:00
|
|
|
GST_DEBUG_OBJECT (mounts, "created");
|
|
|
|
|
2018-06-24 10:44:26 +00:00
|
|
|
mounts->priv = priv = gst_rtsp_mount_points_get_instance_private (mounts);
|
2012-11-29 10:11:05 +00:00
|
|
|
|
|
|
|
g_mutex_init (&priv->lock);
|
2013-07-03 08:25:46 +00:00
|
|
|
priv->mounts = g_sequence_new (data_item_free);
|
|
|
|
priv->dirty = FALSE;
|
2012-11-26 11:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_mount_points_finalize (GObject * obj)
|
|
|
|
{
|
|
|
|
GstRTSPMountPoints *mounts = GST_RTSP_MOUNT_POINTS (obj);
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPMountPointsPrivate *priv = mounts->priv;
|
2012-11-26 11:34:05 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (mounts, "finalized");
|
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
g_sequence_free (priv->mounts);
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_clear (&priv->lock);
|
2012-11-26 11:34:05 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_rtsp_mount_points_parent_class)->finalize (obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_mount_points_new:
|
|
|
|
*
|
|
|
|
* Make a new mount points object.
|
|
|
|
*
|
2014-03-23 20:24:48 +00:00
|
|
|
* Returns: (transfer full): a new #GstRTSPMountPoints
|
2012-11-26 11:34:05 +00:00
|
|
|
*/
|
|
|
|
GstRTSPMountPoints *
|
|
|
|
gst_rtsp_mount_points_new (void)
|
|
|
|
{
|
|
|
|
GstRTSPMountPoints *result;
|
|
|
|
|
|
|
|
result = g_object_new (GST_TYPE_RTSP_MOUNT_POINTS, NULL);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-16 14:47:40 +00:00
|
|
|
static gchar *
|
|
|
|
default_make_path (GstRTSPMountPoints * mounts, const GstRTSPUrl * url)
|
|
|
|
{
|
|
|
|
return g_strdup (url->abspath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_mount_points_make_path:
|
|
|
|
* @mounts: a #GstRTSPMountPoints
|
|
|
|
* @url: a #GstRTSPUrl
|
|
|
|
*
|
|
|
|
* Make a path string from @url.
|
|
|
|
*
|
2018-02-09 23:07:25 +00:00
|
|
|
* Returns: (transfer full) (nullable): a path string for @url, g_free() after usage.
|
2013-09-16 14:47:40 +00:00
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
gst_rtsp_mount_points_make_path (GstRTSPMountPoints * mounts,
|
|
|
|
const GstRTSPUrl * url)
|
|
|
|
{
|
|
|
|
GstRTSPMountPointsClass *klass;
|
|
|
|
gchar *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts), NULL);
|
|
|
|
g_return_val_if_fail (url != NULL, NULL);
|
|
|
|
|
|
|
|
klass = GST_RTSP_MOUNT_POINTS_GET_CLASS (mounts);
|
|
|
|
|
|
|
|
if (klass->make_path)
|
|
|
|
result = klass->make_path (mounts, url);
|
|
|
|
else
|
|
|
|
result = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
static gboolean
|
|
|
|
has_prefix (DataItem * str, DataItem * prefix)
|
|
|
|
{
|
|
|
|
/* prefix needs to be smaller than str */
|
|
|
|
if (str->len < prefix->len)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* if str is larger, it there should be a / following the prefix */
|
|
|
|
if (str->len > prefix->len && str->path[prefix->len] != '/')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return strncmp (str->path, prefix->path, prefix->len) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_mount_points_match:
|
|
|
|
* @mounts: a #GstRTSPMountPoints
|
|
|
|
* @path: a mount point
|
2014-06-11 23:42:08 +00:00
|
|
|
* @matched: (out) (allow-none): the amount of @path matched
|
2013-07-03 08:25:46 +00:00
|
|
|
*
|
|
|
|
* Find the factory in @mounts that has the longest match with @path.
|
|
|
|
*
|
2013-11-22 01:28:28 +00:00
|
|
|
* If @matched is %NULL, @path will match the factory exactly otherwise
|
2013-07-03 08:25:46 +00:00
|
|
|
* the amount of characters that matched is returned in @matched.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): the #GstRTSPMediaFactory for @path.
|
2014-03-23 20:24:48 +00:00
|
|
|
* g_object_unref() after usage.
|
2013-07-03 08:25:46 +00:00
|
|
|
*/
|
|
|
|
GstRTSPMediaFactory *
|
|
|
|
gst_rtsp_mount_points_match (GstRTSPMountPoints * mounts,
|
|
|
|
const gchar * path, gint * matched)
|
|
|
|
{
|
|
|
|
GstRTSPMountPointsPrivate *priv;
|
|
|
|
GstRTSPMediaFactory *result = NULL;
|
|
|
|
GSequenceIter *iter, *best;
|
|
|
|
DataItem item, *ritem;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts), NULL);
|
|
|
|
g_return_val_if_fail (path != NULL, NULL);
|
|
|
|
|
|
|
|
priv = mounts->priv;
|
|
|
|
|
|
|
|
item.path = (gchar *) path;
|
|
|
|
item.len = strlen (path);
|
|
|
|
|
2016-09-16 20:41:19 +00:00
|
|
|
GST_LOG ("Looking for mount point path %s", path);
|
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
if (priv->dirty) {
|
|
|
|
g_sequence_sort (priv->mounts, data_item_compare, mounts);
|
2014-02-09 09:45:28 +00:00
|
|
|
g_sequence_foreach (priv->mounts, (GFunc) data_item_dump,
|
|
|
|
(gpointer) "sort :");
|
2013-07-03 08:25:46 +00:00
|
|
|
priv->dirty = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the location of the media in the hashtable we only use the absolute
|
|
|
|
* path of the uri to find a media factory. If the factory depends on other
|
|
|
|
* properties found in the url, this method should be overridden. */
|
|
|
|
iter = g_sequence_get_begin_iter (priv->mounts);
|
|
|
|
best = NULL;
|
|
|
|
while (!g_sequence_iter_is_end (iter)) {
|
|
|
|
ritem = g_sequence_get (iter);
|
|
|
|
|
|
|
|
data_item_dump (ritem, "inspect: ");
|
|
|
|
|
2016-09-16 20:41:19 +00:00
|
|
|
/* The sequence is sorted, so any prefix match is an improvement upon
|
|
|
|
* the previous best match, as '/abc' will always be before '/abcd' */
|
|
|
|
if (has_prefix (&item, ritem)) {
|
|
|
|
if (best == NULL) {
|
2013-07-03 08:25:46 +00:00
|
|
|
data_item_dump (ritem, "prefix: ");
|
2016-09-16 20:41:19 +00:00
|
|
|
} else {
|
|
|
|
data_item_dump (ritem, "new best: ");
|
2013-07-03 08:25:46 +00:00
|
|
|
}
|
|
|
|
best = iter;
|
2018-01-25 12:09:03 +00:00
|
|
|
} else {
|
|
|
|
/* if have a match and the current item doesn't prefix match the best we
|
|
|
|
* found so far then we're moving away and can bail out of the loop */
|
|
|
|
if (best != NULL && !has_prefix (ritem, g_sequence_get (best)))
|
|
|
|
break;
|
2013-07-03 08:25:46 +00:00
|
|
|
}
|
2016-09-16 20:41:19 +00:00
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
iter = g_sequence_iter_next (iter);
|
|
|
|
}
|
|
|
|
if (best) {
|
|
|
|
ritem = g_sequence_get (best);
|
|
|
|
data_item_dump (ritem, "result: ");
|
|
|
|
if (matched || ritem->len == item.len) {
|
|
|
|
result = g_object_ref (ritem->factory);
|
|
|
|
if (matched)
|
|
|
|
*matched = ritem->len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&priv->lock);
|
|
|
|
|
|
|
|
GST_INFO ("found media factory %p for path %s", result, path);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-11-26 11:34:05 +00:00
|
|
|
/**
|
|
|
|
* gst_rtsp_mount_points_add_factory:
|
|
|
|
* @mounts: a #GstRTSPMountPoints
|
|
|
|
* @path: a mount point
|
|
|
|
* @factory: (transfer full): a #GstRTSPMediaFactory
|
|
|
|
*
|
|
|
|
* Attach @factory to the mount point @path in @mounts.
|
|
|
|
*
|
|
|
|
* @path is of the form (/node)+. Any previous mount point will be freed.
|
|
|
|
*
|
|
|
|
* Ownership is taken of the reference on @factory so that @factory should not be
|
|
|
|
* used after calling this function.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_rtsp_mount_points_add_factory (GstRTSPMountPoints * mounts,
|
|
|
|
const gchar * path, GstRTSPMediaFactory * factory)
|
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPMountPointsPrivate *priv;
|
2013-07-03 08:25:46 +00:00
|
|
|
DataItem *item;
|
2012-11-29 10:11:05 +00:00
|
|
|
|
2012-11-26 11:34:05 +00:00
|
|
|
g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
|
|
|
|
g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
|
|
|
|
g_return_if_fail (path != NULL);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = mounts->priv;
|
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
item = data_item_new (g_strdup (path), strlen (path), factory);
|
|
|
|
|
2013-07-15 09:51:34 +00:00
|
|
|
GST_INFO ("adding media factory %p for path %s", factory, path);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_lock (&priv->lock);
|
2013-07-03 08:25:46 +00:00
|
|
|
g_sequence_append (priv->mounts, item);
|
|
|
|
priv->dirty = TRUE;
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
2012-11-26 11:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_mount_points_remove_factory:
|
|
|
|
* @mounts: a #GstRTSPMountPoints
|
|
|
|
* @path: a mount point
|
|
|
|
*
|
|
|
|
* Remove the #GstRTSPMediaFactory associated with @path in @mounts.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_rtsp_mount_points_remove_factory (GstRTSPMountPoints * mounts,
|
|
|
|
const gchar * path)
|
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPMountPointsPrivate *priv;
|
2013-07-03 08:25:46 +00:00
|
|
|
DataItem item;
|
|
|
|
GSequenceIter *iter;
|
2012-11-29 10:11:05 +00:00
|
|
|
|
2012-11-26 11:34:05 +00:00
|
|
|
g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
|
|
|
|
g_return_if_fail (path != NULL);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = mounts->priv;
|
|
|
|
|
2013-07-03 08:25:46 +00:00
|
|
|
item.path = (gchar *) path;
|
|
|
|
|
2013-07-15 09:51:34 +00:00
|
|
|
GST_INFO ("removing media factory for path %s", path);
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_lock (&priv->lock);
|
2013-12-21 00:39:07 +00:00
|
|
|
if (priv->dirty) {
|
|
|
|
g_sequence_sort (priv->mounts, data_item_compare, mounts);
|
|
|
|
priv->dirty = FALSE;
|
|
|
|
}
|
2013-07-03 08:25:46 +00:00
|
|
|
iter = g_sequence_lookup (priv->mounts, &item, data_item_compare, mounts);
|
|
|
|
if (iter) {
|
|
|
|
g_sequence_remove (iter);
|
|
|
|
priv->dirty = TRUE;
|
|
|
|
}
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
2012-11-26 11:34:05 +00:00
|
|
|
}
|