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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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 EMPTY_ICY_STREAM_TITLE_METADATA \
35     "StreamTitle='';\0"
36 
37 #define ICY_DATA \
38     "aaaaaaaa" \
39     "\x02" \
40     ICY_METADATA \
41     "bbbbbbbb"
42 
43 #define ICY_DATA_EMPTY_METADATA \
44     ICY_DATA \
45     "\x00" \
46     "dddddddd" \
47     "\x01" \
48     EMPTY_ICY_STREAM_TITLE_METADATA \
49     "cccccccc"
50 
51 #define ICYCAPS "application/x-icy, metadata-interval = (int)8"
52 
53 #define SRC_CAPS "application/x-icy, metadata-interval = (int)[0, MAX]"
54 #define SINK_CAPS  "ANY"
55 
56 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS (SRC_CAPS)
60     );
61 
62 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
63     GST_PAD_SINK,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS (SINK_CAPS)
66     );
67 
68 static GstElement *icydemux;
69 static GstBus *bus;
70 GstPad *srcpad, *sinkpad;
71 
72 static GstStaticCaps typefind_caps =
73 GST_STATIC_CAPS ("application/octet-stream");
74 
75 static gboolean fake_typefind_caps;     /* FALSE */
76 
77 static void
typefind_succeed(GstTypeFind * tf,gpointer private)78 typefind_succeed (GstTypeFind * tf, gpointer private)
79 {
80   if (fake_typefind_caps) {
81     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
82         gst_static_caps_get (&typefind_caps));
83   }
84 }
85 
86 static gboolean
test_event_func(GstPad * pad,GstObject * parent,GstEvent * event)87 test_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
88 {
89   GST_LOG_OBJECT (pad, "%s event: %" GST_PTR_FORMAT,
90       GST_EVENT_TYPE_NAME (event), event);
91 
92   /* a sink would post tag events as messages, so do the same here,
93    * esp. since we're polling on the bus waiting for TAG messages.. */
94   if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
95     GstTagList *taglist;
96 
97     gst_event_parse_tag (event, &taglist);
98 
99     gst_bus_post (bus, gst_message_new_tag (GST_OBJECT (pad),
100             gst_tag_list_copy (taglist)));
101   }
102 
103   gst_event_unref (event);
104   return TRUE;
105 }
106 
107 static void
icydemux_found_pad(GstElement * src,GstPad * pad,gpointer data)108 icydemux_found_pad (GstElement * src, GstPad * pad, gpointer data)
109 {
110   GST_DEBUG ("got new pad %" GST_PTR_FORMAT, pad);
111 
112   /* Turns out that this asserts a refcount which is wrong for this
113    * case (adding the pad from a pad-added callback), so just do the same
114    * thing inline... */
115   /* sinkpad = gst_check_setup_sink_pad (icydemux, &sinktemplate, NULL); */
116   sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
117   fail_if (sinkpad == NULL, "Couldn't create sinkpad");
118   srcpad = gst_element_get_static_pad (icydemux, "src");
119   fail_if (srcpad == NULL, "Failed to get srcpad from icydemux");
120   gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
121   gst_pad_set_event_function (sinkpad, test_event_func);
122 
123   GST_DEBUG ("checking srcpad %p refcount", srcpad);
124   /* 1 from element, 1 from signal, 1 from us */
125   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 3);
126 
127   GST_DEBUG ("linking srcpad");
128   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
129       "Failed to link pads");
130   gst_object_unref (srcpad);
131 
132   gst_pad_set_active (sinkpad, TRUE);
133 }
134 
135 static GstElement *
create_icydemux(void)136 create_icydemux (void)
137 {
138   icydemux = gst_check_setup_element ("icydemux");
139   srcpad = gst_check_setup_src_pad (icydemux, &srctemplate);
140 
141   gst_pad_set_active (srcpad, TRUE);
142 
143   g_signal_connect (icydemux, "pad-added", G_CALLBACK (icydemux_found_pad),
144       NULL);
145 
146   bus = gst_bus_new ();
147   gst_element_set_bus (icydemux, bus);
148 
149   fail_unless (gst_element_set_state (icydemux, GST_STATE_PLAYING) !=
150       GST_STATE_CHANGE_FAILURE, "could not set to playing");
151 
152   return icydemux;
153 }
154 
155 static void
cleanup_icydemux(void)156 cleanup_icydemux (void)
157 {
158   gst_bus_set_flushing (bus, TRUE);
159   gst_object_unref (bus);
160   bus = NULL;
161 
162   gst_check_drop_buffers ();
163   gst_check_teardown_src_pad (icydemux);
164   if (sinkpad)
165     gst_check_teardown_sink_pad (icydemux);
166   gst_check_teardown_element (icydemux);
167 
168   srcpad = NULL;
169   sinkpad = NULL;
170   icydemux = NULL;
171 }
172 
173 static void
push_data(const guint8 * data,int len,gint64 offset)174 push_data (const guint8 * data, int len, gint64 offset)
175 {
176   GstFlowReturn res;
177   GstBuffer *buffer = gst_buffer_new_and_alloc (len);
178 
179   gst_buffer_fill (buffer, 0, data, len);
180 
181   GST_BUFFER_OFFSET (buffer) = offset;
182 
183   res = gst_pad_push (srcpad, buffer);
184 
185   fail_unless (res == GST_FLOW_OK, "Failed pushing buffer: %d", res);
186 }
187 
GST_START_TEST(test_demux)188 GST_START_TEST (test_demux)
189 {
190   GstMessage *message;
191   GstTagList *tags;
192   const GValue *tag_val;
193   const gchar *tag;
194   GstCaps *caps;
195 
196   fail_unless (gst_type_find_register (NULL, "success", GST_RANK_PRIMARY,
197           typefind_succeed, NULL, gst_static_caps_get (&typefind_caps), NULL,
198           NULL));
199 
200   fake_typefind_caps = TRUE;
201 
202   caps = gst_caps_from_string (ICYCAPS);
203 
204   create_icydemux ();
205   gst_check_setup_events (srcpad, icydemux, caps, GST_FORMAT_TIME);
206 
207   push_data ((guint8 *) ICY_DATA, sizeof (ICY_DATA), -1);
208 
209   message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
210   fail_unless (message != NULL);
211 
212   gst_message_parse_tag (message, &tags);
213   fail_unless (tags != NULL);
214 
215   tag_val = gst_tag_list_get_value_index (tags, GST_TAG_TITLE, 0);
216   fail_unless (tag_val != NULL);
217 
218   tag = g_value_get_string (tag_val);
219   fail_unless (tag != NULL);
220 
221   fail_unless_equals_string (TEST_METADATA, (char *) tag);
222 
223   gst_tag_list_unref (tags);
224   gst_message_unref (message);
225   gst_caps_unref (caps);
226 
227   cleanup_icydemux ();
228 
229   fake_typefind_caps = FALSE;
230 }
231 
232 GST_END_TEST;
233 
GST_START_TEST(test_demux_empty_data)234 GST_START_TEST (test_demux_empty_data)
235 {
236   GstMessage *message;
237   GstTagList *tags;
238   const GValue *tag_val;
239   const gchar *tag;
240   GstCaps *caps;
241 
242   fail_unless (gst_type_find_register (NULL, "success", GST_RANK_PRIMARY,
243           typefind_succeed, NULL, gst_static_caps_get (&typefind_caps), NULL,
244           NULL));
245 
246   fake_typefind_caps = TRUE;
247 
248   caps = gst_caps_from_string (ICYCAPS);
249 
250   create_icydemux ();
251   gst_check_setup_events (srcpad, icydemux, caps, GST_FORMAT_TIME);
252 
253   push_data ((guint8 *) ICY_DATA_EMPTY_METADATA,
254       sizeof (ICY_DATA_EMPTY_METADATA), -1);
255 
256   message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
257   fail_unless (message != NULL);
258 
259   gst_message_parse_tag (message, &tags);
260   fail_unless (tags != NULL);
261 
262   tag_val = gst_tag_list_get_value_index (tags, GST_TAG_TITLE, 0);
263   fail_unless (tag_val != NULL);
264 
265   tag = g_value_get_string (tag_val);
266   fail_unless (tag != NULL);
267 
268   fail_unless_equals_string (TEST_METADATA, (char *) tag);
269 
270   gst_tag_list_unref (tags);
271   gst_message_unref (message);
272 
273   message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
274   fail_unless (message != NULL);
275 
276   gst_message_parse_tag (message, &tags);
277   fail_unless (tags != NULL);
278 
279   tag_val = gst_tag_list_get_value_index (tags, GST_TAG_TITLE, 0);
280   fail_unless (tag_val == NULL);
281 
282   gst_message_unref (message);
283 
284   /* Ensure that no further tag messages are received, i.e. the empty ICY tag
285    * is skipped */
286   message = gst_bus_poll (bus, GST_MESSAGE_TAG, 100000000);
287   fail_unless (message == NULL);
288 
289   gst_tag_list_unref (tags);
290   gst_caps_unref (caps);
291 
292   cleanup_icydemux ();
293 
294   fake_typefind_caps = FALSE;
295 }
296 
297 GST_END_TEST;
298 
299 /* run this test first before the custom typefind function is set up */
GST_START_TEST(test_first_buf_offset_when_merged_for_typefinding)300 GST_START_TEST (test_first_buf_offset_when_merged_for_typefinding)
301 {
302   const guint8 buf1[] = { 'M' };
303   const guint8 buf2[] = { 'P', '+', 0xff, 0xfb, 0x00, 0x00, 0x00, 0x00,
304     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
305   };
306   GstCaps *icy_caps;
307   GstPad *icy_srcpad;
308 
309   fake_typefind_caps = FALSE;
310 
311   create_icydemux ();
312 
313   icy_caps = gst_caps_from_string (ICYCAPS);
314 
315   gst_check_setup_events (srcpad, icydemux, icy_caps, GST_FORMAT_TIME);
316 
317   push_data (buf1, G_N_ELEMENTS (buf1), 0);
318 
319   /* one byte isn't really enough for typefinding, can't have a srcpad yet */
320   fail_unless (gst_element_get_static_pad (icydemux, "src") == NULL);
321 
322   push_data (buf2, G_N_ELEMENTS (buf2), -1);
323 
324   /* should have been enough to create a audio/x-musepack source pad .. */
325   icy_srcpad = gst_element_get_static_pad (icydemux, "src");
326   fail_unless (icy_srcpad != NULL);
327   gst_object_unref (icy_srcpad);
328 
329   fail_unless (g_list_length (buffers) > 0);
330 
331   /* first buffer should have offset 0 even after it was merged with 2nd buf */
332   fail_unless (GST_BUFFER_OFFSET (GST_BUFFER_CAST (buffers->data)) == 0);
333 
334   gst_caps_unref (icy_caps);
335 
336   cleanup_icydemux ();
337 }
338 
339 GST_END_TEST;
340 
GST_START_TEST(test_not_negotiated)341 GST_START_TEST (test_not_negotiated)
342 {
343   GstBuffer *buf;
344   GstSegment segment;
345 
346   create_icydemux ();
347 
348   gst_segment_init (&segment, GST_FORMAT_BYTES);
349   gst_pad_push_event (srcpad, gst_event_new_stream_start ("test"));
350   gst_pad_push_event (srcpad, gst_event_new_segment (&segment));
351 
352   buf = gst_buffer_new_and_alloc (0);
353   GST_BUFFER_OFFSET (buf) = 0;
354 
355   fail_unless_equals_int (gst_pad_push (srcpad, buf), GST_FLOW_NOT_NEGOTIATED);
356   buf = NULL;
357 
358   cleanup_icydemux ();
359 }
360 
361 GST_END_TEST;
362 
363 static Suite *
icydemux_suite(void)364 icydemux_suite (void)
365 {
366   Suite *s = suite_create ("icydemux");
367   TCase *tc_chain = tcase_create ("general");
368 
369   suite_add_tcase (s, tc_chain);
370   tcase_add_test (tc_chain, test_demux);
371   tcase_add_test (tc_chain, test_demux_empty_data);
372   tcase_add_test (tc_chain, test_first_buf_offset_when_merged_for_typefinding);
373   tcase_add_test (tc_chain, test_not_negotiated);
374 
375   return s;
376 }
377 
378 GST_CHECK_MAIN (icydemux)
379