misc: chain up to collectpads event handler
[platform/upstream/gst-plugins-good.git] / gst / videomixer / videomixer2.c
1 /* Generic video mixer plugin
2  * Copyright (C) 2004, 2008 Wim Taymans <wim@fluendo.com>
3  * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@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 /**
22  * SECTION:element-videomixer
23  *
24  * Videomixer2 can accept AYUV, ARGB and BGRA video streams. For each of the requested
25  * sink pads it will compare the incoming geometry and framerate to define the
26  * output parameters. Indeed output video frames will have the geometry of the
27  * biggest incoming video stream and the framerate of the fastest incoming one.
28  *
29  * All sink pads must be either AYUV, ARGB or BGRA, but a mixture of them is not 
30  * supported. The src pad will have the same colorspace as the sinks. 
31  * No colorspace conversion is done. 
32  * 
33  * Individual parameters for each input stream can be configured on the
34  * #GstVideoMixer2Pad.
35  *
36  * <refsect2>
37  * <title>Sample pipelines</title>
38  * |[
39  * gst-launch-0.11 \
40  *   videotestsrc pattern=1 ! \
41  *   video/x-raw,format=AYUV,framerate=\(fraction\)10/1,width=100,height=100 ! \
42  *   videobox border-alpha=0 top=-70 bottom=-70 right=-220 ! \
43  *   videomixer name=mix sink_0::alpha=0.7 sink_1::alpha=0.5 ! \
44  *   videoconvert ! xvimagesink \
45  *   videotestsrc ! \
46  *   video/x-raw,format=AYUV,framerate=\(fraction\)5/1,width=320,height=240 ! mix.
47  * ]| A pipeline to demonstrate videomixer used together with videobox.
48  * This should show a 320x240 pixels video test source with some transparency
49  * showing the background checker pattern. Another video test source with just
50  * the snow pattern of 100x100 pixels is overlayed on top of the first one on
51  * the left vertically centered with a small transparency showing the first
52  * video test source behind and the checker pattern under it. Note that the
53  * framerate of the output video is 10 frames per second.
54  * |[
55  * gst-launch-0.11 videotestsrc pattern=1 ! \
56  *   video/x-raw, framerate=\(fraction\)10/1, width=100, height=100 ! \
57  *   videomixer name=mix ! videoconvert ! ximagesink \
58  *   videotestsrc !  \
59  *   video/x-raw, framerate=\(fraction\)5/1, width=320, height=240 ! mix.
60  * ]| A pipeline to demostrate bgra mixing. (This does not demonstrate alpha blending). 
61  * |[
62  * gst-launch-0.11 videotestsrc pattern=1 ! \
63  *   video/x-raw,format =I420, framerate=\(fraction\)10/1, width=100, height=100 ! \
64  *   videomixer name=mix ! videoconvert ! ximagesink \
65  *   videotestsrc ! \
66  *   video/x-raw,format=I420, framerate=\(fraction\)5/1, width=320, height=240 ! mix.
67  * ]| A pipeline to test I420
68  * |[
69  * gst-launch-0.11 videomixer name=mixer sink_1::alpha=0.5 sink_1::xpos=50 sink_1::ypos=50 ! \
70  *   videoconvert ! ximagesink \
71  *   videotestsrc pattern=snow timestamp-offset=3000000000 ! \
72  *   "video/x-raw,format=AYUV,width=640,height=480,framerate=(fraction)30/1" ! \
73  *   timeoverlay ! queue2 ! mixer. \
74  *   videotestsrc pattern=smpte ! \
75  *   "video/x-raw,format=AYUV,width=800,height=600,framerate=(fraction)10/1" ! \
76  *   timeoverlay ! queue2 ! mixer.
77  * ]| A pipeline to demonstrate synchronized mixing (the second stream starts after 3 seconds)
78  * </refsect2>
79  */
80
81 #ifdef HAVE_CONFIG_H
82 #include "config.h"
83 #endif
84
85 #include <string.h>
86
87 #include "videomixer2.h"
88 #include "videomixer2pad.h"
89
90 #ifdef DISABLE_ORC
91 #define orc_memset memset
92 #else
93 #include <orc/orcfunctions.h>
94 #endif
95
96 GST_DEBUG_CATEGORY_STATIC (gst_videomixer2_debug);
97 #define GST_CAT_DEFAULT gst_videomixer2_debug
98
99 #define GST_VIDEO_MIXER2_GET_LOCK(mix) \
100   (&GST_VIDEO_MIXER2(mix)->lock)
101 #define GST_VIDEO_MIXER2_LOCK(mix) \
102   (g_mutex_lock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
103 #define GST_VIDEO_MIXER2_UNLOCK(mix) \
104   (g_mutex_unlock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
105
106 #define FORMATS " { AYUV, BGRA, ARGB, RGBA, ABGR, Y444, Y42B, YUY2, UYVY, "\
107                 "   YVYU, I420, YV12, Y41B, RGB, BGR, xRGB, xBGR, RGBx, BGRx } "
108
109 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
110     GST_PAD_SRC,
111     GST_PAD_ALWAYS,
112     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
113     );
114
115 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
116     GST_PAD_SINK,
117     GST_PAD_REQUEST,
118     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
119     );
120
121 static void gst_videomixer2_child_proxy_init (gpointer g_iface,
122     gpointer iface_data);
123 static gboolean gst_videomixer2_push_sink_event (GstVideoMixer2 * mix,
124     GstEvent * event);
125 static void gst_videomixer2_release_pad (GstElement * element, GstPad * pad);
126 static void gst_videomixer2_reset_qos (GstVideoMixer2 * mix);
127
128 struct _GstVideoMixer2Collect
129 {
130   GstCollectData2 collect;      /* we extend the CollectData */
131
132   GstVideoMixer2Pad *mixpad;
133
134   GstBuffer *queued;            /* buffer for which we don't know the end time yet */
135
136   GstBuffer *buffer;            /* buffer that should be blended now */
137   GstClockTime start_time;
138   GstClockTime end_time;
139 };
140
141 #define DEFAULT_PAD_ZORDER 0
142 #define DEFAULT_PAD_XPOS   0
143 #define DEFAULT_PAD_YPOS   0
144 #define DEFAULT_PAD_ALPHA  1.0
145 enum
146 {
147   PROP_PAD_0,
148   PROP_PAD_ZORDER,
149   PROP_PAD_XPOS,
150   PROP_PAD_YPOS,
151   PROP_PAD_ALPHA
152 };
153
154 G_DEFINE_TYPE (GstVideoMixer2Pad, gst_videomixer2_pad, GST_TYPE_PAD);
155
156 static void
157 gst_videomixer2_collect_free (GstCollectData2 * data)
158 {
159   GstVideoMixer2Collect *cdata = (GstVideoMixer2Collect *) data;
160
161   gst_buffer_replace (&cdata->buffer, NULL);
162 }
163
164 static gboolean gst_videomixer2_src_setcaps (GstPad * pad, GstVideoMixer2 * mix,
165     GstCaps * caps);
166
167 static gboolean
168 gst_videomixer2_update_src_caps (GstVideoMixer2 * mix)
169 {
170   GSList *l;
171   gint best_width = -1, best_height = -1;
172   gdouble best_fps = -1, cur_fps;
173   gint best_fps_n = -1, best_fps_d = -1;
174   gboolean ret = TRUE;
175
176   GST_VIDEO_MIXER2_LOCK (mix);
177
178   for (l = mix->sinkpads; l; l = l->next) {
179     GstVideoMixer2Pad *mpad = l->data;
180     gint this_width, this_height;
181     gint fps_n, fps_d;
182     gint width, height;
183
184     fps_n = GST_VIDEO_INFO_FPS_N (&mpad->info);
185     fps_d = GST_VIDEO_INFO_FPS_D (&mpad->info);
186     width = GST_VIDEO_INFO_WIDTH (&mpad->info);
187     height = GST_VIDEO_INFO_HEIGHT (&mpad->info);
188
189     if (fps_n == 0 || fps_d == 0 || width == 0 || height == 0)
190       continue;
191
192     this_width = width + MAX (mpad->xpos, 0);
193     this_height = height + MAX (mpad->ypos, 0);
194
195     if (best_width < this_width)
196       best_width = this_width;
197     if (best_height < this_height)
198       best_height = this_height;
199
200     if (fps_d == 0)
201       cur_fps = 0.0;
202     else
203       gst_util_fraction_to_double (fps_n, fps_d, &cur_fps);
204
205     if (best_fps < cur_fps) {
206       best_fps = cur_fps;
207       best_fps_n = fps_n;
208       best_fps_d = fps_d;
209     }
210   }
211
212   if (best_fps_n <= 0 && best_fps_d <= 0) {
213     best_fps_n = 25;
214     best_fps_d = 1;
215     best_fps = 25.0;
216   }
217
218   if (best_width > 0 && best_height > 0 && best_fps > 0) {
219     GstCaps *caps, *peercaps;
220     GstStructure *s;
221     GstVideoInfo info;
222
223     if (GST_VIDEO_INFO_FPS_N (&mix->info) != best_fps_n ||
224         GST_VIDEO_INFO_FPS_D (&mix->info) != best_fps_d) {
225       if (mix->segment.position != -1) {
226         mix->ts_offset = mix->segment.position - mix->segment.start;
227         mix->nframes = 0;
228       }
229     }
230     gst_video_info_set_format (&info, GST_VIDEO_INFO_FORMAT (&mix->info),
231         best_width, best_height);
232     info.fps_n = best_fps_n;
233     info.fps_d = best_fps_d;
234     info.par_n = GST_VIDEO_INFO_PAR_N (&mix->info);
235     info.par_d = GST_VIDEO_INFO_PAR_D (&mix->info);
236
237     caps = gst_video_info_to_caps (&info);
238
239     peercaps = gst_pad_peer_query_caps (mix->srcpad, NULL);
240     if (peercaps) {
241       GstCaps *tmp;
242
243       s = gst_caps_get_structure (caps, 0);
244       gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, "height",
245           GST_TYPE_INT_RANGE, 1, G_MAXINT, "framerate", GST_TYPE_FRACTION_RANGE,
246           0, 1, G_MAXINT, 1, NULL);
247
248       tmp = gst_caps_intersect (caps, peercaps);
249       gst_caps_unref (caps);
250       gst_caps_unref (peercaps);
251       caps = tmp;
252       if (gst_caps_is_empty (caps)) {
253         GST_DEBUG_OBJECT (mix, "empty caps");
254         ret = FALSE;
255         GST_VIDEO_MIXER2_UNLOCK (mix);
256         goto done;
257       }
258
259       caps = gst_caps_truncate (caps);
260       s = gst_caps_get_structure (caps, 0);
261       gst_structure_fixate_field_nearest_int (s, "width", best_width);
262       gst_structure_fixate_field_nearest_int (s, "height", best_height);
263       gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n,
264           best_fps_d);
265
266       gst_structure_get_int (s, "width", &info.width);
267       gst_structure_get_int (s, "height", &info.height);
268       gst_structure_get_fraction (s, "fraction", &info.fps_n, &info.fps_d);
269     }
270
271     caps = gst_video_info_to_caps (&info);
272
273     GST_VIDEO_MIXER2_UNLOCK (mix);
274     ret = gst_videomixer2_src_setcaps (mix->srcpad, mix, caps);
275     gst_caps_unref (caps);
276   } else {
277     GST_VIDEO_MIXER2_UNLOCK (mix);
278   }
279
280 done:
281   return ret;
282 }
283
284
285 static gboolean
286 gst_videomixer2_pad_sink_setcaps (GstPad * pad, GstObject * parent,
287     GstCaps * caps)
288 {
289   GstVideoMixer2 *mix;
290   GstVideoMixer2Pad *mixpad;
291   GstVideoInfo info;
292   gboolean ret = FALSE;
293
294   GST_INFO_OBJECT (pad, "Setting caps %" GST_PTR_FORMAT, caps);
295
296   mix = GST_VIDEO_MIXER2 (parent);
297   mixpad = GST_VIDEO_MIXER2_PAD (pad);
298
299   if (!gst_video_info_from_caps (&info, caps)) {
300     GST_ERROR_OBJECT (pad, "Failed to parse caps");
301     goto beach;
302   }
303
304   GST_VIDEO_MIXER2_LOCK (mix);
305   if (GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_FORMAT_UNKNOWN) {
306     if (GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_INFO_FORMAT (&info) ||
307         GST_VIDEO_INFO_PAR_N (&mix->info) != GST_VIDEO_INFO_PAR_N (&info) ||
308         GST_VIDEO_INFO_PAR_D (&mix->info) != GST_VIDEO_INFO_PAR_D (&info)) {
309       GST_ERROR_OBJECT (pad, "Caps not compatible with other pads' caps");
310       GST_VIDEO_MIXER2_UNLOCK (mix);
311       goto beach;
312     }
313   }
314
315   mix->info = info;
316   mixpad->info = info;
317
318   GST_VIDEO_MIXER2_UNLOCK (mix);
319
320   ret = gst_videomixer2_update_src_caps (mix);
321
322 beach:
323   return ret;
324 }
325
326 static GstCaps *
327 gst_videomixer2_pad_sink_getcaps (GstPad * pad, GstObject * parent,
328     GstCaps * filter)
329 {
330   GstVideoMixer2 *mix;
331   GstCaps *srccaps;
332   GstStructure *s;
333   gint i, n;
334
335   mix = GST_VIDEO_MIXER2 (parent);
336
337   srccaps = gst_pad_get_current_caps (GST_PAD (mix->srcpad));
338   if (srccaps == NULL)
339     srccaps = gst_pad_get_pad_template_caps (GST_PAD (mix->srcpad));
340
341   srccaps = gst_caps_make_writable (srccaps);
342
343   n = gst_caps_get_size (srccaps);
344   for (i = 0; i < n; i++) {
345     s = gst_caps_get_structure (srccaps, i);
346     gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
347         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
348         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
349     if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
350       gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
351           NULL);
352   }
353
354   GST_DEBUG_OBJECT (pad, "Returning %" GST_PTR_FORMAT, srccaps);
355
356   return srccaps;
357 }
358
359 static gboolean
360 gst_videomixer2_pad_sink_acceptcaps (GstPad * pad, GstObject * parent,
361     GstCaps * caps)
362 {
363   gboolean ret;
364   GstVideoMixer2 *mix;
365   GstCaps *accepted_caps;
366   gint i, n;
367   GstStructure *s;
368
369   mix = GST_VIDEO_MIXER2 (parent);
370   GST_DEBUG_OBJECT (pad, "%" GST_PTR_FORMAT, caps);
371
372   accepted_caps = gst_pad_get_current_caps (GST_PAD (mix->srcpad));
373   if (accepted_caps == NULL)
374     accepted_caps = gst_pad_get_pad_template_caps (GST_PAD (mix->srcpad));
375
376   accepted_caps = gst_caps_make_writable (accepted_caps);
377   GST_LOG_OBJECT (pad, "src caps %" GST_PTR_FORMAT, accepted_caps);
378
379   n = gst_caps_get_size (accepted_caps);
380   for (i = 0; i < n; i++) {
381     s = gst_caps_get_structure (accepted_caps, i);
382     gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
383         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
384         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
385     if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
386       gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
387           NULL);
388   }
389
390   ret = gst_caps_can_intersect (caps, accepted_caps);
391   GST_INFO_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT, (ret ? "" : "not "),
392       caps);
393   GST_INFO_OBJECT (pad, "acceptable caps are %" GST_PTR_FORMAT, accepted_caps);
394   gst_caps_unref (accepted_caps);
395
396   return ret;
397 }
398
399 static gboolean
400 gst_videomixer2_pad_sink_query (GstPad * pad, GstObject * parent,
401     GstQuery * query)
402 {
403   gboolean ret = FALSE;
404
405   switch (GST_QUERY_TYPE (query)) {
406     case GST_QUERY_CAPS:
407     {
408       GstCaps *filter, *caps;
409
410       gst_query_parse_caps (query, &filter);
411       caps = gst_videomixer2_pad_sink_getcaps (pad, parent, filter);
412       gst_query_set_caps_result (query, caps);
413       gst_caps_unref (caps);
414       ret = TRUE;
415       break;
416     }
417     case GST_QUERY_ACCEPT_CAPS:
418     {
419       GstCaps *caps;
420
421       gst_query_parse_accept_caps (query, &caps);
422       ret = gst_videomixer2_pad_sink_acceptcaps (pad, parent, caps);
423       gst_query_set_accept_caps_result (query, ret);
424       ret = TRUE;
425       break;
426     }
427     default:
428       ret = gst_pad_query_default (pad, parent, query);
429       break;
430   }
431   return ret;
432 }
433
434 static void
435 gst_videomixer2_pad_get_property (GObject * object, guint prop_id,
436     GValue * value, GParamSpec * pspec)
437 {
438   GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
439
440   switch (prop_id) {
441     case PROP_PAD_ZORDER:
442       g_value_set_uint (value, pad->zorder);
443       break;
444     case PROP_PAD_XPOS:
445       g_value_set_int (value, pad->xpos);
446       break;
447     case PROP_PAD_YPOS:
448       g_value_set_int (value, pad->ypos);
449       break;
450     case PROP_PAD_ALPHA:
451       g_value_set_double (value, pad->alpha);
452       break;
453     default:
454       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
455       break;
456   }
457 }
458
459 static int
460 pad_zorder_compare (const GstVideoMixer2Pad * pad1,
461     const GstVideoMixer2Pad * pad2)
462 {
463   return pad1->zorder - pad2->zorder;
464 }
465
466 static void
467 gst_videomixer2_pad_set_property (GObject * object, guint prop_id,
468     const GValue * value, GParamSpec * pspec)
469 {
470   GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
471   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (GST_PAD (pad)));
472
473   switch (prop_id) {
474     case PROP_PAD_ZORDER:
475       GST_VIDEO_MIXER2_LOCK (mix);
476       pad->zorder = g_value_get_uint (value);
477
478       mix->sinkpads = g_slist_sort (mix->sinkpads,
479           (GCompareFunc) pad_zorder_compare);
480       GST_VIDEO_MIXER2_UNLOCK (mix);
481       break;
482     case PROP_PAD_XPOS:
483       pad->xpos = g_value_get_int (value);
484       break;
485     case PROP_PAD_YPOS:
486       pad->ypos = g_value_get_int (value);
487       break;
488     case PROP_PAD_ALPHA:
489       pad->alpha = g_value_get_double (value);
490       break;
491     default:
492       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
493       break;
494   }
495
496   gst_object_unref (mix);
497 }
498
499 static void
500 gst_videomixer2_pad_class_init (GstVideoMixer2PadClass * klass)
501 {
502   GObjectClass *gobject_class = (GObjectClass *) klass;
503
504   gobject_class->set_property = gst_videomixer2_pad_set_property;
505   gobject_class->get_property = gst_videomixer2_pad_get_property;
506
507   g_object_class_install_property (gobject_class, PROP_PAD_ZORDER,
508       g_param_spec_uint ("zorder", "Z-Order", "Z Order of the picture",
509           0, 10000, DEFAULT_PAD_ZORDER,
510           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
511   g_object_class_install_property (gobject_class, PROP_PAD_XPOS,
512       g_param_spec_int ("xpos", "X Position", "X Position of the picture",
513           G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
514           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
515   g_object_class_install_property (gobject_class, PROP_PAD_YPOS,
516       g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
517           G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
518           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
519   g_object_class_install_property (gobject_class, PROP_PAD_ALPHA,
520       g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
521           DEFAULT_PAD_ALPHA,
522           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
523 }
524
525 static void
526 gst_videomixer2_pad_init (GstVideoMixer2Pad * mixerpad)
527 {
528   /* setup some pad functions */
529   gst_pad_set_query_function (GST_PAD (mixerpad),
530       gst_videomixer2_pad_sink_query);
531
532   mixerpad->zorder = DEFAULT_PAD_ZORDER;
533   mixerpad->xpos = DEFAULT_PAD_XPOS;
534   mixerpad->ypos = DEFAULT_PAD_YPOS;
535   mixerpad->alpha = DEFAULT_PAD_ALPHA;
536 }
537
538 /* GstVideoMixer2 */
539 #define DEFAULT_BACKGROUND VIDEO_MIXER2_BACKGROUND_CHECKER
540 enum
541 {
542   PROP_0,
543   PROP_BACKGROUND
544 };
545
546 #define GST_TYPE_VIDEO_MIXER2_BACKGROUND (gst_videomixer2_background_get_type())
547 static GType
548 gst_videomixer2_background_get_type (void)
549 {
550   static GType video_mixer_background_type = 0;
551
552   static const GEnumValue video_mixer_background[] = {
553     {VIDEO_MIXER2_BACKGROUND_CHECKER, "Checker pattern", "checker"},
554     {VIDEO_MIXER2_BACKGROUND_BLACK, "Black", "black"},
555     {VIDEO_MIXER2_BACKGROUND_WHITE, "White", "white"},
556     {VIDEO_MIXER2_BACKGROUND_TRANSPARENT,
557         "Transparent Background to enable further mixing", "transparent"},
558     {0, NULL, NULL},
559   };
560
561   if (!video_mixer_background_type) {
562     video_mixer_background_type =
563         g_enum_register_static ("GstVideoMixer2Background",
564         video_mixer_background);
565   }
566   return video_mixer_background_type;
567 }
568
569 #define gst_videomixer2_parent_class parent_class
570 G_DEFINE_TYPE_WITH_CODE (GstVideoMixer2, gst_videomixer2, GST_TYPE_ELEMENT,
571     G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY,
572         gst_videomixer2_child_proxy_init));
573
574 static void
575 gst_videomixer2_update_qos (GstVideoMixer2 * mix, gdouble proportion,
576     GstClockTimeDiff diff, GstClockTime timestamp)
577 {
578   GST_DEBUG_OBJECT (mix,
579       "Updating QoS: proportion %lf, diff %s%" GST_TIME_FORMAT ", timestamp %"
580       GST_TIME_FORMAT, proportion, (diff < 0) ? "-" : "",
581       GST_TIME_ARGS (ABS (diff)), GST_TIME_ARGS (timestamp));
582
583   GST_OBJECT_LOCK (mix);
584   mix->proportion = proportion;
585   if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
586     if (G_UNLIKELY (diff > 0))
587       mix->earliest_time =
588           timestamp + 2 * diff + gst_util_uint64_scale_int (GST_SECOND,
589           GST_VIDEO_INFO_FPS_D (&mix->info), GST_VIDEO_INFO_FPS_N (&mix->info));
590     else
591       mix->earliest_time = timestamp + diff;
592   } else {
593     mix->earliest_time = GST_CLOCK_TIME_NONE;
594   }
595   GST_OBJECT_UNLOCK (mix);
596 }
597
598 static void
599 gst_videomixer2_reset_qos (GstVideoMixer2 * mix)
600 {
601   gst_videomixer2_update_qos (mix, 0.5, 0, GST_CLOCK_TIME_NONE);
602   mix->qos_processed = mix->qos_dropped = 0;
603 }
604
605 static void
606 gst_videomixer2_read_qos (GstVideoMixer2 * mix, gdouble * proportion,
607     GstClockTime * time)
608 {
609   GST_OBJECT_LOCK (mix);
610   *proportion = mix->proportion;
611   *time = mix->earliest_time;
612   GST_OBJECT_UNLOCK (mix);
613 }
614
615 static void
616 gst_videomixer2_reset (GstVideoMixer2 * mix)
617 {
618   GSList *l;
619
620   gst_video_info_init (&mix->info);
621   mix->ts_offset = 0;
622   mix->nframes = 0;
623
624   gst_segment_init (&mix->segment, GST_FORMAT_TIME);
625   mix->segment.position = -1;
626
627   gst_videomixer2_reset_qos (mix);
628
629   for (l = mix->sinkpads; l; l = l->next) {
630     GstVideoMixer2Pad *p = l->data;
631     GstVideoMixer2Collect *mixcol = p->mixcol;
632
633     gst_buffer_replace (&mixcol->buffer, NULL);
634     mixcol->start_time = -1;
635     mixcol->end_time = -1;
636
637     gst_video_info_init (&p->info);
638   }
639
640   mix->newseg_pending = TRUE;
641   mix->flush_stop_pending = FALSE;
642 }
643
644 /*  1 == OK
645  *  0 == need more data
646  * -1 == EOS
647  * -2 == error
648  */
649 static gint
650 gst_videomixer2_fill_queues (GstVideoMixer2 * mix,
651     GstClockTime output_start_time, GstClockTime output_end_time)
652 {
653   GSList *l;
654   gboolean eos = TRUE;
655   gboolean need_more_data = FALSE;
656
657   for (l = mix->sinkpads; l; l = l->next) {
658     GstVideoMixer2Pad *pad = l->data;
659     GstVideoMixer2Collect *mixcol = pad->mixcol;
660     GstSegment *segment = &pad->mixcol->collect.segment;
661     GstBuffer *buf;
662
663     buf = gst_collect_pads2_peek (mix->collect, &mixcol->collect);
664     if (buf) {
665       GstClockTime start_time, end_time;
666
667       start_time = GST_BUFFER_TIMESTAMP (buf);
668       if (start_time == -1) {
669         gst_buffer_unref (buf);
670         GST_ERROR_OBJECT (pad, "Need timestamped buffers!");
671         return -2;
672       }
673
674       /* FIXME: Make all this work with negative rates */
675
676       if ((mixcol->buffer && start_time < GST_BUFFER_TIMESTAMP (mixcol->buffer))
677           || (mixcol->queued
678               && start_time < GST_BUFFER_TIMESTAMP (mixcol->queued))) {
679         GST_WARNING_OBJECT (pad, "Buffer from the past, dropping");
680         gst_buffer_unref (buf);
681         buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
682         gst_buffer_unref (buf);
683         need_more_data = TRUE;
684         continue;
685       }
686
687       if (mixcol->queued) {
688         end_time = start_time - GST_BUFFER_TIMESTAMP (mixcol->queued);
689         start_time = GST_BUFFER_TIMESTAMP (mixcol->queued);
690         gst_buffer_unref (buf);
691         buf = gst_buffer_ref (mixcol->queued);
692       } else {
693         end_time = GST_BUFFER_DURATION (buf);
694
695         if (end_time == -1) {
696           mixcol->queued = buf;
697           need_more_data = TRUE;
698           continue;
699         }
700       }
701
702       g_assert (start_time != -1 && end_time != -1);
703       end_time += start_time;   /* convert from duration to position */
704
705       if (mixcol->end_time != -1 && mixcol->end_time > end_time) {
706         GST_WARNING_OBJECT (pad, "Buffer from the past, dropping");
707         if (buf == mixcol->queued) {
708           gst_buffer_unref (buf);
709           gst_buffer_replace (&mixcol->queued, NULL);
710         } else {
711           gst_buffer_unref (buf);
712           buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
713           gst_buffer_unref (buf);
714         }
715
716         need_more_data = TRUE;
717         continue;
718       }
719
720       /* Check if it's inside the segment */
721       if (start_time >= segment->stop || end_time < segment->start) {
722         GST_DEBUG_OBJECT (pad, "Buffer outside the segment");
723
724         if (buf == mixcol->queued) {
725           gst_buffer_unref (buf);
726           gst_buffer_replace (&mixcol->queued, NULL);
727         } else {
728           gst_buffer_unref (buf);
729           buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
730           gst_buffer_unref (buf);
731         }
732
733         need_more_data = TRUE;
734         continue;
735       }
736
737       /* Clip to segment and convert to running time */
738       start_time = MAX (start_time, segment->start);
739       if (segment->stop != -1)
740         end_time = MIN (end_time, segment->stop);
741       start_time =
742           gst_segment_to_running_time (segment, GST_FORMAT_TIME, start_time);
743       end_time =
744           gst_segment_to_running_time (segment, GST_FORMAT_TIME, end_time);
745       g_assert (start_time != -1 && end_time != -1);
746
747       /* Convert to the output segment rate */
748       if (ABS (mix->segment.rate) != 1.0) {
749         start_time *= ABS (mix->segment.rate);
750         end_time *= ABS (mix->segment.rate);
751       }
752
753       if (end_time >= output_start_time && start_time < output_end_time) {
754         GST_DEBUG_OBJECT (pad,
755             "Taking new buffer with start time %" GST_TIME_FORMAT,
756             GST_TIME_ARGS (start_time));
757         gst_buffer_replace (&mixcol->buffer, buf);
758         mixcol->start_time = start_time;
759         mixcol->end_time = end_time;
760
761         if (buf == mixcol->queued) {
762           gst_buffer_unref (buf);
763           gst_buffer_replace (&mixcol->queued, NULL);
764         } else {
765           gst_buffer_unref (buf);
766           buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
767           gst_buffer_unref (buf);
768         }
769         eos = FALSE;
770       } else if (start_time >= output_end_time) {
771         GST_DEBUG_OBJECT (pad, "Keeping buffer until %" GST_TIME_FORMAT,
772             GST_TIME_ARGS (start_time));
773         gst_buffer_unref (buf);
774         eos = FALSE;
775       } else {
776         GST_DEBUG_OBJECT (pad, "Too old buffer -- dropping");
777         if (buf == mixcol->queued) {
778           gst_buffer_unref (buf);
779           gst_buffer_replace (&mixcol->queued, NULL);
780         } else {
781           gst_buffer_unref (buf);
782           buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
783           gst_buffer_unref (buf);
784         }
785
786         need_more_data = TRUE;
787         continue;
788       }
789     } else {
790       if (mixcol->end_time != -1) {
791         if (mixcol->end_time < output_start_time) {
792           gst_buffer_replace (&mixcol->buffer, NULL);
793           mixcol->start_time = mixcol->end_time = -1;
794           if (!GST_COLLECT_PADS2_STATE_IS_SET (mixcol,
795                   GST_COLLECT_PADS2_STATE_EOS))
796             need_more_data = TRUE;
797         } else {
798           eos = FALSE;
799         }
800       }
801     }
802   }
803
804   if (need_more_data)
805     return 0;
806   if (eos)
807     return -1;
808
809   return 1;
810 }
811
812 static GstFlowReturn
813 gst_videomixer2_blend_buffers (GstVideoMixer2 * mix,
814     GstClockTime output_start_time, GstClockTime output_end_time,
815     GstBuffer ** outbuf)
816 {
817   GSList *l;
818   guint outsize;
819   BlendFunction composite;
820   GstVideoFrame outframe;
821   static GstAllocationParams params = { 0, 0, 0, 15, };
822
823   outsize = GST_VIDEO_INFO_SIZE (&mix->info);
824
825   *outbuf = gst_buffer_new_allocate (NULL, outsize, &params);
826   GST_BUFFER_TIMESTAMP (*outbuf) = output_start_time;
827   GST_BUFFER_DURATION (*outbuf) = output_end_time - output_start_time;
828
829   gst_video_frame_map (&outframe, &mix->info, *outbuf, GST_MAP_READWRITE);
830
831   /* default to blending */
832   composite = mix->blend;
833   switch (mix->background) {
834     case VIDEO_MIXER2_BACKGROUND_CHECKER:
835       mix->fill_checker (&outframe);
836       break;
837     case VIDEO_MIXER2_BACKGROUND_BLACK:
838       mix->fill_color (&outframe, 16, 128, 128);
839       break;
840     case VIDEO_MIXER2_BACKGROUND_WHITE:
841       mix->fill_color (&outframe, 240, 128, 128);
842       break;
843     case VIDEO_MIXER2_BACKGROUND_TRANSPARENT:
844       gst_buffer_memset (*outbuf, 0, 0, outsize);
845       /* use overlay to keep background transparent */
846       composite = mix->overlay;
847       break;
848   }
849
850   for (l = mix->sinkpads; l; l = l->next) {
851     GstVideoMixer2Pad *pad = l->data;
852     GstVideoMixer2Collect *mixcol = pad->mixcol;
853
854     if (mixcol->buffer != NULL) {
855       GstClockTime timestamp;
856       gint64 stream_time;
857       GstSegment *seg;
858       GstVideoFrame frame;
859
860       seg = &mixcol->collect.segment;
861
862       timestamp = GST_BUFFER_TIMESTAMP (mixcol->buffer);
863
864       stream_time =
865           gst_segment_to_stream_time (seg, GST_FORMAT_TIME, timestamp);
866
867       /* sync object properties on stream time */
868       if (GST_CLOCK_TIME_IS_VALID (stream_time))
869         gst_object_sync_values (GST_OBJECT (pad), stream_time);
870
871       gst_video_frame_map (&frame, &pad->info, mixcol->buffer, GST_MAP_READ);
872
873       composite (&frame, pad->xpos, pad->ypos, pad->alpha, &outframe);
874
875       gst_video_frame_unmap (&frame);
876     }
877   }
878   gst_video_frame_unmap (&outframe);
879
880   return GST_FLOW_OK;
881 }
882
883 /* Perform qos calculations before processing the next frame. Returns TRUE if
884  * the frame should be processed, FALSE if the frame can be dropped entirely */
885 static gint64
886 gst_videomixer2_do_qos (GstVideoMixer2 * mix, GstClockTime timestamp)
887 {
888   GstClockTime qostime, earliest_time;
889   gdouble proportion;
890   gint64 jitter;
891
892   /* no timestamp, can't do QoS => process frame */
893   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
894     GST_LOG_OBJECT (mix, "invalid timestamp, can't do QoS, process frame");
895     return -1;
896   }
897
898   /* get latest QoS observation values */
899   gst_videomixer2_read_qos (mix, &proportion, &earliest_time);
900
901   /* skip qos if we have no observation (yet) => process frame */
902   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
903     GST_LOG_OBJECT (mix, "no observation yet, process frame");
904     return -1;
905   }
906
907   /* qos is done on running time */
908   qostime =
909       gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME, timestamp);
910
911   /* see how our next timestamp relates to the latest qos timestamp */
912   GST_LOG_OBJECT (mix, "qostime %" GST_TIME_FORMAT ", earliest %"
913       GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
914
915   jitter = GST_CLOCK_DIFF (qostime, earliest_time);
916   if (qostime != GST_CLOCK_TIME_NONE && jitter > 0) {
917     GST_DEBUG_OBJECT (mix, "we are late, drop frame");
918     return jitter;
919   }
920
921   GST_LOG_OBJECT (mix, "process frame");
922   return jitter;
923 }
924
925 static GstFlowReturn
926 gst_videomixer2_collected (GstCollectPads2 * pads, GstVideoMixer2 * mix)
927 {
928   GstFlowReturn ret;
929   GstClockTime output_start_time, output_end_time;
930   GstBuffer *outbuf = NULL;
931   gint res;
932   gint64 jitter;
933
934   /* If we're not negotiated yet... */
935   if (GST_VIDEO_INFO_FORMAT (&mix->info) == GST_VIDEO_FORMAT_UNKNOWN)
936     return GST_FLOW_NOT_NEGOTIATED;
937
938   if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE, FALSE)) {
939     GST_DEBUG_OBJECT (mix, "pending flush stop");
940     gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop (TRUE));
941   }
942
943   GST_VIDEO_MIXER2_LOCK (mix);
944
945   if (mix->newseg_pending) {
946     GST_DEBUG_OBJECT (mix, "Sending NEWSEGMENT event");
947     if (!gst_pad_push_event (mix->srcpad,
948             gst_event_new_segment (&mix->segment))) {
949       ret = GST_FLOW_ERROR;
950       goto done;
951     }
952     mix->newseg_pending = FALSE;
953   }
954
955   if (mix->segment.position == -1)
956     output_start_time = mix->segment.start;
957   else
958     output_start_time = mix->segment.position;
959
960   if (output_start_time >= mix->segment.stop) {
961     GST_DEBUG_OBJECT (mix, "Segment done");
962     gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
963     ret = GST_FLOW_EOS;
964     goto done;
965   }
966
967   output_end_time =
968       mix->ts_offset + gst_util_uint64_scale (mix->nframes + 1,
969       GST_SECOND * GST_VIDEO_INFO_FPS_D (&mix->info),
970       GST_VIDEO_INFO_FPS_N (&mix->info));
971   if (mix->segment.stop != -1)
972     output_end_time = MIN (output_end_time, mix->segment.stop);
973
974   res = gst_videomixer2_fill_queues (mix, output_start_time, output_end_time);
975
976   if (res == 0) {
977     GST_DEBUG_OBJECT (mix, "Need more data for decisions");
978     ret = GST_FLOW_OK;
979     goto done;
980   } else if (res == -1) {
981     GST_DEBUG_OBJECT (mix, "All sinkpads are EOS -- forwarding");
982     gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
983     ret = GST_FLOW_EOS;
984     goto done;
985   } else if (res == -2) {
986     GST_ERROR_OBJECT (mix, "Error collecting buffers");
987     ret = GST_FLOW_ERROR;
988     goto done;
989   }
990
991   jitter = gst_videomixer2_do_qos (mix, output_start_time);
992   if (jitter <= 0) {
993     ret =
994         gst_videomixer2_blend_buffers (mix, output_start_time,
995         output_end_time, &outbuf);
996     mix->qos_processed++;
997   } else {
998     GstMessage *msg;
999
1000     mix->qos_dropped++;
1001
1002     /* TODO: live */
1003     msg =
1004         gst_message_new_qos (GST_OBJECT_CAST (mix), FALSE,
1005         gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME,
1006             output_start_time), gst_segment_to_stream_time (&mix->segment,
1007             GST_FORMAT_TIME, output_start_time), output_start_time,
1008         output_end_time - output_start_time);
1009     gst_message_set_qos_values (msg, jitter, mix->proportion, 1000000);
1010     gst_message_set_qos_stats (msg, GST_FORMAT_BUFFERS, mix->qos_processed,
1011         mix->qos_dropped);
1012     gst_element_post_message (GST_ELEMENT_CAST (mix), msg);
1013
1014     ret = GST_FLOW_OK;
1015   }
1016
1017   mix->segment.position = output_end_time;
1018   mix->nframes++;
1019
1020   GST_VIDEO_MIXER2_UNLOCK (mix);
1021   if (outbuf) {
1022     GST_LOG_OBJECT (mix,
1023         "Pushing buffer with ts %" GST_TIME_FORMAT " and duration %"
1024         GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1025         GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
1026     ret = gst_pad_push (mix->srcpad, outbuf);
1027   }
1028   GST_VIDEO_MIXER2_LOCK (mix);
1029
1030 done:
1031   GST_VIDEO_MIXER2_UNLOCK (mix);
1032
1033   return ret;
1034 }
1035
1036 static gboolean
1037 gst_videomixer2_query_caps (GstPad * pad, GstObject * parent, GstQuery * query)
1038 {
1039   GstCaps *filter, *caps;
1040   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
1041   GstStructure *s;
1042   gint n;
1043
1044   gst_query_parse_caps (query, &filter);
1045
1046   if (GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_FORMAT_UNKNOWN) {
1047     caps = gst_pad_get_current_caps (mix->srcpad);
1048   } else {
1049     caps = gst_pad_get_pad_template_caps (mix->srcpad);
1050   }
1051
1052   caps = gst_caps_make_writable (caps);
1053
1054   n = gst_caps_get_size (caps) - 1;
1055   for (; n >= 0; n--) {
1056     s = gst_caps_get_structure (caps, n);
1057     gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1058         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
1059     if (GST_VIDEO_INFO_FPS_D (&mix->info) != 0) {
1060       gst_structure_set (s,
1061           "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1062     }
1063   }
1064   gst_query_set_caps_result (query, caps);
1065
1066   return TRUE;
1067 }
1068
1069 static gboolean
1070 gst_videomixer2_query_duration (GstVideoMixer2 * mix, GstQuery * query)
1071 {
1072   GValue item = { 0 };
1073   gint64 max;
1074   gboolean res;
1075   GstFormat format;
1076   GstIterator *it;
1077   gboolean done;
1078
1079   /* parse format */
1080   gst_query_parse_duration (query, &format, NULL);
1081
1082   max = -1;
1083   res = TRUE;
1084   done = FALSE;
1085
1086   /* Take maximum of all durations */
1087   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1088   while (!done) {
1089     switch (gst_iterator_next (it, &item)) {
1090       case GST_ITERATOR_DONE:
1091         done = TRUE;
1092         break;
1093       case GST_ITERATOR_OK:
1094       {
1095         GstPad *pad;
1096         gint64 duration;
1097
1098         pad = g_value_get_object (&item);
1099
1100         /* ask sink peer for duration */
1101         res &= gst_pad_peer_query_duration (pad, format, &duration);
1102         /* take max from all valid return values */
1103         if (res) {
1104           /* valid unknown length, stop searching */
1105           if (duration == -1) {
1106             max = duration;
1107             done = TRUE;
1108           }
1109           /* else see if bigger than current max */
1110           else if (duration > max)
1111             max = duration;
1112         }
1113         g_value_reset (&item);
1114         break;
1115       }
1116       case GST_ITERATOR_RESYNC:
1117         max = -1;
1118         res = TRUE;
1119         gst_iterator_resync (it);
1120         break;
1121       default:
1122         res = FALSE;
1123         done = TRUE;
1124         break;
1125     }
1126   }
1127   g_value_unset (&item);
1128   gst_iterator_free (it);
1129
1130   if (res) {
1131     /* and store the max */
1132     GST_DEBUG_OBJECT (mix, "Total duration in format %s: %"
1133         GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1134     gst_query_set_duration (query, format, max);
1135   }
1136
1137   return res;
1138 }
1139
1140 static gboolean
1141 gst_videomixer2_query_latency (GstVideoMixer2 * mix, GstQuery * query)
1142 {
1143   GstClockTime min, max;
1144   gboolean live;
1145   gboolean res;
1146   GstIterator *it;
1147   gboolean done;
1148   GValue item = { 0 };
1149
1150   res = TRUE;
1151   done = FALSE;
1152   live = FALSE;
1153   min = 0;
1154   max = GST_CLOCK_TIME_NONE;
1155
1156   /* Take maximum of all latency values */
1157   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1158   while (!done) {
1159     switch (gst_iterator_next (it, &item)) {
1160       case GST_ITERATOR_DONE:
1161         done = TRUE;
1162         break;
1163       case GST_ITERATOR_OK:
1164       {
1165         GstPad *pad = g_value_get_object (&item);
1166         GstQuery *peerquery;
1167         GstClockTime min_cur, max_cur;
1168         gboolean live_cur;
1169
1170         peerquery = gst_query_new_latency ();
1171
1172         /* Ask peer for latency */
1173         res &= gst_pad_peer_query (pad, peerquery);
1174
1175         /* take max from all valid return values */
1176         if (res) {
1177           gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
1178
1179           if (min_cur > min)
1180             min = min_cur;
1181
1182           if (max_cur != GST_CLOCK_TIME_NONE &&
1183               ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
1184                   (max == GST_CLOCK_TIME_NONE)))
1185             max = max_cur;
1186
1187           live = live || live_cur;
1188         }
1189
1190         gst_query_unref (peerquery);
1191         g_value_reset (&item);
1192         break;
1193       }
1194       case GST_ITERATOR_RESYNC:
1195         live = FALSE;
1196         min = 0;
1197         max = GST_CLOCK_TIME_NONE;
1198         res = TRUE;
1199         gst_iterator_resync (it);
1200         break;
1201       default:
1202         res = FALSE;
1203         done = TRUE;
1204         break;
1205     }
1206   }
1207   g_value_unset (&item);
1208   gst_iterator_free (it);
1209
1210   if (res) {
1211     /* store the results */
1212     GST_DEBUG_OBJECT (mix, "Calculated total latency: live %s, min %"
1213         GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
1214         (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1215     gst_query_set_latency (query, live, min, max);
1216   }
1217
1218   return res;
1219 }
1220
1221 static gboolean
1222 gst_videomixer2_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1223 {
1224   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
1225   gboolean res = FALSE;
1226
1227   switch (GST_QUERY_TYPE (query)) {
1228     case GST_QUERY_POSITION:
1229     {
1230       GstFormat format;
1231
1232       gst_query_parse_position (query, &format, NULL);
1233
1234       switch (format) {
1235         case GST_FORMAT_TIME:
1236           gst_query_set_position (query, format,
1237               gst_segment_to_stream_time (&mix->segment, GST_FORMAT_TIME,
1238                   mix->segment.position));
1239           res = TRUE;
1240           break;
1241         default:
1242           break;
1243       }
1244       break;
1245     }
1246     case GST_QUERY_DURATION:
1247       res = gst_videomixer2_query_duration (mix, query);
1248       break;
1249     case GST_QUERY_LATENCY:
1250       res = gst_videomixer2_query_latency (mix, query);
1251       break;
1252     case GST_QUERY_CAPS:
1253       res = gst_videomixer2_query_caps (pad, parent, query);
1254       break;
1255     default:
1256       /* FIXME, needs a custom query handler because we have multiple
1257        * sinkpads */
1258       res = FALSE;
1259       gst_query_unref (query);
1260       break;
1261   }
1262   return res;
1263 }
1264
1265 static gboolean
1266 gst_videomixer2_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1267 {
1268   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
1269   gboolean result;
1270
1271   switch (GST_EVENT_TYPE (event)) {
1272     case GST_EVENT_QOS:
1273     {
1274       GstQOSType type;
1275       GstClockTimeDiff diff;
1276       GstClockTime timestamp;
1277       gdouble proportion;
1278
1279       gst_event_parse_qos (event, &type, &proportion, &diff, &timestamp);
1280
1281       gst_videomixer2_update_qos (mix, proportion, diff, timestamp);
1282
1283       result = gst_videomixer2_push_sink_event (mix, event);
1284       break;
1285     }
1286     case GST_EVENT_SEEK:
1287     {
1288       gdouble rate;
1289       GstFormat fmt;
1290       GstSeekFlags flags;
1291       GstSeekType start_type, stop_type;
1292       gint64 start, stop;
1293       GSList *l;
1294       gdouble abs_rate;
1295
1296       /* parse the seek parameters */
1297       gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1298           &start, &stop_type, &stop);
1299
1300       if (rate <= 0.0) {
1301         GST_ERROR_OBJECT (mix, "Negative rates not supported yet");
1302         result = FALSE;
1303         gst_event_unref (event);
1304         break;
1305       }
1306
1307       GST_DEBUG_OBJECT (mix, "Handling SEEK event");
1308
1309       /* check if we are flushing */
1310       if (flags & GST_SEEK_FLAG_FLUSH) {
1311         /* flushing seek, start flush downstream, the flush will be done
1312          * when all pads received a FLUSH_STOP. */
1313         gst_pad_push_event (mix->srcpad, gst_event_new_flush_start ());
1314
1315         /* make sure we accept nothing anymore and return WRONG_STATE */
1316         gst_collect_pads2_set_flushing (mix->collect, TRUE);
1317       }
1318
1319       /* now wait for the collected to be finished and mark a new
1320        * segment */
1321       GST_COLLECT_PADS2_STREAM_LOCK (mix->collect);
1322
1323       abs_rate = ABS (rate);
1324
1325       GST_VIDEO_MIXER2_LOCK (mix);
1326       for (l = mix->sinkpads; l; l = l->next) {
1327         GstVideoMixer2Pad *p = l->data;
1328
1329         if (flags & GST_SEEK_FLAG_FLUSH) {
1330           gst_buffer_replace (&p->mixcol->buffer, NULL);
1331           p->mixcol->start_time = p->mixcol->end_time = -1;
1332           continue;
1333         }
1334
1335         /* Convert to the output segment rate */
1336         if (ABS (mix->segment.rate) != abs_rate) {
1337           if (ABS (mix->segment.rate) != 1.0 && p->mixcol->buffer) {
1338             p->mixcol->start_time /= ABS (mix->segment.rate);
1339             p->mixcol->end_time /= ABS (mix->segment.rate);
1340           }
1341           if (abs_rate != 1.0 && p->mixcol->buffer) {
1342             p->mixcol->start_time *= abs_rate;
1343             p->mixcol->end_time *= abs_rate;
1344           }
1345         }
1346       }
1347       GST_VIDEO_MIXER2_UNLOCK (mix);
1348
1349       gst_segment_do_seek (&mix->segment, rate, fmt, flags, start_type, start,
1350           stop_type, stop, NULL);
1351       mix->segment.position = -1;
1352       mix->ts_offset = 0;
1353       mix->nframes = 0;
1354       mix->newseg_pending = TRUE;
1355
1356       if (flags & GST_SEEK_FLAG_FLUSH) {
1357         gst_collect_pads2_set_flushing (mix->collect, FALSE);
1358
1359         /* we can't send FLUSH_STOP here since upstream could start pushing data
1360          * after we unlock mix->collect.
1361          * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
1362          * forwarding the seek upstream or from gst_videomixer_collected,
1363          * whichever happens first.
1364          */
1365         mix->flush_stop_pending = TRUE;
1366       }
1367
1368       GST_COLLECT_PADS2_STREAM_UNLOCK (mix->collect);
1369
1370       gst_videomixer2_reset_qos (mix);
1371
1372       result = gst_videomixer2_push_sink_event (mix, event);
1373
1374       if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE,
1375               FALSE)) {
1376         GST_DEBUG_OBJECT (mix, "pending flush stop");
1377         gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop (TRUE));
1378       }
1379
1380       break;
1381     }
1382     case GST_EVENT_NAVIGATION:
1383       /* navigation is rather pointless. */
1384       result = FALSE;
1385       gst_event_unref (event);
1386       break;
1387     default:
1388       /* just forward the rest for now */
1389       result = gst_videomixer2_push_sink_event (mix, event);
1390       break;
1391   }
1392
1393   return result;
1394 }
1395
1396 static gboolean
1397 gst_videomixer2_src_setcaps (GstPad * pad, GstVideoMixer2 * mix, GstCaps * caps)
1398 {
1399   gboolean ret = FALSE;
1400   GstVideoInfo info;
1401
1402   GST_INFO_OBJECT (pad, "set src caps: %" GST_PTR_FORMAT, caps);
1403
1404   mix->blend = NULL;
1405   mix->overlay = NULL;
1406   mix->fill_checker = NULL;
1407   mix->fill_color = NULL;
1408
1409   if (!gst_video_info_from_caps (&info, caps))
1410     goto done;
1411
1412   GST_VIDEO_MIXER2_LOCK (mix);
1413
1414   if (GST_VIDEO_INFO_FPS_N (&mix->info) != GST_VIDEO_INFO_FPS_N (&info) ||
1415       GST_VIDEO_INFO_FPS_D (&mix->info) != GST_VIDEO_INFO_FPS_D (&info)) {
1416     if (mix->segment.position != -1) {
1417       mix->ts_offset = mix->segment.position - mix->segment.start;
1418       mix->nframes = 0;
1419     }
1420     gst_videomixer2_reset_qos (mix);
1421   }
1422
1423   mix->info = info;
1424
1425   switch (GST_VIDEO_INFO_FORMAT (&mix->info)) {
1426     case GST_VIDEO_FORMAT_AYUV:
1427       mix->blend = gst_video_mixer_blend_ayuv;
1428       mix->overlay = gst_video_mixer_overlay_ayuv;
1429       mix->fill_checker = gst_video_mixer_fill_checker_ayuv;
1430       mix->fill_color = gst_video_mixer_fill_color_ayuv;
1431       ret = TRUE;
1432       break;
1433     case GST_VIDEO_FORMAT_ARGB:
1434       mix->blend = gst_video_mixer_blend_argb;
1435       mix->overlay = gst_video_mixer_overlay_argb;
1436       mix->fill_checker = gst_video_mixer_fill_checker_argb;
1437       mix->fill_color = gst_video_mixer_fill_color_argb;
1438       ret = TRUE;
1439       break;
1440     case GST_VIDEO_FORMAT_BGRA:
1441       mix->blend = gst_video_mixer_blend_bgra;
1442       mix->overlay = gst_video_mixer_overlay_bgra;
1443       mix->fill_checker = gst_video_mixer_fill_checker_bgra;
1444       mix->fill_color = gst_video_mixer_fill_color_bgra;
1445       ret = TRUE;
1446       break;
1447     case GST_VIDEO_FORMAT_ABGR:
1448       mix->blend = gst_video_mixer_blend_abgr;
1449       mix->overlay = gst_video_mixer_overlay_abgr;
1450       mix->fill_checker = gst_video_mixer_fill_checker_abgr;
1451       mix->fill_color = gst_video_mixer_fill_color_abgr;
1452       ret = TRUE;
1453       break;
1454     case GST_VIDEO_FORMAT_RGBA:
1455       mix->blend = gst_video_mixer_blend_rgba;
1456       mix->overlay = gst_video_mixer_overlay_rgba;
1457       mix->fill_checker = gst_video_mixer_fill_checker_rgba;
1458       mix->fill_color = gst_video_mixer_fill_color_rgba;
1459       ret = TRUE;
1460       break;
1461     case GST_VIDEO_FORMAT_Y444:
1462       mix->blend = gst_video_mixer_blend_y444;
1463       mix->overlay = mix->blend;
1464       mix->fill_checker = gst_video_mixer_fill_checker_y444;
1465       mix->fill_color = gst_video_mixer_fill_color_y444;
1466       ret = TRUE;
1467       break;
1468     case GST_VIDEO_FORMAT_Y42B:
1469       mix->blend = gst_video_mixer_blend_y42b;
1470       mix->overlay = mix->blend;
1471       mix->fill_checker = gst_video_mixer_fill_checker_y42b;
1472       mix->fill_color = gst_video_mixer_fill_color_y42b;
1473       ret = TRUE;
1474       break;
1475     case GST_VIDEO_FORMAT_YUY2:
1476       mix->blend = gst_video_mixer_blend_yuy2;
1477       mix->overlay = mix->blend;
1478       mix->fill_checker = gst_video_mixer_fill_checker_yuy2;
1479       mix->fill_color = gst_video_mixer_fill_color_yuy2;
1480       ret = TRUE;
1481       break;
1482     case GST_VIDEO_FORMAT_UYVY:
1483       mix->blend = gst_video_mixer_blend_uyvy;
1484       mix->overlay = mix->blend;
1485       mix->fill_checker = gst_video_mixer_fill_checker_uyvy;
1486       mix->fill_color = gst_video_mixer_fill_color_uyvy;
1487       ret = TRUE;
1488       break;
1489     case GST_VIDEO_FORMAT_YVYU:
1490       mix->blend = gst_video_mixer_blend_yvyu;
1491       mix->overlay = mix->blend;
1492       mix->fill_checker = gst_video_mixer_fill_checker_yvyu;
1493       mix->fill_color = gst_video_mixer_fill_color_yvyu;
1494       ret = TRUE;
1495       break;
1496     case GST_VIDEO_FORMAT_I420:
1497       mix->blend = gst_video_mixer_blend_i420;
1498       mix->overlay = mix->blend;
1499       mix->fill_checker = gst_video_mixer_fill_checker_i420;
1500       mix->fill_color = gst_video_mixer_fill_color_i420;
1501       ret = TRUE;
1502       break;
1503     case GST_VIDEO_FORMAT_YV12:
1504       mix->blend = gst_video_mixer_blend_yv12;
1505       mix->overlay = mix->blend;
1506       mix->fill_checker = gst_video_mixer_fill_checker_yv12;
1507       mix->fill_color = gst_video_mixer_fill_color_yv12;
1508       ret = TRUE;
1509       break;
1510     case GST_VIDEO_FORMAT_Y41B:
1511       mix->blend = gst_video_mixer_blend_y41b;
1512       mix->overlay = mix->blend;
1513       mix->fill_checker = gst_video_mixer_fill_checker_y41b;
1514       mix->fill_color = gst_video_mixer_fill_color_y41b;
1515       ret = TRUE;
1516       break;
1517     case GST_VIDEO_FORMAT_RGB:
1518       mix->blend = gst_video_mixer_blend_rgb;
1519       mix->overlay = mix->blend;
1520       mix->fill_checker = gst_video_mixer_fill_checker_rgb;
1521       mix->fill_color = gst_video_mixer_fill_color_rgb;
1522       ret = TRUE;
1523       break;
1524     case GST_VIDEO_FORMAT_BGR:
1525       mix->blend = gst_video_mixer_blend_bgr;
1526       mix->overlay = mix->blend;
1527       mix->fill_checker = gst_video_mixer_fill_checker_bgr;
1528       mix->fill_color = gst_video_mixer_fill_color_bgr;
1529       ret = TRUE;
1530       break;
1531     case GST_VIDEO_FORMAT_xRGB:
1532       mix->blend = gst_video_mixer_blend_xrgb;
1533       mix->overlay = mix->blend;
1534       mix->fill_checker = gst_video_mixer_fill_checker_xrgb;
1535       mix->fill_color = gst_video_mixer_fill_color_xrgb;
1536       ret = TRUE;
1537       break;
1538     case GST_VIDEO_FORMAT_xBGR:
1539       mix->blend = gst_video_mixer_blend_xbgr;
1540       mix->overlay = mix->blend;
1541       mix->fill_checker = gst_video_mixer_fill_checker_xbgr;
1542       mix->fill_color = gst_video_mixer_fill_color_xbgr;
1543       ret = TRUE;
1544       break;
1545     case GST_VIDEO_FORMAT_RGBx:
1546       mix->blend = gst_video_mixer_blend_rgbx;
1547       mix->overlay = mix->blend;
1548       mix->fill_checker = gst_video_mixer_fill_checker_rgbx;
1549       mix->fill_color = gst_video_mixer_fill_color_rgbx;
1550       ret = TRUE;
1551       break;
1552     case GST_VIDEO_FORMAT_BGRx:
1553       mix->blend = gst_video_mixer_blend_bgrx;
1554       mix->overlay = mix->blend;
1555       mix->fill_checker = gst_video_mixer_fill_checker_bgrx;
1556       mix->fill_color = gst_video_mixer_fill_color_bgrx;
1557       ret = TRUE;
1558       break;
1559     default:
1560       break;
1561   }
1562   GST_VIDEO_MIXER2_UNLOCK (mix);
1563
1564   ret = gst_pad_set_caps (pad, caps);
1565 done:
1566
1567   return ret;
1568 }
1569
1570 static GstFlowReturn
1571 gst_videomixer2_sink_clip (GstCollectPads2 * pads,
1572     GstCollectData2 * data, GstBuffer * buf, GstBuffer ** outbuf,
1573     GstVideoMixer2 * mix)
1574 {
1575   GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (data->pad);
1576   GstVideoMixer2Collect *mixcol = pad->mixcol;
1577   GstClockTime start_time, end_time;
1578
1579   start_time = GST_BUFFER_TIMESTAMP (buf);
1580   if (start_time == -1) {
1581     GST_ERROR_OBJECT (pad, "Timestamped buffers required!");
1582     gst_buffer_unref (buf);
1583     return GST_FLOW_ERROR;
1584   }
1585
1586   end_time = GST_BUFFER_DURATION (buf);
1587   if (end_time == -1)
1588     end_time =
1589         gst_util_uint64_scale_int (GST_SECOND,
1590         GST_VIDEO_INFO_FPS_D (&pad->info), GST_VIDEO_INFO_FPS_N (&pad->info));
1591   if (end_time == -1) {
1592     *outbuf = buf;
1593     return GST_FLOW_OK;
1594   }
1595
1596   start_time = MAX (start_time, mixcol->collect.segment.start);
1597   start_time =
1598       gst_segment_to_running_time (&mixcol->collect.segment,
1599       GST_FORMAT_TIME, start_time);
1600
1601   end_time += GST_BUFFER_TIMESTAMP (buf);
1602   if (mixcol->collect.segment.stop != -1)
1603     end_time = MIN (end_time, mixcol->collect.segment.stop);
1604   end_time =
1605       gst_segment_to_running_time (&mixcol->collect.segment,
1606       GST_FORMAT_TIME, end_time);
1607
1608   /* Convert to the output segment rate */
1609   if (ABS (mix->segment.rate) != 1.0) {
1610     start_time *= ABS (mix->segment.rate);
1611     end_time *= ABS (mix->segment.rate);
1612   }
1613
1614   if (mixcol->buffer != NULL && end_time < mixcol->end_time) {
1615     gst_buffer_unref (buf);
1616     *outbuf = NULL;
1617     return GST_FLOW_OK;
1618   }
1619
1620   *outbuf = buf;
1621   return GST_FLOW_OK;
1622 }
1623
1624 static gboolean
1625 gst_videomixer2_sink_event (GstCollectPads2 * pads, GstCollectData2 * cdata,
1626     GstEvent * event, GstVideoMixer2 * mix)
1627 {
1628   GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (cdata->pad);
1629   gboolean ret = TRUE;
1630
1631   GST_DEBUG_OBJECT (pad, "Got %s event on pad %s:%s",
1632       GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (pad));
1633
1634   switch (GST_EVENT_TYPE (event)) {
1635     case GST_EVENT_CAPS:
1636     {
1637       GstCaps *caps;
1638
1639       gst_event_parse_caps (event, &caps);
1640       ret =
1641           gst_videomixer2_pad_sink_setcaps (GST_PAD (pad), GST_OBJECT (mix),
1642           caps);
1643       gst_event_unref (event);
1644       event = NULL;
1645       break;
1646     }
1647     case GST_EVENT_SEGMENT:{
1648       GstSegment seg;
1649       gst_event_copy_segment (event, &seg);
1650
1651       g_assert (seg.format == GST_FORMAT_TIME);
1652       break;
1653     }
1654     case GST_EVENT_FLUSH_STOP:
1655       mix->newseg_pending = TRUE;
1656       mix->flush_stop_pending = FALSE;
1657       gst_videomixer2_reset_qos (mix);
1658       gst_buffer_replace (&pad->mixcol->buffer, NULL);
1659       pad->mixcol->start_time = -1;
1660       pad->mixcol->end_time = -1;
1661
1662       gst_segment_init (&mix->segment, GST_FORMAT_TIME);
1663       mix->segment.position = -1;
1664       mix->ts_offset = 0;
1665       mix->nframes = 0;
1666       break;
1667     default:
1668       break;
1669   }
1670
1671   if (event != NULL)
1672     return gst_collect_pads2_event_default (pads, cdata, event, FALSE);
1673
1674   return ret;
1675 }
1676
1677 static gboolean
1678 forward_event_func (GValue * item, GValue * ret, GstEvent * event)
1679 {
1680   GstPad *pad = g_value_get_object (item);
1681   gst_event_ref (event);
1682   GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
1683   if (!gst_pad_push_event (pad, event)) {
1684     g_value_set_boolean (ret, FALSE);
1685     GST_WARNING_OBJECT (pad, "Sending event  %p (%s) failed.",
1686         event, GST_EVENT_TYPE_NAME (event));
1687   } else {
1688     GST_LOG_OBJECT (pad, "Sent event  %p (%s).",
1689         event, GST_EVENT_TYPE_NAME (event));
1690   }
1691   return TRUE;
1692 }
1693
1694 static gboolean
1695 gst_videomixer2_push_sink_event (GstVideoMixer2 * mix, GstEvent * event)
1696 {
1697   GstIterator *it;
1698   GValue vret = { 0 };
1699
1700   GST_LOG_OBJECT (mix, "Forwarding event %p (%s)", event,
1701       GST_EVENT_TYPE_NAME (event));
1702
1703   g_value_init (&vret, G_TYPE_BOOLEAN);
1704   g_value_set_boolean (&vret, TRUE);
1705   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1706   gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
1707       event);
1708   gst_iterator_free (it);
1709   gst_event_unref (event);
1710
1711   return g_value_get_boolean (&vret);
1712 }
1713
1714 /* GstElement vmethods */
1715 static GstStateChangeReturn
1716 gst_videomixer2_change_state (GstElement * element, GstStateChange transition)
1717 {
1718   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (element);
1719   GstStateChangeReturn ret;
1720
1721   switch (transition) {
1722     case GST_STATE_CHANGE_READY_TO_PAUSED:
1723       GST_LOG_OBJECT (mix, "starting collectpads");
1724       gst_collect_pads2_start (mix->collect);
1725       break;
1726     case GST_STATE_CHANGE_PAUSED_TO_READY:
1727       GST_LOG_OBJECT (mix, "stopping collectpads");
1728       gst_collect_pads2_stop (mix->collect);
1729       break;
1730     default:
1731       break;
1732   }
1733
1734   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1735
1736   switch (transition) {
1737     case GST_STATE_CHANGE_PAUSED_TO_READY:
1738       gst_videomixer2_reset (mix);
1739       break;
1740     default:
1741       break;
1742   }
1743
1744   return ret;
1745 }
1746
1747 static GstPad *
1748 gst_videomixer2_request_new_pad (GstElement * element,
1749     GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1750 {
1751   GstVideoMixer2 *mix;
1752   GstVideoMixer2Pad *mixpad;
1753   GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1754
1755   mix = GST_VIDEO_MIXER2 (element);
1756
1757   if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
1758     gint serial = 0;
1759     gchar *name = NULL;
1760     GstVideoMixer2Collect *mixcol = NULL;
1761
1762     GST_VIDEO_MIXER2_LOCK (mix);
1763     if (req_name == NULL || strlen (req_name) < 6
1764         || !g_str_has_prefix (req_name, "sink_")) {
1765       /* no name given when requesting the pad, use next available int */
1766       serial = mix->next_sinkpad++;
1767     } else {
1768       /* parse serial number from requested padname */
1769       serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1770       if (serial >= mix->next_sinkpad)
1771         mix->next_sinkpad = serial + 1;
1772     }
1773     /* create new pad with the name */
1774     name = g_strdup_printf ("sink_%d", serial);
1775     mixpad = g_object_new (GST_TYPE_VIDEO_MIXER2_PAD, "name", name, "direction",
1776         templ->direction, "template", templ, NULL);
1777     g_free (name);
1778
1779     mixpad->zorder = mix->numpads;
1780     mixpad->xpos = DEFAULT_PAD_XPOS;
1781     mixpad->ypos = DEFAULT_PAD_YPOS;
1782     mixpad->alpha = DEFAULT_PAD_ALPHA;
1783
1784     mixcol = (GstVideoMixer2Collect *)
1785         gst_collect_pads2_add_pad_full (mix->collect, GST_PAD (mixpad),
1786         sizeof (GstVideoMixer2Collect),
1787         (GstCollectData2DestroyNotify) gst_videomixer2_collect_free, TRUE);
1788
1789     /* Keep track of each other */
1790     mixcol->mixpad = mixpad;
1791     mixpad->mixcol = mixcol;
1792
1793     mixcol->start_time = -1;
1794     mixcol->end_time = -1;
1795
1796     /* Keep an internal list of mixpads for zordering */
1797     mix->sinkpads = g_slist_append (mix->sinkpads, mixpad);
1798     mix->numpads++;
1799     GST_VIDEO_MIXER2_UNLOCK (mix);
1800   } else {
1801     return NULL;
1802   }
1803
1804   GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (mixpad));
1805
1806   /* add the pad to the element */
1807   gst_element_add_pad (element, GST_PAD (mixpad));
1808   gst_child_proxy_child_added (G_OBJECT (mix), G_OBJECT (mixpad),
1809       GST_OBJECT_NAME (mixpad));
1810
1811   return GST_PAD (mixpad);
1812 }
1813
1814 static void
1815 gst_videomixer2_release_pad (GstElement * element, GstPad * pad)
1816 {
1817   GstVideoMixer2 *mix = NULL;
1818   GstVideoMixer2Pad *mixpad;
1819   gboolean update_caps;
1820
1821   mix = GST_VIDEO_MIXER2 (element);
1822
1823   GST_VIDEO_MIXER2_LOCK (mix);
1824   if (G_UNLIKELY (g_slist_find (mix->sinkpads, pad) == NULL)) {
1825     g_warning ("Unknown pad %s", GST_PAD_NAME (pad));
1826     goto error;
1827   }
1828
1829   mixpad = GST_VIDEO_MIXER2_PAD (pad);
1830
1831   mix->sinkpads = g_slist_remove (mix->sinkpads, pad);
1832   gst_child_proxy_child_removed (G_OBJECT (mix), G_OBJECT (mixpad),
1833       GST_OBJECT_NAME (mixpad));
1834   mix->numpads--;
1835
1836   update_caps = GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_FORMAT_UNKNOWN;
1837   GST_VIDEO_MIXER2_UNLOCK (mix);
1838
1839   gst_collect_pads2_remove_pad (mix->collect, pad);
1840
1841   if (update_caps)
1842     gst_videomixer2_update_src_caps (mix);
1843
1844   gst_element_remove_pad (element, pad);
1845   return;
1846 error:
1847   GST_VIDEO_MIXER2_UNLOCK (mix);
1848 }
1849
1850 /* GObject vmethods */
1851 static void
1852 gst_videomixer2_finalize (GObject * o)
1853 {
1854   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (o);
1855
1856   gst_object_unref (mix->collect);
1857   g_mutex_clear (&mix->lock);
1858
1859   G_OBJECT_CLASS (parent_class)->finalize (o);
1860 }
1861
1862 static void
1863 gst_videomixer2_get_property (GObject * object,
1864     guint prop_id, GValue * value, GParamSpec * pspec)
1865 {
1866   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1867
1868   switch (prop_id) {
1869     case PROP_BACKGROUND:
1870       g_value_set_enum (value, mix->background);
1871       break;
1872     default:
1873       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1874       break;
1875   }
1876 }
1877
1878 static void
1879 gst_videomixer2_set_property (GObject * object,
1880     guint prop_id, const GValue * value, GParamSpec * pspec)
1881 {
1882   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1883
1884   switch (prop_id) {
1885     case PROP_BACKGROUND:
1886       mix->background = g_value_get_enum (value);
1887       break;
1888     default:
1889       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1890       break;
1891   }
1892 }
1893
1894 /* GstChildProxy implementation */
1895 static GObject *
1896 gst_videomixer2_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
1897     guint index)
1898 {
1899   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1900   GObject *obj;
1901
1902   GST_VIDEO_MIXER2_LOCK (mix);
1903   if ((obj = g_slist_nth_data (mix->sinkpads, index)))
1904     g_object_ref (obj);
1905   GST_VIDEO_MIXER2_UNLOCK (mix);
1906   return obj;
1907 }
1908
1909 static guint
1910 gst_videomixer2_child_proxy_get_children_count (GstChildProxy * child_proxy)
1911 {
1912   guint count = 0;
1913   GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1914
1915   GST_VIDEO_MIXER2_LOCK (mix);
1916   count = mix->numpads;
1917   GST_VIDEO_MIXER2_UNLOCK (mix);
1918   GST_INFO_OBJECT (mix, "Children Count: %d", count);
1919   return count;
1920 }
1921
1922 static void
1923 gst_videomixer2_child_proxy_init (gpointer g_iface, gpointer iface_data)
1924 {
1925   GstChildProxyInterface *iface = g_iface;
1926
1927   GST_INFO ("intializing child proxy interface");
1928   iface->get_child_by_index = gst_videomixer2_child_proxy_get_child_by_index;
1929   iface->get_children_count = gst_videomixer2_child_proxy_get_children_count;
1930 }
1931
1932 /* GObject boilerplate */
1933 static void
1934 gst_videomixer2_class_init (GstVideoMixer2Class * klass)
1935 {
1936   GObjectClass *gobject_class = (GObjectClass *) klass;
1937   GstElementClass *gstelement_class = (GstElementClass *) klass;
1938
1939   gobject_class->finalize = gst_videomixer2_finalize;
1940
1941   gobject_class->get_property = gst_videomixer2_get_property;
1942   gobject_class->set_property = gst_videomixer2_set_property;
1943
1944   g_object_class_install_property (gobject_class, PROP_BACKGROUND,
1945       g_param_spec_enum ("background", "Background", "Background type",
1946           GST_TYPE_VIDEO_MIXER2_BACKGROUND,
1947           DEFAULT_BACKGROUND, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1948
1949   gstelement_class->request_new_pad =
1950       GST_DEBUG_FUNCPTR (gst_videomixer2_request_new_pad);
1951   gstelement_class->release_pad =
1952       GST_DEBUG_FUNCPTR (gst_videomixer2_release_pad);
1953   gstelement_class->change_state =
1954       GST_DEBUG_FUNCPTR (gst_videomixer2_change_state);
1955
1956   gst_element_class_add_pad_template (gstelement_class,
1957       gst_static_pad_template_get (&src_factory));
1958   gst_element_class_add_pad_template (gstelement_class,
1959       gst_static_pad_template_get (&sink_factory));
1960
1961   gst_element_class_set_static_metadata (gstelement_class, "Video mixer 2",
1962       "Filter/Editor/Video",
1963       "Mix multiple video streams", "Wim Taymans <wim@fluendo.com>, "
1964       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1965
1966   /* Register the pad class */
1967   g_type_class_ref (GST_TYPE_VIDEO_MIXER2_PAD);
1968 }
1969
1970 static void
1971 gst_videomixer2_init (GstVideoMixer2 * mix)
1972 {
1973   GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix);
1974
1975   mix->srcpad =
1976       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
1977           "src"), "src");
1978   gst_pad_set_query_function (GST_PAD (mix->srcpad),
1979       GST_DEBUG_FUNCPTR (gst_videomixer2_src_query));
1980   gst_pad_set_event_function (GST_PAD (mix->srcpad),
1981       GST_DEBUG_FUNCPTR (gst_videomixer2_src_event));
1982   gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad);
1983
1984   mix->collect = gst_collect_pads2_new ();
1985   mix->background = DEFAULT_BACKGROUND;
1986
1987   gst_collect_pads2_set_function (mix->collect,
1988       (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_videomixer2_collected),
1989       mix);
1990   gst_collect_pads2_set_event_function (mix->collect,
1991       (GstCollectPads2EventFunction) gst_videomixer2_sink_event, mix);
1992   gst_collect_pads2_set_clip_function (mix->collect,
1993       (GstCollectPads2ClipFunction) gst_videomixer2_sink_clip, mix);
1994
1995   g_mutex_init (&mix->lock);
1996   /* initialize variables */
1997   gst_videomixer2_reset (mix);
1998 }
1999
2000 /* Element registration */
2001 static gboolean
2002 plugin_init (GstPlugin * plugin)
2003 {
2004   GST_DEBUG_CATEGORY_INIT (gst_videomixer2_debug, "videomixer", 0,
2005       "video mixer");
2006
2007   gst_video_mixer_init_blend ();
2008
2009   return gst_element_register (plugin, "videomixer", GST_RANK_PRIMARY,
2010       GST_TYPE_VIDEO_MIXER2);
2011 }
2012
2013 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2014     GST_VERSION_MINOR,
2015     videomixer,
2016     "Video mixer", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
2017     GST_PACKAGE_ORIGIN)