client: add locking
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-mount-points.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "rtsp-mount-points.h"
21
22 G_DEFINE_TYPE (GstRTSPMountPoints, gst_rtsp_mount_points, G_TYPE_OBJECT);
23
24 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
25 #define GST_CAT_DEFAULT rtsp_media_debug
26
27 static void gst_rtsp_mount_points_finalize (GObject * obj);
28
29 static GstRTSPMediaFactory *find_factory (GstRTSPMountPoints * mounts,
30     const GstRTSPUrl * url);
31
32 static void
33 gst_rtsp_mount_points_class_init (GstRTSPMountPointsClass * klass)
34 {
35   GObjectClass *gobject_class;
36
37   gobject_class = G_OBJECT_CLASS (klass);
38
39   gobject_class->finalize = gst_rtsp_mount_points_finalize;
40
41   klass->find_factory = find_factory;
42
43   GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmountpoints", 0,
44       "GstRTSPMountPoints");
45 }
46
47 static void
48 gst_rtsp_mount_points_init (GstRTSPMountPoints * mounts)
49 {
50   GST_DEBUG_OBJECT (mounts, "created");
51
52   g_mutex_init (&mounts->lock);
53   mounts->mounts = g_hash_table_new_full (g_str_hash, g_str_equal,
54       g_free, g_object_unref);
55 }
56
57 static void
58 gst_rtsp_mount_points_finalize (GObject * obj)
59 {
60   GstRTSPMountPoints *mounts = GST_RTSP_MOUNT_POINTS (obj);
61
62   GST_DEBUG_OBJECT (mounts, "finalized");
63
64   g_hash_table_unref (mounts->mounts);
65   g_mutex_clear (&mounts->lock);
66
67   G_OBJECT_CLASS (gst_rtsp_mount_points_parent_class)->finalize (obj);
68 }
69
70 /**
71  * gst_rtsp_mount_points_new:
72  *
73  * Make a new mount points object.
74  *
75  * Returns: a new #GstRTSPMountPoints
76  */
77 GstRTSPMountPoints *
78 gst_rtsp_mount_points_new (void)
79 {
80   GstRTSPMountPoints *result;
81
82   result = g_object_new (GST_TYPE_RTSP_MOUNT_POINTS, NULL);
83
84   return result;
85 }
86
87 static GstRTSPMediaFactory *
88 find_factory (GstRTSPMountPoints * mounts, const GstRTSPUrl * url)
89 {
90   GstRTSPMediaFactory *result;
91
92   g_mutex_lock (&mounts->lock);
93   /* find the location of the media in the hashtable we only use the absolute
94    * path of the uri to find a media factory. If the factory depends on other
95    * properties found in the url, this method should be overridden. */
96   result = g_hash_table_lookup (mounts->mounts, url->abspath);
97   if (result)
98     g_object_ref (result);
99   g_mutex_unlock (&mounts->lock);
100
101   GST_INFO ("found media factory %p for url abspath %s", result, url->abspath);
102
103   return result;
104 }
105
106 /**
107  * gst_rtsp_mount_points_find_factory:
108  * @mounts: a #GstRTSPMountPoints
109  * @url: a url
110  *
111  * Find the #GstRTSPMediaFactory for @url. The default implementation of this object
112  * will use the media factory added with gst_rtsp_mount_points_add_factory ().
113  *
114  * Returns: (transfer full): the #GstRTSPMediaFactory for @url. g_object_unref() after usage.
115  */
116 GstRTSPMediaFactory *
117 gst_rtsp_mount_points_find_factory (GstRTSPMountPoints * mounts,
118     const GstRTSPUrl * url)
119 {
120   GstRTSPMediaFactory *result;
121   GstRTSPMountPointsClass *klass;
122
123   klass = GST_RTSP_MOUNT_POINTS_GET_CLASS (mounts);
124
125   if (klass->find_factory)
126     result = klass->find_factory (mounts, url);
127   else
128     result = NULL;
129
130   return result;
131 }
132
133 /**
134  * gst_rtsp_mount_points_add_factory:
135  * @mounts: a #GstRTSPMountPoints
136  * @path: a mount point
137  * @factory: (transfer full): a #GstRTSPMediaFactory
138  *
139  * Attach @factory to the mount point @path in @mounts.
140  *
141  * @path is of the form (/node)+. Any previous mount point will be freed.
142  *
143  * Ownership is taken of the reference on @factory so that @factory should not be
144  * used after calling this function.
145  */
146 void
147 gst_rtsp_mount_points_add_factory (GstRTSPMountPoints * mounts,
148     const gchar * path, GstRTSPMediaFactory * factory)
149 {
150   g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
151   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
152   g_return_if_fail (path != NULL);
153
154   g_mutex_lock (&mounts->lock);
155   g_hash_table_insert (mounts->mounts, g_strdup (path), factory);
156   g_mutex_unlock (&mounts->lock);
157 }
158
159 /**
160  * gst_rtsp_mount_points_remove_factory:
161  * @mounts: a #GstRTSPMountPoints
162  * @path: a mount point
163  *
164  * Remove the #GstRTSPMediaFactory associated with @path in @mounts.
165  */
166 void
167 gst_rtsp_mount_points_remove_factory (GstRTSPMountPoints * mounts,
168     const gchar * path)
169 {
170   g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
171   g_return_if_fail (path != NULL);
172
173   g_mutex_lock (&mounts->lock);
174   g_hash_table_remove (mounts->mounts, path);
175   g_mutex_unlock (&mounts->lock);
176 }