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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/check/gstcheck.h>
27 #include <gst/check/gstbufferstraw.h>
28 #include <gio/gio.h>
29
30 static gboolean got_eos = FALSE;
31
32 static gboolean
message_handler(GstBus * bus,GstMessage * msg,gpointer data)33 message_handler (GstBus * bus, GstMessage * msg, gpointer data)
34 {
35 GMainLoop *loop = (GMainLoop *) data;
36
37 switch (GST_MESSAGE_TYPE (msg)) {
38 case GST_MESSAGE_EOS:
39 got_eos = TRUE;
40 g_main_loop_quit (loop);
41 break;
42 case GST_MESSAGE_ERROR:{
43 gchar *debug;
44 GError *err;
45
46 gst_message_parse_error (msg, &err, &debug);
47 g_free (debug);
48
49 /* Will abort the check */
50 g_warning ("Error: %s\n", err->message);
51 g_error_free (err);
52
53 g_main_loop_quit (loop);
54 break;
55 }
56 default:
57 break;
58 }
59
60 return TRUE;
61 }
62
GST_START_TEST(test_memory_stream)63 GST_START_TEST (test_memory_stream)
64 {
65 GMainLoop *loop;
66 GstElement *bin;
67 GstElement *src, *sink;
68 GstBus *bus;
69 GMemoryInputStream *input;
70 GMemoryOutputStream *output;
71 guint8 *in_data;
72 guint8 *out_data;
73 gint i;
74 gint64 duration;
75 guint bus_watch = 0;
76
77 got_eos = FALSE;
78
79 in_data = g_new (guint8, 512);
80 out_data = g_new (guint8, 512);
81 for (i = 0; i < 512; i++)
82 in_data[i] = i % 256;
83
84 input =
85 G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (in_data, 512,
86 (GDestroyNotify) g_free));
87
88 output = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (out_data, 512,
89 (GReallocFunc) g_realloc, (GDestroyNotify) g_free));
90 out_data = NULL;
91
92 loop = g_main_loop_new (NULL, FALSE);
93
94 bin = gst_pipeline_new ("bin");
95
96 src = gst_element_factory_make ("giostreamsrc", "src");
97 fail_unless (src != NULL);
98 g_object_set (G_OBJECT (src), "stream", input, NULL);
99
100 sink = gst_element_factory_make ("giostreamsink", "sink");
101 fail_unless (sink != NULL);
102 g_object_set (G_OBJECT (sink), "stream", output, NULL);
103
104 gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
105
106 fail_unless (gst_element_link_many (src, sink, NULL));
107
108 bus = gst_element_get_bus (bin);
109 bus_watch = gst_bus_add_watch (bus, message_handler, loop);
110 gst_object_unref (bus);
111
112 gst_element_set_state (bin, GST_STATE_PAUSED);
113 gst_element_get_state (bin, NULL, NULL, -1);
114
115 fail_unless (gst_element_query_duration (bin, GST_FORMAT_BYTES, &duration));
116 fail_unless_equals_int (duration, 512);
117
118 gst_element_set_state (bin, GST_STATE_PLAYING);
119
120 g_main_loop_run (loop);
121
122 gst_element_set_state (bin, GST_STATE_NULL);
123
124 fail_unless (got_eos);
125 got_eos = FALSE;
126
127 out_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (output));
128
129 for (i = 0; i < 512; i++)
130 fail_unless_equals_int (in_data[i], out_data[i]);
131
132 gst_element_set_state (bin, GST_STATE_PAUSED);
133 gst_element_get_state (bin, NULL, NULL, -1);
134
135 fail_unless (gst_element_query_duration (bin, GST_FORMAT_BYTES, &duration));
136 fail_unless_equals_int (duration, 512);
137
138 gst_element_set_state (bin, GST_STATE_PLAYING);
139
140 g_main_loop_run (loop);
141
142 gst_element_set_state (bin, GST_STATE_NULL);
143 gst_object_unref (bin);
144
145 fail_unless (got_eos);
146
147 g_object_unref (input);
148 g_object_unref (output);
149
150 g_main_loop_unref (loop);
151 g_source_remove (bus_watch);
152 }
153
154 GST_END_TEST;
155
156 static Suite *
gio_suite(void)157 gio_suite (void)
158 {
159 Suite *s = suite_create ("gio");
160 TCase *tc_chain = tcase_create ("general");
161
162 suite_add_tcase (s, tc_chain);
163 tcase_add_test (tc_chain, test_memory_stream);
164
165 return s;
166 }
167
168 GST_CHECK_MAIN (gio);
169