media: disconnect from signal handlers in unprepare()
[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 #define GST_RTSP_MOUNT_POINTS_GET_PRIVATE(obj)  \
23        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MOUNT_POINTS, GstRTSPMountPointsPrivate))
24
25 struct _GstRTSPMountPointsPrivate
26 {
27   GMutex lock;
28   GHashTable *mounts;           /* protected by lock */
29 };
30
31 G_DEFINE_TYPE (GstRTSPMountPoints, gst_rtsp_mount_points, G_TYPE_OBJECT);
32
33 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
34 #define GST_CAT_DEFAULT rtsp_media_debug
35
36 static void gst_rtsp_mount_points_finalize (GObject * obj);
37
38 static GstRTSPMediaFactory *find_factory (GstRTSPMountPoints * mounts,
39     const GstRTSPUrl * url);
40
41 static void
42 gst_rtsp_mount_points_class_init (GstRTSPMountPointsClass * klass)
43 {
44   GObjectClass *gobject_class;
45
46   g_type_class_add_private (klass, sizeof (GstRTSPMountPointsPrivate));
47
48   gobject_class = G_OBJECT_CLASS (klass);
49
50   gobject_class->finalize = gst_rtsp_mount_points_finalize;
51
52   klass->find_factory = find_factory;
53
54   GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmountpoints", 0,
55       "GstRTSPMountPoints");
56 }
57
58 static void
59 gst_rtsp_mount_points_init (GstRTSPMountPoints * mounts)
60 {
61   GstRTSPMountPointsPrivate *priv = GST_RTSP_MOUNT_POINTS_GET_PRIVATE (mounts);
62
63   GST_DEBUG_OBJECT (mounts, "created");
64
65   mounts->priv = priv;
66
67   g_mutex_init (&priv->lock);
68   priv->mounts = g_hash_table_new_full (g_str_hash, g_str_equal,
69       g_free, g_object_unref);
70 }
71
72 static void
73 gst_rtsp_mount_points_finalize (GObject * obj)
74 {
75   GstRTSPMountPoints *mounts = GST_RTSP_MOUNT_POINTS (obj);
76   GstRTSPMountPointsPrivate *priv = mounts->priv;
77
78   GST_DEBUG_OBJECT (mounts, "finalized");
79
80   g_hash_table_unref (priv->mounts);
81   g_mutex_clear (&priv->lock);
82
83   G_OBJECT_CLASS (gst_rtsp_mount_points_parent_class)->finalize (obj);
84 }
85
86 /**
87  * gst_rtsp_mount_points_new:
88  *
89  * Make a new mount points object.
90  *
91  * Returns: a new #GstRTSPMountPoints
92  */
93 GstRTSPMountPoints *
94 gst_rtsp_mount_points_new (void)
95 {
96   GstRTSPMountPoints *result;
97
98   result = g_object_new (GST_TYPE_RTSP_MOUNT_POINTS, NULL);
99
100   return result;
101 }
102
103 static GstRTSPMediaFactory *
104 find_factory (GstRTSPMountPoints * mounts, const GstRTSPUrl * url)
105 {
106   GstRTSPMountPointsPrivate *priv = mounts->priv;
107   GstRTSPMediaFactory *result;
108
109   g_mutex_lock (&priv->lock);
110   /* find the location of the media in the hashtable we only use the absolute
111    * path of the uri to find a media factory. If the factory depends on other
112    * properties found in the url, this method should be overridden. */
113   result = g_hash_table_lookup (priv->mounts, url->abspath);
114   if (result)
115     g_object_ref (result);
116   g_mutex_unlock (&priv->lock);
117
118   GST_INFO ("found media factory %p for url abspath %s", result, url->abspath);
119
120   return result;
121 }
122
123 /**
124  * gst_rtsp_mount_points_find_factory:
125  * @mounts: a #GstRTSPMountPoints
126  * @url: a url
127  *
128  * Find the #GstRTSPMediaFactory for @url. The default implementation of this object
129  * will use the media factory added with gst_rtsp_mount_points_add_factory ().
130  *
131  * Returns: (transfer full): the #GstRTSPMediaFactory for @url. g_object_unref() after usage.
132  */
133 GstRTSPMediaFactory *
134 gst_rtsp_mount_points_find_factory (GstRTSPMountPoints * mounts,
135     const GstRTSPUrl * url)
136 {
137   GstRTSPMediaFactory *result;
138   GstRTSPMountPointsClass *klass;
139
140   g_return_val_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts), NULL);
141   g_return_val_if_fail (url != NULL, NULL);
142
143   klass = GST_RTSP_MOUNT_POINTS_GET_CLASS (mounts);
144
145   if (klass->find_factory)
146     result = klass->find_factory (mounts, url);
147   else
148     result = NULL;
149
150   return result;
151 }
152
153 /**
154  * gst_rtsp_mount_points_add_factory:
155  * @mounts: a #GstRTSPMountPoints
156  * @path: a mount point
157  * @factory: (transfer full): a #GstRTSPMediaFactory
158  *
159  * Attach @factory to the mount point @path in @mounts.
160  *
161  * @path is of the form (/node)+. Any previous mount point will be freed.
162  *
163  * Ownership is taken of the reference on @factory so that @factory should not be
164  * used after calling this function.
165  */
166 void
167 gst_rtsp_mount_points_add_factory (GstRTSPMountPoints * mounts,
168     const gchar * path, GstRTSPMediaFactory * factory)
169 {
170   GstRTSPMountPointsPrivate *priv;
171
172   g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
173   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
174   g_return_if_fail (path != NULL);
175
176   priv = mounts->priv;
177
178   g_mutex_lock (&priv->lock);
179   g_hash_table_insert (priv->mounts, g_strdup (path), factory);
180   g_mutex_unlock (&priv->lock);
181 }
182
183 /**
184  * gst_rtsp_mount_points_remove_factory:
185  * @mounts: a #GstRTSPMountPoints
186  * @path: a mount point
187  *
188  * Remove the #GstRTSPMediaFactory associated with @path in @mounts.
189  */
190 void
191 gst_rtsp_mount_points_remove_factory (GstRTSPMountPoints * mounts,
192     const gchar * path)
193 {
194   GstRTSPMountPointsPrivate *priv;
195
196   g_return_if_fail (GST_IS_RTSP_MOUNT_POINTS (mounts));
197   g_return_if_fail (path != NULL);
198
199   priv = mounts->priv;
200
201   g_mutex_lock (&priv->lock);
202   g_hash_table_remove (priv->mounts, path);
203   g_mutex_unlock (&priv->lock);
204 }