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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "rtsp-media-factory.h"
22 #define DEFAULT_LAUNCH NULL
23 #define DEFAULT_SHARED FALSE
33 static void gst_rtsp_media_factory_get_property (GObject *object, guint propid,
34 GValue *value, GParamSpec *pspec);
35 static void gst_rtsp_media_factory_set_property (GObject *object, guint propid,
36 const GValue *value, GParamSpec *pspec);
37 static void gst_rtsp_media_factory_finalize (GObject * obj);
39 static gchar * default_gen_key (GstRTSPMediaFactory *factory, const GstRTSPUrl *url);
40 static GstElement * default_get_element (GstRTSPMediaFactory *factory, const GstRTSPUrl *url);
41 static GstRTSPMedia * default_construct (GstRTSPMediaFactory *factory, const GstRTSPUrl *url);
42 static void default_configure (GstRTSPMediaFactory *factory, GstRTSPMedia *media);
44 G_DEFINE_TYPE (GstRTSPMediaFactory, gst_rtsp_media_factory, G_TYPE_OBJECT);
47 gst_rtsp_media_factory_class_init (GstRTSPMediaFactoryClass * klass)
49 GObjectClass *gobject_class;
51 gobject_class = G_OBJECT_CLASS (klass);
53 gobject_class->get_property = gst_rtsp_media_factory_get_property;
54 gobject_class->set_property = gst_rtsp_media_factory_set_property;
55 gobject_class->finalize = gst_rtsp_media_factory_finalize;
58 * GstRTSPMediaFactory::launch
60 * The gst_parse_launch() line to use for constructing the pipeline in the
61 * default prepare vmethod.
63 * The pipeline description should return a GstBin as the toplevel element
64 * which can be accomplished by enclosing the dscription with brackets '('
67 * The description should return a pipeline with payloaders named pay0, pay1,
68 * etc.. Each of the payloaders will result in a stream.
70 g_object_class_install_property (gobject_class, PROP_LAUNCH,
71 g_param_spec_string ("launch", "Launch", "A launch description of the pipeline",
72 DEFAULT_LAUNCH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
74 g_object_class_install_property (gobject_class, PROP_SHARED,
75 g_param_spec_boolean ("shared", "Shared", "If media from this factory is shared",
76 DEFAULT_SHARED, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
78 klass->gen_key = default_gen_key;
79 klass->get_element = default_get_element;
80 klass->construct = default_construct;
81 klass->configure = default_configure;
85 gst_rtsp_media_factory_init (GstRTSPMediaFactory * factory)
87 factory->launch = g_strdup (DEFAULT_LAUNCH);
88 factory->shared = DEFAULT_SHARED;
90 factory->lock = g_mutex_new ();
91 factory->medias_lock = g_mutex_new ();
92 factory->medias = g_hash_table_new_full (g_str_hash, g_str_equal,
93 g_free, g_object_unref);
97 gst_rtsp_media_factory_finalize (GObject * obj)
99 GstRTSPMediaFactory *factory = GST_RTSP_MEDIA_FACTORY (obj);
101 g_hash_table_unref (factory->medias);
102 g_mutex_free (factory->medias_lock);
103 g_free (factory->launch);
104 g_mutex_free (factory->lock);
106 G_OBJECT_CLASS (gst_rtsp_media_factory_parent_class)->finalize (obj);
110 gst_rtsp_media_factory_get_property (GObject *object, guint propid,
111 GValue *value, GParamSpec *pspec)
113 GstRTSPMediaFactory *factory = GST_RTSP_MEDIA_FACTORY (object);
117 g_value_take_string (value, gst_rtsp_media_factory_get_launch (factory));
120 g_value_set_boolean (value, gst_rtsp_media_factory_is_shared (factory));
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
128 gst_rtsp_media_factory_set_property (GObject *object, guint propid,
129 const GValue *value, GParamSpec *pspec)
131 GstRTSPMediaFactory *factory = GST_RTSP_MEDIA_FACTORY (object);
135 gst_rtsp_media_factory_set_launch (factory, g_value_get_string (value));
138 gst_rtsp_media_factory_set_shared (factory, g_value_get_boolean (value));
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
146 * gst_rtsp_media_factory_new:
148 * Create a new #GstRTSPMediaFactory instance.
150 * Returns: a new #GstRTSPMediaFactory object.
152 GstRTSPMediaFactory *
153 gst_rtsp_media_factory_new (void)
155 GstRTSPMediaFactory *result;
157 result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY, NULL);
163 * gst_rtsp_media_factory_set_launch:
164 * @factory: a #GstRTSPMediaFactory
165 * @launch: the launch description
168 * The gst_parse_launch() line to use for constructing the pipeline in the
169 * default prepare vmethod.
171 * The pipeline description should return a GstBin as the toplevel element
172 * which can be accomplished by enclosing the dscription with brackets '('
175 * The description should return a pipeline with payloaders named pay0, pay1,
176 * etc.. Each of the payloaders will result in a stream.
179 gst_rtsp_media_factory_set_launch (GstRTSPMediaFactory *factory, const gchar *launch)
181 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
182 g_return_if_fail (launch != NULL);
184 g_mutex_lock (factory->lock);
185 g_free (factory->launch);
186 factory->launch = g_strdup (launch);
187 g_mutex_unlock (factory->lock);
191 * gst_rtsp_media_factory_get_launch:
192 * @factory: a #GstRTSPMediaFactory
194 * Get the gst_parse_launch() pipeline description that will be used in the
195 * default prepare vmethod.
197 * Returns: the configured launch description. g_free() after usage.
200 gst_rtsp_media_factory_get_launch (GstRTSPMediaFactory *factory)
204 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory), NULL);
206 g_mutex_lock (factory->lock);
207 result = g_strdup (factory->launch);
208 g_mutex_unlock (factory->lock);
214 * gst_rtsp_media_factory_set_shared:
215 * @factory: a #GstRTSPMediaFactory
216 * @shared: the new value
218 * Configure if media created from this factory can be shared between clients.
221 gst_rtsp_media_factory_set_shared (GstRTSPMediaFactory *factory,
224 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory));
226 g_mutex_lock (factory->lock);
227 factory->shared = shared;
228 g_mutex_unlock (factory->lock);
232 * gst_rtsp_media_factory_is_shared:
233 * @factory: a #GstRTSPMediaFactory
235 * Get if media created from this factory can be shared between clients.
237 * Returns: %TRUE if the media will be shared between clients.
240 gst_rtsp_media_factory_is_shared (GstRTSPMediaFactory *factory)
244 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY (factory), FALSE);
246 g_mutex_lock (factory->lock);
247 result = factory->shared;
248 g_mutex_unlock (factory->lock);
254 compare_media (gpointer key, GstRTSPMedia *media1, GstRTSPMedia *media2)
256 return (media1 == media2);
260 media_unprepared (GstRTSPMedia *media, GstRTSPMediaFactory *factory)
262 g_mutex_lock (factory->medias_lock);
263 g_hash_table_foreach_remove (factory->medias, (GHRFunc) compare_media,
265 g_mutex_unlock (factory->medias_lock);
269 * gst_rtsp_media_factory_construct:
270 * @factory: a #GstRTSPMediaFactory
273 * Prepare the media object and create its streams. Implementations
274 * should create the needed gstreamer elements and add them to the result
275 * object. No state changes should be performed on them yet.
277 * One or more GstRTSPMediaStream objects should be added to the result with
278 * the srcpad member set to a source pad that produces buffer of type
281 * Returns: a new #GstRTSPMedia if the media could be prepared.
284 gst_rtsp_media_factory_construct (GstRTSPMediaFactory *factory, const GstRTSPUrl *url)
288 GstRTSPMediaFactoryClass *klass;
290 klass = GST_RTSP_MEDIA_FACTORY_GET_CLASS (factory);
292 /* convert the url to a key for the hashtable. NULL return or a NULL function
293 * will not cache anything for this factory. */
295 key = klass->gen_key (factory, url);
299 g_mutex_lock (factory->medias_lock);
301 /* we have a key, see if we find a cached media */
302 media = g_hash_table_lookup (factory->medias, key);
304 g_object_ref (media);
310 /* nothing cached found, try to create one */
311 if (klass->construct)
312 media = klass->construct (factory, url);
317 /* configure the media */
318 if (klass->configure)
319 klass->configure (factory, media);
321 /* check if we can cache this media */
322 if (gst_rtsp_media_is_shared (media)) {
323 /* insert in the hashtable, takes ownership of the key */
324 g_object_ref (media);
325 g_hash_table_insert (factory->medias, key, media);
328 if (!gst_rtsp_media_is_reusable (media)) {
329 /* when not reusable, connect to the unprepare signal to remove the item
330 * from our cache when it gets unprepared */
331 g_signal_connect (media, "unprepared", (GCallback) media_unprepared,
336 g_mutex_unlock (factory->medias_lock);
341 g_message ("constructed media %p for url %s", media, url->abspath);
347 default_gen_key (GstRTSPMediaFactory *factory, const GstRTSPUrl *url)
351 result = gst_rtsp_url_get_request_uri ((GstRTSPUrl *)url);
357 default_get_element (GstRTSPMediaFactory *factory, const GstRTSPUrl *url)
360 GError *error = NULL;
362 g_mutex_lock (factory->lock);
363 /* we need a parse syntax */
364 if (factory->launch == NULL)
367 /* parse the user provided launch line */
368 element = gst_parse_launch (factory->launch, &error);
372 g_mutex_unlock (factory->lock);
375 /* a recoverable error was encountered */
376 g_warning ("recoverable parsing error: %s", error->message);
377 g_error_free (error);
384 g_mutex_unlock (factory->lock);
385 g_critical ("no launch line specified");
390 g_mutex_unlock (factory->lock);
391 g_critical ("could not parse launch syntax (%s): %s", factory->launch,
392 (error ? error->message : "unknown reason"));
394 g_error_free (error);
400 static GstRTSPMedia *
401 default_construct (GstRTSPMediaFactory *factory, const GstRTSPUrl *url)
404 GstRTSPMediaStream *stream;
405 GstElement *pay, *element;
408 GstRTSPMediaFactoryClass *klass;
410 klass = GST_RTSP_MEDIA_FACTORY_GET_CLASS (factory);
412 if (klass->get_element)
413 element = klass->get_element (factory, url);
419 /* create a new empty media */
420 media = gst_rtsp_media_new ();
421 media->element = element;
423 /* try to find all the payloader elements, they should be named 'pay%d'. for
424 * each of the payloaders we will create a stream and collect the source pad.
429 name = g_strdup_printf ("pay%d", i);
431 if (!(pay = gst_bin_get_by_name (GST_BIN (element), name))) {
432 /* no more payloaders found, we have found all the streams and we can
438 /* create the stream */
439 stream = g_new0 (GstRTSPMediaStream, 1);
440 stream->payloader = pay;
442 pad = gst_element_get_static_pad (pay, "src");
444 /* ghost the pad of the payloader to the element */
445 stream->srcpad = gst_ghost_pad_new (name, pad);
446 gst_element_add_pad (media->element, stream->srcpad);
447 gst_object_unref (pay);
451 g_array_append_val (media->streams, stream);
459 g_critical ("could not create element");
465 default_configure (GstRTSPMediaFactory *factory, GstRTSPMedia *media)
469 /* configure the sharedness */
470 g_mutex_lock (factory->lock);
471 shared = factory->shared;
472 g_mutex_unlock (factory->lock);
474 gst_rtsp_media_set_shared (media, shared);