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.
22 #include "rtsp-media-factory-uri.h"
24 #define DEFAULT_URI NULL
25 #define DEFAULT_USE_GSTPAY FALSE
36 #define RAW_VIDEO_CAPS \
41 #define RAW_AUDIO_CAPS \
45 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS (RAW_VIDEO_CAPS);
46 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS (RAW_AUDIO_CAPS);
50 GstRTSPMediaFactoryURI *factory;
55 free_data (FactoryData * data)
57 g_object_unref (data->factory);
61 static const gchar *factory_key = "GstRTSPMediaFactoryURI";
63 GST_DEBUG_CATEGORY_STATIC (rtsp_media_factory_uri_debug);
64 #define GST_CAT_DEFAULT rtsp_media_factory_uri_debug
66 static void gst_rtsp_media_factory_uri_get_property (GObject * object,
67 guint propid, GValue * value, GParamSpec * pspec);
68 static void gst_rtsp_media_factory_uri_set_property (GObject * object,
69 guint propid, const GValue * value, GParamSpec * pspec);
70 static void gst_rtsp_media_factory_uri_finalize (GObject * obj);
72 static GstElement *rtsp_media_factory_uri_get_element (GstRTSPMediaFactory *
73 factory, const GstRTSPUrl * url);
75 G_DEFINE_TYPE (GstRTSPMediaFactoryURI, gst_rtsp_media_factory_uri,
76 GST_TYPE_RTSP_MEDIA_FACTORY);
79 gst_rtsp_media_factory_uri_class_init (GstRTSPMediaFactoryURIClass * klass)
81 GObjectClass *gobject_class;
82 GstRTSPMediaFactoryClass *mediafactory_class;
84 gobject_class = G_OBJECT_CLASS (klass);
85 mediafactory_class = GST_RTSP_MEDIA_FACTORY_CLASS (klass);
87 gobject_class->get_property = gst_rtsp_media_factory_uri_get_property;
88 gobject_class->set_property = gst_rtsp_media_factory_uri_set_property;
89 gobject_class->finalize = gst_rtsp_media_factory_uri_finalize;
92 * GstRTSPMediaFactoryURI::uri
94 * The uri of the resource that will be served by this factory.
96 g_object_class_install_property (gobject_class, PROP_URI,
97 g_param_spec_string ("uri", "URI",
98 "The URI of the resource to stream", DEFAULT_URI,
99 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101 * GstRTSPMediaFactoryURI::use-gstpay
103 * Allow the usage of gstpay in order to avoid decoding of compressed formats
104 * without a payloader.
106 g_object_class_install_property (gobject_class, PROP_USE_GSTPAY,
107 g_param_spec_boolean ("use-gstpay", "Use gstpay",
108 "Use the gstpay payloader to avoid decoding", DEFAULT_USE_GSTPAY,
109 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
111 mediafactory_class->get_element = rtsp_media_factory_uri_get_element;
113 GST_DEBUG_CATEGORY_INIT (rtsp_media_factory_uri_debug, "rtspmediafactoryuri",
114 0, "GstRTSPMediaFactoryUri");
125 payloader_filter (GstPluginFeature * feature, FilterData * data)
129 GstElementFactory *fact;
132 /* we only care about element factories */
133 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
136 if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
139 fact = GST_ELEMENT_FACTORY_CAST (feature);
141 klass = gst_element_factory_get_klass (fact);
143 if (strstr (klass, "Decoder"))
144 list = &data->decode;
145 else if (strstr (klass, "Demux"))
147 else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
149 else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
150 list = &data->payload;
153 GST_DEBUG ("adding %s", GST_PLUGIN_FEATURE_NAME (fact));
154 *list = g_list_prepend (*list, fact);
161 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
163 FilterData data = { NULL, NULL, NULL };
165 factory->uri = g_strdup (DEFAULT_URI);
166 factory->use_gstpay = DEFAULT_USE_GSTPAY;
168 /* get the feature list using the filter */
169 gst_default_registry_feature_filter ((GstPluginFeatureFilter)
170 payloader_filter, FALSE, &data);
173 g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
174 factory->payloaders =
175 g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
177 g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
179 factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
180 factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
184 gst_rtsp_media_factory_uri_finalize (GObject * obj)
186 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
188 g_free (factory->uri);
189 gst_plugin_feature_list_free (factory->demuxers);
190 gst_plugin_feature_list_free (factory->payloaders);
191 gst_plugin_feature_list_free (factory->decoders);
192 gst_caps_unref (factory->raw_vcaps);
193 gst_caps_unref (factory->raw_acaps);
195 G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
199 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
200 GValue * value, GParamSpec * pspec)
202 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
206 g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
208 case PROP_USE_GSTPAY:
209 g_value_set_boolean (value, factory->use_gstpay);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
217 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
218 const GValue * value, GParamSpec * pspec)
220 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
224 gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
226 case PROP_USE_GSTPAY:
227 factory->use_gstpay = g_value_get_boolean (value);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
235 * gst_rtsp_media_factory_uri_new:
237 * Create a new #GstRTSPMediaFactoryURI instance.
239 * Returns: a new #GstRTSPMediaFactoryURI object.
241 GstRTSPMediaFactoryURI *
242 gst_rtsp_media_factory_uri_new (void)
244 GstRTSPMediaFactoryURI *result;
246 result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
252 * gst_rtsp_media_factory_uri_set_uri:
253 * @factory: a #GstRTSPMediaFactory
254 * @uri: the uri the stream
256 * Set the URI of the resource that will be streamed by this factory.
259 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
262 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
263 g_return_if_fail (uri != NULL);
265 GST_RTSP_MEDIA_FACTORY_LOCK (factory);
266 g_free (factory->uri);
267 factory->uri = g_strdup (uri);
268 GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
272 * gst_rtsp_media_factory_uri_get_uri:
273 * @factory: a #GstRTSPMediaFactory
275 * Get the URI that will provide media for this factory.
277 * Returns: the configured URI. g_free() after usage.
280 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
284 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
286 GST_RTSP_MEDIA_FACTORY_LOCK (factory);
287 result = g_strdup (factory->uri);
288 GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
293 static GstElementFactory *
294 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
297 GstElementFactory *factory = NULL;
299 /* first find a demuxer that can link */
300 list = gst_element_factory_list_filter (urifact->demuxers, caps,
301 GST_PAD_SINK, FALSE);
304 /* we have a demuxer, try that one first */
305 gst_plugin_feature_list_free (list);
309 /* no demuxer try a depayloader */
310 list = gst_element_factory_list_filter (urifact->payloaders, caps,
311 GST_PAD_SINK, FALSE);
314 if (urifact->use_gstpay) {
315 /* no depayloader or parser/demuxer, use gstpay when allowed */
316 factory = gst_element_factory_find ("rtpgstpay");
318 /* no depayloader, try a decoder, we'll get to a payloader for a decoded
319 * video or audio format, worst case. */
320 list = gst_element_factory_list_filter (urifact->decoders, caps,
321 GST_PAD_SINK, FALSE);
324 /* we have a decoder, try that one first */
325 gst_plugin_feature_list_free (list);
332 factory = GST_ELEMENT_FACTORY_CAST (list->data);
333 g_object_ref (factory);
334 gst_plugin_feature_list_free (list);
340 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
341 GstElement * element)
345 GstElementFactory *factory;
348 GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
349 GST_DEBUG_PAD_NAME (pad), caps);
351 data = g_object_get_data (G_OBJECT (element), factory_key);
353 if (!(factory = find_payloader (data->factory, caps)))
356 /* we found a payloader, stop autoplugging so we can plug the
358 GST_DEBUG ("found factory %s",
359 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
360 gst_object_unref (factory);
367 /* no payloader, continue autoplugging */
368 GST_DEBUG ("no payloader found");
374 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
376 GstRTSPMediaFactoryURI *urifact;
378 GstElementFactory *factory;
379 GstElement *payloader;
381 GstPad *sinkpad, *srcpad, *ghostpad;
385 GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
387 /* link the element now and expose the pad */
388 data = g_object_get_data (G_OBJECT (element), factory_key);
389 urifact = data->factory;
391 /* ref to make refcounting easier later */
392 gst_object_ref (pad);
393 padname = gst_pad_get_name (pad);
395 /* get pad caps first, then call get_caps, then fail */
396 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
397 if ((caps = gst_pad_get_caps (pad, NULL)) == NULL)
400 /* check for raw caps */
401 if (gst_caps_can_intersect (caps, urifact->raw_vcaps)) {
402 /* we have raw video caps, insert converter */
403 convert = gst_element_factory_make ("ffmpegcolorspace", NULL);
404 } else if (gst_caps_can_intersect (caps, urifact->raw_acaps)) {
405 /* we have raw audio caps, insert converter */
406 convert = gst_element_factory_make ("audioconvert", NULL);
412 gst_bin_add (GST_BIN_CAST (element), convert);
413 gst_element_set_state (convert, GST_STATE_PLAYING);
415 sinkpad = gst_element_get_static_pad (convert, "sink");
416 gst_pad_link (pad, sinkpad);
417 gst_object_unref (sinkpad);
419 /* unref old pad, we reffed before */
420 gst_object_unref (pad);
422 /* continue with new pad and caps */
423 pad = gst_element_get_static_pad (convert, "src");
424 if ((caps = gst_pad_get_current_caps (pad)) == NULL)
425 if ((caps = gst_pad_get_caps (pad, NULL)) == NULL)
429 if (!(factory = find_payloader (urifact, caps)))
432 gst_caps_unref (caps);
434 /* we have a payloader now */
435 GST_DEBUG ("found payloader factory %s",
436 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
438 payloader = gst_element_factory_create (factory, NULL);
439 if (payloader == NULL)
442 g_object_set (payloader, "pt", data->pt, NULL);
445 if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
447 g_object_set (payloader, "buffer-list", TRUE, NULL);
449 /* add the payloader to the pipeline */
450 gst_bin_add (GST_BIN_CAST (element), payloader);
451 gst_element_set_state (payloader, GST_STATE_PLAYING);
453 /* link the pad to the sinkpad of the payloader */
454 sinkpad = gst_element_get_static_pad (payloader, "sink");
455 gst_pad_link (pad, sinkpad);
456 gst_object_unref (sinkpad);
457 gst_object_unref (pad);
459 /* now expose the srcpad of the payloader as a ghostpad with the same name
460 * as the uridecodebin pad name. */
461 srcpad = gst_element_get_static_pad (payloader, "src");
462 ghostpad = gst_ghost_pad_new (padname, srcpad);
463 gst_object_unref (srcpad);
466 gst_pad_set_active (ghostpad, TRUE);
467 gst_element_add_pad (element, ghostpad);
474 GST_WARNING ("could not get caps from pad");
476 gst_object_unref (pad);
481 GST_DEBUG ("no payloader found");
483 gst_caps_unref (caps);
484 gst_object_unref (pad);
489 GST_ERROR ("could not create payloader from factory");
491 gst_caps_unref (caps);
492 gst_object_unref (pad);
498 no_more_pads_cb (GstElement * uribin, GstElement * element)
500 GST_DEBUG ("no-more-pads");
501 gst_element_no_more_pads (element);
505 rtsp_media_factory_uri_get_element (GstRTSPMediaFactory * factory,
506 const GstRTSPUrl * url)
508 GstElement *topbin, *element, *uribin;
509 GstRTSPMediaFactoryURI *urifact;
512 urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
514 GST_LOG ("creating element");
516 topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
517 g_assert (topbin != NULL);
519 /* our bin will dynamically expose payloaded pads */
520 element = gst_bin_new ("dynpay0");
521 g_assert (element != NULL);
523 uribin = gst_element_factory_make ("uridecodebin", "uribin");
525 goto no_uridecodebin;
527 g_object_set (uribin, "uri", urifact->uri, NULL);
529 /* keep factory data around */
530 data = g_new0 (FactoryData, 1);
531 data->factory = g_object_ref (factory);
534 g_object_set_data_full (G_OBJECT (element), factory_key,
535 data, (GDestroyNotify) free_data);
537 /* connect to the signals */
538 g_signal_connect (uribin, "autoplug-continue",
539 (GCallback) autoplug_continue_cb, element);
540 g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
541 g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
544 gst_bin_add (GST_BIN_CAST (element), uribin);
545 gst_bin_add (GST_BIN_CAST (topbin), element);
551 g_critical ("can't create uridecodebin element");
552 g_object_unref (element);