volume.c 12.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* GStreamer
 *
 * unit test for volume
 *
 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <unistd.h>

25
#include <gst/base/gstbasetransform.h>
26
27
28
29
30
31
32
33
34
35
#include <gst/check/gstcheck.h>

gboolean have_eos = FALSE;

/* For ease of programming we use globals to keep refs for our floating
 * src and sink pads we create; otherwise we always have to do get_pad,
 * get_peer, and then remove references in every test function */
GstPad *mysrcpad, *mysinkpad;


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
36
37
38
39
40
41
42
#define VOLUME_CAPS_TEMPLATE_STRING     \
    "audio/x-raw-int, "                 \
    "channels = (int) [ 1, MAX ], "     \
    "rate = (int) [ 1,  MAX ], "        \
    "endianness = (int) BYTE_ORDER, "   \
    "width = (int) 16, "                \
    "depth = (int) 16, "                \
43
44
    "signed = (bool) TRUE"

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
45
46
47
48
49
50
51
#define VOLUME_CAPS_STRING              \
    "audio/x-raw-int, "                 \
    "channels = (int) 1, "              \
    "rate = (int) 44100, "              \
    "endianness = (int) BYTE_ORDER, "   \
    "width = (int) 16, "                \
    "depth = (int) 16, "                \
52
53
    "signed = (bool) TRUE"

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54
55
56
57
58
59
60
#define VOLUME_WRONG_CAPS_STRING        \
    "audio/x-raw-int, "                 \
    "channels = (int) 1, "              \
    "rate = (int) 44100, "              \
    "endianness = (int) BYTE_ORDER, "   \
    "width = (int) 16, "                \
    "depth = (int) 16, "                \
61
62
63
    "signed = (bool) FALSE"


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING)
    );
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (VOLUME_CAPS_TEMPLATE_STRING)
    );

GstElement *
setup_volume ()
{
  GstElement *volume;

  GST_DEBUG ("setup_volume");
81
82
83
  volume = gst_check_setup_element ("volume");
  mysrcpad = gst_check_setup_src_pad (volume, &srctemplate, NULL);
  mysinkpad = gst_check_setup_sink_pad (volume, &sinktemplate, NULL);
Wim Taymans's avatar
Wim Taymans committed
84
85
86
  gst_pad_set_active (mysrcpad, TRUE);
  gst_pad_set_active (mysinkpad, TRUE);

87
88
89
90
91
92
93
94
  return volume;
}

void
cleanup_volume (GstElement * volume)
{
  GST_DEBUG ("cleanup_volume");

95
96
97
98
  g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
  g_list_free (buffers);
  buffers = NULL;

99
100
  gst_pad_set_active (mysrcpad, FALSE);
  gst_pad_set_active (mysinkpad, FALSE);
101
102
103
  gst_check_teardown_src_pad (volume);
  gst_check_teardown_sink_pad (volume);
  gst_check_teardown_element (volume);
104
105
106
107
108
109
}

GST_START_TEST (test_unity)
{
  GstElement *volume;
  GstBuffer *inbuffer, *outbuffer;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
110
  GstCaps *caps;
111
  gint16 in[2] = { 16384, -256 };
112
  gint16 *res;
113
114
115

  volume = setup_volume ();
  fail_unless (gst_element_set_state (volume,
116
117
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");
118
119
120

  inbuffer = gst_buffer_new_and_alloc (4);
  memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
121
122
123
  caps = gst_caps_from_string (VOLUME_CAPS_STRING);
  gst_buffer_set_caps (inbuffer, caps);
  gst_caps_unref (caps);
124
125
126
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);

  /* pushing gives away my reference ... */
127
  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
128
129
  /* ... but it ends up being collected on the global buffer list */
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
130
  fail_unless_equals_int (g_list_length (buffers), 1);
131
132
  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
  fail_unless (inbuffer == outbuffer);
133
  res = (gint16 *) GST_BUFFER_DATA (outbuffer);
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
134
  GST_INFO ("expected %+5d %+5d  real %+5d %+5d", in[0], in[1], res[0], res[1]);
135
136
137
138
139
140
141
142
143
144
145
146
147
  fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);

  /* cleanup */
  cleanup_volume (volume);
}

GST_END_TEST;

GST_START_TEST (test_half)
{
  GstElement *volume;
  GstBuffer *inbuffer;
  GstBuffer *outbuffer;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
148
  GstCaps *caps;
149
150
  gint16 in[2] = { 16384, -256 };
  gint16 out[2] = { 8192, -128 };
151
  gint16 *res;
152
153
154
155

  volume = setup_volume ();
  g_object_set (G_OBJECT (volume), "volume", 0.5, NULL);
  fail_unless (gst_element_set_state (volume,
156
157
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");
158
159
160
161

  inbuffer = gst_buffer_new_and_alloc (4);
  memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
  fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
162
163
164
  caps = gst_caps_from_string (VOLUME_CAPS_STRING);
  gst_buffer_set_caps (inbuffer, caps);
  gst_caps_unref (caps);
165
166
167
168
169
170
171
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
  /* FIXME: reffing the inbuffer should make the transformation not be
   * inplace
   gst_buffer_ref (inbuffer);
   */

  /* pushing gives away my reference ... */
172
  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
173
174
175
  /* ... but it ends up being modified inplace and
   * collected on the global buffer list */
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
176
  fail_unless_equals_int (g_list_length (buffers), 1);
177
178
  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
  fail_unless (inbuffer == outbuffer);
179
  res = (gint16 *) GST_BUFFER_DATA (outbuffer);
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
180
  GST_INFO ("expected %+5d %+5d  real %+5d %+5d", out[0], out[1], res[0],
181
      res[1]);
182
183
184
185
186
187
188
189
190
191
192
193
194
  fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0);

  /* cleanup */
  cleanup_volume (volume);
}

GST_END_TEST;

GST_START_TEST (test_double)
{
  GstElement *volume;
  GstBuffer *inbuffer;
  GstBuffer *outbuffer;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
195
  GstCaps *caps;
196
197
  gint16 in[2] = { 16384, -256 };
  gint16 out[2] = { 32767, -512 };      /* notice the clamped sample */
198
  gint16 *res;
199
200
201
202

  volume = setup_volume ();
  g_object_set (G_OBJECT (volume), "volume", 2.0, NULL);
  fail_unless (gst_element_set_state (volume,
203
204
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");
205
206
207
208

  inbuffer = gst_buffer_new_and_alloc (4);
  memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
  fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
209
210
211
  caps = gst_caps_from_string (VOLUME_CAPS_STRING);
  gst_buffer_set_caps (inbuffer, caps);
  gst_caps_unref (caps);
212
213
214
215
216
217
218
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
  /* FIXME: reffing the inbuffer should make the transformation not be
   * inplace
   gst_buffer_ref (inbuffer);
   */

  /* pushing gives away my reference ... */
219
  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
220
221
222
  /* ... but it ends up being modified inplace and
   * collected on the global buffer list */
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
223
  fail_unless_equals_int (g_list_length (buffers), 1);
224
225
  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
  fail_unless (inbuffer == outbuffer);
226
  res = (gint16 *) GST_BUFFER_DATA (outbuffer);
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
227
  GST_INFO ("expected %+5d %+5d  real %+5d %+5d", out[0], out[1], res[0],
228
      res[1]);
229
230
231
232
233
234
235
236
  fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0);

  /* cleanup */
  cleanup_volume (volume);
}

GST_END_TEST;

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

GST_START_TEST (test_mute)
{
  GstElement *volume;
  GstBuffer *inbuffer;
  GstBuffer *outbuffer;
  GstCaps *caps;
  gint16 in[2] = { 16384, -256 };
  gint16 out[2] = { 0, 0 };
  gint16 *res;

  volume = setup_volume ();
  g_object_set (G_OBJECT (volume), "mute", TRUE, NULL);
  fail_unless (gst_element_set_state (volume,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");

  inbuffer = gst_buffer_new_and_alloc (4);
  memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
  fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);
  caps = gst_caps_from_string (VOLUME_CAPS_STRING);
  gst_buffer_set_caps (inbuffer, caps);
  gst_caps_unref (caps);
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
  /* FIXME: reffing the inbuffer should make the transformation not be
   * inplace
   gst_buffer_ref (inbuffer);
   */

  /* pushing gives away my reference ... */
  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
  /* ... but it ends up being modified inplace and
   * collected on the global buffer list */
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
  fail_unless_equals_int (g_list_length (buffers), 1);
  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
  fail_unless (inbuffer == outbuffer);
  res = (gint16 *) GST_BUFFER_DATA (outbuffer);
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
275
  GST_INFO ("expected %+5d %+5d  real %+5d %+5d", out[0], out[1], res[0],
276
277
278
279
280
281
282
283
284
285
      res[1]);
  fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, 4) == 0);

  /* cleanup */
  cleanup_volume (volume);
}

GST_END_TEST;


286
287
288
GST_START_TEST (test_wrong_caps)
{
  GstElement *volume;
289
  GstBuffer *inbuffer;
290
291
292
  gint16 in[2] = { 16384, -256 };
  GstBus *bus;
  GstMessage *message;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
293
  GstCaps *caps;
294
295
296
297
298

  volume = setup_volume ();
  bus = gst_bus_new ();

  fail_unless (gst_element_set_state (volume,
299
300
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");
301
302
303

  inbuffer = gst_buffer_new_and_alloc (4);
  memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
304
305
306
  caps = gst_caps_from_string (VOLUME_WRONG_CAPS_STRING);
  gst_buffer_set_caps (inbuffer, caps);
  gst_caps_unref (caps);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
  gst_buffer_ref (inbuffer);

  /* set a bus here so we avoid getting state change messages */
  gst_element_set_bus (volume, bus);

  /* pushing gives an error because it can't negotiate with wrong caps */
  fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer),
      GST_FLOW_NOT_NEGOTIATED);
  /* ... and the buffer would have been lost if we didn't ref it ourselves */
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
  gst_buffer_unref (inbuffer);
  fail_unless_equals_int (g_list_length (buffers), 0);

  /* volume_set_caps should not have been called since basetransform caught
   * the negotiation problem */
  fail_if ((message = gst_bus_pop (bus)) != NULL);

  /* cleanup */
  gst_element_set_bus (volume, NULL);
  gst_object_unref (GST_OBJECT (bus));
  cleanup_volume (volume);
}

GST_END_TEST;

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
GST_START_TEST (test_passthrough)
{
  GstElement *volume;
  GstBuffer *inbuffer, *outbuffer;
  GstCaps *caps;
  gint16 in[2] = { 16384, -256 };
  gint16 *res;

  volume = setup_volume ();
  g_object_set (G_OBJECT (volume), "volume", 2.0, NULL);
  gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (volume), TRUE);
  fail_unless (gst_element_set_state (volume,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");

  inbuffer = gst_buffer_new_and_alloc (4);
  memcpy (GST_BUFFER_DATA (inbuffer), in, 4);
  caps = gst_caps_from_string (VOLUME_CAPS_STRING);
  gst_buffer_set_caps (inbuffer, caps);
  gst_caps_unref (caps);
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);

  /* pushing gives away my reference ... */
  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
  /* ... but it ends up being collected on the global buffer list */
  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
  fail_unless_equals_int (g_list_length (buffers), 1);
  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
  fail_unless (inbuffer == outbuffer);
  res = (gint16 *) GST_BUFFER_DATA (outbuffer);
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
363
  GST_INFO ("expected %+5d %+5d  real %+5d %+5d", in[0], in[1], res[0], res[1]);
364
365
366
367
368
369
370
371
  fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, 4) == 0);

  /* cleanup */
  cleanup_volume (volume);
}

GST_END_TEST;

372
373
374
375
376
377
378
379
380
381
Suite *
volume_suite (void)
{
  Suite *s = suite_create ("volume");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_unity);
  tcase_add_test (tc_chain, test_half);
  tcase_add_test (tc_chain, test_double);
382
  tcase_add_test (tc_chain, test_mute);
383
  tcase_add_test (tc_chain, test_wrong_caps);
384
  tcase_add_test (tc_chain, test_passthrough);
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

  return s;
}

int
main (int argc, char **argv)
{
  int nf;

  Suite *s = volume_suite ();
  SRunner *sr = srunner_create (s);

  gst_check_init (&argc, &argv);

  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}