10b9213eaf298a8a37a4cb218901c4ffe06bf43c
[platform/upstream/gstreamer.git] / 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   gint64 duration;
72   guint bus_watch = 0;
73
74   got_eos = FALSE;
75
76   in_data = g_new (guint8, 512);
77   out_data = g_new (guint8, 512);
78   for (i = 0; i < 512; i++)
79     in_data[i] = i % 256;
80
81   input =
82       G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (in_data, 512,
83           (GDestroyNotify) g_free));
84
85   output = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (out_data, 512,
86           (GReallocFunc) g_realloc, (GDestroyNotify) g_free));
87   out_data = NULL;
88
89   loop = g_main_loop_new (NULL, FALSE);
90
91   bin = gst_pipeline_new ("bin");
92
93   src = gst_element_factory_make ("giostreamsrc", "src");
94   fail_unless (src != NULL);
95   g_object_set (G_OBJECT (src), "stream", input, NULL);
96
97   sink = gst_element_factory_make ("giostreamsink", "sink");
98   fail_unless (sink != NULL);
99   g_object_set (G_OBJECT (sink), "stream", output, NULL);
100
101   gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
102
103   fail_unless (gst_element_link_many (src, sink, NULL));
104
105   bus = gst_element_get_bus (bin);
106   bus_watch = gst_bus_add_watch (bus, message_handler, loop);
107   gst_object_unref (bus);
108
109   gst_element_set_state (bin, GST_STATE_PAUSED);
110   gst_element_get_state (bin, NULL, NULL, -1);
111
112   fail_unless (gst_element_query_duration (bin, GST_FORMAT_BYTES, &duration));
113   fail_unless_equals_int (duration, 512);
114
115   gst_element_set_state (bin, GST_STATE_PLAYING);
116
117   g_main_loop_run (loop);
118
119   gst_element_set_state (bin, GST_STATE_NULL);
120
121   fail_unless (got_eos);
122   got_eos = FALSE;
123
124   out_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (output));
125
126   for (i = 0; i < 512; i++)
127     fail_unless_equals_int (in_data[i], out_data[i]);
128
129   gst_element_set_state (bin, GST_STATE_PAUSED);
130   gst_element_get_state (bin, NULL, NULL, -1);
131
132   fail_unless (gst_element_query_duration (bin, GST_FORMAT_BYTES, &duration));
133   fail_unless_equals_int (duration, 512);
134
135   gst_element_set_state (bin, GST_STATE_PLAYING);
136
137   g_main_loop_run (loop);
138
139   gst_element_set_state (bin, GST_STATE_NULL);
140   gst_object_unref (bin);
141
142   fail_unless (got_eos);
143
144   g_object_unref (input);
145   g_object_unref (output);
146
147   g_main_loop_unref (loop);
148   g_source_remove (bus_watch);
149 }
150
151 GST_END_TEST;
152
153 static Suite *
154 gio_testsuite (void)
155 {
156   Suite *s = suite_create ("gio");
157   TCase *tc_chain = tcase_create ("general");
158
159   suite_add_tcase (s, tc_chain);
160   tcase_add_test (tc_chain, test_memory_stream);
161
162   return s;
163 }
164
165 int
166 main (int argc, char **argv)
167 {
168   int nf;
169
170   Suite *s = gio_testsuite ();
171   SRunner *sr = srunner_create (s);
172
173   gst_check_init (&argc, &argv);
174
175   srunner_run_all (sr, CK_NORMAL);
176   nf = srunner_ntests_failed (sr);
177   srunner_free (sr);
178
179   return nf;
180 }