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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-videomixer2
24 * Videomixer2 can accept AYUV, ARGB and BGRA video streams. For each of the requested
25 * sink pads it will compare the incoming geometry and framerate to define the
26 * output parameters. Indeed output video frames will have the geometry of the
27 * biggest incoming video stream and the framerate of the fastest incoming one.
29 * All sink pads must be either AYUV, ARGB or BGRA, but a mixture of them is not
30 * supported. The src pad will have the same colorspace as the sinks.
31 * No colorspace conversion is done.
33 * Individual parameters for each input stream can be configured on the
36 * At this stage, videomixer2 is considered UNSTABLE. The API provided in the
37 * properties may yet change in the near future. When videomixer2 is stable,
38 * it will replace #videomixer
41 * <title>Sample pipelines</title>
44 * videotestsrc pattern=1 ! \
45 * video/x-raw-yuv,format=\(fourcc\)AYUV,framerate=\(fraction\)10/1,width=100,height=100 ! \
46 * videobox border-alpha=0 top=-70 bottom=-70 right=-220 ! \
47 * videomixer2 name=mix sink_0::alpha=0.7 sink_1::alpha=0.5 ! \
48 * ffmpegcolorspace ! xvimagesink \
50 * video/x-raw-yuv,format=\(fourcc\)AYUV,framerate=\(fraction\)5/1,width=320,height=240 ! mix.
51 * ]| A pipeline to demonstrate videomixer used together with videobox.
52 * This should show a 320x240 pixels video test source with some transparency
53 * showing the background checker pattern. Another video test source with just
54 * the snow pattern of 100x100 pixels is overlayed on top of the first one on
55 * the left vertically centered with a small transparency showing the first
56 * video test source behind and the checker pattern under it. Note that the
57 * framerate of the output video is 10 frames per second.
59 * gst-launch videotestsrc pattern=1 ! \
60 * video/x-raw-rgb, framerate=\(fraction\)10/1, width=100, height=100 ! \
61 * videomixer2 name=mix ! ffmpegcolorspace ! ximagesink \
63 * video/x-raw-rgb, framerate=\(fraction\)5/1, width=320, height=240 ! mix.
64 * ]| A pipeline to demostrate bgra mixing. (This does not demonstrate alpha blending).
66 * gst-launch videotestsrc pattern=1 ! \
67 * video/x-raw-yuv,format =\(fourcc\)I420, framerate=\(fraction\)10/1, width=100, height=100 ! \
68 * videomixer2 name=mix ! ffmpegcolorspace ! ximagesink \
70 * video/x-raw-yuv,format=\(fourcc\)I420, framerate=\(fraction\)5/1, width=320, height=240 ! mix.
71 * ]| A pipeline to test I420
73 * gst-launch videomixer2 name=mixer sink_1::alpha=0.5 sink_1::xpos=50 sink_1::ypos=50 ! \
74 * ffmpegcolorspace ! ximagesink \
75 * videotestsrc pattern=snow timestamp-offset=3000000000 ! \
76 * "video/x-raw-yuv,format=(fourcc)AYUV,width=640,height=480,framerate=(fraction)30/1" ! \
77 * timeoverlay ! queue2 ! mixer. \
78 * videotestsrc pattern=smpte ! \
79 * "video/x-raw-yuv,format=(fourcc)AYUV,width=800,height=600,framerate=(fraction)10/1" ! \
80 * timeoverlay ! queue2 ! mixer.
81 * ]| A pipeline to demonstrate synchronized mixing (the second stream starts after 3 seconds)
91 #include "videomixer2.h"
92 #include "videomixer2pad.h"
94 #include <gst/controller/gstcontroller.h>
97 #define orc_memset memset
99 #include <orc/orcfunctions.h>
102 GST_DEBUG_CATEGORY_STATIC (gst_videomixer2_debug);
103 #define GST_CAT_DEFAULT gst_videomixer2_debug
105 #define GST_VIDEO_MIXER2_GET_LOCK(mix) \
106 (GST_VIDEO_MIXER2(mix)->lock)
107 #define GST_VIDEO_MIXER2_LOCK(mix) \
108 (g_mutex_lock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
109 #define GST_VIDEO_MIXER2_UNLOCK(mix) \
110 (g_mutex_unlock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
112 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
115 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";"
116 GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_ABGR ";"
117 GST_VIDEO_CAPS_YUV ("Y444") ";" GST_VIDEO_CAPS_YUV ("Y42B") ";"
118 GST_VIDEO_CAPS_YUV ("YUY2") ";" GST_VIDEO_CAPS_YUV ("UYVY") ";"
119 GST_VIDEO_CAPS_YUV ("YVYU") ";"
120 GST_VIDEO_CAPS_YUV ("I420") ";" GST_VIDEO_CAPS_YUV ("YV12") ";"
121 GST_VIDEO_CAPS_YUV ("Y41B") ";" GST_VIDEO_CAPS_RGB ";"
122 GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
123 GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx)
126 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
129 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";"
130 GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_ABGR ";"
131 GST_VIDEO_CAPS_YUV ("Y444") ";" GST_VIDEO_CAPS_YUV ("Y42B") ";"
132 GST_VIDEO_CAPS_YUV ("YUY2") ";" GST_VIDEO_CAPS_YUV ("UYVY") ";"
133 GST_VIDEO_CAPS_YUV ("YVYU") ";"
134 GST_VIDEO_CAPS_YUV ("I420") ";" GST_VIDEO_CAPS_YUV ("YV12") ";"
135 GST_VIDEO_CAPS_YUV ("Y41B") ";" GST_VIDEO_CAPS_RGB ";"
136 GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
137 GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx)
140 static void gst_videomixer2_child_proxy_init (gpointer g_iface,
141 gpointer iface_data);
142 static gboolean gst_videomixer2_push_sink_event (GstVideoMixer2 * mix,
144 static void gst_videomixer2_release_pad (GstElement * element, GstPad * pad);
145 static void gst_videomixer2_reset_qos (GstVideoMixer2 * mix);
148 _do_init (GType object_type)
150 static const GInterfaceInfo child_proxy_info = {
151 (GInterfaceInitFunc) gst_videomixer2_child_proxy_init,
156 g_type_add_interface_static (object_type, GST_TYPE_CHILD_PROXY,
160 struct _GstVideoMixer2Collect
162 GstCollectData2 collect; /* we extend the CollectData */
164 GstVideoMixer2Pad *mixpad;
166 GstBuffer *queued; /* buffer for which we don't know the end time yet */
168 GstBuffer *buffer; /* buffer that should be blended now */
169 GstClockTime start_time;
170 GstClockTime end_time;
173 #define DEFAULT_PAD_ZORDER 0
174 #define DEFAULT_PAD_XPOS 0
175 #define DEFAULT_PAD_YPOS 0
176 #define DEFAULT_PAD_ALPHA 1.0
186 G_DEFINE_TYPE (GstVideoMixer2Pad, gst_videomixer2_pad, GST_TYPE_PAD);
189 gst_videomixer2_collect_free (GstCollectData2 * data)
191 GstVideoMixer2Collect *cdata = (GstVideoMixer2Collect *) data;
193 gst_buffer_replace (&cdata->buffer, NULL);
197 gst_videomixer2_update_src_caps (GstVideoMixer2 * mix)
200 gint best_width = -1, best_height = -1;
201 gdouble best_fps = -1, cur_fps;
202 gint best_fps_n = -1, best_fps_d = -1;
205 GST_VIDEO_MIXER2_LOCK (mix);
207 for (l = mix->sinkpads; l; l = l->next) {
208 GstVideoMixer2Pad *mpad = l->data;
209 gint this_width, this_height;
211 if (mpad->fps_n == 0 || mpad->fps_d == 0 ||
212 mpad->width == 0 || mpad->height == 0)
215 this_width = mpad->width + MAX (mpad->xpos, 0);
216 this_height = mpad->height + MAX (mpad->ypos, 0);
218 if (best_width < this_width)
219 best_width = this_width;
220 if (best_height < this_height)
221 best_height = this_height;
223 if (mpad->fps_d == 0)
226 gst_util_fraction_to_double (mpad->fps_n, mpad->fps_d, &cur_fps);
227 if (best_fps < cur_fps) {
229 best_fps_n = mpad->fps_n;
230 best_fps_d = mpad->fps_d;
234 if (best_fps_n <= 0 && best_fps_d <= 0) {
240 if (best_width > 0 && best_height > 0 && best_fps > 0) {
241 GstCaps *caps, *peercaps;
244 if (mix->fps_n != best_fps_n || mix->fps_d != best_fps_d) {
245 if (mix->segment.last_stop != -1) {
246 mix->ts_offset = mix->segment.last_stop - mix->segment.start;
251 caps = gst_video_format_new_caps (mix->format,
252 best_width, best_height, best_fps_n, best_fps_d,
253 mix->par_n, mix->par_d);
255 peercaps = gst_pad_peer_get_caps (mix->srcpad);
259 s = gst_caps_get_structure (caps, 0);
260 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, "height",
261 GST_TYPE_INT_RANGE, 1, G_MAXINT, "framerate", GST_TYPE_FRACTION_RANGE,
262 0, 1, G_MAXINT, 1, NULL);
264 tmp = gst_caps_intersect (caps, peercaps);
265 gst_caps_unref (caps);
266 gst_caps_unref (peercaps);
268 if (gst_caps_is_empty (caps)) {
270 GST_VIDEO_MIXER2_UNLOCK (mix);
274 gst_caps_truncate (caps);
275 s = gst_caps_get_structure (caps, 0);
276 gst_structure_fixate_field_nearest_int (s, "width", best_width);
277 gst_structure_fixate_field_nearest_int (s, "height", best_height);
278 gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n,
281 gst_structure_get_int (s, "width", &best_width);
282 gst_structure_get_int (s, "height", &best_height);
283 gst_structure_get_fraction (s, "fraction", &best_fps_n, &best_fps_d);
286 mix->fps_n = best_fps_n;
287 mix->fps_d = best_fps_d;
288 mix->width = best_width;
289 mix->height = best_height;
291 GST_VIDEO_MIXER2_UNLOCK (mix);
292 ret = gst_pad_set_caps (mix->srcpad, caps);
293 gst_caps_unref (caps);
295 GST_VIDEO_MIXER2_UNLOCK (mix);
304 gst_videomixer2_pad_sink_setcaps (GstPad * pad, GstCaps * caps)
307 GstVideoMixer2Pad *mixpad;
310 gint fps_n = 0, fps_d = 0;
311 gint par_n = 1, par_d = 1;
312 gboolean ret = FALSE;
315 GST_INFO_OBJECT (pad, "Setting caps %" GST_PTR_FORMAT, caps);
317 mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
318 mixpad = GST_VIDEO_MIXER2_PAD (pad);
320 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
321 !gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d)) {
322 GST_ERROR_OBJECT (pad, "Failed to parse caps");
326 s = gst_caps_get_structure (caps, 0);
327 if (gst_structure_has_field (s, "framerate")
328 && !gst_video_parse_caps_framerate (caps, &fps_n, &fps_d)) {
329 GST_ERROR_OBJECT (pad, "Failed to parse caps");
333 GST_VIDEO_MIXER2_LOCK (mix);
334 if (mix->format != GST_VIDEO_FORMAT_UNKNOWN) {
335 if (mix->format != fmt || mix->par_n != par_n || mix->par_d != par_d) {
336 GST_ERROR_OBJECT (pad, "Caps not compatible with other pads' caps");
337 GST_VIDEO_MIXER2_UNLOCK (mix);
345 mixpad->fps_n = fps_n;
346 mixpad->fps_d = fps_d;
347 mixpad->width = width;
348 mixpad->height = height;
350 GST_VIDEO_MIXER2_UNLOCK (mix);
352 ret = gst_videomixer2_update_src_caps (mix);
355 gst_object_unref (mix);
361 gst_videomixer2_pad_sink_getcaps (GstPad * pad)
368 mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
370 srccaps = gst_pad_get_fixed_caps_func (GST_PAD (mix->srcpad));
371 srccaps = gst_caps_make_writable (srccaps);
373 n = gst_caps_get_size (srccaps);
374 for (i = 0; i < n; i++) {
375 s = gst_caps_get_structure (srccaps, i);
376 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
377 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
378 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
379 if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
380 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
384 GST_DEBUG_OBJECT (pad, "Returning %" GST_PTR_FORMAT, srccaps);
390 gst_videomixer2_pad_sink_acceptcaps (GstPad * pad, GstCaps * caps)
394 GstCaps *accepted_caps;
398 mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
399 GST_DEBUG_OBJECT (pad, "%" GST_PTR_FORMAT, caps);
401 accepted_caps = gst_pad_get_fixed_caps_func (GST_PAD (mix->srcpad));
402 accepted_caps = gst_caps_make_writable (accepted_caps);
403 GST_LOG_OBJECT (pad, "src caps %" GST_PTR_FORMAT, accepted_caps);
405 n = gst_caps_get_size (accepted_caps);
406 for (i = 0; i < n; i++) {
407 s = gst_caps_get_structure (accepted_caps, i);
408 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
409 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
410 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
411 if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
412 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
416 ret = gst_caps_can_intersect (caps, accepted_caps);
417 GST_INFO_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT, (ret ? "" : "not "),
419 GST_INFO_OBJECT (pad, "acceptable caps are %" GST_PTR_FORMAT, accepted_caps);
421 gst_caps_unref (accepted_caps);
423 gst_object_unref (mix);
428 gst_videomixer2_pad_get_property (GObject * object, guint prop_id,
429 GValue * value, GParamSpec * pspec)
431 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
434 case PROP_PAD_ZORDER:
435 g_value_set_uint (value, pad->zorder);
438 g_value_set_int (value, pad->xpos);
441 g_value_set_int (value, pad->ypos);
444 g_value_set_double (value, pad->alpha);
447 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453 pad_zorder_compare (const GstVideoMixer2Pad * pad1,
454 const GstVideoMixer2Pad * pad2)
456 return pad1->zorder - pad2->zorder;
460 gst_videomixer2_pad_set_property (GObject * object, guint prop_id,
461 const GValue * value, GParamSpec * pspec)
463 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
464 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (GST_PAD (pad)));
467 case PROP_PAD_ZORDER:
468 GST_VIDEO_MIXER2_LOCK (mix);
469 pad->zorder = g_value_get_uint (value);
471 mix->sinkpads = g_slist_sort (mix->sinkpads,
472 (GCompareFunc) pad_zorder_compare);
473 GST_VIDEO_MIXER2_UNLOCK (mix);
476 pad->xpos = g_value_get_int (value);
479 pad->ypos = g_value_get_int (value);
482 pad->alpha = g_value_get_double (value);
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
489 gst_object_unref (mix);
493 gst_videomixer2_pad_class_init (GstVideoMixer2PadClass * klass)
495 GObjectClass *gobject_class = (GObjectClass *) klass;
497 gobject_class->set_property = gst_videomixer2_pad_set_property;
498 gobject_class->get_property = gst_videomixer2_pad_get_property;
500 g_object_class_install_property (gobject_class, PROP_PAD_ZORDER,
501 g_param_spec_uint ("zorder", "Z-Order", "Z Order of the picture",
502 0, 10000, DEFAULT_PAD_ZORDER,
503 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
504 g_object_class_install_property (gobject_class, PROP_PAD_XPOS,
505 g_param_spec_int ("xpos", "X Position", "X Position of the picture",
506 G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
507 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
508 g_object_class_install_property (gobject_class, PROP_PAD_YPOS,
509 g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
510 G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
511 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
512 g_object_class_install_property (gobject_class, PROP_PAD_ALPHA,
513 g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
515 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
519 gst_videomixer2_pad_init (GstVideoMixer2Pad * mixerpad)
521 /* setup some pad functions */
522 gst_pad_set_setcaps_function (GST_PAD (mixerpad),
523 gst_videomixer2_pad_sink_setcaps);
524 gst_pad_set_acceptcaps_function (GST_PAD (mixerpad),
525 GST_DEBUG_FUNCPTR (gst_videomixer2_pad_sink_acceptcaps));
526 gst_pad_set_getcaps_function (GST_PAD (mixerpad),
527 gst_videomixer2_pad_sink_getcaps);
529 mixerpad->zorder = DEFAULT_PAD_ZORDER;
530 mixerpad->xpos = DEFAULT_PAD_XPOS;
531 mixerpad->ypos = DEFAULT_PAD_YPOS;
532 mixerpad->alpha = DEFAULT_PAD_ALPHA;
536 #define DEFAULT_BACKGROUND VIDEO_MIXER2_BACKGROUND_CHECKER
543 #define GST_TYPE_VIDEO_MIXER2_BACKGROUND (gst_videomixer2_background_get_type())
545 gst_videomixer2_background_get_type (void)
547 static GType video_mixer_background_type = 0;
549 static const GEnumValue video_mixer_background[] = {
550 {VIDEO_MIXER2_BACKGROUND_CHECKER, "Checker pattern", "checker"},
551 {VIDEO_MIXER2_BACKGROUND_BLACK, "Black", "black"},
552 {VIDEO_MIXER2_BACKGROUND_WHITE, "White", "white"},
553 {VIDEO_MIXER2_BACKGROUND_TRANSPARENT,
554 "Transparent Background to enable further mixing", "transparent"},
558 if (!video_mixer_background_type) {
559 video_mixer_background_type =
560 g_enum_register_static ("GstVideoMixer2Background",
561 video_mixer_background);
563 return video_mixer_background_type;
567 GST_BOILERPLATE_FULL (GstVideoMixer2, gst_videomixer2, GstElement,
568 GST_TYPE_ELEMENT, _do_init);
571 gst_videomixer2_update_qos (GstVideoMixer2 * mix, gdouble proportion,
572 GstClockTimeDiff diff, GstClockTime timestamp)
574 GST_DEBUG_OBJECT (mix,
575 "Updating QoS: proportion %lf, diff %s%" GST_TIME_FORMAT ", timestamp %"
576 GST_TIME_FORMAT, proportion, (diff < 0) ? "-" : "",
577 GST_TIME_ARGS (ABS (diff)), GST_TIME_ARGS (timestamp));
579 GST_OBJECT_LOCK (mix);
580 mix->proportion = proportion;
581 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
582 if (G_UNLIKELY (diff > 0))
584 timestamp + 2 * diff + gst_util_uint64_scale_int (GST_SECOND,
585 mix->fps_d, mix->fps_n);
587 mix->earliest_time = timestamp + diff;
589 mix->earliest_time = GST_CLOCK_TIME_NONE;
591 GST_OBJECT_UNLOCK (mix);
595 gst_videomixer2_reset_qos (GstVideoMixer2 * mix)
597 gst_videomixer2_update_qos (mix, 0.5, 0, GST_CLOCK_TIME_NONE);
598 mix->qos_processed = mix->qos_dropped = 0;
602 gst_videomixer2_read_qos (GstVideoMixer2 * mix, gdouble * proportion,
605 GST_OBJECT_LOCK (mix);
606 *proportion = mix->proportion;
607 *time = mix->earliest_time;
608 GST_OBJECT_UNLOCK (mix);
612 gst_videomixer2_reset (GstVideoMixer2 * mix)
616 mix->format = GST_VIDEO_FORMAT_UNKNOWN;
617 mix->width = mix->height = 0;
618 mix->fps_n = mix->fps_d = 0;
619 mix->par_n = mix->par_d = 0;
623 gst_segment_init (&mix->segment, GST_FORMAT_TIME);
624 mix->segment.last_stop = -1;
626 gst_videomixer2_reset_qos (mix);
628 for (l = mix->sinkpads; l; l = l->next) {
629 GstVideoMixer2Pad *p = l->data;
630 GstVideoMixer2Collect *mixcol = p->mixcol;
632 gst_buffer_replace (&mixcol->buffer, NULL);
633 mixcol->start_time = -1;
634 mixcol->end_time = -1;
636 p->fps_n = p->fps_d = 0;
637 p->width = p->height = 0;
640 mix->newseg_pending = TRUE;
641 mix->flush_stop_pending = FALSE;
645 * 0 == need more data
650 gst_videomixer2_fill_queues (GstVideoMixer2 * mix,
651 GstClockTime output_start_time, GstClockTime output_end_time)
655 gboolean need_more_data = FALSE;
657 for (l = mix->sinkpads; l; l = l->next) {
658 GstVideoMixer2Pad *pad = l->data;
659 GstVideoMixer2Collect *mixcol = pad->mixcol;
660 GstSegment *segment = &pad->mixcol->collect.segment;
663 buf = gst_collect_pads2_peek (mix->collect, &mixcol->collect);
665 GstClockTime start_time, end_time;
667 start_time = GST_BUFFER_TIMESTAMP (buf);
668 if (start_time == -1) {
669 gst_buffer_unref (buf);
670 GST_ERROR_OBJECT (pad, "Need timestamped buffers!");
674 /* FIXME: Make all this work with negative rates */
676 if ((mixcol->buffer && start_time < GST_BUFFER_TIMESTAMP (mixcol->buffer))
678 && start_time < GST_BUFFER_TIMESTAMP (mixcol->queued))) {
679 GST_WARNING_OBJECT (pad, "Buffer from the past, dropping");
680 gst_buffer_unref (buf);
681 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
682 gst_buffer_unref (buf);
683 need_more_data = TRUE;
687 if (mixcol->queued) {
688 end_time = start_time - GST_BUFFER_TIMESTAMP (mixcol->queued);
689 start_time = GST_BUFFER_TIMESTAMP (mixcol->queued);
690 gst_buffer_unref (buf);
691 buf = gst_buffer_ref (mixcol->queued);
693 end_time = GST_BUFFER_DURATION (buf);
695 if (end_time == -1) {
696 mixcol->queued = buf;
697 need_more_data = TRUE;
702 g_assert (start_time != -1 && end_time != -1);
703 end_time += start_time; /* convert from duration to position */
705 if (mixcol->end_time != -1 && mixcol->end_time > end_time) {
706 GST_WARNING_OBJECT (pad, "Buffer from the past, dropping");
707 if (buf == mixcol->queued) {
708 gst_buffer_unref (buf);
709 gst_buffer_replace (&mixcol->queued, NULL);
711 gst_buffer_unref (buf);
712 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
713 gst_buffer_unref (buf);
716 need_more_data = TRUE;
720 /* Check if it's inside the segment */
721 if (start_time >= segment->stop || end_time < segment->start) {
722 GST_DEBUG_OBJECT (pad, "Buffer outside the segment");
724 if (buf == mixcol->queued) {
725 gst_buffer_unref (buf);
726 gst_buffer_replace (&mixcol->queued, NULL);
728 gst_buffer_unref (buf);
729 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
730 gst_buffer_unref (buf);
733 need_more_data = TRUE;
737 /* Clip to segment and convert to running time */
738 start_time = MAX (start_time, segment->start);
739 if (segment->stop != -1)
740 end_time = MIN (end_time, segment->stop);
742 gst_segment_to_running_time (segment, GST_FORMAT_TIME, start_time);
744 gst_segment_to_running_time (segment, GST_FORMAT_TIME, end_time);
745 g_assert (start_time != -1 && end_time != -1);
747 /* Convert to the output segment rate */
748 if (mix->segment.abs_rate != 1.0) {
749 start_time *= mix->segment.abs_rate;
750 end_time *= mix->segment.abs_rate;
753 if (end_time >= output_start_time && start_time < output_end_time) {
754 GST_DEBUG_OBJECT (pad,
755 "Taking new buffer with start time %" GST_TIME_FORMAT,
756 GST_TIME_ARGS (start_time));
757 gst_buffer_replace (&mixcol->buffer, buf);
758 mixcol->start_time = start_time;
759 mixcol->end_time = end_time;
761 if (buf == mixcol->queued) {
762 gst_buffer_unref (buf);
763 gst_buffer_replace (&mixcol->queued, NULL);
765 gst_buffer_unref (buf);
766 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
767 gst_buffer_unref (buf);
770 } else if (start_time >= output_end_time) {
771 GST_DEBUG_OBJECT (pad, "Keeping buffer until %" GST_TIME_FORMAT,
772 GST_TIME_ARGS (start_time));
773 gst_buffer_unref (buf);
776 GST_DEBUG_OBJECT (pad, "Too old buffer -- dropping");
777 if (buf == mixcol->queued) {
778 gst_buffer_unref (buf);
779 gst_buffer_replace (&mixcol->queued, NULL);
781 gst_buffer_unref (buf);
782 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
783 gst_buffer_unref (buf);
786 need_more_data = TRUE;
790 if (mixcol->end_time != -1) {
791 if (mixcol->end_time < output_start_time) {
792 gst_buffer_replace (&mixcol->buffer, NULL);
793 mixcol->start_time = mixcol->end_time = -1;
794 if (!GST_COLLECT_PADS2_STATE_IS_SET (mixcol,
795 GST_COLLECT_PADS2_STATE_EOS))
796 need_more_data = TRUE;
813 gst_videomixer2_blend_buffers (GstVideoMixer2 * mix,
814 GstClockTime output_start_time, GstClockTime output_end_time,
820 BlendFunction composite;
822 outsize = gst_video_format_get_size (mix->format, mix->width, mix->height);
823 ret = gst_pad_alloc_buffer_and_set_caps (mix->srcpad, GST_BUFFER_OFFSET_NONE,
824 outsize, GST_PAD_CAPS (mix->srcpad), outbuf);
825 if (ret != GST_FLOW_OK)
828 GST_BUFFER_TIMESTAMP (*outbuf) = output_start_time;
829 GST_BUFFER_DURATION (*outbuf) = output_end_time - output_start_time;
831 /* default to blending */
832 composite = mix->blend;
833 switch (mix->background) {
834 case VIDEO_MIXER2_BACKGROUND_CHECKER:
835 mix->fill_checker (GST_BUFFER_DATA (*outbuf), mix->width, mix->height);
837 case VIDEO_MIXER2_BACKGROUND_BLACK:
838 mix->fill_color (GST_BUFFER_DATA (*outbuf), mix->width,
839 mix->height, 16, 128, 128);
841 case VIDEO_MIXER2_BACKGROUND_WHITE:
842 mix->fill_color (GST_BUFFER_DATA (*outbuf), mix->width,
843 mix->height, 240, 128, 128);
845 case VIDEO_MIXER2_BACKGROUND_TRANSPARENT:
846 orc_memset (GST_BUFFER_DATA (*outbuf), 0,
847 gst_video_format_get_row_stride (mix->format, 0,
848 mix->width) * mix->height);
849 /* use overlay to keep background transparent */
850 composite = mix->overlay;
854 for (l = mix->sinkpads; l; l = l->next) {
855 GstVideoMixer2Pad *pad = l->data;
856 GstVideoMixer2Collect *mixcol = pad->mixcol;
858 if (mixcol->buffer != NULL) {
859 GstClockTime timestamp;
863 seg = &mixcol->collect.segment;
865 timestamp = GST_BUFFER_TIMESTAMP (mixcol->buffer);
868 gst_segment_to_stream_time (seg, GST_FORMAT_TIME, timestamp);
870 /* sync object properties on stream time */
871 if (GST_CLOCK_TIME_IS_VALID (stream_time))
872 gst_object_sync_values (G_OBJECT (pad), stream_time);
874 composite (GST_BUFFER_DATA (mixcol->buffer),
875 pad->xpos, pad->ypos, pad->width, pad->height, pad->alpha,
876 GST_BUFFER_DATA (*outbuf), mix->width, mix->height);
883 /* Perform qos calculations before processing the next frame. Returns TRUE if
884 * the frame should be processed, FALSE if the frame can be dropped entirely */
886 gst_videomixer2_do_qos (GstVideoMixer2 * mix, GstClockTime timestamp)
888 GstClockTime qostime, earliest_time;
892 /* no timestamp, can't do QoS => process frame */
893 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
894 GST_LOG_OBJECT (mix, "invalid timestamp, can't do QoS, process frame");
898 /* get latest QoS observation values */
899 gst_videomixer2_read_qos (mix, &proportion, &earliest_time);
901 /* skip qos if we have no observation (yet) => process frame */
902 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
903 GST_LOG_OBJECT (mix, "no observation yet, process frame");
907 /* qos is done on running time */
909 gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME, timestamp);
911 /* see how our next timestamp relates to the latest qos timestamp */
912 GST_LOG_OBJECT (mix, "qostime %" GST_TIME_FORMAT ", earliest %"
913 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
915 jitter = GST_CLOCK_DIFF (qostime, earliest_time);
916 if (qostime != GST_CLOCK_TIME_NONE && jitter > 0) {
917 GST_DEBUG_OBJECT (mix, "we are late, drop frame");
921 GST_LOG_OBJECT (mix, "process frame");
926 gst_videomixer2_collected (GstCollectPads2 * pads, GstVideoMixer2 * mix)
929 GstClockTime output_start_time, output_end_time;
930 GstBuffer *outbuf = NULL;
934 /* If we're not negotiated yet... */
935 if (mix->format == GST_VIDEO_FORMAT_UNKNOWN)
936 return GST_FLOW_NOT_NEGOTIATED;
938 if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE, FALSE)) {
939 GST_DEBUG_OBJECT (mix, "pending flush stop");
940 gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop ());
943 GST_VIDEO_MIXER2_LOCK (mix);
945 if (mix->newseg_pending) {
946 GST_DEBUG_OBJECT (mix, "Sending NEWSEGMENT event");
947 if (!gst_pad_push_event (mix->srcpad, gst_event_new_new_segment_full (FALSE,
948 mix->segment.rate, mix->segment.applied_rate,
949 mix->segment.format, mix->segment.start, mix->segment.stop,
950 mix->segment.time))) {
951 ret = GST_FLOW_ERROR;
954 mix->newseg_pending = FALSE;
957 if (mix->segment.last_stop == -1)
958 output_start_time = mix->segment.start;
960 output_start_time = mix->segment.last_stop;
962 if (output_start_time >= mix->segment.stop) {
963 GST_DEBUG_OBJECT (mix, "Segment done");
964 gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
965 ret = GST_FLOW_UNEXPECTED;
970 mix->ts_offset + gst_util_uint64_scale (mix->nframes + 1,
971 GST_SECOND * mix->fps_d, mix->fps_n);
972 if (mix->segment.stop != -1)
973 output_end_time = MIN (output_end_time, mix->segment.stop);
975 res = gst_videomixer2_fill_queues (mix, output_start_time, output_end_time);
978 GST_DEBUG_OBJECT (mix, "Need more data for decisions");
981 } else if (res == -1) {
982 GST_DEBUG_OBJECT (mix, "All sinkpads are EOS -- forwarding");
983 gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
984 ret = GST_FLOW_UNEXPECTED;
986 } else if (res == -2) {
987 GST_ERROR_OBJECT (mix, "Error collecting buffers");
988 ret = GST_FLOW_ERROR;
992 jitter = gst_videomixer2_do_qos (mix, output_start_time);
995 gst_videomixer2_blend_buffers (mix, output_start_time,
996 output_end_time, &outbuf);
997 mix->qos_processed++;
1005 gst_message_new_qos (GST_OBJECT_CAST (mix), FALSE,
1006 gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME,
1007 output_start_time), gst_segment_to_stream_time (&mix->segment,
1008 GST_FORMAT_TIME, output_start_time), output_start_time,
1009 output_end_time - output_start_time);
1010 gst_message_set_qos_values (msg, jitter, mix->proportion, 1000000);
1011 gst_message_set_qos_stats (msg, GST_FORMAT_BUFFERS, mix->qos_processed,
1013 gst_element_post_message (GST_ELEMENT_CAST (mix), msg);
1018 gst_segment_set_last_stop (&mix->segment, GST_FORMAT_TIME, output_end_time);
1021 GST_VIDEO_MIXER2_UNLOCK (mix);
1023 GST_LOG_OBJECT (mix,
1024 "Pushing buffer with ts %" GST_TIME_FORMAT " and duration %"
1025 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1026 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
1027 ret = gst_pad_push (mix->srcpad, outbuf);
1029 GST_VIDEO_MIXER2_LOCK (mix);
1032 GST_VIDEO_MIXER2_UNLOCK (mix);
1038 gst_videomixer2_src_getcaps (GstPad * pad)
1040 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
1045 if (mix->format != GST_VIDEO_FORMAT_UNKNOWN) {
1046 caps = gst_caps_copy (GST_PAD_CAPS (mix->srcpad));
1048 caps = gst_caps_copy (gst_pad_get_pad_template_caps (mix->srcpad));
1051 n = gst_caps_get_size (caps) - 1;
1052 for (; n >= 0; n--) {
1053 s = gst_caps_get_structure (caps, n);
1054 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1055 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
1056 if (mix->fps_d != 0) {
1057 gst_structure_set (s,
1058 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1062 gst_object_unref (mix);
1068 gst_videomixer2_query_duration (GstVideoMixer2 * mix, GstQuery * query)
1077 gst_query_parse_duration (query, &format, NULL);
1083 /* Take maximum of all durations */
1084 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1086 GstIteratorResult ires;
1089 ires = gst_iterator_next (it, &item);
1091 case GST_ITERATOR_DONE:
1094 case GST_ITERATOR_OK:
1096 GstPad *pad = GST_PAD_CAST (item);
1099 /* ask sink peer for duration */
1100 res &= gst_pad_query_peer_duration (pad, &format, &duration);
1101 /* take max from all valid return values */
1103 /* valid unknown length, stop searching */
1104 if (duration == -1) {
1108 /* else see if bigger than current max */
1109 else if (duration > max)
1112 gst_object_unref (pad);
1115 case GST_ITERATOR_RESYNC:
1118 gst_iterator_resync (it);
1126 gst_iterator_free (it);
1129 /* and store the max */
1130 GST_DEBUG_OBJECT (mix, "Total duration in format %s: %"
1131 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1132 gst_query_set_duration (query, format, max);
1139 gst_videomixer2_query_latency (GstVideoMixer2 * mix, GstQuery * query)
1141 GstClockTime min, max;
1151 max = GST_CLOCK_TIME_NONE;
1153 /* Take maximum of all latency values */
1154 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1156 GstIteratorResult ires;
1159 ires = gst_iterator_next (it, &item);
1161 case GST_ITERATOR_DONE:
1164 case GST_ITERATOR_OK:
1166 GstPad *pad = GST_PAD_CAST (item);
1167 GstQuery *peerquery;
1168 GstClockTime min_cur, max_cur;
1171 peerquery = gst_query_new_latency ();
1173 /* Ask peer for latency */
1174 res &= gst_pad_peer_query (pad, peerquery);
1176 /* take max from all valid return values */
1178 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
1183 if (max_cur != GST_CLOCK_TIME_NONE &&
1184 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
1185 (max == GST_CLOCK_TIME_NONE)))
1188 live = live || live_cur;
1191 gst_query_unref (peerquery);
1192 gst_object_unref (pad);
1195 case GST_ITERATOR_RESYNC:
1198 max = GST_CLOCK_TIME_NONE;
1200 gst_iterator_resync (it);
1208 gst_iterator_free (it);
1211 /* store the results */
1212 GST_DEBUG_OBJECT (mix, "Calculated total latency: live %s, min %"
1213 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
1214 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1215 gst_query_set_latency (query, live, min, max);
1222 gst_videomixer2_src_query (GstPad * pad, GstQuery * query)
1224 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
1225 gboolean res = FALSE;
1227 switch (GST_QUERY_TYPE (query)) {
1228 case GST_QUERY_POSITION:
1232 gst_query_parse_position (query, &format, NULL);
1235 case GST_FORMAT_TIME:
1236 gst_query_set_position (query, format,
1237 gst_segment_to_stream_time (&mix->segment, GST_FORMAT_TIME,
1238 mix->segment.last_stop));
1246 case GST_QUERY_DURATION:
1247 res = gst_videomixer2_query_duration (mix, query);
1249 case GST_QUERY_LATENCY:
1250 res = gst_videomixer2_query_latency (mix, query);
1253 /* FIXME, needs a custom query handler because we have multiple
1256 gst_query_unref (query);
1260 gst_object_unref (mix);
1265 gst_videomixer2_src_event (GstPad * pad, GstEvent * event)
1267 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
1270 switch (GST_EVENT_TYPE (event)) {
1271 case GST_EVENT_QOS:{
1272 GstClockTimeDiff diff;
1273 GstClockTime timestamp;
1276 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1278 gst_videomixer2_update_qos (mix, proportion, diff, timestamp);
1280 result = gst_videomixer2_push_sink_event (mix, event);
1283 case GST_EVENT_SEEK:
1288 GstSeekType start_type, stop_type;
1293 /* parse the seek parameters */
1294 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1295 &start, &stop_type, &stop);
1298 GST_ERROR_OBJECT (mix, "Negative rates not supported yet");
1300 gst_event_unref (event);
1304 GST_DEBUG_OBJECT (mix, "Handling SEEK event");
1306 /* check if we are flushing */
1307 if (flags & GST_SEEK_FLAG_FLUSH) {
1308 /* flushing seek, start flush downstream, the flush will be done
1309 * when all pads received a FLUSH_STOP. */
1310 gst_pad_push_event (mix->srcpad, gst_event_new_flush_start ());
1312 /* make sure we accept nothing anymore and return WRONG_STATE */
1313 gst_collect_pads2_set_flushing (mix->collect, TRUE);
1316 /* now wait for the collected to be finished and mark a new
1318 GST_COLLECT_PADS2_STREAM_LOCK (mix->collect);
1320 abs_rate = ABS (rate);
1322 GST_VIDEO_MIXER2_LOCK (mix);
1323 for (l = mix->sinkpads; l; l = l->next) {
1324 GstVideoMixer2Pad *p = l->data;
1326 if (flags & GST_SEEK_FLAG_FLUSH) {
1327 gst_buffer_replace (&p->mixcol->buffer, NULL);
1328 p->mixcol->start_time = p->mixcol->end_time = -1;
1332 /* Convert to the output segment rate */
1333 if (mix->segment.abs_rate != abs_rate) {
1334 if (mix->segment.abs_rate != 1.0 && p->mixcol->buffer) {
1335 p->mixcol->start_time /= mix->segment.abs_rate;
1336 p->mixcol->end_time /= mix->segment.abs_rate;
1338 if (abs_rate != 1.0 && p->mixcol->buffer) {
1339 p->mixcol->start_time *= abs_rate;
1340 p->mixcol->end_time *= abs_rate;
1344 GST_VIDEO_MIXER2_UNLOCK (mix);
1346 gst_segment_set_seek (&mix->segment, rate, fmt, flags, start_type, start,
1347 stop_type, stop, NULL);
1348 mix->segment.last_stop = -1;
1351 mix->newseg_pending = TRUE;
1353 if (flags & GST_SEEK_FLAG_FLUSH) {
1354 gst_collect_pads2_set_flushing (mix->collect, FALSE);
1356 /* we can't send FLUSH_STOP here since upstream could start pushing data
1357 * after we unlock mix->collect.
1358 * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
1359 * forwarding the seek upstream or from gst_videomixer_collected,
1360 * whichever happens first.
1362 mix->flush_stop_pending = TRUE;
1365 GST_COLLECT_PADS2_STREAM_UNLOCK (mix->collect);
1367 gst_videomixer2_reset_qos (mix);
1369 result = gst_videomixer2_push_sink_event (mix, event);
1371 if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE,
1373 GST_DEBUG_OBJECT (mix, "pending flush stop");
1374 gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop ());
1379 case GST_EVENT_NAVIGATION:
1380 /* navigation is rather pointless. */
1382 gst_event_unref (event);
1385 /* just forward the rest for now */
1386 result = gst_videomixer2_push_sink_event (mix, event);
1389 gst_object_unref (mix);
1395 gst_videomixer2_src_setcaps (GstPad * pad, GstCaps * caps)
1397 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent_element (pad));
1398 gboolean ret = FALSE;
1404 GST_INFO_OBJECT (pad, "set src caps: %" GST_PTR_FORMAT, caps);
1407 mix->overlay = NULL;
1408 mix->fill_checker = NULL;
1409 mix->fill_color = NULL;
1411 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
1412 !gst_video_parse_caps_framerate (caps, &fps_n, &fps_d) ||
1413 !gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d))
1416 GST_VIDEO_MIXER2_LOCK (mix);
1418 if (mix->fps_n != fps_n || mix->fps_d != fps_d) {
1419 if (mix->segment.last_stop != -1) {
1420 mix->ts_offset = mix->segment.last_stop - mix->segment.start;
1423 gst_videomixer2_reset_qos (mix);
1428 mix->height = height;
1434 switch (mix->format) {
1435 case GST_VIDEO_FORMAT_AYUV:
1436 mix->blend = gst_video_mixer_blend_ayuv;
1437 mix->overlay = gst_video_mixer_overlay_ayuv;
1438 mix->fill_checker = gst_video_mixer_fill_checker_ayuv;
1439 mix->fill_color = gst_video_mixer_fill_color_ayuv;
1442 case GST_VIDEO_FORMAT_ARGB:
1443 mix->blend = gst_video_mixer_blend_argb;
1444 mix->overlay = gst_video_mixer_overlay_argb;
1445 mix->fill_checker = gst_video_mixer_fill_checker_argb;
1446 mix->fill_color = gst_video_mixer_fill_color_argb;
1449 case GST_VIDEO_FORMAT_BGRA:
1450 mix->blend = gst_video_mixer_blend_bgra;
1451 mix->overlay = gst_video_mixer_overlay_bgra;
1452 mix->fill_checker = gst_video_mixer_fill_checker_bgra;
1453 mix->fill_color = gst_video_mixer_fill_color_bgra;
1456 case GST_VIDEO_FORMAT_ABGR:
1457 mix->blend = gst_video_mixer_blend_abgr;
1458 mix->overlay = gst_video_mixer_overlay_abgr;
1459 mix->fill_checker = gst_video_mixer_fill_checker_abgr;
1460 mix->fill_color = gst_video_mixer_fill_color_abgr;
1463 case GST_VIDEO_FORMAT_RGBA:
1464 mix->blend = gst_video_mixer_blend_rgba;
1465 mix->overlay = gst_video_mixer_overlay_rgba;
1466 mix->fill_checker = gst_video_mixer_fill_checker_rgba;
1467 mix->fill_color = gst_video_mixer_fill_color_rgba;
1470 case GST_VIDEO_FORMAT_Y444:
1471 mix->blend = gst_video_mixer_blend_y444;
1472 mix->overlay = mix->blend;
1473 mix->fill_checker = gst_video_mixer_fill_checker_y444;
1474 mix->fill_color = gst_video_mixer_fill_color_y444;
1477 case GST_VIDEO_FORMAT_Y42B:
1478 mix->blend = gst_video_mixer_blend_y42b;
1479 mix->overlay = mix->blend;
1480 mix->fill_checker = gst_video_mixer_fill_checker_y42b;
1481 mix->fill_color = gst_video_mixer_fill_color_y42b;
1484 case GST_VIDEO_FORMAT_YUY2:
1485 mix->blend = gst_video_mixer_blend_yuy2;
1486 mix->overlay = mix->blend;
1487 mix->fill_checker = gst_video_mixer_fill_checker_yuy2;
1488 mix->fill_color = gst_video_mixer_fill_color_yuy2;
1491 case GST_VIDEO_FORMAT_UYVY:
1492 mix->blend = gst_video_mixer_blend_uyvy;
1493 mix->overlay = mix->blend;
1494 mix->fill_checker = gst_video_mixer_fill_checker_uyvy;
1495 mix->fill_color = gst_video_mixer_fill_color_uyvy;
1498 case GST_VIDEO_FORMAT_YVYU:
1499 mix->blend = gst_video_mixer_blend_yvyu;
1500 mix->overlay = mix->blend;
1501 mix->fill_checker = gst_video_mixer_fill_checker_yvyu;
1502 mix->fill_color = gst_video_mixer_fill_color_yvyu;
1505 case GST_VIDEO_FORMAT_I420:
1506 mix->blend = gst_video_mixer_blend_i420;
1507 mix->overlay = mix->blend;
1508 mix->fill_checker = gst_video_mixer_fill_checker_i420;
1509 mix->fill_color = gst_video_mixer_fill_color_i420;
1512 case GST_VIDEO_FORMAT_YV12:
1513 mix->blend = gst_video_mixer_blend_yv12;
1514 mix->overlay = mix->blend;
1515 mix->fill_checker = gst_video_mixer_fill_checker_yv12;
1516 mix->fill_color = gst_video_mixer_fill_color_yv12;
1519 case GST_VIDEO_FORMAT_Y41B:
1520 mix->blend = gst_video_mixer_blend_y41b;
1521 mix->overlay = mix->blend;
1522 mix->fill_checker = gst_video_mixer_fill_checker_y41b;
1523 mix->fill_color = gst_video_mixer_fill_color_y41b;
1526 case GST_VIDEO_FORMAT_RGB:
1527 mix->blend = gst_video_mixer_blend_rgb;
1528 mix->overlay = mix->blend;
1529 mix->fill_checker = gst_video_mixer_fill_checker_rgb;
1530 mix->fill_color = gst_video_mixer_fill_color_rgb;
1533 case GST_VIDEO_FORMAT_BGR:
1534 mix->blend = gst_video_mixer_blend_bgr;
1535 mix->overlay = mix->blend;
1536 mix->fill_checker = gst_video_mixer_fill_checker_bgr;
1537 mix->fill_color = gst_video_mixer_fill_color_bgr;
1540 case GST_VIDEO_FORMAT_xRGB:
1541 mix->blend = gst_video_mixer_blend_xrgb;
1542 mix->overlay = mix->blend;
1543 mix->fill_checker = gst_video_mixer_fill_checker_xrgb;
1544 mix->fill_color = gst_video_mixer_fill_color_xrgb;
1547 case GST_VIDEO_FORMAT_xBGR:
1548 mix->blend = gst_video_mixer_blend_xbgr;
1549 mix->overlay = mix->blend;
1550 mix->fill_checker = gst_video_mixer_fill_checker_xbgr;
1551 mix->fill_color = gst_video_mixer_fill_color_xbgr;
1554 case GST_VIDEO_FORMAT_RGBx:
1555 mix->blend = gst_video_mixer_blend_rgbx;
1556 mix->overlay = mix->blend;
1557 mix->fill_checker = gst_video_mixer_fill_checker_rgbx;
1558 mix->fill_color = gst_video_mixer_fill_color_rgbx;
1561 case GST_VIDEO_FORMAT_BGRx:
1562 mix->blend = gst_video_mixer_blend_bgrx;
1563 mix->overlay = mix->blend;
1564 mix->fill_checker = gst_video_mixer_fill_checker_bgrx;
1565 mix->fill_color = gst_video_mixer_fill_color_bgrx;
1571 GST_VIDEO_MIXER2_UNLOCK (mix);
1574 gst_object_unref (mix);
1579 static GstFlowReturn
1580 gst_videomixer2_sink_prepare_buffer (GstCollectPads2 * pads,
1581 GstCollectData2 * data, GstBuffer * buf, GstVideoMixer2 * mix)
1583 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (data->pad);
1584 GstVideoMixer2Collect *mixcol = pad->mixcol;
1585 GstClockTime start_time, end_time;
1587 start_time = GST_BUFFER_TIMESTAMP (buf);
1588 if (start_time == -1) {
1589 GST_ERROR_OBJECT (pad, "Timestamped buffers required!");
1590 return GST_FLOW_ERROR;
1593 end_time = GST_BUFFER_DURATION (buf);
1595 end_time = gst_util_uint64_scale_int (GST_SECOND, pad->fps_d, pad->fps_n);
1599 start_time = MAX (start_time, mixcol->collect.segment.start);
1601 gst_segment_to_running_time (&mixcol->collect.segment,
1602 GST_FORMAT_TIME, start_time);
1604 end_time += GST_BUFFER_TIMESTAMP (buf);
1605 if (mixcol->collect.segment.stop != -1)
1606 end_time = MIN (end_time, mixcol->collect.segment.stop);
1608 gst_segment_to_running_time (&mixcol->collect.segment,
1609 GST_FORMAT_TIME, end_time);
1611 /* Convert to the output segment rate */
1612 if (mix->segment.abs_rate != 1.0) {
1613 start_time *= mix->segment.abs_rate;
1614 end_time *= mix->segment.abs_rate;
1617 if (mixcol->buffer != NULL && end_time < mixcol->end_time) {
1618 return GST_COLLECT_PADS2_FLOW_DROP;
1625 gst_videomixer2_sink_event (GstCollectPads2 * pads, GstCollectData2 * cdata,
1626 GstEvent * event, GstVideoMixer2 * mix)
1628 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (cdata->pad);
1629 gboolean ret = TRUE;
1631 GST_DEBUG_OBJECT (pad, "Got %s event on pad %s:%s",
1632 GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (pad));
1634 /* return FALSE => event will be forwarded */
1635 switch (GST_EVENT_TYPE (event)) {
1636 case GST_EVENT_NEWSEGMENT:{
1638 gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
1640 g_assert (fmt == GST_FORMAT_TIME);
1641 /* eat NEWSEGMENT events, collectpads2 unrefs the event */
1645 case GST_EVENT_FLUSH_STOP:
1646 mix->newseg_pending = TRUE;
1647 mix->flush_stop_pending = FALSE;
1648 gst_videomixer2_reset_qos (mix);
1649 gst_buffer_replace (&pad->mixcol->buffer, NULL);
1650 pad->mixcol->start_time = -1;
1651 pad->mixcol->end_time = -1;
1653 gst_segment_init (&mix->segment, GST_FORMAT_TIME);
1654 mix->segment.last_stop = -1;
1658 gst_pad_push_event (mix->srcpad, event);
1661 gst_pad_push_event (mix->srcpad, event);
1669 forward_event_func (GstPad * pad, GValue * ret, GstEvent * event)
1671 gst_event_ref (event);
1672 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
1673 if (!gst_pad_push_event (pad, event)) {
1674 g_value_set_boolean (ret, FALSE);
1675 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
1676 event, GST_EVENT_TYPE_NAME (event));
1678 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
1679 event, GST_EVENT_TYPE_NAME (event));
1681 gst_object_unref (pad);
1686 gst_videomixer2_push_sink_event (GstVideoMixer2 * mix, GstEvent * event)
1689 GValue vret = { 0 };
1691 GST_LOG_OBJECT (mix, "Forwarding event %p (%s)", event,
1692 GST_EVENT_TYPE_NAME (event));
1694 g_value_init (&vret, G_TYPE_BOOLEAN);
1695 g_value_set_boolean (&vret, TRUE);
1696 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1697 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
1699 gst_iterator_free (it);
1700 gst_event_unref (event);
1702 return g_value_get_boolean (&vret);
1705 /* GstElement vmethods */
1706 static GstStateChangeReturn
1707 gst_videomixer2_change_state (GstElement * element, GstStateChange transition)
1709 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (element);
1710 GstStateChangeReturn ret;
1712 switch (transition) {
1713 case GST_STATE_CHANGE_READY_TO_PAUSED:
1714 GST_LOG_OBJECT (mix, "starting collectpads");
1715 gst_collect_pads2_start (mix->collect);
1717 case GST_STATE_CHANGE_PAUSED_TO_READY:
1718 GST_LOG_OBJECT (mix, "stopping collectpads");
1719 gst_collect_pads2_stop (mix->collect);
1725 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1727 switch (transition) {
1728 case GST_STATE_CHANGE_PAUSED_TO_READY:
1729 gst_videomixer2_reset (mix);
1739 gst_videomixer2_request_new_pad (GstElement * element,
1740 GstPadTemplate * templ, const gchar * req_name)
1742 GstVideoMixer2 *mix;
1743 GstVideoMixer2Pad *mixpad;
1744 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1746 mix = GST_VIDEO_MIXER2 (element);
1748 if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
1751 GstVideoMixer2Collect *mixcol = NULL;
1753 GST_VIDEO_MIXER2_LOCK (mix);
1754 if (req_name == NULL || strlen (req_name) < 6
1755 || !g_str_has_prefix (req_name, "sink_")) {
1756 /* no name given when requesting the pad, use next available int */
1757 serial = mix->next_sinkpad++;
1759 /* parse serial number from requested padname */
1760 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1761 if (serial >= mix->next_sinkpad)
1762 mix->next_sinkpad = serial + 1;
1764 /* create new pad with the name */
1765 name = g_strdup_printf ("sink_%d", serial);
1766 mixpad = g_object_new (GST_TYPE_VIDEO_MIXER2_PAD, "name", name, "direction",
1767 templ->direction, "template", templ, NULL);
1770 mixpad->zorder = mix->numpads;
1771 mixpad->xpos = DEFAULT_PAD_XPOS;
1772 mixpad->ypos = DEFAULT_PAD_YPOS;
1773 mixpad->alpha = DEFAULT_PAD_ALPHA;
1775 mixcol = (GstVideoMixer2Collect *)
1776 gst_collect_pads2_add_pad_full (mix->collect, GST_PAD (mixpad),
1777 sizeof (GstVideoMixer2Collect),
1778 (GstCollectData2DestroyNotify) gst_videomixer2_collect_free, TRUE);
1780 /* Keep track of each other */
1781 mixcol->mixpad = mixpad;
1782 mixpad->mixcol = mixcol;
1784 mixcol->start_time = -1;
1785 mixcol->end_time = -1;
1787 /* Keep an internal list of mixpads for zordering */
1788 mix->sinkpads = g_slist_append (mix->sinkpads, mixpad);
1790 GST_VIDEO_MIXER2_UNLOCK (mix);
1795 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (mixpad));
1797 /* add the pad to the element */
1798 gst_element_add_pad (element, GST_PAD (mixpad));
1799 gst_child_proxy_child_added (GST_OBJECT (mix), GST_OBJECT (mixpad));
1801 return GST_PAD (mixpad);
1805 gst_videomixer2_release_pad (GstElement * element, GstPad * pad)
1807 GstVideoMixer2 *mix = NULL;
1808 GstVideoMixer2Pad *mixpad;
1809 gboolean update_caps;
1811 mix = GST_VIDEO_MIXER2 (element);
1813 GST_VIDEO_MIXER2_LOCK (mix);
1814 if (G_UNLIKELY (g_slist_find (mix->sinkpads, pad) == NULL)) {
1815 g_warning ("Unknown pad %s", GST_PAD_NAME (pad));
1819 mixpad = GST_VIDEO_MIXER2_PAD (pad);
1821 mix->sinkpads = g_slist_remove (mix->sinkpads, pad);
1822 gst_child_proxy_child_removed (GST_OBJECT (mix), GST_OBJECT (mixpad));
1825 update_caps = mix->format != GST_VIDEO_FORMAT_UNKNOWN;
1826 GST_VIDEO_MIXER2_UNLOCK (mix);
1828 gst_collect_pads2_remove_pad (mix->collect, pad);
1831 gst_videomixer2_update_src_caps (mix);
1833 gst_element_remove_pad (element, pad);
1836 GST_VIDEO_MIXER2_UNLOCK (mix);
1839 /* GObject vmethods */
1841 gst_videomixer2_finalize (GObject * o)
1843 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (o);
1845 gst_object_unref (mix->collect);
1846 g_mutex_free (mix->lock);
1848 G_OBJECT_CLASS (parent_class)->finalize (o);
1852 gst_videomixer2_get_property (GObject * object,
1853 guint prop_id, GValue * value, GParamSpec * pspec)
1855 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1858 case PROP_BACKGROUND:
1859 g_value_set_enum (value, mix->background);
1862 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1868 gst_videomixer2_set_property (GObject * object,
1869 guint prop_id, const GValue * value, GParamSpec * pspec)
1871 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1874 case PROP_BACKGROUND:
1875 mix->background = g_value_get_enum (value);
1878 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1883 /* GstChildProxy implementation */
1885 gst_videomixer2_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
1888 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1891 GST_VIDEO_MIXER2_LOCK (mix);
1892 if ((obj = g_slist_nth_data (mix->sinkpads, index)))
1893 gst_object_ref (obj);
1894 GST_VIDEO_MIXER2_UNLOCK (mix);
1899 gst_videomixer2_child_proxy_get_children_count (GstChildProxy * child_proxy)
1902 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1904 GST_VIDEO_MIXER2_LOCK (mix);
1905 count = mix->numpads;
1906 GST_VIDEO_MIXER2_UNLOCK (mix);
1907 GST_INFO_OBJECT (mix, "Children Count: %d", count);
1912 gst_videomixer2_child_proxy_init (gpointer g_iface, gpointer iface_data)
1914 GstChildProxyInterface *iface = g_iface;
1916 GST_INFO ("intializing child proxy interface");
1917 iface->get_child_by_index = gst_videomixer2_child_proxy_get_child_by_index;
1918 iface->get_children_count = gst_videomixer2_child_proxy_get_children_count;
1921 /* GObject boilerplate */
1923 gst_videomixer2_base_init (gpointer g_class)
1925 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1927 gst_element_class_add_pad_template (element_class,
1928 gst_static_pad_template_get (&src_factory));
1929 gst_element_class_add_pad_template (element_class,
1930 gst_static_pad_template_get (&sink_factory));
1932 gst_element_class_set_details_simple (element_class, "Video mixer 2",
1933 "Filter/Editor/Video",
1934 "Mix multiple video streams", "Wim Taymans <wim@fluendo.com>, "
1935 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1939 gst_videomixer2_class_init (GstVideoMixer2Class * klass)
1941 GObjectClass *gobject_class = (GObjectClass *) klass;
1942 GstElementClass *gstelement_class = (GstElementClass *) klass;
1944 gobject_class->finalize = gst_videomixer2_finalize;
1946 gobject_class->get_property = gst_videomixer2_get_property;
1947 gobject_class->set_property = gst_videomixer2_set_property;
1949 g_object_class_install_property (gobject_class, PROP_BACKGROUND,
1950 g_param_spec_enum ("background", "Background", "Background type",
1951 GST_TYPE_VIDEO_MIXER2_BACKGROUND,
1952 DEFAULT_BACKGROUND, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1954 gstelement_class->request_new_pad =
1955 GST_DEBUG_FUNCPTR (gst_videomixer2_request_new_pad);
1956 gstelement_class->release_pad =
1957 GST_DEBUG_FUNCPTR (gst_videomixer2_release_pad);
1958 gstelement_class->change_state =
1959 GST_DEBUG_FUNCPTR (gst_videomixer2_change_state);
1961 /* Register the pad class */
1962 g_type_class_ref (GST_TYPE_VIDEO_MIXER2_PAD);
1966 gst_videomixer2_init (GstVideoMixer2 * mix, GstVideoMixer2Class * g_class)
1968 GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix);
1971 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
1973 gst_pad_set_getcaps_function (GST_PAD (mix->srcpad),
1974 GST_DEBUG_FUNCPTR (gst_videomixer2_src_getcaps));
1975 gst_pad_set_setcaps_function (GST_PAD (mix->srcpad),
1976 GST_DEBUG_FUNCPTR (gst_videomixer2_src_setcaps));
1977 gst_pad_set_query_function (GST_PAD (mix->srcpad),
1978 GST_DEBUG_FUNCPTR (gst_videomixer2_src_query));
1979 gst_pad_set_event_function (GST_PAD (mix->srcpad),
1980 GST_DEBUG_FUNCPTR (gst_videomixer2_src_event));
1981 gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad);
1983 mix->collect = gst_collect_pads2_new ();
1984 mix->background = DEFAULT_BACKGROUND;
1986 gst_collect_pads2_set_function (mix->collect,
1987 (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_videomixer2_collected),
1989 gst_collect_pads2_set_event_function (mix->collect,
1990 (GstCollectPads2EventFunction) gst_videomixer2_sink_event, mix);
1991 gst_collect_pads2_set_prepare_buffer_function (mix->collect,
1992 (GstCollectPads2BufferFunction) gst_videomixer2_sink_prepare_buffer, mix);
1994 mix->lock = g_mutex_new ();
1995 /* initialize variables */
1996 gst_videomixer2_reset (mix);
1999 /* Element registration */
2001 gst_videomixer2_register (GstPlugin * plugin)
2003 GST_DEBUG_CATEGORY_INIT (gst_videomixer2_debug, "videomixer2", 0,
2006 return gst_element_register (plugin, "videomixer2", GST_RANK_SECONDARY,
2007 GST_TYPE_VIDEO_MIXER2);