factory-uri: rework the autoplugger.
[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
26 enum
27 {
28   PROP_0,
29   PROP_URI,
30   PROP_LAST
31 };
32
33
34 #define RAW_VIDEO_CAPS \
35     "video/x-raw-yuv; " \
36     "video/x-raw-rgb; " \
37     "video/x-raw-gray"
38
39 #define RAW_AUDIO_CAPS \
40     "audio/x-raw-int; " \
41     "audio/x-raw-float"
42
43 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS (RAW_VIDEO_CAPS);
44 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS (RAW_AUDIO_CAPS);
45
46 typedef struct
47 {
48   GstRTSPMediaFactoryURI *factory;
49   guint pt;
50 } FactoryData;
51
52 static void
53 free_data (FactoryData * data)
54 {
55   g_object_unref (data->factory);
56   g_free (data);
57 }
58
59 static const gchar *factory_key = "GstRTSPMediaFactoryURI";
60
61 GST_DEBUG_CATEGORY (rtsp_media_factory_uri_debug);
62 #define GST_CAT_DEFAULT rtsp_media_factory_uri_debug
63
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);
69
70 static GstElement *rtsp_media_factory_uri_get_element (GstRTSPMediaFactory *
71     factory, const GstRTSPUrl * url);
72
73 G_DEFINE_TYPE (GstRTSPMediaFactoryURI, gst_rtsp_media_factory_uri,
74     GST_TYPE_RTSP_MEDIA_FACTORY);
75
76 static void
77 gst_rtsp_media_factory_uri_class_init (GstRTSPMediaFactoryURIClass * klass)
78 {
79   GObjectClass *gobject_class;
80   GstRTSPMediaFactoryClass *mediafactory_class;
81
82   gobject_class = G_OBJECT_CLASS (klass);
83   mediafactory_class = GST_RTSP_MEDIA_FACTORY_CLASS (klass);
84
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;
88
89   /**
90    * GstRTSPMediaFactoryURI::uri
91    *
92    * The uri of the resource that will be served by this factory.
93    */
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));
98
99   mediafactory_class->get_element = rtsp_media_factory_uri_get_element;
100
101   GST_DEBUG_CATEGORY_INIT (rtsp_media_factory_uri_debug, "rtspmediafactoryuri",
102       0, "GstRTSPMediaFactoryUri");
103 }
104
105 typedef struct
106 {
107   GList *demux;
108   GList *payload;
109   GList *decode;
110 } FilterData;
111
112 static gboolean
113 payloader_filter (GstPluginFeature * feature, FilterData * data)
114 {
115   gboolean res;
116   const gchar *klass;
117   GstElementFactory *fact;
118   GList **list = NULL;
119
120   /* we only care about element factories */
121   if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
122     return FALSE;
123
124   if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
125     return FALSE;
126
127   fact = GST_ELEMENT_FACTORY_CAST (feature);
128
129   klass = gst_element_factory_get_klass (fact);
130
131   if (strstr (klass, "Decoder"))
132     list = &data->decode;
133   else if (strstr (klass, "Demux"))
134     list = &data->demux;
135   else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
136     list = &data->demux;
137   else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
138     list = &data->payload;
139
140   if (list) {
141     GST_DEBUG ("adding %s", GST_PLUGIN_FEATURE_NAME (fact));
142     *list = g_list_prepend (*list, fact);
143   }
144
145   return FALSE;
146 }
147
148 static void
149 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
150 {
151   FilterData data = { NULL, NULL, NULL };
152
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);
157   /* sort */
158   factory->demuxers =
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);
162   factory->decoders =
163       g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
164
165   factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
166   factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
167 }
168
169 static void
170 gst_rtsp_media_factory_uri_finalize (GObject * obj)
171 {
172   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
173
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);
180
181   G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
182 }
183
184 static void
185 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
186     GValue * value, GParamSpec * pspec)
187 {
188   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
189
190   switch (propid) {
191     case PROP_URI:
192       g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
193       break;
194     default:
195       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
196   }
197 }
198
199 static void
200 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
201     const GValue * value, GParamSpec * pspec)
202 {
203   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
204
205   switch (propid) {
206     case PROP_URI:
207       gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
208       break;
209     default:
210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
211   }
212 }
213
214 /**
215  * gst_rtsp_media_factory_uri_new:
216  *
217  * Create a new #GstRTSPMediaFactoryURI instance.
218  *
219  * Returns: a new #GstRTSPMediaFactoryURI object.
220  */
221 GstRTSPMediaFactoryURI *
222 gst_rtsp_media_factory_uri_new (void)
223 {
224   GstRTSPMediaFactoryURI *result;
225
226   result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
227
228   return result;
229 }
230
231 /**
232  * gst_rtsp_media_factory_uri_set_uri:
233  * @factory: a #GstRTSPMediaFactory
234  * @uri: the uri the stream
235  *
236  * Set the URI of the resource that will be streamed by this factory.
237  */
238 void
239 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
240     const gchar * uri)
241 {
242   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
243   g_return_if_fail (uri != NULL);
244
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);
249 }
250
251 /**
252  * gst_rtsp_media_factory_uri_get_uri:
253  * @factory: a #GstRTSPMediaFactory
254  *
255  * Get the URI that will provide media for this factory.
256  *
257  * Returns: the configured URI. g_free() after usage.
258  */
259 gchar *
260 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
261 {
262   gchar *result;
263
264   g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
265
266   GST_RTSP_MEDIA_FACTORY_LOCK (factory);
267   result = g_strdup (factory->uri);
268   GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
269
270   return result;
271 }
272
273 static GstElementFactory *
274 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
275 {
276   GList *list, *tmp;
277   GstElementFactory *factory = NULL;
278
279   /* first find a demuxer that can link */
280   list = gst_element_factory_list_filter (urifact->demuxers, caps,
281       GST_PAD_SINK, FALSE);
282
283   if (list != NULL) {
284     /* we have a demuxer, try that one first */
285     gst_plugin_feature_list_free (list);
286     return NULL;
287   }
288
289   /* no demuxer try a depayloader */
290   list = gst_element_factory_list_filter (urifact->payloaders, caps,
291       GST_PAD_SINK, FALSE);
292
293   if (list == NULL) {
294     /* no depayloader, try a decoder */
295     list = gst_element_factory_list_filter (urifact->decoders, caps,
296         GST_PAD_SINK, FALSE);
297
298     if (list != NULL) {
299       /* we have a decoder, try that one first */
300       gst_plugin_feature_list_free (list);
301       return NULL;
302     }
303   }
304
305   if (list != NULL) {
306     factory = GST_ELEMENT_FACTORY_CAST (list->data);
307     g_object_ref (factory);
308     gst_plugin_feature_list_free (list);
309   }
310   return factory;
311 }
312
313 static gboolean
314 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
315     GstElement * element)
316 {
317   GList *list, *tmp;
318   FactoryData *data;
319   GstElementFactory *factory;
320   gboolean res;
321
322   GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
323       GST_DEBUG_PAD_NAME (pad), caps);
324
325   data = g_object_get_data (G_OBJECT (element), factory_key);
326
327   if (!(factory = find_payloader (data->factory, caps)))
328     goto no_factory;
329
330   /* we found a payloader, stop autoplugging so we can plug the
331    * payloader. */
332   GST_DEBUG ("found factory %s",
333       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
334   gst_object_unref (factory);
335
336   return FALSE;
337
338   /* ERRORS */
339 no_factory:
340   {
341     /* no payloader, continue autoplugging */
342     GST_DEBUG ("no payloader found");
343     return TRUE;
344   }
345 }
346
347 static void
348 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
349 {
350   GstRTSPMediaFactoryURI *urifact;
351   FactoryData *data;
352   GstElementFactory *factory;
353   GstElement *payloader;
354   GstCaps *caps;
355   GstPad *sinkpad, *srcpad, *ghostpad;
356   GstElement *convert;
357   gchar *padname;
358
359   GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
360
361   /* link the element now and expose the pad */
362   data = g_object_get_data (G_OBJECT (element), factory_key);
363   urifact = data->factory;
364
365   /* ref to make refcounting easier later */
366   gst_object_ref (pad);
367   padname = gst_pad_get_name (pad);
368
369   /* get pad caps first, then call get_caps, then fail */
370   if ((caps = GST_PAD_CAPS (pad)))
371     gst_caps_ref (caps);
372   else if ((caps = gst_pad_get_caps (pad)) == NULL)
373     goto no_caps;
374
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);
382   } else {
383     convert = NULL;
384   }
385
386   if (convert) {
387     gst_bin_add (GST_BIN_CAST (element), convert);
388     gst_element_set_state (convert, GST_STATE_PLAYING);
389
390     sinkpad = gst_element_get_static_pad (convert, "sink");
391     gst_pad_link (pad, sinkpad);
392     gst_object_unref (sinkpad);
393
394     /* unref old pad, we reffed before */
395     gst_object_unref (pad);
396
397     /* continue with new pad and caps */
398     pad = gst_element_get_static_pad (convert, "src");
399     if ((caps = GST_PAD_CAPS (pad)))
400       gst_caps_ref (caps);
401     else if ((caps = gst_pad_get_caps (pad)) == NULL)
402       goto no_caps;
403   }
404
405   if (!(factory = find_payloader (urifact, caps)))
406     goto no_factory;
407
408   gst_caps_unref (caps);
409
410   /* we have a payloader now */
411   GST_DEBUG ("found payloader factory %s",
412       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
413
414   payloader = gst_element_factory_create (factory, NULL);
415   if (payloader == NULL)
416     goto no_payloader;
417
418   g_object_set (payloader, "pt", data->pt, NULL);
419   data->pt++;
420
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);
424
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);
430
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);
436   g_free (padname);
437
438   gst_pad_set_active (ghostpad, TRUE);
439   gst_element_add_pad (element, ghostpad);
440
441   return;
442
443   /* ERRORS */
444 no_caps:
445   {
446     GST_WARNING ("could not get caps from pad");
447     g_free (padname);
448     gst_object_unref (pad);
449     return;
450   }
451 no_factory:
452   {
453     GST_DEBUG ("no payloader found");
454     g_free (padname);
455     gst_caps_unref (caps);
456     gst_object_unref (pad);
457     return;
458   }
459 no_payloader:
460   {
461     GST_ERROR ("could not create payloader from factory");
462     g_free (padname);
463     gst_caps_unref (caps);
464     gst_object_unref (pad);
465     return;
466   }
467 }
468
469 static void
470 no_more_pads_cb (GstElement * uribin, GstElement * element)
471 {
472   GST_DEBUG ("no-more-pads");
473   gst_element_no_more_pads (element);
474 }
475
476 static GstElement *
477 rtsp_media_factory_uri_get_element (GstRTSPMediaFactory * factory,
478     const GstRTSPUrl * url)
479 {
480   GstElement *topbin, *element, *uribin;
481   GstRTSPMediaFactoryURI *urifact;
482   FactoryData *data;
483
484   urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
485
486   GST_LOG ("creating element");
487
488   topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
489   g_assert (topbin != NULL);
490
491   /* our bin will dynamically expose payloaded pads */
492   element = gst_bin_new ("dynpay0");
493   g_assert (element != NULL);
494
495   uribin = gst_element_factory_make ("uridecodebin", "uribin");
496   if (uribin == NULL)
497     goto no_uridecodebin;
498
499   g_object_set (uribin, "uri", urifact->uri, NULL);
500
501   /* keep factory data around */
502   data = g_new0 (FactoryData, 1);
503   data->factory = g_object_ref (factory);
504   data->pt = 96;
505
506   g_object_set_data_full (G_OBJECT (element), factory_key,
507       data, (GDestroyNotify) free_data);
508
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,
514       element);
515
516   gst_bin_add (GST_BIN_CAST (element), uribin);
517   gst_bin_add (GST_BIN_CAST (topbin), element);
518
519   return topbin;
520
521 no_uridecodebin:
522   {
523     g_critical ("can't create uridecodebin element");
524     g_object_unref (element);
525     return NULL;
526   }
527 }