1 /* GStreamer Round Robin
2 * Copyright (C) 2019 Net Insight AB
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-roundrobin
25 * This is a generic element that will distribute equally incoming
26 * buffers over multiple src pads. This is the opposite of tee
27 * element, which duplicates buffers over all pads. This element
28 * can be used to distrute load across multiple branches when the buffer
29 * can be processed indepently.
32 #include "gstroundrobin.h"
34 GST_DEBUG_CATEGORY_STATIC (gst_round_robin_debug);
35 #define GST_CAT_DEFAULT gst_round_robin_debug
37 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("ANY"));
42 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src_%d",
45 GST_STATIC_CAPS ("ANY"));
53 G_DEFINE_TYPE_WITH_CODE (GstRoundRobin, gst_round_robin,
54 GST_TYPE_ELEMENT, GST_DEBUG_CATEGORY_INIT (gst_round_robin_debug,
55 "roundrobin", 0, "Round Robin"));
58 gst_round_robin_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
60 GstRoundRobin *disp = (GstRoundRobin *) parent;
61 GstElement *elem = (GstElement *) parent;
62 GstPad *src_pad = NULL;
65 GST_OBJECT_LOCK (disp);
66 if (disp->index >= elem->numsrcpads)
69 src_pad = g_list_nth_data (elem->srcpads, disp->index);
72 gst_object_ref (src_pad);
75 GST_OBJECT_UNLOCK (disp);
78 /* no pad, that's fine */
81 ret = gst_pad_push (src_pad, buffer);
82 gst_object_unref (src_pad);
88 gst_round_robin_request_pad (GstElement * element, GstPadTemplate * templ,
89 const gchar * name, const GstCaps * caps)
93 pad = gst_element_get_static_pad (element, name);
95 gst_object_unref (pad);
99 pad = gst_pad_new_from_static_template (&src_templ, name);
100 gst_element_add_pad (element, pad);
106 gst_round_robin_init (GstRoundRobin * disp)
110 gst_element_create_all_pads (GST_ELEMENT (disp));
111 pad = GST_PAD (GST_ELEMENT (disp)->sinkpads->data);
113 GST_PAD_SET_PROXY_CAPS (pad);
114 GST_PAD_SET_PROXY_SCHEDULING (pad);
115 /* do not proxy allocation, it requires special handling like tee does */
117 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_round_robin_chain));
121 gst_round_robin_class_init (GstRoundRobinClass * klass)
123 GstElementClass *element_class = (GstElementClass *) klass;
125 gst_element_class_set_metadata (element_class,
126 "Round Robin", "Source/Network",
127 "A round robin dispatcher element.",
128 "Nicolas Dufresne <nicolas.dufresne@collabora.com");
130 gst_element_class_add_static_pad_template (element_class, &sink_templ);
131 gst_element_class_add_static_pad_template (element_class, &src_templ);
133 element_class->request_new_pad =
134 GST_DEBUG_FUNCPTR (gst_round_robin_request_pad);