Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / gst / playback / gstplaysinkconvertbin.c
1 /* GStreamer
2  * Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  * Copyright (C) <2011> Vincent Penquerch <vincent.penquerch@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstplaysinkconvertbin.h"
26
27 #include <gst/pbutils/pbutils.h>
28 #include <gst/gst-i18n-plugin.h>
29
30 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_convert_bin_debug);
31 #define GST_CAT_DEFAULT gst_play_sink_convert_bin_debug
32
33 #define parent_class gst_play_sink_convert_bin_parent_class
34
35 static gboolean gst_play_sink_convert_bin_sink_setcaps (GstPlaySinkConvertBin *
36     self, GstCaps * caps);
37
38 G_DEFINE_TYPE (GstPlaySinkConvertBin, gst_play_sink_convert_bin, GST_TYPE_BIN);
39
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS_ANY);
44
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS_ANY);
49
50 static gboolean
51 is_raw_caps (GstCaps * caps, gboolean audio)
52 {
53   gint i, n;
54   GstStructure *s;
55   const gchar *name;
56   const gchar *prefix = audio ? "audio/x-raw-" : "video/x-raw-";
57
58   n = gst_caps_get_size (caps);
59   for (i = 0; i < n; i++) {
60     s = gst_caps_get_structure (caps, i);
61     name = gst_structure_get_name (s);
62     if (!g_str_has_prefix (name, prefix))
63       return FALSE;
64   }
65
66   return TRUE;
67 }
68
69 static void
70 gst_play_sink_convert_bin_post_missing_element_message (GstPlaySinkConvertBin *
71     self, const gchar * name)
72 {
73   GstMessage *msg;
74
75   msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), name);
76   gst_element_post_message (GST_ELEMENT_CAST (self), msg);
77 }
78
79 static void
80 distribute_running_time (GstElement * element, const GstSegment * segment)
81 {
82   GstEvent *event;
83   GstPad *pad;
84
85   pad = gst_element_get_static_pad (element, "sink");
86
87   gst_pad_send_event (pad, gst_event_new_flush_start ());
88   gst_pad_send_event (pad, gst_event_new_flush_stop (FALSE));
89
90   event = gst_event_new_segment (segment);
91   gst_pad_send_event (pad, event);
92
93   gst_object_unref (pad);
94 }
95
96 void
97 gst_play_sink_convert_bin_add_conversion_element (GstPlaySinkConvertBin * self,
98     GstElement * el)
99 {
100   self->conversion_elements = g_list_append (self->conversion_elements, el);
101   gst_bin_add (GST_BIN (self), gst_object_ref (el));
102 }
103
104 GstElement *
105 gst_play_sink_convert_bin_add_conversion_element_factory (GstPlaySinkConvertBin
106     * self, const char *factory, const char *name)
107 {
108   GstElement *el;
109
110   el = gst_element_factory_make (factory, name);
111   if (el == NULL) {
112     gst_play_sink_convert_bin_post_missing_element_message (self, factory);
113     GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
114         (_("Missing element '%s' - check your GStreamer installation."),
115             factory),
116         (self->audio ? "audio rendering might fail" :
117             "video rendering might fail"));
118   } else {
119     gst_play_sink_convert_bin_add_conversion_element (self, el);
120   }
121   return el;
122 }
123
124 void
125 gst_play_sink_convert_bin_add_identity (GstPlaySinkConvertBin * self)
126 {
127   if (self->identity)
128     return;
129
130   self->identity = gst_element_factory_make ("identity", "identity");
131   if (self->identity == NULL) {
132     gst_play_sink_convert_bin_post_missing_element_message (self, "identity");
133     GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
134         (_("Missing element '%s' - check your GStreamer installation."),
135             "identity"), (self->audio ?
136             "audio rendering might fail" : "video rendering might fail")
137
138         );
139   } else {
140     g_object_set (self->identity, "silent", TRUE, NULL);
141     gst_bin_add (GST_BIN_CAST (self), self->identity);
142   }
143 }
144
145 static void
146 gst_play_sink_convert_bin_set_targets (GstPlaySinkConvertBin * self,
147     gboolean passthrough)
148 {
149   GstPad *pad;
150   GstElement *head, *tail;
151
152   GST_DEBUG_OBJECT (self, "Setting pad targets with passthrough %d",
153       passthrough);
154   if (self->conversion_elements == NULL || passthrough) {
155     GST_DEBUG_OBJECT (self, "no conversion elements, using identity (%p) as "
156         "head/tail", self->identity);
157     if (!passthrough) {
158       GST_WARNING_OBJECT (self,
159           "Doing passthrough as no converter elements were added");
160     }
161     head = tail = self->identity;
162   } else {
163     head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
164     tail = GST_ELEMENT (g_list_last (self->conversion_elements)->data);
165     GST_DEBUG_OBJECT (self, "conversion elements in use, picking "
166         "head:%s and tail:%s", GST_OBJECT_NAME (head), GST_OBJECT_NAME (tail));
167   }
168
169   g_return_if_fail (head != NULL);
170   g_return_if_fail (tail != NULL);
171
172   pad = gst_element_get_static_pad (head, "sink");
173   GST_DEBUG_OBJECT (self, "Ghosting bin sink pad to %" GST_PTR_FORMAT, pad);
174   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
175   gst_object_unref (pad);
176
177   pad = gst_element_get_static_pad (tail, "src");
178   GST_DEBUG_OBJECT (self, "Ghosting bin src pad to %" GST_PTR_FORMAT, pad);
179   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
180   gst_object_unref (pad);
181 }
182
183 static void
184 gst_play_sink_convert_bin_remove_element (GstElement * element,
185     GstPlaySinkConvertBin * self)
186 {
187   gst_element_set_state (element, GST_STATE_NULL);
188   gst_bin_remove (GST_BIN_CAST (self), element);
189 }
190
191 static void
192 gst_play_sink_convert_bin_on_element_added (GstElement * element,
193     GstPlaySinkConvertBin * self)
194 {
195   gst_element_sync_state_with_parent (element);
196   distribute_running_time (element, &self->segment);
197 }
198
199 static GstPadProbeReturn
200 pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
201 {
202   GstPlaySinkConvertBin *self = user_data;
203   GstPad *peer;
204   GstCaps *caps;
205   gboolean raw;
206
207   GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
208   GST_DEBUG_OBJECT (self, "Pad blocked");
209
210   /* There must be a peer at this point */
211   peer = gst_pad_get_peer (self->sinkpad);
212   caps = gst_pad_get_current_caps (peer);
213   if (!caps)
214     caps = gst_pad_get_caps (peer, NULL);
215   gst_object_unref (peer);
216
217   raw = is_raw_caps (caps, self->audio);
218   GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
219   gst_caps_unref (caps);
220
221   if (raw == self->raw)
222     goto unblock;
223   self->raw = raw;
224
225   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
226   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
227
228   if (raw) {
229     GST_DEBUG_OBJECT (self, "Switching to raw conversion pipeline");
230
231     if (self->conversion_elements)
232       g_list_foreach (self->conversion_elements,
233           (GFunc) gst_play_sink_convert_bin_on_element_added, self);
234   } else {
235
236     GST_DEBUG_OBJECT (self, "Switch to passthrough pipeline");
237
238     gst_play_sink_convert_bin_on_element_added (self->identity, self);
239   }
240
241   gst_play_sink_convert_bin_set_targets (self, !raw);
242
243 unblock:
244   self->sink_proxypad_block_id = 0;
245   GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
246
247   return GST_PAD_PROBE_REMOVE;
248 }
249
250 static gboolean
251 gst_play_sink_convert_bin_sink_event (GstPad * pad, GstEvent * event)
252 {
253   GstPlaySinkConvertBin *self =
254       GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
255   gboolean ret;
256
257   switch (GST_EVENT_TYPE (event)) {
258     case GST_EVENT_CAPS:
259     {
260       GstCaps *caps;
261
262       gst_event_parse_caps (event, &caps);
263       ret = gst_play_sink_convert_bin_sink_setcaps (self, caps);
264       break;
265     }
266     default:
267       break;
268   }
269
270   ret = gst_proxy_pad_event_default (pad, gst_event_ref (event));
271
272   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
273     GstSegment seg;
274
275     GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
276     gst_event_copy_segment (event, &seg);
277
278     GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
279         &self->segment);
280     self->segment = seg;
281     GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
282         &self->segment);
283     GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
284   } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
285     GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
286     GST_DEBUG_OBJECT (self, "Resetting segment");
287     gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
288     GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
289   }
290
291   gst_event_unref (event);
292   gst_object_unref (self);
293
294   return ret;
295 }
296
297 static void
298 block_proxypad (GstPlaySinkConvertBin * self)
299 {
300   if (self->sink_proxypad_block_id == 0) {
301     self->sink_proxypad_block_id =
302         gst_pad_add_probe (self->sink_proxypad,
303         GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked_cb,
304         gst_object_ref (self), (GDestroyNotify) gst_object_unref);
305   }
306 }
307
308 static void
309 unblock_proxypad (GstPlaySinkConvertBin * self)
310 {
311   if (self->sink_proxypad_block_id != 0) {
312     gst_pad_remove_probe (self->sink_proxypad, self->sink_proxypad_block_id);
313     self->sink_proxypad_block_id = 0;
314   }
315 }
316
317 static gboolean
318 gst_play_sink_convert_bin_sink_setcaps (GstPlaySinkConvertBin * self,
319     GstCaps * caps)
320 {
321   GstStructure *s;
322   const gchar *name;
323   gboolean reconfigure = FALSE;
324   gboolean raw;
325
326   GST_DEBUG_OBJECT (self, "setcaps");
327   GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
328   s = gst_caps_get_structure (caps, 0);
329   name = gst_structure_get_name (s);
330
331   if (self->audio) {
332     raw = g_str_has_prefix (name, "audio/x-raw-");
333   } else {
334     raw = g_str_has_prefix (name, "video/x-raw-");
335   }
336
337   GST_DEBUG_OBJECT (self, "raw %d, self->raw %d, blocked %d",
338       raw, self->raw, gst_pad_is_blocked (self->sink_proxypad));
339   if (raw) {
340     if (!self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
341       GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
342       reconfigure = TRUE;
343       block_proxypad (self);
344     }
345   } else {
346     if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
347       GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
348       reconfigure = TRUE;
349       block_proxypad (self);
350     }
351   }
352
353   /* Otherwise the setcaps below fails */
354   if (reconfigure) {
355     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
356     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
357   }
358
359   GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
360
361   GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT, caps);
362
363   return TRUE;
364 }
365
366 static GstCaps *
367 gst_play_sink_convert_bin_getcaps (GstPad * pad, GstCaps * filter)
368 {
369   GstPlaySinkConvertBin *self =
370       GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
371   GstCaps *ret;
372   GstPad *otherpad, *peer;
373
374   GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
375   if (pad == self->srcpad) {
376     otherpad = self->sinkpad;
377   } else if (pad == self->sinkpad) {
378     otherpad = self->srcpad;
379   } else {
380     GST_ERROR_OBJECT (pad, "Not one of our pads");
381     otherpad = NULL;
382   }
383
384   if (otherpad) {
385     peer = gst_pad_get_peer (otherpad);
386     if (peer) {
387       GstCaps *peer_caps = gst_pad_get_caps (peer, filter);
388       gst_object_unref (peer);
389       if (self->converter_caps) {
390         peer_caps = gst_caps_make_writable (peer_caps);
391         gst_caps_merge (peer_caps, gst_caps_ref (self->converter_caps));
392         ret = peer_caps;
393       } else {
394         ret = peer_caps;
395       }
396     } else {
397       ret = gst_caps_ref (self->converter_caps);
398     }
399   } else {
400     ret = gst_caps_new_any ();
401   }
402   GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
403
404   gst_object_unref (self);
405
406   return ret;
407 }
408
409 void
410 gst_play_sink_convert_bin_remove_elements (GstPlaySinkConvertBin * self)
411 {
412   if (self->conversion_elements) {
413     g_list_foreach (self->conversion_elements,
414         (GFunc) gst_play_sink_convert_bin_remove_element, self);
415     g_list_free (self->conversion_elements);
416     self->conversion_elements = NULL;
417   }
418   if (self->converter_caps) {
419     gst_caps_unref (self->converter_caps);
420     self->converter_caps = NULL;
421   }
422 }
423
424 static void
425 gst_play_sink_convert_bin_finalize (GObject * object)
426 {
427   GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
428
429   gst_play_sink_convert_bin_remove_elements (self);
430
431   gst_object_unref (self->sink_proxypad);
432   g_mutex_free (self->lock);
433
434   G_OBJECT_CLASS (parent_class)->finalize (object);
435 }
436
437 void
438 gst_play_sink_convert_bin_cache_converter_caps (GstPlaySinkConvertBin * self)
439 {
440   GstElement *head;
441   GstPad *pad;
442
443   if (self->converter_caps) {
444     gst_caps_unref (self->converter_caps);
445     self->converter_caps = NULL;
446   }
447
448   if (!self->conversion_elements) {
449     GST_WARNING_OBJECT (self, "No conversion elements");
450     return;
451   }
452
453   head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
454   pad = gst_element_get_static_pad (head, "sink");
455   if (!pad) {
456     GST_WARNING_OBJECT (self, "No sink pad found");
457     return;
458   }
459
460   self->converter_caps = gst_pad_get_caps (pad, NULL);
461   GST_INFO_OBJECT (self, "Converter caps: %" GST_PTR_FORMAT,
462       self->converter_caps);
463
464   gst_object_unref (pad);
465 }
466
467 static GstStateChangeReturn
468 gst_play_sink_convert_bin_change_state (GstElement * element,
469     GstStateChange transition)
470 {
471   GstStateChangeReturn ret;
472   GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (element);
473
474   switch (transition) {
475     case GST_STATE_CHANGE_PAUSED_TO_READY:
476       GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
477       unblock_proxypad (self);
478       GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
479       break;
480     case GST_STATE_CHANGE_READY_TO_PAUSED:
481       GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
482       gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
483       gst_play_sink_convert_bin_set_targets (self, TRUE);
484       self->raw = FALSE;
485       GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
486       break;
487     default:
488       break;
489   }
490
491   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
492   if (ret == GST_STATE_CHANGE_FAILURE)
493     return ret;
494
495   switch (transition) {
496     case GST_STATE_CHANGE_PAUSED_TO_READY:
497       GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
498       gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
499       gst_play_sink_convert_bin_set_targets (self, TRUE);
500       self->raw = FALSE;
501       GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
502       break;
503     case GST_STATE_CHANGE_READY_TO_PAUSED:
504       GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
505       unblock_proxypad (self);
506       GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
507       break;
508     default:
509       break;
510   }
511
512   return ret;
513 }
514
515 static void
516 gst_play_sink_convert_bin_class_init (GstPlaySinkConvertBinClass * klass)
517 {
518   GObjectClass *gobject_class;
519   GstElementClass *gstelement_class;
520
521   GST_DEBUG_CATEGORY_INIT (gst_play_sink_convert_bin_debug,
522       "playsinkconvertbin", 0, "play bin");
523
524   gobject_class = (GObjectClass *) klass;
525   gstelement_class = (GstElementClass *) klass;
526
527   gobject_class->finalize = gst_play_sink_convert_bin_finalize;
528
529   gst_element_class_add_pad_template (gstelement_class,
530       gst_static_pad_template_get (&srctemplate));
531   gst_element_class_add_pad_template (gstelement_class,
532       gst_static_pad_template_get (&sinktemplate));
533   gst_element_class_set_details_simple (gstelement_class,
534       "Player Sink Converter Bin", "Bin/Converter",
535       "Convenience bin for audio/video conversion",
536       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
537
538   gstelement_class->change_state =
539       GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_change_state);
540 }
541
542 static void
543 gst_play_sink_convert_bin_init (GstPlaySinkConvertBin * self)
544 {
545   GstPadTemplate *templ;
546
547   self->lock = g_mutex_new ();
548   gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
549
550   templ = gst_static_pad_template_get (&sinktemplate);
551   self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
552   gst_pad_set_event_function (self->sinkpad,
553       GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_sink_event));
554   gst_pad_set_getcaps_function (self->sinkpad,
555       GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_getcaps));
556
557   self->sink_proxypad =
558       GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
559
560   gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
561   gst_object_unref (templ);
562
563   templ = gst_static_pad_template_get (&srctemplate);
564   self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
565   gst_pad_set_getcaps_function (self->srcpad,
566       GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_getcaps));
567   gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
568   gst_object_unref (templ);
569
570   gst_play_sink_convert_bin_add_identity (self);
571 }