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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "rtsp-media-factory-uri.h"
24 #define GST_RTSP_MEDIA_FACTORY_URI_GET_PRIVATE(obj) \
25 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA_FACTORY_URI, GstRTSPMediaFactoryURIPrivate))
27 struct _GstRTSPMediaFactoryURIPrivate
40 #define DEFAULT_URI NULL
41 #define DEFAULT_USE_GSTPAY FALSE
52 #define RAW_VIDEO_CAPS \
55 #define RAW_AUDIO_CAPS \
58 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS (RAW_VIDEO_CAPS);
59 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS (RAW_AUDIO_CAPS);
63 GstRTSPMediaFactoryURI *factory;
68 free_data (FactoryData * data)
70 g_object_unref (data->factory);
74 static const gchar *factory_key = "GstRTSPMediaFactoryURI";
76 GST_DEBUG_CATEGORY_STATIC (rtsp_media_factory_uri_debug);
77 #define GST_CAT_DEFAULT rtsp_media_factory_uri_debug
79 static void gst_rtsp_media_factory_uri_get_property (GObject * object,
80 guint propid, GValue * value, GParamSpec * pspec);
81 static void gst_rtsp_media_factory_uri_set_property (GObject * object,
82 guint propid, const GValue * value, GParamSpec * pspec);
83 static void gst_rtsp_media_factory_uri_finalize (GObject * obj);
85 static GstElement *rtsp_media_factory_uri_create_element (GstRTSPMediaFactory *
86 factory, const GstRTSPUrl * url);
88 G_DEFINE_TYPE (GstRTSPMediaFactoryURI, gst_rtsp_media_factory_uri,
89 GST_TYPE_RTSP_MEDIA_FACTORY);
92 gst_rtsp_media_factory_uri_class_init (GstRTSPMediaFactoryURIClass * klass)
94 GObjectClass *gobject_class;
95 GstRTSPMediaFactoryClass *mediafactory_class;
97 gobject_class = G_OBJECT_CLASS (klass);
98 mediafactory_class = GST_RTSP_MEDIA_FACTORY_CLASS (klass);
100 gobject_class->get_property = gst_rtsp_media_factory_uri_get_property;
101 gobject_class->set_property = gst_rtsp_media_factory_uri_set_property;
102 gobject_class->finalize = gst_rtsp_media_factory_uri_finalize;
105 * GstRTSPMediaFactoryURI::uri:
107 * The uri of the resource that will be served by this factory.
109 g_object_class_install_property (gobject_class, PROP_URI,
110 g_param_spec_string ("uri", "URI",
111 "The URI of the resource to stream", DEFAULT_URI,
112 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114 * GstRTSPMediaFactoryURI::use-gstpay:
116 * Allow the usage of gstpay in order to avoid decoding of compressed formats
117 * without a payloader.
119 g_object_class_install_property (gobject_class, PROP_USE_GSTPAY,
120 g_param_spec_boolean ("use-gstpay", "Use gstpay",
121 "Use the gstpay payloader to avoid decoding", DEFAULT_USE_GSTPAY,
122 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124 mediafactory_class->create_element = rtsp_media_factory_uri_create_element;
126 GST_DEBUG_CATEGORY_INIT (rtsp_media_factory_uri_debug, "rtspmediafactoryuri",
127 0, "GstRTSPMediaFactoryUri");
138 payloader_filter (GstPluginFeature * feature, FilterData * data)
141 GstElementFactory *fact;
144 /* we only care about element factories */
145 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
148 if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
151 fact = GST_ELEMENT_FACTORY_CAST (feature);
153 klass = gst_element_factory_get_metadata (fact, GST_ELEMENT_METADATA_KLASS);
155 if (strstr (klass, "Decoder"))
156 list = &data->decode;
157 else if (strstr (klass, "Demux"))
159 else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
161 else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
162 list = &data->payload;
165 GST_DEBUG ("adding %s", GST_OBJECT_NAME (fact));
166 *list = g_list_prepend (*list, gst_object_ref (fact));
173 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
175 GstRTSPMediaFactoryURIPrivate *priv =
176 GST_RTSP_MEDIA_FACTORY_URI_GET_PRIVATE (factory);
177 FilterData data = { NULL, NULL, NULL };
179 GST_DEBUG_OBJECT (factory, "new");
181 factory->priv = priv;
183 priv->uri = g_strdup (DEFAULT_URI);
184 priv->use_gstpay = DEFAULT_USE_GSTPAY;
185 g_mutex_init (&priv->lock);
187 /* get the feature list using the filter */
188 gst_registry_feature_filter (gst_registry_get (), (GstPluginFeatureFilter)
189 payloader_filter, FALSE, &data);
192 g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
194 g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
196 g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
198 priv->raw_vcaps = gst_static_caps_get (&raw_video_caps);
199 priv->raw_acaps = gst_static_caps_get (&raw_audio_caps);
203 gst_rtsp_media_factory_uri_finalize (GObject * obj)
205 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
206 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
208 GST_DEBUG_OBJECT (factory, "finalize");
211 gst_plugin_feature_list_free (priv->demuxers);
212 gst_plugin_feature_list_free (priv->payloaders);
213 gst_plugin_feature_list_free (priv->decoders);
214 gst_caps_unref (priv->raw_vcaps);
215 gst_caps_unref (priv->raw_acaps);
216 g_mutex_clear (&priv->lock);
218 G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
222 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
223 GValue * value, GParamSpec * pspec)
225 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
226 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
230 g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
232 case PROP_USE_GSTPAY:
233 g_value_set_boolean (value, priv->use_gstpay);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
241 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
242 const GValue * value, GParamSpec * pspec)
244 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
245 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
249 gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
251 case PROP_USE_GSTPAY:
252 priv->use_gstpay = g_value_get_boolean (value);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
260 * gst_rtsp_media_factory_uri_new:
262 * Create a new #GstRTSPMediaFactoryURI instance.
264 * Returns: a new #GstRTSPMediaFactoryURI object.
266 GstRTSPMediaFactoryURI *
267 gst_rtsp_media_factory_uri_new (void)
269 GstRTSPMediaFactoryURI *result;
271 result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
277 * gst_rtsp_media_factory_uri_set_uri:
278 * @factory: a #GstRTSPMediaFactory
279 * @uri: the uri the stream
281 * Set the URI of the resource that will be streamed by this factory.
284 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
287 GstRTSPMediaFactoryURIPrivate *priv;
289 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
290 g_return_if_fail (uri != NULL);
292 priv = factory->priv;
294 g_mutex_lock (&priv->lock);
296 priv->uri = g_strdup (uri);
297 g_mutex_unlock (&priv->lock);
301 * gst_rtsp_media_factory_uri_get_uri:
302 * @factory: a #GstRTSPMediaFactory
304 * Get the URI that will provide media for this factory.
306 * Returns: the configured URI. g_free() after usage.
309 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
311 GstRTSPMediaFactoryURIPrivate *priv;
314 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
316 priv = factory->priv;
318 g_mutex_lock (&priv->lock);
319 result = g_strdup (priv->uri);
320 g_mutex_unlock (&priv->lock);
325 static GstElementFactory *
326 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
328 GstRTSPMediaFactoryURIPrivate *priv = urifact->priv;
330 GstElementFactory *factory = NULL;
331 gboolean autoplug_more = FALSE;
333 /* first find a demuxer that can link */
334 list = gst_element_factory_list_filter (priv->demuxers, caps,
335 GST_PAD_SINK, FALSE);
338 GstStructure *structure = gst_caps_get_structure (caps, 0);
339 gboolean parsed = FALSE;
341 gst_structure_get_boolean (structure, "parsed", &parsed);
343 /* Avoid plugging parsers in a loop. This is not 100% correct, as some
344 * parsers don't set parsed=true in caps. We should do something like
345 * decodebin does and track decode chains and elements plugged in those
352 for (walk = list; walk; walk = walk->next) {
353 factory = GST_ELEMENT_FACTORY (walk->data);
354 klass = gst_element_factory_get_metadata (factory,
355 GST_ELEMENT_METADATA_KLASS);
356 if (strstr (klass, "Parser"))
357 /* caps have parsed=true, so skip this parser to avoid loops */
360 autoplug_more = TRUE;
364 /* caps don't have parsed=true set and we have a demuxer/parser */
365 autoplug_more = TRUE;
368 gst_plugin_feature_list_free (list);
372 /* we have a demuxer, try that one first */
375 /* no demuxer try a depayloader */
376 list = gst_element_factory_list_filter (priv->payloaders, caps,
377 GST_PAD_SINK, FALSE);
380 if (priv->use_gstpay) {
381 /* no depayloader or parser/demuxer, use gstpay when allowed */
382 factory = gst_element_factory_find ("rtpgstpay");
384 /* no depayloader, try a decoder, we'll get to a payloader for a decoded
385 * video or audio format, worst case. */
386 list = gst_element_factory_list_filter (priv->decoders, caps,
387 GST_PAD_SINK, FALSE);
390 /* we have a decoder, try that one first */
391 gst_plugin_feature_list_free (list);
398 factory = GST_ELEMENT_FACTORY_CAST (list->data);
399 g_object_ref (factory);
400 gst_plugin_feature_list_free (list);
406 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
407 GstElement * element)
410 GstElementFactory *factory;
412 GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
413 GST_DEBUG_PAD_NAME (pad), caps);
415 data = g_object_get_data (G_OBJECT (element), factory_key);
417 if (!(factory = find_payloader (data->factory, caps)))
420 /* we found a payloader, stop autoplugging so we can plug the
422 GST_DEBUG ("found factory %s",
423 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
424 gst_object_unref (factory);
431 /* no payloader, continue autoplugging */
432 GST_DEBUG ("no payloader found");
438 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
440 GstRTSPMediaFactoryURI *urifact;
441 GstRTSPMediaFactoryURIPrivate *priv;
443 GstElementFactory *factory;
444 GstElement *payloader;
446 GstPad *sinkpad, *srcpad, *ghostpad;
450 GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
452 /* link the element now and expose the pad */
453 data = g_object_get_data (G_OBJECT (element), factory_key);
454 urifact = data->factory;
455 priv = urifact->priv;
457 /* ref to make refcounting easier later */
458 gst_object_ref (pad);
459 padname = gst_pad_get_name (pad);
461 /* get pad caps first, then call get_caps, then fail */
462 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
463 if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
466 /* check for raw caps */
467 if (gst_caps_can_intersect (caps, priv->raw_vcaps)) {
468 /* we have raw video caps, insert converter */
469 convert = gst_element_factory_make ("videoconvert", NULL);
470 } else if (gst_caps_can_intersect (caps, priv->raw_acaps)) {
471 /* we have raw audio caps, insert converter */
472 convert = gst_element_factory_make ("audioconvert", NULL);
478 gst_bin_add (GST_BIN_CAST (element), convert);
479 gst_element_set_state (convert, GST_STATE_PLAYING);
481 sinkpad = gst_element_get_static_pad (convert, "sink");
482 gst_pad_link (pad, sinkpad);
483 gst_object_unref (sinkpad);
485 /* unref old pad, we reffed before */
486 gst_object_unref (pad);
488 /* continue with new pad and caps */
489 pad = gst_element_get_static_pad (convert, "src");
490 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
491 if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
495 if (!(factory = find_payloader (urifact, caps)))
498 gst_caps_unref (caps);
500 /* we have a payloader now */
501 GST_DEBUG ("found payloader factory %s",
502 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
504 payloader = gst_element_factory_create (factory, NULL);
505 if (payloader == NULL)
508 g_object_set (payloader, "pt", data->pt, NULL);
511 if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
513 g_object_set (payloader, "buffer-list", TRUE, NULL);
515 /* add the payloader to the pipeline */
516 gst_bin_add (GST_BIN_CAST (element), payloader);
517 gst_element_set_state (payloader, GST_STATE_PLAYING);
519 /* link the pad to the sinkpad of the payloader */
520 sinkpad = gst_element_get_static_pad (payloader, "sink");
521 gst_pad_link (pad, sinkpad);
522 gst_object_unref (sinkpad);
523 gst_object_unref (pad);
525 /* now expose the srcpad of the payloader as a ghostpad with the same name
526 * as the uridecodebin pad name. */
527 srcpad = gst_element_get_static_pad (payloader, "src");
528 ghostpad = gst_ghost_pad_new (padname, srcpad);
529 gst_object_unref (srcpad);
532 gst_pad_set_active (ghostpad, TRUE);
533 gst_element_add_pad (element, ghostpad);
540 GST_WARNING ("could not get caps from pad");
542 gst_object_unref (pad);
547 GST_DEBUG ("no payloader found");
549 gst_caps_unref (caps);
550 gst_object_unref (pad);
555 GST_ERROR ("could not create payloader from factory");
557 gst_caps_unref (caps);
558 gst_object_unref (pad);
564 no_more_pads_cb (GstElement * uribin, GstElement * element)
566 GST_DEBUG ("no-more-pads");
567 gst_element_no_more_pads (element);
571 rtsp_media_factory_uri_create_element (GstRTSPMediaFactory * factory,
572 const GstRTSPUrl * url)
574 GstRTSPMediaFactoryURIPrivate *priv;
575 GstElement *topbin, *element, *uribin;
576 GstRTSPMediaFactoryURI *urifact;
579 urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
580 priv = urifact->priv;
582 GST_LOG ("creating element");
584 topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
585 g_assert (topbin != NULL);
587 /* our bin will dynamically expose payloaded pads */
588 element = gst_bin_new ("dynpay0");
589 g_assert (element != NULL);
591 uribin = gst_element_factory_make ("uridecodebin", "uribin");
593 goto no_uridecodebin;
595 g_object_set (uribin, "uri", priv->uri, NULL);
597 /* keep factory data around */
598 data = g_new0 (FactoryData, 1);
599 data->factory = g_object_ref (factory);
602 g_object_set_data_full (G_OBJECT (element), factory_key,
603 data, (GDestroyNotify) free_data);
605 /* connect to the signals */
606 g_signal_connect (uribin, "autoplug-continue",
607 (GCallback) autoplug_continue_cb, element);
608 g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
609 g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
612 gst_bin_add (GST_BIN_CAST (element), uribin);
613 gst_bin_add (GST_BIN_CAST (topbin), element);
619 g_critical ("can't create uridecodebin element");
620 gst_object_unref (element);