2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.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.
22 #include "rtsp-mount-points.h"
24 #define GST_RTSP_MOUNT_POINTS_GET_PRIVATE(obj) \
25 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MOUNT_POINTS, GstRTSPMountPointsPrivate))
31 GstRTSPMediaFactory *factory;
35 data_item_new (gchar * path, gint len, GstRTSPMediaFactory * factory)
39 item = g_slice_alloc (sizeof (DataItem));
42 item->factory = factory;
48 data_item_free (gpointer data)
50 DataItem *item = data;
53 g_object_unref (item->factory);
54 g_slice_free1 (sizeof (DataItem), item);
58 data_item_dump (gconstpointer a, gpointer prefix)
60 const DataItem *item = a;
62 GST_DEBUG ("%s%s %p\n", (gchar *) prefix, item->path, item->factory);
66 data_item_compare (gconstpointer a, gconstpointer b, gpointer user_data)
68 const DataItem *item1 = a, *item2 = b;
71 res = g_strcmp0 (item1->path, item2->path);
76 struct _GstRTSPMountPointsPrivate
79 GSequence *mounts; /* protected by lock */
83 G_DEFINE_TYPE (GstRTSPMountPoints, gst_rtsp_mount_points, G_TYPE_OBJECT);
85 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
86 #define GST_CAT_DEFAULT rtsp_media_debug
88 static void gst_rtsp_mount_points_finalize (GObject * obj);
91 gst_rtsp_mount_points_class_init (GstRTSPMountPointsClass * klass)
93 GObjectClass *gobject_class;
95 g_type_class_add_private (klass, sizeof (GstRTSPMountPointsPrivate));
97 gobject_class = G_OBJECT_CLASS (klass);
99 gobject_class->finalize = gst_rtsp_mount_points_finalize;
101 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmountpoints", 0,
102 "GstRTSPMountPoints");
106 gst_rtsp_mount_points_init (GstRTSPMountPoints * mounts)
108 GstRTSPMountPointsPrivate *priv = GST_RTSP_MOUNT_POINTS_GET_PRIVATE (mounts);
110 GST_DEBUG_OBJECT (mounts, "created");
114 g_mutex_init (&priv->lock);
115 priv->mounts = g_sequence_new (data_item_free);
120 gst_rtsp_mount_points_finalize (GObject * obj)
122 GstRTSPMountPoints *mounts = GST_RTSP_MOUNT_POINTS (obj);
123 GstRTSPMountPointsPrivate *priv = mounts->priv;
125 GST_DEBUG_OBJECT (mounts, "finalized");
127 g_sequence_free (priv->mounts);
128 g_mutex_clear (&priv->lock);
130 G_OBJECT_CLASS (gst_rtsp_mount_points_parent_class)->finalize (obj);
134 * gst_rtsp_mount_points_new:
136 * Make a new mount points object.
138 * Returns: a new #GstRTSPMountPoints
141 gst_rtsp_mount_points_new (void)
143 GstRTSPMountPoints *result;
145 result = g_object_new (GST_TYPE_RTSP_MOUNT_POINTS, NULL);
151 has_prefix (DataItem * str, DataItem * prefix)
153 /* prefix needs to be smaller than str */
154 if (str->len < prefix->len)
157 /* if str is larger, it there should be a / following the prefix */
158 if (str->len > prefix->len && str->path[prefix->len] != '/')
161 return strncmp (str->path, prefix->path, prefix->len) == 0;
165 * gst_rtsp_mount_points_match:
166 * @mounts: a #GstRTSPMountPoints
167 * @path: a mount point
168 * @matched: the amount of @path matched
170 * Find the factory in @mounts that has the longest match with @path.
172 * If @matched is NULL, @path willt match the factory exactly otherwise
173 * the amount of characters that matched is returned in @matched.
175 * Returns: (transfer full): the #GstRTSPMediaFactory for @path.
176 * g_object_unref() after usage.
178 GstRTSPMediaFactory *
179 gst_rtsp_mount_points_match (GstRTSPMountPoints * mounts,
180 const gchar * path, gint * matched)
182 GstRTSPMountPointsPrivate *priv;
183 GstRTSPMediaFactory *result = NULL;
184 GSequenceIter *iter, *best;
185 DataItem item, *ritem;
187 g_return_val_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts), NULL);
188 g_return_val_if_fail (path != NULL, NULL);
192 item.path = (gchar *) path;
193 item.len = strlen (path);
195 g_mutex_lock (&priv->lock);
197 g_sequence_sort (priv->mounts, data_item_compare, mounts);
198 g_sequence_foreach (priv->mounts, (GFunc) data_item_dump, "sort :");
202 /* find the location of the media in the hashtable we only use the absolute
203 * path of the uri to find a media factory. If the factory depends on other
204 * properties found in the url, this method should be overridden. */
205 iter = g_sequence_get_begin_iter (priv->mounts);
207 while (!g_sequence_iter_is_end (iter)) {
208 ritem = g_sequence_get (iter);
210 data_item_dump (ritem, "inspect: ");
213 if (has_prefix (&item, ritem)) {
214 data_item_dump (ritem, "prefix: ");
218 if (!has_prefix (&item, ritem))
222 data_item_dump (ritem, "new best: ");
224 iter = g_sequence_iter_next (iter);
227 ritem = g_sequence_get (best);
228 data_item_dump (ritem, "result: ");
229 if (matched || ritem->len == item.len) {
230 result = g_object_ref (ritem->factory);
232 *matched = ritem->len;
235 g_mutex_unlock (&priv->lock);
237 GST_INFO ("found media factory %p for path %s", result, path);
243 * gst_rtsp_mount_points_add_factory:
244 * @mounts: a #GstRTSPMountPoints
245 * @path: a mount point
246 * @factory: (transfer full): a #GstRTSPMediaFactory
248 * Attach @factory to the mount point @path in @mounts.
250 * @path is of the form (/node)+. Any previous mount point will be freed.
252 * Ownership is taken of the reference on @factory so that @factory should not be
253 * used after calling this function.
256 gst_rtsp_mount_points_add_factory (GstRTSPMountPoints * mounts,
257 const gchar * path, GstRTSPMediaFactory * factory)
259 GstRTSPMountPointsPrivate *priv;
262 g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
263 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
264 g_return_if_fail (path != NULL);
268 item = data_item_new (g_strdup (path), strlen (path), factory);
270 g_mutex_lock (&priv->lock);
271 g_sequence_append (priv->mounts, item);
273 g_mutex_unlock (&priv->lock);
277 * gst_rtsp_mount_points_remove_factory:
278 * @mounts: a #GstRTSPMountPoints
279 * @path: a mount point
281 * Remove the #GstRTSPMediaFactory associated with @path in @mounts.
284 gst_rtsp_mount_points_remove_factory (GstRTSPMountPoints * mounts,
287 GstRTSPMountPointsPrivate *priv;
291 g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
292 g_return_if_fail (path != NULL);
296 item.path = (gchar *) path;
298 g_mutex_lock (&priv->lock);
299 iter = g_sequence_lookup (priv->mounts, &item, data_item_compare, mounts);
301 g_sequence_remove (iter);
304 g_mutex_unlock (&priv->lock);