2 * Tests and examples of GstHarness
4 * Copyright (C) 2015 Havard Graff <havard@pexip.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include <gst/check/gstcheck.h>
26 #include <gst/check/gstharness.h>
28 GST_START_TEST (test_harness_empty)
30 GstHarness *h = gst_harness_new_empty ();
31 gst_harness_teardown (h);
37 create_destroy_element_harness (gpointer data, gpointer user_data)
39 GstElement *element = user_data;
40 GstHarness *h = gst_harness_new_with_element (element, NULL, NULL);
41 gst_harness_teardown (h);
44 GST_START_TEST (test_harness_element_ref)
46 GstHarness *h = gst_harness_new ("identity");
47 GstHarnessThread *threads[100];
50 for (i = 0; i < G_N_ELEMENTS (threads); i++)
51 threads[i] = gst_harness_stress_custom_start (h, NULL,
52 create_destroy_element_harness, h->element, 0);
53 for (i = 0; i < G_N_ELEMENTS (threads); i++)
54 gst_harness_stress_thread_stop (threads[i]);
56 fail_unless_equals_int (G_OBJECT (h->element)->ref_count, 1);
58 gst_harness_teardown (h);
64 GST_START_TEST (test_src_harness)
66 GstHarness *h = gst_harness_new ("identity");
68 /* add a fakesrc that syncs to the clock and a
69 capsfilter that adds some caps to it */
70 gst_harness_add_src_parse (h,
71 "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
73 /* this cranks the clock and transfers the resulting buffer
74 from the src-harness into the identity element */
75 gst_harness_push_from_src (h);
77 /* verify that identity outputs a buffer by pulling and unreffing */
78 gst_buffer_unref (gst_harness_pull (h));
80 gst_harness_teardown (h);
85 GST_START_TEST (test_src_harness_no_forwarding)
87 GstHarness *h = gst_harness_new ("identity");
89 /* turn of forwarding of necessary events */
90 gst_harness_set_forwarding (h, FALSE);
92 /* add a fakesrc that syncs to the clock and a
93 capsfilter that adds some caps to it */
94 gst_harness_add_src_parse (h,
95 "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
97 /* start the fakesrc to produce the first events */
98 gst_harness_play (h->src_harness);
100 /* transfer STREAM_START event */
101 gst_harness_src_push_event (h);
103 /* crank the clock to produce the CAPS and SEGMENT events */
104 gst_harness_crank_single_clock_wait (h->src_harness);
106 /* transfer CAPS event */
107 gst_harness_src_push_event (h);
109 /* transfer SEGMENT event */
110 gst_harness_src_push_event (h);
112 /* now transfer the buffer produced by exploiting
113 the ability to say 0 cranks but 1 push */
114 gst_harness_src_crank_and_push_many (h, 0, 1);
116 /* and verify that the identity element outputs it */
117 gst_buffer_unref (gst_harness_pull (h));
119 gst_harness_teardown (h);
124 GST_START_TEST (test_add_sink_harness_without_sinkpad)
126 GstHarness *h = gst_harness_new ("fakesink");
128 gst_harness_add_sink (h, "fakesink");
130 gst_harness_teardown (h);
136 create_new_stream_start_event (GstHarness * h, gpointer data)
138 guint *counter = data;
139 gchar *stream_id = g_strdup_printf ("streamid/%d", *counter);
140 GstEvent *event = gst_event_new_stream_start (stream_id);
147 push_query (gpointer data, gpointer user_data)
149 GstHarness *h = user_data;
150 GstCaps *caps = gst_caps_new_empty_simple ("mycaps");
151 GstQuery *query = gst_query_new_allocation (caps, FALSE);
152 gst_caps_unref (caps);
153 gst_pad_peer_query (h->srcpad, query);
154 gst_query_unref (query);
157 GST_START_TEST (test_forward_event_and_query_to_sink_harness_while_teardown)
159 GstHarness *h = gst_harness_new ("identity");
161 GstHarnessThread *e_thread = gst_harness_stress_push_event_with_cb_start (h,
162 create_new_stream_start_event, &counter, NULL);
163 GstHarnessThread *q_thread = gst_harness_stress_custom_start (h, NULL,
165 gdouble duration = 1.0;
166 GTimer *timer = g_timer_new ();
168 while (g_timer_elapsed (timer, NULL) < duration) {
169 gst_harness_add_sink (h, "fakesink");
173 g_timer_destroy (timer);
174 gst_harness_stress_thread_stop (q_thread);
175 gst_harness_stress_thread_stop (e_thread);
176 gst_harness_teardown (h);
182 push_sticky_events (gpointer data, G_GNUC_UNUSED gpointer user_data)
188 h = (GstHarness *) user_data;
190 gst_harness_push_event (h, gst_event_new_stream_start ("999"));
192 caps = gst_caps_new_empty_simple ("mycaps");
193 gst_harness_push_event (h, gst_event_new_caps (caps));
194 gst_caps_unref (caps);
196 gst_segment_init (&segment, GST_FORMAT_TIME);
197 gst_harness_push_event (h, gst_event_new_segment (&segment));
200 GST_START_TEST (test_forward_sticky_events_to_sink_harness_while_teardown)
202 GstHarness *h = gst_harness_new ("identity");
203 GstHarnessThread *e_thread = gst_harness_stress_custom_start (h, NULL,
204 push_sticky_events, h, 0);
205 gdouble duration = 1.0;
206 GTimer *timer = g_timer_new ();
208 while (g_timer_elapsed (timer, NULL) < duration) {
209 gst_harness_add_sink (h, "fakesink");
213 g_timer_destroy (timer);
214 gst_harness_stress_thread_stop (e_thread);
215 gst_harness_teardown (h);
221 harness_new_and_fill_with_data (void)
223 GstHarness *h = gst_harness_new_parse ("fakesrc num-buffers=5 "
224 "filltype=pattern-span sizetype=fixed sizemin=10 sizemax=10");
225 gboolean have_eos = FALSE;
227 gst_harness_play (h);
230 GstEvent *e = gst_harness_pull_event (h);
231 have_eos = GST_EVENT_TYPE (e) == GST_EVENT_EOS;
238 GST_START_TEST (test_get_all_data)
249 for (i = 0; i < G_N_ELEMENTS (expected); ++i)
252 h = harness_new_and_fill_with_data ();
253 buf = gst_harness_take_all_data_as_buffer (h);
254 fail_unless (buf != NULL);
255 fail_unless_equals_int (gst_buffer_get_size (buf), 5 * 10);
256 fail_unless (gst_buffer_memcmp (buf, 0, expected, 5 * 10) == 0);
257 gst_buffer_unref (buf);
258 /* There should be nothing left now. We should still get a non-NULL buffer */
259 buf = gst_harness_take_all_data_as_buffer (h);
260 fail_unless (buf != NULL);
261 fail_unless_equals_int (gst_buffer_get_size (buf), 0);
262 gst_buffer_unref (buf);
263 gst_harness_teardown (h);
265 h = harness_new_and_fill_with_data ();
266 bytes = gst_harness_take_all_data_as_bytes (h);
267 fail_unless (bytes != NULL);
268 cdata = g_bytes_get_data (bytes, &size);
269 fail_unless_equals_int (size, 5 * 10);
270 fail_unless (memcmp (cdata, expected, 50) == 0);
271 g_bytes_unref (bytes);
272 /* There should be nothing left now. We should still get a non-NULL bytes */
273 bytes = gst_harness_take_all_data_as_bytes (h);
274 fail_unless (bytes != NULL);
275 cdata = g_bytes_get_data (bytes, &size);
276 fail_unless (cdata == NULL);
277 fail_unless_equals_int (size, 0);
278 g_bytes_unref (bytes);
279 gst_harness_teardown (h);
281 h = harness_new_and_fill_with_data ();
282 data = gst_harness_take_all_data (h, &size);
283 fail_unless (data != NULL);
284 fail_unless_equals_int (size, 5 * 10);
285 fail_unless (memcmp (data, expected, 50) == 0);
287 /* There should be nothing left now. */
288 data = gst_harness_take_all_data (h, &size);
289 fail_unless (data == NULL);
290 fail_unless_equals_int (size, 0);
291 gst_harness_teardown (h);
297 gst_harness_suite (void)
299 Suite *s = suite_create ("GstHarness");
300 TCase *tc_chain = tcase_create ("harness");
302 suite_add_tcase (s, tc_chain);
304 tcase_add_test (tc_chain, test_harness_empty);
305 tcase_add_test (tc_chain, test_harness_element_ref);
306 tcase_add_test (tc_chain, test_src_harness);
307 tcase_add_test (tc_chain, test_src_harness_no_forwarding);
308 tcase_add_test (tc_chain, test_add_sink_harness_without_sinkpad);
310 tcase_add_test (tc_chain,
311 test_forward_event_and_query_to_sink_harness_while_teardown);
312 tcase_add_test (tc_chain,
313 test_forward_sticky_events_to_sink_harness_while_teardown);
315 tcase_add_test (tc_chain, test_get_all_data);
320 GST_CHECK_MAIN (gst_harness);