Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 <glib/gstdio.h>
22
23 static guint16
24 buffer_get_first_sample (GstBuffer * buf)
25 {
26   GstStructure *s;
27   gint w, d, c, r, e;
28
29   fail_unless (buf != NULL, "NULL buffer");
30   fail_unless (GST_BUFFER_CAPS (buf) != NULL, "buffer without caps");
31
32   /* log buffer details */
33   GST_DEBUG ("buffer with size=%u, caps=%" GST_PTR_FORMAT,
34       GST_BUFFER_SIZE (buf), GST_BUFFER_CAPS (buf));
35   GST_MEMDUMP ("buffer data from decoder", GST_BUFFER_DATA (buf),
36       GST_BUFFER_SIZE (buf));
37
38   /* make sure it's the format we expect */
39   s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
40   fail_unless_equals_string (gst_structure_get_name (s), "audio/x-raw-int");
41   fail_unless (gst_structure_get_int (s, "width", &w));
42   fail_unless_equals_int (w, 16);
43   fail_unless (gst_structure_get_int (s, "depth", &d));
44   fail_unless_equals_int (d, 16);
45   fail_unless (gst_structure_get_int (s, "rate", &r));
46   fail_unless_equals_int (r, 44100);
47   fail_unless (gst_structure_get_int (s, "channels", &c));
48   fail_unless_equals_int (c, 1);
49   fail_unless (gst_structure_get_int (s, "endianness", &e));
50   if (e == G_BIG_ENDIAN)
51     return GST_READ_UINT16_BE (GST_BUFFER_DATA (buf));
52   else
53     return GST_READ_UINT16_LE (GST_BUFFER_DATA (buf));
54 }
55
56 GST_START_TEST (test_decode)
57 {
58   GstElement *pipeline;
59   GstElement *appsink;
60   GstBuffer *buffer = NULL;
61   guint16 first_sample = 0;
62   guint size = 0;
63   gchar *path =
64       g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
65   gchar *pipe_desc =
66       g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
67       path);
68
69   pipeline = gst_parse_launch (pipe_desc, NULL);
70   fail_unless (pipeline != NULL);
71
72   g_free (path);
73   g_free (pipe_desc);
74
75   appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
76   fail_unless (appsink != NULL);
77
78   gst_element_set_state (pipeline, GST_STATE_PLAYING);
79
80   do {
81     g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
82     if (buffer == NULL)
83       break;
84     if (first_sample == 0)
85       first_sample = buffer_get_first_sample (buffer);
86     GST_DEBUG ("buffer: %d\n", buffer->size);
87     GST_DEBUG ("buffer: %04x\n", buffer_get_first_sample (buffer));
88     size += buffer->size;
89
90     gst_buffer_unref (buffer);
91     buffer = NULL;
92   }
93   while (TRUE);
94
95   /* audiotestsrc with samplesperbuffer 1024 and 10 num-buffers */
96   fail_unless_equals_int (size, 20480);
97   fail_unless_equals_int (first_sample, 0x066a);
98
99   gst_element_set_state (pipeline, GST_STATE_NULL);
100   g_object_unref (pipeline);
101   g_object_unref (appsink);
102 }
103
104 GST_END_TEST;
105
106 GST_START_TEST (test_decode_seek_full)
107 {
108   GstElement *pipeline;
109   GstElement *appsink;
110   GstEvent *event;
111   GstBuffer *buffer = NULL;
112   guint16 first_sample = 0;
113   guint size = 0;
114   gchar *path =
115       g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
116   gchar *pipe_desc =
117       g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
118       path);
119
120   pipeline = gst_parse_launch (pipe_desc, NULL);
121   fail_unless (pipeline != NULL);
122
123   g_free (pipe_desc);
124   g_free (path);
125
126   appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
127   fail_unless (appsink != NULL);
128
129   gst_element_set_state (pipeline, GST_STATE_PAUSED);
130   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
131
132   /* do a seek that should give us the complete output */
133   event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
134       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 20480);
135   fail_unless (gst_element_send_event (appsink, event));
136
137   gst_element_set_state (pipeline, GST_STATE_PLAYING);
138
139   do {
140     g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
141     if (buffer == NULL)
142       break;
143     if (first_sample == 0)
144       first_sample = buffer_get_first_sample (buffer);
145     size += buffer->size;
146
147     gst_buffer_unref (buffer);
148     buffer = NULL;
149   }
150   while (TRUE);
151
152   /* file was generated with audiotestsrc
153    * with 1024 samplesperbuffer and 10 num-buffers in 16 bit audio */
154   fail_unless_equals_int (size, 20480);
155   fail_unless_equals_int (first_sample, 0x066a);
156
157   gst_element_set_state (pipeline, GST_STATE_NULL);
158
159   g_object_unref (pipeline);
160   g_object_unref (appsink);
161 }
162
163 GST_END_TEST;
164
165 GST_START_TEST (test_decode_seek_partial)
166 {
167   GstElement *pipeline;
168   GstElement *appsink;
169   GstEvent *event;
170   GstBuffer *buffer = NULL;
171   guint size = 0;
172   guint16 first_sample = 0;
173   gchar *path =
174       g_build_filename (GST_TEST_FILES_PATH, "audiotestsrc.flac", NULL);
175   gchar *pipe_desc =
176       g_strdup_printf ("filesrc location=\"%s\" ! flacdec ! appsink name=sink",
177       path);
178
179   pipeline = gst_parse_launch (pipe_desc, NULL);
180   fail_unless (pipeline != NULL);
181
182   g_free (path);
183   g_free (pipe_desc);
184
185   appsink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
186   fail_unless (appsink != NULL);
187
188   gst_element_set_state (pipeline, GST_STATE_PAUSED);
189   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
190
191   /* do a partial seek to get the first 1024 samples or 2048 bytes */
192   event = gst_event_new_seek (1.0, GST_FORMAT_DEFAULT, GST_SEEK_FLAG_FLUSH,
193       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 1024);
194   GST_DEBUG ("seeking");
195   fail_unless (gst_element_send_event (appsink, event));
196   GST_DEBUG ("seeked");
197
198   gst_element_set_state (pipeline, GST_STATE_PLAYING);
199
200   do {
201     GST_DEBUG ("pulling buffer");
202     g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
203     GST_DEBUG ("pulled buffer %p", buffer);
204     if (buffer == NULL)
205       break;
206     if (first_sample == 0) {
207       fail_unless_equals_int (GST_BUFFER_OFFSET (buffer), 0L);
208       first_sample = buffer_get_first_sample (buffer);
209     }
210     size += buffer->size;
211
212     gst_buffer_unref (buffer);
213     buffer = NULL;
214   }
215   while (TRUE);
216
217   fail_unless_equals_int (size, 2048);
218   fail_unless_equals_int (first_sample, 0x066a);
219
220   gst_element_set_state (pipeline, GST_STATE_NULL);
221
222   g_object_unref (pipeline);
223   g_object_unref (appsink);
224 }
225
226 GST_END_TEST;
227
228
229 static Suite *
230 flacdec_suite (void)
231 {
232   Suite *s = suite_create ("flacdec");
233
234   TCase *tc_chain = tcase_create ("linear");
235
236   /* time out after 60s, not the default 3 */
237   tcase_set_timeout (tc_chain, 60);
238
239   suite_add_tcase (s, tc_chain);
240   tcase_add_test (tc_chain, test_decode);
241   tcase_add_test (tc_chain, test_decode_seek_full);
242   tcase_add_test (tc_chain, test_decode_seek_partial);
243
244   return s;
245 }
246
247 GST_CHECK_MAIN (flacdec);