summaryrefslogtreecommitdiff
path: root/gst/rist/gstroundrobin.c
blob: 55e4152a3655bd7938a6106fa359b71f3ea1e0d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* GStreamer Round Robin
 * Copyright (C) 2019 Net Insight AB
 *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:element-roundrobin
 * @title: roundrobin
 *
 * This is a generic element that will distribute equally incoming
 * buffers over multiple src pads. This is the opposite of tee
 * element, which duplicates buffers over all pads. This element 
 * can be used to distrute load across multiple branches when the buffer
 * can be processed indepently.
 */

#include "gstroundrobin.h"

GST_DEBUG_CATEGORY_STATIC (gst_round_robin_debug);
#define GST_CAT_DEFAULT gst_round_robin_debug

static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("ANY"));

static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src_%d",
    GST_PAD_SRC,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("ANY"));

struct _GstRoundRobin
{
  GstElement parent;
  gint index;
};

G_DEFINE_TYPE_WITH_CODE (GstRoundRobin, gst_round_robin,
    GST_TYPE_ELEMENT, GST_DEBUG_CATEGORY_INIT (gst_round_robin_debug,
        "roundrobin", 0, "Round Robin"));

static GstFlowReturn
gst_round_robin_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
  GstRoundRobin *disp = (GstRoundRobin *) parent;
  GstElement *elem = (GstElement *) parent;
  GstPad *src_pad = NULL;
  GstFlowReturn ret;

  GST_OBJECT_LOCK (disp);
  if (disp->index >= elem->numsrcpads)
    disp->index = 0;

  src_pad = g_list_nth_data (elem->srcpads, disp->index);

  if (src_pad) {
    gst_object_ref (src_pad);
    disp->index += 1;
  }
  GST_OBJECT_UNLOCK (disp);

  if (!src_pad)
    /* no pad, that's fine */
    return GST_FLOW_OK;

  ret = gst_pad_push (src_pad, buffer);
  gst_object_unref (src_pad);

  return ret;
}

static GstPad *
gst_round_robin_request_pad (GstElement * element, GstPadTemplate * templ,
    const gchar * name, const GstCaps * caps)
{
  GstPad *pad;

  pad = gst_element_get_static_pad (element, name);
  if (pad) {
    gst_object_unref (pad);
    return NULL;
  }

  pad = gst_pad_new_from_static_template (&src_templ, name);
  gst_element_add_pad (element, pad);

  return pad;
}

static void
gst_round_robin_init (GstRoundRobin * disp)
{
  GstPad *pad;

  gst_element_create_all_pads (GST_ELEMENT (disp));
  pad = GST_PAD (GST_ELEMENT (disp)->sinkpads->data);

  GST_PAD_SET_PROXY_CAPS (pad);
  GST_PAD_SET_PROXY_SCHEDULING (pad);
  /* do not proxy allocation, it requires special handling like tee does */

  gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_round_robin_chain));
}

static void
gst_round_robin_class_init (GstRoundRobinClass * klass)
{
  GstElementClass *element_class = (GstElementClass *) klass;

  gst_element_class_set_metadata (element_class,
      "Round Robin", "Source/Network",
      "A round robin dispatcher element.",
      "Nicolas Dufresne <nicolas.dufresne@collabora.com");

  gst_element_class_add_static_pad_template (element_class, &sink_templ);
  gst_element_class_add_static_pad_template (element_class, &src_templ);

  element_class->request_new_pad =
      GST_DEBUG_FUNCPTR (gst_round_robin_request_pad);
}