1 /* GStreamer Round Robin
2 * Copyright (C) 2019 Net Insight AB
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 /**
22 * SECTION:element-roundrobin
23 * @title: roundrobin
24 *
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 independently.
30 */
31
32 #include "gstroundrobin.h"
33
34 GST_DEBUG_CATEGORY_STATIC (gst_round_robin_debug);
35 #define GST_CAT_DEFAULT gst_round_robin_debug
36
37 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
38 GST_PAD_SINK,
39 GST_PAD_ALWAYS,
40 GST_STATIC_CAPS ("ANY"));
41
42 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src_%d",
43 GST_PAD_SRC,
44 GST_PAD_REQUEST,
45 GST_STATIC_CAPS ("ANY"));
46
47 struct _GstRoundRobin
48 {
49 GstElement parent;
50 gint index;
51 };
52
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"));
56 GST_ELEMENT_REGISTER_DEFINE (roundrobin, "roundrobin", GST_RANK_NONE,
57 GST_TYPE_ROUND_ROBIN);
58
59 static GstFlowReturn
gst_round_robin_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)60 gst_round_robin_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
61 {
62 GstRoundRobin *disp = (GstRoundRobin *) parent;
63 GstElement *elem = (GstElement *) parent;
64 GstPad *src_pad = NULL;
65 GstFlowReturn ret;
66
67 GST_OBJECT_LOCK (disp);
68 if (disp->index >= elem->numsrcpads)
69 disp->index = 0;
70
71 src_pad = g_list_nth_data (elem->srcpads, disp->index);
72
73 if (src_pad) {
74 gst_object_ref (src_pad);
75 disp->index += 1;
76 }
77 GST_OBJECT_UNLOCK (disp);
78
79 if (!src_pad)
80 /* no pad, that's fine */
81 return GST_FLOW_OK;
82
83 ret = gst_pad_push (src_pad, buffer);
84 gst_object_unref (src_pad);
85
86 return ret;
87 }
88
89 static GstPad *
gst_round_robin_request_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)90 gst_round_robin_request_pad (GstElement * element, GstPadTemplate * templ,
91 const gchar * name, const GstCaps * caps)
92 {
93 GstPad *pad;
94
95 pad = gst_element_get_static_pad (element, name);
96 if (pad) {
97 gst_object_unref (pad);
98 return NULL;
99 }
100
101 pad = gst_pad_new_from_static_template (&src_templ, name);
102 gst_element_add_pad (element, pad);
103
104 return pad;
105 }
106
107 static void
gst_round_robin_init(GstRoundRobin * disp)108 gst_round_robin_init (GstRoundRobin * disp)
109 {
110 GstPad *pad;
111
112 gst_element_create_all_pads (GST_ELEMENT (disp));
113 pad = GST_PAD (GST_ELEMENT (disp)->sinkpads->data);
114
115 GST_PAD_SET_PROXY_CAPS (pad);
116 GST_PAD_SET_PROXY_SCHEDULING (pad);
117 /* do not proxy allocation, it requires special handling like tee does */
118
119 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_round_robin_chain));
120 }
121
122 static void
gst_round_robin_class_init(GstRoundRobinClass * klass)123 gst_round_robin_class_init (GstRoundRobinClass * klass)
124 {
125 GstElementClass *element_class = (GstElementClass *) klass;
126
127 gst_element_class_set_metadata (element_class,
128 "Round Robin", "Source/Network",
129 "A round robin dispatcher element.",
130 "Nicolas Dufresne <nicolas.dufresne@collabora.com");
131
132 gst_element_class_add_static_pad_template (element_class, &sink_templ);
133 gst_element_class_add_static_pad_template (element_class, &src_templ);
134
135 element_class->request_new_pad =
136 GST_DEBUG_FUNCPTR (gst_round_robin_request_pad);
137 }
138