Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / tests / check / pipelines / flacdec.c
1 /* GStreamer
2  * Copyright (C) 2009 Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21 #include <gst/audio/audio.h>
22 #include <glib/gstdio.h>
23
24 static guint16
25 _get_first_sample (GstSample * sample)
26 {
27   GstAudioInfo info;
28   GstCaps *caps;
29   GstBuffer *buf;
30   guint8 *data;
31   gsize size;
32   guint16 res;
33
34   fail_unless (sample != NULL, "NULL sample");
35
36   caps = gst_sample_get_caps (sample);
37   fail_unless (caps != NULL, "sample without caps");
38
39   buf = gst_sample_get_buffer (sample);
40   GST_DEBUG ("buffer with size=%u, caps=%" GST_PTR_FORMAT,
41       gst_buffer_get_size (buf), caps);
42
43   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
44   /* log buffer details */
45   GST_MEMDUMP ("buffer data from decoder", data, size);
46
47   /* make sure it's the format we expect */
48   fail_unless (gst_audio_info_from_caps (&info, caps));
49
50   fail_unless_equals_int (GST_AUDIO_INFO_WIDTH (&info), 16);
51   fail_unless_equals_int (GST_AUDIO_INFO_DEPTH (&info), 16);
52   fail_unless_equals_int (GST_AUDIO_INFO_RATE (&info), 44100);
53   fail_unless_equals_int (GST_AUDIO_INFO_CHANNELS (&info), 1);
54
55   if (GST_AUDIO_INFO_IS_LITTLE_ENDIAN (&info))
56     res = GST_READ_UINT16_LE (data);
57   else
58     res = GST_READ_UINT16_BE (data);
59
60   gst_buffer_unmap (buf, data, size);
61
62   return res;
63 }
64
65 GST_START_TEST (test_decode)
66 {
67   GstElement *pipeline;
68   GstElement *appsink;
69   GstSample *sample = NULL;
70   guint16 first_sample = 0;
71   guint size = 0;
72   gchar *path =
73       g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
74   gchar *pipe_desc =
75       g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
76       path);
77
78   pipeline = gst_parse_launch (pipe_desc, NULL);
79   fail_unless (pipeline != NULL);
80
81   g_free (path);
82   g_free (pipe_desc);
83
84   appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
85   fail_unless (appsink != NULL);
86
87   gst_element_set_state (pipeline, GST_STATE_PLAYING);
88
89   do {
90     g_signal_emit_by_name (appsink, "pull-sample", &sample);
91     if (sample == NULL)
92       break;
93     if (first_sample == 0)
94       first_sample = _get_first_sample (sample);
95
96     size += gst_buffer_get_size (gst_sample_get_buffer (sample));
97
98     gst_sample_unref (sample);
99     sample = NULL;
100   }
101   while (TRUE);
102
103   /* audiotestsrc with samplesperbuffer 1024 and 10 num-buffers */
104   fail_unless_equals_int (size, 20480);
105   fail_unless_equals_int (first_sample, 0x066a);
106
107   gst_element_set_state (pipeline, GST_STATE_NULL);
108   g_object_unref (pipeline);
109   g_object_unref (appsink);
110 }
111
112 GST_END_TEST;
113
114 GST_START_TEST (test_decode_seek_full)
115 {
116   GstElement *pipeline;
117   GstElement *appsink;
118   GstEvent *event;
119   GstSample *sample = NULL;
120   guint16 first_sample = 0;
121   guint size = 0;
122   gchar *path =
123       g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
124   gchar *pipe_desc =
125       g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
126       path);
127
128   pipeline = gst_parse_launch (pipe_desc, NULL);
129   fail_unless (pipeline != NULL);
130
131   g_free (pipe_desc);
132   g_free (path);
133
134   appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
135   fail_unless (appsink != NULL);
136
137   gst_element_set_state (pipeline, GST_STATE_PAUSED);
138   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
139
140   /* do a seek that should give us the complete output */
141   event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
142       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 20480);
143   fail_unless (gst_element_send_event (appsink, event));
144
145   gst_element_set_state (pipeline, GST_STATE_PLAYING);
146
147   do {
148     g_signal_emit_by_name (appsink, "pull-sample", &sample);
149     if (sample == NULL)
150       break;
151     if (first_sample == 0)
152       first_sample = _get_first_sample (sample);
153     size += gst_buffer_get_size (gst_sample_get_buffer (sample));
154
155     gst_sample_unref (sample);
156     sample = NULL;
157   }
158   while (TRUE);
159
160   /* file was generated with audiotestsrc
161    * with 1024 samplesperbuffer and 10 num-buffers in 16 bit audio */
162   fail_unless_equals_int (size, 20480);
163   fail_unless_equals_int (first_sample, 0x066a);
164
165   gst_element_set_state (pipeline, GST_STATE_NULL);
166
167   g_object_unref (pipeline);
168   g_object_unref (appsink);
169 }
170
171 GST_END_TEST;
172
173 GST_START_TEST (test_decode_seek_partial)
174 {
175   GstElement *pipeline;
176   GstElement *appsink;
177   GstEvent *event;
178   GstSample *sample = NULL;
179   guint size = 0;
180   guint16 first_sample = 0;
181   gchar *path =
182       g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
183   gchar *pipe_desc =
184       g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
185       path);
186
187   pipeline = gst_parse_launch (pipe_desc, NULL);
188   fail_unless (pipeline != NULL);
189
190   g_free (path);
191   g_free (pipe_desc);
192
193   appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
194   fail_unless (appsink != NULL);
195
196   gst_element_set_state (pipeline, GST_STATE_PAUSED);
197   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
198
199   /* do a partial seek to get the first 1024 samples or 2048 bytes */
200   event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
201       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 1024);
202   GST_DEBUG ("seeking");
203   fail_unless (gst_element_send_event (appsink, event));
204   GST_DEBUG ("seeked");
205
206   gst_element_set_state (pipeline, GST_STATE_PLAYING);
207
208   do {
209     GST_DEBUG ("pulling sample");
210     g_signal_emit_by_name (appsink, "pull-sample", &sample);
211     GST_DEBUG ("pulled sample %p", sample);
212     if (sample == NULL)
213       break;
214     if (first_sample == 0) {
215 //      fail_unless_equals_int (GST_BUFFER_OFFSET (buffer), 0L);
216       first_sample = _get_first_sample (sample);
217     }
218     size += gst_buffer_get_size (gst_sample_get_buffer (sample));
219
220     gst_sample_unref (sample);
221     sample = NULL;
222   }
223   while (TRUE);
224
225   fail_unless_equals_int (size, 2048);
226   fail_unless_equals_int (first_sample, 0x066a);
227
228   gst_element_set_state (pipeline, GST_STATE_NULL);
229
230   g_object_unref (pipeline);
231   g_object_unref (appsink);
232 }
233
234 GST_END_TEST;
235
236
237 static Suite *
238 flacdec_suite (void)
239 {
240   Suite *s = suite_create ("flacdec");
241
242   TCase *tc_chain = tcase_create ("linear");
243
244   /* time out after 60s, not the default 3 */
245   tcase_set_timeout (tc_chain, 60);
246
247   suite_add_tcase (s, tc_chain);
248   tcase_add_test (tc_chain, test_decode);
249   tcase_add_test (tc_chain, test_decode_seek_full);
250   tcase_add_test (tc_chain, test_decode_seek_partial);
251
252   return s;
253 }
254
255 GST_CHECK_MAIN (flacdec);