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>
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.
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.
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.
22 * SECTION:element-videomixer
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.
29 * Videomixer will do colorspace conversion.
31 * Individual parameters for each input stream can be configured on the
35 * <title>Sample pipelines</title>
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 \
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.
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 \
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).
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 \
64 * video/x-raw,format=I420, framerate=\(fraction\)5/1, width=320, height=240 ! mix.
65 * ]| A pipeline to test I420
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)
85 #include "videomixer2.h"
86 #include "videomixer2pad.h"
89 #define orc_memset memset
91 #include <orc/orcfunctions.h>
94 GST_DEBUG_CATEGORY_STATIC (gst_videomixer2_debug);
95 #define GST_CAT_DEFAULT gst_videomixer2_debug
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)))
110 #define FORMATS " { AYUV, BGRA, ARGB, RGBA, ABGR, Y444, Y42B, YUY2, UYVY, "\
111 " YVYU, I420, YV12, NV12, NV21, Y41B, RGB, BGR, xRGB, xBGR, "\
114 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
117 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
120 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
123 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
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,
130 static void gst_videomixer2_release_pad (GstElement * element, GstPad * pad);
131 static void gst_videomixer2_reset_qos (GstVideoMixer2 * mix);
133 struct _GstVideoMixer2Collect
135 GstCollectData collect; /* we extend the CollectData */
137 GstVideoMixer2Pad *mixpad;
139 GstBuffer *queued; /* buffer for which we don't know the end time yet */
140 GstVideoInfo queued_vinfo;
142 GstBuffer *buffer; /* buffer that should be blended now */
143 GstVideoInfo buffer_vinfo;
145 GstClockTime start_time;
146 GstClockTime end_time;
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
162 G_DEFINE_TYPE (GstVideoMixer2Pad, gst_videomixer2_pad, GST_TYPE_PAD);
165 gst_videomixer2_collect_free (GstCollectData * data)
167 GstVideoMixer2Collect *cdata = (GstVideoMixer2Collect *) data;
169 gst_buffer_replace (&cdata->buffer, NULL);
172 static gboolean gst_videomixer2_src_setcaps (GstPad * pad, GstVideoMixer2 * mix,
176 gst_videomixer2_update_src_caps (GstVideoMixer2 * mix)
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;
184 GST_VIDEO_MIXER2_SETCAPS_LOCK (mix);
185 GST_VIDEO_MIXER2_LOCK (mix);
187 for (l = mix->sinkpads; l; l = l->next) {
188 GstVideoMixer2Pad *mpad = l->data;
189 gint this_width, this_height;
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);
198 if (width == 0 || height == 0)
201 this_width = width + MAX (mpad->xpos, 0);
202 this_height = height + MAX (mpad->ypos, 0);
204 if (best_width < this_width)
205 best_width = this_width;
206 if (best_height < this_height)
207 best_height = this_height;
212 gst_util_fraction_to_double (fps_n, fps_d, &cur_fps);
214 if (best_fps < cur_fps) {
221 if (best_fps_n <= 0 || best_fps_d <= 0 || best_fps == 0.0) {
227 if (best_width > 0 && best_height > 0 && best_fps > 0) {
228 GstCaps *caps, *peercaps;
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;
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);
247 caps = gst_video_info_to_caps (&info);
249 peercaps = gst_pad_peer_query_caps (mix->srcpad, NULL);
250 if (peercaps && !gst_caps_can_intersect (peercaps, caps)) {
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);
258 tmp = gst_caps_intersect (caps, peercaps);
259 gst_caps_unref (caps);
260 gst_caps_unref (peercaps);
262 if (gst_caps_is_empty (caps)) {
263 GST_DEBUG_OBJECT (mix, "empty caps");
265 GST_VIDEO_MIXER2_UNLOCK (mix);
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,
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);
281 gst_caps_unref (caps);
282 caps = gst_video_info_to_caps (&info);
284 GST_VIDEO_MIXER2_UNLOCK (mix);
285 ret = gst_videomixer2_src_setcaps (mix->srcpad, mix, caps);
286 gst_caps_unref (caps);
288 GST_VIDEO_MIXER2_UNLOCK (mix);
292 GST_VIDEO_MIXER2_SETCAPS_UNLOCK (mix);
298 gst_videomixer2_update_converters (GstVideoMixer2 * mix)
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;
313 best_format = GST_VIDEO_FORMAT_UNKNOWN;
314 gst_video_info_init (&best_info);
316 downstream_caps = gst_pad_get_allowed_caps (mix->srcpad);
318 if (!downstream_caps || gst_caps_is_empty (downstream_caps)) {
320 gst_caps_unref (downstream_caps);
324 formats_table = g_hash_table_new (g_direct_hash, g_direct_equal);
326 /* first find new preferred format */
327 for (tmp = mix->sinkpads; tmp; tmp = tmp->next) {
333 if (!pad->info.finfo)
336 if (pad->info.finfo->flags & GST_VIDEO_FORMAT_FLAG_ALPHA)
337 at_least_one_alpha = TRUE;
339 /* If we want alpha, disregard all the other formats */
340 if (need_alpha && !(pad->info.finfo->flags & GST_VIDEO_FORMAT_FLAG_ALPHA))
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)
347 possible_caps = gst_video_info_to_caps (&pad->info);
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);
353 /* Can downstream accept this format ? */
354 if (!gst_caps_can_intersect (downstream_caps, possible_caps)) {
355 gst_caps_unref (possible_caps);
359 gst_caps_unref (possible_caps);
362 GPOINTER_TO_INT (g_hash_table_lookup (formats_table,
363 GINT_TO_POINTER (GST_VIDEO_INFO_FORMAT (&pad->info))));
366 g_hash_table_replace (formats_table,
367 GINT_TO_POINTER (GST_VIDEO_INFO_FORMAT (&pad->info)),
368 GINT_TO_POINTER (format_number));
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)) {
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;
383 g_hash_table_unref (formats_table);
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);
391 gst_caps_unref (downstream_caps);
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"));
401 best_colorimetry = gst_video_colorimetry_to_string (&(best_info.colorimetry));
402 best_chroma = gst_video_chroma_to_string (best_info.chroma_site);
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;
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));
417 mix->info = best_info;
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);
423 /* Then browse the sinks once more, setting or unsetting conversion if needed */
424 for (tmp = mix->sinkpads; tmp; tmp = tmp->next) {
430 if (!pad->info.finfo)
433 if (GST_VIDEO_INFO_FORMAT (&pad->info) == GST_VIDEO_FORMAT_UNKNOWN)
437 gst_video_converter_free (pad->convert);
441 colorimetry = gst_video_colorimetry_to_string (&(pad->info.colorimetry));
442 chroma = gst_video_chroma_to_string (pad->info.chroma_site);
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;
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;
458 g_free (colorimetry);
459 g_free (best_colorimetry);
460 GST_WARNING ("No path found for conversion");
464 GST_DEBUG_OBJECT (pad, "This pad will not need conversion");
466 g_free (colorimetry);
469 g_free (best_colorimetry);
474 gst_videomixer2_pad_sink_setcaps (GstPad * pad, GstObject * parent,
478 GstVideoMixer2Pad *mixpad;
480 gboolean ret = FALSE;
482 GST_INFO_OBJECT (pad, "Setting caps %" GST_PTR_FORMAT, caps);
484 mix = GST_VIDEO_MIXER2 (parent);
485 mixpad = GST_VIDEO_MIXER2_PAD (pad);
487 if (!gst_video_info_from_caps (&info, caps)) {
488 GST_ERROR_OBJECT (pad, "Failed to parse caps");
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);
508 GST_COLLECT_PADS_STREAM_LOCK (mix->collect);
510 ret = gst_videomixer2_update_converters (mix);
512 GST_VIDEO_MIXER2_UNLOCK (mix);
514 ret = gst_videomixer2_update_src_caps (mix);
515 GST_COLLECT_PADS_STREAM_UNLOCK (mix->collect);
522 gst_videomixer2_pad_sink_getcaps (GstPad * pad, GstVideoMixer2 * mix,
526 GstCaps *template_caps;
527 GstCaps *filtered_caps;
528 GstCaps *returned_caps;
530 gboolean had_current_caps = TRUE;
533 template_caps = gst_pad_get_pad_template_caps (GST_PAD (mix->srcpad));
535 srccaps = gst_pad_get_current_caps (GST_PAD (mix->srcpad));
536 if (srccaps == NULL) {
537 had_current_caps = FALSE;
538 srccaps = template_caps;
541 srccaps = gst_caps_make_writable (srccaps);
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,
553 gst_structure_remove_fields (s, "colorimetry", "chroma-site", "format",
557 filtered_caps = srccaps;
559 filtered_caps = gst_caps_intersect (srccaps, filter);
560 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
562 gst_caps_unref (srccaps);
564 gst_caps_unref (filtered_caps);
565 if (had_current_caps)
566 gst_caps_unref (template_caps);
568 return returned_caps;
572 gst_videomixer2_pad_sink_acceptcaps (GstPad * pad, GstVideoMixer2 * mix,
576 GstCaps *modified_caps;
577 GstCaps *accepted_caps;
578 GstCaps *template_caps;
579 gboolean had_current_caps = TRUE;
583 GST_DEBUG_OBJECT (pad, "%" GST_PTR_FORMAT, caps);
585 accepted_caps = gst_pad_get_current_caps (GST_PAD (mix->srcpad));
587 template_caps = gst_pad_get_pad_template_caps (GST_PAD (mix->srcpad));
589 if (accepted_caps == NULL) {
590 accepted_caps = template_caps;
591 had_current_caps = FALSE;
594 accepted_caps = gst_caps_make_writable (accepted_caps);
596 GST_LOG_OBJECT (pad, "src caps %" GST_PTR_FORMAT, accepted_caps);
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,
608 gst_structure_remove_fields (s, "colorimetry", "chroma-site", "format",
612 modified_caps = gst_caps_intersect (accepted_caps, template_caps);
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);
626 gst_videomixer2_sink_query (GstCollectPads * pads, GstCollectData * cdata,
627 GstQuery * query, GstVideoMixer2 * mix)
629 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (cdata->pad);
630 gboolean ret = FALSE;
632 switch (GST_QUERY_TYPE (query)) {
635 GstCaps *filter, *caps;
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);
644 case GST_QUERY_ACCEPT_CAPS:
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);
655 ret = gst_collect_pads_query_default (pads, cdata, query, FALSE);
662 gst_videomixer2_pad_get_property (GObject * object, guint prop_id,
663 GValue * value, GParamSpec * pspec)
665 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
668 case PROP_PAD_ZORDER:
669 g_value_set_uint (value, pad->zorder);
672 g_value_set_int (value, pad->xpos);
675 g_value_set_int (value, pad->ypos);
678 g_value_set_double (value, pad->alpha);
681 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
687 pad_zorder_compare (const GstVideoMixer2Pad * pad1,
688 const GstVideoMixer2Pad * pad2)
690 return pad1->zorder - pad2->zorder;
694 gst_videomixer2_pad_set_property (GObject * object, guint prop_id,
695 const GValue * value, GParamSpec * pspec)
697 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
698 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (GST_PAD (pad)));
701 case PROP_PAD_ZORDER:
702 GST_VIDEO_MIXER2_LOCK (mix);
703 pad->zorder = g_value_get_uint (value);
705 mix->sinkpads = g_slist_sort (mix->sinkpads,
706 (GCompareFunc) pad_zorder_compare);
707 GST_VIDEO_MIXER2_UNLOCK (mix);
710 pad->xpos = g_value_get_int (value);
713 pad->ypos = g_value_get_int (value);
716 pad->alpha = g_value_get_double (value);
719 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
723 gst_object_unref (mix);
727 gst_videomixer2_pad_class_init (GstVideoMixer2PadClass * klass)
729 GObjectClass *gobject_class = (GObjectClass *) klass;
731 gobject_class->set_property = gst_videomixer2_pad_set_property;
732 gobject_class->get_property = gst_videomixer2_pad_get_property;
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,
749 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
753 gst_videomixer2_pad_init (GstVideoMixer2Pad * mixerpad)
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;
764 #define DEFAULT_BACKGROUND VIDEO_MIXER2_BACKGROUND_CHECKER
771 #define GST_TYPE_VIDEO_MIXER2_BACKGROUND (gst_videomixer2_background_get_type())
773 gst_videomixer2_background_get_type (void)
775 static GType video_mixer_background_type = 0;
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"},
786 if (!video_mixer_background_type) {
787 video_mixer_background_type =
788 g_enum_register_static ("GstVideoMixer2Background",
789 video_mixer_background);
791 return video_mixer_background_type;
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));
800 gst_videomixer2_update_qos (GstVideoMixer2 * mix, gdouble proportion,
801 GstClockTimeDiff diff, GstClockTime timestamp)
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));
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))
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));
816 mix->earliest_time = timestamp + diff;
818 mix->earliest_time = GST_CLOCK_TIME_NONE;
820 GST_OBJECT_UNLOCK (mix);
824 gst_videomixer2_reset_qos (GstVideoMixer2 * mix)
826 gst_videomixer2_update_qos (mix, 0.5, 0, GST_CLOCK_TIME_NONE);
827 mix->qos_processed = mix->qos_dropped = 0;
831 gst_videomixer2_read_qos (GstVideoMixer2 * mix, gdouble * proportion,
834 GST_OBJECT_LOCK (mix);
835 *proportion = mix->proportion;
836 *time = mix->earliest_time;
837 GST_OBJECT_UNLOCK (mix);
841 gst_videomixer2_reset (GstVideoMixer2 * mix)
845 gst_video_info_init (&mix->info);
849 gst_segment_init (&mix->segment, GST_FORMAT_TIME);
850 mix->segment.position = -1;
852 gst_videomixer2_reset_qos (mix);
854 for (l = mix->sinkpads; l; l = l->next) {
855 GstVideoMixer2Pad *p = l->data;
856 GstVideoMixer2Collect *mixcol = p->mixcol;
858 gst_buffer_replace (&mixcol->buffer, NULL);
859 mixcol->start_time = -1;
860 mixcol->end_time = -1;
862 gst_video_info_init (&p->info);
865 mix->newseg_pending = TRUE;
869 * 0 == need more data
874 gst_videomixer2_fill_queues (GstVideoMixer2 * mix,
875 GstClockTime output_start_time, GstClockTime output_end_time)
879 gboolean need_more_data = FALSE;
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;
888 buf = gst_collect_pads_peek (mix->collect, &mixcol->collect);
890 GstClockTime start_time, end_time;
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!");
901 /* FIXME: Make all this work with negative rates */
903 if ((mixcol->buffer && start_time < GST_BUFFER_TIMESTAMP (mixcol->buffer))
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;
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;
921 end_time = GST_BUFFER_DURATION (buf);
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;
933 g_assert (start_time != -1 && end_time != -1);
934 end_time += start_time; /* convert from duration to position */
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");
940 if (buf == mixcol->queued) {
941 gst_buffer_unref (buf);
942 gst_buffer_replace (&mixcol->queued, NULL);
944 gst_buffer_unref (buf);
945 buf = gst_collect_pads_pop (mix->collect, &mixcol->collect);
946 gst_buffer_unref (buf);
949 need_more_data = TRUE;
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);
958 gst_segment_to_running_time (segment, GST_FORMAT_TIME, start_time);
960 gst_segment_to_running_time (segment, GST_FORMAT_TIME, end_time);
961 g_assert (start_time != -1 && end_time != -1);
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);
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);
975 gst_buffer_unref (buf);
976 buf = gst_collect_pads_pop (mix->collect, &mixcol->collect);
977 gst_buffer_unref (buf);
980 need_more_data = TRUE;
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;
993 if (buf == mixcol->queued) {
994 gst_buffer_unref (buf);
995 gst_buffer_replace (&mixcol->queued, NULL);
997 gst_buffer_unref (buf);
998 buf = gst_collect_pads_pop (mix->collect, &mixcol->collect);
999 gst_buffer_unref (buf);
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);
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);
1013 gst_buffer_unref (buf);
1014 buf = gst_collect_pads_pop (mix->collect, &mixcol->collect);
1015 gst_buffer_unref (buf);
1018 need_more_data = TRUE;
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)) {
1045 static GstFlowReturn
1046 gst_videomixer2_blend_buffers (GstVideoMixer2 * mix,
1047 GstClockTime output_start_time, GstClockTime output_end_time,
1048 GstBuffer ** outbuf)
1052 BlendFunction composite;
1053 GstVideoFrame outframe;
1054 static GstAllocationParams params = { 0, 15, 0, 0, };
1056 outsize = GST_VIDEO_INFO_SIZE (&mix->info);
1058 *outbuf = gst_buffer_new_allocate (NULL, outsize, ¶ms);
1059 GST_BUFFER_TIMESTAMP (*outbuf) = output_start_time;
1060 GST_BUFFER_DURATION (*outbuf) = output_end_time - output_start_time;
1062 gst_video_frame_map (&outframe, &mix->info, *outbuf, GST_MAP_READWRITE);
1064 /* default to blending */
1065 composite = mix->blend;
1066 switch (mix->background) {
1067 case VIDEO_MIXER2_BACKGROUND_CHECKER:
1068 mix->fill_checker (&outframe);
1070 case VIDEO_MIXER2_BACKGROUND_BLACK:
1071 mix->fill_color (&outframe, 16, 128, 128);
1073 case VIDEO_MIXER2_BACKGROUND_WHITE:
1074 mix->fill_color (&outframe, 240, 128, 128);
1076 case VIDEO_MIXER2_BACKGROUND_TRANSPARENT:
1078 guint i, plane, num_planes, height;
1080 num_planes = GST_VIDEO_FRAME_N_PLANES (&outframe);
1081 for (plane = 0; plane < num_planes; ++plane) {
1083 gsize rowsize, plane_stride;
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;
1096 /* use overlay to keep background transparent */
1097 composite = mix->overlay;
1102 for (l = mix->sinkpads; l; l = l->next) {
1103 GstVideoMixer2Pad *pad = l->data;
1104 GstVideoMixer2Collect *mixcol = pad->mixcol;
1106 if (mixcol->buffer != NULL) {
1107 GstClockTime timestamp;
1110 GstVideoFrame converted_frame;
1111 GstBuffer *converted_buf = NULL;
1112 GstVideoFrame frame;
1114 seg = &mixcol->collect.segment;
1116 timestamp = GST_BUFFER_TIMESTAMP (mixcol->buffer);
1119 gst_segment_to_stream_time (seg, GST_FORMAT_TIME, timestamp);
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);
1125 gst_video_frame_map (&frame, &mixcol->buffer_vinfo, mixcol->buffer,
1129 gint converted_size;
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,
1137 pad->need_conversion_update = FALSE;
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, ¶ms);
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);
1149 converted_frame = frame;
1152 composite (&converted_frame, pad->xpos, pad->ypos, pad->alpha, &outframe);
1155 gst_buffer_unref (converted_buf);
1157 gst_video_frame_unmap (&converted_frame);
1160 gst_video_frame_unmap (&outframe);
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 */
1168 gst_videomixer2_do_qos (GstVideoMixer2 * mix, GstClockTime timestamp)
1170 GstClockTime qostime, earliest_time;
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");
1180 /* get latest QoS observation values */
1181 gst_videomixer2_read_qos (mix, &proportion, &earliest_time);
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");
1189 /* qos is done on running time */
1191 gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME, timestamp);
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));
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");
1203 GST_LOG_OBJECT (mix, "process frame");
1207 static GstFlowReturn
1208 gst_videomixer2_collected (GstCollectPads * pads, GstVideoMixer2 * mix)
1211 GstClockTime output_start_time, output_end_time;
1212 GstBuffer *outbuf = NULL;
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;
1220 if (mix->send_stream_start) {
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");
1228 mix->send_stream_start = FALSE;
1231 if (gst_pad_check_reconfigure (mix->srcpad))
1232 gst_videomixer2_update_src_caps (mix);
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");
1239 mix->send_caps = FALSE;
1242 GST_VIDEO_MIXER2_LOCK (mix);
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;
1252 GST_VIDEO_MIXER2_LOCK (mix);
1253 mix->newseg_pending = FALSE;
1256 if (mix->segment.position == -1)
1257 output_start_time = mix->segment.start;
1259 output_start_time = mix->segment.position;
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;
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 ());
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;
1282 if (mix->segment.stop != -1)
1283 output_end_time = MIN (output_end_time, mix->segment.stop);
1285 res = gst_videomixer2_fill_queues (mix, output_start_time, output_end_time);
1288 GST_DEBUG_OBJECT (mix, "Need more data for decisions");
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 ());
1297 } else if (res == -2) {
1298 GST_ERROR_OBJECT (mix, "Error collecting buffers");
1299 ret = GST_FLOW_ERROR;
1303 jitter = gst_videomixer2_do_qos (mix, output_start_time);
1306 gst_videomixer2_blend_buffers (mix, output_start_time,
1307 output_end_time, &outbuf);
1308 mix->qos_processed++;
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,
1324 gst_element_post_message (GST_ELEMENT_CAST (mix), msg);
1329 mix->segment.position = output_end_time;
1332 GST_VIDEO_MIXER2_UNLOCK (mix);
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);
1343 GST_VIDEO_MIXER2_UNLOCK (mix);
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.
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.
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.
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.
1366 gst_videomixer2_query_duration (GstVideoMixer2 * mix, GstQuery * query)
1368 GValue item = { 0 };
1376 gst_query_parse_duration (query, &format, NULL);
1382 /* Take maximum of all durations */
1383 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1385 switch (gst_iterator_next (it, &item)) {
1386 case GST_ITERATOR_DONE:
1389 case GST_ITERATOR_OK:
1394 pad = g_value_get_object (&item);
1396 /* ask sink peer for duration */
1397 res &= gst_pad_peer_query_duration (pad, format, &duration);
1398 /* take max from all valid return values */
1400 /* valid unknown length, stop searching */
1401 if (duration == -1) {
1405 /* else see if bigger than current max */
1406 else if (duration > max)
1409 g_value_reset (&item);
1412 case GST_ITERATOR_RESYNC:
1415 gst_iterator_resync (it);
1423 g_value_unset (&item);
1424 gst_iterator_free (it);
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);
1437 gst_videomixer2_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1439 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
1440 gboolean res = FALSE;
1442 switch (GST_QUERY_TYPE (query)) {
1443 case GST_QUERY_POSITION:
1447 gst_query_parse_position (query, &format, NULL);
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));
1461 case GST_QUERY_DURATION:
1462 res = gst_videomixer2_query_duration (mix, query);
1464 case GST_QUERY_CAPS:
1465 res = gst_pad_query_default (pad, parent, query);
1468 /* FIXME, needs a custom query handler because we have multiple
1477 gst_videomixer2_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1479 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
1482 switch (GST_EVENT_TYPE (event)) {
1486 GstClockTimeDiff diff;
1487 GstClockTime timestamp;
1490 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1492 gst_videomixer2_update_qos (mix, proportion, diff, timestamp);
1494 result = gst_videomixer2_push_sink_event (mix, event);
1497 case GST_EVENT_SEEK:
1502 GstSeekType start_type, stop_type;
1507 /* parse the seek parameters */
1508 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1509 &start, &stop_type, &stop);
1512 GST_ERROR_OBJECT (mix, "Negative rates not supported yet");
1514 gst_event_unref (event);
1518 GST_DEBUG_OBJECT (mix, "Handling SEEK event");
1520 abs_rate = ABS (rate);
1522 GST_VIDEO_MIXER2_LOCK (mix);
1523 for (l = mix->sinkpads; l; l = l->next) {
1524 GstVideoMixer2Pad *p = l->data;
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;
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);
1538 if (abs_rate != 1.0 && p->mixcol->buffer) {
1539 p->mixcol->start_time *= abs_rate;
1540 p->mixcol->end_time *= abs_rate;
1544 GST_VIDEO_MIXER2_UNLOCK (mix);
1546 gst_segment_do_seek (&mix->segment, rate, fmt, flags, start_type, start,
1547 stop_type, stop, NULL);
1548 mix->segment.position = -1;
1551 mix->newseg_pending = TRUE;
1553 gst_videomixer2_reset_qos (mix);
1555 result = gst_collect_pads_src_event_default (mix->collect, pad, event);
1558 case GST_EVENT_NAVIGATION:
1559 /* navigation is rather pointless. */
1561 gst_event_unref (event);
1564 /* just forward the rest for now */
1565 result = gst_videomixer2_push_sink_event (mix, event);
1573 gst_videomixer2_src_setcaps (GstPad * pad, GstVideoMixer2 * mix, GstCaps * caps)
1575 gboolean ret = FALSE;
1578 GST_INFO_OBJECT (pad, "set src caps: %" GST_PTR_FORMAT, caps);
1580 if (!gst_video_info_from_caps (&info, caps))
1583 GST_VIDEO_MIXER2_LOCK (mix);
1586 mix->overlay = NULL;
1587 mix->fill_checker = NULL;
1588 mix->fill_color = NULL;
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;
1596 gst_videomixer2_reset_qos (mix);
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
1752 GST_VIDEO_MIXER2_UNLOCK (mix);
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;
1764 static GstFlowReturn
1765 gst_videomixer2_sink_clip (GstCollectPads * pads,
1766 GstCollectData * data, GstBuffer * buf, GstBuffer ** outbuf,
1767 GstVideoMixer2 * mix)
1769 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (data->pad);
1770 GstVideoMixer2Collect *mixcol = pad->mixcol;
1771 GstClockTime start_time, end_time;
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;
1780 end_time = GST_BUFFER_DURATION (buf);
1781 if (end_time == -1 && GST_VIDEO_INFO_FPS_N (&pad->info) != 0)
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) {
1790 start_time = MAX (start_time, mixcol->collect.segment.start);
1792 gst_segment_to_running_time (&mixcol->collect.segment,
1793 GST_FORMAT_TIME, start_time);
1795 end_time += GST_BUFFER_TIMESTAMP (buf);
1796 if (mixcol->collect.segment.stop != -1)
1797 end_time = MIN (end_time, mixcol->collect.segment.stop);
1799 gst_segment_to_running_time (&mixcol->collect.segment,
1800 GST_FORMAT_TIME, end_time);
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);
1808 if (mixcol->buffer != NULL && end_time < mixcol->end_time) {
1809 gst_buffer_unref (buf);
1819 gst_videomixer2_flush (GstCollectPads * pads, GstVideoMixer2 * mix)
1821 if (mix->pending_tags) {
1822 gst_tag_list_unref (mix->pending_tags);
1823 mix->pending_tags = NULL;
1828 gst_videomixer2_sink_event (GstCollectPads * pads, GstCollectData * cdata,
1829 GstEvent * event, GstVideoMixer2 * mix)
1831 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (cdata->pad);
1832 gboolean ret = TRUE, discard = FALSE;
1834 GST_DEBUG_OBJECT (pad, "Got %s event: %" GST_PTR_FORMAT,
1835 GST_EVENT_TYPE_NAME (event), event);
1837 switch (GST_EVENT_TYPE (event)) {
1838 case GST_EVENT_CAPS:
1842 gst_event_parse_caps (event, &caps);
1844 gst_videomixer2_pad_sink_setcaps (GST_PAD (pad), GST_OBJECT (mix),
1846 gst_event_unref (event);
1850 case GST_EVENT_SEGMENT:{
1852 gst_event_copy_segment (event, &seg);
1854 g_assert (seg.format == GST_FORMAT_TIME);
1855 gst_videomixer2_reset_qos (mix);
1858 case GST_EVENT_FLUSH_STOP:
1859 mix->newseg_pending = TRUE;
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;
1866 mix->segment.position = -1;
1872 /* collect tags here so we can push them out when we collect data */
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;
1888 return gst_collect_pads_event_default (pads, cdata, event, discard);
1894 forward_event_func (GValue * item, GValue * ret, GstEvent * event)
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));
1904 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
1905 event, GST_EVENT_TYPE_NAME (event));
1911 gst_videomixer2_push_sink_event (GstVideoMixer2 * mix, GstEvent * event)
1914 GValue vret = { 0 };
1916 GST_LOG_OBJECT (mix, "Forwarding event %p (%s)", event,
1917 GST_EVENT_TYPE_NAME (event));
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,
1924 gst_iterator_free (it);
1925 gst_event_unref (event);
1927 return g_value_get_boolean (&vret);
1930 /* GstElement vmethods */
1931 static GstStateChangeReturn
1932 gst_videomixer2_change_state (GstElement * element, GstStateChange transition)
1934 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (element);
1935 GstStateChangeReturn ret;
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);
1946 case GST_STATE_CHANGE_PAUSED_TO_READY:
1947 GST_LOG_OBJECT (mix, "stopping collectpads");
1948 gst_collect_pads_stop (mix->collect);
1954 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1956 switch (transition) {
1957 case GST_STATE_CHANGE_PAUSED_TO_READY:
1958 gst_videomixer2_reset (mix);
1968 gst_videomixer2_request_new_pad (GstElement * element,
1969 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1971 GstVideoMixer2 *mix;
1972 GstVideoMixer2Pad *mixpad;
1973 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1975 mix = GST_VIDEO_MIXER2 (element);
1977 if (templ == gst_element_class_get_pad_template (klass, "sink_%u")) {
1980 GstVideoMixer2Collect *mixcol = NULL;
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++;
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;
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);
1999 mixpad->zorder = mix->numpads;
2000 mixpad->xpos = DEFAULT_PAD_XPOS;
2001 mixpad->ypos = DEFAULT_PAD_YPOS;
2002 mixpad->alpha = DEFAULT_PAD_ALPHA;
2004 mixcol = (GstVideoMixer2Collect *)
2005 gst_collect_pads_add_pad (mix->collect, GST_PAD (mixpad),
2006 sizeof (GstVideoMixer2Collect),
2007 (GstCollectDataDestroyNotify) gst_videomixer2_collect_free, TRUE);
2009 /* Keep track of each other */
2010 mixcol->mixpad = mixpad;
2011 mixpad->mixcol = mixcol;
2013 mixcol->start_time = -1;
2014 mixcol->end_time = -1;
2016 /* Keep an internal list of mixpads for zordering */
2017 mix->sinkpads = g_slist_insert_sorted (mix->sinkpads, mixpad,
2018 (GCompareFunc) pad_zorder_compare);
2020 GST_VIDEO_MIXER2_UNLOCK (mix);
2025 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (mixpad));
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));
2032 return GST_PAD (mixpad);
2036 gst_videomixer2_release_pad (GstElement * element, GstPad * pad)
2038 GstVideoMixer2 *mix = NULL;
2039 GstVideoMixer2Pad *mixpad;
2040 gboolean update_caps;
2042 mix = GST_VIDEO_MIXER2 (element);
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));
2050 mixpad = GST_VIDEO_MIXER2_PAD (pad);
2052 if (mixpad->convert)
2053 gst_video_converter_free (mixpad->convert);
2054 mixpad->convert = NULL;
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));
2061 GST_COLLECT_PADS_STREAM_LOCK (mix->collect);
2062 gst_videomixer2_update_converters (mix);
2063 GST_COLLECT_PADS_STREAM_UNLOCK (mix->collect);
2065 update_caps = GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_FORMAT_UNKNOWN;
2066 GST_VIDEO_MIXER2_UNLOCK (mix);
2068 gst_collect_pads_remove_pad (mix->collect, pad);
2071 gst_videomixer2_update_src_caps (mix);
2073 gst_element_remove_pad (element, pad);
2076 GST_VIDEO_MIXER2_UNLOCK (mix);
2079 /* GObject vmethods */
2081 gst_videomixer2_finalize (GObject * o)
2083 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (o);
2085 gst_object_unref (mix->collect);
2086 g_mutex_clear (&mix->lock);
2087 g_mutex_clear (&mix->setcaps_lock);
2089 G_OBJECT_CLASS (parent_class)->finalize (o);
2093 gst_videomixer2_dispose (GObject * o)
2095 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (o);
2098 for (tmp = mix->sinkpads; tmp; tmp = tmp->next) {
2099 GstVideoMixer2Pad *mixpad = tmp->data;
2101 if (mixpad->convert)
2102 gst_video_converter_free (mixpad->convert);
2103 mixpad->convert = NULL;
2106 if (mix->pending_tags) {
2107 gst_tag_list_unref (mix->pending_tags);
2108 mix->pending_tags = NULL;
2111 gst_caps_replace (&mix->current_caps, NULL);
2113 G_OBJECT_CLASS (parent_class)->dispose (o);
2117 gst_videomixer2_get_property (GObject * object,
2118 guint prop_id, GValue * value, GParamSpec * pspec)
2120 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
2123 case PROP_BACKGROUND:
2124 g_value_set_enum (value, mix->background);
2127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2133 gst_videomixer2_set_property (GObject * object,
2134 guint prop_id, const GValue * value, GParamSpec * pspec)
2136 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
2139 case PROP_BACKGROUND:
2140 mix->background = g_value_get_enum (value);
2143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2148 /* GstChildProxy implementation */
2150 gst_videomixer2_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
2153 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
2156 GST_VIDEO_MIXER2_LOCK (mix);
2157 if ((obj = g_slist_nth_data (mix->sinkpads, index)))
2159 GST_VIDEO_MIXER2_UNLOCK (mix);
2164 gst_videomixer2_child_proxy_get_children_count (GstChildProxy * child_proxy)
2167 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
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);
2177 gst_videomixer2_child_proxy_init (gpointer g_iface, gpointer iface_data)
2179 GstChildProxyInterface *iface = g_iface;
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;
2187 gst_videomixer2_constructed (GObject * obj)
2189 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (obj);
2192 cp_name = g_strconcat (GST_OBJECT_NAME (obj), "-collectpads", NULL);
2193 gst_object_set_name (GST_OBJECT (mix->collect), cp_name);
2196 G_OBJECT_CLASS (gst_videomixer2_parent_class)->constructed (obj);
2199 /* GObject boilerplate */
2201 gst_videomixer2_class_init (GstVideoMixer2Class * klass)
2203 GObjectClass *gobject_class = (GObjectClass *) klass;
2204 GstElementClass *gstelement_class = (GstElementClass *) klass;
2206 gobject_class->constructed = gst_videomixer2_constructed;
2207 gobject_class->finalize = gst_videomixer2_finalize;
2208 gobject_class->dispose = gst_videomixer2_dispose;
2210 gobject_class->get_property = gst_videomixer2_get_property;
2211 gobject_class->set_property = gst_videomixer2_set_property;
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));
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);
2225 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
2226 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
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>");
2233 /* Register the pad class */
2234 g_type_class_ref (GST_TYPE_VIDEO_MIXER2_PAD);
2238 gst_videomixer2_init (GstVideoMixer2 * mix)
2240 GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix);
2243 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
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);
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;
2258 gst_collect_pads_set_function (mix->collect,
2259 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_videomixer2_collected),
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);
2268 g_mutex_init (&mix->lock);
2269 g_mutex_init (&mix->setcaps_lock);
2270 /* initialize variables */
2271 gst_videomixer2_reset (mix);
2274 /* Element registration */
2276 plugin_init (GstPlugin * plugin)
2278 GST_DEBUG_CATEGORY_INIT (gst_videomixer2_debug, "videomixer", 0,
2281 gst_video_mixer_init_blend ();
2283 return gst_element_register (plugin, "videomixer", GST_RANK_PRIMARY,
2284 GST_TYPE_VIDEO_MIXER2);
2287 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2290 "Video mixer", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,