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.
20 * SECTION:rtsp-media-factory-uri
21 * @short_description: A factory for URI sources
22 * @see_also: #GstRTSPMediaFactory, #GstRTSPMedia
24 * This specialized #GstRTSPMediaFactory constructs media pipelines from a URI,
25 * given with gst_rtsp_media_factory_uri_set_uri().
27 * It will automatically demux and payload the different streams found in the
30 * Last reviewed on 2013-07-11 (1.0.0)
35 #include "rtsp-media-factory-uri.h"
37 #define GST_RTSP_MEDIA_FACTORY_URI_GET_PRIVATE(obj) \
38 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA_FACTORY_URI, GstRTSPMediaFactoryURIPrivate))
40 struct _GstRTSPMediaFactoryURIPrivate
43 gchar *uri; /* protected by lock */
53 #define DEFAULT_URI NULL
54 #define DEFAULT_USE_GSTPAY FALSE
65 #define RAW_VIDEO_CAPS \
68 #define RAW_AUDIO_CAPS \
71 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS (RAW_VIDEO_CAPS);
72 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS (RAW_AUDIO_CAPS);
76 GstRTSPMediaFactoryURI *factory;
81 free_data (FactoryData * data)
83 g_object_unref (data->factory);
87 static const gchar *factory_key = "GstRTSPMediaFactoryURI";
89 GST_DEBUG_CATEGORY_STATIC (rtsp_media_factory_uri_debug);
90 #define GST_CAT_DEFAULT rtsp_media_factory_uri_debug
92 static void gst_rtsp_media_factory_uri_get_property (GObject * object,
93 guint propid, GValue * value, GParamSpec * pspec);
94 static void gst_rtsp_media_factory_uri_set_property (GObject * object,
95 guint propid, const GValue * value, GParamSpec * pspec);
96 static void gst_rtsp_media_factory_uri_finalize (GObject * obj);
98 static GstElement *rtsp_media_factory_uri_create_element (GstRTSPMediaFactory *
99 factory, const GstRTSPUrl * url);
101 G_DEFINE_TYPE (GstRTSPMediaFactoryURI, gst_rtsp_media_factory_uri,
102 GST_TYPE_RTSP_MEDIA_FACTORY);
105 gst_rtsp_media_factory_uri_class_init (GstRTSPMediaFactoryURIClass * klass)
107 GObjectClass *gobject_class;
108 GstRTSPMediaFactoryClass *mediafactory_class;
110 g_type_class_add_private (klass, sizeof (GstRTSPMediaFactoryURIPrivate));
112 gobject_class = G_OBJECT_CLASS (klass);
113 mediafactory_class = GST_RTSP_MEDIA_FACTORY_CLASS (klass);
115 gobject_class->get_property = gst_rtsp_media_factory_uri_get_property;
116 gobject_class->set_property = gst_rtsp_media_factory_uri_set_property;
117 gobject_class->finalize = gst_rtsp_media_factory_uri_finalize;
120 * GstRTSPMediaFactoryURI::uri:
122 * The uri of the resource that will be served by this factory.
124 g_object_class_install_property (gobject_class, PROP_URI,
125 g_param_spec_string ("uri", "URI",
126 "The URI of the resource to stream", DEFAULT_URI,
127 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129 * GstRTSPMediaFactoryURI::use-gstpay:
131 * Allow the usage of gstpay in order to avoid decoding of compressed formats
132 * without a payloader.
134 g_object_class_install_property (gobject_class, PROP_USE_GSTPAY,
135 g_param_spec_boolean ("use-gstpay", "Use gstpay",
136 "Use the gstpay payloader to avoid decoding", DEFAULT_USE_GSTPAY,
137 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139 mediafactory_class->create_element = rtsp_media_factory_uri_create_element;
141 GST_DEBUG_CATEGORY_INIT (rtsp_media_factory_uri_debug, "rtspmediafactoryuri",
142 0, "GstRTSPMediaFactoryUri");
153 payloader_filter (GstPluginFeature * feature, FilterData * data)
156 GstElementFactory *fact;
159 /* we only care about element factories */
160 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
163 if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
166 fact = GST_ELEMENT_FACTORY_CAST (feature);
168 klass = gst_element_factory_get_metadata (fact, GST_ELEMENT_METADATA_KLASS);
170 if (strstr (klass, "Decoder"))
171 list = &data->decode;
172 else if (strstr (klass, "Demux"))
174 else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
176 else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
177 list = &data->payload;
180 GST_DEBUG ("adding %s", GST_OBJECT_NAME (fact));
181 *list = g_list_prepend (*list, gst_object_ref (fact));
188 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
190 GstRTSPMediaFactoryURIPrivate *priv =
191 GST_RTSP_MEDIA_FACTORY_URI_GET_PRIVATE (factory);
192 FilterData data = { NULL, NULL, NULL };
194 GST_DEBUG_OBJECT (factory, "new");
196 factory->priv = priv;
198 priv->uri = g_strdup (DEFAULT_URI);
199 priv->use_gstpay = DEFAULT_USE_GSTPAY;
200 g_mutex_init (&priv->lock);
202 /* get the feature list using the filter */
203 gst_registry_feature_filter (gst_registry_get (), (GstPluginFeatureFilter)
204 payloader_filter, FALSE, &data);
207 g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
209 g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
211 g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
213 priv->raw_vcaps = gst_static_caps_get (&raw_video_caps);
214 priv->raw_acaps = gst_static_caps_get (&raw_audio_caps);
218 gst_rtsp_media_factory_uri_finalize (GObject * obj)
220 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
221 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
223 GST_DEBUG_OBJECT (factory, "finalize");
226 gst_plugin_feature_list_free (priv->demuxers);
227 gst_plugin_feature_list_free (priv->payloaders);
228 gst_plugin_feature_list_free (priv->decoders);
229 gst_caps_unref (priv->raw_vcaps);
230 gst_caps_unref (priv->raw_acaps);
231 g_mutex_clear (&priv->lock);
233 G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
237 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
238 GValue * value, GParamSpec * pspec)
240 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
241 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
245 g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
247 case PROP_USE_GSTPAY:
248 g_value_set_boolean (value, priv->use_gstpay);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
256 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
257 const GValue * value, GParamSpec * pspec)
259 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
260 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
264 gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
266 case PROP_USE_GSTPAY:
267 priv->use_gstpay = g_value_get_boolean (value);
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
275 * gst_rtsp_media_factory_uri_new:
277 * Create a new #GstRTSPMediaFactoryURI instance.
279 * Returns: (transfer full): a new #GstRTSPMediaFactoryURI object.
281 GstRTSPMediaFactoryURI *
282 gst_rtsp_media_factory_uri_new (void)
284 GstRTSPMediaFactoryURI *result;
286 result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
292 * gst_rtsp_media_factory_uri_set_uri:
293 * @factory: a #GstRTSPMediaFactory
294 * @uri: the uri the stream
296 * Set the URI of the resource that will be streamed by this factory.
299 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
302 GstRTSPMediaFactoryURIPrivate *priv;
304 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
305 g_return_if_fail (uri != NULL);
307 priv = factory->priv;
309 g_mutex_lock (&priv->lock);
311 priv->uri = g_strdup (uri);
312 g_mutex_unlock (&priv->lock);
316 * gst_rtsp_media_factory_uri_get_uri:
317 * @factory: a #GstRTSPMediaFactory
319 * Get the URI that will provide media for this factory.
321 * Returns: (transfer full): the configured URI. g_free() after usage.
324 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
326 GstRTSPMediaFactoryURIPrivate *priv;
329 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
331 priv = factory->priv;
333 g_mutex_lock (&priv->lock);
334 result = g_strdup (priv->uri);
335 g_mutex_unlock (&priv->lock);
340 static GstElementFactory *
341 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
343 GstRTSPMediaFactoryURIPrivate *priv = urifact->priv;
345 GstElementFactory *factory = NULL;
346 gboolean autoplug_more = FALSE;
348 /* first find a demuxer that can link */
349 list = gst_element_factory_list_filter (priv->demuxers, caps,
350 GST_PAD_SINK, FALSE);
353 GstStructure *structure = gst_caps_get_structure (caps, 0);
354 gboolean parsed = FALSE;
355 gint mpegversion = 0;
357 if (!gst_structure_get_boolean (structure, "parsed", &parsed) &&
358 gst_structure_has_name (structure, "audio/mpeg") &&
359 gst_structure_get_int (structure, "mpegversion", &mpegversion) &&
360 (mpegversion == 2 || mpegversion == 4)) {
361 /* for AAC it's framed=true instead of parsed=true */
362 gst_structure_get_boolean (structure, "framed", &parsed);
365 /* Avoid plugging parsers in a loop. This is not 100% correct, as some
366 * parsers don't set parsed=true in caps. We should do something like
367 * decodebin does and track decode chains and elements plugged in those
374 for (walk = list; walk; walk = walk->next) {
375 factory = GST_ELEMENT_FACTORY (walk->data);
376 klass = gst_element_factory_get_metadata (factory,
377 GST_ELEMENT_METADATA_KLASS);
378 if (strstr (klass, "Parser"))
379 /* caps have parsed=true, so skip this parser to avoid loops */
382 autoplug_more = TRUE;
386 /* caps don't have parsed=true set and we have a demuxer/parser */
387 autoplug_more = TRUE;
390 gst_plugin_feature_list_free (list);
394 /* we have a demuxer, try that one first */
397 /* no demuxer try a depayloader */
398 list = gst_element_factory_list_filter (priv->payloaders, caps,
399 GST_PAD_SINK, FALSE);
402 if (priv->use_gstpay) {
403 /* no depayloader or parser/demuxer, use gstpay when allowed */
404 factory = gst_element_factory_find ("rtpgstpay");
406 /* no depayloader, try a decoder, we'll get to a payloader for a decoded
407 * video or audio format, worst case. */
408 list = gst_element_factory_list_filter (priv->decoders, caps,
409 GST_PAD_SINK, FALSE);
412 /* we have a decoder, try that one first */
413 gst_plugin_feature_list_free (list);
420 factory = GST_ELEMENT_FACTORY_CAST (list->data);
421 g_object_ref (factory);
422 gst_plugin_feature_list_free (list);
428 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
429 GstElement * element)
432 GstElementFactory *factory;
434 GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
435 GST_DEBUG_PAD_NAME (pad), caps);
437 data = g_object_get_data (G_OBJECT (element), factory_key);
439 if (!(factory = find_payloader (data->factory, caps)))
442 /* we found a payloader, stop autoplugging so we can plug the
444 GST_DEBUG ("found factory %s",
445 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
446 gst_object_unref (factory);
453 /* no payloader, continue autoplugging */
454 GST_DEBUG ("no payloader found");
460 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
462 GstRTSPMediaFactoryURI *urifact;
463 GstRTSPMediaFactoryURIPrivate *priv;
465 GstElementFactory *factory;
466 GstElement *payloader;
468 GstPad *sinkpad, *srcpad, *ghostpad;
472 GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
474 /* link the element now and expose the pad */
475 data = g_object_get_data (G_OBJECT (element), factory_key);
476 urifact = data->factory;
477 priv = urifact->priv;
479 /* ref to make refcounting easier later */
480 gst_object_ref (pad);
481 padname = gst_pad_get_name (pad);
483 /* get pad caps first, then call get_caps, then fail */
484 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
485 if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
488 /* check for raw caps */
489 if (gst_caps_can_intersect (caps, priv->raw_vcaps)) {
490 /* we have raw video caps, insert converter */
491 convert = gst_element_factory_make ("videoconvert", NULL);
492 } else if (gst_caps_can_intersect (caps, priv->raw_acaps)) {
493 /* we have raw audio caps, insert converter */
494 convert = gst_element_factory_make ("audioconvert", NULL);
500 gst_bin_add (GST_BIN_CAST (element), convert);
501 gst_element_set_state (convert, GST_STATE_PLAYING);
503 sinkpad = gst_element_get_static_pad (convert, "sink");
504 gst_pad_link (pad, sinkpad);
505 gst_object_unref (sinkpad);
507 /* unref old pad, we reffed before */
508 gst_object_unref (pad);
510 /* continue with new pad and caps */
511 pad = gst_element_get_static_pad (convert, "src");
512 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
513 if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
517 if (!(factory = find_payloader (urifact, caps)))
520 gst_caps_unref (caps);
522 /* we have a payloader now */
523 GST_DEBUG ("found payloader factory %s",
524 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
526 payloader = gst_element_factory_create (factory, NULL);
527 if (payloader == NULL)
530 g_object_set (payloader, "pt", data->pt, NULL);
533 if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
535 g_object_set (payloader, "buffer-list", TRUE, NULL);
537 /* add the payloader to the pipeline */
538 gst_bin_add (GST_BIN_CAST (element), payloader);
539 gst_element_set_state (payloader, GST_STATE_PLAYING);
541 /* link the pad to the sinkpad of the payloader */
542 sinkpad = gst_element_get_static_pad (payloader, "sink");
543 gst_pad_link (pad, sinkpad);
544 gst_object_unref (sinkpad);
545 gst_object_unref (pad);
547 /* now expose the srcpad of the payloader as a ghostpad with the same name
548 * as the uridecodebin pad name. */
549 srcpad = gst_element_get_static_pad (payloader, "src");
550 ghostpad = gst_ghost_pad_new (padname, srcpad);
551 gst_object_unref (srcpad);
554 gst_pad_set_active (ghostpad, TRUE);
555 gst_element_add_pad (element, ghostpad);
562 GST_WARNING ("could not get caps from pad");
564 gst_object_unref (pad);
569 GST_DEBUG ("no payloader found");
571 gst_caps_unref (caps);
572 gst_object_unref (pad);
577 GST_ERROR ("could not create payloader from factory");
579 gst_caps_unref (caps);
580 gst_object_unref (pad);
586 no_more_pads_cb (GstElement * uribin, GstElement * element)
588 GST_DEBUG ("no-more-pads");
589 gst_element_no_more_pads (element);
593 rtsp_media_factory_uri_create_element (GstRTSPMediaFactory * factory,
594 const GstRTSPUrl * url)
596 GstRTSPMediaFactoryURIPrivate *priv;
597 GstElement *topbin, *element, *uribin;
598 GstRTSPMediaFactoryURI *urifact;
601 urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
602 priv = urifact->priv;
604 GST_LOG ("creating element");
606 topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
607 g_assert (topbin != NULL);
609 /* our bin will dynamically expose payloaded pads */
610 element = gst_bin_new ("dynpay0");
611 g_assert (element != NULL);
613 uribin = gst_element_factory_make ("uridecodebin", "uribin");
615 goto no_uridecodebin;
617 g_object_set (uribin, "uri", priv->uri, NULL);
619 /* keep factory data around */
620 data = g_new0 (FactoryData, 1);
621 data->factory = g_object_ref (urifact);
624 g_object_set_data_full (G_OBJECT (element), factory_key,
625 data, (GDestroyNotify) free_data);
627 /* connect to the signals */
628 g_signal_connect (uribin, "autoplug-continue",
629 (GCallback) autoplug_continue_cb, element);
630 g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
631 g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
634 gst_bin_add (GST_BIN_CAST (element), uribin);
635 gst_bin_add (GST_BIN_CAST (topbin), element);
641 g_critical ("can't create uridecodebin element");
642 gst_object_unref (element);