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
30 gchar *uri; /* protected by lock */
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 g_type_class_add_private (klass, sizeof (GstRTSPMediaFactoryURIPrivate));
99 gobject_class = G_OBJECT_CLASS (klass);
100 mediafactory_class = GST_RTSP_MEDIA_FACTORY_CLASS (klass);
102 gobject_class->get_property = gst_rtsp_media_factory_uri_get_property;
103 gobject_class->set_property = gst_rtsp_media_factory_uri_set_property;
104 gobject_class->finalize = gst_rtsp_media_factory_uri_finalize;
107 * GstRTSPMediaFactoryURI::uri:
109 * The uri of the resource that will be served by this factory.
111 g_object_class_install_property (gobject_class, PROP_URI,
112 g_param_spec_string ("uri", "URI",
113 "The URI of the resource to stream", DEFAULT_URI,
114 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 * GstRTSPMediaFactoryURI::use-gstpay:
118 * Allow the usage of gstpay in order to avoid decoding of compressed formats
119 * without a payloader.
121 g_object_class_install_property (gobject_class, PROP_USE_GSTPAY,
122 g_param_spec_boolean ("use-gstpay", "Use gstpay",
123 "Use the gstpay payloader to avoid decoding", DEFAULT_USE_GSTPAY,
124 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126 mediafactory_class->create_element = rtsp_media_factory_uri_create_element;
128 GST_DEBUG_CATEGORY_INIT (rtsp_media_factory_uri_debug, "rtspmediafactoryuri",
129 0, "GstRTSPMediaFactoryUri");
140 payloader_filter (GstPluginFeature * feature, FilterData * data)
143 GstElementFactory *fact;
146 /* we only care about element factories */
147 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
150 if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
153 fact = GST_ELEMENT_FACTORY_CAST (feature);
155 klass = gst_element_factory_get_metadata (fact, GST_ELEMENT_METADATA_KLASS);
157 if (strstr (klass, "Decoder"))
158 list = &data->decode;
159 else if (strstr (klass, "Demux"))
161 else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
163 else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
164 list = &data->payload;
167 GST_DEBUG ("adding %s", GST_OBJECT_NAME (fact));
168 *list = g_list_prepend (*list, gst_object_ref (fact));
175 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
177 GstRTSPMediaFactoryURIPrivate *priv =
178 GST_RTSP_MEDIA_FACTORY_URI_GET_PRIVATE (factory);
179 FilterData data = { NULL, NULL, NULL };
181 GST_DEBUG_OBJECT (factory, "new");
183 factory->priv = priv;
185 priv->uri = g_strdup (DEFAULT_URI);
186 priv->use_gstpay = DEFAULT_USE_GSTPAY;
187 g_mutex_init (&priv->lock);
189 /* get the feature list using the filter */
190 gst_registry_feature_filter (gst_registry_get (), (GstPluginFeatureFilter)
191 payloader_filter, FALSE, &data);
194 g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
196 g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
198 g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
200 priv->raw_vcaps = gst_static_caps_get (&raw_video_caps);
201 priv->raw_acaps = gst_static_caps_get (&raw_audio_caps);
205 gst_rtsp_media_factory_uri_finalize (GObject * obj)
207 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
208 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
210 GST_DEBUG_OBJECT (factory, "finalize");
213 gst_plugin_feature_list_free (priv->demuxers);
214 gst_plugin_feature_list_free (priv->payloaders);
215 gst_plugin_feature_list_free (priv->decoders);
216 gst_caps_unref (priv->raw_vcaps);
217 gst_caps_unref (priv->raw_acaps);
218 g_mutex_clear (&priv->lock);
220 G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
224 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
225 GValue * value, GParamSpec * pspec)
227 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
228 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
232 g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
234 case PROP_USE_GSTPAY:
235 g_value_set_boolean (value, priv->use_gstpay);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
243 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
244 const GValue * value, GParamSpec * pspec)
246 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
247 GstRTSPMediaFactoryURIPrivate *priv = factory->priv;
251 gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
253 case PROP_USE_GSTPAY:
254 priv->use_gstpay = g_value_get_boolean (value);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
262 * gst_rtsp_media_factory_uri_new:
264 * Create a new #GstRTSPMediaFactoryURI instance.
266 * Returns: a new #GstRTSPMediaFactoryURI object.
268 GstRTSPMediaFactoryURI *
269 gst_rtsp_media_factory_uri_new (void)
271 GstRTSPMediaFactoryURI *result;
273 result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
279 * gst_rtsp_media_factory_uri_set_uri:
280 * @factory: a #GstRTSPMediaFactory
281 * @uri: the uri the stream
283 * Set the URI of the resource that will be streamed by this factory.
286 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
289 GstRTSPMediaFactoryURIPrivate *priv;
291 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
292 g_return_if_fail (uri != NULL);
294 priv = factory->priv;
296 g_mutex_lock (&priv->lock);
298 priv->uri = g_strdup (uri);
299 g_mutex_unlock (&priv->lock);
303 * gst_rtsp_media_factory_uri_get_uri:
304 * @factory: a #GstRTSPMediaFactory
306 * Get the URI that will provide media for this factory.
308 * Returns: the configured URI. g_free() after usage.
311 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
313 GstRTSPMediaFactoryURIPrivate *priv;
316 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
318 priv = factory->priv;
320 g_mutex_lock (&priv->lock);
321 result = g_strdup (priv->uri);
322 g_mutex_unlock (&priv->lock);
327 static GstElementFactory *
328 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
330 GstRTSPMediaFactoryURIPrivate *priv = urifact->priv;
332 GstElementFactory *factory = NULL;
333 gboolean autoplug_more = FALSE;
335 /* first find a demuxer that can link */
336 list = gst_element_factory_list_filter (priv->demuxers, caps,
337 GST_PAD_SINK, FALSE);
340 GstStructure *structure = gst_caps_get_structure (caps, 0);
341 gboolean parsed = FALSE;
343 gst_structure_get_boolean (structure, "parsed", &parsed);
345 /* Avoid plugging parsers in a loop. This is not 100% correct, as some
346 * parsers don't set parsed=true in caps. We should do something like
347 * decodebin does and track decode chains and elements plugged in those
354 for (walk = list; walk; walk = walk->next) {
355 factory = GST_ELEMENT_FACTORY (walk->data);
356 klass = gst_element_factory_get_metadata (factory,
357 GST_ELEMENT_METADATA_KLASS);
358 if (strstr (klass, "Parser"))
359 /* caps have parsed=true, so skip this parser to avoid loops */
362 autoplug_more = TRUE;
366 /* caps don't have parsed=true set and we have a demuxer/parser */
367 autoplug_more = TRUE;
370 gst_plugin_feature_list_free (list);
374 /* we have a demuxer, try that one first */
377 /* no demuxer try a depayloader */
378 list = gst_element_factory_list_filter (priv->payloaders, caps,
379 GST_PAD_SINK, FALSE);
382 if (priv->use_gstpay) {
383 /* no depayloader or parser/demuxer, use gstpay when allowed */
384 factory = gst_element_factory_find ("rtpgstpay");
386 /* no depayloader, try a decoder, we'll get to a payloader for a decoded
387 * video or audio format, worst case. */
388 list = gst_element_factory_list_filter (priv->decoders, caps,
389 GST_PAD_SINK, FALSE);
392 /* we have a decoder, try that one first */
393 gst_plugin_feature_list_free (list);
400 factory = GST_ELEMENT_FACTORY_CAST (list->data);
401 g_object_ref (factory);
402 gst_plugin_feature_list_free (list);
408 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
409 GstElement * element)
412 GstElementFactory *factory;
414 GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
415 GST_DEBUG_PAD_NAME (pad), caps);
417 data = g_object_get_data (G_OBJECT (element), factory_key);
419 if (!(factory = find_payloader (data->factory, caps)))
422 /* we found a payloader, stop autoplugging so we can plug the
424 GST_DEBUG ("found factory %s",
425 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
426 gst_object_unref (factory);
433 /* no payloader, continue autoplugging */
434 GST_DEBUG ("no payloader found");
440 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
442 GstRTSPMediaFactoryURI *urifact;
443 GstRTSPMediaFactoryURIPrivate *priv;
445 GstElementFactory *factory;
446 GstElement *payloader;
448 GstPad *sinkpad, *srcpad, *ghostpad;
452 GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
454 /* link the element now and expose the pad */
455 data = g_object_get_data (G_OBJECT (element), factory_key);
456 urifact = data->factory;
457 priv = urifact->priv;
459 /* ref to make refcounting easier later */
460 gst_object_ref (pad);
461 padname = gst_pad_get_name (pad);
463 /* get pad caps first, then call get_caps, then fail */
464 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
465 if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
468 /* check for raw caps */
469 if (gst_caps_can_intersect (caps, priv->raw_vcaps)) {
470 /* we have raw video caps, insert converter */
471 convert = gst_element_factory_make ("videoconvert", NULL);
472 } else if (gst_caps_can_intersect (caps, priv->raw_acaps)) {
473 /* we have raw audio caps, insert converter */
474 convert = gst_element_factory_make ("audioconvert", NULL);
480 gst_bin_add (GST_BIN_CAST (element), convert);
481 gst_element_set_state (convert, GST_STATE_PLAYING);
483 sinkpad = gst_element_get_static_pad (convert, "sink");
484 gst_pad_link (pad, sinkpad);
485 gst_object_unref (sinkpad);
487 /* unref old pad, we reffed before */
488 gst_object_unref (pad);
490 /* continue with new pad and caps */
491 pad = gst_element_get_static_pad (convert, "src");
492 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
493 if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
497 if (!(factory = find_payloader (urifact, caps)))
500 gst_caps_unref (caps);
502 /* we have a payloader now */
503 GST_DEBUG ("found payloader factory %s",
504 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
506 payloader = gst_element_factory_create (factory, NULL);
507 if (payloader == NULL)
510 g_object_set (payloader, "pt", data->pt, NULL);
513 if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
515 g_object_set (payloader, "buffer-list", TRUE, NULL);
517 /* add the payloader to the pipeline */
518 gst_bin_add (GST_BIN_CAST (element), payloader);
519 gst_element_set_state (payloader, GST_STATE_PLAYING);
521 /* link the pad to the sinkpad of the payloader */
522 sinkpad = gst_element_get_static_pad (payloader, "sink");
523 gst_pad_link (pad, sinkpad);
524 gst_object_unref (sinkpad);
525 gst_object_unref (pad);
527 /* now expose the srcpad of the payloader as a ghostpad with the same name
528 * as the uridecodebin pad name. */
529 srcpad = gst_element_get_static_pad (payloader, "src");
530 ghostpad = gst_ghost_pad_new (padname, srcpad);
531 gst_object_unref (srcpad);
534 gst_pad_set_active (ghostpad, TRUE);
535 gst_element_add_pad (element, ghostpad);
542 GST_WARNING ("could not get caps from pad");
544 gst_object_unref (pad);
549 GST_DEBUG ("no payloader found");
551 gst_caps_unref (caps);
552 gst_object_unref (pad);
557 GST_ERROR ("could not create payloader from factory");
559 gst_caps_unref (caps);
560 gst_object_unref (pad);
566 no_more_pads_cb (GstElement * uribin, GstElement * element)
568 GST_DEBUG ("no-more-pads");
569 gst_element_no_more_pads (element);
573 rtsp_media_factory_uri_create_element (GstRTSPMediaFactory * factory,
574 const GstRTSPUrl * url)
576 GstRTSPMediaFactoryURIPrivate *priv;
577 GstElement *topbin, *element, *uribin;
578 GstRTSPMediaFactoryURI *urifact;
581 urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
582 priv = urifact->priv;
584 GST_LOG ("creating element");
586 topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
587 g_assert (topbin != NULL);
589 /* our bin will dynamically expose payloaded pads */
590 element = gst_bin_new ("dynpay0");
591 g_assert (element != NULL);
593 uribin = gst_element_factory_make ("uridecodebin", "uribin");
595 goto no_uridecodebin;
597 g_object_set (uribin, "uri", priv->uri, NULL);
599 /* keep factory data around */
600 data = g_new0 (FactoryData, 1);
601 data->factory = g_object_ref (factory);
604 g_object_set_data_full (G_OBJECT (element), factory_key,
605 data, (GDestroyNotify) free_data);
607 /* connect to the signals */
608 g_signal_connect (uribin, "autoplug-continue",
609 (GCallback) autoplug_continue_cb, element);
610 g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
611 g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
614 gst_bin_add (GST_BIN_CAST (element), uribin);
615 gst_bin_add (GST_BIN_CAST (topbin), element);
621 g_critical ("can't create uridecodebin element");
622 gst_object_unref (element);