55e4152a3655bd7938a6106fa359b71f3ea1e0d7
[platform/upstream/gstreamer.git] / gst / rist / gstroundrobin.c
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 indepently.
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
57 static GstFlowReturn
58 gst_round_robin_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
59 {
60   GstRoundRobin *disp = (GstRoundRobin *) parent;
61   GstElement *elem = (GstElement *) parent;
62   GstPad *src_pad = NULL;
63   GstFlowReturn ret;
64
65   GST_OBJECT_LOCK (disp);
66   if (disp->index >= elem->numsrcpads)
67     disp->index = 0;
68
69   src_pad = g_list_nth_data (elem->srcpads, disp->index);
70
71   if (src_pad) {
72     gst_object_ref (src_pad);
73     disp->index += 1;
74   }
75   GST_OBJECT_UNLOCK (disp);
76
77   if (!src_pad)
78     /* no pad, that's fine */
79     return GST_FLOW_OK;
80
81   ret = gst_pad_push (src_pad, buffer);
82   gst_object_unref (src_pad);
83
84   return ret;
85 }
86
87 static GstPad *
88 gst_round_robin_request_pad (GstElement * element, GstPadTemplate * templ,
89     const gchar * name, const GstCaps * caps)
90 {
91   GstPad *pad;
92
93   pad = gst_element_get_static_pad (element, name);
94   if (pad) {
95     gst_object_unref (pad);
96     return NULL;
97   }
98
99   pad = gst_pad_new_from_static_template (&src_templ, name);
100   gst_element_add_pad (element, pad);
101
102   return pad;
103 }
104
105 static void
106 gst_round_robin_init (GstRoundRobin * disp)
107 {
108   GstPad *pad;
109
110   gst_element_create_all_pads (GST_ELEMENT (disp));
111   pad = GST_PAD (GST_ELEMENT (disp)->sinkpads->data);
112
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 */
116
117   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_round_robin_chain));
118 }
119
120 static void
121 gst_round_robin_class_init (GstRoundRobinClass * klass)
122 {
123   GstElementClass *element_class = (GstElementClass *) klass;
124
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");
129
130   gst_element_class_add_static_pad_template (element_class, &sink_templ);
131   gst_element_class_add_static_pad_template (element_class, &src_templ);
132
133   element_class->request_new_pad =
134       GST_DEBUG_FUNCPTR (gst_round_robin_request_pad);
135 }