c38ea585c5a05686d59bc971b698cc0dfe8215a1
[platform/upstream/gstreamer.git] / tests / check / pipelines / streamheader.c
1 /* GStreamer
2  *
3  * unit test for streamheader handling
4  *
5  * Copyright (C) 2007 Thomas Vander Stichele <thomas at apestaart dot org>
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26 #include <gst/check/gstbufferstraw.h>
27
28 #ifndef GST_DISABLE_PARSE
29
30 /* this tests a gdp-serialized tag from audiotestsrc being sent only once
31  * to clients of multifdsink */
32
33 static int n_tags = 0;
34
35 gboolean
36 tag_event_probe_cb (GstPad * pad, GstEvent * event, GMainLoop * loop)
37 {
38   switch (GST_EVENT_TYPE (event)) {
39     case GST_EVENT_TAG:
40     {
41       ++n_tags;
42       fail_if (n_tags > 1, "More than 1 tag received");
43       break;
44     }
45     case GST_EVENT_EOS:
46     {
47       g_main_loop_quit (loop);
48       break;
49     }
50     default:
51       break;
52   }
53
54   return TRUE;
55 }
56
57 GST_START_TEST (test_multifdsink_gdp_tag)
58 {
59   GstElement *p1, *p2;
60   GstElement *src, *sink, *depay;
61   GstPad *pad;
62   GMainLoop *loop;
63   int pfd[2];
64
65   loop = g_main_loop_new (NULL, FALSE);
66
67   p1 = gst_parse_launch ("audiotestsrc num-buffers=10 ! gdppay"
68       " ! multifdsink name=p1sink", NULL);
69   fail_if (p1 == NULL);
70   p2 = gst_parse_launch ("fdsrc name=p2src ! gdpdepay name=depay"
71       " ! fakesink name=p2sink signal-handoffs=True", NULL);
72   fail_if (p2 == NULL);
73
74   fail_if (pipe (pfd) == -1);
75
76
77   gst_element_set_state (p1, GST_STATE_READY);
78
79   sink = gst_bin_get_by_name (GST_BIN (p1), "p1sink");
80   g_signal_emit_by_name (sink, "add", pfd[1], NULL);
81   gst_object_unref (sink);
82
83   src = gst_bin_get_by_name (GST_BIN (p2), "p2src");
84   g_object_set (G_OBJECT (src), "fd", pfd[0], NULL);
85   gst_object_unref (src);
86
87   depay = gst_bin_get_by_name (GST_BIN (p2), "depay");
88   fail_if (depay == NULL);
89
90   pad = gst_element_get_pad (depay, "src");
91   fail_unless (pad != NULL, "Could not get pad out of depay");
92   gst_object_unref (depay);
93
94   gst_pad_add_event_probe (pad, G_CALLBACK (tag_event_probe_cb), loop);
95
96   gst_element_set_state (p1, GST_STATE_PLAYING);
97   gst_element_set_state (p2, GST_STATE_PLAYING);
98
99   g_main_loop_run (loop);
100
101   assert_equals_int (n_tags, 1);
102 }
103
104 GST_END_TEST;
105
106 /* this tests gdp-serialized Vorbis header pages being sent only once
107  * to clients of multifdsink; the gdp depayloader should deserialize
108  * exactly three in_caps buffers for the three header packets */
109
110 static int n_in_caps = 0;
111 gboolean
112 buffer_probe_cb (GstPad * pad, GstBuffer * buffer)
113 {
114   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_IN_CAPS)) {
115     GstCaps *caps;
116     GstStructure *s;
117     const GValue *sh;
118     GArray *buffers;
119     GstBuffer *buf;
120     int i;
121     gboolean found = FALSE;
122
123     n_in_caps++;
124
125     caps = gst_buffer_get_caps (buffer);
126     s = gst_caps_get_structure (caps, 0);
127     fail_unless (gst_structure_has_field (s, "streamheader"));
128     sh = gst_structure_get_value (s, "streamheader");
129     buffers = g_value_peek_pointer (sh);
130     assert_equals_int (buffers->len, 3);
131
132
133     for (i = 0; i < 3; ++i) {
134       GValue *val;
135
136       val = &g_array_index (buffers, GValue, i);
137       buf = g_value_peek_pointer (val);
138       fail_unless (GST_IS_BUFFER (buf));
139       if (GST_BUFFER_SIZE (buf) == GST_BUFFER_SIZE (buffer)) {
140         if (memcmp (GST_BUFFER_DATA (buf), GST_BUFFER_DATA (buffer),
141                 GST_BUFFER_SIZE (buffer)) == 0) {
142           found = TRUE;
143         }
144       }
145     }
146     fail_unless (found, "Did not find incoming IN_CAPS buffer %p on caps",
147         buffer);
148   }
149
150   return TRUE;
151 }
152
153 GST_START_TEST (test_multifdsink_gdp_vorbisenc)
154 {
155   GstElement *p1, *p2;
156   GstElement *src, *sink, *depay;
157   GstPad *pad;
158   GMainLoop *loop;
159   int pfd[2];
160
161   loop = g_main_loop_new (NULL, FALSE);
162
163   p1 = gst_parse_launch ("audiotestsrc num-buffers=10 ! audioconvert "
164       " ! vorbisenc ! gdppay ! multifdsink name=p1sink", NULL);
165   fail_if (p1 == NULL);
166   p2 = gst_parse_launch ("fdsrc name=p2src ! gdpdepay name=depay"
167       " ! fakesink name=p2sink signal-handoffs=True", NULL);
168   fail_if (p2 == NULL);
169
170   fail_if (pipe (pfd) == -1);
171
172
173   gst_element_set_state (p1, GST_STATE_READY);
174
175   sink = gst_bin_get_by_name (GST_BIN (p1), "p1sink");
176   g_signal_emit_by_name (sink, "add", pfd[1], NULL);
177   gst_object_unref (sink);
178
179   src = gst_bin_get_by_name (GST_BIN (p2), "p2src");
180   g_object_set (G_OBJECT (src), "fd", pfd[0], NULL);
181   gst_object_unref (src);
182
183   depay = gst_bin_get_by_name (GST_BIN (p2), "depay");
184   fail_if (depay == NULL);
185
186   pad = gst_element_get_pad (depay, "src");
187   fail_unless (pad != NULL, "Could not get pad out of depay");
188   gst_object_unref (depay);
189
190   gst_pad_add_event_probe (pad, G_CALLBACK (tag_event_probe_cb), loop);
191   gst_pad_add_buffer_probe (pad, G_CALLBACK (buffer_probe_cb), NULL);
192
193   gst_element_set_state (p1, GST_STATE_PLAYING);
194   gst_element_set_state (p2, GST_STATE_PLAYING);
195
196   g_main_loop_run (loop);
197
198   //FIXME: here's the bug
199   //assert_equals_int (n_in_caps, 3);
200 }
201
202 GST_END_TEST;
203
204
205 #endif /* #ifndef GST_DISABLE_PARSE */
206
207 Suite *
208 streamheader_suite (void)
209 {
210   Suite *s = suite_create ("streamheader");
211   TCase *tc_chain = tcase_create ("general");
212
213   suite_add_tcase (s, tc_chain);
214 #ifndef GST_DISABLE_PARSE
215   tcase_add_test (tc_chain, test_multifdsink_gdp_tag);
216   tcase_add_test (tc_chain, test_multifdsink_gdp_vorbisenc);
217 #endif
218
219   return s;
220 }
221
222 int
223 main (int argc, char **argv)
224 {
225   int nf;
226
227   Suite *s = streamheader_suite ();
228   SRunner *sr = srunner_create (s);
229
230   gst_check_init (&argc, &argv);
231
232   srunner_run_all (sr, CK_NORMAL);
233   nf = srunner_ntests_failed (sr);
234   srunner_free (sr);
235
236   return nf;
237 }