factory: plug pad leak in collect_streams
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-media-mapping.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "rtsp-media-mapping.h"
21
22 G_DEFINE_TYPE (GstRTSPMediaMapping, gst_rtsp_media_mapping, 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_media_mapping_finalize (GObject * obj);
28
29 static GstRTSPMediaFactory *find_media (GstRTSPMediaMapping * mapping,
30     const GstRTSPUrl * url);
31
32 static void
33 gst_rtsp_media_mapping_class_init (GstRTSPMediaMappingClass * klass)
34 {
35   GObjectClass *gobject_class;
36
37   gobject_class = G_OBJECT_CLASS (klass);
38
39   gobject_class->finalize = gst_rtsp_media_mapping_finalize;
40
41   klass->find_media = find_media;
42
43   GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmediamapping", 0,
44       "GstRTSPMediaMapping");
45 }
46
47 static void
48 gst_rtsp_media_mapping_init (GstRTSPMediaMapping * mapping)
49 {
50   GST_DEBUG_OBJECT (mapping, "created");
51
52   mapping->mappings = g_hash_table_new_full (g_str_hash, g_str_equal,
53       g_free, g_object_unref);
54 }
55
56 static void
57 gst_rtsp_media_mapping_finalize (GObject * obj)
58 {
59   GstRTSPMediaMapping *mapping = GST_RTSP_MEDIA_MAPPING (obj);
60
61   GST_DEBUG_OBJECT (mapping, "finalized");
62
63   g_hash_table_unref (mapping->mappings);
64
65   G_OBJECT_CLASS (gst_rtsp_media_mapping_parent_class)->finalize (obj);
66 }
67
68 GstRTSPMediaMapping *
69 gst_rtsp_media_mapping_new (void)
70 {
71   GstRTSPMediaMapping *result;
72
73   result = g_object_new (GST_TYPE_RTSP_MEDIA_MAPPING, NULL);
74
75   return result;
76 }
77
78 static GstRTSPMediaFactory *
79 find_media (GstRTSPMediaMapping * mapping, const GstRTSPUrl * url)
80 {
81   GstRTSPMediaFactory *result;
82
83   /* find the location of the media in the hashtable we only use the absolute
84    * path of the uri to find a mapping. If the mapping depends on other
85    * properties found in the url, this method should be overridden. */
86   result = g_hash_table_lookup (mapping->mappings, url->abspath);
87   if (result)
88     g_object_ref (result);
89
90   GST_INFO ("found media %p for url abspath %s", result, url->abspath);
91
92   return result;
93 }
94
95 /**
96  * gst_rtsp_media_mapping_find_factory:
97  * @mapping: a #GstRTSPMediaMapping
98  * @url: a url
99  *
100  * Find the #GstRTSPMediaFactory for @url. The default implementation of this object 
101  * will use the mappings added with gst_rtsp_media_mapping_add_factory ().
102  *
103  * Returns: the #GstRTSPMediaFactory for @url. g_object_unref() after usage.
104  */
105 GstRTSPMediaFactory *
106 gst_rtsp_media_mapping_find_factory (GstRTSPMediaMapping * mapping,
107     const GstRTSPUrl * url)
108 {
109   GstRTSPMediaFactory *result;
110   GstRTSPMediaMappingClass *klass;
111
112   klass = GST_RTSP_MEDIA_MAPPING_GET_CLASS (mapping);
113
114   if (klass->find_media)
115     result = klass->find_media (mapping, url);
116   else
117     result = NULL;
118
119   return result;
120 }
121
122 /**
123  * gst_rtsp_media_mapping_add_factory:
124  * @mapping: a #GstRTSPMediaMapping
125  * @path: a mount point
126  * @factory: a #GstRTSPMediaFactory
127  *
128  * Attach @factory to the mount point @path in @mapping.
129  *
130  * @path is of the form (/node)+. Any previous mapping will be freed.
131  *
132  * Ownership is taken of the reference on @factory so that @factory should not be
133  * used after calling this function.
134  */
135 void
136 gst_rtsp_media_mapping_add_factory (GstRTSPMediaMapping * mapping,
137     const gchar * path, GstRTSPMediaFactory * factory)
138 {
139   g_return_if_fail (GST_IS_RTSP_MEDIA_MAPPING (mapping));
140   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
141   g_return_if_fail (path != NULL);
142
143   g_hash_table_insert (mapping->mappings, g_strdup (path), factory);
144 }
145
146 /**
147  * gst_rtsp_media_mapping_remove_factory:
148  * @mapping: a #GstRTSPMediaMapping
149  * @path: a mount point
150  *
151  * Remove the #GstRTSPMediaFactory associated with @path in @mapping.
152  */
153 void
154 gst_rtsp_media_mapping_remove_factory (GstRTSPMediaMapping * mapping,
155     const gchar * path)
156 {
157   g_return_if_fail (GST_IS_RTSP_MEDIA_MAPPING (mapping));
158   g_return_if_fail (path != NULL);
159
160   g_hash_table_remove (mapping->mappings, path);
161 }