Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / tests / check / elements / neonhttpsrc.c
1 /* GStreamer unit tests for the neonhttpsrc element
2  * Copyright (C) 2006-2007 Tim-Philipp Müller <tim centricular net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <gst/check/gstcheck.h>
25
26 static void
27 handoff_cb (GstElement * fakesink, GstBuffer * buf, GstPad * pad,
28     GstBuffer ** p_outbuf)
29 {
30   GST_LOG ("handoff, buf = %p", buf);
31   if (*p_outbuf == NULL)
32     *p_outbuf = gst_buffer_ref (buf);
33 }
34
35 GST_START_TEST (test_first_buffer_has_offset)
36 {
37   GstStateChangeReturn ret;
38   GstElement *pipe, *src, *sink;
39   GstBuffer *buf = NULL;
40   gchar **cookies;
41
42   pipe = gst_pipeline_new (NULL);
43
44   src = gst_element_factory_make ("neonhttpsrc", NULL);
45   fail_unless (src != NULL);
46
47   sink = gst_element_factory_make ("fakesink", NULL);
48   fail_unless (sink != NULL);
49
50   gst_bin_add (GST_BIN (pipe), src);
51   gst_bin_add (GST_BIN (pipe), sink);
52   fail_unless (gst_element_link (src, sink));
53
54   g_object_set (src, "location", "http://gstreamer.freedesktop.org/", NULL);
55   g_object_set (src, "automatic-redirect", TRUE, NULL);
56
57   /* set some cookies (shouldn't hurt) */
58   cookies = g_strsplit ("foo=1234,bar=9871615348162523726337x99FB", ",", -1);
59   g_object_set (src, "cookies", cookies, NULL);
60   g_strfreev (cookies);
61
62   g_object_set (sink, "signal-handoffs", TRUE, NULL);
63   g_signal_connect (sink, "preroll-handoff", G_CALLBACK (handoff_cb), &buf);
64
65   ret = gst_element_set_state (pipe, GST_STATE_PAUSED);
66   if (ret != GST_STATE_CHANGE_ASYNC) {
67     GST_DEBUG ("failed to start up neon http src, ret = %d", ret);
68     goto done;
69   }
70
71   /* don't wait for more than 10 seconds */
72   ret = gst_element_get_state (pipe, NULL, NULL, 10 * GST_SECOND);
73   GST_LOG ("ret = %u", ret);
74
75   if (buf == NULL) {
76     /* we want to test the buffer offset, nothing else; if there's a failure
77      * it might be for lots of reasons (no network connection, whatever), we're
78      * not interested in those */
79     GST_DEBUG ("didn't manage to get data within 10 seconds, skipping test");
80     goto done;
81   }
82
83   GST_DEBUG ("buffer offset = %" G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buf));
84
85   /* first buffer should have a 0 offset */
86   fail_unless (GST_BUFFER_OFFSET (buf) == 0);
87   gst_buffer_unref (buf);
88
89 done:
90
91   gst_element_set_state (pipe, GST_STATE_NULL);
92   gst_object_unref (pipe);
93 }
94
95 GST_END_TEST;
96
97 GST_START_TEST (test_icy_stream)
98 {
99   GstElement *pipe, *src, *sink;
100   GstMessage *msg;
101
102   pipe = gst_pipeline_new (NULL);
103
104   src = gst_element_factory_make ("neonhttpsrc", NULL);
105   fail_unless (src != NULL);
106
107   sink = gst_element_factory_make ("fakesink", NULL);
108   fail_unless (sink != NULL);
109
110   gst_bin_add (GST_BIN (pipe), src);
111   gst_bin_add (GST_BIN (pipe), sink);
112   fail_unless (gst_element_link (src, sink));
113
114   /* First try Virgin Radio Ogg stream, to see if there's connectivity and all
115    * (which is an attempt to work around the completely horrid error reporting
116    * and that we can't distinguish different types of failures here).
117    * Note that neonhttpsrc does the whole connect + session initiation all in
118    * the state change function. */
119
120   g_object_set (src, "location", "http://ogg2.smgradio.com/vr32.ogg", NULL);
121   g_object_set (src, "automatic-redirect", FALSE, NULL);
122   g_object_set (src, "num-buffers", 1, NULL);
123   gst_element_set_state (pipe, GST_STATE_PLAYING);
124
125   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe),
126       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
127   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
128     GST_INFO ("looks like there's no net connectivity or sgmradio.com is "
129         "down. In any case, let's just skip this test");
130     gst_message_unref (msg);
131     goto done;
132   }
133   gst_message_unref (msg);
134   msg = NULL;
135   gst_element_set_state (pipe, GST_STATE_NULL);
136
137   /* Now, if the ogg stream works, the mp3 shoutcast stream should work as
138    * well (time will tell if that's true) */
139
140   /* Virgin Radio 32kbps mp3 shoutcast stream */
141   g_object_set (src, "location", "http://mp3-vr-32.smgradio.com:80/", NULL);
142   g_object_set (src, "automatic-redirect", FALSE, NULL);
143
144   /* g_object_set (src, "neon-http-debug", TRUE, NULL); */
145
146   /* EOS after the first buffer */
147   g_object_set (src, "num-buffers", 1, NULL);
148
149   gst_element_set_state (pipe, GST_STATE_PLAYING);
150   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe),
151       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
152
153   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS) {
154     GST_DEBUG ("success, we're done here");
155     gst_message_unref (msg);
156     goto done;
157   }
158
159   {
160     GError *err = NULL;
161
162     gst_message_parse_error (msg, &err, NULL);
163     gst_message_unref (msg);
164     g_error ("Error with ICY mp3 shoutcast stream: %s", err->message);
165     g_error_free (err);
166   }
167
168 done:
169
170   gst_element_set_state (pipe, GST_STATE_NULL);
171   gst_object_unref (pipe);
172 }
173
174 GST_END_TEST;
175
176 static Suite *
177 neonhttpsrc_suite (void)
178 {
179   Suite *s = suite_create ("neonhttpsrc");
180   TCase *tc_chain = tcase_create ("general");
181
182   suite_add_tcase (s, tc_chain);
183   tcase_set_timeout (tc_chain, 5);
184   tcase_add_test (tc_chain, test_first_buffer_has_offset);
185   tcase_add_test (tc_chain, test_icy_stream);
186
187   return s;
188 }
189
190 GST_CHECK_MAIN (neonhttpsrc);