playsink: fix passthrough mode (hopefully)
[platform/upstream/gstreamer.git] / gst / playback / gstplaysinkvideoconvert.c
1 /* GStreamer
2  * Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstplaysinkvideoconvert.h"
25
26 #include <gst/pbutils/pbutils.h>
27 #include <gst/gst-i18n-plugin.h>
28
29 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_video_convert_debug);
30 #define GST_CAT_DEFAULT gst_play_sink_video_convert_debug
31
32 #define parent_class gst_play_sink_video_convert_parent_class
33
34 G_DEFINE_TYPE (GstPlaySinkVideoConvert, gst_play_sink_video_convert,
35     GST_TYPE_BIN);
36
37 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
38     GST_PAD_SRC,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS_ANY);
41
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS_ANY);
46
47 static gboolean
48 is_raw_caps (GstCaps * caps)
49 {
50   gint i, n;
51   GstStructure *s;
52   const gchar *name;
53
54   n = gst_caps_get_size (caps);
55   for (i = 0; i < n; i++) {
56     s = gst_caps_get_structure (caps, i);
57     name = gst_structure_get_name (s);
58     if (!g_str_has_prefix (name, "video/x-raw"))
59       return FALSE;
60   }
61
62   return TRUE;
63 }
64
65 static void
66 post_missing_element_message (GstPlaySinkVideoConvert * self,
67     const gchar * name)
68 {
69   GstMessage *msg;
70
71   msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), name);
72   gst_element_post_message (GST_ELEMENT_CAST (self), msg);
73 }
74
75 static void
76 distribute_running_time (GstElement * element, const GstSegment * segment)
77 {
78   GstEvent *event;
79   GstPad *pad;
80
81   pad = gst_element_get_static_pad (element, "sink");
82
83   if (segment->accum) {
84     event = gst_event_new_new_segment_full (FALSE, segment->rate,
85         segment->applied_rate, segment->format, 0, segment->accum, 0);
86     gst_pad_send_event (pad, event);
87   }
88
89   event = gst_event_new_new_segment_full (FALSE, segment->rate,
90       segment->applied_rate, segment->format,
91       segment->start, segment->stop, segment->time);
92   gst_pad_send_event (pad, event);
93
94   gst_object_unref (pad);
95 }
96
97 static void
98 gst_play_sink_video_convert_add_identity (GstPlaySinkVideoConvert * self)
99 {
100   self->identity = gst_element_factory_make ("identity", "identity");
101   if (self->identity == NULL) {
102     post_missing_element_message (self, "identity");
103     GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
104         (_("Missing element '%s' - check your GStreamer installation."),
105             "identity"), ("video rendering might fail"));
106   } else {
107     gst_bin_add (GST_BIN_CAST (self), self->identity);
108     gst_element_sync_state_with_parent (self->identity);
109     distribute_running_time (self->identity, &self->segment);
110   }
111 }
112
113 static void
114 gst_play_sink_video_convert_set_targets (GstPlaySinkVideoConvert * self,
115     GstElement * head, GstElement * tail)
116 {
117   GstPad *pad;
118
119   pad = gst_element_get_static_pad (head, "sink");
120   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
121   gst_object_unref (pad);
122
123   pad = gst_element_get_static_pad (tail, "src");
124   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
125   gst_object_unref (pad);
126 }
127
128 static void
129 pad_blocked_cb (GstPad * pad, gboolean blocked, GstPlaySinkVideoConvert * self)
130 {
131   GstPad *peer;
132   GstCaps *caps;
133   gboolean raw;
134   GstBin *bin = GST_BIN_CAST (self);
135   GstElement *head = NULL, *prev = NULL;
136
137   GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
138   self->sink_proxypad_blocked = blocked;
139   GST_DEBUG_OBJECT (self, "Pad blocked: %d", blocked);
140   if (!blocked)
141     goto done;
142
143   /* There must be a peer at this point */
144   peer = gst_pad_get_peer (self->sinkpad);
145   caps = gst_pad_get_negotiated_caps (peer);
146   if (!caps)
147     caps = gst_pad_get_caps_reffed (peer);
148   gst_object_unref (peer);
149
150   raw = is_raw_caps (caps);
151   GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
152   gst_caps_unref (caps);
153
154   if (raw == self->raw)
155     goto unblock;
156   self->raw = raw;
157
158   if (raw) {
159
160     GST_DEBUG_OBJECT (self, "Creating raw conversion pipeline");
161
162     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
163     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
164
165     self->conv = gst_element_factory_make (COLORSPACE, "conv");
166     if (self->conv == NULL) {
167       post_missing_element_message (self, COLORSPACE);
168       GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
169           (_("Missing element '%s' - check your GStreamer installation."),
170               COLORSPACE), ("video rendering might fail"));
171     } else {
172       gst_bin_add (bin, self->conv);
173       gst_element_sync_state_with_parent (self->conv);
174       distribute_running_time (self->conv, &self->segment);
175       prev = head = self->conv;
176     }
177
178     self->scale = gst_element_factory_make ("videoscale", "scale");
179     if (self->scale == NULL) {
180       post_missing_element_message (self, "videoscale");
181       GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
182           (_("Missing element '%s' - check your GStreamer installation."),
183               "videoscale"), ("possibly a liboil version mismatch?"));
184     } else {
185       /* Add black borders if necessary to keep the DAR */
186       g_object_set (self->scale, "add-borders", TRUE, NULL);
187       gst_bin_add (bin, self->scale);
188       gst_element_sync_state_with_parent (self->scale);
189       distribute_running_time (self->scale, &self->segment);
190       if (prev) {
191         if (!gst_element_link_pads_full (prev, "src", self->scale, "sink",
192                 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
193           goto link_failed;
194       } else {
195         head = self->scale;
196       }
197       prev = self->scale;
198     }
199
200     GST_DEBUG_OBJECT (self, "Raw conversion pipeline created");
201   } else {
202
203     GST_DEBUG_OBJECT (self, "Removing raw conversion pipeline");
204
205     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
206     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
207
208     if (self->conv) {
209       gst_element_set_state (self->conv, GST_STATE_NULL);
210       gst_bin_remove (bin, self->conv);
211       self->conv = NULL;
212     }
213     if (self->scale) {
214       gst_element_set_state (self->scale, GST_STATE_NULL);
215       gst_bin_remove (bin, self->scale);
216       self->scale = NULL;
217     }
218
219     GST_DEBUG_OBJECT (self, "Raw conversion pipeline removed");
220   }
221
222   g_assert ((head != NULL) == (prev != NULL));
223
224   /* to make things simple and avoid counterintuitive pad juggling,
225      ensure there is at least one element in the list */
226   if (!head) {
227     gst_play_sink_video_convert_add_identity (self);
228     prev = head = self->identity;
229   }
230
231   gst_play_sink_video_convert_set_targets (self, head, prev);
232
233 unblock:
234   gst_pad_set_blocked_async_full (self->sink_proxypad, FALSE,
235       (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
236       (GDestroyNotify) gst_object_unref);
237
238 done:
239   GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
240   return;
241
242 link_failed:
243   {
244     GST_ELEMENT_ERROR (self, CORE, PAD,
245         (NULL), ("Failed to configure the video converter."));
246
247     /* use a simple identity, better than nothing */
248     gst_play_sink_video_convert_add_identity (self);
249     gst_play_sink_video_convert_set_targets (self, self->identity,
250         self->identity);
251
252     gst_pad_set_blocked_async_full (self->sink_proxypad, FALSE,
253         (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
254         (GDestroyNotify) gst_object_unref);
255
256     GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
257     return;
258   }
259 }
260
261 static gboolean
262 gst_play_sink_video_convert_sink_event (GstPad * pad, GstEvent * event)
263 {
264   GstPlaySinkVideoConvert *self =
265       GST_PLAY_SINK_VIDEO_CONVERT (gst_pad_get_parent (pad));
266   gboolean ret;
267
268   ret = gst_proxy_pad_event_default (pad, gst_event_ref (event));
269
270   if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
271     gboolean update;
272     gdouble rate, applied_rate;
273     GstFormat format;
274     gint64 start, stop, position;
275
276     GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
277     gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
278         &format, &start, &stop, &position);
279
280     GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
281         &self->segment);
282     gst_segment_set_newsegment_full (&self->segment, update, rate, applied_rate,
283         format, start, stop, position);
284     GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
285         &self->segment);
286     GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
287   } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
288     GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
289     GST_DEBUG_OBJECT (self, "Resetting segment");
290     gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
291     GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
292   }
293
294   gst_event_unref (event);
295   gst_object_unref (self);
296
297   return ret;
298 }
299
300 static gboolean
301 gst_play_sink_video_convert_sink_setcaps (GstPad * pad, GstCaps * caps)
302 {
303   GstPlaySinkVideoConvert *self =
304       GST_PLAY_SINK_VIDEO_CONVERT (gst_pad_get_parent (pad));
305   gboolean ret;
306   GstStructure *s;
307   const gchar *name;
308   gboolean reconfigure = FALSE;
309
310   GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
311   s = gst_caps_get_structure (caps, 0);
312   name = gst_structure_get_name (s);
313
314   if (g_str_has_prefix (name, "video/x-raw-")) {
315     if (!self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
316       GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
317       reconfigure = TRUE;
318       gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
319           (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
320           (GDestroyNotify) gst_object_unref);
321     }
322   } else {
323     if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
324       GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
325       reconfigure = TRUE;
326       gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
327           (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
328           (GDestroyNotify) gst_object_unref);
329     }
330   }
331
332   /* Otherwise the setcaps below fails */
333   if (reconfigure) {
334     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
335     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
336   }
337   GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
338
339   ret = gst_ghost_pad_setcaps_default (pad, caps);
340
341   GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT ": %d", caps,
342       ret);
343
344   gst_object_unref (self);
345
346   return ret;
347 }
348
349 static GstCaps *
350 gst_play_sink_video_convert_getcaps (GstPad * pad)
351 {
352   GstPlaySinkVideoConvert *self =
353       GST_PLAY_SINK_VIDEO_CONVERT (gst_pad_get_parent (pad));
354   GstCaps *ret;
355   GstPad *otherpad, *peer;
356
357   GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
358   otherpad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
359   if (!otherpad) {
360     if (pad == self->srcpad) {
361       otherpad = self->sink_proxypad;
362     } else if (pad == self->sinkpad) {
363       otherpad =
364           GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
365               (self->sinkpad)));
366     } else {
367       GST_ERROR_OBJECT (pad, "Not one of our pads");
368     }
369   }
370   GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
371
372   if (otherpad) {
373     peer = gst_pad_get_peer (otherpad);
374     if (peer) {
375       ret = gst_pad_get_caps_reffed (peer);
376       gst_object_unref (peer);
377     } else {
378       ret = gst_caps_new_any ();
379     }
380     gst_object_unref (otherpad);
381   } else {
382     GST_WARNING_OBJECT (self, "Could not traverse bin");
383     ret = gst_caps_new_any ();
384   }
385
386   gst_object_unref (self);
387
388   return ret;
389 }
390
391 static void
392 gst_play_sink_video_convert_finalize (GObject * object)
393 {
394   GstPlaySinkVideoConvert *self = GST_PLAY_SINK_VIDEO_CONVERT_CAST (object);
395
396   gst_object_unref (self->sink_proxypad);
397   g_mutex_free (self->lock);
398
399   G_OBJECT_CLASS (parent_class)->finalize (object);
400 }
401
402 static GstStateChangeReturn
403 gst_play_sink_video_convert_change_state (GstElement * element,
404     GstStateChange transition)
405 {
406   GstStateChangeReturn ret;
407   GstPlaySinkVideoConvert *self = GST_PLAY_SINK_VIDEO_CONVERT_CAST (element);
408
409   switch (transition) {
410     case GST_STATE_CHANGE_PAUSED_TO_READY:
411       GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
412       if (gst_pad_is_blocked (self->sink_proxypad))
413         gst_pad_set_blocked_async_full (self->sink_proxypad, FALSE,
414             (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
415             (GDestroyNotify) gst_object_unref);
416       GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
417       break;
418     default:
419       break;
420   }
421
422   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
423   if (ret == GST_STATE_CHANGE_FAILURE)
424     return ret;
425
426   switch (transition) {
427     case GST_STATE_CHANGE_PAUSED_TO_READY:
428       GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
429       gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
430       if (self->conv) {
431         gst_element_set_state (self->conv, GST_STATE_NULL);
432         gst_bin_remove (GST_BIN_CAST (self), self->conv);
433         self->conv = NULL;
434       }
435       if (self->scale) {
436         gst_element_set_state (self->scale, GST_STATE_NULL);
437         gst_bin_remove (GST_BIN_CAST (self), self->scale);
438         self->scale = NULL;
439       }
440       if (!self->identity) {
441         gst_play_sink_video_convert_add_identity (self);
442       }
443       gst_play_sink_video_convert_set_targets (self, self->identity,
444           self->identity);
445       self->raw = FALSE;
446       GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
447       break;
448     case GST_STATE_CHANGE_READY_TO_PAUSED:
449       GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
450       if (!gst_pad_is_blocked (self->sink_proxypad))
451         gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
452             (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
453             (GDestroyNotify) gst_object_unref);
454       GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
455       break;
456     case GST_STATE_CHANGE_READY_TO_NULL:
457       if (self->identity) {
458         gst_element_set_state (self->identity, GST_STATE_NULL);
459         gst_bin_remove (GST_BIN_CAST (self), self->identity);
460         self->identity = NULL;
461       }
462       break;
463     default:
464       break;
465   }
466
467   return ret;
468 }
469
470 static void
471 gst_play_sink_video_convert_class_init (GstPlaySinkVideoConvertClass * klass)
472 {
473   GObjectClass *gobject_class;
474   GstElementClass *gstelement_class;
475
476   GST_DEBUG_CATEGORY_INIT (gst_play_sink_video_convert_debug,
477       "playsinkvideoconvert", 0, "play bin");
478
479   gobject_class = (GObjectClass *) klass;
480   gstelement_class = (GstElementClass *) klass;
481
482   gobject_class->finalize = gst_play_sink_video_convert_finalize;
483
484   gst_element_class_add_pad_template (gstelement_class,
485       gst_static_pad_template_get (&srctemplate));
486   gst_element_class_add_pad_template (gstelement_class,
487       gst_static_pad_template_get (&sinktemplate));
488   gst_element_class_set_details_simple (gstelement_class,
489       "Player Sink Video Converter", "Video/Bin/Converter",
490       "Convenience bin for video conversion",
491       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
492
493   gstelement_class->change_state =
494       GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_change_state);
495 }
496
497 static void
498 gst_play_sink_video_convert_init (GstPlaySinkVideoConvert * self)
499 {
500   GstPadTemplate *templ;
501
502   self->lock = g_mutex_new ();
503   gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
504
505   templ = gst_static_pad_template_get (&sinktemplate);
506   self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
507   gst_pad_set_event_function (self->sinkpad,
508       GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_sink_event));
509   gst_pad_set_setcaps_function (self->sinkpad,
510       GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_sink_setcaps));
511   gst_pad_set_getcaps_function (self->sinkpad,
512       GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_getcaps));
513
514   self->sink_proxypad =
515       GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
516
517   gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
518   gst_object_unref (templ);
519
520   templ = gst_static_pad_template_get (&srctemplate);
521   self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
522   gst_pad_set_getcaps_function (self->srcpad,
523       GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_getcaps));
524   gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
525   gst_object_unref (templ);
526
527   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
528       self->sink_proxypad);
529 }