tests: fix icydemux unit test
[platform/upstream/gstreamer.git] / tests / check / elements / icydemux.c
1 /*
2  * icydemux.c - Test icydemux element
3  * Copyright (C) 2006 Michael Smith <msmith@fluendo.com>
4  * 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/check/gstcheck.h>
25
26 /* Chunk of data: 8 bytes, followed by a metadata-length byte of 2, followed by
27  * some metadata (32 bytes), then some more data.
28  */
29 #define TEST_METADATA \
30     "Test metadata"
31 #define ICY_METADATA \
32     "StreamTitle='" TEST_METADATA "';\0\0\0\0"
33
34 #define ICY_DATA \
35     "aaaaaaaa" \
36     "\x02" \
37     ICY_METADATA \
38     "bbbbbbbb"
39
40 #define ICYCAPS "application/x-icy, metadata-interval = (int)8"
41
42 #define SRC_CAPS "application/x-icy, metadata-interval = (int)[0, MAX]"
43 #define SINK_CAPS  "ANY"
44
45 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS (SRC_CAPS)
49     );
50
51 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS (SINK_CAPS)
55     );
56
57 static GstElement *icydemux;
58 static GstBus *bus;
59 GstPad *srcpad, *sinkpad;
60
61 static GstStaticCaps typefind_caps =
62 GST_STATIC_CAPS ("application/octet-stream");
63
64 static gboolean fake_typefind_caps;     /* FALSE */
65
66 static void
67 typefind_succeed (GstTypeFind * tf, gpointer private)
68 {
69   if (fake_typefind_caps) {
70     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
71         gst_static_caps_get (&typefind_caps));
72   }
73 }
74
75 static gboolean
76 test_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
77 {
78   GST_LOG_OBJECT (pad, "%s event: %" GST_PTR_FORMAT,
79       GST_EVENT_TYPE_NAME (event), event);
80
81   /* a sink would post tag events as messages, so do the same here,
82    * esp. since we're polling on the bus waiting for TAG messages.. */
83   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
84     GstTagList *taglist;
85
86     gst_event_parse_tag (event, &taglist);
87
88     gst_bus_post (bus, gst_message_new_tag (GST_OBJECT (pad),
89             gst_tag_list_copy (taglist)));
90   }
91
92   gst_event_unref (event);
93   return TRUE;
94 }
95
96 static void
97 icydemux_found_pad (GstElement * src, GstPad * pad, gpointer data)
98 {
99   GST_DEBUG ("got new pad %" GST_PTR_FORMAT, pad);
100
101   /* Turns out that this asserts a refcount which is wrong for this
102    * case (adding the pad from a pad-added callback), so just do the same
103    * thing inline... */
104   /* sinkpad = gst_check_setup_sink_pad (icydemux, &sinktemplate, NULL); */
105   sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
106   fail_if (sinkpad == NULL, "Couldn't create sinkpad");
107   srcpad = gst_element_get_static_pad (icydemux, "src");
108   fail_if (srcpad == NULL, "Failed to get srcpad from icydemux");
109   gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
110   gst_pad_set_event_function (sinkpad, test_event_func);
111
112   GST_DEBUG ("checking srcpad %p refcount", srcpad);
113   /* 1 from element, 1 from signal, 1 from us */
114   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 3);
115
116   GST_DEBUG ("linking srcpad");
117   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
118       "Failed to link pads");
119   gst_object_unref (srcpad);
120
121   gst_pad_set_active (sinkpad, TRUE);
122 }
123
124 static GstElement *
125 create_icydemux (void)
126 {
127   icydemux = gst_check_setup_element ("icydemux");
128   srcpad = gst_check_setup_src_pad (icydemux, &srctemplate);
129
130   gst_pad_set_active (srcpad, TRUE);
131
132   g_signal_connect (icydemux, "pad-added", G_CALLBACK (icydemux_found_pad),
133       NULL);
134
135   bus = gst_bus_new ();
136   gst_element_set_bus (icydemux, bus);
137
138   fail_unless (gst_element_set_state (icydemux, GST_STATE_PLAYING) !=
139       GST_STATE_CHANGE_FAILURE, "could not set to playing");
140
141   return icydemux;
142 }
143
144 static void
145 cleanup_icydemux (void)
146 {
147   gst_bus_set_flushing (bus, TRUE);
148   gst_object_unref (bus);
149   bus = NULL;
150
151   gst_check_teardown_src_pad (icydemux);
152   if (sinkpad)
153     gst_check_teardown_sink_pad (icydemux);
154   gst_check_teardown_element (icydemux);
155
156   srcpad = NULL;
157   sinkpad = NULL;
158   icydemux = NULL;
159 }
160
161 static void
162 push_data (const guint8 * data, int len, GstCaps * caps, gint64 offset)
163 {
164   GstFlowReturn res;
165   GstBuffer *buffer = gst_buffer_new_and_alloc (len);
166
167   gst_buffer_fill (buffer, 0, data, len);
168
169   GST_BUFFER_OFFSET (buffer) = offset;
170
171   gst_pad_set_caps (srcpad, caps);
172   res = gst_pad_push (srcpad, buffer);
173
174   fail_unless (res == GST_FLOW_OK, "Failed pushing buffer: %d", res);
175 }
176
177 GST_START_TEST (test_demux)
178 {
179   GstMessage *message;
180   GstTagList *tags;
181   const GValue *tag_val;
182   const gchar *tag;
183   GstCaps *caps;
184
185   fail_unless (gst_type_find_register (NULL, "success", GST_RANK_PRIMARY,
186           typefind_succeed, NULL, gst_static_caps_get (&typefind_caps), NULL,
187           NULL));
188
189   fake_typefind_caps = TRUE;
190
191   caps = gst_caps_from_string (ICYCAPS);
192
193   create_icydemux ();
194
195   push_data ((guint8 *) ICY_DATA, sizeof (ICY_DATA), caps, -1);
196
197   message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
198   fail_unless (message != NULL);
199
200   gst_message_parse_tag (message, &tags);
201   fail_unless (tags != NULL);
202
203   tag_val = gst_tag_list_get_value_index (tags, GST_TAG_TITLE, 0);
204   fail_unless (tag_val != NULL);
205
206   tag = g_value_get_string (tag_val);
207   fail_unless (tag != NULL);
208
209   fail_unless_equals_string (TEST_METADATA, (char *) tag);
210
211   gst_tag_list_unref (tags);
212   gst_message_unref (message);
213   gst_caps_unref (caps);
214
215   cleanup_icydemux ();
216
217   fake_typefind_caps = FALSE;
218 }
219
220 GST_END_TEST;
221
222 /* run this test first before the custom typefind function is set up */
223 GST_START_TEST (test_first_buf_offset_when_merged_for_typefinding)
224 {
225   const guint8 buf1[] = { 'M' };
226   const guint8 buf2[] = { 'P', '+', 0xff, 0xfb, 0x00, 0x00, 0x00, 0x00,
227     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
228   };
229   GstCaps *icy_caps;
230   GstPad *icy_srcpad;
231
232   fake_typefind_caps = FALSE;
233
234   create_icydemux ();
235
236   icy_caps = gst_caps_from_string (ICYCAPS);
237
238   push_data (buf1, G_N_ELEMENTS (buf1), icy_caps, 0);
239
240   /* one byte isn't really enough for typefinding, can't have a srcpad yet */
241   fail_unless (gst_element_get_static_pad (icydemux, "src") == NULL);
242
243   push_data (buf2, G_N_ELEMENTS (buf2), icy_caps, -1);
244
245   /* should have been enough to create a audio/x-musepack source pad .. */
246   icy_srcpad = gst_element_get_static_pad (icydemux, "src");
247   fail_unless (icy_srcpad != NULL);
248   gst_object_unref (icy_srcpad);
249
250   fail_unless (g_list_length (buffers) > 0);
251
252   /* first buffer should have offset 0 even after it was merged with 2nd buf */
253   fail_unless (GST_BUFFER_OFFSET (GST_BUFFER_CAST (buffers->data)) == 0);
254
255   gst_caps_unref (icy_caps);
256
257   cleanup_icydemux ();
258 }
259
260 GST_END_TEST;
261
262 GST_START_TEST (test_not_negotiated)
263 {
264   GstBuffer *buf;
265
266   create_icydemux ();
267
268   buf = gst_buffer_new_and_alloc (0);
269   GST_BUFFER_OFFSET (buf) = 0;
270
271   fail_unless_equals_int (gst_pad_push (srcpad, buf), GST_FLOW_NOT_NEGOTIATED);
272   buf = NULL;
273
274   cleanup_icydemux ();
275 }
276
277 GST_END_TEST;
278
279 static Suite *
280 icydemux_suite (void)
281 {
282   Suite *s = suite_create ("icydemux");
283   TCase *tc_chain = tcase_create ("general");
284
285   suite_add_tcase (s, tc_chain);
286   tcase_add_test (tc_chain, test_demux);
287   tcase_add_test (tc_chain, test_first_buf_offset_when_merged_for_typefinding);
288   tcase_add_test (tc_chain, test_not_negotiated);
289
290   return s;
291 }
292
293 GST_CHECK_MAIN (icydemux)