Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / check / elements / decodebin2.c
1 /* GStreamer unit tests for decodebin2
2  *
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <gst/check/gstcheck.h>
26 #include <unistd.h>
27
28 static const gchar dummytext[] =
29     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
30     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
31     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
32     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
33     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
34     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
35     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox "
36     "jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
37     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
38     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
39     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
40     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
41     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
42     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox ";
43
44 static void
45 src_handoff_cb (GstElement * src, GstBuffer * buf, GstPad * pad, gpointer data)
46 {
47   GST_BUFFER_DATA (buf) = (guint8 *) dummytext;
48   GST_BUFFER_SIZE (buf) = sizeof (dummytext);
49   GST_BUFFER_OFFSET (buf) = 0;
50   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_READONLY);
51 }
52
53 static void
54 decodebin_new_decoded_pad_cb (GstElement * decodebin, GstPad * pad,
55     gboolean last, gboolean * p_flag)
56 {
57   /* we should not be reached */
58   fail_unless (decodebin == NULL, "new-decoded-pad should not be emitted");
59 }
60
61 /* make sure that decodebin errors out instead of creating a new decoded pad
62  * if the entire stream is a plain text file */
63 GST_START_TEST (test_text_plain_streams)
64 {
65   GstElement *pipe, *src, *decodebin;
66   GstMessage *msg;
67
68   pipe = gst_pipeline_new (NULL);
69   fail_unless (pipe != NULL, "failed to create pipeline");
70
71   src = gst_element_factory_make ("fakesrc", "src");
72   fail_unless (src != NULL, "Failed to create fakesrc element");
73
74   g_object_set (src, "signal-handoffs", TRUE, NULL);
75   g_object_set (src, "num-buffers", 1, NULL);
76   g_object_set (src, "can-activate-pull", FALSE, NULL);
77   g_signal_connect (src, "handoff", G_CALLBACK (src_handoff_cb), NULL);
78
79   decodebin = gst_element_factory_make ("decodebin2", "decodebin");
80   fail_unless (decodebin != NULL, "Failed to create decodebin element");
81
82   g_signal_connect (decodebin, "new-decoded-pad",
83       G_CALLBACK (decodebin_new_decoded_pad_cb), NULL);
84
85   fail_unless (gst_bin_add (GST_BIN (pipe), src));
86   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
87   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
88
89   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
90       GST_STATE_CHANGE_SUCCESS);
91   /* it's push-based, so should be async */
92   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
93       GST_STATE_CHANGE_ASYNC);
94
95   /* it should error out at some point */
96   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, -1);
97   fail_unless (msg != NULL);
98   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
99   gst_message_unref (msg);
100
101   gst_element_set_state (pipe, GST_STATE_NULL);
102   gst_object_unref (pipe);
103 }
104
105 GST_END_TEST;
106
107 static void
108 new_decoded_pad_plug_fakesink_cb (GstElement * decodebin, GstPad * srcpad,
109     gboolean last, GstElement * pipeline)
110 {
111   GstElement *sink;
112   GstPad *sinkpad;
113
114   GST_LOG ("Linking fakesink");
115
116   sink = gst_element_factory_make ("fakesink", "sink");
117   fail_unless (sink != NULL, "Failed to create fakesink element");
118
119   gst_bin_add (GST_BIN (pipeline), sink);
120
121   sinkpad = gst_element_get_static_pad (sink, "sink");
122   fail_unless_equals_int (gst_pad_link (srcpad, sinkpad), GST_PAD_LINK_OK);
123   gst_object_unref (sinkpad);
124
125   gst_element_set_state (sink, GST_STATE_PLAYING);
126 }
127
128 GST_START_TEST (test_reuse_without_decoders)
129 {
130   GstElement *pipe, *src, *decodebin, *sink;
131
132   pipe = gst_pipeline_new (NULL);
133   fail_unless (pipe != NULL, "failed to create pipeline");
134
135   src = gst_element_factory_make ("audiotestsrc", "src");
136   fail_unless (src != NULL, "Failed to create audiotestsrc element");
137
138   decodebin = gst_element_factory_make ("decodebin2", "decodebin");
139   fail_unless (decodebin != NULL, "Failed to create decodebin element");
140
141   g_signal_connect (decodebin, "new-decoded-pad",
142       G_CALLBACK (new_decoded_pad_plug_fakesink_cb), pipe);
143
144   fail_unless (gst_bin_add (GST_BIN (pipe), src));
145   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
146   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
147
148   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
149       GST_STATE_CHANGE_SUCCESS);
150   /* it's push-based, so should be async */
151   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
152       GST_STATE_CHANGE_ASYNC);
153
154   /* wait for state change to complete */
155   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
156       GST_STATE_CHANGE_SUCCESS);
157
158   /* there shouldn't be any errors */
159   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
160
161   /* reset */
162   gst_element_set_state (pipe, GST_STATE_READY);
163
164   sink = gst_bin_get_by_name (GST_BIN (pipe), "sink");
165   gst_bin_remove (GST_BIN (pipe), sink);
166   gst_element_set_state (sink, GST_STATE_NULL);
167   gst_object_unref (sink);
168
169   GST_LOG ("second try");
170
171   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
172       GST_STATE_CHANGE_SUCCESS);
173   /* it's push-based, so should be async */
174   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
175       GST_STATE_CHANGE_ASYNC);
176
177   /* wait for state change to complete */
178   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
179       GST_STATE_CHANGE_SUCCESS);
180
181   /* there shouldn't be any errors */
182   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
183
184   gst_element_set_state (pipe, GST_STATE_NULL);
185   gst_object_unref (pipe);
186 }
187
188 GST_END_TEST;
189
190 static Suite *
191 decodebin2_suite (void)
192 {
193   Suite *s = suite_create ("decodebin2");
194   TCase *tc_chain = tcase_create ("general");
195
196   suite_add_tcase (s, tc_chain);
197   tcase_add_test (tc_chain, test_text_plain_streams);
198   tcase_add_test (tc_chain, test_reuse_without_decoders);
199
200   return s;
201 }
202
203 GST_CHECK_MAIN (decodebin2);