1 /* GStreamer
2 *
3 * unit test for capssetter
4 *
5 * Copyright (C) <2009> Mark Nauwelaerts <mnauw@users.sourceforge.net>
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
23 #include <gst/check/gstcheck.h>
24
25
26 /* For ease of programming we use globals to keep refs for our floating
27 * src and sink pads we create; otherwise we always have to do get_pad,
28 * get_peer, and then remove references in every test function */
29 static GstPad *mysrcpad, *mysinkpad;
30
31
32 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
33 GST_PAD_SINK,
34 GST_PAD_ALWAYS,
35 GST_STATIC_CAPS_ANY);
36 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
37 GST_PAD_SRC,
38 GST_PAD_ALWAYS,
39 GST_STATIC_CAPS_ANY);
40
41 static GstElement *
setup_capssetter(void)42 setup_capssetter (void)
43 {
44 GstElement *capssetter;
45
46 GST_DEBUG ("setup_capssetter");
47
48 capssetter = gst_check_setup_element ("capssetter");
49 mysrcpad = gst_check_setup_src_pad (capssetter, &srctemplate);
50 mysinkpad = gst_check_setup_sink_pad (capssetter, &sinktemplate);
51 gst_pad_set_active (mysrcpad, TRUE);
52 gst_pad_set_active (mysinkpad, TRUE);
53
54 return capssetter;
55 }
56
57 static void
cleanup_capssetter(GstElement * capssetter)58 cleanup_capssetter (GstElement * capssetter)
59 {
60 GST_DEBUG ("cleanup_capssetter");
61
62 gst_check_drop_buffers ();
63 gst_pad_set_active (mysrcpad, FALSE);
64 gst_pad_set_active (mysinkpad, FALSE);
65 gst_check_teardown_src_pad (capssetter);
66 gst_check_teardown_sink_pad (capssetter);
67 gst_check_teardown_element (capssetter);
68 }
69
70 static void
push_and_test(GstCaps * prop_caps,gboolean join,gboolean replace,GstCaps * in_caps,GstCaps * out_caps)71 push_and_test (GstCaps * prop_caps, gboolean join, gboolean replace,
72 GstCaps * in_caps, GstCaps * out_caps)
73 {
74 GstElement *capssetter;
75 GstBuffer *buffer;
76 GstCaps *current_out;
77
78 capssetter = setup_capssetter ();
79 fail_unless (gst_element_set_state (capssetter,
80 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
81 "could not set to playing");
82
83 g_object_set (capssetter, "join", join, NULL);
84 g_object_set (capssetter, "replace", replace, NULL);
85 g_object_set (capssetter, "caps", prop_caps, NULL);
86 gst_caps_unref (prop_caps);
87
88 buffer = gst_buffer_new_and_alloc (4);
89 ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
90 gst_buffer_fill (buffer, 0, "data", 4);
91
92 gst_check_setup_events (mysrcpad, capssetter, in_caps, GST_FORMAT_TIME);
93 gst_caps_unref (in_caps);
94
95 /* pushing gives away my reference ... */
96 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK,
97 "Failed pushing buffer to capssetter");
98
99 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
100
101 /* ... but it should end up being collected on the global buffer list */
102 fail_unless (g_list_length (buffers) == 1);
103 buffer = g_list_first (buffers)->data;
104 ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
105
106 current_out = gst_pad_get_current_caps (mysinkpad);
107 fail_unless (gst_caps_is_equal (out_caps, current_out));
108 gst_caps_unref (current_out);
109 gst_caps_unref (out_caps);
110
111 /* cleanup */
112 cleanup_capssetter (capssetter);
113 }
114
115 #define SRC_WIDTH 8
116 #define SRC_HEIGHT 12
117
118 static GstCaps *
make_src_caps(void)119 make_src_caps (void)
120 {
121 return gst_caps_new_simple ("video/x-foo", "width", G_TYPE_INT, SRC_WIDTH,
122 "height", G_TYPE_INT, SRC_HEIGHT, NULL);
123 }
124
125 /* don't try these caps mutations at home, folks */
126
GST_START_TEST(test_n_join_n_replace)127 GST_START_TEST (test_n_join_n_replace)
128 {
129 GstCaps *in_caps, *prop_caps, *out_caps;
130
131 in_caps = make_src_caps ();
132 prop_caps = gst_caps_new_simple ("video/x-bar",
133 "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
134 out_caps = gst_caps_new_simple ("video/x-bar",
135 "width", G_TYPE_INT, 2 * SRC_WIDTH,
136 "height", G_TYPE_INT, SRC_HEIGHT, NULL);
137 push_and_test (prop_caps, FALSE, FALSE, in_caps, out_caps);
138 }
139
140 GST_END_TEST;
141
GST_START_TEST(test_n_join_replace)142 GST_START_TEST (test_n_join_replace)
143 {
144 GstCaps *in_caps, *prop_caps, *out_caps;
145
146 in_caps = make_src_caps ();
147 prop_caps = gst_caps_new_simple ("video/x-bar",
148 "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
149 out_caps = gst_caps_copy (prop_caps);
150 push_and_test (prop_caps, FALSE, TRUE, in_caps, out_caps);
151 }
152
153 GST_END_TEST;
154
GST_START_TEST(test_join_n_replace_n_match)155 GST_START_TEST (test_join_n_replace_n_match)
156 {
157 GstCaps *in_caps, *prop_caps, *out_caps;
158
159 /* non joining caps */
160 in_caps = make_src_caps ();
161 prop_caps = gst_caps_new_simple ("video/x-bar",
162 "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
163 out_caps = gst_caps_copy (in_caps);
164 push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
165 }
166
167 GST_END_TEST;
168
GST_START_TEST(test_join_n_replace_match)169 GST_START_TEST (test_join_n_replace_match)
170 {
171 GstCaps *in_caps, *prop_caps, *out_caps;
172
173 /* joining caps */
174 in_caps = make_src_caps ();
175 prop_caps = gst_caps_new_simple ("video/x-foo",
176 "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
177 out_caps = gst_caps_new_simple ("video/x-foo",
178 "width", G_TYPE_INT, 2 * SRC_WIDTH,
179 "height", G_TYPE_INT, SRC_HEIGHT, NULL);
180 push_and_test (prop_caps, TRUE, FALSE, in_caps, out_caps);
181 }
182
183 GST_END_TEST;
184
GST_START_TEST(test_join_replace_n_match)185 GST_START_TEST (test_join_replace_n_match)
186 {
187 GstCaps *in_caps, *prop_caps, *out_caps;
188
189 /* non joining caps */
190 in_caps = make_src_caps ();
191 prop_caps = gst_caps_new_simple ("video/x-bar",
192 "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
193 out_caps = gst_caps_copy (in_caps);
194 push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
195 }
196
197 GST_END_TEST;
198
GST_START_TEST(test_join_replace_match)199 GST_START_TEST (test_join_replace_match)
200 {
201 GstCaps *in_caps, *prop_caps, *out_caps;
202
203 /* joining caps */
204 in_caps = make_src_caps ();
205 prop_caps = gst_caps_new_simple ("video/x-foo",
206 "width", G_TYPE_INT, 2 * SRC_WIDTH, NULL);
207 out_caps = gst_caps_copy (prop_caps);
208 push_and_test (prop_caps, TRUE, TRUE, in_caps, out_caps);
209 }
210
211 GST_END_TEST;
212
213 static Suite *
capssetter_suite(void)214 capssetter_suite (void)
215 {
216 Suite *s = suite_create ("capssetter");
217 TCase *tc_chain = tcase_create ("general");
218
219 suite_add_tcase (s, tc_chain);
220 tcase_add_test (tc_chain, test_n_join_n_replace);
221 tcase_add_test (tc_chain, test_n_join_replace);
222 tcase_add_test (tc_chain, test_join_n_replace_n_match);
223 tcase_add_test (tc_chain, test_join_n_replace_match);
224 tcase_add_test (tc_chain, test_join_replace_n_match);
225 tcase_add_test (tc_chain, test_join_replace_match);
226
227 return s;
228 }
229
230 GST_CHECK_MAIN (capssetter);
231