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