df697ddab10de06c0a362cf12ab5470120de093f
[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 static gboolean
106 payloader_filter (GstPluginFeature * feature, gpointer data)
107 {
108   gboolean res;
109   const gchar *klass;
110
111   /* we only care about element factories */
112   if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
113     return FALSE;
114
115   if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
116     return FALSE;
117
118   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY_CAST (feature));
119
120   if (strstr (klass, "Payloader") == NULL)
121     return FALSE;
122
123   if (strstr (klass, "RTP") == NULL)
124     return FALSE;
125
126   return TRUE;
127 }
128
129 static void
130 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
131 {
132   factory->uri = g_strdup (DEFAULT_URI);
133   /* get the feature list using the filter */
134   factory->factories =
135       gst_default_registry_feature_filter ((GstPluginFeatureFilter)
136       payloader_filter, FALSE, NULL);
137   /* sort on rank and name */
138   factory->factories =
139       g_list_sort (factory->factories, gst_plugin_feature_rank_compare_func);
140
141   factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
142   factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
143 }
144
145 static void
146 gst_rtsp_media_factory_uri_finalize (GObject * obj)
147 {
148   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
149
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);
154
155   G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
156 }
157
158 static void
159 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
160     GValue * value, GParamSpec * pspec)
161 {
162   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
163
164   switch (propid) {
165     case PROP_URI:
166       g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
167       break;
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
170   }
171 }
172
173 static void
174 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
175     const GValue * value, GParamSpec * pspec)
176 {
177   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
178
179   switch (propid) {
180     case PROP_URI:
181       gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
185   }
186 }
187
188 /**
189  * gst_rtsp_media_factory_uri_new:
190  *
191  * Create a new #GstRTSPMediaFactoryURI instance.
192  *
193  * Returns: a new #GstRTSPMediaFactoryURI object.
194  */
195 GstRTSPMediaFactoryURI *
196 gst_rtsp_media_factory_uri_new (void)
197 {
198   GstRTSPMediaFactoryURI *result;
199
200   result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
201
202   return result;
203 }
204
205 /**
206  * gst_rtsp_media_factory_uri_set_uri:
207  * @factory: a #GstRTSPMediaFactory
208  * @uri: the uri the stream
209  *
210  * Set the URI of the resource that will be streamed by this factory.
211  */
212 void
213 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
214     const gchar * uri)
215 {
216   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
217   g_return_if_fail (uri != NULL);
218
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);
223 }
224
225 /**
226  * gst_rtsp_media_factory_uri_get_uri:
227  * @factory: a #GstRTSPMediaFactory
228  *
229  * Get the URI that will provide media for this factory.
230  *
231  * Returns: the configured URI. g_free() after usage.
232  */
233 gchar *
234 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
235 {
236   gchar *result;
237
238   g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
239
240   GST_RTSP_MEDIA_FACTORY_LOCK (factory);
241   result = g_strdup (factory->uri);
242   GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
243
244   return result;
245 }
246
247 static GstElementFactory *
248 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
249 {
250   GList *list, *tmp;
251   GstElementFactory *factory = NULL;
252
253   /* find payloader that can link */
254   list = gst_element_factory_list_filter (urifact->factories, caps,
255       GST_PAD_SINK, FALSE);
256
257   for (tmp = list; tmp; tmp = g_list_next (tmp)) {
258     GstElementFactory *f = GST_ELEMENT_FACTORY_CAST (tmp->data);
259     const gchar *name;
260
261     name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (f));
262
263     factory = f;
264     break;
265   }
266   if (factory)
267     g_object_ref (factory);
268
269   gst_plugin_feature_list_free (list);
270
271   return factory;
272 }
273
274 static gboolean
275 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
276     GstElement * element)
277 {
278   GList *list, *tmp;
279   FactoryData *data;
280   GstElementFactory *factory;
281   gboolean res;
282
283   GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
284       GST_DEBUG_PAD_NAME (pad), caps);
285
286   data = g_object_get_data (G_OBJECT (element), factory_key);
287
288   if (!(factory = find_payloader (data->factory, caps)))
289     goto no_factory;
290
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);
295
296   return FALSE;
297
298   /* ERRORS */
299 no_factory:
300   {
301     /* no payloader, continue autoplugging */
302     GST_DEBUG ("no payloader found");
303     return TRUE;
304   }
305 }
306
307 static void
308 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
309 {
310   GstRTSPMediaFactoryURI *urifact;
311   FactoryData *data;
312   GstElementFactory *factory;
313   GstElement *payloader;
314   GstCaps *caps;
315   GstPad *sinkpad, *srcpad, *ghostpad;
316   GstElement *convert;
317   gchar *padname;
318
319   GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
320
321   /* link the element now and expose the pad */
322   data = g_object_get_data (G_OBJECT (element), factory_key);
323   urifact = data->factory;
324
325   /* ref to make refcounting easier later */
326   gst_object_ref (pad);
327   padname = gst_pad_get_name (pad);
328
329   /* get pad caps first, then call get_caps, then fail */
330   if ((caps = GST_PAD_CAPS (pad)))
331     gst_caps_ref (caps);
332   else if ((caps = gst_pad_get_caps (pad)) == NULL)
333     goto no_caps;
334
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);
342   } else {
343     convert = NULL;
344   }
345
346   if (convert) {
347     gst_bin_add (GST_BIN_CAST (element), convert);
348     gst_element_set_state (convert, GST_STATE_PLAYING);
349
350     sinkpad = gst_element_get_static_pad (convert, "sink");
351     gst_pad_link (pad, sinkpad);
352     gst_object_unref (sinkpad);
353
354     /* unref old pad, we reffed before */
355     gst_object_unref (pad);
356
357     /* continue with new pad and caps */
358     pad = gst_element_get_static_pad (convert, "src");
359     if ((caps = GST_PAD_CAPS (pad)))
360       gst_caps_ref (caps);
361     else if ((caps = gst_pad_get_caps (pad)) == NULL)
362       goto no_caps;
363   }
364
365   if (!(factory = find_payloader (urifact, caps)))
366     goto no_factory;
367
368   gst_caps_unref (caps);
369
370   /* we have a payloader now */
371   GST_DEBUG ("found payloader factory %s",
372       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
373
374   payloader = gst_element_factory_create (factory, NULL);
375   if (payloader == NULL)
376     goto no_payloader;
377
378   g_object_set (payloader, "pt", data->pt, NULL);
379   data->pt++;
380
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);
384
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);
390
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);
396   g_free (padname);
397
398   gst_pad_set_active (ghostpad, TRUE);
399   gst_element_add_pad (element, ghostpad);
400
401   return;
402
403   /* ERRORS */
404 no_caps:
405   {
406     GST_WARNING ("could not get caps from pad");
407     g_free (padname);
408     gst_object_unref (pad);
409     return;
410   }
411 no_factory:
412   {
413     GST_DEBUG ("no payloader found");
414     g_free (padname);
415     gst_caps_unref (caps);
416     gst_object_unref (pad);
417     return;
418   }
419 no_payloader:
420   {
421     GST_ERROR ("could not create payloader from factory");
422     g_free (padname);
423     gst_caps_unref (caps);
424     gst_object_unref (pad);
425     return;
426   }
427 }
428
429 static void
430 no_more_pads_cb (GstElement * uribin, GstElement * element)
431 {
432   GST_DEBUG ("no-more-pads");
433   gst_element_no_more_pads (element);
434 }
435
436 static GstElement *
437 rtsp_media_factory_uri_get_element (GstRTSPMediaFactory * factory,
438     const GstRTSPUrl * url)
439 {
440   GstElement *topbin, *element, *uribin;
441   GstRTSPMediaFactoryURI *urifact;
442   FactoryData *data;
443
444   urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
445
446   GST_LOG ("creating element");
447
448   topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
449   g_assert (topbin != NULL);
450
451   /* our bin will dynamically expose payloaded pads */
452   element = gst_bin_new ("dynpay0");
453   g_assert (element != NULL);
454
455   uribin = gst_element_factory_make ("uridecodebin", "uribin");
456   if (uribin == NULL)
457     goto no_uridecodebin;
458
459   g_object_set (uribin, "uri", urifact->uri, NULL);
460
461   /* keep factory data around */
462   data = g_new0 (FactoryData, 1);
463   data->factory = g_object_ref (factory);
464   data->pt = 96;
465
466   g_object_set_data_full (G_OBJECT (element), factory_key,
467       data, (GDestroyNotify) free_data);
468
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,
474       element);
475
476   gst_bin_add (GST_BIN_CAST (element), uribin);
477   gst_bin_add (GST_BIN_CAST (topbin), element);
478
479   return topbin;
480
481 no_uridecodebin:
482   {
483     g_critical ("can't create uridecodebin element");
484     g_object_unref (element);
485     return NULL;
486   }
487 }