appsink.c 9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* GStreamer
 *
 * Copyright (C) 2009, Axis Communications AB, LUND, SWEDEN
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17
18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19
20
21
 */

#include <gst/check/gstcheck.h>
22
#include <gst/app/gstappsink.h>
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

gint global_testdata;

static GstPad *mysrcpad;

static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-gst-check")
    );

static GstElement *
setup_appsink (void)
{
  GstElement *appsink;

  GST_DEBUG ("setup_appsink");
  appsink = gst_check_setup_element ("appsink");
41
  mysrcpad = gst_check_setup_src_pad (appsink, &srctemplate);
Wim Taymans's avatar
Wim Taymans committed
42
  gst_pad_set_active (mysrcpad, TRUE);
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

  return appsink;
}

static void
cleanup_appsink (GstElement * appsink)
{
  GST_DEBUG ("cleanup_appsink");

  gst_check_teardown_src_pad (appsink);
  gst_check_teardown_element (appsink);
}

/* This function does an operation to it's indata argument and returns it.
 * The exact operation performed doesn't matter. Currently it multiplies with
 * two, but it could do anything. The idea is to use the function to verify
 * that the code calling it gets run. */
60
static gint
61
62
63
64
65
operate_on_data (gint indata)
{
  return indata * 2;
}

Wim Taymans's avatar
Wim Taymans committed
66
static GstFlowReturn
67
68
69
callback_function (GstAppSink * appsink, gpointer callback_data)
{
  global_testdata = operate_on_data (*((gint *) callback_data));
Wim Taymans's avatar
Wim Taymans committed
70
71

  return GST_FLOW_OK;
72
73
}

74
static void
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
notify_function (gpointer callback_data)
{
  global_testdata = operate_on_data (*((gint *) callback_data));
}

GST_START_TEST (test_non_clients)
{
  GstElement *sink;
  GstBuffer *buffer;
  GstCaps *caps;

  sink = setup_appsink ();

  ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);

  caps = gst_caps_from_string ("application/x-gst-check");
  buffer = gst_buffer_new_and_alloc (4);
92
  gst_pad_set_caps (mysrcpad, caps);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  gst_caps_unref (caps);
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);

  GST_DEBUG ("cleaning up appsink");
  ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
  cleanup_appsink (sink);
}

GST_END_TEST;

/* Verifies that the handoff callback gets run one time when passing a buffer */
GST_START_TEST (test_handoff_callback)
{
  GstElement *sink;
  GstBuffer *buffer;
  GstCaps *caps;
  gint testdata;
  GstAppSinkCallbacks callbacks = { NULL };

  sink = setup_appsink ();

  global_testdata = 0;
  testdata = 5;                 /* Arbitrary value */

117
  callbacks.new_sample = callback_function;
118
119
120
121
122
123
124

  gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);

  ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);

  caps = gst_caps_from_string ("application/x-gst-check");
  buffer = gst_buffer_new_and_alloc (4);
125
  gst_pad_set_caps (mysrcpad, caps);
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
  gst_caps_unref (caps);
  /* Pushing a buffer should run our callback */
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);

  testdata = operate_on_data (testdata);

  /* If both test_data & global_testdata have been operated on, we're happy. */
  fail_unless (testdata == global_testdata);

  GST_DEBUG ("cleaning up appsink");
  ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
  cleanup_appsink (sink);
}

GST_END_TEST;

/* Verifies that the notify function gets executed when the sink is destroyed */
GST_START_TEST (test_notify0)
{
  GstElement *sink;
  gint testdata;
  GstAppSinkCallbacks callbacks = { NULL };

  sink = gst_element_factory_make ("appsink", NULL);

  global_testdata = 0;
  testdata = 17;                /* Arbitrary value */

  gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
      &testdata, (*notify_function));

  GST_DEBUG ("cleaning up appsink");
  /* Destroying sink should call our notify_function */
  gst_object_unref (sink);

  testdata = operate_on_data (testdata);

  /* If both test_data & global_testdata have been operated on, we're happy. */
  fail_unless (testdata == global_testdata);
}

GST_END_TEST;


/* Verifies that the notify function gets executed when
 * gst_app_sink_set_callbacks () gets called */
GST_START_TEST (test_notify1)
{
  GstElement *sink;
  gint testdata;
  GstAppSinkCallbacks callbacks = { NULL };

  sink = gst_element_factory_make ("appsink", NULL);

  global_testdata = 0;
  testdata = 42;                /* Arbitrary value */

  gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
      &testdata, (*notify_function));
185
  /* Setting new callbacks should trigger the destroy of the old data */
186
187
188
189
190
191
192
193
194
195
196
197
198
  gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);

  testdata = operate_on_data (testdata);

  /* If both test_data & global_testdata have been operated on, we're happy. */
  fail_unless (testdata == global_testdata);

  GST_DEBUG ("cleaning up appsink");
  gst_object_unref (sink);
}

GST_END_TEST;

199
200
201
static GstBufferList *mylist;
static GstCaps *mycaps;

202
203
static gint values[] = { 1, 2, 4 };

204
205
206
static GstBufferList *
create_buffer_list (void)
{
Wim Taymans's avatar
Wim Taymans committed
207
  guint len;
208
209
210
211
212
213
214
215
  GstBuffer *buffer;

  mylist = gst_buffer_list_new ();
  fail_if (mylist == NULL);

  mycaps = gst_caps_from_string ("application/x-gst-check");
  fail_if (mycaps == NULL);

216
  len = gst_buffer_list_length (mylist);
Wim Taymans's avatar
Wim Taymans committed
217
  fail_if (len != 0);
218
219

  buffer = gst_buffer_new_and_alloc (sizeof (gint));
220
  gst_buffer_fill (buffer, 0, &values[0], sizeof (gint));
Wim Taymans's avatar
Wim Taymans committed
221
  gst_buffer_list_add (mylist, buffer);
222
223

  buffer = gst_buffer_new_and_alloc (sizeof (gint));
224
  gst_buffer_fill (buffer, 0, &values[1], sizeof (gint));
Wim Taymans's avatar
Wim Taymans committed
225
  gst_buffer_list_add (mylist, buffer);
226
227

  buffer = gst_buffer_new_and_alloc (sizeof (gint));
228
  gst_buffer_fill (buffer, 0, &values[2], sizeof (gint));
Wim Taymans's avatar
Wim Taymans committed
229
  gst_buffer_list_add (mylist, buffer);
230

231
232
  gst_pad_set_caps (mysrcpad, mycaps);
  gst_caps_unref (mycaps);
233

234
  return mylist;
235
236
}

237
static GstFlowReturn
238
callback_function_sample (GstAppSink * appsink, gpointer p_counter)
239
{
240
  GstSample *sample;
241
242
243
  GstBuffer *buf;
  gint *p_int_counter = p_counter;

244
245
  sample = gst_app_sink_pull_sample (appsink);
  buf = gst_sample_get_buffer (sample);
246
247
248
249
250
  fail_unless (GST_IS_BUFFER (buf));

  /* buffer list has 3 buffers in two groups */
  switch (*p_int_counter) {
    case 0:
251
252
      fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (gint));
      gst_check_buffer_data (buf, &values[0], sizeof (gint));
253
254
      break;
    case 1:
Wim Taymans's avatar
Wim Taymans committed
255
256
257
258
259
260
      fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (gint));
      gst_check_buffer_data (buf, &values[1], sizeof (gint));
      break;
    case 2:
      fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (gint));
      gst_check_buffer_data (buf, &values[2], sizeof (gint));
261
262
263
264
265
266
      break;
    default:
      g_warn_if_reached ();
      break;
  }

267
  gst_sample_unref (sample);
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

  *p_int_counter += 1;

  return GST_FLOW_OK;
}

GST_START_TEST (test_buffer_list_fallback)
{
  GstElement *sink;
  GstBufferList *list;
  GstAppSinkCallbacks callbacks = { NULL };
  gint counter = 0;

  sink = setup_appsink ();

283
  callbacks.new_sample = callback_function_sample;
284
285
286
287
288
289
290
291

  gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &counter, NULL);

  ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);

  list = create_buffer_list ();
  fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);

Wim Taymans's avatar
Wim Taymans committed
292
  fail_unless_equals_int (counter, 3);
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

  ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
  cleanup_appsink (sink);
}

GST_END_TEST;

GST_START_TEST (test_buffer_list_fallback_signal)
{
  GstElement *sink;
  GstBufferList *list;
  gint counter = 0;

  sink = setup_appsink ();

  /* C calling convention to the rescue.. */
309
  g_signal_connect (sink, "new-sample", G_CALLBACK (callback_function_sample),
310
311
312
313
314
315
316
317
318
      &counter);

  g_object_set (sink, "emit-signals", TRUE, NULL);

  ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);

  list = create_buffer_list ();
  fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);

Wim Taymans's avatar
Wim Taymans committed
319
  fail_unless_equals_int (counter, 3);
320
321
322
323
324
325
326

  ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
  cleanup_appsink (sink);
}

GST_END_TEST;

327
328
329
330
331
332
333
334
335
336
337
static Suite *
appsink_suite (void)
{
  Suite *s = suite_create ("appsink");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_non_clients);
  tcase_add_test (tc_chain, test_handoff_callback);
  tcase_add_test (tc_chain, test_notify0);
  tcase_add_test (tc_chain, test_notify1);
338
339
  tcase_add_test (tc_chain, test_buffer_list_fallback);
  tcase_add_test (tc_chain, test_buffer_list_fallback_signal);
340
341
342
343

  return s;
}

344
GST_CHECK_MAIN (appsink);