aboutsummaryrefslogtreecommitdiff
path: root/tests/check/elements/udpsink.c
blob: 0e938eef1309409037672154a4edcd4a2440a059 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* GStreamer RTP payloader unit tests
 * Copyright (C) 2009 Axis Communications <dev-gstreamer@axis.com>
 * @author Ognyan Tonchev <ognyan@axis.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.
 */
#include <gst/check/gstcheck.h>
#include <gst/base/gstbasesink.h>
#include <stdlib.h>

static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

#define RTP_HEADER_SIZE 12
#define RTP_PAYLOAD_SIZE 1024

/*
 * Number of bytes received in the render function when using buffer lists
 */
static guint render_list_bytes_received;

/*
 * Render function for testing udpsink with buffer lists
 */
static GstFlowReturn
udpsink_render_list (GstBaseSink * sink, GstBufferList * list)
{
  guint i, num;

  num = gst_buffer_list_length (list);
  for (i = 0; i < num; ++i) {
    GstBuffer *buf = gst_buffer_list_get (list, i);
    gsize size = gst_buffer_get_size (buf);

    GST_DEBUG ("rendered %" G_GSIZE_FORMAT " bytes", size);
    render_list_bytes_received += size;
  }

  return GST_FLOW_OK;
}

static void
set_render_list_function (GstElement * bsink)
{
  GstBaseSinkClass *bsclass;

  bsclass = GST_BASE_SINK_GET_CLASS ((GstBaseSink *) bsink);

  /* Add callback function for the buffer list tests */
  bsclass->render_list = udpsink_render_list;
}

static GstBufferList *
create_buffer_list (guint * data_size)
{
  GstBufferList *list;
  GstBuffer *rtp_buffer;
  GstBuffer *data_buffer;

  list = gst_buffer_list_new ();

  /*** First group, i.e. first packet. **/

  /* Create the RTP header buffer */
  rtp_buffer = gst_buffer_new_allocate (NULL, RTP_HEADER_SIZE, NULL);
  gst_buffer_memset (rtp_buffer, 0, 0, RTP_HEADER_SIZE);

  /* Create the buffer that holds the payload */
  data_buffer = gst_buffer_new_allocate (NULL, RTP_PAYLOAD_SIZE, NULL);
  gst_buffer_memset (data_buffer, 0, 0, RTP_PAYLOAD_SIZE);

  /* Create a new group to hold the rtp header and the payload */
  gst_buffer_list_add (list, gst_buffer_append (rtp_buffer, data_buffer));

  /***  Second group, i.e. second packet. ***/

  /* Create the RTP header buffer */
  rtp_buffer = gst_buffer_new_allocate (NULL, RTP_HEADER_SIZE, NULL);
  gst_buffer_memset (rtp_buffer, 0, 0, RTP_HEADER_SIZE);

  /* Create the buffer that holds the payload */
  data_buffer = gst_buffer_new_allocate (NULL, RTP_PAYLOAD_SIZE, NULL);
  gst_buffer_memset (data_buffer, 0, 0, RTP_PAYLOAD_SIZE);

  /* Create a new group to hold the rtp header and the payload */
  gst_buffer_list_add (list, gst_buffer_append (rtp_buffer, data_buffer));

  /* Calculate the size of the data */
  *data_size = 2 * RTP_HEADER_SIZE + 2 * RTP_PAYLOAD_SIZE;

  return list;
}

static void
udpsink_test (gboolean use_buffer_lists)
{
  GstSegment segment;
  GstElement *udpsink;
  GstPad *srcpad;
  GstBufferList *list;
  guint data_size;

  list = create_buffer_list (&data_size);

  udpsink = gst_check_setup_element ("udpsink");
  if (use_buffer_lists)
    set_render_list_function (udpsink);

  srcpad = gst_check_setup_src_pad_by_name (udpsink, &srctemplate, "sink");

  gst_element_set_state (udpsink, GST_STATE_PLAYING);
  gst_pad_set_active (srcpad, TRUE);

  gst_pad_push_event (srcpad, gst_event_new_stream_start ("hey there!"));

  gst_segment_init (&segment, GST_FORMAT_TIME);
  gst_pad_push_event (srcpad, gst_event_new_segment (&segment));

  fail_unless_equals_int (gst_pad_push_list (srcpad, list), GST_FLOW_OK);

  gst_check_teardown_pad_by_name (udpsink, "sink");
  gst_check_teardown_element (udpsink);

  if (use_buffer_lists)
    fail_unless_equals_int (data_size, render_list_bytes_received);
}

GST_START_TEST (test_udpsink)
{
  udpsink_test (FALSE);
}

GST_END_TEST;


GST_START_TEST (test_udpsink_bufferlist)
{
  udpsink_test (TRUE);
}

GST_END_TEST;

GST_START_TEST (test_udpsink_client_add_remove)
{
  GstElement *udpsink;

  /* Note: keep in mind that these are in addition to the client added by
   * the host/port properties (by default 'localhost:5004' */

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "remove", "localhost", 5004, NULL);
  gst_object_unref (udpsink);

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  gst_object_unref (udpsink);

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  gst_object_unref (udpsink);

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  g_signal_emit_by_name (udpsink, "remove", "127.0.0.1", 5554, NULL);
  gst_object_unref (udpsink);

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  g_signal_emit_by_name (udpsink, "remove", "127.0.0.1", 5555, NULL);
  gst_object_unref (udpsink);

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5555, NULL);
  gst_object_unref (udpsink);

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  g_signal_emit_by_name (udpsink, "add", "10.2.0.1", 5554, NULL);
  gst_object_unref (udpsink);

  udpsink = gst_check_setup_element ("udpsink");
  g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
  g_signal_emit_by_name (udpsink, "add", "10.2.0.1", 5554, NULL);
  g_signal_emit_by_name (udpsink, "remove", "127.0.0.1", 5554, NULL);
  gst_object_unref (udpsink);
}

GST_END_TEST;

static Suite *
udpsink_suite (void)
{
  Suite *s = suite_create ("udpsink_test");
  TCase *tc_chain = tcase_create ("linear");

  suite_add_tcase (s, tc_chain);

  tcase_add_test (tc_chain, test_udpsink);
  tcase_add_test (tc_chain, test_udpsink_bufferlist);
  tcase_add_test (tc_chain, test_udpsink_client_add_remove);

  return s;
}

GST_CHECK_MAIN (udpsink)