Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / check / pipelines / gio.c
1 /* GStreamer
2  *
3  * unit test for GIO
4  *
5  * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.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 #include <gst/check/gstcheck.h>
24 #include <gst/check/gstbufferstraw.h>
25 #include <gio/gio.h>
26
27 static gboolean got_eos = FALSE;
28
29 static gboolean
30 message_handler (GstBus * bus, GstMessage * msg, gpointer data)
31 {
32   GMainLoop *loop = (GMainLoop *) data;
33
34   switch (GST_MESSAGE_TYPE (msg)) {
35     case GST_MESSAGE_EOS:
36       got_eos = TRUE;
37       g_main_loop_quit (loop);
38       break;
39     case GST_MESSAGE_ERROR:{
40       gchar *debug;
41       GError *err;
42
43       gst_message_parse_error (msg, &err, &debug);
44       g_free (debug);
45
46       /* Will abort the check */
47       g_warning ("Error: %s\n", err->message);
48       g_error_free (err);
49
50       g_main_loop_quit (loop);
51       break;
52     }
53     default:
54       break;
55   }
56
57   return TRUE;
58 }
59
60 GST_START_TEST (test_memory_stream)
61 {
62   GMainLoop *loop;
63   GstElement *bin;
64   GstElement *src, *sink;
65   GstBus *bus;
66   GMemoryInputStream *input;
67   GMemoryOutputStream *output;
68   guint8 *in_data;
69   guint8 *out_data;
70   gint i;
71   GstFormat fmt = GST_FORMAT_BYTES;
72   gint64 duration;
73   guint bus_watch = 0;
74
75   got_eos = FALSE;
76
77   in_data = g_new (guint8, 512);
78   out_data = g_new (guint8, 512);
79   for (i = 0; i < 512; i++)
80     in_data[i] = i % 256;
81
82   input =
83       G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (in_data, 512,
84           (GDestroyNotify) g_free));
85
86   output = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (out_data, 512,
87           (GReallocFunc) g_realloc, (GDestroyNotify) g_free));
88   out_data = NULL;
89
90   loop = g_main_loop_new (NULL, FALSE);
91
92   bin = gst_pipeline_new ("bin");
93
94   src = gst_element_factory_make ("giostreamsrc", "src");
95   fail_unless (src != NULL);
96   g_object_set (G_OBJECT (src), "stream", input, NULL);
97
98   sink = gst_element_factory_make ("giostreamsink", "sink");
99   fail_unless (sink != NULL);
100   g_object_set (G_OBJECT (sink), "stream", output, NULL);
101
102   gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
103
104   fail_unless (gst_element_link_many (src, sink, NULL));
105
106   bus = gst_element_get_bus (bin);
107   bus_watch = gst_bus_add_watch (bus, message_handler, loop);
108   gst_object_unref (bus);
109
110   gst_element_set_state (bin, GST_STATE_PAUSED);
111   gst_element_get_state (bin, NULL, NULL, -1);
112
113   fail_unless (gst_element_query_duration (bin, &fmt, &duration));
114   fail_unless_equals_int (duration, 512);
115
116   gst_element_set_state (bin, GST_STATE_PLAYING);
117
118   g_main_loop_run (loop);
119
120   gst_element_set_state (bin, GST_STATE_NULL);
121
122   fail_unless (got_eos);
123   got_eos = FALSE;
124
125   out_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (output));
126
127   for (i = 0; i < 512; i++)
128     fail_unless_equals_int (in_data[i], out_data[i]);
129
130   gst_element_set_state (bin, GST_STATE_PAUSED);
131   gst_element_get_state (bin, NULL, NULL, -1);
132
133   fail_unless (gst_element_query_duration (bin, &fmt, &duration));
134   fail_unless_equals_int (duration, 512);
135
136   gst_element_set_state (bin, GST_STATE_PLAYING);
137
138   g_main_loop_run (loop);
139
140   gst_element_set_state (bin, GST_STATE_NULL);
141   gst_object_unref (bin);
142
143   fail_unless (got_eos);
144
145   g_object_unref (input);
146   g_object_unref (output);
147
148   g_main_loop_unref (loop);
149   g_source_remove (bus_watch);
150 }
151
152 GST_END_TEST;
153
154 static Suite *
155 gio_testsuite (void)
156 {
157   Suite *s = suite_create ("gio");
158   TCase *tc_chain = tcase_create ("general");
159
160   suite_add_tcase (s, tc_chain);
161   tcase_add_test (tc_chain, test_memory_stream);
162
163   return s;
164 }
165
166 int
167 main (int argc, char **argv)
168 {
169   int nf;
170
171   Suite *s = gio_testsuite ();
172   SRunner *sr = srunner_create (s);
173
174   gst_check_init (&argc, &argv);
175
176   srunner_run_all (sr, CK_NORMAL);
177   nf = srunner_ntests_failed (sr);
178   srunner_free (sr);
179
180   return nf;
181 }