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
34 #define RAW_VIDEO_CAPS \
39 #define RAW_AUDIO_CAPS \
43 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS (RAW_VIDEO_CAPS);
44 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS (RAW_AUDIO_CAPS);
48 GstRTSPMediaFactoryURI *factory;
53 free_data (FactoryData * data)
55 g_object_unref (data->factory);
59 static const gchar *factory_key = "GstRTSPMediaFactoryURI";
61 GST_DEBUG_CATEGORY (rtsp_media_factory_uri_debug);
62 #define GST_CAT_DEFAULT rtsp_media_factory_uri_debug
64 static void gst_rtsp_media_factory_uri_get_property (GObject * object,
65 guint propid, GValue * value, GParamSpec * pspec);
66 static void gst_rtsp_media_factory_uri_set_property (GObject * object,
67 guint propid, const GValue * value, GParamSpec * pspec);
68 static void gst_rtsp_media_factory_uri_finalize (GObject * obj);
70 static GstElement *rtsp_media_factory_uri_get_element (GstRTSPMediaFactory *
71 factory, const GstRTSPUrl * url);
73 G_DEFINE_TYPE (GstRTSPMediaFactoryURI, gst_rtsp_media_factory_uri,
74 GST_TYPE_RTSP_MEDIA_FACTORY);
77 gst_rtsp_media_factory_uri_class_init (GstRTSPMediaFactoryURIClass * klass)
79 GObjectClass *gobject_class;
80 GstRTSPMediaFactoryClass *mediafactory_class;
82 gobject_class = G_OBJECT_CLASS (klass);
83 mediafactory_class = GST_RTSP_MEDIA_FACTORY_CLASS (klass);
85 gobject_class->get_property = gst_rtsp_media_factory_uri_get_property;
86 gobject_class->set_property = gst_rtsp_media_factory_uri_set_property;
87 gobject_class->finalize = gst_rtsp_media_factory_uri_finalize;
90 * GstRTSPMediaFactoryURI::uri
92 * The uri of the resource that will be served by this factory.
94 g_object_class_install_property (gobject_class, PROP_URI,
95 g_param_spec_string ("uri", "URI",
96 "The URI of the resource to stream", DEFAULT_URI,
97 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
99 mediafactory_class->get_element = rtsp_media_factory_uri_get_element;
101 GST_DEBUG_CATEGORY_INIT (rtsp_media_factory_uri_debug, "rtspmediafactoryuri",
102 0, "GstRTSPMediaFactoryUri");
113 payloader_filter (GstPluginFeature * feature, FilterData * data)
117 GstElementFactory *fact;
120 /* we only care about element factories */
121 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
124 if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
127 fact = GST_ELEMENT_FACTORY_CAST (feature);
129 klass = gst_element_factory_get_klass (fact);
131 if (strstr (klass, "Decoder"))
132 list = &data->decode;
133 else if (strstr (klass, "Demux"))
135 else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
137 else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
138 list = &data->payload;
141 GST_DEBUG ("adding %s", GST_PLUGIN_FEATURE_NAME (fact));
142 *list = g_list_prepend (*list, fact);
149 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
151 FilterData data = { NULL, NULL, NULL };
153 factory->uri = g_strdup (DEFAULT_URI);
154 /* get the feature list using the filter */
155 gst_default_registry_feature_filter ((GstPluginFeatureFilter)
156 payloader_filter, FALSE, &data);
159 g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
160 factory->payloaders =
161 g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
163 g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
165 factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
166 factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
170 gst_rtsp_media_factory_uri_finalize (GObject * obj)
172 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
174 g_free (factory->uri);
175 gst_plugin_feature_list_free (factory->demuxers);
176 gst_plugin_feature_list_free (factory->payloaders);
177 gst_plugin_feature_list_free (factory->decoders);
178 gst_caps_unref (factory->raw_vcaps);
179 gst_caps_unref (factory->raw_acaps);
181 G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
185 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
186 GValue * value, GParamSpec * pspec)
188 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
192 g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
200 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
201 const GValue * value, GParamSpec * pspec)
203 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
207 gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
215 * gst_rtsp_media_factory_uri_new:
217 * Create a new #GstRTSPMediaFactoryURI instance.
219 * Returns: a new #GstRTSPMediaFactoryURI object.
221 GstRTSPMediaFactoryURI *
222 gst_rtsp_media_factory_uri_new (void)
224 GstRTSPMediaFactoryURI *result;
226 result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
232 * gst_rtsp_media_factory_uri_set_uri:
233 * @factory: a #GstRTSPMediaFactory
234 * @uri: the uri the stream
236 * Set the URI of the resource that will be streamed by this factory.
239 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
242 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
243 g_return_if_fail (uri != NULL);
245 GST_RTSP_MEDIA_FACTORY_LOCK (factory);
246 g_free (factory->uri);
247 factory->uri = g_strdup (uri);
248 GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
252 * gst_rtsp_media_factory_uri_get_uri:
253 * @factory: a #GstRTSPMediaFactory
255 * Get the URI that will provide media for this factory.
257 * Returns: the configured URI. g_free() after usage.
260 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
264 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
266 GST_RTSP_MEDIA_FACTORY_LOCK (factory);
267 result = g_strdup (factory->uri);
268 GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
273 static GstElementFactory *
274 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
277 GstElementFactory *factory = NULL;
279 /* first find a demuxer that can link */
280 list = gst_element_factory_list_filter (urifact->demuxers, caps,
281 GST_PAD_SINK, FALSE);
284 /* we have a demuxer, try that one first */
285 gst_plugin_feature_list_free (list);
289 /* no demuxer try a depayloader */
290 list = gst_element_factory_list_filter (urifact->payloaders, caps,
291 GST_PAD_SINK, FALSE);
294 /* no depayloader, try a decoder */
295 list = gst_element_factory_list_filter (urifact->decoders, caps,
296 GST_PAD_SINK, FALSE);
299 /* we have a decoder, try that one first */
300 gst_plugin_feature_list_free (list);
306 factory = GST_ELEMENT_FACTORY_CAST (list->data);
307 g_object_ref (factory);
308 gst_plugin_feature_list_free (list);
314 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
315 GstElement * element)
319 GstElementFactory *factory;
322 GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
323 GST_DEBUG_PAD_NAME (pad), caps);
325 data = g_object_get_data (G_OBJECT (element), factory_key);
327 if (!(factory = find_payloader (data->factory, caps)))
330 /* we found a payloader, stop autoplugging so we can plug the
332 GST_DEBUG ("found factory %s",
333 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
334 gst_object_unref (factory);
341 /* no payloader, continue autoplugging */
342 GST_DEBUG ("no payloader found");
348 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
350 GstRTSPMediaFactoryURI *urifact;
352 GstElementFactory *factory;
353 GstElement *payloader;
355 GstPad *sinkpad, *srcpad, *ghostpad;
359 GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
361 /* link the element now and expose the pad */
362 data = g_object_get_data (G_OBJECT (element), factory_key);
363 urifact = data->factory;
365 /* ref to make refcounting easier later */
366 gst_object_ref (pad);
367 padname = gst_pad_get_name (pad);
369 /* get pad caps first, then call get_caps, then fail */
370 if ((caps = GST_PAD_CAPS (pad)))
372 else if ((caps = gst_pad_get_caps (pad)) == NULL)
375 /* check for raw caps */
376 if (gst_caps_can_intersect (caps, urifact->raw_vcaps)) {
377 /* we have raw video caps, insert converter */
378 convert = gst_element_factory_make ("ffmpegcolorspace", NULL);
379 } else if (gst_caps_can_intersect (caps, urifact->raw_acaps)) {
380 /* we have raw audio caps, insert converter */
381 convert = gst_element_factory_make ("audioconvert", NULL);
387 gst_bin_add (GST_BIN_CAST (element), convert);
388 gst_element_set_state (convert, GST_STATE_PLAYING);
390 sinkpad = gst_element_get_static_pad (convert, "sink");
391 gst_pad_link (pad, sinkpad);
392 gst_object_unref (sinkpad);
394 /* unref old pad, we reffed before */
395 gst_object_unref (pad);
397 /* continue with new pad and caps */
398 pad = gst_element_get_static_pad (convert, "src");
399 if ((caps = GST_PAD_CAPS (pad)))
401 else if ((caps = gst_pad_get_caps (pad)) == NULL)
405 if (!(factory = find_payloader (urifact, caps)))
408 gst_caps_unref (caps);
410 /* we have a payloader now */
411 GST_DEBUG ("found payloader factory %s",
412 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
414 payloader = gst_element_factory_create (factory, NULL);
415 if (payloader == NULL)
418 g_object_set (payloader, "pt", data->pt, NULL);
421 /* add the payloader to the pipeline */
422 gst_bin_add (GST_BIN_CAST (element), payloader);
423 gst_element_set_state (payloader, GST_STATE_PLAYING);
425 /* link the pad to the sinkpad of the payloader */
426 sinkpad = gst_element_get_static_pad (payloader, "sink");
427 gst_pad_link (pad, sinkpad);
428 gst_object_unref (sinkpad);
429 gst_object_unref (pad);
431 /* now expose the srcpad of the payloader as a ghostpad with the same name
432 * as the uridecodebin pad name. */
433 srcpad = gst_element_get_static_pad (payloader, "src");
434 ghostpad = gst_ghost_pad_new (padname, srcpad);
435 gst_object_unref (srcpad);
438 gst_pad_set_active (ghostpad, TRUE);
439 gst_element_add_pad (element, ghostpad);
446 GST_WARNING ("could not get caps from pad");
448 gst_object_unref (pad);
453 GST_DEBUG ("no payloader found");
455 gst_caps_unref (caps);
456 gst_object_unref (pad);
461 GST_ERROR ("could not create payloader from factory");
463 gst_caps_unref (caps);
464 gst_object_unref (pad);
470 no_more_pads_cb (GstElement * uribin, GstElement * element)
472 GST_DEBUG ("no-more-pads");
473 gst_element_no_more_pads (element);
477 rtsp_media_factory_uri_get_element (GstRTSPMediaFactory * factory,
478 const GstRTSPUrl * url)
480 GstElement *topbin, *element, *uribin;
481 GstRTSPMediaFactoryURI *urifact;
484 urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
486 GST_LOG ("creating element");
488 topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
489 g_assert (topbin != NULL);
491 /* our bin will dynamically expose payloaded pads */
492 element = gst_bin_new ("dynpay0");
493 g_assert (element != NULL);
495 uribin = gst_element_factory_make ("uridecodebin", "uribin");
497 goto no_uridecodebin;
499 g_object_set (uribin, "uri", urifact->uri, NULL);
501 /* keep factory data around */
502 data = g_new0 (FactoryData, 1);
503 data->factory = g_object_ref (factory);
506 g_object_set_data_full (G_OBJECT (element), factory_key,
507 data, (GDestroyNotify) free_data);
509 /* connect to the signals */
510 g_signal_connect (uribin, "autoplug-continue",
511 (GCallback) autoplug_continue_cb, element);
512 g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
513 g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
516 gst_bin_add (GST_BIN_CAST (element), uribin);
517 gst_bin_add (GST_BIN_CAST (topbin), element);
523 g_critical ("can't create uridecodebin element");
524 g_object_unref (element);