Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-media-factory-uri.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
21
22 #include "rtsp-media-factory-uri.h"
23
24 #define DEFAULT_URI         NULL
25 #define DEFAULT_USE_GSTPAY  FALSE
26
27 enum
28 {
29   PROP_0,
30   PROP_URI,
31   PROP_USE_GSTPAY,
32   PROP_LAST
33 };
34
35
36 #define RAW_VIDEO_CAPS \
37     "video/x-raw"
38
39 #define RAW_AUDIO_CAPS \
40     "audio/x-raw"
41
42 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS (RAW_VIDEO_CAPS);
43 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS (RAW_AUDIO_CAPS);
44
45 typedef struct
46 {
47   GstRTSPMediaFactoryURI *factory;
48   guint pt;
49 } FactoryData;
50
51 static void
52 free_data (FactoryData * data)
53 {
54   g_object_unref (data->factory);
55   g_free (data);
56 }
57
58 static const gchar *factory_key = "GstRTSPMediaFactoryURI";
59
60 GST_DEBUG_CATEGORY_STATIC (rtsp_media_factory_uri_debug);
61 #define GST_CAT_DEFAULT rtsp_media_factory_uri_debug
62
63 static void gst_rtsp_media_factory_uri_get_property (GObject * object,
64     guint propid, GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_media_factory_uri_set_property (GObject * object,
66     guint propid, const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_media_factory_uri_finalize (GObject * obj);
68
69 static GstElement *rtsp_media_factory_uri_get_element (GstRTSPMediaFactory *
70     factory, const GstRTSPUrl * url);
71
72 G_DEFINE_TYPE (GstRTSPMediaFactoryURI, gst_rtsp_media_factory_uri,
73     GST_TYPE_RTSP_MEDIA_FACTORY);
74
75 static void
76 gst_rtsp_media_factory_uri_class_init (GstRTSPMediaFactoryURIClass * klass)
77 {
78   GObjectClass *gobject_class;
79   GstRTSPMediaFactoryClass *mediafactory_class;
80
81   gobject_class = G_OBJECT_CLASS (klass);
82   mediafactory_class = GST_RTSP_MEDIA_FACTORY_CLASS (klass);
83
84   gobject_class->get_property = gst_rtsp_media_factory_uri_get_property;
85   gobject_class->set_property = gst_rtsp_media_factory_uri_set_property;
86   gobject_class->finalize = gst_rtsp_media_factory_uri_finalize;
87
88   /**
89    * GstRTSPMediaFactoryURI::uri
90    *
91    * The uri of the resource that will be served by this factory.
92    */
93   g_object_class_install_property (gobject_class, PROP_URI,
94       g_param_spec_string ("uri", "URI",
95           "The URI of the resource to stream", DEFAULT_URI,
96           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
97   /**
98    * GstRTSPMediaFactoryURI::use-gstpay
99    *
100    * Allow the usage of gstpay in order to avoid decoding of compressed formats
101    * without a payloader.
102    */
103   g_object_class_install_property (gobject_class, PROP_USE_GSTPAY,
104       g_param_spec_boolean ("use-gstpay", "Use gstpay",
105           "Use the gstpay payloader to avoid decoding", DEFAULT_USE_GSTPAY,
106           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107
108   mediafactory_class->get_element = rtsp_media_factory_uri_get_element;
109
110   GST_DEBUG_CATEGORY_INIT (rtsp_media_factory_uri_debug, "rtspmediafactoryuri",
111       0, "GstRTSPMediaFactoryUri");
112 }
113
114 typedef struct
115 {
116   GList *demux;
117   GList *payload;
118   GList *decode;
119 } FilterData;
120
121 static gboolean
122 payloader_filter (GstPluginFeature * feature, FilterData * data)
123 {
124   gboolean res;
125   const gchar *klass;
126   GstElementFactory *fact;
127   GList **list = NULL;
128
129   /* we only care about element factories */
130   if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
131     return FALSE;
132
133   if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
134     return FALSE;
135
136   fact = GST_ELEMENT_FACTORY_CAST (feature);
137
138   klass = gst_element_factory_get_klass (fact);
139
140   if (strstr (klass, "Decoder"))
141     list = &data->decode;
142   else if (strstr (klass, "Demux"))
143     list = &data->demux;
144   else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
145     list = &data->demux;
146   else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
147     list = &data->payload;
148
149   if (list) {
150     GST_DEBUG ("adding %s", GST_OBJECT_NAME (fact));
151     *list = g_list_prepend (*list, fact);
152   }
153
154   return FALSE;
155 }
156
157 static void
158 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
159 {
160   FilterData data = { NULL, NULL, NULL };
161
162   factory->uri = g_strdup (DEFAULT_URI);
163   factory->use_gstpay = DEFAULT_USE_GSTPAY;
164
165   /* get the feature list using the filter */
166   gst_registry_feature_filter (gst_registry_get (), (GstPluginFeatureFilter)
167       payloader_filter, FALSE, &data);
168   /* sort */
169   factory->demuxers =
170       g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
171   factory->payloaders =
172       g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
173   factory->decoders =
174       g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
175
176   factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
177   factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
178 }
179
180 static void
181 gst_rtsp_media_factory_uri_finalize (GObject * obj)
182 {
183   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
184
185   g_free (factory->uri);
186   gst_plugin_feature_list_free (factory->demuxers);
187   gst_plugin_feature_list_free (factory->payloaders);
188   gst_plugin_feature_list_free (factory->decoders);
189   gst_caps_unref (factory->raw_vcaps);
190   gst_caps_unref (factory->raw_acaps);
191
192   G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
193 }
194
195 static void
196 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
197     GValue * value, GParamSpec * pspec)
198 {
199   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
200
201   switch (propid) {
202     case PROP_URI:
203       g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
204       break;
205     case PROP_USE_GSTPAY:
206       g_value_set_boolean (value, factory->use_gstpay);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
210   }
211 }
212
213 static void
214 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
215     const GValue * value, GParamSpec * pspec)
216 {
217   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
218
219   switch (propid) {
220     case PROP_URI:
221       gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
222       break;
223     case PROP_USE_GSTPAY:
224       factory->use_gstpay = g_value_get_boolean (value);
225       break;
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
228   }
229 }
230
231 /**
232  * gst_rtsp_media_factory_uri_new:
233  *
234  * Create a new #GstRTSPMediaFactoryURI instance.
235  *
236  * Returns: a new #GstRTSPMediaFactoryURI object.
237  */
238 GstRTSPMediaFactoryURI *
239 gst_rtsp_media_factory_uri_new (void)
240 {
241   GstRTSPMediaFactoryURI *result;
242
243   result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
244
245   return result;
246 }
247
248 /**
249  * gst_rtsp_media_factory_uri_set_uri:
250  * @factory: a #GstRTSPMediaFactory
251  * @uri: the uri the stream
252  *
253  * Set the URI of the resource that will be streamed by this factory.
254  */
255 void
256 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
257     const gchar * uri)
258 {
259   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
260   g_return_if_fail (uri != NULL);
261
262   GST_RTSP_MEDIA_FACTORY_LOCK (factory);
263   g_free (factory->uri);
264   factory->uri = g_strdup (uri);
265   GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
266 }
267
268 /**
269  * gst_rtsp_media_factory_uri_get_uri:
270  * @factory: a #GstRTSPMediaFactory
271  *
272  * Get the URI that will provide media for this factory.
273  *
274  * Returns: the configured URI. g_free() after usage.
275  */
276 gchar *
277 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
278 {
279   gchar *result;
280
281   g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
282
283   GST_RTSP_MEDIA_FACTORY_LOCK (factory);
284   result = g_strdup (factory->uri);
285   GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
286
287   return result;
288 }
289
290 static GstElementFactory *
291 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
292 {
293   GList *list, *tmp;
294   GstElementFactory *factory = NULL;
295
296   /* first find a demuxer that can link */
297   list = gst_element_factory_list_filter (urifact->demuxers, caps,
298       GST_PAD_SINK, FALSE);
299
300   if (list != NULL) {
301     /* we have a demuxer, try that one first */
302     gst_plugin_feature_list_free (list);
303     return NULL;
304   }
305
306   /* no demuxer try a depayloader */
307   list = gst_element_factory_list_filter (urifact->payloaders, caps,
308       GST_PAD_SINK, FALSE);
309
310   if (list == NULL) {
311     if (urifact->use_gstpay) {
312       /* no depayloader or parser/demuxer, use gstpay when allowed */
313       factory = gst_element_factory_find ("rtpgstpay");
314     } else {
315       /* no depayloader, try a decoder, we'll get to a payloader for a decoded
316        * video or audio format, worst case. */
317       list = gst_element_factory_list_filter (urifact->decoders, caps,
318           GST_PAD_SINK, FALSE);
319
320       if (list != NULL) {
321         /* we have a decoder, try that one first */
322         gst_plugin_feature_list_free (list);
323         return NULL;
324       }
325     }
326   }
327
328   if (list != NULL) {
329     factory = GST_ELEMENT_FACTORY_CAST (list->data);
330     g_object_ref (factory);
331     gst_plugin_feature_list_free (list);
332   }
333   return factory;
334 }
335
336 static gboolean
337 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
338     GstElement * element)
339 {
340   GList *list, *tmp;
341   FactoryData *data;
342   GstElementFactory *factory;
343   gboolean res;
344
345   GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
346       GST_DEBUG_PAD_NAME (pad), caps);
347
348   data = g_object_get_data (G_OBJECT (element), factory_key);
349
350   if (!(factory = find_payloader (data->factory, caps)))
351     goto no_factory;
352
353   /* we found a payloader, stop autoplugging so we can plug the
354    * payloader. */
355   GST_DEBUG ("found factory %s",
356       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
357   gst_object_unref (factory);
358
359   return FALSE;
360
361   /* ERRORS */
362 no_factory:
363   {
364     /* no payloader, continue autoplugging */
365     GST_DEBUG ("no payloader found");
366     return TRUE;
367   }
368 }
369
370 static void
371 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
372 {
373   GstRTSPMediaFactoryURI *urifact;
374   FactoryData *data;
375   GstElementFactory *factory;
376   GstElement *payloader;
377   GstCaps *caps;
378   GstPad *sinkpad, *srcpad, *ghostpad;
379   GstElement *convert;
380   gchar *padname;
381
382   GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
383
384   /* link the element now and expose the pad */
385   data = g_object_get_data (G_OBJECT (element), factory_key);
386   urifact = data->factory;
387
388   /* ref to make refcounting easier later */
389   gst_object_ref (pad);
390   padname = gst_pad_get_name (pad);
391
392   /* get pad caps first, then call get_caps, then fail */
393   if ((caps = gst_pad_get_current_caps (pad)) == NULL)
394     if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
395       goto no_caps;
396
397   /* check for raw caps */
398   if (gst_caps_can_intersect (caps, urifact->raw_vcaps)) {
399     /* we have raw video caps, insert converter */
400     convert = gst_element_factory_make ("videoconvert", NULL);
401   } else if (gst_caps_can_intersect (caps, urifact->raw_acaps)) {
402     /* we have raw audio caps, insert converter */
403     convert = gst_element_factory_make ("audioconvert", NULL);
404   } else {
405     convert = NULL;
406   }
407
408   if (convert) {
409     gst_bin_add (GST_BIN_CAST (element), convert);
410     gst_element_set_state (convert, GST_STATE_PLAYING);
411
412     sinkpad = gst_element_get_static_pad (convert, "sink");
413     gst_pad_link (pad, sinkpad);
414     gst_object_unref (sinkpad);
415
416     /* unref old pad, we reffed before */
417     gst_object_unref (pad);
418
419     /* continue with new pad and caps */
420     pad = gst_element_get_static_pad (convert, "src");
421     if ((caps = gst_pad_get_current_caps (pad)) == NULL)
422       if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
423         goto no_caps;
424   }
425
426   if (!(factory = find_payloader (urifact, caps)))
427     goto no_factory;
428
429   gst_caps_unref (caps);
430
431   /* we have a payloader now */
432   GST_DEBUG ("found payloader factory %s",
433       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
434
435   payloader = gst_element_factory_create (factory, NULL);
436   if (payloader == NULL)
437     goto no_payloader;
438
439   g_object_set (payloader, "pt", data->pt, NULL);
440   data->pt++;
441
442   if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
443           "buffer-list"))
444     g_object_set (payloader, "buffer-list", TRUE, NULL);
445
446   /* add the payloader to the pipeline */
447   gst_bin_add (GST_BIN_CAST (element), payloader);
448   gst_element_set_state (payloader, GST_STATE_PLAYING);
449
450   /* link the pad to the sinkpad of the payloader */
451   sinkpad = gst_element_get_static_pad (payloader, "sink");
452   gst_pad_link (pad, sinkpad);
453   gst_object_unref (sinkpad);
454   gst_object_unref (pad);
455
456   /* now expose the srcpad of the payloader as a ghostpad with the same name
457    * as the uridecodebin pad name. */
458   srcpad = gst_element_get_static_pad (payloader, "src");
459   ghostpad = gst_ghost_pad_new (padname, srcpad);
460   gst_object_unref (srcpad);
461   g_free (padname);
462
463   gst_pad_set_active (ghostpad, TRUE);
464   gst_element_add_pad (element, ghostpad);
465
466   return;
467
468   /* ERRORS */
469 no_caps:
470   {
471     GST_WARNING ("could not get caps from pad");
472     g_free (padname);
473     gst_object_unref (pad);
474     return;
475   }
476 no_factory:
477   {
478     GST_DEBUG ("no payloader found");
479     g_free (padname);
480     gst_caps_unref (caps);
481     gst_object_unref (pad);
482     return;
483   }
484 no_payloader:
485   {
486     GST_ERROR ("could not create payloader from factory");
487     g_free (padname);
488     gst_caps_unref (caps);
489     gst_object_unref (pad);
490     return;
491   }
492 }
493
494 static void
495 no_more_pads_cb (GstElement * uribin, GstElement * element)
496 {
497   GST_DEBUG ("no-more-pads");
498   gst_element_no_more_pads (element);
499 }
500
501 static GstElement *
502 rtsp_media_factory_uri_get_element (GstRTSPMediaFactory * factory,
503     const GstRTSPUrl * url)
504 {
505   GstElement *topbin, *element, *uribin;
506   GstRTSPMediaFactoryURI *urifact;
507   FactoryData *data;
508
509   urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
510
511   GST_LOG ("creating element");
512
513   topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
514   g_assert (topbin != NULL);
515
516   /* our bin will dynamically expose payloaded pads */
517   element = gst_bin_new ("dynpay0");
518   g_assert (element != NULL);
519
520   uribin = gst_element_factory_make ("uridecodebin", "uribin");
521   if (uribin == NULL)
522     goto no_uridecodebin;
523
524   g_object_set (uribin, "uri", urifact->uri, NULL);
525
526   /* keep factory data around */
527   data = g_new0 (FactoryData, 1);
528   data->factory = g_object_ref (factory);
529   data->pt = 96;
530
531   g_object_set_data_full (G_OBJECT (element), factory_key,
532       data, (GDestroyNotify) free_data);
533
534   /* connect to the signals */
535   g_signal_connect (uribin, "autoplug-continue",
536       (GCallback) autoplug_continue_cb, element);
537   g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
538   g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
539       element);
540
541   gst_bin_add (GST_BIN_CAST (element), uribin);
542   gst_bin_add (GST_BIN_CAST (topbin), element);
543
544   return topbin;
545
546 no_uridecodebin:
547   {
548     g_critical ("can't create uridecodebin element");
549     g_object_unref (element);
550     return NULL;
551   }
552 }