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");
106 payloader_filter (GstPluginFeature * feature, gpointer data)
111 /* we only care about element factories */
112 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
115 if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
118 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY_CAST (feature));
120 if (strstr (klass, "Payloader") == NULL)
123 if (strstr (klass, "RTP") == NULL)
130 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
132 factory->uri = g_strdup (DEFAULT_URI);
133 /* get the feature list using the filter */
135 gst_default_registry_feature_filter ((GstPluginFeatureFilter)
136 payloader_filter, FALSE, NULL);
137 /* sort on rank and name */
139 g_list_sort (factory->factories, gst_plugin_feature_rank_compare_func);
141 factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
142 factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
146 gst_rtsp_media_factory_uri_finalize (GObject * obj)
148 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
150 g_free (factory->uri);
151 gst_plugin_feature_list_free (factory->factories);
152 gst_caps_unref (factory->raw_vcaps);
153 gst_caps_unref (factory->raw_acaps);
155 G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
159 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
160 GValue * value, GParamSpec * pspec)
162 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
166 g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
174 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
175 const GValue * value, GParamSpec * pspec)
177 GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
181 gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
189 * gst_rtsp_media_factory_uri_new:
191 * Create a new #GstRTSPMediaFactoryURI instance.
193 * Returns: a new #GstRTSPMediaFactoryURI object.
195 GstRTSPMediaFactoryURI *
196 gst_rtsp_media_factory_uri_new (void)
198 GstRTSPMediaFactoryURI *result;
200 result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
206 * gst_rtsp_media_factory_uri_set_uri:
207 * @factory: a #GstRTSPMediaFactory
208 * @uri: the uri the stream
210 * Set the URI of the resource that will be streamed by this factory.
213 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
216 g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
217 g_return_if_fail (uri != NULL);
219 GST_RTSP_MEDIA_FACTORY_LOCK (factory);
220 g_free (factory->uri);
221 factory->uri = g_strdup (uri);
222 GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
226 * gst_rtsp_media_factory_uri_get_uri:
227 * @factory: a #GstRTSPMediaFactory
229 * Get the URI that will provide media for this factory.
231 * Returns: the configured URI. g_free() after usage.
234 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
238 g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
240 GST_RTSP_MEDIA_FACTORY_LOCK (factory);
241 result = g_strdup (factory->uri);
242 GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
247 static GstElementFactory *
248 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
251 GstElementFactory *factory = NULL;
253 /* find payloader that can link */
254 list = gst_element_factory_list_filter (urifact->factories, caps,
255 GST_PAD_SINK, FALSE);
257 for (tmp = list; tmp; tmp = g_list_next (tmp)) {
258 GstElementFactory *f = GST_ELEMENT_FACTORY_CAST (tmp->data);
261 name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (f));
267 g_object_ref (factory);
269 gst_plugin_feature_list_free (list);
275 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
276 GstElement * element)
280 GstElementFactory *factory;
283 GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
284 GST_DEBUG_PAD_NAME (pad), caps);
286 data = g_object_get_data (G_OBJECT (element), factory_key);
288 if (!(factory = find_payloader (data->factory, caps)))
291 /* we found a payloader, stop autoplugging */
292 GST_DEBUG ("found payloader factory %s",
293 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
294 gst_object_unref (factory);
301 /* no payloader, continue autoplugging */
302 GST_DEBUG ("no payloader found");
308 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
310 GstRTSPMediaFactoryURI *urifact;
312 GstElementFactory *factory;
313 GstElement *payloader;
315 GstPad *sinkpad, *srcpad, *ghostpad;
319 GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
321 /* link the element now and expose the pad */
322 data = g_object_get_data (G_OBJECT (element), factory_key);
323 urifact = data->factory;
325 /* ref to make refcounting easier later */
326 gst_object_ref (pad);
327 padname = gst_pad_get_name (pad);
329 /* get pad caps first, then call get_caps, then fail */
330 if ((caps = GST_PAD_CAPS (pad)))
332 else if ((caps = gst_pad_get_caps (pad)) == NULL)
335 /* check for raw caps */
336 if (gst_caps_can_intersect (caps, urifact->raw_vcaps)) {
337 /* we have raw video caps, insert converter */
338 convert = gst_element_factory_make ("ffmpegcolorspace", NULL);
339 } else if (gst_caps_can_intersect (caps, urifact->raw_acaps)) {
340 /* we have raw audio caps, insert converter */
341 convert = gst_element_factory_make ("audioconvert", NULL);
347 gst_bin_add (GST_BIN_CAST (element), convert);
348 gst_element_set_state (convert, GST_STATE_PLAYING);
350 sinkpad = gst_element_get_static_pad (convert, "sink");
351 gst_pad_link (pad, sinkpad);
352 gst_object_unref (sinkpad);
354 /* unref old pad, we reffed before */
355 gst_object_unref (pad);
357 /* continue with new pad and caps */
358 pad = gst_element_get_static_pad (convert, "src");
359 if ((caps = GST_PAD_CAPS (pad)))
361 else if ((caps = gst_pad_get_caps (pad)) == NULL)
365 if (!(factory = find_payloader (urifact, caps)))
368 gst_caps_unref (caps);
370 /* we have a payloader now */
371 GST_DEBUG ("found payloader factory %s",
372 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
374 payloader = gst_element_factory_create (factory, NULL);
375 if (payloader == NULL)
378 g_object_set (payloader, "pt", data->pt, NULL);
381 /* add the payloader to the pipeline */
382 gst_bin_add (GST_BIN_CAST (element), payloader);
383 gst_element_set_state (payloader, GST_STATE_PLAYING);
385 /* link the pad to the sinkpad of the payloader */
386 sinkpad = gst_element_get_static_pad (payloader, "sink");
387 gst_pad_link (pad, sinkpad);
388 gst_object_unref (sinkpad);
389 gst_object_unref (pad);
391 /* now expose the srcpad of the payloader as a ghostpad with the same name
392 * as the uridecodebin pad name. */
393 srcpad = gst_element_get_static_pad (payloader, "src");
394 ghostpad = gst_ghost_pad_new (padname, srcpad);
395 gst_object_unref (srcpad);
398 gst_pad_set_active (ghostpad, TRUE);
399 gst_element_add_pad (element, ghostpad);
406 GST_WARNING ("could not get caps from pad");
408 gst_object_unref (pad);
413 GST_DEBUG ("no payloader found");
415 gst_caps_unref (caps);
416 gst_object_unref (pad);
421 GST_ERROR ("could not create payloader from factory");
423 gst_caps_unref (caps);
424 gst_object_unref (pad);
430 no_more_pads_cb (GstElement * uribin, GstElement * element)
432 GST_DEBUG ("no-more-pads");
433 gst_element_no_more_pads (element);
437 rtsp_media_factory_uri_get_element (GstRTSPMediaFactory * factory,
438 const GstRTSPUrl * url)
440 GstElement *topbin, *element, *uribin;
441 GstRTSPMediaFactoryURI *urifact;
444 urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
446 GST_LOG ("creating element");
448 topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
449 g_assert (topbin != NULL);
451 /* our bin will dynamically expose payloaded pads */
452 element = gst_bin_new ("dynpay0");
453 g_assert (element != NULL);
455 uribin = gst_element_factory_make ("uridecodebin", "uribin");
457 goto no_uridecodebin;
459 g_object_set (uribin, "uri", urifact->uri, NULL);
461 /* keep factory data around */
462 data = g_new0 (FactoryData, 1);
463 data->factory = g_object_ref (factory);
466 g_object_set_data_full (G_OBJECT (element), factory_key,
467 data, (GDestroyNotify) free_data);
469 /* connect to the signals */
470 g_signal_connect (uribin, "autoplug-continue",
471 (GCallback) autoplug_continue_cb, element);
472 g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
473 g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
476 gst_bin_add (GST_BIN_CAST (element), uribin);
477 gst_bin_add (GST_BIN_CAST (topbin), element);
483 g_critical ("can't create uridecodebin element");
484 g_object_unref (element);