• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit test for aiffparse
4  * Copyright (C) 2013 Collabora Ltd.
5  *   Author: Matthieu Bouron <matthieu.bouron@collabora.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include <gst/check/gstcheck.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <glib/gstdio.h>
27 
28 #define DATA_FILENAME "s16be-id3v2.aiff"
29 #define DATA_SIZE 23254
30 #define SSND_DATA_OFFSET 68
31 #define SSND_DATA_SIZE 20480
32 
33 static GstPad *sinkpad;
34 static GMainLoop *loop = NULL;
35 static gboolean have_eos = FALSE;
36 static gboolean have_tags = FALSE;
37 static gchar *data = NULL;
38 static gsize data_size = 0;
39 static guint64 data_read = 0;
40 static guint64 data_offset = 0;
41 
42 static GstStaticPadTemplate sinktemplate =
43 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
44     GST_STATIC_CAPS_ANY);
45 
46 static void
sink_check_caps(GstPad * pad,GstCaps * caps)47 sink_check_caps (GstPad * pad, GstCaps * caps)
48 {
49   GstCaps *tcaps = gst_caps_new_simple ("audio/x-raw",
50       "rate", G_TYPE_INT, 44100,
51       "channels", G_TYPE_INT, 2,
52       "format", G_TYPE_STRING, "S16BE",
53       "layout", G_TYPE_STRING, "interleaved",
54       NULL);
55 
56   fail_unless (gst_caps_can_intersect (caps, tcaps));
57   gst_caps_unref (tcaps);
58 }
59 
60 static GstFlowReturn
sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)61 sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
62 {
63   GstMapInfo info;
64 
65   gst_buffer_map (buffer, &info, GST_MAP_READ);
66 
67   fail_unless ((data_offset + info.size) <= data_size);
68   fail_unless (memcmp (info.data, data + data_offset, info.size) == 0);
69 
70   data_read += info.size;
71   data_offset += info.size;
72 
73   gst_buffer_unmap (buffer, &info);
74   gst_buffer_unref (buffer);
75 
76   return GST_FLOW_OK;
77 }
78 
79 static gboolean
sink_event(GstPad * pad,GstObject * parent,GstEvent * event)80 sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
81 {
82   GST_DEBUG_OBJECT (pad, "Got %s event %p: %" GST_PTR_FORMAT,
83       GST_EVENT_TYPE_NAME (event), event, event);
84 
85   switch (GST_EVENT_TYPE (event)) {
86     case GST_EVENT_EOS:
87       if (loop) {
88         while (!g_main_loop_is_running (loop));
89       }
90 
91       have_eos = TRUE;
92       if (loop)
93         g_main_loop_quit (loop);
94       break;
95     case GST_EVENT_CAPS:
96     {
97       GstCaps *caps;
98 
99       gst_event_parse_caps (event, &caps);
100       sink_check_caps (pad, caps);
101       break;
102     }
103     case GST_EVENT_TAG:
104     {
105       int i, ret;
106       gchar *buf = NULL;
107       GstTagList *aiff_tags = NULL;
108       const gchar *tags[][2] = {
109         {"title", "Title"}, {"artist", "Artist"},
110       };
111 
112       gst_event_parse_tag (event, &aiff_tags);
113       fail_unless (aiff_tags != NULL);
114 
115       have_tags = TRUE;
116       for (i = 0; i < sizeof (tags) / sizeof (*tags); i++) {
117         buf = NULL;
118         if (!gst_tag_list_get_string (aiff_tags, tags[i][0], &buf)) {
119           have_tags = FALSE;
120           continue;
121         }
122         ret = g_strcmp0 (buf, tags[i][1]);
123         g_free (buf);
124         if (ret != 0) {
125           have_tags = FALSE;
126           continue;
127         }
128       }
129 
130       break;
131     }
132     default:
133       break;
134   }
135 
136   gst_event_unref (event);
137 
138   return TRUE;
139 }
140 
141 static GstPad *
create_sink_pad(void)142 create_sink_pad (void)
143 {
144   sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
145 
146   gst_pad_set_chain_function (sinkpad, sink_chain);
147   gst_pad_set_event_function (sinkpad, sink_event);
148 
149   return sinkpad;
150 }
151 
152 static void
run_check(gboolean push_mode)153 run_check (gboolean push_mode)
154 {
155   gchar *path;
156   GstPad *aiff_srcpad;
157   GError *error = NULL;
158   GstElement *src, *sep, *aiffparse;
159 
160   have_eos = FALSE;
161   have_tags = FALSE;
162   data_read = 0;
163   data_size = 0;
164   data_offset = SSND_DATA_OFFSET;
165   loop = g_main_loop_new (NULL, FALSE);
166 
167   GST_INFO ("%s mode", push_mode ? "Pull" : "Push");
168 
169   aiffparse = gst_element_factory_make ("aiffparse", "aiffparse");
170   fail_unless (aiffparse != NULL);
171 
172   aiff_srcpad = gst_element_get_static_pad (aiffparse, "src");
173   fail_unless (aiff_srcpad != NULL);
174 
175   src = gst_element_factory_make ("filesrc", "filesrc");
176   fail_unless (src != NULL);
177 
178   sinkpad = create_sink_pad ();
179   fail_unless (sinkpad != NULL);
180 
181   if (push_mode) {
182     sep = gst_element_factory_make ("queue", "queue");
183   } else {
184     sep = gst_element_factory_make ("identity", "identity");
185   }
186   fail_unless (sep != NULL);
187 
188   fail_unless (gst_element_link (src, sep));
189   fail_unless (gst_element_link (sep, aiffparse));
190 
191   fail_unless (gst_pad_link (aiff_srcpad, sinkpad) == GST_PAD_LINK_OK);
192   gst_object_unref (aiff_srcpad);
193 
194   path = g_build_filename (GST_TEST_FILES_PATH, DATA_FILENAME, NULL);
195   GST_LOG ("Reading file '%s'", path);
196   g_object_set (src, "location", path, NULL);
197 
198   fail_unless (g_file_get_contents (path, &data, &data_size, &error));
199   fail_unless (data_size == DATA_SIZE);
200 
201   GST_INFO ("Setting to PLAYING");
202   gst_pad_set_active (sinkpad, TRUE);
203   fail_unless (gst_element_set_state (aiffparse,
204           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
205   fail_unless (gst_element_set_state (sep,
206           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
207   fail_unless (gst_element_set_state (src,
208           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
209 
210   g_main_loop_run (loop);
211   fail_unless (have_eos == TRUE);
212   fail_unless (data_read == SSND_DATA_SIZE);
213   fail_unless (push_mode || (have_tags == TRUE));
214 
215   gst_pad_set_active (sinkpad, FALSE);
216   gst_element_set_state (aiffparse, GST_STATE_NULL);
217   gst_element_set_state (sep, GST_STATE_NULL);
218   gst_element_set_state (src, GST_STATE_NULL);
219 
220   gst_object_unref (aiffparse);
221   gst_object_unref (sep);
222   gst_object_unref (src);
223   gst_object_unref (sinkpad);
224   g_main_loop_unref (loop);
225   loop = NULL;
226   g_free (path);
227   g_free (data);
228 }
229 
GST_START_TEST(test_pull)230 GST_START_TEST (test_pull)
231 {
232   run_check (FALSE);
233 }
234 
235 GST_END_TEST;
236 
GST_START_TEST(test_push)237 GST_START_TEST (test_push)
238 {
239   run_check (TRUE);
240 }
241 
242 GST_END_TEST;
243 
244 static Suite *
aiffparse_suite(void)245 aiffparse_suite (void)
246 {
247   Suite *s = suite_create ("aiffparse");
248   TCase *tc_chain = tcase_create ("general");
249 
250   suite_add_tcase (s, tc_chain);
251   tcase_add_test (tc_chain, test_pull);
252   tcase_add_test (tc_chain, test_push);
253 
254   return s;
255 }
256 
257 GST_CHECK_MAIN (aiffparse);
258