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)
89 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
90 * with newer GLib versions (>= 2.31.0) */
91 #define GLIB_DISABLE_DEPRECATION_WARNINGS
95 #include "videomixer2.h"
96 #include "videomixer2pad.h"
98 #include <gst/controller/gstcontroller.h>
100 #include "gst/glib-compat-private.h"
103 #define orc_memset memset
105 #include <orc/orcfunctions.h>
108 GST_DEBUG_CATEGORY_STATIC (gst_videomixer2_debug);
109 #define GST_CAT_DEFAULT gst_videomixer2_debug
111 #define GST_VIDEO_MIXER2_GET_LOCK(mix) \
112 (GST_VIDEO_MIXER2(mix)->lock)
113 #define GST_VIDEO_MIXER2_LOCK(mix) \
114 (g_mutex_lock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
115 #define GST_VIDEO_MIXER2_UNLOCK(mix) \
116 (g_mutex_unlock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
118 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
121 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";"
122 GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_ABGR ";"
123 GST_VIDEO_CAPS_YUV ("Y444") ";" GST_VIDEO_CAPS_YUV ("Y42B") ";"
124 GST_VIDEO_CAPS_YUV ("YUY2") ";" GST_VIDEO_CAPS_YUV ("UYVY") ";"
125 GST_VIDEO_CAPS_YUV ("YVYU") ";"
126 GST_VIDEO_CAPS_YUV ("I420") ";" GST_VIDEO_CAPS_YUV ("YV12") ";"
127 GST_VIDEO_CAPS_YUV ("Y41B") ";" GST_VIDEO_CAPS_RGB ";"
128 GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
129 GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx)
132 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
135 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";"
136 GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_ABGR ";"
137 GST_VIDEO_CAPS_YUV ("Y444") ";" GST_VIDEO_CAPS_YUV ("Y42B") ";"
138 GST_VIDEO_CAPS_YUV ("YUY2") ";" GST_VIDEO_CAPS_YUV ("UYVY") ";"
139 GST_VIDEO_CAPS_YUV ("YVYU") ";"
140 GST_VIDEO_CAPS_YUV ("I420") ";" GST_VIDEO_CAPS_YUV ("YV12") ";"
141 GST_VIDEO_CAPS_YUV ("Y41B") ";" GST_VIDEO_CAPS_RGB ";"
142 GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
143 GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx)
146 static void gst_videomixer2_child_proxy_init (gpointer g_iface,
147 gpointer iface_data);
148 static gboolean gst_videomixer2_push_sink_event (GstVideoMixer2 * mix,
150 static void gst_videomixer2_release_pad (GstElement * element, GstPad * pad);
151 static void gst_videomixer2_reset_qos (GstVideoMixer2 * mix);
154 _do_init (GType object_type)
156 static const GInterfaceInfo child_proxy_info = {
157 (GInterfaceInitFunc) gst_videomixer2_child_proxy_init,
162 g_type_add_interface_static (object_type, GST_TYPE_CHILD_PROXY,
166 struct _GstVideoMixer2Collect
168 GstCollectData2 collect; /* we extend the CollectData */
170 GstVideoMixer2Pad *mixpad;
172 GstBuffer *queued; /* buffer for which we don't know the end time yet */
174 GstBuffer *buffer; /* buffer that should be blended now */
175 GstClockTime start_time;
176 GstClockTime end_time;
179 #define DEFAULT_PAD_ZORDER 0
180 #define DEFAULT_PAD_XPOS 0
181 #define DEFAULT_PAD_YPOS 0
182 #define DEFAULT_PAD_ALPHA 1.0
192 G_DEFINE_TYPE (GstVideoMixer2Pad, gst_videomixer2_pad, GST_TYPE_PAD);
195 gst_videomixer2_collect_free (GstCollectData2 * data)
197 GstVideoMixer2Collect *cdata = (GstVideoMixer2Collect *) data;
199 gst_buffer_replace (&cdata->buffer, NULL);
203 gst_videomixer2_update_src_caps (GstVideoMixer2 * mix)
206 gint best_width = -1, best_height = -1;
207 gdouble best_fps = -1, cur_fps;
208 gint best_fps_n = -1, best_fps_d = -1;
211 GST_VIDEO_MIXER2_LOCK (mix);
213 for (l = mix->sinkpads; l; l = l->next) {
214 GstVideoMixer2Pad *mpad = l->data;
215 gint this_width, this_height;
217 if (mpad->fps_n == 0 || mpad->fps_d == 0 ||
218 mpad->width == 0 || mpad->height == 0)
221 this_width = mpad->width + MAX (mpad->xpos, 0);
222 this_height = mpad->height + MAX (mpad->ypos, 0);
224 if (best_width < this_width)
225 best_width = this_width;
226 if (best_height < this_height)
227 best_height = this_height;
229 if (mpad->fps_d == 0)
232 gst_util_fraction_to_double (mpad->fps_n, mpad->fps_d, &cur_fps);
233 if (best_fps < cur_fps) {
235 best_fps_n = mpad->fps_n;
236 best_fps_d = mpad->fps_d;
240 if (best_fps_n <= 0 && best_fps_d <= 0) {
246 if (best_width > 0 && best_height > 0 && best_fps > 0) {
247 GstCaps *caps, *peercaps;
250 if (mix->fps_n != best_fps_n || mix->fps_d != best_fps_d) {
251 if (mix->segment.last_stop != -1) {
252 mix->ts_offset = mix->segment.last_stop - mix->segment.start;
257 caps = gst_video_format_new_caps (mix->format,
258 best_width, best_height, best_fps_n, best_fps_d,
259 mix->par_n, mix->par_d);
261 peercaps = gst_pad_peer_get_caps (mix->srcpad);
265 s = gst_caps_get_structure (caps, 0);
266 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, "height",
267 GST_TYPE_INT_RANGE, 1, G_MAXINT, "framerate", GST_TYPE_FRACTION_RANGE,
268 0, 1, G_MAXINT, 1, NULL);
270 tmp = gst_caps_intersect (caps, peercaps);
271 gst_caps_unref (caps);
272 gst_caps_unref (peercaps);
274 if (gst_caps_is_empty (caps)) {
276 GST_VIDEO_MIXER2_UNLOCK (mix);
280 gst_caps_truncate (caps);
281 s = gst_caps_get_structure (caps, 0);
282 gst_structure_fixate_field_nearest_int (s, "width", best_width);
283 gst_structure_fixate_field_nearest_int (s, "height", best_height);
284 gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n,
287 gst_structure_get_int (s, "width", &best_width);
288 gst_structure_get_int (s, "height", &best_height);
289 gst_structure_get_fraction (s, "fraction", &best_fps_n, &best_fps_d);
292 mix->fps_n = best_fps_n;
293 mix->fps_d = best_fps_d;
294 mix->width = best_width;
295 mix->height = best_height;
297 GST_VIDEO_MIXER2_UNLOCK (mix);
298 ret = gst_pad_set_caps (mix->srcpad, caps);
299 gst_caps_unref (caps);
301 GST_VIDEO_MIXER2_UNLOCK (mix);
310 gst_videomixer2_pad_sink_setcaps (GstPad * pad, GstCaps * caps)
313 GstVideoMixer2Pad *mixpad;
316 gint fps_n = 0, fps_d = 0;
317 gint par_n = 1, par_d = 1;
318 gboolean ret = FALSE;
321 GST_INFO_OBJECT (pad, "Setting caps %" GST_PTR_FORMAT, caps);
323 mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
324 mixpad = GST_VIDEO_MIXER2_PAD (pad);
326 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
327 !gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d)) {
328 GST_ERROR_OBJECT (pad, "Failed to parse caps");
332 s = gst_caps_get_structure (caps, 0);
333 if (gst_structure_has_field (s, "framerate")
334 && !gst_video_parse_caps_framerate (caps, &fps_n, &fps_d)) {
335 GST_ERROR_OBJECT (pad, "Failed to parse caps");
339 GST_VIDEO_MIXER2_LOCK (mix);
340 if (mix->format != GST_VIDEO_FORMAT_UNKNOWN) {
341 if (mix->format != fmt || mix->par_n != par_n || mix->par_d != par_d) {
342 GST_ERROR_OBJECT (pad, "Caps not compatible with other pads' caps");
343 GST_VIDEO_MIXER2_UNLOCK (mix);
351 mixpad->fps_n = fps_n;
352 mixpad->fps_d = fps_d;
353 mixpad->width = width;
354 mixpad->height = height;
356 GST_VIDEO_MIXER2_UNLOCK (mix);
358 ret = gst_videomixer2_update_src_caps (mix);
361 gst_object_unref (mix);
367 gst_videomixer2_pad_sink_getcaps (GstPad * pad)
374 mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
376 srccaps = gst_pad_get_fixed_caps_func (GST_PAD (mix->srcpad));
377 srccaps = gst_caps_make_writable (srccaps);
379 n = gst_caps_get_size (srccaps);
380 for (i = 0; i < n; i++) {
381 s = gst_caps_get_structure (srccaps, i);
382 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
383 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
384 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
385 if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
386 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
390 GST_DEBUG_OBJECT (pad, "Returning %" GST_PTR_FORMAT, srccaps);
396 gst_videomixer2_pad_sink_acceptcaps (GstPad * pad, GstCaps * caps)
400 GstCaps *accepted_caps;
404 mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
405 GST_DEBUG_OBJECT (pad, "%" GST_PTR_FORMAT, caps);
407 accepted_caps = gst_pad_get_fixed_caps_func (GST_PAD (mix->srcpad));
408 accepted_caps = gst_caps_make_writable (accepted_caps);
409 GST_LOG_OBJECT (pad, "src caps %" GST_PTR_FORMAT, accepted_caps);
411 n = gst_caps_get_size (accepted_caps);
412 for (i = 0; i < n; i++) {
413 s = gst_caps_get_structure (accepted_caps, i);
414 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
415 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
416 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
417 if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
418 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
422 ret = gst_caps_can_intersect (caps, accepted_caps);
423 GST_INFO_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT, (ret ? "" : "not "),
425 GST_INFO_OBJECT (pad, "acceptable caps are %" GST_PTR_FORMAT, accepted_caps);
427 gst_caps_unref (accepted_caps);
429 gst_object_unref (mix);
434 gst_videomixer2_pad_get_property (GObject * object, guint prop_id,
435 GValue * value, GParamSpec * pspec)
437 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
440 case PROP_PAD_ZORDER:
441 g_value_set_uint (value, pad->zorder);
444 g_value_set_int (value, pad->xpos);
447 g_value_set_int (value, pad->ypos);
450 g_value_set_double (value, pad->alpha);
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459 pad_zorder_compare (const GstVideoMixer2Pad * pad1,
460 const GstVideoMixer2Pad * pad2)
462 return pad1->zorder - pad2->zorder;
466 gst_videomixer2_pad_set_property (GObject * object, guint prop_id,
467 const GValue * value, GParamSpec * pspec)
469 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
470 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (GST_PAD (pad)));
473 case PROP_PAD_ZORDER:
474 GST_VIDEO_MIXER2_LOCK (mix);
475 pad->zorder = g_value_get_uint (value);
477 mix->sinkpads = g_slist_sort (mix->sinkpads,
478 (GCompareFunc) pad_zorder_compare);
479 GST_VIDEO_MIXER2_UNLOCK (mix);
482 pad->xpos = g_value_get_int (value);
485 pad->ypos = g_value_get_int (value);
488 pad->alpha = g_value_get_double (value);
491 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gst_object_unref (mix);
499 gst_videomixer2_pad_class_init (GstVideoMixer2PadClass * klass)
501 GObjectClass *gobject_class = (GObjectClass *) klass;
503 gobject_class->set_property = gst_videomixer2_pad_set_property;
504 gobject_class->get_property = gst_videomixer2_pad_get_property;
506 g_object_class_install_property (gobject_class, PROP_PAD_ZORDER,
507 g_param_spec_uint ("zorder", "Z-Order", "Z Order of the picture",
508 0, 10000, DEFAULT_PAD_ZORDER,
509 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
510 g_object_class_install_property (gobject_class, PROP_PAD_XPOS,
511 g_param_spec_int ("xpos", "X Position", "X Position of the picture",
512 G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
513 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
514 g_object_class_install_property (gobject_class, PROP_PAD_YPOS,
515 g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
516 G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
517 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
518 g_object_class_install_property (gobject_class, PROP_PAD_ALPHA,
519 g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
521 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
525 gst_videomixer2_pad_init (GstVideoMixer2Pad * mixerpad)
527 /* setup some pad functions */
528 gst_pad_set_setcaps_function (GST_PAD (mixerpad),
529 gst_videomixer2_pad_sink_setcaps);
530 gst_pad_set_acceptcaps_function (GST_PAD (mixerpad),
531 GST_DEBUG_FUNCPTR (gst_videomixer2_pad_sink_acceptcaps));
532 gst_pad_set_getcaps_function (GST_PAD (mixerpad),
533 gst_videomixer2_pad_sink_getcaps);
535 mixerpad->zorder = DEFAULT_PAD_ZORDER;
536 mixerpad->xpos = DEFAULT_PAD_XPOS;
537 mixerpad->ypos = DEFAULT_PAD_YPOS;
538 mixerpad->alpha = DEFAULT_PAD_ALPHA;
542 #define DEFAULT_BACKGROUND VIDEO_MIXER2_BACKGROUND_CHECKER
549 #define GST_TYPE_VIDEO_MIXER2_BACKGROUND (gst_videomixer2_background_get_type())
551 gst_videomixer2_background_get_type (void)
553 static GType video_mixer_background_type = 0;
555 static const GEnumValue video_mixer_background[] = {
556 {VIDEO_MIXER2_BACKGROUND_CHECKER, "Checker pattern", "checker"},
557 {VIDEO_MIXER2_BACKGROUND_BLACK, "Black", "black"},
558 {VIDEO_MIXER2_BACKGROUND_WHITE, "White", "white"},
559 {VIDEO_MIXER2_BACKGROUND_TRANSPARENT,
560 "Transparent Background to enable further mixing", "transparent"},
564 if (!video_mixer_background_type) {
565 video_mixer_background_type =
566 g_enum_register_static ("GstVideoMixer2Background",
567 video_mixer_background);
569 return video_mixer_background_type;
573 GST_BOILERPLATE_FULL (GstVideoMixer2, gst_videomixer2, GstElement,
574 GST_TYPE_ELEMENT, _do_init);
577 gst_videomixer2_update_qos (GstVideoMixer2 * mix, gdouble proportion,
578 GstClockTimeDiff diff, GstClockTime timestamp)
580 GST_DEBUG_OBJECT (mix,
581 "Updating QoS: proportion %lf, diff %s%" GST_TIME_FORMAT ", timestamp %"
582 GST_TIME_FORMAT, proportion, (diff < 0) ? "-" : "",
583 GST_TIME_ARGS (ABS (diff)), GST_TIME_ARGS (timestamp));
585 GST_OBJECT_LOCK (mix);
586 mix->proportion = proportion;
587 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
588 if (G_UNLIKELY (diff > 0))
590 timestamp + 2 * diff + gst_util_uint64_scale_int (GST_SECOND,
591 mix->fps_d, mix->fps_n);
593 mix->earliest_time = timestamp + diff;
595 mix->earliest_time = GST_CLOCK_TIME_NONE;
597 GST_OBJECT_UNLOCK (mix);
601 gst_videomixer2_reset_qos (GstVideoMixer2 * mix)
603 gst_videomixer2_update_qos (mix, 0.5, 0, GST_CLOCK_TIME_NONE);
604 mix->qos_processed = mix->qos_dropped = 0;
608 gst_videomixer2_read_qos (GstVideoMixer2 * mix, gdouble * proportion,
611 GST_OBJECT_LOCK (mix);
612 *proportion = mix->proportion;
613 *time = mix->earliest_time;
614 GST_OBJECT_UNLOCK (mix);
618 gst_videomixer2_reset (GstVideoMixer2 * mix)
622 mix->format = GST_VIDEO_FORMAT_UNKNOWN;
623 mix->width = mix->height = 0;
624 mix->fps_n = mix->fps_d = 0;
625 mix->par_n = mix->par_d = 0;
629 gst_segment_init (&mix->segment, GST_FORMAT_TIME);
630 mix->segment.last_stop = -1;
632 gst_videomixer2_reset_qos (mix);
634 for (l = mix->sinkpads; l; l = l->next) {
635 GstVideoMixer2Pad *p = l->data;
636 GstVideoMixer2Collect *mixcol = p->mixcol;
638 gst_buffer_replace (&mixcol->buffer, NULL);
639 mixcol->start_time = -1;
640 mixcol->end_time = -1;
642 p->fps_n = p->fps_d = 0;
643 p->width = p->height = 0;
646 mix->newseg_pending = TRUE;
647 mix->flush_stop_pending = FALSE;
651 * 0 == need more data
656 gst_videomixer2_fill_queues (GstVideoMixer2 * mix,
657 GstClockTime output_start_time, GstClockTime output_end_time)
661 gboolean need_more_data = FALSE;
663 for (l = mix->sinkpads; l; l = l->next) {
664 GstVideoMixer2Pad *pad = l->data;
665 GstVideoMixer2Collect *mixcol = pad->mixcol;
666 GstSegment *segment = &pad->mixcol->collect.segment;
669 buf = gst_collect_pads2_peek (mix->collect, &mixcol->collect);
671 GstClockTime start_time, end_time;
673 start_time = GST_BUFFER_TIMESTAMP (buf);
674 if (start_time == -1) {
675 gst_buffer_unref (buf);
676 GST_ERROR_OBJECT (pad, "Need timestamped buffers!");
680 /* FIXME: Make all this work with negative rates */
682 if ((mixcol->buffer && start_time < GST_BUFFER_TIMESTAMP (mixcol->buffer))
684 && start_time < GST_BUFFER_TIMESTAMP (mixcol->queued))) {
685 GST_WARNING_OBJECT (pad, "Buffer from the past, dropping");
686 gst_buffer_unref (buf);
687 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
688 gst_buffer_unref (buf);
689 need_more_data = TRUE;
693 if (mixcol->queued) {
694 end_time = start_time - GST_BUFFER_TIMESTAMP (mixcol->queued);
695 start_time = GST_BUFFER_TIMESTAMP (mixcol->queued);
696 gst_buffer_unref (buf);
697 buf = gst_buffer_ref (mixcol->queued);
699 end_time = GST_BUFFER_DURATION (buf);
701 if (end_time == -1) {
702 mixcol->queued = buf;
703 need_more_data = TRUE;
708 g_assert (start_time != -1 && end_time != -1);
709 end_time += start_time; /* convert from duration to position */
711 if (mixcol->end_time != -1 && mixcol->end_time > end_time) {
712 GST_WARNING_OBJECT (pad, "Buffer from the past, dropping");
713 if (buf == mixcol->queued) {
714 gst_buffer_unref (buf);
715 gst_buffer_replace (&mixcol->queued, NULL);
717 gst_buffer_unref (buf);
718 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
719 gst_buffer_unref (buf);
722 need_more_data = TRUE;
726 /* Check if it's inside the segment */
727 if (start_time >= segment->stop || end_time < segment->start) {
728 GST_DEBUG_OBJECT (pad, "Buffer outside the segment");
730 if (buf == mixcol->queued) {
731 gst_buffer_unref (buf);
732 gst_buffer_replace (&mixcol->queued, NULL);
734 gst_buffer_unref (buf);
735 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
736 gst_buffer_unref (buf);
739 need_more_data = TRUE;
743 /* Clip to segment and convert to running time */
744 start_time = MAX (start_time, segment->start);
745 if (segment->stop != -1)
746 end_time = MIN (end_time, segment->stop);
748 gst_segment_to_running_time (segment, GST_FORMAT_TIME, start_time);
750 gst_segment_to_running_time (segment, GST_FORMAT_TIME, end_time);
751 g_assert (start_time != -1 && end_time != -1);
753 /* Convert to the output segment rate */
754 if (mix->segment.abs_rate != 1.0) {
755 start_time *= mix->segment.abs_rate;
756 end_time *= mix->segment.abs_rate;
759 if (end_time >= output_start_time && start_time < output_end_time) {
760 GST_DEBUG_OBJECT (pad,
761 "Taking new buffer with start time %" GST_TIME_FORMAT,
762 GST_TIME_ARGS (start_time));
763 gst_buffer_replace (&mixcol->buffer, buf);
764 mixcol->start_time = start_time;
765 mixcol->end_time = end_time;
767 if (buf == mixcol->queued) {
768 gst_buffer_unref (buf);
769 gst_buffer_replace (&mixcol->queued, NULL);
771 gst_buffer_unref (buf);
772 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
773 gst_buffer_unref (buf);
776 } else if (start_time >= output_end_time) {
777 GST_DEBUG_OBJECT (pad, "Keeping buffer until %" GST_TIME_FORMAT,
778 GST_TIME_ARGS (start_time));
779 gst_buffer_unref (buf);
782 GST_DEBUG_OBJECT (pad, "Too old buffer -- dropping");
783 if (buf == mixcol->queued) {
784 gst_buffer_unref (buf);
785 gst_buffer_replace (&mixcol->queued, NULL);
787 gst_buffer_unref (buf);
788 buf = gst_collect_pads2_pop (mix->collect, &mixcol->collect);
789 gst_buffer_unref (buf);
792 need_more_data = TRUE;
796 if (mixcol->end_time != -1) {
797 if (mixcol->end_time < output_start_time) {
798 gst_buffer_replace (&mixcol->buffer, NULL);
799 mixcol->start_time = mixcol->end_time = -1;
800 if (!GST_COLLECT_PADS2_STATE_IS_SET (mixcol,
801 GST_COLLECT_PADS2_STATE_EOS))
802 need_more_data = TRUE;
819 gst_videomixer2_blend_buffers (GstVideoMixer2 * mix,
820 GstClockTime output_start_time, GstClockTime output_end_time,
826 BlendFunction composite;
828 outsize = gst_video_format_get_size (mix->format, mix->width, mix->height);
829 ret = gst_pad_alloc_buffer_and_set_caps (mix->srcpad, GST_BUFFER_OFFSET_NONE,
830 outsize, GST_PAD_CAPS (mix->srcpad), outbuf);
831 if (ret != GST_FLOW_OK)
834 GST_BUFFER_TIMESTAMP (*outbuf) = output_start_time;
835 GST_BUFFER_DURATION (*outbuf) = output_end_time - output_start_time;
837 /* default to blending */
838 composite = mix->blend;
839 switch (mix->background) {
840 case VIDEO_MIXER2_BACKGROUND_CHECKER:
841 mix->fill_checker (GST_BUFFER_DATA (*outbuf), mix->width, mix->height);
843 case VIDEO_MIXER2_BACKGROUND_BLACK:
844 mix->fill_color (GST_BUFFER_DATA (*outbuf), mix->width,
845 mix->height, 16, 128, 128);
847 case VIDEO_MIXER2_BACKGROUND_WHITE:
848 mix->fill_color (GST_BUFFER_DATA (*outbuf), mix->width,
849 mix->height, 240, 128, 128);
851 case VIDEO_MIXER2_BACKGROUND_TRANSPARENT:
852 orc_memset (GST_BUFFER_DATA (*outbuf), 0,
853 gst_video_format_get_row_stride (mix->format, 0,
854 mix->width) * mix->height);
855 /* use overlay to keep background transparent */
856 composite = mix->overlay;
860 for (l = mix->sinkpads; l; l = l->next) {
861 GstVideoMixer2Pad *pad = l->data;
862 GstVideoMixer2Collect *mixcol = pad->mixcol;
864 if (mixcol->buffer != NULL) {
865 GstClockTime timestamp;
869 seg = &mixcol->collect.segment;
871 timestamp = GST_BUFFER_TIMESTAMP (mixcol->buffer);
874 gst_segment_to_stream_time (seg, GST_FORMAT_TIME, timestamp);
876 /* sync object properties on stream time */
877 if (GST_CLOCK_TIME_IS_VALID (stream_time))
878 gst_object_sync_values (G_OBJECT (pad), stream_time);
880 composite (GST_BUFFER_DATA (mixcol->buffer),
881 pad->xpos, pad->ypos, pad->width, pad->height, pad->alpha,
882 GST_BUFFER_DATA (*outbuf), mix->width, mix->height);
889 /* Perform qos calculations before processing the next frame. Returns TRUE if
890 * the frame should be processed, FALSE if the frame can be dropped entirely */
892 gst_videomixer2_do_qos (GstVideoMixer2 * mix, GstClockTime timestamp)
894 GstClockTime qostime, earliest_time;
898 /* no timestamp, can't do QoS => process frame */
899 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
900 GST_LOG_OBJECT (mix, "invalid timestamp, can't do QoS, process frame");
904 /* get latest QoS observation values */
905 gst_videomixer2_read_qos (mix, &proportion, &earliest_time);
907 /* skip qos if we have no observation (yet) => process frame */
908 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
909 GST_LOG_OBJECT (mix, "no observation yet, process frame");
913 /* qos is done on running time */
915 gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME, timestamp);
917 /* see how our next timestamp relates to the latest qos timestamp */
918 GST_LOG_OBJECT (mix, "qostime %" GST_TIME_FORMAT ", earliest %"
919 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
921 jitter = GST_CLOCK_DIFF (qostime, earliest_time);
922 if (qostime != GST_CLOCK_TIME_NONE && jitter > 0) {
923 GST_DEBUG_OBJECT (mix, "we are late, drop frame");
927 GST_LOG_OBJECT (mix, "process frame");
932 gst_videomixer2_collected (GstCollectPads2 * pads, GstVideoMixer2 * mix)
935 GstClockTime output_start_time, output_end_time;
936 GstBuffer *outbuf = NULL;
940 /* If we're not negotiated yet... */
941 if (mix->format == GST_VIDEO_FORMAT_UNKNOWN)
942 return GST_FLOW_NOT_NEGOTIATED;
944 if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE, FALSE)) {
945 GST_DEBUG_OBJECT (mix, "pending flush stop");
946 gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop ());
949 GST_VIDEO_MIXER2_LOCK (mix);
951 if (mix->newseg_pending) {
952 GST_DEBUG_OBJECT (mix, "Sending NEWSEGMENT event");
953 if (!gst_pad_push_event (mix->srcpad, gst_event_new_new_segment_full (FALSE,
954 mix->segment.rate, mix->segment.applied_rate,
955 mix->segment.format, mix->segment.start, mix->segment.stop,
956 mix->segment.time))) {
957 ret = GST_FLOW_ERROR;
960 mix->newseg_pending = FALSE;
963 if (mix->segment.last_stop == -1)
964 output_start_time = mix->segment.start;
966 output_start_time = mix->segment.last_stop;
968 if (output_start_time >= mix->segment.stop) {
969 GST_DEBUG_OBJECT (mix, "Segment done");
970 gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
971 ret = GST_FLOW_UNEXPECTED;
976 mix->ts_offset + gst_util_uint64_scale (mix->nframes + 1,
977 GST_SECOND * mix->fps_d, mix->fps_n);
978 if (mix->segment.stop != -1)
979 output_end_time = MIN (output_end_time, mix->segment.stop);
981 res = gst_videomixer2_fill_queues (mix, output_start_time, output_end_time);
984 GST_DEBUG_OBJECT (mix, "Need more data for decisions");
987 } else if (res == -1) {
988 GST_DEBUG_OBJECT (mix, "All sinkpads are EOS -- forwarding");
989 gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
990 ret = GST_FLOW_UNEXPECTED;
992 } else if (res == -2) {
993 GST_ERROR_OBJECT (mix, "Error collecting buffers");
994 ret = GST_FLOW_ERROR;
998 jitter = gst_videomixer2_do_qos (mix, output_start_time);
1001 gst_videomixer2_blend_buffers (mix, output_start_time,
1002 output_end_time, &outbuf);
1003 mix->qos_processed++;
1011 gst_message_new_qos (GST_OBJECT_CAST (mix), FALSE,
1012 gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME,
1013 output_start_time), gst_segment_to_stream_time (&mix->segment,
1014 GST_FORMAT_TIME, output_start_time), output_start_time,
1015 output_end_time - output_start_time);
1016 gst_message_set_qos_values (msg, jitter, mix->proportion, 1000000);
1017 gst_message_set_qos_stats (msg, GST_FORMAT_BUFFERS, mix->qos_processed,
1019 gst_element_post_message (GST_ELEMENT_CAST (mix), msg);
1024 gst_segment_set_last_stop (&mix->segment, GST_FORMAT_TIME, output_end_time);
1027 GST_VIDEO_MIXER2_UNLOCK (mix);
1029 GST_LOG_OBJECT (mix,
1030 "Pushing buffer with ts %" GST_TIME_FORMAT " and duration %"
1031 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1032 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
1033 ret = gst_pad_push (mix->srcpad, outbuf);
1035 GST_VIDEO_MIXER2_LOCK (mix);
1038 GST_VIDEO_MIXER2_UNLOCK (mix);
1044 gst_videomixer2_src_getcaps (GstPad * pad)
1046 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
1051 if (mix->format != GST_VIDEO_FORMAT_UNKNOWN) {
1052 caps = gst_caps_copy (GST_PAD_CAPS (mix->srcpad));
1054 caps = gst_caps_copy (gst_pad_get_pad_template_caps (mix->srcpad));
1057 n = gst_caps_get_size (caps) - 1;
1058 for (; n >= 0; n--) {
1059 s = gst_caps_get_structure (caps, n);
1060 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1061 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
1062 if (mix->fps_d != 0) {
1063 gst_structure_set (s,
1064 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1068 gst_object_unref (mix);
1074 gst_videomixer2_query_duration (GstVideoMixer2 * mix, GstQuery * query)
1083 gst_query_parse_duration (query, &format, NULL);
1089 /* Take maximum of all durations */
1090 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1092 GstIteratorResult ires;
1095 ires = gst_iterator_next (it, &item);
1097 case GST_ITERATOR_DONE:
1100 case GST_ITERATOR_OK:
1102 GstPad *pad = GST_PAD_CAST (item);
1105 /* ask sink peer for duration */
1106 res &= gst_pad_query_peer_duration (pad, &format, &duration);
1107 /* take max from all valid return values */
1109 /* valid unknown length, stop searching */
1110 if (duration == -1) {
1114 /* else see if bigger than current max */
1115 else if (duration > max)
1118 gst_object_unref (pad);
1121 case GST_ITERATOR_RESYNC:
1124 gst_iterator_resync (it);
1132 gst_iterator_free (it);
1135 /* and store the max */
1136 GST_DEBUG_OBJECT (mix, "Total duration in format %s: %"
1137 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1138 gst_query_set_duration (query, format, max);
1145 gst_videomixer2_query_latency (GstVideoMixer2 * mix, GstQuery * query)
1147 GstClockTime min, max;
1157 max = GST_CLOCK_TIME_NONE;
1159 /* Take maximum of all latency values */
1160 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1162 GstIteratorResult ires;
1165 ires = gst_iterator_next (it, &item);
1167 case GST_ITERATOR_DONE:
1170 case GST_ITERATOR_OK:
1172 GstPad *pad = GST_PAD_CAST (item);
1173 GstQuery *peerquery;
1174 GstClockTime min_cur, max_cur;
1177 peerquery = gst_query_new_latency ();
1179 /* Ask peer for latency */
1180 res &= gst_pad_peer_query (pad, peerquery);
1182 /* take max from all valid return values */
1184 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
1189 if (max_cur != GST_CLOCK_TIME_NONE &&
1190 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
1191 (max == GST_CLOCK_TIME_NONE)))
1194 live = live || live_cur;
1197 gst_query_unref (peerquery);
1198 gst_object_unref (pad);
1201 case GST_ITERATOR_RESYNC:
1204 max = GST_CLOCK_TIME_NONE;
1206 gst_iterator_resync (it);
1214 gst_iterator_free (it);
1217 /* store the results */
1218 GST_DEBUG_OBJECT (mix, "Calculated total latency: live %s, min %"
1219 GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
1220 (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1221 gst_query_set_latency (query, live, min, max);
1228 gst_videomixer2_src_query (GstPad * pad, GstQuery * query)
1230 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
1231 gboolean res = FALSE;
1233 switch (GST_QUERY_TYPE (query)) {
1234 case GST_QUERY_POSITION:
1238 gst_query_parse_position (query, &format, NULL);
1241 case GST_FORMAT_TIME:
1242 gst_query_set_position (query, format,
1243 gst_segment_to_stream_time (&mix->segment, GST_FORMAT_TIME,
1244 mix->segment.last_stop));
1252 case GST_QUERY_DURATION:
1253 res = gst_videomixer2_query_duration (mix, query);
1255 case GST_QUERY_LATENCY:
1256 res = gst_videomixer2_query_latency (mix, query);
1259 /* FIXME, needs a custom query handler because we have multiple
1262 gst_query_unref (query);
1266 gst_object_unref (mix);
1271 gst_videomixer2_src_event (GstPad * pad, GstEvent * event)
1273 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (pad));
1276 switch (GST_EVENT_TYPE (event)) {
1277 case GST_EVENT_QOS:{
1278 GstClockTimeDiff diff;
1279 GstClockTime timestamp;
1282 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1284 gst_videomixer2_update_qos (mix, proportion, diff, timestamp);
1286 result = gst_videomixer2_push_sink_event (mix, event);
1289 case GST_EVENT_SEEK:
1294 GstSeekType start_type, stop_type;
1299 /* parse the seek parameters */
1300 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1301 &start, &stop_type, &stop);
1304 GST_ERROR_OBJECT (mix, "Negative rates not supported yet");
1306 gst_event_unref (event);
1310 GST_DEBUG_OBJECT (mix, "Handling SEEK event");
1312 /* check if we are flushing */
1313 if (flags & GST_SEEK_FLAG_FLUSH) {
1314 /* flushing seek, start flush downstream, the flush will be done
1315 * when all pads received a FLUSH_STOP. */
1316 gst_pad_push_event (mix->srcpad, gst_event_new_flush_start ());
1318 /* make sure we accept nothing anymore and return WRONG_STATE */
1319 gst_collect_pads2_set_flushing (mix->collect, TRUE);
1322 /* now wait for the collected to be finished and mark a new
1324 GST_COLLECT_PADS2_STREAM_LOCK (mix->collect);
1326 abs_rate = ABS (rate);
1328 GST_VIDEO_MIXER2_LOCK (mix);
1329 for (l = mix->sinkpads; l; l = l->next) {
1330 GstVideoMixer2Pad *p = l->data;
1332 if (flags & GST_SEEK_FLAG_FLUSH) {
1333 gst_buffer_replace (&p->mixcol->buffer, NULL);
1334 p->mixcol->start_time = p->mixcol->end_time = -1;
1338 /* Convert to the output segment rate */
1339 if (mix->segment.abs_rate != abs_rate) {
1340 if (mix->segment.abs_rate != 1.0 && p->mixcol->buffer) {
1341 p->mixcol->start_time /= mix->segment.abs_rate;
1342 p->mixcol->end_time /= mix->segment.abs_rate;
1344 if (abs_rate != 1.0 && p->mixcol->buffer) {
1345 p->mixcol->start_time *= abs_rate;
1346 p->mixcol->end_time *= abs_rate;
1350 GST_VIDEO_MIXER2_UNLOCK (mix);
1352 gst_segment_set_seek (&mix->segment, rate, fmt, flags, start_type, start,
1353 stop_type, stop, NULL);
1354 mix->segment.last_stop = -1;
1357 mix->newseg_pending = TRUE;
1359 if (flags & GST_SEEK_FLAG_FLUSH) {
1360 gst_collect_pads2_set_flushing (mix->collect, FALSE);
1362 /* we can't send FLUSH_STOP here since upstream could start pushing data
1363 * after we unlock mix->collect.
1364 * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
1365 * forwarding the seek upstream or from gst_videomixer_collected,
1366 * whichever happens first.
1368 mix->flush_stop_pending = TRUE;
1371 GST_COLLECT_PADS2_STREAM_UNLOCK (mix->collect);
1373 gst_videomixer2_reset_qos (mix);
1375 result = gst_videomixer2_push_sink_event (mix, event);
1377 if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE,
1379 GST_DEBUG_OBJECT (mix, "pending flush stop");
1380 gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop ());
1385 case GST_EVENT_NAVIGATION:
1386 /* navigation is rather pointless. */
1388 gst_event_unref (event);
1391 /* just forward the rest for now */
1392 result = gst_videomixer2_push_sink_event (mix, event);
1395 gst_object_unref (mix);
1401 gst_videomixer2_src_setcaps (GstPad * pad, GstCaps * caps)
1403 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent_element (pad));
1404 gboolean ret = FALSE;
1410 GST_INFO_OBJECT (pad, "set src caps: %" GST_PTR_FORMAT, caps);
1413 mix->overlay = NULL;
1414 mix->fill_checker = NULL;
1415 mix->fill_color = NULL;
1417 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
1418 !gst_video_parse_caps_framerate (caps, &fps_n, &fps_d) ||
1419 !gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d))
1422 GST_VIDEO_MIXER2_LOCK (mix);
1424 if (mix->fps_n != fps_n || mix->fps_d != fps_d) {
1425 if (mix->segment.last_stop != -1) {
1426 mix->ts_offset = mix->segment.last_stop - mix->segment.start;
1429 gst_videomixer2_reset_qos (mix);
1434 mix->height = height;
1440 switch (mix->format) {
1441 case GST_VIDEO_FORMAT_AYUV:
1442 mix->blend = gst_video_mixer_blend_ayuv;
1443 mix->overlay = gst_video_mixer_overlay_ayuv;
1444 mix->fill_checker = gst_video_mixer_fill_checker_ayuv;
1445 mix->fill_color = gst_video_mixer_fill_color_ayuv;
1448 case GST_VIDEO_FORMAT_ARGB:
1449 mix->blend = gst_video_mixer_blend_argb;
1450 mix->overlay = gst_video_mixer_overlay_argb;
1451 mix->fill_checker = gst_video_mixer_fill_checker_argb;
1452 mix->fill_color = gst_video_mixer_fill_color_argb;
1455 case GST_VIDEO_FORMAT_BGRA:
1456 mix->blend = gst_video_mixer_blend_bgra;
1457 mix->overlay = gst_video_mixer_overlay_bgra;
1458 mix->fill_checker = gst_video_mixer_fill_checker_bgra;
1459 mix->fill_color = gst_video_mixer_fill_color_bgra;
1462 case GST_VIDEO_FORMAT_ABGR:
1463 mix->blend = gst_video_mixer_blend_abgr;
1464 mix->overlay = gst_video_mixer_overlay_abgr;
1465 mix->fill_checker = gst_video_mixer_fill_checker_abgr;
1466 mix->fill_color = gst_video_mixer_fill_color_abgr;
1469 case GST_VIDEO_FORMAT_RGBA:
1470 mix->blend = gst_video_mixer_blend_rgba;
1471 mix->overlay = gst_video_mixer_overlay_rgba;
1472 mix->fill_checker = gst_video_mixer_fill_checker_rgba;
1473 mix->fill_color = gst_video_mixer_fill_color_rgba;
1476 case GST_VIDEO_FORMAT_Y444:
1477 mix->blend = gst_video_mixer_blend_y444;
1478 mix->overlay = mix->blend;
1479 mix->fill_checker = gst_video_mixer_fill_checker_y444;
1480 mix->fill_color = gst_video_mixer_fill_color_y444;
1483 case GST_VIDEO_FORMAT_Y42B:
1484 mix->blend = gst_video_mixer_blend_y42b;
1485 mix->overlay = mix->blend;
1486 mix->fill_checker = gst_video_mixer_fill_checker_y42b;
1487 mix->fill_color = gst_video_mixer_fill_color_y42b;
1490 case GST_VIDEO_FORMAT_YUY2:
1491 mix->blend = gst_video_mixer_blend_yuy2;
1492 mix->overlay = mix->blend;
1493 mix->fill_checker = gst_video_mixer_fill_checker_yuy2;
1494 mix->fill_color = gst_video_mixer_fill_color_yuy2;
1497 case GST_VIDEO_FORMAT_UYVY:
1498 mix->blend = gst_video_mixer_blend_uyvy;
1499 mix->overlay = mix->blend;
1500 mix->fill_checker = gst_video_mixer_fill_checker_uyvy;
1501 mix->fill_color = gst_video_mixer_fill_color_uyvy;
1504 case GST_VIDEO_FORMAT_YVYU:
1505 mix->blend = gst_video_mixer_blend_yvyu;
1506 mix->overlay = mix->blend;
1507 mix->fill_checker = gst_video_mixer_fill_checker_yvyu;
1508 mix->fill_color = gst_video_mixer_fill_color_yvyu;
1511 case GST_VIDEO_FORMAT_I420:
1512 mix->blend = gst_video_mixer_blend_i420;
1513 mix->overlay = mix->blend;
1514 mix->fill_checker = gst_video_mixer_fill_checker_i420;
1515 mix->fill_color = gst_video_mixer_fill_color_i420;
1518 case GST_VIDEO_FORMAT_YV12:
1519 mix->blend = gst_video_mixer_blend_yv12;
1520 mix->overlay = mix->blend;
1521 mix->fill_checker = gst_video_mixer_fill_checker_yv12;
1522 mix->fill_color = gst_video_mixer_fill_color_yv12;
1525 case GST_VIDEO_FORMAT_Y41B:
1526 mix->blend = gst_video_mixer_blend_y41b;
1527 mix->overlay = mix->blend;
1528 mix->fill_checker = gst_video_mixer_fill_checker_y41b;
1529 mix->fill_color = gst_video_mixer_fill_color_y41b;
1532 case GST_VIDEO_FORMAT_RGB:
1533 mix->blend = gst_video_mixer_blend_rgb;
1534 mix->overlay = mix->blend;
1535 mix->fill_checker = gst_video_mixer_fill_checker_rgb;
1536 mix->fill_color = gst_video_mixer_fill_color_rgb;
1539 case GST_VIDEO_FORMAT_BGR:
1540 mix->blend = gst_video_mixer_blend_bgr;
1541 mix->overlay = mix->blend;
1542 mix->fill_checker = gst_video_mixer_fill_checker_bgr;
1543 mix->fill_color = gst_video_mixer_fill_color_bgr;
1546 case GST_VIDEO_FORMAT_xRGB:
1547 mix->blend = gst_video_mixer_blend_xrgb;
1548 mix->overlay = mix->blend;
1549 mix->fill_checker = gst_video_mixer_fill_checker_xrgb;
1550 mix->fill_color = gst_video_mixer_fill_color_xrgb;
1553 case GST_VIDEO_FORMAT_xBGR:
1554 mix->blend = gst_video_mixer_blend_xbgr;
1555 mix->overlay = mix->blend;
1556 mix->fill_checker = gst_video_mixer_fill_checker_xbgr;
1557 mix->fill_color = gst_video_mixer_fill_color_xbgr;
1560 case GST_VIDEO_FORMAT_RGBx:
1561 mix->blend = gst_video_mixer_blend_rgbx;
1562 mix->overlay = mix->blend;
1563 mix->fill_checker = gst_video_mixer_fill_checker_rgbx;
1564 mix->fill_color = gst_video_mixer_fill_color_rgbx;
1567 case GST_VIDEO_FORMAT_BGRx:
1568 mix->blend = gst_video_mixer_blend_bgrx;
1569 mix->overlay = mix->blend;
1570 mix->fill_checker = gst_video_mixer_fill_checker_bgrx;
1571 mix->fill_color = gst_video_mixer_fill_color_bgrx;
1577 GST_VIDEO_MIXER2_UNLOCK (mix);
1580 gst_object_unref (mix);
1585 static GstFlowReturn
1586 gst_videomixer2_sink_clip (GstCollectPads2 * pads,
1587 GstCollectData2 * data, GstBuffer * buf, GstBuffer ** outbuf,
1588 GstVideoMixer2 * mix)
1590 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (data->pad);
1591 GstVideoMixer2Collect *mixcol = pad->mixcol;
1592 GstClockTime start_time, end_time;
1594 start_time = GST_BUFFER_TIMESTAMP (buf);
1595 if (start_time == -1) {
1596 GST_ERROR_OBJECT (pad, "Timestamped buffers required!");
1597 gst_buffer_unref (buf);
1598 return GST_FLOW_ERROR;
1601 end_time = GST_BUFFER_DURATION (buf);
1603 end_time = gst_util_uint64_scale_int (GST_SECOND, pad->fps_d, pad->fps_n);
1604 if (end_time == -1) {
1609 start_time = MAX (start_time, mixcol->collect.segment.start);
1611 gst_segment_to_running_time (&mixcol->collect.segment,
1612 GST_FORMAT_TIME, start_time);
1614 end_time += GST_BUFFER_TIMESTAMP (buf);
1615 if (mixcol->collect.segment.stop != -1)
1616 end_time = MIN (end_time, mixcol->collect.segment.stop);
1618 gst_segment_to_running_time (&mixcol->collect.segment,
1619 GST_FORMAT_TIME, end_time);
1621 /* Convert to the output segment rate */
1622 if (mix->segment.abs_rate != 1.0) {
1623 start_time *= mix->segment.abs_rate;
1624 end_time *= mix->segment.abs_rate;
1627 if (mixcol->buffer != NULL && end_time < mixcol->end_time) {
1628 gst_buffer_unref (buf);
1638 gst_videomixer2_sink_event (GstCollectPads2 * pads, GstCollectData2 * cdata,
1639 GstEvent * event, GstVideoMixer2 * mix)
1641 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (cdata->pad);
1642 gboolean ret = TRUE;
1644 GST_DEBUG_OBJECT (pad, "Got %s event on pad %s:%s",
1645 GST_EVENT_TYPE_NAME (event), GST_DEBUG_PAD_NAME (pad));
1647 /* return FALSE => event will be forwarded */
1648 switch (GST_EVENT_TYPE (event)) {
1649 case GST_EVENT_NEWSEGMENT:{
1651 gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
1653 g_assert (fmt == GST_FORMAT_TIME);
1654 /* eat NEWSEGMENT events, collectpads2 unrefs the event */
1658 case GST_EVENT_FLUSH_STOP:
1659 mix->newseg_pending = TRUE;
1660 mix->flush_stop_pending = FALSE;
1661 gst_videomixer2_reset_qos (mix);
1662 gst_buffer_replace (&pad->mixcol->buffer, NULL);
1663 pad->mixcol->start_time = -1;
1664 pad->mixcol->end_time = -1;
1666 gst_segment_init (&mix->segment, GST_FORMAT_TIME);
1667 mix->segment.last_stop = -1;
1671 gst_pad_push_event (mix->srcpad, event);
1674 gst_pad_push_event (mix->srcpad, event);
1682 forward_event_func (GstPad * pad, GValue * ret, GstEvent * event)
1684 gst_event_ref (event);
1685 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
1686 if (!gst_pad_push_event (pad, event)) {
1687 g_value_set_boolean (ret, FALSE);
1688 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
1689 event, GST_EVENT_TYPE_NAME (event));
1691 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
1692 event, GST_EVENT_TYPE_NAME (event));
1694 gst_object_unref (pad);
1699 gst_videomixer2_push_sink_event (GstVideoMixer2 * mix, GstEvent * event)
1702 GValue vret = { 0 };
1704 GST_LOG_OBJECT (mix, "Forwarding event %p (%s)", event,
1705 GST_EVENT_TYPE_NAME (event));
1707 g_value_init (&vret, G_TYPE_BOOLEAN);
1708 g_value_set_boolean (&vret, TRUE);
1709 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1710 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
1712 gst_iterator_free (it);
1713 gst_event_unref (event);
1715 return g_value_get_boolean (&vret);
1718 /* GstElement vmethods */
1719 static GstStateChangeReturn
1720 gst_videomixer2_change_state (GstElement * element, GstStateChange transition)
1722 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (element);
1723 GstStateChangeReturn ret;
1725 switch (transition) {
1726 case GST_STATE_CHANGE_READY_TO_PAUSED:
1727 GST_LOG_OBJECT (mix, "starting collectpads");
1728 gst_collect_pads2_start (mix->collect);
1730 case GST_STATE_CHANGE_PAUSED_TO_READY:
1731 GST_LOG_OBJECT (mix, "stopping collectpads");
1732 gst_collect_pads2_stop (mix->collect);
1738 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1740 switch (transition) {
1741 case GST_STATE_CHANGE_PAUSED_TO_READY:
1742 gst_videomixer2_reset (mix);
1752 gst_videomixer2_request_new_pad (GstElement * element,
1753 GstPadTemplate * templ, const gchar * req_name)
1755 GstVideoMixer2 *mix;
1756 GstVideoMixer2Pad *mixpad;
1757 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1759 mix = GST_VIDEO_MIXER2 (element);
1761 if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
1764 GstVideoMixer2Collect *mixcol = NULL;
1766 GST_VIDEO_MIXER2_LOCK (mix);
1767 if (req_name == NULL || strlen (req_name) < 6
1768 || !g_str_has_prefix (req_name, "sink_")) {
1769 /* no name given when requesting the pad, use next available int */
1770 serial = mix->next_sinkpad++;
1772 /* parse serial number from requested padname */
1773 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1774 if (serial >= mix->next_sinkpad)
1775 mix->next_sinkpad = serial + 1;
1777 /* create new pad with the name */
1778 name = g_strdup_printf ("sink_%d", serial);
1779 mixpad = g_object_new (GST_TYPE_VIDEO_MIXER2_PAD, "name", name, "direction",
1780 templ->direction, "template", templ, NULL);
1783 mixpad->zorder = mix->numpads;
1784 mixpad->xpos = DEFAULT_PAD_XPOS;
1785 mixpad->ypos = DEFAULT_PAD_YPOS;
1786 mixpad->alpha = DEFAULT_PAD_ALPHA;
1788 mixcol = (GstVideoMixer2Collect *)
1789 gst_collect_pads2_add_pad_full (mix->collect, GST_PAD (mixpad),
1790 sizeof (GstVideoMixer2Collect),
1791 (GstCollectData2DestroyNotify) gst_videomixer2_collect_free, TRUE);
1793 /* Keep track of each other */
1794 mixcol->mixpad = mixpad;
1795 mixpad->mixcol = mixcol;
1797 mixcol->start_time = -1;
1798 mixcol->end_time = -1;
1800 /* Keep an internal list of mixpads for zordering */
1801 mix->sinkpads = g_slist_append (mix->sinkpads, mixpad);
1803 GST_VIDEO_MIXER2_UNLOCK (mix);
1808 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (mixpad));
1810 /* add the pad to the element */
1811 gst_element_add_pad (element, GST_PAD (mixpad));
1812 gst_child_proxy_child_added (GST_OBJECT (mix), GST_OBJECT (mixpad));
1814 return GST_PAD (mixpad);
1818 gst_videomixer2_release_pad (GstElement * element, GstPad * pad)
1820 GstVideoMixer2 *mix = NULL;
1821 GstVideoMixer2Pad *mixpad;
1822 gboolean update_caps;
1824 mix = GST_VIDEO_MIXER2 (element);
1826 GST_VIDEO_MIXER2_LOCK (mix);
1827 if (G_UNLIKELY (g_slist_find (mix->sinkpads, pad) == NULL)) {
1828 g_warning ("Unknown pad %s", GST_PAD_NAME (pad));
1832 mixpad = GST_VIDEO_MIXER2_PAD (pad);
1834 mix->sinkpads = g_slist_remove (mix->sinkpads, pad);
1835 gst_child_proxy_child_removed (GST_OBJECT (mix), GST_OBJECT (mixpad));
1838 update_caps = mix->format != GST_VIDEO_FORMAT_UNKNOWN;
1839 GST_VIDEO_MIXER2_UNLOCK (mix);
1841 gst_collect_pads2_remove_pad (mix->collect, pad);
1844 gst_videomixer2_update_src_caps (mix);
1846 gst_element_remove_pad (element, pad);
1849 GST_VIDEO_MIXER2_UNLOCK (mix);
1852 /* GObject vmethods */
1854 gst_videomixer2_finalize (GObject * o)
1856 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (o);
1858 gst_object_unref (mix->collect);
1859 g_mutex_free (mix->lock);
1861 G_OBJECT_CLASS (parent_class)->finalize (o);
1865 gst_videomixer2_get_property (GObject * object,
1866 guint prop_id, GValue * value, GParamSpec * pspec)
1868 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1871 case PROP_BACKGROUND:
1872 g_value_set_enum (value, mix->background);
1875 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1881 gst_videomixer2_set_property (GObject * object,
1882 guint prop_id, const GValue * value, GParamSpec * pspec)
1884 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1887 case PROP_BACKGROUND:
1888 mix->background = g_value_get_enum (value);
1891 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1896 /* GstChildProxy implementation */
1898 gst_videomixer2_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
1901 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1904 GST_VIDEO_MIXER2_LOCK (mix);
1905 if ((obj = g_slist_nth_data (mix->sinkpads, index)))
1906 gst_object_ref (obj);
1907 GST_VIDEO_MIXER2_UNLOCK (mix);
1912 gst_videomixer2_child_proxy_get_children_count (GstChildProxy * child_proxy)
1915 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1917 GST_VIDEO_MIXER2_LOCK (mix);
1918 count = mix->numpads;
1919 GST_VIDEO_MIXER2_UNLOCK (mix);
1920 GST_INFO_OBJECT (mix, "Children Count: %d", count);
1925 gst_videomixer2_child_proxy_init (gpointer g_iface, gpointer iface_data)
1927 GstChildProxyInterface *iface = g_iface;
1929 GST_INFO ("intializing child proxy interface");
1930 iface->get_child_by_index = gst_videomixer2_child_proxy_get_child_by_index;
1931 iface->get_children_count = gst_videomixer2_child_proxy_get_children_count;
1934 /* GObject boilerplate */
1936 gst_videomixer2_base_init (gpointer g_class)
1938 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1940 gst_element_class_add_static_pad_template (element_class, &src_factory);
1941 gst_element_class_add_static_pad_template (element_class, &sink_factory);
1943 gst_element_class_set_details_simple (element_class, "Video mixer 2",
1944 "Filter/Editor/Video",
1945 "Mix multiple video streams", "Wim Taymans <wim@fluendo.com>, "
1946 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1950 gst_videomixer2_class_init (GstVideoMixer2Class * klass)
1952 GObjectClass *gobject_class = (GObjectClass *) klass;
1953 GstElementClass *gstelement_class = (GstElementClass *) klass;
1955 gobject_class->finalize = gst_videomixer2_finalize;
1957 gobject_class->get_property = gst_videomixer2_get_property;
1958 gobject_class->set_property = gst_videomixer2_set_property;
1960 g_object_class_install_property (gobject_class, PROP_BACKGROUND,
1961 g_param_spec_enum ("background", "Background", "Background type",
1962 GST_TYPE_VIDEO_MIXER2_BACKGROUND,
1963 DEFAULT_BACKGROUND, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1965 gstelement_class->request_new_pad =
1966 GST_DEBUG_FUNCPTR (gst_videomixer2_request_new_pad);
1967 gstelement_class->release_pad =
1968 GST_DEBUG_FUNCPTR (gst_videomixer2_release_pad);
1969 gstelement_class->change_state =
1970 GST_DEBUG_FUNCPTR (gst_videomixer2_change_state);
1972 /* Register the pad class */
1973 g_type_class_ref (GST_TYPE_VIDEO_MIXER2_PAD);
1977 gst_videomixer2_init (GstVideoMixer2 * mix, GstVideoMixer2Class * g_class)
1979 GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix);
1982 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
1984 gst_pad_set_getcaps_function (GST_PAD (mix->srcpad),
1985 GST_DEBUG_FUNCPTR (gst_videomixer2_src_getcaps));
1986 gst_pad_set_setcaps_function (GST_PAD (mix->srcpad),
1987 GST_DEBUG_FUNCPTR (gst_videomixer2_src_setcaps));
1988 gst_pad_set_query_function (GST_PAD (mix->srcpad),
1989 GST_DEBUG_FUNCPTR (gst_videomixer2_src_query));
1990 gst_pad_set_event_function (GST_PAD (mix->srcpad),
1991 GST_DEBUG_FUNCPTR (gst_videomixer2_src_event));
1992 gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad);
1994 mix->collect = gst_collect_pads2_new ();
1995 mix->background = DEFAULT_BACKGROUND;
1997 gst_collect_pads2_set_function (mix->collect,
1998 (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_videomixer2_collected),
2000 gst_collect_pads2_set_event_function (mix->collect,
2001 (GstCollectPads2EventFunction) gst_videomixer2_sink_event, mix);
2002 gst_collect_pads2_set_clip_function (mix->collect,
2003 (GstCollectPads2ClipFunction) gst_videomixer2_sink_clip, mix);
2005 mix->lock = g_mutex_new ();
2006 /* initialize variables */
2007 gst_videomixer2_reset (mix);
2010 /* Element registration */
2012 gst_videomixer2_register (GstPlugin * plugin)
2014 GST_DEBUG_CATEGORY_INIT (gst_videomixer2_debug, "videomixer2", 0,
2017 return gst_element_register (plugin, "videomixer2", GST_RANK_SECONDARY,
2018 GST_TYPE_VIDEO_MIXER2);