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-videomixer
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
37 * <title>Sample pipelines</title>
40 * videotestsrc pattern=1 ! \
41 * video/x-raw,format=AYUV,framerate=\(fraction\)10/1,width=100,height=100 ! \
42 * videobox border-alpha=0 top=-70 bottom=-70 right=-220 ! \
43 * videomixer name=mix sink_0::alpha=0.7 sink_1::alpha=0.5 ! \
44 * videoconvert ! xvimagesink \
46 * video/x-raw,format=AYUV,framerate=\(fraction\)5/1,width=320,height=240 ! mix.
47 * ]| A pipeline to demonstrate videomixer used together with videobox.
48 * This should show a 320x240 pixels video test source with some transparency
49 * showing the background checker pattern. Another video test source with just
50 * the snow pattern of 100x100 pixels is overlayed on top of the first one on
51 * the left vertically centered with a small transparency showing the first
52 * video test source behind and the checker pattern under it. Note that the
53 * framerate of the output video is 10 frames per second.
55 * gst-launch-0.11 videotestsrc pattern=1 ! \
56 * video/x-raw, framerate=\(fraction\)10/1, width=100, height=100 ! \
57 * videomixer name=mix ! videoconvert ! ximagesink \
59 * video/x-raw, framerate=\(fraction\)5/1, width=320, height=240 ! mix.
60 * ]| A pipeline to demostrate bgra mixing. (This does not demonstrate alpha blending).
62 * gst-launch-0.11 videotestsrc pattern=1 ! \
63 * video/x-raw,format =I420, framerate=\(fraction\)10/1, width=100, height=100 ! \
64 * videomixer name=mix ! videoconvert ! ximagesink \
66 * video/x-raw,format=I420, framerate=\(fraction\)5/1, width=320, height=240 ! mix.
67 * ]| A pipeline to test I420
69 * gst-launch-0.11 videomixer name=mixer sink_1::alpha=0.5 sink_1::xpos=50 sink_1::ypos=50 ! \
70 * videoconvert ! ximagesink \
71 * videotestsrc pattern=snow timestamp-offset=3000000000 ! \
72 * "video/x-raw,format=AYUV,width=640,height=480,framerate=(fraction)30/1" ! \
73 * timeoverlay ! queue2 ! mixer. \
74 * videotestsrc pattern=smpte ! \
75 * "video/x-raw,format=AYUV,width=800,height=600,framerate=(fraction)10/1" ! \
76 * timeoverlay ! queue2 ! mixer.
77 * ]| A pipeline to demonstrate synchronized mixing (the second stream starts after 3 seconds)
87 #include "videomixer2.h"
88 #include "videomixer2pad.h"
91 #define orc_memset memset
93 #include <orc/orcfunctions.h>
96 GST_DEBUG_CATEGORY_STATIC (gst_videomixer2_debug);
97 #define GST_CAT_DEFAULT gst_videomixer2_debug
99 #define GST_VIDEO_MIXER2_GET_LOCK(mix) \
100 (&GST_VIDEO_MIXER2(mix)->lock)
101 #define GST_VIDEO_MIXER2_LOCK(mix) \
102 (g_mutex_lock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
103 #define GST_VIDEO_MIXER2_UNLOCK(mix) \
104 (g_mutex_unlock(GST_VIDEO_MIXER2_GET_LOCK (mix)))
106 #define FORMATS " { AYUV, BGRA, ARGB, RGBA, ABGR, Y444, Y42B, YUY2, UYVY, "\
107 " YVYU, I420, YV12, Y41B, RGB, BGR, xRGB, xBGR, RGBx, BGRx } "
109 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
112 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
115 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
118 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
121 static void gst_videomixer2_child_proxy_init (gpointer g_iface,
122 gpointer iface_data);
123 static gboolean gst_videomixer2_push_sink_event (GstVideoMixer2 * mix,
125 static void gst_videomixer2_release_pad (GstElement * element, GstPad * pad);
126 static void gst_videomixer2_reset_qos (GstVideoMixer2 * mix);
128 struct _GstVideoMixer2Collect
130 GstCollectData2 collect; /* we extend the CollectData */
132 GstVideoMixer2Pad *mixpad;
134 GstBuffer *queued; /* buffer for which we don't know the end time yet */
136 GstBuffer *buffer; /* buffer that should be blended now */
137 GstClockTime start_time;
138 GstClockTime end_time;
141 #define DEFAULT_PAD_ZORDER 0
142 #define DEFAULT_PAD_XPOS 0
143 #define DEFAULT_PAD_YPOS 0
144 #define DEFAULT_PAD_ALPHA 1.0
154 G_DEFINE_TYPE (GstVideoMixer2Pad, gst_videomixer2_pad, GST_TYPE_PAD);
157 gst_videomixer2_collect_free (GstCollectData2 * data)
159 GstVideoMixer2Collect *cdata = (GstVideoMixer2Collect *) data;
161 gst_buffer_replace (&cdata->buffer, NULL);
164 static gboolean gst_videomixer2_src_setcaps (GstPad * pad, GstVideoMixer2 * mix,
168 gst_videomixer2_update_src_caps (GstVideoMixer2 * mix)
171 gint best_width = -1, best_height = -1;
172 gdouble best_fps = -1, cur_fps;
173 gint best_fps_n = -1, best_fps_d = -1;
176 GST_VIDEO_MIXER2_LOCK (mix);
178 for (l = mix->sinkpads; l; l = l->next) {
179 GstVideoMixer2Pad *mpad = l->data;
180 gint this_width, this_height;
184 fps_n = GST_VIDEO_INFO_FPS_N (&mpad->info);
185 fps_d = GST_VIDEO_INFO_FPS_D (&mpad->info);
186 width = GST_VIDEO_INFO_WIDTH (&mpad->info);
187 height = GST_VIDEO_INFO_HEIGHT (&mpad->info);
189 if (fps_n == 0 || fps_d == 0 || width == 0 || height == 0)
192 this_width = width + MAX (mpad->xpos, 0);
193 this_height = height + MAX (mpad->ypos, 0);
195 if (best_width < this_width)
196 best_width = this_width;
197 if (best_height < this_height)
198 best_height = this_height;
203 gst_util_fraction_to_double (fps_n, fps_d, &cur_fps);
205 if (best_fps < cur_fps) {
212 if (best_fps_n <= 0 && best_fps_d <= 0) {
218 if (best_width > 0 && best_height > 0 && best_fps > 0) {
219 GstCaps *caps, *peercaps;
223 if (GST_VIDEO_INFO_FPS_N (&mix->info) != best_fps_n ||
224 GST_VIDEO_INFO_FPS_D (&mix->info) != best_fps_d) {
225 if (mix->segment.position != -1) {
226 mix->ts_offset = mix->segment.position - mix->segment.start;
230 gst_video_info_set_format (&info, GST_VIDEO_INFO_FORMAT (&mix->info),
231 best_width, best_height);
232 info.fps_n = best_fps_n;
233 info.fps_d = best_fps_d;
234 info.par_n = GST_VIDEO_INFO_PAR_N (&mix->info);
235 info.par_d = GST_VIDEO_INFO_PAR_D (&mix->info);
237 caps = gst_video_info_to_caps (&info);
239 peercaps = gst_pad_peer_query_caps (mix->srcpad, NULL);
243 s = gst_caps_get_structure (caps, 0);
244 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT, "height",
245 GST_TYPE_INT_RANGE, 1, G_MAXINT, "framerate", GST_TYPE_FRACTION_RANGE,
246 0, 1, G_MAXINT, 1, NULL);
248 tmp = gst_caps_intersect (caps, peercaps);
249 gst_caps_unref (caps);
250 gst_caps_unref (peercaps);
252 if (gst_caps_is_empty (caps)) {
253 GST_DEBUG_OBJECT (mix, "empty caps");
255 GST_VIDEO_MIXER2_UNLOCK (mix);
259 caps = gst_caps_truncate (caps);
260 s = gst_caps_get_structure (caps, 0);
261 gst_structure_fixate_field_nearest_int (s, "width", best_width);
262 gst_structure_fixate_field_nearest_int (s, "height", best_height);
263 gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n,
266 gst_structure_get_int (s, "width", &info.width);
267 gst_structure_get_int (s, "height", &info.height);
268 gst_structure_get_fraction (s, "fraction", &info.fps_n, &info.fps_d);
271 caps = gst_video_info_to_caps (&info);
273 GST_VIDEO_MIXER2_UNLOCK (mix);
274 ret = gst_videomixer2_src_setcaps (mix->srcpad, mix, caps);
275 gst_caps_unref (caps);
277 GST_VIDEO_MIXER2_UNLOCK (mix);
286 gst_videomixer2_pad_sink_setcaps (GstPad * pad, GstObject * parent,
290 GstVideoMixer2Pad *mixpad;
292 gboolean ret = FALSE;
294 GST_INFO_OBJECT (pad, "Setting caps %" GST_PTR_FORMAT, caps);
296 mix = GST_VIDEO_MIXER2 (parent);
297 mixpad = GST_VIDEO_MIXER2_PAD (pad);
299 if (!gst_video_info_from_caps (&info, caps)) {
300 GST_ERROR_OBJECT (pad, "Failed to parse caps");
304 GST_VIDEO_MIXER2_LOCK (mix);
305 if (GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_FORMAT_UNKNOWN) {
306 if (GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_INFO_FORMAT (&info) ||
307 GST_VIDEO_INFO_PAR_N (&mix->info) != GST_VIDEO_INFO_PAR_N (&info) ||
308 GST_VIDEO_INFO_PAR_D (&mix->info) != GST_VIDEO_INFO_PAR_D (&info)) {
309 GST_ERROR_OBJECT (pad, "Caps not compatible with other pads' caps");
310 GST_VIDEO_MIXER2_UNLOCK (mix);
318 GST_VIDEO_MIXER2_UNLOCK (mix);
320 ret = gst_videomixer2_update_src_caps (mix);
327 gst_videomixer2_pad_sink_getcaps (GstPad * pad, GstObject * parent,
335 mix = GST_VIDEO_MIXER2 (parent);
337 srccaps = gst_pad_get_current_caps (GST_PAD (mix->srcpad));
339 srccaps = gst_pad_get_pad_template_caps (GST_PAD (mix->srcpad));
341 srccaps = gst_caps_make_writable (srccaps);
343 n = gst_caps_get_size (srccaps);
344 for (i = 0; i < n; i++) {
345 s = gst_caps_get_structure (srccaps, i);
346 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
347 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
348 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
349 if (!gst_structure_has_field (s, "pixel-aspect-ratio"))
350 gst_structure_set (s, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
354 GST_DEBUG_OBJECT (pad, "Returning %" GST_PTR_FORMAT, srccaps);
360 gst_videomixer2_pad_sink_acceptcaps (GstPad * pad, GstObject * parent,
365 GstCaps *accepted_caps;
369 mix = GST_VIDEO_MIXER2 (parent);
370 GST_DEBUG_OBJECT (pad, "%" GST_PTR_FORMAT, caps);
372 accepted_caps = gst_pad_get_current_caps (GST_PAD (mix->srcpad));
373 if (accepted_caps == NULL)
374 accepted_caps = gst_pad_get_pad_template_caps (GST_PAD (mix->srcpad));
376 accepted_caps = gst_caps_make_writable (accepted_caps);
377 GST_LOG_OBJECT (pad, "src caps %" GST_PTR_FORMAT, accepted_caps);
379 n = gst_caps_get_size (accepted_caps);
380 for (i = 0; i < n; i++) {
381 s = gst_caps_get_structure (accepted_caps, 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 ret = gst_caps_can_intersect (caps, accepted_caps);
391 GST_INFO_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT, (ret ? "" : "not "),
393 GST_INFO_OBJECT (pad, "acceptable caps are %" GST_PTR_FORMAT, accepted_caps);
394 gst_caps_unref (accepted_caps);
400 gst_videomixer2_pad_sink_query (GstPad * pad, GstObject * parent,
403 gboolean ret = FALSE;
405 switch (GST_QUERY_TYPE (query)) {
408 GstCaps *filter, *caps;
410 gst_query_parse_caps (query, &filter);
411 caps = gst_videomixer2_pad_sink_getcaps (pad, parent, filter);
412 gst_query_set_caps_result (query, caps);
413 gst_caps_unref (caps);
417 case GST_QUERY_ACCEPT_CAPS:
421 gst_query_parse_accept_caps (query, &caps);
422 ret = gst_videomixer2_pad_sink_acceptcaps (pad, parent, caps);
423 gst_query_set_accept_caps_result (query, ret);
428 ret = gst_pad_query_default (pad, parent, query);
435 gst_videomixer2_pad_get_property (GObject * object, guint prop_id,
436 GValue * value, GParamSpec * pspec)
438 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
441 case PROP_PAD_ZORDER:
442 g_value_set_uint (value, pad->zorder);
445 g_value_set_int (value, pad->xpos);
448 g_value_set_int (value, pad->ypos);
451 g_value_set_double (value, pad->alpha);
454 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
460 pad_zorder_compare (const GstVideoMixer2Pad * pad1,
461 const GstVideoMixer2Pad * pad2)
463 return pad1->zorder - pad2->zorder;
467 gst_videomixer2_pad_set_property (GObject * object, guint prop_id,
468 const GValue * value, GParamSpec * pspec)
470 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (object);
471 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (gst_pad_get_parent (GST_PAD (pad)));
474 case PROP_PAD_ZORDER:
475 GST_VIDEO_MIXER2_LOCK (mix);
476 pad->zorder = g_value_get_uint (value);
478 mix->sinkpads = g_slist_sort (mix->sinkpads,
479 (GCompareFunc) pad_zorder_compare);
480 GST_VIDEO_MIXER2_UNLOCK (mix);
483 pad->xpos = g_value_get_int (value);
486 pad->ypos = g_value_get_int (value);
489 pad->alpha = g_value_get_double (value);
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 gst_object_unref (mix);
500 gst_videomixer2_pad_class_init (GstVideoMixer2PadClass * klass)
502 GObjectClass *gobject_class = (GObjectClass *) klass;
504 gobject_class->set_property = gst_videomixer2_pad_set_property;
505 gobject_class->get_property = gst_videomixer2_pad_get_property;
507 g_object_class_install_property (gobject_class, PROP_PAD_ZORDER,
508 g_param_spec_uint ("zorder", "Z-Order", "Z Order of the picture",
509 0, 10000, DEFAULT_PAD_ZORDER,
510 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
511 g_object_class_install_property (gobject_class, PROP_PAD_XPOS,
512 g_param_spec_int ("xpos", "X Position", "X Position of the picture",
513 G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
514 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
515 g_object_class_install_property (gobject_class, PROP_PAD_YPOS,
516 g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
517 G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
518 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
519 g_object_class_install_property (gobject_class, PROP_PAD_ALPHA,
520 g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
522 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
526 gst_videomixer2_pad_init (GstVideoMixer2Pad * mixerpad)
528 /* setup some pad functions */
529 gst_pad_set_query_function (GST_PAD (mixerpad),
530 gst_videomixer2_pad_sink_query);
532 mixerpad->zorder = DEFAULT_PAD_ZORDER;
533 mixerpad->xpos = DEFAULT_PAD_XPOS;
534 mixerpad->ypos = DEFAULT_PAD_YPOS;
535 mixerpad->alpha = DEFAULT_PAD_ALPHA;
539 #define DEFAULT_BACKGROUND VIDEO_MIXER2_BACKGROUND_CHECKER
546 #define GST_TYPE_VIDEO_MIXER2_BACKGROUND (gst_videomixer2_background_get_type())
548 gst_videomixer2_background_get_type (void)
550 static GType video_mixer_background_type = 0;
552 static const GEnumValue video_mixer_background[] = {
553 {VIDEO_MIXER2_BACKGROUND_CHECKER, "Checker pattern", "checker"},
554 {VIDEO_MIXER2_BACKGROUND_BLACK, "Black", "black"},
555 {VIDEO_MIXER2_BACKGROUND_WHITE, "White", "white"},
556 {VIDEO_MIXER2_BACKGROUND_TRANSPARENT,
557 "Transparent Background to enable further mixing", "transparent"},
561 if (!video_mixer_background_type) {
562 video_mixer_background_type =
563 g_enum_register_static ("GstVideoMixer2Background",
564 video_mixer_background);
566 return video_mixer_background_type;
569 #define gst_videomixer2_parent_class parent_class
570 G_DEFINE_TYPE_WITH_CODE (GstVideoMixer2, gst_videomixer2, GST_TYPE_ELEMENT,
571 G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY,
572 gst_videomixer2_child_proxy_init));
575 gst_videomixer2_update_qos (GstVideoMixer2 * mix, gdouble proportion,
576 GstClockTimeDiff diff, GstClockTime timestamp)
578 GST_DEBUG_OBJECT (mix,
579 "Updating QoS: proportion %lf, diff %s%" GST_TIME_FORMAT ", timestamp %"
580 GST_TIME_FORMAT, proportion, (diff < 0) ? "-" : "",
581 GST_TIME_ARGS (ABS (diff)), GST_TIME_ARGS (timestamp));
583 GST_OBJECT_LOCK (mix);
584 mix->proportion = proportion;
585 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
586 if (G_UNLIKELY (diff > 0))
588 timestamp + 2 * diff + gst_util_uint64_scale_int (GST_SECOND,
589 GST_VIDEO_INFO_FPS_D (&mix->info), GST_VIDEO_INFO_FPS_N (&mix->info));
591 mix->earliest_time = timestamp + diff;
593 mix->earliest_time = GST_CLOCK_TIME_NONE;
595 GST_OBJECT_UNLOCK (mix);
599 gst_videomixer2_reset_qos (GstVideoMixer2 * mix)
601 gst_videomixer2_update_qos (mix, 0.5, 0, GST_CLOCK_TIME_NONE);
602 mix->qos_processed = mix->qos_dropped = 0;
606 gst_videomixer2_read_qos (GstVideoMixer2 * mix, gdouble * proportion,
609 GST_OBJECT_LOCK (mix);
610 *proportion = mix->proportion;
611 *time = mix->earliest_time;
612 GST_OBJECT_UNLOCK (mix);
616 gst_videomixer2_reset (GstVideoMixer2 * mix)
620 gst_video_info_init (&mix->info);
624 gst_segment_init (&mix->segment, GST_FORMAT_TIME);
625 mix->segment.position = -1;
627 gst_videomixer2_reset_qos (mix);
629 for (l = mix->sinkpads; l; l = l->next) {
630 GstVideoMixer2Pad *p = l->data;
631 GstVideoMixer2Collect *mixcol = p->mixcol;
633 gst_buffer_replace (&mixcol->buffer, NULL);
634 mixcol->start_time = -1;
635 mixcol->end_time = -1;
637 gst_video_info_init (&p->info);
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 (ABS (mix->segment.rate) != 1.0) {
749 start_time *= ABS (mix->segment.rate);
750 end_time *= ABS (mix->segment.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,
819 BlendFunction composite;
820 GstVideoFrame outframe;
821 static GstAllocationParams params = { 0, 0, 0, 15, };
823 outsize = GST_VIDEO_INFO_SIZE (&mix->info);
825 *outbuf = gst_buffer_new_allocate (NULL, outsize, ¶ms);
826 GST_BUFFER_TIMESTAMP (*outbuf) = output_start_time;
827 GST_BUFFER_DURATION (*outbuf) = output_end_time - output_start_time;
829 gst_video_frame_map (&outframe, &mix->info, *outbuf, GST_MAP_READWRITE);
831 /* default to blending */
832 composite = mix->blend;
833 switch (mix->background) {
834 case VIDEO_MIXER2_BACKGROUND_CHECKER:
835 mix->fill_checker (&outframe);
837 case VIDEO_MIXER2_BACKGROUND_BLACK:
838 mix->fill_color (&outframe, 16, 128, 128);
840 case VIDEO_MIXER2_BACKGROUND_WHITE:
841 mix->fill_color (&outframe, 240, 128, 128);
843 case VIDEO_MIXER2_BACKGROUND_TRANSPARENT:
844 gst_buffer_memset (*outbuf, 0, 0, outsize);
845 /* use overlay to keep background transparent */
846 composite = mix->overlay;
850 for (l = mix->sinkpads; l; l = l->next) {
851 GstVideoMixer2Pad *pad = l->data;
852 GstVideoMixer2Collect *mixcol = pad->mixcol;
854 if (mixcol->buffer != NULL) {
855 GstClockTime timestamp;
860 seg = &mixcol->collect.segment;
862 timestamp = GST_BUFFER_TIMESTAMP (mixcol->buffer);
865 gst_segment_to_stream_time (seg, GST_FORMAT_TIME, timestamp);
867 /* sync object properties on stream time */
868 if (GST_CLOCK_TIME_IS_VALID (stream_time))
869 gst_object_sync_values (GST_OBJECT (pad), stream_time);
871 gst_video_frame_map (&frame, &pad->info, mixcol->buffer, GST_MAP_READ);
873 composite (&frame, pad->xpos, pad->ypos, pad->alpha, &outframe);
875 gst_video_frame_unmap (&frame);
878 gst_video_frame_unmap (&outframe);
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 (GST_VIDEO_INFO_FORMAT (&mix->info) == 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 (TRUE));
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,
948 gst_event_new_segment (&mix->segment))) {
949 ret = GST_FLOW_ERROR;
952 mix->newseg_pending = FALSE;
955 if (mix->segment.position == -1)
956 output_start_time = mix->segment.start;
958 output_start_time = mix->segment.position;
960 if (output_start_time >= mix->segment.stop) {
961 GST_DEBUG_OBJECT (mix, "Segment done");
962 gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
968 mix->ts_offset + gst_util_uint64_scale (mix->nframes + 1,
969 GST_SECOND * GST_VIDEO_INFO_FPS_D (&mix->info),
970 GST_VIDEO_INFO_FPS_N (&mix->info));
971 if (mix->segment.stop != -1)
972 output_end_time = MIN (output_end_time, mix->segment.stop);
974 res = gst_videomixer2_fill_queues (mix, output_start_time, output_end_time);
977 GST_DEBUG_OBJECT (mix, "Need more data for decisions");
980 } else if (res == -1) {
981 GST_DEBUG_OBJECT (mix, "All sinkpads are EOS -- forwarding");
982 gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
985 } else if (res == -2) {
986 GST_ERROR_OBJECT (mix, "Error collecting buffers");
987 ret = GST_FLOW_ERROR;
991 jitter = gst_videomixer2_do_qos (mix, output_start_time);
994 gst_videomixer2_blend_buffers (mix, output_start_time,
995 output_end_time, &outbuf);
996 mix->qos_processed++;
1004 gst_message_new_qos (GST_OBJECT_CAST (mix), FALSE,
1005 gst_segment_to_running_time (&mix->segment, GST_FORMAT_TIME,
1006 output_start_time), gst_segment_to_stream_time (&mix->segment,
1007 GST_FORMAT_TIME, output_start_time), output_start_time,
1008 output_end_time - output_start_time);
1009 gst_message_set_qos_values (msg, jitter, mix->proportion, 1000000);
1010 gst_message_set_qos_stats (msg, GST_FORMAT_BUFFERS, mix->qos_processed,
1012 gst_element_post_message (GST_ELEMENT_CAST (mix), msg);
1017 mix->segment.position = output_end_time;
1020 GST_VIDEO_MIXER2_UNLOCK (mix);
1022 GST_LOG_OBJECT (mix,
1023 "Pushing buffer with ts %" GST_TIME_FORMAT " and duration %"
1024 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1025 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
1026 ret = gst_pad_push (mix->srcpad, outbuf);
1028 GST_VIDEO_MIXER2_LOCK (mix);
1031 GST_VIDEO_MIXER2_UNLOCK (mix);
1037 gst_videomixer2_query_caps (GstPad * pad, GstObject * parent, GstQuery * query)
1039 GstCaps *filter, *caps;
1040 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
1044 gst_query_parse_caps (query, &filter);
1046 if (GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_FORMAT_UNKNOWN) {
1047 caps = gst_pad_get_current_caps (mix->srcpad);
1049 caps = gst_pad_get_pad_template_caps (mix->srcpad);
1052 caps = gst_caps_make_writable (caps);
1054 n = gst_caps_get_size (caps) - 1;
1055 for (; n >= 0; n--) {
1056 s = gst_caps_get_structure (caps, n);
1057 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1058 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
1059 if (GST_VIDEO_INFO_FPS_D (&mix->info) != 0) {
1060 gst_structure_set (s,
1061 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1064 gst_query_set_caps_result (query, caps);
1070 gst_videomixer2_query_duration (GstVideoMixer2 * mix, GstQuery * query)
1072 GValue item = { 0 };
1080 gst_query_parse_duration (query, &format, NULL);
1086 /* Take maximum of all durations */
1087 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1089 switch (gst_iterator_next (it, &item)) {
1090 case GST_ITERATOR_DONE:
1093 case GST_ITERATOR_OK:
1098 pad = g_value_get_object (&item);
1100 /* ask sink peer for duration */
1101 res &= gst_pad_peer_query_duration (pad, format, &duration);
1102 /* take max from all valid return values */
1104 /* valid unknown length, stop searching */
1105 if (duration == -1) {
1109 /* else see if bigger than current max */
1110 else if (duration > max)
1113 g_value_reset (&item);
1116 case GST_ITERATOR_RESYNC:
1119 gst_iterator_resync (it);
1127 g_value_unset (&item);
1128 gst_iterator_free (it);
1131 /* and store the max */
1132 GST_DEBUG_OBJECT (mix, "Total duration in format %s: %"
1133 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1134 gst_query_set_duration (query, format, max);
1141 gst_videomixer2_query_latency (GstVideoMixer2 * mix, GstQuery * query)
1143 GstClockTime min, max;
1148 GValue item = { 0 };
1154 max = GST_CLOCK_TIME_NONE;
1156 /* Take maximum of all latency values */
1157 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1159 switch (gst_iterator_next (it, &item)) {
1160 case GST_ITERATOR_DONE:
1163 case GST_ITERATOR_OK:
1165 GstPad *pad = g_value_get_object (&item);
1166 GstQuery *peerquery;
1167 GstClockTime min_cur, max_cur;
1170 peerquery = gst_query_new_latency ();
1172 /* Ask peer for latency */
1173 res &= gst_pad_peer_query (pad, peerquery);
1175 /* take max from all valid return values */
1177 gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
1182 if (max_cur != GST_CLOCK_TIME_NONE &&
1183 ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
1184 (max == GST_CLOCK_TIME_NONE)))
1187 live = live || live_cur;
1190 gst_query_unref (peerquery);
1191 g_value_reset (&item);
1194 case GST_ITERATOR_RESYNC:
1197 max = GST_CLOCK_TIME_NONE;
1199 gst_iterator_resync (it);
1207 g_value_unset (&item);
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, GstObject * parent, GstQuery * query)
1224 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
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.position));
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);
1252 case GST_QUERY_CAPS:
1253 res = gst_videomixer2_query_caps (pad, parent, query);
1256 /* FIXME, needs a custom query handler because we have multiple
1259 gst_query_unref (query);
1266 gst_videomixer2_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1268 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (parent);
1271 switch (GST_EVENT_TYPE (event)) {
1275 GstClockTimeDiff diff;
1276 GstClockTime timestamp;
1279 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1281 gst_videomixer2_update_qos (mix, proportion, diff, timestamp);
1283 result = gst_videomixer2_push_sink_event (mix, event);
1286 case GST_EVENT_SEEK:
1291 GstSeekType start_type, stop_type;
1296 /* parse the seek parameters */
1297 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
1298 &start, &stop_type, &stop);
1301 GST_ERROR_OBJECT (mix, "Negative rates not supported yet");
1303 gst_event_unref (event);
1307 GST_DEBUG_OBJECT (mix, "Handling SEEK event");
1309 /* check if we are flushing */
1310 if (flags & GST_SEEK_FLAG_FLUSH) {
1311 /* flushing seek, start flush downstream, the flush will be done
1312 * when all pads received a FLUSH_STOP. */
1313 gst_pad_push_event (mix->srcpad, gst_event_new_flush_start ());
1315 /* make sure we accept nothing anymore and return WRONG_STATE */
1316 gst_collect_pads2_set_flushing (mix->collect, TRUE);
1319 /* now wait for the collected to be finished and mark a new
1321 GST_COLLECT_PADS2_STREAM_LOCK (mix->collect);
1323 abs_rate = ABS (rate);
1325 GST_VIDEO_MIXER2_LOCK (mix);
1326 for (l = mix->sinkpads; l; l = l->next) {
1327 GstVideoMixer2Pad *p = l->data;
1329 if (flags & GST_SEEK_FLAG_FLUSH) {
1330 gst_buffer_replace (&p->mixcol->buffer, NULL);
1331 p->mixcol->start_time = p->mixcol->end_time = -1;
1335 /* Convert to the output segment rate */
1336 if (ABS (mix->segment.rate) != abs_rate) {
1337 if (ABS (mix->segment.rate) != 1.0 && p->mixcol->buffer) {
1338 p->mixcol->start_time /= ABS (mix->segment.rate);
1339 p->mixcol->end_time /= ABS (mix->segment.rate);
1341 if (abs_rate != 1.0 && p->mixcol->buffer) {
1342 p->mixcol->start_time *= abs_rate;
1343 p->mixcol->end_time *= abs_rate;
1347 GST_VIDEO_MIXER2_UNLOCK (mix);
1349 gst_segment_do_seek (&mix->segment, rate, fmt, flags, start_type, start,
1350 stop_type, stop, NULL);
1351 mix->segment.position = -1;
1354 mix->newseg_pending = TRUE;
1356 if (flags & GST_SEEK_FLAG_FLUSH) {
1357 gst_collect_pads2_set_flushing (mix->collect, FALSE);
1359 /* we can't send FLUSH_STOP here since upstream could start pushing data
1360 * after we unlock mix->collect.
1361 * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
1362 * forwarding the seek upstream or from gst_videomixer_collected,
1363 * whichever happens first.
1365 mix->flush_stop_pending = TRUE;
1368 GST_COLLECT_PADS2_STREAM_UNLOCK (mix->collect);
1370 gst_videomixer2_reset_qos (mix);
1372 result = gst_videomixer2_push_sink_event (mix, event);
1374 if (g_atomic_int_compare_and_exchange (&mix->flush_stop_pending, TRUE,
1376 GST_DEBUG_OBJECT (mix, "pending flush stop");
1377 gst_pad_push_event (mix->srcpad, gst_event_new_flush_stop (TRUE));
1382 case GST_EVENT_NAVIGATION:
1383 /* navigation is rather pointless. */
1385 gst_event_unref (event);
1388 /* just forward the rest for now */
1389 result = gst_videomixer2_push_sink_event (mix, event);
1397 gst_videomixer2_src_setcaps (GstPad * pad, GstVideoMixer2 * mix, GstCaps * caps)
1399 gboolean ret = FALSE;
1402 GST_INFO_OBJECT (pad, "set src caps: %" GST_PTR_FORMAT, caps);
1405 mix->overlay = NULL;
1406 mix->fill_checker = NULL;
1407 mix->fill_color = NULL;
1409 if (!gst_video_info_from_caps (&info, caps))
1412 GST_VIDEO_MIXER2_LOCK (mix);
1414 if (GST_VIDEO_INFO_FPS_N (&mix->info) != GST_VIDEO_INFO_FPS_N (&info) ||
1415 GST_VIDEO_INFO_FPS_D (&mix->info) != GST_VIDEO_INFO_FPS_D (&info)) {
1416 if (mix->segment.position != -1) {
1417 mix->ts_offset = mix->segment.position - mix->segment.start;
1420 gst_videomixer2_reset_qos (mix);
1425 switch (GST_VIDEO_INFO_FORMAT (&mix->info)) {
1426 case GST_VIDEO_FORMAT_AYUV:
1427 mix->blend = gst_video_mixer_blend_ayuv;
1428 mix->overlay = gst_video_mixer_overlay_ayuv;
1429 mix->fill_checker = gst_video_mixer_fill_checker_ayuv;
1430 mix->fill_color = gst_video_mixer_fill_color_ayuv;
1433 case GST_VIDEO_FORMAT_ARGB:
1434 mix->blend = gst_video_mixer_blend_argb;
1435 mix->overlay = gst_video_mixer_overlay_argb;
1436 mix->fill_checker = gst_video_mixer_fill_checker_argb;
1437 mix->fill_color = gst_video_mixer_fill_color_argb;
1440 case GST_VIDEO_FORMAT_BGRA:
1441 mix->blend = gst_video_mixer_blend_bgra;
1442 mix->overlay = gst_video_mixer_overlay_bgra;
1443 mix->fill_checker = gst_video_mixer_fill_checker_bgra;
1444 mix->fill_color = gst_video_mixer_fill_color_bgra;
1447 case GST_VIDEO_FORMAT_ABGR:
1448 mix->blend = gst_video_mixer_blend_abgr;
1449 mix->overlay = gst_video_mixer_overlay_abgr;
1450 mix->fill_checker = gst_video_mixer_fill_checker_abgr;
1451 mix->fill_color = gst_video_mixer_fill_color_abgr;
1454 case GST_VIDEO_FORMAT_RGBA:
1455 mix->blend = gst_video_mixer_blend_rgba;
1456 mix->overlay = gst_video_mixer_overlay_rgba;
1457 mix->fill_checker = gst_video_mixer_fill_checker_rgba;
1458 mix->fill_color = gst_video_mixer_fill_color_rgba;
1461 case GST_VIDEO_FORMAT_Y444:
1462 mix->blend = gst_video_mixer_blend_y444;
1463 mix->overlay = mix->blend;
1464 mix->fill_checker = gst_video_mixer_fill_checker_y444;
1465 mix->fill_color = gst_video_mixer_fill_color_y444;
1468 case GST_VIDEO_FORMAT_Y42B:
1469 mix->blend = gst_video_mixer_blend_y42b;
1470 mix->overlay = mix->blend;
1471 mix->fill_checker = gst_video_mixer_fill_checker_y42b;
1472 mix->fill_color = gst_video_mixer_fill_color_y42b;
1475 case GST_VIDEO_FORMAT_YUY2:
1476 mix->blend = gst_video_mixer_blend_yuy2;
1477 mix->overlay = mix->blend;
1478 mix->fill_checker = gst_video_mixer_fill_checker_yuy2;
1479 mix->fill_color = gst_video_mixer_fill_color_yuy2;
1482 case GST_VIDEO_FORMAT_UYVY:
1483 mix->blend = gst_video_mixer_blend_uyvy;
1484 mix->overlay = mix->blend;
1485 mix->fill_checker = gst_video_mixer_fill_checker_uyvy;
1486 mix->fill_color = gst_video_mixer_fill_color_uyvy;
1489 case GST_VIDEO_FORMAT_YVYU:
1490 mix->blend = gst_video_mixer_blend_yvyu;
1491 mix->overlay = mix->blend;
1492 mix->fill_checker = gst_video_mixer_fill_checker_yvyu;
1493 mix->fill_color = gst_video_mixer_fill_color_yvyu;
1496 case GST_VIDEO_FORMAT_I420:
1497 mix->blend = gst_video_mixer_blend_i420;
1498 mix->overlay = mix->blend;
1499 mix->fill_checker = gst_video_mixer_fill_checker_i420;
1500 mix->fill_color = gst_video_mixer_fill_color_i420;
1503 case GST_VIDEO_FORMAT_YV12:
1504 mix->blend = gst_video_mixer_blend_yv12;
1505 mix->overlay = mix->blend;
1506 mix->fill_checker = gst_video_mixer_fill_checker_yv12;
1507 mix->fill_color = gst_video_mixer_fill_color_yv12;
1510 case GST_VIDEO_FORMAT_Y41B:
1511 mix->blend = gst_video_mixer_blend_y41b;
1512 mix->overlay = mix->blend;
1513 mix->fill_checker = gst_video_mixer_fill_checker_y41b;
1514 mix->fill_color = gst_video_mixer_fill_color_y41b;
1517 case GST_VIDEO_FORMAT_RGB:
1518 mix->blend = gst_video_mixer_blend_rgb;
1519 mix->overlay = mix->blend;
1520 mix->fill_checker = gst_video_mixer_fill_checker_rgb;
1521 mix->fill_color = gst_video_mixer_fill_color_rgb;
1524 case GST_VIDEO_FORMAT_BGR:
1525 mix->blend = gst_video_mixer_blend_bgr;
1526 mix->overlay = mix->blend;
1527 mix->fill_checker = gst_video_mixer_fill_checker_bgr;
1528 mix->fill_color = gst_video_mixer_fill_color_bgr;
1531 case GST_VIDEO_FORMAT_xRGB:
1532 mix->blend = gst_video_mixer_blend_xrgb;
1533 mix->overlay = mix->blend;
1534 mix->fill_checker = gst_video_mixer_fill_checker_xrgb;
1535 mix->fill_color = gst_video_mixer_fill_color_xrgb;
1538 case GST_VIDEO_FORMAT_xBGR:
1539 mix->blend = gst_video_mixer_blend_xbgr;
1540 mix->overlay = mix->blend;
1541 mix->fill_checker = gst_video_mixer_fill_checker_xbgr;
1542 mix->fill_color = gst_video_mixer_fill_color_xbgr;
1545 case GST_VIDEO_FORMAT_RGBx:
1546 mix->blend = gst_video_mixer_blend_rgbx;
1547 mix->overlay = mix->blend;
1548 mix->fill_checker = gst_video_mixer_fill_checker_rgbx;
1549 mix->fill_color = gst_video_mixer_fill_color_rgbx;
1552 case GST_VIDEO_FORMAT_BGRx:
1553 mix->blend = gst_video_mixer_blend_bgrx;
1554 mix->overlay = mix->blend;
1555 mix->fill_checker = gst_video_mixer_fill_checker_bgrx;
1556 mix->fill_color = gst_video_mixer_fill_color_bgrx;
1562 GST_VIDEO_MIXER2_UNLOCK (mix);
1564 ret = gst_pad_set_caps (pad, caps);
1570 static GstFlowReturn
1571 gst_videomixer2_sink_clip (GstCollectPads2 * pads,
1572 GstCollectData2 * data, GstBuffer * buf, GstBuffer ** outbuf,
1573 GstVideoMixer2 * mix)
1575 GstVideoMixer2Pad *pad = GST_VIDEO_MIXER2_PAD (data->pad);
1576 GstVideoMixer2Collect *mixcol = pad->mixcol;
1577 GstClockTime start_time, end_time;
1579 start_time = GST_BUFFER_TIMESTAMP (buf);
1580 if (start_time == -1) {
1581 GST_ERROR_OBJECT (pad, "Timestamped buffers required!");
1582 gst_buffer_unref (buf);
1583 return GST_FLOW_ERROR;
1586 end_time = GST_BUFFER_DURATION (buf);
1589 gst_util_uint64_scale_int (GST_SECOND,
1590 GST_VIDEO_INFO_FPS_D (&pad->info), GST_VIDEO_INFO_FPS_N (&pad->info));
1591 if (end_time == -1) {
1596 start_time = MAX (start_time, mixcol->collect.segment.start);
1598 gst_segment_to_running_time (&mixcol->collect.segment,
1599 GST_FORMAT_TIME, start_time);
1601 end_time += GST_BUFFER_TIMESTAMP (buf);
1602 if (mixcol->collect.segment.stop != -1)
1603 end_time = MIN (end_time, mixcol->collect.segment.stop);
1605 gst_segment_to_running_time (&mixcol->collect.segment,
1606 GST_FORMAT_TIME, end_time);
1608 /* Convert to the output segment rate */
1609 if (ABS (mix->segment.rate) != 1.0) {
1610 start_time *= ABS (mix->segment.rate);
1611 end_time *= ABS (mix->segment.rate);
1614 if (mixcol->buffer != NULL && end_time < mixcol->end_time) {
1615 gst_buffer_unref (buf);
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_CAPS:
1640 gst_event_parse_caps (event, &caps);
1642 gst_videomixer2_pad_sink_setcaps (GST_PAD (pad), GST_OBJECT (mix),
1644 gst_event_unref (event);
1647 case GST_EVENT_SEGMENT:{
1649 gst_event_copy_segment (event, &seg);
1651 g_assert (seg.format == GST_FORMAT_TIME);
1652 /* eat SEGMENT events */
1654 gst_event_unref (event);
1657 case GST_EVENT_FLUSH_STOP:
1658 mix->newseg_pending = TRUE;
1659 mix->flush_stop_pending = FALSE;
1660 gst_videomixer2_reset_qos (mix);
1661 gst_buffer_replace (&pad->mixcol->buffer, NULL);
1662 pad->mixcol->start_time = -1;
1663 pad->mixcol->end_time = -1;
1665 gst_segment_init (&mix->segment, GST_FORMAT_TIME);
1666 mix->segment.position = -1;
1670 ret = gst_pad_event_default (cdata->pad, GST_OBJECT (mix), event);
1673 gst_event_unref (event);
1677 ret = gst_pad_event_default (cdata->pad, GST_OBJECT (mix), event);
1685 forward_event_func (GValue * item, GValue * ret, GstEvent * event)
1687 GstPad *pad = g_value_get_object (item);
1688 gst_event_ref (event);
1689 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
1690 if (!gst_pad_push_event (pad, event)) {
1691 g_value_set_boolean (ret, FALSE);
1692 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
1693 event, GST_EVENT_TYPE_NAME (event));
1695 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
1696 event, GST_EVENT_TYPE_NAME (event));
1702 gst_videomixer2_push_sink_event (GstVideoMixer2 * mix, GstEvent * event)
1705 GValue vret = { 0 };
1707 GST_LOG_OBJECT (mix, "Forwarding event %p (%s)", event,
1708 GST_EVENT_TYPE_NAME (event));
1710 g_value_init (&vret, G_TYPE_BOOLEAN);
1711 g_value_set_boolean (&vret, TRUE);
1712 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mix));
1713 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
1715 gst_iterator_free (it);
1716 gst_event_unref (event);
1718 return g_value_get_boolean (&vret);
1721 /* GstElement vmethods */
1722 static GstStateChangeReturn
1723 gst_videomixer2_change_state (GstElement * element, GstStateChange transition)
1725 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (element);
1726 GstStateChangeReturn ret;
1728 switch (transition) {
1729 case GST_STATE_CHANGE_READY_TO_PAUSED:
1730 GST_LOG_OBJECT (mix, "starting collectpads");
1731 gst_collect_pads2_start (mix->collect);
1733 case GST_STATE_CHANGE_PAUSED_TO_READY:
1734 GST_LOG_OBJECT (mix, "stopping collectpads");
1735 gst_collect_pads2_stop (mix->collect);
1741 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1743 switch (transition) {
1744 case GST_STATE_CHANGE_PAUSED_TO_READY:
1745 gst_videomixer2_reset (mix);
1755 gst_videomixer2_request_new_pad (GstElement * element,
1756 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
1758 GstVideoMixer2 *mix;
1759 GstVideoMixer2Pad *mixpad;
1760 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
1762 mix = GST_VIDEO_MIXER2 (element);
1764 if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
1767 GstVideoMixer2Collect *mixcol = NULL;
1769 GST_VIDEO_MIXER2_LOCK (mix);
1770 if (req_name == NULL || strlen (req_name) < 6
1771 || !g_str_has_prefix (req_name, "sink_")) {
1772 /* no name given when requesting the pad, use next available int */
1773 serial = mix->next_sinkpad++;
1775 /* parse serial number from requested padname */
1776 serial = g_ascii_strtoull (&req_name[5], NULL, 10);
1777 if (serial >= mix->next_sinkpad)
1778 mix->next_sinkpad = serial + 1;
1780 /* create new pad with the name */
1781 name = g_strdup_printf ("sink_%d", serial);
1782 mixpad = g_object_new (GST_TYPE_VIDEO_MIXER2_PAD, "name", name, "direction",
1783 templ->direction, "template", templ, NULL);
1786 mixpad->zorder = mix->numpads;
1787 mixpad->xpos = DEFAULT_PAD_XPOS;
1788 mixpad->ypos = DEFAULT_PAD_YPOS;
1789 mixpad->alpha = DEFAULT_PAD_ALPHA;
1791 mixcol = (GstVideoMixer2Collect *)
1792 gst_collect_pads2_add_pad_full (mix->collect, GST_PAD (mixpad),
1793 sizeof (GstVideoMixer2Collect),
1794 (GstCollectData2DestroyNotify) gst_videomixer2_collect_free, TRUE);
1796 /* Keep track of each other */
1797 mixcol->mixpad = mixpad;
1798 mixpad->mixcol = mixcol;
1800 mixcol->start_time = -1;
1801 mixcol->end_time = -1;
1803 /* Keep an internal list of mixpads for zordering */
1804 mix->sinkpads = g_slist_append (mix->sinkpads, mixpad);
1806 GST_VIDEO_MIXER2_UNLOCK (mix);
1811 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (mixpad));
1813 /* add the pad to the element */
1814 gst_element_add_pad (element, GST_PAD (mixpad));
1815 gst_child_proxy_child_added (G_OBJECT (mix), G_OBJECT (mixpad),
1816 GST_OBJECT_NAME (mixpad));
1818 return GST_PAD (mixpad);
1822 gst_videomixer2_release_pad (GstElement * element, GstPad * pad)
1824 GstVideoMixer2 *mix = NULL;
1825 GstVideoMixer2Pad *mixpad;
1826 gboolean update_caps;
1828 mix = GST_VIDEO_MIXER2 (element);
1830 GST_VIDEO_MIXER2_LOCK (mix);
1831 if (G_UNLIKELY (g_slist_find (mix->sinkpads, pad) == NULL)) {
1832 g_warning ("Unknown pad %s", GST_PAD_NAME (pad));
1836 mixpad = GST_VIDEO_MIXER2_PAD (pad);
1838 mix->sinkpads = g_slist_remove (mix->sinkpads, pad);
1839 gst_child_proxy_child_removed (G_OBJECT (mix), G_OBJECT (mixpad),
1840 GST_OBJECT_NAME (mixpad));
1843 update_caps = GST_VIDEO_INFO_FORMAT (&mix->info) != GST_VIDEO_FORMAT_UNKNOWN;
1844 GST_VIDEO_MIXER2_UNLOCK (mix);
1846 gst_collect_pads2_remove_pad (mix->collect, pad);
1849 gst_videomixer2_update_src_caps (mix);
1851 gst_element_remove_pad (element, pad);
1854 GST_VIDEO_MIXER2_UNLOCK (mix);
1857 /* GObject vmethods */
1859 gst_videomixer2_finalize (GObject * o)
1861 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (o);
1863 gst_object_unref (mix->collect);
1864 g_mutex_clear (&mix->lock);
1866 G_OBJECT_CLASS (parent_class)->finalize (o);
1870 gst_videomixer2_get_property (GObject * object,
1871 guint prop_id, GValue * value, GParamSpec * pspec)
1873 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1876 case PROP_BACKGROUND:
1877 g_value_set_enum (value, mix->background);
1880 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1886 gst_videomixer2_set_property (GObject * object,
1887 guint prop_id, const GValue * value, GParamSpec * pspec)
1889 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (object);
1892 case PROP_BACKGROUND:
1893 mix->background = g_value_get_enum (value);
1896 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1901 /* GstChildProxy implementation */
1903 gst_videomixer2_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
1906 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1909 GST_VIDEO_MIXER2_LOCK (mix);
1910 if ((obj = g_slist_nth_data (mix->sinkpads, index)))
1912 GST_VIDEO_MIXER2_UNLOCK (mix);
1917 gst_videomixer2_child_proxy_get_children_count (GstChildProxy * child_proxy)
1920 GstVideoMixer2 *mix = GST_VIDEO_MIXER2 (child_proxy);
1922 GST_VIDEO_MIXER2_LOCK (mix);
1923 count = mix->numpads;
1924 GST_VIDEO_MIXER2_UNLOCK (mix);
1925 GST_INFO_OBJECT (mix, "Children Count: %d", count);
1930 gst_videomixer2_child_proxy_init (gpointer g_iface, gpointer iface_data)
1932 GstChildProxyInterface *iface = g_iface;
1934 GST_INFO ("intializing child proxy interface");
1935 iface->get_child_by_index = gst_videomixer2_child_proxy_get_child_by_index;
1936 iface->get_children_count = gst_videomixer2_child_proxy_get_children_count;
1939 /* GObject boilerplate */
1941 gst_videomixer2_class_init (GstVideoMixer2Class * klass)
1943 GObjectClass *gobject_class = (GObjectClass *) klass;
1944 GstElementClass *gstelement_class = (GstElementClass *) klass;
1946 gobject_class->finalize = gst_videomixer2_finalize;
1948 gobject_class->get_property = gst_videomixer2_get_property;
1949 gobject_class->set_property = gst_videomixer2_set_property;
1951 g_object_class_install_property (gobject_class, PROP_BACKGROUND,
1952 g_param_spec_enum ("background", "Background", "Background type",
1953 GST_TYPE_VIDEO_MIXER2_BACKGROUND,
1954 DEFAULT_BACKGROUND, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1956 gstelement_class->request_new_pad =
1957 GST_DEBUG_FUNCPTR (gst_videomixer2_request_new_pad);
1958 gstelement_class->release_pad =
1959 GST_DEBUG_FUNCPTR (gst_videomixer2_release_pad);
1960 gstelement_class->change_state =
1961 GST_DEBUG_FUNCPTR (gst_videomixer2_change_state);
1963 gst_element_class_add_pad_template (gstelement_class,
1964 gst_static_pad_template_get (&src_factory));
1965 gst_element_class_add_pad_template (gstelement_class,
1966 gst_static_pad_template_get (&sink_factory));
1968 gst_element_class_set_static_metadata (gstelement_class, "Video mixer 2",
1969 "Filter/Editor/Video",
1970 "Mix multiple video streams", "Wim Taymans <wim@fluendo.com>, "
1971 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1973 /* Register the pad class */
1974 g_type_class_ref (GST_TYPE_VIDEO_MIXER2_PAD);
1978 gst_videomixer2_init (GstVideoMixer2 * mix)
1980 GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix);
1983 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
1985 gst_pad_set_query_function (GST_PAD (mix->srcpad),
1986 GST_DEBUG_FUNCPTR (gst_videomixer2_src_query));
1987 gst_pad_set_event_function (GST_PAD (mix->srcpad),
1988 GST_DEBUG_FUNCPTR (gst_videomixer2_src_event));
1989 gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad);
1991 mix->collect = gst_collect_pads2_new ();
1992 mix->background = DEFAULT_BACKGROUND;
1994 gst_collect_pads2_set_function (mix->collect,
1995 (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_videomixer2_collected),
1997 gst_collect_pads2_set_event_function (mix->collect,
1998 (GstCollectPads2EventFunction) gst_videomixer2_sink_event, mix);
1999 gst_collect_pads2_set_clip_function (mix->collect,
2000 (GstCollectPads2ClipFunction) gst_videomixer2_sink_clip, mix);
2002 g_mutex_init (&mix->lock);
2003 /* initialize variables */
2004 gst_videomixer2_reset (mix);
2007 /* Element registration */
2009 plugin_init (GstPlugin * plugin)
2011 GST_DEBUG_CATEGORY_INIT (gst_videomixer2_debug, "videomixer", 0,
2014 gst_video_mixer_init_blend ();
2016 return gst_element_register (plugin, "videomixer", GST_RANK_PRIMARY,
2017 GST_TYPE_VIDEO_MIXER2);
2020 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2023 "Video mixer", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,