summaryrefslogtreecommitdiffstats
path: root/tests/check/pipelines/flacdec.c
blob: 2f2010fbec3812ff02271fda96c16846cf2e085d (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
/* GStreamer
 * Copyright (C) 2009 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 <gst/check/gstcheck.h>

GST_START_TEST (test_decode)
{
  GstElement *pipeline;

  GstElement *appsink;

  GstBuffer *buffer = NULL;

  guint8 firstbyte = 0;
  guint size = 0;

  pipeline = gst_parse_launch ("filesrc location=audiotestsrc.flac"
      " ! flacdec ! appsink name=sink", NULL);
  fail_unless (pipeline != NULL);

  appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
  fail_unless (appsink != NULL);

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  do {
    g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
    if (buffer == NULL)
      break;
    if (firstbyte == 0)
      firstbyte = GST_BUFFER_DATA (buffer)[0];
    g_print ("buffer: %d\n", buffer->size);
    g_print ("buffer: %08x\n", GST_BUFFER_DATA (buffer)[0]);
    size += buffer->size;
  }
  while (TRUE);

  /* audiotestsrc with samplesperbuffer 1024 and 10 num-buffers */
  fail_unless_equals_int (size, 20480);
  fail_unless_equals_int (firstbyte, 0x6a);

  gst_element_set_state (pipeline, GST_STATE_NULL);
  g_object_unref (pipeline);
}

GST_END_TEST;

GST_START_TEST (test_decode_seek_full)
{
  GstElement *pipeline;

  GstElement *appsink;

  GstEvent *event;

  GstBuffer *buffer = NULL;

  guint8 firstbyte = 0;

  gboolean result;
  guint size = 0;

  pipeline = gst_parse_launch ("filesrc location=audiotestsrc.flac"
      " ! flacdec ! appsink name=sink", NULL);
  fail_unless (pipeline != NULL);

  appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
  fail_unless (appsink != NULL);

  gst_element_set_state (pipeline, GST_STATE_PAUSED);
  gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);

  /* do a seek that should give us the complete output */
  event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 20480);
  result = gst_element_send_event (appsink, event);

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  do {
    g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
    if (buffer == NULL)
      break;
    if (firstbyte == 0)
      firstbyte = GST_BUFFER_DATA (buffer)[0];
    size += buffer->size;
  }
  while (TRUE);

  /* file was generated with audiotestsrc
   * with 1024 samplesperbuffer and 10 num-buffers in 16 bit audio */
  fail_unless_equals_int (size, 20480);
  fail_unless_equals_int (firstbyte, 0x6a);

  gst_element_set_state (pipeline, GST_STATE_NULL);

  g_object_unref (pipeline);
}

GST_END_TEST;

GST_START_TEST (test_decode_seek_partial)
{
  GstElement *pipeline;

  GstElement *appsink;

  GstEvent *event;

  GstBuffer *buffer = NULL;

  gboolean result;
  guint size = 0;
  guint8 firstbyte = 0;

  pipeline = gst_parse_launch ("filesrc location=audiotestsrc.flac"
      " ! flacdec ! appsink name=sink", NULL);
  fail_unless (pipeline != NULL);

  appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
  fail_unless (appsink != NULL);

  gst_element_set_state (pipeline, GST_STATE_PAUSED);
  gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);

  /* do a partial seek to get the first 1024 samples or 2048 bytes */
  event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 1024);
  GST_DEBUG ("seeking");
  result = gst_element_send_event (appsink, event);
  GST_DEBUG ("seeked");

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  do {
    GST_DEBUG ("pulling buffer");
    g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
    GST_DEBUG ("pulled buffer %x", buffer);
    if (buffer == NULL)
      break;
    if (firstbyte == 0) {
      fail_unless_equals_int (GST_BUFFER_OFFSET (buffer), 0L);
      firstbyte = GST_BUFFER_DATA (buffer)[0];
    }
    size += buffer->size;
  }
  while (TRUE);

  fail_unless_equals_int (size, 2048);
  fail_unless_equals_int (firstbyte, 0x6a);

  gst_element_set_state (pipeline, GST_STATE_NULL);

  g_object_unref (pipeline);
}

GST_END_TEST;


Suite *
flacdec_suite (void)
{
  Suite *s = suite_create ("flacdec");

  TCase *tc_chain = tcase_create ("linear");

  /* time out after 60s, not the default 3 */
  tcase_set_timeout (tc_chain, 60);

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_decode);
  tcase_add_test (tc_chain, test_decode_seek_full);
  tcase_add_test (tc_chain, test_decode_seek_partial);

  return s;
}

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

  Suite *s = flacdec_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;
}