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