media: add lock to protect state changes
[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_klass (fact);
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, 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   factory->uri = g_strdup (DEFAULT_URI);
162   factory->use_gstpay = DEFAULT_USE_GSTPAY;
163
164   /* get the feature list using the filter */
165   gst_registry_feature_filter (gst_registry_get (), (GstPluginFeatureFilter)
166       payloader_filter, FALSE, &data);
167   /* sort */
168   factory->demuxers =
169       g_list_sort (data.demux, gst_plugin_feature_rank_compare_func);
170   factory->payloaders =
171       g_list_sort (data.payload, gst_plugin_feature_rank_compare_func);
172   factory->decoders =
173       g_list_sort (data.decode, gst_plugin_feature_rank_compare_func);
174
175   factory->raw_vcaps = gst_static_caps_get (&raw_video_caps);
176   factory->raw_acaps = gst_static_caps_get (&raw_audio_caps);
177 }
178
179 static void
180 gst_rtsp_media_factory_uri_finalize (GObject * obj)
181 {
182   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (obj);
183
184   g_free (factory->uri);
185   gst_plugin_feature_list_free (factory->demuxers);
186   gst_plugin_feature_list_free (factory->payloaders);
187   gst_plugin_feature_list_free (factory->decoders);
188   gst_caps_unref (factory->raw_vcaps);
189   gst_caps_unref (factory->raw_acaps);
190
191   G_OBJECT_CLASS (gst_rtsp_media_factory_uri_parent_class)->finalize (obj);
192 }
193
194 static void
195 gst_rtsp_media_factory_uri_get_property (GObject * object, guint propid,
196     GValue * value, GParamSpec * pspec)
197 {
198   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
199
200   switch (propid) {
201     case PROP_URI:
202       g_value_take_string (value, gst_rtsp_media_factory_uri_get_uri (factory));
203       break;
204     case PROP_USE_GSTPAY:
205       g_value_set_boolean (value, factory->use_gstpay);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
209   }
210 }
211
212 static void
213 gst_rtsp_media_factory_uri_set_property (GObject * object, guint propid,
214     const GValue * value, GParamSpec * pspec)
215 {
216   GstRTSPMediaFactoryURI *factory = GST_RTSP_MEDIA_FACTORY_URI (object);
217
218   switch (propid) {
219     case PROP_URI:
220       gst_rtsp_media_factory_uri_set_uri (factory, g_value_get_string (value));
221       break;
222     case PROP_USE_GSTPAY:
223       factory->use_gstpay = g_value_get_boolean (value);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
227   }
228 }
229
230 /**
231  * gst_rtsp_media_factory_uri_new:
232  *
233  * Create a new #GstRTSPMediaFactoryURI instance.
234  *
235  * Returns: a new #GstRTSPMediaFactoryURI object.
236  */
237 GstRTSPMediaFactoryURI *
238 gst_rtsp_media_factory_uri_new (void)
239 {
240   GstRTSPMediaFactoryURI *result;
241
242   result = g_object_new (GST_TYPE_RTSP_MEDIA_FACTORY_URI, NULL);
243
244   return result;
245 }
246
247 /**
248  * gst_rtsp_media_factory_uri_set_uri:
249  * @factory: a #GstRTSPMediaFactory
250  * @uri: the uri the stream
251  *
252  * Set the URI of the resource that will be streamed by this factory.
253  */
254 void
255 gst_rtsp_media_factory_uri_set_uri (GstRTSPMediaFactoryURI * factory,
256     const gchar * uri)
257 {
258   g_return_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory));
259   g_return_if_fail (uri != NULL);
260
261   GST_RTSP_MEDIA_FACTORY_LOCK (factory);
262   g_free (factory->uri);
263   factory->uri = g_strdup (uri);
264   GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
265 }
266
267 /**
268  * gst_rtsp_media_factory_uri_get_uri:
269  * @factory: a #GstRTSPMediaFactory
270  *
271  * Get the URI that will provide media for this factory.
272  *
273  * Returns: the configured URI. g_free() after usage.
274  */
275 gchar *
276 gst_rtsp_media_factory_uri_get_uri (GstRTSPMediaFactoryURI * factory)
277 {
278   gchar *result;
279
280   g_return_val_if_fail (GST_IS_RTSP_MEDIA_FACTORY_URI (factory), NULL);
281
282   GST_RTSP_MEDIA_FACTORY_LOCK (factory);
283   result = g_strdup (factory->uri);
284   GST_RTSP_MEDIA_FACTORY_UNLOCK (factory);
285
286   return result;
287 }
288
289 static GstElementFactory *
290 find_payloader (GstRTSPMediaFactoryURI * urifact, GstCaps * caps)
291 {
292   GList *list;
293   GstElementFactory *factory = NULL;
294   gboolean autoplug_more = FALSE;
295
296   /* first find a demuxer that can link */
297   list = gst_element_factory_list_filter (urifact->demuxers, caps,
298       GST_PAD_SINK, FALSE);
299
300   if (list) {
301     GstStructure *structure = gst_caps_get_structure (caps, 0);
302     gboolean parsed = FALSE;
303
304     gst_structure_get_boolean (structure, "parsed", &parsed);
305
306     /* Avoid plugging parsers in a loop. This is not 100% correct, as some
307      * parsers don't set parsed=true in caps. We should do something like
308      * decodebin does and track decode chains and elements plugged in those
309      * chains...
310      */
311     if (parsed) {
312       GList *walk;
313       const gchar *klass;
314
315       for (walk = list; walk; walk = walk->next) {
316         factory = GST_ELEMENT_FACTORY (walk->data);
317         klass = gst_element_factory_get_klass (factory);
318         if (strstr (klass, "Parser"))
319           /* caps have parsed=true, so skip this parser to avoid loops */
320           continue;
321
322         autoplug_more = TRUE;
323         break;
324       }
325     } else {
326       /* caps don't have parsed=true set and we have a demuxer/parser */
327       autoplug_more = TRUE;
328     }
329
330     gst_plugin_feature_list_free (list);
331   }
332
333   if (autoplug_more)
334     /* we have a demuxer, try that one first */
335     return NULL;
336
337   /* no demuxer try a depayloader */
338   list = gst_element_factory_list_filter (urifact->payloaders, caps,
339       GST_PAD_SINK, FALSE);
340
341   if (list == NULL) {
342     if (urifact->use_gstpay) {
343       /* no depayloader or parser/demuxer, use gstpay when allowed */
344       factory = gst_element_factory_find ("rtpgstpay");
345     } else {
346       /* no depayloader, try a decoder, we'll get to a payloader for a decoded
347        * video or audio format, worst case. */
348       list = gst_element_factory_list_filter (urifact->decoders, caps,
349           GST_PAD_SINK, FALSE);
350
351       if (list != NULL) {
352         /* we have a decoder, try that one first */
353         gst_plugin_feature_list_free (list);
354         return NULL;
355       }
356     }
357   }
358
359   if (list != NULL) {
360     factory = GST_ELEMENT_FACTORY_CAST (list->data);
361     g_object_ref (factory);
362     gst_plugin_feature_list_free (list);
363   }
364   return factory;
365 }
366
367 static gboolean
368 autoplug_continue_cb (GstElement * uribin, GstPad * pad, GstCaps * caps,
369     GstElement * element)
370 {
371   FactoryData *data;
372   GstElementFactory *factory;
373
374   GST_DEBUG ("found pad %s:%s of caps %" GST_PTR_FORMAT,
375       GST_DEBUG_PAD_NAME (pad), caps);
376
377   data = g_object_get_data (G_OBJECT (element), factory_key);
378
379   if (!(factory = find_payloader (data->factory, caps)))
380     goto no_factory;
381
382   /* we found a payloader, stop autoplugging so we can plug the
383    * payloader. */
384   GST_DEBUG ("found factory %s",
385       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
386   gst_object_unref (factory);
387
388   return FALSE;
389
390   /* ERRORS */
391 no_factory:
392   {
393     /* no payloader, continue autoplugging */
394     GST_DEBUG ("no payloader found");
395     return TRUE;
396   }
397 }
398
399 static void
400 pad_added_cb (GstElement * uribin, GstPad * pad, GstElement * element)
401 {
402   GstRTSPMediaFactoryURI *urifact;
403   FactoryData *data;
404   GstElementFactory *factory;
405   GstElement *payloader;
406   GstCaps *caps;
407   GstPad *sinkpad, *srcpad, *ghostpad;
408   GstElement *convert;
409   gchar *padname;
410
411   GST_DEBUG ("added pad %s:%s", GST_DEBUG_PAD_NAME (pad));
412
413   /* link the element now and expose the pad */
414   data = g_object_get_data (G_OBJECT (element), factory_key);
415   urifact = data->factory;
416
417   /* ref to make refcounting easier later */
418   gst_object_ref (pad);
419   padname = gst_pad_get_name (pad);
420
421   /* get pad caps first, then call get_caps, then fail */
422   if ((caps = gst_pad_get_current_caps (pad)) == NULL)
423     if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
424       goto no_caps;
425
426   /* check for raw caps */
427   if (gst_caps_can_intersect (caps, urifact->raw_vcaps)) {
428     /* we have raw video caps, insert converter */
429     convert = gst_element_factory_make ("videoconvert", NULL);
430   } else if (gst_caps_can_intersect (caps, urifact->raw_acaps)) {
431     /* we have raw audio caps, insert converter */
432     convert = gst_element_factory_make ("audioconvert", NULL);
433   } else {
434     convert = NULL;
435   }
436
437   if (convert) {
438     gst_bin_add (GST_BIN_CAST (element), convert);
439     gst_element_set_state (convert, GST_STATE_PLAYING);
440
441     sinkpad = gst_element_get_static_pad (convert, "sink");
442     gst_pad_link (pad, sinkpad);
443     gst_object_unref (sinkpad);
444
445     /* unref old pad, we reffed before */
446     gst_object_unref (pad);
447
448     /* continue with new pad and caps */
449     pad = gst_element_get_static_pad (convert, "src");
450     if ((caps = gst_pad_get_current_caps (pad)) == NULL)
451       if ((caps = gst_pad_query_caps (pad, NULL)) == NULL)
452         goto no_caps;
453   }
454
455   if (!(factory = find_payloader (urifact, caps)))
456     goto no_factory;
457
458   gst_caps_unref (caps);
459
460   /* we have a payloader now */
461   GST_DEBUG ("found payloader factory %s",
462       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
463
464   payloader = gst_element_factory_create (factory, NULL);
465   if (payloader == NULL)
466     goto no_payloader;
467
468   g_object_set (payloader, "pt", data->pt, NULL);
469   data->pt++;
470
471   if (g_object_class_find_property (G_OBJECT_GET_CLASS (payloader),
472           "buffer-list"))
473     g_object_set (payloader, "buffer-list", TRUE, NULL);
474
475   /* add the payloader to the pipeline */
476   gst_bin_add (GST_BIN_CAST (element), payloader);
477   gst_element_set_state (payloader, GST_STATE_PLAYING);
478
479   /* link the pad to the sinkpad of the payloader */
480   sinkpad = gst_element_get_static_pad (payloader, "sink");
481   gst_pad_link (pad, sinkpad);
482   gst_object_unref (sinkpad);
483   gst_object_unref (pad);
484
485   /* now expose the srcpad of the payloader as a ghostpad with the same name
486    * as the uridecodebin pad name. */
487   srcpad = gst_element_get_static_pad (payloader, "src");
488   ghostpad = gst_ghost_pad_new (padname, srcpad);
489   gst_object_unref (srcpad);
490   g_free (padname);
491
492   gst_pad_set_active (ghostpad, TRUE);
493   gst_element_add_pad (element, ghostpad);
494
495   return;
496
497   /* ERRORS */
498 no_caps:
499   {
500     GST_WARNING ("could not get caps from pad");
501     g_free (padname);
502     gst_object_unref (pad);
503     return;
504   }
505 no_factory:
506   {
507     GST_DEBUG ("no payloader found");
508     g_free (padname);
509     gst_caps_unref (caps);
510     gst_object_unref (pad);
511     return;
512   }
513 no_payloader:
514   {
515     GST_ERROR ("could not create payloader from factory");
516     g_free (padname);
517     gst_caps_unref (caps);
518     gst_object_unref (pad);
519     return;
520   }
521 }
522
523 static void
524 no_more_pads_cb (GstElement * uribin, GstElement * element)
525 {
526   GST_DEBUG ("no-more-pads");
527   gst_element_no_more_pads (element);
528 }
529
530 static GstElement *
531 rtsp_media_factory_uri_create_element (GstRTSPMediaFactory * factory,
532     const GstRTSPUrl * url)
533 {
534   GstElement *topbin, *element, *uribin;
535   GstRTSPMediaFactoryURI *urifact;
536   FactoryData *data;
537
538   urifact = GST_RTSP_MEDIA_FACTORY_URI_CAST (factory);
539
540   GST_LOG ("creating element");
541
542   topbin = gst_bin_new ("GstRTSPMediaFactoryURI");
543   g_assert (topbin != NULL);
544
545   /* our bin will dynamically expose payloaded pads */
546   element = gst_bin_new ("dynpay0");
547   g_assert (element != NULL);
548
549   uribin = gst_element_factory_make ("uridecodebin", "uribin");
550   if (uribin == NULL)
551     goto no_uridecodebin;
552
553   g_object_set (uribin, "uri", urifact->uri, NULL);
554
555   /* keep factory data around */
556   data = g_new0 (FactoryData, 1);
557   data->factory = g_object_ref (factory);
558   data->pt = 96;
559
560   g_object_set_data_full (G_OBJECT (element), factory_key,
561       data, (GDestroyNotify) free_data);
562
563   /* connect to the signals */
564   g_signal_connect (uribin, "autoplug-continue",
565       (GCallback) autoplug_continue_cb, element);
566   g_signal_connect (uribin, "pad-added", (GCallback) pad_added_cb, element);
567   g_signal_connect (uribin, "no-more-pads", (GCallback) no_more_pads_cb,
568       element);
569
570   gst_bin_add (GST_BIN_CAST (element), uribin);
571   gst_bin_add (GST_BIN_CAST (topbin), element);
572
573   return topbin;
574
575 no_uridecodebin:
576   {
577     g_critical ("can't create uridecodebin element");
578     g_object_unref (element);
579     return NULL;
580   }
581 }