Make the server handle arbitrary pipelines
[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 static void gst_rtsp_media_mapping_finalize (GObject * obj);
25
26 static GstRTSPMediaFactory * find_media (GstRTSPMediaMapping *mapping, const GstRTSPUrl *url);
27
28 static void
29 gst_rtsp_media_mapping_class_init (GstRTSPMediaMappingClass * klass)
30 {
31   GObjectClass *gobject_class;
32
33   gobject_class = G_OBJECT_CLASS (klass);
34
35   gobject_class->finalize = gst_rtsp_media_mapping_finalize;
36
37   klass->find_media = find_media;
38 }
39
40 static void
41 gst_rtsp_media_mapping_init (GstRTSPMediaMapping * mapping)
42 {
43   mapping->mappings = g_hash_table_new_full (g_str_hash, g_str_equal,
44                         g_free, g_object_unref);
45 }
46
47 static void
48 gst_rtsp_media_mapping_finalize (GObject * obj)
49 {
50   GstRTSPMediaMapping *mapping = GST_RTSP_MEDIA_MAPPING (obj);
51
52   g_hash_table_unref (mapping->mappings);
53
54   G_OBJECT_CLASS (gst_rtsp_media_mapping_parent_class)->finalize (obj);
55 }
56
57 GstRTSPMediaMapping *
58 gst_rtsp_media_mapping_new (void)
59 {
60   GstRTSPMediaMapping *result;
61
62   result = g_object_new (GST_TYPE_RTSP_MEDIA_MAPPING, NULL);
63
64   return result;
65 }
66
67 static GstRTSPMediaFactory *
68 find_media (GstRTSPMediaMapping *mapping, const GstRTSPUrl *url)
69 {
70   GstRTSPMediaFactory *result;
71
72   /* find the location of the media in the hashtable */
73   result = g_hash_table_lookup (mapping->mappings, url->abspath);
74   if (result) 
75     g_object_ref (result);
76
77   g_message ("found media %p for url abspath %s", result, url->abspath);
78
79   return result;
80 }
81
82 /**
83  * gst_rtsp_media_mapping_find_factory:
84  * @mapping: a #GstRTSPMediaMapping
85  * @url: a url
86  *
87  * Find the #GstRTSPMediaFactory for @url from the mappings registered in @mapping.
88  *
89  * Returns: the #GstRTSPMediaFactory for @url. g_object_unref() after usage.
90  */
91 GstRTSPMediaFactory *
92 gst_rtsp_media_mapping_find_factory (GstRTSPMediaMapping *mapping, const GstRTSPUrl *url)
93 {
94   GstRTSPMediaFactory *result;
95   GstRTSPMediaMappingClass *klass;
96
97   klass = GST_RTSP_MEDIA_MAPPING_GET_CLASS (mapping);
98
99   if (klass->find_media)
100     result = klass->find_media (mapping, url);
101   else
102     result = NULL;
103
104   return result;
105 }
106
107 /**
108  * gst_rtsp_media_mapping_add_factory:
109  * @mapping: a #GstRTSPMediaMapping
110  * @path: a mount point
111  * @factory: a #GstRTSPMediaFactory
112  *
113  * Attach @factory to the mount point @path in @mapping.
114  *
115  * @path is of the form (/node)+. Any previous mapping will be freed.
116  *
117  * Ownership is taken of the reference on @factory so that @factory should not be
118  * used after calling this function.
119  */
120 void
121 gst_rtsp_media_mapping_add_factory (GstRTSPMediaMapping *mapping, const gchar *path,
122     GstRTSPMediaFactory *factory)
123 {
124   g_return_if_fail (GST_IS_RTSP_MEDIA_MAPPING (mapping));
125   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
126   g_return_if_fail (path != NULL);
127
128   g_hash_table_insert (mapping->mappings, g_strdup (path), factory);
129 }
130
131 /**
132  * gst_rtsp_media_mapping_remove_factory:
133  * @mapping: a #GstRTSPMediaMapping
134  * @path: a mount point
135  *
136  * Remove the #GstRTSPMediaFactory associated with @path in @mapping.
137  */
138 void
139 gst_rtsp_media_mapping_remove_factory (GstRTSPMediaMapping *mapping, const gchar *path)
140 {
141   g_return_if_fail (GST_IS_RTSP_MEDIA_MAPPING (mapping));
142   g_return_if_fail (path != NULL);
143
144   g_hash_table_remove (mapping->mappings, path);
145 }
146