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