dde816fb5f9c5aaa53e3ac91690714d5eb498fa6
[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_STATIC (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_get_current_caps (pad)) == NULL)
397     if ((caps = gst_pad_get_caps (pad, NULL)) == NULL)
398       goto no_caps;
399
400   /* check for raw caps */
401   if (gst_caps_can_intersect (caps, urifact->raw_vcaps)) {
402     /* we have raw video caps, insert converter */
403     convert = gst_element_factory_make ("ffmpegcolorspace", NULL);
404   } else if (gst_caps_can_intersect (caps, urifact->raw_acaps)) {
405     /* we have raw audio caps, insert converter */
406     convert = gst_element_factory_make ("audioconvert", NULL);
407   } else {
408     convert = NULL;
409   }
410
411   if (convert) {
412     gst_bin_add (GST_BIN_CAST (element), convert);
413     gst_element_set_state (convert, GST_STATE_PLAYING);
414
415     sinkpad = gst_element_get_static_pad (convert, "sink");
416     gst_pad_link (pad, sinkpad);
417     gst_object_unref (sinkpad);
418
419     /* unref old pad, we reffed before */
420     gst_object_unref (pad);
421
422     /* continue with new pad and caps */
423     pad = gst_element_get_static_pad (convert, "src");
424     if ((caps = gst_pad_get_current_caps (pad)) == NULL)
425       if ((caps = gst_pad_get_caps (pad, NULL)) == NULL)
426         goto no_caps;
427   }
428
429   if (!(factory = find_payloader (urifact, caps)))
430     goto no_factory;
431
432   gst_caps_unref (caps);
433
434   /* we have a payloader now */
435   GST_DEBUG ("found payloader factory %s",
436       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
437
438   payloader = gst_element_factory_create (factory, NULL);
439   if (payloader == NULL)
440     goto no_payloader;
441
442   g_object_set (payloader, "pt", data->pt, NULL);
443   data->pt++;
444
445   if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
446           "buffer-list"))
447     g_object_set (payloader, "buffer-list", TRUE, NULL);
448
449   /* add the payloader to the pipeline */
450   gst_bin_add (GST_BIN_CAST (element), payloader);
451   gst_element_set_state (payloader, GST_STATE_PLAYING);
452
453   /* link the pad to the sinkpad of the payloader */
454   sinkpad = gst_element_get_static_pad (payloader, "sink");
455   gst_pad_link (pad, sinkpad);
456   gst_object_unref (sinkpad);
457   gst_object_unref (pad);
458
459   /* now expose the srcpad of the payloader as a ghostpad with the same name
460    * as the uridecodebin pad name. */
461   srcpad = gst_element_get_static_pad (payloader, "src");
462   ghostpad = gst_ghost_pad_new (padname, srcpad);
463   gst_object_unref (srcpad);
464   g_free (padname);
465
466   gst_pad_set_active (ghostpad, TRUE);
467   gst_element_add_pad (element, ghostpad);
468
469   return;
470
471   /* ERRORS */
472 no_caps:
473   {
474     GST_WARNING ("could not get caps from pad");
475     g_free (padname);
476     gst_object_unref (pad);
477     return;
478   }
479 no_factory:
480   {
481     GST_DEBUG ("no payloader found");
482     g_free (padname);
483     gst_caps_unref (caps);
484     gst_object_unref (pad);
485     return;
486   }
487 no_payloader:
488   {
489     GST_ERROR ("could not create payloader from factory");
490     g_free (padname);
491     gst_caps_unref (caps);
492     gst_object_unref (pad);
493     return;
494   }
495 }
496
497 static void
498 no_more_pads_cb (GstElement * uribin, GstElement * element)
499 {
500   GST_DEBUG ("no-more-pads");
501   gst_element_no_more_pads (element);
502 }
503
504 static GstElement *
505 rtsp_media_factory_uri_get_element (GstRTSPMediaFactory * factory,
506     const GstRTSPUrl * url)
507 {
508   GstElement *topbin, *element, *uribin;
509   GstRTSPMediaFactoryURI *urifact;
510   FactoryData *data;
511
512   urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
513
514   GST_LOG ("creating element");
515
516   topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
517   g_assert (topbin != NULL);
518
519   /* our bin will dynamically expose payloaded pads */
520   element = gst_bin_new ("dynpay0");
521   g_assert (element != NULL);
522
523   uribin = gst_element_factory_make ("uridecodebin", "uribin");
524   if (uribin == NULL)
525     goto no_uridecodebin;
526
527   g_object_set (uribin, "uri", urifact->uri, NULL);
528
529   /* keep factory data around */
530   data = g_new0 (FactoryData, 1);
531   data->factory = g_object_ref (factory);
532   data->pt = 96;
533
534   g_object_set_data_full (G_OBJECT (element), factory_key,
535       data, (GDestroyNotify) free_data);
536
537   /* connect to the signals */
538   g_signal_connect (uribin, "autoplug-continue",
539       (GCallback) autoplug_continue_cb, element);
540   g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
541   g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
542       element);
543
544   gst_bin_add (GST_BIN_CAST (element), uribin);
545   gst_bin_add (GST_BIN_CAST (topbin), element);
546
547   return topbin;
548
549 no_uridecodebin:
550   {
551     g_critical ("can't create uridecodebin element");
552     g_object_unref (element);
553     return NULL;
554   }
555 }