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