client: add locking
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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_create_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->create_element = rtsp_media_factory_uri_create_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   const gchar *klass;
125   GstElementFactory *fact;
126   GList **list = NULL;
127
128   /* we only care about element factories */
129   if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
130     return FALSE;
131
132   if (gst_plugin_feature_get_rank (feature) < GST_RANK_MARGINAL)
133     return FALSE;
134
135   fact = GST_ELEMENT_FACTORY_CAST (feature);
136
137   klass = gst_element_factory_get_metadata (fact, GST_ELEMENT_METADATA_KLASS);
138
139   if (strstr (klass, "Decoder"))
140     list = &data->decode;
141   else if (strstr (klass, "Demux"))
142     list = &data->demux;
143   else if (strstr (klass, "Parser") && strstr (klass, "Codec"))
144     list = &data->demux;
145   else if (strstr (klass, "Payloader") && strstr (klass, "RTP"))
146     list = &data->payload;
147
148   if (list) {
149     GST_DEBUG ("adding %s", GST_OBJECT_NAME (fact));
150     *list = g_list_prepend (*list, gst_object_ref (fact));
151   }
152
153   return FALSE;
154 }
155
156 static void
157 gst_rtsp_media_factory_uri_init (GstRTSPMediaFactoryURI * factory)
158 {
159   FilterData data = { NULL, NULL, NULL };
160
161   GST_DEBUG_OBJECT (factory, "new");
162
163   factory->uri = g_strdup (DEFAULT_URI);
164   factory->use_gstpay = DEFAULT_USE_GSTPAY;
165
166   /* get the feature list using the filter */
167   gst_registry_feature_filter (gst_registry_get (), (GstPluginFeatureFilter)
168       payloader_filter, FALSE, &data);
169   /* sort */
170   factory->demuxers =
171       g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
172   factory->payloaders =
173       g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
174   factory->decoders =
175       g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
176
177   factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
178   factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
179 }
180
181 static void
182 gst_rtsp_media_factory_uri_finalize (GObject * obj)
183 {
184   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
185
186   GST_DEBUG_OBJECT (factory, "finalize");
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;
297   GstElementFactory *factory = NULL;
298   gboolean autoplug_more = FALSE;
299
300   /* first find a demuxer that can link */
301   list = gst_element_factory_list_filter (urifact->demuxers, caps,
302       GST_PAD_SINK, FALSE);
303
304   if (list) {
305     GstStructure *structure = gst_caps_get_structure (caps, 0);
306     gboolean parsed = FALSE;
307
308     gst_structure_get_boolean (structure, "parsed", &parsed);
309
310     /* Avoid plugging parsers in a loop. This is not 100% correct, as some
311      * parsers don't set parsed=true in caps. We should do something like
312      * decodebin does and track decode chains and elements plugged in those
313      * chains...
314      */
315     if (parsed) {
316       GList *walk;
317       const gchar *klass;
318
319       for (walk = list; walk; walk = walk->next) {
320         factory = GST_ELEMENT_FACTORY (walk->data);
321         klass = gst_element_factory_get_metadata (factory,
322             GST_ELEMENT_METADATA_KLASS);
323         if (strstr (klass, "Parser"))
324           /* caps have parsed=true, so skip this parser to avoid loops */
325           continue;
326
327         autoplug_more = TRUE;
328         break;
329       }
330     } else {
331       /* caps don't have parsed=true set and we have a demuxer/parser */
332       autoplug_more = TRUE;
333     }
334
335     gst_plugin_feature_list_free (list);
336   }
337
338   if (autoplug_more)
339     /* we have a demuxer, try that one first */
340     return NULL;
341
342   /* no demuxer try a depayloader */
343   list = gst_element_factory_list_filter (urifact->payloaders, caps,
344       GST_PAD_SINK, FALSE);
345
346   if (list == NULL) {
347     if (urifact->use_gstpay) {
348       /* no depayloader or parser/demuxer, use gstpay when allowed */
349       factory = gst_element_factory_find ("rtpgstpay");
350     } else {
351       /* no depayloader, try a decoder, we'll get to a payloader for a decoded
352        * video or audio format, worst case. */
353       list = gst_element_factory_list_filter (urifact->decoders, caps,
354           GST_PAD_SINK, FALSE);
355
356       if (list != NULL) {
357         /* we have a decoder, try that one first */
358         gst_plugin_feature_list_free (list);
359         return NULL;
360       }
361     }
362   }
363
364   if (list != NULL) {
365     factory = GST_ELEMENT_FACTORY_CAST (list->data);
366     g_object_ref (factory);
367     gst_plugin_feature_list_free (list);
368   }
369   return factory;
370 }
371
372 static gboolean
373 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
374     GstElement * element)
375 {
376   FactoryData *data;
377   GstElementFactory *factory;
378
379   GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
380       GST_DEBUG_PAD_NAME (pad), caps);
381
382   data = g_object_get_data (G_OBJECT (element), factory_key);
383
384   if (!(factory = find_payloader (data->factory, caps)))
385     goto no_factory;
386
387   /* we found a payloader, stop autoplugging so we can plug the
388    * payloader. */
389   GST_DEBUG ("found factory %s",
390       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
391   gst_object_unref (factory);
392
393   return FALSE;
394
395   /* ERRORS */
396 no_factory:
397   {
398     /* no payloader, continue autoplugging */
399     GST_DEBUG ("no payloader found");
400     return TRUE;
401   }
402 }
403
404 static void
405 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
406 {
407   GstRTSPMediaFactoryURI *urifact;
408   FactoryData *data;
409   GstElementFactory *factory;
410   GstElement *payloader;
411   GstCaps *caps;
412   GstPad *sinkpad, *srcpad, *ghostpad;
413   GstElement *convert;
414   gchar *padname;
415
416   GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
417
418   /* link the element now and expose the pad */
419   data = g_object_get_data (G_OBJECT (element), factory_key);
420   urifact = data->factory;
421
422   /* ref to make refcounting easier later */
423   gst_object_ref (pad);
424   padname = gst_pad_get_name (pad);
425
426   /* get pad caps first, then call get_caps, then fail */
427   if ((caps = gst_pad_get_current_caps (pad)) == NULL)
428     if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
429       goto no_caps;
430
431   /* check for raw caps */
432   if (gst_caps_can_intersect (caps, urifact->raw_vcaps)) {
433     /* we have raw video caps, insert converter */
434     convert = gst_element_factory_make ("videoconvert", NULL);
435   } else if (gst_caps_can_intersect (caps, urifact->raw_acaps)) {
436     /* we have raw audio caps, insert converter */
437     convert = gst_element_factory_make ("audioconvert", NULL);
438   } else {
439     convert = NULL;
440   }
441
442   if (convert) {
443     gst_bin_add (GST_BIN_CAST (element), convert);
444     gst_element_set_state (convert, GST_STATE_PLAYING);
445
446     sinkpad = gst_element_get_static_pad (convert, "sink");
447     gst_pad_link (pad, sinkpad);
448     gst_object_unref (sinkpad);
449
450     /* unref old pad, we reffed before */
451     gst_object_unref (pad);
452
453     /* continue with new pad and caps */
454     pad = gst_element_get_static_pad (convert, "src");
455     if ((caps = gst_pad_get_current_caps (pad)) == NULL)
456       if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
457         goto no_caps;
458   }
459
460   if (!(factory = find_payloader (urifact, caps)))
461     goto no_factory;
462
463   gst_caps_unref (caps);
464
465   /* we have a payloader now */
466   GST_DEBUG ("found payloader factory %s",
467       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
468
469   payloader = gst_element_factory_create (factory, NULL);
470   if (payloader == NULL)
471     goto no_payloader;
472
473   g_object_set (payloader, "pt", data->pt, NULL);
474   data->pt++;
475
476   if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
477           "buffer-list"))
478     g_object_set (payloader, "buffer-list", TRUE, NULL);
479
480   /* add the payloader to the pipeline */
481   gst_bin_add (GST_BIN_CAST (element), payloader);
482   gst_element_set_state (payloader, GST_STATE_PLAYING);
483
484   /* link the pad to the sinkpad of the payloader */
485   sinkpad = gst_element_get_static_pad (payloader, "sink");
486   gst_pad_link (pad, sinkpad);
487   gst_object_unref (sinkpad);
488   gst_object_unref (pad);
489
490   /* now expose the srcpad of the payloader as a ghostpad with the same name
491    * as the uridecodebin pad name. */
492   srcpad = gst_element_get_static_pad (payloader, "src");
493   ghostpad = gst_ghost_pad_new (padname, srcpad);
494   gst_object_unref (srcpad);
495   g_free (padname);
496
497   gst_pad_set_active (ghostpad, TRUE);
498   gst_element_add_pad (element, ghostpad);
499
500   return;
501
502   /* ERRORS */
503 no_caps:
504   {
505     GST_WARNING ("could not get caps from pad");
506     g_free (padname);
507     gst_object_unref (pad);
508     return;
509   }
510 no_factory:
511   {
512     GST_DEBUG ("no payloader found");
513     g_free (padname);
514     gst_caps_unref (caps);
515     gst_object_unref (pad);
516     return;
517   }
518 no_payloader:
519   {
520     GST_ERROR ("could not create payloader from factory");
521     g_free (padname);
522     gst_caps_unref (caps);
523     gst_object_unref (pad);
524     return;
525   }
526 }
527
528 static void
529 no_more_pads_cb (GstElement * uribin, GstElement * element)
530 {
531   GST_DEBUG ("no-more-pads");
532   gst_element_no_more_pads (element);
533 }
534
535 static GstElement *
536 rtsp_media_factory_uri_create_element (GstRTSPMediaFactory * factory,
537     const GstRTSPUrl * url)
538 {
539   GstElement *topbin, *element, *uribin;
540   GstRTSPMediaFactoryURI *urifact;
541   FactoryData *data;
542
543   urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
544
545   GST_LOG ("creating element");
546
547   topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
548   g_assert (topbin != NULL);
549
550   /* our bin will dynamically expose payloaded pads */
551   element = gst_bin_new ("dynpay0");
552   g_assert (element != NULL);
553
554   uribin = gst_element_factory_make ("uridecodebin", "uribin");
555   if (uribin == NULL)
556     goto no_uridecodebin;
557
558   g_object_set (uribin, "uri", urifact->uri, NULL);
559
560   /* keep factory data around */
561   data = g_new0 (FactoryData, 1);
562   data->factory = g_object_ref (factory);
563   data->pt = 96;
564
565   g_object_set_data_full (G_OBJECT (element), factory_key,
566       data, (GDestroyNotify) free_data);
567
568   /* connect to the signals */
569   g_signal_connect (uribin, "autoplug-continue",
570       (GCallback) autoplug_continue_cb, element);
571   g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
572   g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
573       element);
574
575   gst_bin_add (GST_BIN_CAST (element), uribin);
576   gst_bin_add (GST_BIN_CAST (topbin), element);
577
578   return topbin;
579
580 no_uridecodebin:
581   {
582     g_critical ("can't create uridecodebin element");
583     g_object_unref (element);
584     return NULL;
585   }
586 }